blob: 7e1e8ce10e4590b42c7c004dad33a3dde25cff5d [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
Anders Carlsson0c6139d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor1b7f8982008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
John McCalle402e722012-09-25 07:32:39 +000021#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000022#include "llvm/ADT/SmallString.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000023#include "RAIIObjectsForParser.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000024using namespace clang;
25
26/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000027/// may either be a top level namespace or a block-level namespace alias. If
28/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000029///
30/// namespace-definition: [C++ 7.3: basic.namespace]
31/// named-namespace-definition
32/// unnamed-namespace-definition
33///
34/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000035/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000036///
37/// named-namespace-definition:
38/// original-namespace-definition
39/// extension-namespace-definition
40///
41/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000042/// 'inline'[opt] 'namespace' identifier attributes[opt]
43/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000044///
45/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000046/// 'inline'[opt] 'namespace' original-namespace-name
47/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000048///
Chris Lattner8f08cb72007-08-25 06:57:03 +000049/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
50/// 'namespace' identifier '=' qualified-namespace-specifier ';'
51///
John McCalld226f652010-08-21 09:40:31 +000052Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000053 SourceLocation &DeclEnd,
54 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000055 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000056 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +000057 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000058
Douglas Gregor49f40bd2009-09-18 19:03:04 +000059 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000060 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000061 cutOffParsing();
62 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +000063 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000064
Chris Lattner8f08cb72007-08-25 06:57:03 +000065 SourceLocation IdentLoc;
66 IdentifierInfo *Ident = 0;
Richard Trieuf858bd82011-05-26 20:11:09 +000067 std::vector<SourceLocation> ExtraIdentLoc;
68 std::vector<IdentifierInfo*> ExtraIdent;
69 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000070
71 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000072
Chris Lattner04d66662007-10-09 17:33:22 +000073 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000074 Ident = Tok.getIdentifierInfo();
75 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieuf858bd82011-05-26 20:11:09 +000076 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
77 ExtraNamespaceLoc.push_back(ConsumeToken());
78 ExtraIdent.push_back(Tok.getIdentifierInfo());
79 ExtraIdentLoc.push_back(ConsumeToken());
80 }
Chris Lattner8f08cb72007-08-25 06:57:03 +000081 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Chris Lattner8f08cb72007-08-25 06:57:03 +000083 // Read label attributes, if present.
John McCall0b7e6782011-03-24 11:26:52 +000084 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000085 if (Tok.is(tok::kw___attribute)) {
86 attrTok = Tok;
John McCall7f040a92010-12-24 02:08:15 +000087 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000088 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Douglas Gregor6a588dd2009-06-17 19:49:00 +000090 if (Tok.is(tok::equal)) {
John McCall7f040a92010-12-24 02:08:15 +000091 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +000092 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +000093 if (InlineLoc.isValid())
94 Diag(InlineLoc, diag::err_inline_namespace_alias)
95 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +000096 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000097 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Richard Trieuf858bd82011-05-26 20:11:09 +000099
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000100 BalancedDelimiterTracker T(*this, tok::l_brace);
101 if (T.consumeOpen()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000102 if (!ExtraIdent.empty()) {
103 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
104 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +0000107 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +0000108 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000109 }
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Douglas Gregor23c94db2010-07-02 17:43:08 +0000111 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
112 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
113 getCurScope()->getFnParent()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000114 if (!ExtraIdent.empty()) {
115 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
116 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
117 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000118 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Douglas Gregor95f1b152010-05-14 05:08:22 +0000119 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000120 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000121 }
122
Richard Trieuf858bd82011-05-26 20:11:09 +0000123 if (!ExtraIdent.empty()) {
124 TentativeParsingAction TPA(*this);
125 SkipUntil(tok::r_brace, /*StopAtSemi*/false, /*DontConsume*/true);
126 Token rBraceToken = Tok;
127 TPA.Revert();
128
129 if (!rBraceToken.is(tok::r_brace)) {
130 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
131 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
132 } else {
Benjamin Kramer9910df02011-05-26 21:32:30 +0000133 std::string NamespaceFix;
Richard Trieuf858bd82011-05-26 20:11:09 +0000134 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
135 E = ExtraIdent.end(); I != E; ++I) {
136 NamespaceFix += " { namespace ";
137 NamespaceFix += (*I)->getName();
138 }
Benjamin Kramer9910df02011-05-26 21:32:30 +0000139
Richard Trieuf858bd82011-05-26 20:11:09 +0000140 std::string RBraces;
Benjamin Kramer9910df02011-05-26 21:32:30 +0000141 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieuf858bd82011-05-26 20:11:09 +0000142 RBraces += "} ";
Benjamin Kramer9910df02011-05-26 21:32:30 +0000143
Richard Trieuf858bd82011-05-26 20:11:09 +0000144 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
145 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
146 ExtraIdentLoc.back()),
147 NamespaceFix)
148 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
149 }
150 }
151
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000152 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith7fe62082011-10-15 05:09:34 +0000153 if (InlineLoc.isValid())
David Blaikie4e4d0842012-03-11 07:00:24 +0000154 Diag(InlineLoc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +0000155 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000156
Chris Lattner51448322009-03-29 14:02:43 +0000157 // Enter a scope for the namespace.
158 ParseScope NamespaceScope(this, Scope::DeclScope);
159
John McCalld226f652010-08-21 09:40:31 +0000160 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000161 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000162 IdentLoc, Ident, T.getOpenLocation(),
163 attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000164
John McCallf312b1e2010-08-26 23:41:50 +0000165 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
166 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Richard Trieuf858bd82011-05-26 20:11:09 +0000168 // Parse the contents of the namespace. This includes parsing recovery on
169 // any improperly nested namespaces.
170 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000171 InlineLoc, attrs, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Chris Lattner51448322009-03-29 14:02:43 +0000173 // Leave the namespace scope.
174 NamespaceScope.Exit();
175
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000176 DeclEnd = T.getCloseLocation();
177 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner51448322009-03-29 14:02:43 +0000178
179 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000180}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000181
Richard Trieuf858bd82011-05-26 20:11:09 +0000182/// ParseInnerNamespace - Parse the contents of a namespace.
183void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
184 std::vector<IdentifierInfo*>& Ident,
185 std::vector<SourceLocation>& NamespaceLoc,
186 unsigned int index, SourceLocation& InlineLoc,
Richard Trieuf858bd82011-05-26 20:11:09 +0000187 ParsedAttributes& attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000188 BalancedDelimiterTracker &Tracker) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000189 if (index == Ident.size()) {
190 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
191 ParsedAttributesWithRange attrs(AttrFactory);
192 MaybeParseCXX0XAttributes(attrs);
193 MaybeParseMicrosoftAttributes(attrs);
194 ParseExternalDeclaration(attrs);
195 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000196
197 // The caller is what called check -- we are simply calling
198 // the close for it.
199 Tracker.consumeClose();
Richard Trieuf858bd82011-05-26 20:11:09 +0000200
201 return;
202 }
203
204 // Parse improperly nested namespaces.
205 ParseScope NamespaceScope(this, Scope::DeclScope);
206 Decl *NamespcDecl =
207 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
208 NamespaceLoc[index], IdentLoc[index],
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000209 Ident[index], Tracker.getOpenLocation(),
210 attrs.getList());
Richard Trieuf858bd82011-05-26 20:11:09 +0000211
212 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000213 attrs, Tracker);
Richard Trieuf858bd82011-05-26 20:11:09 +0000214
215 NamespaceScope.Exit();
216
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000217 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieuf858bd82011-05-26 20:11:09 +0000218}
219
Anders Carlssonf67606a2009-03-28 04:07:16 +0000220/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
221/// alias definition.
222///
John McCalld226f652010-08-21 09:40:31 +0000223Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000224 SourceLocation AliasLoc,
225 IdentifierInfo *Alias,
226 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000227 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Anders Carlssonf67606a2009-03-28 04:07:16 +0000229 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000231 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000232 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000233 cutOffParsing();
234 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000235 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000236
Anders Carlssonf67606a2009-03-28 04:07:16 +0000237 CXXScopeSpec SS;
238 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000239 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000240
241 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
242 Diag(Tok, diag::err_expected_namespace_name);
243 // Skip to end of the definition and eat the ';'.
244 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000245 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000246 }
247
248 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000249 IdentifierInfo *Ident = Tok.getIdentifierInfo();
250 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Anders Carlssonf67606a2009-03-28 04:07:16 +0000252 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000253 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000254 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
255 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Douglas Gregor23c94db2010-07-02 17:43:08 +0000257 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000258 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000259}
260
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000261/// ParseLinkage - We know that the current token is a string_literal
262/// and just before that, that extern was seen.
263///
264/// linkage-specification: [C++ 7.5p2: dcl.link]
265/// 'extern' string-literal '{' declaration-seq[opt] '}'
266/// 'extern' string-literal declaration
267///
Chris Lattner7d642712010-11-09 20:15:55 +0000268Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000269 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000270 SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000271 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000272 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000273 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000274 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000275
Richard Smith99831e42012-03-06 03:21:47 +0000276 // FIXME: This is incorrect: linkage-specifiers are parsed in translation
277 // phase 7, so string-literal concatenation is supposed to occur.
278 // extern "" "C" "" "+" "+" { } is legal.
279 if (Tok.hasUDSuffix())
280 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000281 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000282
Douglas Gregor074149e2009-01-05 19:45:36 +0000283 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000284 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000285 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000286 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000287 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000288 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000289 : SourceLocation());
290
John McCall0b7e6782011-03-24 11:26:52 +0000291 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000292 MaybeParseCXX0XAttributes(attrs);
293 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000294
Douglas Gregor074149e2009-01-05 19:45:36 +0000295 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-05-01 16:25:54 +0000296 // Reset the source range in DS, as the leading "extern"
297 // does not really belong to the inner declaration ...
298 DS.SetRangeStart(SourceLocation());
299 DS.SetRangeEnd(SourceLocation());
300 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000301 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000302 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000303 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000304 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000305 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000306
Douglas Gregor63a01132010-02-07 08:38:28 +0000307 DS.abort();
308
John McCall7f040a92010-12-24 02:08:15 +0000309 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000310
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000311 BalancedDelimiterTracker T(*this, tok::l_brace);
312 T.consumeOpen();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000313 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000314 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000315 MaybeParseCXX0XAttributes(attrs);
316 MaybeParseMicrosoftAttributes(attrs);
317 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000318 }
319
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000320 T.consumeClose();
Chris Lattner7d642712010-11-09 20:15:55 +0000321 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000322 T.getCloseLocation());
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000323}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000324
Douglas Gregorf780abc2008-12-30 03:27:21 +0000325/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
326/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000327Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000328 const ParsedTemplateInfo &TemplateInfo,
329 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000330 ParsedAttributesWithRange &attrs,
331 Decl **OwnedType) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000332 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000333 ObjCDeclContextSwitch ObjCDC(*this);
334
Douglas Gregorf780abc2008-12-30 03:27:21 +0000335 // Eat 'using'.
336 SourceLocation UsingLoc = ConsumeToken();
337
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000338 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000339 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000340 cutOffParsing();
341 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000342 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000343
John McCall78b81052010-11-10 02:40:36 +0000344 // 'using namespace' means this is a using-directive.
345 if (Tok.is(tok::kw_namespace)) {
346 // Template parameters are always an error here.
347 if (TemplateInfo.Kind) {
348 SourceRange R = TemplateInfo.getSourceRange();
349 Diag(UsingLoc, diag::err_templated_using_directive)
350 << R << FixItHint::CreateRemoval(R);
351 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000352
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000353 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000354 }
355
Richard Smith162e1c12011-04-15 14:24:37 +0000356 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000357
358 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000359 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000360
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000361 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000362 AS_none, OwnedType);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000363}
364
365/// ParseUsingDirective - Parse C++ using-directive, assumes
366/// that current token is 'namespace' and 'using' was already parsed.
367///
368/// using-directive: [C++ 7.3.p4: namespace.udir]
369/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
370/// namespace-name ;
371/// [GNU] using-directive:
372/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
373/// namespace-name attributes[opt] ;
374///
John McCalld226f652010-08-21 09:40:31 +0000375Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000376 SourceLocation UsingLoc,
377 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000378 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000379 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
380
381 // Eat 'namespace'.
382 SourceLocation NamespcLoc = ConsumeToken();
383
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000384 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000385 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000386 cutOffParsing();
387 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000388 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000389
Douglas Gregorf780abc2008-12-30 03:27:21 +0000390 CXXScopeSpec SS;
391 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000392 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000393
Douglas Gregorf780abc2008-12-30 03:27:21 +0000394 IdentifierInfo *NamespcName = 0;
395 SourceLocation IdentLoc = SourceLocation();
396
397 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000398 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000399 Diag(Tok, diag::err_expected_namespace_name);
400 // If there was invalid namespace name, skip to end of decl, and eat ';'.
401 SkipUntil(tok::semi);
402 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000403 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000404 }
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Chris Lattner823c44e2009-01-06 07:27:21 +0000406 // Parse identifier.
407 NamespcName = Tok.getIdentifierInfo();
408 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattner823c44e2009-01-06 07:27:21 +0000410 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000411 bool GNUAttr = false;
412 if (Tok.is(tok::kw___attribute)) {
413 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000414 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000415 }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner823c44e2009-01-06 07:27:21 +0000417 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000418 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000419 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000420 GNUAttr ? diag::err_expected_semi_after_attribute_list
421 : diag::err_expected_semi_after_namespace_name,
422 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000423
Douglas Gregor23c94db2010-07-02 17:43:08 +0000424 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000425 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000426}
427
Richard Smith162e1c12011-04-15 14:24:37 +0000428/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
429/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000430///
431/// using-declaration: [C++ 7.3.p3: namespace.udecl]
432/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000433/// unqualified-id
434/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000435///
Richard Smith162e1c12011-04-15 14:24:37 +0000436/// alias-declaration: C++0x [decl.typedef]p2
437/// 'using' identifier = type-id ;
438///
John McCalld226f652010-08-21 09:40:31 +0000439Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000440 const ParsedTemplateInfo &TemplateInfo,
441 SourceLocation UsingLoc,
442 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000443 AccessSpecifier AS,
444 Decl **OwnedType) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000445 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000446 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000447 bool IsTypeName;
Sean Hunt2edf0a22012-06-23 05:07:58 +0000448 ParsedAttributesWithRange attrs(AttrFactory);
449
450 // FIXME: Simply skip the attributes and diagnose, don't bother parsing them.
451 MaybeParseCXX0XAttributes(attrs);
452 ProhibitAttributes(attrs);
453 attrs.clear();
454 attrs.Range = SourceRange();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000455
456 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000457 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000458 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000459 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000460 ConsumeToken();
461 IsTypeName = true;
462 }
463 else
464 IsTypeName = false;
465
466 // Parse nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000467 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000468
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000469 // Check nested-name specifier.
470 if (SS.isInvalid()) {
471 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000472 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000473 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000474
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000475 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000476 // destructor names and allow the action module to diagnose any semantic
477 // errors.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000478 SourceLocation TemplateKWLoc;
Douglas Gregor12c118a2009-11-04 16:30:06 +0000479 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000480 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000481 /*EnteringContext=*/false,
482 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000483 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000484 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000485 TemplateKWLoc,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000486 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000487 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000488 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000489 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000490
Sean Hunt2edf0a22012-06-23 05:07:58 +0000491 MaybeParseCXX0XAttributes(attrs);
Richard Smith162e1c12011-04-15 14:24:37 +0000492
493 // Maybe this is an alias-declaration.
494 bool IsAliasDecl = Tok.is(tok::equal);
495 TypeResult TypeAlias;
496 if (IsAliasDecl) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000497 // TODO: Attribute support. C++0x attributes may appear before the equals.
498 // Where can GNU attributes appear?
Richard Smith162e1c12011-04-15 14:24:37 +0000499 ConsumeToken();
500
David Blaikie4e4d0842012-03-11 07:00:24 +0000501 Diag(Tok.getLocation(), getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +0000502 diag::warn_cxx98_compat_alias_declaration :
503 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000504
Richard Smith3e4c6c42011-05-05 21:57:07 +0000505 // Type alias templates cannot be specialized.
506 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000507 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
508 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000509 SpecKind = 0;
510 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
511 SpecKind = 1;
512 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
513 SpecKind = 2;
514 if (SpecKind != -1) {
515 SourceRange Range;
516 if (SpecKind == 0)
517 Range = SourceRange(Name.TemplateId->LAngleLoc,
518 Name.TemplateId->RAngleLoc);
519 else
520 Range = TemplateInfo.getSourceRange();
521 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
522 << SpecKind << Range;
523 SkipUntil(tok::semi);
524 return 0;
525 }
526
Richard Smith162e1c12011-04-15 14:24:37 +0000527 // Name must be an identifier.
528 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
529 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
530 // No removal fixit: can't recover from this.
531 SkipUntil(tok::semi);
532 return 0;
533 } else if (IsTypeName)
534 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
535 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
536 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
537 else if (SS.isNotEmpty())
538 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
539 << FixItHint::CreateRemoval(SS.getRange());
540
Richard Smith3e4c6c42011-05-05 21:57:07 +0000541 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
542 Declarator::AliasTemplateContext :
John McCallcdda47f2011-10-01 09:56:14 +0000543 Declarator::AliasDeclContext, AS, OwnedType);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000544 } else {
545 // C++11 attributes are not allowed on a using-declaration, but GNU ones
546 // are.
547 ProhibitAttributes(attrs);
548
Richard Smith162e1c12011-04-15 14:24:37 +0000549 // Parse (optional) attributes (most likely GNU strong-using extension).
550 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000551 }
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000553 // Eat ';'.
554 DeclEnd = Tok.getLocation();
555 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith162e1c12011-04-15 14:24:37 +0000556 !attrs.empty() ? "attributes list" :
557 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000558 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000559
John McCall78b81052010-11-10 02:40:36 +0000560 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith3e4c6c42011-05-05 21:57:07 +0000561 // In C++0x, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000562 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000563 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000564 SourceRange R = TemplateInfo.getSourceRange();
565 Diag(UsingLoc, diag::err_templated_using_declaration)
566 << R << FixItHint::CreateRemoval(R);
567
568 // Unfortunately, we have to bail out instead of recovering by
569 // ignoring the parameters, just in case the nested name specifier
570 // depends on the parameters.
571 return 0;
572 }
573
Douglas Gregor480b53c2011-09-26 14:30:28 +0000574 // "typename" keyword is allowed for identifiers only,
575 // because it may be a type definition.
576 if (IsTypeName && Name.getKind() != UnqualifiedId::IK_Identifier) {
577 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
578 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
579 // Proceed parsing, but reset the IsTypeName flag.
580 IsTypeName = false;
581 }
582
Richard Smith3e4c6c42011-05-05 21:57:07 +0000583 if (IsAliasDecl) {
584 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramer5354e772012-08-23 23:38:35 +0000585 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3e4c6c42011-05-05 21:57:07 +0000586 TemplateParams ? TemplateParams->data() : 0,
587 TemplateParams ? TemplateParams->size() : 0);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000588 // FIXME: Propagate attributes.
Richard Smith3e4c6c42011-05-05 21:57:07 +0000589 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
590 UsingLoc, Name, TypeAlias);
591 }
Richard Smith162e1c12011-04-15 14:24:37 +0000592
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000593 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000594 Name, attrs.getList(),
595 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000596}
597
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000598/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000599///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000600/// [C++0x] static_assert-declaration:
601/// static_assert ( constant-expression , string-literal ) ;
602///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000603/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000604/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000605///
John McCalld226f652010-08-21 09:40:31 +0000606Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000607 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
608 "Not a static_assert declaration");
609
David Blaikie4e4d0842012-03-11 07:00:24 +0000610 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000611 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000612 if (Tok.is(tok::kw_static_assert))
613 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000614
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000615 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000617 BalancedDelimiterTracker T(*this, tok::l_paren);
618 if (T.consumeOpen()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000619 Diag(Tok, diag::err_expected_lparen);
Richard Smith3686c712012-09-13 19:12:50 +0000620 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000621 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000622 }
Mike Stump1eb44332009-09-09 15:08:12 +0000623
John McCall60d7b3a2010-08-24 06:29:42 +0000624 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000625 if (AssertExpr.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000626 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000627 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000628 }
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Anders Carlssonad5f9602009-03-13 23:29:20 +0000630 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000631 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000632
Richard Smith0cc323c2012-03-05 23:20:05 +0000633 if (!isTokenStringLiteral()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000634 Diag(Tok, diag::err_expected_string_literal);
Richard Smith3686c712012-09-13 19:12:50 +0000635 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000636 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000637 }
Mike Stump1eb44332009-09-09 15:08:12 +0000638
John McCall60d7b3a2010-08-24 06:29:42 +0000639 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smith99831e42012-03-06 03:21:47 +0000640 if (AssertMessage.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000641 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000642 return 0;
Richard Smith99831e42012-03-06 03:21:47 +0000643 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000644
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000645 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Chris Lattner97144fc2009-04-02 04:16:50 +0000647 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000648 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000649
John McCall9ae2f072010-08-23 23:25:46 +0000650 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
651 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000652 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000653 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000654}
655
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000656/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
657///
658/// 'decltype' ( expression )
659///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000660SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
661 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
662 && "Not a decltype specifier");
663
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000664
David Blaikie42d6d0c2011-12-04 05:04:18 +0000665 ExprResult Result;
666 SourceLocation StartLoc = Tok.getLocation();
667 SourceLocation EndLoc;
668
669 if (Tok.is(tok::annot_decltype)) {
670 Result = getExprAnnotation(Tok);
671 EndLoc = Tok.getAnnotationEndLoc();
672 ConsumeToken();
673 if (Result.isInvalid()) {
674 DS.SetTypeSpecError();
675 return EndLoc;
676 }
677 } else {
Richard Smithc7b55432012-02-24 22:30:04 +0000678 if (Tok.getIdentifierInfo()->isStr("decltype"))
679 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000680
David Blaikie42d6d0c2011-12-04 05:04:18 +0000681 ConsumeToken();
682
683 BalancedDelimiterTracker T(*this, tok::l_paren);
684 if (T.expectAndConsume(diag::err_expected_lparen_after,
685 "decltype", tok::r_paren)) {
686 DS.SetTypeSpecError();
687 return T.getOpenLocation() == Tok.getLocation() ?
688 StartLoc : T.getOpenLocation();
689 }
690
691 // Parse the expression
692
693 // C++0x [dcl.type.simple]p4:
694 // The operand of the decltype specifier is an unevaluated operand.
Richard Smith76f3f692012-02-22 02:04:18 +0000695 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
696 0, /*IsDecltype=*/true);
David Blaikie42d6d0c2011-12-04 05:04:18 +0000697 Result = ParseExpression();
698 if (Result.isInvalid()) {
Richard Smithd8e4dac2012-02-27 05:24:00 +0000699 SkipUntil(tok::r_paren);
David Blaikie42d6d0c2011-12-04 05:04:18 +0000700 DS.SetTypeSpecError();
Richard Smithd8e4dac2012-02-27 05:24:00 +0000701 return StartLoc;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000702 }
703
704 // Match the ')'
705 T.consumeClose();
706 if (T.getCloseLocation().isInvalid()) {
707 DS.SetTypeSpecError();
708 // FIXME: this should return the location of the last token
709 // that was consumed (by "consumeClose()")
710 return T.getCloseLocation();
711 }
712
Richard Smith76f3f692012-02-22 02:04:18 +0000713 Result = Actions.ActOnDecltypeExpression(Result.take());
714 if (Result.isInvalid()) {
715 DS.SetTypeSpecError();
716 return T.getCloseLocation();
717 }
718
David Blaikie42d6d0c2011-12-04 05:04:18 +0000719 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000720 }
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000722 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000723 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000724 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000725 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
David Blaikie42d6d0c2011-12-04 05:04:18 +0000726 DiagID, Result.release())) {
John McCallfec54012009-08-03 20:12:06 +0000727 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000728 DS.SetTypeSpecError();
729 }
730 return EndLoc;
731}
732
733void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
734 SourceLocation StartLoc,
735 SourceLocation EndLoc) {
736 // make sure we have a token we can turn into an annotation token
737 if (PP.isBacktrackEnabled())
738 PP.RevertCachedTokens(1);
739 else
740 PP.EnterToken(Tok);
741
742 Tok.setKind(tok::annot_decltype);
743 setExprAnnotation(Tok, DS.getTypeSpecType() == TST_decltype ?
744 DS.getRepAsExpr() : ExprResult());
745 Tok.setAnnotationEndLoc(EndLoc);
746 Tok.setLocation(StartLoc);
747 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000748}
749
Sean Huntdb5d44b2011-05-19 05:37:45 +0000750void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
751 assert(Tok.is(tok::kw___underlying_type) &&
752 "Not an underlying type specifier");
753
754 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000755 BalancedDelimiterTracker T(*this, tok::l_paren);
756 if (T.expectAndConsume(diag::err_expected_lparen_after,
757 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000758 return;
759 }
760
761 TypeResult Result = ParseTypeName();
762 if (Result.isInvalid()) {
763 SkipUntil(tok::r_paren);
764 return;
765 }
766
767 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000768 T.consumeClose();
769 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000770 return;
771
772 const char *PrevSpec = 0;
773 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000774 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000775 DiagID, Result.release()))
776 Diag(StartLoc, DiagID) << PrevSpec;
777}
778
David Blaikie09048df2011-10-25 15:01:20 +0000779/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
780/// class name or decltype-specifier. Note that we only check that the result
781/// names a type; semantic analysis will need to verify that the type names a
782/// class. The result is either a type or null, depending on whether a type
783/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000784///
David Blaikie09048df2011-10-25 15:01:20 +0000785/// base-type-specifier: [C++ 10.1]
786/// class-or-decltype
787/// class-or-decltype: [C++ 10.1]
788/// nested-name-specifier[opt] class-name
789/// decltype-specifier
Douglas Gregor42a552f2008-11-05 20:51:48 +0000790/// class-name: [C++ 9.1]
791/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000792/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000793///
David Blaikie22216eb2011-10-25 17:10:12 +0000794Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
795 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000796 // Ignore attempts to use typename
797 if (Tok.is(tok::kw_typename)) {
798 Diag(Tok, diag::err_expected_class_name_not_template)
799 << FixItHint::CreateRemoval(Tok.getLocation());
800 ConsumeToken();
801 }
802
David Blaikie152aa4b2011-10-25 18:17:58 +0000803 // Parse optional nested-name-specifier
804 CXXScopeSpec SS;
805 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
806
807 BaseLoc = Tok.getLocation();
808
David Blaikie22216eb2011-10-25 17:10:12 +0000809 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000810 // tok == kw_decltype is just error recovery, it can only happen when SS
811 // isn't empty
812 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000813 if (SS.isNotEmpty())
814 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
815 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000816 // Fake up a Declarator to use with ActOnTypeName.
817 DeclSpec DS(AttrFactory);
818
David Blaikieb5777572011-12-08 04:53:15 +0000819 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000820
821 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
822 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
823 }
824
Douglas Gregor7f43d672009-02-25 23:52:28 +0000825 // Check whether we have a template-id that names a type.
826 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000827 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000828 if (TemplateId->Kind == TNK_Type_template ||
829 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000830 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000831
832 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000833 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000834 EndLocation = Tok.getAnnotationEndLoc();
835 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000836
837 if (Type)
838 return Type;
839 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000840 }
841
842 // Fall through to produce an error below.
843 }
844
Douglas Gregor42a552f2008-11-05 20:51:48 +0000845 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000846 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000847 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000848 }
849
Douglas Gregor84d0a192010-01-12 21:28:44 +0000850 IdentifierInfo *Id = Tok.getIdentifierInfo();
851 SourceLocation IdLoc = ConsumeToken();
852
853 if (Tok.is(tok::less)) {
854 // It looks the user intended to write a template-id here, but the
855 // template-name was wrong. Try to fix that.
856 TemplateNameKind TNK = TNK_Type_template;
857 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000858 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000859 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000860 Diag(IdLoc, diag::err_unknown_template_name)
861 << Id;
862 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000863
Douglas Gregor84d0a192010-01-12 21:28:44 +0000864 if (!Template)
865 return true;
866
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000867 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000868 UnqualifiedId TemplateName;
869 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000870
Douglas Gregor84d0a192010-01-12 21:28:44 +0000871 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000872 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
873 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000874 return true;
875 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000876 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000877
Douglas Gregor84d0a192010-01-12 21:28:44 +0000878 // If we didn't end up with a typename token, there's nothing more we
879 // can do.
880 if (Tok.isNot(tok::annot_typename))
881 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000882
Douglas Gregor84d0a192010-01-12 21:28:44 +0000883 // Retrieve the type from the annotation token, consume that token, and
884 // return.
885 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000886 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000887 ConsumeToken();
888 return Type;
889 }
890
Douglas Gregor42a552f2008-11-05 20:51:48 +0000891 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000892 IdentifierInfo *CorrectedII = 0;
Douglas Gregor059101f2011-03-02 00:47:37 +0000893 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000894 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000895 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000896 /*NonTrivialTypeSourceInfo=*/true,
897 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000898 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000899 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000900 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000901 }
902
903 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000904 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000905
906 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000907 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000908 DS.SetRangeStart(IdLoc);
909 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000910 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000911
912 const char *PrevSpec = 0;
913 unsigned DiagID;
914 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
915
916 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
917 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000918}
919
John McCallc052dbb2012-05-22 21:28:12 +0000920void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
921 while (Tok.is(tok::kw___single_inheritance) ||
922 Tok.is(tok::kw___multiple_inheritance) ||
923 Tok.is(tok::kw___virtual_inheritance)) {
924 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
925 SourceLocation AttrNameLoc = ConsumeToken();
926 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000927 SourceLocation(), 0, 0, AttributeList::AS_GNU);
John McCallc052dbb2012-05-22 21:28:12 +0000928 }
929}
930
Richard Smithc9f35172012-06-25 21:37:02 +0000931/// Determine whether the following tokens are valid after a type-specifier
932/// which could be a standalone declaration. This will conservatively return
933/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith139be702012-07-02 19:14:01 +0000934bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smithc9f35172012-06-25 21:37:02 +0000935 // This switch enumerates the valid "follow" set for type-specifiers.
936 switch (Tok.getKind()) {
937 default: break;
938 case tok::semi: // struct foo {...} ;
939 case tok::star: // struct foo {...} * P;
940 case tok::amp: // struct foo {...} & R = ...
941 case tok::identifier: // struct foo {...} V ;
942 case tok::r_paren: //(struct foo {...} ) {4}
943 case tok::annot_cxxscope: // struct foo {...} a:: b;
944 case tok::annot_typename: // struct foo {...} a ::b;
945 case tok::annot_template_id: // struct foo {...} a<int> ::b;
946 case tok::l_paren: // struct foo {...} ( x);
947 case tok::comma: // __builtin_offsetof(struct foo{...} ,
948 return true;
Richard Smith139be702012-07-02 19:14:01 +0000949 case tok::colon:
950 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smithc9f35172012-06-25 21:37:02 +0000951 // Type qualifiers
952 case tok::kw_const: // struct foo {...} const x;
953 case tok::kw_volatile: // struct foo {...} volatile x;
954 case tok::kw_restrict: // struct foo {...} restrict x;
955 case tok::kw_inline: // struct foo {...} inline foo() {};
956 // Storage-class specifiers
957 case tok::kw_static: // struct foo {...} static x;
958 case tok::kw_extern: // struct foo {...} extern x;
959 case tok::kw_typedef: // struct foo {...} typedef x;
960 case tok::kw_register: // struct foo {...} register x;
961 case tok::kw_auto: // struct foo {...} auto x;
962 case tok::kw_mutable: // struct foo {...} mutable x;
963 case tok::kw_constexpr: // struct foo {...} constexpr x;
964 // As shown above, type qualifiers and storage class specifiers absolutely
965 // can occur after class specifiers according to the grammar. However,
966 // almost no one actually writes code like this. If we see one of these,
967 // it is much more likely that someone missed a semi colon and the
968 // type/storage class specifier we're seeing is part of the *next*
969 // intended declaration, as in:
970 //
971 // struct foo { ... }
972 // typedef int X;
973 //
974 // We'd really like to emit a missing semicolon error instead of emitting
975 // an error on the 'int' saying that you can't have two type specifiers in
976 // the same declaration of X. Because of this, we look ahead past this
977 // token to see if it's a type specifier. If so, we know the code is
978 // otherwise invalid, so we can produce the expected semi error.
979 if (!isKnownToBeTypeSpecifier(NextToken()))
980 return true;
981 break;
982 case tok::r_brace: // struct bar { struct foo {...} }
983 // Missing ';' at end of struct is accepted as an extension in C mode.
984 if (!getLangOpts().CPlusPlus)
985 return true;
986 break;
987 }
988 return false;
989}
990
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000991/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
992/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
993/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +0000994/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000995///
996/// class-specifier: [C++ class]
997/// class-head '{' member-specification[opt] '}'
998/// class-head '{' member-specification[opt] '}' attributes[opt]
999/// class-head:
1000/// class-key identifier[opt] base-clause[opt]
1001/// class-key nested-name-specifier identifier base-clause[opt]
1002/// class-key nested-name-specifier[opt] simple-template-id
1003/// base-clause[opt]
1004/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001005/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001006/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001007/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001008/// simple-template-id base-clause[opt]
1009/// class-key:
1010/// 'class'
1011/// 'struct'
1012/// 'union'
1013///
1014/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +00001015/// class-key ::[opt] nested-name-specifier[opt] identifier
1016/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1017/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001018///
1019/// Note that the C++ class-specifier and elaborated-type-specifier,
1020/// together, subsume the C99 struct-or-union-specifier:
1021///
1022/// struct-or-union-specifier: [C99 6.7.2.1]
1023/// struct-or-union identifier[opt] '{' struct-contents '}'
1024/// struct-or-union identifier
1025/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1026/// '}' attributes[opt]
1027/// [GNU] struct-or-union attributes[opt] identifier
1028/// struct-or-union:
1029/// 'struct'
1030/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +00001031void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1032 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001033 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001034 AccessSpecifier AS,
Richard Smith69730c12012-03-12 07:56:15 +00001035 bool EnteringContext, DeclSpecContext DSC) {
Joao Matos17d35c32012-08-31 22:18:20 +00001036 DeclSpec::TST TagType;
1037 if (TagTokKind == tok::kw_struct)
1038 TagType = DeclSpec::TST_struct;
1039 else if (TagTokKind == tok::kw___interface)
1040 TagType = DeclSpec::TST_interface;
1041 else if (TagTokKind == tok::kw_class)
1042 TagType = DeclSpec::TST_class;
1043 else {
Chris Lattner4c97d762009-04-12 21:49:30 +00001044 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1045 TagType = DeclSpec::TST_union;
1046 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001047
Douglas Gregor374929f2009-09-18 15:37:17 +00001048 if (Tok.is(tok::code_completion)) {
1049 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001050 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001051 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00001052 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001053
Chandler Carruth926c4b42010-06-28 08:39:25 +00001054 // C++03 [temp.explicit] 14.7.2/8:
1055 // The usual access checking rules do not apply to names used to specify
1056 // explicit instantiations.
1057 //
1058 // As an extension we do not perform access checking on the names used to
1059 // specify explicit specializations either. This is important to allow
1060 // specializing traits classes for private types.
John McCall13489672012-05-07 06:16:58 +00001061 //
1062 // Note that we don't suppress if this turns out to be an elaborated
1063 // type specifier.
1064 bool shouldDelayDiagsInTag =
1065 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1066 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1067 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001068
Sean Hunt2edf0a22012-06-23 05:07:58 +00001069 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001070 // If attributes exist after tag, parse them.
1071 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +00001072 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001073
Steve Narofff59e17e2008-12-24 20:59:21 +00001074 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001075 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001076 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001077
John McCallc052dbb2012-05-22 21:28:12 +00001078 // Parse inheritance specifiers.
1079 if (Tok.is(tok::kw___single_inheritance) ||
1080 Tok.is(tok::kw___multiple_inheritance) ||
1081 Tok.is(tok::kw___virtual_inheritance))
1082 ParseMicrosoftInheritanceClassAttributes(attrs);
1083
Sean Huntbbd37c62009-11-21 08:43:09 +00001084 // If C++0x attributes exist here, parse them.
1085 // FIXME: Are we consistent with the ordering of parsing of different
1086 // styles of attributes?
John McCall7f040a92010-12-24 02:08:15 +00001087 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001088
John Wiegley20c0da72011-04-27 23:09:49 +00001089 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +00001090 !Tok.is(tok::identifier) &&
1091 Tok.getIdentifierInfo() &&
1092 (Tok.is(tok::kw___is_arithmetic) ||
1093 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001094 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001095 Tok.is(tok::kw___is_floating_point) ||
1096 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001097 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001098 Tok.is(tok::kw___is_integral) ||
1099 Tok.is(tok::kw___is_member_function_pointer) ||
1100 Tok.is(tok::kw___is_member_pointer) ||
1101 Tok.is(tok::kw___is_pod) ||
1102 Tok.is(tok::kw___is_pointer) ||
1103 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001104 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001105 Tok.is(tok::kw___is_signed) ||
1106 Tok.is(tok::kw___is_unsigned) ||
1107 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001108 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001109 // name of struct templates, but some are keywords in GCC >= 4.3
1110 // and Clang. Therefore, when we see the token sequence "struct
1111 // X", make X into a normal identifier rather than a keyword, to
1112 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001113 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001114 Tok.setKind(tok::identifier);
1115 }
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001117 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001118 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001119 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001120 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1121 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001122
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001123 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001124 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001125 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001126 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1127 Diag(Tok, diag::err_expected_ident);
1128 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001129
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001130 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1131
Douglas Gregorcc636682009-02-17 23:15:12 +00001132 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001133 IdentifierInfo *Name = 0;
1134 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001135 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001136 if (Tok.is(tok::identifier)) {
1137 Name = Tok.getIdentifierInfo();
1138 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001139
David Blaikie4e4d0842012-03-11 07:00:24 +00001140 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001141 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001142 // Eat the template argument list and try to continue parsing this as
1143 // a class (or template thereof).
1144 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001145 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001146 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001147 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001148 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001149 // We couldn't parse the template argument list at all, so don't
1150 // try to give any location information for the list.
1151 LAngleLoc = RAngleLoc = SourceLocation();
1152 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001153
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001154 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matos17d35c32012-08-31 22:18:20 +00001155 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1156 << (TagType == DeclSpec::TST_class? 0
1157 : TagType == DeclSpec::TST_struct? 1
1158 : TagType == DeclSpec::TST_interface? 2
1159 : 3)
1160 << Name
1161 << SourceRange(LAngleLoc, RAngleLoc);
1162
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001163 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001164 // we've removed its template argument list.
1165 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1166 if (TemplateParams && TemplateParams->size() > 1) {
1167 TemplateParams->pop_back();
1168 } else {
1169 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001170 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001171 = ParsedTemplateInfo::NonTemplate;
1172 }
1173 } else if (TemplateInfo.Kind
1174 == ParsedTemplateInfo::ExplicitInstantiation) {
1175 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001176 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001177 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001178 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001179 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001180 = SourceLocation();
1181 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1182 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001183 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001184 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001185 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001186 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001187 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001188
Douglas Gregor059101f2011-03-02 00:47:37 +00001189 if (TemplateId->Kind != TNK_Type_template &&
1190 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001191 // The template-name in the simple-template-id refers to
1192 // something other than a class template. Give an appropriate
1193 // error message and skip to the ';'.
1194 SourceRange Range(NameLoc);
1195 if (SS.isNotEmpty())
1196 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001197
Douglas Gregor39a8de12009-02-25 19:37:18 +00001198 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1199 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Douglas Gregor39a8de12009-02-25 19:37:18 +00001201 DS.SetTypeSpecError();
1202 SkipUntil(tok::semi, false, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001203 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001204 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001205 }
1206
Richard Smith7796eb52012-03-12 08:56:40 +00001207 // There are four options here.
1208 // - If we are in a trailing return type, this is always just a reference,
1209 // and we must not try to parse a definition. For instance,
1210 // [] () -> struct S { };
1211 // does not define a type.
1212 // - If we have 'struct foo {...', 'struct foo :...',
1213 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1214 // - If we have 'struct foo;', then this is either a forward declaration
1215 // or a friend declaration, which have to be treated differently.
1216 // - Otherwise we have something like 'struct foo xyz', a reference.
Richard Smith69730c12012-03-12 07:56:15 +00001217 // However, in type-specifier-seq's, things look like declarations but are
1218 // just references, e.g.
1219 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001220 // or
Richard Smith69730c12012-03-12 07:56:15 +00001221 // &T::operator struct s;
1222 // For these, DSC is DSC_type_specifier.
John McCallf312b1e2010-08-26 23:41:50 +00001223 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001224 if (DSC == DSC_trailing)
1225 TUK = Sema::TUK_Reference;
1226 else if (Tok.is(tok::l_brace) ||
1227 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
1228 (isCXX0XFinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001229 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001230 if (DS.isFriendSpecified()) {
1231 // C++ [class.friend]p2:
1232 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001233 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001234 << SourceRange(DS.getFriendSpecLoc());
1235
1236 // Skip everything up to the semicolon, so that this looks like a proper
1237 // friend class (or template thereof) declaration.
1238 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001239 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001240 } else {
1241 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001242 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001243 }
Richard Smithc9f35172012-06-25 21:37:02 +00001244 } else if (DSC != DSC_type_specifier &&
1245 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00001246 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallf312b1e2010-08-26 23:41:50 +00001247 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matos17d35c32012-08-31 22:18:20 +00001248 if (Tok.isNot(tok::semi)) {
1249 // A semicolon was missing after this declaration. Diagnose and recover.
1250 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1251 DeclSpec::getSpecifierName(TagType));
1252 PP.EnterToken(Tok);
1253 Tok.setKind(tok::semi);
1254 }
Richard Smithc9f35172012-06-25 21:37:02 +00001255 } else
John McCallf312b1e2010-08-26 23:41:50 +00001256 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001257
John McCall13489672012-05-07 06:16:58 +00001258 // If this is an elaborated type specifier, and we delayed
1259 // diagnostics before, just merge them into the current pool.
1260 if (shouldDelayDiagsInTag) {
1261 diagsFromTag.done();
1262 if (TUK == Sema::TUK_Reference)
1263 diagsFromTag.redelay();
1264 }
1265
John McCall207014e2010-07-30 06:26:29 +00001266 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001267 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001268 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1269 // We have a declaration or reference to an anonymous class.
1270 Diag(StartLoc, diag::err_anon_type_definition)
1271 << DeclSpec::getSpecifierName(TagType);
1272 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001273
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001274 SkipUntil(tok::comma, true);
1275 return;
1276 }
1277
Douglas Gregorddc29e12009-02-06 22:42:48 +00001278 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001279 DeclResult TagOrTempResult = true; // invalid
1280 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001281
Douglas Gregor402abb52009-05-28 23:31:59 +00001282 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001283 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001284 // Explicit specialization, class template partial specialization,
1285 // or explicit instantiation.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001286 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001287 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001288 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001289 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001290 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001291 ProhibitAttributes(attrs);
1292
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001293 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001294 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001295 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001296 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001297 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001298 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001299 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001300 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001301 TemplateId->TemplateNameLoc,
1302 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001303 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001304 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001305 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001306
1307 // Friend template-ids are treated as references unless
1308 // they have template headers, in which case they're ill-formed
1309 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1310 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001311 } else if (TUK == Sema::TUK_Reference ||
1312 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001313 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001314 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001315 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001316 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001317 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001318 TemplateId->Template,
1319 TemplateId->TemplateNameLoc,
1320 TemplateId->LAngleLoc,
1321 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001322 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001323 } else {
1324 // This is an explicit specialization or a class template
1325 // partial specialization.
1326 TemplateParameterLists FakedParamLists;
1327
1328 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1329 // This looks like an explicit instantiation, because we have
1330 // something like
1331 //
1332 // template class Foo<X>
1333 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001334 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001335 // meant to be an explicit specialization, but the user forgot
1336 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +00001337 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001338
Mike Stump1eb44332009-09-09 15:08:12 +00001339 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001340 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001341 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001342 diag::err_explicit_instantiation_with_definition)
1343 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +00001344 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001345
1346 // Create a fake template parameter list that contains only
1347 // "template<>", so that we treat this construct as a class
1348 // template specialization.
1349 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +00001350 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001351 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001352 LAngleLoc,
1353 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001354 LAngleLoc));
1355 TemplateParams = &FakedParamLists;
1356 }
1357
1358 // Build the class template specialization.
1359 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001360 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001361 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001362 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001363 TemplateId->TemplateNameLoc,
1364 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001365 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001366 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001367 attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001368 MultiTemplateParamsArg(
Douglas Gregorcc636682009-02-17 23:15:12 +00001369 TemplateParams? &(*TemplateParams)[0] : 0,
1370 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001371 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001372 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001373 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001374 // Explicit instantiation of a member of a class template
1375 // specialization, e.g.,
1376 //
1377 // template struct Outer<int>::Inner;
1378 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001379 ProhibitAttributes(attrs);
1380
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001381 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001382 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001383 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001384 TemplateInfo.TemplateLoc,
1385 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001386 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001387 } else if (TUK == Sema::TUK_Friend &&
1388 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001389 ProhibitAttributes(attrs);
1390
John McCall9a34edb2010-10-19 01:40:49 +00001391 TagOrTempResult =
1392 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1393 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001394 Name, NameLoc, attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001395 MultiTemplateParamsArg(
John McCall9a34edb2010-10-19 01:40:49 +00001396 TemplateParams? &(*TemplateParams)[0] : 0,
1397 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001398 } else {
1399 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001400 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001401 // FIXME: Diagnose this particular error.
1402 }
1403
Sean Hunt2edf0a22012-06-23 05:07:58 +00001404 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1405 ProhibitAttributes(attrs);
1406
John McCallc4e70192009-09-11 04:59:25 +00001407 bool IsDependent = false;
1408
John McCalla25c4082010-10-19 18:40:57 +00001409 // Don't pass down template parameter lists if this is just a tag
1410 // reference. For example, we don't need the template parameters here:
1411 // template <class T> class A *makeA(T t);
1412 MultiTemplateParamsArg TParams;
1413 if (TUK != Sema::TUK_Reference && TemplateParams)
1414 TParams =
1415 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1416
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001417 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001418 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001419 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001420 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001421 TParams, Owned, IsDependent,
1422 SourceLocation(), false,
1423 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001424
1425 // If ActOnTag said the type was dependent, try again with the
1426 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001427 if (IsDependent) {
1428 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001429 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001430 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001431 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001432 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001433
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001434 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001435 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001436 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001437 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001438 isCXX0XFinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001439 if (getLangOpts().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +00001440 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001441 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001442 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001443 }
1444
John McCallb3d87482010-08-24 05:47:05 +00001445 const char *PrevSpec = 0;
1446 unsigned DiagID;
1447 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001448 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001449 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1450 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001451 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001452 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001453 Result = DS.SetTypeSpecType(TagType, StartLoc,
1454 NameLoc.isValid() ? NameLoc : StartLoc,
1455 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001456 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001457 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001458 return;
1459 }
Mike Stump1eb44332009-09-09 15:08:12 +00001460
John McCallb3d87482010-08-24 05:47:05 +00001461 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001462 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001463
Chris Lattner4ed5d912010-02-02 01:23:29 +00001464 // At this point, we've successfully parsed a class-specifier in 'definition'
1465 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1466 // going to look at what comes after it to improve error recovery. If an
1467 // impossible token occurs next, we assume that the programmer forgot a ; at
1468 // the end of the declaration and recover that way.
1469 //
Richard Smithc9f35172012-06-25 21:37:02 +00001470 // Also enforce C++ [temp]p3:
1471 // In a template-declaration which defines a class, no declarator
1472 // is permitted.
Joao Matos17d35c32012-08-31 22:18:20 +00001473 if (TUK == Sema::TUK_Definition &&
1474 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
1475 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1476 DeclSpec::getSpecifierName(TagType));
1477 // Push this token back into the preprocessor and change our current token
1478 // to ';' so that the rest of the code recovers as though there were an
1479 // ';' after the definition.
Richard Smithc9f35172012-06-25 21:37:02 +00001480 PP.EnterToken(Tok);
1481 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001482 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001483}
1484
Mike Stump1eb44332009-09-09 15:08:12 +00001485/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001486///
1487/// base-clause : [C++ class.derived]
1488/// ':' base-specifier-list
1489/// base-specifier-list:
1490/// base-specifier '...'[opt]
1491/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001492void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001493 assert(Tok.is(tok::colon) && "Not a base clause");
1494 ConsumeToken();
1495
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001496 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001497 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001498
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001499 while (true) {
1500 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001501 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001502 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001503 // Skip the rest of this base specifier, up until the comma or
1504 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001505 SkipUntil(tok::comma, tok::l_brace, true, true);
1506 } else {
1507 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001508 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001509 }
1510
1511 // If the next token is a comma, consume it and keep reading
1512 // base-specifiers.
1513 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001515 // Consume the comma.
1516 ConsumeToken();
1517 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001518
1519 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001520 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001521}
1522
1523/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1524/// one entry in the base class list of a class specifier, for example:
1525/// class foo : public bar, virtual private baz {
1526/// 'public bar' and 'virtual private baz' are each base-specifiers.
1527///
1528/// base-specifier: [C++ class.derived]
1529/// ::[opt] nested-name-specifier[opt] class-name
1530/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001531/// base-type-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001532/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001533/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001534Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001535 bool IsVirtual = false;
1536 SourceLocation StartLoc = Tok.getLocation();
1537
1538 // Parse the 'virtual' keyword.
1539 if (Tok.is(tok::kw_virtual)) {
1540 ConsumeToken();
1541 IsVirtual = true;
1542 }
1543
1544 // Parse an (optional) access specifier.
1545 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001546 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001547 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001549 // Parse the 'virtual' keyword (again!), in case it came after the
1550 // access specifier.
1551 if (Tok.is(tok::kw_virtual)) {
1552 SourceLocation VirtualLoc = ConsumeToken();
1553 if (IsVirtual) {
1554 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001555 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001556 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001557 }
1558
1559 IsVirtual = true;
1560 }
1561
Douglas Gregor42a552f2008-11-05 20:51:48 +00001562 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001563 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001564 SourceLocation BaseLoc;
1565 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001566 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001567 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001569 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1570 // actually part of the base-specifier-list grammar productions, but we
1571 // parse it here for convenience.
1572 SourceLocation EllipsisLoc;
1573 if (Tok.is(tok::ellipsis))
1574 EllipsisLoc = ConsumeToken();
1575
Mike Stump1eb44332009-09-09 15:08:12 +00001576 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001577 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001579 // Notify semantic analysis that we have parsed a complete
1580 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001581 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001582 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001583}
1584
1585/// getAccessSpecifierIfPresent - Determine whether the next token is
1586/// a C++ access-specifier.
1587///
1588/// access-specifier: [C++ class.derived]
1589/// 'private'
1590/// 'protected'
1591/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001592AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001593 switch (Tok.getKind()) {
1594 default: return AS_none;
1595 case tok::kw_private: return AS_private;
1596 case tok::kw_protected: return AS_protected;
1597 case tok::kw_public: return AS_public;
1598 }
1599}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001600
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001601/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001602/// delayed, e.g., default arguments, create a late-parsed method declaration
1603/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001604void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1605 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001606 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001607 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001608 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001609 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001610 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001611
Eli Friedmand33133c2009-07-22 21:45:50 +00001612 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1613 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1614 if (!LateMethod) {
1615 // Push this method onto the stack of late-parsed method
1616 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001617 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1618 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001619 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001620
1621 // Add all of the parameters prior to this one (they don't
1622 // have default arguments).
1623 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1624 for (unsigned I = 0; I < ParamIdx; ++I)
1625 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001626 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001627 }
1628
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001629 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001630 // not have a default argument).
1631 LateMethod->DefaultArgs.push_back(
1632 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1633 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1634 }
1635 }
1636}
1637
Richard Smith1c94c162012-01-09 22:31:44 +00001638/// isCXX0XVirtSpecifier - Determine whether the given token is a C++0x
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001639/// virt-specifier.
1640///
1641/// virt-specifier:
1642/// override
1643/// final
Richard Smith1c94c162012-01-09 22:31:44 +00001644VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001645 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001646 return VirtSpecifiers::VS_None;
1647
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001648 if (Tok.is(tok::identifier)) {
1649 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001650
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001651 // Initialize the contextual keywords.
1652 if (!Ident_final) {
1653 Ident_final = &PP.getIdentifierTable().get("final");
1654 Ident_override = &PP.getIdentifierTable().get("override");
1655 }
1656
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001657 if (II == Ident_override)
1658 return VirtSpecifiers::VS_Override;
1659
1660 if (II == Ident_final)
1661 return VirtSpecifiers::VS_Final;
1662 }
1663
1664 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001665}
1666
1667/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1668///
1669/// virt-specifier-seq:
1670/// virt-specifier
1671/// virt-specifier-seq virt-specifier
John McCalle402e722012-09-25 07:32:39 +00001672void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS,
1673 bool IsInterface) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001674 while (true) {
Anders Carlssoncc54d592011-01-22 16:56:46 +00001675 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001676 if (Specifier == VirtSpecifiers::VS_None)
1677 return;
1678
1679 // C++ [class.mem]p8:
1680 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001681 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001682 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001683 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1684 << PrevSpec
1685 << FixItHint::CreateRemoval(Tok.getLocation());
1686
John McCalle402e722012-09-25 07:32:39 +00001687 if (IsInterface && Specifier == VirtSpecifiers::VS_Final) {
1688 Diag(Tok.getLocation(), diag::err_override_control_interface)
1689 << VirtSpecifiers::getSpecifierName(Specifier);
1690 } else {
1691 Diag(Tok.getLocation(), getLangOpts().CPlusPlus0x ?
1692 diag::warn_cxx98_compat_override_control_keyword :
1693 diag::ext_override_control_keyword)
1694 << VirtSpecifiers::getSpecifierName(Specifier);
1695 }
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001696 ConsumeToken();
1697 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001698}
1699
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001700/// isCXX0XFinalKeyword - Determine whether the next token is a C++0x
1701/// contextual 'final' keyword.
1702bool Parser::isCXX0XFinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001703 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001704 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001705
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001706 if (!Tok.is(tok::identifier))
1707 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001708
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001709 // Initialize the contextual keywords.
1710 if (!Ident_final) {
1711 Ident_final = &PP.getIdentifierTable().get("final");
1712 Ident_override = &PP.getIdentifierTable().get("override");
1713 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001714
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001715 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001716}
1717
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001718/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1719///
1720/// member-declaration:
1721/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1722/// function-definition ';'[opt]
1723/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1724/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001725/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001726/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001727/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001728///
1729/// member-declarator-list:
1730/// member-declarator
1731/// member-declarator-list ',' member-declarator
1732///
1733/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001734/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001735/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001736/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001737/// identifier[opt] ':' constant-expression
1738///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001739/// virt-specifier-seq:
1740/// virt-specifier
1741/// virt-specifier-seq virt-specifier
1742///
1743/// virt-specifier:
1744/// override
1745/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001746///
Sebastian Redle2b68332009-04-12 17:16:29 +00001747/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001748/// '= 0'
1749///
1750/// constant-initializer:
1751/// '=' constant-expression
1752///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001753void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001754 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001755 const ParsedTemplateInfo &TemplateInfo,
1756 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001757 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001758 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001759 Diag(Tok, diag::err_at_defs_cxx);
1760 else
1761 Diag(Tok, diag::err_at_in_class);
1762
1763 ConsumeToken();
1764 SkipUntil(tok::r_brace);
1765 return;
1766 }
1767
John McCall60fa3cf2009-12-11 02:10:03 +00001768 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001769 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001770 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001771 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1772 if (TryAnnotateCXXScopeToken())
1773 MalformedTypeSpec = true;
1774
1775 bool isAccessDecl;
1776 if (Tok.isNot(tok::annot_cxxscope))
1777 isAccessDecl = false;
1778 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001779 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1780 else
1781 isAccessDecl = NextToken().is(tok::kw_operator);
1782
1783 if (isAccessDecl) {
1784 // Collect the scope specifier token we annotated earlier.
1785 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001786 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1787 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001788
1789 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001790 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001791 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001792 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1793 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001794 SkipUntil(tok::semi);
1795 return;
1796 }
1797
1798 // TODO: recover from mistakenly-qualified operator declarations.
1799 if (ExpectAndConsume(tok::semi,
1800 diag::err_expected_semi_after,
1801 "access declaration",
1802 tok::semi))
1803 return;
1804
Douglas Gregor23c94db2010-07-02 17:43:08 +00001805 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001806 false, SourceLocation(),
1807 SS, Name,
1808 /* AttrList */ 0,
1809 /* IsTypeName */ false,
1810 SourceLocation());
1811 return;
1812 }
1813 }
1814
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001815 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001816 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001817 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001818 SourceLocation DeclEnd;
1819 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001820 return;
1821 }
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Chris Lattner682bf922009-03-29 16:50:03 +00001823 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001824 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001825 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001826 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001827 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001828 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001829 return;
1830 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001831
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001832 // Handle: member-declaration ::= '__extension__' member-declaration
1833 if (Tok.is(tok::kw___extension__)) {
1834 // __extension__ silences extension warnings in the subexpression.
1835 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1836 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001837 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1838 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001839 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001840
Chris Lattner4ed5d912010-02-02 01:23:29 +00001841 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1842 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001843 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001844
John McCall0b7e6782011-03-24 11:26:52 +00001845 ParsedAttributesWithRange attrs(AttrFactory);
Sean Huntbbd37c62009-11-21 08:43:09 +00001846 // Optional C++0x attribute-specifier
John McCall7f040a92010-12-24 02:08:15 +00001847 MaybeParseCXX0XAttributes(attrs);
1848 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001849
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001850 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00001851 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001852
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001853 // Eat 'using'.
1854 SourceLocation UsingLoc = ConsumeToken();
1855
1856 if (Tok.is(tok::kw_namespace)) {
1857 Diag(UsingLoc, diag::err_using_namespace_in_class);
1858 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001859 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001860 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001861 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00001862 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1863 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001864 }
1865 return;
1866 }
1867
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001868 // Hold late-parsed attributes so we can attach a Decl to them later.
1869 LateParsedAttrList CommonLateParsedAttrs;
1870
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001871 // decl-specifier-seq:
1872 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001873 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00001874 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00001875 if (MalformedTypeSpec)
1876 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001877 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
1878 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001879
Benjamin Kramer5354e772012-08-23 23:38:35 +00001880 MultiTemplateParamsArg TemplateParams(
John McCalldd4a3b02009-09-16 22:47:08 +00001881 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1882 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1883
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001884 if (Tok.is(tok::semi)) {
1885 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001886 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00001887 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00001888 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001889 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001890 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001891
John McCall54abf7d2009-11-04 02:18:39 +00001892 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00001893 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001894
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001895 // Hold late-parsed attributes so we can attach a Decl to them later.
1896 LateParsedAttrList LateParsedAttrs;
1897
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001898 SourceLocation EqualLoc;
1899 bool HasInitializer = false;
1900 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001901 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001902 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1903 ColonProtectionRAIIObject X(*this);
1904
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001905 // Parse the first declarator.
1906 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00001907 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001908 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001909 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00001910 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001911 if (Tok.is(tok::semi))
1912 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001913 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001914 }
1915
John McCalle402e722012-09-25 07:32:39 +00001916 ParseOptionalCXX0XVirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber48673472011-01-28 06:07:34 +00001917
John Thompson1b2fc0f2009-11-25 22:58:06 +00001918 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001919 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00001920
Francois Pichet6a247472011-05-11 02:14:46 +00001921 // MSVC permits pure specifier on inline functions declared at class scope.
1922 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00001923 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00001924 DeclaratorInfo.isFunctionDeclarator() &&
1925 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001926 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00001927 Init = ParseInitializer();
1928 if (Init.isInvalid())
1929 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001930 else
1931 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00001932 }
1933
Douglas Gregor45fa5602011-11-07 20:56:01 +00001934 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001935 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00001936 //
1937 // In C++11, a non-function declarator followed by an open brace is a
1938 // braced-init-list for an in-class member initialization, not an
1939 // erroneous function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00001940 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus0x) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00001941 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00001942 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00001943 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00001944 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00001945 } else if (Tok.is(tok::equal)) {
1946 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00001947 if (KW.is(tok::kw_default))
1948 DefinitionKind = FDK_Defaulted;
1949 else if (KW.is(tok::kw_delete))
1950 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00001951 }
1952 }
1953
Douglas Gregor45fa5602011-11-07 20:56:01 +00001954 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001955 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00001956 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001957 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00001958 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001959
1960 // Consume the optional ';'
1961 if (Tok.is(tok::semi))
1962 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001963 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001964 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001965
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001966 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00001967 Diag(DeclaratorInfo.getIdentifierLoc(),
1968 diag::err_function_declared_typedef);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001969 // This recovery skips the entire function body. It would be nice
1970 // to simply call ParseCXXInlineMethodDef() below, however Sema
1971 // assumes the declarator represents a function, not a typedef.
1972 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00001973 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001974
1975 // Consume the optional ';'
1976 if (Tok.is(tok::semi))
1977 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001978 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001979 }
1980
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001981 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001982 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00001983 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001984
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001985 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
1986 CommonLateParsedAttrs[i]->addDecl(FunDecl);
1987 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001988 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001989 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001990 }
1991 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00001992
1993 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00001994 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00001995 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001996
Chris Lattner682bf922009-03-29 16:50:03 +00001997 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001998 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001999 }
2000
2001 // member-declarator-list:
2002 // member-declarator
2003 // member-declarator-list ',' member-declarator
2004
Chris Lattner5f9e2722011-07-23 10:55:15 +00002005 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00002006 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00002007 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002008
2009 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002010 // member-declarator:
2011 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002012 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002013 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002014 if (Tok.is(tok::colon)) {
2015 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002016 BitfieldSize = ParseConstantExpression();
2017 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002018 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002019 }
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Chris Lattnere6563252010-06-13 05:34:18 +00002021 // If a simple-asm-expr is present, parse it.
2022 if (Tok.is(tok::kw_asm)) {
2023 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002024 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002025 if (AsmLabel.isInvalid())
2026 SkipUntil(tok::comma, true, true);
2027
2028 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2029 DeclaratorInfo.SetRangeEnd(Loc);
2030 }
2031
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002032 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002033 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002034
Richard Smith7a614d82011-06-11 17:19:42 +00002035 // FIXME: When g++ adds support for this, we'll need to check whether it
2036 // goes before or after the GNU attributes and __asm__.
John McCalle402e722012-09-25 07:32:39 +00002037 ParseOptionalCXX0XVirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith7a614d82011-06-11 17:19:42 +00002038
Richard Smithca523302012-06-10 03:12:00 +00002039 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002040 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002041 if (BitfieldSize.get()) {
2042 Diag(Tok, diag::err_bitfield_member_init);
2043 SkipUntil(tok::comma, true, true);
2044 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002045 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002046 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2047 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2048 != DeclSpec::SCS_static &&
2049 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2050 != DeclSpec::SCS_typedef)
2051 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002052 }
2053 }
2054
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002055 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002056 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002057 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002058
John McCalld226f652010-08-21 09:40:31 +00002059 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002060 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00002061 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002062 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002063 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002064 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002065 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002066 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002067 TemplateParams,
John McCall67d1a672009-08-06 02:15:43 +00002068 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002069 VS, HasInClassInit);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002070 if (AccessAttrs)
2071 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2072 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002073 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002074
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002075 // Set the Decl for any late parsed attributes
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002076 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2077 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2078 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002079 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002080 LateParsedAttrs[i]->addDecl(ThisDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002081 }
2082 LateParsedAttrs.clear();
2083
Douglas Gregor147545d2011-10-10 14:49:18 +00002084 // Handle the initializer.
Richard Smithca523302012-06-10 03:12:00 +00002085 if (HasInClassInit != ICIS_NoInit) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002086 // The initializer was deferred; parse it and cache the tokens.
David Blaikie4e4d0842012-03-11 07:00:24 +00002087 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00002088 diag::warn_cxx98_compat_nonstatic_member_init :
2089 diag::ext_nonstatic_member_init);
2090
Richard Smith7a614d82011-06-11 17:19:42 +00002091 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002092 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2093 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002094 //
2095 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002096 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002097 Diag(Tok, diag::err_incomplete_array_member_init);
2098 SkipUntil(tok::comma, true, true);
David Blaikie3164c142012-02-14 09:00:46 +00002099 if (ThisDecl)
2100 // Avoid later warnings about a class member of incomplete type.
2101 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002102 } else
2103 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002104 } else if (HasInitializer) {
2105 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002106 if (!Init.isUsable())
Douglas Gregor552e2992012-02-21 02:22:07 +00002107 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002108 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2109
Douglas Gregor147545d2011-10-10 14:49:18 +00002110 if (Init.isInvalid())
2111 SkipUntil(tok::comma, true, true);
2112 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002113 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002114 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor147545d2011-10-10 14:49:18 +00002115 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2116 // No initializer.
2117 Actions.ActOnUninitializedDecl(ThisDecl,
2118 DS.getTypeSpecType() == DeclSpec::TST_auto);
Richard Smith7a614d82011-06-11 17:19:42 +00002119 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002120
2121 if (ThisDecl) {
2122 Actions.FinalizeDeclaration(ThisDecl);
2123 DeclsInGroup.push_back(ThisDecl);
2124 }
2125
Richard Smithe5310012012-04-29 07:31:09 +00002126 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor147545d2011-10-10 14:49:18 +00002127 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2128 != DeclSpec::SCS_typedef) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002129 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002130 }
2131
2132 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002133
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002134 // If we don't have a comma, it is either the end of the list (a ';')
2135 // or an error, bail out.
2136 if (Tok.isNot(tok::comma))
2137 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002138
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002139 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002140 SourceLocation CommaLoc = ConsumeToken();
2141
2142 if (Tok.isAtStartOfLine() &&
2143 !MightBeDeclarator(Declarator::MemberContext)) {
2144 // This comma was followed by a line-break and something which can't be
2145 // the start of a declarator. The comma was probably a typo for a
2146 // semicolon.
2147 Diag(CommaLoc, diag::err_expected_semi_declaration)
2148 << FixItHint::CreateReplacement(CommaLoc, ";");
2149 ExpectSemi = false;
2150 break;
2151 }
Mike Stump1eb44332009-09-09 15:08:12 +00002152
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002153 // Parse the next declarator.
2154 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002155 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002156 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002157 Init = true;
2158 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002159 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002160
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002161 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002162 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002163
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002164 if (Tok.isNot(tok::colon))
2165 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002166 }
2167
Richard Smith1c94c162012-01-09 22:31:44 +00002168 if (ExpectSemi &&
2169 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002170 // Skip to end of block or statement.
2171 SkipUntil(tok::r_brace, true, true);
2172 // If we stopped at a ';', eat it.
2173 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002174 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002175 }
2176
Douglas Gregor23c94db2010-07-02 17:43:08 +00002177 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00002178 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002179}
2180
Richard Smith7a614d82011-06-11 17:19:42 +00002181/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2182/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2183/// function definition. The location of the '=', if any, will be placed in
2184/// EqualLoc.
2185///
2186/// pure-specifier:
2187/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002188///
Richard Smith7a614d82011-06-11 17:19:42 +00002189/// brace-or-equal-initializer:
2190/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002191/// braced-init-list
2192///
Richard Smith7a614d82011-06-11 17:19:42 +00002193/// initializer-clause:
2194/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002195/// braced-init-list
2196///
Richard Smith7a614d82011-06-11 17:19:42 +00002197/// defaulted/deleted function-definition:
2198/// '=' 'default'
2199/// '=' 'delete'
2200///
2201/// Prior to C++0x, the assignment-expression in an initializer-clause must
2202/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002203ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002204 SourceLocation &EqualLoc) {
2205 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2206 && "Data member initializer not starting with '=' or '{'");
2207
Douglas Gregor552e2992012-02-21 02:22:07 +00002208 EnterExpressionEvaluationContext Context(Actions,
2209 Sema::PotentiallyEvaluated,
2210 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002211 if (Tok.is(tok::equal)) {
2212 EqualLoc = ConsumeToken();
2213 if (Tok.is(tok::kw_delete)) {
2214 // In principle, an initializer of '= delete p;' is legal, but it will
2215 // never type-check. It's better to diagnose it as an ill-formed expression
2216 // than as an ill-formed deleted non-function member.
2217 // An initializer of '= delete p, foo' will never be parsed, because
2218 // a top-level comma always ends the initializer expression.
2219 const Token &Next = NextToken();
2220 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2221 Next.is(tok::eof)) {
2222 if (IsFunction)
2223 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2224 << 1 /* delete */;
2225 else
2226 Diag(ConsumeToken(), diag::err_deleted_non_function);
2227 return ExprResult();
2228 }
2229 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002230 if (IsFunction)
2231 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2232 << 0 /* default */;
2233 else
2234 Diag(ConsumeToken(), diag::err_default_special_members);
2235 return ExprResult();
2236 }
2237
Sebastian Redl33deb352012-02-22 10:50:08 +00002238 }
2239 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002240}
2241
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002242/// ParseCXXMemberSpecification - Parse the class definition.
2243///
2244/// member-specification:
2245/// member-declaration member-specification[opt]
2246/// access-specifier ':' member-specification[opt]
2247///
Joao Matos17d35c32012-08-31 22:18:20 +00002248void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
2249 unsigned TagType, Decl *TagDecl) {
2250 assert((TagType == DeclSpec::TST_struct ||
2251 TagType == DeclSpec::TST_interface ||
2252 TagType == DeclSpec::TST_union ||
2253 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2254
John McCallf312b1e2010-08-26 23:41:50 +00002255 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2256 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002257
Douglas Gregor26997fd2010-01-16 20:52:59 +00002258 // Determine whether this is a non-nested class. Note that local
2259 // classes are *not* considered to be nested classes.
2260 bool NonNestedClass = true;
2261 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002262 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002263 if (S->isClassScope()) {
2264 // We're inside a class scope, so this is a nested class.
2265 NonNestedClass = false;
John McCalle402e722012-09-25 07:32:39 +00002266
2267 // The Microsoft extension __interface does not permit nested classes.
2268 if (getCurrentClass().IsInterface) {
2269 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2270 << /*ErrorType=*/6
2271 << (isa<NamedDecl>(TagDecl)
2272 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2273 : "<anonymous>");
2274 }
Douglas Gregor26997fd2010-01-16 20:52:59 +00002275 break;
2276 }
2277
2278 if ((S->getFlags() & Scope::FnScope)) {
2279 // If we're in a function or function template declared in the
2280 // body of a class, then this is a local class rather than a
2281 // nested class.
2282 const Scope *Parent = S->getParent();
2283 if (Parent->isTemplateParamScope())
2284 Parent = Parent->getParent();
2285 if (Parent->isClassScope())
2286 break;
2287 }
2288 }
2289 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002290
2291 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002292 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002293
Douglas Gregor6569d682009-05-27 23:11:45 +00002294 // Note that we are parsing a new (potentially-nested) class definition.
John McCalle402e722012-09-25 07:32:39 +00002295 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2296 TagType == DeclSpec::TST_interface);
Douglas Gregor6569d682009-05-27 23:11:45 +00002297
Douglas Gregorddc29e12009-02-06 22:42:48 +00002298 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002299 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002300
Anders Carlssonb184a182011-03-25 14:46:08 +00002301 SourceLocation FinalLoc;
2302
2303 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002304 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith8b11b5e2011-10-15 04:21:46 +00002305 assert(isCXX0XFinalKeyword() && "not a class definition");
2306 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002307
John McCalle402e722012-09-25 07:32:39 +00002308 if (TagType == DeclSpec::TST_interface) {
2309 Diag(FinalLoc, diag::err_override_control_interface)
2310 << "final";
2311 } else {
2312 Diag(FinalLoc, getLangOpts().CPlusPlus0x ?
2313 diag::warn_cxx98_compat_override_control_keyword :
2314 diag::ext_override_control_keyword) << "final";
2315 }
Anders Carlssonb184a182011-03-25 14:46:08 +00002316 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002317
John McCallbd0dfa52009-12-19 21:48:58 +00002318 if (Tok.is(tok::colon)) {
2319 ParseBaseClause(TagDecl);
2320
2321 if (!Tok.is(tok::l_brace)) {
2322 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002323
2324 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002325 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002326 return;
2327 }
2328 }
2329
2330 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002331 BalancedDelimiterTracker T(*this, tok::l_brace);
2332 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002333
John McCall42a4f662010-05-28 08:11:17 +00002334 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002335 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002336 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002337
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002338 // C++ 11p3: Members of a class defined with the keyword class are private
2339 // by default. Members of a class defined with the keywords struct or union
2340 // are public by default.
2341 AccessSpecifier CurAS;
2342 if (TagType == DeclSpec::TST_class)
2343 CurAS = AS_private;
2344 else
2345 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002346 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002347
Douglas Gregor07976d22010-06-21 22:31:09 +00002348 if (TagDecl) {
2349 // While we still have something to read, read the member-declarations.
2350 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2351 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002352
David Blaikie4e4d0842012-03-11 07:00:24 +00002353 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002354 Tok.is(tok::kw___if_not_exists))) {
2355 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2356 continue;
2357 }
2358
Douglas Gregor07976d22010-06-21 22:31:09 +00002359 // Check for extraneous top-level semicolon.
2360 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002361 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002362 continue;
2363 }
2364
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002365 if (Tok.is(tok::annot_pragma_vis)) {
2366 HandlePragmaVisibility();
2367 continue;
2368 }
2369
2370 if (Tok.is(tok::annot_pragma_pack)) {
2371 HandlePragmaPack();
2372 continue;
2373 }
2374
Douglas Gregor07976d22010-06-21 22:31:09 +00002375 AccessSpecifier AS = getAccessSpecifierIfPresent();
2376 if (AS != AS_none) {
2377 // Current token is a C++ access specifier.
2378 CurAS = AS;
2379 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002380 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002381 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002382 AccessAttrs.clear();
2383 MaybeParseGNUAttributes(AccessAttrs);
2384
David Blaikie13f8daf2011-10-13 06:08:43 +00002385 SourceLocation EndLoc;
2386 if (Tok.is(tok::colon)) {
2387 EndLoc = Tok.getLocation();
2388 ConsumeToken();
2389 } else if (Tok.is(tok::semi)) {
2390 EndLoc = Tok.getLocation();
2391 ConsumeToken();
2392 Diag(EndLoc, diag::err_expected_colon)
2393 << FixItHint::CreateReplacement(EndLoc, ":");
2394 } else {
2395 EndLoc = ASLoc.getLocWithOffset(TokLength);
2396 Diag(EndLoc, diag::err_expected_colon)
2397 << FixItHint::CreateInsertion(EndLoc, ":");
2398 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002399
John McCalle402e722012-09-25 07:32:39 +00002400 // The Microsoft extension __interface does not permit non-public
2401 // access specifiers.
2402 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2403 Diag(ASLoc, diag::err_access_specifier_interface)
2404 << (CurAS == AS_protected);
2405 }
2406
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002407 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2408 AccessAttrs.getList())) {
2409 // found another attribute than only annotations
2410 AccessAttrs.clear();
2411 }
2412
Douglas Gregor07976d22010-06-21 22:31:09 +00002413 continue;
2414 }
2415
2416 // FIXME: Make sure we don't have a template here.
2417
2418 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002419 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002420 }
2421
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002422 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002423 } else {
2424 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002425 }
Mike Stump1eb44332009-09-09 15:08:12 +00002426
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002427 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002428 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002429 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002430
John McCall42a4f662010-05-28 08:11:17 +00002431 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002432 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002433 T.getOpenLocation(),
2434 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002435 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002436
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002437 // C++11 [class.mem]p2:
2438 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002439 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002440 // brace-or-equal-initializers for non-static data members (including such
2441 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002442 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002443 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002444 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002445 // declarations and the lexed inline method definitions, along with any
2446 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002447 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002448 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002449 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002450
2451 // We've finished with all pending member declarations.
2452 Actions.ActOnFinishCXXMemberDecls();
2453
Richard Smith7a614d82011-06-11 17:19:42 +00002454 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002455 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002456 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002457 }
2458
John McCall42a4f662010-05-28 08:11:17 +00002459 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002460 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2461 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002462
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002463 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002464 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002465 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002466}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002467
2468/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2469/// which explicitly initializes the members or base classes of a
2470/// class (C++ [class.base.init]). For example, the three initializers
2471/// after the ':' in the Derived constructor below:
2472///
2473/// @code
2474/// class Base { };
2475/// class Derived : Base {
2476/// int x;
2477/// float f;
2478/// public:
2479/// Derived(float f) : Base(), x(17), f(f) { }
2480/// };
2481/// @endcode
2482///
Mike Stump1eb44332009-09-09 15:08:12 +00002483/// [C++] ctor-initializer:
2484/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002485///
Mike Stump1eb44332009-09-09 15:08:12 +00002486/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002487/// mem-initializer ...[opt]
2488/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002489void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002490 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2491
John Wiegley28bbe4b2011-04-28 01:08:34 +00002492 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2493 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002494 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002495
Chris Lattner5f9e2722011-07-23 10:55:15 +00002496 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002497 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002498
Douglas Gregor7ad83902008-11-05 04:29:56 +00002499 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002500 if (Tok.is(tok::code_completion)) {
2501 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2502 MemInitializers.data(),
2503 MemInitializers.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002504 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002505 } else {
2506 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2507 if (!MemInit.isInvalid())
2508 MemInitializers.push_back(MemInit.get());
2509 else
2510 AnyErrors = true;
2511 }
2512
Douglas Gregor7ad83902008-11-05 04:29:56 +00002513 if (Tok.is(tok::comma))
2514 ConsumeToken();
2515 else if (Tok.is(tok::l_brace))
2516 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002517 // If the next token looks like a base or member initializer, assume that
2518 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002519 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2520 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2521 Diag(Loc, diag::err_ctor_init_missing_comma)
2522 << FixItHint::CreateInsertion(Loc, ", ");
2523 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002524 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002525 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002526 SkipUntil(tok::l_brace, true, true);
2527 break;
2528 }
2529 } while (true);
2530
Mike Stump1eb44332009-09-09 15:08:12 +00002531 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002532 MemInitializers.data(), MemInitializers.size(),
2533 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002534}
2535
2536/// ParseMemInitializer - Parse a C++ member initializer, which is
2537/// part of a constructor initializer that explicitly initializes one
2538/// member or base class (C++ [class.base.init]). See
2539/// ParseConstructorInitializer for an example.
2540///
2541/// [C++] mem-initializer:
2542/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002543/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002544///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002545/// [C++] mem-initializer-id:
2546/// '::'[opt] nested-name-specifier[opt] class-name
2547/// identifier
John McCalld226f652010-08-21 09:40:31 +00002548Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002549 // parse '::'[opt] nested-name-specifier[opt]
2550 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002551 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002552 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002553 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002554 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002555 if (TemplateId->Kind == TNK_Type_template ||
2556 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002557 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002558 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002559 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002560 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002561 }
David Blaikief2116622012-01-24 06:03:59 +00002562 // Uses of decltype will already have been converted to annot_decltype by
2563 // ParseOptionalCXXScopeSpecifier at this point.
2564 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2565 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002566 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002567 return true;
2568 }
Mike Stump1eb44332009-09-09 15:08:12 +00002569
David Blaikief2116622012-01-24 06:03:59 +00002570 IdentifierInfo *II = 0;
2571 DeclSpec DS(AttrFactory);
2572 SourceLocation IdLoc = Tok.getLocation();
2573 if (Tok.is(tok::annot_decltype)) {
2574 // Get the decltype expression, if there is one.
2575 ParseDecltypeSpecifier(DS);
2576 } else {
2577 if (Tok.is(tok::identifier))
2578 // Get the identifier. This may be a member name or a class name,
2579 // but we'll let the semantic analysis determine which it is.
2580 II = Tok.getIdentifierInfo();
2581 ConsumeToken();
2582 }
2583
Douglas Gregor7ad83902008-11-05 04:29:56 +00002584
2585 // Parse the '('.
David Blaikie4e4d0842012-03-11 07:00:24 +00002586 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002587 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2588
Sebastian Redl6df65482011-09-24 17:48:25 +00002589 ExprResult InitList = ParseBraceInitializer();
2590 if (InitList.isInvalid())
2591 return true;
2592
2593 SourceLocation EllipsisLoc;
2594 if (Tok.is(tok::ellipsis))
2595 EllipsisLoc = ConsumeToken();
2596
2597 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002598 TemplateTypeTy, DS, IdLoc,
2599 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002600 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002601 BalancedDelimiterTracker T(*this, tok::l_paren);
2602 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002603
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002604 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002605 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002606 CommaLocsTy CommaLocs;
2607 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2608 SkipUntil(tok::r_paren);
2609 return true;
2610 }
2611
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002612 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002613
2614 SourceLocation EllipsisLoc;
2615 if (Tok.is(tok::ellipsis))
2616 EllipsisLoc = ConsumeToken();
2617
2618 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002619 TemplateTypeTy, DS, IdLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002620 T.getOpenLocation(), ArgExprs.data(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002621 ArgExprs.size(), T.getCloseLocation(),
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002622 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002623 }
2624
David Blaikie4e4d0842012-03-11 07:00:24 +00002625 Diag(Tok, getLangOpts().CPlusPlus0x ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002626 : diag::err_expected_lparen);
2627 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002628}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002629
Sebastian Redl7acafd02011-03-05 14:45:16 +00002630/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002631///
Douglas Gregora4745612008-12-01 18:00:20 +00002632/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002633/// dynamic-exception-specification
2634/// noexcept-specification
2635///
2636/// noexcept-specification:
2637/// 'noexcept'
2638/// 'noexcept' '(' constant-expression ')'
2639ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002640Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002641 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002642 SmallVectorImpl<ParsedType> &DynamicExceptions,
2643 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002644 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002645 ExceptionSpecificationType Result = EST_None;
2646
2647 // See if there's a dynamic specification.
2648 if (Tok.is(tok::kw_throw)) {
2649 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2650 DynamicExceptions,
2651 DynamicExceptionRanges);
2652 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2653 "Produced different number of exception types and ranges.");
2654 }
2655
2656 // If there's no noexcept specification, we're done.
2657 if (Tok.isNot(tok::kw_noexcept))
2658 return Result;
2659
Richard Smith841804b2011-10-17 23:06:20 +00002660 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2661
Sebastian Redl7acafd02011-03-05 14:45:16 +00002662 // If we already had a dynamic specification, parse the noexcept for,
2663 // recovery, but emit a diagnostic and don't store the results.
2664 SourceRange NoexceptRange;
2665 ExceptionSpecificationType NoexceptType = EST_None;
2666
2667 SourceLocation KeywordLoc = ConsumeToken();
2668 if (Tok.is(tok::l_paren)) {
2669 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002670 BalancedDelimiterTracker T(*this, tok::l_paren);
2671 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002672 NoexceptType = EST_ComputedNoexcept;
2673 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002674 // The argument must be contextually convertible to bool. We use
2675 // ActOnBooleanCondition for this purpose.
2676 if (!NoexceptExpr.isInvalid())
2677 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2678 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002679 T.consumeClose();
2680 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002681 } else {
2682 // There is no argument.
2683 NoexceptType = EST_BasicNoexcept;
2684 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2685 }
2686
2687 if (Result == EST_None) {
2688 SpecificationRange = NoexceptRange;
2689 Result = NoexceptType;
2690
2691 // If there's a dynamic specification after a noexcept specification,
2692 // parse that and ignore the results.
2693 if (Tok.is(tok::kw_throw)) {
2694 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2695 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2696 DynamicExceptionRanges);
2697 }
2698 } else {
2699 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2700 }
2701
2702 return Result;
2703}
2704
2705/// ParseDynamicExceptionSpecification - Parse a C++
2706/// dynamic-exception-specification (C++ [except.spec]).
2707///
2708/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002709/// 'throw' '(' type-id-list [opt] ')'
2710/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002711///
Douglas Gregora4745612008-12-01 18:00:20 +00002712/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002713/// type-id ... [opt]
2714/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002715///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002716ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2717 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002718 SmallVectorImpl<ParsedType> &Exceptions,
2719 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002720 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002721
Sebastian Redl7acafd02011-03-05 14:45:16 +00002722 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002723 BalancedDelimiterTracker T(*this, tok::l_paren);
2724 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002725 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2726 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002727 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002728 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002729
Douglas Gregora4745612008-12-01 18:00:20 +00002730 // Parse throw(...), a Microsoft extension that means "this function
2731 // can throw anything".
2732 if (Tok.is(tok::ellipsis)) {
2733 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002734 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002735 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002736 T.consumeClose();
2737 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002738 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002739 }
2740
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002741 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002742 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002743 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002744 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002745
Douglas Gregora04426c2010-12-20 23:57:46 +00002746 if (Tok.is(tok::ellipsis)) {
2747 // C++0x [temp.variadic]p5:
2748 // - In a dynamic-exception-specification (15.4); the pattern is a
2749 // type-id.
2750 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002751 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002752 if (!Res.isInvalid())
2753 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2754 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002755
Sebastian Redlef65f062009-05-29 18:02:33 +00002756 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002757 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002758 Ranges.push_back(Range);
2759 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002760
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002761 if (Tok.is(tok::comma))
2762 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002763 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002764 break;
2765 }
2766
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002767 T.consumeClose();
2768 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002769 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002770}
Douglas Gregor6569d682009-05-27 23:11:45 +00002771
Douglas Gregordab60ad2010-10-01 18:44:50 +00002772/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2773/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002774TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002775 assert(Tok.is(tok::arrow) && "expected arrow");
2776
2777 ConsumeToken();
2778
Richard Smith7796eb52012-03-12 08:56:40 +00002779 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002780}
2781
Douglas Gregor6569d682009-05-27 23:11:45 +00002782/// \brief We have just started parsing the definition of a new class,
2783/// so push that class onto our stack of classes that is currently
2784/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002785Sema::ParsingClassState
John McCalle402e722012-09-25 07:32:39 +00002786Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
2787 bool IsInterface) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002788 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002789 "Nested class without outer class");
John McCalle402e722012-09-25 07:32:39 +00002790 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCalleee1d542011-02-14 07:13:47 +00002791 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002792}
2793
2794/// \brief Deallocate the given parsed class and all of its nested
2795/// classes.
2796void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002797 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2798 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002799 delete Class;
2800}
2801
2802/// \brief Pop the top class of the stack of classes that are
2803/// currently being parsed.
2804///
2805/// This routine should be called when we have finished parsing the
2806/// definition of a class, but have not yet popped the Scope
2807/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00002808void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002809 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002810
John McCalleee1d542011-02-14 07:13:47 +00002811 Actions.PopParsingClass(state);
2812
Douglas Gregor6569d682009-05-27 23:11:45 +00002813 ParsingClass *Victim = ClassStack.top();
2814 ClassStack.pop();
2815 if (Victim->TopLevelClass) {
2816 // Deallocate all of the nested classes of this class,
2817 // recursively: we don't need to keep any of this information.
2818 DeallocateParsedClasses(Victim);
2819 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002820 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002821 assert(!ClassStack.empty() && "Missing top-level class?");
2822
Douglas Gregord54eb442010-10-12 16:25:54 +00002823 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002824 // The victim is a nested class, but we will not need to perform
2825 // any processing after the definition of this class since it has
2826 // no members whose handling was delayed. Therefore, we can just
2827 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002828 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002829 return;
2830 }
2831
2832 // This nested class has some members that will need to be processed
2833 // after the top-level class is completely defined. Therefore, add
2834 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002835 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002836 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002837 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00002838}
Sean Huntbbd37c62009-11-21 08:43:09 +00002839
Richard Smithc56298d2012-04-10 03:25:07 +00002840/// \brief Try to parse an 'identifier' which appears within an attribute-token.
2841///
2842/// \return the parsed identifier on success, and 0 if the next token is not an
2843/// attribute-token.
2844///
2845/// C++11 [dcl.attr.grammar]p3:
2846/// If a keyword or an alternative token that satisfies the syntactic
2847/// requirements of an identifier is contained in an attribute-token,
2848/// it is considered an identifier.
2849IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
2850 switch (Tok.getKind()) {
2851 default:
2852 // Identifiers and keywords have identifier info attached.
2853 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
2854 Loc = ConsumeToken();
2855 return II;
2856 }
2857 return 0;
2858
2859 case tok::ampamp: // 'and'
2860 case tok::pipe: // 'bitor'
2861 case tok::pipepipe: // 'or'
2862 case tok::caret: // 'xor'
2863 case tok::tilde: // 'compl'
2864 case tok::amp: // 'bitand'
2865 case tok::ampequal: // 'and_eq'
2866 case tok::pipeequal: // 'or_eq'
2867 case tok::caretequal: // 'xor_eq'
2868 case tok::exclaim: // 'not'
2869 case tok::exclaimequal: // 'not_eq'
2870 // Alternative tokens do not have identifier info, but their spelling
2871 // starts with an alphabetical character.
2872 llvm::SmallString<8> SpellingBuf;
2873 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
2874 if (std::isalpha(Spelling[0])) {
2875 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00002876 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00002877 }
2878 return 0;
2879 }
2880}
2881
Michael Han6880f492012-10-03 01:56:22 +00002882static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
2883 IdentifierInfo *ScopeName) {
2884 switch (AttributeList::getKind(AttrName, ScopeName,
2885 AttributeList::AS_CXX11)) {
2886 case AttributeList::AT_CarriesDependency:
2887 case AttributeList::AT_FallThrough:
2888 case AttributeList::AT_NoReturn: {
2889 return true;
2890 }
2891
2892 default:
2893 return false;
2894 }
2895}
2896
Richard Smithc56298d2012-04-10 03:25:07 +00002897/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002898/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00002899///
Richard Smith6ee326a2012-04-10 01:32:12 +00002900/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00002901/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002902/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00002903///
Richard Smith6ee326a2012-04-10 01:32:12 +00002904/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00002905/// attribute[opt]
2906/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00002907/// attribute '...'
2908/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00002909///
Richard Smith6ee326a2012-04-10 01:32:12 +00002910/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002911/// attribute-token attribute-argument-clause[opt]
2912///
Richard Smith6ee326a2012-04-10 01:32:12 +00002913/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00002914/// identifier
2915/// attribute-scoped-token
2916///
Richard Smith6ee326a2012-04-10 01:32:12 +00002917/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00002918/// attribute-namespace '::' identifier
2919///
Richard Smith6ee326a2012-04-10 01:32:12 +00002920/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00002921/// identifier
2922///
Richard Smith6ee326a2012-04-10 01:32:12 +00002923/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00002924/// '(' balanced-token-seq ')'
2925///
Richard Smith6ee326a2012-04-10 01:32:12 +00002926/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00002927/// balanced-token
2928/// balanced-token-seq balanced-token
2929///
Richard Smith6ee326a2012-04-10 01:32:12 +00002930/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00002931/// '(' balanced-token-seq ')'
2932/// '[' balanced-token-seq ']'
2933/// '{' balanced-token-seq '}'
2934/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00002935void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002936 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002937 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00002938 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002939 ParseAlignmentSpecifier(attrs, endLoc);
2940 return;
2941 }
2942
Sean Huntbbd37c62009-11-21 08:43:09 +00002943 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00002944 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00002945
Richard Smith41be6732011-10-14 20:48:27 +00002946 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
2947
Sean Huntbbd37c62009-11-21 08:43:09 +00002948 ConsumeBracket();
2949 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002950
Richard Smithc56298d2012-04-10 03:25:07 +00002951 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002952 // attribute not present
2953 if (Tok.is(tok::comma)) {
2954 ConsumeToken();
2955 continue;
2956 }
2957
Richard Smithc56298d2012-04-10 03:25:07 +00002958 SourceLocation ScopeLoc, AttrLoc;
2959 IdentifierInfo *ScopeName = 0, *AttrName = 0;
2960
2961 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
2962 if (!AttrName)
2963 // Break out to the "expected ']'" diagnostic.
2964 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002965
Sean Huntbbd37c62009-11-21 08:43:09 +00002966 // scoped attribute
2967 if (Tok.is(tok::coloncolon)) {
2968 ConsumeToken();
2969
Richard Smithc56298d2012-04-10 03:25:07 +00002970 ScopeName = AttrName;
2971 ScopeLoc = AttrLoc;
2972
2973 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
2974 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002975 Diag(Tok.getLocation(), diag::err_expected_ident);
2976 SkipUntil(tok::r_square, tok::comma, true, true);
2977 continue;
2978 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002979 }
2980
Michael Han6880f492012-10-03 01:56:22 +00002981 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Sean Huntbbd37c62009-11-21 08:43:09 +00002982 bool AttrParsed = false;
Sean Huntbbd37c62009-11-21 08:43:09 +00002983
Michael Han6880f492012-10-03 01:56:22 +00002984 // Parse attribute arguments
2985 if (Tok.is(tok::l_paren)) {
2986 if (ScopeName && ScopeName->getName() == "gnu") {
2987 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
2988 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
2989 AttrParsed = true;
2990 } else {
2991 if (StandardAttr)
2992 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
2993 << AttrName->getName();
2994
2995 // FIXME: handle other formats of c++11 attribute arguments
2996 ConsumeParen();
2997 SkipUntil(tok::r_paren, false);
2998 }
2999 }
3000
3001 if (!AttrParsed)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00003002 attrs.addNew(AttrName,
3003 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3004 AttrLoc),
3005 ScopeName, ScopeLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00003006 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smith6ee326a2012-04-10 01:32:12 +00003007
Richard Smithc56298d2012-04-10 03:25:07 +00003008 if (Tok.is(tok::ellipsis)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003009 ConsumeToken();
Michael Han6880f492012-10-03 01:56:22 +00003010
3011 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3012 << AttrName->getName();
Richard Smithc56298d2012-04-10 03:25:07 +00003013 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003014 }
3015
3016 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3017 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003018 if (endLoc)
3019 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00003020 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3021 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003022}
Sean Huntbbd37c62009-11-21 08:43:09 +00003023
Sean Hunt2edf0a22012-06-23 05:07:58 +00003024/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003025///
3026/// attribute-specifier-seq:
3027/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00003028void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003029 SourceLocation *endLoc) {
3030 SourceLocation StartLoc = Tok.getLocation(), Loc;
3031 if (!endLoc)
3032 endLoc = &Loc;
3033
Douglas Gregor8828ee72011-10-07 20:35:25 +00003034 do {
Richard Smithc56298d2012-04-10 03:25:07 +00003035 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003036 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003037
3038 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003039}
3040
Francois Pichet334d47e2010-10-11 12:59:39 +00003041/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3042///
3043/// [MS] ms-attribute:
3044/// '[' token-seq ']'
3045///
3046/// [MS] ms-attribute-seq:
3047/// ms-attribute[opt]
3048/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003049void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3050 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003051 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3052
3053 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003054 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003055 ConsumeBracket();
3056 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003057 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003058 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3059 }
3060}
Francois Pichet563a6452011-05-25 10:19:49 +00003061
3062void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3063 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003064 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003065 if (ParseMicrosoftIfExistsCondition(Result))
3066 return;
3067
Douglas Gregor3896fc52011-10-24 22:31:10 +00003068 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3069 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003070 Diag(Tok, diag::err_expected_lbrace);
3071 return;
3072 }
Francois Pichet563a6452011-05-25 10:19:49 +00003073
Douglas Gregor3896fc52011-10-24 22:31:10 +00003074 switch (Result.Behavior) {
3075 case IEB_Parse:
3076 // Parse the declarations below.
3077 break;
3078
3079 case IEB_Dependent:
3080 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3081 << Result.IsIfExists;
3082 // Fall through to skip.
3083
3084 case IEB_Skip:
3085 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003086 return;
3087 }
3088
Douglas Gregor3896fc52011-10-24 22:31:10 +00003089 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003090 // __if_exists, __if_not_exists can nest.
3091 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3092 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3093 continue;
3094 }
3095
3096 // Check for extraneous top-level semicolon.
3097 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003098 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003099 continue;
3100 }
3101
3102 AccessSpecifier AS = getAccessSpecifierIfPresent();
3103 if (AS != AS_none) {
3104 // Current token is a C++ access specifier.
3105 CurAS = AS;
3106 SourceLocation ASLoc = Tok.getLocation();
3107 ConsumeToken();
3108 if (Tok.is(tok::colon))
3109 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3110 else
3111 Diag(Tok, diag::err_expected_colon);
3112 ConsumeToken();
3113 continue;
3114 }
3115
3116 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003117 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003118 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003119
3120 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003121}