blob: 64a3ef0debf3f47deb3cb03b9e56fb8abf897e69 [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson0c6139d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor1b7f8982008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000021#include "llvm/ADT/SmallString.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000022#include "RAIIObjectsForParser.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000023using namespace clang;
24
25/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000026/// may either be a top level namespace or a block-level namespace alias. If
27/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000028///
29/// namespace-definition: [C++ 7.3: basic.namespace]
30/// named-namespace-definition
31/// unnamed-namespace-definition
32///
33/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000034/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000035///
36/// named-namespace-definition:
37/// original-namespace-definition
38/// extension-namespace-definition
39///
40/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000041/// 'inline'[opt] 'namespace' identifier attributes[opt]
42/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000043///
44/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000045/// 'inline'[opt] 'namespace' original-namespace-name
46/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000047///
Chris Lattner8f08cb72007-08-25 06:57:03 +000048/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
49/// 'namespace' identifier '=' qualified-namespace-specifier ';'
50///
John McCalld226f652010-08-21 09:40:31 +000051Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000052 SourceLocation &DeclEnd,
53 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000054 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000055 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +000056 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000057
Douglas Gregor49f40bd2009-09-18 19:03:04 +000058 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000059 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000060 cutOffParsing();
61 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +000062 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000063
Chris Lattner8f08cb72007-08-25 06:57:03 +000064 SourceLocation IdentLoc;
65 IdentifierInfo *Ident = 0;
Richard Trieuf858bd82011-05-26 20:11:09 +000066 std::vector<SourceLocation> ExtraIdentLoc;
67 std::vector<IdentifierInfo*> ExtraIdent;
68 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000069
70 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000071
Chris Lattner04d66662007-10-09 17:33:22 +000072 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000073 Ident = Tok.getIdentifierInfo();
74 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieuf858bd82011-05-26 20:11:09 +000075 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
76 ExtraNamespaceLoc.push_back(ConsumeToken());
77 ExtraIdent.push_back(Tok.getIdentifierInfo());
78 ExtraIdentLoc.push_back(ConsumeToken());
79 }
Chris Lattner8f08cb72007-08-25 06:57:03 +000080 }
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattner8f08cb72007-08-25 06:57:03 +000082 // Read label attributes, if present.
John McCall0b7e6782011-03-24 11:26:52 +000083 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000084 if (Tok.is(tok::kw___attribute)) {
85 attrTok = Tok;
John McCall7f040a92010-12-24 02:08:15 +000086 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000087 }
Mike Stump1eb44332009-09-09 15:08:12 +000088
Douglas Gregor6a588dd2009-06-17 19:49:00 +000089 if (Tok.is(tok::equal)) {
John McCall7f040a92010-12-24 02:08:15 +000090 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +000091 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +000092 if (InlineLoc.isValid())
93 Diag(InlineLoc, diag::err_inline_namespace_alias)
94 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +000095 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000096 }
Mike Stump1eb44332009-09-09 15:08:12 +000097
Richard Trieuf858bd82011-05-26 20:11:09 +000098
Douglas Gregor4a8dfb52011-10-12 16:37:45 +000099 BalancedDelimiterTracker T(*this, tok::l_brace);
100 if (T.consumeOpen()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000101 if (!ExtraIdent.empty()) {
102 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
103 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
104 }
Mike Stump1eb44332009-09-09 15:08:12 +0000105 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +0000106 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +0000107 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000108 }
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Douglas Gregor23c94db2010-07-02 17:43:08 +0000110 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
111 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
112 getCurScope()->getFnParent()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000113 if (!ExtraIdent.empty()) {
114 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
115 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
116 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000117 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Douglas Gregor95f1b152010-05-14 05:08:22 +0000118 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000119 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000120 }
121
Richard Trieuf858bd82011-05-26 20:11:09 +0000122 if (!ExtraIdent.empty()) {
123 TentativeParsingAction TPA(*this);
124 SkipUntil(tok::r_brace, /*StopAtSemi*/false, /*DontConsume*/true);
125 Token rBraceToken = Tok;
126 TPA.Revert();
127
128 if (!rBraceToken.is(tok::r_brace)) {
129 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
130 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
131 } else {
Benjamin Kramer9910df02011-05-26 21:32:30 +0000132 std::string NamespaceFix;
Richard Trieuf858bd82011-05-26 20:11:09 +0000133 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
134 E = ExtraIdent.end(); I != E; ++I) {
135 NamespaceFix += " { namespace ";
136 NamespaceFix += (*I)->getName();
137 }
Benjamin Kramer9910df02011-05-26 21:32:30 +0000138
Richard Trieuf858bd82011-05-26 20:11:09 +0000139 std::string RBraces;
Benjamin Kramer9910df02011-05-26 21:32:30 +0000140 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieuf858bd82011-05-26 20:11:09 +0000141 RBraces += "} ";
Benjamin Kramer9910df02011-05-26 21:32:30 +0000142
Richard Trieuf858bd82011-05-26 20:11:09 +0000143 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
144 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
145 ExtraIdentLoc.back()),
146 NamespaceFix)
147 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
148 }
149 }
150
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000151 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith7fe62082011-10-15 05:09:34 +0000152 if (InlineLoc.isValid())
David Blaikie4e4d0842012-03-11 07:00:24 +0000153 Diag(InlineLoc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +0000154 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000155
Chris Lattner51448322009-03-29 14:02:43 +0000156 // Enter a scope for the namespace.
157 ParseScope NamespaceScope(this, Scope::DeclScope);
158
John McCalld226f652010-08-21 09:40:31 +0000159 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000160 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000161 IdentLoc, Ident, T.getOpenLocation(),
162 attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000163
John McCallf312b1e2010-08-26 23:41:50 +0000164 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
165 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Richard Trieuf858bd82011-05-26 20:11:09 +0000167 // Parse the contents of the namespace. This includes parsing recovery on
168 // any improperly nested namespaces.
169 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000170 InlineLoc, attrs, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Chris Lattner51448322009-03-29 14:02:43 +0000172 // Leave the namespace scope.
173 NamespaceScope.Exit();
174
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000175 DeclEnd = T.getCloseLocation();
176 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner51448322009-03-29 14:02:43 +0000177
178 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000179}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000180
Richard Trieuf858bd82011-05-26 20:11:09 +0000181/// ParseInnerNamespace - Parse the contents of a namespace.
182void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
183 std::vector<IdentifierInfo*>& Ident,
184 std::vector<SourceLocation>& NamespaceLoc,
185 unsigned int index, SourceLocation& InlineLoc,
Richard Trieuf858bd82011-05-26 20:11:09 +0000186 ParsedAttributes& attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000187 BalancedDelimiterTracker &Tracker) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000188 if (index == Ident.size()) {
189 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
190 ParsedAttributesWithRange attrs(AttrFactory);
191 MaybeParseCXX0XAttributes(attrs);
192 MaybeParseMicrosoftAttributes(attrs);
193 ParseExternalDeclaration(attrs);
194 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000195
196 // The caller is what called check -- we are simply calling
197 // the close for it.
198 Tracker.consumeClose();
Richard Trieuf858bd82011-05-26 20:11:09 +0000199
200 return;
201 }
202
203 // Parse improperly nested namespaces.
204 ParseScope NamespaceScope(this, Scope::DeclScope);
205 Decl *NamespcDecl =
206 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
207 NamespaceLoc[index], IdentLoc[index],
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000208 Ident[index], Tracker.getOpenLocation(),
209 attrs.getList());
Richard Trieuf858bd82011-05-26 20:11:09 +0000210
211 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000212 attrs, Tracker);
Richard Trieuf858bd82011-05-26 20:11:09 +0000213
214 NamespaceScope.Exit();
215
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000216 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieuf858bd82011-05-26 20:11:09 +0000217}
218
Anders Carlssonf67606a2009-03-28 04:07:16 +0000219/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
220/// alias definition.
221///
John McCalld226f652010-08-21 09:40:31 +0000222Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000223 SourceLocation AliasLoc,
224 IdentifierInfo *Alias,
225 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000226 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Anders Carlssonf67606a2009-03-28 04:07:16 +0000228 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000230 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000231 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000232 cutOffParsing();
233 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000234 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000235
Anders Carlssonf67606a2009-03-28 04:07:16 +0000236 CXXScopeSpec SS;
237 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000238 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000239
240 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
241 Diag(Tok, diag::err_expected_namespace_name);
242 // Skip to end of the definition and eat the ';'.
243 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000244 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000245 }
246
247 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000248 IdentifierInfo *Ident = Tok.getIdentifierInfo();
249 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Anders Carlssonf67606a2009-03-28 04:07:16 +0000251 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000252 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000253 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
254 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Douglas Gregor23c94db2010-07-02 17:43:08 +0000256 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000257 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000258}
259
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000260/// ParseLinkage - We know that the current token is a string_literal
261/// and just before that, that extern was seen.
262///
263/// linkage-specification: [C++ 7.5p2: dcl.link]
264/// 'extern' string-literal '{' declaration-seq[opt] '}'
265/// 'extern' string-literal declaration
266///
Chris Lattner7d642712010-11-09 20:15:55 +0000267Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000268 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000269 SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000270 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000271 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000272 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000273 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000274
Richard Smith99831e42012-03-06 03:21:47 +0000275 // FIXME: This is incorrect: linkage-specifiers are parsed in translation
276 // phase 7, so string-literal concatenation is supposed to occur.
277 // extern "" "C" "" "+" "+" { } is legal.
278 if (Tok.hasUDSuffix())
279 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000280 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000281
Douglas Gregor074149e2009-01-05 19:45:36 +0000282 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000283 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000284 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000285 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000286 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000287 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000288 : SourceLocation());
289
John McCall0b7e6782011-03-24 11:26:52 +0000290 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000291 MaybeParseCXX0XAttributes(attrs);
292 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000293
Douglas Gregor074149e2009-01-05 19:45:36 +0000294 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-05-01 16:25:54 +0000295 // Reset the source range in DS, as the leading "extern"
296 // does not really belong to the inner declaration ...
297 DS.SetRangeStart(SourceLocation());
298 DS.SetRangeEnd(SourceLocation());
299 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000300 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000301 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000302 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000303 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000304 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000305
Douglas Gregor63a01132010-02-07 08:38:28 +0000306 DS.abort();
307
John McCall7f040a92010-12-24 02:08:15 +0000308 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000309
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000310 BalancedDelimiterTracker T(*this, tok::l_brace);
311 T.consumeOpen();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000312 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000313 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000314 MaybeParseCXX0XAttributes(attrs);
315 MaybeParseMicrosoftAttributes(attrs);
316 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000317 }
318
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000319 T.consumeClose();
Chris Lattner7d642712010-11-09 20:15:55 +0000320 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000321 T.getCloseLocation());
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000322}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000323
Douglas Gregorf780abc2008-12-30 03:27:21 +0000324/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
325/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000326Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000327 const ParsedTemplateInfo &TemplateInfo,
328 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000329 ParsedAttributesWithRange &attrs,
330 Decl **OwnedType) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000331 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000332 ObjCDeclContextSwitch ObjCDC(*this);
333
Douglas Gregorf780abc2008-12-30 03:27:21 +0000334 // Eat 'using'.
335 SourceLocation UsingLoc = ConsumeToken();
336
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000337 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000338 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000339 cutOffParsing();
340 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000341 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000342
John McCall78b81052010-11-10 02:40:36 +0000343 // 'using namespace' means this is a using-directive.
344 if (Tok.is(tok::kw_namespace)) {
345 // Template parameters are always an error here.
346 if (TemplateInfo.Kind) {
347 SourceRange R = TemplateInfo.getSourceRange();
348 Diag(UsingLoc, diag::err_templated_using_directive)
349 << R << FixItHint::CreateRemoval(R);
350 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000351
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000352 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000353 }
354
Richard Smith162e1c12011-04-15 14:24:37 +0000355 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000356
357 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000358 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000359
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000360 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000361 AS_none, OwnedType);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000362}
363
364/// ParseUsingDirective - Parse C++ using-directive, assumes
365/// that current token is 'namespace' and 'using' was already parsed.
366///
367/// using-directive: [C++ 7.3.p4: namespace.udir]
368/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
369/// namespace-name ;
370/// [GNU] using-directive:
371/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
372/// namespace-name attributes[opt] ;
373///
John McCalld226f652010-08-21 09:40:31 +0000374Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000375 SourceLocation UsingLoc,
376 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000377 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000378 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
379
380 // Eat 'namespace'.
381 SourceLocation NamespcLoc = ConsumeToken();
382
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000383 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000384 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000385 cutOffParsing();
386 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000387 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000388
Douglas Gregorf780abc2008-12-30 03:27:21 +0000389 CXXScopeSpec SS;
390 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000391 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000392
Douglas Gregorf780abc2008-12-30 03:27:21 +0000393 IdentifierInfo *NamespcName = 0;
394 SourceLocation IdentLoc = SourceLocation();
395
396 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000397 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000398 Diag(Tok, diag::err_expected_namespace_name);
399 // If there was invalid namespace name, skip to end of decl, and eat ';'.
400 SkipUntil(tok::semi);
401 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000402 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000403 }
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattner823c44e2009-01-06 07:27:21 +0000405 // Parse identifier.
406 NamespcName = Tok.getIdentifierInfo();
407 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Chris Lattner823c44e2009-01-06 07:27:21 +0000409 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000410 bool GNUAttr = false;
411 if (Tok.is(tok::kw___attribute)) {
412 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000413 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000414 }
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Chris Lattner823c44e2009-01-06 07:27:21 +0000416 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000417 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000418 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000419 GNUAttr ? diag::err_expected_semi_after_attribute_list
420 : diag::err_expected_semi_after_namespace_name,
421 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000422
Douglas Gregor23c94db2010-07-02 17:43:08 +0000423 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000424 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000425}
426
Richard Smith162e1c12011-04-15 14:24:37 +0000427/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
428/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000429///
430/// using-declaration: [C++ 7.3.p3: namespace.udecl]
431/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000432/// unqualified-id
433/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000434///
Richard Smith162e1c12011-04-15 14:24:37 +0000435/// alias-declaration: C++0x [decl.typedef]p2
436/// 'using' identifier = type-id ;
437///
John McCalld226f652010-08-21 09:40:31 +0000438Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000439 const ParsedTemplateInfo &TemplateInfo,
440 SourceLocation UsingLoc,
441 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000442 AccessSpecifier AS,
443 Decl **OwnedType) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000444 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000445 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000446 bool IsTypeName;
Sean Hunt2edf0a22012-06-23 05:07:58 +0000447 ParsedAttributesWithRange attrs(AttrFactory);
448
449 // FIXME: Simply skip the attributes and diagnose, don't bother parsing them.
450 MaybeParseCXX0XAttributes(attrs);
451 ProhibitAttributes(attrs);
452 attrs.clear();
453 attrs.Range = SourceRange();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000454
455 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000456 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000457 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000458 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000459 ConsumeToken();
460 IsTypeName = true;
461 }
462 else
463 IsTypeName = false;
464
465 // Parse nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000466 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000467
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000468 // Check nested-name specifier.
469 if (SS.isInvalid()) {
470 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000471 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000472 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000473
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000474 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000475 // destructor names and allow the action module to diagnose any semantic
476 // errors.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000477 SourceLocation TemplateKWLoc;
Douglas Gregor12c118a2009-11-04 16:30:06 +0000478 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000479 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000480 /*EnteringContext=*/false,
481 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000482 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000483 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000484 TemplateKWLoc,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000485 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000486 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000487 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000488 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000489
Sean Hunt2edf0a22012-06-23 05:07:58 +0000490 MaybeParseCXX0XAttributes(attrs);
Richard Smith162e1c12011-04-15 14:24:37 +0000491
492 // Maybe this is an alias-declaration.
493 bool IsAliasDecl = Tok.is(tok::equal);
494 TypeResult TypeAlias;
495 if (IsAliasDecl) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000496 // TODO: Attribute support. C++0x attributes may appear before the equals.
497 // Where can GNU attributes appear?
Richard Smith162e1c12011-04-15 14:24:37 +0000498 ConsumeToken();
499
David Blaikie4e4d0842012-03-11 07:00:24 +0000500 Diag(Tok.getLocation(), getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +0000501 diag::warn_cxx98_compat_alias_declaration :
502 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000503
Richard Smith3e4c6c42011-05-05 21:57:07 +0000504 // Type alias templates cannot be specialized.
505 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000506 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
507 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000508 SpecKind = 0;
509 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
510 SpecKind = 1;
511 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
512 SpecKind = 2;
513 if (SpecKind != -1) {
514 SourceRange Range;
515 if (SpecKind == 0)
516 Range = SourceRange(Name.TemplateId->LAngleLoc,
517 Name.TemplateId->RAngleLoc);
518 else
519 Range = TemplateInfo.getSourceRange();
520 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
521 << SpecKind << Range;
522 SkipUntil(tok::semi);
523 return 0;
524 }
525
Richard Smith162e1c12011-04-15 14:24:37 +0000526 // Name must be an identifier.
527 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
528 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
529 // No removal fixit: can't recover from this.
530 SkipUntil(tok::semi);
531 return 0;
532 } else if (IsTypeName)
533 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
534 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
535 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
536 else if (SS.isNotEmpty())
537 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
538 << FixItHint::CreateRemoval(SS.getRange());
539
Richard Smith3e4c6c42011-05-05 21:57:07 +0000540 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
541 Declarator::AliasTemplateContext :
John McCallcdda47f2011-10-01 09:56:14 +0000542 Declarator::AliasDeclContext, AS, OwnedType);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000543 } else {
544 // C++11 attributes are not allowed on a using-declaration, but GNU ones
545 // are.
546 ProhibitAttributes(attrs);
547
Richard Smith162e1c12011-04-15 14:24:37 +0000548 // Parse (optional) attributes (most likely GNU strong-using extension).
549 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000550 }
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000552 // Eat ';'.
553 DeclEnd = Tok.getLocation();
554 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith162e1c12011-04-15 14:24:37 +0000555 !attrs.empty() ? "attributes list" :
556 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000557 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000558
John McCall78b81052010-11-10 02:40:36 +0000559 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith3e4c6c42011-05-05 21:57:07 +0000560 // In C++0x, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000561 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000562 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000563 SourceRange R = TemplateInfo.getSourceRange();
564 Diag(UsingLoc, diag::err_templated_using_declaration)
565 << R << FixItHint::CreateRemoval(R);
566
567 // Unfortunately, we have to bail out instead of recovering by
568 // ignoring the parameters, just in case the nested name specifier
569 // depends on the parameters.
570 return 0;
571 }
572
Douglas Gregor480b53c2011-09-26 14:30:28 +0000573 // "typename" keyword is allowed for identifiers only,
574 // because it may be a type definition.
575 if (IsTypeName && Name.getKind() != UnqualifiedId::IK_Identifier) {
576 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
577 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
578 // Proceed parsing, but reset the IsTypeName flag.
579 IsTypeName = false;
580 }
581
Richard Smith3e4c6c42011-05-05 21:57:07 +0000582 if (IsAliasDecl) {
583 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramer5354e772012-08-23 23:38:35 +0000584 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3e4c6c42011-05-05 21:57:07 +0000585 TemplateParams ? TemplateParams->data() : 0,
586 TemplateParams ? TemplateParams->size() : 0);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000587 // FIXME: Propagate attributes.
Richard Smith3e4c6c42011-05-05 21:57:07 +0000588 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
589 UsingLoc, Name, TypeAlias);
590 }
Richard Smith162e1c12011-04-15 14:24:37 +0000591
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000592 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000593 Name, attrs.getList(),
594 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000595}
596
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000597/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000598///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000599/// [C++0x] static_assert-declaration:
600/// static_assert ( constant-expression , string-literal ) ;
601///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000602/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000603/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000604///
John McCalld226f652010-08-21 09:40:31 +0000605Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000606 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
607 "Not a static_assert declaration");
608
David Blaikie4e4d0842012-03-11 07:00:24 +0000609 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000610 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000611 if (Tok.is(tok::kw_static_assert))
612 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000613
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000614 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000616 BalancedDelimiterTracker T(*this, tok::l_paren);
617 if (T.consumeOpen()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000618 Diag(Tok, diag::err_expected_lparen);
Richard Smith3686c712012-09-13 19:12:50 +0000619 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000620 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000621 }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
John McCall60d7b3a2010-08-24 06:29:42 +0000623 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000624 if (AssertExpr.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000625 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000626 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000627 }
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Anders Carlssonad5f9602009-03-13 23:29:20 +0000629 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000630 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000631
Richard Smith0cc323c2012-03-05 23:20:05 +0000632 if (!isTokenStringLiteral()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000633 Diag(Tok, diag::err_expected_string_literal);
Richard Smith3686c712012-09-13 19:12:50 +0000634 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000635 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000636 }
Mike Stump1eb44332009-09-09 15:08:12 +0000637
John McCall60d7b3a2010-08-24 06:29:42 +0000638 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smith99831e42012-03-06 03:21:47 +0000639 if (AssertMessage.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000640 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000641 return 0;
Richard Smith99831e42012-03-06 03:21:47 +0000642 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000643
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000644 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Chris Lattner97144fc2009-04-02 04:16:50 +0000646 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000647 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000648
John McCall9ae2f072010-08-23 23:25:46 +0000649 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
650 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000651 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000652 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000653}
654
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000655/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
656///
657/// 'decltype' ( expression )
658///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000659SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
660 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
661 && "Not a decltype specifier");
662
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000663
David Blaikie42d6d0c2011-12-04 05:04:18 +0000664 ExprResult Result;
665 SourceLocation StartLoc = Tok.getLocation();
666 SourceLocation EndLoc;
667
668 if (Tok.is(tok::annot_decltype)) {
669 Result = getExprAnnotation(Tok);
670 EndLoc = Tok.getAnnotationEndLoc();
671 ConsumeToken();
672 if (Result.isInvalid()) {
673 DS.SetTypeSpecError();
674 return EndLoc;
675 }
676 } else {
Richard Smithc7b55432012-02-24 22:30:04 +0000677 if (Tok.getIdentifierInfo()->isStr("decltype"))
678 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000679
David Blaikie42d6d0c2011-12-04 05:04:18 +0000680 ConsumeToken();
681
682 BalancedDelimiterTracker T(*this, tok::l_paren);
683 if (T.expectAndConsume(diag::err_expected_lparen_after,
684 "decltype", tok::r_paren)) {
685 DS.SetTypeSpecError();
686 return T.getOpenLocation() == Tok.getLocation() ?
687 StartLoc : T.getOpenLocation();
688 }
689
690 // Parse the expression
691
692 // C++0x [dcl.type.simple]p4:
693 // The operand of the decltype specifier is an unevaluated operand.
Richard Smith76f3f692012-02-22 02:04:18 +0000694 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
695 0, /*IsDecltype=*/true);
David Blaikie42d6d0c2011-12-04 05:04:18 +0000696 Result = ParseExpression();
697 if (Result.isInvalid()) {
Richard Smithd8e4dac2012-02-27 05:24:00 +0000698 SkipUntil(tok::r_paren);
David Blaikie42d6d0c2011-12-04 05:04:18 +0000699 DS.SetTypeSpecError();
Richard Smithd8e4dac2012-02-27 05:24:00 +0000700 return StartLoc;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000701 }
702
703 // Match the ')'
704 T.consumeClose();
705 if (T.getCloseLocation().isInvalid()) {
706 DS.SetTypeSpecError();
707 // FIXME: this should return the location of the last token
708 // that was consumed (by "consumeClose()")
709 return T.getCloseLocation();
710 }
711
Richard Smith76f3f692012-02-22 02:04:18 +0000712 Result = Actions.ActOnDecltypeExpression(Result.take());
713 if (Result.isInvalid()) {
714 DS.SetTypeSpecError();
715 return T.getCloseLocation();
716 }
717
David Blaikie42d6d0c2011-12-04 05:04:18 +0000718 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000719 }
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000721 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000722 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000723 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000724 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
David Blaikie42d6d0c2011-12-04 05:04:18 +0000725 DiagID, Result.release())) {
John McCallfec54012009-08-03 20:12:06 +0000726 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000727 DS.SetTypeSpecError();
728 }
729 return EndLoc;
730}
731
732void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
733 SourceLocation StartLoc,
734 SourceLocation EndLoc) {
735 // make sure we have a token we can turn into an annotation token
736 if (PP.isBacktrackEnabled())
737 PP.RevertCachedTokens(1);
738 else
739 PP.EnterToken(Tok);
740
741 Tok.setKind(tok::annot_decltype);
742 setExprAnnotation(Tok, DS.getTypeSpecType() == TST_decltype ?
743 DS.getRepAsExpr() : ExprResult());
744 Tok.setAnnotationEndLoc(EndLoc);
745 Tok.setLocation(StartLoc);
746 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000747}
748
Sean Huntdb5d44b2011-05-19 05:37:45 +0000749void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
750 assert(Tok.is(tok::kw___underlying_type) &&
751 "Not an underlying type specifier");
752
753 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000754 BalancedDelimiterTracker T(*this, tok::l_paren);
755 if (T.expectAndConsume(diag::err_expected_lparen_after,
756 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000757 return;
758 }
759
760 TypeResult Result = ParseTypeName();
761 if (Result.isInvalid()) {
762 SkipUntil(tok::r_paren);
763 return;
764 }
765
766 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000767 T.consumeClose();
768 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000769 return;
770
771 const char *PrevSpec = 0;
772 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000773 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000774 DiagID, Result.release()))
775 Diag(StartLoc, DiagID) << PrevSpec;
776}
777
David Blaikie09048df2011-10-25 15:01:20 +0000778/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
779/// class name or decltype-specifier. Note that we only check that the result
780/// names a type; semantic analysis will need to verify that the type names a
781/// class. The result is either a type or null, depending on whether a type
782/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000783///
David Blaikie09048df2011-10-25 15:01:20 +0000784/// base-type-specifier: [C++ 10.1]
785/// class-or-decltype
786/// class-or-decltype: [C++ 10.1]
787/// nested-name-specifier[opt] class-name
788/// decltype-specifier
Douglas Gregor42a552f2008-11-05 20:51:48 +0000789/// class-name: [C++ 9.1]
790/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000791/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000792///
David Blaikie22216eb2011-10-25 17:10:12 +0000793Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
794 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000795 // Ignore attempts to use typename
796 if (Tok.is(tok::kw_typename)) {
797 Diag(Tok, diag::err_expected_class_name_not_template)
798 << FixItHint::CreateRemoval(Tok.getLocation());
799 ConsumeToken();
800 }
801
David Blaikie152aa4b2011-10-25 18:17:58 +0000802 // Parse optional nested-name-specifier
803 CXXScopeSpec SS;
804 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
805
806 BaseLoc = Tok.getLocation();
807
David Blaikie22216eb2011-10-25 17:10:12 +0000808 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000809 // tok == kw_decltype is just error recovery, it can only happen when SS
810 // isn't empty
811 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000812 if (SS.isNotEmpty())
813 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
814 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000815 // Fake up a Declarator to use with ActOnTypeName.
816 DeclSpec DS(AttrFactory);
817
David Blaikieb5777572011-12-08 04:53:15 +0000818 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000819
820 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
821 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
822 }
823
Douglas Gregor7f43d672009-02-25 23:52:28 +0000824 // Check whether we have a template-id that names a type.
825 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000826 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000827 if (TemplateId->Kind == TNK_Type_template ||
828 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000829 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000830
831 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000832 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000833 EndLocation = Tok.getAnnotationEndLoc();
834 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000835
836 if (Type)
837 return Type;
838 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000839 }
840
841 // Fall through to produce an error below.
842 }
843
Douglas Gregor42a552f2008-11-05 20:51:48 +0000844 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000845 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000846 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000847 }
848
Douglas Gregor84d0a192010-01-12 21:28:44 +0000849 IdentifierInfo *Id = Tok.getIdentifierInfo();
850 SourceLocation IdLoc = ConsumeToken();
851
852 if (Tok.is(tok::less)) {
853 // It looks the user intended to write a template-id here, but the
854 // template-name was wrong. Try to fix that.
855 TemplateNameKind TNK = TNK_Type_template;
856 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000857 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000858 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000859 Diag(IdLoc, diag::err_unknown_template_name)
860 << Id;
861 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000862
Douglas Gregor84d0a192010-01-12 21:28:44 +0000863 if (!Template)
864 return true;
865
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000866 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000867 UnqualifiedId TemplateName;
868 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000869
Douglas Gregor84d0a192010-01-12 21:28:44 +0000870 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000871 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
872 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000873 return true;
874 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000875 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000876
Douglas Gregor84d0a192010-01-12 21:28:44 +0000877 // If we didn't end up with a typename token, there's nothing more we
878 // can do.
879 if (Tok.isNot(tok::annot_typename))
880 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000881
Douglas Gregor84d0a192010-01-12 21:28:44 +0000882 // Retrieve the type from the annotation token, consume that token, and
883 // return.
884 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000885 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000886 ConsumeToken();
887 return Type;
888 }
889
Douglas Gregor42a552f2008-11-05 20:51:48 +0000890 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000891 IdentifierInfo *CorrectedII = 0;
Douglas Gregor059101f2011-03-02 00:47:37 +0000892 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000893 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000894 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000895 /*NonTrivialTypeSourceInfo=*/true,
896 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000897 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000898 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000899 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000900 }
901
902 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000903 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000904
905 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000906 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000907 DS.SetRangeStart(IdLoc);
908 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000909 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000910
911 const char *PrevSpec = 0;
912 unsigned DiagID;
913 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
914
915 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
916 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000917}
918
John McCallc052dbb2012-05-22 21:28:12 +0000919void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
920 while (Tok.is(tok::kw___single_inheritance) ||
921 Tok.is(tok::kw___multiple_inheritance) ||
922 Tok.is(tok::kw___virtual_inheritance)) {
923 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
924 SourceLocation AttrNameLoc = ConsumeToken();
925 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000926 SourceLocation(), 0, 0, AttributeList::AS_GNU);
John McCallc052dbb2012-05-22 21:28:12 +0000927 }
928}
929
Richard Smithc9f35172012-06-25 21:37:02 +0000930/// Determine whether the following tokens are valid after a type-specifier
931/// which could be a standalone declaration. This will conservatively return
932/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith139be702012-07-02 19:14:01 +0000933bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smithc9f35172012-06-25 21:37:02 +0000934 // This switch enumerates the valid "follow" set for type-specifiers.
935 switch (Tok.getKind()) {
936 default: break;
937 case tok::semi: // struct foo {...} ;
938 case tok::star: // struct foo {...} * P;
939 case tok::amp: // struct foo {...} & R = ...
940 case tok::identifier: // struct foo {...} V ;
941 case tok::r_paren: //(struct foo {...} ) {4}
942 case tok::annot_cxxscope: // struct foo {...} a:: b;
943 case tok::annot_typename: // struct foo {...} a ::b;
944 case tok::annot_template_id: // struct foo {...} a<int> ::b;
945 case tok::l_paren: // struct foo {...} ( x);
946 case tok::comma: // __builtin_offsetof(struct foo{...} ,
947 return true;
Richard Smith139be702012-07-02 19:14:01 +0000948 case tok::colon:
949 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smithc9f35172012-06-25 21:37:02 +0000950 // Type qualifiers
951 case tok::kw_const: // struct foo {...} const x;
952 case tok::kw_volatile: // struct foo {...} volatile x;
953 case tok::kw_restrict: // struct foo {...} restrict x;
954 case tok::kw_inline: // struct foo {...} inline foo() {};
955 // Storage-class specifiers
956 case tok::kw_static: // struct foo {...} static x;
957 case tok::kw_extern: // struct foo {...} extern x;
958 case tok::kw_typedef: // struct foo {...} typedef x;
959 case tok::kw_register: // struct foo {...} register x;
960 case tok::kw_auto: // struct foo {...} auto x;
961 case tok::kw_mutable: // struct foo {...} mutable x;
962 case tok::kw_constexpr: // struct foo {...} constexpr x;
963 // As shown above, type qualifiers and storage class specifiers absolutely
964 // can occur after class specifiers according to the grammar. However,
965 // almost no one actually writes code like this. If we see one of these,
966 // it is much more likely that someone missed a semi colon and the
967 // type/storage class specifier we're seeing is part of the *next*
968 // intended declaration, as in:
969 //
970 // struct foo { ... }
971 // typedef int X;
972 //
973 // We'd really like to emit a missing semicolon error instead of emitting
974 // an error on the 'int' saying that you can't have two type specifiers in
975 // the same declaration of X. Because of this, we look ahead past this
976 // token to see if it's a type specifier. If so, we know the code is
977 // otherwise invalid, so we can produce the expected semi error.
978 if (!isKnownToBeTypeSpecifier(NextToken()))
979 return true;
980 break;
981 case tok::r_brace: // struct bar { struct foo {...} }
982 // Missing ';' at end of struct is accepted as an extension in C mode.
983 if (!getLangOpts().CPlusPlus)
984 return true;
985 break;
986 }
987 return false;
988}
989
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000990/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
991/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
992/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +0000993/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000994///
995/// class-specifier: [C++ class]
996/// class-head '{' member-specification[opt] '}'
997/// class-head '{' member-specification[opt] '}' attributes[opt]
998/// class-head:
999/// class-key identifier[opt] base-clause[opt]
1000/// class-key nested-name-specifier identifier base-clause[opt]
1001/// class-key nested-name-specifier[opt] simple-template-id
1002/// base-clause[opt]
1003/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001004/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001005/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001006/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001007/// simple-template-id base-clause[opt]
1008/// class-key:
1009/// 'class'
1010/// 'struct'
1011/// 'union'
1012///
1013/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +00001014/// class-key ::[opt] nested-name-specifier[opt] identifier
1015/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1016/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001017///
1018/// Note that the C++ class-specifier and elaborated-type-specifier,
1019/// together, subsume the C99 struct-or-union-specifier:
1020///
1021/// struct-or-union-specifier: [C99 6.7.2.1]
1022/// struct-or-union identifier[opt] '{' struct-contents '}'
1023/// struct-or-union identifier
1024/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1025/// '}' attributes[opt]
1026/// [GNU] struct-or-union attributes[opt] identifier
1027/// struct-or-union:
1028/// 'struct'
1029/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +00001030void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1031 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001032 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001033 AccessSpecifier AS,
Richard Smith69730c12012-03-12 07:56:15 +00001034 bool EnteringContext, DeclSpecContext DSC) {
Joao Matos17d35c32012-08-31 22:18:20 +00001035 DeclSpec::TST TagType;
1036 if (TagTokKind == tok::kw_struct)
1037 TagType = DeclSpec::TST_struct;
1038 else if (TagTokKind == tok::kw___interface)
1039 TagType = DeclSpec::TST_interface;
1040 else if (TagTokKind == tok::kw_class)
1041 TagType = DeclSpec::TST_class;
1042 else {
Chris Lattner4c97d762009-04-12 21:49:30 +00001043 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1044 TagType = DeclSpec::TST_union;
1045 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001046
Douglas Gregor374929f2009-09-18 15:37:17 +00001047 if (Tok.is(tok::code_completion)) {
1048 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001049 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001050 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00001051 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001052
Chandler Carruth926c4b42010-06-28 08:39:25 +00001053 // C++03 [temp.explicit] 14.7.2/8:
1054 // The usual access checking rules do not apply to names used to specify
1055 // explicit instantiations.
1056 //
1057 // As an extension we do not perform access checking on the names used to
1058 // specify explicit specializations either. This is important to allow
1059 // specializing traits classes for private types.
John McCall13489672012-05-07 06:16:58 +00001060 //
1061 // Note that we don't suppress if this turns out to be an elaborated
1062 // type specifier.
1063 bool shouldDelayDiagsInTag =
1064 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1065 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1066 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001067
Sean Hunt2edf0a22012-06-23 05:07:58 +00001068 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001069 // If attributes exist after tag, parse them.
1070 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +00001071 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001072
Steve Narofff59e17e2008-12-24 20:59:21 +00001073 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001074 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001075 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001076
John McCallc052dbb2012-05-22 21:28:12 +00001077 // Parse inheritance specifiers.
1078 if (Tok.is(tok::kw___single_inheritance) ||
1079 Tok.is(tok::kw___multiple_inheritance) ||
1080 Tok.is(tok::kw___virtual_inheritance))
1081 ParseMicrosoftInheritanceClassAttributes(attrs);
1082
Sean Huntbbd37c62009-11-21 08:43:09 +00001083 // If C++0x attributes exist here, parse them.
1084 // FIXME: Are we consistent with the ordering of parsing of different
1085 // styles of attributes?
John McCall7f040a92010-12-24 02:08:15 +00001086 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001087
John Wiegley20c0da72011-04-27 23:09:49 +00001088 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +00001089 !Tok.is(tok::identifier) &&
1090 Tok.getIdentifierInfo() &&
1091 (Tok.is(tok::kw___is_arithmetic) ||
1092 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001093 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001094 Tok.is(tok::kw___is_floating_point) ||
1095 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001096 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001097 Tok.is(tok::kw___is_integral) ||
1098 Tok.is(tok::kw___is_member_function_pointer) ||
1099 Tok.is(tok::kw___is_member_pointer) ||
1100 Tok.is(tok::kw___is_pod) ||
1101 Tok.is(tok::kw___is_pointer) ||
1102 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001103 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001104 Tok.is(tok::kw___is_signed) ||
1105 Tok.is(tok::kw___is_unsigned) ||
1106 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001107 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001108 // name of struct templates, but some are keywords in GCC >= 4.3
1109 // and Clang. Therefore, when we see the token sequence "struct
1110 // X", make X into a normal identifier rather than a keyword, to
1111 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001112 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001113 Tok.setKind(tok::identifier);
1114 }
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001116 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001117 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001118 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001119 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1120 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001121
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001122 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001123 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001124 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001125 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1126 Diag(Tok, diag::err_expected_ident);
1127 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001128
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001129 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1130
Douglas Gregorcc636682009-02-17 23:15:12 +00001131 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001132 IdentifierInfo *Name = 0;
1133 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001134 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001135 if (Tok.is(tok::identifier)) {
1136 Name = Tok.getIdentifierInfo();
1137 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001138
David Blaikie4e4d0842012-03-11 07:00:24 +00001139 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001140 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001141 // Eat the template argument list and try to continue parsing this as
1142 // a class (or template thereof).
1143 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001144 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001145 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001146 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001147 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001148 // We couldn't parse the template argument list at all, so don't
1149 // try to give any location information for the list.
1150 LAngleLoc = RAngleLoc = SourceLocation();
1151 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001152
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001153 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matos17d35c32012-08-31 22:18:20 +00001154 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1155 << (TagType == DeclSpec::TST_class? 0
1156 : TagType == DeclSpec::TST_struct? 1
1157 : TagType == DeclSpec::TST_interface? 2
1158 : 3)
1159 << Name
1160 << SourceRange(LAngleLoc, RAngleLoc);
1161
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001162 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001163 // we've removed its template argument list.
1164 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1165 if (TemplateParams && TemplateParams->size() > 1) {
1166 TemplateParams->pop_back();
1167 } else {
1168 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001169 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001170 = ParsedTemplateInfo::NonTemplate;
1171 }
1172 } else if (TemplateInfo.Kind
1173 == ParsedTemplateInfo::ExplicitInstantiation) {
1174 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001175 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001176 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001177 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001178 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001179 = SourceLocation();
1180 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1181 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001182 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001183 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001184 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001185 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001186 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001187
Douglas Gregor059101f2011-03-02 00:47:37 +00001188 if (TemplateId->Kind != TNK_Type_template &&
1189 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001190 // The template-name in the simple-template-id refers to
1191 // something other than a class template. Give an appropriate
1192 // error message and skip to the ';'.
1193 SourceRange Range(NameLoc);
1194 if (SS.isNotEmpty())
1195 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001196
Douglas Gregor39a8de12009-02-25 19:37:18 +00001197 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1198 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Douglas Gregor39a8de12009-02-25 19:37:18 +00001200 DS.SetTypeSpecError();
1201 SkipUntil(tok::semi, false, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001202 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001203 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001204 }
1205
Richard Smith7796eb52012-03-12 08:56:40 +00001206 // There are four options here.
1207 // - If we are in a trailing return type, this is always just a reference,
1208 // and we must not try to parse a definition. For instance,
1209 // [] () -> struct S { };
1210 // does not define a type.
1211 // - If we have 'struct foo {...', 'struct foo :...',
1212 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1213 // - If we have 'struct foo;', then this is either a forward declaration
1214 // or a friend declaration, which have to be treated differently.
1215 // - Otherwise we have something like 'struct foo xyz', a reference.
Richard Smith69730c12012-03-12 07:56:15 +00001216 // However, in type-specifier-seq's, things look like declarations but are
1217 // just references, e.g.
1218 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001219 // or
Richard Smith69730c12012-03-12 07:56:15 +00001220 // &T::operator struct s;
1221 // For these, DSC is DSC_type_specifier.
John McCallf312b1e2010-08-26 23:41:50 +00001222 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001223 if (DSC == DSC_trailing)
1224 TUK = Sema::TUK_Reference;
1225 else if (Tok.is(tok::l_brace) ||
1226 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
1227 (isCXX0XFinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001228 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001229 if (DS.isFriendSpecified()) {
1230 // C++ [class.friend]p2:
1231 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001232 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001233 << SourceRange(DS.getFriendSpecLoc());
1234
1235 // Skip everything up to the semicolon, so that this looks like a proper
1236 // friend class (or template thereof) declaration.
1237 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001238 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001239 } else {
1240 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001241 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001242 }
Richard Smithc9f35172012-06-25 21:37:02 +00001243 } else if (DSC != DSC_type_specifier &&
1244 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00001245 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallf312b1e2010-08-26 23:41:50 +00001246 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matos17d35c32012-08-31 22:18:20 +00001247 if (Tok.isNot(tok::semi)) {
1248 // A semicolon was missing after this declaration. Diagnose and recover.
1249 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1250 DeclSpec::getSpecifierName(TagType));
1251 PP.EnterToken(Tok);
1252 Tok.setKind(tok::semi);
1253 }
Richard Smithc9f35172012-06-25 21:37:02 +00001254 } else
John McCallf312b1e2010-08-26 23:41:50 +00001255 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001256
John McCall13489672012-05-07 06:16:58 +00001257 // If this is an elaborated type specifier, and we delayed
1258 // diagnostics before, just merge them into the current pool.
1259 if (shouldDelayDiagsInTag) {
1260 diagsFromTag.done();
1261 if (TUK == Sema::TUK_Reference)
1262 diagsFromTag.redelay();
1263 }
1264
John McCall207014e2010-07-30 06:26:29 +00001265 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001266 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001267 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1268 // We have a declaration or reference to an anonymous class.
1269 Diag(StartLoc, diag::err_anon_type_definition)
1270 << DeclSpec::getSpecifierName(TagType);
1271 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001272
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001273 SkipUntil(tok::comma, true);
1274 return;
1275 }
1276
Douglas Gregorddc29e12009-02-06 22:42:48 +00001277 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001278 DeclResult TagOrTempResult = true; // invalid
1279 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001280
Douglas Gregor402abb52009-05-28 23:31:59 +00001281 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001282 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001283 // Explicit specialization, class template partial specialization,
1284 // or explicit instantiation.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001285 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001286 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001287 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001288 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001289 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001290 ProhibitAttributes(attrs);
1291
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001292 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001293 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001294 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001295 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001296 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001297 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001298 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001299 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001300 TemplateId->TemplateNameLoc,
1301 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001302 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001303 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001304 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001305
1306 // Friend template-ids are treated as references unless
1307 // they have template headers, in which case they're ill-formed
1308 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1309 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001310 } else if (TUK == Sema::TUK_Reference ||
1311 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001312 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001313 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001314 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001315 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001316 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001317 TemplateId->Template,
1318 TemplateId->TemplateNameLoc,
1319 TemplateId->LAngleLoc,
1320 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001321 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001322 } else {
1323 // This is an explicit specialization or a class template
1324 // partial specialization.
1325 TemplateParameterLists FakedParamLists;
1326
1327 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1328 // This looks like an explicit instantiation, because we have
1329 // something like
1330 //
1331 // template class Foo<X>
1332 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001333 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001334 // meant to be an explicit specialization, but the user forgot
1335 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +00001336 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001337
Mike Stump1eb44332009-09-09 15:08:12 +00001338 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001339 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001340 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001341 diag::err_explicit_instantiation_with_definition)
1342 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +00001343 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001344
1345 // Create a fake template parameter list that contains only
1346 // "template<>", so that we treat this construct as a class
1347 // template specialization.
1348 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +00001349 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001350 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001351 LAngleLoc,
1352 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001353 LAngleLoc));
1354 TemplateParams = &FakedParamLists;
1355 }
1356
1357 // Build the class template specialization.
1358 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001359 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001360 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001361 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001362 TemplateId->TemplateNameLoc,
1363 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001364 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001365 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001366 attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001367 MultiTemplateParamsArg(
Douglas Gregorcc636682009-02-17 23:15:12 +00001368 TemplateParams? &(*TemplateParams)[0] : 0,
1369 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001370 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001371 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001372 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001373 // Explicit instantiation of a member of a class template
1374 // specialization, e.g.,
1375 //
1376 // template struct Outer<int>::Inner;
1377 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001378 ProhibitAttributes(attrs);
1379
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001380 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001381 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001382 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001383 TemplateInfo.TemplateLoc,
1384 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001385 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001386 } else if (TUK == Sema::TUK_Friend &&
1387 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001388 ProhibitAttributes(attrs);
1389
John McCall9a34edb2010-10-19 01:40:49 +00001390 TagOrTempResult =
1391 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1392 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001393 Name, NameLoc, attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001394 MultiTemplateParamsArg(
John McCall9a34edb2010-10-19 01:40:49 +00001395 TemplateParams? &(*TemplateParams)[0] : 0,
1396 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001397 } else {
1398 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001399 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001400 // FIXME: Diagnose this particular error.
1401 }
1402
Sean Hunt2edf0a22012-06-23 05:07:58 +00001403 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1404 ProhibitAttributes(attrs);
1405
John McCallc4e70192009-09-11 04:59:25 +00001406 bool IsDependent = false;
1407
John McCalla25c4082010-10-19 18:40:57 +00001408 // Don't pass down template parameter lists if this is just a tag
1409 // reference. For example, we don't need the template parameters here:
1410 // template <class T> class A *makeA(T t);
1411 MultiTemplateParamsArg TParams;
1412 if (TUK != Sema::TUK_Reference && TemplateParams)
1413 TParams =
1414 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1415
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001416 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001417 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001418 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001419 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001420 TParams, Owned, IsDependent,
1421 SourceLocation(), false,
1422 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001423
1424 // If ActOnTag said the type was dependent, try again with the
1425 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001426 if (IsDependent) {
1427 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001428 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001429 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001430 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001431 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001432
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001433 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001434 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001435 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001436 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001437 isCXX0XFinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001438 if (getLangOpts().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +00001439 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001440 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001441 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001442 }
1443
John McCallb3d87482010-08-24 05:47:05 +00001444 const char *PrevSpec = 0;
1445 unsigned DiagID;
1446 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001447 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001448 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1449 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001450 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001451 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001452 Result = DS.SetTypeSpecType(TagType, StartLoc,
1453 NameLoc.isValid() ? NameLoc : StartLoc,
1454 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001455 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001456 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001457 return;
1458 }
Mike Stump1eb44332009-09-09 15:08:12 +00001459
John McCallb3d87482010-08-24 05:47:05 +00001460 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001461 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001462
Chris Lattner4ed5d912010-02-02 01:23:29 +00001463 // At this point, we've successfully parsed a class-specifier in 'definition'
1464 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1465 // going to look at what comes after it to improve error recovery. If an
1466 // impossible token occurs next, we assume that the programmer forgot a ; at
1467 // the end of the declaration and recover that way.
1468 //
Richard Smithc9f35172012-06-25 21:37:02 +00001469 // Also enforce C++ [temp]p3:
1470 // In a template-declaration which defines a class, no declarator
1471 // is permitted.
Joao Matos17d35c32012-08-31 22:18:20 +00001472 if (TUK == Sema::TUK_Definition &&
1473 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
1474 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1475 DeclSpec::getSpecifierName(TagType));
1476 // Push this token back into the preprocessor and change our current token
1477 // to ';' so that the rest of the code recovers as though there were an
1478 // ';' after the definition.
Richard Smithc9f35172012-06-25 21:37:02 +00001479 PP.EnterToken(Tok);
1480 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001481 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001482}
1483
Mike Stump1eb44332009-09-09 15:08:12 +00001484/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001485///
1486/// base-clause : [C++ class.derived]
1487/// ':' base-specifier-list
1488/// base-specifier-list:
1489/// base-specifier '...'[opt]
1490/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001491void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001492 assert(Tok.is(tok::colon) && "Not a base clause");
1493 ConsumeToken();
1494
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001495 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001496 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001497
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001498 while (true) {
1499 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001500 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001501 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001502 // Skip the rest of this base specifier, up until the comma or
1503 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001504 SkipUntil(tok::comma, tok::l_brace, true, true);
1505 } else {
1506 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001507 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001508 }
1509
1510 // If the next token is a comma, consume it and keep reading
1511 // base-specifiers.
1512 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001514 // Consume the comma.
1515 ConsumeToken();
1516 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001517
1518 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001519 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001520}
1521
1522/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1523/// one entry in the base class list of a class specifier, for example:
1524/// class foo : public bar, virtual private baz {
1525/// 'public bar' and 'virtual private baz' are each base-specifiers.
1526///
1527/// base-specifier: [C++ class.derived]
1528/// ::[opt] nested-name-specifier[opt] class-name
1529/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001530/// base-type-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001531/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001532/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001533Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001534 bool IsVirtual = false;
1535 SourceLocation StartLoc = Tok.getLocation();
1536
1537 // Parse the 'virtual' keyword.
1538 if (Tok.is(tok::kw_virtual)) {
1539 ConsumeToken();
1540 IsVirtual = true;
1541 }
1542
1543 // Parse an (optional) access specifier.
1544 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001545 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001546 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001548 // Parse the 'virtual' keyword (again!), in case it came after the
1549 // access specifier.
1550 if (Tok.is(tok::kw_virtual)) {
1551 SourceLocation VirtualLoc = ConsumeToken();
1552 if (IsVirtual) {
1553 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001554 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001555 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001556 }
1557
1558 IsVirtual = true;
1559 }
1560
Douglas Gregor42a552f2008-11-05 20:51:48 +00001561 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001562 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001563 SourceLocation BaseLoc;
1564 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001565 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001566 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001567
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001568 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1569 // actually part of the base-specifier-list grammar productions, but we
1570 // parse it here for convenience.
1571 SourceLocation EllipsisLoc;
1572 if (Tok.is(tok::ellipsis))
1573 EllipsisLoc = ConsumeToken();
1574
Mike Stump1eb44332009-09-09 15:08:12 +00001575 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001576 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001578 // Notify semantic analysis that we have parsed a complete
1579 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001580 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001581 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001582}
1583
1584/// getAccessSpecifierIfPresent - Determine whether the next token is
1585/// a C++ access-specifier.
1586///
1587/// access-specifier: [C++ class.derived]
1588/// 'private'
1589/// 'protected'
1590/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001591AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001592 switch (Tok.getKind()) {
1593 default: return AS_none;
1594 case tok::kw_private: return AS_private;
1595 case tok::kw_protected: return AS_protected;
1596 case tok::kw_public: return AS_public;
1597 }
1598}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001599
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001600/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001601/// delayed, e.g., default arguments, create a late-parsed method declaration
1602/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001603void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1604 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001605 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001606 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001607 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001608 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001609 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001610
Eli Friedmand33133c2009-07-22 21:45:50 +00001611 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1612 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1613 if (!LateMethod) {
1614 // Push this method onto the stack of late-parsed method
1615 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001616 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1617 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001618 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001619
1620 // Add all of the parameters prior to this one (they don't
1621 // have default arguments).
1622 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1623 for (unsigned I = 0; I < ParamIdx; ++I)
1624 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001625 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001626 }
1627
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001628 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001629 // not have a default argument).
1630 LateMethod->DefaultArgs.push_back(
1631 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1632 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1633 }
1634 }
1635}
1636
Richard Smith1c94c162012-01-09 22:31:44 +00001637/// isCXX0XVirtSpecifier - Determine whether the given token is a C++0x
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001638/// virt-specifier.
1639///
1640/// virt-specifier:
1641/// override
1642/// final
Richard Smith1c94c162012-01-09 22:31:44 +00001643VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001644 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001645 return VirtSpecifiers::VS_None;
1646
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001647 if (Tok.is(tok::identifier)) {
1648 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001649
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001650 // Initialize the contextual keywords.
1651 if (!Ident_final) {
1652 Ident_final = &PP.getIdentifierTable().get("final");
1653 Ident_override = &PP.getIdentifierTable().get("override");
1654 }
1655
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001656 if (II == Ident_override)
1657 return VirtSpecifiers::VS_Override;
1658
1659 if (II == Ident_final)
1660 return VirtSpecifiers::VS_Final;
1661 }
1662
1663 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001664}
1665
1666/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1667///
1668/// virt-specifier-seq:
1669/// virt-specifier
1670/// virt-specifier-seq virt-specifier
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001671void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001672 while (true) {
Anders Carlssoncc54d592011-01-22 16:56:46 +00001673 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001674 if (Specifier == VirtSpecifiers::VS_None)
1675 return;
1676
1677 // C++ [class.mem]p8:
1678 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001679 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001680 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001681 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1682 << PrevSpec
1683 << FixItHint::CreateRemoval(Tok.getLocation());
1684
David Blaikie4e4d0842012-03-11 07:00:24 +00001685 Diag(Tok.getLocation(), getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00001686 diag::warn_cxx98_compat_override_control_keyword :
1687 diag::ext_override_control_keyword)
1688 << VirtSpecifiers::getSpecifierName(Specifier);
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001689 ConsumeToken();
1690 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001691}
1692
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001693/// isCXX0XFinalKeyword - Determine whether the next token is a C++0x
1694/// contextual 'final' keyword.
1695bool Parser::isCXX0XFinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001696 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001697 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001698
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001699 if (!Tok.is(tok::identifier))
1700 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001701
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001702 // Initialize the contextual keywords.
1703 if (!Ident_final) {
1704 Ident_final = &PP.getIdentifierTable().get("final");
1705 Ident_override = &PP.getIdentifierTable().get("override");
1706 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001707
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001708 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001709}
1710
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001711/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1712///
1713/// member-declaration:
1714/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1715/// function-definition ';'[opt]
1716/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1717/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001718/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001719/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001720/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001721///
1722/// member-declarator-list:
1723/// member-declarator
1724/// member-declarator-list ',' member-declarator
1725///
1726/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001727/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001728/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001729/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001730/// identifier[opt] ':' constant-expression
1731///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001732/// virt-specifier-seq:
1733/// virt-specifier
1734/// virt-specifier-seq virt-specifier
1735///
1736/// virt-specifier:
1737/// override
1738/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001739///
Sebastian Redle2b68332009-04-12 17:16:29 +00001740/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001741/// '= 0'
1742///
1743/// constant-initializer:
1744/// '=' constant-expression
1745///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001746void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001747 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001748 const ParsedTemplateInfo &TemplateInfo,
1749 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001750 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001751 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001752 Diag(Tok, diag::err_at_defs_cxx);
1753 else
1754 Diag(Tok, diag::err_at_in_class);
1755
1756 ConsumeToken();
1757 SkipUntil(tok::r_brace);
1758 return;
1759 }
1760
John McCall60fa3cf2009-12-11 02:10:03 +00001761 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001762 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001763 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001764 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1765 if (TryAnnotateCXXScopeToken())
1766 MalformedTypeSpec = true;
1767
1768 bool isAccessDecl;
1769 if (Tok.isNot(tok::annot_cxxscope))
1770 isAccessDecl = false;
1771 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001772 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1773 else
1774 isAccessDecl = NextToken().is(tok::kw_operator);
1775
1776 if (isAccessDecl) {
1777 // Collect the scope specifier token we annotated earlier.
1778 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001779 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1780 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001781
1782 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001783 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001784 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001785 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1786 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001787 SkipUntil(tok::semi);
1788 return;
1789 }
1790
1791 // TODO: recover from mistakenly-qualified operator declarations.
1792 if (ExpectAndConsume(tok::semi,
1793 diag::err_expected_semi_after,
1794 "access declaration",
1795 tok::semi))
1796 return;
1797
Douglas Gregor23c94db2010-07-02 17:43:08 +00001798 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001799 false, SourceLocation(),
1800 SS, Name,
1801 /* AttrList */ 0,
1802 /* IsTypeName */ false,
1803 SourceLocation());
1804 return;
1805 }
1806 }
1807
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001808 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001809 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001810 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001811 SourceLocation DeclEnd;
1812 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001813 return;
1814 }
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Chris Lattner682bf922009-03-29 16:50:03 +00001816 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001817 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001818 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001819 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001820 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001821 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001822 return;
1823 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001824
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001825 // Handle: member-declaration ::= '__extension__' member-declaration
1826 if (Tok.is(tok::kw___extension__)) {
1827 // __extension__ silences extension warnings in the subexpression.
1828 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1829 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001830 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1831 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001832 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001833
Chris Lattner4ed5d912010-02-02 01:23:29 +00001834 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1835 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001836 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001837
John McCall0b7e6782011-03-24 11:26:52 +00001838 ParsedAttributesWithRange attrs(AttrFactory);
Sean Huntbbd37c62009-11-21 08:43:09 +00001839 // Optional C++0x attribute-specifier
John McCall7f040a92010-12-24 02:08:15 +00001840 MaybeParseCXX0XAttributes(attrs);
1841 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001842
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001843 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00001844 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001845
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001846 // Eat 'using'.
1847 SourceLocation UsingLoc = ConsumeToken();
1848
1849 if (Tok.is(tok::kw_namespace)) {
1850 Diag(UsingLoc, diag::err_using_namespace_in_class);
1851 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001852 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001853 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001854 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00001855 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1856 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001857 }
1858 return;
1859 }
1860
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001861 // Hold late-parsed attributes so we can attach a Decl to them later.
1862 LateParsedAttrList CommonLateParsedAttrs;
1863
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001864 // decl-specifier-seq:
1865 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001866 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00001867 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00001868 if (MalformedTypeSpec)
1869 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001870 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
1871 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001872
Benjamin Kramer5354e772012-08-23 23:38:35 +00001873 MultiTemplateParamsArg TemplateParams(
John McCalldd4a3b02009-09-16 22:47:08 +00001874 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1875 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1876
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001877 if (Tok.is(tok::semi)) {
1878 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001879 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00001880 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00001881 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001882 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001883 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001884
John McCall54abf7d2009-11-04 02:18:39 +00001885 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00001886 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001887
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001888 // Hold late-parsed attributes so we can attach a Decl to them later.
1889 LateParsedAttrList LateParsedAttrs;
1890
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001891 SourceLocation EqualLoc;
1892 bool HasInitializer = false;
1893 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001894 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001895 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1896 ColonProtectionRAIIObject X(*this);
1897
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001898 // Parse the first declarator.
1899 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00001900 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001901 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001902 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00001903 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001904 if (Tok.is(tok::semi))
1905 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001906 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001907 }
1908
Nico Weber48673472011-01-28 06:07:34 +00001909 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1910
John Thompson1b2fc0f2009-11-25 22:58:06 +00001911 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001912 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00001913
Francois Pichet6a247472011-05-11 02:14:46 +00001914 // MSVC permits pure specifier on inline functions declared at class scope.
1915 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00001916 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00001917 DeclaratorInfo.isFunctionDeclarator() &&
1918 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001919 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00001920 Init = ParseInitializer();
1921 if (Init.isInvalid())
1922 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001923 else
1924 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00001925 }
1926
Douglas Gregor45fa5602011-11-07 20:56:01 +00001927 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001928 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00001929 //
1930 // In C++11, a non-function declarator followed by an open brace is a
1931 // braced-init-list for an in-class member initialization, not an
1932 // erroneous function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00001933 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus0x) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00001934 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00001935 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00001936 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00001937 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00001938 } else if (Tok.is(tok::equal)) {
1939 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00001940 if (KW.is(tok::kw_default))
1941 DefinitionKind = FDK_Defaulted;
1942 else if (KW.is(tok::kw_delete))
1943 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00001944 }
1945 }
1946
Douglas Gregor45fa5602011-11-07 20:56:01 +00001947 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001948 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00001949 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001950 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00001951 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001952
1953 // Consume the optional ';'
1954 if (Tok.is(tok::semi))
1955 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001956 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001957 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001958
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001959 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00001960 Diag(DeclaratorInfo.getIdentifierLoc(),
1961 diag::err_function_declared_typedef);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001962 // This recovery skips the entire function body. It would be nice
1963 // to simply call ParseCXXInlineMethodDef() below, however Sema
1964 // assumes the declarator represents a function, not a typedef.
1965 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00001966 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001967
1968 // Consume the optional ';'
1969 if (Tok.is(tok::semi))
1970 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001971 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001972 }
1973
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001974 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001975 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00001976 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001977
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001978 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
1979 CommonLateParsedAttrs[i]->addDecl(FunDecl);
1980 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001981 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001982 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001983 }
1984 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00001985
1986 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00001987 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00001988 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001989
Chris Lattner682bf922009-03-29 16:50:03 +00001990 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001991 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001992 }
1993
1994 // member-declarator-list:
1995 // member-declarator
1996 // member-declarator-list ',' member-declarator
1997
Chris Lattner5f9e2722011-07-23 10:55:15 +00001998 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00001999 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00002000 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002001
2002 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002003 // member-declarator:
2004 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002005 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002006 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002007 if (Tok.is(tok::colon)) {
2008 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002009 BitfieldSize = ParseConstantExpression();
2010 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002011 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002012 }
Mike Stump1eb44332009-09-09 15:08:12 +00002013
Chris Lattnere6563252010-06-13 05:34:18 +00002014 // If a simple-asm-expr is present, parse it.
2015 if (Tok.is(tok::kw_asm)) {
2016 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002017 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002018 if (AsmLabel.isInvalid())
2019 SkipUntil(tok::comma, true, true);
2020
2021 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2022 DeclaratorInfo.SetRangeEnd(Loc);
2023 }
2024
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002025 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002026 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002027
Richard Smith7a614d82011-06-11 17:19:42 +00002028 // FIXME: When g++ adds support for this, we'll need to check whether it
2029 // goes before or after the GNU attributes and __asm__.
2030 ParseOptionalCXX0XVirtSpecifierSeq(VS);
2031
Richard Smithca523302012-06-10 03:12:00 +00002032 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002033 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002034 if (BitfieldSize.get()) {
2035 Diag(Tok, diag::err_bitfield_member_init);
2036 SkipUntil(tok::comma, true, true);
2037 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002038 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002039 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2040 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2041 != DeclSpec::SCS_static &&
2042 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2043 != DeclSpec::SCS_typedef)
2044 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002045 }
2046 }
2047
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002048 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002049 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002050 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002051
John McCalld226f652010-08-21 09:40:31 +00002052 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002053 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00002054 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002055 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002056 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002057 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002058 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002059 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002060 TemplateParams,
John McCall67d1a672009-08-06 02:15:43 +00002061 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002062 VS, HasInClassInit);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002063 if (AccessAttrs)
2064 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2065 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002066 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002067
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002068 // Set the Decl for any late parsed attributes
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002069 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2070 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
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(ThisDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002074 }
2075 LateParsedAttrs.clear();
2076
Douglas Gregor147545d2011-10-10 14:49:18 +00002077 // Handle the initializer.
Richard Smithca523302012-06-10 03:12:00 +00002078 if (HasInClassInit != ICIS_NoInit) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002079 // The initializer was deferred; parse it and cache the tokens.
David Blaikie4e4d0842012-03-11 07:00:24 +00002080 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00002081 diag::warn_cxx98_compat_nonstatic_member_init :
2082 diag::ext_nonstatic_member_init);
2083
Richard Smith7a614d82011-06-11 17:19:42 +00002084 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002085 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2086 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002087 //
2088 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002089 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002090 Diag(Tok, diag::err_incomplete_array_member_init);
2091 SkipUntil(tok::comma, true, true);
David Blaikie3164c142012-02-14 09:00:46 +00002092 if (ThisDecl)
2093 // Avoid later warnings about a class member of incomplete type.
2094 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002095 } else
2096 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002097 } else if (HasInitializer) {
2098 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002099 if (!Init.isUsable())
Douglas Gregor552e2992012-02-21 02:22:07 +00002100 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002101 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2102
Douglas Gregor147545d2011-10-10 14:49:18 +00002103 if (Init.isInvalid())
2104 SkipUntil(tok::comma, true, true);
2105 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002106 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002107 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor147545d2011-10-10 14:49:18 +00002108 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2109 // No initializer.
2110 Actions.ActOnUninitializedDecl(ThisDecl,
2111 DS.getTypeSpecType() == DeclSpec::TST_auto);
Richard Smith7a614d82011-06-11 17:19:42 +00002112 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002113
2114 if (ThisDecl) {
2115 Actions.FinalizeDeclaration(ThisDecl);
2116 DeclsInGroup.push_back(ThisDecl);
2117 }
2118
Richard Smithe5310012012-04-29 07:31:09 +00002119 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor147545d2011-10-10 14:49:18 +00002120 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2121 != DeclSpec::SCS_typedef) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002122 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002123 }
2124
2125 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002126
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002127 // If we don't have a comma, it is either the end of the list (a ';')
2128 // or an error, bail out.
2129 if (Tok.isNot(tok::comma))
2130 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002131
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002132 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002133 SourceLocation CommaLoc = ConsumeToken();
2134
2135 if (Tok.isAtStartOfLine() &&
2136 !MightBeDeclarator(Declarator::MemberContext)) {
2137 // This comma was followed by a line-break and something which can't be
2138 // the start of a declarator. The comma was probably a typo for a
2139 // semicolon.
2140 Diag(CommaLoc, diag::err_expected_semi_declaration)
2141 << FixItHint::CreateReplacement(CommaLoc, ";");
2142 ExpectSemi = false;
2143 break;
2144 }
Mike Stump1eb44332009-09-09 15:08:12 +00002145
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002146 // Parse the next declarator.
2147 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002148 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002149 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002150 Init = true;
2151 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002152 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002153
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002154 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002155 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002156
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002157 if (Tok.isNot(tok::colon))
2158 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002159 }
2160
Richard Smith1c94c162012-01-09 22:31:44 +00002161 if (ExpectSemi &&
2162 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002163 // Skip to end of block or statement.
2164 SkipUntil(tok::r_brace, true, true);
2165 // If we stopped at a ';', eat it.
2166 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002167 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002168 }
2169
Douglas Gregor23c94db2010-07-02 17:43:08 +00002170 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00002171 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002172}
2173
Richard Smith7a614d82011-06-11 17:19:42 +00002174/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2175/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2176/// function definition. The location of the '=', if any, will be placed in
2177/// EqualLoc.
2178///
2179/// pure-specifier:
2180/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002181///
Richard Smith7a614d82011-06-11 17:19:42 +00002182/// brace-or-equal-initializer:
2183/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002184/// braced-init-list
2185///
Richard Smith7a614d82011-06-11 17:19:42 +00002186/// initializer-clause:
2187/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002188/// braced-init-list
2189///
Richard Smith7a614d82011-06-11 17:19:42 +00002190/// defaulted/deleted function-definition:
2191/// '=' 'default'
2192/// '=' 'delete'
2193///
2194/// Prior to C++0x, the assignment-expression in an initializer-clause must
2195/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002196ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002197 SourceLocation &EqualLoc) {
2198 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2199 && "Data member initializer not starting with '=' or '{'");
2200
Douglas Gregor552e2992012-02-21 02:22:07 +00002201 EnterExpressionEvaluationContext Context(Actions,
2202 Sema::PotentiallyEvaluated,
2203 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002204 if (Tok.is(tok::equal)) {
2205 EqualLoc = ConsumeToken();
2206 if (Tok.is(tok::kw_delete)) {
2207 // In principle, an initializer of '= delete p;' is legal, but it will
2208 // never type-check. It's better to diagnose it as an ill-formed expression
2209 // than as an ill-formed deleted non-function member.
2210 // An initializer of '= delete p, foo' will never be parsed, because
2211 // a top-level comma always ends the initializer expression.
2212 const Token &Next = NextToken();
2213 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2214 Next.is(tok::eof)) {
2215 if (IsFunction)
2216 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2217 << 1 /* delete */;
2218 else
2219 Diag(ConsumeToken(), diag::err_deleted_non_function);
2220 return ExprResult();
2221 }
2222 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002223 if (IsFunction)
2224 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2225 << 0 /* default */;
2226 else
2227 Diag(ConsumeToken(), diag::err_default_special_members);
2228 return ExprResult();
2229 }
2230
Sebastian Redl33deb352012-02-22 10:50:08 +00002231 }
2232 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002233}
2234
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002235/// ParseCXXMemberSpecification - Parse the class definition.
2236///
2237/// member-specification:
2238/// member-declaration member-specification[opt]
2239/// access-specifier ':' member-specification[opt]
2240///
Joao Matos17d35c32012-08-31 22:18:20 +00002241void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
2242 unsigned TagType, Decl *TagDecl) {
2243 assert((TagType == DeclSpec::TST_struct ||
2244 TagType == DeclSpec::TST_interface ||
2245 TagType == DeclSpec::TST_union ||
2246 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2247
John McCallf312b1e2010-08-26 23:41:50 +00002248 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2249 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002250
Douglas Gregor26997fd2010-01-16 20:52:59 +00002251 // Determine whether this is a non-nested class. Note that local
2252 // classes are *not* considered to be nested classes.
2253 bool NonNestedClass = true;
2254 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002255 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002256 if (S->isClassScope()) {
2257 // We're inside a class scope, so this is a nested class.
2258 NonNestedClass = false;
2259 break;
2260 }
2261
2262 if ((S->getFlags() & Scope::FnScope)) {
2263 // If we're in a function or function template declared in the
2264 // body of a class, then this is a local class rather than a
2265 // nested class.
2266 const Scope *Parent = S->getParent();
2267 if (Parent->isTemplateParamScope())
2268 Parent = Parent->getParent();
2269 if (Parent->isClassScope())
2270 break;
2271 }
2272 }
2273 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002274
2275 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002276 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002277
Douglas Gregor6569d682009-05-27 23:11:45 +00002278 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00002279 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00002280
Douglas Gregorddc29e12009-02-06 22:42:48 +00002281 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002282 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002283
Anders Carlssonb184a182011-03-25 14:46:08 +00002284 SourceLocation FinalLoc;
2285
2286 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002287 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith8b11b5e2011-10-15 04:21:46 +00002288 assert(isCXX0XFinalKeyword() && "not a class definition");
2289 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002290
David Blaikie4e4d0842012-03-11 07:00:24 +00002291 Diag(FinalLoc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00002292 diag::warn_cxx98_compat_override_control_keyword :
2293 diag::ext_override_control_keyword) << "final";
Anders Carlssonb184a182011-03-25 14:46:08 +00002294 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002295
John McCallbd0dfa52009-12-19 21:48:58 +00002296 if (Tok.is(tok::colon)) {
2297 ParseBaseClause(TagDecl);
2298
2299 if (!Tok.is(tok::l_brace)) {
2300 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002301
2302 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002303 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002304 return;
2305 }
2306 }
2307
2308 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002309 BalancedDelimiterTracker T(*this, tok::l_brace);
2310 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002311
John McCall42a4f662010-05-28 08:11:17 +00002312 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002313 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002314 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002315
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002316 // C++ 11p3: Members of a class defined with the keyword class are private
2317 // by default. Members of a class defined with the keywords struct or union
2318 // are public by default.
2319 AccessSpecifier CurAS;
2320 if (TagType == DeclSpec::TST_class)
2321 CurAS = AS_private;
2322 else
2323 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002324 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002325
Douglas Gregor07976d22010-06-21 22:31:09 +00002326 if (TagDecl) {
2327 // While we still have something to read, read the member-declarations.
2328 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2329 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002330
David Blaikie4e4d0842012-03-11 07:00:24 +00002331 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002332 Tok.is(tok::kw___if_not_exists))) {
2333 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2334 continue;
2335 }
2336
Douglas Gregor07976d22010-06-21 22:31:09 +00002337 // Check for extraneous top-level semicolon.
2338 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002339 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002340 continue;
2341 }
2342
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002343 if (Tok.is(tok::annot_pragma_vis)) {
2344 HandlePragmaVisibility();
2345 continue;
2346 }
2347
2348 if (Tok.is(tok::annot_pragma_pack)) {
2349 HandlePragmaPack();
2350 continue;
2351 }
2352
Douglas Gregor07976d22010-06-21 22:31:09 +00002353 AccessSpecifier AS = getAccessSpecifierIfPresent();
2354 if (AS != AS_none) {
2355 // Current token is a C++ access specifier.
2356 CurAS = AS;
2357 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002358 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002359 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002360 AccessAttrs.clear();
2361 MaybeParseGNUAttributes(AccessAttrs);
2362
David Blaikie13f8daf2011-10-13 06:08:43 +00002363 SourceLocation EndLoc;
2364 if (Tok.is(tok::colon)) {
2365 EndLoc = Tok.getLocation();
2366 ConsumeToken();
2367 } else if (Tok.is(tok::semi)) {
2368 EndLoc = Tok.getLocation();
2369 ConsumeToken();
2370 Diag(EndLoc, diag::err_expected_colon)
2371 << FixItHint::CreateReplacement(EndLoc, ":");
2372 } else {
2373 EndLoc = ASLoc.getLocWithOffset(TokLength);
2374 Diag(EndLoc, diag::err_expected_colon)
2375 << FixItHint::CreateInsertion(EndLoc, ":");
2376 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002377
2378 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2379 AccessAttrs.getList())) {
2380 // found another attribute than only annotations
2381 AccessAttrs.clear();
2382 }
2383
Douglas Gregor07976d22010-06-21 22:31:09 +00002384 continue;
2385 }
2386
2387 // FIXME: Make sure we don't have a template here.
2388
2389 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002390 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002391 }
2392
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002393 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002394 } else {
2395 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002396 }
Mike Stump1eb44332009-09-09 15:08:12 +00002397
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002398 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002399 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002400 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002401
John McCall42a4f662010-05-28 08:11:17 +00002402 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002403 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002404 T.getOpenLocation(),
2405 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002406 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002407
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002408 // C++11 [class.mem]p2:
2409 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002410 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002411 // brace-or-equal-initializers for non-static data members (including such
2412 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002413 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002414 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002415 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002416 // declarations and the lexed inline method definitions, along with any
2417 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002418 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002419 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002420 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002421
2422 // We've finished with all pending member declarations.
2423 Actions.ActOnFinishCXXMemberDecls();
2424
Richard Smith7a614d82011-06-11 17:19:42 +00002425 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002426 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002427 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002428 }
2429
John McCall42a4f662010-05-28 08:11:17 +00002430 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002431 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2432 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002433
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002434 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002435 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002436 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002437}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002438
2439/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2440/// which explicitly initializes the members or base classes of a
2441/// class (C++ [class.base.init]). For example, the three initializers
2442/// after the ':' in the Derived constructor below:
2443///
2444/// @code
2445/// class Base { };
2446/// class Derived : Base {
2447/// int x;
2448/// float f;
2449/// public:
2450/// Derived(float f) : Base(), x(17), f(f) { }
2451/// };
2452/// @endcode
2453///
Mike Stump1eb44332009-09-09 15:08:12 +00002454/// [C++] ctor-initializer:
2455/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002456///
Mike Stump1eb44332009-09-09 15:08:12 +00002457/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002458/// mem-initializer ...[opt]
2459/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002460void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002461 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2462
John Wiegley28bbe4b2011-04-28 01:08:34 +00002463 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2464 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002465 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002466
Chris Lattner5f9e2722011-07-23 10:55:15 +00002467 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002468 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002469
Douglas Gregor7ad83902008-11-05 04:29:56 +00002470 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002471 if (Tok.is(tok::code_completion)) {
2472 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2473 MemInitializers.data(),
2474 MemInitializers.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002475 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002476 } else {
2477 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2478 if (!MemInit.isInvalid())
2479 MemInitializers.push_back(MemInit.get());
2480 else
2481 AnyErrors = true;
2482 }
2483
Douglas Gregor7ad83902008-11-05 04:29:56 +00002484 if (Tok.is(tok::comma))
2485 ConsumeToken();
2486 else if (Tok.is(tok::l_brace))
2487 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002488 // If the next token looks like a base or member initializer, assume that
2489 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002490 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2491 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2492 Diag(Loc, diag::err_ctor_init_missing_comma)
2493 << FixItHint::CreateInsertion(Loc, ", ");
2494 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002495 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002496 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002497 SkipUntil(tok::l_brace, true, true);
2498 break;
2499 }
2500 } while (true);
2501
Mike Stump1eb44332009-09-09 15:08:12 +00002502 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002503 MemInitializers.data(), MemInitializers.size(),
2504 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002505}
2506
2507/// ParseMemInitializer - Parse a C++ member initializer, which is
2508/// part of a constructor initializer that explicitly initializes one
2509/// member or base class (C++ [class.base.init]). See
2510/// ParseConstructorInitializer for an example.
2511///
2512/// [C++] mem-initializer:
2513/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002514/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002515///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002516/// [C++] mem-initializer-id:
2517/// '::'[opt] nested-name-specifier[opt] class-name
2518/// identifier
John McCalld226f652010-08-21 09:40:31 +00002519Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002520 // parse '::'[opt] nested-name-specifier[opt]
2521 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002522 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002523 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002524 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002525 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002526 if (TemplateId->Kind == TNK_Type_template ||
2527 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002528 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002529 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002530 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002531 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002532 }
David Blaikief2116622012-01-24 06:03:59 +00002533 // Uses of decltype will already have been converted to annot_decltype by
2534 // ParseOptionalCXXScopeSpecifier at this point.
2535 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2536 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002537 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002538 return true;
2539 }
Mike Stump1eb44332009-09-09 15:08:12 +00002540
David Blaikief2116622012-01-24 06:03:59 +00002541 IdentifierInfo *II = 0;
2542 DeclSpec DS(AttrFactory);
2543 SourceLocation IdLoc = Tok.getLocation();
2544 if (Tok.is(tok::annot_decltype)) {
2545 // Get the decltype expression, if there is one.
2546 ParseDecltypeSpecifier(DS);
2547 } else {
2548 if (Tok.is(tok::identifier))
2549 // Get the identifier. This may be a member name or a class name,
2550 // but we'll let the semantic analysis determine which it is.
2551 II = Tok.getIdentifierInfo();
2552 ConsumeToken();
2553 }
2554
Douglas Gregor7ad83902008-11-05 04:29:56 +00002555
2556 // Parse the '('.
David Blaikie4e4d0842012-03-11 07:00:24 +00002557 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002558 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2559
Sebastian Redl6df65482011-09-24 17:48:25 +00002560 ExprResult InitList = ParseBraceInitializer();
2561 if (InitList.isInvalid())
2562 return true;
2563
2564 SourceLocation EllipsisLoc;
2565 if (Tok.is(tok::ellipsis))
2566 EllipsisLoc = ConsumeToken();
2567
2568 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002569 TemplateTypeTy, DS, IdLoc,
2570 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002571 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002572 BalancedDelimiterTracker T(*this, tok::l_paren);
2573 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002574
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002575 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002576 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002577 CommaLocsTy CommaLocs;
2578 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2579 SkipUntil(tok::r_paren);
2580 return true;
2581 }
2582
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002583 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002584
2585 SourceLocation EllipsisLoc;
2586 if (Tok.is(tok::ellipsis))
2587 EllipsisLoc = ConsumeToken();
2588
2589 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002590 TemplateTypeTy, DS, IdLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002591 T.getOpenLocation(), ArgExprs.data(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002592 ArgExprs.size(), T.getCloseLocation(),
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002593 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002594 }
2595
David Blaikie4e4d0842012-03-11 07:00:24 +00002596 Diag(Tok, getLangOpts().CPlusPlus0x ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002597 : diag::err_expected_lparen);
2598 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002599}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002600
Sebastian Redl7acafd02011-03-05 14:45:16 +00002601/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002602///
Douglas Gregora4745612008-12-01 18:00:20 +00002603/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002604/// dynamic-exception-specification
2605/// noexcept-specification
2606///
2607/// noexcept-specification:
2608/// 'noexcept'
2609/// 'noexcept' '(' constant-expression ')'
2610ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002611Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002612 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002613 SmallVectorImpl<ParsedType> &DynamicExceptions,
2614 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002615 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002616 ExceptionSpecificationType Result = EST_None;
2617
2618 // See if there's a dynamic specification.
2619 if (Tok.is(tok::kw_throw)) {
2620 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2621 DynamicExceptions,
2622 DynamicExceptionRanges);
2623 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2624 "Produced different number of exception types and ranges.");
2625 }
2626
2627 // If there's no noexcept specification, we're done.
2628 if (Tok.isNot(tok::kw_noexcept))
2629 return Result;
2630
Richard Smith841804b2011-10-17 23:06:20 +00002631 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2632
Sebastian Redl7acafd02011-03-05 14:45:16 +00002633 // If we already had a dynamic specification, parse the noexcept for,
2634 // recovery, but emit a diagnostic and don't store the results.
2635 SourceRange NoexceptRange;
2636 ExceptionSpecificationType NoexceptType = EST_None;
2637
2638 SourceLocation KeywordLoc = ConsumeToken();
2639 if (Tok.is(tok::l_paren)) {
2640 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002641 BalancedDelimiterTracker T(*this, tok::l_paren);
2642 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002643 NoexceptType = EST_ComputedNoexcept;
2644 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002645 // The argument must be contextually convertible to bool. We use
2646 // ActOnBooleanCondition for this purpose.
2647 if (!NoexceptExpr.isInvalid())
2648 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2649 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002650 T.consumeClose();
2651 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002652 } else {
2653 // There is no argument.
2654 NoexceptType = EST_BasicNoexcept;
2655 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2656 }
2657
2658 if (Result == EST_None) {
2659 SpecificationRange = NoexceptRange;
2660 Result = NoexceptType;
2661
2662 // If there's a dynamic specification after a noexcept specification,
2663 // parse that and ignore the results.
2664 if (Tok.is(tok::kw_throw)) {
2665 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2666 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2667 DynamicExceptionRanges);
2668 }
2669 } else {
2670 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2671 }
2672
2673 return Result;
2674}
2675
2676/// ParseDynamicExceptionSpecification - Parse a C++
2677/// dynamic-exception-specification (C++ [except.spec]).
2678///
2679/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002680/// 'throw' '(' type-id-list [opt] ')'
2681/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002682///
Douglas Gregora4745612008-12-01 18:00:20 +00002683/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002684/// type-id ... [opt]
2685/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002686///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002687ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2688 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002689 SmallVectorImpl<ParsedType> &Exceptions,
2690 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002691 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002692
Sebastian Redl7acafd02011-03-05 14:45:16 +00002693 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002694 BalancedDelimiterTracker T(*this, tok::l_paren);
2695 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002696 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2697 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002698 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002699 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002700
Douglas Gregora4745612008-12-01 18:00:20 +00002701 // Parse throw(...), a Microsoft extension that means "this function
2702 // can throw anything".
2703 if (Tok.is(tok::ellipsis)) {
2704 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002705 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002706 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002707 T.consumeClose();
2708 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002709 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002710 }
2711
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002712 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002713 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002714 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002715 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002716
Douglas Gregora04426c2010-12-20 23:57:46 +00002717 if (Tok.is(tok::ellipsis)) {
2718 // C++0x [temp.variadic]p5:
2719 // - In a dynamic-exception-specification (15.4); the pattern is a
2720 // type-id.
2721 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002722 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002723 if (!Res.isInvalid())
2724 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2725 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002726
Sebastian Redlef65f062009-05-29 18:02:33 +00002727 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002728 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002729 Ranges.push_back(Range);
2730 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002731
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002732 if (Tok.is(tok::comma))
2733 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002734 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002735 break;
2736 }
2737
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002738 T.consumeClose();
2739 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002740 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002741}
Douglas Gregor6569d682009-05-27 23:11:45 +00002742
Douglas Gregordab60ad2010-10-01 18:44:50 +00002743/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2744/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002745TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002746 assert(Tok.is(tok::arrow) && "expected arrow");
2747
2748 ConsumeToken();
2749
Richard Smith7796eb52012-03-12 08:56:40 +00002750 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002751}
2752
Douglas Gregor6569d682009-05-27 23:11:45 +00002753/// \brief We have just started parsing the definition of a new class,
2754/// so push that class onto our stack of classes that is currently
2755/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002756Sema::ParsingClassState
2757Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002758 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002759 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00002760 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
John McCalleee1d542011-02-14 07:13:47 +00002761 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002762}
2763
2764/// \brief Deallocate the given parsed class and all of its nested
2765/// classes.
2766void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002767 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2768 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002769 delete Class;
2770}
2771
2772/// \brief Pop the top class of the stack of classes that are
2773/// currently being parsed.
2774///
2775/// This routine should be called when we have finished parsing the
2776/// definition of a class, but have not yet popped the Scope
2777/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00002778void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002779 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002780
John McCalleee1d542011-02-14 07:13:47 +00002781 Actions.PopParsingClass(state);
2782
Douglas Gregor6569d682009-05-27 23:11:45 +00002783 ParsingClass *Victim = ClassStack.top();
2784 ClassStack.pop();
2785 if (Victim->TopLevelClass) {
2786 // Deallocate all of the nested classes of this class,
2787 // recursively: we don't need to keep any of this information.
2788 DeallocateParsedClasses(Victim);
2789 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002790 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002791 assert(!ClassStack.empty() && "Missing top-level class?");
2792
Douglas Gregord54eb442010-10-12 16:25:54 +00002793 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002794 // The victim is a nested class, but we will not need to perform
2795 // any processing after the definition of this class since it has
2796 // no members whose handling was delayed. Therefore, we can just
2797 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002798 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002799 return;
2800 }
2801
2802 // This nested class has some members that will need to be processed
2803 // after the top-level class is completely defined. Therefore, add
2804 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002805 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002806 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002807 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00002808}
Sean Huntbbd37c62009-11-21 08:43:09 +00002809
Richard Smithc56298d2012-04-10 03:25:07 +00002810/// \brief Try to parse an 'identifier' which appears within an attribute-token.
2811///
2812/// \return the parsed identifier on success, and 0 if the next token is not an
2813/// attribute-token.
2814///
2815/// C++11 [dcl.attr.grammar]p3:
2816/// If a keyword or an alternative token that satisfies the syntactic
2817/// requirements of an identifier is contained in an attribute-token,
2818/// it is considered an identifier.
2819IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
2820 switch (Tok.getKind()) {
2821 default:
2822 // Identifiers and keywords have identifier info attached.
2823 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
2824 Loc = ConsumeToken();
2825 return II;
2826 }
2827 return 0;
2828
2829 case tok::ampamp: // 'and'
2830 case tok::pipe: // 'bitor'
2831 case tok::pipepipe: // 'or'
2832 case tok::caret: // 'xor'
2833 case tok::tilde: // 'compl'
2834 case tok::amp: // 'bitand'
2835 case tok::ampequal: // 'and_eq'
2836 case tok::pipeequal: // 'or_eq'
2837 case tok::caretequal: // 'xor_eq'
2838 case tok::exclaim: // 'not'
2839 case tok::exclaimequal: // 'not_eq'
2840 // Alternative tokens do not have identifier info, but their spelling
2841 // starts with an alphabetical character.
2842 llvm::SmallString<8> SpellingBuf;
2843 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
2844 if (std::isalpha(Spelling[0])) {
2845 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00002846 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00002847 }
2848 return 0;
2849 }
2850}
2851
2852/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002853/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00002854///
Richard Smith6ee326a2012-04-10 01:32:12 +00002855/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00002856/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002857/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00002858///
Richard Smith6ee326a2012-04-10 01:32:12 +00002859/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00002860/// attribute[opt]
2861/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00002862/// attribute '...'
2863/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00002864///
Richard Smith6ee326a2012-04-10 01:32:12 +00002865/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002866/// attribute-token attribute-argument-clause[opt]
2867///
Richard Smith6ee326a2012-04-10 01:32:12 +00002868/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00002869/// identifier
2870/// attribute-scoped-token
2871///
Richard Smith6ee326a2012-04-10 01:32:12 +00002872/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00002873/// attribute-namespace '::' identifier
2874///
Richard Smith6ee326a2012-04-10 01:32:12 +00002875/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00002876/// identifier
2877///
Richard Smith6ee326a2012-04-10 01:32:12 +00002878/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00002879/// '(' balanced-token-seq ')'
2880///
Richard Smith6ee326a2012-04-10 01:32:12 +00002881/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00002882/// balanced-token
2883/// balanced-token-seq balanced-token
2884///
Richard Smith6ee326a2012-04-10 01:32:12 +00002885/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00002886/// '(' balanced-token-seq ')'
2887/// '[' balanced-token-seq ']'
2888/// '{' balanced-token-seq '}'
2889/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00002890void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002891 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002892 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00002893 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002894 ParseAlignmentSpecifier(attrs, endLoc);
2895 return;
2896 }
2897
Sean Huntbbd37c62009-11-21 08:43:09 +00002898 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00002899 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00002900
Richard Smith41be6732011-10-14 20:48:27 +00002901 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
2902
Sean Huntbbd37c62009-11-21 08:43:09 +00002903 ConsumeBracket();
2904 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002905
Richard Smithc56298d2012-04-10 03:25:07 +00002906 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002907 // attribute not present
2908 if (Tok.is(tok::comma)) {
2909 ConsumeToken();
2910 continue;
2911 }
2912
Richard Smithc56298d2012-04-10 03:25:07 +00002913 SourceLocation ScopeLoc, AttrLoc;
2914 IdentifierInfo *ScopeName = 0, *AttrName = 0;
2915
2916 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
2917 if (!AttrName)
2918 // Break out to the "expected ']'" diagnostic.
2919 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002920
Sean Huntbbd37c62009-11-21 08:43:09 +00002921 // scoped attribute
2922 if (Tok.is(tok::coloncolon)) {
2923 ConsumeToken();
2924
Richard Smithc56298d2012-04-10 03:25:07 +00002925 ScopeName = AttrName;
2926 ScopeLoc = AttrLoc;
2927
2928 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
2929 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002930 Diag(Tok.getLocation(), diag::err_expected_ident);
2931 SkipUntil(tok::r_square, tok::comma, true, true);
2932 continue;
2933 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002934 }
2935
2936 bool AttrParsed = false;
Sean Hunt93f95f22012-06-18 16:13:52 +00002937 switch (AttributeList::getKind(AttrName, ScopeName,
2938 AttributeList::AS_CXX11)) {
Richard Smithe0d3b4c2012-05-03 18:27:39 +00002939 // No arguments
Sean Hunt8e083e72012-06-19 23:57:03 +00002940 case AttributeList::AT_CarriesDependency:
Richard Smithe0d3b4c2012-05-03 18:27:39 +00002941 // FIXME: implement generic support of attributes with C++11 syntax
2942 // see Parse/ParseDecl.cpp: ParseGNUAttributes
Sean Hunt8e083e72012-06-19 23:57:03 +00002943 case AttributeList::AT_FallThrough:
2944 case AttributeList::AT_NoReturn: {
Richard Smithe0d3b4c2012-05-03 18:27:39 +00002945 if (Tok.is(tok::l_paren)) {
2946 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
2947 << AttrName->getName();
Sean Huntbbd37c62009-11-21 08:43:09 +00002948 break;
2949 }
2950
Richard Smithe0d3b4c2012-05-03 18:27:39 +00002951 attrs.addNew(AttrName,
2952 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
2953 AttrLoc),
2954 ScopeName, ScopeLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002955 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smithe0d3b4c2012-05-03 18:27:39 +00002956 AttrParsed = true;
2957 break;
2958 }
2959
2960 // Silence warnings
2961 default: break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002962 }
2963
2964 // Skip the entire parameter clause, if any
2965 if (!AttrParsed && Tok.is(tok::l_paren)) {
2966 ConsumeParen();
2967 // SkipUntil maintains the balancedness of tokens.
2968 SkipUntil(tok::r_paren, false);
2969 }
Richard Smith6ee326a2012-04-10 01:32:12 +00002970
Richard Smithc56298d2012-04-10 03:25:07 +00002971 if (Tok.is(tok::ellipsis)) {
2972 if (AttrParsed)
2973 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
2974 << AttrName->getName();
Richard Smith6ee326a2012-04-10 01:32:12 +00002975 ConsumeToken();
Richard Smithc56298d2012-04-10 03:25:07 +00002976 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002977 }
2978
2979 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2980 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002981 if (endLoc)
2982 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00002983 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2984 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002985}
Sean Huntbbd37c62009-11-21 08:43:09 +00002986
Sean Hunt2edf0a22012-06-23 05:07:58 +00002987/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002988///
2989/// attribute-specifier-seq:
2990/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00002991void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002992 SourceLocation *endLoc) {
2993 SourceLocation StartLoc = Tok.getLocation(), Loc;
2994 if (!endLoc)
2995 endLoc = &Loc;
2996
Douglas Gregor8828ee72011-10-07 20:35:25 +00002997 do {
Richard Smithc56298d2012-04-10 03:25:07 +00002998 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00002999 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003000
3001 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003002}
3003
Francois Pichet334d47e2010-10-11 12:59:39 +00003004/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3005///
3006/// [MS] ms-attribute:
3007/// '[' token-seq ']'
3008///
3009/// [MS] ms-attribute-seq:
3010/// ms-attribute[opt]
3011/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003012void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3013 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003014 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3015
3016 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003017 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003018 ConsumeBracket();
3019 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003020 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003021 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3022 }
3023}
Francois Pichet563a6452011-05-25 10:19:49 +00003024
3025void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3026 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003027 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003028 if (ParseMicrosoftIfExistsCondition(Result))
3029 return;
3030
Douglas Gregor3896fc52011-10-24 22:31:10 +00003031 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3032 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003033 Diag(Tok, diag::err_expected_lbrace);
3034 return;
3035 }
Francois Pichet563a6452011-05-25 10:19:49 +00003036
Douglas Gregor3896fc52011-10-24 22:31:10 +00003037 switch (Result.Behavior) {
3038 case IEB_Parse:
3039 // Parse the declarations below.
3040 break;
3041
3042 case IEB_Dependent:
3043 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3044 << Result.IsIfExists;
3045 // Fall through to skip.
3046
3047 case IEB_Skip:
3048 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003049 return;
3050 }
3051
Douglas Gregor3896fc52011-10-24 22:31:10 +00003052 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003053 // __if_exists, __if_not_exists can nest.
3054 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3055 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3056 continue;
3057 }
3058
3059 // Check for extraneous top-level semicolon.
3060 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003061 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003062 continue;
3063 }
3064
3065 AccessSpecifier AS = getAccessSpecifierIfPresent();
3066 if (AS != AS_none) {
3067 // Current token is a C++ access specifier.
3068 CurAS = AS;
3069 SourceLocation ASLoc = Tok.getLocation();
3070 ConsumeToken();
3071 if (Tok.is(tok::colon))
3072 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3073 else
3074 Diag(Tok, diag::err_expected_colon);
3075 ConsumeToken();
3076 continue;
3077 }
3078
3079 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003080 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003081 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003082
3083 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003084}