blob: c296ee5b4aa9526eb4e88a1bae5cce5e25c870e9 [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;
584 MultiTemplateParamsArg TemplateParamsArg(Actions,
585 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);
John McCalld226f652010-08-21 09:40:31 +0000619 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000620 }
Mike Stump1eb44332009-09-09 15:08:12 +0000621
John McCall60d7b3a2010-08-24 06:29:42 +0000622 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000623 if (AssertExpr.isInvalid()) {
624 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000625 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000626 }
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Anders Carlssonad5f9602009-03-13 23:29:20 +0000628 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000629 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000630
Richard Smith0cc323c2012-03-05 23:20:05 +0000631 if (!isTokenStringLiteral()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000632 Diag(Tok, diag::err_expected_string_literal);
633 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000634 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000635 }
Mike Stump1eb44332009-09-09 15:08:12 +0000636
John McCall60d7b3a2010-08-24 06:29:42 +0000637 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smith99831e42012-03-06 03:21:47 +0000638 if (AssertMessage.isInvalid()) {
639 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000640 return 0;
Richard Smith99831e42012-03-06 03:21:47 +0000641 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000642
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000643 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Chris Lattner97144fc2009-04-02 04:16:50 +0000645 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000646 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000647
John McCall9ae2f072010-08-23 23:25:46 +0000648 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
649 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000650 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000651 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000652}
653
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000654/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
655///
656/// 'decltype' ( expression )
657///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000658SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
659 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
660 && "Not a decltype specifier");
661
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000662
David Blaikie42d6d0c2011-12-04 05:04:18 +0000663 ExprResult Result;
664 SourceLocation StartLoc = Tok.getLocation();
665 SourceLocation EndLoc;
666
667 if (Tok.is(tok::annot_decltype)) {
668 Result = getExprAnnotation(Tok);
669 EndLoc = Tok.getAnnotationEndLoc();
670 ConsumeToken();
671 if (Result.isInvalid()) {
672 DS.SetTypeSpecError();
673 return EndLoc;
674 }
675 } else {
Richard Smithc7b55432012-02-24 22:30:04 +0000676 if (Tok.getIdentifierInfo()->isStr("decltype"))
677 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000678
David Blaikie42d6d0c2011-12-04 05:04:18 +0000679 ConsumeToken();
680
681 BalancedDelimiterTracker T(*this, tok::l_paren);
682 if (T.expectAndConsume(diag::err_expected_lparen_after,
683 "decltype", tok::r_paren)) {
684 DS.SetTypeSpecError();
685 return T.getOpenLocation() == Tok.getLocation() ?
686 StartLoc : T.getOpenLocation();
687 }
688
689 // Parse the expression
690
691 // C++0x [dcl.type.simple]p4:
692 // The operand of the decltype specifier is an unevaluated operand.
Richard Smith76f3f692012-02-22 02:04:18 +0000693 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
694 0, /*IsDecltype=*/true);
David Blaikie42d6d0c2011-12-04 05:04:18 +0000695 Result = ParseExpression();
696 if (Result.isInvalid()) {
Richard Smithd8e4dac2012-02-27 05:24:00 +0000697 SkipUntil(tok::r_paren);
David Blaikie42d6d0c2011-12-04 05:04:18 +0000698 DS.SetTypeSpecError();
Richard Smithd8e4dac2012-02-27 05:24:00 +0000699 return StartLoc;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000700 }
701
702 // Match the ')'
703 T.consumeClose();
704 if (T.getCloseLocation().isInvalid()) {
705 DS.SetTypeSpecError();
706 // FIXME: this should return the location of the last token
707 // that was consumed (by "consumeClose()")
708 return T.getCloseLocation();
709 }
710
Richard Smith76f3f692012-02-22 02:04:18 +0000711 Result = Actions.ActOnDecltypeExpression(Result.take());
712 if (Result.isInvalid()) {
713 DS.SetTypeSpecError();
714 return T.getCloseLocation();
715 }
716
David Blaikie42d6d0c2011-12-04 05:04:18 +0000717 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000718 }
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000720 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000721 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000722 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000723 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
David Blaikie42d6d0c2011-12-04 05:04:18 +0000724 DiagID, Result.release())) {
John McCallfec54012009-08-03 20:12:06 +0000725 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000726 DS.SetTypeSpecError();
727 }
728 return EndLoc;
729}
730
731void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
732 SourceLocation StartLoc,
733 SourceLocation EndLoc) {
734 // make sure we have a token we can turn into an annotation token
735 if (PP.isBacktrackEnabled())
736 PP.RevertCachedTokens(1);
737 else
738 PP.EnterToken(Tok);
739
740 Tok.setKind(tok::annot_decltype);
741 setExprAnnotation(Tok, DS.getTypeSpecType() == TST_decltype ?
742 DS.getRepAsExpr() : ExprResult());
743 Tok.setAnnotationEndLoc(EndLoc);
744 Tok.setLocation(StartLoc);
745 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000746}
747
Sean Huntdb5d44b2011-05-19 05:37:45 +0000748void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
749 assert(Tok.is(tok::kw___underlying_type) &&
750 "Not an underlying type specifier");
751
752 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000753 BalancedDelimiterTracker T(*this, tok::l_paren);
754 if (T.expectAndConsume(diag::err_expected_lparen_after,
755 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000756 return;
757 }
758
759 TypeResult Result = ParseTypeName();
760 if (Result.isInvalid()) {
761 SkipUntil(tok::r_paren);
762 return;
763 }
764
765 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000766 T.consumeClose();
767 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000768 return;
769
770 const char *PrevSpec = 0;
771 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000772 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000773 DiagID, Result.release()))
774 Diag(StartLoc, DiagID) << PrevSpec;
775}
776
David Blaikie09048df2011-10-25 15:01:20 +0000777/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
778/// class name or decltype-specifier. Note that we only check that the result
779/// names a type; semantic analysis will need to verify that the type names a
780/// class. The result is either a type or null, depending on whether a type
781/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000782///
David Blaikie09048df2011-10-25 15:01:20 +0000783/// base-type-specifier: [C++ 10.1]
784/// class-or-decltype
785/// class-or-decltype: [C++ 10.1]
786/// nested-name-specifier[opt] class-name
787/// decltype-specifier
Douglas Gregor42a552f2008-11-05 20:51:48 +0000788/// class-name: [C++ 9.1]
789/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000790/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000791///
David Blaikie22216eb2011-10-25 17:10:12 +0000792Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
793 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000794 // Ignore attempts to use typename
795 if (Tok.is(tok::kw_typename)) {
796 Diag(Tok, diag::err_expected_class_name_not_template)
797 << FixItHint::CreateRemoval(Tok.getLocation());
798 ConsumeToken();
799 }
800
David Blaikie152aa4b2011-10-25 18:17:58 +0000801 // Parse optional nested-name-specifier
802 CXXScopeSpec SS;
803 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
804
805 BaseLoc = Tok.getLocation();
806
David Blaikie22216eb2011-10-25 17:10:12 +0000807 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000808 // tok == kw_decltype is just error recovery, it can only happen when SS
809 // isn't empty
810 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000811 if (SS.isNotEmpty())
812 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
813 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000814 // Fake up a Declarator to use with ActOnTypeName.
815 DeclSpec DS(AttrFactory);
816
David Blaikieb5777572011-12-08 04:53:15 +0000817 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000818
819 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
820 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
821 }
822
Douglas Gregor7f43d672009-02-25 23:52:28 +0000823 // Check whether we have a template-id that names a type.
824 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000825 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000826 if (TemplateId->Kind == TNK_Type_template ||
827 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000828 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000829
830 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000831 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000832 EndLocation = Tok.getAnnotationEndLoc();
833 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000834
835 if (Type)
836 return Type;
837 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000838 }
839
840 // Fall through to produce an error below.
841 }
842
Douglas Gregor42a552f2008-11-05 20:51:48 +0000843 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000844 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000845 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000846 }
847
Douglas Gregor84d0a192010-01-12 21:28:44 +0000848 IdentifierInfo *Id = Tok.getIdentifierInfo();
849 SourceLocation IdLoc = ConsumeToken();
850
851 if (Tok.is(tok::less)) {
852 // It looks the user intended to write a template-id here, but the
853 // template-name was wrong. Try to fix that.
854 TemplateNameKind TNK = TNK_Type_template;
855 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000856 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000857 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000858 Diag(IdLoc, diag::err_unknown_template_name)
859 << Id;
860 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000861
Douglas Gregor84d0a192010-01-12 21:28:44 +0000862 if (!Template)
863 return true;
864
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000865 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000866 UnqualifiedId TemplateName;
867 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000868
Douglas Gregor84d0a192010-01-12 21:28:44 +0000869 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000870 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
871 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000872 return true;
873 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000874 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000875
Douglas Gregor84d0a192010-01-12 21:28:44 +0000876 // If we didn't end up with a typename token, there's nothing more we
877 // can do.
878 if (Tok.isNot(tok::annot_typename))
879 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000880
Douglas Gregor84d0a192010-01-12 21:28:44 +0000881 // Retrieve the type from the annotation token, consume that token, and
882 // return.
883 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000884 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000885 ConsumeToken();
886 return Type;
887 }
888
Douglas Gregor42a552f2008-11-05 20:51:48 +0000889 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000890 IdentifierInfo *CorrectedII = 0;
Douglas Gregor059101f2011-03-02 00:47:37 +0000891 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000892 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000893 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000894 /*NonTrivialTypeSourceInfo=*/true,
895 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000896 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000897 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000898 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000899 }
900
901 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000902 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000903
904 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000905 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000906 DS.SetRangeStart(IdLoc);
907 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000908 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000909
910 const char *PrevSpec = 0;
911 unsigned DiagID;
912 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
913
914 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
915 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000916}
917
John McCallc052dbb2012-05-22 21:28:12 +0000918void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
919 while (Tok.is(tok::kw___single_inheritance) ||
920 Tok.is(tok::kw___multiple_inheritance) ||
921 Tok.is(tok::kw___virtual_inheritance)) {
922 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
923 SourceLocation AttrNameLoc = ConsumeToken();
924 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000925 SourceLocation(), 0, 0, AttributeList::AS_GNU);
John McCallc052dbb2012-05-22 21:28:12 +0000926 }
927}
928
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000929/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
930/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
931/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +0000932/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000933///
934/// class-specifier: [C++ class]
935/// class-head '{' member-specification[opt] '}'
936/// class-head '{' member-specification[opt] '}' attributes[opt]
937/// class-head:
938/// class-key identifier[opt] base-clause[opt]
939/// class-key nested-name-specifier identifier base-clause[opt]
940/// class-key nested-name-specifier[opt] simple-template-id
941/// base-clause[opt]
942/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000943/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000944/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000945/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000946/// simple-template-id base-clause[opt]
947/// class-key:
948/// 'class'
949/// 'struct'
950/// 'union'
951///
952/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000953/// class-key ::[opt] nested-name-specifier[opt] identifier
954/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
955/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000956///
957/// Note that the C++ class-specifier and elaborated-type-specifier,
958/// together, subsume the C99 struct-or-union-specifier:
959///
960/// struct-or-union-specifier: [C99 6.7.2.1]
961/// struct-or-union identifier[opt] '{' struct-contents '}'
962/// struct-or-union identifier
963/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
964/// '}' attributes[opt]
965/// [GNU] struct-or-union attributes[opt] identifier
966/// struct-or-union:
967/// 'struct'
968/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000969void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
970 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000971 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000972 AccessSpecifier AS,
Richard Smith69730c12012-03-12 07:56:15 +0000973 bool EnteringContext, DeclSpecContext DSC) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000974 DeclSpec::TST TagType;
975 if (TagTokKind == tok::kw_struct)
976 TagType = DeclSpec::TST_struct;
977 else if (TagTokKind == tok::kw_class)
978 TagType = DeclSpec::TST_class;
979 else {
980 assert(TagTokKind == tok::kw_union && "Not a class specifier");
981 TagType = DeclSpec::TST_union;
982 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000983
Douglas Gregor374929f2009-09-18 15:37:17 +0000984 if (Tok.is(tok::code_completion)) {
985 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000986 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000987 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +0000988 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000989
Chandler Carruth926c4b42010-06-28 08:39:25 +0000990 // C++03 [temp.explicit] 14.7.2/8:
991 // The usual access checking rules do not apply to names used to specify
992 // explicit instantiations.
993 //
994 // As an extension we do not perform access checking on the names used to
995 // specify explicit specializations either. This is important to allow
996 // specializing traits classes for private types.
John McCall13489672012-05-07 06:16:58 +0000997 //
998 // Note that we don't suppress if this turns out to be an elaborated
999 // type specifier.
1000 bool shouldDelayDiagsInTag =
1001 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1002 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1003 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001004
Sean Hunt2edf0a22012-06-23 05:07:58 +00001005 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001006 // If attributes exist after tag, parse them.
1007 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +00001008 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001009
Steve Narofff59e17e2008-12-24 20:59:21 +00001010 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001011 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001012 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001013
John McCallc052dbb2012-05-22 21:28:12 +00001014 // Parse inheritance specifiers.
1015 if (Tok.is(tok::kw___single_inheritance) ||
1016 Tok.is(tok::kw___multiple_inheritance) ||
1017 Tok.is(tok::kw___virtual_inheritance))
1018 ParseMicrosoftInheritanceClassAttributes(attrs);
1019
Sean Huntbbd37c62009-11-21 08:43:09 +00001020 // If C++0x attributes exist here, parse them.
1021 // FIXME: Are we consistent with the ordering of parsing of different
1022 // styles of attributes?
John McCall7f040a92010-12-24 02:08:15 +00001023 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001024
John Wiegley20c0da72011-04-27 23:09:49 +00001025 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +00001026 !Tok.is(tok::identifier) &&
1027 Tok.getIdentifierInfo() &&
1028 (Tok.is(tok::kw___is_arithmetic) ||
1029 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001030 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001031 Tok.is(tok::kw___is_floating_point) ||
1032 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001033 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001034 Tok.is(tok::kw___is_integral) ||
1035 Tok.is(tok::kw___is_member_function_pointer) ||
1036 Tok.is(tok::kw___is_member_pointer) ||
1037 Tok.is(tok::kw___is_pod) ||
1038 Tok.is(tok::kw___is_pointer) ||
1039 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001040 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001041 Tok.is(tok::kw___is_signed) ||
1042 Tok.is(tok::kw___is_unsigned) ||
1043 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001044 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001045 // name of struct templates, but some are keywords in GCC >= 4.3
1046 // and Clang. Therefore, when we see the token sequence "struct
1047 // X", make X into a normal identifier rather than a keyword, to
1048 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001049 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001050 Tok.setKind(tok::identifier);
1051 }
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001053 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001054 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001055 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001056 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1057 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001058
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001059 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001060 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001061 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001062 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1063 Diag(Tok, diag::err_expected_ident);
1064 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001065
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001066 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1067
Douglas Gregorcc636682009-02-17 23:15:12 +00001068 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001069 IdentifierInfo *Name = 0;
1070 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001071 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001072 if (Tok.is(tok::identifier)) {
1073 Name = Tok.getIdentifierInfo();
1074 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001075
David Blaikie4e4d0842012-03-11 07:00:24 +00001076 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001077 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001078 // Eat the template argument list and try to continue parsing this as
1079 // a class (or template thereof).
1080 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001081 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001082 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001083 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001084 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001085 // We couldn't parse the template argument list at all, so don't
1086 // try to give any location information for the list.
1087 LAngleLoc = RAngleLoc = SourceLocation();
1088 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001089
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001090 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001091 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001092 << (TagType == DeclSpec::TST_class? 0
1093 : TagType == DeclSpec::TST_struct? 1
1094 : 2)
1095 << Name
1096 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001097
1098 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001099 // we've removed its template argument list.
1100 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1101 if (TemplateParams && TemplateParams->size() > 1) {
1102 TemplateParams->pop_back();
1103 } else {
1104 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001105 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001106 = ParsedTemplateInfo::NonTemplate;
1107 }
1108 } else if (TemplateInfo.Kind
1109 == ParsedTemplateInfo::ExplicitInstantiation) {
1110 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001111 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001112 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001113 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001114 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001115 = SourceLocation();
1116 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1117 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001118 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001119 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001120 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001121 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001122 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001123
Douglas Gregor059101f2011-03-02 00:47:37 +00001124 if (TemplateId->Kind != TNK_Type_template &&
1125 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001126 // The template-name in the simple-template-id refers to
1127 // something other than a class template. Give an appropriate
1128 // error message and skip to the ';'.
1129 SourceRange Range(NameLoc);
1130 if (SS.isNotEmpty())
1131 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001132
Douglas Gregor39a8de12009-02-25 19:37:18 +00001133 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1134 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Douglas Gregor39a8de12009-02-25 19:37:18 +00001136 DS.SetTypeSpecError();
1137 SkipUntil(tok::semi, false, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001138 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001139 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001140 }
1141
Richard Smith7796eb52012-03-12 08:56:40 +00001142 // There are four options here.
1143 // - If we are in a trailing return type, this is always just a reference,
1144 // and we must not try to parse a definition. For instance,
1145 // [] () -> struct S { };
1146 // does not define a type.
1147 // - If we have 'struct foo {...', 'struct foo :...',
1148 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1149 // - If we have 'struct foo;', then this is either a forward declaration
1150 // or a friend declaration, which have to be treated differently.
1151 // - Otherwise we have something like 'struct foo xyz', a reference.
Richard Smith69730c12012-03-12 07:56:15 +00001152 // However, in type-specifier-seq's, things look like declarations but are
1153 // just references, e.g.
1154 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001155 // or
Richard Smith69730c12012-03-12 07:56:15 +00001156 // &T::operator struct s;
1157 // For these, DSC is DSC_type_specifier.
John McCallf312b1e2010-08-26 23:41:50 +00001158 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001159 if (DSC == DSC_trailing)
1160 TUK = Sema::TUK_Reference;
1161 else if (Tok.is(tok::l_brace) ||
1162 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
1163 (isCXX0XFinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001164 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001165 if (DS.isFriendSpecified()) {
1166 // C++ [class.friend]p2:
1167 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001168 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001169 << SourceRange(DS.getFriendSpecLoc());
1170
1171 // Skip everything up to the semicolon, so that this looks like a proper
1172 // friend class (or template thereof) declaration.
1173 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001174 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001175 } else {
1176 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001177 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001178 }
Richard Smith69730c12012-03-12 07:56:15 +00001179 } else if (Tok.is(tok::semi) && DSC != DSC_type_specifier)
John McCallf312b1e2010-08-26 23:41:50 +00001180 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001181 else
John McCallf312b1e2010-08-26 23:41:50 +00001182 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001183
John McCall13489672012-05-07 06:16:58 +00001184 // If this is an elaborated type specifier, and we delayed
1185 // diagnostics before, just merge them into the current pool.
1186 if (shouldDelayDiagsInTag) {
1187 diagsFromTag.done();
1188 if (TUK == Sema::TUK_Reference)
1189 diagsFromTag.redelay();
1190 }
1191
John McCall207014e2010-07-30 06:26:29 +00001192 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001193 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001194 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1195 // We have a declaration or reference to an anonymous class.
1196 Diag(StartLoc, diag::err_anon_type_definition)
1197 << DeclSpec::getSpecifierName(TagType);
1198 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001199
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001200 SkipUntil(tok::comma, true);
1201 return;
1202 }
1203
Douglas Gregorddc29e12009-02-06 22:42:48 +00001204 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001205 DeclResult TagOrTempResult = true; // invalid
1206 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001207
Douglas Gregor402abb52009-05-28 23:31:59 +00001208 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001209 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001210 // Explicit specialization, class template partial specialization,
1211 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001212 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001213 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001214 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001215 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001216 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001217 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001218 ProhibitAttributes(attrs);
1219
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001220 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001221 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001222 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001223 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001224 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001225 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001226 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001227 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001228 TemplateId->TemplateNameLoc,
1229 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001230 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001231 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001232 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001233
1234 // Friend template-ids are treated as references unless
1235 // they have template headers, in which case they're ill-formed
1236 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1237 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001238 } else if (TUK == Sema::TUK_Reference ||
1239 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001240 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001241 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001242 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001243 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001244 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001245 TemplateId->Template,
1246 TemplateId->TemplateNameLoc,
1247 TemplateId->LAngleLoc,
1248 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001249 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001250 } else {
1251 // This is an explicit specialization or a class template
1252 // partial specialization.
1253 TemplateParameterLists FakedParamLists;
1254
1255 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1256 // This looks like an explicit instantiation, because we have
1257 // something like
1258 //
1259 // template class Foo<X>
1260 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001261 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001262 // meant to be an explicit specialization, but the user forgot
1263 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +00001264 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001265
Mike Stump1eb44332009-09-09 15:08:12 +00001266 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001267 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001268 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001269 diag::err_explicit_instantiation_with_definition)
1270 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +00001271 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001272
1273 // Create a fake template parameter list that contains only
1274 // "template<>", so that we treat this construct as a class
1275 // template specialization.
1276 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +00001277 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001278 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001279 LAngleLoc,
1280 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001281 LAngleLoc));
1282 TemplateParams = &FakedParamLists;
1283 }
1284
1285 // Build the class template specialization.
1286 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001287 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001288 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001289 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001290 TemplateId->TemplateNameLoc,
1291 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001292 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001293 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001294 attrs.getList(),
John McCallf312b1e2010-08-26 23:41:50 +00001295 MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +00001296 TemplateParams? &(*TemplateParams)[0] : 0,
1297 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001298 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001299 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001300 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001301 // Explicit instantiation of a member of a class template
1302 // specialization, e.g.,
1303 //
1304 // template struct Outer<int>::Inner;
1305 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001306 ProhibitAttributes(attrs);
1307
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001308 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001309 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001310 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001311 TemplateInfo.TemplateLoc,
1312 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001313 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001314 } else if (TUK == Sema::TUK_Friend &&
1315 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001316 ProhibitAttributes(attrs);
1317
John McCall9a34edb2010-10-19 01:40:49 +00001318 TagOrTempResult =
1319 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1320 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001321 Name, NameLoc, attrs.getList(),
John McCall9a34edb2010-10-19 01:40:49 +00001322 MultiTemplateParamsArg(Actions,
1323 TemplateParams? &(*TemplateParams)[0] : 0,
1324 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001325 } else {
1326 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001327 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001328 // FIXME: Diagnose this particular error.
1329 }
1330
Sean Hunt2edf0a22012-06-23 05:07:58 +00001331 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1332 ProhibitAttributes(attrs);
1333
John McCallc4e70192009-09-11 04:59:25 +00001334 bool IsDependent = false;
1335
John McCalla25c4082010-10-19 18:40:57 +00001336 // Don't pass down template parameter lists if this is just a tag
1337 // reference. For example, we don't need the template parameters here:
1338 // template <class T> class A *makeA(T t);
1339 MultiTemplateParamsArg TParams;
1340 if (TUK != Sema::TUK_Reference && TemplateParams)
1341 TParams =
1342 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1343
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001344 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001345 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001346 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001347 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001348 TParams, Owned, IsDependent,
1349 SourceLocation(), false,
1350 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001351
1352 // If ActOnTag said the type was dependent, try again with the
1353 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001354 if (IsDependent) {
1355 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001356 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001357 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001358 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001359 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001360
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001361 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001362 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001363 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001364 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001365 isCXX0XFinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001366 if (getLangOpts().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +00001367 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001368 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001369 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001370 }
1371
John McCallb3d87482010-08-24 05:47:05 +00001372 const char *PrevSpec = 0;
1373 unsigned DiagID;
1374 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001375 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001376 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1377 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001378 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001379 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001380 Result = DS.SetTypeSpecType(TagType, StartLoc,
1381 NameLoc.isValid() ? NameLoc : StartLoc,
1382 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001383 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001384 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001385 return;
1386 }
Mike Stump1eb44332009-09-09 15:08:12 +00001387
John McCallb3d87482010-08-24 05:47:05 +00001388 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001389 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001390
Chris Lattner4ed5d912010-02-02 01:23:29 +00001391 // At this point, we've successfully parsed a class-specifier in 'definition'
1392 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1393 // going to look at what comes after it to improve error recovery. If an
1394 // impossible token occurs next, we assume that the programmer forgot a ; at
1395 // the end of the declaration and recover that way.
1396 //
1397 // This switch enumerates the valid "follow" set for definition.
John McCallf312b1e2010-08-26 23:41:50 +00001398 if (TUK == Sema::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001399 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001400 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001401 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001402 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +00001403 case tok::star: // struct foo {...} * P;
1404 case tok::amp: // struct foo {...} & R = ...
1405 case tok::identifier: // struct foo {...} V ;
1406 case tok::r_paren: //(struct foo {...} ) {4}
1407 case tok::annot_cxxscope: // struct foo {...} a:: b;
1408 case tok::annot_typename: // struct foo {...} a ::b;
1409 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +00001410 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +00001411 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001412 ExpectedSemi = false;
1413 break;
1414 // Type qualifiers
1415 case tok::kw_const: // struct foo {...} const x;
1416 case tok::kw_volatile: // struct foo {...} volatile x;
1417 case tok::kw_restrict: // struct foo {...} restrict x;
1418 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +00001419 // Storage-class specifiers
1420 case tok::kw_static: // struct foo {...} static x;
1421 case tok::kw_extern: // struct foo {...} extern x;
1422 case tok::kw_typedef: // struct foo {...} typedef x;
1423 case tok::kw_register: // struct foo {...} register x;
1424 case tok::kw_auto: // struct foo {...} auto x;
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001425 case tok::kw_mutable: // struct foo {...} mutable x;
1426 case tok::kw_constexpr: // struct foo {...} constexpr x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001427 // As shown above, type qualifiers and storage class specifiers absolutely
1428 // can occur after class specifiers according to the grammar. However,
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001429 // almost no one actually writes code like this. If we see one of these,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001430 // it is much more likely that someone missed a semi colon and the
1431 // type/storage class specifier we're seeing is part of the *next*
1432 // intended declaration, as in:
1433 //
1434 // struct foo { ... }
1435 // typedef int X;
1436 //
1437 // We'd really like to emit a missing semicolon error instead of emitting
1438 // an error on the 'int' saying that you can't have two type specifiers in
1439 // the same declaration of X. Because of this, we look ahead past this
1440 // token to see if it's a type specifier. If so, we know the code is
1441 // otherwise invalid, so we can produce the expected semi error.
1442 if (!isKnownToBeTypeSpecifier(NextToken()))
1443 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001444 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001445
1446 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001447 // Missing ';' at end of struct is accepted as an extension in C mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00001448 if (!getLangOpts().CPlusPlus)
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001449 ExpectedSemi = false;
1450 break;
1451 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001452
Richard Smithcf6b0a22011-07-14 21:35:26 +00001453 // C++ [temp]p3 In a template-declaration which defines a class, no
1454 // declarator is permitted.
1455 if (TemplateInfo.Kind)
1456 ExpectedSemi = true;
1457
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001458 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001459 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1460 TagType == DeclSpec::TST_class ? "class"
1461 : TagType == DeclSpec::TST_struct? "struct" : "union");
1462 // Push this token back into the preprocessor and change our current token
1463 // to ';' so that the rest of the code recovers as though there were an
1464 // ';' after the definition.
1465 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001466 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001467 }
1468 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001469}
1470
Mike Stump1eb44332009-09-09 15:08:12 +00001471/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001472///
1473/// base-clause : [C++ class.derived]
1474/// ':' base-specifier-list
1475/// base-specifier-list:
1476/// base-specifier '...'[opt]
1477/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001478void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001479 assert(Tok.is(tok::colon) && "Not a base clause");
1480 ConsumeToken();
1481
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001482 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001483 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001484
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001485 while (true) {
1486 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001487 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001488 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001489 // Skip the rest of this base specifier, up until the comma or
1490 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001491 SkipUntil(tok::comma, tok::l_brace, true, true);
1492 } else {
1493 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001494 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001495 }
1496
1497 // If the next token is a comma, consume it and keep reading
1498 // base-specifiers.
1499 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001501 // Consume the comma.
1502 ConsumeToken();
1503 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001504
1505 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001506 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001507}
1508
1509/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1510/// one entry in the base class list of a class specifier, for example:
1511/// class foo : public bar, virtual private baz {
1512/// 'public bar' and 'virtual private baz' are each base-specifiers.
1513///
1514/// base-specifier: [C++ class.derived]
1515/// ::[opt] nested-name-specifier[opt] class-name
1516/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001517/// base-type-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001518/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001519/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001520Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001521 bool IsVirtual = false;
1522 SourceLocation StartLoc = Tok.getLocation();
1523
1524 // Parse the 'virtual' keyword.
1525 if (Tok.is(tok::kw_virtual)) {
1526 ConsumeToken();
1527 IsVirtual = true;
1528 }
1529
1530 // Parse an (optional) access specifier.
1531 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001532 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001533 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001535 // Parse the 'virtual' keyword (again!), in case it came after the
1536 // access specifier.
1537 if (Tok.is(tok::kw_virtual)) {
1538 SourceLocation VirtualLoc = ConsumeToken();
1539 if (IsVirtual) {
1540 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001541 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001542 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001543 }
1544
1545 IsVirtual = true;
1546 }
1547
Douglas Gregor42a552f2008-11-05 20:51:48 +00001548 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001549 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001550 SourceLocation BaseLoc;
1551 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001552 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001553 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001555 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1556 // actually part of the base-specifier-list grammar productions, but we
1557 // parse it here for convenience.
1558 SourceLocation EllipsisLoc;
1559 if (Tok.is(tok::ellipsis))
1560 EllipsisLoc = ConsumeToken();
1561
Mike Stump1eb44332009-09-09 15:08:12 +00001562 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001563 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001565 // Notify semantic analysis that we have parsed a complete
1566 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001567 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001568 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001569}
1570
1571/// getAccessSpecifierIfPresent - Determine whether the next token is
1572/// a C++ access-specifier.
1573///
1574/// access-specifier: [C++ class.derived]
1575/// 'private'
1576/// 'protected'
1577/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001578AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001579 switch (Tok.getKind()) {
1580 default: return AS_none;
1581 case tok::kw_private: return AS_private;
1582 case tok::kw_protected: return AS_protected;
1583 case tok::kw_public: return AS_public;
1584 }
1585}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001586
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001587/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001588/// delayed, e.g., default arguments, create a late-parsed method declaration
1589/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001590void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1591 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001592 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001593 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001594 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001595 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001596 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001597
Eli Friedmand33133c2009-07-22 21:45:50 +00001598 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1599 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1600 if (!LateMethod) {
1601 // Push this method onto the stack of late-parsed method
1602 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001603 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1604 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001605 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001606
1607 // Add all of the parameters prior to this one (they don't
1608 // have default arguments).
1609 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1610 for (unsigned I = 0; I < ParamIdx; ++I)
1611 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001612 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001613 }
1614
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001615 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001616 // not have a default argument).
1617 LateMethod->DefaultArgs.push_back(
1618 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1619 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1620 }
1621 }
1622}
1623
Richard Smith1c94c162012-01-09 22:31:44 +00001624/// isCXX0XVirtSpecifier - Determine whether the given token is a C++0x
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001625/// virt-specifier.
1626///
1627/// virt-specifier:
1628/// override
1629/// final
Richard Smith1c94c162012-01-09 22:31:44 +00001630VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001631 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001632 return VirtSpecifiers::VS_None;
1633
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001634 if (Tok.is(tok::identifier)) {
1635 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001636
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001637 // Initialize the contextual keywords.
1638 if (!Ident_final) {
1639 Ident_final = &PP.getIdentifierTable().get("final");
1640 Ident_override = &PP.getIdentifierTable().get("override");
1641 }
1642
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001643 if (II == Ident_override)
1644 return VirtSpecifiers::VS_Override;
1645
1646 if (II == Ident_final)
1647 return VirtSpecifiers::VS_Final;
1648 }
1649
1650 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001651}
1652
1653/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1654///
1655/// virt-specifier-seq:
1656/// virt-specifier
1657/// virt-specifier-seq virt-specifier
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001658void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001659 while (true) {
Anders Carlssoncc54d592011-01-22 16:56:46 +00001660 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001661 if (Specifier == VirtSpecifiers::VS_None)
1662 return;
1663
1664 // C++ [class.mem]p8:
1665 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001666 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001667 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001668 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1669 << PrevSpec
1670 << FixItHint::CreateRemoval(Tok.getLocation());
1671
David Blaikie4e4d0842012-03-11 07:00:24 +00001672 Diag(Tok.getLocation(), getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00001673 diag::warn_cxx98_compat_override_control_keyword :
1674 diag::ext_override_control_keyword)
1675 << VirtSpecifiers::getSpecifierName(Specifier);
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001676 ConsumeToken();
1677 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001678}
1679
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001680/// isCXX0XFinalKeyword - Determine whether the next token is a C++0x
1681/// contextual 'final' keyword.
1682bool Parser::isCXX0XFinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001683 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001684 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001685
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001686 if (!Tok.is(tok::identifier))
1687 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001688
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001689 // Initialize the contextual keywords.
1690 if (!Ident_final) {
1691 Ident_final = &PP.getIdentifierTable().get("final");
1692 Ident_override = &PP.getIdentifierTable().get("override");
1693 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001694
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001695 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001696}
1697
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001698/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1699///
1700/// member-declaration:
1701/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1702/// function-definition ';'[opt]
1703/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1704/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001705/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001706/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001707/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001708///
1709/// member-declarator-list:
1710/// member-declarator
1711/// member-declarator-list ',' member-declarator
1712///
1713/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001714/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001715/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001716/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001717/// identifier[opt] ':' constant-expression
1718///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001719/// virt-specifier-seq:
1720/// virt-specifier
1721/// virt-specifier-seq virt-specifier
1722///
1723/// virt-specifier:
1724/// override
1725/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001726///
Sebastian Redle2b68332009-04-12 17:16:29 +00001727/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001728/// '= 0'
1729///
1730/// constant-initializer:
1731/// '=' constant-expression
1732///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001733void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001734 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001735 const ParsedTemplateInfo &TemplateInfo,
1736 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001737 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001738 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001739 Diag(Tok, diag::err_at_defs_cxx);
1740 else
1741 Diag(Tok, diag::err_at_in_class);
1742
1743 ConsumeToken();
1744 SkipUntil(tok::r_brace);
1745 return;
1746 }
1747
John McCall60fa3cf2009-12-11 02:10:03 +00001748 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001749 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001750 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001751 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1752 if (TryAnnotateCXXScopeToken())
1753 MalformedTypeSpec = true;
1754
1755 bool isAccessDecl;
1756 if (Tok.isNot(tok::annot_cxxscope))
1757 isAccessDecl = false;
1758 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001759 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1760 else
1761 isAccessDecl = NextToken().is(tok::kw_operator);
1762
1763 if (isAccessDecl) {
1764 // Collect the scope specifier token we annotated earlier.
1765 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001766 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1767 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001768
1769 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001770 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001771 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001772 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1773 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001774 SkipUntil(tok::semi);
1775 return;
1776 }
1777
1778 // TODO: recover from mistakenly-qualified operator declarations.
1779 if (ExpectAndConsume(tok::semi,
1780 diag::err_expected_semi_after,
1781 "access declaration",
1782 tok::semi))
1783 return;
1784
Douglas Gregor23c94db2010-07-02 17:43:08 +00001785 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001786 false, SourceLocation(),
1787 SS, Name,
1788 /* AttrList */ 0,
1789 /* IsTypeName */ false,
1790 SourceLocation());
1791 return;
1792 }
1793 }
1794
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001795 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001796 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001797 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001798 SourceLocation DeclEnd;
1799 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001800 return;
1801 }
Mike Stump1eb44332009-09-09 15:08:12 +00001802
Chris Lattner682bf922009-03-29 16:50:03 +00001803 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001804 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001805 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001806 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001807 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001808 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001809 return;
1810 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001811
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001812 // Handle: member-declaration ::= '__extension__' member-declaration
1813 if (Tok.is(tok::kw___extension__)) {
1814 // __extension__ silences extension warnings in the subexpression.
1815 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1816 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001817 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1818 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001819 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001820
Chris Lattner4ed5d912010-02-02 01:23:29 +00001821 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1822 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001823 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001824
John McCall0b7e6782011-03-24 11:26:52 +00001825 ParsedAttributesWithRange attrs(AttrFactory);
Sean Huntbbd37c62009-11-21 08:43:09 +00001826 // Optional C++0x attribute-specifier
John McCall7f040a92010-12-24 02:08:15 +00001827 MaybeParseCXX0XAttributes(attrs);
1828 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001829
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001830 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00001831 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001833 // Eat 'using'.
1834 SourceLocation UsingLoc = ConsumeToken();
1835
1836 if (Tok.is(tok::kw_namespace)) {
1837 Diag(UsingLoc, diag::err_using_namespace_in_class);
1838 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001839 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001840 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001841 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00001842 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1843 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001844 }
1845 return;
1846 }
1847
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001848 // Hold late-parsed attributes so we can attach a Decl to them later.
1849 LateParsedAttrList CommonLateParsedAttrs;
1850
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001851 // decl-specifier-seq:
1852 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001853 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00001854 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00001855 if (MalformedTypeSpec)
1856 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001857 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
1858 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001859
John McCallf312b1e2010-08-26 23:41:50 +00001860 MultiTemplateParamsArg TemplateParams(Actions,
John McCalldd4a3b02009-09-16 22:47:08 +00001861 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1862 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1863
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001864 if (Tok.is(tok::semi)) {
1865 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001866 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00001867 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00001868 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001869 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001870 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001871
John McCall54abf7d2009-11-04 02:18:39 +00001872 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00001873 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001874
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001875 // Hold late-parsed attributes so we can attach a Decl to them later.
1876 LateParsedAttrList LateParsedAttrs;
1877
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001878 SourceLocation EqualLoc;
1879 bool HasInitializer = false;
1880 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001881 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001882 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1883 ColonProtectionRAIIObject X(*this);
1884
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001885 // Parse the first declarator.
1886 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00001887 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001888 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001889 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00001890 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001891 if (Tok.is(tok::semi))
1892 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001893 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001894 }
1895
Nico Weber48673472011-01-28 06:07:34 +00001896 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1897
John Thompson1b2fc0f2009-11-25 22:58:06 +00001898 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001899 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00001900
Francois Pichet6a247472011-05-11 02:14:46 +00001901 // MSVC permits pure specifier on inline functions declared at class scope.
1902 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00001903 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00001904 DeclaratorInfo.isFunctionDeclarator() &&
1905 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001906 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00001907 Init = ParseInitializer();
1908 if (Init.isInvalid())
1909 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001910 else
1911 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00001912 }
1913
Douglas Gregor45fa5602011-11-07 20:56:01 +00001914 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001915 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00001916 //
1917 // In C++11, a non-function declarator followed by an open brace is a
1918 // braced-init-list for an in-class member initialization, not an
1919 // erroneous function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00001920 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus0x) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00001921 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00001922 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00001923 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00001924 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00001925 } else if (Tok.is(tok::equal)) {
1926 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00001927 if (KW.is(tok::kw_default))
1928 DefinitionKind = FDK_Defaulted;
1929 else if (KW.is(tok::kw_delete))
1930 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00001931 }
1932 }
1933
Douglas Gregor45fa5602011-11-07 20:56:01 +00001934 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001935 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00001936 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001937 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00001938 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001939
1940 // Consume the optional ';'
1941 if (Tok.is(tok::semi))
1942 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001943 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001944 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001945
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001946 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00001947 Diag(DeclaratorInfo.getIdentifierLoc(),
1948 diag::err_function_declared_typedef);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001949 // This recovery skips the entire function body. It would be nice
1950 // to simply call ParseCXXInlineMethodDef() below, however Sema
1951 // assumes the declarator represents a function, not a typedef.
1952 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00001953 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001954
1955 // Consume the optional ';'
1956 if (Tok.is(tok::semi))
1957 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001958 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001959 }
1960
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001961 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001962 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00001963 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001964
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001965 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
1966 CommonLateParsedAttrs[i]->addDecl(FunDecl);
1967 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001968 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001969 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001970 }
1971 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00001972
1973 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00001974 if (Tok.is(tok::semi))
1975 ConsumeExtraSemi(AfterDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001976
Chris Lattner682bf922009-03-29 16:50:03 +00001977 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001978 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001979 }
1980
1981 // member-declarator-list:
1982 // member-declarator
1983 // member-declarator-list ',' member-declarator
1984
Chris Lattner5f9e2722011-07-23 10:55:15 +00001985 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00001986 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00001987 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001988
1989 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001990 // member-declarator:
1991 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001992 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001993 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001994 if (Tok.is(tok::colon)) {
1995 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001996 BitfieldSize = ParseConstantExpression();
1997 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001998 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001999 }
Mike Stump1eb44332009-09-09 15:08:12 +00002000
Chris Lattnere6563252010-06-13 05:34:18 +00002001 // If a simple-asm-expr is present, parse it.
2002 if (Tok.is(tok::kw_asm)) {
2003 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002004 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002005 if (AsmLabel.isInvalid())
2006 SkipUntil(tok::comma, true, true);
2007
2008 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2009 DeclaratorInfo.SetRangeEnd(Loc);
2010 }
2011
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002012 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002013 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002014
Richard Smith7a614d82011-06-11 17:19:42 +00002015 // FIXME: When g++ adds support for this, we'll need to check whether it
2016 // goes before or after the GNU attributes and __asm__.
2017 ParseOptionalCXX0XVirtSpecifierSeq(VS);
2018
Richard Smithca523302012-06-10 03:12:00 +00002019 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002020 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002021 if (BitfieldSize.get()) {
2022 Diag(Tok, diag::err_bitfield_member_init);
2023 SkipUntil(tok::comma, true, true);
2024 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002025 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002026 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2027 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2028 != DeclSpec::SCS_static &&
2029 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2030 != DeclSpec::SCS_typedef)
2031 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002032 }
2033 }
2034
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002035 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002036 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002037 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002038
John McCalld226f652010-08-21 09:40:31 +00002039 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002040 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00002041 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002042 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCallbbbcdd92009-09-11 21:02:39 +00002043 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00002044 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002045 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002046 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00002047 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00002048 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002049 VS, HasInClassInit);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002050 if (AccessAttrs)
2051 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2052 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002053 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002054
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002055 // Set the Decl for any late parsed attributes
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002056 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2057 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2058 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002059 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002060 LateParsedAttrs[i]->addDecl(ThisDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002061 }
2062 LateParsedAttrs.clear();
2063
Douglas Gregor147545d2011-10-10 14:49:18 +00002064 // Handle the initializer.
Richard Smithca523302012-06-10 03:12:00 +00002065 if (HasInClassInit != ICIS_NoInit) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002066 // The initializer was deferred; parse it and cache the tokens.
David Blaikie4e4d0842012-03-11 07:00:24 +00002067 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00002068 diag::warn_cxx98_compat_nonstatic_member_init :
2069 diag::ext_nonstatic_member_init);
2070
Richard Smith7a614d82011-06-11 17:19:42 +00002071 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002072 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2073 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002074 //
2075 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002076 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002077 Diag(Tok, diag::err_incomplete_array_member_init);
2078 SkipUntil(tok::comma, true, true);
David Blaikie3164c142012-02-14 09:00:46 +00002079 if (ThisDecl)
2080 // Avoid later warnings about a class member of incomplete type.
2081 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002082 } else
2083 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002084 } else if (HasInitializer) {
2085 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002086 if (!Init.isUsable())
Douglas Gregor552e2992012-02-21 02:22:07 +00002087 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002088 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2089
Douglas Gregor147545d2011-10-10 14:49:18 +00002090 if (Init.isInvalid())
2091 SkipUntil(tok::comma, true, true);
2092 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002093 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002094 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor147545d2011-10-10 14:49:18 +00002095 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2096 // No initializer.
2097 Actions.ActOnUninitializedDecl(ThisDecl,
2098 DS.getTypeSpecType() == DeclSpec::TST_auto);
Richard Smith7a614d82011-06-11 17:19:42 +00002099 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002100
2101 if (ThisDecl) {
2102 Actions.FinalizeDeclaration(ThisDecl);
2103 DeclsInGroup.push_back(ThisDecl);
2104 }
2105
Richard Smithe5310012012-04-29 07:31:09 +00002106 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor147545d2011-10-10 14:49:18 +00002107 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2108 != DeclSpec::SCS_typedef) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002109 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002110 }
2111
2112 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002113
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002114 // If we don't have a comma, it is either the end of the list (a ';')
2115 // or an error, bail out.
2116 if (Tok.isNot(tok::comma))
2117 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002118
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002119 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002120 SourceLocation CommaLoc = ConsumeToken();
2121
2122 if (Tok.isAtStartOfLine() &&
2123 !MightBeDeclarator(Declarator::MemberContext)) {
2124 // This comma was followed by a line-break and something which can't be
2125 // the start of a declarator. The comma was probably a typo for a
2126 // semicolon.
2127 Diag(CommaLoc, diag::err_expected_semi_declaration)
2128 << FixItHint::CreateReplacement(CommaLoc, ";");
2129 ExpectSemi = false;
2130 break;
2131 }
Mike Stump1eb44332009-09-09 15:08:12 +00002132
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002133 // Parse the next declarator.
2134 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002135 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002136 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002137 Init = true;
2138 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002139 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002140
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002141 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002142 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002143
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002144 if (Tok.isNot(tok::colon))
2145 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002146 }
2147
Richard Smith1c94c162012-01-09 22:31:44 +00002148 if (ExpectSemi &&
2149 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002150 // Skip to end of block or statement.
2151 SkipUntil(tok::r_brace, true, true);
2152 // If we stopped at a ';', eat it.
2153 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002154 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002155 }
2156
Douglas Gregor23c94db2010-07-02 17:43:08 +00002157 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00002158 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002159}
2160
Richard Smith7a614d82011-06-11 17:19:42 +00002161/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2162/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2163/// function definition. The location of the '=', if any, will be placed in
2164/// EqualLoc.
2165///
2166/// pure-specifier:
2167/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002168///
Richard Smith7a614d82011-06-11 17:19:42 +00002169/// brace-or-equal-initializer:
2170/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002171/// braced-init-list
2172///
Richard Smith7a614d82011-06-11 17:19:42 +00002173/// initializer-clause:
2174/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002175/// braced-init-list
2176///
Richard Smith7a614d82011-06-11 17:19:42 +00002177/// defaulted/deleted function-definition:
2178/// '=' 'default'
2179/// '=' 'delete'
2180///
2181/// Prior to C++0x, the assignment-expression in an initializer-clause must
2182/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002183ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002184 SourceLocation &EqualLoc) {
2185 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2186 && "Data member initializer not starting with '=' or '{'");
2187
Douglas Gregor552e2992012-02-21 02:22:07 +00002188 EnterExpressionEvaluationContext Context(Actions,
2189 Sema::PotentiallyEvaluated,
2190 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002191 if (Tok.is(tok::equal)) {
2192 EqualLoc = ConsumeToken();
2193 if (Tok.is(tok::kw_delete)) {
2194 // In principle, an initializer of '= delete p;' is legal, but it will
2195 // never type-check. It's better to diagnose it as an ill-formed expression
2196 // than as an ill-formed deleted non-function member.
2197 // An initializer of '= delete p, foo' will never be parsed, because
2198 // a top-level comma always ends the initializer expression.
2199 const Token &Next = NextToken();
2200 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2201 Next.is(tok::eof)) {
2202 if (IsFunction)
2203 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2204 << 1 /* delete */;
2205 else
2206 Diag(ConsumeToken(), diag::err_deleted_non_function);
2207 return ExprResult();
2208 }
2209 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002210 if (IsFunction)
2211 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2212 << 0 /* default */;
2213 else
2214 Diag(ConsumeToken(), diag::err_default_special_members);
2215 return ExprResult();
2216 }
2217
Sebastian Redl33deb352012-02-22 10:50:08 +00002218 }
2219 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002220}
2221
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002222/// ParseCXXMemberSpecification - Parse the class definition.
2223///
2224/// member-specification:
2225/// member-declaration member-specification[opt]
2226/// access-specifier ':' member-specification[opt]
2227///
2228void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002229 unsigned TagType, Decl *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00002230 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002231 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00002232 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002233
John McCallf312b1e2010-08-26 23:41:50 +00002234 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2235 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002236
Douglas Gregor26997fd2010-01-16 20:52:59 +00002237 // Determine whether this is a non-nested class. Note that local
2238 // classes are *not* considered to be nested classes.
2239 bool NonNestedClass = true;
2240 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002241 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002242 if (S->isClassScope()) {
2243 // We're inside a class scope, so this is a nested class.
2244 NonNestedClass = false;
2245 break;
2246 }
2247
2248 if ((S->getFlags() & Scope::FnScope)) {
2249 // If we're in a function or function template declared in the
2250 // body of a class, then this is a local class rather than a
2251 // nested class.
2252 const Scope *Parent = S->getParent();
2253 if (Parent->isTemplateParamScope())
2254 Parent = Parent->getParent();
2255 if (Parent->isClassScope())
2256 break;
2257 }
2258 }
2259 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002260
2261 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002262 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002263
Douglas Gregor6569d682009-05-27 23:11:45 +00002264 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00002265 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00002266
Douglas Gregorddc29e12009-02-06 22:42:48 +00002267 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002268 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002269
Anders Carlssonb184a182011-03-25 14:46:08 +00002270 SourceLocation FinalLoc;
2271
2272 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002273 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith8b11b5e2011-10-15 04:21:46 +00002274 assert(isCXX0XFinalKeyword() && "not a class definition");
2275 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002276
David Blaikie4e4d0842012-03-11 07:00:24 +00002277 Diag(FinalLoc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00002278 diag::warn_cxx98_compat_override_control_keyword :
2279 diag::ext_override_control_keyword) << "final";
Anders Carlssonb184a182011-03-25 14:46:08 +00002280 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002281
John McCallbd0dfa52009-12-19 21:48:58 +00002282 if (Tok.is(tok::colon)) {
2283 ParseBaseClause(TagDecl);
2284
2285 if (!Tok.is(tok::l_brace)) {
2286 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002287
2288 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002289 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002290 return;
2291 }
2292 }
2293
2294 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002295 BalancedDelimiterTracker T(*this, tok::l_brace);
2296 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002297
John McCall42a4f662010-05-28 08:11:17 +00002298 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002299 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002300 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002301
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002302 // C++ 11p3: Members of a class defined with the keyword class are private
2303 // by default. Members of a class defined with the keywords struct or union
2304 // are public by default.
2305 AccessSpecifier CurAS;
2306 if (TagType == DeclSpec::TST_class)
2307 CurAS = AS_private;
2308 else
2309 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002310 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002311
Douglas Gregor07976d22010-06-21 22:31:09 +00002312 if (TagDecl) {
2313 // While we still have something to read, read the member-declarations.
2314 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2315 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002316
David Blaikie4e4d0842012-03-11 07:00:24 +00002317 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002318 Tok.is(tok::kw___if_not_exists))) {
2319 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2320 continue;
2321 }
2322
Douglas Gregor07976d22010-06-21 22:31:09 +00002323 // Check for extraneous top-level semicolon.
2324 if (Tok.is(tok::semi)) {
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002325 ConsumeExtraSemi(InsideStruct,
2326 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Douglas Gregor07976d22010-06-21 22:31:09 +00002327 continue;
2328 }
2329
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002330 if (Tok.is(tok::annot_pragma_vis)) {
2331 HandlePragmaVisibility();
2332 continue;
2333 }
2334
2335 if (Tok.is(tok::annot_pragma_pack)) {
2336 HandlePragmaPack();
2337 continue;
2338 }
2339
Douglas Gregor07976d22010-06-21 22:31:09 +00002340 AccessSpecifier AS = getAccessSpecifierIfPresent();
2341 if (AS != AS_none) {
2342 // Current token is a C++ access specifier.
2343 CurAS = AS;
2344 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002345 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002346 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002347 AccessAttrs.clear();
2348 MaybeParseGNUAttributes(AccessAttrs);
2349
David Blaikie13f8daf2011-10-13 06:08:43 +00002350 SourceLocation EndLoc;
2351 if (Tok.is(tok::colon)) {
2352 EndLoc = Tok.getLocation();
2353 ConsumeToken();
2354 } else if (Tok.is(tok::semi)) {
2355 EndLoc = Tok.getLocation();
2356 ConsumeToken();
2357 Diag(EndLoc, diag::err_expected_colon)
2358 << FixItHint::CreateReplacement(EndLoc, ":");
2359 } else {
2360 EndLoc = ASLoc.getLocWithOffset(TokLength);
2361 Diag(EndLoc, diag::err_expected_colon)
2362 << FixItHint::CreateInsertion(EndLoc, ":");
2363 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002364
2365 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2366 AccessAttrs.getList())) {
2367 // found another attribute than only annotations
2368 AccessAttrs.clear();
2369 }
2370
Douglas Gregor07976d22010-06-21 22:31:09 +00002371 continue;
2372 }
2373
2374 // FIXME: Make sure we don't have a template here.
2375
2376 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002377 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002378 }
2379
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002380 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002381 } else {
2382 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002383 }
Mike Stump1eb44332009-09-09 15:08:12 +00002384
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002385 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002386 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002387 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002388
John McCall42a4f662010-05-28 08:11:17 +00002389 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002390 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002391 T.getOpenLocation(),
2392 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002393 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002394
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002395 // C++11 [class.mem]p2:
2396 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002397 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002398 // brace-or-equal-initializers for non-static data members (including such
2399 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002400 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002401 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002402 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002403 // declarations and the lexed inline method definitions, along with any
2404 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002405 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002406 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002407 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002408
2409 // We've finished with all pending member declarations.
2410 Actions.ActOnFinishCXXMemberDecls();
2411
Richard Smith7a614d82011-06-11 17:19:42 +00002412 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002413 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002414 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002415 }
2416
John McCall42a4f662010-05-28 08:11:17 +00002417 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002418 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2419 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002420
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002421 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002422 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002423 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002424}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002425
2426/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2427/// which explicitly initializes the members or base classes of a
2428/// class (C++ [class.base.init]). For example, the three initializers
2429/// after the ':' in the Derived constructor below:
2430///
2431/// @code
2432/// class Base { };
2433/// class Derived : Base {
2434/// int x;
2435/// float f;
2436/// public:
2437/// Derived(float f) : Base(), x(17), f(f) { }
2438/// };
2439/// @endcode
2440///
Mike Stump1eb44332009-09-09 15:08:12 +00002441/// [C++] ctor-initializer:
2442/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002443///
Mike Stump1eb44332009-09-09 15:08:12 +00002444/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002445/// mem-initializer ...[opt]
2446/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002447void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002448 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2449
John Wiegley28bbe4b2011-04-28 01:08:34 +00002450 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2451 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002452 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002453
Chris Lattner5f9e2722011-07-23 10:55:15 +00002454 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002455 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002456
Douglas Gregor7ad83902008-11-05 04:29:56 +00002457 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002458 if (Tok.is(tok::code_completion)) {
2459 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2460 MemInitializers.data(),
2461 MemInitializers.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002462 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002463 } else {
2464 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2465 if (!MemInit.isInvalid())
2466 MemInitializers.push_back(MemInit.get());
2467 else
2468 AnyErrors = true;
2469 }
2470
Douglas Gregor7ad83902008-11-05 04:29:56 +00002471 if (Tok.is(tok::comma))
2472 ConsumeToken();
2473 else if (Tok.is(tok::l_brace))
2474 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002475 // If the next token looks like a base or member initializer, assume that
2476 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002477 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2478 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2479 Diag(Loc, diag::err_ctor_init_missing_comma)
2480 << FixItHint::CreateInsertion(Loc, ", ");
2481 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002482 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002483 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002484 SkipUntil(tok::l_brace, true, true);
2485 break;
2486 }
2487 } while (true);
2488
Mike Stump1eb44332009-09-09 15:08:12 +00002489 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002490 MemInitializers.data(), MemInitializers.size(),
2491 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002492}
2493
2494/// ParseMemInitializer - Parse a C++ member initializer, which is
2495/// part of a constructor initializer that explicitly initializes one
2496/// member or base class (C++ [class.base.init]). See
2497/// ParseConstructorInitializer for an example.
2498///
2499/// [C++] mem-initializer:
2500/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002501/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002502///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002503/// [C++] mem-initializer-id:
2504/// '::'[opt] nested-name-specifier[opt] class-name
2505/// identifier
John McCalld226f652010-08-21 09:40:31 +00002506Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002507 // parse '::'[opt] nested-name-specifier[opt]
2508 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002509 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002510 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002511 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002512 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002513 if (TemplateId->Kind == TNK_Type_template ||
2514 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002515 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002516 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002517 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002518 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002519 }
David Blaikief2116622012-01-24 06:03:59 +00002520 // Uses of decltype will already have been converted to annot_decltype by
2521 // ParseOptionalCXXScopeSpecifier at this point.
2522 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2523 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002524 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002525 return true;
2526 }
Mike Stump1eb44332009-09-09 15:08:12 +00002527
David Blaikief2116622012-01-24 06:03:59 +00002528 IdentifierInfo *II = 0;
2529 DeclSpec DS(AttrFactory);
2530 SourceLocation IdLoc = Tok.getLocation();
2531 if (Tok.is(tok::annot_decltype)) {
2532 // Get the decltype expression, if there is one.
2533 ParseDecltypeSpecifier(DS);
2534 } else {
2535 if (Tok.is(tok::identifier))
2536 // Get the identifier. This may be a member name or a class name,
2537 // but we'll let the semantic analysis determine which it is.
2538 II = Tok.getIdentifierInfo();
2539 ConsumeToken();
2540 }
2541
Douglas Gregor7ad83902008-11-05 04:29:56 +00002542
2543 // Parse the '('.
David Blaikie4e4d0842012-03-11 07:00:24 +00002544 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002545 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2546
Sebastian Redl6df65482011-09-24 17:48:25 +00002547 ExprResult InitList = ParseBraceInitializer();
2548 if (InitList.isInvalid())
2549 return true;
2550
2551 SourceLocation EllipsisLoc;
2552 if (Tok.is(tok::ellipsis))
2553 EllipsisLoc = ConsumeToken();
2554
2555 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002556 TemplateTypeTy, DS, IdLoc,
2557 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002558 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002559 BalancedDelimiterTracker T(*this, tok::l_paren);
2560 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002561
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002562 // Parse the optional expression-list.
2563 ExprVector ArgExprs(Actions);
2564 CommaLocsTy CommaLocs;
2565 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2566 SkipUntil(tok::r_paren);
2567 return true;
2568 }
2569
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002570 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002571
2572 SourceLocation EllipsisLoc;
2573 if (Tok.is(tok::ellipsis))
2574 EllipsisLoc = ConsumeToken();
2575
2576 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002577 TemplateTypeTy, DS, IdLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002578 T.getOpenLocation(), ArgExprs.take(),
2579 ArgExprs.size(), T.getCloseLocation(),
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002580 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002581 }
2582
David Blaikie4e4d0842012-03-11 07:00:24 +00002583 Diag(Tok, getLangOpts().CPlusPlus0x ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002584 : diag::err_expected_lparen);
2585 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002586}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002587
Sebastian Redl7acafd02011-03-05 14:45:16 +00002588/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002589///
Douglas Gregora4745612008-12-01 18:00:20 +00002590/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002591/// dynamic-exception-specification
2592/// noexcept-specification
2593///
2594/// noexcept-specification:
2595/// 'noexcept'
2596/// 'noexcept' '(' constant-expression ')'
2597ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002598Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002599 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002600 SmallVectorImpl<ParsedType> &DynamicExceptions,
2601 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002602 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002603 ExceptionSpecificationType Result = EST_None;
2604
2605 // See if there's a dynamic specification.
2606 if (Tok.is(tok::kw_throw)) {
2607 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2608 DynamicExceptions,
2609 DynamicExceptionRanges);
2610 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2611 "Produced different number of exception types and ranges.");
2612 }
2613
2614 // If there's no noexcept specification, we're done.
2615 if (Tok.isNot(tok::kw_noexcept))
2616 return Result;
2617
Richard Smith841804b2011-10-17 23:06:20 +00002618 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2619
Sebastian Redl7acafd02011-03-05 14:45:16 +00002620 // If we already had a dynamic specification, parse the noexcept for,
2621 // recovery, but emit a diagnostic and don't store the results.
2622 SourceRange NoexceptRange;
2623 ExceptionSpecificationType NoexceptType = EST_None;
2624
2625 SourceLocation KeywordLoc = ConsumeToken();
2626 if (Tok.is(tok::l_paren)) {
2627 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002628 BalancedDelimiterTracker T(*this, tok::l_paren);
2629 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002630 NoexceptType = EST_ComputedNoexcept;
2631 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002632 // The argument must be contextually convertible to bool. We use
2633 // ActOnBooleanCondition for this purpose.
2634 if (!NoexceptExpr.isInvalid())
2635 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2636 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002637 T.consumeClose();
2638 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002639 } else {
2640 // There is no argument.
2641 NoexceptType = EST_BasicNoexcept;
2642 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2643 }
2644
2645 if (Result == EST_None) {
2646 SpecificationRange = NoexceptRange;
2647 Result = NoexceptType;
2648
2649 // If there's a dynamic specification after a noexcept specification,
2650 // parse that and ignore the results.
2651 if (Tok.is(tok::kw_throw)) {
2652 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2653 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2654 DynamicExceptionRanges);
2655 }
2656 } else {
2657 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2658 }
2659
2660 return Result;
2661}
2662
2663/// ParseDynamicExceptionSpecification - Parse a C++
2664/// dynamic-exception-specification (C++ [except.spec]).
2665///
2666/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002667/// 'throw' '(' type-id-list [opt] ')'
2668/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002669///
Douglas Gregora4745612008-12-01 18:00:20 +00002670/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002671/// type-id ... [opt]
2672/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002673///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002674ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2675 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002676 SmallVectorImpl<ParsedType> &Exceptions,
2677 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002678 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002679
Sebastian Redl7acafd02011-03-05 14:45:16 +00002680 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002681 BalancedDelimiterTracker T(*this, tok::l_paren);
2682 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002683 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2684 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002685 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002686 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002687
Douglas Gregora4745612008-12-01 18:00:20 +00002688 // Parse throw(...), a Microsoft extension that means "this function
2689 // can throw anything".
2690 if (Tok.is(tok::ellipsis)) {
2691 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002692 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002693 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002694 T.consumeClose();
2695 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002696 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002697 }
2698
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002699 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002700 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002701 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002702 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002703
Douglas Gregora04426c2010-12-20 23:57:46 +00002704 if (Tok.is(tok::ellipsis)) {
2705 // C++0x [temp.variadic]p5:
2706 // - In a dynamic-exception-specification (15.4); the pattern is a
2707 // type-id.
2708 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002709 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002710 if (!Res.isInvalid())
2711 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2712 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002713
Sebastian Redlef65f062009-05-29 18:02:33 +00002714 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002715 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002716 Ranges.push_back(Range);
2717 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002718
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002719 if (Tok.is(tok::comma))
2720 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002721 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002722 break;
2723 }
2724
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002725 T.consumeClose();
2726 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002727 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002728}
Douglas Gregor6569d682009-05-27 23:11:45 +00002729
Douglas Gregordab60ad2010-10-01 18:44:50 +00002730/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2731/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002732TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002733 assert(Tok.is(tok::arrow) && "expected arrow");
2734
2735 ConsumeToken();
2736
Richard Smith7796eb52012-03-12 08:56:40 +00002737 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002738}
2739
Douglas Gregor6569d682009-05-27 23:11:45 +00002740/// \brief We have just started parsing the definition of a new class,
2741/// so push that class onto our stack of classes that is currently
2742/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002743Sema::ParsingClassState
2744Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002745 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002746 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00002747 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
John McCalleee1d542011-02-14 07:13:47 +00002748 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002749}
2750
2751/// \brief Deallocate the given parsed class and all of its nested
2752/// classes.
2753void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002754 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2755 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002756 delete Class;
2757}
2758
2759/// \brief Pop the top class of the stack of classes that are
2760/// currently being parsed.
2761///
2762/// This routine should be called when we have finished parsing the
2763/// definition of a class, but have not yet popped the Scope
2764/// associated with the class's definition.
2765///
2766/// \returns true if the class we've popped is a top-level class,
2767/// false otherwise.
John McCalleee1d542011-02-14 07:13:47 +00002768void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002769 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002770
John McCalleee1d542011-02-14 07:13:47 +00002771 Actions.PopParsingClass(state);
2772
Douglas Gregor6569d682009-05-27 23:11:45 +00002773 ParsingClass *Victim = ClassStack.top();
2774 ClassStack.pop();
2775 if (Victim->TopLevelClass) {
2776 // Deallocate all of the nested classes of this class,
2777 // recursively: we don't need to keep any of this information.
2778 DeallocateParsedClasses(Victim);
2779 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002780 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002781 assert(!ClassStack.empty() && "Missing top-level class?");
2782
Douglas Gregord54eb442010-10-12 16:25:54 +00002783 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002784 // The victim is a nested class, but we will not need to perform
2785 // any processing after the definition of this class since it has
2786 // no members whose handling was delayed. Therefore, we can just
2787 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002788 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002789 return;
2790 }
2791
2792 // This nested class has some members that will need to be processed
2793 // after the top-level class is completely defined. Therefore, add
2794 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002795 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002796 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002797 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00002798}
Sean Huntbbd37c62009-11-21 08:43:09 +00002799
Richard Smithc56298d2012-04-10 03:25:07 +00002800/// \brief Try to parse an 'identifier' which appears within an attribute-token.
2801///
2802/// \return the parsed identifier on success, and 0 if the next token is not an
2803/// attribute-token.
2804///
2805/// C++11 [dcl.attr.grammar]p3:
2806/// If a keyword or an alternative token that satisfies the syntactic
2807/// requirements of an identifier is contained in an attribute-token,
2808/// it is considered an identifier.
2809IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
2810 switch (Tok.getKind()) {
2811 default:
2812 // Identifiers and keywords have identifier info attached.
2813 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
2814 Loc = ConsumeToken();
2815 return II;
2816 }
2817 return 0;
2818
2819 case tok::ampamp: // 'and'
2820 case tok::pipe: // 'bitor'
2821 case tok::pipepipe: // 'or'
2822 case tok::caret: // 'xor'
2823 case tok::tilde: // 'compl'
2824 case tok::amp: // 'bitand'
2825 case tok::ampequal: // 'and_eq'
2826 case tok::pipeequal: // 'or_eq'
2827 case tok::caretequal: // 'xor_eq'
2828 case tok::exclaim: // 'not'
2829 case tok::exclaimequal: // 'not_eq'
2830 // Alternative tokens do not have identifier info, but their spelling
2831 // starts with an alphabetical character.
2832 llvm::SmallString<8> SpellingBuf;
2833 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
2834 if (std::isalpha(Spelling[0])) {
2835 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00002836 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00002837 }
2838 return 0;
2839 }
2840}
2841
2842/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002843/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00002844///
Richard Smith6ee326a2012-04-10 01:32:12 +00002845/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00002846/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002847/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00002848///
Richard Smith6ee326a2012-04-10 01:32:12 +00002849/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00002850/// attribute[opt]
2851/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00002852/// attribute '...'
2853/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00002854///
Richard Smith6ee326a2012-04-10 01:32:12 +00002855/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002856/// attribute-token attribute-argument-clause[opt]
2857///
Richard Smith6ee326a2012-04-10 01:32:12 +00002858/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00002859/// identifier
2860/// attribute-scoped-token
2861///
Richard Smith6ee326a2012-04-10 01:32:12 +00002862/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00002863/// attribute-namespace '::' identifier
2864///
Richard Smith6ee326a2012-04-10 01:32:12 +00002865/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00002866/// identifier
2867///
Richard Smith6ee326a2012-04-10 01:32:12 +00002868/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00002869/// '(' balanced-token-seq ')'
2870///
Richard Smith6ee326a2012-04-10 01:32:12 +00002871/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00002872/// balanced-token
2873/// balanced-token-seq balanced-token
2874///
Richard Smith6ee326a2012-04-10 01:32:12 +00002875/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00002876/// '(' balanced-token-seq ')'
2877/// '[' balanced-token-seq ']'
2878/// '{' balanced-token-seq '}'
2879/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00002880void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002881 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002882 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00002883 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002884 ParseAlignmentSpecifier(attrs, endLoc);
2885 return;
2886 }
2887
Sean Huntbbd37c62009-11-21 08:43:09 +00002888 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00002889 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00002890
Richard Smith41be6732011-10-14 20:48:27 +00002891 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
2892
Sean Huntbbd37c62009-11-21 08:43:09 +00002893 ConsumeBracket();
2894 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002895
Richard Smithc56298d2012-04-10 03:25:07 +00002896 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002897 // attribute not present
2898 if (Tok.is(tok::comma)) {
2899 ConsumeToken();
2900 continue;
2901 }
2902
Richard Smithc56298d2012-04-10 03:25:07 +00002903 SourceLocation ScopeLoc, AttrLoc;
2904 IdentifierInfo *ScopeName = 0, *AttrName = 0;
2905
2906 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
2907 if (!AttrName)
2908 // Break out to the "expected ']'" diagnostic.
2909 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002910
Sean Huntbbd37c62009-11-21 08:43:09 +00002911 // scoped attribute
2912 if (Tok.is(tok::coloncolon)) {
2913 ConsumeToken();
2914
Richard Smithc56298d2012-04-10 03:25:07 +00002915 ScopeName = AttrName;
2916 ScopeLoc = AttrLoc;
2917
2918 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
2919 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002920 Diag(Tok.getLocation(), diag::err_expected_ident);
2921 SkipUntil(tok::r_square, tok::comma, true, true);
2922 continue;
2923 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002924 }
2925
2926 bool AttrParsed = false;
Sean Hunt93f95f22012-06-18 16:13:52 +00002927 switch (AttributeList::getKind(AttrName, ScopeName,
2928 AttributeList::AS_CXX11)) {
Richard Smithe0d3b4c2012-05-03 18:27:39 +00002929 // No arguments
Sean Hunt8e083e72012-06-19 23:57:03 +00002930 case AttributeList::AT_CarriesDependency:
Richard Smithe0d3b4c2012-05-03 18:27:39 +00002931 // FIXME: implement generic support of attributes with C++11 syntax
2932 // see Parse/ParseDecl.cpp: ParseGNUAttributes
Sean Hunt8e083e72012-06-19 23:57:03 +00002933 case AttributeList::AT_FallThrough:
2934 case AttributeList::AT_NoReturn: {
Richard Smithe0d3b4c2012-05-03 18:27:39 +00002935 if (Tok.is(tok::l_paren)) {
2936 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
2937 << AttrName->getName();
Sean Huntbbd37c62009-11-21 08:43:09 +00002938 break;
2939 }
2940
Richard Smithe0d3b4c2012-05-03 18:27:39 +00002941 attrs.addNew(AttrName,
2942 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
2943 AttrLoc),
2944 ScopeName, ScopeLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002945 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smithe0d3b4c2012-05-03 18:27:39 +00002946 AttrParsed = true;
2947 break;
2948 }
2949
2950 // Silence warnings
2951 default: break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002952 }
2953
2954 // Skip the entire parameter clause, if any
2955 if (!AttrParsed && Tok.is(tok::l_paren)) {
2956 ConsumeParen();
2957 // SkipUntil maintains the balancedness of tokens.
2958 SkipUntil(tok::r_paren, false);
2959 }
Richard Smith6ee326a2012-04-10 01:32:12 +00002960
Richard Smithc56298d2012-04-10 03:25:07 +00002961 if (Tok.is(tok::ellipsis)) {
2962 if (AttrParsed)
2963 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
2964 << AttrName->getName();
Richard Smith6ee326a2012-04-10 01:32:12 +00002965 ConsumeToken();
Richard Smithc56298d2012-04-10 03:25:07 +00002966 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002967 }
2968
2969 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2970 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002971 if (endLoc)
2972 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00002973 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2974 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002975}
Sean Huntbbd37c62009-11-21 08:43:09 +00002976
Sean Hunt2edf0a22012-06-23 05:07:58 +00002977/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002978///
2979/// attribute-specifier-seq:
2980/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00002981void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002982 SourceLocation *endLoc) {
2983 SourceLocation StartLoc = Tok.getLocation(), Loc;
2984 if (!endLoc)
2985 endLoc = &Loc;
2986
Douglas Gregor8828ee72011-10-07 20:35:25 +00002987 do {
Richard Smithc56298d2012-04-10 03:25:07 +00002988 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00002989 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002990
2991 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002992}
2993
Francois Pichet334d47e2010-10-11 12:59:39 +00002994/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
2995///
2996/// [MS] ms-attribute:
2997/// '[' token-seq ']'
2998///
2999/// [MS] ms-attribute-seq:
3000/// ms-attribute[opt]
3001/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003002void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3003 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003004 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3005
3006 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003007 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003008 ConsumeBracket();
3009 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003010 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003011 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3012 }
3013}
Francois Pichet563a6452011-05-25 10:19:49 +00003014
3015void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3016 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003017 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003018 if (ParseMicrosoftIfExistsCondition(Result))
3019 return;
3020
Douglas Gregor3896fc52011-10-24 22:31:10 +00003021 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3022 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003023 Diag(Tok, diag::err_expected_lbrace);
3024 return;
3025 }
Francois Pichet563a6452011-05-25 10:19:49 +00003026
Douglas Gregor3896fc52011-10-24 22:31:10 +00003027 switch (Result.Behavior) {
3028 case IEB_Parse:
3029 // Parse the declarations below.
3030 break;
3031
3032 case IEB_Dependent:
3033 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3034 << Result.IsIfExists;
3035 // Fall through to skip.
3036
3037 case IEB_Skip:
3038 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003039 return;
3040 }
3041
Douglas Gregor3896fc52011-10-24 22:31:10 +00003042 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003043 // __if_exists, __if_not_exists can nest.
3044 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3045 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3046 continue;
3047 }
3048
3049 // Check for extraneous top-level semicolon.
3050 if (Tok.is(tok::semi)) {
Richard Trieu4b0e6f12012-05-16 19:04:59 +00003051 ConsumeExtraSemi(InsideStruct,
3052 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Francois Pichet563a6452011-05-25 10:19:49 +00003053 continue;
3054 }
3055
3056 AccessSpecifier AS = getAccessSpecifierIfPresent();
3057 if (AS != AS_none) {
3058 // Current token is a C++ access specifier.
3059 CurAS = AS;
3060 SourceLocation ASLoc = Tok.getLocation();
3061 ConsumeToken();
3062 if (Tok.is(tok::colon))
3063 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3064 else
3065 Diag(Tok, diag::err_expected_colon);
3066 ConsumeToken();
3067 continue;
3068 }
3069
3070 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003071 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003072 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003073
3074 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003075}