blob: 492250da1d49ac7df8337e5487489963de41cd6d [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()) {
David Blaikie42d6d0c2011-12-04 05:04:18 +0000699 DS.SetTypeSpecError();
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000700 if (SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true)) {
701 EndLoc = ConsumeParen();
702 } else {
703 assert(Tok.is(tok::semi));
704 if (PP.isBacktrackEnabled()) {
705 // Backtrack to get the location of the last token before the semi.
706 PP.RevertCachedTokens(2);
707 ConsumeToken(); // the semi.
708 EndLoc = ConsumeAnyToken();
709 assert(Tok.is(tok::semi));
710 } else {
711 EndLoc = Tok.getLocation();
712 }
713 }
714 return EndLoc;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000715 }
716
717 // Match the ')'
718 T.consumeClose();
719 if (T.getCloseLocation().isInvalid()) {
720 DS.SetTypeSpecError();
721 // FIXME: this should return the location of the last token
722 // that was consumed (by "consumeClose()")
723 return T.getCloseLocation();
724 }
725
Richard Smith76f3f692012-02-22 02:04:18 +0000726 Result = Actions.ActOnDecltypeExpression(Result.take());
727 if (Result.isInvalid()) {
728 DS.SetTypeSpecError();
729 return T.getCloseLocation();
730 }
731
David Blaikie42d6d0c2011-12-04 05:04:18 +0000732 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000733 }
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000735 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000736 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000737 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000738 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
David Blaikie42d6d0c2011-12-04 05:04:18 +0000739 DiagID, Result.release())) {
John McCallfec54012009-08-03 20:12:06 +0000740 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000741 DS.SetTypeSpecError();
742 }
743 return EndLoc;
744}
745
746void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
747 SourceLocation StartLoc,
748 SourceLocation EndLoc) {
749 // make sure we have a token we can turn into an annotation token
750 if (PP.isBacktrackEnabled())
751 PP.RevertCachedTokens(1);
752 else
753 PP.EnterToken(Tok);
754
755 Tok.setKind(tok::annot_decltype);
756 setExprAnnotation(Tok, DS.getTypeSpecType() == TST_decltype ?
757 DS.getRepAsExpr() : ExprResult());
758 Tok.setAnnotationEndLoc(EndLoc);
759 Tok.setLocation(StartLoc);
760 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000761}
762
Sean Huntdb5d44b2011-05-19 05:37:45 +0000763void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
764 assert(Tok.is(tok::kw___underlying_type) &&
765 "Not an underlying type specifier");
766
767 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000768 BalancedDelimiterTracker T(*this, tok::l_paren);
769 if (T.expectAndConsume(diag::err_expected_lparen_after,
770 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000771 return;
772 }
773
774 TypeResult Result = ParseTypeName();
775 if (Result.isInvalid()) {
776 SkipUntil(tok::r_paren);
777 return;
778 }
779
780 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000781 T.consumeClose();
782 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000783 return;
784
785 const char *PrevSpec = 0;
786 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000787 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000788 DiagID, Result.release()))
789 Diag(StartLoc, DiagID) << PrevSpec;
790}
791
David Blaikie09048df2011-10-25 15:01:20 +0000792/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
793/// class name or decltype-specifier. Note that we only check that the result
794/// names a type; semantic analysis will need to verify that the type names a
795/// class. The result is either a type or null, depending on whether a type
796/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000797///
David Blaikie09048df2011-10-25 15:01:20 +0000798/// base-type-specifier: [C++ 10.1]
799/// class-or-decltype
800/// class-or-decltype: [C++ 10.1]
801/// nested-name-specifier[opt] class-name
802/// decltype-specifier
Douglas Gregor42a552f2008-11-05 20:51:48 +0000803/// class-name: [C++ 9.1]
804/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000805/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000806///
David Blaikie22216eb2011-10-25 17:10:12 +0000807Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
808 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000809 // Ignore attempts to use typename
810 if (Tok.is(tok::kw_typename)) {
811 Diag(Tok, diag::err_expected_class_name_not_template)
812 << FixItHint::CreateRemoval(Tok.getLocation());
813 ConsumeToken();
814 }
815
David Blaikie152aa4b2011-10-25 18:17:58 +0000816 // Parse optional nested-name-specifier
817 CXXScopeSpec SS;
818 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
819
820 BaseLoc = Tok.getLocation();
821
David Blaikie22216eb2011-10-25 17:10:12 +0000822 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000823 // tok == kw_decltype is just error recovery, it can only happen when SS
824 // isn't empty
825 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000826 if (SS.isNotEmpty())
827 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
828 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000829 // Fake up a Declarator to use with ActOnTypeName.
830 DeclSpec DS(AttrFactory);
831
David Blaikieb5777572011-12-08 04:53:15 +0000832 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000833
834 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
835 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
836 }
837
Douglas Gregor7f43d672009-02-25 23:52:28 +0000838 // Check whether we have a template-id that names a type.
839 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000840 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000841 if (TemplateId->Kind == TNK_Type_template ||
842 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000843 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000844
845 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000846 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000847 EndLocation = Tok.getAnnotationEndLoc();
848 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000849
850 if (Type)
851 return Type;
852 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000853 }
854
855 // Fall through to produce an error below.
856 }
857
Douglas Gregor42a552f2008-11-05 20:51:48 +0000858 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000859 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000860 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000861 }
862
Douglas Gregor84d0a192010-01-12 21:28:44 +0000863 IdentifierInfo *Id = Tok.getIdentifierInfo();
864 SourceLocation IdLoc = ConsumeToken();
865
866 if (Tok.is(tok::less)) {
867 // It looks the user intended to write a template-id here, but the
868 // template-name was wrong. Try to fix that.
869 TemplateNameKind TNK = TNK_Type_template;
870 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000871 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000872 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000873 Diag(IdLoc, diag::err_unknown_template_name)
874 << Id;
875 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000876
Douglas Gregor84d0a192010-01-12 21:28:44 +0000877 if (!Template)
878 return true;
879
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000880 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000881 UnqualifiedId TemplateName;
882 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000883
Douglas Gregor84d0a192010-01-12 21:28:44 +0000884 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000885 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
886 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000887 return true;
888 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000889 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000890
Douglas Gregor84d0a192010-01-12 21:28:44 +0000891 // If we didn't end up with a typename token, there's nothing more we
892 // can do.
893 if (Tok.isNot(tok::annot_typename))
894 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000895
Douglas Gregor84d0a192010-01-12 21:28:44 +0000896 // Retrieve the type from the annotation token, consume that token, and
897 // return.
898 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000899 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000900 ConsumeToken();
901 return Type;
902 }
903
Douglas Gregor42a552f2008-11-05 20:51:48 +0000904 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000905 IdentifierInfo *CorrectedII = 0;
Douglas Gregor059101f2011-03-02 00:47:37 +0000906 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000907 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000908 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000909 /*NonTrivialTypeSourceInfo=*/true,
910 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000911 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000912 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000913 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000914 }
915
916 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000917 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000918
919 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000920 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000921 DS.SetRangeStart(IdLoc);
922 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000923 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000924
925 const char *PrevSpec = 0;
926 unsigned DiagID;
927 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
928
929 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
930 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000931}
932
John McCallc052dbb2012-05-22 21:28:12 +0000933void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
934 while (Tok.is(tok::kw___single_inheritance) ||
935 Tok.is(tok::kw___multiple_inheritance) ||
936 Tok.is(tok::kw___virtual_inheritance)) {
937 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
938 SourceLocation AttrNameLoc = ConsumeToken();
939 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000940 SourceLocation(), 0, 0, AttributeList::AS_GNU);
John McCallc052dbb2012-05-22 21:28:12 +0000941 }
942}
943
Richard Smithc9f35172012-06-25 21:37:02 +0000944/// Determine whether the following tokens are valid after a type-specifier
945/// which could be a standalone declaration. This will conservatively return
946/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith139be702012-07-02 19:14:01 +0000947bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smithc9f35172012-06-25 21:37:02 +0000948 // This switch enumerates the valid "follow" set for type-specifiers.
949 switch (Tok.getKind()) {
950 default: break;
951 case tok::semi: // struct foo {...} ;
952 case tok::star: // struct foo {...} * P;
953 case tok::amp: // struct foo {...} & R = ...
954 case tok::identifier: // struct foo {...} V ;
955 case tok::r_paren: //(struct foo {...} ) {4}
956 case tok::annot_cxxscope: // struct foo {...} a:: b;
957 case tok::annot_typename: // struct foo {...} a ::b;
958 case tok::annot_template_id: // struct foo {...} a<int> ::b;
959 case tok::l_paren: // struct foo {...} ( x);
960 case tok::comma: // __builtin_offsetof(struct foo{...} ,
961 return true;
Richard Smith139be702012-07-02 19:14:01 +0000962 case tok::colon:
963 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smithc9f35172012-06-25 21:37:02 +0000964 // Type qualifiers
965 case tok::kw_const: // struct foo {...} const x;
966 case tok::kw_volatile: // struct foo {...} volatile x;
967 case tok::kw_restrict: // struct foo {...} restrict x;
968 case tok::kw_inline: // struct foo {...} inline foo() {};
969 // Storage-class specifiers
970 case tok::kw_static: // struct foo {...} static x;
971 case tok::kw_extern: // struct foo {...} extern x;
972 case tok::kw_typedef: // struct foo {...} typedef x;
973 case tok::kw_register: // struct foo {...} register x;
974 case tok::kw_auto: // struct foo {...} auto x;
975 case tok::kw_mutable: // struct foo {...} mutable x;
976 case tok::kw_constexpr: // struct foo {...} constexpr x;
977 // As shown above, type qualifiers and storage class specifiers absolutely
978 // can occur after class specifiers according to the grammar. However,
979 // almost no one actually writes code like this. If we see one of these,
980 // it is much more likely that someone missed a semi colon and the
981 // type/storage class specifier we're seeing is part of the *next*
982 // intended declaration, as in:
983 //
984 // struct foo { ... }
985 // typedef int X;
986 //
987 // We'd really like to emit a missing semicolon error instead of emitting
988 // an error on the 'int' saying that you can't have two type specifiers in
989 // the same declaration of X. Because of this, we look ahead past this
990 // token to see if it's a type specifier. If so, we know the code is
991 // otherwise invalid, so we can produce the expected semi error.
992 if (!isKnownToBeTypeSpecifier(NextToken()))
993 return true;
994 break;
995 case tok::r_brace: // struct bar { struct foo {...} }
996 // Missing ';' at end of struct is accepted as an extension in C mode.
997 if (!getLangOpts().CPlusPlus)
998 return true;
999 break;
1000 }
1001 return false;
1002}
1003
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001004/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1005/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1006/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +00001007/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001008///
1009/// class-specifier: [C++ class]
1010/// class-head '{' member-specification[opt] '}'
1011/// class-head '{' member-specification[opt] '}' attributes[opt]
1012/// class-head:
1013/// class-key identifier[opt] base-clause[opt]
1014/// class-key nested-name-specifier identifier base-clause[opt]
1015/// class-key nested-name-specifier[opt] simple-template-id
1016/// base-clause[opt]
1017/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001018/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001019/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001020/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001021/// simple-template-id base-clause[opt]
1022/// class-key:
1023/// 'class'
1024/// 'struct'
1025/// 'union'
1026///
1027/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +00001028/// class-key ::[opt] nested-name-specifier[opt] identifier
1029/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1030/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001031///
1032/// Note that the C++ class-specifier and elaborated-type-specifier,
1033/// together, subsume the C99 struct-or-union-specifier:
1034///
1035/// struct-or-union-specifier: [C99 6.7.2.1]
1036/// struct-or-union identifier[opt] '{' struct-contents '}'
1037/// struct-or-union identifier
1038/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1039/// '}' attributes[opt]
1040/// [GNU] struct-or-union attributes[opt] identifier
1041/// struct-or-union:
1042/// 'struct'
1043/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +00001044void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1045 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001046 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001047 AccessSpecifier AS,
Richard Smith69730c12012-03-12 07:56:15 +00001048 bool EnteringContext, DeclSpecContext DSC) {
Joao Matos17d35c32012-08-31 22:18:20 +00001049 DeclSpec::TST TagType;
1050 if (TagTokKind == tok::kw_struct)
1051 TagType = DeclSpec::TST_struct;
1052 else if (TagTokKind == tok::kw___interface)
1053 TagType = DeclSpec::TST_interface;
1054 else if (TagTokKind == tok::kw_class)
1055 TagType = DeclSpec::TST_class;
1056 else {
Chris Lattner4c97d762009-04-12 21:49:30 +00001057 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1058 TagType = DeclSpec::TST_union;
1059 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001060
Douglas Gregor374929f2009-09-18 15:37:17 +00001061 if (Tok.is(tok::code_completion)) {
1062 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001063 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001064 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00001065 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001066
Chandler Carruth926c4b42010-06-28 08:39:25 +00001067 // C++03 [temp.explicit] 14.7.2/8:
1068 // The usual access checking rules do not apply to names used to specify
1069 // explicit instantiations.
1070 //
1071 // As an extension we do not perform access checking on the names used to
1072 // specify explicit specializations either. This is important to allow
1073 // specializing traits classes for private types.
John McCall13489672012-05-07 06:16:58 +00001074 //
1075 // Note that we don't suppress if this turns out to be an elaborated
1076 // type specifier.
1077 bool shouldDelayDiagsInTag =
1078 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1079 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1080 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001081
Sean Hunt2edf0a22012-06-23 05:07:58 +00001082 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001083 // If attributes exist after tag, parse them.
1084 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +00001085 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001086
Steve Narofff59e17e2008-12-24 20:59:21 +00001087 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001088 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001089 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001090
John McCallc052dbb2012-05-22 21:28:12 +00001091 // Parse inheritance specifiers.
1092 if (Tok.is(tok::kw___single_inheritance) ||
1093 Tok.is(tok::kw___multiple_inheritance) ||
1094 Tok.is(tok::kw___virtual_inheritance))
1095 ParseMicrosoftInheritanceClassAttributes(attrs);
1096
Sean Huntbbd37c62009-11-21 08:43:09 +00001097 // If C++0x attributes exist here, parse them.
1098 // FIXME: Are we consistent with the ordering of parsing of different
1099 // styles of attributes?
John McCall7f040a92010-12-24 02:08:15 +00001100 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001101
John Wiegley20c0da72011-04-27 23:09:49 +00001102 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +00001103 !Tok.is(tok::identifier) &&
1104 Tok.getIdentifierInfo() &&
1105 (Tok.is(tok::kw___is_arithmetic) ||
1106 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001107 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001108 Tok.is(tok::kw___is_floating_point) ||
1109 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001110 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001111 Tok.is(tok::kw___is_integral) ||
1112 Tok.is(tok::kw___is_member_function_pointer) ||
1113 Tok.is(tok::kw___is_member_pointer) ||
1114 Tok.is(tok::kw___is_pod) ||
1115 Tok.is(tok::kw___is_pointer) ||
1116 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001117 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001118 Tok.is(tok::kw___is_signed) ||
1119 Tok.is(tok::kw___is_unsigned) ||
1120 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001121 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001122 // name of struct templates, but some are keywords in GCC >= 4.3
1123 // and Clang. Therefore, when we see the token sequence "struct
1124 // X", make X into a normal identifier rather than a keyword, to
1125 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001126 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001127 Tok.setKind(tok::identifier);
1128 }
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001130 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001131 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001132 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001133 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1134 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001135
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001136 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001137 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001138 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001139 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1140 Diag(Tok, diag::err_expected_ident);
1141 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001142
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001143 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1144
Douglas Gregorcc636682009-02-17 23:15:12 +00001145 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001146 IdentifierInfo *Name = 0;
1147 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001148 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001149 if (Tok.is(tok::identifier)) {
1150 Name = Tok.getIdentifierInfo();
1151 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001152
David Blaikie4e4d0842012-03-11 07:00:24 +00001153 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001154 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001155 // Eat the template argument list and try to continue parsing this as
1156 // a class (or template thereof).
1157 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001158 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001159 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001160 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001161 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001162 // We couldn't parse the template argument list at all, so don't
1163 // try to give any location information for the list.
1164 LAngleLoc = RAngleLoc = SourceLocation();
1165 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001166
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001167 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matos17d35c32012-08-31 22:18:20 +00001168 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1169 << (TagType == DeclSpec::TST_class? 0
1170 : TagType == DeclSpec::TST_struct? 1
1171 : TagType == DeclSpec::TST_interface? 2
1172 : 3)
1173 << Name
1174 << SourceRange(LAngleLoc, RAngleLoc);
1175
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001176 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001177 // we've removed its template argument list.
1178 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1179 if (TemplateParams && TemplateParams->size() > 1) {
1180 TemplateParams->pop_back();
1181 } else {
1182 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001183 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001184 = ParsedTemplateInfo::NonTemplate;
1185 }
1186 } else if (TemplateInfo.Kind
1187 == ParsedTemplateInfo::ExplicitInstantiation) {
1188 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001189 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001190 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001191 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001192 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001193 = SourceLocation();
1194 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1195 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001196 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001197 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001198 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001199 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001200 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001201
Douglas Gregor059101f2011-03-02 00:47:37 +00001202 if (TemplateId->Kind != TNK_Type_template &&
1203 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001204 // The template-name in the simple-template-id refers to
1205 // something other than a class template. Give an appropriate
1206 // error message and skip to the ';'.
1207 SourceRange Range(NameLoc);
1208 if (SS.isNotEmpty())
1209 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001210
Douglas Gregor39a8de12009-02-25 19:37:18 +00001211 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1212 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Douglas Gregor39a8de12009-02-25 19:37:18 +00001214 DS.SetTypeSpecError();
1215 SkipUntil(tok::semi, false, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001216 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001217 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001218 }
1219
Richard Smith7796eb52012-03-12 08:56:40 +00001220 // There are four options here.
1221 // - If we are in a trailing return type, this is always just a reference,
1222 // and we must not try to parse a definition. For instance,
1223 // [] () -> struct S { };
1224 // does not define a type.
1225 // - If we have 'struct foo {...', 'struct foo :...',
1226 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1227 // - If we have 'struct foo;', then this is either a forward declaration
1228 // or a friend declaration, which have to be treated differently.
1229 // - Otherwise we have something like 'struct foo xyz', a reference.
Richard Smith69730c12012-03-12 07:56:15 +00001230 // However, in type-specifier-seq's, things look like declarations but are
1231 // just references, e.g.
1232 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001233 // or
Richard Smith69730c12012-03-12 07:56:15 +00001234 // &T::operator struct s;
1235 // For these, DSC is DSC_type_specifier.
John McCallf312b1e2010-08-26 23:41:50 +00001236 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001237 if (DSC == DSC_trailing)
1238 TUK = Sema::TUK_Reference;
1239 else if (Tok.is(tok::l_brace) ||
1240 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
1241 (isCXX0XFinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001242 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001243 if (DS.isFriendSpecified()) {
1244 // C++ [class.friend]p2:
1245 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001246 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001247 << SourceRange(DS.getFriendSpecLoc());
1248
1249 // Skip everything up to the semicolon, so that this looks like a proper
1250 // friend class (or template thereof) declaration.
1251 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001252 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001253 } else {
1254 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001255 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001256 }
Richard Smithc9f35172012-06-25 21:37:02 +00001257 } else if (DSC != DSC_type_specifier &&
1258 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00001259 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallf312b1e2010-08-26 23:41:50 +00001260 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matos17d35c32012-08-31 22:18:20 +00001261 if (Tok.isNot(tok::semi)) {
1262 // A semicolon was missing after this declaration. Diagnose and recover.
1263 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1264 DeclSpec::getSpecifierName(TagType));
1265 PP.EnterToken(Tok);
1266 Tok.setKind(tok::semi);
1267 }
Richard Smithc9f35172012-06-25 21:37:02 +00001268 } else
John McCallf312b1e2010-08-26 23:41:50 +00001269 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001270
John McCall13489672012-05-07 06:16:58 +00001271 // If this is an elaborated type specifier, and we delayed
1272 // diagnostics before, just merge them into the current pool.
1273 if (shouldDelayDiagsInTag) {
1274 diagsFromTag.done();
1275 if (TUK == Sema::TUK_Reference)
1276 diagsFromTag.redelay();
1277 }
1278
John McCall207014e2010-07-30 06:26:29 +00001279 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001280 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001281 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1282 // We have a declaration or reference to an anonymous class.
1283 Diag(StartLoc, diag::err_anon_type_definition)
1284 << DeclSpec::getSpecifierName(TagType);
1285 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001286
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001287 SkipUntil(tok::comma, true);
1288 return;
1289 }
1290
Douglas Gregorddc29e12009-02-06 22:42:48 +00001291 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001292 DeclResult TagOrTempResult = true; // invalid
1293 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001294
Douglas Gregor402abb52009-05-28 23:31:59 +00001295 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001296 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001297 // Explicit specialization, class template partial specialization,
1298 // or explicit instantiation.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001299 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001300 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001301 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001302 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001303 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001304 ProhibitAttributes(attrs);
1305
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001306 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001307 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001308 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001309 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001310 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001311 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001312 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001313 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001314 TemplateId->TemplateNameLoc,
1315 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001316 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001317 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001318 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001319
1320 // Friend template-ids are treated as references unless
1321 // they have template headers, in which case they're ill-formed
1322 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1323 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001324 } else if (TUK == Sema::TUK_Reference ||
1325 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001326 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001327 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001328 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001329 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001330 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001331 TemplateId->Template,
1332 TemplateId->TemplateNameLoc,
1333 TemplateId->LAngleLoc,
1334 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001335 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001336 } else {
1337 // This is an explicit specialization or a class template
1338 // partial specialization.
1339 TemplateParameterLists FakedParamLists;
1340
1341 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1342 // This looks like an explicit instantiation, because we have
1343 // something like
1344 //
1345 // template class Foo<X>
1346 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001347 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001348 // meant to be an explicit specialization, but the user forgot
1349 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +00001350 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001351
Mike Stump1eb44332009-09-09 15:08:12 +00001352 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001353 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001354 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001355 diag::err_explicit_instantiation_with_definition)
1356 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +00001357 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001358
1359 // Create a fake template parameter list that contains only
1360 // "template<>", so that we treat this construct as a class
1361 // template specialization.
1362 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +00001363 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001364 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001365 LAngleLoc,
1366 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001367 LAngleLoc));
1368 TemplateParams = &FakedParamLists;
1369 }
1370
1371 // Build the class template specialization.
1372 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001373 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001374 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001375 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001376 TemplateId->TemplateNameLoc,
1377 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001378 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001379 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001380 attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001381 MultiTemplateParamsArg(
Douglas Gregorcc636682009-02-17 23:15:12 +00001382 TemplateParams? &(*TemplateParams)[0] : 0,
1383 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001384 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001385 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001386 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001387 // Explicit instantiation of a member of a class template
1388 // specialization, e.g.,
1389 //
1390 // template struct Outer<int>::Inner;
1391 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001392 ProhibitAttributes(attrs);
1393
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001394 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001395 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001396 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001397 TemplateInfo.TemplateLoc,
1398 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001399 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001400 } else if (TUK == Sema::TUK_Friend &&
1401 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001402 ProhibitAttributes(attrs);
1403
John McCall9a34edb2010-10-19 01:40:49 +00001404 TagOrTempResult =
1405 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1406 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001407 Name, NameLoc, attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001408 MultiTemplateParamsArg(
John McCall9a34edb2010-10-19 01:40:49 +00001409 TemplateParams? &(*TemplateParams)[0] : 0,
1410 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001411 } else {
1412 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001413 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001414 // FIXME: Diagnose this particular error.
1415 }
1416
Sean Hunt2edf0a22012-06-23 05:07:58 +00001417 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1418 ProhibitAttributes(attrs);
1419
John McCallc4e70192009-09-11 04:59:25 +00001420 bool IsDependent = false;
1421
John McCalla25c4082010-10-19 18:40:57 +00001422 // Don't pass down template parameter lists if this is just a tag
1423 // reference. For example, we don't need the template parameters here:
1424 // template <class T> class A *makeA(T t);
1425 MultiTemplateParamsArg TParams;
1426 if (TUK != Sema::TUK_Reference && TemplateParams)
1427 TParams =
1428 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1429
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001430 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001431 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001432 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001433 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001434 TParams, Owned, IsDependent,
1435 SourceLocation(), false,
1436 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001437
1438 // If ActOnTag said the type was dependent, try again with the
1439 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001440 if (IsDependent) {
1441 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001442 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001443 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001444 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001445 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001446
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001447 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001448 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001449 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001450 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001451 isCXX0XFinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001452 if (getLangOpts().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +00001453 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001454 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001455 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001456 }
1457
John McCallb3d87482010-08-24 05:47:05 +00001458 const char *PrevSpec = 0;
1459 unsigned DiagID;
1460 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001461 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001462 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1463 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001464 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001465 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001466 Result = DS.SetTypeSpecType(TagType, StartLoc,
1467 NameLoc.isValid() ? NameLoc : StartLoc,
1468 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001469 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001470 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001471 return;
1472 }
Mike Stump1eb44332009-09-09 15:08:12 +00001473
John McCallb3d87482010-08-24 05:47:05 +00001474 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001475 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001476
Chris Lattner4ed5d912010-02-02 01:23:29 +00001477 // At this point, we've successfully parsed a class-specifier in 'definition'
1478 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1479 // going to look at what comes after it to improve error recovery. If an
1480 // impossible token occurs next, we assume that the programmer forgot a ; at
1481 // the end of the declaration and recover that way.
1482 //
Richard Smithc9f35172012-06-25 21:37:02 +00001483 // Also enforce C++ [temp]p3:
1484 // In a template-declaration which defines a class, no declarator
1485 // is permitted.
Joao Matos17d35c32012-08-31 22:18:20 +00001486 if (TUK == Sema::TUK_Definition &&
1487 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
1488 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1489 DeclSpec::getSpecifierName(TagType));
1490 // Push this token back into the preprocessor and change our current token
1491 // to ';' so that the rest of the code recovers as though there were an
1492 // ';' after the definition.
Richard Smithc9f35172012-06-25 21:37:02 +00001493 PP.EnterToken(Tok);
1494 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001495 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001496}
1497
Mike Stump1eb44332009-09-09 15:08:12 +00001498/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001499///
1500/// base-clause : [C++ class.derived]
1501/// ':' base-specifier-list
1502/// base-specifier-list:
1503/// base-specifier '...'[opt]
1504/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001505void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001506 assert(Tok.is(tok::colon) && "Not a base clause");
1507 ConsumeToken();
1508
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001509 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001510 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001511
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001512 while (true) {
1513 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001514 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001515 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001516 // Skip the rest of this base specifier, up until the comma or
1517 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001518 SkipUntil(tok::comma, tok::l_brace, true, true);
1519 } else {
1520 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001521 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001522 }
1523
1524 // If the next token is a comma, consume it and keep reading
1525 // base-specifiers.
1526 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001528 // Consume the comma.
1529 ConsumeToken();
1530 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001531
1532 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001533 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001534}
1535
1536/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1537/// one entry in the base class list of a class specifier, for example:
1538/// class foo : public bar, virtual private baz {
1539/// 'public bar' and 'virtual private baz' are each base-specifiers.
1540///
1541/// base-specifier: [C++ class.derived]
1542/// ::[opt] nested-name-specifier[opt] class-name
1543/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001544/// base-type-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001545/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001546/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001547Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001548 bool IsVirtual = false;
1549 SourceLocation StartLoc = Tok.getLocation();
1550
1551 // Parse the 'virtual' keyword.
1552 if (Tok.is(tok::kw_virtual)) {
1553 ConsumeToken();
1554 IsVirtual = true;
1555 }
1556
1557 // Parse an (optional) access specifier.
1558 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001559 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001560 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001562 // Parse the 'virtual' keyword (again!), in case it came after the
1563 // access specifier.
1564 if (Tok.is(tok::kw_virtual)) {
1565 SourceLocation VirtualLoc = ConsumeToken();
1566 if (IsVirtual) {
1567 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001568 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001569 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001570 }
1571
1572 IsVirtual = true;
1573 }
1574
Douglas Gregor42a552f2008-11-05 20:51:48 +00001575 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001576 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001577 SourceLocation BaseLoc;
1578 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001579 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001580 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001582 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1583 // actually part of the base-specifier-list grammar productions, but we
1584 // parse it here for convenience.
1585 SourceLocation EllipsisLoc;
1586 if (Tok.is(tok::ellipsis))
1587 EllipsisLoc = ConsumeToken();
1588
Mike Stump1eb44332009-09-09 15:08:12 +00001589 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001590 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001591
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001592 // Notify semantic analysis that we have parsed a complete
1593 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001594 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001595 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001596}
1597
1598/// getAccessSpecifierIfPresent - Determine whether the next token is
1599/// a C++ access-specifier.
1600///
1601/// access-specifier: [C++ class.derived]
1602/// 'private'
1603/// 'protected'
1604/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001605AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001606 switch (Tok.getKind()) {
1607 default: return AS_none;
1608 case tok::kw_private: return AS_private;
1609 case tok::kw_protected: return AS_protected;
1610 case tok::kw_public: return AS_public;
1611 }
1612}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001613
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001614/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001615/// delayed, e.g., default arguments, create a late-parsed method declaration
1616/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001617void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1618 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001619 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001620 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001621 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001622 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001623 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001624
Eli Friedmand33133c2009-07-22 21:45:50 +00001625 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1626 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1627 if (!LateMethod) {
1628 // Push this method onto the stack of late-parsed method
1629 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001630 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1631 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001632 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001633
1634 // Add all of the parameters prior to this one (they don't
1635 // have default arguments).
1636 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1637 for (unsigned I = 0; I < ParamIdx; ++I)
1638 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001639 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001640 }
1641
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001642 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001643 // not have a default argument).
1644 LateMethod->DefaultArgs.push_back(
1645 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1646 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1647 }
1648 }
1649}
1650
Richard Smith1c94c162012-01-09 22:31:44 +00001651/// isCXX0XVirtSpecifier - Determine whether the given token is a C++0x
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001652/// virt-specifier.
1653///
1654/// virt-specifier:
1655/// override
1656/// final
Richard Smith1c94c162012-01-09 22:31:44 +00001657VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001658 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001659 return VirtSpecifiers::VS_None;
1660
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001661 if (Tok.is(tok::identifier)) {
1662 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001663
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001664 // Initialize the contextual keywords.
1665 if (!Ident_final) {
1666 Ident_final = &PP.getIdentifierTable().get("final");
1667 Ident_override = &PP.getIdentifierTable().get("override");
1668 }
1669
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001670 if (II == Ident_override)
1671 return VirtSpecifiers::VS_Override;
1672
1673 if (II == Ident_final)
1674 return VirtSpecifiers::VS_Final;
1675 }
1676
1677 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001678}
1679
1680/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1681///
1682/// virt-specifier-seq:
1683/// virt-specifier
1684/// virt-specifier-seq virt-specifier
John McCalle402e722012-09-25 07:32:39 +00001685void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS,
1686 bool IsInterface) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001687 while (true) {
Anders Carlssoncc54d592011-01-22 16:56:46 +00001688 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001689 if (Specifier == VirtSpecifiers::VS_None)
1690 return;
1691
1692 // C++ [class.mem]p8:
1693 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001694 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001695 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001696 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1697 << PrevSpec
1698 << FixItHint::CreateRemoval(Tok.getLocation());
1699
John McCalle402e722012-09-25 07:32:39 +00001700 if (IsInterface && Specifier == VirtSpecifiers::VS_Final) {
1701 Diag(Tok.getLocation(), diag::err_override_control_interface)
1702 << VirtSpecifiers::getSpecifierName(Specifier);
1703 } else {
1704 Diag(Tok.getLocation(), getLangOpts().CPlusPlus0x ?
1705 diag::warn_cxx98_compat_override_control_keyword :
1706 diag::ext_override_control_keyword)
1707 << VirtSpecifiers::getSpecifierName(Specifier);
1708 }
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001709 ConsumeToken();
1710 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001711}
1712
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001713/// isCXX0XFinalKeyword - Determine whether the next token is a C++0x
1714/// contextual 'final' keyword.
1715bool Parser::isCXX0XFinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001716 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001717 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001718
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001719 if (!Tok.is(tok::identifier))
1720 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001721
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001722 // Initialize the contextual keywords.
1723 if (!Ident_final) {
1724 Ident_final = &PP.getIdentifierTable().get("final");
1725 Ident_override = &PP.getIdentifierTable().get("override");
1726 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001727
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001728 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001729}
1730
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001731/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1732///
1733/// member-declaration:
1734/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1735/// function-definition ';'[opt]
1736/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1737/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001738/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001739/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001740/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001741///
1742/// member-declarator-list:
1743/// member-declarator
1744/// member-declarator-list ',' member-declarator
1745///
1746/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001747/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001748/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001749/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001750/// identifier[opt] ':' constant-expression
1751///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001752/// virt-specifier-seq:
1753/// virt-specifier
1754/// virt-specifier-seq virt-specifier
1755///
1756/// virt-specifier:
1757/// override
1758/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001759///
Sebastian Redle2b68332009-04-12 17:16:29 +00001760/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001761/// '= 0'
1762///
1763/// constant-initializer:
1764/// '=' constant-expression
1765///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001766void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001767 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001768 const ParsedTemplateInfo &TemplateInfo,
1769 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001770 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001771 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001772 Diag(Tok, diag::err_at_defs_cxx);
1773 else
1774 Diag(Tok, diag::err_at_in_class);
1775
1776 ConsumeToken();
1777 SkipUntil(tok::r_brace);
1778 return;
1779 }
1780
John McCall60fa3cf2009-12-11 02:10:03 +00001781 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001782 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001783 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001784 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1785 if (TryAnnotateCXXScopeToken())
1786 MalformedTypeSpec = true;
1787
1788 bool isAccessDecl;
1789 if (Tok.isNot(tok::annot_cxxscope))
1790 isAccessDecl = false;
1791 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001792 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1793 else
1794 isAccessDecl = NextToken().is(tok::kw_operator);
1795
1796 if (isAccessDecl) {
1797 // Collect the scope specifier token we annotated earlier.
1798 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001799 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1800 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001801
1802 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001803 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001804 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001805 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1806 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001807 SkipUntil(tok::semi);
1808 return;
1809 }
1810
1811 // TODO: recover from mistakenly-qualified operator declarations.
1812 if (ExpectAndConsume(tok::semi,
1813 diag::err_expected_semi_after,
1814 "access declaration",
1815 tok::semi))
1816 return;
1817
Douglas Gregor23c94db2010-07-02 17:43:08 +00001818 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001819 false, SourceLocation(),
1820 SS, Name,
1821 /* AttrList */ 0,
1822 /* IsTypeName */ false,
1823 SourceLocation());
1824 return;
1825 }
1826 }
1827
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001828 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001829 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001830 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001831 SourceLocation DeclEnd;
1832 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001833 return;
1834 }
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Chris Lattner682bf922009-03-29 16:50:03 +00001836 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001837 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001838 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001839 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001840 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001841 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001842 return;
1843 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001844
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001845 // Handle: member-declaration ::= '__extension__' member-declaration
1846 if (Tok.is(tok::kw___extension__)) {
1847 // __extension__ silences extension warnings in the subexpression.
1848 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1849 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001850 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1851 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001852 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001853
Chris Lattner4ed5d912010-02-02 01:23:29 +00001854 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1855 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001856 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001857
John McCall0b7e6782011-03-24 11:26:52 +00001858 ParsedAttributesWithRange attrs(AttrFactory);
Sean Huntbbd37c62009-11-21 08:43:09 +00001859 // Optional C++0x attribute-specifier
John McCall7f040a92010-12-24 02:08:15 +00001860 MaybeParseCXX0XAttributes(attrs);
1861 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001862
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001863 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00001864 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001865
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001866 // Eat 'using'.
1867 SourceLocation UsingLoc = ConsumeToken();
1868
1869 if (Tok.is(tok::kw_namespace)) {
1870 Diag(UsingLoc, diag::err_using_namespace_in_class);
1871 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001872 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001873 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001874 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00001875 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1876 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001877 }
1878 return;
1879 }
1880
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001881 // Hold late-parsed attributes so we can attach a Decl to them later.
1882 LateParsedAttrList CommonLateParsedAttrs;
1883
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001884 // decl-specifier-seq:
1885 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001886 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00001887 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00001888 if (MalformedTypeSpec)
1889 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001890 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
1891 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001892
Benjamin Kramer5354e772012-08-23 23:38:35 +00001893 MultiTemplateParamsArg TemplateParams(
John McCalldd4a3b02009-09-16 22:47:08 +00001894 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1895 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1896
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001897 if (Tok.is(tok::semi)) {
1898 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001899 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00001900 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00001901 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001902 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001903 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001904
John McCall54abf7d2009-11-04 02:18:39 +00001905 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00001906 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001907
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001908 // Hold late-parsed attributes so we can attach a Decl to them later.
1909 LateParsedAttrList LateParsedAttrs;
1910
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001911 SourceLocation EqualLoc;
1912 bool HasInitializer = false;
1913 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001914 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001915 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1916 ColonProtectionRAIIObject X(*this);
1917
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001918 // Parse the first declarator.
1919 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00001920 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001921 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001922 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00001923 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001924 if (Tok.is(tok::semi))
1925 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001926 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001927 }
1928
John McCalle402e722012-09-25 07:32:39 +00001929 ParseOptionalCXX0XVirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber48673472011-01-28 06:07:34 +00001930
John Thompson1b2fc0f2009-11-25 22:58:06 +00001931 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001932 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00001933
Francois Pichet6a247472011-05-11 02:14:46 +00001934 // MSVC permits pure specifier on inline functions declared at class scope.
1935 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00001936 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00001937 DeclaratorInfo.isFunctionDeclarator() &&
1938 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001939 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00001940 Init = ParseInitializer();
1941 if (Init.isInvalid())
1942 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001943 else
1944 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00001945 }
1946
Douglas Gregor45fa5602011-11-07 20:56:01 +00001947 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001948 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00001949 //
1950 // In C++11, a non-function declarator followed by an open brace is a
1951 // braced-init-list for an in-class member initialization, not an
1952 // erroneous function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00001953 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus0x) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00001954 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00001955 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00001956 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00001957 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00001958 } else if (Tok.is(tok::equal)) {
1959 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00001960 if (KW.is(tok::kw_default))
1961 DefinitionKind = FDK_Defaulted;
1962 else if (KW.is(tok::kw_delete))
1963 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00001964 }
1965 }
1966
Douglas Gregor45fa5602011-11-07 20:56:01 +00001967 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001968 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00001969 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001970 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00001971 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001972
1973 // Consume the optional ';'
1974 if (Tok.is(tok::semi))
1975 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001976 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001977 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001978
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001979 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00001980 Diag(DeclaratorInfo.getIdentifierLoc(),
1981 diag::err_function_declared_typedef);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001982 // This recovery skips the entire function body. It would be nice
1983 // to simply call ParseCXXInlineMethodDef() below, however Sema
1984 // assumes the declarator represents a function, not a typedef.
1985 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00001986 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001987
1988 // Consume the optional ';'
1989 if (Tok.is(tok::semi))
1990 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001991 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001992 }
1993
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001994 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001995 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00001996 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001997
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001998 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
1999 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2000 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002001 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002002 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002003 }
2004 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00002005
2006 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002007 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00002008 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002009
Chris Lattner682bf922009-03-29 16:50:03 +00002010 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002011 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002012 }
2013
2014 // member-declarator-list:
2015 // member-declarator
2016 // member-declarator-list ',' member-declarator
2017
Chris Lattner5f9e2722011-07-23 10:55:15 +00002018 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00002019 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00002020 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002021
2022 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002023 // member-declarator:
2024 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002025 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002026 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002027 if (Tok.is(tok::colon)) {
2028 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002029 BitfieldSize = ParseConstantExpression();
2030 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002031 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002032 }
Mike Stump1eb44332009-09-09 15:08:12 +00002033
Chris Lattnere6563252010-06-13 05:34:18 +00002034 // If a simple-asm-expr is present, parse it.
2035 if (Tok.is(tok::kw_asm)) {
2036 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002037 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002038 if (AsmLabel.isInvalid())
2039 SkipUntil(tok::comma, true, true);
2040
2041 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2042 DeclaratorInfo.SetRangeEnd(Loc);
2043 }
2044
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002045 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002046 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002047
Richard Smith7a614d82011-06-11 17:19:42 +00002048 // FIXME: When g++ adds support for this, we'll need to check whether it
2049 // goes before or after the GNU attributes and __asm__.
John McCalle402e722012-09-25 07:32:39 +00002050 ParseOptionalCXX0XVirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith7a614d82011-06-11 17:19:42 +00002051
Richard Smithca523302012-06-10 03:12:00 +00002052 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002053 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002054 if (BitfieldSize.get()) {
2055 Diag(Tok, diag::err_bitfield_member_init);
2056 SkipUntil(tok::comma, true, true);
2057 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002058 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002059 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2060 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2061 != DeclSpec::SCS_static &&
2062 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2063 != DeclSpec::SCS_typedef)
2064 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002065 }
2066 }
2067
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002068 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002069 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002070 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002071
John McCalld226f652010-08-21 09:40:31 +00002072 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002073 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00002074 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002075 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002076 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002077 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002078 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002079 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002080 TemplateParams,
John McCall67d1a672009-08-06 02:15:43 +00002081 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002082 VS, HasInClassInit);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002083 if (AccessAttrs)
2084 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2085 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002086 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002087
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002088 // Set the Decl for any late parsed attributes
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002089 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2090 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2091 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002092 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002093 LateParsedAttrs[i]->addDecl(ThisDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002094 }
2095 LateParsedAttrs.clear();
2096
Douglas Gregor147545d2011-10-10 14:49:18 +00002097 // Handle the initializer.
Richard Smithca523302012-06-10 03:12:00 +00002098 if (HasInClassInit != ICIS_NoInit) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002099 // The initializer was deferred; parse it and cache the tokens.
David Blaikie4e4d0842012-03-11 07:00:24 +00002100 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00002101 diag::warn_cxx98_compat_nonstatic_member_init :
2102 diag::ext_nonstatic_member_init);
2103
Richard Smith7a614d82011-06-11 17:19:42 +00002104 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002105 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2106 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002107 //
2108 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002109 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002110 Diag(Tok, diag::err_incomplete_array_member_init);
2111 SkipUntil(tok::comma, true, true);
David Blaikie3164c142012-02-14 09:00:46 +00002112 if (ThisDecl)
2113 // Avoid later warnings about a class member of incomplete type.
2114 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002115 } else
2116 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002117 } else if (HasInitializer) {
2118 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002119 if (!Init.isUsable())
Douglas Gregor552e2992012-02-21 02:22:07 +00002120 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002121 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2122
Douglas Gregor147545d2011-10-10 14:49:18 +00002123 if (Init.isInvalid())
2124 SkipUntil(tok::comma, true, true);
2125 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002126 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002127 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor147545d2011-10-10 14:49:18 +00002128 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2129 // No initializer.
2130 Actions.ActOnUninitializedDecl(ThisDecl,
2131 DS.getTypeSpecType() == DeclSpec::TST_auto);
Richard Smith7a614d82011-06-11 17:19:42 +00002132 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002133
2134 if (ThisDecl) {
2135 Actions.FinalizeDeclaration(ThisDecl);
2136 DeclsInGroup.push_back(ThisDecl);
2137 }
2138
Richard Smithe5310012012-04-29 07:31:09 +00002139 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor147545d2011-10-10 14:49:18 +00002140 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2141 != DeclSpec::SCS_typedef) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002142 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002143 }
2144
2145 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002146
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002147 // If we don't have a comma, it is either the end of the list (a ';')
2148 // or an error, bail out.
2149 if (Tok.isNot(tok::comma))
2150 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002151
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002152 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002153 SourceLocation CommaLoc = ConsumeToken();
2154
2155 if (Tok.isAtStartOfLine() &&
2156 !MightBeDeclarator(Declarator::MemberContext)) {
2157 // This comma was followed by a line-break and something which can't be
2158 // the start of a declarator. The comma was probably a typo for a
2159 // semicolon.
2160 Diag(CommaLoc, diag::err_expected_semi_declaration)
2161 << FixItHint::CreateReplacement(CommaLoc, ";");
2162 ExpectSemi = false;
2163 break;
2164 }
Mike Stump1eb44332009-09-09 15:08:12 +00002165
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002166 // Parse the next declarator.
2167 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002168 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002169 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002170 Init = true;
2171 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002172 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002173
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002174 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002175 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002176
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002177 if (Tok.isNot(tok::colon))
2178 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002179 }
2180
Richard Smith1c94c162012-01-09 22:31:44 +00002181 if (ExpectSemi &&
2182 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002183 // Skip to end of block or statement.
2184 SkipUntil(tok::r_brace, true, true);
2185 // If we stopped at a ';', eat it.
2186 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002187 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002188 }
2189
Douglas Gregor23c94db2010-07-02 17:43:08 +00002190 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00002191 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002192}
2193
Richard Smith7a614d82011-06-11 17:19:42 +00002194/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2195/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2196/// function definition. The location of the '=', if any, will be placed in
2197/// EqualLoc.
2198///
2199/// pure-specifier:
2200/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002201///
Richard Smith7a614d82011-06-11 17:19:42 +00002202/// brace-or-equal-initializer:
2203/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002204/// braced-init-list
2205///
Richard Smith7a614d82011-06-11 17:19:42 +00002206/// initializer-clause:
2207/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002208/// braced-init-list
2209///
Richard Smith7a614d82011-06-11 17:19:42 +00002210/// defaulted/deleted function-definition:
2211/// '=' 'default'
2212/// '=' 'delete'
2213///
2214/// Prior to C++0x, the assignment-expression in an initializer-clause must
2215/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002216ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002217 SourceLocation &EqualLoc) {
2218 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2219 && "Data member initializer not starting with '=' or '{'");
2220
Douglas Gregor552e2992012-02-21 02:22:07 +00002221 EnterExpressionEvaluationContext Context(Actions,
2222 Sema::PotentiallyEvaluated,
2223 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002224 if (Tok.is(tok::equal)) {
2225 EqualLoc = ConsumeToken();
2226 if (Tok.is(tok::kw_delete)) {
2227 // In principle, an initializer of '= delete p;' is legal, but it will
2228 // never type-check. It's better to diagnose it as an ill-formed expression
2229 // than as an ill-formed deleted non-function member.
2230 // An initializer of '= delete p, foo' will never be parsed, because
2231 // a top-level comma always ends the initializer expression.
2232 const Token &Next = NextToken();
2233 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2234 Next.is(tok::eof)) {
2235 if (IsFunction)
2236 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2237 << 1 /* delete */;
2238 else
2239 Diag(ConsumeToken(), diag::err_deleted_non_function);
2240 return ExprResult();
2241 }
2242 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002243 if (IsFunction)
2244 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2245 << 0 /* default */;
2246 else
2247 Diag(ConsumeToken(), diag::err_default_special_members);
2248 return ExprResult();
2249 }
2250
Sebastian Redl33deb352012-02-22 10:50:08 +00002251 }
2252 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002253}
2254
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002255/// ParseCXXMemberSpecification - Parse the class definition.
2256///
2257/// member-specification:
2258/// member-declaration member-specification[opt]
2259/// access-specifier ':' member-specification[opt]
2260///
Joao Matos17d35c32012-08-31 22:18:20 +00002261void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
2262 unsigned TagType, Decl *TagDecl) {
2263 assert((TagType == DeclSpec::TST_struct ||
2264 TagType == DeclSpec::TST_interface ||
2265 TagType == DeclSpec::TST_union ||
2266 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2267
John McCallf312b1e2010-08-26 23:41:50 +00002268 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2269 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002270
Douglas Gregor26997fd2010-01-16 20:52:59 +00002271 // Determine whether this is a non-nested class. Note that local
2272 // classes are *not* considered to be nested classes.
2273 bool NonNestedClass = true;
2274 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002275 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002276 if (S->isClassScope()) {
2277 // We're inside a class scope, so this is a nested class.
2278 NonNestedClass = false;
John McCalle402e722012-09-25 07:32:39 +00002279
2280 // The Microsoft extension __interface does not permit nested classes.
2281 if (getCurrentClass().IsInterface) {
2282 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2283 << /*ErrorType=*/6
2284 << (isa<NamedDecl>(TagDecl)
2285 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2286 : "<anonymous>");
2287 }
Douglas Gregor26997fd2010-01-16 20:52:59 +00002288 break;
2289 }
2290
2291 if ((S->getFlags() & Scope::FnScope)) {
2292 // If we're in a function or function template declared in the
2293 // body of a class, then this is a local class rather than a
2294 // nested class.
2295 const Scope *Parent = S->getParent();
2296 if (Parent->isTemplateParamScope())
2297 Parent = Parent->getParent();
2298 if (Parent->isClassScope())
2299 break;
2300 }
2301 }
2302 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002303
2304 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002305 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002306
Douglas Gregor6569d682009-05-27 23:11:45 +00002307 // Note that we are parsing a new (potentially-nested) class definition.
John McCalle402e722012-09-25 07:32:39 +00002308 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2309 TagType == DeclSpec::TST_interface);
Douglas Gregor6569d682009-05-27 23:11:45 +00002310
Douglas Gregorddc29e12009-02-06 22:42:48 +00002311 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002312 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002313
Anders Carlssonb184a182011-03-25 14:46:08 +00002314 SourceLocation FinalLoc;
2315
2316 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002317 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith8b11b5e2011-10-15 04:21:46 +00002318 assert(isCXX0XFinalKeyword() && "not a class definition");
2319 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002320
John McCalle402e722012-09-25 07:32:39 +00002321 if (TagType == DeclSpec::TST_interface) {
2322 Diag(FinalLoc, diag::err_override_control_interface)
2323 << "final";
2324 } else {
2325 Diag(FinalLoc, getLangOpts().CPlusPlus0x ?
2326 diag::warn_cxx98_compat_override_control_keyword :
2327 diag::ext_override_control_keyword) << "final";
2328 }
Anders Carlssonb184a182011-03-25 14:46:08 +00002329 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002330
John McCallbd0dfa52009-12-19 21:48:58 +00002331 if (Tok.is(tok::colon)) {
2332 ParseBaseClause(TagDecl);
2333
2334 if (!Tok.is(tok::l_brace)) {
2335 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002336
2337 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002338 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002339 return;
2340 }
2341 }
2342
2343 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002344 BalancedDelimiterTracker T(*this, tok::l_brace);
2345 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002346
John McCall42a4f662010-05-28 08:11:17 +00002347 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002348 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002349 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002350
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002351 // C++ 11p3: Members of a class defined with the keyword class are private
2352 // by default. Members of a class defined with the keywords struct or union
2353 // are public by default.
2354 AccessSpecifier CurAS;
2355 if (TagType == DeclSpec::TST_class)
2356 CurAS = AS_private;
2357 else
2358 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002359 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002360
Douglas Gregor07976d22010-06-21 22:31:09 +00002361 if (TagDecl) {
2362 // While we still have something to read, read the member-declarations.
2363 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2364 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002365
David Blaikie4e4d0842012-03-11 07:00:24 +00002366 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002367 Tok.is(tok::kw___if_not_exists))) {
2368 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2369 continue;
2370 }
2371
Douglas Gregor07976d22010-06-21 22:31:09 +00002372 // Check for extraneous top-level semicolon.
2373 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002374 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002375 continue;
2376 }
2377
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002378 if (Tok.is(tok::annot_pragma_vis)) {
2379 HandlePragmaVisibility();
2380 continue;
2381 }
2382
2383 if (Tok.is(tok::annot_pragma_pack)) {
2384 HandlePragmaPack();
2385 continue;
2386 }
2387
Argyrios Kyrtzidisf4deaef2012-10-12 17:39:59 +00002388 if (Tok.is(tok::annot_pragma_align)) {
2389 HandlePragmaAlign();
2390 continue;
2391 }
2392
Douglas Gregor07976d22010-06-21 22:31:09 +00002393 AccessSpecifier AS = getAccessSpecifierIfPresent();
2394 if (AS != AS_none) {
2395 // Current token is a C++ access specifier.
2396 CurAS = AS;
2397 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002398 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002399 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002400 AccessAttrs.clear();
2401 MaybeParseGNUAttributes(AccessAttrs);
2402
David Blaikie13f8daf2011-10-13 06:08:43 +00002403 SourceLocation EndLoc;
2404 if (Tok.is(tok::colon)) {
2405 EndLoc = Tok.getLocation();
2406 ConsumeToken();
2407 } else if (Tok.is(tok::semi)) {
2408 EndLoc = Tok.getLocation();
2409 ConsumeToken();
2410 Diag(EndLoc, diag::err_expected_colon)
2411 << FixItHint::CreateReplacement(EndLoc, ":");
2412 } else {
2413 EndLoc = ASLoc.getLocWithOffset(TokLength);
2414 Diag(EndLoc, diag::err_expected_colon)
2415 << FixItHint::CreateInsertion(EndLoc, ":");
2416 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002417
John McCalle402e722012-09-25 07:32:39 +00002418 // The Microsoft extension __interface does not permit non-public
2419 // access specifiers.
2420 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2421 Diag(ASLoc, diag::err_access_specifier_interface)
2422 << (CurAS == AS_protected);
2423 }
2424
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002425 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2426 AccessAttrs.getList())) {
2427 // found another attribute than only annotations
2428 AccessAttrs.clear();
2429 }
2430
Douglas Gregor07976d22010-06-21 22:31:09 +00002431 continue;
2432 }
2433
2434 // FIXME: Make sure we don't have a template here.
2435
2436 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002437 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002438 }
2439
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002440 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002441 } else {
2442 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002443 }
Mike Stump1eb44332009-09-09 15:08:12 +00002444
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002445 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002446 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002447 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002448
John McCall42a4f662010-05-28 08:11:17 +00002449 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002450 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002451 T.getOpenLocation(),
2452 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002453 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002454
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002455 // C++11 [class.mem]p2:
2456 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002457 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002458 // brace-or-equal-initializers for non-static data members (including such
2459 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002460 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002461 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002462 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002463 // declarations and the lexed inline method definitions, along with any
2464 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002465 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002466 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002467 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002468
2469 // We've finished with all pending member declarations.
2470 Actions.ActOnFinishCXXMemberDecls();
2471
Richard Smith7a614d82011-06-11 17:19:42 +00002472 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002473 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002474 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002475 }
2476
John McCall42a4f662010-05-28 08:11:17 +00002477 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002478 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2479 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002480
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002481 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002482 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002483 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002484}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002485
2486/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2487/// which explicitly initializes the members or base classes of a
2488/// class (C++ [class.base.init]). For example, the three initializers
2489/// after the ':' in the Derived constructor below:
2490///
2491/// @code
2492/// class Base { };
2493/// class Derived : Base {
2494/// int x;
2495/// float f;
2496/// public:
2497/// Derived(float f) : Base(), x(17), f(f) { }
2498/// };
2499/// @endcode
2500///
Mike Stump1eb44332009-09-09 15:08:12 +00002501/// [C++] ctor-initializer:
2502/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002503///
Mike Stump1eb44332009-09-09 15:08:12 +00002504/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002505/// mem-initializer ...[opt]
2506/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002507void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002508 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2509
John Wiegley28bbe4b2011-04-28 01:08:34 +00002510 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2511 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002512 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002513
Chris Lattner5f9e2722011-07-23 10:55:15 +00002514 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002515 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002516
Douglas Gregor7ad83902008-11-05 04:29:56 +00002517 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002518 if (Tok.is(tok::code_completion)) {
2519 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2520 MemInitializers.data(),
2521 MemInitializers.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002522 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002523 } else {
2524 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2525 if (!MemInit.isInvalid())
2526 MemInitializers.push_back(MemInit.get());
2527 else
2528 AnyErrors = true;
2529 }
2530
Douglas Gregor7ad83902008-11-05 04:29:56 +00002531 if (Tok.is(tok::comma))
2532 ConsumeToken();
2533 else if (Tok.is(tok::l_brace))
2534 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002535 // If the next token looks like a base or member initializer, assume that
2536 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002537 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2538 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2539 Diag(Loc, diag::err_ctor_init_missing_comma)
2540 << FixItHint::CreateInsertion(Loc, ", ");
2541 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002542 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002543 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002544 SkipUntil(tok::l_brace, true, true);
2545 break;
2546 }
2547 } while (true);
2548
Mike Stump1eb44332009-09-09 15:08:12 +00002549 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002550 MemInitializers.data(), MemInitializers.size(),
2551 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002552}
2553
2554/// ParseMemInitializer - Parse a C++ member initializer, which is
2555/// part of a constructor initializer that explicitly initializes one
2556/// member or base class (C++ [class.base.init]). See
2557/// ParseConstructorInitializer for an example.
2558///
2559/// [C++] mem-initializer:
2560/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002561/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002562///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002563/// [C++] mem-initializer-id:
2564/// '::'[opt] nested-name-specifier[opt] class-name
2565/// identifier
John McCalld226f652010-08-21 09:40:31 +00002566Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002567 // parse '::'[opt] nested-name-specifier[opt]
2568 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002569 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002570 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002571 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002572 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002573 if (TemplateId->Kind == TNK_Type_template ||
2574 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002575 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002576 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002577 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002578 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002579 }
David Blaikief2116622012-01-24 06:03:59 +00002580 // Uses of decltype will already have been converted to annot_decltype by
2581 // ParseOptionalCXXScopeSpecifier at this point.
2582 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2583 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002584 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002585 return true;
2586 }
Mike Stump1eb44332009-09-09 15:08:12 +00002587
David Blaikief2116622012-01-24 06:03:59 +00002588 IdentifierInfo *II = 0;
2589 DeclSpec DS(AttrFactory);
2590 SourceLocation IdLoc = Tok.getLocation();
2591 if (Tok.is(tok::annot_decltype)) {
2592 // Get the decltype expression, if there is one.
2593 ParseDecltypeSpecifier(DS);
2594 } else {
2595 if (Tok.is(tok::identifier))
2596 // Get the identifier. This may be a member name or a class name,
2597 // but we'll let the semantic analysis determine which it is.
2598 II = Tok.getIdentifierInfo();
2599 ConsumeToken();
2600 }
2601
Douglas Gregor7ad83902008-11-05 04:29:56 +00002602
2603 // Parse the '('.
David Blaikie4e4d0842012-03-11 07:00:24 +00002604 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002605 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2606
Sebastian Redl6df65482011-09-24 17:48:25 +00002607 ExprResult InitList = ParseBraceInitializer();
2608 if (InitList.isInvalid())
2609 return true;
2610
2611 SourceLocation EllipsisLoc;
2612 if (Tok.is(tok::ellipsis))
2613 EllipsisLoc = ConsumeToken();
2614
2615 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002616 TemplateTypeTy, DS, IdLoc,
2617 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002618 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002619 BalancedDelimiterTracker T(*this, tok::l_paren);
2620 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002621
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002622 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002623 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002624 CommaLocsTy CommaLocs;
2625 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2626 SkipUntil(tok::r_paren);
2627 return true;
2628 }
2629
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002630 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002631
2632 SourceLocation EllipsisLoc;
2633 if (Tok.is(tok::ellipsis))
2634 EllipsisLoc = ConsumeToken();
2635
2636 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002637 TemplateTypeTy, DS, IdLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002638 T.getOpenLocation(), ArgExprs.data(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002639 ArgExprs.size(), T.getCloseLocation(),
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002640 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002641 }
2642
David Blaikie4e4d0842012-03-11 07:00:24 +00002643 Diag(Tok, getLangOpts().CPlusPlus0x ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002644 : diag::err_expected_lparen);
2645 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002646}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002647
Sebastian Redl7acafd02011-03-05 14:45:16 +00002648/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002649///
Douglas Gregora4745612008-12-01 18:00:20 +00002650/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002651/// dynamic-exception-specification
2652/// noexcept-specification
2653///
2654/// noexcept-specification:
2655/// 'noexcept'
2656/// 'noexcept' '(' constant-expression ')'
2657ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002658Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002659 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002660 SmallVectorImpl<ParsedType> &DynamicExceptions,
2661 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002662 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002663 ExceptionSpecificationType Result = EST_None;
2664
2665 // See if there's a dynamic specification.
2666 if (Tok.is(tok::kw_throw)) {
2667 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2668 DynamicExceptions,
2669 DynamicExceptionRanges);
2670 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2671 "Produced different number of exception types and ranges.");
2672 }
2673
2674 // If there's no noexcept specification, we're done.
2675 if (Tok.isNot(tok::kw_noexcept))
2676 return Result;
2677
Richard Smith841804b2011-10-17 23:06:20 +00002678 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2679
Sebastian Redl7acafd02011-03-05 14:45:16 +00002680 // If we already had a dynamic specification, parse the noexcept for,
2681 // recovery, but emit a diagnostic and don't store the results.
2682 SourceRange NoexceptRange;
2683 ExceptionSpecificationType NoexceptType = EST_None;
2684
2685 SourceLocation KeywordLoc = ConsumeToken();
2686 if (Tok.is(tok::l_paren)) {
2687 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002688 BalancedDelimiterTracker T(*this, tok::l_paren);
2689 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002690 NoexceptType = EST_ComputedNoexcept;
2691 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002692 // The argument must be contextually convertible to bool. We use
2693 // ActOnBooleanCondition for this purpose.
2694 if (!NoexceptExpr.isInvalid())
2695 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2696 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002697 T.consumeClose();
2698 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002699 } else {
2700 // There is no argument.
2701 NoexceptType = EST_BasicNoexcept;
2702 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2703 }
2704
2705 if (Result == EST_None) {
2706 SpecificationRange = NoexceptRange;
2707 Result = NoexceptType;
2708
2709 // If there's a dynamic specification after a noexcept specification,
2710 // parse that and ignore the results.
2711 if (Tok.is(tok::kw_throw)) {
2712 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2713 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2714 DynamicExceptionRanges);
2715 }
2716 } else {
2717 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2718 }
2719
2720 return Result;
2721}
2722
2723/// ParseDynamicExceptionSpecification - Parse a C++
2724/// dynamic-exception-specification (C++ [except.spec]).
2725///
2726/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002727/// 'throw' '(' type-id-list [opt] ')'
2728/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002729///
Douglas Gregora4745612008-12-01 18:00:20 +00002730/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002731/// type-id ... [opt]
2732/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002733///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002734ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2735 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002736 SmallVectorImpl<ParsedType> &Exceptions,
2737 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002738 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002739
Sebastian Redl7acafd02011-03-05 14:45:16 +00002740 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002741 BalancedDelimiterTracker T(*this, tok::l_paren);
2742 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002743 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2744 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002745 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002746 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002747
Douglas Gregora4745612008-12-01 18:00:20 +00002748 // Parse throw(...), a Microsoft extension that means "this function
2749 // can throw anything".
2750 if (Tok.is(tok::ellipsis)) {
2751 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002752 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002753 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002754 T.consumeClose();
2755 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002756 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002757 }
2758
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002759 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002760 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002761 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002762 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002763
Douglas Gregora04426c2010-12-20 23:57:46 +00002764 if (Tok.is(tok::ellipsis)) {
2765 // C++0x [temp.variadic]p5:
2766 // - In a dynamic-exception-specification (15.4); the pattern is a
2767 // type-id.
2768 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002769 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002770 if (!Res.isInvalid())
2771 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2772 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002773
Sebastian Redlef65f062009-05-29 18:02:33 +00002774 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002775 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002776 Ranges.push_back(Range);
2777 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002778
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002779 if (Tok.is(tok::comma))
2780 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002781 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002782 break;
2783 }
2784
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002785 T.consumeClose();
2786 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002787 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002788}
Douglas Gregor6569d682009-05-27 23:11:45 +00002789
Douglas Gregordab60ad2010-10-01 18:44:50 +00002790/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2791/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002792TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002793 assert(Tok.is(tok::arrow) && "expected arrow");
2794
2795 ConsumeToken();
2796
Richard Smith7796eb52012-03-12 08:56:40 +00002797 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002798}
2799
Douglas Gregor6569d682009-05-27 23:11:45 +00002800/// \brief We have just started parsing the definition of a new class,
2801/// so push that class onto our stack of classes that is currently
2802/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002803Sema::ParsingClassState
John McCalle402e722012-09-25 07:32:39 +00002804Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
2805 bool IsInterface) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002806 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002807 "Nested class without outer class");
John McCalle402e722012-09-25 07:32:39 +00002808 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCalleee1d542011-02-14 07:13:47 +00002809 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002810}
2811
2812/// \brief Deallocate the given parsed class and all of its nested
2813/// classes.
2814void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002815 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2816 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002817 delete Class;
2818}
2819
2820/// \brief Pop the top class of the stack of classes that are
2821/// currently being parsed.
2822///
2823/// This routine should be called when we have finished parsing the
2824/// definition of a class, but have not yet popped the Scope
2825/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00002826void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002827 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002828
John McCalleee1d542011-02-14 07:13:47 +00002829 Actions.PopParsingClass(state);
2830
Douglas Gregor6569d682009-05-27 23:11:45 +00002831 ParsingClass *Victim = ClassStack.top();
2832 ClassStack.pop();
2833 if (Victim->TopLevelClass) {
2834 // Deallocate all of the nested classes of this class,
2835 // recursively: we don't need to keep any of this information.
2836 DeallocateParsedClasses(Victim);
2837 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002838 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002839 assert(!ClassStack.empty() && "Missing top-level class?");
2840
Douglas Gregord54eb442010-10-12 16:25:54 +00002841 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002842 // The victim is a nested class, but we will not need to perform
2843 // any processing after the definition of this class since it has
2844 // no members whose handling was delayed. Therefore, we can just
2845 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002846 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002847 return;
2848 }
2849
2850 // This nested class has some members that will need to be processed
2851 // after the top-level class is completely defined. Therefore, add
2852 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002853 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002854 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002855 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00002856}
Sean Huntbbd37c62009-11-21 08:43:09 +00002857
Richard Smithc56298d2012-04-10 03:25:07 +00002858/// \brief Try to parse an 'identifier' which appears within an attribute-token.
2859///
2860/// \return the parsed identifier on success, and 0 if the next token is not an
2861/// attribute-token.
2862///
2863/// C++11 [dcl.attr.grammar]p3:
2864/// If a keyword or an alternative token that satisfies the syntactic
2865/// requirements of an identifier is contained in an attribute-token,
2866/// it is considered an identifier.
2867IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
2868 switch (Tok.getKind()) {
2869 default:
2870 // Identifiers and keywords have identifier info attached.
2871 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
2872 Loc = ConsumeToken();
2873 return II;
2874 }
2875 return 0;
2876
2877 case tok::ampamp: // 'and'
2878 case tok::pipe: // 'bitor'
2879 case tok::pipepipe: // 'or'
2880 case tok::caret: // 'xor'
2881 case tok::tilde: // 'compl'
2882 case tok::amp: // 'bitand'
2883 case tok::ampequal: // 'and_eq'
2884 case tok::pipeequal: // 'or_eq'
2885 case tok::caretequal: // 'xor_eq'
2886 case tok::exclaim: // 'not'
2887 case tok::exclaimequal: // 'not_eq'
2888 // Alternative tokens do not have identifier info, but their spelling
2889 // starts with an alphabetical character.
2890 llvm::SmallString<8> SpellingBuf;
2891 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
2892 if (std::isalpha(Spelling[0])) {
2893 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00002894 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00002895 }
2896 return 0;
2897 }
2898}
2899
Michael Han6880f492012-10-03 01:56:22 +00002900static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
2901 IdentifierInfo *ScopeName) {
2902 switch (AttributeList::getKind(AttrName, ScopeName,
2903 AttributeList::AS_CXX11)) {
2904 case AttributeList::AT_CarriesDependency:
2905 case AttributeList::AT_FallThrough:
2906 case AttributeList::AT_NoReturn: {
2907 return true;
2908 }
2909
2910 default:
2911 return false;
2912 }
2913}
2914
Richard Smithc56298d2012-04-10 03:25:07 +00002915/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002916/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00002917///
Richard Smith6ee326a2012-04-10 01:32:12 +00002918/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00002919/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002920/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00002921///
Richard Smith6ee326a2012-04-10 01:32:12 +00002922/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00002923/// attribute[opt]
2924/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00002925/// attribute '...'
2926/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00002927///
Richard Smith6ee326a2012-04-10 01:32:12 +00002928/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002929/// attribute-token attribute-argument-clause[opt]
2930///
Richard Smith6ee326a2012-04-10 01:32:12 +00002931/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00002932/// identifier
2933/// attribute-scoped-token
2934///
Richard Smith6ee326a2012-04-10 01:32:12 +00002935/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00002936/// attribute-namespace '::' identifier
2937///
Richard Smith6ee326a2012-04-10 01:32:12 +00002938/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00002939/// identifier
2940///
Richard Smith6ee326a2012-04-10 01:32:12 +00002941/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00002942/// '(' balanced-token-seq ')'
2943///
Richard Smith6ee326a2012-04-10 01:32:12 +00002944/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00002945/// balanced-token
2946/// balanced-token-seq balanced-token
2947///
Richard Smith6ee326a2012-04-10 01:32:12 +00002948/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00002949/// '(' balanced-token-seq ')'
2950/// '[' balanced-token-seq ']'
2951/// '{' balanced-token-seq '}'
2952/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00002953void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002954 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002955 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00002956 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002957 ParseAlignmentSpecifier(attrs, endLoc);
2958 return;
2959 }
2960
Sean Huntbbd37c62009-11-21 08:43:09 +00002961 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00002962 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00002963
Richard Smith41be6732011-10-14 20:48:27 +00002964 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
2965
Sean Huntbbd37c62009-11-21 08:43:09 +00002966 ConsumeBracket();
2967 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002968
Richard Smithc56298d2012-04-10 03:25:07 +00002969 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002970 // attribute not present
2971 if (Tok.is(tok::comma)) {
2972 ConsumeToken();
2973 continue;
2974 }
2975
Richard Smithc56298d2012-04-10 03:25:07 +00002976 SourceLocation ScopeLoc, AttrLoc;
2977 IdentifierInfo *ScopeName = 0, *AttrName = 0;
2978
2979 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
2980 if (!AttrName)
2981 // Break out to the "expected ']'" diagnostic.
2982 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002983
Sean Huntbbd37c62009-11-21 08:43:09 +00002984 // scoped attribute
2985 if (Tok.is(tok::coloncolon)) {
2986 ConsumeToken();
2987
Richard Smithc56298d2012-04-10 03:25:07 +00002988 ScopeName = AttrName;
2989 ScopeLoc = AttrLoc;
2990
2991 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
2992 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002993 Diag(Tok.getLocation(), diag::err_expected_ident);
2994 SkipUntil(tok::r_square, tok::comma, true, true);
2995 continue;
2996 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002997 }
2998
Michael Han6880f492012-10-03 01:56:22 +00002999 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Sean Huntbbd37c62009-11-21 08:43:09 +00003000 bool AttrParsed = false;
Sean Huntbbd37c62009-11-21 08:43:09 +00003001
Michael Han6880f492012-10-03 01:56:22 +00003002 // Parse attribute arguments
3003 if (Tok.is(tok::l_paren)) {
3004 if (ScopeName && ScopeName->getName() == "gnu") {
3005 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3006 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3007 AttrParsed = true;
3008 } else {
3009 if (StandardAttr)
3010 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3011 << AttrName->getName();
3012
3013 // FIXME: handle other formats of c++11 attribute arguments
3014 ConsumeParen();
3015 SkipUntil(tok::r_paren, false);
3016 }
3017 }
3018
3019 if (!AttrParsed)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00003020 attrs.addNew(AttrName,
3021 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3022 AttrLoc),
3023 ScopeName, ScopeLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00003024 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smith6ee326a2012-04-10 01:32:12 +00003025
Richard Smithc56298d2012-04-10 03:25:07 +00003026 if (Tok.is(tok::ellipsis)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003027 ConsumeToken();
Michael Han6880f492012-10-03 01:56:22 +00003028
3029 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3030 << AttrName->getName();
Richard Smithc56298d2012-04-10 03:25:07 +00003031 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003032 }
3033
3034 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3035 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003036 if (endLoc)
3037 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00003038 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3039 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003040}
Sean Huntbbd37c62009-11-21 08:43:09 +00003041
Sean Hunt2edf0a22012-06-23 05:07:58 +00003042/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003043///
3044/// attribute-specifier-seq:
3045/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00003046void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003047 SourceLocation *endLoc) {
3048 SourceLocation StartLoc = Tok.getLocation(), Loc;
3049 if (!endLoc)
3050 endLoc = &Loc;
3051
Douglas Gregor8828ee72011-10-07 20:35:25 +00003052 do {
Richard Smithc56298d2012-04-10 03:25:07 +00003053 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003054 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003055
3056 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003057}
3058
Francois Pichet334d47e2010-10-11 12:59:39 +00003059/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3060///
3061/// [MS] ms-attribute:
3062/// '[' token-seq ']'
3063///
3064/// [MS] ms-attribute-seq:
3065/// ms-attribute[opt]
3066/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003067void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3068 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003069 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3070
3071 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003072 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003073 ConsumeBracket();
3074 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003075 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003076 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3077 }
3078}
Francois Pichet563a6452011-05-25 10:19:49 +00003079
3080void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3081 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003082 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003083 if (ParseMicrosoftIfExistsCondition(Result))
3084 return;
3085
Douglas Gregor3896fc52011-10-24 22:31:10 +00003086 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3087 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003088 Diag(Tok, diag::err_expected_lbrace);
3089 return;
3090 }
Francois Pichet563a6452011-05-25 10:19:49 +00003091
Douglas Gregor3896fc52011-10-24 22:31:10 +00003092 switch (Result.Behavior) {
3093 case IEB_Parse:
3094 // Parse the declarations below.
3095 break;
3096
3097 case IEB_Dependent:
3098 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3099 << Result.IsIfExists;
3100 // Fall through to skip.
3101
3102 case IEB_Skip:
3103 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003104 return;
3105 }
3106
Douglas Gregor3896fc52011-10-24 22:31:10 +00003107 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003108 // __if_exists, __if_not_exists can nest.
3109 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3110 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3111 continue;
3112 }
3113
3114 // Check for extraneous top-level semicolon.
3115 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003116 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003117 continue;
3118 }
3119
3120 AccessSpecifier AS = getAccessSpecifierIfPresent();
3121 if (AS != AS_none) {
3122 // Current token is a C++ access specifier.
3123 CurAS = AS;
3124 SourceLocation ASLoc = Tok.getLocation();
3125 ConsumeToken();
3126 if (Tok.is(tok::colon))
3127 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3128 else
3129 Diag(Tok, diag::err_expected_colon);
3130 ConsumeToken();
3131 continue;
3132 }
3133
3134 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003135 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003136 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003137
3138 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003139}