blob: 503d436c3bda2dcb945f296aa0fc52fafe02d096 [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor1b7f8982008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
16#include "clang/Basic/OperatorKinds.h"
Chris Lattner500d3292009-01-29 05:15:15 +000017#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000018#include "clang/Sema/DeclSpec.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Sema/Scope.h"
John McCalle402e722012-09-25 07:32:39 +000022#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.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)) {
Nico Webere1bb3292012-10-27 23:44:27 +000091 if (Ident == 0) {
92 Diag(Tok, diag::err_expected_ident);
93 // Skip to end of the definition and eat the ';'.
94 SkipUntil(tok::semi);
95 return 0;
96 }
John McCall7f040a92010-12-24 02:08:15 +000097 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +000098 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +000099 if (InlineLoc.isValid())
100 Diag(InlineLoc, diag::err_inline_namespace_alias)
101 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000102 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000103 }
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Richard Trieuf858bd82011-05-26 20:11:09 +0000105
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000106 BalancedDelimiterTracker T(*this, tok::l_brace);
107 if (T.consumeOpen()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000108 if (!ExtraIdent.empty()) {
109 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
110 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
111 }
Mike Stump1eb44332009-09-09 15:08:12 +0000112 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +0000113 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +0000114 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000115 }
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Douglas Gregor23c94db2010-07-02 17:43:08 +0000117 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
118 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
119 getCurScope()->getFnParent()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000120 if (!ExtraIdent.empty()) {
121 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
122 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
123 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000124 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Douglas Gregor95f1b152010-05-14 05:08:22 +0000125 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000126 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000127 }
128
Richard Trieuf858bd82011-05-26 20:11:09 +0000129 if (!ExtraIdent.empty()) {
130 TentativeParsingAction TPA(*this);
131 SkipUntil(tok::r_brace, /*StopAtSemi*/false, /*DontConsume*/true);
132 Token rBraceToken = Tok;
133 TPA.Revert();
134
135 if (!rBraceToken.is(tok::r_brace)) {
136 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
137 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
138 } else {
Benjamin Kramer9910df02011-05-26 21:32:30 +0000139 std::string NamespaceFix;
Richard Trieuf858bd82011-05-26 20:11:09 +0000140 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
141 E = ExtraIdent.end(); I != E; ++I) {
142 NamespaceFix += " { namespace ";
143 NamespaceFix += (*I)->getName();
144 }
Benjamin Kramer9910df02011-05-26 21:32:30 +0000145
Richard Trieuf858bd82011-05-26 20:11:09 +0000146 std::string RBraces;
Benjamin Kramer9910df02011-05-26 21:32:30 +0000147 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieuf858bd82011-05-26 20:11:09 +0000148 RBraces += "} ";
Benjamin Kramer9910df02011-05-26 21:32:30 +0000149
Richard Trieuf858bd82011-05-26 20:11:09 +0000150 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
151 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
152 ExtraIdentLoc.back()),
153 NamespaceFix)
154 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
155 }
156 }
157
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000158 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith7fe62082011-10-15 05:09:34 +0000159 if (InlineLoc.isValid())
David Blaikie4e4d0842012-03-11 07:00:24 +0000160 Diag(InlineLoc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +0000161 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000162
Chris Lattner51448322009-03-29 14:02:43 +0000163 // Enter a scope for the namespace.
164 ParseScope NamespaceScope(this, Scope::DeclScope);
165
John McCalld226f652010-08-21 09:40:31 +0000166 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000167 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000168 IdentLoc, Ident, T.getOpenLocation(),
169 attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000170
John McCallf312b1e2010-08-26 23:41:50 +0000171 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
172 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Richard Trieuf858bd82011-05-26 20:11:09 +0000174 // Parse the contents of the namespace. This includes parsing recovery on
175 // any improperly nested namespaces.
176 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000177 InlineLoc, attrs, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattner51448322009-03-29 14:02:43 +0000179 // Leave the namespace scope.
180 NamespaceScope.Exit();
181
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000182 DeclEnd = T.getCloseLocation();
183 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner51448322009-03-29 14:02:43 +0000184
185 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000186}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000187
Richard Trieuf858bd82011-05-26 20:11:09 +0000188/// ParseInnerNamespace - Parse the contents of a namespace.
189void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
190 std::vector<IdentifierInfo*>& Ident,
191 std::vector<SourceLocation>& NamespaceLoc,
192 unsigned int index, SourceLocation& InlineLoc,
Richard Trieuf858bd82011-05-26 20:11:09 +0000193 ParsedAttributes& attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000194 BalancedDelimiterTracker &Tracker) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000195 if (index == Ident.size()) {
196 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
197 ParsedAttributesWithRange attrs(AttrFactory);
198 MaybeParseCXX0XAttributes(attrs);
199 MaybeParseMicrosoftAttributes(attrs);
200 ParseExternalDeclaration(attrs);
201 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000202
203 // The caller is what called check -- we are simply calling
204 // the close for it.
205 Tracker.consumeClose();
Richard Trieuf858bd82011-05-26 20:11:09 +0000206
207 return;
208 }
209
210 // Parse improperly nested namespaces.
211 ParseScope NamespaceScope(this, Scope::DeclScope);
212 Decl *NamespcDecl =
213 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
214 NamespaceLoc[index], IdentLoc[index],
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000215 Ident[index], Tracker.getOpenLocation(),
216 attrs.getList());
Richard Trieuf858bd82011-05-26 20:11:09 +0000217
218 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000219 attrs, Tracker);
Richard Trieuf858bd82011-05-26 20:11:09 +0000220
221 NamespaceScope.Exit();
222
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000223 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieuf858bd82011-05-26 20:11:09 +0000224}
225
Anders Carlssonf67606a2009-03-28 04:07:16 +0000226/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
227/// alias definition.
228///
John McCalld226f652010-08-21 09:40:31 +0000229Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000230 SourceLocation AliasLoc,
231 IdentifierInfo *Alias,
232 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000233 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Anders Carlssonf67606a2009-03-28 04:07:16 +0000235 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000237 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000238 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000239 cutOffParsing();
240 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000241 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000242
Anders Carlssonf67606a2009-03-28 04:07:16 +0000243 CXXScopeSpec SS;
244 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000245 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000246
247 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
248 Diag(Tok, diag::err_expected_namespace_name);
249 // Skip to end of the definition and eat the ';'.
250 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000251 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000252 }
253
254 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000255 IdentifierInfo *Ident = Tok.getIdentifierInfo();
256 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Anders Carlssonf67606a2009-03-28 04:07:16 +0000258 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000259 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000260 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
261 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Douglas Gregor23c94db2010-07-02 17:43:08 +0000263 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000264 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000265}
266
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000267/// ParseLinkage - We know that the current token is a string_literal
268/// and just before that, that extern was seen.
269///
270/// linkage-specification: [C++ 7.5p2: dcl.link]
271/// 'extern' string-literal '{' declaration-seq[opt] '}'
272/// 'extern' string-literal declaration
273///
Chris Lattner7d642712010-11-09 20:15:55 +0000274Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000275 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000276 SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000277 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000278 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000279 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000280 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000281
Richard Smith99831e42012-03-06 03:21:47 +0000282 // FIXME: This is incorrect: linkage-specifiers are parsed in translation
283 // phase 7, so string-literal concatenation is supposed to occur.
284 // extern "" "C" "" "+" "+" { } is legal.
285 if (Tok.hasUDSuffix())
286 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000287 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000288
Douglas Gregor074149e2009-01-05 19:45:36 +0000289 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000290 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000291 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000292 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000293 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000294 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000295 : SourceLocation());
296
John McCall0b7e6782011-03-24 11:26:52 +0000297 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000298 MaybeParseCXX0XAttributes(attrs);
299 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000300
Douglas Gregor074149e2009-01-05 19:45:36 +0000301 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-05-01 16:25:54 +0000302 // Reset the source range in DS, as the leading "extern"
303 // does not really belong to the inner declaration ...
304 DS.SetRangeStart(SourceLocation());
305 DS.SetRangeEnd(SourceLocation());
306 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000307 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000308 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000309 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000310 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000311 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000312
Douglas Gregor63a01132010-02-07 08:38:28 +0000313 DS.abort();
314
John McCall7f040a92010-12-24 02:08:15 +0000315 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000316
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000317 BalancedDelimiterTracker T(*this, tok::l_brace);
318 T.consumeOpen();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000319 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000320 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000321 MaybeParseCXX0XAttributes(attrs);
322 MaybeParseMicrosoftAttributes(attrs);
323 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000324 }
325
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000326 T.consumeClose();
Chris Lattner7d642712010-11-09 20:15:55 +0000327 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000328 T.getCloseLocation());
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000329}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000330
Douglas Gregorf780abc2008-12-30 03:27:21 +0000331/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
332/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000333Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000334 const ParsedTemplateInfo &TemplateInfo,
335 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000336 ParsedAttributesWithRange &attrs,
337 Decl **OwnedType) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000338 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000339 ObjCDeclContextSwitch ObjCDC(*this);
340
Douglas Gregorf780abc2008-12-30 03:27:21 +0000341 // Eat 'using'.
342 SourceLocation UsingLoc = ConsumeToken();
343
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000344 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000345 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000346 cutOffParsing();
347 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000348 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000349
John McCall78b81052010-11-10 02:40:36 +0000350 // 'using namespace' means this is a using-directive.
351 if (Tok.is(tok::kw_namespace)) {
352 // Template parameters are always an error here.
353 if (TemplateInfo.Kind) {
354 SourceRange R = TemplateInfo.getSourceRange();
355 Diag(UsingLoc, diag::err_templated_using_directive)
356 << R << FixItHint::CreateRemoval(R);
357 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000358
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000359 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000360 }
361
Richard Smith162e1c12011-04-15 14:24:37 +0000362 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000363
364 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000365 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000366
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000367 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000368 AS_none, OwnedType);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000369}
370
371/// ParseUsingDirective - Parse C++ using-directive, assumes
372/// that current token is 'namespace' and 'using' was already parsed.
373///
374/// using-directive: [C++ 7.3.p4: namespace.udir]
375/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
376/// namespace-name ;
377/// [GNU] using-directive:
378/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
379/// namespace-name attributes[opt] ;
380///
John McCalld226f652010-08-21 09:40:31 +0000381Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000382 SourceLocation UsingLoc,
383 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000384 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000385 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
386
387 // Eat 'namespace'.
388 SourceLocation NamespcLoc = ConsumeToken();
389
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000390 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000391 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000392 cutOffParsing();
393 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000394 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000395
Douglas Gregorf780abc2008-12-30 03:27:21 +0000396 CXXScopeSpec SS;
397 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000398 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000399
Douglas Gregorf780abc2008-12-30 03:27:21 +0000400 IdentifierInfo *NamespcName = 0;
401 SourceLocation IdentLoc = SourceLocation();
402
403 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000404 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000405 Diag(Tok, diag::err_expected_namespace_name);
406 // If there was invalid namespace name, skip to end of decl, and eat ';'.
407 SkipUntil(tok::semi);
408 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000409 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Chris Lattner823c44e2009-01-06 07:27:21 +0000412 // Parse identifier.
413 NamespcName = Tok.getIdentifierInfo();
414 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Chris Lattner823c44e2009-01-06 07:27:21 +0000416 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000417 bool GNUAttr = false;
418 if (Tok.is(tok::kw___attribute)) {
419 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000420 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Chris Lattner823c44e2009-01-06 07:27:21 +0000423 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000424 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000425 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000426 GNUAttr ? diag::err_expected_semi_after_attribute_list
427 : diag::err_expected_semi_after_namespace_name,
428 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000429
Douglas Gregor23c94db2010-07-02 17:43:08 +0000430 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000431 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000432}
433
Richard Smith162e1c12011-04-15 14:24:37 +0000434/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
435/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000436///
437/// using-declaration: [C++ 7.3.p3: namespace.udecl]
438/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000439/// unqualified-id
440/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000441///
Richard Smith162e1c12011-04-15 14:24:37 +0000442/// alias-declaration: C++0x [decl.typedef]p2
443/// 'using' identifier = type-id ;
444///
John McCalld226f652010-08-21 09:40:31 +0000445Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000446 const ParsedTemplateInfo &TemplateInfo,
447 SourceLocation UsingLoc,
448 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000449 AccessSpecifier AS,
450 Decl **OwnedType) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000451 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000452 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000453 bool IsTypeName;
Sean Hunt2edf0a22012-06-23 05:07:58 +0000454 ParsedAttributesWithRange attrs(AttrFactory);
455
456 // FIXME: Simply skip the attributes and diagnose, don't bother parsing them.
457 MaybeParseCXX0XAttributes(attrs);
458 ProhibitAttributes(attrs);
459 attrs.clear();
460 attrs.Range = SourceRange();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000461
462 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000463 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000464 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000465 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000466 ConsumeToken();
467 IsTypeName = true;
468 }
469 else
470 IsTypeName = false;
471
472 // Parse nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000473 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000474
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000475 // Check nested-name specifier.
476 if (SS.isInvalid()) {
477 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000478 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000479 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000480
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000481 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000482 // destructor names and allow the action module to diagnose any semantic
483 // errors.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000484 SourceLocation TemplateKWLoc;
Douglas Gregor12c118a2009-11-04 16:30:06 +0000485 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000486 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000487 /*EnteringContext=*/false,
488 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000489 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000490 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000491 TemplateKWLoc,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000492 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000493 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000494 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000495 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000496
Sean Hunt2edf0a22012-06-23 05:07:58 +0000497 MaybeParseCXX0XAttributes(attrs);
Richard Smith162e1c12011-04-15 14:24:37 +0000498
499 // Maybe this is an alias-declaration.
500 bool IsAliasDecl = Tok.is(tok::equal);
501 TypeResult TypeAlias;
502 if (IsAliasDecl) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000503 // TODO: Attribute support. C++0x attributes may appear before the equals.
504 // Where can GNU attributes appear?
Richard Smith162e1c12011-04-15 14:24:37 +0000505 ConsumeToken();
506
David Blaikie4e4d0842012-03-11 07:00:24 +0000507 Diag(Tok.getLocation(), getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +0000508 diag::warn_cxx98_compat_alias_declaration :
509 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000510
Richard Smith3e4c6c42011-05-05 21:57:07 +0000511 // Type alias templates cannot be specialized.
512 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000513 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
514 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000515 SpecKind = 0;
516 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
517 SpecKind = 1;
518 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
519 SpecKind = 2;
520 if (SpecKind != -1) {
521 SourceRange Range;
522 if (SpecKind == 0)
523 Range = SourceRange(Name.TemplateId->LAngleLoc,
524 Name.TemplateId->RAngleLoc);
525 else
526 Range = TemplateInfo.getSourceRange();
527 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
528 << SpecKind << Range;
529 SkipUntil(tok::semi);
530 return 0;
531 }
532
Richard Smith162e1c12011-04-15 14:24:37 +0000533 // Name must be an identifier.
534 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
535 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
536 // No removal fixit: can't recover from this.
537 SkipUntil(tok::semi);
538 return 0;
539 } else if (IsTypeName)
540 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
541 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
542 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
543 else if (SS.isNotEmpty())
544 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
545 << FixItHint::CreateRemoval(SS.getRange());
546
Richard Smith3e4c6c42011-05-05 21:57:07 +0000547 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
548 Declarator::AliasTemplateContext :
John McCallcdda47f2011-10-01 09:56:14 +0000549 Declarator::AliasDeclContext, AS, OwnedType);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000550 } else {
551 // C++11 attributes are not allowed on a using-declaration, but GNU ones
552 // are.
553 ProhibitAttributes(attrs);
554
Richard Smith162e1c12011-04-15 14:24:37 +0000555 // Parse (optional) attributes (most likely GNU strong-using extension).
556 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000557 }
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000559 // Eat ';'.
560 DeclEnd = Tok.getLocation();
561 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith162e1c12011-04-15 14:24:37 +0000562 !attrs.empty() ? "attributes list" :
563 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000564 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000565
John McCall78b81052010-11-10 02:40:36 +0000566 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith3e4c6c42011-05-05 21:57:07 +0000567 // In C++0x, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000568 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000569 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000570 SourceRange R = TemplateInfo.getSourceRange();
571 Diag(UsingLoc, diag::err_templated_using_declaration)
572 << R << FixItHint::CreateRemoval(R);
573
574 // Unfortunately, we have to bail out instead of recovering by
575 // ignoring the parameters, just in case the nested name specifier
576 // depends on the parameters.
577 return 0;
578 }
579
Douglas Gregor480b53c2011-09-26 14:30:28 +0000580 // "typename" keyword is allowed for identifiers only,
581 // because it may be a type definition.
582 if (IsTypeName && Name.getKind() != UnqualifiedId::IK_Identifier) {
583 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
584 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
585 // Proceed parsing, but reset the IsTypeName flag.
586 IsTypeName = false;
587 }
588
Richard Smith3e4c6c42011-05-05 21:57:07 +0000589 if (IsAliasDecl) {
590 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramer5354e772012-08-23 23:38:35 +0000591 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3e4c6c42011-05-05 21:57:07 +0000592 TemplateParams ? TemplateParams->data() : 0,
593 TemplateParams ? TemplateParams->size() : 0);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000594 // FIXME: Propagate attributes.
Richard Smith3e4c6c42011-05-05 21:57:07 +0000595 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
596 UsingLoc, Name, TypeAlias);
597 }
Richard Smith162e1c12011-04-15 14:24:37 +0000598
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000599 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000600 Name, attrs.getList(),
601 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000602}
603
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000604/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000605///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000606/// [C++0x] static_assert-declaration:
607/// static_assert ( constant-expression , string-literal ) ;
608///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000609/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000610/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000611///
John McCalld226f652010-08-21 09:40:31 +0000612Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000613 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
614 "Not a static_assert declaration");
615
David Blaikie4e4d0842012-03-11 07:00:24 +0000616 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000617 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000618 if (Tok.is(tok::kw_static_assert))
619 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000620
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000621 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000623 BalancedDelimiterTracker T(*this, tok::l_paren);
624 if (T.consumeOpen()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000625 Diag(Tok, diag::err_expected_lparen);
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
John McCall60d7b3a2010-08-24 06:29:42 +0000630 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000631 if (AssertExpr.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000632 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000633 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000634 }
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Anders Carlssonad5f9602009-03-13 23:29:20 +0000636 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000637 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000638
Richard Smith0cc323c2012-03-05 23:20:05 +0000639 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000640 Diag(Tok, diag::err_expected_string_literal)
641 << /*Source='static_assert'*/1;
Richard Smith3686c712012-09-13 19:12:50 +0000642 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000643 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000644 }
Mike Stump1eb44332009-09-09 15:08:12 +0000645
John McCall60d7b3a2010-08-24 06:29:42 +0000646 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smith99831e42012-03-06 03:21:47 +0000647 if (AssertMessage.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000648 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000649 return 0;
Richard Smith99831e42012-03-06 03:21:47 +0000650 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000651
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000652 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Chris Lattner97144fc2009-04-02 04:16:50 +0000654 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000655 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000656
John McCall9ae2f072010-08-23 23:25:46 +0000657 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
658 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000659 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000660 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000661}
662
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000663/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
664///
665/// 'decltype' ( expression )
666///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000667SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
668 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
669 && "Not a decltype specifier");
670
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000671
David Blaikie42d6d0c2011-12-04 05:04:18 +0000672 ExprResult Result;
673 SourceLocation StartLoc = Tok.getLocation();
674 SourceLocation EndLoc;
675
676 if (Tok.is(tok::annot_decltype)) {
677 Result = getExprAnnotation(Tok);
678 EndLoc = Tok.getAnnotationEndLoc();
679 ConsumeToken();
680 if (Result.isInvalid()) {
681 DS.SetTypeSpecError();
682 return EndLoc;
683 }
684 } else {
Richard Smithc7b55432012-02-24 22:30:04 +0000685 if (Tok.getIdentifierInfo()->isStr("decltype"))
686 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000687
David Blaikie42d6d0c2011-12-04 05:04:18 +0000688 ConsumeToken();
689
690 BalancedDelimiterTracker T(*this, tok::l_paren);
691 if (T.expectAndConsume(diag::err_expected_lparen_after,
692 "decltype", tok::r_paren)) {
693 DS.SetTypeSpecError();
694 return T.getOpenLocation() == Tok.getLocation() ?
695 StartLoc : T.getOpenLocation();
696 }
697
698 // Parse the expression
699
700 // C++0x [dcl.type.simple]p4:
701 // The operand of the decltype specifier is an unevaluated operand.
Richard Smith76f3f692012-02-22 02:04:18 +0000702 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
703 0, /*IsDecltype=*/true);
David Blaikie42d6d0c2011-12-04 05:04:18 +0000704 Result = ParseExpression();
705 if (Result.isInvalid()) {
David Blaikie42d6d0c2011-12-04 05:04:18 +0000706 DS.SetTypeSpecError();
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000707 if (SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true)) {
708 EndLoc = ConsumeParen();
709 } else {
710 assert(Tok.is(tok::semi));
711 if (PP.isBacktrackEnabled()) {
712 // Backtrack to get the location of the last token before the semi.
713 PP.RevertCachedTokens(2);
714 ConsumeToken(); // the semi.
715 EndLoc = ConsumeAnyToken();
716 assert(Tok.is(tok::semi));
717 } else {
718 EndLoc = Tok.getLocation();
719 }
720 }
721 return EndLoc;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000722 }
723
724 // Match the ')'
725 T.consumeClose();
726 if (T.getCloseLocation().isInvalid()) {
727 DS.SetTypeSpecError();
728 // FIXME: this should return the location of the last token
729 // that was consumed (by "consumeClose()")
730 return T.getCloseLocation();
731 }
732
Richard Smith76f3f692012-02-22 02:04:18 +0000733 Result = Actions.ActOnDecltypeExpression(Result.take());
734 if (Result.isInvalid()) {
735 DS.SetTypeSpecError();
736 return T.getCloseLocation();
737 }
738
David Blaikie42d6d0c2011-12-04 05:04:18 +0000739 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000740 }
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000742 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000743 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000744 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000745 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
David Blaikie42d6d0c2011-12-04 05:04:18 +0000746 DiagID, Result.release())) {
John McCallfec54012009-08-03 20:12:06 +0000747 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000748 DS.SetTypeSpecError();
749 }
750 return EndLoc;
751}
752
753void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
754 SourceLocation StartLoc,
755 SourceLocation EndLoc) {
756 // make sure we have a token we can turn into an annotation token
757 if (PP.isBacktrackEnabled())
758 PP.RevertCachedTokens(1);
759 else
760 PP.EnterToken(Tok);
761
762 Tok.setKind(tok::annot_decltype);
763 setExprAnnotation(Tok, DS.getTypeSpecType() == TST_decltype ?
764 DS.getRepAsExpr() : ExprResult());
765 Tok.setAnnotationEndLoc(EndLoc);
766 Tok.setLocation(StartLoc);
767 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000768}
769
Sean Huntdb5d44b2011-05-19 05:37:45 +0000770void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
771 assert(Tok.is(tok::kw___underlying_type) &&
772 "Not an underlying type specifier");
773
774 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000775 BalancedDelimiterTracker T(*this, tok::l_paren);
776 if (T.expectAndConsume(diag::err_expected_lparen_after,
777 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000778 return;
779 }
780
781 TypeResult Result = ParseTypeName();
782 if (Result.isInvalid()) {
783 SkipUntil(tok::r_paren);
784 return;
785 }
786
787 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000788 T.consumeClose();
789 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000790 return;
791
792 const char *PrevSpec = 0;
793 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000794 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000795 DiagID, Result.release()))
796 Diag(StartLoc, DiagID) << PrevSpec;
797}
798
David Blaikie09048df2011-10-25 15:01:20 +0000799/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
800/// class name or decltype-specifier. Note that we only check that the result
801/// names a type; semantic analysis will need to verify that the type names a
802/// class. The result is either a type or null, depending on whether a type
803/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000804///
David Blaikie09048df2011-10-25 15:01:20 +0000805/// base-type-specifier: [C++ 10.1]
806/// class-or-decltype
807/// class-or-decltype: [C++ 10.1]
808/// nested-name-specifier[opt] class-name
809/// decltype-specifier
Douglas Gregor42a552f2008-11-05 20:51:48 +0000810/// class-name: [C++ 9.1]
811/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000812/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000813///
David Blaikie22216eb2011-10-25 17:10:12 +0000814Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
815 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000816 // Ignore attempts to use typename
817 if (Tok.is(tok::kw_typename)) {
818 Diag(Tok, diag::err_expected_class_name_not_template)
819 << FixItHint::CreateRemoval(Tok.getLocation());
820 ConsumeToken();
821 }
822
David Blaikie152aa4b2011-10-25 18:17:58 +0000823 // Parse optional nested-name-specifier
824 CXXScopeSpec SS;
825 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
826
827 BaseLoc = Tok.getLocation();
828
David Blaikie22216eb2011-10-25 17:10:12 +0000829 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000830 // tok == kw_decltype is just error recovery, it can only happen when SS
831 // isn't empty
832 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000833 if (SS.isNotEmpty())
834 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
835 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000836 // Fake up a Declarator to use with ActOnTypeName.
837 DeclSpec DS(AttrFactory);
838
David Blaikieb5777572011-12-08 04:53:15 +0000839 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000840
841 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
842 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
843 }
844
Douglas Gregor7f43d672009-02-25 23:52:28 +0000845 // Check whether we have a template-id that names a type.
846 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000847 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000848 if (TemplateId->Kind == TNK_Type_template ||
849 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000850 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000851
852 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000853 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000854 EndLocation = Tok.getAnnotationEndLoc();
855 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000856
857 if (Type)
858 return Type;
859 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000860 }
861
862 // Fall through to produce an error below.
863 }
864
Douglas Gregor42a552f2008-11-05 20:51:48 +0000865 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000866 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000867 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000868 }
869
Douglas Gregor84d0a192010-01-12 21:28:44 +0000870 IdentifierInfo *Id = Tok.getIdentifierInfo();
871 SourceLocation IdLoc = ConsumeToken();
872
873 if (Tok.is(tok::less)) {
874 // It looks the user intended to write a template-id here, but the
875 // template-name was wrong. Try to fix that.
876 TemplateNameKind TNK = TNK_Type_template;
877 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000878 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000879 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000880 Diag(IdLoc, diag::err_unknown_template_name)
881 << Id;
882 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000883
Douglas Gregor84d0a192010-01-12 21:28:44 +0000884 if (!Template)
885 return true;
886
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000887 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000888 UnqualifiedId TemplateName;
889 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000890
Douglas Gregor84d0a192010-01-12 21:28:44 +0000891 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000892 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
893 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000894 return true;
895 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000896 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000897
Douglas Gregor84d0a192010-01-12 21:28:44 +0000898 // If we didn't end up with a typename token, there's nothing more we
899 // can do.
900 if (Tok.isNot(tok::annot_typename))
901 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000902
Douglas Gregor84d0a192010-01-12 21:28:44 +0000903 // Retrieve the type from the annotation token, consume that token, and
904 // return.
905 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000906 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000907 ConsumeToken();
908 return Type;
909 }
910
Douglas Gregor42a552f2008-11-05 20:51:48 +0000911 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000912 IdentifierInfo *CorrectedII = 0;
Douglas Gregor059101f2011-03-02 00:47:37 +0000913 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000914 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000915 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000916 /*NonTrivialTypeSourceInfo=*/true,
917 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000918 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000919 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000920 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000921 }
922
923 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000924 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000925
926 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000927 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000928 DS.SetRangeStart(IdLoc);
929 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000930 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000931
932 const char *PrevSpec = 0;
933 unsigned DiagID;
934 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
935
936 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
937 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000938}
939
John McCallc052dbb2012-05-22 21:28:12 +0000940void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
941 while (Tok.is(tok::kw___single_inheritance) ||
942 Tok.is(tok::kw___multiple_inheritance) ||
943 Tok.is(tok::kw___virtual_inheritance)) {
944 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
945 SourceLocation AttrNameLoc = ConsumeToken();
946 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000947 SourceLocation(), 0, 0, AttributeList::AS_GNU);
John McCallc052dbb2012-05-22 21:28:12 +0000948 }
949}
950
Richard Smithc9f35172012-06-25 21:37:02 +0000951/// Determine whether the following tokens are valid after a type-specifier
952/// which could be a standalone declaration. This will conservatively return
953/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith139be702012-07-02 19:14:01 +0000954bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smithc9f35172012-06-25 21:37:02 +0000955 // This switch enumerates the valid "follow" set for type-specifiers.
956 switch (Tok.getKind()) {
957 default: break;
958 case tok::semi: // struct foo {...} ;
959 case tok::star: // struct foo {...} * P;
960 case tok::amp: // struct foo {...} & R = ...
961 case tok::identifier: // struct foo {...} V ;
962 case tok::r_paren: //(struct foo {...} ) {4}
963 case tok::annot_cxxscope: // struct foo {...} a:: b;
964 case tok::annot_typename: // struct foo {...} a ::b;
965 case tok::annot_template_id: // struct foo {...} a<int> ::b;
966 case tok::l_paren: // struct foo {...} ( x);
967 case tok::comma: // __builtin_offsetof(struct foo{...} ,
968 return true;
Richard Smith139be702012-07-02 19:14:01 +0000969 case tok::colon:
970 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smithc9f35172012-06-25 21:37:02 +0000971 // Type qualifiers
972 case tok::kw_const: // struct foo {...} const x;
973 case tok::kw_volatile: // struct foo {...} volatile x;
974 case tok::kw_restrict: // struct foo {...} restrict x;
975 case tok::kw_inline: // struct foo {...} inline foo() {};
976 // Storage-class specifiers
977 case tok::kw_static: // struct foo {...} static x;
978 case tok::kw_extern: // struct foo {...} extern x;
979 case tok::kw_typedef: // struct foo {...} typedef x;
980 case tok::kw_register: // struct foo {...} register x;
981 case tok::kw_auto: // struct foo {...} auto x;
982 case tok::kw_mutable: // struct foo {...} mutable x;
983 case tok::kw_constexpr: // struct foo {...} constexpr x;
984 // As shown above, type qualifiers and storage class specifiers absolutely
985 // can occur after class specifiers according to the grammar. However,
986 // almost no one actually writes code like this. If we see one of these,
987 // it is much more likely that someone missed a semi colon and the
988 // type/storage class specifier we're seeing is part of the *next*
989 // intended declaration, as in:
990 //
991 // struct foo { ... }
992 // typedef int X;
993 //
994 // We'd really like to emit a missing semicolon error instead of emitting
995 // an error on the 'int' saying that you can't have two type specifiers in
996 // the same declaration of X. Because of this, we look ahead past this
997 // token to see if it's a type specifier. If so, we know the code is
998 // otherwise invalid, so we can produce the expected semi error.
999 if (!isKnownToBeTypeSpecifier(NextToken()))
1000 return true;
1001 break;
1002 case tok::r_brace: // struct bar { struct foo {...} }
1003 // Missing ';' at end of struct is accepted as an extension in C mode.
1004 if (!getLangOpts().CPlusPlus)
1005 return true;
1006 break;
1007 }
1008 return false;
1009}
1010
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001011/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1012/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1013/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +00001014/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001015///
1016/// class-specifier: [C++ class]
1017/// class-head '{' member-specification[opt] '}'
1018/// class-head '{' member-specification[opt] '}' attributes[opt]
1019/// class-head:
1020/// class-key identifier[opt] base-clause[opt]
1021/// class-key nested-name-specifier identifier base-clause[opt]
1022/// class-key nested-name-specifier[opt] simple-template-id
1023/// base-clause[opt]
1024/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001025/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001026/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001027/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001028/// simple-template-id base-clause[opt]
1029/// class-key:
1030/// 'class'
1031/// 'struct'
1032/// 'union'
1033///
1034/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +00001035/// class-key ::[opt] nested-name-specifier[opt] identifier
1036/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1037/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001038///
1039/// Note that the C++ class-specifier and elaborated-type-specifier,
1040/// together, subsume the C99 struct-or-union-specifier:
1041///
1042/// struct-or-union-specifier: [C99 6.7.2.1]
1043/// struct-or-union identifier[opt] '{' struct-contents '}'
1044/// struct-or-union identifier
1045/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1046/// '}' attributes[opt]
1047/// [GNU] struct-or-union attributes[opt] identifier
1048/// struct-or-union:
1049/// 'struct'
1050/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +00001051void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1052 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001053 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001054 AccessSpecifier AS,
Michael Han2e397132012-11-26 22:54:45 +00001055 bool EnteringContext, DeclSpecContext DSC,
1056 ParsedAttributesWithRange &Attributes) {
Joao Matos17d35c32012-08-31 22:18:20 +00001057 DeclSpec::TST TagType;
1058 if (TagTokKind == tok::kw_struct)
1059 TagType = DeclSpec::TST_struct;
1060 else if (TagTokKind == tok::kw___interface)
1061 TagType = DeclSpec::TST_interface;
1062 else if (TagTokKind == tok::kw_class)
1063 TagType = DeclSpec::TST_class;
1064 else {
Chris Lattner4c97d762009-04-12 21:49:30 +00001065 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1066 TagType = DeclSpec::TST_union;
1067 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001068
Douglas Gregor374929f2009-09-18 15:37:17 +00001069 if (Tok.is(tok::code_completion)) {
1070 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001071 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001072 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00001073 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001074
Chandler Carruth926c4b42010-06-28 08:39:25 +00001075 // C++03 [temp.explicit] 14.7.2/8:
1076 // The usual access checking rules do not apply to names used to specify
1077 // explicit instantiations.
1078 //
1079 // As an extension we do not perform access checking on the names used to
1080 // specify explicit specializations either. This is important to allow
1081 // specializing traits classes for private types.
John McCall13489672012-05-07 06:16:58 +00001082 //
1083 // Note that we don't suppress if this turns out to be an elaborated
1084 // type specifier.
1085 bool shouldDelayDiagsInTag =
1086 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1087 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1088 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001089
Sean Hunt2edf0a22012-06-23 05:07:58 +00001090 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001091 // If attributes exist after tag, parse them.
1092 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +00001093 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001094
Steve Narofff59e17e2008-12-24 20:59:21 +00001095 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001096 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001097 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001098
John McCallc052dbb2012-05-22 21:28:12 +00001099 // Parse inheritance specifiers.
1100 if (Tok.is(tok::kw___single_inheritance) ||
1101 Tok.is(tok::kw___multiple_inheritance) ||
1102 Tok.is(tok::kw___virtual_inheritance))
1103 ParseMicrosoftInheritanceClassAttributes(attrs);
1104
Sean Huntbbd37c62009-11-21 08:43:09 +00001105 // If C++0x attributes exist here, parse them.
1106 // FIXME: Are we consistent with the ordering of parsing of different
1107 // styles of attributes?
John McCall7f040a92010-12-24 02:08:15 +00001108 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001109
John Wiegley20c0da72011-04-27 23:09:49 +00001110 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +00001111 !Tok.is(tok::identifier) &&
1112 Tok.getIdentifierInfo() &&
1113 (Tok.is(tok::kw___is_arithmetic) ||
1114 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001115 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001116 Tok.is(tok::kw___is_floating_point) ||
1117 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001118 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001119 Tok.is(tok::kw___is_integral) ||
1120 Tok.is(tok::kw___is_member_function_pointer) ||
1121 Tok.is(tok::kw___is_member_pointer) ||
1122 Tok.is(tok::kw___is_pod) ||
1123 Tok.is(tok::kw___is_pointer) ||
1124 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001125 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001126 Tok.is(tok::kw___is_signed) ||
1127 Tok.is(tok::kw___is_unsigned) ||
1128 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001129 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001130 // name of struct templates, but some are keywords in GCC >= 4.3
1131 // and Clang. Therefore, when we see the token sequence "struct
1132 // X", make X into a normal identifier rather than a keyword, to
1133 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001134 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001135 Tok.setKind(tok::identifier);
1136 }
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001138 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001139 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001140 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001141 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1142 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001143
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001144 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001145 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001146 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001147 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1148 Diag(Tok, diag::err_expected_ident);
1149 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001150
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001151 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1152
Douglas Gregorcc636682009-02-17 23:15:12 +00001153 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001154 IdentifierInfo *Name = 0;
1155 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001156 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001157 if (Tok.is(tok::identifier)) {
1158 Name = Tok.getIdentifierInfo();
1159 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001160
David Blaikie4e4d0842012-03-11 07:00:24 +00001161 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001162 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001163 // Eat the template argument list and try to continue parsing this as
1164 // a class (or template thereof).
1165 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001166 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001167 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001168 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001169 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001170 // We couldn't parse the template argument list at all, so don't
1171 // try to give any location information for the list.
1172 LAngleLoc = RAngleLoc = SourceLocation();
1173 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001174
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001175 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matos17d35c32012-08-31 22:18:20 +00001176 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1177 << (TagType == DeclSpec::TST_class? 0
1178 : TagType == DeclSpec::TST_struct? 1
1179 : TagType == DeclSpec::TST_interface? 2
1180 : 3)
1181 << Name
1182 << SourceRange(LAngleLoc, RAngleLoc);
1183
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001184 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001185 // we've removed its template argument list.
1186 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1187 if (TemplateParams && TemplateParams->size() > 1) {
1188 TemplateParams->pop_back();
1189 } else {
1190 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001191 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001192 = ParsedTemplateInfo::NonTemplate;
1193 }
1194 } else if (TemplateInfo.Kind
1195 == ParsedTemplateInfo::ExplicitInstantiation) {
1196 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001197 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001198 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001199 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001200 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001201 = SourceLocation();
1202 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1203 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001204 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001205 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001206 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001207 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001208 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001209
Douglas Gregor059101f2011-03-02 00:47:37 +00001210 if (TemplateId->Kind != TNK_Type_template &&
1211 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001212 // The template-name in the simple-template-id refers to
1213 // something other than a class template. Give an appropriate
1214 // error message and skip to the ';'.
1215 SourceRange Range(NameLoc);
1216 if (SS.isNotEmpty())
1217 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001218
Douglas Gregor39a8de12009-02-25 19:37:18 +00001219 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1220 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Douglas Gregor39a8de12009-02-25 19:37:18 +00001222 DS.SetTypeSpecError();
1223 SkipUntil(tok::semi, false, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001224 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001225 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001226 }
1227
Richard Smith7796eb52012-03-12 08:56:40 +00001228 // There are four options here.
1229 // - If we are in a trailing return type, this is always just a reference,
1230 // and we must not try to parse a definition. For instance,
1231 // [] () -> struct S { };
1232 // does not define a type.
1233 // - If we have 'struct foo {...', 'struct foo :...',
1234 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1235 // - If we have 'struct foo;', then this is either a forward declaration
1236 // or a friend declaration, which have to be treated differently.
1237 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han2e397132012-11-26 22:54:45 +00001238 //
1239 // We also detect these erroneous cases to provide better diagnostic for
1240 // C++11 attributes parsing.
1241 // - attributes follow class name:
1242 // struct foo [[]] {};
1243 // - attributes appear before or after 'final':
1244 // struct foo [[]] final [[]] {};
1245 //
Richard Smith69730c12012-03-12 07:56:15 +00001246 // However, in type-specifier-seq's, things look like declarations but are
1247 // just references, e.g.
1248 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001249 // or
Richard Smith69730c12012-03-12 07:56:15 +00001250 // &T::operator struct s;
1251 // For these, DSC is DSC_type_specifier.
Michael Han2e397132012-11-26 22:54:45 +00001252
1253 // If there are attributes after class name, parse them.
1254 MaybeParseCXX0XAttributes(Attributes);
1255
John McCallf312b1e2010-08-26 23:41:50 +00001256 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001257 if (DSC == DSC_trailing)
1258 TUK = Sema::TUK_Reference;
1259 else if (Tok.is(tok::l_brace) ||
1260 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
1261 (isCXX0XFinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001262 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001263 if (DS.isFriendSpecified()) {
1264 // C++ [class.friend]p2:
1265 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001266 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001267 << SourceRange(DS.getFriendSpecLoc());
1268
1269 // Skip everything up to the semicolon, so that this looks like a proper
1270 // friend class (or template thereof) declaration.
1271 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001272 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001273 } else {
1274 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001275 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001276 }
Michael Han2e397132012-11-26 22:54:45 +00001277 } else if (isCXX0XFinalKeyword() && (NextToken().is(tok::l_square) ||
1278 NextToken().is(tok::kw_alignas) ||
1279 NextToken().is(tok::kw__Alignas))) {
1280 // We can't tell if this is a definition or reference
1281 // until we skipped the 'final' and C++11 attribute specifiers.
1282 TentativeParsingAction PA(*this);
1283
1284 // Skip the 'final' keyword.
1285 ConsumeToken();
1286
1287 // Skip C++11 attribute specifiers.
1288 while (true) {
1289 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1290 ConsumeBracket();
1291 if (!SkipUntil(tok::r_square))
1292 break;
1293 } else if ((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1294 NextToken().is(tok::l_paren)) {
1295 ConsumeToken();
1296 ConsumeParen();
1297 if (!SkipUntil(tok::r_paren))
1298 break;
1299 } else {
1300 break;
1301 }
1302 }
1303
1304 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1305 TUK = Sema::TUK_Definition;
1306 else
1307 TUK = Sema::TUK_Reference;
1308
1309 PA.Revert();
Richard Smithc9f35172012-06-25 21:37:02 +00001310 } else if (DSC != DSC_type_specifier &&
1311 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00001312 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallf312b1e2010-08-26 23:41:50 +00001313 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matos17d35c32012-08-31 22:18:20 +00001314 if (Tok.isNot(tok::semi)) {
1315 // A semicolon was missing after this declaration. Diagnose and recover.
1316 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1317 DeclSpec::getSpecifierName(TagType));
1318 PP.EnterToken(Tok);
1319 Tok.setKind(tok::semi);
1320 }
Richard Smithc9f35172012-06-25 21:37:02 +00001321 } else
John McCallf312b1e2010-08-26 23:41:50 +00001322 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001323
Michael Han2e397132012-11-26 22:54:45 +00001324 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1325 // to caller to handle.
1326 // FIXME: provide fix-it hints if we can.
1327 if (TUK != Sema::TUK_Reference)
1328 ProhibitAttributes(Attributes);
1329
John McCall13489672012-05-07 06:16:58 +00001330 // If this is an elaborated type specifier, and we delayed
1331 // diagnostics before, just merge them into the current pool.
1332 if (shouldDelayDiagsInTag) {
1333 diagsFromTag.done();
1334 if (TUK == Sema::TUK_Reference)
1335 diagsFromTag.redelay();
1336 }
1337
John McCall207014e2010-07-30 06:26:29 +00001338 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001339 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001340 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1341 // We have a declaration or reference to an anonymous class.
1342 Diag(StartLoc, diag::err_anon_type_definition)
1343 << DeclSpec::getSpecifierName(TagType);
1344 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001345
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001346 SkipUntil(tok::comma, true);
1347 return;
1348 }
1349
Douglas Gregorddc29e12009-02-06 22:42:48 +00001350 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001351 DeclResult TagOrTempResult = true; // invalid
1352 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001353
Douglas Gregor402abb52009-05-28 23:31:59 +00001354 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001355 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001356 // Explicit specialization, class template partial specialization,
1357 // or explicit instantiation.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001358 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001359 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001360 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001361 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001362 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001363 ProhibitAttributes(attrs);
1364
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001365 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001366 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001367 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001368 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001369 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001370 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001371 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001372 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001373 TemplateId->TemplateNameLoc,
1374 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001375 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001376 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001377 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001378
1379 // Friend template-ids are treated as references unless
1380 // they have template headers, in which case they're ill-formed
1381 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1382 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001383 } else if (TUK == Sema::TUK_Reference ||
1384 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001385 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001386 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001387 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001388 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001389 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001390 TemplateId->Template,
1391 TemplateId->TemplateNameLoc,
1392 TemplateId->LAngleLoc,
1393 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001394 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001395 } else {
1396 // This is an explicit specialization or a class template
1397 // partial specialization.
1398 TemplateParameterLists FakedParamLists;
1399
1400 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1401 // This looks like an explicit instantiation, because we have
1402 // something like
1403 //
1404 // template class Foo<X>
1405 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001406 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001407 // meant to be an explicit specialization, but the user forgot
1408 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +00001409 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001410
Mike Stump1eb44332009-09-09 15:08:12 +00001411 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001412 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001413 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001414 diag::err_explicit_instantiation_with_definition)
1415 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +00001416 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001417
1418 // Create a fake template parameter list that contains only
1419 // "template<>", so that we treat this construct as a class
1420 // template specialization.
1421 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +00001422 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001423 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001424 LAngleLoc,
1425 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001426 LAngleLoc));
1427 TemplateParams = &FakedParamLists;
1428 }
1429
1430 // Build the class template specialization.
1431 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001432 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001433 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001434 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001435 TemplateId->TemplateNameLoc,
1436 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001437 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001438 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001439 attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001440 MultiTemplateParamsArg(
Douglas Gregorcc636682009-02-17 23:15:12 +00001441 TemplateParams? &(*TemplateParams)[0] : 0,
1442 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001443 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001444 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001445 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001446 // Explicit instantiation of a member of a class template
1447 // specialization, e.g.,
1448 //
1449 // template struct Outer<int>::Inner;
1450 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001451 ProhibitAttributes(attrs);
1452
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001453 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001454 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001455 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001456 TemplateInfo.TemplateLoc,
1457 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001458 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001459 } else if (TUK == Sema::TUK_Friend &&
1460 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001461 ProhibitAttributes(attrs);
1462
John McCall9a34edb2010-10-19 01:40:49 +00001463 TagOrTempResult =
1464 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1465 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001466 Name, NameLoc, attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001467 MultiTemplateParamsArg(
John McCall9a34edb2010-10-19 01:40:49 +00001468 TemplateParams? &(*TemplateParams)[0] : 0,
1469 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001470 } else {
1471 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001472 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001473 // FIXME: Diagnose this particular error.
1474 }
1475
Sean Hunt2edf0a22012-06-23 05:07:58 +00001476 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1477 ProhibitAttributes(attrs);
1478
John McCallc4e70192009-09-11 04:59:25 +00001479 bool IsDependent = false;
1480
John McCalla25c4082010-10-19 18:40:57 +00001481 // Don't pass down template parameter lists if this is just a tag
1482 // reference. For example, we don't need the template parameters here:
1483 // template <class T> class A *makeA(T t);
1484 MultiTemplateParamsArg TParams;
1485 if (TUK != Sema::TUK_Reference && TemplateParams)
1486 TParams =
1487 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1488
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001489 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001490 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001491 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001492 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001493 TParams, Owned, IsDependent,
1494 SourceLocation(), false,
1495 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001496
1497 // If ActOnTag said the type was dependent, try again with the
1498 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001499 if (IsDependent) {
1500 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001501 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001502 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001503 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001504 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001505
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001506 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001507 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001508 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001509 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001510 isCXX0XFinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001511 if (getLangOpts().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +00001512 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001513 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001514 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001515 }
1516
John McCallb3d87482010-08-24 05:47:05 +00001517 const char *PrevSpec = 0;
1518 unsigned DiagID;
1519 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001520 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001521 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1522 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001523 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001524 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001525 Result = DS.SetTypeSpecType(TagType, StartLoc,
1526 NameLoc.isValid() ? NameLoc : StartLoc,
1527 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001528 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001529 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001530 return;
1531 }
Mike Stump1eb44332009-09-09 15:08:12 +00001532
John McCallb3d87482010-08-24 05:47:05 +00001533 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001534 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001535
Chris Lattner4ed5d912010-02-02 01:23:29 +00001536 // At this point, we've successfully parsed a class-specifier in 'definition'
1537 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1538 // going to look at what comes after it to improve error recovery. If an
1539 // impossible token occurs next, we assume that the programmer forgot a ; at
1540 // the end of the declaration and recover that way.
1541 //
Richard Smithc9f35172012-06-25 21:37:02 +00001542 // Also enforce C++ [temp]p3:
1543 // In a template-declaration which defines a class, no declarator
1544 // is permitted.
Joao Matos17d35c32012-08-31 22:18:20 +00001545 if (TUK == Sema::TUK_Definition &&
1546 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
1547 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1548 DeclSpec::getSpecifierName(TagType));
1549 // Push this token back into the preprocessor and change our current token
1550 // to ';' so that the rest of the code recovers as though there were an
1551 // ';' after the definition.
Richard Smithc9f35172012-06-25 21:37:02 +00001552 PP.EnterToken(Tok);
1553 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001554 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001555}
1556
Mike Stump1eb44332009-09-09 15:08:12 +00001557/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001558///
1559/// base-clause : [C++ class.derived]
1560/// ':' base-specifier-list
1561/// base-specifier-list:
1562/// base-specifier '...'[opt]
1563/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001564void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001565 assert(Tok.is(tok::colon) && "Not a base clause");
1566 ConsumeToken();
1567
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001568 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001569 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001570
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001571 while (true) {
1572 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001573 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001574 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001575 // Skip the rest of this base specifier, up until the comma or
1576 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001577 SkipUntil(tok::comma, tok::l_brace, true, true);
1578 } else {
1579 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001580 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001581 }
1582
1583 // If the next token is a comma, consume it and keep reading
1584 // base-specifiers.
1585 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001587 // Consume the comma.
1588 ConsumeToken();
1589 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001590
1591 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001592 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001593}
1594
1595/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1596/// one entry in the base class list of a class specifier, for example:
1597/// class foo : public bar, virtual private baz {
1598/// 'public bar' and 'virtual private baz' are each base-specifiers.
1599///
1600/// base-specifier: [C++ class.derived]
1601/// ::[opt] nested-name-specifier[opt] class-name
1602/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001603/// base-type-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001604/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001605/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001606Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001607 bool IsVirtual = false;
1608 SourceLocation StartLoc = Tok.getLocation();
1609
1610 // Parse the 'virtual' keyword.
1611 if (Tok.is(tok::kw_virtual)) {
1612 ConsumeToken();
1613 IsVirtual = true;
1614 }
1615
1616 // Parse an (optional) access specifier.
1617 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001618 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001619 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001621 // Parse the 'virtual' keyword (again!), in case it came after the
1622 // access specifier.
1623 if (Tok.is(tok::kw_virtual)) {
1624 SourceLocation VirtualLoc = ConsumeToken();
1625 if (IsVirtual) {
1626 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001627 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001628 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001629 }
1630
1631 IsVirtual = true;
1632 }
1633
Douglas Gregor42a552f2008-11-05 20:51:48 +00001634 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001635 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001636 SourceLocation BaseLoc;
1637 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001638 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001639 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001640
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001641 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1642 // actually part of the base-specifier-list grammar productions, but we
1643 // parse it here for convenience.
1644 SourceLocation EllipsisLoc;
1645 if (Tok.is(tok::ellipsis))
1646 EllipsisLoc = ConsumeToken();
1647
Mike Stump1eb44332009-09-09 15:08:12 +00001648 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001649 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001651 // Notify semantic analysis that we have parsed a complete
1652 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001653 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001654 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001655}
1656
1657/// getAccessSpecifierIfPresent - Determine whether the next token is
1658/// a C++ access-specifier.
1659///
1660/// access-specifier: [C++ class.derived]
1661/// 'private'
1662/// 'protected'
1663/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001664AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001665 switch (Tok.getKind()) {
1666 default: return AS_none;
1667 case tok::kw_private: return AS_private;
1668 case tok::kw_protected: return AS_protected;
1669 case tok::kw_public: return AS_public;
1670 }
1671}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001672
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001673/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001674/// delayed, e.g., default arguments, create a late-parsed method declaration
1675/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001676void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1677 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001678 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001679 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001680 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001681 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001682 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001683
Eli Friedmand33133c2009-07-22 21:45:50 +00001684 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1685 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1686 if (!LateMethod) {
1687 // Push this method onto the stack of late-parsed method
1688 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001689 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1690 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001691 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001692
1693 // Add all of the parameters prior to this one (they don't
1694 // have default arguments).
1695 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1696 for (unsigned I = 0; I < ParamIdx; ++I)
1697 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001698 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001699 }
1700
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001701 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001702 // not have a default argument).
1703 LateMethod->DefaultArgs.push_back(
1704 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1705 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1706 }
1707 }
1708}
1709
Richard Smith1c94c162012-01-09 22:31:44 +00001710/// isCXX0XVirtSpecifier - Determine whether the given token is a C++0x
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001711/// virt-specifier.
1712///
1713/// virt-specifier:
1714/// override
1715/// final
Richard Smith1c94c162012-01-09 22:31:44 +00001716VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001717 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001718 return VirtSpecifiers::VS_None;
1719
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001720 if (Tok.is(tok::identifier)) {
1721 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001722
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001723 // Initialize the contextual keywords.
1724 if (!Ident_final) {
1725 Ident_final = &PP.getIdentifierTable().get("final");
1726 Ident_override = &PP.getIdentifierTable().get("override");
1727 }
1728
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001729 if (II == Ident_override)
1730 return VirtSpecifiers::VS_Override;
1731
1732 if (II == Ident_final)
1733 return VirtSpecifiers::VS_Final;
1734 }
1735
1736 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001737}
1738
1739/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1740///
1741/// virt-specifier-seq:
1742/// virt-specifier
1743/// virt-specifier-seq virt-specifier
John McCalle402e722012-09-25 07:32:39 +00001744void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS,
1745 bool IsInterface) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001746 while (true) {
Anders Carlssoncc54d592011-01-22 16:56:46 +00001747 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001748 if (Specifier == VirtSpecifiers::VS_None)
1749 return;
1750
1751 // C++ [class.mem]p8:
1752 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001753 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001754 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001755 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1756 << PrevSpec
1757 << FixItHint::CreateRemoval(Tok.getLocation());
1758
John McCalle402e722012-09-25 07:32:39 +00001759 if (IsInterface && Specifier == VirtSpecifiers::VS_Final) {
1760 Diag(Tok.getLocation(), diag::err_override_control_interface)
1761 << VirtSpecifiers::getSpecifierName(Specifier);
1762 } else {
1763 Diag(Tok.getLocation(), getLangOpts().CPlusPlus0x ?
1764 diag::warn_cxx98_compat_override_control_keyword :
1765 diag::ext_override_control_keyword)
1766 << VirtSpecifiers::getSpecifierName(Specifier);
1767 }
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001768 ConsumeToken();
1769 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001770}
1771
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001772/// isCXX0XFinalKeyword - Determine whether the next token is a C++0x
1773/// contextual 'final' keyword.
1774bool Parser::isCXX0XFinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001775 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001776 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001777
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001778 if (!Tok.is(tok::identifier))
1779 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001780
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001781 // Initialize the contextual keywords.
1782 if (!Ident_final) {
1783 Ident_final = &PP.getIdentifierTable().get("final");
1784 Ident_override = &PP.getIdentifierTable().get("override");
1785 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001786
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001787 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001788}
1789
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001790/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1791///
1792/// member-declaration:
1793/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1794/// function-definition ';'[opt]
1795/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1796/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001797/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001798/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001799/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001800///
1801/// member-declarator-list:
1802/// member-declarator
1803/// member-declarator-list ',' member-declarator
1804///
1805/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001806/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001807/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001808/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001809/// identifier[opt] ':' constant-expression
1810///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001811/// virt-specifier-seq:
1812/// virt-specifier
1813/// virt-specifier-seq virt-specifier
1814///
1815/// virt-specifier:
1816/// override
1817/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001818///
Sebastian Redle2b68332009-04-12 17:16:29 +00001819/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001820/// '= 0'
1821///
1822/// constant-initializer:
1823/// '=' constant-expression
1824///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001825void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001826 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001827 const ParsedTemplateInfo &TemplateInfo,
1828 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001829 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001830 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001831 Diag(Tok, diag::err_at_defs_cxx);
1832 else
1833 Diag(Tok, diag::err_at_in_class);
1834
1835 ConsumeToken();
1836 SkipUntil(tok::r_brace);
1837 return;
1838 }
1839
John McCall60fa3cf2009-12-11 02:10:03 +00001840 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001841 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001842 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001843 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1844 if (TryAnnotateCXXScopeToken())
1845 MalformedTypeSpec = true;
1846
1847 bool isAccessDecl;
1848 if (Tok.isNot(tok::annot_cxxscope))
1849 isAccessDecl = false;
1850 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001851 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1852 else
1853 isAccessDecl = NextToken().is(tok::kw_operator);
1854
1855 if (isAccessDecl) {
1856 // Collect the scope specifier token we annotated earlier.
1857 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001858 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1859 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001860
1861 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001862 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001863 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001864 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1865 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001866 SkipUntil(tok::semi);
1867 return;
1868 }
1869
1870 // TODO: recover from mistakenly-qualified operator declarations.
1871 if (ExpectAndConsume(tok::semi,
1872 diag::err_expected_semi_after,
1873 "access declaration",
1874 tok::semi))
1875 return;
1876
Douglas Gregor23c94db2010-07-02 17:43:08 +00001877 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001878 false, SourceLocation(),
1879 SS, Name,
1880 /* AttrList */ 0,
1881 /* IsTypeName */ false,
1882 SourceLocation());
1883 return;
1884 }
1885 }
1886
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001887 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001888 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001889 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001890 SourceLocation DeclEnd;
1891 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001892 return;
1893 }
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Chris Lattner682bf922009-03-29 16:50:03 +00001895 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001896 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001897 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001898 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001899 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001900 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001901 return;
1902 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001903
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001904 // Handle: member-declaration ::= '__extension__' member-declaration
1905 if (Tok.is(tok::kw___extension__)) {
1906 // __extension__ silences extension warnings in the subexpression.
1907 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1908 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001909 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1910 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001911 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001912
Chris Lattner4ed5d912010-02-02 01:23:29 +00001913 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1914 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001915 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001916
John McCall0b7e6782011-03-24 11:26:52 +00001917 ParsedAttributesWithRange attrs(AttrFactory);
Michael Han52b501c2012-11-28 23:17:40 +00001918 ParsedAttributesWithRange FnAttrs(AttrFactory);
Sean Huntbbd37c62009-11-21 08:43:09 +00001919 // Optional C++0x attribute-specifier
John McCall7f040a92010-12-24 02:08:15 +00001920 MaybeParseCXX0XAttributes(attrs);
Michael Han52b501c2012-11-28 23:17:40 +00001921 // We need to keep these attributes for future diagnostic
1922 // before they are taken over by declaration specifier.
1923 FnAttrs.addAll(attrs.getList());
1924 FnAttrs.Range = attrs.Range;
1925
John McCall7f040a92010-12-24 02:08:15 +00001926 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001927
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001928 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00001929 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001931 // Eat 'using'.
1932 SourceLocation UsingLoc = ConsumeToken();
1933
1934 if (Tok.is(tok::kw_namespace)) {
1935 Diag(UsingLoc, diag::err_using_namespace_in_class);
1936 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001937 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001938 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001939 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00001940 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1941 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001942 }
1943 return;
1944 }
1945
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001946 // Hold late-parsed attributes so we can attach a Decl to them later.
1947 LateParsedAttrList CommonLateParsedAttrs;
1948
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001949 // decl-specifier-seq:
1950 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001951 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00001952 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00001953 if (MalformedTypeSpec)
1954 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001955 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
1956 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001957
Benjamin Kramer5354e772012-08-23 23:38:35 +00001958 MultiTemplateParamsArg TemplateParams(
John McCalldd4a3b02009-09-16 22:47:08 +00001959 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1960 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1961
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001962 if (Tok.is(tok::semi)) {
1963 ConsumeToken();
Michael Han52b501c2012-11-28 23:17:40 +00001964
1965 if (DS.isFriendSpecified())
1966 ProhibitAttributes(FnAttrs);
1967
John McCalld226f652010-08-21 09:40:31 +00001968 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00001969 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00001970 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001971 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001972 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001973
John McCall54abf7d2009-11-04 02:18:39 +00001974 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00001975 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001976
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001977 // Hold late-parsed attributes so we can attach a Decl to them later.
1978 LateParsedAttrList LateParsedAttrs;
1979
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001980 SourceLocation EqualLoc;
1981 bool HasInitializer = false;
1982 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001983 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001984 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1985 ColonProtectionRAIIObject X(*this);
1986
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001987 // Parse the first declarator.
1988 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00001989 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001990 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001991 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00001992 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001993 if (Tok.is(tok::semi))
1994 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001995 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001996 }
1997
John McCalle402e722012-09-25 07:32:39 +00001998 ParseOptionalCXX0XVirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber48673472011-01-28 06:07:34 +00001999
John Thompson1b2fc0f2009-11-25 22:58:06 +00002000 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002001 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00002002
Francois Pichet6a247472011-05-11 02:14:46 +00002003 // MSVC permits pure specifier on inline functions declared at class scope.
2004 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00002005 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00002006 DeclaratorInfo.isFunctionDeclarator() &&
2007 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002008 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00002009 Init = ParseInitializer();
2010 if (Init.isInvalid())
2011 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002012 else
2013 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00002014 }
2015
Douglas Gregor45fa5602011-11-07 20:56:01 +00002016 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002017 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00002018 //
2019 // In C++11, a non-function declarator followed by an open brace is a
2020 // braced-init-list for an in-class member initialization, not an
2021 // erroneous function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00002022 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus0x) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002023 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002024 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00002025 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002026 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002027 } else if (Tok.is(tok::equal)) {
2028 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00002029 if (KW.is(tok::kw_default))
2030 DefinitionKind = FDK_Defaulted;
2031 else if (KW.is(tok::kw_delete))
2032 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00002033 }
2034 }
2035
Michael Han52b501c2012-11-28 23:17:40 +00002036 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2037 // to a friend declaration, that declaration shall be a definition.
2038 if (DeclaratorInfo.isFunctionDeclarator() &&
2039 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2040 // Diagnose attributes that appear before decl specifier:
2041 // [[]] friend int foo();
2042 ProhibitAttributes(FnAttrs);
2043 }
2044
Douglas Gregor45fa5602011-11-07 20:56:01 +00002045 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002046 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002047 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002048 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00002049 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Michael Han52b501c2012-11-28 23:17:40 +00002050
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002051 // Consume the optional ';'
2052 if (Tok.is(tok::semi))
2053 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002054 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002055 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002056
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002057 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002058 Diag(DeclaratorInfo.getIdentifierLoc(),
2059 diag::err_function_declared_typedef);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002060
Richard Smith6f9a4452012-11-15 22:54:20 +00002061 // Recover by treating the 'typedef' as spurious.
2062 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002063 }
2064
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002065 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002066 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00002067 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002068
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002069 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2070 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2071 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002072 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002073 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002074 }
2075 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00002076
2077 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002078 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00002079 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002080
Chris Lattner682bf922009-03-29 16:50:03 +00002081 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002082 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002083 }
2084
2085 // member-declarator-list:
2086 // member-declarator
2087 // member-declarator-list ',' member-declarator
2088
Chris Lattner5f9e2722011-07-23 10:55:15 +00002089 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00002090 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00002091 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002092
2093 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002094 // member-declarator:
2095 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002096 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002097 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002098 if (Tok.is(tok::colon)) {
2099 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002100 BitfieldSize = ParseConstantExpression();
2101 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002102 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002103 }
Mike Stump1eb44332009-09-09 15:08:12 +00002104
Chris Lattnere6563252010-06-13 05:34:18 +00002105 // If a simple-asm-expr is present, parse it.
2106 if (Tok.is(tok::kw_asm)) {
2107 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002108 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002109 if (AsmLabel.isInvalid())
2110 SkipUntil(tok::comma, true, true);
2111
2112 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2113 DeclaratorInfo.SetRangeEnd(Loc);
2114 }
2115
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002116 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002117 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002118
Richard Smith7a614d82011-06-11 17:19:42 +00002119 // FIXME: When g++ adds support for this, we'll need to check whether it
2120 // goes before or after the GNU attributes and __asm__.
John McCalle402e722012-09-25 07:32:39 +00002121 ParseOptionalCXX0XVirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith7a614d82011-06-11 17:19:42 +00002122
Richard Smithca523302012-06-10 03:12:00 +00002123 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002124 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002125 if (BitfieldSize.get()) {
2126 Diag(Tok, diag::err_bitfield_member_init);
2127 SkipUntil(tok::comma, true, true);
2128 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002129 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002130 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2131 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2132 != DeclSpec::SCS_static &&
2133 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2134 != DeclSpec::SCS_typedef)
2135 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002136 }
2137 }
2138
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002139 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002140 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002141 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002142
John McCalld226f652010-08-21 09:40:31 +00002143 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002144 if (DS.isFriendSpecified()) {
Michael Han52b501c2012-11-28 23:17:40 +00002145 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2146 // to a friend declaration, that declaration shall be a definition.
2147 //
2148 // Diagnose attributes appear after friend member function declarator:
2149 // foo [[]] ();
2150 SmallVector<SourceRange, 4> Ranges;
2151 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2152 if (!Ranges.empty()) {
2153 for (SmallVector<SourceRange, 4>::iterator I = Ranges.begin(),
2154 E = Ranges.end(); I != E; ++I) {
2155 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2156 << *I;
2157 }
2158 }
2159
John McCallbbbcdd92009-09-11 21:02:39 +00002160 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002161 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002162 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002163 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002164 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002165 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002166 TemplateParams,
John McCall67d1a672009-08-06 02:15:43 +00002167 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002168 VS, HasInClassInit);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002169 if (AccessAttrs)
2170 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2171 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002172 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002173
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002174 // Set the Decl for any late parsed attributes
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002175 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2176 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2177 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002178 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002179 LateParsedAttrs[i]->addDecl(ThisDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002180 }
2181 LateParsedAttrs.clear();
2182
Douglas Gregor147545d2011-10-10 14:49:18 +00002183 // Handle the initializer.
Richard Smithca523302012-06-10 03:12:00 +00002184 if (HasInClassInit != ICIS_NoInit) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002185 // The initializer was deferred; parse it and cache the tokens.
David Blaikie4e4d0842012-03-11 07:00:24 +00002186 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00002187 diag::warn_cxx98_compat_nonstatic_member_init :
2188 diag::ext_nonstatic_member_init);
2189
Richard Smith7a614d82011-06-11 17:19:42 +00002190 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002191 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2192 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002193 //
2194 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002195 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002196 Diag(Tok, diag::err_incomplete_array_member_init);
2197 SkipUntil(tok::comma, true, true);
David Blaikie3164c142012-02-14 09:00:46 +00002198 if (ThisDecl)
2199 // Avoid later warnings about a class member of incomplete type.
2200 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002201 } else
2202 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002203 } else if (HasInitializer) {
2204 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002205 if (!Init.isUsable())
Douglas Gregor552e2992012-02-21 02:22:07 +00002206 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002207 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2208
Douglas Gregor147545d2011-10-10 14:49:18 +00002209 if (Init.isInvalid())
2210 SkipUntil(tok::comma, true, true);
2211 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002212 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002213 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor147545d2011-10-10 14:49:18 +00002214 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2215 // No initializer.
2216 Actions.ActOnUninitializedDecl(ThisDecl,
2217 DS.getTypeSpecType() == DeclSpec::TST_auto);
Richard Smith7a614d82011-06-11 17:19:42 +00002218 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002219
2220 if (ThisDecl) {
2221 Actions.FinalizeDeclaration(ThisDecl);
2222 DeclsInGroup.push_back(ThisDecl);
2223 }
2224
Richard Smithe5310012012-04-29 07:31:09 +00002225 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor147545d2011-10-10 14:49:18 +00002226 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2227 != DeclSpec::SCS_typedef) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002228 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002229 }
2230
2231 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002232
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002233 // If we don't have a comma, it is either the end of the list (a ';')
2234 // or an error, bail out.
2235 if (Tok.isNot(tok::comma))
2236 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002237
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002238 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002239 SourceLocation CommaLoc = ConsumeToken();
2240
2241 if (Tok.isAtStartOfLine() &&
2242 !MightBeDeclarator(Declarator::MemberContext)) {
2243 // This comma was followed by a line-break and something which can't be
2244 // the start of a declarator. The comma was probably a typo for a
2245 // semicolon.
2246 Diag(CommaLoc, diag::err_expected_semi_declaration)
2247 << FixItHint::CreateReplacement(CommaLoc, ";");
2248 ExpectSemi = false;
2249 break;
2250 }
Mike Stump1eb44332009-09-09 15:08:12 +00002251
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002252 // Parse the next declarator.
2253 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002254 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002255 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002256 Init = true;
2257 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002258 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002259
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002260 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002261 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002262
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002263 if (Tok.isNot(tok::colon))
2264 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002265 }
2266
Richard Smith1c94c162012-01-09 22:31:44 +00002267 if (ExpectSemi &&
2268 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002269 // Skip to end of block or statement.
2270 SkipUntil(tok::r_brace, true, true);
2271 // If we stopped at a ';', eat it.
2272 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002273 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002274 }
2275
Douglas Gregor23c94db2010-07-02 17:43:08 +00002276 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00002277 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002278}
2279
Richard Smith7a614d82011-06-11 17:19:42 +00002280/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2281/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2282/// function definition. The location of the '=', if any, will be placed in
2283/// EqualLoc.
2284///
2285/// pure-specifier:
2286/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002287///
Richard Smith7a614d82011-06-11 17:19:42 +00002288/// brace-or-equal-initializer:
2289/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002290/// braced-init-list
2291///
Richard Smith7a614d82011-06-11 17:19:42 +00002292/// initializer-clause:
2293/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002294/// braced-init-list
2295///
Richard Smith7a614d82011-06-11 17:19:42 +00002296/// defaulted/deleted function-definition:
2297/// '=' 'default'
2298/// '=' 'delete'
2299///
2300/// Prior to C++0x, the assignment-expression in an initializer-clause must
2301/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002302ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002303 SourceLocation &EqualLoc) {
2304 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2305 && "Data member initializer not starting with '=' or '{'");
2306
Douglas Gregor552e2992012-02-21 02:22:07 +00002307 EnterExpressionEvaluationContext Context(Actions,
2308 Sema::PotentiallyEvaluated,
2309 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002310 if (Tok.is(tok::equal)) {
2311 EqualLoc = ConsumeToken();
2312 if (Tok.is(tok::kw_delete)) {
2313 // In principle, an initializer of '= delete p;' is legal, but it will
2314 // never type-check. It's better to diagnose it as an ill-formed expression
2315 // than as an ill-formed deleted non-function member.
2316 // An initializer of '= delete p, foo' will never be parsed, because
2317 // a top-level comma always ends the initializer expression.
2318 const Token &Next = NextToken();
2319 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2320 Next.is(tok::eof)) {
2321 if (IsFunction)
2322 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2323 << 1 /* delete */;
2324 else
2325 Diag(ConsumeToken(), diag::err_deleted_non_function);
2326 return ExprResult();
2327 }
2328 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002329 if (IsFunction)
2330 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2331 << 0 /* default */;
2332 else
2333 Diag(ConsumeToken(), diag::err_default_special_members);
2334 return ExprResult();
2335 }
2336
Sebastian Redl33deb352012-02-22 10:50:08 +00002337 }
2338 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002339}
2340
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002341/// ParseCXXMemberSpecification - Parse the class definition.
2342///
2343/// member-specification:
2344/// member-declaration member-specification[opt]
2345/// access-specifier ':' member-specification[opt]
2346///
Joao Matos17d35c32012-08-31 22:18:20 +00002347void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
2348 unsigned TagType, Decl *TagDecl) {
2349 assert((TagType == DeclSpec::TST_struct ||
2350 TagType == DeclSpec::TST_interface ||
2351 TagType == DeclSpec::TST_union ||
2352 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2353
John McCallf312b1e2010-08-26 23:41:50 +00002354 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2355 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002356
Douglas Gregor26997fd2010-01-16 20:52:59 +00002357 // Determine whether this is a non-nested class. Note that local
2358 // classes are *not* considered to be nested classes.
2359 bool NonNestedClass = true;
2360 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002361 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002362 if (S->isClassScope()) {
2363 // We're inside a class scope, so this is a nested class.
2364 NonNestedClass = false;
John McCalle402e722012-09-25 07:32:39 +00002365
2366 // The Microsoft extension __interface does not permit nested classes.
2367 if (getCurrentClass().IsInterface) {
2368 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2369 << /*ErrorType=*/6
2370 << (isa<NamedDecl>(TagDecl)
2371 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2372 : "<anonymous>");
2373 }
Douglas Gregor26997fd2010-01-16 20:52:59 +00002374 break;
2375 }
2376
2377 if ((S->getFlags() & Scope::FnScope)) {
2378 // If we're in a function or function template declared in the
2379 // body of a class, then this is a local class rather than a
2380 // nested class.
2381 const Scope *Parent = S->getParent();
2382 if (Parent->isTemplateParamScope())
2383 Parent = Parent->getParent();
2384 if (Parent->isClassScope())
2385 break;
2386 }
2387 }
2388 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002389
2390 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002391 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002392
Douglas Gregor6569d682009-05-27 23:11:45 +00002393 // Note that we are parsing a new (potentially-nested) class definition.
John McCalle402e722012-09-25 07:32:39 +00002394 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2395 TagType == DeclSpec::TST_interface);
Douglas Gregor6569d682009-05-27 23:11:45 +00002396
Douglas Gregorddc29e12009-02-06 22:42:48 +00002397 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002398 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002399
Anders Carlssonb184a182011-03-25 14:46:08 +00002400 SourceLocation FinalLoc;
2401
2402 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002403 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith8b11b5e2011-10-15 04:21:46 +00002404 assert(isCXX0XFinalKeyword() && "not a class definition");
2405 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002406
John McCalle402e722012-09-25 07:32:39 +00002407 if (TagType == DeclSpec::TST_interface) {
2408 Diag(FinalLoc, diag::err_override_control_interface)
2409 << "final";
2410 } else {
2411 Diag(FinalLoc, getLangOpts().CPlusPlus0x ?
2412 diag::warn_cxx98_compat_override_control_keyword :
2413 diag::ext_override_control_keyword) << "final";
2414 }
Michael Han2e397132012-11-26 22:54:45 +00002415
2416 // Forbid C++11 attributes that appear here.
2417 ParsedAttributesWithRange Attrs(AttrFactory);
2418 MaybeParseCXX0XAttributes(Attrs);
2419 ProhibitAttributes(Attrs);
Anders Carlssonb184a182011-03-25 14:46:08 +00002420 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002421
John McCallbd0dfa52009-12-19 21:48:58 +00002422 if (Tok.is(tok::colon)) {
2423 ParseBaseClause(TagDecl);
2424
2425 if (!Tok.is(tok::l_brace)) {
2426 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002427
2428 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002429 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002430 return;
2431 }
2432 }
2433
2434 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002435 BalancedDelimiterTracker T(*this, tok::l_brace);
2436 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002437
John McCall42a4f662010-05-28 08:11:17 +00002438 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002439 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002440 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002441
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002442 // C++ 11p3: Members of a class defined with the keyword class are private
2443 // by default. Members of a class defined with the keywords struct or union
2444 // are public by default.
2445 AccessSpecifier CurAS;
2446 if (TagType == DeclSpec::TST_class)
2447 CurAS = AS_private;
2448 else
2449 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002450 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002451
Douglas Gregor07976d22010-06-21 22:31:09 +00002452 if (TagDecl) {
2453 // While we still have something to read, read the member-declarations.
2454 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2455 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002456
David Blaikie4e4d0842012-03-11 07:00:24 +00002457 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002458 Tok.is(tok::kw___if_not_exists))) {
2459 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2460 continue;
2461 }
2462
Douglas Gregor07976d22010-06-21 22:31:09 +00002463 // Check for extraneous top-level semicolon.
2464 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002465 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002466 continue;
2467 }
2468
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002469 if (Tok.is(tok::annot_pragma_vis)) {
2470 HandlePragmaVisibility();
2471 continue;
2472 }
2473
2474 if (Tok.is(tok::annot_pragma_pack)) {
2475 HandlePragmaPack();
2476 continue;
2477 }
2478
Argyrios Kyrtzidisf4deaef2012-10-12 17:39:59 +00002479 if (Tok.is(tok::annot_pragma_align)) {
2480 HandlePragmaAlign();
2481 continue;
2482 }
2483
Douglas Gregor07976d22010-06-21 22:31:09 +00002484 AccessSpecifier AS = getAccessSpecifierIfPresent();
2485 if (AS != AS_none) {
2486 // Current token is a C++ access specifier.
2487 CurAS = AS;
2488 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002489 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002490 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002491 AccessAttrs.clear();
2492 MaybeParseGNUAttributes(AccessAttrs);
2493
David Blaikie13f8daf2011-10-13 06:08:43 +00002494 SourceLocation EndLoc;
2495 if (Tok.is(tok::colon)) {
2496 EndLoc = Tok.getLocation();
2497 ConsumeToken();
2498 } else if (Tok.is(tok::semi)) {
2499 EndLoc = Tok.getLocation();
2500 ConsumeToken();
2501 Diag(EndLoc, diag::err_expected_colon)
2502 << FixItHint::CreateReplacement(EndLoc, ":");
2503 } else {
2504 EndLoc = ASLoc.getLocWithOffset(TokLength);
2505 Diag(EndLoc, diag::err_expected_colon)
2506 << FixItHint::CreateInsertion(EndLoc, ":");
2507 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002508
John McCalle402e722012-09-25 07:32:39 +00002509 // The Microsoft extension __interface does not permit non-public
2510 // access specifiers.
2511 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2512 Diag(ASLoc, diag::err_access_specifier_interface)
2513 << (CurAS == AS_protected);
2514 }
2515
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002516 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2517 AccessAttrs.getList())) {
2518 // found another attribute than only annotations
2519 AccessAttrs.clear();
2520 }
2521
Douglas Gregor07976d22010-06-21 22:31:09 +00002522 continue;
2523 }
2524
2525 // FIXME: Make sure we don't have a template here.
2526
2527 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002528 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002529 }
2530
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002531 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002532 } else {
2533 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002534 }
Mike Stump1eb44332009-09-09 15:08:12 +00002535
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002536 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002537 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002538 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002539
John McCall42a4f662010-05-28 08:11:17 +00002540 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002541 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002542 T.getOpenLocation(),
2543 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002544 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002545
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002546 // C++11 [class.mem]p2:
2547 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002548 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002549 // brace-or-equal-initializers for non-static data members (including such
2550 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002551 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002552 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002553 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002554 // declarations and the lexed inline method definitions, along with any
2555 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002556 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002557 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002558 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002559
2560 // We've finished with all pending member declarations.
2561 Actions.ActOnFinishCXXMemberDecls();
2562
Richard Smith7a614d82011-06-11 17:19:42 +00002563 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002564 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002565 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002566 }
2567
John McCall42a4f662010-05-28 08:11:17 +00002568 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002569 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2570 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002571
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002572 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002573 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002574 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002575}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002576
2577/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2578/// which explicitly initializes the members or base classes of a
2579/// class (C++ [class.base.init]). For example, the three initializers
2580/// after the ':' in the Derived constructor below:
2581///
2582/// @code
2583/// class Base { };
2584/// class Derived : Base {
2585/// int x;
2586/// float f;
2587/// public:
2588/// Derived(float f) : Base(), x(17), f(f) { }
2589/// };
2590/// @endcode
2591///
Mike Stump1eb44332009-09-09 15:08:12 +00002592/// [C++] ctor-initializer:
2593/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002594///
Mike Stump1eb44332009-09-09 15:08:12 +00002595/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002596/// mem-initializer ...[opt]
2597/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002598void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002599 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2600
John Wiegley28bbe4b2011-04-28 01:08:34 +00002601 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2602 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002603 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002604
Chris Lattner5f9e2722011-07-23 10:55:15 +00002605 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002606 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002607
Douglas Gregor7ad83902008-11-05 04:29:56 +00002608 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002609 if (Tok.is(tok::code_completion)) {
2610 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2611 MemInitializers.data(),
2612 MemInitializers.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002613 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002614 } else {
2615 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2616 if (!MemInit.isInvalid())
2617 MemInitializers.push_back(MemInit.get());
2618 else
2619 AnyErrors = true;
2620 }
2621
Douglas Gregor7ad83902008-11-05 04:29:56 +00002622 if (Tok.is(tok::comma))
2623 ConsumeToken();
2624 else if (Tok.is(tok::l_brace))
2625 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002626 // If the next token looks like a base or member initializer, assume that
2627 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002628 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2629 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2630 Diag(Loc, diag::err_ctor_init_missing_comma)
2631 << FixItHint::CreateInsertion(Loc, ", ");
2632 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002633 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002634 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002635 SkipUntil(tok::l_brace, true, true);
2636 break;
2637 }
2638 } while (true);
2639
Mike Stump1eb44332009-09-09 15:08:12 +00002640 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002641 MemInitializers.data(), MemInitializers.size(),
2642 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002643}
2644
2645/// ParseMemInitializer - Parse a C++ member initializer, which is
2646/// part of a constructor initializer that explicitly initializes one
2647/// member or base class (C++ [class.base.init]). See
2648/// ParseConstructorInitializer for an example.
2649///
2650/// [C++] mem-initializer:
2651/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002652/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002653///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002654/// [C++] mem-initializer-id:
2655/// '::'[opt] nested-name-specifier[opt] class-name
2656/// identifier
John McCalld226f652010-08-21 09:40:31 +00002657Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002658 // parse '::'[opt] nested-name-specifier[opt]
2659 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002660 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002661 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002662 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002663 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002664 if (TemplateId->Kind == TNK_Type_template ||
2665 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002666 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002667 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002668 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002669 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002670 }
David Blaikief2116622012-01-24 06:03:59 +00002671 // Uses of decltype will already have been converted to annot_decltype by
2672 // ParseOptionalCXXScopeSpecifier at this point.
2673 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2674 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002675 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002676 return true;
2677 }
Mike Stump1eb44332009-09-09 15:08:12 +00002678
David Blaikief2116622012-01-24 06:03:59 +00002679 IdentifierInfo *II = 0;
2680 DeclSpec DS(AttrFactory);
2681 SourceLocation IdLoc = Tok.getLocation();
2682 if (Tok.is(tok::annot_decltype)) {
2683 // Get the decltype expression, if there is one.
2684 ParseDecltypeSpecifier(DS);
2685 } else {
2686 if (Tok.is(tok::identifier))
2687 // Get the identifier. This may be a member name or a class name,
2688 // but we'll let the semantic analysis determine which it is.
2689 II = Tok.getIdentifierInfo();
2690 ConsumeToken();
2691 }
2692
Douglas Gregor7ad83902008-11-05 04:29:56 +00002693
2694 // Parse the '('.
David Blaikie4e4d0842012-03-11 07:00:24 +00002695 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002696 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2697
Sebastian Redl6df65482011-09-24 17:48:25 +00002698 ExprResult InitList = ParseBraceInitializer();
2699 if (InitList.isInvalid())
2700 return true;
2701
2702 SourceLocation EllipsisLoc;
2703 if (Tok.is(tok::ellipsis))
2704 EllipsisLoc = ConsumeToken();
2705
2706 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002707 TemplateTypeTy, DS, IdLoc,
2708 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002709 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002710 BalancedDelimiterTracker T(*this, tok::l_paren);
2711 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002712
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002713 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002714 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002715 CommaLocsTy CommaLocs;
2716 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2717 SkipUntil(tok::r_paren);
2718 return true;
2719 }
2720
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002721 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002722
2723 SourceLocation EllipsisLoc;
2724 if (Tok.is(tok::ellipsis))
2725 EllipsisLoc = ConsumeToken();
2726
2727 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002728 TemplateTypeTy, DS, IdLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002729 T.getOpenLocation(), ArgExprs.data(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002730 ArgExprs.size(), T.getCloseLocation(),
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002731 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002732 }
2733
David Blaikie4e4d0842012-03-11 07:00:24 +00002734 Diag(Tok, getLangOpts().CPlusPlus0x ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002735 : diag::err_expected_lparen);
2736 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002737}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002738
Sebastian Redl7acafd02011-03-05 14:45:16 +00002739/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002740///
Douglas Gregora4745612008-12-01 18:00:20 +00002741/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002742/// dynamic-exception-specification
2743/// noexcept-specification
2744///
2745/// noexcept-specification:
2746/// 'noexcept'
2747/// 'noexcept' '(' constant-expression ')'
2748ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002749Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002750 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002751 SmallVectorImpl<ParsedType> &DynamicExceptions,
2752 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002753 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002754 ExceptionSpecificationType Result = EST_None;
2755
2756 // See if there's a dynamic specification.
2757 if (Tok.is(tok::kw_throw)) {
2758 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2759 DynamicExceptions,
2760 DynamicExceptionRanges);
2761 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2762 "Produced different number of exception types and ranges.");
2763 }
2764
2765 // If there's no noexcept specification, we're done.
2766 if (Tok.isNot(tok::kw_noexcept))
2767 return Result;
2768
Richard Smith841804b2011-10-17 23:06:20 +00002769 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2770
Sebastian Redl7acafd02011-03-05 14:45:16 +00002771 // If we already had a dynamic specification, parse the noexcept for,
2772 // recovery, but emit a diagnostic and don't store the results.
2773 SourceRange NoexceptRange;
2774 ExceptionSpecificationType NoexceptType = EST_None;
2775
2776 SourceLocation KeywordLoc = ConsumeToken();
2777 if (Tok.is(tok::l_paren)) {
2778 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002779 BalancedDelimiterTracker T(*this, tok::l_paren);
2780 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002781 NoexceptType = EST_ComputedNoexcept;
2782 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002783 // The argument must be contextually convertible to bool. We use
2784 // ActOnBooleanCondition for this purpose.
2785 if (!NoexceptExpr.isInvalid())
2786 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2787 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002788 T.consumeClose();
2789 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002790 } else {
2791 // There is no argument.
2792 NoexceptType = EST_BasicNoexcept;
2793 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2794 }
2795
2796 if (Result == EST_None) {
2797 SpecificationRange = NoexceptRange;
2798 Result = NoexceptType;
2799
2800 // If there's a dynamic specification after a noexcept specification,
2801 // parse that and ignore the results.
2802 if (Tok.is(tok::kw_throw)) {
2803 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2804 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2805 DynamicExceptionRanges);
2806 }
2807 } else {
2808 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2809 }
2810
2811 return Result;
2812}
2813
2814/// ParseDynamicExceptionSpecification - Parse a C++
2815/// dynamic-exception-specification (C++ [except.spec]).
2816///
2817/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002818/// 'throw' '(' type-id-list [opt] ')'
2819/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002820///
Douglas Gregora4745612008-12-01 18:00:20 +00002821/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002822/// type-id ... [opt]
2823/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002824///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002825ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2826 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002827 SmallVectorImpl<ParsedType> &Exceptions,
2828 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002829 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002830
Sebastian Redl7acafd02011-03-05 14:45:16 +00002831 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002832 BalancedDelimiterTracker T(*this, tok::l_paren);
2833 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002834 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2835 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002836 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002837 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002838
Douglas Gregora4745612008-12-01 18:00:20 +00002839 // Parse throw(...), a Microsoft extension that means "this function
2840 // can throw anything".
2841 if (Tok.is(tok::ellipsis)) {
2842 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002843 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002844 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002845 T.consumeClose();
2846 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002847 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002848 }
2849
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002850 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002851 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002852 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002853 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002854
Douglas Gregora04426c2010-12-20 23:57:46 +00002855 if (Tok.is(tok::ellipsis)) {
2856 // C++0x [temp.variadic]p5:
2857 // - In a dynamic-exception-specification (15.4); the pattern is a
2858 // type-id.
2859 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002860 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002861 if (!Res.isInvalid())
2862 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2863 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002864
Sebastian Redlef65f062009-05-29 18:02:33 +00002865 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002866 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002867 Ranges.push_back(Range);
2868 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002869
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002870 if (Tok.is(tok::comma))
2871 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002872 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002873 break;
2874 }
2875
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002876 T.consumeClose();
2877 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002878 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002879}
Douglas Gregor6569d682009-05-27 23:11:45 +00002880
Douglas Gregordab60ad2010-10-01 18:44:50 +00002881/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2882/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002883TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002884 assert(Tok.is(tok::arrow) && "expected arrow");
2885
2886 ConsumeToken();
2887
Richard Smith7796eb52012-03-12 08:56:40 +00002888 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002889}
2890
Douglas Gregor6569d682009-05-27 23:11:45 +00002891/// \brief We have just started parsing the definition of a new class,
2892/// so push that class onto our stack of classes that is currently
2893/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002894Sema::ParsingClassState
John McCalle402e722012-09-25 07:32:39 +00002895Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
2896 bool IsInterface) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002897 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002898 "Nested class without outer class");
John McCalle402e722012-09-25 07:32:39 +00002899 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCalleee1d542011-02-14 07:13:47 +00002900 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002901}
2902
2903/// \brief Deallocate the given parsed class and all of its nested
2904/// classes.
2905void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002906 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2907 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002908 delete Class;
2909}
2910
2911/// \brief Pop the top class of the stack of classes that are
2912/// currently being parsed.
2913///
2914/// This routine should be called when we have finished parsing the
2915/// definition of a class, but have not yet popped the Scope
2916/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00002917void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002918 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002919
John McCalleee1d542011-02-14 07:13:47 +00002920 Actions.PopParsingClass(state);
2921
Douglas Gregor6569d682009-05-27 23:11:45 +00002922 ParsingClass *Victim = ClassStack.top();
2923 ClassStack.pop();
2924 if (Victim->TopLevelClass) {
2925 // Deallocate all of the nested classes of this class,
2926 // recursively: we don't need to keep any of this information.
2927 DeallocateParsedClasses(Victim);
2928 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002929 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002930 assert(!ClassStack.empty() && "Missing top-level class?");
2931
Douglas Gregord54eb442010-10-12 16:25:54 +00002932 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002933 // The victim is a nested class, but we will not need to perform
2934 // any processing after the definition of this class since it has
2935 // no members whose handling was delayed. Therefore, we can just
2936 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002937 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002938 return;
2939 }
2940
2941 // This nested class has some members that will need to be processed
2942 // after the top-level class is completely defined. Therefore, add
2943 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002944 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002945 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002946 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00002947}
Sean Huntbbd37c62009-11-21 08:43:09 +00002948
Richard Smithc56298d2012-04-10 03:25:07 +00002949/// \brief Try to parse an 'identifier' which appears within an attribute-token.
2950///
2951/// \return the parsed identifier on success, and 0 if the next token is not an
2952/// attribute-token.
2953///
2954/// C++11 [dcl.attr.grammar]p3:
2955/// If a keyword or an alternative token that satisfies the syntactic
2956/// requirements of an identifier is contained in an attribute-token,
2957/// it is considered an identifier.
2958IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
2959 switch (Tok.getKind()) {
2960 default:
2961 // Identifiers and keywords have identifier info attached.
2962 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
2963 Loc = ConsumeToken();
2964 return II;
2965 }
2966 return 0;
2967
2968 case tok::ampamp: // 'and'
2969 case tok::pipe: // 'bitor'
2970 case tok::pipepipe: // 'or'
2971 case tok::caret: // 'xor'
2972 case tok::tilde: // 'compl'
2973 case tok::amp: // 'bitand'
2974 case tok::ampequal: // 'and_eq'
2975 case tok::pipeequal: // 'or_eq'
2976 case tok::caretequal: // 'xor_eq'
2977 case tok::exclaim: // 'not'
2978 case tok::exclaimequal: // 'not_eq'
2979 // Alternative tokens do not have identifier info, but their spelling
2980 // starts with an alphabetical character.
2981 llvm::SmallString<8> SpellingBuf;
2982 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
2983 if (std::isalpha(Spelling[0])) {
2984 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00002985 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00002986 }
2987 return 0;
2988 }
2989}
2990
Michael Han6880f492012-10-03 01:56:22 +00002991static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
2992 IdentifierInfo *ScopeName) {
2993 switch (AttributeList::getKind(AttrName, ScopeName,
2994 AttributeList::AS_CXX11)) {
2995 case AttributeList::AT_CarriesDependency:
2996 case AttributeList::AT_FallThrough:
2997 case AttributeList::AT_NoReturn: {
2998 return true;
2999 }
3000
3001 default:
3002 return false;
3003 }
3004}
3005
Richard Smithc56298d2012-04-10 03:25:07 +00003006/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003007/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00003008///
Richard Smith6ee326a2012-04-10 01:32:12 +00003009/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00003010/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003011/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00003012///
Richard Smith6ee326a2012-04-10 01:32:12 +00003013/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00003014/// attribute[opt]
3015/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00003016/// attribute '...'
3017/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00003018///
Richard Smith6ee326a2012-04-10 01:32:12 +00003019/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00003020/// attribute-token attribute-argument-clause[opt]
3021///
Richard Smith6ee326a2012-04-10 01:32:12 +00003022/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003023/// identifier
3024/// attribute-scoped-token
3025///
Richard Smith6ee326a2012-04-10 01:32:12 +00003026/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003027/// attribute-namespace '::' identifier
3028///
Richard Smith6ee326a2012-04-10 01:32:12 +00003029/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00003030/// identifier
3031///
Richard Smith6ee326a2012-04-10 01:32:12 +00003032/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00003033/// '(' balanced-token-seq ')'
3034///
Richard Smith6ee326a2012-04-10 01:32:12 +00003035/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00003036/// balanced-token
3037/// balanced-token-seq balanced-token
3038///
Richard Smith6ee326a2012-04-10 01:32:12 +00003039/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003040/// '(' balanced-token-seq ')'
3041/// '[' balanced-token-seq ']'
3042/// '{' balanced-token-seq '}'
3043/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00003044void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003045 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003046 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00003047 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003048 ParseAlignmentSpecifier(attrs, endLoc);
3049 return;
3050 }
3051
Sean Huntbbd37c62009-11-21 08:43:09 +00003052 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00003053 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00003054
Richard Smith41be6732011-10-14 20:48:27 +00003055 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3056
Sean Huntbbd37c62009-11-21 08:43:09 +00003057 ConsumeBracket();
3058 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003059
Richard Smithc56298d2012-04-10 03:25:07 +00003060 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003061 // attribute not present
3062 if (Tok.is(tok::comma)) {
3063 ConsumeToken();
3064 continue;
3065 }
3066
Richard Smithc56298d2012-04-10 03:25:07 +00003067 SourceLocation ScopeLoc, AttrLoc;
3068 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3069
3070 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3071 if (!AttrName)
3072 // Break out to the "expected ']'" diagnostic.
3073 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003074
Sean Huntbbd37c62009-11-21 08:43:09 +00003075 // scoped attribute
3076 if (Tok.is(tok::coloncolon)) {
3077 ConsumeToken();
3078
Richard Smithc56298d2012-04-10 03:25:07 +00003079 ScopeName = AttrName;
3080 ScopeLoc = AttrLoc;
3081
3082 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3083 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003084 Diag(Tok.getLocation(), diag::err_expected_ident);
3085 SkipUntil(tok::r_square, tok::comma, true, true);
3086 continue;
3087 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003088 }
3089
Michael Han6880f492012-10-03 01:56:22 +00003090 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Sean Huntbbd37c62009-11-21 08:43:09 +00003091 bool AttrParsed = false;
Sean Huntbbd37c62009-11-21 08:43:09 +00003092
Michael Han6880f492012-10-03 01:56:22 +00003093 // Parse attribute arguments
3094 if (Tok.is(tok::l_paren)) {
3095 if (ScopeName && ScopeName->getName() == "gnu") {
3096 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3097 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3098 AttrParsed = true;
3099 } else {
3100 if (StandardAttr)
3101 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3102 << AttrName->getName();
3103
3104 // FIXME: handle other formats of c++11 attribute arguments
3105 ConsumeParen();
3106 SkipUntil(tok::r_paren, false);
3107 }
3108 }
3109
3110 if (!AttrParsed)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00003111 attrs.addNew(AttrName,
3112 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3113 AttrLoc),
3114 ScopeName, ScopeLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00003115 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smith6ee326a2012-04-10 01:32:12 +00003116
Richard Smithc56298d2012-04-10 03:25:07 +00003117 if (Tok.is(tok::ellipsis)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003118 ConsumeToken();
Michael Han6880f492012-10-03 01:56:22 +00003119
3120 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3121 << AttrName->getName();
Richard Smithc56298d2012-04-10 03:25:07 +00003122 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003123 }
3124
3125 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3126 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003127 if (endLoc)
3128 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00003129 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3130 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003131}
Sean Huntbbd37c62009-11-21 08:43:09 +00003132
Sean Hunt2edf0a22012-06-23 05:07:58 +00003133/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003134///
3135/// attribute-specifier-seq:
3136/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00003137void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003138 SourceLocation *endLoc) {
3139 SourceLocation StartLoc = Tok.getLocation(), Loc;
3140 if (!endLoc)
3141 endLoc = &Loc;
3142
Douglas Gregor8828ee72011-10-07 20:35:25 +00003143 do {
Richard Smithc56298d2012-04-10 03:25:07 +00003144 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003145 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003146
3147 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003148}
3149
Francois Pichet334d47e2010-10-11 12:59:39 +00003150/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3151///
3152/// [MS] ms-attribute:
3153/// '[' token-seq ']'
3154///
3155/// [MS] ms-attribute-seq:
3156/// ms-attribute[opt]
3157/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003158void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3159 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003160 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3161
3162 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003163 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003164 ConsumeBracket();
3165 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003166 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003167 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3168 }
3169}
Francois Pichet563a6452011-05-25 10:19:49 +00003170
3171void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3172 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003173 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003174 if (ParseMicrosoftIfExistsCondition(Result))
3175 return;
3176
Douglas Gregor3896fc52011-10-24 22:31:10 +00003177 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3178 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003179 Diag(Tok, diag::err_expected_lbrace);
3180 return;
3181 }
Francois Pichet563a6452011-05-25 10:19:49 +00003182
Douglas Gregor3896fc52011-10-24 22:31:10 +00003183 switch (Result.Behavior) {
3184 case IEB_Parse:
3185 // Parse the declarations below.
3186 break;
3187
3188 case IEB_Dependent:
3189 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3190 << Result.IsIfExists;
3191 // Fall through to skip.
3192
3193 case IEB_Skip:
3194 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003195 return;
3196 }
3197
Douglas Gregor3896fc52011-10-24 22:31:10 +00003198 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003199 // __if_exists, __if_not_exists can nest.
3200 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3201 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3202 continue;
3203 }
3204
3205 // Check for extraneous top-level semicolon.
3206 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003207 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003208 continue;
3209 }
3210
3211 AccessSpecifier AS = getAccessSpecifierIfPresent();
3212 if (AS != AS_none) {
3213 // Current token is a C++ access specifier.
3214 CurAS = AS;
3215 SourceLocation ASLoc = Tok.getLocation();
3216 ConsumeToken();
3217 if (Tok.is(tok::colon))
3218 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3219 else
3220 Diag(Tok, diag::err_expected_colon);
3221 ConsumeToken();
3222 continue;
3223 }
3224
3225 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003226 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003227 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003228
3229 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003230}