blob: c1ebf91ef84bbe7c81026423473d261d3a512d33 [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.
670 EnterExpressionEvaluationContext Unevaluated(Actions,
671 Sema::Unevaluated);
672 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
688 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000689 }
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000691 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000692 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000693 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000694 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
David Blaikie42d6d0c2011-12-04 05:04:18 +0000695 DiagID, Result.release())) {
John McCallfec54012009-08-03 20:12:06 +0000696 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000697 DS.SetTypeSpecError();
698 }
699 return EndLoc;
700}
701
702void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
703 SourceLocation StartLoc,
704 SourceLocation EndLoc) {
705 // make sure we have a token we can turn into an annotation token
706 if (PP.isBacktrackEnabled())
707 PP.RevertCachedTokens(1);
708 else
709 PP.EnterToken(Tok);
710
711 Tok.setKind(tok::annot_decltype);
712 setExprAnnotation(Tok, DS.getTypeSpecType() == TST_decltype ?
713 DS.getRepAsExpr() : ExprResult());
714 Tok.setAnnotationEndLoc(EndLoc);
715 Tok.setLocation(StartLoc);
716 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000717}
718
Sean Huntdb5d44b2011-05-19 05:37:45 +0000719void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
720 assert(Tok.is(tok::kw___underlying_type) &&
721 "Not an underlying type specifier");
722
723 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000724 BalancedDelimiterTracker T(*this, tok::l_paren);
725 if (T.expectAndConsume(diag::err_expected_lparen_after,
726 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000727 return;
728 }
729
730 TypeResult Result = ParseTypeName();
731 if (Result.isInvalid()) {
732 SkipUntil(tok::r_paren);
733 return;
734 }
735
736 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000737 T.consumeClose();
738 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000739 return;
740
741 const char *PrevSpec = 0;
742 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000743 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000744 DiagID, Result.release()))
745 Diag(StartLoc, DiagID) << PrevSpec;
746}
747
David Blaikie09048df2011-10-25 15:01:20 +0000748/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
749/// class name or decltype-specifier. Note that we only check that the result
750/// names a type; semantic analysis will need to verify that the type names a
751/// class. The result is either a type or null, depending on whether a type
752/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000753///
David Blaikie09048df2011-10-25 15:01:20 +0000754/// base-type-specifier: [C++ 10.1]
755/// class-or-decltype
756/// class-or-decltype: [C++ 10.1]
757/// nested-name-specifier[opt] class-name
758/// decltype-specifier
Douglas Gregor42a552f2008-11-05 20:51:48 +0000759/// class-name: [C++ 9.1]
760/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000761/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000762///
David Blaikie22216eb2011-10-25 17:10:12 +0000763Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
764 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000765 // Ignore attempts to use typename
766 if (Tok.is(tok::kw_typename)) {
767 Diag(Tok, diag::err_expected_class_name_not_template)
768 << FixItHint::CreateRemoval(Tok.getLocation());
769 ConsumeToken();
770 }
771
David Blaikie152aa4b2011-10-25 18:17:58 +0000772 // Parse optional nested-name-specifier
773 CXXScopeSpec SS;
774 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
775
776 BaseLoc = Tok.getLocation();
777
David Blaikie22216eb2011-10-25 17:10:12 +0000778 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000779 // tok == kw_decltype is just error recovery, it can only happen when SS
780 // isn't empty
781 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000782 if (SS.isNotEmpty())
783 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
784 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000785 // Fake up a Declarator to use with ActOnTypeName.
786 DeclSpec DS(AttrFactory);
787
David Blaikieb5777572011-12-08 04:53:15 +0000788 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000789
790 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
791 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
792 }
793
Douglas Gregor7f43d672009-02-25 23:52:28 +0000794 // Check whether we have a template-id that names a type.
795 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000796 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000797 if (TemplateId->Kind == TNK_Type_template ||
798 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000799 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000800
801 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000802 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000803 EndLocation = Tok.getAnnotationEndLoc();
804 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000805
806 if (Type)
807 return Type;
808 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000809 }
810
811 // Fall through to produce an error below.
812 }
813
Douglas Gregor42a552f2008-11-05 20:51:48 +0000814 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000815 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000816 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000817 }
818
Douglas Gregor84d0a192010-01-12 21:28:44 +0000819 IdentifierInfo *Id = Tok.getIdentifierInfo();
820 SourceLocation IdLoc = ConsumeToken();
821
822 if (Tok.is(tok::less)) {
823 // It looks the user intended to write a template-id here, but the
824 // template-name was wrong. Try to fix that.
825 TemplateNameKind TNK = TNK_Type_template;
826 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000827 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000828 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000829 Diag(IdLoc, diag::err_unknown_template_name)
830 << Id;
831 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000832
Douglas Gregor84d0a192010-01-12 21:28:44 +0000833 if (!Template)
834 return true;
835
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000836 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000837 UnqualifiedId TemplateName;
838 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000839
Douglas Gregor84d0a192010-01-12 21:28:44 +0000840 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000841 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
842 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000843 return true;
844 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000845 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000846
Douglas Gregor84d0a192010-01-12 21:28:44 +0000847 // If we didn't end up with a typename token, there's nothing more we
848 // can do.
849 if (Tok.isNot(tok::annot_typename))
850 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000851
Douglas Gregor84d0a192010-01-12 21:28:44 +0000852 // Retrieve the type from the annotation token, consume that token, and
853 // return.
854 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000855 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000856 ConsumeToken();
857 return Type;
858 }
859
Douglas Gregor42a552f2008-11-05 20:51:48 +0000860 // We have an identifier; check whether it is actually a type.
Douglas Gregor059101f2011-03-02 00:47:37 +0000861 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000862 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000863 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +0000864 /*NonTrivialTypeSourceInfo=*/true);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000865 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000866 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000867 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000868 }
869
870 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000871 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000872
873 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000874 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000875 DS.SetRangeStart(IdLoc);
876 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000877 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000878
879 const char *PrevSpec = 0;
880 unsigned DiagID;
881 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
882
883 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
884 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000885}
886
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000887/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
888/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
889/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000890/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000891///
892/// class-specifier: [C++ class]
893/// class-head '{' member-specification[opt] '}'
894/// class-head '{' member-specification[opt] '}' attributes[opt]
895/// class-head:
896/// class-key identifier[opt] base-clause[opt]
897/// class-key nested-name-specifier identifier base-clause[opt]
898/// class-key nested-name-specifier[opt] simple-template-id
899/// base-clause[opt]
900/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000901/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000902/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000903/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000904/// simple-template-id base-clause[opt]
905/// class-key:
906/// 'class'
907/// 'struct'
908/// 'union'
909///
910/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000911/// class-key ::[opt] nested-name-specifier[opt] identifier
912/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
913/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000914///
915/// Note that the C++ class-specifier and elaborated-type-specifier,
916/// together, subsume the C99 struct-or-union-specifier:
917///
918/// struct-or-union-specifier: [C99 6.7.2.1]
919/// struct-or-union identifier[opt] '{' struct-contents '}'
920/// struct-or-union identifier
921/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
922/// '}' attributes[opt]
923/// [GNU] struct-or-union attributes[opt] identifier
924/// struct-or-union:
925/// 'struct'
926/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000927void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
928 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000929 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000930 AccessSpecifier AS,
931 bool EnteringContext,
932 bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000933 DeclSpec::TST TagType;
934 if (TagTokKind == tok::kw_struct)
935 TagType = DeclSpec::TST_struct;
936 else if (TagTokKind == tok::kw_class)
937 TagType = DeclSpec::TST_class;
938 else {
939 assert(TagTokKind == tok::kw_union && "Not a class specifier");
940 TagType = DeclSpec::TST_union;
941 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000942
Douglas Gregor374929f2009-09-18 15:37:17 +0000943 if (Tok.is(tok::code_completion)) {
944 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000945 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000946 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +0000947 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000948
Chandler Carruth926c4b42010-06-28 08:39:25 +0000949 // C++03 [temp.explicit] 14.7.2/8:
950 // The usual access checking rules do not apply to names used to specify
951 // explicit instantiations.
952 //
953 // As an extension we do not perform access checking on the names used to
954 // specify explicit specializations either. This is important to allow
955 // specializing traits classes for private types.
956 bool SuppressingAccessChecks = false;
957 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
958 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
959 Actions.ActOnStartSuppressingAccessChecks();
960 SuppressingAccessChecks = true;
961 }
962
John McCall0b7e6782011-03-24 11:26:52 +0000963 ParsedAttributes attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000964 // If attributes exist after tag, parse them.
965 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +0000966 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000967
Steve Narofff59e17e2008-12-24 20:59:21 +0000968 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +0000969 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +0000970 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000971
Sean Huntbbd37c62009-11-21 08:43:09 +0000972 // If C++0x attributes exist here, parse them.
973 // FIXME: Are we consistent with the ordering of parsing of different
974 // styles of attributes?
John McCall7f040a92010-12-24 02:08:15 +0000975 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000976
John Wiegley20c0da72011-04-27 23:09:49 +0000977 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +0000978 !Tok.is(tok::identifier) &&
979 Tok.getIdentifierInfo() &&
980 (Tok.is(tok::kw___is_arithmetic) ||
981 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +0000982 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +0000983 Tok.is(tok::kw___is_floating_point) ||
984 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +0000985 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +0000986 Tok.is(tok::kw___is_integral) ||
987 Tok.is(tok::kw___is_member_function_pointer) ||
988 Tok.is(tok::kw___is_member_pointer) ||
989 Tok.is(tok::kw___is_pod) ||
990 Tok.is(tok::kw___is_pointer) ||
991 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +0000992 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +0000993 Tok.is(tok::kw___is_signed) ||
994 Tok.is(tok::kw___is_unsigned) ||
995 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +0000996 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +0000997 // name of struct templates, but some are keywords in GCC >= 4.3
998 // and Clang. Therefore, when we see the token sequence "struct
999 // X", make X into a normal identifier rather than a keyword, to
1000 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001001 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001002 Tok.setKind(tok::identifier);
1003 }
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001005 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001006 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +00001007 if (getLang().CPlusPlus) {
1008 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1009 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001010
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001011 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001012 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001013 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001014 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1015 Diag(Tok, diag::err_expected_ident);
1016 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001017
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001018 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1019
Douglas Gregorcc636682009-02-17 23:15:12 +00001020 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001021 IdentifierInfo *Name = 0;
1022 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001023 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001024 if (Tok.is(tok::identifier)) {
1025 Name = Tok.getIdentifierInfo();
1026 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001027
Douglas Gregor5ee37342010-05-30 22:30:21 +00001028 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001029 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001030 // Eat the template argument list and try to continue parsing this as
1031 // a class (or template thereof).
1032 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001033 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001034 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001035 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001036 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001037 // We couldn't parse the template argument list at all, so don't
1038 // try to give any location information for the list.
1039 LAngleLoc = RAngleLoc = SourceLocation();
1040 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001041
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001042 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001043 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001044 << (TagType == DeclSpec::TST_class? 0
1045 : TagType == DeclSpec::TST_struct? 1
1046 : 2)
1047 << Name
1048 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001049
1050 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001051 // we've removed its template argument list.
1052 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1053 if (TemplateParams && TemplateParams->size() > 1) {
1054 TemplateParams->pop_back();
1055 } else {
1056 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001057 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001058 = ParsedTemplateInfo::NonTemplate;
1059 }
1060 } else if (TemplateInfo.Kind
1061 == ParsedTemplateInfo::ExplicitInstantiation) {
1062 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001063 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001064 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001065 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001066 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001067 = SourceLocation();
1068 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1069 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001070 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001071 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001072 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001073 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001074 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001075
Douglas Gregor059101f2011-03-02 00:47:37 +00001076 if (TemplateId->Kind != TNK_Type_template &&
1077 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001078 // The template-name in the simple-template-id refers to
1079 // something other than a class template. Give an appropriate
1080 // error message and skip to the ';'.
1081 SourceRange Range(NameLoc);
1082 if (SS.isNotEmpty())
1083 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001084
Douglas Gregor39a8de12009-02-25 19:37:18 +00001085 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1086 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Douglas Gregor39a8de12009-02-25 19:37:18 +00001088 DS.SetTypeSpecError();
1089 SkipUntil(tok::semi, false, true);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001090 if (SuppressingAccessChecks)
1091 Actions.ActOnStopSuppressingAccessChecks();
1092
Douglas Gregor39a8de12009-02-25 19:37:18 +00001093 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001094 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001095 }
1096
Chandler Carruth926c4b42010-06-28 08:39:25 +00001097 // As soon as we're finished parsing the class's template-id, turn access
1098 // checking back on.
1099 if (SuppressingAccessChecks)
1100 Actions.ActOnStopSuppressingAccessChecks();
1101
John McCall67d1a672009-08-06 02:15:43 +00001102 // There are four options here. If we have 'struct foo;', then this
1103 // is either a forward declaration or a friend declaration, which
Anders Carlssoncc54d592011-01-22 16:56:46 +00001104 // have to be treated differently. If we have 'struct foo {...',
Anders Carlsson1d209272011-03-25 14:55:14 +00001105 // 'struct foo :...' or 'struct foo final[opt]' then this is a
Anders Carlssoncc54d592011-01-22 16:56:46 +00001106 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +00001107 // However, in some contexts, things look like declarations but are just
1108 // references, e.g.
1109 // new struct s;
1110 // or
1111 // &T::operator struct s;
1112 // For these, SuppressDeclarations is true.
John McCallf312b1e2010-08-26 23:41:50 +00001113 Sema::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001114 if (SuppressDeclarations)
John McCallf312b1e2010-08-26 23:41:50 +00001115 TUK = Sema::TUK_Reference;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001116 else if (Tok.is(tok::l_brace) ||
1117 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001118 isCXX0XFinalKeyword()) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001119 if (DS.isFriendSpecified()) {
1120 // C++ [class.friend]p2:
1121 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001122 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001123 << SourceRange(DS.getFriendSpecLoc());
1124
1125 // Skip everything up to the semicolon, so that this looks like a proper
1126 // friend class (or template thereof) declaration.
1127 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001128 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001129 } else {
1130 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001131 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001132 }
1133 } else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00001134 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001135 else
John McCallf312b1e2010-08-26 23:41:50 +00001136 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001137
John McCall207014e2010-07-30 06:26:29 +00001138 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001139 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001140 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1141 // We have a declaration or reference to an anonymous class.
1142 Diag(StartLoc, diag::err_anon_type_definition)
1143 << DeclSpec::getSpecifierName(TagType);
1144 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001145
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001146 SkipUntil(tok::comma, true);
1147 return;
1148 }
1149
Douglas Gregorddc29e12009-02-06 22:42:48 +00001150 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001151 DeclResult TagOrTempResult = true; // invalid
1152 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001153
Douglas Gregor402abb52009-05-28 23:31:59 +00001154 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001155 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001156 // Explicit specialization, class template partial specialization,
1157 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001158 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001159 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001160 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001161 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001162 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001163 // This is an explicit instantiation of a class template.
1164 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001165 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001166 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001167 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001168 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001169 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001170 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001171 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001172 TemplateId->TemplateNameLoc,
1173 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001174 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001175 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001176 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001177
1178 // Friend template-ids are treated as references unless
1179 // they have template headers, in which case they're ill-formed
1180 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1181 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001182 } else if (TUK == Sema::TUK_Reference ||
1183 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001184 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001185 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType,
1186 StartLoc,
1187 TemplateId->SS,
1188 TemplateId->Template,
1189 TemplateId->TemplateNameLoc,
1190 TemplateId->LAngleLoc,
1191 TemplateArgsPtr,
1192 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001193 } else {
1194 // This is an explicit specialization or a class template
1195 // partial specialization.
1196 TemplateParameterLists FakedParamLists;
1197
1198 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1199 // This looks like an explicit instantiation, because we have
1200 // something like
1201 //
1202 // template class Foo<X>
1203 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001204 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001205 // meant to be an explicit specialization, but the user forgot
1206 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +00001207 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001208
Mike Stump1eb44332009-09-09 15:08:12 +00001209 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001210 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001211 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001212 diag::err_explicit_instantiation_with_definition)
1213 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +00001214 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001215
1216 // Create a fake template parameter list that contains only
1217 // "template<>", so that we treat this construct as a class
1218 // template specialization.
1219 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +00001220 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001221 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001222 LAngleLoc,
1223 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001224 LAngleLoc));
1225 TemplateParams = &FakedParamLists;
1226 }
1227
1228 // Build the class template specialization.
1229 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001230 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001231 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001232 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001233 TemplateId->TemplateNameLoc,
1234 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001235 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001236 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001237 attrs.getList(),
John McCallf312b1e2010-08-26 23:41:50 +00001238 MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +00001239 TemplateParams? &(*TemplateParams)[0] : 0,
1240 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001241 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001242 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001243 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001244 // Explicit instantiation of a member of a class template
1245 // specialization, e.g.,
1246 //
1247 // template struct Outer<int>::Inner;
1248 //
1249 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001250 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001251 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001252 TemplateInfo.TemplateLoc,
1253 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001254 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001255 } else if (TUK == Sema::TUK_Friend &&
1256 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
1257 TagOrTempResult =
1258 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1259 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001260 Name, NameLoc, attrs.getList(),
John McCall9a34edb2010-10-19 01:40:49 +00001261 MultiTemplateParamsArg(Actions,
1262 TemplateParams? &(*TemplateParams)[0] : 0,
1263 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001264 } else {
1265 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001266 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001267 // FIXME: Diagnose this particular error.
1268 }
1269
John McCallc4e70192009-09-11 04:59:25 +00001270 bool IsDependent = false;
1271
John McCalla25c4082010-10-19 18:40:57 +00001272 // Don't pass down template parameter lists if this is just a tag
1273 // reference. For example, we don't need the template parameters here:
1274 // template <class T> class A *makeA(T t);
1275 MultiTemplateParamsArg TParams;
1276 if (TUK != Sema::TUK_Reference && TemplateParams)
1277 TParams =
1278 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1279
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001280 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001281 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001282 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001283 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001284 TParams, Owned, IsDependent,
1285 SourceLocation(), false,
1286 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001287
1288 // If ActOnTag said the type was dependent, try again with the
1289 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001290 if (IsDependent) {
1291 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001292 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001293 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001294 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001295 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001296
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001297 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001298 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001299 assert(Tok.is(tok::l_brace) ||
Anders Carlssoncc54d592011-01-22 16:56:46 +00001300 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001301 isCXX0XFinalKeyword());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001302 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +00001303 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001304 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001305 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001306 }
1307
John McCallb3d87482010-08-24 05:47:05 +00001308 const char *PrevSpec = 0;
1309 unsigned DiagID;
1310 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001311 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001312 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1313 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001314 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001315 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001316 Result = DS.SetTypeSpecType(TagType, StartLoc,
1317 NameLoc.isValid() ? NameLoc : StartLoc,
1318 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001319 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001320 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001321 return;
1322 }
Mike Stump1eb44332009-09-09 15:08:12 +00001323
John McCallb3d87482010-08-24 05:47:05 +00001324 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001325 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001326
Chris Lattner4ed5d912010-02-02 01:23:29 +00001327 // At this point, we've successfully parsed a class-specifier in 'definition'
1328 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1329 // going to look at what comes after it to improve error recovery. If an
1330 // impossible token occurs next, we assume that the programmer forgot a ; at
1331 // the end of the declaration and recover that way.
1332 //
1333 // This switch enumerates the valid "follow" set for definition.
John McCallf312b1e2010-08-26 23:41:50 +00001334 if (TUK == Sema::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001335 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001336 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001337 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001338 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +00001339 case tok::star: // struct foo {...} * P;
1340 case tok::amp: // struct foo {...} & R = ...
1341 case tok::identifier: // struct foo {...} V ;
1342 case tok::r_paren: //(struct foo {...} ) {4}
1343 case tok::annot_cxxscope: // struct foo {...} a:: b;
1344 case tok::annot_typename: // struct foo {...} a ::b;
1345 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +00001346 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +00001347 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001348 ExpectedSemi = false;
1349 break;
1350 // Type qualifiers
1351 case tok::kw_const: // struct foo {...} const x;
1352 case tok::kw_volatile: // struct foo {...} volatile x;
1353 case tok::kw_restrict: // struct foo {...} restrict x;
1354 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +00001355 // Storage-class specifiers
1356 case tok::kw_static: // struct foo {...} static x;
1357 case tok::kw_extern: // struct foo {...} extern x;
1358 case tok::kw_typedef: // struct foo {...} typedef x;
1359 case tok::kw_register: // struct foo {...} register x;
1360 case tok::kw_auto: // struct foo {...} auto x;
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001361 case tok::kw_mutable: // struct foo {...} mutable x;
1362 case tok::kw_constexpr: // struct foo {...} constexpr x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001363 // As shown above, type qualifiers and storage class specifiers absolutely
1364 // can occur after class specifiers according to the grammar. However,
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001365 // almost no one actually writes code like this. If we see one of these,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001366 // it is much more likely that someone missed a semi colon and the
1367 // type/storage class specifier we're seeing is part of the *next*
1368 // intended declaration, as in:
1369 //
1370 // struct foo { ... }
1371 // typedef int X;
1372 //
1373 // We'd really like to emit a missing semicolon error instead of emitting
1374 // an error on the 'int' saying that you can't have two type specifiers in
1375 // the same declaration of X. Because of this, we look ahead past this
1376 // token to see if it's a type specifier. If so, we know the code is
1377 // otherwise invalid, so we can produce the expected semi error.
1378 if (!isKnownToBeTypeSpecifier(NextToken()))
1379 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001380 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001381
1382 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001383 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001384 if (!getLang().CPlusPlus)
1385 ExpectedSemi = false;
1386 break;
1387 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001388
Richard Smithcf6b0a22011-07-14 21:35:26 +00001389 // C++ [temp]p3 In a template-declaration which defines a class, no
1390 // declarator is permitted.
1391 if (TemplateInfo.Kind)
1392 ExpectedSemi = true;
1393
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001394 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001395 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1396 TagType == DeclSpec::TST_class ? "class"
1397 : TagType == DeclSpec::TST_struct? "struct" : "union");
1398 // Push this token back into the preprocessor and change our current token
1399 // to ';' so that the rest of the code recovers as though there were an
1400 // ';' after the definition.
1401 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001402 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001403 }
1404 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001405}
1406
Mike Stump1eb44332009-09-09 15:08:12 +00001407/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001408///
1409/// base-clause : [C++ class.derived]
1410/// ':' base-specifier-list
1411/// base-specifier-list:
1412/// base-specifier '...'[opt]
1413/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001414void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001415 assert(Tok.is(tok::colon) && "Not a base clause");
1416 ConsumeToken();
1417
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001418 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001419 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001420
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001421 while (true) {
1422 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001423 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001424 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001425 // Skip the rest of this base specifier, up until the comma or
1426 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001427 SkipUntil(tok::comma, tok::l_brace, true, true);
1428 } else {
1429 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001430 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001431 }
1432
1433 // If the next token is a comma, consume it and keep reading
1434 // base-specifiers.
1435 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001437 // Consume the comma.
1438 ConsumeToken();
1439 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001440
1441 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001442 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001443}
1444
1445/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1446/// one entry in the base class list of a class specifier, for example:
1447/// class foo : public bar, virtual private baz {
1448/// 'public bar' and 'virtual private baz' are each base-specifiers.
1449///
1450/// base-specifier: [C++ class.derived]
1451/// ::[opt] nested-name-specifier[opt] class-name
1452/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001453/// base-type-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001454/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001455/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001456Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001457 bool IsVirtual = false;
1458 SourceLocation StartLoc = Tok.getLocation();
1459
1460 // Parse the 'virtual' keyword.
1461 if (Tok.is(tok::kw_virtual)) {
1462 ConsumeToken();
1463 IsVirtual = true;
1464 }
1465
1466 // Parse an (optional) access specifier.
1467 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001468 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001469 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001471 // Parse the 'virtual' keyword (again!), in case it came after the
1472 // access specifier.
1473 if (Tok.is(tok::kw_virtual)) {
1474 SourceLocation VirtualLoc = ConsumeToken();
1475 if (IsVirtual) {
1476 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001477 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001478 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001479 }
1480
1481 IsVirtual = true;
1482 }
1483
Douglas Gregor42a552f2008-11-05 20:51:48 +00001484 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001485 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001486 SourceLocation BaseLoc;
1487 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001488 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001489 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001491 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1492 // actually part of the base-specifier-list grammar productions, but we
1493 // parse it here for convenience.
1494 SourceLocation EllipsisLoc;
1495 if (Tok.is(tok::ellipsis))
1496 EllipsisLoc = ConsumeToken();
1497
Mike Stump1eb44332009-09-09 15:08:12 +00001498 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001499 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001501 // Notify semantic analysis that we have parsed a complete
1502 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001503 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001504 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001505}
1506
1507/// getAccessSpecifierIfPresent - Determine whether the next token is
1508/// a C++ access-specifier.
1509///
1510/// access-specifier: [C++ class.derived]
1511/// 'private'
1512/// 'protected'
1513/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001514AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001515 switch (Tok.getKind()) {
1516 default: return AS_none;
1517 case tok::kw_private: return AS_private;
1518 case tok::kw_protected: return AS_protected;
1519 case tok::kw_public: return AS_public;
1520 }
1521}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001522
Eli Friedmand33133c2009-07-22 21:45:50 +00001523void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
John McCalld226f652010-08-21 09:40:31 +00001524 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001525 // We just declared a member function. If this member function
1526 // has any default arguments, we'll need to parse them later.
1527 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001528 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001529 = DeclaratorInfo.getFunctionTypeInfo();
Eli Friedmand33133c2009-07-22 21:45:50 +00001530 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1531 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1532 if (!LateMethod) {
1533 // Push this method onto the stack of late-parsed method
1534 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001535 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1536 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001537 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001538
1539 // Add all of the parameters prior to this one (they don't
1540 // have default arguments).
1541 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1542 for (unsigned I = 0; I < ParamIdx; ++I)
1543 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001544 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001545 }
1546
1547 // Add this parameter to the list of parameters (it or may
1548 // not have a default argument).
1549 LateMethod->DefaultArgs.push_back(
1550 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1551 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1552 }
1553 }
1554}
1555
Richard Smith1c94c162012-01-09 22:31:44 +00001556/// isCXX0XVirtSpecifier - Determine whether the given token is a C++0x
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001557/// virt-specifier.
1558///
1559/// virt-specifier:
1560/// override
1561/// final
Richard Smith1c94c162012-01-09 22:31:44 +00001562VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier(const Token &Tok) const {
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001563 if (!getLang().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001564 return VirtSpecifiers::VS_None;
1565
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001566 if (Tok.is(tok::identifier)) {
1567 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001568
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001569 // Initialize the contextual keywords.
1570 if (!Ident_final) {
1571 Ident_final = &PP.getIdentifierTable().get("final");
1572 Ident_override = &PP.getIdentifierTable().get("override");
1573 }
1574
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001575 if (II == Ident_override)
1576 return VirtSpecifiers::VS_Override;
1577
1578 if (II == Ident_final)
1579 return VirtSpecifiers::VS_Final;
1580 }
1581
1582 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001583}
1584
1585/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1586///
1587/// virt-specifier-seq:
1588/// virt-specifier
1589/// virt-specifier-seq virt-specifier
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001590void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001591 while (true) {
Anders Carlssoncc54d592011-01-22 16:56:46 +00001592 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001593 if (Specifier == VirtSpecifiers::VS_None)
1594 return;
1595
1596 // C++ [class.mem]p8:
1597 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001598 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001599 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001600 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1601 << PrevSpec
1602 << FixItHint::CreateRemoval(Tok.getLocation());
1603
Richard Smith7fe62082011-10-15 05:09:34 +00001604 Diag(Tok.getLocation(), getLang().CPlusPlus0x ?
1605 diag::warn_cxx98_compat_override_control_keyword :
1606 diag::ext_override_control_keyword)
1607 << VirtSpecifiers::getSpecifierName(Specifier);
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001608 ConsumeToken();
1609 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001610}
1611
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001612/// isCXX0XFinalKeyword - Determine whether the next token is a C++0x
1613/// contextual 'final' keyword.
1614bool Parser::isCXX0XFinalKeyword() const {
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001615 if (!getLang().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001616 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001617
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001618 if (!Tok.is(tok::identifier))
1619 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001620
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001621 // Initialize the contextual keywords.
1622 if (!Ident_final) {
1623 Ident_final = &PP.getIdentifierTable().get("final");
1624 Ident_override = &PP.getIdentifierTable().get("override");
1625 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001626
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001627 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001628}
1629
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001630/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1631///
1632/// member-declaration:
1633/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1634/// function-definition ';'[opt]
1635/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1636/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001637/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001638/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001639/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001640///
1641/// member-declarator-list:
1642/// member-declarator
1643/// member-declarator-list ',' member-declarator
1644///
1645/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001646/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001647/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001648/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001649/// identifier[opt] ':' constant-expression
1650///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001651/// virt-specifier-seq:
1652/// virt-specifier
1653/// virt-specifier-seq virt-specifier
1654///
1655/// virt-specifier:
1656/// override
1657/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001658///
Sebastian Redle2b68332009-04-12 17:16:29 +00001659/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001660/// '= 0'
1661///
1662/// constant-initializer:
1663/// '=' constant-expression
1664///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001665void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001666 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001667 const ParsedTemplateInfo &TemplateInfo,
1668 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001669 if (Tok.is(tok::at)) {
1670 if (getLang().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
1671 Diag(Tok, diag::err_at_defs_cxx);
1672 else
1673 Diag(Tok, diag::err_at_in_class);
1674
1675 ConsumeToken();
1676 SkipUntil(tok::r_brace);
1677 return;
1678 }
1679
John McCall60fa3cf2009-12-11 02:10:03 +00001680 // Access declarations.
1681 if (!TemplateInfo.Kind &&
1682 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001683 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001684 Tok.is(tok::annot_cxxscope)) {
1685 bool isAccessDecl = false;
1686 if (NextToken().is(tok::identifier))
1687 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1688 else
1689 isAccessDecl = NextToken().is(tok::kw_operator);
1690
1691 if (isAccessDecl) {
1692 // Collect the scope specifier token we annotated earlier.
1693 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001694 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1695 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001696
1697 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001698 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001699 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001700 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1701 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001702 SkipUntil(tok::semi);
1703 return;
1704 }
1705
1706 // TODO: recover from mistakenly-qualified operator declarations.
1707 if (ExpectAndConsume(tok::semi,
1708 diag::err_expected_semi_after,
1709 "access declaration",
1710 tok::semi))
1711 return;
1712
Douglas Gregor23c94db2010-07-02 17:43:08 +00001713 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001714 false, SourceLocation(),
1715 SS, Name,
1716 /* AttrList */ 0,
1717 /* IsTypeName */ false,
1718 SourceLocation());
1719 return;
1720 }
1721 }
1722
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001723 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001724 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001725 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001726 SourceLocation DeclEnd;
1727 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001728 return;
1729 }
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Chris Lattner682bf922009-03-29 16:50:03 +00001731 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001732 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001733 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001734 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001735 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001736 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001737 return;
1738 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001739
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001740 // Handle: member-declaration ::= '__extension__' member-declaration
1741 if (Tok.is(tok::kw___extension__)) {
1742 // __extension__ silences extension warnings in the subexpression.
1743 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1744 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001745 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1746 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001747 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001748
Chris Lattner4ed5d912010-02-02 01:23:29 +00001749 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1750 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001751 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001752
John McCall0b7e6782011-03-24 11:26:52 +00001753 ParsedAttributesWithRange attrs(AttrFactory);
Sean Huntbbd37c62009-11-21 08:43:09 +00001754 // Optional C++0x attribute-specifier
John McCall7f040a92010-12-24 02:08:15 +00001755 MaybeParseCXX0XAttributes(attrs);
1756 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001757
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001758 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00001759 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001761 // Eat 'using'.
1762 SourceLocation UsingLoc = ConsumeToken();
1763
1764 if (Tok.is(tok::kw_namespace)) {
1765 Diag(UsingLoc, diag::err_using_namespace_in_class);
1766 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001767 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001768 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001769 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00001770 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1771 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001772 }
1773 return;
1774 }
1775
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001776 // decl-specifier-seq:
1777 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001778 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00001779 DS.takeAttributesFrom(attrs);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001780 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001781
John McCallf312b1e2010-08-26 23:41:50 +00001782 MultiTemplateParamsArg TemplateParams(Actions,
John McCalldd4a3b02009-09-16 22:47:08 +00001783 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1784 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1785
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001786 if (Tok.is(tok::semi)) {
1787 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001788 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00001789 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00001790 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001791 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001792 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001793
John McCall54abf7d2009-11-04 02:18:39 +00001794 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00001795 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001796
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001797 // Hold late-parsed attributes so we can attach a Decl to them later.
1798 LateParsedAttrList LateParsedAttrs;
1799
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001800 SourceLocation EqualLoc;
1801 bool HasInitializer = false;
1802 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001803 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001804 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1805 ColonProtectionRAIIObject X(*this);
1806
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001807 // Parse the first declarator.
1808 ParseDeclarator(DeclaratorInfo);
1809 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001810 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001811 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00001812 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001813 if (Tok.is(tok::semi))
1814 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001815 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001816 }
1817
Nico Weber48673472011-01-28 06:07:34 +00001818 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1819
John Thompson1b2fc0f2009-11-25 22:58:06 +00001820 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001821 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00001822
Francois Pichet6a247472011-05-11 02:14:46 +00001823 // MSVC permits pure specifier on inline functions declared at class scope.
1824 // Hence check for =0 before checking for function definition.
Francois Pichet62ec1f22011-09-17 17:15:52 +00001825 if (getLang().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00001826 DeclaratorInfo.isFunctionDeclarator() &&
1827 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001828 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00001829 Init = ParseInitializer();
1830 if (Init.isInvalid())
1831 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001832 else
1833 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00001834 }
1835
Douglas Gregor45fa5602011-11-07 20:56:01 +00001836 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001837 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00001838 //
1839 // In C++11, a non-function declarator followed by an open brace is a
1840 // braced-init-list for an in-class member initialization, not an
1841 // erroneous function definition.
1842 if (Tok.is(tok::l_brace) && !getLang().CPlusPlus0x) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00001843 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00001844 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00001845 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00001846 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00001847 } else if (Tok.is(tok::equal)) {
1848 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00001849 if (KW.is(tok::kw_default))
1850 DefinitionKind = FDK_Defaulted;
1851 else if (KW.is(tok::kw_delete))
1852 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00001853 }
1854 }
1855
Douglas Gregor45fa5602011-11-07 20:56:01 +00001856 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001857 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00001858 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001859 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00001860 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001861
1862 // Consume the optional ';'
1863 if (Tok.is(tok::semi))
1864 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001865 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001866 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001867
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001868 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00001869 Diag(DeclaratorInfo.getIdentifierLoc(),
1870 diag::err_function_declared_typedef);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001871 // This recovery skips the entire function body. It would be nice
1872 // to simply call ParseCXXInlineMethodDef() below, however Sema
1873 // assumes the declarator represents a function, not a typedef.
1874 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00001875 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001876
1877 // Consume the optional ';'
1878 if (Tok.is(tok::semi))
1879 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001880 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001881 }
1882
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001883 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001884 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00001885 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001886
1887 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
1888 LateParsedAttrs[i]->setDecl(FunDecl);
1889 }
1890 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00001891
1892 // Consume the ';' - it's optional unless we have a delete or default
1893 if (Tok.is(tok::semi)) {
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001894 ConsumeToken();
Sean Hunte4246a62011-05-12 06:15:49 +00001895 }
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001896
Chris Lattner682bf922009-03-29 16:50:03 +00001897 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001898 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001899 }
1900
1901 // member-declarator-list:
1902 // member-declarator
1903 // member-declarator-list ',' member-declarator
1904
Chris Lattner5f9e2722011-07-23 10:55:15 +00001905 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00001906 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00001907 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001908
1909 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001910 // member-declarator:
1911 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001912 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001913 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001914 if (Tok.is(tok::colon)) {
1915 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001916 BitfieldSize = ParseConstantExpression();
1917 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001918 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001919 }
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Chris Lattnere6563252010-06-13 05:34:18 +00001921 // If a simple-asm-expr is present, parse it.
1922 if (Tok.is(tok::kw_asm)) {
1923 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001924 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00001925 if (AsmLabel.isInvalid())
1926 SkipUntil(tok::comma, true, true);
1927
1928 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1929 DeclaratorInfo.SetRangeEnd(Loc);
1930 }
1931
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001932 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001933 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001934
Richard Smith7a614d82011-06-11 17:19:42 +00001935 // FIXME: When g++ adds support for this, we'll need to check whether it
1936 // goes before or after the GNU attributes and __asm__.
1937 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1938
1939 bool HasDeferredInitializer = false;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00001940 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00001941 if (BitfieldSize.get()) {
1942 Diag(Tok, diag::err_bitfield_member_init);
1943 SkipUntil(tok::comma, true, true);
1944 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00001945 HasInitializer = true;
Douglas Gregor555f57e2011-06-25 00:56:27 +00001946 HasDeferredInitializer = !DeclaratorInfo.isDeclarationOfFunction() &&
Richard Smith7a614d82011-06-11 17:19:42 +00001947 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smithc2cdd532011-06-12 11:43:46 +00001948 != DeclSpec::SCS_static &&
1949 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
1950 != DeclSpec::SCS_typedef;
Richard Smith7a614d82011-06-11 17:19:42 +00001951 }
1952 }
1953
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001954 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001955 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001956 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001957
John McCalld226f652010-08-21 09:40:31 +00001958 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00001959 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001960 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00001961 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCallbbbcdd92009-09-11 21:02:39 +00001962 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001963 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001964 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00001965 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001966 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001967 BitfieldSize.release(),
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00001968 VS, HasDeferredInitializer);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001969 if (AccessAttrs)
1970 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
1971 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001972 }
Douglas Gregor147545d2011-10-10 14:49:18 +00001973
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001974 // Set the Decl for any late parsed attributes
1975 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
1976 LateParsedAttrs[i]->setDecl(ThisDecl);
1977 }
1978 LateParsedAttrs.clear();
1979
Douglas Gregor147545d2011-10-10 14:49:18 +00001980 // Handle the initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00001981 if (HasDeferredInitializer) {
Douglas Gregor147545d2011-10-10 14:49:18 +00001982 // The initializer was deferred; parse it and cache the tokens.
Richard Smith7fe62082011-10-15 05:09:34 +00001983 Diag(Tok, getLang().CPlusPlus0x ?
1984 diag::warn_cxx98_compat_nonstatic_member_init :
1985 diag::ext_nonstatic_member_init);
1986
Richard Smith7a614d82011-06-11 17:19:42 +00001987 if (DeclaratorInfo.isArrayOfUnknownBound()) {
1988 // C++0x [dcl.array]p3: An array bound may also be omitted when the
1989 // declarator is followed by an initializer.
1990 //
1991 // A brace-or-equal-initializer for a member-declarator is not an
1992 // initializer in the gramamr, so this is ill-formed.
1993 Diag(Tok, diag::err_incomplete_array_member_init);
1994 SkipUntil(tok::comma, true, true);
1995 // Avoid later warnings about a class member of incomplete type.
1996 ThisDecl->setInvalidDecl();
1997 } else
1998 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00001999 } else if (HasInitializer) {
2000 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002001 if (!Init.isUsable())
2002 Init = ParseCXXMemberInitializer(
2003 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2004
Douglas Gregor147545d2011-10-10 14:49:18 +00002005 if (Init.isInvalid())
2006 SkipUntil(tok::comma, true, true);
2007 else if (ThisDecl)
2008 Actions.AddInitializerToDecl(ThisDecl, Init.get(), false,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002009 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor147545d2011-10-10 14:49:18 +00002010 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2011 // No initializer.
2012 Actions.ActOnUninitializedDecl(ThisDecl,
2013 DS.getTypeSpecType() == DeclSpec::TST_auto);
Richard Smith7a614d82011-06-11 17:19:42 +00002014 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002015
2016 if (ThisDecl) {
2017 Actions.FinalizeDeclaration(ThisDecl);
2018 DeclsInGroup.push_back(ThisDecl);
2019 }
2020
2021 if (DeclaratorInfo.isFunctionDeclarator() &&
2022 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2023 != DeclSpec::SCS_typedef) {
2024 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
2025 }
2026
2027 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002028
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002029 // If we don't have a comma, it is either the end of the list (a ';')
2030 // or an error, bail out.
2031 if (Tok.isNot(tok::comma))
2032 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002033
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002034 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002035 SourceLocation CommaLoc = ConsumeToken();
2036
2037 if (Tok.isAtStartOfLine() &&
2038 !MightBeDeclarator(Declarator::MemberContext)) {
2039 // This comma was followed by a line-break and something which can't be
2040 // the start of a declarator. The comma was probably a typo for a
2041 // semicolon.
2042 Diag(CommaLoc, diag::err_expected_semi_declaration)
2043 << FixItHint::CreateReplacement(CommaLoc, ";");
2044 ExpectSemi = false;
2045 break;
2046 }
Mike Stump1eb44332009-09-09 15:08:12 +00002047
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002048 // Parse the next declarator.
2049 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002050 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002051 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002052 Init = true;
2053 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002054 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002055
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002056 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002057 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002058
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002059 if (Tok.isNot(tok::colon))
2060 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002061 }
2062
Richard Smith1c94c162012-01-09 22:31:44 +00002063 if (ExpectSemi &&
2064 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002065 // Skip to end of block or statement.
2066 SkipUntil(tok::r_brace, true, true);
2067 // If we stopped at a ';', eat it.
2068 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002069 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002070 }
2071
Douglas Gregor23c94db2010-07-02 17:43:08 +00002072 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00002073 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002074}
2075
Richard Smith7a614d82011-06-11 17:19:42 +00002076/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2077/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2078/// function definition. The location of the '=', if any, will be placed in
2079/// EqualLoc.
2080///
2081/// pure-specifier:
2082/// '= 0'
2083///
2084/// brace-or-equal-initializer:
2085/// '=' initializer-expression
2086/// braced-init-list [TODO]
2087///
2088/// initializer-clause:
2089/// assignment-expression
2090/// braced-init-list [TODO]
2091///
2092/// defaulted/deleted function-definition:
2093/// '=' 'default'
2094/// '=' 'delete'
2095///
2096/// Prior to C++0x, the assignment-expression in an initializer-clause must
2097/// be a constant-expression.
2098ExprResult Parser::ParseCXXMemberInitializer(bool IsFunction,
2099 SourceLocation &EqualLoc) {
2100 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2101 && "Data member initializer not starting with '=' or '{'");
2102
2103 if (Tok.is(tok::equal)) {
2104 EqualLoc = ConsumeToken();
2105 if (Tok.is(tok::kw_delete)) {
2106 // In principle, an initializer of '= delete p;' is legal, but it will
2107 // never type-check. It's better to diagnose it as an ill-formed expression
2108 // than as an ill-formed deleted non-function member.
2109 // An initializer of '= delete p, foo' will never be parsed, because
2110 // a top-level comma always ends the initializer expression.
2111 const Token &Next = NextToken();
2112 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2113 Next.is(tok::eof)) {
2114 if (IsFunction)
2115 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2116 << 1 /* delete */;
2117 else
2118 Diag(ConsumeToken(), diag::err_deleted_non_function);
2119 return ExprResult();
2120 }
2121 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002122 if (IsFunction)
2123 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2124 << 0 /* default */;
2125 else
2126 Diag(ConsumeToken(), diag::err_default_special_members);
2127 return ExprResult();
2128 }
2129
2130 return ParseInitializer();
2131 } else
2132 return ExprError(Diag(Tok, diag::err_generalized_initializer_lists));
2133}
2134
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002135/// ParseCXXMemberSpecification - Parse the class definition.
2136///
2137/// member-specification:
2138/// member-declaration member-specification[opt]
2139/// access-specifier ':' member-specification[opt]
2140///
2141void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002142 unsigned TagType, Decl *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00002143 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002144 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00002145 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002146
John McCallf312b1e2010-08-26 23:41:50 +00002147 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2148 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002149
Douglas Gregor26997fd2010-01-16 20:52:59 +00002150 // Determine whether this is a non-nested class. Note that local
2151 // classes are *not* considered to be nested classes.
2152 bool NonNestedClass = true;
2153 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002154 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002155 if (S->isClassScope()) {
2156 // We're inside a class scope, so this is a nested class.
2157 NonNestedClass = false;
2158 break;
2159 }
2160
2161 if ((S->getFlags() & Scope::FnScope)) {
2162 // If we're in a function or function template declared in the
2163 // body of a class, then this is a local class rather than a
2164 // nested class.
2165 const Scope *Parent = S->getParent();
2166 if (Parent->isTemplateParamScope())
2167 Parent = Parent->getParent();
2168 if (Parent->isClassScope())
2169 break;
2170 }
2171 }
2172 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002173
2174 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002175 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002176
Douglas Gregor6569d682009-05-27 23:11:45 +00002177 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00002178 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00002179
Douglas Gregorddc29e12009-02-06 22:42:48 +00002180 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002181 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002182
Anders Carlssonb184a182011-03-25 14:46:08 +00002183 SourceLocation FinalLoc;
2184
2185 // Parse the optional 'final' keyword.
2186 if (getLang().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith8b11b5e2011-10-15 04:21:46 +00002187 assert(isCXX0XFinalKeyword() && "not a class definition");
2188 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002189
Richard Smith7fe62082011-10-15 05:09:34 +00002190 Diag(FinalLoc, getLang().CPlusPlus0x ?
2191 diag::warn_cxx98_compat_override_control_keyword :
2192 diag::ext_override_control_keyword) << "final";
Anders Carlssonb184a182011-03-25 14:46:08 +00002193 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002194
John McCallbd0dfa52009-12-19 21:48:58 +00002195 if (Tok.is(tok::colon)) {
2196 ParseBaseClause(TagDecl);
2197
2198 if (!Tok.is(tok::l_brace)) {
2199 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002200
2201 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002202 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002203 return;
2204 }
2205 }
2206
2207 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002208 BalancedDelimiterTracker T(*this, tok::l_brace);
2209 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002210
John McCall42a4f662010-05-28 08:11:17 +00002211 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002212 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002213 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002214
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002215 // C++ 11p3: Members of a class defined with the keyword class are private
2216 // by default. Members of a class defined with the keywords struct or union
2217 // are public by default.
2218 AccessSpecifier CurAS;
2219 if (TagType == DeclSpec::TST_class)
2220 CurAS = AS_private;
2221 else
2222 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002223 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002224
Douglas Gregor07976d22010-06-21 22:31:09 +00002225 if (TagDecl) {
2226 // While we still have something to read, read the member-declarations.
2227 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2228 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002229
Francois Pichet62ec1f22011-09-17 17:15:52 +00002230 if (getLang().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002231 Tok.is(tok::kw___if_not_exists))) {
2232 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2233 continue;
2234 }
2235
Douglas Gregor07976d22010-06-21 22:31:09 +00002236 // Check for extraneous top-level semicolon.
2237 if (Tok.is(tok::semi)) {
2238 Diag(Tok, diag::ext_extra_struct_semi)
2239 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
2240 << FixItHint::CreateRemoval(Tok.getLocation());
2241 ConsumeToken();
2242 continue;
2243 }
2244
2245 AccessSpecifier AS = getAccessSpecifierIfPresent();
2246 if (AS != AS_none) {
2247 // Current token is a C++ access specifier.
2248 CurAS = AS;
2249 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002250 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002251 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002252 AccessAttrs.clear();
2253 MaybeParseGNUAttributes(AccessAttrs);
2254
David Blaikie13f8daf2011-10-13 06:08:43 +00002255 SourceLocation EndLoc;
2256 if (Tok.is(tok::colon)) {
2257 EndLoc = Tok.getLocation();
2258 ConsumeToken();
2259 } else if (Tok.is(tok::semi)) {
2260 EndLoc = Tok.getLocation();
2261 ConsumeToken();
2262 Diag(EndLoc, diag::err_expected_colon)
2263 << FixItHint::CreateReplacement(EndLoc, ":");
2264 } else {
2265 EndLoc = ASLoc.getLocWithOffset(TokLength);
2266 Diag(EndLoc, diag::err_expected_colon)
2267 << FixItHint::CreateInsertion(EndLoc, ":");
2268 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002269
2270 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2271 AccessAttrs.getList())) {
2272 // found another attribute than only annotations
2273 AccessAttrs.clear();
2274 }
2275
Douglas Gregor07976d22010-06-21 22:31:09 +00002276 continue;
2277 }
2278
2279 // FIXME: Make sure we don't have a template here.
2280
2281 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002282 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002283 }
2284
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002285 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002286 } else {
2287 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002288 }
Mike Stump1eb44332009-09-09 15:08:12 +00002289
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002290 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002291 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002292 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002293
John McCall42a4f662010-05-28 08:11:17 +00002294 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002295 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002296 T.getOpenLocation(),
2297 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002298 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002299
Richard Smith7a614d82011-06-11 17:19:42 +00002300 // C++0x [class.mem]p2: Within the class member-specification, the class is
2301 // regarded as complete within function bodies, default arguments, exception-
2302 // specifications, and brace-or-equal-initializers for non-static data
2303 // members (including such things in nested classes).
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002304 //
Richard Smith7a614d82011-06-11 17:19:42 +00002305 // FIXME: Only function bodies and brace-or-equal-initializers are currently
2306 // handled. Fix the others!
Douglas Gregor07976d22010-06-21 22:31:09 +00002307 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002308 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002309 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002310 // declarations and the lexed inline method definitions, along with any
2311 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002312 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002313 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002314 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith7a614d82011-06-11 17:19:42 +00002315 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002316 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002317 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002318 }
2319
John McCall42a4f662010-05-28 08:11:17 +00002320 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002321 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2322 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002323
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002324 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002325 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002326 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002327}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002328
2329/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2330/// which explicitly initializes the members or base classes of a
2331/// class (C++ [class.base.init]). For example, the three initializers
2332/// after the ':' in the Derived constructor below:
2333///
2334/// @code
2335/// class Base { };
2336/// class Derived : Base {
2337/// int x;
2338/// float f;
2339/// public:
2340/// Derived(float f) : Base(), x(17), f(f) { }
2341/// };
2342/// @endcode
2343///
Mike Stump1eb44332009-09-09 15:08:12 +00002344/// [C++] ctor-initializer:
2345/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002346///
Mike Stump1eb44332009-09-09 15:08:12 +00002347/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002348/// mem-initializer ...[opt]
2349/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002350void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002351 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2352
John Wiegley28bbe4b2011-04-28 01:08:34 +00002353 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2354 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002355 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002356
Chris Lattner5f9e2722011-07-23 10:55:15 +00002357 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002358 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002359
Douglas Gregor7ad83902008-11-05 04:29:56 +00002360 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002361 if (Tok.is(tok::code_completion)) {
2362 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2363 MemInitializers.data(),
2364 MemInitializers.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002365 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002366 } else {
2367 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2368 if (!MemInit.isInvalid())
2369 MemInitializers.push_back(MemInit.get());
2370 else
2371 AnyErrors = true;
2372 }
2373
Douglas Gregor7ad83902008-11-05 04:29:56 +00002374 if (Tok.is(tok::comma))
2375 ConsumeToken();
2376 else if (Tok.is(tok::l_brace))
2377 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002378 // If the next token looks like a base or member initializer, assume that
2379 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002380 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2381 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2382 Diag(Loc, diag::err_ctor_init_missing_comma)
2383 << FixItHint::CreateInsertion(Loc, ", ");
2384 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002385 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002386 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002387 SkipUntil(tok::l_brace, true, true);
2388 break;
2389 }
2390 } while (true);
2391
Mike Stump1eb44332009-09-09 15:08:12 +00002392 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002393 MemInitializers.data(), MemInitializers.size(),
2394 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002395}
2396
2397/// ParseMemInitializer - Parse a C++ member initializer, which is
2398/// part of a constructor initializer that explicitly initializes one
2399/// member or base class (C++ [class.base.init]). See
2400/// ParseConstructorInitializer for an example.
2401///
2402/// [C++] mem-initializer:
2403/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002404/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002405///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002406/// [C++] mem-initializer-id:
2407/// '::'[opt] nested-name-specifier[opt] class-name
2408/// identifier
John McCalld226f652010-08-21 09:40:31 +00002409Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002410 // parse '::'[opt] nested-name-specifier[opt]
2411 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002412 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002413 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002414 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002415 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002416 if (TemplateId->Kind == TNK_Type_template ||
2417 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002418 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002419 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002420 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002421 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002422 }
David Blaikief2116622012-01-24 06:03:59 +00002423 // Uses of decltype will already have been converted to annot_decltype by
2424 // ParseOptionalCXXScopeSpecifier at this point.
2425 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2426 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002427 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002428 return true;
2429 }
Mike Stump1eb44332009-09-09 15:08:12 +00002430
David Blaikief2116622012-01-24 06:03:59 +00002431 IdentifierInfo *II = 0;
2432 DeclSpec DS(AttrFactory);
2433 SourceLocation IdLoc = Tok.getLocation();
2434 if (Tok.is(tok::annot_decltype)) {
2435 // Get the decltype expression, if there is one.
2436 ParseDecltypeSpecifier(DS);
2437 } else {
2438 if (Tok.is(tok::identifier))
2439 // Get the identifier. This may be a member name or a class name,
2440 // but we'll let the semantic analysis determine which it is.
2441 II = Tok.getIdentifierInfo();
2442 ConsumeToken();
2443 }
2444
Douglas Gregor7ad83902008-11-05 04:29:56 +00002445
2446 // Parse the '('.
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002447 if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002448 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2449
Sebastian Redl6df65482011-09-24 17:48:25 +00002450 ExprResult InitList = ParseBraceInitializer();
2451 if (InitList.isInvalid())
2452 return true;
2453
2454 SourceLocation EllipsisLoc;
2455 if (Tok.is(tok::ellipsis))
2456 EllipsisLoc = ConsumeToken();
2457
2458 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002459 TemplateTypeTy, DS, IdLoc,
2460 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002461 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002462 BalancedDelimiterTracker T(*this, tok::l_paren);
2463 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002464
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002465 // Parse the optional expression-list.
2466 ExprVector ArgExprs(Actions);
2467 CommaLocsTy CommaLocs;
2468 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2469 SkipUntil(tok::r_paren);
2470 return true;
2471 }
2472
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002473 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002474
2475 SourceLocation EllipsisLoc;
2476 if (Tok.is(tok::ellipsis))
2477 EllipsisLoc = ConsumeToken();
2478
2479 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002480 TemplateTypeTy, DS, IdLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002481 T.getOpenLocation(), ArgExprs.take(),
2482 ArgExprs.size(), T.getCloseLocation(),
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002483 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002484 }
2485
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002486 Diag(Tok, getLang().CPlusPlus0x ? diag::err_expected_lparen_or_lbrace
2487 : diag::err_expected_lparen);
2488 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002489}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002490
Sebastian Redl7acafd02011-03-05 14:45:16 +00002491/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002492///
Douglas Gregora4745612008-12-01 18:00:20 +00002493/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002494/// dynamic-exception-specification
2495/// noexcept-specification
2496///
2497/// noexcept-specification:
2498/// 'noexcept'
2499/// 'noexcept' '(' constant-expression ')'
2500ExceptionSpecificationType
2501Parser::MaybeParseExceptionSpecification(SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002502 SmallVectorImpl<ParsedType> &DynamicExceptions,
2503 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Sebastian Redl7acafd02011-03-05 14:45:16 +00002504 ExprResult &NoexceptExpr) {
2505 ExceptionSpecificationType Result = EST_None;
2506
2507 // See if there's a dynamic specification.
2508 if (Tok.is(tok::kw_throw)) {
2509 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2510 DynamicExceptions,
2511 DynamicExceptionRanges);
2512 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2513 "Produced different number of exception types and ranges.");
2514 }
2515
2516 // If there's no noexcept specification, we're done.
2517 if (Tok.isNot(tok::kw_noexcept))
2518 return Result;
2519
Richard Smith841804b2011-10-17 23:06:20 +00002520 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2521
Sebastian Redl7acafd02011-03-05 14:45:16 +00002522 // If we already had a dynamic specification, parse the noexcept for,
2523 // recovery, but emit a diagnostic and don't store the results.
2524 SourceRange NoexceptRange;
2525 ExceptionSpecificationType NoexceptType = EST_None;
2526
2527 SourceLocation KeywordLoc = ConsumeToken();
2528 if (Tok.is(tok::l_paren)) {
2529 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002530 BalancedDelimiterTracker T(*this, tok::l_paren);
2531 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002532 NoexceptType = EST_ComputedNoexcept;
2533 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002534 // The argument must be contextually convertible to bool. We use
2535 // ActOnBooleanCondition for this purpose.
2536 if (!NoexceptExpr.isInvalid())
2537 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2538 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002539 T.consumeClose();
2540 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002541 } else {
2542 // There is no argument.
2543 NoexceptType = EST_BasicNoexcept;
2544 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2545 }
2546
2547 if (Result == EST_None) {
2548 SpecificationRange = NoexceptRange;
2549 Result = NoexceptType;
2550
2551 // If there's a dynamic specification after a noexcept specification,
2552 // parse that and ignore the results.
2553 if (Tok.is(tok::kw_throw)) {
2554 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2555 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2556 DynamicExceptionRanges);
2557 }
2558 } else {
2559 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2560 }
2561
2562 return Result;
2563}
2564
2565/// ParseDynamicExceptionSpecification - Parse a C++
2566/// dynamic-exception-specification (C++ [except.spec]).
2567///
2568/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002569/// 'throw' '(' type-id-list [opt] ')'
2570/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002571///
Douglas Gregora4745612008-12-01 18:00:20 +00002572/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002573/// type-id ... [opt]
2574/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002575///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002576ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2577 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002578 SmallVectorImpl<ParsedType> &Exceptions,
2579 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002580 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002581
Sebastian Redl7acafd02011-03-05 14:45:16 +00002582 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002583 BalancedDelimiterTracker T(*this, tok::l_paren);
2584 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002585 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2586 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002587 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002588 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002589
Douglas Gregora4745612008-12-01 18:00:20 +00002590 // Parse throw(...), a Microsoft extension that means "this function
2591 // can throw anything".
2592 if (Tok.is(tok::ellipsis)) {
2593 SourceLocation EllipsisLoc = ConsumeToken();
Francois Pichet62ec1f22011-09-17 17:15:52 +00002594 if (!getLang().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002595 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002596 T.consumeClose();
2597 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002598 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002599 }
2600
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002601 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002602 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002603 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002604 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002605
Douglas Gregora04426c2010-12-20 23:57:46 +00002606 if (Tok.is(tok::ellipsis)) {
2607 // C++0x [temp.variadic]p5:
2608 // - In a dynamic-exception-specification (15.4); the pattern is a
2609 // type-id.
2610 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002611 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002612 if (!Res.isInvalid())
2613 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2614 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002615
Sebastian Redlef65f062009-05-29 18:02:33 +00002616 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002617 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002618 Ranges.push_back(Range);
2619 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002620
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002621 if (Tok.is(tok::comma))
2622 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002623 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002624 break;
2625 }
2626
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002627 T.consumeClose();
2628 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002629 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002630}
Douglas Gregor6569d682009-05-27 23:11:45 +00002631
Douglas Gregordab60ad2010-10-01 18:44:50 +00002632/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2633/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002634TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002635 assert(Tok.is(tok::arrow) && "expected arrow");
2636
2637 ConsumeToken();
2638
2639 // FIXME: Need to suppress declarations when parsing this typename.
2640 // Otherwise in this function definition:
2641 //
2642 // auto f() -> struct X {}
2643 //
2644 // struct X is parsed as class definition because of the trailing
2645 // brace.
Douglas Gregordab60ad2010-10-01 18:44:50 +00002646 return ParseTypeName(&Range);
2647}
2648
Douglas Gregor6569d682009-05-27 23:11:45 +00002649/// \brief We have just started parsing the definition of a new class,
2650/// so push that class onto our stack of classes that is currently
2651/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002652Sema::ParsingClassState
2653Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002654 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002655 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00002656 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
John McCalleee1d542011-02-14 07:13:47 +00002657 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002658}
2659
2660/// \brief Deallocate the given parsed class and all of its nested
2661/// classes.
2662void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002663 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2664 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002665 delete Class;
2666}
2667
2668/// \brief Pop the top class of the stack of classes that are
2669/// currently being parsed.
2670///
2671/// This routine should be called when we have finished parsing the
2672/// definition of a class, but have not yet popped the Scope
2673/// associated with the class's definition.
2674///
2675/// \returns true if the class we've popped is a top-level class,
2676/// false otherwise.
John McCalleee1d542011-02-14 07:13:47 +00002677void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002678 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002679
John McCalleee1d542011-02-14 07:13:47 +00002680 Actions.PopParsingClass(state);
2681
Douglas Gregor6569d682009-05-27 23:11:45 +00002682 ParsingClass *Victim = ClassStack.top();
2683 ClassStack.pop();
2684 if (Victim->TopLevelClass) {
2685 // Deallocate all of the nested classes of this class,
2686 // recursively: we don't need to keep any of this information.
2687 DeallocateParsedClasses(Victim);
2688 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002689 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002690 assert(!ClassStack.empty() && "Missing top-level class?");
2691
Douglas Gregord54eb442010-10-12 16:25:54 +00002692 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002693 // The victim is a nested class, but we will not need to perform
2694 // any processing after the definition of this class since it has
2695 // no members whose handling was delayed. Therefore, we can just
2696 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002697 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002698 return;
2699 }
2700
2701 // This nested class has some members that will need to be processed
2702 // after the top-level class is completely defined. Therefore, add
2703 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002704 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002705 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002706 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00002707}
Sean Huntbbd37c62009-11-21 08:43:09 +00002708
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002709/// ParseCXX0XAttributeSpecifier - Parse a C++0x attribute-specifier. Currently
2710/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00002711///
2712/// [C++0x] attribute-specifier:
2713/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002714/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00002715///
2716/// [C++0x] attribute-list:
2717/// attribute[opt]
2718/// attribute-list ',' attribute[opt]
2719///
2720/// [C++0x] attribute:
2721/// attribute-token attribute-argument-clause[opt]
2722///
2723/// [C++0x] attribute-token:
2724/// identifier
2725/// attribute-scoped-token
2726///
2727/// [C++0x] attribute-scoped-token:
2728/// attribute-namespace '::' identifier
2729///
2730/// [C++0x] attribute-namespace:
2731/// identifier
2732///
2733/// [C++0x] attribute-argument-clause:
2734/// '(' balanced-token-seq ')'
2735///
2736/// [C++0x] balanced-token-seq:
2737/// balanced-token
2738/// balanced-token-seq balanced-token
2739///
2740/// [C++0x] balanced-token:
2741/// '(' balanced-token-seq ')'
2742/// '[' balanced-token-seq ']'
2743/// '{' balanced-token-seq '}'
2744/// any token but '(', ')', '[', ']', '{', or '}'
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002745void Parser::ParseCXX0XAttributeSpecifier(ParsedAttributes &attrs,
2746 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002747 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00002748 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002749 ParseAlignmentSpecifier(attrs, endLoc);
2750 return;
2751 }
2752
Sean Huntbbd37c62009-11-21 08:43:09 +00002753 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
2754 && "Not a C++0x attribute list");
2755
Richard Smith41be6732011-10-14 20:48:27 +00002756 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
2757
Sean Huntbbd37c62009-11-21 08:43:09 +00002758 ConsumeBracket();
2759 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002760
Sean Huntbbd37c62009-11-21 08:43:09 +00002761 if (Tok.is(tok::comma)) {
2762 Diag(Tok.getLocation(), diag::err_expected_ident);
2763 ConsumeToken();
2764 }
2765
2766 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
2767 // attribute not present
2768 if (Tok.is(tok::comma)) {
2769 ConsumeToken();
2770 continue;
2771 }
2772
2773 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
2774 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002775
Sean Huntbbd37c62009-11-21 08:43:09 +00002776 // scoped attribute
2777 if (Tok.is(tok::coloncolon)) {
2778 ConsumeToken();
2779
2780 if (!Tok.is(tok::identifier)) {
2781 Diag(Tok.getLocation(), diag::err_expected_ident);
2782 SkipUntil(tok::r_square, tok::comma, true, true);
2783 continue;
2784 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002785
Sean Huntbbd37c62009-11-21 08:43:09 +00002786 ScopeName = AttrName;
2787 ScopeLoc = AttrLoc;
2788
2789 AttrName = Tok.getIdentifierInfo();
2790 AttrLoc = ConsumeToken();
2791 }
2792
2793 bool AttrParsed = false;
2794 // No scoped names are supported; ideally we could put all non-standard
2795 // attributes into namespaces.
2796 if (!ScopeName) {
2797 switch(AttributeList::getKind(AttrName))
2798 {
2799 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00002800 case AttributeList::AT_carries_dependency:
Anders Carlsson15e14a22011-01-23 21:33:18 +00002801 case AttributeList::AT_noreturn: {
Sean Huntbbd37c62009-11-21 08:43:09 +00002802 if (Tok.is(tok::l_paren)) {
2803 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
2804 << AttrName->getName();
2805 break;
2806 }
2807
John McCall0b7e6782011-03-24 11:26:52 +00002808 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc, 0,
2809 SourceLocation(), 0, 0, false, true);
Sean Huntbbd37c62009-11-21 08:43:09 +00002810 AttrParsed = true;
2811 break;
2812 }
2813
Sean Huntbbd37c62009-11-21 08:43:09 +00002814 // Silence warnings
2815 default: break;
2816 }
2817 }
2818
2819 // Skip the entire parameter clause, if any
2820 if (!AttrParsed && Tok.is(tok::l_paren)) {
2821 ConsumeParen();
2822 // SkipUntil maintains the balancedness of tokens.
2823 SkipUntil(tok::r_paren, false);
2824 }
2825 }
2826
2827 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2828 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002829 if (endLoc)
2830 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00002831 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2832 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002833}
Sean Huntbbd37c62009-11-21 08:43:09 +00002834
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002835/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier-seq.
2836///
2837/// attribute-specifier-seq:
2838/// attribute-specifier-seq[opt] attribute-specifier
2839void Parser::ParseCXX0XAttributes(ParsedAttributesWithRange &attrs,
2840 SourceLocation *endLoc) {
2841 SourceLocation StartLoc = Tok.getLocation(), Loc;
2842 if (!endLoc)
2843 endLoc = &Loc;
2844
Douglas Gregor8828ee72011-10-07 20:35:25 +00002845 do {
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002846 ParseCXX0XAttributeSpecifier(attrs, endLoc);
Douglas Gregor8828ee72011-10-07 20:35:25 +00002847 } while (isCXX0XAttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00002848
2849 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002850}
2851
Francois Pichet334d47e2010-10-11 12:59:39 +00002852/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
2853///
2854/// [MS] ms-attribute:
2855/// '[' token-seq ']'
2856///
2857/// [MS] ms-attribute-seq:
2858/// ms-attribute[opt]
2859/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00002860void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
2861 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00002862 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
2863
2864 while (Tok.is(tok::l_square)) {
2865 ConsumeBracket();
2866 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00002867 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00002868 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
2869 }
2870}
Francois Pichet563a6452011-05-25 10:19:49 +00002871
2872void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
2873 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00002874 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00002875 if (ParseMicrosoftIfExistsCondition(Result))
2876 return;
2877
Douglas Gregor3896fc52011-10-24 22:31:10 +00002878 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2879 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00002880 Diag(Tok, diag::err_expected_lbrace);
2881 return;
2882 }
Francois Pichet563a6452011-05-25 10:19:49 +00002883
Douglas Gregor3896fc52011-10-24 22:31:10 +00002884 switch (Result.Behavior) {
2885 case IEB_Parse:
2886 // Parse the declarations below.
2887 break;
2888
2889 case IEB_Dependent:
2890 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
2891 << Result.IsIfExists;
2892 // Fall through to skip.
2893
2894 case IEB_Skip:
2895 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00002896 return;
2897 }
2898
Douglas Gregor3896fc52011-10-24 22:31:10 +00002899 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00002900 // __if_exists, __if_not_exists can nest.
2901 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
2902 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2903 continue;
2904 }
2905
2906 // Check for extraneous top-level semicolon.
2907 if (Tok.is(tok::semi)) {
2908 Diag(Tok, diag::ext_extra_struct_semi)
2909 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
2910 << FixItHint::CreateRemoval(Tok.getLocation());
2911 ConsumeToken();
2912 continue;
2913 }
2914
2915 AccessSpecifier AS = getAccessSpecifierIfPresent();
2916 if (AS != AS_none) {
2917 // Current token is a C++ access specifier.
2918 CurAS = AS;
2919 SourceLocation ASLoc = Tok.getLocation();
2920 ConsumeToken();
2921 if (Tok.is(tok::colon))
2922 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
2923 else
2924 Diag(Tok, diag::err_expected_colon);
2925 ConsumeToken();
2926 continue;
2927 }
2928
2929 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002930 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00002931 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00002932
2933 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00002934}