blob: 27fed393d4f1d385ed62e8ed1e196fa8850de5b5 [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;
447
448 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000449 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000450 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000451 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000452 ConsumeToken();
453 IsTypeName = true;
454 }
455 else
456 IsTypeName = false;
457
458 // Parse nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000459 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000460
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000461 // Check nested-name specifier.
462 if (SS.isInvalid()) {
463 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000464 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000465 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000466
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000467 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000468 // destructor names and allow the action module to diagnose any semantic
469 // errors.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000470 SourceLocation TemplateKWLoc;
Douglas Gregor12c118a2009-11-04 16:30:06 +0000471 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000472 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000473 /*EnteringContext=*/false,
474 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000475 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000476 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000477 TemplateKWLoc,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000478 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000479 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000480 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000481 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000482
John McCall0b7e6782011-03-24 11:26:52 +0000483 ParsedAttributes attrs(AttrFactory);
Richard Smith162e1c12011-04-15 14:24:37 +0000484
485 // Maybe this is an alias-declaration.
486 bool IsAliasDecl = Tok.is(tok::equal);
487 TypeResult TypeAlias;
488 if (IsAliasDecl) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000489 // TODO: Attribute support. C++0x attributes may appear before the equals.
490 // Where can GNU attributes appear?
Richard Smith162e1c12011-04-15 14:24:37 +0000491 ConsumeToken();
492
David Blaikie4e4d0842012-03-11 07:00:24 +0000493 Diag(Tok.getLocation(), getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +0000494 diag::warn_cxx98_compat_alias_declaration :
495 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000496
Richard Smith3e4c6c42011-05-05 21:57:07 +0000497 // Type alias templates cannot be specialized.
498 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000499 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
500 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000501 SpecKind = 0;
502 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
503 SpecKind = 1;
504 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
505 SpecKind = 2;
506 if (SpecKind != -1) {
507 SourceRange Range;
508 if (SpecKind == 0)
509 Range = SourceRange(Name.TemplateId->LAngleLoc,
510 Name.TemplateId->RAngleLoc);
511 else
512 Range = TemplateInfo.getSourceRange();
513 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
514 << SpecKind << Range;
515 SkipUntil(tok::semi);
516 return 0;
517 }
518
Richard Smith162e1c12011-04-15 14:24:37 +0000519 // Name must be an identifier.
520 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
521 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
522 // No removal fixit: can't recover from this.
523 SkipUntil(tok::semi);
524 return 0;
525 } else if (IsTypeName)
526 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
527 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
528 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
529 else if (SS.isNotEmpty())
530 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
531 << FixItHint::CreateRemoval(SS.getRange());
532
Richard Smith3e4c6c42011-05-05 21:57:07 +0000533 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
534 Declarator::AliasTemplateContext :
John McCallcdda47f2011-10-01 09:56:14 +0000535 Declarator::AliasDeclContext, AS, OwnedType);
Richard Smith162e1c12011-04-15 14:24:37 +0000536 } else
537 // Parse (optional) attributes (most likely GNU strong-using extension).
538 MaybeParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000540 // Eat ';'.
541 DeclEnd = Tok.getLocation();
542 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith162e1c12011-04-15 14:24:37 +0000543 !attrs.empty() ? "attributes list" :
544 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000545 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000546
John McCall78b81052010-11-10 02:40:36 +0000547 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith3e4c6c42011-05-05 21:57:07 +0000548 // In C++0x, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000549 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000550 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000551 SourceRange R = TemplateInfo.getSourceRange();
552 Diag(UsingLoc, diag::err_templated_using_declaration)
553 << R << FixItHint::CreateRemoval(R);
554
555 // Unfortunately, we have to bail out instead of recovering by
556 // ignoring the parameters, just in case the nested name specifier
557 // depends on the parameters.
558 return 0;
559 }
560
Douglas Gregor480b53c2011-09-26 14:30:28 +0000561 // "typename" keyword is allowed for identifiers only,
562 // because it may be a type definition.
563 if (IsTypeName && Name.getKind() != UnqualifiedId::IK_Identifier) {
564 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
565 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
566 // Proceed parsing, but reset the IsTypeName flag.
567 IsTypeName = false;
568 }
569
Richard Smith3e4c6c42011-05-05 21:57:07 +0000570 if (IsAliasDecl) {
571 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
572 MultiTemplateParamsArg TemplateParamsArg(Actions,
573 TemplateParams ? TemplateParams->data() : 0,
574 TemplateParams ? TemplateParams->size() : 0);
575 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
576 UsingLoc, Name, TypeAlias);
577 }
Richard Smith162e1c12011-04-15 14:24:37 +0000578
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000579 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000580 Name, attrs.getList(),
581 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000582}
583
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000584/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000585///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000586/// [C++0x] static_assert-declaration:
587/// static_assert ( constant-expression , string-literal ) ;
588///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000589/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000590/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000591///
John McCalld226f652010-08-21 09:40:31 +0000592Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000593 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
594 "Not a static_assert declaration");
595
David Blaikie4e4d0842012-03-11 07:00:24 +0000596 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000597 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000598 if (Tok.is(tok::kw_static_assert))
599 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000600
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000601 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000603 BalancedDelimiterTracker T(*this, tok::l_paren);
604 if (T.consumeOpen()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000605 Diag(Tok, diag::err_expected_lparen);
John McCalld226f652010-08-21 09:40:31 +0000606 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000607 }
Mike Stump1eb44332009-09-09 15:08:12 +0000608
John McCall60d7b3a2010-08-24 06:29:42 +0000609 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000610 if (AssertExpr.isInvalid()) {
611 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000612 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Anders Carlssonad5f9602009-03-13 23:29:20 +0000615 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000616 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000617
Richard Smith0cc323c2012-03-05 23:20:05 +0000618 if (!isTokenStringLiteral()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000619 Diag(Tok, diag::err_expected_string_literal);
620 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000621 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000622 }
Mike Stump1eb44332009-09-09 15:08:12 +0000623
John McCall60d7b3a2010-08-24 06:29:42 +0000624 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smith99831e42012-03-06 03:21:47 +0000625 if (AssertMessage.isInvalid()) {
626 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000627 return 0;
Richard Smith99831e42012-03-06 03:21:47 +0000628 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000629
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000630 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Chris Lattner97144fc2009-04-02 04:16:50 +0000632 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000633 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000634
John McCall9ae2f072010-08-23 23:25:46 +0000635 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
636 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000637 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000638 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000639}
640
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000641/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
642///
643/// 'decltype' ( expression )
644///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000645SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
646 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
647 && "Not a decltype specifier");
648
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000649
David Blaikie42d6d0c2011-12-04 05:04:18 +0000650 ExprResult Result;
651 SourceLocation StartLoc = Tok.getLocation();
652 SourceLocation EndLoc;
653
654 if (Tok.is(tok::annot_decltype)) {
655 Result = getExprAnnotation(Tok);
656 EndLoc = Tok.getAnnotationEndLoc();
657 ConsumeToken();
658 if (Result.isInvalid()) {
659 DS.SetTypeSpecError();
660 return EndLoc;
661 }
662 } else {
Richard Smithc7b55432012-02-24 22:30:04 +0000663 if (Tok.getIdentifierInfo()->isStr("decltype"))
664 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000665
David Blaikie42d6d0c2011-12-04 05:04:18 +0000666 ConsumeToken();
667
668 BalancedDelimiterTracker T(*this, tok::l_paren);
669 if (T.expectAndConsume(diag::err_expected_lparen_after,
670 "decltype", tok::r_paren)) {
671 DS.SetTypeSpecError();
672 return T.getOpenLocation() == Tok.getLocation() ?
673 StartLoc : T.getOpenLocation();
674 }
675
676 // Parse the expression
677
678 // C++0x [dcl.type.simple]p4:
679 // The operand of the decltype specifier is an unevaluated operand.
Richard Smith76f3f692012-02-22 02:04:18 +0000680 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
681 0, /*IsDecltype=*/true);
David Blaikie42d6d0c2011-12-04 05:04:18 +0000682 Result = ParseExpression();
683 if (Result.isInvalid()) {
Richard Smithd8e4dac2012-02-27 05:24:00 +0000684 SkipUntil(tok::r_paren);
David Blaikie42d6d0c2011-12-04 05:04:18 +0000685 DS.SetTypeSpecError();
Richard Smithd8e4dac2012-02-27 05:24:00 +0000686 return StartLoc;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000687 }
688
689 // Match the ')'
690 T.consumeClose();
691 if (T.getCloseLocation().isInvalid()) {
692 DS.SetTypeSpecError();
693 // FIXME: this should return the location of the last token
694 // that was consumed (by "consumeClose()")
695 return T.getCloseLocation();
696 }
697
Richard Smith76f3f692012-02-22 02:04:18 +0000698 Result = Actions.ActOnDecltypeExpression(Result.take());
699 if (Result.isInvalid()) {
700 DS.SetTypeSpecError();
701 return T.getCloseLocation();
702 }
703
David Blaikie42d6d0c2011-12-04 05:04:18 +0000704 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000707 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000708 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000709 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000710 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
David Blaikie42d6d0c2011-12-04 05:04:18 +0000711 DiagID, Result.release())) {
John McCallfec54012009-08-03 20:12:06 +0000712 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000713 DS.SetTypeSpecError();
714 }
715 return EndLoc;
716}
717
718void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
719 SourceLocation StartLoc,
720 SourceLocation EndLoc) {
721 // make sure we have a token we can turn into an annotation token
722 if (PP.isBacktrackEnabled())
723 PP.RevertCachedTokens(1);
724 else
725 PP.EnterToken(Tok);
726
727 Tok.setKind(tok::annot_decltype);
728 setExprAnnotation(Tok, DS.getTypeSpecType() == TST_decltype ?
729 DS.getRepAsExpr() : ExprResult());
730 Tok.setAnnotationEndLoc(EndLoc);
731 Tok.setLocation(StartLoc);
732 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000733}
734
Sean Huntdb5d44b2011-05-19 05:37:45 +0000735void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
736 assert(Tok.is(tok::kw___underlying_type) &&
737 "Not an underlying type specifier");
738
739 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000740 BalancedDelimiterTracker T(*this, tok::l_paren);
741 if (T.expectAndConsume(diag::err_expected_lparen_after,
742 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000743 return;
744 }
745
746 TypeResult Result = ParseTypeName();
747 if (Result.isInvalid()) {
748 SkipUntil(tok::r_paren);
749 return;
750 }
751
752 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000753 T.consumeClose();
754 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000755 return;
756
757 const char *PrevSpec = 0;
758 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000759 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000760 DiagID, Result.release()))
761 Diag(StartLoc, DiagID) << PrevSpec;
762}
763
David Blaikie09048df2011-10-25 15:01:20 +0000764/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
765/// class name or decltype-specifier. Note that we only check that the result
766/// names a type; semantic analysis will need to verify that the type names a
767/// class. The result is either a type or null, depending on whether a type
768/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000769///
David Blaikie09048df2011-10-25 15:01:20 +0000770/// base-type-specifier: [C++ 10.1]
771/// class-or-decltype
772/// class-or-decltype: [C++ 10.1]
773/// nested-name-specifier[opt] class-name
774/// decltype-specifier
Douglas Gregor42a552f2008-11-05 20:51:48 +0000775/// class-name: [C++ 9.1]
776/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000777/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000778///
David Blaikie22216eb2011-10-25 17:10:12 +0000779Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
780 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000781 // Ignore attempts to use typename
782 if (Tok.is(tok::kw_typename)) {
783 Diag(Tok, diag::err_expected_class_name_not_template)
784 << FixItHint::CreateRemoval(Tok.getLocation());
785 ConsumeToken();
786 }
787
David Blaikie152aa4b2011-10-25 18:17:58 +0000788 // Parse optional nested-name-specifier
789 CXXScopeSpec SS;
790 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
791
792 BaseLoc = Tok.getLocation();
793
David Blaikie22216eb2011-10-25 17:10:12 +0000794 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000795 // tok == kw_decltype is just error recovery, it can only happen when SS
796 // isn't empty
797 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000798 if (SS.isNotEmpty())
799 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
800 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000801 // Fake up a Declarator to use with ActOnTypeName.
802 DeclSpec DS(AttrFactory);
803
David Blaikieb5777572011-12-08 04:53:15 +0000804 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000805
806 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
807 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
808 }
809
Douglas Gregor7f43d672009-02-25 23:52:28 +0000810 // Check whether we have a template-id that names a type.
811 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000812 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000813 if (TemplateId->Kind == TNK_Type_template ||
814 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000815 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000816
817 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000818 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000819 EndLocation = Tok.getAnnotationEndLoc();
820 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000821
822 if (Type)
823 return Type;
824 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000825 }
826
827 // Fall through to produce an error below.
828 }
829
Douglas Gregor42a552f2008-11-05 20:51:48 +0000830 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000831 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000832 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000833 }
834
Douglas Gregor84d0a192010-01-12 21:28:44 +0000835 IdentifierInfo *Id = Tok.getIdentifierInfo();
836 SourceLocation IdLoc = ConsumeToken();
837
838 if (Tok.is(tok::less)) {
839 // It looks the user intended to write a template-id here, but the
840 // template-name was wrong. Try to fix that.
841 TemplateNameKind TNK = TNK_Type_template;
842 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000843 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000844 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000845 Diag(IdLoc, diag::err_unknown_template_name)
846 << Id;
847 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000848
Douglas Gregor84d0a192010-01-12 21:28:44 +0000849 if (!Template)
850 return true;
851
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000852 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000853 UnqualifiedId TemplateName;
854 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000855
Douglas Gregor84d0a192010-01-12 21:28:44 +0000856 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000857 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
858 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000859 return true;
860 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000861 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000862
Douglas Gregor84d0a192010-01-12 21:28:44 +0000863 // If we didn't end up with a typename token, there's nothing more we
864 // can do.
865 if (Tok.isNot(tok::annot_typename))
866 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000867
Douglas Gregor84d0a192010-01-12 21:28:44 +0000868 // Retrieve the type from the annotation token, consume that token, and
869 // return.
870 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000871 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000872 ConsumeToken();
873 return Type;
874 }
875
Douglas Gregor42a552f2008-11-05 20:51:48 +0000876 // We have an identifier; check whether it is actually a type.
Douglas Gregor059101f2011-03-02 00:47:37 +0000877 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000878 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000879 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +0000880 /*NonTrivialTypeSourceInfo=*/true);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000881 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000882 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000883 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000884 }
885
886 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000887 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000888
889 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000890 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000891 DS.SetRangeStart(IdLoc);
892 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000893 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000894
895 const char *PrevSpec = 0;
896 unsigned DiagID;
897 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
898
899 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
900 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000901}
902
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000903/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
904/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
905/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +0000906/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000907///
908/// class-specifier: [C++ class]
909/// class-head '{' member-specification[opt] '}'
910/// class-head '{' member-specification[opt] '}' attributes[opt]
911/// class-head:
912/// class-key identifier[opt] base-clause[opt]
913/// class-key nested-name-specifier identifier base-clause[opt]
914/// class-key nested-name-specifier[opt] simple-template-id
915/// base-clause[opt]
916/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000917/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000918/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000919/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000920/// simple-template-id base-clause[opt]
921/// class-key:
922/// 'class'
923/// 'struct'
924/// 'union'
925///
926/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000927/// class-key ::[opt] nested-name-specifier[opt] identifier
928/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
929/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000930///
931/// Note that the C++ class-specifier and elaborated-type-specifier,
932/// together, subsume the C99 struct-or-union-specifier:
933///
934/// struct-or-union-specifier: [C99 6.7.2.1]
935/// struct-or-union identifier[opt] '{' struct-contents '}'
936/// struct-or-union identifier
937/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
938/// '}' attributes[opt]
939/// [GNU] struct-or-union attributes[opt] identifier
940/// struct-or-union:
941/// 'struct'
942/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000943void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
944 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000945 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000946 AccessSpecifier AS,
Richard Smith69730c12012-03-12 07:56:15 +0000947 bool EnteringContext, DeclSpecContext DSC) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000948 DeclSpec::TST TagType;
949 if (TagTokKind == tok::kw_struct)
950 TagType = DeclSpec::TST_struct;
951 else if (TagTokKind == tok::kw_class)
952 TagType = DeclSpec::TST_class;
953 else {
954 assert(TagTokKind == tok::kw_union && "Not a class specifier");
955 TagType = DeclSpec::TST_union;
956 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000957
Douglas Gregor374929f2009-09-18 15:37:17 +0000958 if (Tok.is(tok::code_completion)) {
959 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000960 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000961 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +0000962 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000963
Chandler Carruth926c4b42010-06-28 08:39:25 +0000964 // C++03 [temp.explicit] 14.7.2/8:
965 // The usual access checking rules do not apply to names used to specify
966 // explicit instantiations.
967 //
968 // As an extension we do not perform access checking on the names used to
969 // specify explicit specializations either. This is important to allow
970 // specializing traits classes for private types.
Richard Smith1af83c42012-03-23 03:33:32 +0000971 Sema::SuppressAccessChecksRAII SuppressAccess(Actions,
972 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
973 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
Chandler Carruth926c4b42010-06-28 08:39:25 +0000974
John McCall0b7e6782011-03-24 11:26:52 +0000975 ParsedAttributes attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000976 // If attributes exist after tag, parse them.
977 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +0000978 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000979
Steve Narofff59e17e2008-12-24 20:59:21 +0000980 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +0000981 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +0000982 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000983
Sean Huntbbd37c62009-11-21 08:43:09 +0000984 // If C++0x attributes exist here, parse them.
985 // FIXME: Are we consistent with the ordering of parsing of different
986 // styles of attributes?
John McCall7f040a92010-12-24 02:08:15 +0000987 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000988
John Wiegley20c0da72011-04-27 23:09:49 +0000989 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +0000990 !Tok.is(tok::identifier) &&
991 Tok.getIdentifierInfo() &&
992 (Tok.is(tok::kw___is_arithmetic) ||
993 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +0000994 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +0000995 Tok.is(tok::kw___is_floating_point) ||
996 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +0000997 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +0000998 Tok.is(tok::kw___is_integral) ||
999 Tok.is(tok::kw___is_member_function_pointer) ||
1000 Tok.is(tok::kw___is_member_pointer) ||
1001 Tok.is(tok::kw___is_pod) ||
1002 Tok.is(tok::kw___is_pointer) ||
1003 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001004 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001005 Tok.is(tok::kw___is_signed) ||
1006 Tok.is(tok::kw___is_unsigned) ||
1007 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001008 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001009 // name of struct templates, but some are keywords in GCC >= 4.3
1010 // and Clang. Therefore, when we see the token sequence "struct
1011 // X", make X into a normal identifier rather than a keyword, to
1012 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001013 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001014 Tok.setKind(tok::identifier);
1015 }
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001017 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001018 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001019 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001020 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1021 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001022
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001023 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001024 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001025 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001026 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1027 Diag(Tok, diag::err_expected_ident);
1028 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001029
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001030 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1031
Douglas Gregorcc636682009-02-17 23:15:12 +00001032 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001033 IdentifierInfo *Name = 0;
1034 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001035 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001036 if (Tok.is(tok::identifier)) {
1037 Name = Tok.getIdentifierInfo();
1038 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001039
David Blaikie4e4d0842012-03-11 07:00:24 +00001040 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001041 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001042 // Eat the template argument list and try to continue parsing this as
1043 // a class (or template thereof).
1044 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001045 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001046 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001047 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001048 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001049 // We couldn't parse the template argument list at all, so don't
1050 // try to give any location information for the list.
1051 LAngleLoc = RAngleLoc = SourceLocation();
1052 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001053
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001054 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001055 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001056 << (TagType == DeclSpec::TST_class? 0
1057 : TagType == DeclSpec::TST_struct? 1
1058 : 2)
1059 << Name
1060 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001061
1062 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001063 // we've removed its template argument list.
1064 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1065 if (TemplateParams && TemplateParams->size() > 1) {
1066 TemplateParams->pop_back();
1067 } else {
1068 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001069 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001070 = ParsedTemplateInfo::NonTemplate;
1071 }
1072 } else if (TemplateInfo.Kind
1073 == ParsedTemplateInfo::ExplicitInstantiation) {
1074 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001075 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001076 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001077 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001078 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001079 = SourceLocation();
1080 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1081 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001082 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001083 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001084 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001085 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001086 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001087
Douglas Gregor059101f2011-03-02 00:47:37 +00001088 if (TemplateId->Kind != TNK_Type_template &&
1089 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001090 // The template-name in the simple-template-id refers to
1091 // something other than a class template. Give an appropriate
1092 // error message and skip to the ';'.
1093 SourceRange Range(NameLoc);
1094 if (SS.isNotEmpty())
1095 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001096
Douglas Gregor39a8de12009-02-25 19:37:18 +00001097 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1098 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Douglas Gregor39a8de12009-02-25 19:37:18 +00001100 DS.SetTypeSpecError();
1101 SkipUntil(tok::semi, false, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001102 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001103 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001104 }
1105
Chandler Carruth926c4b42010-06-28 08:39:25 +00001106 // As soon as we're finished parsing the class's template-id, turn access
1107 // checking back on.
Richard Smith1af83c42012-03-23 03:33:32 +00001108 SuppressAccess.done();
Chandler Carruth926c4b42010-06-28 08:39:25 +00001109
Richard Smith7796eb52012-03-12 08:56:40 +00001110 // There are four options here.
1111 // - If we are in a trailing return type, this is always just a reference,
1112 // and we must not try to parse a definition. For instance,
1113 // [] () -> struct S { };
1114 // does not define a type.
1115 // - If we have 'struct foo {...', 'struct foo :...',
1116 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1117 // - If we have 'struct foo;', then this is either a forward declaration
1118 // or a friend declaration, which have to be treated differently.
1119 // - Otherwise we have something like 'struct foo xyz', a reference.
Richard Smith69730c12012-03-12 07:56:15 +00001120 // However, in type-specifier-seq's, things look like declarations but are
1121 // just references, e.g.
1122 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001123 // or
Richard Smith69730c12012-03-12 07:56:15 +00001124 // &T::operator struct s;
1125 // For these, DSC is DSC_type_specifier.
John McCallf312b1e2010-08-26 23:41:50 +00001126 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001127 if (DSC == DSC_trailing)
1128 TUK = Sema::TUK_Reference;
1129 else if (Tok.is(tok::l_brace) ||
1130 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
1131 (isCXX0XFinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001132 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001133 if (DS.isFriendSpecified()) {
1134 // C++ [class.friend]p2:
1135 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001136 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001137 << SourceRange(DS.getFriendSpecLoc());
1138
1139 // Skip everything up to the semicolon, so that this looks like a proper
1140 // friend class (or template thereof) declaration.
1141 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001142 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001143 } else {
1144 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001145 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001146 }
Richard Smith69730c12012-03-12 07:56:15 +00001147 } else if (Tok.is(tok::semi) && DSC != DSC_type_specifier)
John McCallf312b1e2010-08-26 23:41:50 +00001148 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001149 else
John McCallf312b1e2010-08-26 23:41:50 +00001150 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001151
John McCall207014e2010-07-30 06:26:29 +00001152 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001153 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001154 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1155 // We have a declaration or reference to an anonymous class.
1156 Diag(StartLoc, diag::err_anon_type_definition)
1157 << DeclSpec::getSpecifierName(TagType);
1158 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001159
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001160 SkipUntil(tok::comma, true);
1161 return;
1162 }
1163
Douglas Gregorddc29e12009-02-06 22:42:48 +00001164 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001165 DeclResult TagOrTempResult = true; // invalid
1166 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001167
Douglas Gregor402abb52009-05-28 23:31:59 +00001168 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001169 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001170 // Explicit specialization, class template partial specialization,
1171 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001172 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001173 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001174 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001175 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001176 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001177 // This is an explicit instantiation of a class template.
1178 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001179 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001180 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001181 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001182 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001183 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001184 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001185 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001186 TemplateId->TemplateNameLoc,
1187 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001188 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001189 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001190 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001191
1192 // Friend template-ids are treated as references unless
1193 // they have template headers, in which case they're ill-formed
1194 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1195 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001196 } else if (TUK == Sema::TUK_Reference ||
1197 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001198 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001199 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001200 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001201 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001202 TemplateId->Template,
1203 TemplateId->TemplateNameLoc,
1204 TemplateId->LAngleLoc,
1205 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001206 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001207 } else {
1208 // This is an explicit specialization or a class template
1209 // partial specialization.
1210 TemplateParameterLists FakedParamLists;
1211
1212 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1213 // This looks like an explicit instantiation, because we have
1214 // something like
1215 //
1216 // template class Foo<X>
1217 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001218 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001219 // meant to be an explicit specialization, but the user forgot
1220 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +00001221 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001222
Mike Stump1eb44332009-09-09 15:08:12 +00001223 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001224 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001225 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001226 diag::err_explicit_instantiation_with_definition)
1227 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +00001228 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001229
1230 // Create a fake template parameter list that contains only
1231 // "template<>", so that we treat this construct as a class
1232 // template specialization.
1233 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +00001234 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001235 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001236 LAngleLoc,
1237 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001238 LAngleLoc));
1239 TemplateParams = &FakedParamLists;
1240 }
1241
1242 // Build the class template specialization.
1243 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001244 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001245 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001246 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001247 TemplateId->TemplateNameLoc,
1248 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001249 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001250 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001251 attrs.getList(),
John McCallf312b1e2010-08-26 23:41:50 +00001252 MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +00001253 TemplateParams? &(*TemplateParams)[0] : 0,
1254 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001255 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001256 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001257 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001258 // Explicit instantiation of a member of a class template
1259 // specialization, e.g.,
1260 //
1261 // template struct Outer<int>::Inner;
1262 //
1263 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001264 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001265 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001266 TemplateInfo.TemplateLoc,
1267 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001268 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001269 } else if (TUK == Sema::TUK_Friend &&
1270 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
1271 TagOrTempResult =
1272 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1273 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001274 Name, NameLoc, attrs.getList(),
John McCall9a34edb2010-10-19 01:40:49 +00001275 MultiTemplateParamsArg(Actions,
1276 TemplateParams? &(*TemplateParams)[0] : 0,
1277 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001278 } else {
1279 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001280 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001281 // FIXME: Diagnose this particular error.
1282 }
1283
John McCallc4e70192009-09-11 04:59:25 +00001284 bool IsDependent = false;
1285
John McCalla25c4082010-10-19 18:40:57 +00001286 // Don't pass down template parameter lists if this is just a tag
1287 // reference. For example, we don't need the template parameters here:
1288 // template <class T> class A *makeA(T t);
1289 MultiTemplateParamsArg TParams;
1290 if (TUK != Sema::TUK_Reference && TemplateParams)
1291 TParams =
1292 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1293
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001294 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001295 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001296 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001297 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001298 TParams, Owned, IsDependent,
1299 SourceLocation(), false,
1300 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001301
1302 // If ActOnTag said the type was dependent, try again with the
1303 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001304 if (IsDependent) {
1305 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001306 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001307 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001308 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001309 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001310
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001311 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001312 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001313 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001314 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001315 isCXX0XFinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001316 if (getLangOpts().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +00001317 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001318 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001319 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001320 }
1321
John McCallb3d87482010-08-24 05:47:05 +00001322 const char *PrevSpec = 0;
1323 unsigned DiagID;
1324 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001325 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001326 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1327 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001328 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001329 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001330 Result = DS.SetTypeSpecType(TagType, StartLoc,
1331 NameLoc.isValid() ? NameLoc : StartLoc,
1332 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001333 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001334 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001335 return;
1336 }
Mike Stump1eb44332009-09-09 15:08:12 +00001337
John McCallb3d87482010-08-24 05:47:05 +00001338 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001339 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001340
Chris Lattner4ed5d912010-02-02 01:23:29 +00001341 // At this point, we've successfully parsed a class-specifier in 'definition'
1342 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1343 // going to look at what comes after it to improve error recovery. If an
1344 // impossible token occurs next, we assume that the programmer forgot a ; at
1345 // the end of the declaration and recover that way.
1346 //
1347 // This switch enumerates the valid "follow" set for definition.
John McCallf312b1e2010-08-26 23:41:50 +00001348 if (TUK == Sema::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001349 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001350 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001351 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001352 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +00001353 case tok::star: // struct foo {...} * P;
1354 case tok::amp: // struct foo {...} & R = ...
1355 case tok::identifier: // struct foo {...} V ;
1356 case tok::r_paren: //(struct foo {...} ) {4}
1357 case tok::annot_cxxscope: // struct foo {...} a:: b;
1358 case tok::annot_typename: // struct foo {...} a ::b;
1359 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +00001360 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +00001361 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001362 ExpectedSemi = false;
1363 break;
1364 // Type qualifiers
1365 case tok::kw_const: // struct foo {...} const x;
1366 case tok::kw_volatile: // struct foo {...} volatile x;
1367 case tok::kw_restrict: // struct foo {...} restrict x;
1368 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +00001369 // Storage-class specifiers
1370 case tok::kw_static: // struct foo {...} static x;
1371 case tok::kw_extern: // struct foo {...} extern x;
1372 case tok::kw_typedef: // struct foo {...} typedef x;
1373 case tok::kw_register: // struct foo {...} register x;
1374 case tok::kw_auto: // struct foo {...} auto x;
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001375 case tok::kw_mutable: // struct foo {...} mutable x;
1376 case tok::kw_constexpr: // struct foo {...} constexpr x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001377 // As shown above, type qualifiers and storage class specifiers absolutely
1378 // can occur after class specifiers according to the grammar. However,
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001379 // almost no one actually writes code like this. If we see one of these,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001380 // it is much more likely that someone missed a semi colon and the
1381 // type/storage class specifier we're seeing is part of the *next*
1382 // intended declaration, as in:
1383 //
1384 // struct foo { ... }
1385 // typedef int X;
1386 //
1387 // We'd really like to emit a missing semicolon error instead of emitting
1388 // an error on the 'int' saying that you can't have two type specifiers in
1389 // the same declaration of X. Because of this, we look ahead past this
1390 // token to see if it's a type specifier. If so, we know the code is
1391 // otherwise invalid, so we can produce the expected semi error.
1392 if (!isKnownToBeTypeSpecifier(NextToken()))
1393 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001394 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001395
1396 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001397 // Missing ';' at end of struct is accepted as an extension in C mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00001398 if (!getLangOpts().CPlusPlus)
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001399 ExpectedSemi = false;
1400 break;
1401 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001402
Richard Smithcf6b0a22011-07-14 21:35:26 +00001403 // C++ [temp]p3 In a template-declaration which defines a class, no
1404 // declarator is permitted.
1405 if (TemplateInfo.Kind)
1406 ExpectedSemi = true;
1407
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001408 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001409 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1410 TagType == DeclSpec::TST_class ? "class"
1411 : TagType == DeclSpec::TST_struct? "struct" : "union");
1412 // Push this token back into the preprocessor and change our current token
1413 // to ';' so that the rest of the code recovers as though there were an
1414 // ';' after the definition.
1415 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001416 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001417 }
1418 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001419}
1420
Mike Stump1eb44332009-09-09 15:08:12 +00001421/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001422///
1423/// base-clause : [C++ class.derived]
1424/// ':' base-specifier-list
1425/// base-specifier-list:
1426/// base-specifier '...'[opt]
1427/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001428void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001429 assert(Tok.is(tok::colon) && "Not a base clause");
1430 ConsumeToken();
1431
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001432 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001433 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001434
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001435 while (true) {
1436 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001437 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001438 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001439 // Skip the rest of this base specifier, up until the comma or
1440 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001441 SkipUntil(tok::comma, tok::l_brace, true, true);
1442 } else {
1443 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001444 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001445 }
1446
1447 // If the next token is a comma, consume it and keep reading
1448 // base-specifiers.
1449 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001451 // Consume the comma.
1452 ConsumeToken();
1453 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001454
1455 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001456 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001457}
1458
1459/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1460/// one entry in the base class list of a class specifier, for example:
1461/// class foo : public bar, virtual private baz {
1462/// 'public bar' and 'virtual private baz' are each base-specifiers.
1463///
1464/// base-specifier: [C++ class.derived]
1465/// ::[opt] nested-name-specifier[opt] class-name
1466/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001467/// base-type-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001468/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001469/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001470Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001471 bool IsVirtual = false;
1472 SourceLocation StartLoc = Tok.getLocation();
1473
1474 // Parse the 'virtual' keyword.
1475 if (Tok.is(tok::kw_virtual)) {
1476 ConsumeToken();
1477 IsVirtual = true;
1478 }
1479
1480 // Parse an (optional) access specifier.
1481 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001482 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001483 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001485 // Parse the 'virtual' keyword (again!), in case it came after the
1486 // access specifier.
1487 if (Tok.is(tok::kw_virtual)) {
1488 SourceLocation VirtualLoc = ConsumeToken();
1489 if (IsVirtual) {
1490 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001491 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001492 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001493 }
1494
1495 IsVirtual = true;
1496 }
1497
Douglas Gregor42a552f2008-11-05 20:51:48 +00001498 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001499 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001500 SourceLocation BaseLoc;
1501 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001502 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001503 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001505 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1506 // actually part of the base-specifier-list grammar productions, but we
1507 // parse it here for convenience.
1508 SourceLocation EllipsisLoc;
1509 if (Tok.is(tok::ellipsis))
1510 EllipsisLoc = ConsumeToken();
1511
Mike Stump1eb44332009-09-09 15:08:12 +00001512 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001513 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001515 // Notify semantic analysis that we have parsed a complete
1516 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001517 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001518 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001519}
1520
1521/// getAccessSpecifierIfPresent - Determine whether the next token is
1522/// a C++ access-specifier.
1523///
1524/// access-specifier: [C++ class.derived]
1525/// 'private'
1526/// 'protected'
1527/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001528AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001529 switch (Tok.getKind()) {
1530 default: return AS_none;
1531 case tok::kw_private: return AS_private;
1532 case tok::kw_protected: return AS_protected;
1533 case tok::kw_public: return AS_public;
1534 }
1535}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001536
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001537/// \brief If the given declarator has any parts for which parsing has to be
1538/// delayed, e.g., default arguments or an exception-specification, create a
1539/// late-parsed method declaration record to handle the parsing at the end of
1540/// the class definition.
1541void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1542 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001543 // We just declared a member function. If this member function
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001544 // has any default arguments or an exception-specification, we'll need to
1545 // parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001546 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001547 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001548 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001549
1550 // If there was a delayed exception-specification, hold onto its tokens.
1551 if (FTI.getExceptionSpecType() == EST_Delayed) {
1552 // Push this method onto the stack of late-parsed method
1553 // declarations.
1554 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1555 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
1556 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
1557
1558 // Stash the exception-specification tokens in the late-pased mthod.
1559 LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens;
1560 FTI.ExceptionSpecTokens = 0;
1561
1562 // Reserve space for the parameters.
1563 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1564 }
1565
Eli Friedmand33133c2009-07-22 21:45:50 +00001566 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1567 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1568 if (!LateMethod) {
1569 // Push this method onto the stack of late-parsed method
1570 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001571 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1572 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001573 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001574
1575 // Add all of the parameters prior to this one (they don't
1576 // have default arguments).
1577 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1578 for (unsigned I = 0; I < ParamIdx; ++I)
1579 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001580 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001581 }
1582
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001583 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001584 // not have a default argument).
1585 LateMethod->DefaultArgs.push_back(
1586 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1587 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1588 }
1589 }
1590}
1591
Richard Smith1c94c162012-01-09 22:31:44 +00001592/// isCXX0XVirtSpecifier - Determine whether the given token is a C++0x
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001593/// virt-specifier.
1594///
1595/// virt-specifier:
1596/// override
1597/// final
Richard Smith1c94c162012-01-09 22:31:44 +00001598VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001599 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001600 return VirtSpecifiers::VS_None;
1601
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001602 if (Tok.is(tok::identifier)) {
1603 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001604
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001605 // Initialize the contextual keywords.
1606 if (!Ident_final) {
1607 Ident_final = &PP.getIdentifierTable().get("final");
1608 Ident_override = &PP.getIdentifierTable().get("override");
1609 }
1610
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001611 if (II == Ident_override)
1612 return VirtSpecifiers::VS_Override;
1613
1614 if (II == Ident_final)
1615 return VirtSpecifiers::VS_Final;
1616 }
1617
1618 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001619}
1620
1621/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1622///
1623/// virt-specifier-seq:
1624/// virt-specifier
1625/// virt-specifier-seq virt-specifier
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001626void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001627 while (true) {
Anders Carlssoncc54d592011-01-22 16:56:46 +00001628 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001629 if (Specifier == VirtSpecifiers::VS_None)
1630 return;
1631
1632 // C++ [class.mem]p8:
1633 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001634 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001635 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001636 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1637 << PrevSpec
1638 << FixItHint::CreateRemoval(Tok.getLocation());
1639
David Blaikie4e4d0842012-03-11 07:00:24 +00001640 Diag(Tok.getLocation(), getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00001641 diag::warn_cxx98_compat_override_control_keyword :
1642 diag::ext_override_control_keyword)
1643 << VirtSpecifiers::getSpecifierName(Specifier);
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001644 ConsumeToken();
1645 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001646}
1647
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001648/// isCXX0XFinalKeyword - Determine whether the next token is a C++0x
1649/// contextual 'final' keyword.
1650bool Parser::isCXX0XFinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001651 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001652 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001653
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001654 if (!Tok.is(tok::identifier))
1655 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001656
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001657 // Initialize the contextual keywords.
1658 if (!Ident_final) {
1659 Ident_final = &PP.getIdentifierTable().get("final");
1660 Ident_override = &PP.getIdentifierTable().get("override");
1661 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001662
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001663 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001664}
1665
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001666/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1667///
1668/// member-declaration:
1669/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1670/// function-definition ';'[opt]
1671/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1672/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001673/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001674/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001675/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001676///
1677/// member-declarator-list:
1678/// member-declarator
1679/// member-declarator-list ',' member-declarator
1680///
1681/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001682/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001683/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001684/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001685/// identifier[opt] ':' constant-expression
1686///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001687/// virt-specifier-seq:
1688/// virt-specifier
1689/// virt-specifier-seq virt-specifier
1690///
1691/// virt-specifier:
1692/// override
1693/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001694///
Sebastian Redle2b68332009-04-12 17:16:29 +00001695/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001696/// '= 0'
1697///
1698/// constant-initializer:
1699/// '=' constant-expression
1700///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001701void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001702 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001703 const ParsedTemplateInfo &TemplateInfo,
1704 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001705 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001706 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001707 Diag(Tok, diag::err_at_defs_cxx);
1708 else
1709 Diag(Tok, diag::err_at_in_class);
1710
1711 ConsumeToken();
1712 SkipUntil(tok::r_brace);
1713 return;
1714 }
1715
John McCall60fa3cf2009-12-11 02:10:03 +00001716 // Access declarations.
1717 if (!TemplateInfo.Kind &&
1718 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001719 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001720 Tok.is(tok::annot_cxxscope)) {
1721 bool isAccessDecl = false;
1722 if (NextToken().is(tok::identifier))
1723 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1724 else
1725 isAccessDecl = NextToken().is(tok::kw_operator);
1726
1727 if (isAccessDecl) {
1728 // Collect the scope specifier token we annotated earlier.
1729 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001730 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1731 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001732
1733 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001734 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001735 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001736 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1737 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001738 SkipUntil(tok::semi);
1739 return;
1740 }
1741
1742 // TODO: recover from mistakenly-qualified operator declarations.
1743 if (ExpectAndConsume(tok::semi,
1744 diag::err_expected_semi_after,
1745 "access declaration",
1746 tok::semi))
1747 return;
1748
Douglas Gregor23c94db2010-07-02 17:43:08 +00001749 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001750 false, SourceLocation(),
1751 SS, Name,
1752 /* AttrList */ 0,
1753 /* IsTypeName */ false,
1754 SourceLocation());
1755 return;
1756 }
1757 }
1758
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001759 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001760 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001761 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001762 SourceLocation DeclEnd;
1763 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001764 return;
1765 }
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Chris Lattner682bf922009-03-29 16:50:03 +00001767 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001768 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001769 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001770 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001771 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001772 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001773 return;
1774 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001775
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001776 // Handle: member-declaration ::= '__extension__' member-declaration
1777 if (Tok.is(tok::kw___extension__)) {
1778 // __extension__ silences extension warnings in the subexpression.
1779 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1780 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001781 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1782 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001783 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001784
Chris Lattner4ed5d912010-02-02 01:23:29 +00001785 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1786 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001787 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001788
John McCall0b7e6782011-03-24 11:26:52 +00001789 ParsedAttributesWithRange attrs(AttrFactory);
Sean Huntbbd37c62009-11-21 08:43:09 +00001790 // Optional C++0x attribute-specifier
John McCall7f040a92010-12-24 02:08:15 +00001791 MaybeParseCXX0XAttributes(attrs);
1792 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001793
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001794 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00001795 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001797 // Eat 'using'.
1798 SourceLocation UsingLoc = ConsumeToken();
1799
1800 if (Tok.is(tok::kw_namespace)) {
1801 Diag(UsingLoc, diag::err_using_namespace_in_class);
1802 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001803 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001804 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001805 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00001806 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1807 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001808 }
1809 return;
1810 }
1811
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001812 // Hold late-parsed attributes so we can attach a Decl to them later.
1813 LateParsedAttrList CommonLateParsedAttrs;
1814
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001815 // decl-specifier-seq:
1816 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001817 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00001818 DS.takeAttributesFrom(attrs);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001819 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
1820 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001821
John McCallf312b1e2010-08-26 23:41:50 +00001822 MultiTemplateParamsArg TemplateParams(Actions,
John McCalldd4a3b02009-09-16 22:47:08 +00001823 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1824 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1825
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001826 if (Tok.is(tok::semi)) {
1827 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001828 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00001829 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00001830 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001831 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001832 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001833
John McCall54abf7d2009-11-04 02:18:39 +00001834 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00001835 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001836
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001837 // Hold late-parsed attributes so we can attach a Decl to them later.
1838 LateParsedAttrList LateParsedAttrs;
1839
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001840 SourceLocation EqualLoc;
1841 bool HasInitializer = false;
1842 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001843 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001844 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1845 ColonProtectionRAIIObject X(*this);
1846
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001847 // Parse the first declarator.
1848 ParseDeclarator(DeclaratorInfo);
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001849 // Error parsin g the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001850 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001851 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00001852 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001853 if (Tok.is(tok::semi))
1854 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001855 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001856 }
1857
Nico Weber48673472011-01-28 06:07:34 +00001858 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1859
John Thompson1b2fc0f2009-11-25 22:58:06 +00001860 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001861 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00001862
Francois Pichet6a247472011-05-11 02:14:46 +00001863 // MSVC permits pure specifier on inline functions declared at class scope.
1864 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00001865 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00001866 DeclaratorInfo.isFunctionDeclarator() &&
1867 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001868 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00001869 Init = ParseInitializer();
1870 if (Init.isInvalid())
1871 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001872 else
1873 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00001874 }
1875
Douglas Gregor45fa5602011-11-07 20:56:01 +00001876 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001877 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00001878 //
1879 // In C++11, a non-function declarator followed by an open brace is a
1880 // braced-init-list for an in-class member initialization, not an
1881 // erroneous function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00001882 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus0x) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00001883 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00001884 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00001885 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00001886 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00001887 } else if (Tok.is(tok::equal)) {
1888 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00001889 if (KW.is(tok::kw_default))
1890 DefinitionKind = FDK_Defaulted;
1891 else if (KW.is(tok::kw_delete))
1892 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00001893 }
1894 }
1895
Douglas Gregor45fa5602011-11-07 20:56:01 +00001896 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001897 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00001898 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001899 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00001900 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001901
1902 // Consume the optional ';'
1903 if (Tok.is(tok::semi))
1904 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001905 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001906 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001907
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001908 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00001909 Diag(DeclaratorInfo.getIdentifierLoc(),
1910 diag::err_function_declared_typedef);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001911 // This recovery skips the entire function body. It would be nice
1912 // to simply call ParseCXXInlineMethodDef() below, however Sema
1913 // assumes the declarator represents a function, not a typedef.
1914 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00001915 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001916
1917 // Consume the optional ';'
1918 if (Tok.is(tok::semi))
1919 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001920 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001921 }
1922
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001923 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001924 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00001925 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001926
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001927 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
1928 CommonLateParsedAttrs[i]->addDecl(FunDecl);
1929 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001930 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001931 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001932 }
1933 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00001934
1935 // Consume the ';' - it's optional unless we have a delete or default
1936 if (Tok.is(tok::semi)) {
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001937 ConsumeToken();
Sean Hunte4246a62011-05-12 06:15:49 +00001938 }
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001939
Chris Lattner682bf922009-03-29 16:50:03 +00001940 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001941 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001942 }
1943
1944 // member-declarator-list:
1945 // member-declarator
1946 // member-declarator-list ',' member-declarator
1947
Chris Lattner5f9e2722011-07-23 10:55:15 +00001948 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00001949 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00001950 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001951
1952 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001953 // member-declarator:
1954 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001955 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001956 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001957 if (Tok.is(tok::colon)) {
1958 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001959 BitfieldSize = ParseConstantExpression();
1960 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001961 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001962 }
Mike Stump1eb44332009-09-09 15:08:12 +00001963
Chris Lattnere6563252010-06-13 05:34:18 +00001964 // If a simple-asm-expr is present, parse it.
1965 if (Tok.is(tok::kw_asm)) {
1966 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001967 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00001968 if (AsmLabel.isInvalid())
1969 SkipUntil(tok::comma, true, true);
1970
1971 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1972 DeclaratorInfo.SetRangeEnd(Loc);
1973 }
1974
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001975 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001976 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001977
Richard Smith7a614d82011-06-11 17:19:42 +00001978 // FIXME: When g++ adds support for this, we'll need to check whether it
1979 // goes before or after the GNU attributes and __asm__.
1980 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1981
1982 bool HasDeferredInitializer = false;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001983 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00001984 if (BitfieldSize.get()) {
1985 Diag(Tok, diag::err_bitfield_member_init);
1986 SkipUntil(tok::comma, true, true);
1987 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00001988 HasInitializer = true;
Douglas Gregor555f57e2011-06-25 00:56:27 +00001989 HasDeferredInitializer = !DeclaratorInfo.isDeclarationOfFunction() &&
Richard Smith7a614d82011-06-11 17:19:42 +00001990 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smithc2cdd532011-06-12 11:43:46 +00001991 != DeclSpec::SCS_static &&
1992 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
1993 != DeclSpec::SCS_typedef;
Richard Smith7a614d82011-06-11 17:19:42 +00001994 }
1995 }
1996
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001997 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001998 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001999 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002000
John McCalld226f652010-08-21 09:40:31 +00002001 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002002 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00002003 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002004 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCallbbbcdd92009-09-11 21:02:39 +00002005 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00002006 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002007 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002008 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00002009 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00002010 BitfieldSize.release(),
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00002011 VS, HasDeferredInitializer);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002012 if (AccessAttrs)
2013 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2014 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002015 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002016
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002017 // Set the Decl for any late parsed attributes
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002018 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2019 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2020 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002021 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002022 LateParsedAttrs[i]->addDecl(ThisDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002023 }
2024 LateParsedAttrs.clear();
2025
Douglas Gregor147545d2011-10-10 14:49:18 +00002026 // Handle the initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002027 if (HasDeferredInitializer) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002028 // The initializer was deferred; parse it and cache the tokens.
David Blaikie4e4d0842012-03-11 07:00:24 +00002029 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00002030 diag::warn_cxx98_compat_nonstatic_member_init :
2031 diag::ext_nonstatic_member_init);
2032
Richard Smith7a614d82011-06-11 17:19:42 +00002033 if (DeclaratorInfo.isArrayOfUnknownBound()) {
2034 // C++0x [dcl.array]p3: An array bound may also be omitted when the
2035 // declarator is followed by an initializer.
2036 //
2037 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002038 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002039 Diag(Tok, diag::err_incomplete_array_member_init);
2040 SkipUntil(tok::comma, true, true);
David Blaikie3164c142012-02-14 09:00:46 +00002041 if (ThisDecl)
2042 // Avoid later warnings about a class member of incomplete type.
2043 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002044 } else
2045 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002046 } else if (HasInitializer) {
2047 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002048 if (!Init.isUsable())
Douglas Gregor552e2992012-02-21 02:22:07 +00002049 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002050 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2051
Douglas Gregor147545d2011-10-10 14:49:18 +00002052 if (Init.isInvalid())
2053 SkipUntil(tok::comma, true, true);
2054 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002055 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002056 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor147545d2011-10-10 14:49:18 +00002057 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2058 // No initializer.
2059 Actions.ActOnUninitializedDecl(ThisDecl,
2060 DS.getTypeSpecType() == DeclSpec::TST_auto);
Richard Smith7a614d82011-06-11 17:19:42 +00002061 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002062
2063 if (ThisDecl) {
2064 Actions.FinalizeDeclaration(ThisDecl);
2065 DeclsInGroup.push_back(ThisDecl);
2066 }
2067
Richard Smithe5310012012-04-29 07:31:09 +00002068 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor147545d2011-10-10 14:49:18 +00002069 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2070 != DeclSpec::SCS_typedef) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002071 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002072 }
2073
2074 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002075
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002076 // If we don't have a comma, it is either the end of the list (a ';')
2077 // or an error, bail out.
2078 if (Tok.isNot(tok::comma))
2079 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002080
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002081 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002082 SourceLocation CommaLoc = ConsumeToken();
2083
2084 if (Tok.isAtStartOfLine() &&
2085 !MightBeDeclarator(Declarator::MemberContext)) {
2086 // This comma was followed by a line-break and something which can't be
2087 // the start of a declarator. The comma was probably a typo for a
2088 // semicolon.
2089 Diag(CommaLoc, diag::err_expected_semi_declaration)
2090 << FixItHint::CreateReplacement(CommaLoc, ";");
2091 ExpectSemi = false;
2092 break;
2093 }
Mike Stump1eb44332009-09-09 15:08:12 +00002094
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002095 // Parse the next declarator.
2096 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002097 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002098 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002099 Init = true;
2100 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002101 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002102
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002103 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002104 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002105
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002106 if (Tok.isNot(tok::colon))
2107 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002108 }
2109
Richard Smith1c94c162012-01-09 22:31:44 +00002110 if (ExpectSemi &&
2111 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002112 // Skip to end of block or statement.
2113 SkipUntil(tok::r_brace, true, true);
2114 // If we stopped at a ';', eat it.
2115 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002116 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002117 }
2118
Douglas Gregor23c94db2010-07-02 17:43:08 +00002119 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00002120 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002121}
2122
Richard Smith7a614d82011-06-11 17:19:42 +00002123/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2124/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2125/// function definition. The location of the '=', if any, will be placed in
2126/// EqualLoc.
2127///
2128/// pure-specifier:
2129/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002130///
Richard Smith7a614d82011-06-11 17:19:42 +00002131/// brace-or-equal-initializer:
2132/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002133/// braced-init-list
2134///
Richard Smith7a614d82011-06-11 17:19:42 +00002135/// initializer-clause:
2136/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002137/// braced-init-list
2138///
Richard Smith7a614d82011-06-11 17:19:42 +00002139/// defaulted/deleted function-definition:
2140/// '=' 'default'
2141/// '=' 'delete'
2142///
2143/// Prior to C++0x, the assignment-expression in an initializer-clause must
2144/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002145ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002146 SourceLocation &EqualLoc) {
2147 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2148 && "Data member initializer not starting with '=' or '{'");
2149
Douglas Gregor552e2992012-02-21 02:22:07 +00002150 EnterExpressionEvaluationContext Context(Actions,
2151 Sema::PotentiallyEvaluated,
2152 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002153 if (Tok.is(tok::equal)) {
2154 EqualLoc = ConsumeToken();
2155 if (Tok.is(tok::kw_delete)) {
2156 // In principle, an initializer of '= delete p;' is legal, but it will
2157 // never type-check. It's better to diagnose it as an ill-formed expression
2158 // than as an ill-formed deleted non-function member.
2159 // An initializer of '= delete p, foo' will never be parsed, because
2160 // a top-level comma always ends the initializer expression.
2161 const Token &Next = NextToken();
2162 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2163 Next.is(tok::eof)) {
2164 if (IsFunction)
2165 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2166 << 1 /* delete */;
2167 else
2168 Diag(ConsumeToken(), diag::err_deleted_non_function);
2169 return ExprResult();
2170 }
2171 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002172 if (IsFunction)
2173 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2174 << 0 /* default */;
2175 else
2176 Diag(ConsumeToken(), diag::err_default_special_members);
2177 return ExprResult();
2178 }
2179
Sebastian Redl33deb352012-02-22 10:50:08 +00002180 }
2181 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002182}
2183
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002184/// ParseCXXMemberSpecification - Parse the class definition.
2185///
2186/// member-specification:
2187/// member-declaration member-specification[opt]
2188/// access-specifier ':' member-specification[opt]
2189///
2190void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002191 unsigned TagType, Decl *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00002192 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002193 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00002194 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002195
John McCallf312b1e2010-08-26 23:41:50 +00002196 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2197 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002198
Douglas Gregor26997fd2010-01-16 20:52:59 +00002199 // Determine whether this is a non-nested class. Note that local
2200 // classes are *not* considered to be nested classes.
2201 bool NonNestedClass = true;
2202 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002203 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002204 if (S->isClassScope()) {
2205 // We're inside a class scope, so this is a nested class.
2206 NonNestedClass = false;
2207 break;
2208 }
2209
2210 if ((S->getFlags() & Scope::FnScope)) {
2211 // If we're in a function or function template declared in the
2212 // body of a class, then this is a local class rather than a
2213 // nested class.
2214 const Scope *Parent = S->getParent();
2215 if (Parent->isTemplateParamScope())
2216 Parent = Parent->getParent();
2217 if (Parent->isClassScope())
2218 break;
2219 }
2220 }
2221 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002222
2223 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002224 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002225
Douglas Gregor6569d682009-05-27 23:11:45 +00002226 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00002227 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00002228
Douglas Gregorddc29e12009-02-06 22:42:48 +00002229 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002230 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002231
Anders Carlssonb184a182011-03-25 14:46:08 +00002232 SourceLocation FinalLoc;
2233
2234 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002235 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith8b11b5e2011-10-15 04:21:46 +00002236 assert(isCXX0XFinalKeyword() && "not a class definition");
2237 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002238
David Blaikie4e4d0842012-03-11 07:00:24 +00002239 Diag(FinalLoc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00002240 diag::warn_cxx98_compat_override_control_keyword :
2241 diag::ext_override_control_keyword) << "final";
Anders Carlssonb184a182011-03-25 14:46:08 +00002242 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002243
John McCallbd0dfa52009-12-19 21:48:58 +00002244 if (Tok.is(tok::colon)) {
2245 ParseBaseClause(TagDecl);
2246
2247 if (!Tok.is(tok::l_brace)) {
2248 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002249
2250 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002251 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002252 return;
2253 }
2254 }
2255
2256 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002257 BalancedDelimiterTracker T(*this, tok::l_brace);
2258 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002259
John McCall42a4f662010-05-28 08:11:17 +00002260 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002261 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002262 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002263
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002264 // C++ 11p3: Members of a class defined with the keyword class are private
2265 // by default. Members of a class defined with the keywords struct or union
2266 // are public by default.
2267 AccessSpecifier CurAS;
2268 if (TagType == DeclSpec::TST_class)
2269 CurAS = AS_private;
2270 else
2271 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002272 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002273
Douglas Gregor07976d22010-06-21 22:31:09 +00002274 if (TagDecl) {
2275 // While we still have something to read, read the member-declarations.
2276 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2277 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002278
David Blaikie4e4d0842012-03-11 07:00:24 +00002279 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002280 Tok.is(tok::kw___if_not_exists))) {
2281 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2282 continue;
2283 }
2284
Douglas Gregor07976d22010-06-21 22:31:09 +00002285 // Check for extraneous top-level semicolon.
2286 if (Tok.is(tok::semi)) {
2287 Diag(Tok, diag::ext_extra_struct_semi)
2288 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
2289 << FixItHint::CreateRemoval(Tok.getLocation());
2290 ConsumeToken();
2291 continue;
2292 }
2293
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002294 if (Tok.is(tok::annot_pragma_vis)) {
2295 HandlePragmaVisibility();
2296 continue;
2297 }
2298
2299 if (Tok.is(tok::annot_pragma_pack)) {
2300 HandlePragmaPack();
2301 continue;
2302 }
2303
Douglas Gregor07976d22010-06-21 22:31:09 +00002304 AccessSpecifier AS = getAccessSpecifierIfPresent();
2305 if (AS != AS_none) {
2306 // Current token is a C++ access specifier.
2307 CurAS = AS;
2308 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002309 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002310 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002311 AccessAttrs.clear();
2312 MaybeParseGNUAttributes(AccessAttrs);
2313
David Blaikie13f8daf2011-10-13 06:08:43 +00002314 SourceLocation EndLoc;
2315 if (Tok.is(tok::colon)) {
2316 EndLoc = Tok.getLocation();
2317 ConsumeToken();
2318 } else if (Tok.is(tok::semi)) {
2319 EndLoc = Tok.getLocation();
2320 ConsumeToken();
2321 Diag(EndLoc, diag::err_expected_colon)
2322 << FixItHint::CreateReplacement(EndLoc, ":");
2323 } else {
2324 EndLoc = ASLoc.getLocWithOffset(TokLength);
2325 Diag(EndLoc, diag::err_expected_colon)
2326 << FixItHint::CreateInsertion(EndLoc, ":");
2327 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002328
2329 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2330 AccessAttrs.getList())) {
2331 // found another attribute than only annotations
2332 AccessAttrs.clear();
2333 }
2334
Douglas Gregor07976d22010-06-21 22:31:09 +00002335 continue;
2336 }
2337
2338 // FIXME: Make sure we don't have a template here.
2339
2340 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002341 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002342 }
2343
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002344 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002345 } else {
2346 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002347 }
Mike Stump1eb44332009-09-09 15:08:12 +00002348
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002349 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002350 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002351 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002352
John McCall42a4f662010-05-28 08:11:17 +00002353 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002354 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002355 T.getOpenLocation(),
2356 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002357 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002358
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002359 // C++11 [class.mem]p2:
2360 // Within the class member-specification, the class is regarded as complete
2361 // within function bodies, default arguments, exception-specifications, and
2362 // brace-or-equal-initializers for non-static data members (including such
2363 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002364 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002365 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002366 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002367 // declarations and the lexed inline method definitions, along with any
2368 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002369 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002370 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002371 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002372
2373 // We've finished with all pending member declarations.
2374 Actions.ActOnFinishCXXMemberDecls();
2375
Richard Smith7a614d82011-06-11 17:19:42 +00002376 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002377 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002378 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002379 }
2380
John McCall42a4f662010-05-28 08:11:17 +00002381 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002382 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2383 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002384
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002385 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002386 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002387 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002388}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002389
2390/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2391/// which explicitly initializes the members or base classes of a
2392/// class (C++ [class.base.init]). For example, the three initializers
2393/// after the ':' in the Derived constructor below:
2394///
2395/// @code
2396/// class Base { };
2397/// class Derived : Base {
2398/// int x;
2399/// float f;
2400/// public:
2401/// Derived(float f) : Base(), x(17), f(f) { }
2402/// };
2403/// @endcode
2404///
Mike Stump1eb44332009-09-09 15:08:12 +00002405/// [C++] ctor-initializer:
2406/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002407///
Mike Stump1eb44332009-09-09 15:08:12 +00002408/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002409/// mem-initializer ...[opt]
2410/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002411void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002412 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2413
John Wiegley28bbe4b2011-04-28 01:08:34 +00002414 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2415 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002416 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002417
Chris Lattner5f9e2722011-07-23 10:55:15 +00002418 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002419 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002420
Douglas Gregor7ad83902008-11-05 04:29:56 +00002421 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002422 if (Tok.is(tok::code_completion)) {
2423 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2424 MemInitializers.data(),
2425 MemInitializers.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002426 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002427 } else {
2428 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2429 if (!MemInit.isInvalid())
2430 MemInitializers.push_back(MemInit.get());
2431 else
2432 AnyErrors = true;
2433 }
2434
Douglas Gregor7ad83902008-11-05 04:29:56 +00002435 if (Tok.is(tok::comma))
2436 ConsumeToken();
2437 else if (Tok.is(tok::l_brace))
2438 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002439 // If the next token looks like a base or member initializer, assume that
2440 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002441 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2442 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2443 Diag(Loc, diag::err_ctor_init_missing_comma)
2444 << FixItHint::CreateInsertion(Loc, ", ");
2445 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002446 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002447 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002448 SkipUntil(tok::l_brace, true, true);
2449 break;
2450 }
2451 } while (true);
2452
Mike Stump1eb44332009-09-09 15:08:12 +00002453 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002454 MemInitializers.data(), MemInitializers.size(),
2455 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002456}
2457
2458/// ParseMemInitializer - Parse a C++ member initializer, which is
2459/// part of a constructor initializer that explicitly initializes one
2460/// member or base class (C++ [class.base.init]). See
2461/// ParseConstructorInitializer for an example.
2462///
2463/// [C++] mem-initializer:
2464/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002465/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002466///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002467/// [C++] mem-initializer-id:
2468/// '::'[opt] nested-name-specifier[opt] class-name
2469/// identifier
John McCalld226f652010-08-21 09:40:31 +00002470Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002471 // parse '::'[opt] nested-name-specifier[opt]
2472 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002473 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002474 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002475 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002476 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002477 if (TemplateId->Kind == TNK_Type_template ||
2478 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002479 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002480 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002481 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002482 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002483 }
David Blaikief2116622012-01-24 06:03:59 +00002484 // Uses of decltype will already have been converted to annot_decltype by
2485 // ParseOptionalCXXScopeSpecifier at this point.
2486 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2487 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002488 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002489 return true;
2490 }
Mike Stump1eb44332009-09-09 15:08:12 +00002491
David Blaikief2116622012-01-24 06:03:59 +00002492 IdentifierInfo *II = 0;
2493 DeclSpec DS(AttrFactory);
2494 SourceLocation IdLoc = Tok.getLocation();
2495 if (Tok.is(tok::annot_decltype)) {
2496 // Get the decltype expression, if there is one.
2497 ParseDecltypeSpecifier(DS);
2498 } else {
2499 if (Tok.is(tok::identifier))
2500 // Get the identifier. This may be a member name or a class name,
2501 // but we'll let the semantic analysis determine which it is.
2502 II = Tok.getIdentifierInfo();
2503 ConsumeToken();
2504 }
2505
Douglas Gregor7ad83902008-11-05 04:29:56 +00002506
2507 // Parse the '('.
David Blaikie4e4d0842012-03-11 07:00:24 +00002508 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002509 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2510
Sebastian Redl6df65482011-09-24 17:48:25 +00002511 ExprResult InitList = ParseBraceInitializer();
2512 if (InitList.isInvalid())
2513 return true;
2514
2515 SourceLocation EllipsisLoc;
2516 if (Tok.is(tok::ellipsis))
2517 EllipsisLoc = ConsumeToken();
2518
2519 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002520 TemplateTypeTy, DS, IdLoc,
2521 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002522 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002523 BalancedDelimiterTracker T(*this, tok::l_paren);
2524 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002525
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002526 // Parse the optional expression-list.
2527 ExprVector ArgExprs(Actions);
2528 CommaLocsTy CommaLocs;
2529 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2530 SkipUntil(tok::r_paren);
2531 return true;
2532 }
2533
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002534 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002535
2536 SourceLocation EllipsisLoc;
2537 if (Tok.is(tok::ellipsis))
2538 EllipsisLoc = ConsumeToken();
2539
2540 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002541 TemplateTypeTy, DS, IdLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002542 T.getOpenLocation(), ArgExprs.take(),
2543 ArgExprs.size(), T.getCloseLocation(),
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002544 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002545 }
2546
David Blaikie4e4d0842012-03-11 07:00:24 +00002547 Diag(Tok, getLangOpts().CPlusPlus0x ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002548 : diag::err_expected_lparen);
2549 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002550}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002551
Sebastian Redl7acafd02011-03-05 14:45:16 +00002552/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002553///
Douglas Gregora4745612008-12-01 18:00:20 +00002554/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002555/// dynamic-exception-specification
2556/// noexcept-specification
2557///
2558/// noexcept-specification:
2559/// 'noexcept'
2560/// 'noexcept' '(' constant-expression ')'
2561ExceptionSpecificationType
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002562Parser::tryParseExceptionSpecification(bool Delayed,
2563 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002564 SmallVectorImpl<ParsedType> &DynamicExceptions,
2565 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002566 ExprResult &NoexceptExpr,
2567 CachedTokens *&ExceptionSpecTokens) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002568 ExceptionSpecificationType Result = EST_None;
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002569 ExceptionSpecTokens = 0;
2570
2571 // Handle delayed parsing of exception-specifications.
2572 if (Delayed) {
2573 if (Tok.isNot(tok::kw_throw) && Tok.isNot(tok::kw_noexcept))
2574 return EST_None;
Sebastian Redl7acafd02011-03-05 14:45:16 +00002575
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002576 // Consume and cache the starting token.
2577 bool IsNoexcept = Tok.is(tok::kw_noexcept);
2578 Token StartTok = Tok;
2579 SpecificationRange = SourceRange(ConsumeToken());
2580
2581 // Check for a '('.
2582 if (!Tok.is(tok::l_paren)) {
2583 // If this is a bare 'noexcept', we're done.
2584 if (IsNoexcept) {
2585 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2586 NoexceptExpr = 0;
2587 return EST_BasicNoexcept;
2588 }
2589
2590 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2591 return EST_DynamicNone;
2592 }
2593
2594 // Cache the tokens for the exception-specification.
2595 ExceptionSpecTokens = new CachedTokens;
2596 ExceptionSpecTokens->push_back(StartTok); // 'throw' or 'noexcept'
2597 ExceptionSpecTokens->push_back(Tok); // '('
2598 SpecificationRange.setEnd(ConsumeParen()); // '('
2599
2600 if (!ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens,
2601 /*StopAtSemi=*/true,
2602 /*ConsumeFinalToken=*/true)) {
2603 NoexceptExpr = 0;
2604 delete ExceptionSpecTokens;
2605 ExceptionSpecTokens = 0;
2606 return IsNoexcept? EST_BasicNoexcept : EST_DynamicNone;
2607 }
2608 SpecificationRange.setEnd(Tok.getLocation());
2609
2610 // Add the 'stop' token.
2611 Token End;
2612 End.startToken();
2613 End.setKind(tok::cxx_exceptspec_end);
2614 End.setLocation(Tok.getLocation());
2615 ExceptionSpecTokens->push_back(End);
2616 return EST_Delayed;
2617 }
2618
Sebastian Redl7acafd02011-03-05 14:45:16 +00002619 // See if there's a dynamic specification.
2620 if (Tok.is(tok::kw_throw)) {
2621 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2622 DynamicExceptions,
2623 DynamicExceptionRanges);
2624 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2625 "Produced different number of exception types and ranges.");
2626 }
2627
2628 // If there's no noexcept specification, we're done.
2629 if (Tok.isNot(tok::kw_noexcept))
2630 return Result;
2631
Richard Smith841804b2011-10-17 23:06:20 +00002632 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2633
Sebastian Redl7acafd02011-03-05 14:45:16 +00002634 // If we already had a dynamic specification, parse the noexcept for,
2635 // recovery, but emit a diagnostic and don't store the results.
2636 SourceRange NoexceptRange;
2637 ExceptionSpecificationType NoexceptType = EST_None;
2638
2639 SourceLocation KeywordLoc = ConsumeToken();
2640 if (Tok.is(tok::l_paren)) {
2641 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002642 BalancedDelimiterTracker T(*this, tok::l_paren);
2643 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002644 NoexceptType = EST_ComputedNoexcept;
2645 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002646 // The argument must be contextually convertible to bool. We use
2647 // ActOnBooleanCondition for this purpose.
2648 if (!NoexceptExpr.isInvalid())
2649 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2650 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002651 T.consumeClose();
2652 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002653 } else {
2654 // There is no argument.
2655 NoexceptType = EST_BasicNoexcept;
2656 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2657 }
2658
2659 if (Result == EST_None) {
2660 SpecificationRange = NoexceptRange;
2661 Result = NoexceptType;
2662
2663 // If there's a dynamic specification after a noexcept specification,
2664 // parse that and ignore the results.
2665 if (Tok.is(tok::kw_throw)) {
2666 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2667 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2668 DynamicExceptionRanges);
2669 }
2670 } else {
2671 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2672 }
2673
2674 return Result;
2675}
2676
2677/// ParseDynamicExceptionSpecification - Parse a C++
2678/// dynamic-exception-specification (C++ [except.spec]).
2679///
2680/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002681/// 'throw' '(' type-id-list [opt] ')'
2682/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002683///
Douglas Gregora4745612008-12-01 18:00:20 +00002684/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002685/// type-id ... [opt]
2686/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002687///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002688ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2689 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002690 SmallVectorImpl<ParsedType> &Exceptions,
2691 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002692 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002693
Sebastian Redl7acafd02011-03-05 14:45:16 +00002694 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002695 BalancedDelimiterTracker T(*this, tok::l_paren);
2696 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002697 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2698 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002699 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002700 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002701
Douglas Gregora4745612008-12-01 18:00:20 +00002702 // Parse throw(...), a Microsoft extension that means "this function
2703 // can throw anything".
2704 if (Tok.is(tok::ellipsis)) {
2705 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002706 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002707 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002708 T.consumeClose();
2709 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002710 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002711 }
2712
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002713 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002714 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002715 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002716 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002717
Douglas Gregora04426c2010-12-20 23:57:46 +00002718 if (Tok.is(tok::ellipsis)) {
2719 // C++0x [temp.variadic]p5:
2720 // - In a dynamic-exception-specification (15.4); the pattern is a
2721 // type-id.
2722 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002723 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002724 if (!Res.isInvalid())
2725 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2726 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002727
Sebastian Redlef65f062009-05-29 18:02:33 +00002728 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002729 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002730 Ranges.push_back(Range);
2731 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002732
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002733 if (Tok.is(tok::comma))
2734 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002735 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002736 break;
2737 }
2738
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002739 T.consumeClose();
2740 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002741 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002742}
Douglas Gregor6569d682009-05-27 23:11:45 +00002743
Douglas Gregordab60ad2010-10-01 18:44:50 +00002744/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2745/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002746TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002747 assert(Tok.is(tok::arrow) && "expected arrow");
2748
2749 ConsumeToken();
2750
Richard Smith7796eb52012-03-12 08:56:40 +00002751 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002752}
2753
Douglas Gregor6569d682009-05-27 23:11:45 +00002754/// \brief We have just started parsing the definition of a new class,
2755/// so push that class onto our stack of classes that is currently
2756/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002757Sema::ParsingClassState
2758Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002759 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002760 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00002761 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
John McCalleee1d542011-02-14 07:13:47 +00002762 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002763}
2764
2765/// \brief Deallocate the given parsed class and all of its nested
2766/// classes.
2767void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002768 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2769 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002770 delete Class;
2771}
2772
2773/// \brief Pop the top class of the stack of classes that are
2774/// currently being parsed.
2775///
2776/// This routine should be called when we have finished parsing the
2777/// definition of a class, but have not yet popped the Scope
2778/// associated with the class's definition.
2779///
2780/// \returns true if the class we've popped is a top-level class,
2781/// false otherwise.
John McCalleee1d542011-02-14 07:13:47 +00002782void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002783 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002784
John McCalleee1d542011-02-14 07:13:47 +00002785 Actions.PopParsingClass(state);
2786
Douglas Gregor6569d682009-05-27 23:11:45 +00002787 ParsingClass *Victim = ClassStack.top();
2788 ClassStack.pop();
2789 if (Victim->TopLevelClass) {
2790 // Deallocate all of the nested classes of this class,
2791 // recursively: we don't need to keep any of this information.
2792 DeallocateParsedClasses(Victim);
2793 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002794 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002795 assert(!ClassStack.empty() && "Missing top-level class?");
2796
Douglas Gregord54eb442010-10-12 16:25:54 +00002797 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002798 // The victim is a nested class, but we will not need to perform
2799 // any processing after the definition of this class since it has
2800 // no members whose handling was delayed. Therefore, we can just
2801 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002802 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002803 return;
2804 }
2805
2806 // This nested class has some members that will need to be processed
2807 // after the top-level class is completely defined. Therefore, add
2808 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002809 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002810 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002811 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00002812}
Sean Huntbbd37c62009-11-21 08:43:09 +00002813
Richard Smithc56298d2012-04-10 03:25:07 +00002814/// \brief Try to parse an 'identifier' which appears within an attribute-token.
2815///
2816/// \return the parsed identifier on success, and 0 if the next token is not an
2817/// attribute-token.
2818///
2819/// C++11 [dcl.attr.grammar]p3:
2820/// If a keyword or an alternative token that satisfies the syntactic
2821/// requirements of an identifier is contained in an attribute-token,
2822/// it is considered an identifier.
2823IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
2824 switch (Tok.getKind()) {
2825 default:
2826 // Identifiers and keywords have identifier info attached.
2827 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
2828 Loc = ConsumeToken();
2829 return II;
2830 }
2831 return 0;
2832
2833 case tok::ampamp: // 'and'
2834 case tok::pipe: // 'bitor'
2835 case tok::pipepipe: // 'or'
2836 case tok::caret: // 'xor'
2837 case tok::tilde: // 'compl'
2838 case tok::amp: // 'bitand'
2839 case tok::ampequal: // 'and_eq'
2840 case tok::pipeequal: // 'or_eq'
2841 case tok::caretequal: // 'xor_eq'
2842 case tok::exclaim: // 'not'
2843 case tok::exclaimequal: // 'not_eq'
2844 // Alternative tokens do not have identifier info, but their spelling
2845 // starts with an alphabetical character.
2846 llvm::SmallString<8> SpellingBuf;
2847 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
2848 if (std::isalpha(Spelling[0])) {
2849 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00002850 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00002851 }
2852 return 0;
2853 }
2854}
2855
2856/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002857/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00002858///
Richard Smith6ee326a2012-04-10 01:32:12 +00002859/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00002860/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002861/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00002862///
Richard Smith6ee326a2012-04-10 01:32:12 +00002863/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00002864/// attribute[opt]
2865/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00002866/// attribute '...'
2867/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00002868///
Richard Smith6ee326a2012-04-10 01:32:12 +00002869/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002870/// attribute-token attribute-argument-clause[opt]
2871///
Richard Smith6ee326a2012-04-10 01:32:12 +00002872/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00002873/// identifier
2874/// attribute-scoped-token
2875///
Richard Smith6ee326a2012-04-10 01:32:12 +00002876/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00002877/// attribute-namespace '::' identifier
2878///
Richard Smith6ee326a2012-04-10 01:32:12 +00002879/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00002880/// identifier
2881///
Richard Smith6ee326a2012-04-10 01:32:12 +00002882/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00002883/// '(' balanced-token-seq ')'
2884///
Richard Smith6ee326a2012-04-10 01:32:12 +00002885/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00002886/// balanced-token
2887/// balanced-token-seq balanced-token
2888///
Richard Smith6ee326a2012-04-10 01:32:12 +00002889/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00002890/// '(' balanced-token-seq ')'
2891/// '[' balanced-token-seq ']'
2892/// '{' balanced-token-seq '}'
2893/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00002894void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002895 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002896 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00002897 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002898 ParseAlignmentSpecifier(attrs, endLoc);
2899 return;
2900 }
2901
Sean Huntbbd37c62009-11-21 08:43:09 +00002902 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00002903 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00002904
Richard Smith41be6732011-10-14 20:48:27 +00002905 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
2906
Sean Huntbbd37c62009-11-21 08:43:09 +00002907 ConsumeBracket();
2908 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002909
Richard Smithc56298d2012-04-10 03:25:07 +00002910 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002911 // attribute not present
2912 if (Tok.is(tok::comma)) {
2913 ConsumeToken();
2914 continue;
2915 }
2916
Richard Smithc56298d2012-04-10 03:25:07 +00002917 SourceLocation ScopeLoc, AttrLoc;
2918 IdentifierInfo *ScopeName = 0, *AttrName = 0;
2919
2920 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
2921 if (!AttrName)
2922 // Break out to the "expected ']'" diagnostic.
2923 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002924
Sean Huntbbd37c62009-11-21 08:43:09 +00002925 // scoped attribute
2926 if (Tok.is(tok::coloncolon)) {
2927 ConsumeToken();
2928
Richard Smithc56298d2012-04-10 03:25:07 +00002929 ScopeName = AttrName;
2930 ScopeLoc = AttrLoc;
2931
2932 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
2933 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002934 Diag(Tok.getLocation(), diag::err_expected_ident);
2935 SkipUntil(tok::r_square, tok::comma, true, true);
2936 continue;
2937 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002938 }
2939
2940 bool AttrParsed = false;
2941 // No scoped names are supported; ideally we could put all non-standard
2942 // attributes into namespaces.
2943 if (!ScopeName) {
Richard Smith6ee326a2012-04-10 01:32:12 +00002944 switch (AttributeList::getKind(AttrName)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002945 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00002946 case AttributeList::AT_carries_dependency:
Anders Carlsson15e14a22011-01-23 21:33:18 +00002947 case AttributeList::AT_noreturn: {
Sean Huntbbd37c62009-11-21 08:43:09 +00002948 if (Tok.is(tok::l_paren)) {
Richard Smithc56298d2012-04-10 03:25:07 +00002949 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
Sean Huntbbd37c62009-11-21 08:43:09 +00002950 << AttrName->getName();
2951 break;
2952 }
2953
John McCall0b7e6782011-03-24 11:26:52 +00002954 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc, 0,
2955 SourceLocation(), 0, 0, false, true);
Sean Huntbbd37c62009-11-21 08:43:09 +00002956 AttrParsed = true;
2957 break;
2958 }
2959
Sean Huntbbd37c62009-11-21 08:43:09 +00002960 // Silence warnings
2961 default: break;
2962 }
2963 }
2964
2965 // Skip the entire parameter clause, if any
2966 if (!AttrParsed && Tok.is(tok::l_paren)) {
2967 ConsumeParen();
2968 // SkipUntil maintains the balancedness of tokens.
2969 SkipUntil(tok::r_paren, false);
2970 }
Richard Smith6ee326a2012-04-10 01:32:12 +00002971
Richard Smithc56298d2012-04-10 03:25:07 +00002972 if (Tok.is(tok::ellipsis)) {
2973 if (AttrParsed)
2974 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
2975 << AttrName->getName();
Richard Smith6ee326a2012-04-10 01:32:12 +00002976 ConsumeToken();
Richard Smithc56298d2012-04-10 03:25:07 +00002977 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002978 }
2979
2980 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2981 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002982 if (endLoc)
2983 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00002984 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2985 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002986}
Sean Huntbbd37c62009-11-21 08:43:09 +00002987
Richard Smithc56298d2012-04-10 03:25:07 +00002988/// ParseCXX11Attributes - Parse a C++0x attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002989///
2990/// attribute-specifier-seq:
2991/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00002992void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002993 SourceLocation *endLoc) {
2994 SourceLocation StartLoc = Tok.getLocation(), Loc;
2995 if (!endLoc)
2996 endLoc = &Loc;
2997
Douglas Gregor8828ee72011-10-07 20:35:25 +00002998 do {
Richard Smithc56298d2012-04-10 03:25:07 +00002999 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003000 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003001
3002 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003003}
3004
Francois Pichet334d47e2010-10-11 12:59:39 +00003005/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3006///
3007/// [MS] ms-attribute:
3008/// '[' token-seq ']'
3009///
3010/// [MS] ms-attribute-seq:
3011/// ms-attribute[opt]
3012/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003013void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3014 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003015 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3016
3017 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003018 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003019 ConsumeBracket();
3020 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003021 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003022 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3023 }
3024}
Francois Pichet563a6452011-05-25 10:19:49 +00003025
3026void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3027 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003028 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003029 if (ParseMicrosoftIfExistsCondition(Result))
3030 return;
3031
Douglas Gregor3896fc52011-10-24 22:31:10 +00003032 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3033 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003034 Diag(Tok, diag::err_expected_lbrace);
3035 return;
3036 }
Francois Pichet563a6452011-05-25 10:19:49 +00003037
Douglas Gregor3896fc52011-10-24 22:31:10 +00003038 switch (Result.Behavior) {
3039 case IEB_Parse:
3040 // Parse the declarations below.
3041 break;
3042
3043 case IEB_Dependent:
3044 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3045 << Result.IsIfExists;
3046 // Fall through to skip.
3047
3048 case IEB_Skip:
3049 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003050 return;
3051 }
3052
Douglas Gregor3896fc52011-10-24 22:31:10 +00003053 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003054 // __if_exists, __if_not_exists can nest.
3055 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3056 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3057 continue;
3058 }
3059
3060 // Check for extraneous top-level semicolon.
3061 if (Tok.is(tok::semi)) {
3062 Diag(Tok, diag::ext_extra_struct_semi)
3063 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
3064 << FixItHint::CreateRemoval(Tok.getLocation());
3065 ConsumeToken();
3066 continue;
3067 }
3068
3069 AccessSpecifier AS = getAccessSpecifierIfPresent();
3070 if (AS != AS_none) {
3071 // Current token is a C++ access specifier.
3072 CurAS = AS;
3073 SourceLocation ASLoc = Tok.getLocation();
3074 ConsumeToken();
3075 if (Tok.is(tok::colon))
3076 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3077 else
3078 Diag(Tok, diag::err_expected_colon);
3079 ConsumeToken();
3080 continue;
3081 }
3082
3083 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003084 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003085 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003086
3087 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003088}