blob: 26389832da8aa6bc4a79934043d7b53f0acdc279 [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
Douglas Gregor1b7f8982008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
16#include "clang/Basic/OperatorKinds.h"
Chris Lattner500d3292009-01-29 05:15:15 +000017#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000018#include "clang/Sema/DeclSpec.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Sema/Scope.h"
John McCalle402e722012-09-25 07:32:39 +000022#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000024using namespace clang;
25
26/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000027/// may either be a top level namespace or a block-level namespace alias. If
28/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000029///
30/// namespace-definition: [C++ 7.3: basic.namespace]
31/// named-namespace-definition
32/// unnamed-namespace-definition
33///
34/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000035/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000036///
37/// named-namespace-definition:
38/// original-namespace-definition
39/// extension-namespace-definition
40///
41/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000042/// 'inline'[opt] 'namespace' identifier attributes[opt]
43/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000044///
45/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000046/// 'inline'[opt] 'namespace' original-namespace-name
47/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000048///
Chris Lattner8f08cb72007-08-25 06:57:03 +000049/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
50/// 'namespace' identifier '=' qualified-namespace-specifier ';'
51///
John McCalld226f652010-08-21 09:40:31 +000052Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000053 SourceLocation &DeclEnd,
54 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000055 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000056 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +000057 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000058
Douglas Gregor49f40bd2009-09-18 19:03:04 +000059 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000060 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000061 cutOffParsing();
62 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +000063 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000064
Chris Lattner8f08cb72007-08-25 06:57:03 +000065 SourceLocation IdentLoc;
66 IdentifierInfo *Ident = 0;
Richard Trieuf858bd82011-05-26 20:11:09 +000067 std::vector<SourceLocation> ExtraIdentLoc;
68 std::vector<IdentifierInfo*> ExtraIdent;
69 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000070
71 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000072
Chris Lattner04d66662007-10-09 17:33:22 +000073 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000074 Ident = Tok.getIdentifierInfo();
75 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieuf858bd82011-05-26 20:11:09 +000076 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
77 ExtraNamespaceLoc.push_back(ConsumeToken());
78 ExtraIdent.push_back(Tok.getIdentifierInfo());
79 ExtraIdentLoc.push_back(ConsumeToken());
80 }
Chris Lattner8f08cb72007-08-25 06:57:03 +000081 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Chris Lattner8f08cb72007-08-25 06:57:03 +000083 // Read label attributes, if present.
John McCall0b7e6782011-03-24 11:26:52 +000084 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000085 if (Tok.is(tok::kw___attribute)) {
86 attrTok = Tok;
John McCall7f040a92010-12-24 02:08:15 +000087 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000088 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Douglas Gregor6a588dd2009-06-17 19:49:00 +000090 if (Tok.is(tok::equal)) {
Nico Webere1bb3292012-10-27 23:44:27 +000091 if (Ident == 0) {
92 Diag(Tok, diag::err_expected_ident);
93 // Skip to end of the definition and eat the ';'.
94 SkipUntil(tok::semi);
95 return 0;
96 }
John McCall7f040a92010-12-24 02:08:15 +000097 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +000098 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +000099 if (InlineLoc.isValid())
100 Diag(InlineLoc, diag::err_inline_namespace_alias)
101 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000102 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000103 }
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Richard Trieuf858bd82011-05-26 20:11:09 +0000105
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000106 BalancedDelimiterTracker T(*this, tok::l_brace);
107 if (T.consumeOpen()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000108 if (!ExtraIdent.empty()) {
109 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
110 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
111 }
Mike Stump1eb44332009-09-09 15:08:12 +0000112 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +0000113 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +0000114 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000115 }
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Douglas Gregor23c94db2010-07-02 17:43:08 +0000117 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
118 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
119 getCurScope()->getFnParent()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000120 if (!ExtraIdent.empty()) {
121 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
122 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
123 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000124 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Douglas Gregor95f1b152010-05-14 05:08:22 +0000125 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000126 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000127 }
128
Richard Trieuf858bd82011-05-26 20:11:09 +0000129 if (!ExtraIdent.empty()) {
130 TentativeParsingAction TPA(*this);
131 SkipUntil(tok::r_brace, /*StopAtSemi*/false, /*DontConsume*/true);
132 Token rBraceToken = Tok;
133 TPA.Revert();
134
135 if (!rBraceToken.is(tok::r_brace)) {
136 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
137 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
138 } else {
Benjamin Kramer9910df02011-05-26 21:32:30 +0000139 std::string NamespaceFix;
Richard Trieuf858bd82011-05-26 20:11:09 +0000140 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
141 E = ExtraIdent.end(); I != E; ++I) {
142 NamespaceFix += " { namespace ";
143 NamespaceFix += (*I)->getName();
144 }
Benjamin Kramer9910df02011-05-26 21:32:30 +0000145
Richard Trieuf858bd82011-05-26 20:11:09 +0000146 std::string RBraces;
Benjamin Kramer9910df02011-05-26 21:32:30 +0000147 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieuf858bd82011-05-26 20:11:09 +0000148 RBraces += "} ";
Benjamin Kramer9910df02011-05-26 21:32:30 +0000149
Richard Trieuf858bd82011-05-26 20:11:09 +0000150 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
151 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
152 ExtraIdentLoc.back()),
153 NamespaceFix)
154 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
155 }
156 }
157
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000158 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith7fe62082011-10-15 05:09:34 +0000159 if (InlineLoc.isValid())
Richard Smith80ad52f2013-01-02 11:42:31 +0000160 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000161 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000162
Chris Lattner51448322009-03-29 14:02:43 +0000163 // Enter a scope for the namespace.
164 ParseScope NamespaceScope(this, Scope::DeclScope);
165
John McCalld226f652010-08-21 09:40:31 +0000166 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000167 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000168 IdentLoc, Ident, T.getOpenLocation(),
169 attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000170
John McCallf312b1e2010-08-26 23:41:50 +0000171 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
172 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Richard Trieuf858bd82011-05-26 20:11:09 +0000174 // Parse the contents of the namespace. This includes parsing recovery on
175 // any improperly nested namespaces.
176 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000177 InlineLoc, attrs, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattner51448322009-03-29 14:02:43 +0000179 // Leave the namespace scope.
180 NamespaceScope.Exit();
181
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000182 DeclEnd = T.getCloseLocation();
183 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner51448322009-03-29 14:02:43 +0000184
185 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000186}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000187
Richard Trieuf858bd82011-05-26 20:11:09 +0000188/// ParseInnerNamespace - Parse the contents of a namespace.
189void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
190 std::vector<IdentifierInfo*>& Ident,
191 std::vector<SourceLocation>& NamespaceLoc,
192 unsigned int index, SourceLocation& InlineLoc,
Richard Trieuf858bd82011-05-26 20:11:09 +0000193 ParsedAttributes& attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000194 BalancedDelimiterTracker &Tracker) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000195 if (index == Ident.size()) {
196 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
197 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000198 MaybeParseCXX11Attributes(attrs);
Richard Trieuf858bd82011-05-26 20:11:09 +0000199 MaybeParseMicrosoftAttributes(attrs);
200 ParseExternalDeclaration(attrs);
201 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000202
203 // The caller is what called check -- we are simply calling
204 // the close for it.
205 Tracker.consumeClose();
Richard Trieuf858bd82011-05-26 20:11:09 +0000206
207 return;
208 }
209
210 // Parse improperly nested namespaces.
211 ParseScope NamespaceScope(this, Scope::DeclScope);
212 Decl *NamespcDecl =
213 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
214 NamespaceLoc[index], IdentLoc[index],
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000215 Ident[index], Tracker.getOpenLocation(),
216 attrs.getList());
Richard Trieuf858bd82011-05-26 20:11:09 +0000217
218 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000219 attrs, Tracker);
Richard Trieuf858bd82011-05-26 20:11:09 +0000220
221 NamespaceScope.Exit();
222
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000223 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieuf858bd82011-05-26 20:11:09 +0000224}
225
Anders Carlssonf67606a2009-03-28 04:07:16 +0000226/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
227/// alias definition.
228///
John McCalld226f652010-08-21 09:40:31 +0000229Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000230 SourceLocation AliasLoc,
231 IdentifierInfo *Alias,
232 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000233 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Anders Carlssonf67606a2009-03-28 04:07:16 +0000235 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000237 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000238 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000239 cutOffParsing();
240 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000241 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000242
Anders Carlssonf67606a2009-03-28 04:07:16 +0000243 CXXScopeSpec SS;
244 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000245 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000246
247 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
248 Diag(Tok, diag::err_expected_namespace_name);
249 // Skip to end of the definition and eat the ';'.
250 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000251 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000252 }
253
254 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000255 IdentifierInfo *Ident = Tok.getIdentifierInfo();
256 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Anders Carlssonf67606a2009-03-28 04:07:16 +0000258 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000259 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000260 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
261 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Douglas Gregor23c94db2010-07-02 17:43:08 +0000263 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000264 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000265}
266
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000267/// ParseLinkage - We know that the current token is a string_literal
268/// and just before that, that extern was seen.
269///
270/// linkage-specification: [C++ 7.5p2: dcl.link]
271/// 'extern' string-literal '{' declaration-seq[opt] '}'
272/// 'extern' string-literal declaration
273///
Chris Lattner7d642712010-11-09 20:15:55 +0000274Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000275 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000276 SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000277 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000278 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000279 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000280 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000281
Richard Smith99831e42012-03-06 03:21:47 +0000282 // FIXME: This is incorrect: linkage-specifiers are parsed in translation
283 // phase 7, so string-literal concatenation is supposed to occur.
284 // extern "" "C" "" "+" "+" { } is legal.
285 if (Tok.hasUDSuffix())
286 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000287 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000288
Douglas Gregor074149e2009-01-05 19:45:36 +0000289 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000290 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000291 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000292 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000293 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000294 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000295 : SourceLocation());
296
John McCall0b7e6782011-03-24 11:26:52 +0000297 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000298 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000299 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000300
Douglas Gregor074149e2009-01-05 19:45:36 +0000301 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-05-01 16:25:54 +0000302 // Reset the source range in DS, as the leading "extern"
303 // does not really belong to the inner declaration ...
304 DS.SetRangeStart(SourceLocation());
305 DS.SetRangeEnd(SourceLocation());
306 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000307 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000308 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000309 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000310 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000311 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000312
Douglas Gregor63a01132010-02-07 08:38:28 +0000313 DS.abort();
314
John McCall7f040a92010-12-24 02:08:15 +0000315 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000316
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000317 BalancedDelimiterTracker T(*this, tok::l_brace);
318 T.consumeOpen();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000319 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000320 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000321 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000322 MaybeParseMicrosoftAttributes(attrs);
323 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000324 }
325
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000326 T.consumeClose();
Chris Lattner7d642712010-11-09 20:15:55 +0000327 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000328 T.getCloseLocation());
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000329}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000330
Douglas Gregorf780abc2008-12-30 03:27:21 +0000331/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
332/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000333Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000334 const ParsedTemplateInfo &TemplateInfo,
335 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000336 ParsedAttributesWithRange &attrs,
337 Decl **OwnedType) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000338 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000339 ObjCDeclContextSwitch ObjCDC(*this);
340
Douglas Gregorf780abc2008-12-30 03:27:21 +0000341 // Eat 'using'.
342 SourceLocation UsingLoc = ConsumeToken();
343
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000344 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000345 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000346 cutOffParsing();
347 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000348 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000349
John McCall78b81052010-11-10 02:40:36 +0000350 // 'using namespace' means this is a using-directive.
351 if (Tok.is(tok::kw_namespace)) {
352 // Template parameters are always an error here.
353 if (TemplateInfo.Kind) {
354 SourceRange R = TemplateInfo.getSourceRange();
355 Diag(UsingLoc, diag::err_templated_using_directive)
356 << R << FixItHint::CreateRemoval(R);
357 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000358
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000359 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000360 }
361
Richard Smith162e1c12011-04-15 14:24:37 +0000362 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000363
364 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000365 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000366
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000367 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000368 AS_none, OwnedType);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000369}
370
371/// ParseUsingDirective - Parse C++ using-directive, assumes
372/// that current token is 'namespace' and 'using' was already parsed.
373///
374/// using-directive: [C++ 7.3.p4: namespace.udir]
375/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
376/// namespace-name ;
377/// [GNU] using-directive:
378/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
379/// namespace-name attributes[opt] ;
380///
John McCalld226f652010-08-21 09:40:31 +0000381Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000382 SourceLocation UsingLoc,
383 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000384 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000385 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
386
387 // Eat 'namespace'.
388 SourceLocation NamespcLoc = ConsumeToken();
389
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000390 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000391 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000392 cutOffParsing();
393 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000394 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000395
Douglas Gregorf780abc2008-12-30 03:27:21 +0000396 CXXScopeSpec SS;
397 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000398 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000399
Douglas Gregorf780abc2008-12-30 03:27:21 +0000400 IdentifierInfo *NamespcName = 0;
401 SourceLocation IdentLoc = SourceLocation();
402
403 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000404 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000405 Diag(Tok, diag::err_expected_namespace_name);
406 // If there was invalid namespace name, skip to end of decl, and eat ';'.
407 SkipUntil(tok::semi);
408 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000409 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Chris Lattner823c44e2009-01-06 07:27:21 +0000412 // Parse identifier.
413 NamespcName = Tok.getIdentifierInfo();
414 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Chris Lattner823c44e2009-01-06 07:27:21 +0000416 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000417 bool GNUAttr = false;
418 if (Tok.is(tok::kw___attribute)) {
419 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000420 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Chris Lattner823c44e2009-01-06 07:27:21 +0000423 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000424 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000425 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000426 GNUAttr ? diag::err_expected_semi_after_attribute_list
427 : diag::err_expected_semi_after_namespace_name,
428 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000429
Douglas Gregor23c94db2010-07-02 17:43:08 +0000430 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000431 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000432}
433
Richard Smith162e1c12011-04-15 14:24:37 +0000434/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
435/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000436///
437/// using-declaration: [C++ 7.3.p3: namespace.udecl]
438/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000439/// unqualified-id
440/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000441///
Richard Smith162e1c12011-04-15 14:24:37 +0000442/// alias-declaration: C++0x [decl.typedef]p2
443/// 'using' identifier = type-id ;
444///
John McCalld226f652010-08-21 09:40:31 +0000445Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000446 const ParsedTemplateInfo &TemplateInfo,
447 SourceLocation UsingLoc,
448 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000449 AccessSpecifier AS,
450 Decl **OwnedType) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000451 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000452 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000453 bool IsTypeName;
Sean Hunt2edf0a22012-06-23 05:07:58 +0000454 ParsedAttributesWithRange attrs(AttrFactory);
455
456 // FIXME: Simply skip the attributes and diagnose, don't bother parsing them.
Richard Smith4e24f0f2013-01-02 12:01:23 +0000457 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000458 ProhibitAttributes(attrs);
459 attrs.clear();
460 attrs.Range = SourceRange();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000461
462 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000463 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000464 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000465 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000466 ConsumeToken();
467 IsTypeName = true;
468 }
469 else
470 IsTypeName = false;
471
472 // Parse nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000473 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000474
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000475 // Check nested-name specifier.
476 if (SS.isInvalid()) {
477 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000478 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000479 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000480
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000481 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000482 // destructor names and allow the action module to diagnose any semantic
483 // errors.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000484 SourceLocation TemplateKWLoc;
Douglas Gregor12c118a2009-11-04 16:30:06 +0000485 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000486 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000487 /*EnteringContext=*/false,
488 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000489 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000490 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000491 TemplateKWLoc,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000492 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000493 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000494 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000495 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000496
Richard Smith4e24f0f2013-01-02 12:01:23 +0000497 MaybeParseCXX11Attributes(attrs);
Richard Smith162e1c12011-04-15 14:24:37 +0000498
499 // Maybe this is an alias-declaration.
500 bool IsAliasDecl = Tok.is(tok::equal);
501 TypeResult TypeAlias;
502 if (IsAliasDecl) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000503 // TODO: Attribute support. C++0x attributes may appear before the equals.
504 // Where can GNU attributes appear?
Richard Smith162e1c12011-04-15 14:24:37 +0000505 ConsumeToken();
506
Richard Smith80ad52f2013-01-02 11:42:31 +0000507 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000508 diag::warn_cxx98_compat_alias_declaration :
509 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000510
Richard Smith3e4c6c42011-05-05 21:57:07 +0000511 // Type alias templates cannot be specialized.
512 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000513 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
514 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000515 SpecKind = 0;
516 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
517 SpecKind = 1;
518 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
519 SpecKind = 2;
520 if (SpecKind != -1) {
521 SourceRange Range;
522 if (SpecKind == 0)
523 Range = SourceRange(Name.TemplateId->LAngleLoc,
524 Name.TemplateId->RAngleLoc);
525 else
526 Range = TemplateInfo.getSourceRange();
527 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
528 << SpecKind << Range;
529 SkipUntil(tok::semi);
530 return 0;
531 }
532
Richard Smith162e1c12011-04-15 14:24:37 +0000533 // Name must be an identifier.
534 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
535 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
536 // No removal fixit: can't recover from this.
537 SkipUntil(tok::semi);
538 return 0;
539 } else if (IsTypeName)
540 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
541 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
542 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
543 else if (SS.isNotEmpty())
544 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
545 << FixItHint::CreateRemoval(SS.getRange());
546
Richard Smith3e4c6c42011-05-05 21:57:07 +0000547 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
548 Declarator::AliasTemplateContext :
John McCallcdda47f2011-10-01 09:56:14 +0000549 Declarator::AliasDeclContext, AS, OwnedType);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000550 } else {
551 // C++11 attributes are not allowed on a using-declaration, but GNU ones
552 // are.
553 ProhibitAttributes(attrs);
554
Richard Smith162e1c12011-04-15 14:24:37 +0000555 // Parse (optional) attributes (most likely GNU strong-using extension).
556 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000557 }
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000559 // Eat ';'.
560 DeclEnd = Tok.getLocation();
561 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith162e1c12011-04-15 14:24:37 +0000562 !attrs.empty() ? "attributes list" :
563 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000564 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000565
John McCall78b81052010-11-10 02:40:36 +0000566 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith3e4c6c42011-05-05 21:57:07 +0000567 // In C++0x, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000568 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000569 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000570 SourceRange R = TemplateInfo.getSourceRange();
571 Diag(UsingLoc, diag::err_templated_using_declaration)
572 << R << FixItHint::CreateRemoval(R);
573
574 // Unfortunately, we have to bail out instead of recovering by
575 // ignoring the parameters, just in case the nested name specifier
576 // depends on the parameters.
577 return 0;
578 }
579
Douglas Gregor480b53c2011-09-26 14:30:28 +0000580 // "typename" keyword is allowed for identifiers only,
581 // because it may be a type definition.
582 if (IsTypeName && Name.getKind() != UnqualifiedId::IK_Identifier) {
583 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
584 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
585 // Proceed parsing, but reset the IsTypeName flag.
586 IsTypeName = false;
587 }
588
Richard Smith3e4c6c42011-05-05 21:57:07 +0000589 if (IsAliasDecl) {
590 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramer5354e772012-08-23 23:38:35 +0000591 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3e4c6c42011-05-05 21:57:07 +0000592 TemplateParams ? TemplateParams->data() : 0,
593 TemplateParams ? TemplateParams->size() : 0);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000594 // FIXME: Propagate attributes.
Richard Smith3e4c6c42011-05-05 21:57:07 +0000595 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
596 UsingLoc, Name, TypeAlias);
597 }
Richard Smith162e1c12011-04-15 14:24:37 +0000598
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000599 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000600 Name, attrs.getList(),
601 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000602}
603
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000604/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000605///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000606/// [C++0x] static_assert-declaration:
607/// static_assert ( constant-expression , string-literal ) ;
608///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000609/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000610/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000611///
John McCalld226f652010-08-21 09:40:31 +0000612Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000613 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
614 "Not a static_assert declaration");
615
David Blaikie4e4d0842012-03-11 07:00:24 +0000616 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000617 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000618 if (Tok.is(tok::kw_static_assert))
619 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000620
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000621 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000623 BalancedDelimiterTracker T(*this, tok::l_paren);
624 if (T.consumeOpen()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000625 Diag(Tok, diag::err_expected_lparen);
Richard Smith3686c712012-09-13 19:12:50 +0000626 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000627 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000628 }
Mike Stump1eb44332009-09-09 15:08:12 +0000629
John McCall60d7b3a2010-08-24 06:29:42 +0000630 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000631 if (AssertExpr.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000632 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000633 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000634 }
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Anders Carlssonad5f9602009-03-13 23:29:20 +0000636 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000637 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000638
Richard Smith0cc323c2012-03-05 23:20:05 +0000639 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000640 Diag(Tok, diag::err_expected_string_literal)
641 << /*Source='static_assert'*/1;
Richard Smith3686c712012-09-13 19:12:50 +0000642 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000643 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000644 }
Mike Stump1eb44332009-09-09 15:08:12 +0000645
John McCall60d7b3a2010-08-24 06:29:42 +0000646 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smith99831e42012-03-06 03:21:47 +0000647 if (AssertMessage.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000648 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000649 return 0;
Richard Smith99831e42012-03-06 03:21:47 +0000650 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000651
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000652 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Chris Lattner97144fc2009-04-02 04:16:50 +0000654 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000655 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000656
John McCall9ae2f072010-08-23 23:25:46 +0000657 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
658 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000659 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000660 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000661}
662
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000663/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
664///
665/// 'decltype' ( expression )
666///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000667SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
668 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
669 && "Not a decltype specifier");
670
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000671
David Blaikie42d6d0c2011-12-04 05:04:18 +0000672 ExprResult Result;
673 SourceLocation StartLoc = Tok.getLocation();
674 SourceLocation EndLoc;
675
676 if (Tok.is(tok::annot_decltype)) {
677 Result = getExprAnnotation(Tok);
678 EndLoc = Tok.getAnnotationEndLoc();
679 ConsumeToken();
680 if (Result.isInvalid()) {
681 DS.SetTypeSpecError();
682 return EndLoc;
683 }
684 } else {
Richard Smithc7b55432012-02-24 22:30:04 +0000685 if (Tok.getIdentifierInfo()->isStr("decltype"))
686 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000687
David Blaikie42d6d0c2011-12-04 05:04:18 +0000688 ConsumeToken();
689
690 BalancedDelimiterTracker T(*this, tok::l_paren);
691 if (T.expectAndConsume(diag::err_expected_lparen_after,
692 "decltype", tok::r_paren)) {
693 DS.SetTypeSpecError();
694 return T.getOpenLocation() == Tok.getLocation() ?
695 StartLoc : T.getOpenLocation();
696 }
697
698 // Parse the expression
699
700 // C++0x [dcl.type.simple]p4:
701 // The operand of the decltype specifier is an unevaluated operand.
Richard Smith76f3f692012-02-22 02:04:18 +0000702 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
703 0, /*IsDecltype=*/true);
David Blaikie42d6d0c2011-12-04 05:04:18 +0000704 Result = ParseExpression();
705 if (Result.isInvalid()) {
David Blaikie42d6d0c2011-12-04 05:04:18 +0000706 DS.SetTypeSpecError();
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000707 if (SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true)) {
708 EndLoc = ConsumeParen();
709 } else {
Richard Smith569cdc82012-12-09 04:17:57 +0000710 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000711 // Backtrack to get the location of the last token before the semi.
712 PP.RevertCachedTokens(2);
713 ConsumeToken(); // the semi.
714 EndLoc = ConsumeAnyToken();
715 assert(Tok.is(tok::semi));
716 } else {
717 EndLoc = Tok.getLocation();
718 }
719 }
720 return EndLoc;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000721 }
722
723 // Match the ')'
724 T.consumeClose();
725 if (T.getCloseLocation().isInvalid()) {
726 DS.SetTypeSpecError();
727 // FIXME: this should return the location of the last token
728 // that was consumed (by "consumeClose()")
729 return T.getCloseLocation();
730 }
731
Richard Smith76f3f692012-02-22 02:04:18 +0000732 Result = Actions.ActOnDecltypeExpression(Result.take());
733 if (Result.isInvalid()) {
734 DS.SetTypeSpecError();
735 return T.getCloseLocation();
736 }
737
David Blaikie42d6d0c2011-12-04 05:04:18 +0000738 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000739 }
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000741 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000742 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000743 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000744 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
David Blaikie42d6d0c2011-12-04 05:04:18 +0000745 DiagID, Result.release())) {
John McCallfec54012009-08-03 20:12:06 +0000746 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000747 DS.SetTypeSpecError();
748 }
749 return EndLoc;
750}
751
752void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
753 SourceLocation StartLoc,
754 SourceLocation EndLoc) {
755 // make sure we have a token we can turn into an annotation token
756 if (PP.isBacktrackEnabled())
757 PP.RevertCachedTokens(1);
758 else
759 PP.EnterToken(Tok);
760
761 Tok.setKind(tok::annot_decltype);
762 setExprAnnotation(Tok, DS.getTypeSpecType() == TST_decltype ?
763 DS.getRepAsExpr() : ExprResult());
764 Tok.setAnnotationEndLoc(EndLoc);
765 Tok.setLocation(StartLoc);
766 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000767}
768
Sean Huntdb5d44b2011-05-19 05:37:45 +0000769void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
770 assert(Tok.is(tok::kw___underlying_type) &&
771 "Not an underlying type specifier");
772
773 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000774 BalancedDelimiterTracker T(*this, tok::l_paren);
775 if (T.expectAndConsume(diag::err_expected_lparen_after,
776 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000777 return;
778 }
779
780 TypeResult Result = ParseTypeName();
781 if (Result.isInvalid()) {
782 SkipUntil(tok::r_paren);
783 return;
784 }
785
786 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000787 T.consumeClose();
788 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000789 return;
790
791 const char *PrevSpec = 0;
792 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000793 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000794 DiagID, Result.release()))
795 Diag(StartLoc, DiagID) << PrevSpec;
796}
797
David Blaikie09048df2011-10-25 15:01:20 +0000798/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
799/// class name or decltype-specifier. Note that we only check that the result
800/// names a type; semantic analysis will need to verify that the type names a
801/// class. The result is either a type or null, depending on whether a type
802/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000803///
David Blaikie09048df2011-10-25 15:01:20 +0000804/// base-type-specifier: [C++ 10.1]
805/// class-or-decltype
806/// class-or-decltype: [C++ 10.1]
807/// nested-name-specifier[opt] class-name
808/// decltype-specifier
Douglas Gregor42a552f2008-11-05 20:51:48 +0000809/// class-name: [C++ 9.1]
810/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000811/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000812///
David Blaikie22216eb2011-10-25 17:10:12 +0000813Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
814 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000815 // Ignore attempts to use typename
816 if (Tok.is(tok::kw_typename)) {
817 Diag(Tok, diag::err_expected_class_name_not_template)
818 << FixItHint::CreateRemoval(Tok.getLocation());
819 ConsumeToken();
820 }
821
David Blaikie152aa4b2011-10-25 18:17:58 +0000822 // Parse optional nested-name-specifier
823 CXXScopeSpec SS;
824 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
825
826 BaseLoc = Tok.getLocation();
827
David Blaikie22216eb2011-10-25 17:10:12 +0000828 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000829 // tok == kw_decltype is just error recovery, it can only happen when SS
830 // isn't empty
831 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000832 if (SS.isNotEmpty())
833 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
834 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000835 // Fake up a Declarator to use with ActOnTypeName.
836 DeclSpec DS(AttrFactory);
837
David Blaikieb5777572011-12-08 04:53:15 +0000838 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000839
840 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
841 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
842 }
843
Douglas Gregor7f43d672009-02-25 23:52:28 +0000844 // Check whether we have a template-id that names a type.
845 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000846 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000847 if (TemplateId->Kind == TNK_Type_template ||
848 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000849 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000850
851 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000852 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000853 EndLocation = Tok.getAnnotationEndLoc();
854 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000855
856 if (Type)
857 return Type;
858 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000859 }
860
861 // Fall through to produce an error below.
862 }
863
Douglas Gregor42a552f2008-11-05 20:51:48 +0000864 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000865 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000866 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000867 }
868
Douglas Gregor84d0a192010-01-12 21:28:44 +0000869 IdentifierInfo *Id = Tok.getIdentifierInfo();
870 SourceLocation IdLoc = ConsumeToken();
871
872 if (Tok.is(tok::less)) {
873 // It looks the user intended to write a template-id here, but the
874 // template-name was wrong. Try to fix that.
875 TemplateNameKind TNK = TNK_Type_template;
876 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000877 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000878 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000879 Diag(IdLoc, diag::err_unknown_template_name)
880 << Id;
881 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000882
Douglas Gregor84d0a192010-01-12 21:28:44 +0000883 if (!Template)
884 return true;
885
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000886 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000887 UnqualifiedId TemplateName;
888 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000889
Douglas Gregor84d0a192010-01-12 21:28:44 +0000890 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000891 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
892 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000893 return true;
894 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000895 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000896
Douglas Gregor84d0a192010-01-12 21:28:44 +0000897 // If we didn't end up with a typename token, there's nothing more we
898 // can do.
899 if (Tok.isNot(tok::annot_typename))
900 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000901
Douglas Gregor84d0a192010-01-12 21:28:44 +0000902 // Retrieve the type from the annotation token, consume that token, and
903 // return.
904 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000905 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000906 ConsumeToken();
907 return Type;
908 }
909
Douglas Gregor42a552f2008-11-05 20:51:48 +0000910 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000911 IdentifierInfo *CorrectedII = 0;
Douglas Gregor059101f2011-03-02 00:47:37 +0000912 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000913 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000914 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000915 /*NonTrivialTypeSourceInfo=*/true,
916 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000917 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000918 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000919 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000920 }
921
922 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000923 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000924
925 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000926 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000927 DS.SetRangeStart(IdLoc);
928 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000929 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000930
931 const char *PrevSpec = 0;
932 unsigned DiagID;
933 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
934
935 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
936 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000937}
938
John McCallc052dbb2012-05-22 21:28:12 +0000939void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
940 while (Tok.is(tok::kw___single_inheritance) ||
941 Tok.is(tok::kw___multiple_inheritance) ||
942 Tok.is(tok::kw___virtual_inheritance)) {
943 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
944 SourceLocation AttrNameLoc = ConsumeToken();
945 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000946 SourceLocation(), 0, 0, AttributeList::AS_GNU);
John McCallc052dbb2012-05-22 21:28:12 +0000947 }
948}
949
Richard Smithc9f35172012-06-25 21:37:02 +0000950/// Determine whether the following tokens are valid after a type-specifier
951/// which could be a standalone declaration. This will conservatively return
952/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith139be702012-07-02 19:14:01 +0000953bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smithc9f35172012-06-25 21:37:02 +0000954 // This switch enumerates the valid "follow" set for type-specifiers.
955 switch (Tok.getKind()) {
956 default: break;
957 case tok::semi: // struct foo {...} ;
958 case tok::star: // struct foo {...} * P;
959 case tok::amp: // struct foo {...} & R = ...
960 case tok::identifier: // struct foo {...} V ;
961 case tok::r_paren: //(struct foo {...} ) {4}
962 case tok::annot_cxxscope: // struct foo {...} a:: b;
963 case tok::annot_typename: // struct foo {...} a ::b;
964 case tok::annot_template_id: // struct foo {...} a<int> ::b;
965 case tok::l_paren: // struct foo {...} ( x);
966 case tok::comma: // __builtin_offsetof(struct foo{...} ,
967 return true;
Richard Smith139be702012-07-02 19:14:01 +0000968 case tok::colon:
969 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smithc9f35172012-06-25 21:37:02 +0000970 // Type qualifiers
971 case tok::kw_const: // struct foo {...} const x;
972 case tok::kw_volatile: // struct foo {...} volatile x;
973 case tok::kw_restrict: // struct foo {...} restrict x;
974 case tok::kw_inline: // struct foo {...} inline foo() {};
975 // Storage-class specifiers
976 case tok::kw_static: // struct foo {...} static x;
977 case tok::kw_extern: // struct foo {...} extern x;
978 case tok::kw_typedef: // struct foo {...} typedef x;
979 case tok::kw_register: // struct foo {...} register x;
980 case tok::kw_auto: // struct foo {...} auto x;
981 case tok::kw_mutable: // struct foo {...} mutable x;
982 case tok::kw_constexpr: // struct foo {...} constexpr x;
983 // As shown above, type qualifiers and storage class specifiers absolutely
984 // can occur after class specifiers according to the grammar. However,
985 // almost no one actually writes code like this. If we see one of these,
986 // it is much more likely that someone missed a semi colon and the
987 // type/storage class specifier we're seeing is part of the *next*
988 // intended declaration, as in:
989 //
990 // struct foo { ... }
991 // typedef int X;
992 //
993 // We'd really like to emit a missing semicolon error instead of emitting
994 // an error on the 'int' saying that you can't have two type specifiers in
995 // the same declaration of X. Because of this, we look ahead past this
996 // token to see if it's a type specifier. If so, we know the code is
997 // otherwise invalid, so we can produce the expected semi error.
998 if (!isKnownToBeTypeSpecifier(NextToken()))
999 return true;
1000 break;
1001 case tok::r_brace: // struct bar { struct foo {...} }
1002 // Missing ';' at end of struct is accepted as an extension in C mode.
1003 if (!getLangOpts().CPlusPlus)
1004 return true;
1005 break;
1006 }
1007 return false;
1008}
1009
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001010/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1011/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1012/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +00001013/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001014///
1015/// class-specifier: [C++ class]
1016/// class-head '{' member-specification[opt] '}'
1017/// class-head '{' member-specification[opt] '}' attributes[opt]
1018/// class-head:
1019/// class-key identifier[opt] base-clause[opt]
1020/// class-key nested-name-specifier identifier base-clause[opt]
1021/// class-key nested-name-specifier[opt] simple-template-id
1022/// base-clause[opt]
1023/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001024/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001025/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001026/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001027/// simple-template-id base-clause[opt]
1028/// class-key:
1029/// 'class'
1030/// 'struct'
1031/// 'union'
1032///
1033/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +00001034/// class-key ::[opt] nested-name-specifier[opt] identifier
1035/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1036/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001037///
1038/// Note that the C++ class-specifier and elaborated-type-specifier,
1039/// together, subsume the C99 struct-or-union-specifier:
1040///
1041/// struct-or-union-specifier: [C99 6.7.2.1]
1042/// struct-or-union identifier[opt] '{' struct-contents '}'
1043/// struct-or-union identifier
1044/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1045/// '}' attributes[opt]
1046/// [GNU] struct-or-union attributes[opt] identifier
1047/// struct-or-union:
1048/// 'struct'
1049/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +00001050void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1051 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001052 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001053 AccessSpecifier AS,
Michael Han2e397132012-11-26 22:54:45 +00001054 bool EnteringContext, DeclSpecContext DSC,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001055 ParsedAttributesWithRange &Attributes) {
Joao Matos17d35c32012-08-31 22:18:20 +00001056 DeclSpec::TST TagType;
1057 if (TagTokKind == tok::kw_struct)
1058 TagType = DeclSpec::TST_struct;
1059 else if (TagTokKind == tok::kw___interface)
1060 TagType = DeclSpec::TST_interface;
1061 else if (TagTokKind == tok::kw_class)
1062 TagType = DeclSpec::TST_class;
1063 else {
Chris Lattner4c97d762009-04-12 21:49:30 +00001064 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1065 TagType = DeclSpec::TST_union;
1066 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001067
Douglas Gregor374929f2009-09-18 15:37:17 +00001068 if (Tok.is(tok::code_completion)) {
1069 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001070 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001071 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00001072 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001073
Chandler Carruth926c4b42010-06-28 08:39:25 +00001074 // C++03 [temp.explicit] 14.7.2/8:
1075 // The usual access checking rules do not apply to names used to specify
1076 // explicit instantiations.
1077 //
1078 // As an extension we do not perform access checking on the names used to
1079 // specify explicit specializations either. This is important to allow
1080 // specializing traits classes for private types.
John McCall13489672012-05-07 06:16:58 +00001081 //
1082 // Note that we don't suppress if this turns out to be an elaborated
1083 // type specifier.
1084 bool shouldDelayDiagsInTag =
1085 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1086 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1087 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001088
Sean Hunt2edf0a22012-06-23 05:07:58 +00001089 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001090 // If attributes exist after tag, parse them.
1091 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +00001092 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001093
Steve Narofff59e17e2008-12-24 20:59:21 +00001094 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001095 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001096 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001097
John McCallc052dbb2012-05-22 21:28:12 +00001098 // Parse inheritance specifiers.
1099 if (Tok.is(tok::kw___single_inheritance) ||
1100 Tok.is(tok::kw___multiple_inheritance) ||
1101 Tok.is(tok::kw___virtual_inheritance))
1102 ParseMicrosoftInheritanceClassAttributes(attrs);
1103
Sean Huntbbd37c62009-11-21 08:43:09 +00001104 // If C++0x attributes exist here, parse them.
1105 // FIXME: Are we consistent with the ordering of parsing of different
1106 // styles of attributes?
Richard Smith4e24f0f2013-01-02 12:01:23 +00001107 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001108
Michael Han07fc1ba2013-01-07 16:57:11 +00001109 // Source location used by FIXIT to insert misplaced
1110 // C++11 attributes
1111 SourceLocation AttrFixitLoc = Tok.getLocation();
1112
John Wiegley20c0da72011-04-27 23:09:49 +00001113 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +00001114 !Tok.is(tok::identifier) &&
1115 Tok.getIdentifierInfo() &&
1116 (Tok.is(tok::kw___is_arithmetic) ||
1117 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001118 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001119 Tok.is(tok::kw___is_floating_point) ||
1120 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001121 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001122 Tok.is(tok::kw___is_integral) ||
1123 Tok.is(tok::kw___is_member_function_pointer) ||
1124 Tok.is(tok::kw___is_member_pointer) ||
1125 Tok.is(tok::kw___is_pod) ||
1126 Tok.is(tok::kw___is_pointer) ||
1127 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001128 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001129 Tok.is(tok::kw___is_signed) ||
1130 Tok.is(tok::kw___is_unsigned) ||
1131 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001132 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001133 // name of struct templates, but some are keywords in GCC >= 4.3
1134 // and Clang. Therefore, when we see the token sequence "struct
1135 // X", make X into a normal identifier rather than a keyword, to
1136 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001137 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001138 Tok.setKind(tok::identifier);
1139 }
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001141 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001142 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001143 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001144 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1145 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001146
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001147 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001148 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001149 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001150 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1151 Diag(Tok, diag::err_expected_ident);
1152 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001153
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001154 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1155
Douglas Gregorcc636682009-02-17 23:15:12 +00001156 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001157 IdentifierInfo *Name = 0;
1158 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001159 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001160 if (Tok.is(tok::identifier)) {
1161 Name = Tok.getIdentifierInfo();
1162 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001163
David Blaikie4e4d0842012-03-11 07:00:24 +00001164 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001165 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001166 // Eat the template argument list and try to continue parsing this as
1167 // a class (or template thereof).
1168 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001169 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001170 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001171 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001172 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001173 // We couldn't parse the template argument list at all, so don't
1174 // try to give any location information for the list.
1175 LAngleLoc = RAngleLoc = SourceLocation();
1176 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001177
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001178 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matos17d35c32012-08-31 22:18:20 +00001179 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1180 << (TagType == DeclSpec::TST_class? 0
1181 : TagType == DeclSpec::TST_struct? 1
1182 : TagType == DeclSpec::TST_interface? 2
1183 : 3)
1184 << Name
1185 << SourceRange(LAngleLoc, RAngleLoc);
1186
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001187 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001188 // we've removed its template argument list.
1189 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1190 if (TemplateParams && TemplateParams->size() > 1) {
1191 TemplateParams->pop_back();
1192 } else {
1193 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001194 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001195 = ParsedTemplateInfo::NonTemplate;
1196 }
1197 } else if (TemplateInfo.Kind
1198 == ParsedTemplateInfo::ExplicitInstantiation) {
1199 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001200 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001201 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001202 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001203 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001204 = SourceLocation();
1205 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1206 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001207 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001208 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001209 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001210 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001211 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001212
Douglas Gregor059101f2011-03-02 00:47:37 +00001213 if (TemplateId->Kind != TNK_Type_template &&
1214 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001215 // The template-name in the simple-template-id refers to
1216 // something other than a class template. Give an appropriate
1217 // error message and skip to the ';'.
1218 SourceRange Range(NameLoc);
1219 if (SS.isNotEmpty())
1220 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001221
Douglas Gregor39a8de12009-02-25 19:37:18 +00001222 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1223 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Douglas Gregor39a8de12009-02-25 19:37:18 +00001225 DS.SetTypeSpecError();
1226 SkipUntil(tok::semi, false, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001227 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001228 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001229 }
1230
Richard Smith7796eb52012-03-12 08:56:40 +00001231 // There are four options here.
1232 // - If we are in a trailing return type, this is always just a reference,
1233 // and we must not try to parse a definition. For instance,
1234 // [] () -> struct S { };
1235 // does not define a type.
1236 // - If we have 'struct foo {...', 'struct foo :...',
1237 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1238 // - If we have 'struct foo;', then this is either a forward declaration
1239 // or a friend declaration, which have to be treated differently.
1240 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han2e397132012-11-26 22:54:45 +00001241 //
1242 // We also detect these erroneous cases to provide better diagnostic for
1243 // C++11 attributes parsing.
1244 // - attributes follow class name:
1245 // struct foo [[]] {};
1246 // - attributes appear before or after 'final':
1247 // struct foo [[]] final [[]] {};
1248 //
Richard Smith69730c12012-03-12 07:56:15 +00001249 // However, in type-specifier-seq's, things look like declarations but are
1250 // just references, e.g.
1251 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001252 // or
Richard Smith69730c12012-03-12 07:56:15 +00001253 // &T::operator struct s;
1254 // For these, DSC is DSC_type_specifier.
Michael Han2e397132012-11-26 22:54:45 +00001255
1256 // If there are attributes after class name, parse them.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001257 MaybeParseCXX11Attributes(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00001258
John McCallf312b1e2010-08-26 23:41:50 +00001259 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001260 if (DSC == DSC_trailing)
1261 TUK = Sema::TUK_Reference;
1262 else if (Tok.is(tok::l_brace) ||
1263 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001264 (isCXX11FinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001265 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001266 if (DS.isFriendSpecified()) {
1267 // C++ [class.friend]p2:
1268 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001269 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001270 << SourceRange(DS.getFriendSpecLoc());
1271
1272 // Skip everything up to the semicolon, so that this looks like a proper
1273 // friend class (or template thereof) declaration.
1274 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001275 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001276 } else {
1277 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001278 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001279 }
Richard Smith4e24f0f2013-01-02 12:01:23 +00001280 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
Michael Han2e397132012-11-26 22:54:45 +00001281 NextToken().is(tok::kw_alignas) ||
1282 NextToken().is(tok::kw__Alignas))) {
1283 // We can't tell if this is a definition or reference
1284 // until we skipped the 'final' and C++11 attribute specifiers.
1285 TentativeParsingAction PA(*this);
1286
1287 // Skip the 'final' keyword.
1288 ConsumeToken();
1289
1290 // Skip C++11 attribute specifiers.
1291 while (true) {
1292 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1293 ConsumeBracket();
1294 if (!SkipUntil(tok::r_square))
1295 break;
1296 } else if ((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1297 NextToken().is(tok::l_paren)) {
1298 ConsumeToken();
1299 ConsumeParen();
1300 if (!SkipUntil(tok::r_paren))
1301 break;
1302 } else {
1303 break;
1304 }
1305 }
1306
1307 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1308 TUK = Sema::TUK_Definition;
1309 else
1310 TUK = Sema::TUK_Reference;
1311
1312 PA.Revert();
Richard Smithc9f35172012-06-25 21:37:02 +00001313 } else if (DSC != DSC_type_specifier &&
1314 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00001315 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallf312b1e2010-08-26 23:41:50 +00001316 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matos17d35c32012-08-31 22:18:20 +00001317 if (Tok.isNot(tok::semi)) {
1318 // A semicolon was missing after this declaration. Diagnose and recover.
1319 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1320 DeclSpec::getSpecifierName(TagType));
1321 PP.EnterToken(Tok);
1322 Tok.setKind(tok::semi);
1323 }
Richard Smithc9f35172012-06-25 21:37:02 +00001324 } else
John McCallf312b1e2010-08-26 23:41:50 +00001325 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001326
Michael Han2e397132012-11-26 22:54:45 +00001327 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1328 // to caller to handle.
Michael Han07fc1ba2013-01-07 16:57:11 +00001329 if (TUK != Sema::TUK_Reference) {
1330 // If this is not a reference, then the only possible
1331 // valid place for C++11 attributes to appear here
1332 // is between class-key and class-name. If there are
1333 // any attributes after class-name, we try a fixit to move
1334 // them to the right place.
1335 SourceRange AttrRange = Attributes.Range;
1336 if (AttrRange.isValid()) {
1337 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1338 << AttrRange
1339 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1340 CharSourceRange(AttrRange, true))
1341 << FixItHint::CreateRemoval(AttrRange);
1342
1343 // Recover by adding misplaced attributes to the attribute list
1344 // of the class so they can be applied on the class later.
1345 attrs.takeAllFrom(Attributes);
1346 }
1347 }
Michael Han2e397132012-11-26 22:54:45 +00001348
John McCall13489672012-05-07 06:16:58 +00001349 // If this is an elaborated type specifier, and we delayed
1350 // diagnostics before, just merge them into the current pool.
1351 if (shouldDelayDiagsInTag) {
1352 diagsFromTag.done();
1353 if (TUK == Sema::TUK_Reference)
1354 diagsFromTag.redelay();
1355 }
1356
John McCall207014e2010-07-30 06:26:29 +00001357 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001358 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001359 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1360 // We have a declaration or reference to an anonymous class.
1361 Diag(StartLoc, diag::err_anon_type_definition)
1362 << DeclSpec::getSpecifierName(TagType);
1363 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001364
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001365 SkipUntil(tok::comma, true);
1366 return;
1367 }
1368
Douglas Gregorddc29e12009-02-06 22:42:48 +00001369 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001370 DeclResult TagOrTempResult = true; // invalid
1371 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001372
Douglas Gregor402abb52009-05-28 23:31:59 +00001373 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001374 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001375 // Explicit specialization, class template partial specialization,
1376 // or explicit instantiation.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001377 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001378 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001379 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001380 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001381 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001382 ProhibitAttributes(attrs);
1383
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001384 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001385 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001386 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001387 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001388 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001389 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001390 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001391 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001392 TemplateId->TemplateNameLoc,
1393 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001394 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001395 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001396 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001397
1398 // Friend template-ids are treated as references unless
1399 // they have template headers, in which case they're ill-formed
1400 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1401 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001402 } else if (TUK == Sema::TUK_Reference ||
1403 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001404 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001405 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001406 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001407 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001408 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001409 TemplateId->Template,
1410 TemplateId->TemplateNameLoc,
1411 TemplateId->LAngleLoc,
1412 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001413 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001414 } else {
1415 // This is an explicit specialization or a class template
1416 // partial specialization.
1417 TemplateParameterLists FakedParamLists;
1418
1419 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1420 // This looks like an explicit instantiation, because we have
1421 // something like
1422 //
1423 // template class Foo<X>
1424 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001425 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001426 // meant to be an explicit specialization, but the user forgot
1427 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +00001428 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001429
Mike Stump1eb44332009-09-09 15:08:12 +00001430 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001431 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001432 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001433 diag::err_explicit_instantiation_with_definition)
1434 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +00001435 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001436
1437 // Create a fake template parameter list that contains only
1438 // "template<>", so that we treat this construct as a class
1439 // template specialization.
1440 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +00001441 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001442 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001443 LAngleLoc,
1444 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001445 LAngleLoc));
1446 TemplateParams = &FakedParamLists;
1447 }
1448
1449 // Build the class template specialization.
1450 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001451 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001452 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001453 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001454 TemplateId->TemplateNameLoc,
1455 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001456 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001457 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001458 attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001459 MultiTemplateParamsArg(
Douglas Gregorcc636682009-02-17 23:15:12 +00001460 TemplateParams? &(*TemplateParams)[0] : 0,
1461 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001462 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001463 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001464 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001465 // Explicit instantiation of a member of a class template
1466 // specialization, e.g.,
1467 //
1468 // template struct Outer<int>::Inner;
1469 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001470 ProhibitAttributes(attrs);
1471
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001472 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001473 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001474 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001475 TemplateInfo.TemplateLoc,
1476 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001477 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001478 } else if (TUK == Sema::TUK_Friend &&
1479 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001480 ProhibitAttributes(attrs);
1481
John McCall9a34edb2010-10-19 01:40:49 +00001482 TagOrTempResult =
1483 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1484 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001485 Name, NameLoc, attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001486 MultiTemplateParamsArg(
John McCall9a34edb2010-10-19 01:40:49 +00001487 TemplateParams? &(*TemplateParams)[0] : 0,
1488 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001489 } else {
1490 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001491 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001492 // FIXME: Diagnose this particular error.
1493 }
1494
Sean Hunt2edf0a22012-06-23 05:07:58 +00001495 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1496 ProhibitAttributes(attrs);
1497
John McCallc4e70192009-09-11 04:59:25 +00001498 bool IsDependent = false;
1499
John McCalla25c4082010-10-19 18:40:57 +00001500 // Don't pass down template parameter lists if this is just a tag
1501 // reference. For example, we don't need the template parameters here:
1502 // template <class T> class A *makeA(T t);
1503 MultiTemplateParamsArg TParams;
1504 if (TUK != Sema::TUK_Reference && TemplateParams)
1505 TParams =
1506 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1507
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001508 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001509 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001510 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001511 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001512 TParams, Owned, IsDependent,
1513 SourceLocation(), false,
1514 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001515
1516 // If ActOnTag said the type was dependent, try again with the
1517 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001518 if (IsDependent) {
1519 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001520 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001521 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001522 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001523 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001524
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001525 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001526 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001527 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001528 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001529 isCXX11FinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001530 if (getLangOpts().CPlusPlus)
Michael Han07fc1ba2013-01-07 16:57:11 +00001531 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1532 TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001533 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001534 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001535 }
1536
John McCallb3d87482010-08-24 05:47:05 +00001537 const char *PrevSpec = 0;
1538 unsigned DiagID;
1539 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001540 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001541 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1542 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001543 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001544 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001545 Result = DS.SetTypeSpecType(TagType, StartLoc,
1546 NameLoc.isValid() ? NameLoc : StartLoc,
1547 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001548 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001549 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001550 return;
1551 }
Mike Stump1eb44332009-09-09 15:08:12 +00001552
John McCallb3d87482010-08-24 05:47:05 +00001553 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001554 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001555
Chris Lattner4ed5d912010-02-02 01:23:29 +00001556 // At this point, we've successfully parsed a class-specifier in 'definition'
1557 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1558 // going to look at what comes after it to improve error recovery. If an
1559 // impossible token occurs next, we assume that the programmer forgot a ; at
1560 // the end of the declaration and recover that way.
1561 //
Richard Smithc9f35172012-06-25 21:37:02 +00001562 // Also enforce C++ [temp]p3:
1563 // In a template-declaration which defines a class, no declarator
1564 // is permitted.
Joao Matos17d35c32012-08-31 22:18:20 +00001565 if (TUK == Sema::TUK_Definition &&
1566 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidis7d033b22012-12-17 20:10:43 +00001567 if (Tok.isNot(tok::semi)) {
1568 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1569 DeclSpec::getSpecifierName(TagType));
1570 // Push this token back into the preprocessor and change our current token
1571 // to ';' so that the rest of the code recovers as though there were an
1572 // ';' after the definition.
1573 PP.EnterToken(Tok);
1574 Tok.setKind(tok::semi);
1575 }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001576 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001577}
1578
Mike Stump1eb44332009-09-09 15:08:12 +00001579/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001580///
1581/// base-clause : [C++ class.derived]
1582/// ':' base-specifier-list
1583/// base-specifier-list:
1584/// base-specifier '...'[opt]
1585/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001586void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001587 assert(Tok.is(tok::colon) && "Not a base clause");
1588 ConsumeToken();
1589
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001590 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001591 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001592
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001593 while (true) {
1594 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001595 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001596 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001597 // Skip the rest of this base specifier, up until the comma or
1598 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001599 SkipUntil(tok::comma, tok::l_brace, true, true);
1600 } else {
1601 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001602 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001603 }
1604
1605 // If the next token is a comma, consume it and keep reading
1606 // base-specifiers.
1607 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001609 // Consume the comma.
1610 ConsumeToken();
1611 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001612
1613 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001614 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001615}
1616
1617/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1618/// one entry in the base class list of a class specifier, for example:
1619/// class foo : public bar, virtual private baz {
1620/// 'public bar' and 'virtual private baz' are each base-specifiers.
1621///
1622/// base-specifier: [C++ class.derived]
1623/// ::[opt] nested-name-specifier[opt] class-name
1624/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001625/// base-type-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001626/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001627/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001628Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001629 bool IsVirtual = false;
1630 SourceLocation StartLoc = Tok.getLocation();
1631
1632 // Parse the 'virtual' keyword.
1633 if (Tok.is(tok::kw_virtual)) {
1634 ConsumeToken();
1635 IsVirtual = true;
1636 }
1637
1638 // Parse an (optional) access specifier.
1639 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001640 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001641 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001643 // Parse the 'virtual' keyword (again!), in case it came after the
1644 // access specifier.
1645 if (Tok.is(tok::kw_virtual)) {
1646 SourceLocation VirtualLoc = ConsumeToken();
1647 if (IsVirtual) {
1648 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001649 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001650 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001651 }
1652
1653 IsVirtual = true;
1654 }
1655
Douglas Gregor42a552f2008-11-05 20:51:48 +00001656 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001657 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001658 SourceLocation BaseLoc;
1659 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001660 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001661 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001663 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1664 // actually part of the base-specifier-list grammar productions, but we
1665 // parse it here for convenience.
1666 SourceLocation EllipsisLoc;
1667 if (Tok.is(tok::ellipsis))
1668 EllipsisLoc = ConsumeToken();
1669
Mike Stump1eb44332009-09-09 15:08:12 +00001670 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001671 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001673 // Notify semantic analysis that we have parsed a complete
1674 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001675 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001676 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001677}
1678
1679/// getAccessSpecifierIfPresent - Determine whether the next token is
1680/// a C++ access-specifier.
1681///
1682/// access-specifier: [C++ class.derived]
1683/// 'private'
1684/// 'protected'
1685/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001686AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001687 switch (Tok.getKind()) {
1688 default: return AS_none;
1689 case tok::kw_private: return AS_private;
1690 case tok::kw_protected: return AS_protected;
1691 case tok::kw_public: return AS_public;
1692 }
1693}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001694
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001695/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001696/// delayed, e.g., default arguments, create a late-parsed method declaration
1697/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001698void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1699 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001700 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001701 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001702 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001703 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001704 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001705
Eli Friedmand33133c2009-07-22 21:45:50 +00001706 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1707 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1708 if (!LateMethod) {
1709 // Push this method onto the stack of late-parsed method
1710 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001711 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1712 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001713 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001714
1715 // Add all of the parameters prior to this one (they don't
1716 // have default arguments).
1717 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1718 for (unsigned I = 0; I < ParamIdx; ++I)
1719 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001720 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001721 }
1722
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001723 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001724 // not have a default argument).
1725 LateMethod->DefaultArgs.push_back(
1726 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1727 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1728 }
1729 }
1730}
1731
Richard Smith4e24f0f2013-01-02 12:01:23 +00001732/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001733/// virt-specifier.
1734///
1735/// virt-specifier:
1736/// override
1737/// final
Richard Smith4e24f0f2013-01-02 12:01:23 +00001738VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001739 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001740 return VirtSpecifiers::VS_None;
1741
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001742 if (Tok.is(tok::identifier)) {
1743 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001744
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001745 // Initialize the contextual keywords.
1746 if (!Ident_final) {
1747 Ident_final = &PP.getIdentifierTable().get("final");
1748 Ident_override = &PP.getIdentifierTable().get("override");
1749 }
1750
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001751 if (II == Ident_override)
1752 return VirtSpecifiers::VS_Override;
1753
1754 if (II == Ident_final)
1755 return VirtSpecifiers::VS_Final;
1756 }
1757
1758 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001759}
1760
Richard Smith4e24f0f2013-01-02 12:01:23 +00001761/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001762///
1763/// virt-specifier-seq:
1764/// virt-specifier
1765/// virt-specifier-seq virt-specifier
Richard Smith4e24f0f2013-01-02 12:01:23 +00001766void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalle402e722012-09-25 07:32:39 +00001767 bool IsInterface) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001768 while (true) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001769 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001770 if (Specifier == VirtSpecifiers::VS_None)
1771 return;
1772
1773 // C++ [class.mem]p8:
1774 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001775 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001776 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001777 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1778 << PrevSpec
1779 << FixItHint::CreateRemoval(Tok.getLocation());
1780
John McCalle402e722012-09-25 07:32:39 +00001781 if (IsInterface && Specifier == VirtSpecifiers::VS_Final) {
1782 Diag(Tok.getLocation(), diag::err_override_control_interface)
1783 << VirtSpecifiers::getSpecifierName(Specifier);
1784 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00001785 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00001786 diag::warn_cxx98_compat_override_control_keyword :
1787 diag::ext_override_control_keyword)
1788 << VirtSpecifiers::getSpecifierName(Specifier);
1789 }
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001790 ConsumeToken();
1791 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001792}
1793
Richard Smith4e24f0f2013-01-02 12:01:23 +00001794/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001795/// contextual 'final' keyword.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001796bool Parser::isCXX11FinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001797 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001798 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001799
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001800 if (!Tok.is(tok::identifier))
1801 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001802
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001803 // Initialize the contextual keywords.
1804 if (!Ident_final) {
1805 Ident_final = &PP.getIdentifierTable().get("final");
1806 Ident_override = &PP.getIdentifierTable().get("override");
1807 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001808
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001809 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001810}
1811
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001812/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1813///
1814/// member-declaration:
1815/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1816/// function-definition ';'[opt]
1817/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1818/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001819/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001820/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001821/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001822///
1823/// member-declarator-list:
1824/// member-declarator
1825/// member-declarator-list ',' member-declarator
1826///
1827/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001828/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001829/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001830/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001831/// identifier[opt] ':' constant-expression
1832///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001833/// virt-specifier-seq:
1834/// virt-specifier
1835/// virt-specifier-seq virt-specifier
1836///
1837/// virt-specifier:
1838/// override
1839/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001840///
Sebastian Redle2b68332009-04-12 17:16:29 +00001841/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001842/// '= 0'
1843///
1844/// constant-initializer:
1845/// '=' constant-expression
1846///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001847void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001848 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001849 const ParsedTemplateInfo &TemplateInfo,
1850 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001851 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001852 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001853 Diag(Tok, diag::err_at_defs_cxx);
1854 else
1855 Diag(Tok, diag::err_at_in_class);
1856
1857 ConsumeToken();
1858 SkipUntil(tok::r_brace);
1859 return;
1860 }
1861
John McCall60fa3cf2009-12-11 02:10:03 +00001862 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001863 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001864 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001865 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1866 if (TryAnnotateCXXScopeToken())
1867 MalformedTypeSpec = true;
1868
1869 bool isAccessDecl;
1870 if (Tok.isNot(tok::annot_cxxscope))
1871 isAccessDecl = false;
1872 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001873 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1874 else
1875 isAccessDecl = NextToken().is(tok::kw_operator);
1876
1877 if (isAccessDecl) {
1878 // Collect the scope specifier token we annotated earlier.
1879 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001880 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1881 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001882
1883 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001884 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001885 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001886 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1887 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001888 SkipUntil(tok::semi);
1889 return;
1890 }
1891
1892 // TODO: recover from mistakenly-qualified operator declarations.
1893 if (ExpectAndConsume(tok::semi,
1894 diag::err_expected_semi_after,
1895 "access declaration",
1896 tok::semi))
1897 return;
1898
Douglas Gregor23c94db2010-07-02 17:43:08 +00001899 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001900 false, SourceLocation(),
1901 SS, Name,
1902 /* AttrList */ 0,
1903 /* IsTypeName */ false,
1904 SourceLocation());
1905 return;
1906 }
1907 }
1908
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001909 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001910 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001911 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001912 SourceLocation DeclEnd;
1913 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001914 return;
1915 }
Mike Stump1eb44332009-09-09 15:08:12 +00001916
Chris Lattner682bf922009-03-29 16:50:03 +00001917 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001918 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001919 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001920 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001921 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001922 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001923 return;
1924 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001925
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001926 // Handle: member-declaration ::= '__extension__' member-declaration
1927 if (Tok.is(tok::kw___extension__)) {
1928 // __extension__ silences extension warnings in the subexpression.
1929 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1930 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001931 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1932 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001933 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001934
Chris Lattner4ed5d912010-02-02 01:23:29 +00001935 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1936 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001937 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001938
John McCall0b7e6782011-03-24 11:26:52 +00001939 ParsedAttributesWithRange attrs(AttrFactory);
Michael Han52b501c2012-11-28 23:17:40 +00001940 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001941 // Optional C++11 attribute-specifier
1942 MaybeParseCXX11Attributes(attrs);
Michael Han52b501c2012-11-28 23:17:40 +00001943 // We need to keep these attributes for future diagnostic
1944 // before they are taken over by declaration specifier.
1945 FnAttrs.addAll(attrs.getList());
1946 FnAttrs.Range = attrs.Range;
1947
John McCall7f040a92010-12-24 02:08:15 +00001948 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001949
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001950 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00001951 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001952
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001953 // Eat 'using'.
1954 SourceLocation UsingLoc = ConsumeToken();
1955
1956 if (Tok.is(tok::kw_namespace)) {
1957 Diag(UsingLoc, diag::err_using_namespace_in_class);
1958 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001959 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001960 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001961 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00001962 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1963 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001964 }
1965 return;
1966 }
1967
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001968 // Hold late-parsed attributes so we can attach a Decl to them later.
1969 LateParsedAttrList CommonLateParsedAttrs;
1970
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001971 // decl-specifier-seq:
1972 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001973 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00001974 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00001975 if (MalformedTypeSpec)
1976 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001977 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
1978 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001979
Benjamin Kramer5354e772012-08-23 23:38:35 +00001980 MultiTemplateParamsArg TemplateParams(
John McCalldd4a3b02009-09-16 22:47:08 +00001981 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1982 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1983
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001984 if (Tok.is(tok::semi)) {
1985 ConsumeToken();
Michael Han52b501c2012-11-28 23:17:40 +00001986
1987 if (DS.isFriendSpecified())
1988 ProhibitAttributes(FnAttrs);
1989
John McCalld226f652010-08-21 09:40:31 +00001990 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00001991 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00001992 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001993 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001994 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001995
John McCall54abf7d2009-11-04 02:18:39 +00001996 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00001997 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001998
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001999 // Hold late-parsed attributes so we can attach a Decl to them later.
2000 LateParsedAttrList LateParsedAttrs;
2001
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002002 SourceLocation EqualLoc;
2003 bool HasInitializer = false;
2004 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002005 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002006 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2007 ColonProtectionRAIIObject X(*this);
2008
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002009 // Parse the first declarator.
2010 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00002011 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00002012 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002013 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00002014 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002015 if (Tok.is(tok::semi))
2016 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002017 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002018 }
2019
Richard Smith4e24f0f2013-01-02 12:01:23 +00002020 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber48673472011-01-28 06:07:34 +00002021
John Thompson1b2fc0f2009-11-25 22:58:06 +00002022 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002023 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00002024
Francois Pichet6a247472011-05-11 02:14:46 +00002025 // MSVC permits pure specifier on inline functions declared at class scope.
2026 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00002027 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00002028 DeclaratorInfo.isFunctionDeclarator() &&
2029 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002030 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00002031 Init = ParseInitializer();
2032 if (Init.isInvalid())
2033 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002034 else
2035 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00002036 }
2037
Douglas Gregor45fa5602011-11-07 20:56:01 +00002038 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002039 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00002040 //
2041 // In C++11, a non-function declarator followed by an open brace is a
2042 // braced-init-list for an in-class member initialization, not an
2043 // erroneous function definition.
Richard Smith80ad52f2013-01-02 11:42:31 +00002044 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002045 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002046 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00002047 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002048 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002049 } else if (Tok.is(tok::equal)) {
2050 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00002051 if (KW.is(tok::kw_default))
2052 DefinitionKind = FDK_Defaulted;
2053 else if (KW.is(tok::kw_delete))
2054 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00002055 }
2056 }
2057
Michael Han52b501c2012-11-28 23:17:40 +00002058 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2059 // to a friend declaration, that declaration shall be a definition.
2060 if (DeclaratorInfo.isFunctionDeclarator() &&
2061 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2062 // Diagnose attributes that appear before decl specifier:
2063 // [[]] friend int foo();
2064 ProhibitAttributes(FnAttrs);
2065 }
2066
Douglas Gregor45fa5602011-11-07 20:56:01 +00002067 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002068 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002069 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002070 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00002071 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Michael Han52b501c2012-11-28 23:17:40 +00002072
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002073 // Consume the optional ';'
2074 if (Tok.is(tok::semi))
2075 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002076 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002077 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002078
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002079 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002080 Diag(DeclaratorInfo.getIdentifierLoc(),
2081 diag::err_function_declared_typedef);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002082
Richard Smith6f9a4452012-11-15 22:54:20 +00002083 // Recover by treating the 'typedef' as spurious.
2084 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002085 }
2086
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002087 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002088 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00002089 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002090
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002091 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2092 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2093 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002094 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002095 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002096 }
2097 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00002098
2099 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002100 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00002101 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002102
Chris Lattner682bf922009-03-29 16:50:03 +00002103 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002104 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002105 }
2106
2107 // member-declarator-list:
2108 // member-declarator
2109 // member-declarator-list ',' member-declarator
2110
Chris Lattner5f9e2722011-07-23 10:55:15 +00002111 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00002112 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00002113 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002114
2115 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002116 // member-declarator:
2117 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002118 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002119 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002120 if (Tok.is(tok::colon)) {
2121 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002122 BitfieldSize = ParseConstantExpression();
2123 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002124 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002125 }
Mike Stump1eb44332009-09-09 15:08:12 +00002126
Chris Lattnere6563252010-06-13 05:34:18 +00002127 // If a simple-asm-expr is present, parse it.
2128 if (Tok.is(tok::kw_asm)) {
2129 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002130 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002131 if (AsmLabel.isInvalid())
2132 SkipUntil(tok::comma, true, true);
2133
2134 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2135 DeclaratorInfo.SetRangeEnd(Loc);
2136 }
2137
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002138 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002139 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002140
Richard Smith7a614d82011-06-11 17:19:42 +00002141 // FIXME: When g++ adds support for this, we'll need to check whether it
2142 // goes before or after the GNU attributes and __asm__.
Richard Smith4e24f0f2013-01-02 12:01:23 +00002143 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith7a614d82011-06-11 17:19:42 +00002144
Richard Smithca523302012-06-10 03:12:00 +00002145 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002146 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002147 if (BitfieldSize.get()) {
2148 Diag(Tok, diag::err_bitfield_member_init);
2149 SkipUntil(tok::comma, true, true);
2150 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002151 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002152 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2153 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2154 != DeclSpec::SCS_static &&
2155 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2156 != DeclSpec::SCS_typedef)
2157 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002158 }
2159 }
2160
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002161 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002162 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002163 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002164
John McCalld226f652010-08-21 09:40:31 +00002165 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002166 if (DS.isFriendSpecified()) {
Michael Han52b501c2012-11-28 23:17:40 +00002167 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2168 // to a friend declaration, that declaration shall be a definition.
2169 //
2170 // Diagnose attributes appear after friend member function declarator:
2171 // foo [[]] ();
2172 SmallVector<SourceRange, 4> Ranges;
2173 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2174 if (!Ranges.empty()) {
2175 for (SmallVector<SourceRange, 4>::iterator I = Ranges.begin(),
2176 E = Ranges.end(); I != E; ++I) {
2177 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2178 << *I;
2179 }
2180 }
2181
John McCallbbbcdd92009-09-11 21:02:39 +00002182 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002183 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002184 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002185 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002186 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002187 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002188 TemplateParams,
John McCall67d1a672009-08-06 02:15:43 +00002189 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002190 VS, HasInClassInit);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002191 if (AccessAttrs)
2192 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2193 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002194 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002195
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002196 // Set the Decl for any late parsed attributes
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002197 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2198 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2199 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002200 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002201 LateParsedAttrs[i]->addDecl(ThisDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002202 }
2203 LateParsedAttrs.clear();
2204
Douglas Gregor147545d2011-10-10 14:49:18 +00002205 // Handle the initializer.
Richard Smithca523302012-06-10 03:12:00 +00002206 if (HasInClassInit != ICIS_NoInit) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002207 // The initializer was deferred; parse it and cache the tokens.
Richard Smith80ad52f2013-01-02 11:42:31 +00002208 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00002209 diag::warn_cxx98_compat_nonstatic_member_init :
2210 diag::ext_nonstatic_member_init);
2211
Richard Smith7a614d82011-06-11 17:19:42 +00002212 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002213 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2214 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002215 //
2216 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002217 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002218 Diag(Tok, diag::err_incomplete_array_member_init);
2219 SkipUntil(tok::comma, true, true);
David Blaikie3164c142012-02-14 09:00:46 +00002220 if (ThisDecl)
2221 // Avoid later warnings about a class member of incomplete type.
2222 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002223 } else
2224 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002225 } else if (HasInitializer) {
2226 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002227 if (!Init.isUsable())
Douglas Gregor552e2992012-02-21 02:22:07 +00002228 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002229 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2230
Douglas Gregor147545d2011-10-10 14:49:18 +00002231 if (Init.isInvalid())
2232 SkipUntil(tok::comma, true, true);
2233 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002234 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002235 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor147545d2011-10-10 14:49:18 +00002236 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2237 // No initializer.
2238 Actions.ActOnUninitializedDecl(ThisDecl,
2239 DS.getTypeSpecType() == DeclSpec::TST_auto);
Richard Smith7a614d82011-06-11 17:19:42 +00002240 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002241
2242 if (ThisDecl) {
2243 Actions.FinalizeDeclaration(ThisDecl);
2244 DeclsInGroup.push_back(ThisDecl);
2245 }
2246
Richard Smithe5310012012-04-29 07:31:09 +00002247 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor147545d2011-10-10 14:49:18 +00002248 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2249 != DeclSpec::SCS_typedef) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002250 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002251 }
2252
2253 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002254
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002255 // If we don't have a comma, it is either the end of the list (a ';')
2256 // or an error, bail out.
2257 if (Tok.isNot(tok::comma))
2258 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002259
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002260 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002261 SourceLocation CommaLoc = ConsumeToken();
2262
2263 if (Tok.isAtStartOfLine() &&
2264 !MightBeDeclarator(Declarator::MemberContext)) {
2265 // This comma was followed by a line-break and something which can't be
2266 // the start of a declarator. The comma was probably a typo for a
2267 // semicolon.
2268 Diag(CommaLoc, diag::err_expected_semi_declaration)
2269 << FixItHint::CreateReplacement(CommaLoc, ";");
2270 ExpectSemi = false;
2271 break;
2272 }
Mike Stump1eb44332009-09-09 15:08:12 +00002273
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002274 // Parse the next declarator.
2275 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002276 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002277 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002278 Init = true;
2279 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002280 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002281
Bill Wendlingad017fa2012-12-20 19:22:21 +00002282 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002283 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002284
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002285 if (Tok.isNot(tok::colon))
2286 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002287 }
2288
Richard Smith1c94c162012-01-09 22:31:44 +00002289 if (ExpectSemi &&
2290 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002291 // Skip to end of block or statement.
2292 SkipUntil(tok::r_brace, true, true);
2293 // If we stopped at a ';', eat it.
2294 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002295 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002296 }
2297
Douglas Gregor23c94db2010-07-02 17:43:08 +00002298 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00002299 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002300}
2301
Richard Smith7a614d82011-06-11 17:19:42 +00002302/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2303/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2304/// function definition. The location of the '=', if any, will be placed in
2305/// EqualLoc.
2306///
2307/// pure-specifier:
2308/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002309///
Richard Smith7a614d82011-06-11 17:19:42 +00002310/// brace-or-equal-initializer:
2311/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002312/// braced-init-list
2313///
Richard Smith7a614d82011-06-11 17:19:42 +00002314/// initializer-clause:
2315/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002316/// braced-init-list
2317///
Richard Smith7a614d82011-06-11 17:19:42 +00002318/// defaulted/deleted function-definition:
2319/// '=' 'default'
2320/// '=' 'delete'
2321///
2322/// Prior to C++0x, the assignment-expression in an initializer-clause must
2323/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002324ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002325 SourceLocation &EqualLoc) {
2326 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2327 && "Data member initializer not starting with '=' or '{'");
2328
Douglas Gregor552e2992012-02-21 02:22:07 +00002329 EnterExpressionEvaluationContext Context(Actions,
2330 Sema::PotentiallyEvaluated,
2331 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002332 if (Tok.is(tok::equal)) {
2333 EqualLoc = ConsumeToken();
2334 if (Tok.is(tok::kw_delete)) {
2335 // In principle, an initializer of '= delete p;' is legal, but it will
2336 // never type-check. It's better to diagnose it as an ill-formed expression
2337 // than as an ill-formed deleted non-function member.
2338 // An initializer of '= delete p, foo' will never be parsed, because
2339 // a top-level comma always ends the initializer expression.
2340 const Token &Next = NextToken();
2341 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2342 Next.is(tok::eof)) {
2343 if (IsFunction)
2344 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2345 << 1 /* delete */;
2346 else
2347 Diag(ConsumeToken(), diag::err_deleted_non_function);
2348 return ExprResult();
2349 }
2350 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002351 if (IsFunction)
2352 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2353 << 0 /* default */;
2354 else
2355 Diag(ConsumeToken(), diag::err_default_special_members);
2356 return ExprResult();
2357 }
2358
Sebastian Redl33deb352012-02-22 10:50:08 +00002359 }
2360 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002361}
2362
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002363/// ParseCXXMemberSpecification - Parse the class definition.
2364///
2365/// member-specification:
2366/// member-declaration member-specification[opt]
2367/// access-specifier ':' member-specification[opt]
2368///
Joao Matos17d35c32012-08-31 22:18:20 +00002369void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han07fc1ba2013-01-07 16:57:11 +00002370 SourceLocation AttrFixitLoc,
2371 ParsedAttributes &Attrs,
Joao Matos17d35c32012-08-31 22:18:20 +00002372 unsigned TagType, Decl *TagDecl) {
2373 assert((TagType == DeclSpec::TST_struct ||
2374 TagType == DeclSpec::TST_interface ||
2375 TagType == DeclSpec::TST_union ||
2376 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2377
John McCallf312b1e2010-08-26 23:41:50 +00002378 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2379 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002380
Douglas Gregor26997fd2010-01-16 20:52:59 +00002381 // Determine whether this is a non-nested class. Note that local
2382 // classes are *not* considered to be nested classes.
2383 bool NonNestedClass = true;
2384 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002385 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002386 if (S->isClassScope()) {
2387 // We're inside a class scope, so this is a nested class.
2388 NonNestedClass = false;
John McCalle402e722012-09-25 07:32:39 +00002389
2390 // The Microsoft extension __interface does not permit nested classes.
2391 if (getCurrentClass().IsInterface) {
2392 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2393 << /*ErrorType=*/6
2394 << (isa<NamedDecl>(TagDecl)
2395 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2396 : "<anonymous>");
2397 }
Douglas Gregor26997fd2010-01-16 20:52:59 +00002398 break;
2399 }
2400
2401 if ((S->getFlags() & Scope::FnScope)) {
2402 // If we're in a function or function template declared in the
2403 // body of a class, then this is a local class rather than a
2404 // nested class.
2405 const Scope *Parent = S->getParent();
2406 if (Parent->isTemplateParamScope())
2407 Parent = Parent->getParent();
2408 if (Parent->isClassScope())
2409 break;
2410 }
2411 }
2412 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002413
2414 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002415 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002416
Douglas Gregor6569d682009-05-27 23:11:45 +00002417 // Note that we are parsing a new (potentially-nested) class definition.
John McCalle402e722012-09-25 07:32:39 +00002418 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2419 TagType == DeclSpec::TST_interface);
Douglas Gregor6569d682009-05-27 23:11:45 +00002420
Douglas Gregorddc29e12009-02-06 22:42:48 +00002421 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002422 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002423
Anders Carlssonb184a182011-03-25 14:46:08 +00002424 SourceLocation FinalLoc;
2425
2426 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002427 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00002428 assert(isCXX11FinalKeyword() && "not a class definition");
Richard Smith8b11b5e2011-10-15 04:21:46 +00002429 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002430
John McCalle402e722012-09-25 07:32:39 +00002431 if (TagType == DeclSpec::TST_interface) {
2432 Diag(FinalLoc, diag::err_override_control_interface)
2433 << "final";
2434 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00002435 Diag(FinalLoc, getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00002436 diag::warn_cxx98_compat_override_control_keyword :
2437 diag::ext_override_control_keyword) << "final";
2438 }
Michael Han2e397132012-11-26 22:54:45 +00002439
Michael Han07fc1ba2013-01-07 16:57:11 +00002440 // Parse any C++11 attributes after 'final' keyword.
2441 // These attributes are not allowed to appear here,
2442 // and the only possible place for them to appertain
2443 // to the class would be between class-key and class-name.
2444 ParsedAttributesWithRange Attributes(AttrFactory);
2445 MaybeParseCXX11Attributes(Attributes);
2446 SourceRange AttrRange = Attributes.Range;
2447 if (AttrRange.isValid()) {
2448 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
2449 << AttrRange
2450 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
2451 CharSourceRange(AttrRange, true))
2452 << FixItHint::CreateRemoval(AttrRange);
2453
2454 // Recover by adding attributes to the attribute list of the class
2455 // so they can be applied on the class later.
2456 Attrs.takeAllFrom(Attributes);
2457 }
Anders Carlssonb184a182011-03-25 14:46:08 +00002458 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002459
John McCallbd0dfa52009-12-19 21:48:58 +00002460 if (Tok.is(tok::colon)) {
2461 ParseBaseClause(TagDecl);
2462
2463 if (!Tok.is(tok::l_brace)) {
2464 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002465
2466 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002467 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002468 return;
2469 }
2470 }
2471
2472 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002473 BalancedDelimiterTracker T(*this, tok::l_brace);
2474 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002475
John McCall42a4f662010-05-28 08:11:17 +00002476 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002477 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002478 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002479
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002480 // C++ 11p3: Members of a class defined with the keyword class are private
2481 // by default. Members of a class defined with the keywords struct or union
2482 // are public by default.
2483 AccessSpecifier CurAS;
2484 if (TagType == DeclSpec::TST_class)
2485 CurAS = AS_private;
2486 else
2487 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002488 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002489
Douglas Gregor07976d22010-06-21 22:31:09 +00002490 if (TagDecl) {
2491 // While we still have something to read, read the member-declarations.
2492 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2493 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002494
David Blaikie4e4d0842012-03-11 07:00:24 +00002495 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002496 Tok.is(tok::kw___if_not_exists))) {
2497 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2498 continue;
2499 }
2500
Douglas Gregor07976d22010-06-21 22:31:09 +00002501 // Check for extraneous top-level semicolon.
2502 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002503 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002504 continue;
2505 }
2506
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002507 if (Tok.is(tok::annot_pragma_vis)) {
2508 HandlePragmaVisibility();
2509 continue;
2510 }
2511
2512 if (Tok.is(tok::annot_pragma_pack)) {
2513 HandlePragmaPack();
2514 continue;
2515 }
2516
Argyrios Kyrtzidisf4deaef2012-10-12 17:39:59 +00002517 if (Tok.is(tok::annot_pragma_align)) {
2518 HandlePragmaAlign();
2519 continue;
2520 }
2521
Douglas Gregor07976d22010-06-21 22:31:09 +00002522 AccessSpecifier AS = getAccessSpecifierIfPresent();
2523 if (AS != AS_none) {
2524 // Current token is a C++ access specifier.
2525 CurAS = AS;
2526 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002527 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002528 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002529 AccessAttrs.clear();
2530 MaybeParseGNUAttributes(AccessAttrs);
2531
David Blaikie13f8daf2011-10-13 06:08:43 +00002532 SourceLocation EndLoc;
2533 if (Tok.is(tok::colon)) {
2534 EndLoc = Tok.getLocation();
2535 ConsumeToken();
2536 } else if (Tok.is(tok::semi)) {
2537 EndLoc = Tok.getLocation();
2538 ConsumeToken();
2539 Diag(EndLoc, diag::err_expected_colon)
2540 << FixItHint::CreateReplacement(EndLoc, ":");
2541 } else {
2542 EndLoc = ASLoc.getLocWithOffset(TokLength);
2543 Diag(EndLoc, diag::err_expected_colon)
2544 << FixItHint::CreateInsertion(EndLoc, ":");
2545 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002546
John McCalle402e722012-09-25 07:32:39 +00002547 // The Microsoft extension __interface does not permit non-public
2548 // access specifiers.
2549 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2550 Diag(ASLoc, diag::err_access_specifier_interface)
2551 << (CurAS == AS_protected);
2552 }
2553
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002554 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2555 AccessAttrs.getList())) {
2556 // found another attribute than only annotations
2557 AccessAttrs.clear();
2558 }
2559
Douglas Gregor07976d22010-06-21 22:31:09 +00002560 continue;
2561 }
2562
2563 // FIXME: Make sure we don't have a template here.
2564
2565 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002566 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002567 }
2568
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002569 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002570 } else {
2571 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002572 }
Mike Stump1eb44332009-09-09 15:08:12 +00002573
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002574 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002575 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002576 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002577
John McCall42a4f662010-05-28 08:11:17 +00002578 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002579 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002580 T.getOpenLocation(),
2581 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002582 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002583
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002584 // C++11 [class.mem]p2:
2585 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002586 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002587 // brace-or-equal-initializers for non-static data members (including such
2588 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002589 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002590 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002591 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002592 // declarations and the lexed inline method definitions, along with any
2593 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002594 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002595 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002596 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002597
2598 // We've finished with all pending member declarations.
2599 Actions.ActOnFinishCXXMemberDecls();
2600
Richard Smith7a614d82011-06-11 17:19:42 +00002601 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002602 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002603 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002604 }
2605
John McCall42a4f662010-05-28 08:11:17 +00002606 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002607 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2608 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002609
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002610 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002611 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002612 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002613}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002614
2615/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2616/// which explicitly initializes the members or base classes of a
2617/// class (C++ [class.base.init]). For example, the three initializers
2618/// after the ':' in the Derived constructor below:
2619///
2620/// @code
2621/// class Base { };
2622/// class Derived : Base {
2623/// int x;
2624/// float f;
2625/// public:
2626/// Derived(float f) : Base(), x(17), f(f) { }
2627/// };
2628/// @endcode
2629///
Mike Stump1eb44332009-09-09 15:08:12 +00002630/// [C++] ctor-initializer:
2631/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002632///
Mike Stump1eb44332009-09-09 15:08:12 +00002633/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002634/// mem-initializer ...[opt]
2635/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002636void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002637 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2638
John Wiegley28bbe4b2011-04-28 01:08:34 +00002639 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2640 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002641 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002642
Chris Lattner5f9e2722011-07-23 10:55:15 +00002643 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002644 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002645
Douglas Gregor7ad83902008-11-05 04:29:56 +00002646 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002647 if (Tok.is(tok::code_completion)) {
2648 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2649 MemInitializers.data(),
2650 MemInitializers.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002651 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002652 } else {
2653 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2654 if (!MemInit.isInvalid())
2655 MemInitializers.push_back(MemInit.get());
2656 else
2657 AnyErrors = true;
2658 }
2659
Douglas Gregor7ad83902008-11-05 04:29:56 +00002660 if (Tok.is(tok::comma))
2661 ConsumeToken();
2662 else if (Tok.is(tok::l_brace))
2663 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002664 // If the next token looks like a base or member initializer, assume that
2665 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002666 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2667 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2668 Diag(Loc, diag::err_ctor_init_missing_comma)
2669 << FixItHint::CreateInsertion(Loc, ", ");
2670 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002671 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002672 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002673 SkipUntil(tok::l_brace, true, true);
2674 break;
2675 }
2676 } while (true);
2677
Mike Stump1eb44332009-09-09 15:08:12 +00002678 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002679 MemInitializers.data(), MemInitializers.size(),
2680 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002681}
2682
2683/// ParseMemInitializer - Parse a C++ member initializer, which is
2684/// part of a constructor initializer that explicitly initializes one
2685/// member or base class (C++ [class.base.init]). See
2686/// ParseConstructorInitializer for an example.
2687///
2688/// [C++] mem-initializer:
2689/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002690/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002691///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002692/// [C++] mem-initializer-id:
2693/// '::'[opt] nested-name-specifier[opt] class-name
2694/// identifier
John McCalld226f652010-08-21 09:40:31 +00002695Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002696 // parse '::'[opt] nested-name-specifier[opt]
2697 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002698 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002699 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002700 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002701 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002702 if (TemplateId->Kind == TNK_Type_template ||
2703 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002704 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002705 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002706 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002707 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002708 }
David Blaikief2116622012-01-24 06:03:59 +00002709 // Uses of decltype will already have been converted to annot_decltype by
2710 // ParseOptionalCXXScopeSpecifier at this point.
2711 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2712 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002713 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002714 return true;
2715 }
Mike Stump1eb44332009-09-09 15:08:12 +00002716
David Blaikief2116622012-01-24 06:03:59 +00002717 IdentifierInfo *II = 0;
2718 DeclSpec DS(AttrFactory);
2719 SourceLocation IdLoc = Tok.getLocation();
2720 if (Tok.is(tok::annot_decltype)) {
2721 // Get the decltype expression, if there is one.
2722 ParseDecltypeSpecifier(DS);
2723 } else {
2724 if (Tok.is(tok::identifier))
2725 // Get the identifier. This may be a member name or a class name,
2726 // but we'll let the semantic analysis determine which it is.
2727 II = Tok.getIdentifierInfo();
2728 ConsumeToken();
2729 }
2730
Douglas Gregor7ad83902008-11-05 04:29:56 +00002731
2732 // Parse the '('.
Richard Smith80ad52f2013-01-02 11:42:31 +00002733 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002734 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2735
Sebastian Redl6df65482011-09-24 17:48:25 +00002736 ExprResult InitList = ParseBraceInitializer();
2737 if (InitList.isInvalid())
2738 return true;
2739
2740 SourceLocation EllipsisLoc;
2741 if (Tok.is(tok::ellipsis))
2742 EllipsisLoc = ConsumeToken();
2743
2744 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002745 TemplateTypeTy, DS, IdLoc,
2746 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002747 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002748 BalancedDelimiterTracker T(*this, tok::l_paren);
2749 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002750
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002751 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002752 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002753 CommaLocsTy CommaLocs;
2754 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2755 SkipUntil(tok::r_paren);
2756 return true;
2757 }
2758
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002759 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002760
2761 SourceLocation EllipsisLoc;
2762 if (Tok.is(tok::ellipsis))
2763 EllipsisLoc = ConsumeToken();
2764
2765 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002766 TemplateTypeTy, DS, IdLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002767 T.getOpenLocation(), ArgExprs.data(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002768 ArgExprs.size(), T.getCloseLocation(),
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002769 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002770 }
2771
Richard Smith80ad52f2013-01-02 11:42:31 +00002772 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002773 : diag::err_expected_lparen);
2774 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002775}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002776
Sebastian Redl7acafd02011-03-05 14:45:16 +00002777/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002778///
Douglas Gregora4745612008-12-01 18:00:20 +00002779/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002780/// dynamic-exception-specification
2781/// noexcept-specification
2782///
2783/// noexcept-specification:
2784/// 'noexcept'
2785/// 'noexcept' '(' constant-expression ')'
2786ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002787Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002788 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002789 SmallVectorImpl<ParsedType> &DynamicExceptions,
2790 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002791 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002792 ExceptionSpecificationType Result = EST_None;
2793
2794 // See if there's a dynamic specification.
2795 if (Tok.is(tok::kw_throw)) {
2796 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2797 DynamicExceptions,
2798 DynamicExceptionRanges);
2799 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2800 "Produced different number of exception types and ranges.");
2801 }
2802
2803 // If there's no noexcept specification, we're done.
2804 if (Tok.isNot(tok::kw_noexcept))
2805 return Result;
2806
Richard Smith841804b2011-10-17 23:06:20 +00002807 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2808
Sebastian Redl7acafd02011-03-05 14:45:16 +00002809 // If we already had a dynamic specification, parse the noexcept for,
2810 // recovery, but emit a diagnostic and don't store the results.
2811 SourceRange NoexceptRange;
2812 ExceptionSpecificationType NoexceptType = EST_None;
2813
2814 SourceLocation KeywordLoc = ConsumeToken();
2815 if (Tok.is(tok::l_paren)) {
2816 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002817 BalancedDelimiterTracker T(*this, tok::l_paren);
2818 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002819 NoexceptType = EST_ComputedNoexcept;
2820 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002821 // The argument must be contextually convertible to bool. We use
2822 // ActOnBooleanCondition for this purpose.
2823 if (!NoexceptExpr.isInvalid())
2824 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2825 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002826 T.consumeClose();
2827 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002828 } else {
2829 // There is no argument.
2830 NoexceptType = EST_BasicNoexcept;
2831 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2832 }
2833
2834 if (Result == EST_None) {
2835 SpecificationRange = NoexceptRange;
2836 Result = NoexceptType;
2837
2838 // If there's a dynamic specification after a noexcept specification,
2839 // parse that and ignore the results.
2840 if (Tok.is(tok::kw_throw)) {
2841 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2842 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2843 DynamicExceptionRanges);
2844 }
2845 } else {
2846 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2847 }
2848
2849 return Result;
2850}
2851
2852/// ParseDynamicExceptionSpecification - Parse a C++
2853/// dynamic-exception-specification (C++ [except.spec]).
2854///
2855/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002856/// 'throw' '(' type-id-list [opt] ')'
2857/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002858///
Douglas Gregora4745612008-12-01 18:00:20 +00002859/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002860/// type-id ... [opt]
2861/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002862///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002863ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2864 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002865 SmallVectorImpl<ParsedType> &Exceptions,
2866 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002867 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002868
Sebastian Redl7acafd02011-03-05 14:45:16 +00002869 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002870 BalancedDelimiterTracker T(*this, tok::l_paren);
2871 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002872 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2873 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002874 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002875 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002876
Douglas Gregora4745612008-12-01 18:00:20 +00002877 // Parse throw(...), a Microsoft extension that means "this function
2878 // can throw anything".
2879 if (Tok.is(tok::ellipsis)) {
2880 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002881 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002882 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002883 T.consumeClose();
2884 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002885 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002886 }
2887
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002888 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002889 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002890 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002891 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002892
Douglas Gregora04426c2010-12-20 23:57:46 +00002893 if (Tok.is(tok::ellipsis)) {
2894 // C++0x [temp.variadic]p5:
2895 // - In a dynamic-exception-specification (15.4); the pattern is a
2896 // type-id.
2897 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002898 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002899 if (!Res.isInvalid())
2900 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2901 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002902
Sebastian Redlef65f062009-05-29 18:02:33 +00002903 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002904 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002905 Ranges.push_back(Range);
2906 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002907
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002908 if (Tok.is(tok::comma))
2909 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002910 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002911 break;
2912 }
2913
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002914 T.consumeClose();
2915 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002916 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002917}
Douglas Gregor6569d682009-05-27 23:11:45 +00002918
Douglas Gregordab60ad2010-10-01 18:44:50 +00002919/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2920/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002921TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002922 assert(Tok.is(tok::arrow) && "expected arrow");
2923
2924 ConsumeToken();
2925
Richard Smith7796eb52012-03-12 08:56:40 +00002926 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002927}
2928
Douglas Gregor6569d682009-05-27 23:11:45 +00002929/// \brief We have just started parsing the definition of a new class,
2930/// so push that class onto our stack of classes that is currently
2931/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002932Sema::ParsingClassState
John McCalle402e722012-09-25 07:32:39 +00002933Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
2934 bool IsInterface) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002935 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002936 "Nested class without outer class");
John McCalle402e722012-09-25 07:32:39 +00002937 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCalleee1d542011-02-14 07:13:47 +00002938 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002939}
2940
2941/// \brief Deallocate the given parsed class and all of its nested
2942/// classes.
2943void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002944 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2945 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002946 delete Class;
2947}
2948
2949/// \brief Pop the top class of the stack of classes that are
2950/// currently being parsed.
2951///
2952/// This routine should be called when we have finished parsing the
2953/// definition of a class, but have not yet popped the Scope
2954/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00002955void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002956 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002957
John McCalleee1d542011-02-14 07:13:47 +00002958 Actions.PopParsingClass(state);
2959
Douglas Gregor6569d682009-05-27 23:11:45 +00002960 ParsingClass *Victim = ClassStack.top();
2961 ClassStack.pop();
2962 if (Victim->TopLevelClass) {
2963 // Deallocate all of the nested classes of this class,
2964 // recursively: we don't need to keep any of this information.
2965 DeallocateParsedClasses(Victim);
2966 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002967 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002968 assert(!ClassStack.empty() && "Missing top-level class?");
2969
Douglas Gregord54eb442010-10-12 16:25:54 +00002970 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002971 // The victim is a nested class, but we will not need to perform
2972 // any processing after the definition of this class since it has
2973 // no members whose handling was delayed. Therefore, we can just
2974 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002975 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002976 return;
2977 }
2978
2979 // This nested class has some members that will need to be processed
2980 // after the top-level class is completely defined. Therefore, add
2981 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002982 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002983 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002984 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00002985}
Sean Huntbbd37c62009-11-21 08:43:09 +00002986
Richard Smithc56298d2012-04-10 03:25:07 +00002987/// \brief Try to parse an 'identifier' which appears within an attribute-token.
2988///
2989/// \return the parsed identifier on success, and 0 if the next token is not an
2990/// attribute-token.
2991///
2992/// C++11 [dcl.attr.grammar]p3:
2993/// If a keyword or an alternative token that satisfies the syntactic
2994/// requirements of an identifier is contained in an attribute-token,
2995/// it is considered an identifier.
2996IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
2997 switch (Tok.getKind()) {
2998 default:
2999 // Identifiers and keywords have identifier info attached.
3000 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3001 Loc = ConsumeToken();
3002 return II;
3003 }
3004 return 0;
3005
3006 case tok::ampamp: // 'and'
3007 case tok::pipe: // 'bitor'
3008 case tok::pipepipe: // 'or'
3009 case tok::caret: // 'xor'
3010 case tok::tilde: // 'compl'
3011 case tok::amp: // 'bitand'
3012 case tok::ampequal: // 'and_eq'
3013 case tok::pipeequal: // 'or_eq'
3014 case tok::caretequal: // 'xor_eq'
3015 case tok::exclaim: // 'not'
3016 case tok::exclaimequal: // 'not_eq'
3017 // Alternative tokens do not have identifier info, but their spelling
3018 // starts with an alphabetical character.
3019 llvm::SmallString<8> SpellingBuf;
3020 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
3021 if (std::isalpha(Spelling[0])) {
3022 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00003023 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00003024 }
3025 return 0;
3026 }
3027}
3028
Michael Han6880f492012-10-03 01:56:22 +00003029static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3030 IdentifierInfo *ScopeName) {
3031 switch (AttributeList::getKind(AttrName, ScopeName,
3032 AttributeList::AS_CXX11)) {
3033 case AttributeList::AT_CarriesDependency:
3034 case AttributeList::AT_FallThrough:
3035 case AttributeList::AT_NoReturn: {
3036 return true;
3037 }
3038
3039 default:
3040 return false;
3041 }
3042}
3043
Richard Smithc56298d2012-04-10 03:25:07 +00003044/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003045/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00003046///
Richard Smith6ee326a2012-04-10 01:32:12 +00003047/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00003048/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003049/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00003050///
Richard Smith6ee326a2012-04-10 01:32:12 +00003051/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00003052/// attribute[opt]
3053/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00003054/// attribute '...'
3055/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00003056///
Richard Smith6ee326a2012-04-10 01:32:12 +00003057/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00003058/// attribute-token attribute-argument-clause[opt]
3059///
Richard Smith6ee326a2012-04-10 01:32:12 +00003060/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003061/// identifier
3062/// attribute-scoped-token
3063///
Richard Smith6ee326a2012-04-10 01:32:12 +00003064/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003065/// attribute-namespace '::' identifier
3066///
Richard Smith6ee326a2012-04-10 01:32:12 +00003067/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00003068/// identifier
3069///
Richard Smith6ee326a2012-04-10 01:32:12 +00003070/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00003071/// '(' balanced-token-seq ')'
3072///
Richard Smith6ee326a2012-04-10 01:32:12 +00003073/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00003074/// balanced-token
3075/// balanced-token-seq balanced-token
3076///
Richard Smith6ee326a2012-04-10 01:32:12 +00003077/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003078/// '(' balanced-token-seq ')'
3079/// '[' balanced-token-seq ']'
3080/// '{' balanced-token-seq '}'
3081/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00003082void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003083 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003084 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00003085 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003086 ParseAlignmentSpecifier(attrs, endLoc);
3087 return;
3088 }
3089
Sean Huntbbd37c62009-11-21 08:43:09 +00003090 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00003091 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00003092
Richard Smith41be6732011-10-14 20:48:27 +00003093 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3094
Sean Huntbbd37c62009-11-21 08:43:09 +00003095 ConsumeBracket();
3096 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003097
Richard Smithc56298d2012-04-10 03:25:07 +00003098 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003099 // attribute not present
3100 if (Tok.is(tok::comma)) {
3101 ConsumeToken();
3102 continue;
3103 }
3104
Richard Smithc56298d2012-04-10 03:25:07 +00003105 SourceLocation ScopeLoc, AttrLoc;
3106 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3107
3108 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3109 if (!AttrName)
3110 // Break out to the "expected ']'" diagnostic.
3111 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003112
Sean Huntbbd37c62009-11-21 08:43:09 +00003113 // scoped attribute
3114 if (Tok.is(tok::coloncolon)) {
3115 ConsumeToken();
3116
Richard Smithc56298d2012-04-10 03:25:07 +00003117 ScopeName = AttrName;
3118 ScopeLoc = AttrLoc;
3119
3120 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3121 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003122 Diag(Tok.getLocation(), diag::err_expected_ident);
3123 SkipUntil(tok::r_square, tok::comma, true, true);
3124 continue;
3125 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003126 }
3127
Michael Han6880f492012-10-03 01:56:22 +00003128 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Sean Huntbbd37c62009-11-21 08:43:09 +00003129 bool AttrParsed = false;
Sean Huntbbd37c62009-11-21 08:43:09 +00003130
Michael Han6880f492012-10-03 01:56:22 +00003131 // Parse attribute arguments
3132 if (Tok.is(tok::l_paren)) {
3133 if (ScopeName && ScopeName->getName() == "gnu") {
3134 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3135 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3136 AttrParsed = true;
3137 } else {
3138 if (StandardAttr)
3139 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3140 << AttrName->getName();
3141
3142 // FIXME: handle other formats of c++11 attribute arguments
3143 ConsumeParen();
3144 SkipUntil(tok::r_paren, false);
3145 }
3146 }
3147
3148 if (!AttrParsed)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00003149 attrs.addNew(AttrName,
3150 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3151 AttrLoc),
3152 ScopeName, ScopeLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00003153 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smith6ee326a2012-04-10 01:32:12 +00003154
Richard Smithc56298d2012-04-10 03:25:07 +00003155 if (Tok.is(tok::ellipsis)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003156 ConsumeToken();
Michael Han6880f492012-10-03 01:56:22 +00003157
3158 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3159 << AttrName->getName();
Richard Smithc56298d2012-04-10 03:25:07 +00003160 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003161 }
3162
3163 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3164 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003165 if (endLoc)
3166 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00003167 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3168 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003169}
Sean Huntbbd37c62009-11-21 08:43:09 +00003170
Sean Hunt2edf0a22012-06-23 05:07:58 +00003171/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003172///
3173/// attribute-specifier-seq:
3174/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00003175void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003176 SourceLocation *endLoc) {
3177 SourceLocation StartLoc = Tok.getLocation(), Loc;
3178 if (!endLoc)
3179 endLoc = &Loc;
3180
Douglas Gregor8828ee72011-10-07 20:35:25 +00003181 do {
Richard Smithc56298d2012-04-10 03:25:07 +00003182 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003183 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003184
3185 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003186}
3187
Francois Pichet334d47e2010-10-11 12:59:39 +00003188/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3189///
3190/// [MS] ms-attribute:
3191/// '[' token-seq ']'
3192///
3193/// [MS] ms-attribute-seq:
3194/// ms-attribute[opt]
3195/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003196void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3197 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003198 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3199
3200 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003201 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003202 ConsumeBracket();
3203 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003204 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003205 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3206 }
3207}
Francois Pichet563a6452011-05-25 10:19:49 +00003208
3209void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3210 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003211 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003212 if (ParseMicrosoftIfExistsCondition(Result))
3213 return;
3214
Douglas Gregor3896fc52011-10-24 22:31:10 +00003215 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3216 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003217 Diag(Tok, diag::err_expected_lbrace);
3218 return;
3219 }
Francois Pichet563a6452011-05-25 10:19:49 +00003220
Douglas Gregor3896fc52011-10-24 22:31:10 +00003221 switch (Result.Behavior) {
3222 case IEB_Parse:
3223 // Parse the declarations below.
3224 break;
3225
3226 case IEB_Dependent:
3227 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3228 << Result.IsIfExists;
3229 // Fall through to skip.
3230
3231 case IEB_Skip:
3232 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003233 return;
3234 }
3235
Douglas Gregor3896fc52011-10-24 22:31:10 +00003236 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003237 // __if_exists, __if_not_exists can nest.
3238 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3239 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3240 continue;
3241 }
3242
3243 // Check for extraneous top-level semicolon.
3244 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003245 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003246 continue;
3247 }
3248
3249 AccessSpecifier AS = getAccessSpecifierIfPresent();
3250 if (AS != AS_none) {
3251 // Current token is a C++ access specifier.
3252 CurAS = AS;
3253 SourceLocation ASLoc = Tok.getLocation();
3254 ConsumeToken();
3255 if (Tok.is(tok::colon))
3256 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3257 else
3258 Diag(Tok, diag::err_expected_colon);
3259 ConsumeToken();
3260 continue;
3261 }
3262
3263 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003264 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003265 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003266
3267 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003268}