blob: c2bf54db2cdfec31c9b2a194032c81621aa9279d [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())
153 Diag(InlineLoc, getLang().CPlusPlus0x ?
154 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
275 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000276
Douglas Gregor074149e2009-01-05 19:45:36 +0000277 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000278 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000279 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000280 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000281 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000282 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000283 : SourceLocation());
284
John McCall0b7e6782011-03-24 11:26:52 +0000285 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000286 MaybeParseCXX0XAttributes(attrs);
287 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000288
Douglas Gregor074149e2009-01-05 19:45:36 +0000289 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-05-01 16:25:54 +0000290 // Reset the source range in DS, as the leading "extern"
291 // does not really belong to the inner declaration ...
292 DS.SetRangeStart(SourceLocation());
293 DS.SetRangeEnd(SourceLocation());
294 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000295 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000296 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000297 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000298 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000299 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000300
Douglas Gregor63a01132010-02-07 08:38:28 +0000301 DS.abort();
302
John McCall7f040a92010-12-24 02:08:15 +0000303 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000304
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000305 BalancedDelimiterTracker T(*this, tok::l_brace);
306 T.consumeOpen();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000307 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000308 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000309 MaybeParseCXX0XAttributes(attrs);
310 MaybeParseMicrosoftAttributes(attrs);
311 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000312 }
313
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000314 T.consumeClose();
Chris Lattner7d642712010-11-09 20:15:55 +0000315 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000316 T.getCloseLocation());
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000317}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000318
Douglas Gregorf780abc2008-12-30 03:27:21 +0000319/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
320/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000321Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000322 const ParsedTemplateInfo &TemplateInfo,
323 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000324 ParsedAttributesWithRange &attrs,
325 Decl **OwnedType) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000326 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000327 ObjCDeclContextSwitch ObjCDC(*this);
328
Douglas Gregorf780abc2008-12-30 03:27:21 +0000329 // Eat 'using'.
330 SourceLocation UsingLoc = ConsumeToken();
331
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000332 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000333 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000334 cutOffParsing();
335 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000336 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000337
John McCall78b81052010-11-10 02:40:36 +0000338 // 'using namespace' means this is a using-directive.
339 if (Tok.is(tok::kw_namespace)) {
340 // Template parameters are always an error here.
341 if (TemplateInfo.Kind) {
342 SourceRange R = TemplateInfo.getSourceRange();
343 Diag(UsingLoc, diag::err_templated_using_directive)
344 << R << FixItHint::CreateRemoval(R);
345 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000346
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000347 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000348 }
349
Richard Smith162e1c12011-04-15 14:24:37 +0000350 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000351
352 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000353 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000354
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000355 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000356 AS_none, OwnedType);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000357}
358
359/// ParseUsingDirective - Parse C++ using-directive, assumes
360/// that current token is 'namespace' and 'using' was already parsed.
361///
362/// using-directive: [C++ 7.3.p4: namespace.udir]
363/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
364/// namespace-name ;
365/// [GNU] using-directive:
366/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
367/// namespace-name attributes[opt] ;
368///
John McCalld226f652010-08-21 09:40:31 +0000369Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000370 SourceLocation UsingLoc,
371 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000372 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000373 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
374
375 // Eat 'namespace'.
376 SourceLocation NamespcLoc = ConsumeToken();
377
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000378 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000379 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000380 cutOffParsing();
381 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000382 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000383
Douglas Gregorf780abc2008-12-30 03:27:21 +0000384 CXXScopeSpec SS;
385 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000386 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000387
Douglas Gregorf780abc2008-12-30 03:27:21 +0000388 IdentifierInfo *NamespcName = 0;
389 SourceLocation IdentLoc = SourceLocation();
390
391 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000392 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000393 Diag(Tok, diag::err_expected_namespace_name);
394 // If there was invalid namespace name, skip to end of decl, and eat ';'.
395 SkipUntil(tok::semi);
396 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000397 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000398 }
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattner823c44e2009-01-06 07:27:21 +0000400 // Parse identifier.
401 NamespcName = Tok.getIdentifierInfo();
402 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattner823c44e2009-01-06 07:27:21 +0000404 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000405 bool GNUAttr = false;
406 if (Tok.is(tok::kw___attribute)) {
407 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000408 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000409 }
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Chris Lattner823c44e2009-01-06 07:27:21 +0000411 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000412 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000413 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000414 GNUAttr ? diag::err_expected_semi_after_attribute_list
415 : diag::err_expected_semi_after_namespace_name,
416 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000417
Douglas Gregor23c94db2010-07-02 17:43:08 +0000418 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000419 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000420}
421
Richard Smith162e1c12011-04-15 14:24:37 +0000422/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
423/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000424///
425/// using-declaration: [C++ 7.3.p3: namespace.udecl]
426/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000427/// unqualified-id
428/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000429///
Richard Smith162e1c12011-04-15 14:24:37 +0000430/// alias-declaration: C++0x [decl.typedef]p2
431/// 'using' identifier = type-id ;
432///
John McCalld226f652010-08-21 09:40:31 +0000433Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000434 const ParsedTemplateInfo &TemplateInfo,
435 SourceLocation UsingLoc,
436 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000437 AccessSpecifier AS,
438 Decl **OwnedType) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000439 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000440 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000441 bool IsTypeName;
442
443 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000444 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000445 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000446 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000447 ConsumeToken();
448 IsTypeName = true;
449 }
450 else
451 IsTypeName = false;
452
453 // Parse nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000454 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000455
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000456 // Check nested-name specifier.
457 if (SS.isInvalid()) {
458 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000459 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000460 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000461
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000462 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000463 // destructor names and allow the action module to diagnose any semantic
464 // errors.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000465 SourceLocation TemplateKWLoc;
Douglas Gregor12c118a2009-11-04 16:30:06 +0000466 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000467 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000468 /*EnteringContext=*/false,
469 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000470 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000471 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000472 TemplateKWLoc,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000473 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000474 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000475 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000476 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000477
John McCall0b7e6782011-03-24 11:26:52 +0000478 ParsedAttributes attrs(AttrFactory);
Richard Smith162e1c12011-04-15 14:24:37 +0000479
480 // Maybe this is an alias-declaration.
481 bool IsAliasDecl = Tok.is(tok::equal);
482 TypeResult TypeAlias;
483 if (IsAliasDecl) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000484 // TODO: Attribute support. C++0x attributes may appear before the equals.
485 // Where can GNU attributes appear?
Richard Smith162e1c12011-04-15 14:24:37 +0000486 ConsumeToken();
487
Richard Smith7fe62082011-10-15 05:09:34 +0000488 Diag(Tok.getLocation(), getLang().CPlusPlus0x ?
489 diag::warn_cxx98_compat_alias_declaration :
490 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000491
Richard Smith3e4c6c42011-05-05 21:57:07 +0000492 // Type alias templates cannot be specialized.
493 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000494 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
495 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000496 SpecKind = 0;
497 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
498 SpecKind = 1;
499 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
500 SpecKind = 2;
501 if (SpecKind != -1) {
502 SourceRange Range;
503 if (SpecKind == 0)
504 Range = SourceRange(Name.TemplateId->LAngleLoc,
505 Name.TemplateId->RAngleLoc);
506 else
507 Range = TemplateInfo.getSourceRange();
508 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
509 << SpecKind << Range;
510 SkipUntil(tok::semi);
511 return 0;
512 }
513
Richard Smith162e1c12011-04-15 14:24:37 +0000514 // Name must be an identifier.
515 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
516 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
517 // No removal fixit: can't recover from this.
518 SkipUntil(tok::semi);
519 return 0;
520 } else if (IsTypeName)
521 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
522 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
523 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
524 else if (SS.isNotEmpty())
525 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
526 << FixItHint::CreateRemoval(SS.getRange());
527
Richard Smith3e4c6c42011-05-05 21:57:07 +0000528 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
529 Declarator::AliasTemplateContext :
John McCallcdda47f2011-10-01 09:56:14 +0000530 Declarator::AliasDeclContext, AS, OwnedType);
Richard Smith162e1c12011-04-15 14:24:37 +0000531 } else
532 // Parse (optional) attributes (most likely GNU strong-using extension).
533 MaybeParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000535 // Eat ';'.
536 DeclEnd = Tok.getLocation();
537 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith162e1c12011-04-15 14:24:37 +0000538 !attrs.empty() ? "attributes list" :
539 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000540 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000541
John McCall78b81052010-11-10 02:40:36 +0000542 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith3e4c6c42011-05-05 21:57:07 +0000543 // In C++0x, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000544 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000545 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000546 SourceRange R = TemplateInfo.getSourceRange();
547 Diag(UsingLoc, diag::err_templated_using_declaration)
548 << R << FixItHint::CreateRemoval(R);
549
550 // Unfortunately, we have to bail out instead of recovering by
551 // ignoring the parameters, just in case the nested name specifier
552 // depends on the parameters.
553 return 0;
554 }
555
Douglas Gregor480b53c2011-09-26 14:30:28 +0000556 // "typename" keyword is allowed for identifiers only,
557 // because it may be a type definition.
558 if (IsTypeName && Name.getKind() != UnqualifiedId::IK_Identifier) {
559 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
560 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
561 // Proceed parsing, but reset the IsTypeName flag.
562 IsTypeName = false;
563 }
564
Richard Smith3e4c6c42011-05-05 21:57:07 +0000565 if (IsAliasDecl) {
566 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
567 MultiTemplateParamsArg TemplateParamsArg(Actions,
568 TemplateParams ? TemplateParams->data() : 0,
569 TemplateParams ? TemplateParams->size() : 0);
570 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
571 UsingLoc, Name, TypeAlias);
572 }
Richard Smith162e1c12011-04-15 14:24:37 +0000573
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000574 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000575 Name, attrs.getList(),
576 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000577}
578
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000579/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000580///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000581/// [C++0x] static_assert-declaration:
582/// static_assert ( constant-expression , string-literal ) ;
583///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000584/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000585/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000586///
John McCalld226f652010-08-21 09:40:31 +0000587Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000588 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
589 "Not a static_assert declaration");
590
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000591 if (Tok.is(tok::kw__Static_assert) && !getLang().C11)
592 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000593 if (Tok.is(tok::kw_static_assert))
594 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000595
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000596 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000598 BalancedDelimiterTracker T(*this, tok::l_paren);
599 if (T.consumeOpen()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000600 Diag(Tok, diag::err_expected_lparen);
John McCalld226f652010-08-21 09:40:31 +0000601 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000602 }
Mike Stump1eb44332009-09-09 15:08:12 +0000603
John McCall60d7b3a2010-08-24 06:29:42 +0000604 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000605 if (AssertExpr.isInvalid()) {
606 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000607 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000608 }
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Anders Carlssonad5f9602009-03-13 23:29:20 +0000610 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000611 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000612
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000613 if (Tok.isNot(tok::string_literal)) {
614 Diag(Tok, diag::err_expected_string_literal);
615 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000616 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
John McCall60d7b3a2010-08-24 06:29:42 +0000619 ExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000620 if (AssertMessage.isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000621 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000622
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000623 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Chris Lattner97144fc2009-04-02 04:16:50 +0000625 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000626 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000627
John McCall9ae2f072010-08-23 23:25:46 +0000628 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
629 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000630 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000631 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000632}
633
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000634/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
635///
636/// 'decltype' ( expression )
637///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000638SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
639 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
640 && "Not a decltype specifier");
641
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000642
David Blaikie42d6d0c2011-12-04 05:04:18 +0000643 ExprResult Result;
644 SourceLocation StartLoc = Tok.getLocation();
645 SourceLocation EndLoc;
646
647 if (Tok.is(tok::annot_decltype)) {
648 Result = getExprAnnotation(Tok);
649 EndLoc = Tok.getAnnotationEndLoc();
650 ConsumeToken();
651 if (Result.isInvalid()) {
652 DS.SetTypeSpecError();
653 return EndLoc;
654 }
655 } else {
656 ConsumeToken();
657
658 BalancedDelimiterTracker T(*this, tok::l_paren);
659 if (T.expectAndConsume(diag::err_expected_lparen_after,
660 "decltype", tok::r_paren)) {
661 DS.SetTypeSpecError();
662 return T.getOpenLocation() == Tok.getLocation() ?
663 StartLoc : T.getOpenLocation();
664 }
665
666 // Parse the expression
667
668 // C++0x [dcl.type.simple]p4:
669 // The operand of the decltype specifier is an unevaluated operand.
Richard Smith76f3f692012-02-22 02:04:18 +0000670 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
671 0, /*IsDecltype=*/true);
David Blaikie42d6d0c2011-12-04 05:04:18 +0000672 Result = ParseExpression();
673 if (Result.isInvalid()) {
674 SkipUntil(tok::r_paren, true, true);
675 DS.SetTypeSpecError();
676 return Tok.is(tok::eof) ? Tok.getLocation() : ConsumeParen();
677 }
678
679 // Match the ')'
680 T.consumeClose();
681 if (T.getCloseLocation().isInvalid()) {
682 DS.SetTypeSpecError();
683 // FIXME: this should return the location of the last token
684 // that was consumed (by "consumeClose()")
685 return T.getCloseLocation();
686 }
687
Richard Smith76f3f692012-02-22 02:04:18 +0000688 Result = Actions.ActOnDecltypeExpression(Result.take());
689 if (Result.isInvalid()) {
690 DS.SetTypeSpecError();
691 return T.getCloseLocation();
692 }
693
David Blaikie42d6d0c2011-12-04 05:04:18 +0000694 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000695 }
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000697 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000698 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000699 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000700 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
David Blaikie42d6d0c2011-12-04 05:04:18 +0000701 DiagID, Result.release())) {
John McCallfec54012009-08-03 20:12:06 +0000702 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000703 DS.SetTypeSpecError();
704 }
705 return EndLoc;
706}
707
708void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
709 SourceLocation StartLoc,
710 SourceLocation EndLoc) {
711 // make sure we have a token we can turn into an annotation token
712 if (PP.isBacktrackEnabled())
713 PP.RevertCachedTokens(1);
714 else
715 PP.EnterToken(Tok);
716
717 Tok.setKind(tok::annot_decltype);
718 setExprAnnotation(Tok, DS.getTypeSpecType() == TST_decltype ?
719 DS.getRepAsExpr() : ExprResult());
720 Tok.setAnnotationEndLoc(EndLoc);
721 Tok.setLocation(StartLoc);
722 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000723}
724
Sean Huntdb5d44b2011-05-19 05:37:45 +0000725void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
726 assert(Tok.is(tok::kw___underlying_type) &&
727 "Not an underlying type specifier");
728
729 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000730 BalancedDelimiterTracker T(*this, tok::l_paren);
731 if (T.expectAndConsume(diag::err_expected_lparen_after,
732 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000733 return;
734 }
735
736 TypeResult Result = ParseTypeName();
737 if (Result.isInvalid()) {
738 SkipUntil(tok::r_paren);
739 return;
740 }
741
742 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000743 T.consumeClose();
744 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000745 return;
746
747 const char *PrevSpec = 0;
748 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000749 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000750 DiagID, Result.release()))
751 Diag(StartLoc, DiagID) << PrevSpec;
752}
753
David Blaikie09048df2011-10-25 15:01:20 +0000754/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
755/// class name or decltype-specifier. Note that we only check that the result
756/// names a type; semantic analysis will need to verify that the type names a
757/// class. The result is either a type or null, depending on whether a type
758/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000759///
David Blaikie09048df2011-10-25 15:01:20 +0000760/// base-type-specifier: [C++ 10.1]
761/// class-or-decltype
762/// class-or-decltype: [C++ 10.1]
763/// nested-name-specifier[opt] class-name
764/// decltype-specifier
Douglas Gregor42a552f2008-11-05 20:51:48 +0000765/// class-name: [C++ 9.1]
766/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000767/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000768///
David Blaikie22216eb2011-10-25 17:10:12 +0000769Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
770 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000771 // Ignore attempts to use typename
772 if (Tok.is(tok::kw_typename)) {
773 Diag(Tok, diag::err_expected_class_name_not_template)
774 << FixItHint::CreateRemoval(Tok.getLocation());
775 ConsumeToken();
776 }
777
David Blaikie152aa4b2011-10-25 18:17:58 +0000778 // Parse optional nested-name-specifier
779 CXXScopeSpec SS;
780 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
781
782 BaseLoc = Tok.getLocation();
783
David Blaikie22216eb2011-10-25 17:10:12 +0000784 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000785 // tok == kw_decltype is just error recovery, it can only happen when SS
786 // isn't empty
787 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000788 if (SS.isNotEmpty())
789 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
790 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000791 // Fake up a Declarator to use with ActOnTypeName.
792 DeclSpec DS(AttrFactory);
793
David Blaikieb5777572011-12-08 04:53:15 +0000794 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000795
796 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
797 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
798 }
799
Douglas Gregor7f43d672009-02-25 23:52:28 +0000800 // Check whether we have a template-id that names a type.
801 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000802 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000803 if (TemplateId->Kind == TNK_Type_template ||
804 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000805 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000806
807 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000808 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000809 EndLocation = Tok.getAnnotationEndLoc();
810 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000811
812 if (Type)
813 return Type;
814 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000815 }
816
817 // Fall through to produce an error below.
818 }
819
Douglas Gregor42a552f2008-11-05 20:51:48 +0000820 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000821 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000822 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000823 }
824
Douglas Gregor84d0a192010-01-12 21:28:44 +0000825 IdentifierInfo *Id = Tok.getIdentifierInfo();
826 SourceLocation IdLoc = ConsumeToken();
827
828 if (Tok.is(tok::less)) {
829 // It looks the user intended to write a template-id here, but the
830 // template-name was wrong. Try to fix that.
831 TemplateNameKind TNK = TNK_Type_template;
832 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000833 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000834 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000835 Diag(IdLoc, diag::err_unknown_template_name)
836 << Id;
837 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000838
Douglas Gregor84d0a192010-01-12 21:28:44 +0000839 if (!Template)
840 return true;
841
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000842 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000843 UnqualifiedId TemplateName;
844 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000845
Douglas Gregor84d0a192010-01-12 21:28:44 +0000846 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000847 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
848 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000849 return true;
850 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000851 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000852
Douglas Gregor84d0a192010-01-12 21:28:44 +0000853 // If we didn't end up with a typename token, there's nothing more we
854 // can do.
855 if (Tok.isNot(tok::annot_typename))
856 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000857
Douglas Gregor84d0a192010-01-12 21:28:44 +0000858 // Retrieve the type from the annotation token, consume that token, and
859 // return.
860 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000861 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000862 ConsumeToken();
863 return Type;
864 }
865
Douglas Gregor42a552f2008-11-05 20:51:48 +0000866 // We have an identifier; check whether it is actually a type.
Douglas Gregor059101f2011-03-02 00:47:37 +0000867 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000868 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000869 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +0000870 /*NonTrivialTypeSourceInfo=*/true);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000871 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000872 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000873 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000874 }
875
876 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000877 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000878
879 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000880 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000881 DS.SetRangeStart(IdLoc);
882 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000883 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000884
885 const char *PrevSpec = 0;
886 unsigned DiagID;
887 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
888
889 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
890 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000891}
892
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000893/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
894/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
895/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000896/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000897///
898/// class-specifier: [C++ class]
899/// class-head '{' member-specification[opt] '}'
900/// class-head '{' member-specification[opt] '}' attributes[opt]
901/// class-head:
902/// class-key identifier[opt] base-clause[opt]
903/// class-key nested-name-specifier identifier base-clause[opt]
904/// class-key nested-name-specifier[opt] simple-template-id
905/// base-clause[opt]
906/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000907/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000908/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000909/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000910/// simple-template-id base-clause[opt]
911/// class-key:
912/// 'class'
913/// 'struct'
914/// 'union'
915///
916/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000917/// class-key ::[opt] nested-name-specifier[opt] identifier
918/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
919/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000920///
921/// Note that the C++ class-specifier and elaborated-type-specifier,
922/// together, subsume the C99 struct-or-union-specifier:
923///
924/// struct-or-union-specifier: [C99 6.7.2.1]
925/// struct-or-union identifier[opt] '{' struct-contents '}'
926/// struct-or-union identifier
927/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
928/// '}' attributes[opt]
929/// [GNU] struct-or-union attributes[opt] identifier
930/// struct-or-union:
931/// 'struct'
932/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000933void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
934 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000935 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000936 AccessSpecifier AS,
937 bool EnteringContext,
938 bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000939 DeclSpec::TST TagType;
940 if (TagTokKind == tok::kw_struct)
941 TagType = DeclSpec::TST_struct;
942 else if (TagTokKind == tok::kw_class)
943 TagType = DeclSpec::TST_class;
944 else {
945 assert(TagTokKind == tok::kw_union && "Not a class specifier");
946 TagType = DeclSpec::TST_union;
947 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000948
Douglas Gregor374929f2009-09-18 15:37:17 +0000949 if (Tok.is(tok::code_completion)) {
950 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000951 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000952 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +0000953 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000954
Chandler Carruth926c4b42010-06-28 08:39:25 +0000955 // C++03 [temp.explicit] 14.7.2/8:
956 // The usual access checking rules do not apply to names used to specify
957 // explicit instantiations.
958 //
959 // As an extension we do not perform access checking on the names used to
960 // specify explicit specializations either. This is important to allow
961 // specializing traits classes for private types.
962 bool SuppressingAccessChecks = false;
963 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
964 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
965 Actions.ActOnStartSuppressingAccessChecks();
966 SuppressingAccessChecks = true;
967 }
968
John McCall0b7e6782011-03-24 11:26:52 +0000969 ParsedAttributes attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000970 // If attributes exist after tag, parse them.
971 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +0000972 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000973
Steve Narofff59e17e2008-12-24 20:59:21 +0000974 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +0000975 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +0000976 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000977
Sean Huntbbd37c62009-11-21 08:43:09 +0000978 // If C++0x attributes exist here, parse them.
979 // FIXME: Are we consistent with the ordering of parsing of different
980 // styles of attributes?
John McCall7f040a92010-12-24 02:08:15 +0000981 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000982
John Wiegley20c0da72011-04-27 23:09:49 +0000983 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +0000984 !Tok.is(tok::identifier) &&
985 Tok.getIdentifierInfo() &&
986 (Tok.is(tok::kw___is_arithmetic) ||
987 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +0000988 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +0000989 Tok.is(tok::kw___is_floating_point) ||
990 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +0000991 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +0000992 Tok.is(tok::kw___is_integral) ||
993 Tok.is(tok::kw___is_member_function_pointer) ||
994 Tok.is(tok::kw___is_member_pointer) ||
995 Tok.is(tok::kw___is_pod) ||
996 Tok.is(tok::kw___is_pointer) ||
997 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +0000998 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +0000999 Tok.is(tok::kw___is_signed) ||
1000 Tok.is(tok::kw___is_unsigned) ||
1001 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001002 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001003 // name of struct templates, but some are keywords in GCC >= 4.3
1004 // and Clang. Therefore, when we see the token sequence "struct
1005 // X", make X into a normal identifier rather than a keyword, to
1006 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001007 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001008 Tok.setKind(tok::identifier);
1009 }
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001011 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001012 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +00001013 if (getLang().CPlusPlus) {
1014 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1015 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001016
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001017 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001018 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001019 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001020 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1021 Diag(Tok, diag::err_expected_ident);
1022 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001023
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001024 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1025
Douglas Gregorcc636682009-02-17 23:15:12 +00001026 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001027 IdentifierInfo *Name = 0;
1028 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001029 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001030 if (Tok.is(tok::identifier)) {
1031 Name = Tok.getIdentifierInfo();
1032 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001033
Douglas Gregor5ee37342010-05-30 22:30:21 +00001034 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001035 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001036 // Eat the template argument list and try to continue parsing this as
1037 // a class (or template thereof).
1038 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001039 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001040 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001041 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001042 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001043 // We couldn't parse the template argument list at all, so don't
1044 // try to give any location information for the list.
1045 LAngleLoc = RAngleLoc = SourceLocation();
1046 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001047
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001048 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001049 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001050 << (TagType == DeclSpec::TST_class? 0
1051 : TagType == DeclSpec::TST_struct? 1
1052 : 2)
1053 << Name
1054 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001055
1056 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001057 // we've removed its template argument list.
1058 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1059 if (TemplateParams && TemplateParams->size() > 1) {
1060 TemplateParams->pop_back();
1061 } else {
1062 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001063 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001064 = ParsedTemplateInfo::NonTemplate;
1065 }
1066 } else if (TemplateInfo.Kind
1067 == ParsedTemplateInfo::ExplicitInstantiation) {
1068 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001069 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001070 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001071 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001072 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001073 = SourceLocation();
1074 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1075 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001076 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001077 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001078 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001079 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001080 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001081
Douglas Gregor059101f2011-03-02 00:47:37 +00001082 if (TemplateId->Kind != TNK_Type_template &&
1083 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001084 // The template-name in the simple-template-id refers to
1085 // something other than a class template. Give an appropriate
1086 // error message and skip to the ';'.
1087 SourceRange Range(NameLoc);
1088 if (SS.isNotEmpty())
1089 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001090
Douglas Gregor39a8de12009-02-25 19:37:18 +00001091 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1092 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Douglas Gregor39a8de12009-02-25 19:37:18 +00001094 DS.SetTypeSpecError();
1095 SkipUntil(tok::semi, false, true);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001096 if (SuppressingAccessChecks)
1097 Actions.ActOnStopSuppressingAccessChecks();
1098
Douglas Gregor39a8de12009-02-25 19:37:18 +00001099 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001100 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001101 }
1102
Chandler Carruth926c4b42010-06-28 08:39:25 +00001103 // As soon as we're finished parsing the class's template-id, turn access
1104 // checking back on.
1105 if (SuppressingAccessChecks)
1106 Actions.ActOnStopSuppressingAccessChecks();
1107
John McCall67d1a672009-08-06 02:15:43 +00001108 // There are four options here. If we have 'struct foo;', then this
1109 // is either a forward declaration or a friend declaration, which
Anders Carlssoncc54d592011-01-22 16:56:46 +00001110 // have to be treated differently. If we have 'struct foo {...',
Anders Carlsson1d209272011-03-25 14:55:14 +00001111 // 'struct foo :...' or 'struct foo final[opt]' then this is a
Anders Carlssoncc54d592011-01-22 16:56:46 +00001112 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001113 // However, in some contexts, things look like declarations but are just
1114 // references, e.g.
1115 // new struct s;
1116 // or
1117 // &T::operator struct s;
1118 // For these, SuppressDeclarations is true.
John McCallf312b1e2010-08-26 23:41:50 +00001119 Sema::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001120 if (SuppressDeclarations)
John McCallf312b1e2010-08-26 23:41:50 +00001121 TUK = Sema::TUK_Reference;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001122 else if (Tok.is(tok::l_brace) ||
1123 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001124 isCXX0XFinalKeyword()) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001125 if (DS.isFriendSpecified()) {
1126 // C++ [class.friend]p2:
1127 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001128 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001129 << SourceRange(DS.getFriendSpecLoc());
1130
1131 // Skip everything up to the semicolon, so that this looks like a proper
1132 // friend class (or template thereof) declaration.
1133 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001134 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001135 } else {
1136 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001137 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001138 }
1139 } else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00001140 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001141 else
John McCallf312b1e2010-08-26 23:41:50 +00001142 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001143
John McCall207014e2010-07-30 06:26:29 +00001144 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001145 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001146 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1147 // We have a declaration or reference to an anonymous class.
1148 Diag(StartLoc, diag::err_anon_type_definition)
1149 << DeclSpec::getSpecifierName(TagType);
1150 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001151
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001152 SkipUntil(tok::comma, true);
1153 return;
1154 }
1155
Douglas Gregorddc29e12009-02-06 22:42:48 +00001156 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001157 DeclResult TagOrTempResult = true; // invalid
1158 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001159
Douglas Gregor402abb52009-05-28 23:31:59 +00001160 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001161 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001162 // Explicit specialization, class template partial specialization,
1163 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001164 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001165 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001166 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001167 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001168 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001169 // This is an explicit instantiation of a class template.
1170 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001171 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001172 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001173 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001174 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001175 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001176 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001177 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001178 TemplateId->TemplateNameLoc,
1179 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001180 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001181 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001182 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001183
1184 // Friend template-ids are treated as references unless
1185 // they have template headers, in which case they're ill-formed
1186 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1187 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001188 } else if (TUK == Sema::TUK_Reference ||
1189 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001190 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001191 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001192 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001193 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001194 TemplateId->Template,
1195 TemplateId->TemplateNameLoc,
1196 TemplateId->LAngleLoc,
1197 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001198 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001199 } else {
1200 // This is an explicit specialization or a class template
1201 // partial specialization.
1202 TemplateParameterLists FakedParamLists;
1203
1204 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1205 // This looks like an explicit instantiation, because we have
1206 // something like
1207 //
1208 // template class Foo<X>
1209 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001210 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001211 // meant to be an explicit specialization, but the user forgot
1212 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +00001213 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001214
Mike Stump1eb44332009-09-09 15:08:12 +00001215 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001216 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001217 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001218 diag::err_explicit_instantiation_with_definition)
1219 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +00001220 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001221
1222 // Create a fake template parameter list that contains only
1223 // "template<>", so that we treat this construct as a class
1224 // template specialization.
1225 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +00001226 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001227 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001228 LAngleLoc,
1229 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001230 LAngleLoc));
1231 TemplateParams = &FakedParamLists;
1232 }
1233
1234 // Build the class template specialization.
1235 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001236 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001237 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001238 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001239 TemplateId->TemplateNameLoc,
1240 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001241 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001242 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001243 attrs.getList(),
John McCallf312b1e2010-08-26 23:41:50 +00001244 MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +00001245 TemplateParams? &(*TemplateParams)[0] : 0,
1246 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001247 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001248 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001249 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001250 // Explicit instantiation of a member of a class template
1251 // specialization, e.g.,
1252 //
1253 // template struct Outer<int>::Inner;
1254 //
1255 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001256 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001257 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001258 TemplateInfo.TemplateLoc,
1259 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001260 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001261 } else if (TUK == Sema::TUK_Friend &&
1262 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
1263 TagOrTempResult =
1264 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1265 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001266 Name, NameLoc, attrs.getList(),
John McCall9a34edb2010-10-19 01:40:49 +00001267 MultiTemplateParamsArg(Actions,
1268 TemplateParams? &(*TemplateParams)[0] : 0,
1269 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001270 } else {
1271 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001272 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001273 // FIXME: Diagnose this particular error.
1274 }
1275
John McCallc4e70192009-09-11 04:59:25 +00001276 bool IsDependent = false;
1277
John McCalla25c4082010-10-19 18:40:57 +00001278 // Don't pass down template parameter lists if this is just a tag
1279 // reference. For example, we don't need the template parameters here:
1280 // template <class T> class A *makeA(T t);
1281 MultiTemplateParamsArg TParams;
1282 if (TUK != Sema::TUK_Reference && TemplateParams)
1283 TParams =
1284 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1285
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001286 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001287 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001288 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001289 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001290 TParams, Owned, IsDependent,
1291 SourceLocation(), false,
1292 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001293
1294 // If ActOnTag said the type was dependent, try again with the
1295 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001296 if (IsDependent) {
1297 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001298 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001299 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001300 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001301 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001302
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001303 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001304 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001305 assert(Tok.is(tok::l_brace) ||
Anders Carlssoncc54d592011-01-22 16:56:46 +00001306 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001307 isCXX0XFinalKeyword());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001308 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +00001309 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001310 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001311 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001312 }
1313
John McCallb3d87482010-08-24 05:47:05 +00001314 const char *PrevSpec = 0;
1315 unsigned DiagID;
1316 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001317 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001318 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1319 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001320 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001321 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001322 Result = DS.SetTypeSpecType(TagType, StartLoc,
1323 NameLoc.isValid() ? NameLoc : StartLoc,
1324 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001325 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001326 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001327 return;
1328 }
Mike Stump1eb44332009-09-09 15:08:12 +00001329
John McCallb3d87482010-08-24 05:47:05 +00001330 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001331 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001332
Chris Lattner4ed5d912010-02-02 01:23:29 +00001333 // At this point, we've successfully parsed a class-specifier in 'definition'
1334 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1335 // going to look at what comes after it to improve error recovery. If an
1336 // impossible token occurs next, we assume that the programmer forgot a ; at
1337 // the end of the declaration and recover that way.
1338 //
1339 // This switch enumerates the valid "follow" set for definition.
John McCallf312b1e2010-08-26 23:41:50 +00001340 if (TUK == Sema::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001341 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001342 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001343 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001344 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +00001345 case tok::star: // struct foo {...} * P;
1346 case tok::amp: // struct foo {...} & R = ...
1347 case tok::identifier: // struct foo {...} V ;
1348 case tok::r_paren: //(struct foo {...} ) {4}
1349 case tok::annot_cxxscope: // struct foo {...} a:: b;
1350 case tok::annot_typename: // struct foo {...} a ::b;
1351 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +00001352 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +00001353 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001354 ExpectedSemi = false;
1355 break;
1356 // Type qualifiers
1357 case tok::kw_const: // struct foo {...} const x;
1358 case tok::kw_volatile: // struct foo {...} volatile x;
1359 case tok::kw_restrict: // struct foo {...} restrict x;
1360 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +00001361 // Storage-class specifiers
1362 case tok::kw_static: // struct foo {...} static x;
1363 case tok::kw_extern: // struct foo {...} extern x;
1364 case tok::kw_typedef: // struct foo {...} typedef x;
1365 case tok::kw_register: // struct foo {...} register x;
1366 case tok::kw_auto: // struct foo {...} auto x;
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001367 case tok::kw_mutable: // struct foo {...} mutable x;
1368 case tok::kw_constexpr: // struct foo {...} constexpr x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001369 // As shown above, type qualifiers and storage class specifiers absolutely
1370 // can occur after class specifiers according to the grammar. However,
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001371 // almost no one actually writes code like this. If we see one of these,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001372 // it is much more likely that someone missed a semi colon and the
1373 // type/storage class specifier we're seeing is part of the *next*
1374 // intended declaration, as in:
1375 //
1376 // struct foo { ... }
1377 // typedef int X;
1378 //
1379 // We'd really like to emit a missing semicolon error instead of emitting
1380 // an error on the 'int' saying that you can't have two type specifiers in
1381 // the same declaration of X. Because of this, we look ahead past this
1382 // token to see if it's a type specifier. If so, we know the code is
1383 // otherwise invalid, so we can produce the expected semi error.
1384 if (!isKnownToBeTypeSpecifier(NextToken()))
1385 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001386 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001387
1388 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001389 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001390 if (!getLang().CPlusPlus)
1391 ExpectedSemi = false;
1392 break;
1393 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001394
Richard Smithcf6b0a22011-07-14 21:35:26 +00001395 // C++ [temp]p3 In a template-declaration which defines a class, no
1396 // declarator is permitted.
1397 if (TemplateInfo.Kind)
1398 ExpectedSemi = true;
1399
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001400 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001401 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1402 TagType == DeclSpec::TST_class ? "class"
1403 : TagType == DeclSpec::TST_struct? "struct" : "union");
1404 // Push this token back into the preprocessor and change our current token
1405 // to ';' so that the rest of the code recovers as though there were an
1406 // ';' after the definition.
1407 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001408 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001409 }
1410 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001411}
1412
Mike Stump1eb44332009-09-09 15:08:12 +00001413/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001414///
1415/// base-clause : [C++ class.derived]
1416/// ':' base-specifier-list
1417/// base-specifier-list:
1418/// base-specifier '...'[opt]
1419/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001420void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001421 assert(Tok.is(tok::colon) && "Not a base clause");
1422 ConsumeToken();
1423
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001424 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001425 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001426
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001427 while (true) {
1428 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001429 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001430 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001431 // Skip the rest of this base specifier, up until the comma or
1432 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001433 SkipUntil(tok::comma, tok::l_brace, true, true);
1434 } else {
1435 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001436 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001437 }
1438
1439 // If the next token is a comma, consume it and keep reading
1440 // base-specifiers.
1441 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001442
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001443 // Consume the comma.
1444 ConsumeToken();
1445 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001446
1447 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001448 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001449}
1450
1451/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1452/// one entry in the base class list of a class specifier, for example:
1453/// class foo : public bar, virtual private baz {
1454/// 'public bar' and 'virtual private baz' are each base-specifiers.
1455///
1456/// base-specifier: [C++ class.derived]
1457/// ::[opt] nested-name-specifier[opt] class-name
1458/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001459/// base-type-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001460/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001461/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001462Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001463 bool IsVirtual = false;
1464 SourceLocation StartLoc = Tok.getLocation();
1465
1466 // Parse the 'virtual' keyword.
1467 if (Tok.is(tok::kw_virtual)) {
1468 ConsumeToken();
1469 IsVirtual = true;
1470 }
1471
1472 // Parse an (optional) access specifier.
1473 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001474 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001475 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001477 // Parse the 'virtual' keyword (again!), in case it came after the
1478 // access specifier.
1479 if (Tok.is(tok::kw_virtual)) {
1480 SourceLocation VirtualLoc = ConsumeToken();
1481 if (IsVirtual) {
1482 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001483 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001484 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001485 }
1486
1487 IsVirtual = true;
1488 }
1489
Douglas Gregor42a552f2008-11-05 20:51:48 +00001490 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001491 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001492 SourceLocation BaseLoc;
1493 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001494 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001495 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001497 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1498 // actually part of the base-specifier-list grammar productions, but we
1499 // parse it here for convenience.
1500 SourceLocation EllipsisLoc;
1501 if (Tok.is(tok::ellipsis))
1502 EllipsisLoc = ConsumeToken();
1503
Mike Stump1eb44332009-09-09 15:08:12 +00001504 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001505 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001507 // Notify semantic analysis that we have parsed a complete
1508 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001509 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001510 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001511}
1512
1513/// getAccessSpecifierIfPresent - Determine whether the next token is
1514/// a C++ access-specifier.
1515///
1516/// access-specifier: [C++ class.derived]
1517/// 'private'
1518/// 'protected'
1519/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001520AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001521 switch (Tok.getKind()) {
1522 default: return AS_none;
1523 case tok::kw_private: return AS_private;
1524 case tok::kw_protected: return AS_protected;
1525 case tok::kw_public: return AS_public;
1526 }
1527}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001528
Eli Friedmand33133c2009-07-22 21:45:50 +00001529void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
John McCalld226f652010-08-21 09:40:31 +00001530 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001531 // We just declared a member function. If this member function
1532 // has any default arguments, we'll need to parse them later.
1533 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001534 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001535 = DeclaratorInfo.getFunctionTypeInfo();
Eli Friedmand33133c2009-07-22 21:45:50 +00001536 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1537 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1538 if (!LateMethod) {
1539 // Push this method onto the stack of late-parsed method
1540 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001541 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1542 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001543 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001544
1545 // Add all of the parameters prior to this one (they don't
1546 // have default arguments).
1547 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1548 for (unsigned I = 0; I < ParamIdx; ++I)
1549 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001550 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001551 }
1552
1553 // Add this parameter to the list of parameters (it or may
1554 // not have a default argument).
1555 LateMethod->DefaultArgs.push_back(
1556 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1557 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1558 }
1559 }
1560}
1561
Richard Smith1c94c162012-01-09 22:31:44 +00001562/// isCXX0XVirtSpecifier - Determine whether the given token is a C++0x
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001563/// virt-specifier.
1564///
1565/// virt-specifier:
1566/// override
1567/// final
Richard Smith1c94c162012-01-09 22:31:44 +00001568VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier(const Token &Tok) const {
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001569 if (!getLang().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001570 return VirtSpecifiers::VS_None;
1571
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001572 if (Tok.is(tok::identifier)) {
1573 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001574
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001575 // Initialize the contextual keywords.
1576 if (!Ident_final) {
1577 Ident_final = &PP.getIdentifierTable().get("final");
1578 Ident_override = &PP.getIdentifierTable().get("override");
1579 }
1580
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001581 if (II == Ident_override)
1582 return VirtSpecifiers::VS_Override;
1583
1584 if (II == Ident_final)
1585 return VirtSpecifiers::VS_Final;
1586 }
1587
1588 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001589}
1590
1591/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1592///
1593/// virt-specifier-seq:
1594/// virt-specifier
1595/// virt-specifier-seq virt-specifier
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001596void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001597 while (true) {
Anders Carlssoncc54d592011-01-22 16:56:46 +00001598 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001599 if (Specifier == VirtSpecifiers::VS_None)
1600 return;
1601
1602 // C++ [class.mem]p8:
1603 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001604 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001605 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001606 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1607 << PrevSpec
1608 << FixItHint::CreateRemoval(Tok.getLocation());
1609
Richard Smith7fe62082011-10-15 05:09:34 +00001610 Diag(Tok.getLocation(), getLang().CPlusPlus0x ?
1611 diag::warn_cxx98_compat_override_control_keyword :
1612 diag::ext_override_control_keyword)
1613 << VirtSpecifiers::getSpecifierName(Specifier);
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001614 ConsumeToken();
1615 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001616}
1617
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001618/// isCXX0XFinalKeyword - Determine whether the next token is a C++0x
1619/// contextual 'final' keyword.
1620bool Parser::isCXX0XFinalKeyword() const {
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001621 if (!getLang().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001622 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001623
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001624 if (!Tok.is(tok::identifier))
1625 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001626
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001627 // Initialize the contextual keywords.
1628 if (!Ident_final) {
1629 Ident_final = &PP.getIdentifierTable().get("final");
1630 Ident_override = &PP.getIdentifierTable().get("override");
1631 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001632
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001633 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001634}
1635
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001636/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1637///
1638/// member-declaration:
1639/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1640/// function-definition ';'[opt]
1641/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1642/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001643/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001644/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001645/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001646///
1647/// member-declarator-list:
1648/// member-declarator
1649/// member-declarator-list ',' member-declarator
1650///
1651/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001652/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001653/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001654/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001655/// identifier[opt] ':' constant-expression
1656///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001657/// virt-specifier-seq:
1658/// virt-specifier
1659/// virt-specifier-seq virt-specifier
1660///
1661/// virt-specifier:
1662/// override
1663/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001664///
Sebastian Redle2b68332009-04-12 17:16:29 +00001665/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001666/// '= 0'
1667///
1668/// constant-initializer:
1669/// '=' constant-expression
1670///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001671void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001672 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001673 const ParsedTemplateInfo &TemplateInfo,
1674 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001675 if (Tok.is(tok::at)) {
1676 if (getLang().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
1677 Diag(Tok, diag::err_at_defs_cxx);
1678 else
1679 Diag(Tok, diag::err_at_in_class);
1680
1681 ConsumeToken();
1682 SkipUntil(tok::r_brace);
1683 return;
1684 }
1685
John McCall60fa3cf2009-12-11 02:10:03 +00001686 // Access declarations.
1687 if (!TemplateInfo.Kind &&
1688 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001689 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001690 Tok.is(tok::annot_cxxscope)) {
1691 bool isAccessDecl = false;
1692 if (NextToken().is(tok::identifier))
1693 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1694 else
1695 isAccessDecl = NextToken().is(tok::kw_operator);
1696
1697 if (isAccessDecl) {
1698 // Collect the scope specifier token we annotated earlier.
1699 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001700 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1701 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001702
1703 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001704 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001705 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001706 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1707 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001708 SkipUntil(tok::semi);
1709 return;
1710 }
1711
1712 // TODO: recover from mistakenly-qualified operator declarations.
1713 if (ExpectAndConsume(tok::semi,
1714 diag::err_expected_semi_after,
1715 "access declaration",
1716 tok::semi))
1717 return;
1718
Douglas Gregor23c94db2010-07-02 17:43:08 +00001719 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001720 false, SourceLocation(),
1721 SS, Name,
1722 /* AttrList */ 0,
1723 /* IsTypeName */ false,
1724 SourceLocation());
1725 return;
1726 }
1727 }
1728
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001729 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001730 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001731 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001732 SourceLocation DeclEnd;
1733 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001734 return;
1735 }
Mike Stump1eb44332009-09-09 15:08:12 +00001736
Chris Lattner682bf922009-03-29 16:50:03 +00001737 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001738 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001739 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001740 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001741 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001742 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001743 return;
1744 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001745
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001746 // Handle: member-declaration ::= '__extension__' member-declaration
1747 if (Tok.is(tok::kw___extension__)) {
1748 // __extension__ silences extension warnings in the subexpression.
1749 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1750 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001751 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1752 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001753 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001754
Chris Lattner4ed5d912010-02-02 01:23:29 +00001755 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1756 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001757 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001758
John McCall0b7e6782011-03-24 11:26:52 +00001759 ParsedAttributesWithRange attrs(AttrFactory);
Sean Huntbbd37c62009-11-21 08:43:09 +00001760 // Optional C++0x attribute-specifier
John McCall7f040a92010-12-24 02:08:15 +00001761 MaybeParseCXX0XAttributes(attrs);
1762 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001763
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001764 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00001765 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001767 // Eat 'using'.
1768 SourceLocation UsingLoc = ConsumeToken();
1769
1770 if (Tok.is(tok::kw_namespace)) {
1771 Diag(UsingLoc, diag::err_using_namespace_in_class);
1772 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001773 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001774 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001775 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00001776 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1777 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001778 }
1779 return;
1780 }
1781
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001782 // decl-specifier-seq:
1783 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001784 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00001785 DS.takeAttributesFrom(attrs);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001786 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001787
John McCallf312b1e2010-08-26 23:41:50 +00001788 MultiTemplateParamsArg TemplateParams(Actions,
John McCalldd4a3b02009-09-16 22:47:08 +00001789 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1790 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1791
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001792 if (Tok.is(tok::semi)) {
1793 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001794 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00001795 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00001796 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001797 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001798 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001799
John McCall54abf7d2009-11-04 02:18:39 +00001800 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00001801 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001802
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001803 // Hold late-parsed attributes so we can attach a Decl to them later.
1804 LateParsedAttrList LateParsedAttrs;
1805
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001806 SourceLocation EqualLoc;
1807 bool HasInitializer = false;
1808 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001809 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001810 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1811 ColonProtectionRAIIObject X(*this);
1812
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001813 // Parse the first declarator.
1814 ParseDeclarator(DeclaratorInfo);
1815 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001816 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001817 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00001818 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001819 if (Tok.is(tok::semi))
1820 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001821 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001822 }
1823
Nico Weber48673472011-01-28 06:07:34 +00001824 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1825
John Thompson1b2fc0f2009-11-25 22:58:06 +00001826 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001827 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00001828
Francois Pichet6a247472011-05-11 02:14:46 +00001829 // MSVC permits pure specifier on inline functions declared at class scope.
1830 // Hence check for =0 before checking for function definition.
Francois Pichet62ec1f22011-09-17 17:15:52 +00001831 if (getLang().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00001832 DeclaratorInfo.isFunctionDeclarator() &&
1833 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001834 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00001835 Init = ParseInitializer();
1836 if (Init.isInvalid())
1837 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001838 else
1839 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00001840 }
1841
Douglas Gregor45fa5602011-11-07 20:56:01 +00001842 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001843 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00001844 //
1845 // In C++11, a non-function declarator followed by an open brace is a
1846 // braced-init-list for an in-class member initialization, not an
1847 // erroneous function definition.
1848 if (Tok.is(tok::l_brace) && !getLang().CPlusPlus0x) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00001849 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00001850 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00001851 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00001852 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00001853 } else if (Tok.is(tok::equal)) {
1854 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00001855 if (KW.is(tok::kw_default))
1856 DefinitionKind = FDK_Defaulted;
1857 else if (KW.is(tok::kw_delete))
1858 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00001859 }
1860 }
1861
Douglas Gregor45fa5602011-11-07 20:56:01 +00001862 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001863 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00001864 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001865 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00001866 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001867
1868 // Consume the optional ';'
1869 if (Tok.is(tok::semi))
1870 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001871 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001872 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001873
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001874 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00001875 Diag(DeclaratorInfo.getIdentifierLoc(),
1876 diag::err_function_declared_typedef);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001877 // This recovery skips the entire function body. It would be nice
1878 // to simply call ParseCXXInlineMethodDef() below, however Sema
1879 // assumes the declarator represents a function, not a typedef.
1880 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00001881 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001882
1883 // Consume the optional ';'
1884 if (Tok.is(tok::semi))
1885 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001886 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001887 }
1888
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001889 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001890 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00001891 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001892
1893 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
1894 LateParsedAttrs[i]->setDecl(FunDecl);
1895 }
1896 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00001897
1898 // Consume the ';' - it's optional unless we have a delete or default
1899 if (Tok.is(tok::semi)) {
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001900 ConsumeToken();
Sean Hunte4246a62011-05-12 06:15:49 +00001901 }
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001902
Chris Lattner682bf922009-03-29 16:50:03 +00001903 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001904 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001905 }
1906
1907 // member-declarator-list:
1908 // member-declarator
1909 // member-declarator-list ',' member-declarator
1910
Chris Lattner5f9e2722011-07-23 10:55:15 +00001911 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00001912 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00001913 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001914
1915 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001916 // member-declarator:
1917 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001918 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001919 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001920 if (Tok.is(tok::colon)) {
1921 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001922 BitfieldSize = ParseConstantExpression();
1923 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001924 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001925 }
Mike Stump1eb44332009-09-09 15:08:12 +00001926
Chris Lattnere6563252010-06-13 05:34:18 +00001927 // If a simple-asm-expr is present, parse it.
1928 if (Tok.is(tok::kw_asm)) {
1929 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001930 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00001931 if (AsmLabel.isInvalid())
1932 SkipUntil(tok::comma, true, true);
1933
1934 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1935 DeclaratorInfo.SetRangeEnd(Loc);
1936 }
1937
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001938 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001939 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001940
Richard Smith7a614d82011-06-11 17:19:42 +00001941 // FIXME: When g++ adds support for this, we'll need to check whether it
1942 // goes before or after the GNU attributes and __asm__.
1943 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1944
1945 bool HasDeferredInitializer = false;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001946 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00001947 if (BitfieldSize.get()) {
1948 Diag(Tok, diag::err_bitfield_member_init);
1949 SkipUntil(tok::comma, true, true);
1950 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00001951 HasInitializer = true;
Douglas Gregor555f57e2011-06-25 00:56:27 +00001952 HasDeferredInitializer = !DeclaratorInfo.isDeclarationOfFunction() &&
Richard Smith7a614d82011-06-11 17:19:42 +00001953 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smithc2cdd532011-06-12 11:43:46 +00001954 != DeclSpec::SCS_static &&
1955 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
1956 != DeclSpec::SCS_typedef;
Richard Smith7a614d82011-06-11 17:19:42 +00001957 }
1958 }
1959
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001960 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001961 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001962 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001963
John McCalld226f652010-08-21 09:40:31 +00001964 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00001965 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001966 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00001967 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCallbbbcdd92009-09-11 21:02:39 +00001968 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001969 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001970 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00001971 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001972 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001973 BitfieldSize.release(),
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00001974 VS, HasDeferredInitializer);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001975 if (AccessAttrs)
1976 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
1977 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001978 }
Douglas Gregor147545d2011-10-10 14:49:18 +00001979
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001980 // Set the Decl for any late parsed attributes
1981 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
1982 LateParsedAttrs[i]->setDecl(ThisDecl);
1983 }
1984 LateParsedAttrs.clear();
1985
Douglas Gregor147545d2011-10-10 14:49:18 +00001986 // Handle the initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00001987 if (HasDeferredInitializer) {
Douglas Gregor147545d2011-10-10 14:49:18 +00001988 // The initializer was deferred; parse it and cache the tokens.
Richard Smith7fe62082011-10-15 05:09:34 +00001989 Diag(Tok, getLang().CPlusPlus0x ?
1990 diag::warn_cxx98_compat_nonstatic_member_init :
1991 diag::ext_nonstatic_member_init);
1992
Richard Smith7a614d82011-06-11 17:19:42 +00001993 if (DeclaratorInfo.isArrayOfUnknownBound()) {
1994 // C++0x [dcl.array]p3: An array bound may also be omitted when the
1995 // declarator is followed by an initializer.
1996 //
1997 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00001998 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00001999 Diag(Tok, diag::err_incomplete_array_member_init);
2000 SkipUntil(tok::comma, true, true);
David Blaikie3164c142012-02-14 09:00:46 +00002001 if (ThisDecl)
2002 // Avoid later warnings about a class member of incomplete type.
2003 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002004 } else
2005 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002006 } else if (HasInitializer) {
2007 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002008 if (!Init.isUsable())
Douglas Gregor552e2992012-02-21 02:22:07 +00002009 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002010 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2011
Douglas Gregor147545d2011-10-10 14:49:18 +00002012 if (Init.isInvalid())
2013 SkipUntil(tok::comma, true, true);
2014 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002015 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002016 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor147545d2011-10-10 14:49:18 +00002017 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2018 // No initializer.
2019 Actions.ActOnUninitializedDecl(ThisDecl,
2020 DS.getTypeSpecType() == DeclSpec::TST_auto);
Richard Smith7a614d82011-06-11 17:19:42 +00002021 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002022
2023 if (ThisDecl) {
2024 Actions.FinalizeDeclaration(ThisDecl);
2025 DeclsInGroup.push_back(ThisDecl);
2026 }
2027
2028 if (DeclaratorInfo.isFunctionDeclarator() &&
2029 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2030 != DeclSpec::SCS_typedef) {
2031 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
2032 }
2033
2034 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002035
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002036 // If we don't have a comma, it is either the end of the list (a ';')
2037 // or an error, bail out.
2038 if (Tok.isNot(tok::comma))
2039 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002040
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002041 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002042 SourceLocation CommaLoc = ConsumeToken();
2043
2044 if (Tok.isAtStartOfLine() &&
2045 !MightBeDeclarator(Declarator::MemberContext)) {
2046 // This comma was followed by a line-break and something which can't be
2047 // the start of a declarator. The comma was probably a typo for a
2048 // semicolon.
2049 Diag(CommaLoc, diag::err_expected_semi_declaration)
2050 << FixItHint::CreateReplacement(CommaLoc, ";");
2051 ExpectSemi = false;
2052 break;
2053 }
Mike Stump1eb44332009-09-09 15:08:12 +00002054
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002055 // Parse the next declarator.
2056 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002057 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002058 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002059 Init = true;
2060 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002061 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002062
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002063 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002064 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002065
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002066 if (Tok.isNot(tok::colon))
2067 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002068 }
2069
Richard Smith1c94c162012-01-09 22:31:44 +00002070 if (ExpectSemi &&
2071 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002072 // Skip to end of block or statement.
2073 SkipUntil(tok::r_brace, true, true);
2074 // If we stopped at a ';', eat it.
2075 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002076 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002077 }
2078
Douglas Gregor23c94db2010-07-02 17:43:08 +00002079 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00002080 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002081}
2082
Richard Smith7a614d82011-06-11 17:19:42 +00002083/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2084/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2085/// function definition. The location of the '=', if any, will be placed in
2086/// EqualLoc.
2087///
2088/// pure-specifier:
2089/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002090///
Richard Smith7a614d82011-06-11 17:19:42 +00002091/// brace-or-equal-initializer:
2092/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002093/// braced-init-list
2094///
Richard Smith7a614d82011-06-11 17:19:42 +00002095/// initializer-clause:
2096/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002097/// braced-init-list
2098///
Richard Smith7a614d82011-06-11 17:19:42 +00002099/// defaulted/deleted function-definition:
2100/// '=' 'default'
2101/// '=' 'delete'
2102///
2103/// Prior to C++0x, the assignment-expression in an initializer-clause must
2104/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002105ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002106 SourceLocation &EqualLoc) {
2107 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2108 && "Data member initializer not starting with '=' or '{'");
2109
Douglas Gregor552e2992012-02-21 02:22:07 +00002110 EnterExpressionEvaluationContext Context(Actions,
2111 Sema::PotentiallyEvaluated,
2112 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002113 if (Tok.is(tok::equal)) {
2114 EqualLoc = ConsumeToken();
2115 if (Tok.is(tok::kw_delete)) {
2116 // In principle, an initializer of '= delete p;' is legal, but it will
2117 // never type-check. It's better to diagnose it as an ill-formed expression
2118 // than as an ill-formed deleted non-function member.
2119 // An initializer of '= delete p, foo' will never be parsed, because
2120 // a top-level comma always ends the initializer expression.
2121 const Token &Next = NextToken();
2122 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2123 Next.is(tok::eof)) {
2124 if (IsFunction)
2125 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2126 << 1 /* delete */;
2127 else
2128 Diag(ConsumeToken(), diag::err_deleted_non_function);
2129 return ExprResult();
2130 }
2131 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002132 if (IsFunction)
2133 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2134 << 0 /* default */;
2135 else
2136 Diag(ConsumeToken(), diag::err_default_special_members);
2137 return ExprResult();
2138 }
2139
Sebastian Redl33deb352012-02-22 10:50:08 +00002140 }
2141 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002142}
2143
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002144/// ParseCXXMemberSpecification - Parse the class definition.
2145///
2146/// member-specification:
2147/// member-declaration member-specification[opt]
2148/// access-specifier ':' member-specification[opt]
2149///
2150void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002151 unsigned TagType, Decl *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00002152 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002153 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00002154 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002155
John McCallf312b1e2010-08-26 23:41:50 +00002156 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2157 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002158
Douglas Gregor26997fd2010-01-16 20:52:59 +00002159 // Determine whether this is a non-nested class. Note that local
2160 // classes are *not* considered to be nested classes.
2161 bool NonNestedClass = true;
2162 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002163 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002164 if (S->isClassScope()) {
2165 // We're inside a class scope, so this is a nested class.
2166 NonNestedClass = false;
2167 break;
2168 }
2169
2170 if ((S->getFlags() & Scope::FnScope)) {
2171 // If we're in a function or function template declared in the
2172 // body of a class, then this is a local class rather than a
2173 // nested class.
2174 const Scope *Parent = S->getParent();
2175 if (Parent->isTemplateParamScope())
2176 Parent = Parent->getParent();
2177 if (Parent->isClassScope())
2178 break;
2179 }
2180 }
2181 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002182
2183 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002184 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002185
Douglas Gregor6569d682009-05-27 23:11:45 +00002186 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00002187 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00002188
Douglas Gregorddc29e12009-02-06 22:42:48 +00002189 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002190 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002191
Anders Carlssonb184a182011-03-25 14:46:08 +00002192 SourceLocation FinalLoc;
2193
2194 // Parse the optional 'final' keyword.
2195 if (getLang().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith8b11b5e2011-10-15 04:21:46 +00002196 assert(isCXX0XFinalKeyword() && "not a class definition");
2197 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002198
Richard Smith7fe62082011-10-15 05:09:34 +00002199 Diag(FinalLoc, getLang().CPlusPlus0x ?
2200 diag::warn_cxx98_compat_override_control_keyword :
2201 diag::ext_override_control_keyword) << "final";
Anders Carlssonb184a182011-03-25 14:46:08 +00002202 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002203
John McCallbd0dfa52009-12-19 21:48:58 +00002204 if (Tok.is(tok::colon)) {
2205 ParseBaseClause(TagDecl);
2206
2207 if (!Tok.is(tok::l_brace)) {
2208 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002209
2210 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002211 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002212 return;
2213 }
2214 }
2215
2216 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002217 BalancedDelimiterTracker T(*this, tok::l_brace);
2218 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002219
John McCall42a4f662010-05-28 08:11:17 +00002220 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002221 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002222 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002223
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002224 // C++ 11p3: Members of a class defined with the keyword class are private
2225 // by default. Members of a class defined with the keywords struct or union
2226 // are public by default.
2227 AccessSpecifier CurAS;
2228 if (TagType == DeclSpec::TST_class)
2229 CurAS = AS_private;
2230 else
2231 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002232 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002233
Douglas Gregor07976d22010-06-21 22:31:09 +00002234 if (TagDecl) {
2235 // While we still have something to read, read the member-declarations.
2236 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2237 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002238
Francois Pichet62ec1f22011-09-17 17:15:52 +00002239 if (getLang().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002240 Tok.is(tok::kw___if_not_exists))) {
2241 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2242 continue;
2243 }
2244
Douglas Gregor07976d22010-06-21 22:31:09 +00002245 // Check for extraneous top-level semicolon.
2246 if (Tok.is(tok::semi)) {
2247 Diag(Tok, diag::ext_extra_struct_semi)
2248 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
2249 << FixItHint::CreateRemoval(Tok.getLocation());
2250 ConsumeToken();
2251 continue;
2252 }
2253
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002254 if (Tok.is(tok::annot_pragma_vis)) {
2255 HandlePragmaVisibility();
2256 continue;
2257 }
2258
2259 if (Tok.is(tok::annot_pragma_pack)) {
2260 HandlePragmaPack();
2261 continue;
2262 }
2263
Douglas Gregor07976d22010-06-21 22:31:09 +00002264 AccessSpecifier AS = getAccessSpecifierIfPresent();
2265 if (AS != AS_none) {
2266 // Current token is a C++ access specifier.
2267 CurAS = AS;
2268 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002269 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002270 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002271 AccessAttrs.clear();
2272 MaybeParseGNUAttributes(AccessAttrs);
2273
David Blaikie13f8daf2011-10-13 06:08:43 +00002274 SourceLocation EndLoc;
2275 if (Tok.is(tok::colon)) {
2276 EndLoc = Tok.getLocation();
2277 ConsumeToken();
2278 } else if (Tok.is(tok::semi)) {
2279 EndLoc = Tok.getLocation();
2280 ConsumeToken();
2281 Diag(EndLoc, diag::err_expected_colon)
2282 << FixItHint::CreateReplacement(EndLoc, ":");
2283 } else {
2284 EndLoc = ASLoc.getLocWithOffset(TokLength);
2285 Diag(EndLoc, diag::err_expected_colon)
2286 << FixItHint::CreateInsertion(EndLoc, ":");
2287 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002288
2289 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2290 AccessAttrs.getList())) {
2291 // found another attribute than only annotations
2292 AccessAttrs.clear();
2293 }
2294
Douglas Gregor07976d22010-06-21 22:31:09 +00002295 continue;
2296 }
2297
2298 // FIXME: Make sure we don't have a template here.
2299
2300 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002301 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002302 }
2303
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002304 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002305 } else {
2306 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002307 }
Mike Stump1eb44332009-09-09 15:08:12 +00002308
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002309 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002310 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002311 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002312
John McCall42a4f662010-05-28 08:11:17 +00002313 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002314 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002315 T.getOpenLocation(),
2316 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002317 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002318
Richard Smith7a614d82011-06-11 17:19:42 +00002319 // C++0x [class.mem]p2: Within the class member-specification, the class is
2320 // regarded as complete within function bodies, default arguments, exception-
2321 // specifications, and brace-or-equal-initializers for non-static data
2322 // members (including such things in nested classes).
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002323 //
Richard Smith7a614d82011-06-11 17:19:42 +00002324 // FIXME: Only function bodies and brace-or-equal-initializers are currently
2325 // handled. Fix the others!
Douglas Gregor07976d22010-06-21 22:31:09 +00002326 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002327 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002328 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002329 // declarations and the lexed inline method definitions, along with any
2330 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002331 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002332 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002333 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith7a614d82011-06-11 17:19:42 +00002334 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002335 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002336 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002337 }
2338
John McCall42a4f662010-05-28 08:11:17 +00002339 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002340 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2341 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002342
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002343 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002344 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002345 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002346}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002347
2348/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2349/// which explicitly initializes the members or base classes of a
2350/// class (C++ [class.base.init]). For example, the three initializers
2351/// after the ':' in the Derived constructor below:
2352///
2353/// @code
2354/// class Base { };
2355/// class Derived : Base {
2356/// int x;
2357/// float f;
2358/// public:
2359/// Derived(float f) : Base(), x(17), f(f) { }
2360/// };
2361/// @endcode
2362///
Mike Stump1eb44332009-09-09 15:08:12 +00002363/// [C++] ctor-initializer:
2364/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002365///
Mike Stump1eb44332009-09-09 15:08:12 +00002366/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002367/// mem-initializer ...[opt]
2368/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002369void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002370 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2371
John Wiegley28bbe4b2011-04-28 01:08:34 +00002372 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2373 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002374 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002375
Chris Lattner5f9e2722011-07-23 10:55:15 +00002376 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002377 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002378
Douglas Gregor7ad83902008-11-05 04:29:56 +00002379 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002380 if (Tok.is(tok::code_completion)) {
2381 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2382 MemInitializers.data(),
2383 MemInitializers.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002384 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002385 } else {
2386 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2387 if (!MemInit.isInvalid())
2388 MemInitializers.push_back(MemInit.get());
2389 else
2390 AnyErrors = true;
2391 }
2392
Douglas Gregor7ad83902008-11-05 04:29:56 +00002393 if (Tok.is(tok::comma))
2394 ConsumeToken();
2395 else if (Tok.is(tok::l_brace))
2396 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002397 // If the next token looks like a base or member initializer, assume that
2398 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002399 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2400 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2401 Diag(Loc, diag::err_ctor_init_missing_comma)
2402 << FixItHint::CreateInsertion(Loc, ", ");
2403 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002404 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002405 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002406 SkipUntil(tok::l_brace, true, true);
2407 break;
2408 }
2409 } while (true);
2410
Mike Stump1eb44332009-09-09 15:08:12 +00002411 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002412 MemInitializers.data(), MemInitializers.size(),
2413 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002414}
2415
2416/// ParseMemInitializer - Parse a C++ member initializer, which is
2417/// part of a constructor initializer that explicitly initializes one
2418/// member or base class (C++ [class.base.init]). See
2419/// ParseConstructorInitializer for an example.
2420///
2421/// [C++] mem-initializer:
2422/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002423/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002424///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002425/// [C++] mem-initializer-id:
2426/// '::'[opt] nested-name-specifier[opt] class-name
2427/// identifier
John McCalld226f652010-08-21 09:40:31 +00002428Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002429 // parse '::'[opt] nested-name-specifier[opt]
2430 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002431 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002432 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002433 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002434 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002435 if (TemplateId->Kind == TNK_Type_template ||
2436 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002437 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002438 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002439 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002440 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002441 }
David Blaikief2116622012-01-24 06:03:59 +00002442 // Uses of decltype will already have been converted to annot_decltype by
2443 // ParseOptionalCXXScopeSpecifier at this point.
2444 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2445 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002446 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002447 return true;
2448 }
Mike Stump1eb44332009-09-09 15:08:12 +00002449
David Blaikief2116622012-01-24 06:03:59 +00002450 IdentifierInfo *II = 0;
2451 DeclSpec DS(AttrFactory);
2452 SourceLocation IdLoc = Tok.getLocation();
2453 if (Tok.is(tok::annot_decltype)) {
2454 // Get the decltype expression, if there is one.
2455 ParseDecltypeSpecifier(DS);
2456 } else {
2457 if (Tok.is(tok::identifier))
2458 // Get the identifier. This may be a member name or a class name,
2459 // but we'll let the semantic analysis determine which it is.
2460 II = Tok.getIdentifierInfo();
2461 ConsumeToken();
2462 }
2463
Douglas Gregor7ad83902008-11-05 04:29:56 +00002464
2465 // Parse the '('.
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002466 if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002467 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2468
Sebastian Redl6df65482011-09-24 17:48:25 +00002469 ExprResult InitList = ParseBraceInitializer();
2470 if (InitList.isInvalid())
2471 return true;
2472
2473 SourceLocation EllipsisLoc;
2474 if (Tok.is(tok::ellipsis))
2475 EllipsisLoc = ConsumeToken();
2476
2477 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002478 TemplateTypeTy, DS, IdLoc,
2479 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002480 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002481 BalancedDelimiterTracker T(*this, tok::l_paren);
2482 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002483
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002484 // Parse the optional expression-list.
2485 ExprVector ArgExprs(Actions);
2486 CommaLocsTy CommaLocs;
2487 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2488 SkipUntil(tok::r_paren);
2489 return true;
2490 }
2491
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002492 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002493
2494 SourceLocation EllipsisLoc;
2495 if (Tok.is(tok::ellipsis))
2496 EllipsisLoc = ConsumeToken();
2497
2498 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002499 TemplateTypeTy, DS, IdLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002500 T.getOpenLocation(), ArgExprs.take(),
2501 ArgExprs.size(), T.getCloseLocation(),
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002502 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002503 }
2504
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002505 Diag(Tok, getLang().CPlusPlus0x ? diag::err_expected_lparen_or_lbrace
2506 : diag::err_expected_lparen);
2507 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002508}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002509
Sebastian Redl7acafd02011-03-05 14:45:16 +00002510/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002511///
Douglas Gregora4745612008-12-01 18:00:20 +00002512/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002513/// dynamic-exception-specification
2514/// noexcept-specification
2515///
2516/// noexcept-specification:
2517/// 'noexcept'
2518/// 'noexcept' '(' constant-expression ')'
2519ExceptionSpecificationType
2520Parser::MaybeParseExceptionSpecification(SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002521 SmallVectorImpl<ParsedType> &DynamicExceptions,
2522 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Sebastian Redl7acafd02011-03-05 14:45:16 +00002523 ExprResult &NoexceptExpr) {
2524 ExceptionSpecificationType Result = EST_None;
2525
2526 // See if there's a dynamic specification.
2527 if (Tok.is(tok::kw_throw)) {
2528 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2529 DynamicExceptions,
2530 DynamicExceptionRanges);
2531 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2532 "Produced different number of exception types and ranges.");
2533 }
2534
2535 // If there's no noexcept specification, we're done.
2536 if (Tok.isNot(tok::kw_noexcept))
2537 return Result;
2538
Richard Smith841804b2011-10-17 23:06:20 +00002539 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2540
Sebastian Redl7acafd02011-03-05 14:45:16 +00002541 // If we already had a dynamic specification, parse the noexcept for,
2542 // recovery, but emit a diagnostic and don't store the results.
2543 SourceRange NoexceptRange;
2544 ExceptionSpecificationType NoexceptType = EST_None;
2545
2546 SourceLocation KeywordLoc = ConsumeToken();
2547 if (Tok.is(tok::l_paren)) {
2548 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002549 BalancedDelimiterTracker T(*this, tok::l_paren);
2550 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002551 NoexceptType = EST_ComputedNoexcept;
2552 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002553 // The argument must be contextually convertible to bool. We use
2554 // ActOnBooleanCondition for this purpose.
2555 if (!NoexceptExpr.isInvalid())
2556 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2557 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002558 T.consumeClose();
2559 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002560 } else {
2561 // There is no argument.
2562 NoexceptType = EST_BasicNoexcept;
2563 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2564 }
2565
2566 if (Result == EST_None) {
2567 SpecificationRange = NoexceptRange;
2568 Result = NoexceptType;
2569
2570 // If there's a dynamic specification after a noexcept specification,
2571 // parse that and ignore the results.
2572 if (Tok.is(tok::kw_throw)) {
2573 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2574 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2575 DynamicExceptionRanges);
2576 }
2577 } else {
2578 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2579 }
2580
2581 return Result;
2582}
2583
2584/// ParseDynamicExceptionSpecification - Parse a C++
2585/// dynamic-exception-specification (C++ [except.spec]).
2586///
2587/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002588/// 'throw' '(' type-id-list [opt] ')'
2589/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002590///
Douglas Gregora4745612008-12-01 18:00:20 +00002591/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002592/// type-id ... [opt]
2593/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002594///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002595ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2596 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002597 SmallVectorImpl<ParsedType> &Exceptions,
2598 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002599 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002600
Sebastian Redl7acafd02011-03-05 14:45:16 +00002601 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002602 BalancedDelimiterTracker T(*this, tok::l_paren);
2603 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002604 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2605 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002606 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002607 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002608
Douglas Gregora4745612008-12-01 18:00:20 +00002609 // Parse throw(...), a Microsoft extension that means "this function
2610 // can throw anything".
2611 if (Tok.is(tok::ellipsis)) {
2612 SourceLocation EllipsisLoc = ConsumeToken();
Francois Pichet62ec1f22011-09-17 17:15:52 +00002613 if (!getLang().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002614 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002615 T.consumeClose();
2616 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002617 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002618 }
2619
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002620 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002621 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002622 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002623 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002624
Douglas Gregora04426c2010-12-20 23:57:46 +00002625 if (Tok.is(tok::ellipsis)) {
2626 // C++0x [temp.variadic]p5:
2627 // - In a dynamic-exception-specification (15.4); the pattern is a
2628 // type-id.
2629 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002630 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002631 if (!Res.isInvalid())
2632 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2633 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002634
Sebastian Redlef65f062009-05-29 18:02:33 +00002635 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002636 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002637 Ranges.push_back(Range);
2638 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002639
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002640 if (Tok.is(tok::comma))
2641 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002642 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002643 break;
2644 }
2645
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002646 T.consumeClose();
2647 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002648 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002649}
Douglas Gregor6569d682009-05-27 23:11:45 +00002650
Douglas Gregordab60ad2010-10-01 18:44:50 +00002651/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2652/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002653TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002654 assert(Tok.is(tok::arrow) && "expected arrow");
2655
2656 ConsumeToken();
2657
2658 // FIXME: Need to suppress declarations when parsing this typename.
2659 // Otherwise in this function definition:
2660 //
2661 // auto f() -> struct X {}
2662 //
2663 // struct X is parsed as class definition because of the trailing
2664 // brace.
Douglas Gregordab60ad2010-10-01 18:44:50 +00002665 return ParseTypeName(&Range);
2666}
2667
Douglas Gregor6569d682009-05-27 23:11:45 +00002668/// \brief We have just started parsing the definition of a new class,
2669/// so push that class onto our stack of classes that is currently
2670/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002671Sema::ParsingClassState
2672Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002673 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002674 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00002675 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
John McCalleee1d542011-02-14 07:13:47 +00002676 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002677}
2678
2679/// \brief Deallocate the given parsed class and all of its nested
2680/// classes.
2681void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002682 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2683 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002684 delete Class;
2685}
2686
2687/// \brief Pop the top class of the stack of classes that are
2688/// currently being parsed.
2689///
2690/// This routine should be called when we have finished parsing the
2691/// definition of a class, but have not yet popped the Scope
2692/// associated with the class's definition.
2693///
2694/// \returns true if the class we've popped is a top-level class,
2695/// false otherwise.
John McCalleee1d542011-02-14 07:13:47 +00002696void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002697 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002698
John McCalleee1d542011-02-14 07:13:47 +00002699 Actions.PopParsingClass(state);
2700
Douglas Gregor6569d682009-05-27 23:11:45 +00002701 ParsingClass *Victim = ClassStack.top();
2702 ClassStack.pop();
2703 if (Victim->TopLevelClass) {
2704 // Deallocate all of the nested classes of this class,
2705 // recursively: we don't need to keep any of this information.
2706 DeallocateParsedClasses(Victim);
2707 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002708 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002709 assert(!ClassStack.empty() && "Missing top-level class?");
2710
Douglas Gregord54eb442010-10-12 16:25:54 +00002711 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002712 // The victim is a nested class, but we will not need to perform
2713 // any processing after the definition of this class since it has
2714 // no members whose handling was delayed. Therefore, we can just
2715 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002716 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002717 return;
2718 }
2719
2720 // This nested class has some members that will need to be processed
2721 // after the top-level class is completely defined. Therefore, add
2722 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002723 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002724 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002725 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00002726}
Sean Huntbbd37c62009-11-21 08:43:09 +00002727
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002728/// ParseCXX0XAttributeSpecifier - Parse a C++0x attribute-specifier. Currently
2729/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00002730///
2731/// [C++0x] attribute-specifier:
2732/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002733/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00002734///
2735/// [C++0x] attribute-list:
2736/// attribute[opt]
2737/// attribute-list ',' attribute[opt]
2738///
2739/// [C++0x] attribute:
2740/// attribute-token attribute-argument-clause[opt]
2741///
2742/// [C++0x] attribute-token:
2743/// identifier
2744/// attribute-scoped-token
2745///
2746/// [C++0x] attribute-scoped-token:
2747/// attribute-namespace '::' identifier
2748///
2749/// [C++0x] attribute-namespace:
2750/// identifier
2751///
2752/// [C++0x] attribute-argument-clause:
2753/// '(' balanced-token-seq ')'
2754///
2755/// [C++0x] balanced-token-seq:
2756/// balanced-token
2757/// balanced-token-seq balanced-token
2758///
2759/// [C++0x] balanced-token:
2760/// '(' balanced-token-seq ')'
2761/// '[' balanced-token-seq ']'
2762/// '{' balanced-token-seq '}'
2763/// any token but '(', ')', '[', ']', '{', or '}'
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002764void Parser::ParseCXX0XAttributeSpecifier(ParsedAttributes &attrs,
2765 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002766 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00002767 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002768 ParseAlignmentSpecifier(attrs, endLoc);
2769 return;
2770 }
2771
Sean Huntbbd37c62009-11-21 08:43:09 +00002772 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
2773 && "Not a C++0x attribute list");
2774
Richard Smith41be6732011-10-14 20:48:27 +00002775 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
2776
Sean Huntbbd37c62009-11-21 08:43:09 +00002777 ConsumeBracket();
2778 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002779
Sean Huntbbd37c62009-11-21 08:43:09 +00002780 if (Tok.is(tok::comma)) {
2781 Diag(Tok.getLocation(), diag::err_expected_ident);
2782 ConsumeToken();
2783 }
2784
2785 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
2786 // attribute not present
2787 if (Tok.is(tok::comma)) {
2788 ConsumeToken();
2789 continue;
2790 }
2791
2792 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
2793 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002794
Sean Huntbbd37c62009-11-21 08:43:09 +00002795 // scoped attribute
2796 if (Tok.is(tok::coloncolon)) {
2797 ConsumeToken();
2798
2799 if (!Tok.is(tok::identifier)) {
2800 Diag(Tok.getLocation(), diag::err_expected_ident);
2801 SkipUntil(tok::r_square, tok::comma, true, true);
2802 continue;
2803 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002804
Sean Huntbbd37c62009-11-21 08:43:09 +00002805 ScopeName = AttrName;
2806 ScopeLoc = AttrLoc;
2807
2808 AttrName = Tok.getIdentifierInfo();
2809 AttrLoc = ConsumeToken();
2810 }
2811
2812 bool AttrParsed = false;
2813 // No scoped names are supported; ideally we could put all non-standard
2814 // attributes into namespaces.
2815 if (!ScopeName) {
2816 switch(AttributeList::getKind(AttrName))
2817 {
2818 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00002819 case AttributeList::AT_carries_dependency:
Anders Carlsson15e14a22011-01-23 21:33:18 +00002820 case AttributeList::AT_noreturn: {
Sean Huntbbd37c62009-11-21 08:43:09 +00002821 if (Tok.is(tok::l_paren)) {
2822 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
2823 << AttrName->getName();
2824 break;
2825 }
2826
John McCall0b7e6782011-03-24 11:26:52 +00002827 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc, 0,
2828 SourceLocation(), 0, 0, false, true);
Sean Huntbbd37c62009-11-21 08:43:09 +00002829 AttrParsed = true;
2830 break;
2831 }
2832
Sean Huntbbd37c62009-11-21 08:43:09 +00002833 // Silence warnings
2834 default: break;
2835 }
2836 }
2837
2838 // Skip the entire parameter clause, if any
2839 if (!AttrParsed && Tok.is(tok::l_paren)) {
2840 ConsumeParen();
2841 // SkipUntil maintains the balancedness of tokens.
2842 SkipUntil(tok::r_paren, false);
2843 }
2844 }
2845
2846 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2847 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002848 if (endLoc)
2849 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00002850 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2851 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002852}
Sean Huntbbd37c62009-11-21 08:43:09 +00002853
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002854/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier-seq.
2855///
2856/// attribute-specifier-seq:
2857/// attribute-specifier-seq[opt] attribute-specifier
2858void Parser::ParseCXX0XAttributes(ParsedAttributesWithRange &attrs,
2859 SourceLocation *endLoc) {
2860 SourceLocation StartLoc = Tok.getLocation(), Loc;
2861 if (!endLoc)
2862 endLoc = &Loc;
2863
Douglas Gregor8828ee72011-10-07 20:35:25 +00002864 do {
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002865 ParseCXX0XAttributeSpecifier(attrs, endLoc);
Douglas Gregor8828ee72011-10-07 20:35:25 +00002866 } while (isCXX0XAttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002867
2868 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002869}
2870
Francois Pichet334d47e2010-10-11 12:59:39 +00002871/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
2872///
2873/// [MS] ms-attribute:
2874/// '[' token-seq ']'
2875///
2876/// [MS] ms-attribute-seq:
2877/// ms-attribute[opt]
2878/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00002879void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
2880 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00002881 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
2882
2883 while (Tok.is(tok::l_square)) {
2884 ConsumeBracket();
2885 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00002886 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00002887 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
2888 }
2889}
Francois Pichet563a6452011-05-25 10:19:49 +00002890
2891void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
2892 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00002893 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00002894 if (ParseMicrosoftIfExistsCondition(Result))
2895 return;
2896
Douglas Gregor3896fc52011-10-24 22:31:10 +00002897 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2898 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00002899 Diag(Tok, diag::err_expected_lbrace);
2900 return;
2901 }
Francois Pichet563a6452011-05-25 10:19:49 +00002902
Douglas Gregor3896fc52011-10-24 22:31:10 +00002903 switch (Result.Behavior) {
2904 case IEB_Parse:
2905 // Parse the declarations below.
2906 break;
2907
2908 case IEB_Dependent:
2909 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
2910 << Result.IsIfExists;
2911 // Fall through to skip.
2912
2913 case IEB_Skip:
2914 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00002915 return;
2916 }
2917
Douglas Gregor3896fc52011-10-24 22:31:10 +00002918 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00002919 // __if_exists, __if_not_exists can nest.
2920 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
2921 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2922 continue;
2923 }
2924
2925 // Check for extraneous top-level semicolon.
2926 if (Tok.is(tok::semi)) {
2927 Diag(Tok, diag::ext_extra_struct_semi)
2928 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
2929 << FixItHint::CreateRemoval(Tok.getLocation());
2930 ConsumeToken();
2931 continue;
2932 }
2933
2934 AccessSpecifier AS = getAccessSpecifierIfPresent();
2935 if (AS != AS_none) {
2936 // Current token is a C++ access specifier.
2937 CurAS = AS;
2938 SourceLocation ASLoc = Tok.getLocation();
2939 ConsumeToken();
2940 if (Tok.is(tok::colon))
2941 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
2942 else
2943 Diag(Tok, diag::err_expected_colon);
2944 ConsumeToken();
2945 continue;
2946 }
2947
2948 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002949 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00002950 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00002951
2952 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00002953}