blob: 4f0c893b040388855dc86403f233d6271f207499 [file] [log] [blame]
Chris Lattnera5235172007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnera5235172007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor423984d2008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
16#include "clang/Basic/OperatorKinds.h"
Chris Lattner60f36222009-01-29 05:15:15 +000017#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000018#include "clang/Sema/DeclSpec.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Sema/Scope.h"
John McCalldb632ac2012-09-25 07:32:39 +000022#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Chris Lattnera5235172007-08-25 06:57:03 +000024using namespace clang;
25
26/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redl67667942010-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 Lattnera5235172007-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 Redl67667942010-08-27 23:12:46 +000035/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000036///
37/// named-namespace-definition:
38/// original-namespace-definition
39/// extension-namespace-definition
40///
41/// original-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000042/// 'inline'[opt] 'namespace' identifier attributes[opt]
43/// '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000044///
45/// extension-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000046/// 'inline'[opt] 'namespace' original-namespace-name
47/// '{' namespace-body '}'
Mike Stump11289f42009-09-09 15:08:12 +000048///
Chris Lattnera5235172007-08-25 06:57:03 +000049/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
50/// 'namespace' identifier '=' qualified-namespace-specifier ';'
51///
John McCall48871652010-08-21 09:40:31 +000052Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redl67667942010-08-27 23:12:46 +000053 SourceLocation &DeclEnd,
54 SourceLocation InlineLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000055 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000056 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian4bf82622011-08-22 17:59:19 +000057 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000058
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000059 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000060 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000061 cutOffParsing();
62 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000063 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000064
Chris Lattnera5235172007-08-25 06:57:03 +000065 SourceLocation IdentLoc;
66 IdentifierInfo *Ident = 0;
Richard Trieu61384cb2011-05-26 20:11:09 +000067 std::vector<SourceLocation> ExtraIdentLoc;
68 std::vector<IdentifierInfo*> ExtraIdent;
69 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000070
71 Token attrTok;
Mike Stump11289f42009-09-09 15:08:12 +000072
Chris Lattner76c72282007-10-09 17:33:22 +000073 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000074 Ident = Tok.getIdentifierInfo();
75 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieu61384cb2011-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 Lattnera5235172007-08-25 06:57:03 +000081 }
Mike Stump11289f42009-09-09 15:08:12 +000082
Chris Lattnera5235172007-08-25 06:57:03 +000083 // Read label attributes, if present.
John McCall084e83d2011-03-24 11:26:52 +000084 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000085 if (Tok.is(tok::kw___attribute)) {
86 attrTok = Tok;
John McCall53fa7142010-12-24 02:08:15 +000087 ParseGNUAttributes(attrs);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000088 }
Mike Stump11289f42009-09-09 15:08:12 +000089
Douglas Gregor6b6bba42009-06-17 19:49:00 +000090 if (Tok.is(tok::equal)) {
Nico Weber729f1e22012-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 McCall53fa7142010-12-24 02:08:15 +000097 if (!attrs.empty())
Douglas Gregor6b6bba42009-06-17 19:49:00 +000098 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redl67667942010-08-27 23:12:46 +000099 if (InlineLoc.isValid())
100 Diag(InlineLoc, diag::err_inline_namespace_alias)
101 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000102 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000103 }
Mike Stump11289f42009-09-09 15:08:12 +0000104
Richard Trieu61384cb2011-05-26 20:11:09 +0000105
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000106 BalancedDelimiterTracker T(*this, tok::l_brace);
107 if (T.consumeOpen()) {
Richard Trieu61384cb2011-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 Stump11289f42009-09-09 15:08:12 +0000112 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner4de55aa2009-03-29 14:02:43 +0000113 diag::err_expected_ident_lbrace);
John McCall48871652010-08-21 09:40:31 +0000114 return 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000115 }
Mike Stump11289f42009-09-09 15:08:12 +0000116
Douglas Gregor0be31a22010-07-02 17:43:08 +0000117 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
118 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
119 getCurScope()->getFnParent()) {
Richard Trieu61384cb2011-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 Gregore7a8e3b2011-10-12 16:37:45 +0000124 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Douglas Gregor05cfc292010-05-14 05:08:22 +0000125 SkipUntil(tok::r_brace, false);
John McCall48871652010-08-21 09:40:31 +0000126 return 0;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000127 }
128
Richard Trieu61384cb2011-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 Kramerf546f412011-05-26 21:32:30 +0000139 std::string NamespaceFix;
Richard Trieu61384cb2011-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 Kramerf546f412011-05-26 21:32:30 +0000145
Richard Trieu61384cb2011-05-26 20:11:09 +0000146 std::string RBraces;
Benjamin Kramerf546f412011-05-26 21:32:30 +0000147 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieu61384cb2011-05-26 20:11:09 +0000148 RBraces += "} ";
Benjamin Kramerf546f412011-05-26 21:32:30 +0000149
Richard Trieu61384cb2011-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 Redl5a5f2c72010-08-31 00:36:45 +0000158 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith5d164bc2011-10-15 05:09:34 +0000159 if (InlineLoc.isValid())
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000160 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000161 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000162
Chris Lattner4de55aa2009-03-29 14:02:43 +0000163 // Enter a scope for the namespace.
164 ParseScope NamespaceScope(this, Scope::DeclScope);
165
John McCall48871652010-08-21 09:40:31 +0000166 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000167 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000168 IdentLoc, Ident, T.getOpenLocation(),
169 attrs.getList());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000170
John McCallfaf5fb42010-08-26 23:41:50 +0000171 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
172 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000173
Richard Trieu61384cb2011-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 Gregore7a8e3b2011-10-12 16:37:45 +0000177 InlineLoc, attrs, T);
Mike Stump11289f42009-09-09 15:08:12 +0000178
Chris Lattner4de55aa2009-03-29 14:02:43 +0000179 // Leave the namespace scope.
180 NamespaceScope.Exit();
181
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000182 DeclEnd = T.getCloseLocation();
183 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000184
185 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000186}
Chris Lattner38376f12008-01-12 07:05:38 +0000187
Richard Trieu61384cb2011-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 Trieu61384cb2011-05-26 20:11:09 +0000193 ParsedAttributes& attrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000194 BalancedDelimiterTracker &Tracker) {
Richard Trieu61384cb2011-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 Smith89645bc2013-01-02 12:01:23 +0000198 MaybeParseCXX11Attributes(attrs);
Richard Trieu61384cb2011-05-26 20:11:09 +0000199 MaybeParseMicrosoftAttributes(attrs);
200 ParseExternalDeclaration(attrs);
201 }
Douglas Gregore7a8e3b2011-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 Trieu61384cb2011-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 Gregore7a8e3b2011-10-12 16:37:45 +0000215 Ident[index], Tracker.getOpenLocation(),
216 attrs.getList());
Richard Trieu61384cb2011-05-26 20:11:09 +0000217
218 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000219 attrs, Tracker);
Richard Trieu61384cb2011-05-26 20:11:09 +0000220
221 NamespaceScope.Exit();
222
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000223 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieu61384cb2011-05-26 20:11:09 +0000224}
225
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000226/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
227/// alias definition.
228///
John McCall48871652010-08-21 09:40:31 +0000229Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall084e83d2011-03-24 11:26:52 +0000230 SourceLocation AliasLoc,
231 IdentifierInfo *Alias,
232 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000233 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000234
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000235 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000236
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000237 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000238 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000239 cutOffParsing();
240 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000241 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000242
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000243 CXXScopeSpec SS;
244 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000245 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlsson1894f0d42009-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 McCall48871652010-08-21 09:40:31 +0000251 return 0;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000252 }
253
254 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000255 IdentifierInfo *Ident = Tok.getIdentifierInfo();
256 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000257
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000258 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000259 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000260 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
261 "", tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000262
Douglas Gregor0be31a22010-07-02 17:43:08 +0000263 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000264 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000265}
266
Chris Lattner38376f12008-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 Lattner8ea64422010-11-09 20:15:55 +0000274Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregor15799fd2008-11-21 16:10:08 +0000275 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000276 SmallString<8> LangBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000277 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000278 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000279 if (Invalid)
John McCall48871652010-08-21 09:40:31 +0000280 return 0;
Chris Lattner38376f12008-01-12 07:05:38 +0000281
Richard Smithd67aea22012-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 Lattner38376f12008-01-12 07:05:38 +0000287 SourceLocation Loc = ConsumeStringToken();
Chris Lattner38376f12008-01-12 07:05:38 +0000288
Douglas Gregor07665a62009-01-05 19:45:36 +0000289 ParseScope LinkageScope(this, Scope::DeclScope);
John McCall48871652010-08-21 09:40:31 +0000290 Decl *LinkageSpec
Douglas Gregor0be31a22010-07-02 17:43:08 +0000291 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000292 DS.getSourceRange().getBegin(),
Benjamin Kramerbebee842010-05-03 13:08:54 +0000293 Loc, Lang,
Abramo Bagnaraea947882011-03-08 16:41:52 +0000294 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor07665a62009-01-05 19:45:36 +0000295 : SourceLocation());
296
John McCall084e83d2011-03-24 11:26:52 +0000297 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000298 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000299 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000300
Douglas Gregor07665a62009-01-05 19:45:36 +0000301 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara4d423992011-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 Bagnaraed5b6892010-07-30 16:47:02 +0000307 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000308 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000309 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor07665a62009-01-05 19:45:36 +0000310 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000311 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000312
Douglas Gregorb65a9132010-02-07 08:38:28 +0000313 DS.abort();
314
John McCall53fa7142010-12-24 02:08:15 +0000315 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000316
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000317 BalancedDelimiterTracker T(*this, tok::l_brace);
318 T.consumeOpen();
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000319 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall084e83d2011-03-24 11:26:52 +0000320 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000321 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000322 MaybeParseMicrosoftAttributes(attrs);
323 ParseExternalDeclaration(attrs);
Chris Lattner38376f12008-01-12 07:05:38 +0000324 }
325
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000326 T.consumeClose();
Chris Lattner8ea64422010-11-09 20:15:55 +0000327 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000328 T.getCloseLocation());
Chris Lattner38376f12008-01-12 07:05:38 +0000329}
Douglas Gregor556877c2008-04-13 21:30:24 +0000330
Douglas Gregord7c4d982008-12-30 03:27:21 +0000331/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
332/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000333Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000334 const ParsedTemplateInfo &TemplateInfo,
335 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000336 ParsedAttributesWithRange &attrs,
337 Decl **OwnedType) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000338 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000339 ObjCDeclContextSwitch ObjCDC(*this);
340
Douglas Gregord7c4d982008-12-30 03:27:21 +0000341 // Eat 'using'.
342 SourceLocation UsingLoc = ConsumeToken();
343
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000344 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000345 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000346 cutOffParsing();
347 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000348 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000349
John McCall9b72f892010-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 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000358
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000359 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000360 }
361
Richard Smithdda56e42011-04-15 14:24:37 +0000362 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +0000363
364 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000365 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000366
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000367 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000368 AS_none, OwnedType);
Douglas Gregord7c4d982008-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 McCall48871652010-08-21 09:40:31 +0000381Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000382 SourceLocation UsingLoc,
383 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000384 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-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 Gregor7e90c6d2009-09-18 19:03:04 +0000390 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000391 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000392 cutOffParsing();
393 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000394 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000395
Douglas Gregord7c4d982008-12-30 03:27:21 +0000396 CXXScopeSpec SS;
397 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000398 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000399
Douglas Gregord7c4d982008-12-30 03:27:21 +0000400 IdentifierInfo *NamespcName = 0;
401 SourceLocation IdentLoc = SourceLocation();
402
403 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000404 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-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 McCall48871652010-08-21 09:40:31 +0000409 return 0;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000410 }
Mike Stump11289f42009-09-09 15:08:12 +0000411
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000412 // Parse identifier.
413 NamespcName = Tok.getIdentifierInfo();
414 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000415
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000416 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000417 bool GNUAttr = false;
418 if (Tok.is(tok::kw___attribute)) {
419 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000420 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000421 }
Mike Stump11289f42009-09-09 15:08:12 +0000422
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000423 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000424 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000425 ExpectAndConsume(tok::semi,
Douglas Gregor45d6bdf2010-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 Gregord7c4d982008-12-30 03:27:21 +0000429
Douglas Gregor0be31a22010-07-02 17:43:08 +0000430 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000431 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000432}
433
Richard Smithdda56e42011-04-15 14:24:37 +0000434/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
435/// Assumes that 'using' was already seen.
Douglas Gregord7c4d982008-12-30 03:27:21 +0000436///
437/// using-declaration: [C++ 7.3.p3: namespace.udecl]
438/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000439/// unqualified-id
440/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000441///
Richard Smithdda56e42011-04-15 14:24:37 +0000442/// alias-declaration: C++0x [decl.typedef]p2
443/// 'using' identifier = type-id ;
444///
John McCall48871652010-08-21 09:40:31 +0000445Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000446 const ParsedTemplateInfo &TemplateInfo,
447 SourceLocation UsingLoc,
448 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000449 AccessSpecifier AS,
450 Decl **OwnedType) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000451 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000452 SourceLocation TypenameLoc;
Douglas Gregorfec52632009-06-20 00:51:54 +0000453 bool IsTypeName;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000454 ParsedAttributesWithRange attrs(AttrFactory);
455
456 // FIXME: Simply skip the attributes and diagnose, don't bother parsing them.
Richard Smith89645bc2013-01-02 12:01:23 +0000457 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000458 ProhibitAttributes(attrs);
459 attrs.clear();
460 attrs.Range = SourceRange();
Douglas Gregorfec52632009-06-20 00:51:54 +0000461
462 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000463 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregorfec52632009-06-20 00:51:54 +0000464 if (Tok.is(tok::kw_typename)) {
John McCalle61f2ba2009-11-18 02:36:19 +0000465 TypenameLoc = Tok.getLocation();
Douglas Gregorfec52632009-06-20 00:51:54 +0000466 ConsumeToken();
467 IsTypeName = true;
468 }
469 else
470 IsTypeName = false;
471
472 // Parse nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000473 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregorfec52632009-06-20 00:51:54 +0000474
Douglas Gregorfec52632009-06-20 00:51:54 +0000475 // Check nested-name specifier.
476 if (SS.isInvalid()) {
477 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000478 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000479 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000480
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000481 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000482 // destructor names and allow the action module to diagnose any semantic
483 // errors.
Abramo Bagnara7945c982012-01-27 09:46:47 +0000484 SourceLocation TemplateKWLoc;
Douglas Gregor220f4272009-11-04 16:30:06 +0000485 UnqualifiedId Name;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000486 if (ParseUnqualifiedId(SS,
Douglas Gregor220f4272009-11-04 16:30:06 +0000487 /*EnteringContext=*/false,
488 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000489 /*AllowConstructorName=*/true,
John McCallba7bf592010-08-24 05:47:05 +0000490 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000491 TemplateKWLoc,
Douglas Gregor220f4272009-11-04 16:30:06 +0000492 Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000493 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000494 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000495 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000496
Richard Smith89645bc2013-01-02 12:01:23 +0000497 MaybeParseCXX11Attributes(attrs);
Richard Smithdda56e42011-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 Smith3f1b5d02011-05-05 21:57:07 +0000503 // TODO: Attribute support. C++0x attributes may appear before the equals.
504 // Where can GNU attributes appear?
Richard Smithdda56e42011-04-15 14:24:37 +0000505 ConsumeToken();
506
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000507 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000508 diag::warn_cxx98_compat_alias_declaration :
509 diag::ext_alias_declaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000510
Richard Smith3f1b5d02011-05-05 21:57:07 +0000511 // Type alias templates cannot be specialized.
512 int SpecKind = -1;
Richard Smith14034022011-05-05 22:36:10 +0000513 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
514 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3f1b5d02011-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 Smithdda56e42011-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 Smith3f1b5d02011-05-05 21:57:07 +0000547 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
548 Declarator::AliasTemplateContext :
John McCalla55902b2011-10-01 09:56:14 +0000549 Declarator::AliasDeclContext, AS, OwnedType);
Alexis Hunt6aa9bee2012-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 Smithdda56e42011-04-15 14:24:37 +0000555 // Parse (optional) attributes (most likely GNU strong-using extension).
556 MaybeParseGNUAttributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000557 }
Mike Stump11289f42009-09-09 15:08:12 +0000558
Douglas Gregorfec52632009-06-20 00:51:54 +0000559 // Eat ';'.
560 DeclEnd = Tok.getLocation();
561 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smithdda56e42011-04-15 14:24:37 +0000562 !attrs.empty() ? "attributes list" :
563 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor220f4272009-11-04 16:30:06 +0000564 tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000565
John McCall9b72f892010-11-10 02:40:36 +0000566 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith3f1b5d02011-05-05 21:57:07 +0000567 // In C++0x, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000568 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000569 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-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 Gregor882a61a2011-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 Smith3f1b5d02011-05-05 21:57:07 +0000589 if (IsAliasDecl) {
590 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000591 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3f1b5d02011-05-05 21:57:07 +0000592 TemplateParams ? TemplateParams->data() : 0,
593 TemplateParams ? TemplateParams->size() : 0);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000594 // FIXME: Propagate attributes.
Richard Smith3f1b5d02011-05-05 21:57:07 +0000595 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
596 UsingLoc, Name, TypeAlias);
597 }
Richard Smithdda56e42011-04-15 14:24:37 +0000598
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000599 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000600 Name, attrs.getList(),
601 IsTypeName, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000602}
603
Benjamin Kramere56f3932011-12-23 17:00:35 +0000604/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000605///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000606/// [C++0x] static_assert-declaration:
607/// static_assert ( constant-expression , string-literal ) ;
608///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000609/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000610/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000611///
John McCall48871652010-08-21 09:40:31 +0000612Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-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 Blaikiebbafb8a2012-03-11 07:00:24 +0000616 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000617 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000618 if (Tok.is(tok::kw_static_assert))
619 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000620
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000621 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000622
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000623 BalancedDelimiterTracker T(*this, tok::l_paren);
624 if (T.consumeOpen()) {
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000625 Diag(Tok, diag::err_expected_lparen);
Richard Smith76965712012-09-13 19:12:50 +0000626 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000627 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000628 }
Mike Stump11289f42009-09-09 15:08:12 +0000629
John McCalldadc5752010-08-24 06:29:42 +0000630 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000631 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000632 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000633 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000634 }
Mike Stump11289f42009-09-09 15:08:12 +0000635
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000636 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCall48871652010-08-21 09:40:31 +0000637 return 0;
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000638
Richard Smithf506eaf2012-03-05 23:20:05 +0000639 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000640 Diag(Tok, diag::err_expected_string_literal)
641 << /*Source='static_assert'*/1;
Richard Smith76965712012-09-13 19:12:50 +0000642 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000643 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000644 }
Mike Stump11289f42009-09-09 15:08:12 +0000645
John McCalldadc5752010-08-24 06:29:42 +0000646 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smithd67aea22012-03-06 03:21:47 +0000647 if (AssertMessage.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000648 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000649 return 0;
Richard Smithd67aea22012-03-06 03:21:47 +0000650 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000651
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000652 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000653
Chris Lattner49836b42009-04-02 04:16:50 +0000654 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000655 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000656
John McCallb268a282010-08-23 23:25:46 +0000657 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
658 AssertExpr.take(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000659 AssertMessage.take(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000660 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000661}
662
Anders Carlsson74948d02009-06-24 17:47:40 +0000663/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
664///
665/// 'decltype' ( expression )
666///
David Blaikie15a430a2011-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 Carlsson74948d02009-06-24 17:47:40 +0000671
David Blaikie15a430a2011-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 Smith324df552012-02-24 22:30:04 +0000685 if (Tok.getIdentifierInfo()->isStr("decltype"))
686 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000687
David Blaikie15a430a2011-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 Smithfd555f62012-02-22 02:04:18 +0000702 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
703 0, /*IsDecltype=*/true);
David Blaikie15a430a2011-12-04 05:04:18 +0000704 Result = ParseExpression();
705 if (Result.isInvalid()) {
David Blaikie15a430a2011-12-04 05:04:18 +0000706 DS.SetTypeSpecError();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000707 if (SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true)) {
708 EndLoc = ConsumeParen();
709 } else {
Richard Smithc88e4042012-12-09 04:17:57 +0000710 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
Argyrios Kyrtzidisc38395a2012-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 Blaikie15a430a2011-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 Smithfd555f62012-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 Blaikie15a430a2011-12-04 05:04:18 +0000738 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000739 }
Mike Stump11289f42009-09-09 15:08:12 +0000740
Anders Carlsson74948d02009-06-24 17:47:40 +0000741 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000742 unsigned DiagID;
Anders Carlsson74948d02009-06-24 17:47:40 +0000743 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump11289f42009-09-09 15:08:12 +0000744 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
David Blaikie15a430a2011-12-04 05:04:18 +0000745 DiagID, Result.release())) {
John McCall49bfce42009-08-03 20:12:06 +0000746 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-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 Carlsson74948d02009-06-24 17:47:40 +0000767}
768
Alexis Hunt4a257072011-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 Gregore7a8e3b2011-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)) {
Alexis Hunt4a257072011-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 Gregore7a8e3b2011-10-12 16:37:45 +0000787 T.consumeClose();
788 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +0000789 return;
790
791 const char *PrevSpec = 0;
792 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000793 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Alexis Hunt4a257072011-05-19 05:37:45 +0000794 DiagID, Result.release()))
795 Diag(StartLoc, DiagID) << PrevSpec;
796}
797
David Blaikie00ee7a082011-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 Gregor831c93f2008-11-05 20:51:48 +0000803///
David Blaikie00ee7a082011-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 Gregor831c93f2008-11-05 20:51:48 +0000809/// class-name: [C++ 9.1]
810/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000811/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000812///
David Blaikie1cd50022011-10-25 17:10:12 +0000813Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
814 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-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 Blaikieafa155f2011-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 Blaikie1cd50022011-10-25 17:10:12 +0000828 // Parse decltype-specifier
David Blaikie15a430a2011-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 Blaikieafa155f2011-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 Blaikie1cd50022011-10-25 17:10:12 +0000835 // Fake up a Declarator to use with ActOnTypeName.
836 DeclSpec DS(AttrFactory);
837
David Blaikie7491e732011-12-08 04:53:15 +0000838 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +0000839
840 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
841 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
842 }
843
Douglas Gregord54dfb82009-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 Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000846 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +0000847 if (TemplateId->Kind == TNK_Type_template ||
848 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000849 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000850
851 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000852 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000853 EndLocation = Tok.getAnnotationEndLoc();
854 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000855
856 if (Type)
857 return Type;
858 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000859 }
860
861 // Fall through to produce an error below.
862 }
863
Douglas Gregor831c93f2008-11-05 20:51:48 +0000864 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000865 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000866 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000867 }
868
Douglas Gregor18473f32010-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 Gregor0be31a22010-07-02 17:43:08 +0000877 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000878 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000879 Diag(IdLoc, diag::err_unknown_template_name)
880 << Id;
881 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000882
Douglas Gregor18473f32010-01-12 21:28:44 +0000883 if (!Template)
884 return true;
885
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000886 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000887 UnqualifiedId TemplateName;
888 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000889
Douglas Gregor18473f32010-01-12 21:28:44 +0000890 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +0000891 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
892 TemplateName, true))
Douglas Gregor18473f32010-01-12 21:28:44 +0000893 return true;
894 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +0000895 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000896
Douglas Gregor18473f32010-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 Rajaratnamba2c6522010-03-13 10:17:05 +0000901
Douglas Gregor18473f32010-01-12 21:28:44 +0000902 // Retrieve the type from the annotation token, consume that token, and
903 // return.
904 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +0000905 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +0000906 ConsumeToken();
907 return Type;
908 }
909
Douglas Gregor831c93f2008-11-05 20:51:48 +0000910 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +0000911 IdentifierInfo *CorrectedII = 0;
Douglas Gregore7c20652011-03-02 00:47:37 +0000912 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +0000913 false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +0000914 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +0000915 /*NonTrivialTypeSourceInfo=*/true,
916 &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000917 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +0000918 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000919 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000920 }
921
922 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +0000923 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000924
925 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000926 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +0000927 DS.SetRangeStart(IdLoc);
928 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +0000929 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-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 Gregor831c93f2008-11-05 20:51:48 +0000937}
938
John McCall8d32c052012-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,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000946 SourceLocation(), 0, 0, AttributeList::AS_GNU);
John McCall8d32c052012-05-22 21:28:12 +0000947 }
948}
949
Richard Smith369b9f92012-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 Smith200f47c2012-07-02 19:14:01 +0000953bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-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 = ...
Richard Smith1ac67d12013-01-19 03:48:05 +0000960 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +0000961 case tok::identifier: // struct foo {...} V ;
962 case tok::r_paren: //(struct foo {...} ) {4}
963 case tok::annot_cxxscope: // struct foo {...} a:: b;
964 case tok::annot_typename: // struct foo {...} a ::b;
965 case tok::annot_template_id: // struct foo {...} a<int> ::b;
966 case tok::l_paren: // struct foo {...} ( x);
967 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +0000968 case tok::kw_operator: // struct foo operator ++() {...}
Richard Smith369b9f92012-06-25 21:37:02 +0000969 return true;
Richard Smith200f47c2012-07-02 19:14:01 +0000970 case tok::colon:
971 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smith369b9f92012-06-25 21:37:02 +0000972 // Type qualifiers
973 case tok::kw_const: // struct foo {...} const x;
974 case tok::kw_volatile: // struct foo {...} volatile x;
975 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith1ac67d12013-01-19 03:48:05 +0000976 // Function specifiers
977 // Note, no 'explicit'. An explicit function must be either a conversion
978 // operator or a constructor. Either way, it can't have a return type.
979 case tok::kw_inline: // struct foo inline f();
980 case tok::kw_virtual: // struct foo virtual f();
981 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +0000982 // Storage-class specifiers
983 case tok::kw_static: // struct foo {...} static x;
984 case tok::kw_extern: // struct foo {...} extern x;
985 case tok::kw_typedef: // struct foo {...} typedef x;
986 case tok::kw_register: // struct foo {...} register x;
987 case tok::kw_auto: // struct foo {...} auto x;
988 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +0000989 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +0000990 case tok::kw_constexpr: // struct foo {...} constexpr x;
991 // As shown above, type qualifiers and storage class specifiers absolutely
992 // can occur after class specifiers according to the grammar. However,
993 // almost no one actually writes code like this. If we see one of these,
994 // it is much more likely that someone missed a semi colon and the
995 // type/storage class specifier we're seeing is part of the *next*
996 // intended declaration, as in:
997 //
998 // struct foo { ... }
999 // typedef int X;
1000 //
1001 // We'd really like to emit a missing semicolon error instead of emitting
1002 // an error on the 'int' saying that you can't have two type specifiers in
1003 // the same declaration of X. Because of this, we look ahead past this
1004 // token to see if it's a type specifier. If so, we know the code is
1005 // otherwise invalid, so we can produce the expected semi error.
1006 if (!isKnownToBeTypeSpecifier(NextToken()))
1007 return true;
1008 break;
1009 case tok::r_brace: // struct bar { struct foo {...} }
1010 // Missing ';' at end of struct is accepted as an extension in C mode.
1011 if (!getLangOpts().CPlusPlus)
1012 return true;
1013 break;
Richard Smith1ac67d12013-01-19 03:48:05 +00001014 // C++11 attributes
1015 case tok::l_square: // enum E [[]] x
1016 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1017 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith52c5b872013-01-29 04:13:32 +00001018 case tok::greater:
1019 // template<class T = class X>
1020 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001021 }
1022 return false;
1023}
1024
Douglas Gregor556877c2008-04-13 21:30:24 +00001025/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1026/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1027/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001028/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001029///
1030/// class-specifier: [C++ class]
1031/// class-head '{' member-specification[opt] '}'
1032/// class-head '{' member-specification[opt] '}' attributes[opt]
1033/// class-head:
1034/// class-key identifier[opt] base-clause[opt]
1035/// class-key nested-name-specifier identifier base-clause[opt]
1036/// class-key nested-name-specifier[opt] simple-template-id
1037/// base-clause[opt]
1038/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001039/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001040/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001041/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001042/// simple-template-id base-clause[opt]
1043/// class-key:
1044/// 'class'
1045/// 'struct'
1046/// 'union'
1047///
1048/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001049/// class-key ::[opt] nested-name-specifier[opt] identifier
1050/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1051/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001052///
1053/// Note that the C++ class-specifier and elaborated-type-specifier,
1054/// together, subsume the C99 struct-or-union-specifier:
1055///
1056/// struct-or-union-specifier: [C99 6.7.2.1]
1057/// struct-or-union identifier[opt] '{' struct-contents '}'
1058/// struct-or-union identifier
1059/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1060/// '}' attributes[opt]
1061/// [GNU] struct-or-union attributes[opt] identifier
1062/// struct-or-union:
1063/// 'struct'
1064/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001065void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1066 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001067 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001068 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001069 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001070 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001071 DeclSpec::TST TagType;
1072 if (TagTokKind == tok::kw_struct)
1073 TagType = DeclSpec::TST_struct;
1074 else if (TagTokKind == tok::kw___interface)
1075 TagType = DeclSpec::TST_interface;
1076 else if (TagTokKind == tok::kw_class)
1077 TagType = DeclSpec::TST_class;
1078 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001079 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1080 TagType = DeclSpec::TST_union;
1081 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001082
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001083 if (Tok.is(tok::code_completion)) {
1084 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001085 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001086 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001087 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001088
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001089 // C++03 [temp.explicit] 14.7.2/8:
1090 // The usual access checking rules do not apply to names used to specify
1091 // explicit instantiations.
1092 //
1093 // As an extension we do not perform access checking on the names used to
1094 // specify explicit specializations either. This is important to allow
1095 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001096 //
1097 // Note that we don't suppress if this turns out to be an elaborated
1098 // type specifier.
1099 bool shouldDelayDiagsInTag =
1100 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1101 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1102 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001103
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001104 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001105 // If attributes exist after tag, parse them.
1106 if (Tok.is(tok::kw___attribute))
John McCall53fa7142010-12-24 02:08:15 +00001107 ParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +00001108
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001109 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +00001110 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +00001111 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001112
John McCall8d32c052012-05-22 21:28:12 +00001113 // Parse inheritance specifiers.
1114 if (Tok.is(tok::kw___single_inheritance) ||
1115 Tok.is(tok::kw___multiple_inheritance) ||
1116 Tok.is(tok::kw___virtual_inheritance))
1117 ParseMicrosoftInheritanceClassAttributes(attrs);
1118
Alexis Hunt96d5c762009-11-21 08:43:09 +00001119 // If C++0x attributes exist here, parse them.
1120 // FIXME: Are we consistent with the ordering of parsing of different
1121 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001122 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001123
Michael Han309af292013-01-07 16:57:11 +00001124 // Source location used by FIXIT to insert misplaced
1125 // C++11 attributes
1126 SourceLocation AttrFixitLoc = Tok.getLocation();
1127
John Wiegley65497cc2011-04-27 23:09:49 +00001128 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorf1fce5d2011-04-29 15:31:39 +00001129 !Tok.is(tok::identifier) &&
1130 Tok.getIdentifierInfo() &&
1131 (Tok.is(tok::kw___is_arithmetic) ||
1132 Tok.is(tok::kw___is_convertible) ||
John Wiegley65497cc2011-04-27 23:09:49 +00001133 Tok.is(tok::kw___is_empty) ||
Douglas Gregorf1fce5d2011-04-29 15:31:39 +00001134 Tok.is(tok::kw___is_floating_point) ||
1135 Tok.is(tok::kw___is_function) ||
John Wiegley65497cc2011-04-27 23:09:49 +00001136 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorf1fce5d2011-04-29 15:31:39 +00001137 Tok.is(tok::kw___is_integral) ||
1138 Tok.is(tok::kw___is_member_function_pointer) ||
1139 Tok.is(tok::kw___is_member_pointer) ||
1140 Tok.is(tok::kw___is_pod) ||
1141 Tok.is(tok::kw___is_pointer) ||
1142 Tok.is(tok::kw___is_same) ||
Douglas Gregor63180b12011-04-29 01:38:03 +00001143 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorf1fce5d2011-04-29 15:31:39 +00001144 Tok.is(tok::kw___is_signed) ||
1145 Tok.is(tok::kw___is_unsigned) ||
1146 Tok.is(tok::kw___is_void))) {
Douglas Gregordf445f02011-07-30 07:01:49 +00001147 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorf1fce5d2011-04-29 15:31:39 +00001148 // name of struct templates, but some are keywords in GCC >= 4.3
1149 // and Clang. Therefore, when we see the token sequence "struct
1150 // X", make X into a normal identifier rather than a keyword, to
1151 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001152 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregor119b0c72009-09-04 05:53:02 +00001153 Tok.setKind(tok::identifier);
1154 }
Mike Stump11289f42009-09-09 15:08:12 +00001155
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001156 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001157 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001158 if (getLangOpts().CPlusPlus) {
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001159 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1160 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001161
Douglas Gregordf593fb2011-11-07 17:33:42 +00001162 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall413021a2010-07-30 06:26:29 +00001163 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +00001164 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001165 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1166 Diag(Tok, diag::err_expected_ident);
1167 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001168
Douglas Gregor916462b2009-10-30 21:46:58 +00001169 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1170
Douglas Gregor67a65642009-02-17 23:15:12 +00001171 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +00001172 IdentifierInfo *Name = 0;
1173 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +00001174 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +00001175 if (Tok.is(tok::identifier)) {
1176 Name = Tok.getIdentifierInfo();
1177 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001178
David Blaikiebbafb8a2012-03-11 07:00:24 +00001179 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001180 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001181 // Eat the template argument list and try to continue parsing this as
1182 // a class (or template thereof).
1183 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001184 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +00001185 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +00001186 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001187 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001188 // We couldn't parse the template argument list at all, so don't
1189 // try to give any location information for the list.
1190 LAngleLoc = RAngleLoc = SourceLocation();
1191 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001192
Douglas Gregor916462b2009-10-30 21:46:58 +00001193 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matose9a3ed42012-08-31 22:18:20 +00001194 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1195 << (TagType == DeclSpec::TST_class? 0
1196 : TagType == DeclSpec::TST_struct? 1
1197 : TagType == DeclSpec::TST_interface? 2
1198 : 3)
1199 << Name
1200 << SourceRange(LAngleLoc, RAngleLoc);
1201
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001202 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001203 // we've removed its template argument list.
1204 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1205 if (TemplateParams && TemplateParams->size() > 1) {
1206 TemplateParams->pop_back();
1207 } else {
1208 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001209 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001210 = ParsedTemplateInfo::NonTemplate;
1211 }
1212 } else if (TemplateInfo.Kind
1213 == ParsedTemplateInfo::ExplicitInstantiation) {
1214 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +00001215 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001216 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001217 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001218 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001219 = SourceLocation();
1220 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1221 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001222 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001223 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001224 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001225 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001226 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001227
Douglas Gregore7c20652011-03-02 00:47:37 +00001228 if (TemplateId->Kind != TNK_Type_template &&
1229 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001230 // The template-name in the simple-template-id refers to
1231 // something other than a class template. Give an appropriate
1232 // error message and skip to the ';'.
1233 SourceRange Range(NameLoc);
1234 if (SS.isNotEmpty())
1235 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001236
Douglas Gregor7f741122009-02-25 19:37:18 +00001237 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1238 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001239
Douglas Gregor7f741122009-02-25 19:37:18 +00001240 DS.SetTypeSpecError();
1241 SkipUntil(tok::semi, false, true);
Douglas Gregor7f741122009-02-25 19:37:18 +00001242 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001243 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001244 }
1245
Richard Smithbfdb1082012-03-12 08:56:40 +00001246 // There are four options here.
1247 // - If we are in a trailing return type, this is always just a reference,
1248 // and we must not try to parse a definition. For instance,
1249 // [] () -> struct S { };
1250 // does not define a type.
1251 // - If we have 'struct foo {...', 'struct foo :...',
1252 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1253 // - If we have 'struct foo;', then this is either a forward declaration
1254 // or a friend declaration, which have to be treated differently.
1255 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001256 //
1257 // We also detect these erroneous cases to provide better diagnostic for
1258 // C++11 attributes parsing.
1259 // - attributes follow class name:
1260 // struct foo [[]] {};
1261 // - attributes appear before or after 'final':
1262 // struct foo [[]] final [[]] {};
1263 //
Richard Smithc5b05522012-03-12 07:56:15 +00001264 // However, in type-specifier-seq's, things look like declarations but are
1265 // just references, e.g.
1266 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001267 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001268 // &T::operator struct s;
1269 // For these, DSC is DSC_type_specifier.
Michael Han9407e502012-11-26 22:54:45 +00001270
1271 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001272 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001273
John McCallfaf5fb42010-08-26 23:41:50 +00001274 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001275 if (DSC == DSC_trailing)
1276 TUK = Sema::TUK_Reference;
1277 else if (Tok.is(tok::l_brace) ||
1278 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001279 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001280 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001281 if (DS.isFriendSpecified()) {
1282 // C++ [class.friend]p2:
1283 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001284 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001285 << SourceRange(DS.getFriendSpecLoc());
1286
1287 // Skip everything up to the semicolon, so that this looks like a proper
1288 // friend class (or template thereof) declaration.
1289 SkipUntil(tok::semi, true, true);
John McCallfaf5fb42010-08-26 23:41:50 +00001290 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001291 } else {
1292 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001293 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001294 }
Richard Smith89645bc2013-01-02 12:01:23 +00001295 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
Michael Han9407e502012-11-26 22:54:45 +00001296 NextToken().is(tok::kw_alignas) ||
1297 NextToken().is(tok::kw__Alignas))) {
1298 // We can't tell if this is a definition or reference
1299 // until we skipped the 'final' and C++11 attribute specifiers.
1300 TentativeParsingAction PA(*this);
1301
1302 // Skip the 'final' keyword.
1303 ConsumeToken();
1304
1305 // Skip C++11 attribute specifiers.
1306 while (true) {
1307 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1308 ConsumeBracket();
1309 if (!SkipUntil(tok::r_square))
1310 break;
1311 } else if ((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1312 NextToken().is(tok::l_paren)) {
1313 ConsumeToken();
1314 ConsumeParen();
1315 if (!SkipUntil(tok::r_paren))
1316 break;
1317 } else {
1318 break;
1319 }
1320 }
1321
1322 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1323 TUK = Sema::TUK_Definition;
1324 else
1325 TUK = Sema::TUK_Reference;
1326
1327 PA.Revert();
Richard Smith369b9f92012-06-25 21:37:02 +00001328 } else if (DSC != DSC_type_specifier &&
1329 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001330 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001331 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001332 if (Tok.isNot(tok::semi)) {
1333 // A semicolon was missing after this declaration. Diagnose and recover.
1334 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1335 DeclSpec::getSpecifierName(TagType));
1336 PP.EnterToken(Tok);
1337 Tok.setKind(tok::semi);
1338 }
Richard Smith369b9f92012-06-25 21:37:02 +00001339 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001340 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001341
Michael Han9407e502012-11-26 22:54:45 +00001342 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1343 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001344 if (TUK != Sema::TUK_Reference) {
1345 // If this is not a reference, then the only possible
1346 // valid place for C++11 attributes to appear here
1347 // is between class-key and class-name. If there are
1348 // any attributes after class-name, we try a fixit to move
1349 // them to the right place.
1350 SourceRange AttrRange = Attributes.Range;
1351 if (AttrRange.isValid()) {
1352 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1353 << AttrRange
1354 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1355 CharSourceRange(AttrRange, true))
1356 << FixItHint::CreateRemoval(AttrRange);
1357
1358 // Recover by adding misplaced attributes to the attribute list
1359 // of the class so they can be applied on the class later.
1360 attrs.takeAllFrom(Attributes);
1361 }
1362 }
Michael Han9407e502012-11-26 22:54:45 +00001363
John McCall6347b682012-05-07 06:16:58 +00001364 // If this is an elaborated type specifier, and we delayed
1365 // diagnostics before, just merge them into the current pool.
1366 if (shouldDelayDiagsInTag) {
1367 diagsFromTag.done();
1368 if (TUK == Sema::TUK_Reference)
1369 diagsFromTag.redelay();
1370 }
1371
John McCall413021a2010-07-30 06:26:29 +00001372 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001373 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001374 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1375 // We have a declaration or reference to an anonymous class.
1376 Diag(StartLoc, diag::err_anon_type_definition)
1377 << DeclSpec::getSpecifierName(TagType);
1378 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001379
Douglas Gregor556877c2008-04-13 21:30:24 +00001380 SkipUntil(tok::comma, true);
1381 return;
1382 }
1383
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001384 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001385 DeclResult TagOrTempResult = true; // invalid
1386 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001387
Douglas Gregord6ab8742009-05-28 23:31:59 +00001388 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +00001389 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001390 // Explicit specialization, class template partial specialization,
1391 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001392 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001393 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001394 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001395 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001396 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001397 ProhibitAttributes(attrs);
1398
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001399 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001400 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001401 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001402 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001403 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001404 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001405 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001406 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001407 TemplateId->TemplateNameLoc,
1408 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001409 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001410 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001411 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001412
1413 // Friend template-ids are treated as references unless
1414 // they have template headers, in which case they're ill-formed
1415 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1416 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001417 } else if (TUK == Sema::TUK_Reference ||
1418 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001419 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001420 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001421 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001422 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001423 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001424 TemplateId->Template,
1425 TemplateId->TemplateNameLoc,
1426 TemplateId->LAngleLoc,
1427 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001428 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001429 } else {
1430 // This is an explicit specialization or a class template
1431 // partial specialization.
1432 TemplateParameterLists FakedParamLists;
1433
1434 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1435 // This looks like an explicit instantiation, because we have
1436 // something like
1437 //
1438 // template class Foo<X>
1439 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001440 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001441 // meant to be an explicit specialization, but the user forgot
1442 // the '<>' after 'template'.
John McCallfaf5fb42010-08-26 23:41:50 +00001443 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001444
Mike Stump11289f42009-09-09 15:08:12 +00001445 SourceLocation LAngleLoc
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001446 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001447 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001448 diag::err_explicit_instantiation_with_definition)
1449 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregora771f462010-03-31 17:46:05 +00001450 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001451
1452 // Create a fake template parameter list that contains only
1453 // "template<>", so that we treat this construct as a class
1454 // template specialization.
1455 FakedParamLists.push_back(
Mike Stump11289f42009-09-09 15:08:12 +00001456 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001457 TemplateInfo.TemplateLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001458 LAngleLoc,
1459 0, 0,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001460 LAngleLoc));
1461 TemplateParams = &FakedParamLists;
1462 }
1463
1464 // Build the class template specialization.
1465 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001466 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor3c7cd6a2011-09-09 20:53:38 +00001467 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall3e56fd42010-08-23 07:28:44 +00001468 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001469 TemplateId->TemplateNameLoc,
1470 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +00001471 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001472 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001473 attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001474 MultiTemplateParamsArg(
Douglas Gregor67a65642009-02-17 23:15:12 +00001475 TemplateParams? &(*TemplateParams)[0] : 0,
1476 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001477 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001478 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001479 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001480 // Explicit instantiation of a member of a class template
1481 // specialization, e.g.,
1482 //
1483 // template struct Outer<int>::Inner;
1484 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001485 ProhibitAttributes(attrs);
1486
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001487 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001488 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001489 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001490 TemplateInfo.TemplateLoc,
1491 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001492 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001493 } else if (TUK == Sema::TUK_Friend &&
1494 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001495 ProhibitAttributes(attrs);
1496
John McCallace48cd2010-10-19 01:40:49 +00001497 TagOrTempResult =
1498 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1499 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001500 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001501 MultiTemplateParamsArg(
John McCallace48cd2010-10-19 01:40:49 +00001502 TemplateParams? &(*TemplateParams)[0] : 0,
1503 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001504 } else {
1505 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001506 TUK == Sema::TUK_Definition) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001507 // FIXME: Diagnose this particular error.
1508 }
1509
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001510 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1511 ProhibitAttributes(attrs);
1512
John McCall7f41d982009-09-11 04:59:25 +00001513 bool IsDependent = false;
1514
John McCall32723e92010-10-19 18:40:57 +00001515 // Don't pass down template parameter lists if this is just a tag
1516 // reference. For example, we don't need the template parameters here:
1517 // template <class T> class A *makeA(T t);
1518 MultiTemplateParamsArg TParams;
1519 if (TUK != Sema::TUK_Reference && TemplateParams)
1520 TParams =
1521 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1522
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001523 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001524 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001525 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001526 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001527 TParams, Owned, IsDependent,
1528 SourceLocation(), false,
1529 clang::TypeResult());
John McCall7f41d982009-09-11 04:59:25 +00001530
1531 // If ActOnTag said the type was dependent, try again with the
1532 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001533 if (IsDependent) {
1534 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001535 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001536 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001537 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001538 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001539
Douglas Gregor556877c2008-04-13 21:30:24 +00001540 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001541 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001542 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001543 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001544 isCXX11FinalKeyword());
David Blaikiebbafb8a2012-03-11 07:00:24 +00001545 if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001546 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1547 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001548 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001549 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001550 }
1551
John McCallba7bf592010-08-24 05:47:05 +00001552 const char *PrevSpec = 0;
1553 unsigned DiagID;
1554 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001555 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001556 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1557 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallba7bf592010-08-24 05:47:05 +00001558 PrevSpec, DiagID, TypeResult.get());
John McCall7f41d982009-09-11 04:59:25 +00001559 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001560 Result = DS.SetTypeSpecType(TagType, StartLoc,
1561 NameLoc.isValid() ? NameLoc : StartLoc,
1562 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCall7f41d982009-09-11 04:59:25 +00001563 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001564 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001565 return;
1566 }
Mike Stump11289f42009-09-09 15:08:12 +00001567
John McCallba7bf592010-08-24 05:47:05 +00001568 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001569 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001570
Chris Lattnercf251412010-02-02 01:23:29 +00001571 // At this point, we've successfully parsed a class-specifier in 'definition'
1572 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1573 // going to look at what comes after it to improve error recovery. If an
1574 // impossible token occurs next, we assume that the programmer forgot a ; at
1575 // the end of the declaration and recover that way.
1576 //
Richard Smith369b9f92012-06-25 21:37:02 +00001577 // Also enforce C++ [temp]p3:
1578 // In a template-declaration which defines a class, no declarator
1579 // is permitted.
Joao Matose9a3ed42012-08-31 22:18:20 +00001580 if (TUK == Sema::TUK_Definition &&
1581 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001582 if (Tok.isNot(tok::semi)) {
1583 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1584 DeclSpec::getSpecifierName(TagType));
1585 // Push this token back into the preprocessor and change our current token
1586 // to ';' so that the rest of the code recovers as though there were an
1587 // ';' after the definition.
1588 PP.EnterToken(Tok);
1589 Tok.setKind(tok::semi);
1590 }
Chris Lattnercf251412010-02-02 01:23:29 +00001591 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001592}
1593
Mike Stump11289f42009-09-09 15:08:12 +00001594/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001595///
1596/// base-clause : [C++ class.derived]
1597/// ':' base-specifier-list
1598/// base-specifier-list:
1599/// base-specifier '...'[opt]
1600/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001601void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001602 assert(Tok.is(tok::colon) && "Not a base clause");
1603 ConsumeToken();
1604
Douglas Gregor29a92472008-10-22 17:49:05 +00001605 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001606 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001607
Douglas Gregor556877c2008-04-13 21:30:24 +00001608 while (true) {
1609 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001610 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001611 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001612 // Skip the rest of this base specifier, up until the comma or
1613 // opening brace.
Douglas Gregor29a92472008-10-22 17:49:05 +00001614 SkipUntil(tok::comma, tok::l_brace, true, true);
1615 } else {
1616 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001617 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001618 }
1619
1620 // If the next token is a comma, consume it and keep reading
1621 // base-specifiers.
1622 if (Tok.isNot(tok::comma)) break;
Mike Stump11289f42009-09-09 15:08:12 +00001623
Douglas Gregor556877c2008-04-13 21:30:24 +00001624 // Consume the comma.
1625 ConsumeToken();
1626 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001627
1628 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001629 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001630}
1631
1632/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1633/// one entry in the base class list of a class specifier, for example:
1634/// class foo : public bar, virtual private baz {
1635/// 'public bar' and 'virtual private baz' are each base-specifiers.
1636///
1637/// base-specifier: [C++ class.derived]
1638/// ::[opt] nested-name-specifier[opt] class-name
1639/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
David Blaikie00ee7a082011-10-25 15:01:20 +00001640/// base-type-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001641/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
David Blaikie00ee7a082011-10-25 15:01:20 +00001642/// base-type-specifier
John McCall48871652010-08-21 09:40:31 +00001643Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001644 bool IsVirtual = false;
1645 SourceLocation StartLoc = Tok.getLocation();
1646
1647 // Parse the 'virtual' keyword.
1648 if (Tok.is(tok::kw_virtual)) {
1649 ConsumeToken();
1650 IsVirtual = true;
1651 }
1652
1653 // Parse an (optional) access specifier.
1654 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001655 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001656 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001657
Douglas Gregor556877c2008-04-13 21:30:24 +00001658 // Parse the 'virtual' keyword (again!), in case it came after the
1659 // access specifier.
1660 if (Tok.is(tok::kw_virtual)) {
1661 SourceLocation VirtualLoc = ConsumeToken();
1662 if (IsVirtual) {
1663 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001664 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001665 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001666 }
1667
1668 IsVirtual = true;
1669 }
1670
Douglas Gregor831c93f2008-11-05 20:51:48 +00001671 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001672 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00001673 SourceLocation BaseLoc;
1674 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001675 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001676 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001677
Douglas Gregor752a5952011-01-03 22:36:02 +00001678 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1679 // actually part of the base-specifier-list grammar productions, but we
1680 // parse it here for convenience.
1681 SourceLocation EllipsisLoc;
1682 if (Tok.is(tok::ellipsis))
1683 EllipsisLoc = ConsumeToken();
1684
Mike Stump11289f42009-09-09 15:08:12 +00001685 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001686 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001687
Douglas Gregor556877c2008-04-13 21:30:24 +00001688 // Notify semantic analysis that we have parsed a complete
1689 // base-specifier.
Sebastian Redl511ed552008-11-25 22:21:31 +00001690 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor752a5952011-01-03 22:36:02 +00001691 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001692}
1693
1694/// getAccessSpecifierIfPresent - Determine whether the next token is
1695/// a C++ access-specifier.
1696///
1697/// access-specifier: [C++ class.derived]
1698/// 'private'
1699/// 'protected'
1700/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001701AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001702 switch (Tok.getKind()) {
1703 default: return AS_none;
1704 case tok::kw_private: return AS_private;
1705 case tok::kw_protected: return AS_protected;
1706 case tok::kw_public: return AS_public;
1707 }
1708}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001709
Douglas Gregor433e0532012-04-16 18:27:27 +00001710/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith2331bbf2012-05-02 22:22:32 +00001711/// delayed, e.g., default arguments, create a late-parsed method declaration
1712/// record to handle the parsing at the end of the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00001713void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1714 Decl *ThisDecl) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001715 // We just declared a member function. If this member function
Richard Smith2331bbf2012-05-02 22:22:32 +00001716 // has any default arguments, we'll need to parse them later.
Eli Friedman3af2a772009-07-22 21:45:50 +00001717 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001718 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001719 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor433e0532012-04-16 18:27:27 +00001720
Eli Friedman3af2a772009-07-22 21:45:50 +00001721 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1722 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1723 if (!LateMethod) {
1724 // Push this method onto the stack of late-parsed method
1725 // declarations.
Douglas Gregorefc46952010-10-12 16:25:54 +00001726 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1727 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001728 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001729
1730 // Add all of the parameters prior to this one (they don't
1731 // have default arguments).
1732 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1733 for (unsigned I = 0; I < ParamIdx; ++I)
1734 LateMethod->DefaultArgs.push_back(
Douglas Gregor1d85d292010-03-02 01:29:43 +00001735 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001736 }
1737
Douglas Gregor433e0532012-04-16 18:27:27 +00001738 // Add this parameter to the list of parameters (it may or may
Eli Friedman3af2a772009-07-22 21:45:50 +00001739 // not have a default argument).
1740 LateMethod->DefaultArgs.push_back(
1741 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1742 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1743 }
1744 }
1745}
1746
Richard Smith89645bc2013-01-02 12:01:23 +00001747/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001748/// virt-specifier.
1749///
1750/// virt-specifier:
1751/// override
1752/// final
Richard Smith89645bc2013-01-02 12:01:23 +00001753VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001754 if (!getLangOpts().CPlusPlus)
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001755 return VirtSpecifiers::VS_None;
1756
Anders Carlsson56104902011-01-17 03:05:47 +00001757 if (Tok.is(tok::identifier)) {
1758 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001759
Anders Carlsson428803b2011-01-20 03:47:08 +00001760 // Initialize the contextual keywords.
1761 if (!Ident_final) {
1762 Ident_final = &PP.getIdentifierTable().get("final");
1763 Ident_override = &PP.getIdentifierTable().get("override");
1764 }
1765
Anders Carlsson56104902011-01-17 03:05:47 +00001766 if (II == Ident_override)
1767 return VirtSpecifiers::VS_Override;
1768
1769 if (II == Ident_final)
1770 return VirtSpecifiers::VS_Final;
1771 }
1772
1773 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001774}
1775
Richard Smith89645bc2013-01-02 12:01:23 +00001776/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001777///
1778/// virt-specifier-seq:
1779/// virt-specifier
1780/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00001781void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalldb632ac2012-09-25 07:32:39 +00001782 bool IsInterface) {
Anders Carlsson56104902011-01-17 03:05:47 +00001783 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00001784 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001785 if (Specifier == VirtSpecifiers::VS_None)
1786 return;
1787
1788 // C++ [class.mem]p8:
1789 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001790 const char *PrevSpec = 0;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001791 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001792 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1793 << PrevSpec
1794 << FixItHint::CreateRemoval(Tok.getLocation());
1795
John McCalldb632ac2012-09-25 07:32:39 +00001796 if (IsInterface && Specifier == VirtSpecifiers::VS_Final) {
1797 Diag(Tok.getLocation(), diag::err_override_control_interface)
1798 << VirtSpecifiers::getSpecifierName(Specifier);
1799 } else {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001800 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
John McCalldb632ac2012-09-25 07:32:39 +00001801 diag::warn_cxx98_compat_override_control_keyword :
1802 diag::ext_override_control_keyword)
1803 << VirtSpecifiers::getSpecifierName(Specifier);
1804 }
Anders Carlsson56104902011-01-17 03:05:47 +00001805 ConsumeToken();
1806 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001807}
1808
Richard Smith89645bc2013-01-02 12:01:23 +00001809/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlssoncafbab72011-03-25 14:53:29 +00001810/// contextual 'final' keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00001811bool Parser::isCXX11FinalKeyword() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001812 if (!getLangOpts().CPlusPlus)
Anders Carlssoncafbab72011-03-25 14:53:29 +00001813 return false;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001814
Anders Carlssoncafbab72011-03-25 14:53:29 +00001815 if (!Tok.is(tok::identifier))
1816 return false;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001817
Anders Carlssoncafbab72011-03-25 14:53:29 +00001818 // Initialize the contextual keywords.
1819 if (!Ident_final) {
1820 Ident_final = &PP.getIdentifierTable().get("final");
1821 Ident_override = &PP.getIdentifierTable().get("override");
1822 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001823
Anders Carlssoncafbab72011-03-25 14:53:29 +00001824 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001825}
1826
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001827/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1828///
1829/// member-declaration:
1830/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1831/// function-definition ';'[opt]
1832/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1833/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001834/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001835/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001836/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001837///
1838/// member-declarator-list:
1839/// member-declarator
1840/// member-declarator-list ',' member-declarator
1841///
1842/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001843/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001844/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00001845/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001846/// identifier[opt] ':' constant-expression
1847///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001848/// virt-specifier-seq:
1849/// virt-specifier
1850/// virt-specifier-seq virt-specifier
1851///
1852/// virt-specifier:
1853/// override
1854/// final
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001855///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001856/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001857/// '= 0'
1858///
1859/// constant-initializer:
1860/// '=' constant-expression
1861///
Douglas Gregor3447e762009-08-20 22:52:58 +00001862void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00001863 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00001864 const ParsedTemplateInfo &TemplateInfo,
1865 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00001866 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001867 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00001868 Diag(Tok, diag::err_at_defs_cxx);
1869 else
1870 Diag(Tok, diag::err_at_in_class);
1871
1872 ConsumeToken();
1873 SkipUntil(tok::r_brace);
1874 return;
1875 }
1876
John McCalla0097262009-12-11 02:10:03 +00001877 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00001878 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00001879 if (!TemplateInfo.Kind &&
Richard Smith45855df2012-05-09 08:23:23 +00001880 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1881 if (TryAnnotateCXXScopeToken())
1882 MalformedTypeSpec = true;
1883
1884 bool isAccessDecl;
1885 if (Tok.isNot(tok::annot_cxxscope))
1886 isAccessDecl = false;
1887 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00001888 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1889 else
1890 isAccessDecl = NextToken().is(tok::kw_operator);
1891
1892 if (isAccessDecl) {
1893 // Collect the scope specifier token we annotated earlier.
1894 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00001895 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1896 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00001897
1898 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001899 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00001900 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001901 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1902 TemplateKWLoc, Name)) {
John McCalla0097262009-12-11 02:10:03 +00001903 SkipUntil(tok::semi);
1904 return;
1905 }
1906
1907 // TODO: recover from mistakenly-qualified operator declarations.
1908 if (ExpectAndConsume(tok::semi,
1909 diag::err_expected_semi_after,
1910 "access declaration",
1911 tok::semi))
1912 return;
1913
Douglas Gregor0be31a22010-07-02 17:43:08 +00001914 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCalla0097262009-12-11 02:10:03 +00001915 false, SourceLocation(),
1916 SS, Name,
1917 /* AttrList */ 0,
1918 /* IsTypeName */ false,
1919 SourceLocation());
1920 return;
1921 }
1922 }
1923
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001924 // static_assert-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001925 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001926 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00001927 SourceLocation DeclEnd;
1928 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001929 return;
1930 }
Mike Stump11289f42009-09-09 15:08:12 +00001931
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001932 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00001933 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00001934 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00001935 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00001936 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00001937 AS, AccessAttrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001938 return;
1939 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001940
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001941 // Handle: member-declaration ::= '__extension__' member-declaration
1942 if (Tok.is(tok::kw___extension__)) {
1943 // __extension__ silences extension warnings in the subexpression.
1944 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1945 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00001946 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1947 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001948 }
Douglas Gregorfec52632009-06-20 00:51:54 +00001949
Chris Lattnercf251412010-02-02 01:23:29 +00001950 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1951 // is a bitfield.
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001952 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001953
John McCall084e83d2011-03-24 11:26:52 +00001954 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00001955 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001956 // Optional C++11 attribute-specifier
1957 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00001958 // We need to keep these attributes for future diagnostic
1959 // before they are taken over by declaration specifier.
1960 FnAttrs.addAll(attrs.getList());
1961 FnAttrs.Range = attrs.Range;
1962
John McCall53fa7142010-12-24 02:08:15 +00001963 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001964
Douglas Gregorfec52632009-06-20 00:51:54 +00001965 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00001966 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001967
Douglas Gregorfec52632009-06-20 00:51:54 +00001968 // Eat 'using'.
1969 SourceLocation UsingLoc = ConsumeToken();
1970
1971 if (Tok.is(tok::kw_namespace)) {
1972 Diag(UsingLoc, diag::err_using_namespace_in_class);
1973 SkipUntil(tok::semi, true, true);
Chris Lattner916dbf12010-02-02 00:43:15 +00001974 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00001975 SourceLocation DeclEnd;
Richard Smith3f1b5d02011-05-05 21:57:07 +00001976 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +00001977 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1978 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00001979 }
1980 return;
1981 }
1982
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001983 // Hold late-parsed attributes so we can attach a Decl to them later.
1984 LateParsedAttrList CommonLateParsedAttrs;
1985
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001986 // decl-specifier-seq:
1987 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00001988 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00001989 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00001990 if (MalformedTypeSpec)
1991 DS.SetTypeSpecError();
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001992 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
1993 &CommonLateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001994
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001995 MultiTemplateParamsArg TemplateParams(
John McCall11083da2009-09-16 22:47:08 +00001996 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1997 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1998
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001999 if (Tok.is(tok::semi)) {
2000 ConsumeToken();
Michael Handdc016d2012-11-28 23:17:40 +00002001
2002 if (DS.isFriendSpecified())
2003 ProhibitAttributes(FnAttrs);
2004
John McCall48871652010-08-21 09:40:31 +00002005 Decl *TheDecl =
Chandler Carruth7c9856d2011-05-03 18:35:10 +00002006 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCall796c2a52010-07-16 08:13:16 +00002007 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00002008 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002009 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002010
John McCall28a6aea2009-11-04 02:18:39 +00002011 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002012 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002013
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002014 // Hold late-parsed attributes so we can attach a Decl to them later.
2015 LateParsedAttrList LateParsedAttrs;
2016
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002017 SourceLocation EqualLoc;
2018 bool HasInitializer = false;
2019 ExprResult Init;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002020 if (Tok.isNot(tok::colon)) {
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002021 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2022 ColonProtectionRAIIObject X(*this);
2023
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002024 // Parse the first declarator.
2025 ParseDeclarator(DeclaratorInfo);
Richard Smith2331bbf2012-05-02 22:22:32 +00002026 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +00002027 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002028 // If so, skip until the semi-colon or a }.
Sebastian Redl83f3b852011-04-24 16:27:48 +00002029 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002030 if (Tok.is(tok::semi))
2031 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002032 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002033 }
2034
Richard Smith89645bc2013-01-02 12:01:23 +00002035 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber24b2a822011-01-28 06:07:34 +00002036
John Thompson5bc5cbe2009-11-25 22:58:06 +00002037 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002038 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson5bc5cbe2009-11-25 22:58:06 +00002039
Francois Pichet3abc9b82011-05-11 02:14:46 +00002040 // MSVC permits pure specifier on inline functions declared at class scope.
2041 // Hence check for =0 before checking for function definition.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002042 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet3abc9b82011-05-11 02:14:46 +00002043 DeclaratorInfo.isFunctionDeclarator() &&
2044 NextToken().is(tok::numeric_constant)) {
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002045 EqualLoc = ConsumeToken();
Francois Pichet3abc9b82011-05-11 02:14:46 +00002046 Init = ParseInitializer();
2047 if (Init.isInvalid())
2048 SkipUntil(tok::comma, true, true);
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002049 else
2050 HasInitializer = true;
Francois Pichet3abc9b82011-05-11 02:14:46 +00002051 }
2052
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002053 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002054 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002055 //
2056 // In C++11, a non-function declarator followed by an open brace is a
2057 // braced-init-list for an in-class member initialization, not an
2058 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002059 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002060 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002061 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002062 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002063 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002064 } else if (Tok.is(tok::equal)) {
2065 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002066 if (KW.is(tok::kw_default))
2067 DefinitionKind = FDK_Defaulted;
2068 else if (KW.is(tok::kw_delete))
2069 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002070 }
2071 }
2072
Michael Handdc016d2012-11-28 23:17:40 +00002073 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2074 // to a friend declaration, that declaration shall be a definition.
2075 if (DeclaratorInfo.isFunctionDeclarator() &&
2076 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2077 // Diagnose attributes that appear before decl specifier:
2078 // [[]] friend int foo();
2079 ProhibitAttributes(FnAttrs);
2080 }
2081
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002082 if (DefinitionKind) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002083 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002084 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002085 ConsumeBrace();
Richard Trieu0d730542012-01-21 02:59:18 +00002086 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Michael Handdc016d2012-11-28 23:17:40 +00002087
Douglas Gregor8a4db832011-01-19 16:41:58 +00002088 // Consume the optional ';'
2089 if (Tok.is(tok::semi))
2090 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002091 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002092 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002093
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002094 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002095 Diag(DeclaratorInfo.getIdentifierLoc(),
2096 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002097
Richard Smith2603b092012-11-15 22:54:20 +00002098 // Recover by treating the 'typedef' as spurious.
2099 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002100 }
2101
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002102 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002103 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002104 VS, DefinitionKind, Init);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002105
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002106 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2107 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2108 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002109 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002110 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002111 }
2112 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002113
2114 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002115 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002116 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002117
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002118 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002119 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002120 }
2121
2122 // member-declarator-list:
2123 // member-declarator
2124 // member-declarator-list ',' member-declarator
2125
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002126 SmallVector<Decl *, 8> DeclsInGroup;
John McCalldadc5752010-08-24 06:29:42 +00002127 ExprResult BitfieldSize;
Richard Smithc8a79032012-01-09 22:31:44 +00002128 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002129
2130 while (1) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002131 // member-declarator:
2132 // declarator pure-specifier[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00002133 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002134 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002135 if (Tok.is(tok::colon)) {
2136 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002137 BitfieldSize = ParseConstantExpression();
2138 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002139 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002140 }
Mike Stump11289f42009-09-09 15:08:12 +00002141
Chris Lattnerf3d3b362010-06-13 05:34:18 +00002142 // If a simple-asm-expr is present, parse it.
2143 if (Tok.is(tok::kw_asm)) {
2144 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00002145 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnerf3d3b362010-06-13 05:34:18 +00002146 if (AsmLabel.isInvalid())
2147 SkipUntil(tok::comma, true, true);
2148
2149 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2150 DeclaratorInfo.SetRangeEnd(Loc);
2151 }
2152
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002153 // If attributes exist after the declarator, parse them.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002154 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002155
Richard Smith938f40b2011-06-11 17:19:42 +00002156 // FIXME: When g++ adds support for this, we'll need to check whether it
2157 // goes before or after the GNU attributes and __asm__.
Richard Smith89645bc2013-01-02 12:01:23 +00002158 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith938f40b2011-06-11 17:19:42 +00002159
Richard Smith2b013182012-06-10 03:12:00 +00002160 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002161 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith938f40b2011-06-11 17:19:42 +00002162 if (BitfieldSize.get()) {
2163 Diag(Tok, diag::err_bitfield_member_init);
2164 SkipUntil(tok::comma, true, true);
2165 } else {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002166 HasInitializer = true;
Richard Smith2b013182012-06-10 03:12:00 +00002167 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2168 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2169 != DeclSpec::SCS_static &&
2170 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2171 != DeclSpec::SCS_typedef)
2172 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002173 }
2174 }
2175
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002176 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002177 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002178 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002179
Rafael Espindola0a67e2f2013-01-08 20:44:06 +00002180 NamedDecl *ThisDecl = 0;
John McCall07e91c02009-08-06 02:15:43 +00002181 if (DS.isFriendSpecified()) {
Michael Handdc016d2012-11-28 23:17:40 +00002182 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2183 // to a friend declaration, that declaration shall be a definition.
2184 //
2185 // Diagnose attributes appear after friend member function declarator:
2186 // foo [[]] ();
2187 SmallVector<SourceRange, 4> Ranges;
2188 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2189 if (!Ranges.empty()) {
2190 for (SmallVector<SourceRange, 4>::iterator I = Ranges.begin(),
2191 E = Ranges.end(); I != E; ++I) {
2192 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2193 << *I;
2194 }
2195 }
2196
John McCall2f212b32009-09-11 21:02:39 +00002197 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor0be31a22010-07-02 17:43:08 +00002198 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002199 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002200 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002201 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002202 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002203 TemplateParams,
John McCall07e91c02009-08-06 02:15:43 +00002204 BitfieldSize.release(),
Richard Smith2b013182012-06-10 03:12:00 +00002205 VS, HasInClassInit);
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002206 if (AccessAttrs)
2207 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2208 false, true);
Douglas Gregor3447e762009-08-20 22:52:58 +00002209 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002210
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002211 // Set the Decl for any late parsed attributes
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002212 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2213 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2214 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002215 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002216 LateParsedAttrs[i]->addDecl(ThisDecl);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002217 }
2218 LateParsedAttrs.clear();
2219
Douglas Gregor728d00b2011-10-10 14:49:18 +00002220 // Handle the initializer.
Richard Smith2b013182012-06-10 03:12:00 +00002221 if (HasInClassInit != ICIS_NoInit) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002222 // The initializer was deferred; parse it and cache the tokens.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002223 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00002224 diag::warn_cxx98_compat_nonstatic_member_init :
2225 diag::ext_nonstatic_member_init);
2226
Richard Smith938f40b2011-06-11 17:19:42 +00002227 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002228 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2229 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002230 //
2231 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002232 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002233 Diag(Tok, diag::err_incomplete_array_member_init);
2234 SkipUntil(tok::comma, true, true);
David Blaikiecdd91db2012-02-14 09:00:46 +00002235 if (ThisDecl)
2236 // Avoid later warnings about a class member of incomplete type.
2237 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002238 } else
2239 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002240 } else if (HasInitializer) {
2241 // Normal initializer.
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002242 if (!Init.isUsable())
Douglas Gregor926410d2012-02-21 02:22:07 +00002243 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002244 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2245
Douglas Gregor728d00b2011-10-10 14:49:18 +00002246 if (Init.isInvalid())
2247 SkipUntil(tok::comma, true, true);
2248 else if (ThisDecl)
Sebastian Redleef474c2012-02-22 10:50:08 +00002249 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002250 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002251 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2252 // No initializer.
2253 Actions.ActOnUninitializedDecl(ThisDecl,
2254 DS.getTypeSpecType() == DeclSpec::TST_auto);
Richard Smith938f40b2011-06-11 17:19:42 +00002255 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002256
2257 if (ThisDecl) {
2258 Actions.FinalizeDeclaration(ThisDecl);
2259 DeclsInGroup.push_back(ThisDecl);
2260 }
2261
Richard Smith4f402bd2012-04-29 07:31:09 +00002262 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor728d00b2011-10-10 14:49:18 +00002263 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2264 != DeclSpec::SCS_typedef) {
Douglas Gregor433e0532012-04-16 18:27:27 +00002265 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002266 }
2267
2268 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002269
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002270 // If we don't have a comma, it is either the end of the list (a ';')
2271 // or an error, bail out.
2272 if (Tok.isNot(tok::comma))
2273 break;
Mike Stump11289f42009-09-09 15:08:12 +00002274
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002275 // Consume the comma.
Richard Smithc8a79032012-01-09 22:31:44 +00002276 SourceLocation CommaLoc = ConsumeToken();
2277
2278 if (Tok.isAtStartOfLine() &&
2279 !MightBeDeclarator(Declarator::MemberContext)) {
2280 // This comma was followed by a line-break and something which can't be
2281 // the start of a declarator. The comma was probably a typo for a
2282 // semicolon.
2283 Diag(CommaLoc, diag::err_expected_semi_declaration)
2284 << FixItHint::CreateReplacement(CommaLoc, ";");
2285 ExpectSemi = false;
2286 break;
2287 }
Mike Stump11289f42009-09-09 15:08:12 +00002288
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002289 // Parse the next declarator.
2290 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002291 VS.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002292 BitfieldSize = true;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002293 Init = true;
2294 HasInitializer = false;
Richard Smith8d06f422012-01-12 23:53:29 +00002295 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002296
Bill Wendling44426052012-12-20 19:22:21 +00002297 // Attributes are only allowed on the second declarator.
John McCall53fa7142010-12-24 02:08:15 +00002298 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002299
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002300 if (Tok.isNot(tok::colon))
2301 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002302 }
2303
Richard Smithc8a79032012-01-09 22:31:44 +00002304 if (ExpectSemi &&
2305 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002306 // Skip to end of block or statement.
2307 SkipUntil(tok::r_brace, true, true);
2308 // If we stopped at a ';', eat it.
2309 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002310 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002311 }
2312
Douglas Gregor0be31a22010-07-02 17:43:08 +00002313 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattner916dbf12010-02-02 00:43:15 +00002314 DeclsInGroup.size());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002315}
2316
Richard Smith938f40b2011-06-11 17:19:42 +00002317/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2318/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2319/// function definition. The location of the '=', if any, will be placed in
2320/// EqualLoc.
2321///
2322/// pure-specifier:
2323/// '= 0'
Sebastian Redleef474c2012-02-22 10:50:08 +00002324///
Richard Smith938f40b2011-06-11 17:19:42 +00002325/// brace-or-equal-initializer:
2326/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002327/// braced-init-list
2328///
Richard Smith938f40b2011-06-11 17:19:42 +00002329/// initializer-clause:
2330/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002331/// braced-init-list
2332///
Richard Smith938f40b2011-06-11 17:19:42 +00002333/// defaulted/deleted function-definition:
2334/// '=' 'default'
2335/// '=' 'delete'
2336///
2337/// Prior to C++0x, the assignment-expression in an initializer-clause must
2338/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002339ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002340 SourceLocation &EqualLoc) {
2341 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2342 && "Data member initializer not starting with '=' or '{'");
2343
Douglas Gregor926410d2012-02-21 02:22:07 +00002344 EnterExpressionEvaluationContext Context(Actions,
2345 Sema::PotentiallyEvaluated,
2346 D);
Richard Smith938f40b2011-06-11 17:19:42 +00002347 if (Tok.is(tok::equal)) {
2348 EqualLoc = ConsumeToken();
2349 if (Tok.is(tok::kw_delete)) {
2350 // In principle, an initializer of '= delete p;' is legal, but it will
2351 // never type-check. It's better to diagnose it as an ill-formed expression
2352 // than as an ill-formed deleted non-function member.
2353 // An initializer of '= delete p, foo' will never be parsed, because
2354 // a top-level comma always ends the initializer expression.
2355 const Token &Next = NextToken();
2356 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2357 Next.is(tok::eof)) {
2358 if (IsFunction)
2359 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2360 << 1 /* delete */;
2361 else
2362 Diag(ConsumeToken(), diag::err_deleted_non_function);
2363 return ExprResult();
2364 }
2365 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002366 if (IsFunction)
2367 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2368 << 0 /* default */;
2369 else
2370 Diag(ConsumeToken(), diag::err_default_special_members);
2371 return ExprResult();
2372 }
2373
Sebastian Redleef474c2012-02-22 10:50:08 +00002374 }
2375 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002376}
2377
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002378/// ParseCXXMemberSpecification - Parse the class definition.
2379///
2380/// member-specification:
2381/// member-declaration member-specification[opt]
2382/// access-specifier ':' member-specification[opt]
2383///
Joao Matose9a3ed42012-08-31 22:18:20 +00002384void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00002385 SourceLocation AttrFixitLoc,
2386 ParsedAttributes &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00002387 unsigned TagType, Decl *TagDecl) {
2388 assert((TagType == DeclSpec::TST_struct ||
2389 TagType == DeclSpec::TST_interface ||
2390 TagType == DeclSpec::TST_union ||
2391 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2392
John McCallfaf5fb42010-08-26 23:41:50 +00002393 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2394 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00002395
Douglas Gregoredf8f392010-01-16 20:52:59 +00002396 // Determine whether this is a non-nested class. Note that local
2397 // classes are *not* considered to be nested classes.
2398 bool NonNestedClass = true;
2399 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002400 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002401 if (S->isClassScope()) {
2402 // We're inside a class scope, so this is a nested class.
2403 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00002404
2405 // The Microsoft extension __interface does not permit nested classes.
2406 if (getCurrentClass().IsInterface) {
2407 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2408 << /*ErrorType=*/6
2409 << (isa<NamedDecl>(TagDecl)
2410 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2411 : "<anonymous>");
2412 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00002413 break;
2414 }
2415
2416 if ((S->getFlags() & Scope::FnScope)) {
2417 // If we're in a function or function template declared in the
2418 // body of a class, then this is a local class rather than a
2419 // nested class.
2420 const Scope *Parent = S->getParent();
2421 if (Parent->isTemplateParamScope())
2422 Parent = Parent->getParent();
2423 if (Parent->isClassScope())
2424 break;
2425 }
2426 }
2427 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002428
2429 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00002430 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002431
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002432 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00002433 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2434 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002435
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002436 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002437 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002438
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002439 SourceLocation FinalLoc;
2440
2441 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002442 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith89645bc2013-01-02 12:01:23 +00002443 assert(isCXX11FinalKeyword() && "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00002444 FinalLoc = ConsumeToken();
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002445
John McCalldb632ac2012-09-25 07:32:39 +00002446 if (TagType == DeclSpec::TST_interface) {
2447 Diag(FinalLoc, diag::err_override_control_interface)
2448 << "final";
2449 } else {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002450 Diag(FinalLoc, getLangOpts().CPlusPlus11 ?
John McCalldb632ac2012-09-25 07:32:39 +00002451 diag::warn_cxx98_compat_override_control_keyword :
2452 diag::ext_override_control_keyword) << "final";
2453 }
Michael Han9407e502012-11-26 22:54:45 +00002454
Michael Han309af292013-01-07 16:57:11 +00002455 // Parse any C++11 attributes after 'final' keyword.
2456 // These attributes are not allowed to appear here,
2457 // and the only possible place for them to appertain
2458 // to the class would be between class-key and class-name.
2459 ParsedAttributesWithRange Attributes(AttrFactory);
2460 MaybeParseCXX11Attributes(Attributes);
2461 SourceRange AttrRange = Attributes.Range;
2462 if (AttrRange.isValid()) {
2463 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
2464 << AttrRange
2465 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
2466 CharSourceRange(AttrRange, true))
2467 << FixItHint::CreateRemoval(AttrRange);
2468
2469 // Recover by adding attributes to the attribute list of the class
2470 // so they can be applied on the class later.
2471 Attrs.takeAllFrom(Attributes);
2472 }
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002473 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002474
John McCall2d814c32009-12-19 21:48:58 +00002475 if (Tok.is(tok::colon)) {
2476 ParseBaseClause(TagDecl);
2477
2478 if (!Tok.is(tok::l_brace)) {
2479 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCall2ff380a2010-03-17 00:38:33 +00002480
2481 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002482 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002483 return;
2484 }
2485 }
2486
2487 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002488 BalancedDelimiterTracker T(*this, tok::l_brace);
2489 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00002490
John McCall08bede42010-05-28 08:11:17 +00002491 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00002492 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002493 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00002494
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002495 // C++ 11p3: Members of a class defined with the keyword class are private
2496 // by default. Members of a class defined with the keywords struct or union
2497 // are public by default.
2498 AccessSpecifier CurAS;
2499 if (TagType == DeclSpec::TST_class)
2500 CurAS = AS_private;
2501 else
2502 CurAS = AS_public;
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002503 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002504
Douglas Gregor9377c822010-06-21 22:31:09 +00002505 if (TagDecl) {
2506 // While we still have something to read, read the member-declarations.
2507 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2508 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002509
David Blaikiebbafb8a2012-03-11 07:00:24 +00002510 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet8f981d52011-05-25 10:19:49 +00002511 Tok.is(tok::kw___if_not_exists))) {
2512 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2513 continue;
2514 }
2515
Douglas Gregor9377c822010-06-21 22:31:09 +00002516 // Check for extraneous top-level semicolon.
2517 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002518 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor9377c822010-06-21 22:31:09 +00002519 continue;
2520 }
2521
Eli Friedmanec52f922012-02-23 23:47:16 +00002522 if (Tok.is(tok::annot_pragma_vis)) {
2523 HandlePragmaVisibility();
2524 continue;
2525 }
2526
2527 if (Tok.is(tok::annot_pragma_pack)) {
2528 HandlePragmaPack();
2529 continue;
2530 }
2531
Argyrios Kyrtzidis5c2021b2012-10-12 17:39:59 +00002532 if (Tok.is(tok::annot_pragma_align)) {
2533 HandlePragmaAlign();
2534 continue;
2535 }
2536
Douglas Gregor9377c822010-06-21 22:31:09 +00002537 AccessSpecifier AS = getAccessSpecifierIfPresent();
2538 if (AS != AS_none) {
2539 // Current token is a C++ access specifier.
2540 CurAS = AS;
2541 SourceLocation ASLoc = Tok.getLocation();
David Blaikieeba32c22011-10-13 06:08:43 +00002542 unsigned TokLength = Tok.getLength();
Douglas Gregor9377c822010-06-21 22:31:09 +00002543 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002544 AccessAttrs.clear();
2545 MaybeParseGNUAttributes(AccessAttrs);
2546
David Blaikieeba32c22011-10-13 06:08:43 +00002547 SourceLocation EndLoc;
2548 if (Tok.is(tok::colon)) {
2549 EndLoc = Tok.getLocation();
2550 ConsumeToken();
2551 } else if (Tok.is(tok::semi)) {
2552 EndLoc = Tok.getLocation();
2553 ConsumeToken();
2554 Diag(EndLoc, diag::err_expected_colon)
2555 << FixItHint::CreateReplacement(EndLoc, ":");
2556 } else {
2557 EndLoc = ASLoc.getLocWithOffset(TokLength);
2558 Diag(EndLoc, diag::err_expected_colon)
2559 << FixItHint::CreateInsertion(EndLoc, ":");
2560 }
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002561
John McCalldb632ac2012-09-25 07:32:39 +00002562 // The Microsoft extension __interface does not permit non-public
2563 // access specifiers.
2564 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2565 Diag(ASLoc, diag::err_access_specifier_interface)
2566 << (CurAS == AS_protected);
2567 }
2568
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002569 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2570 AccessAttrs.getList())) {
2571 // found another attribute than only annotations
2572 AccessAttrs.clear();
2573 }
2574
Douglas Gregor9377c822010-06-21 22:31:09 +00002575 continue;
2576 }
2577
2578 // FIXME: Make sure we don't have a template here.
2579
2580 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002581 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002582 }
2583
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002584 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00002585 } else {
2586 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002587 }
Mike Stump11289f42009-09-09 15:08:12 +00002588
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002589 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002590 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002591 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002592
John McCall08bede42010-05-28 08:11:17 +00002593 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002594 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002595 T.getOpenLocation(),
2596 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002597 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002598
Douglas Gregor433e0532012-04-16 18:27:27 +00002599 // C++11 [class.mem]p2:
2600 // Within the class member-specification, the class is regarded as complete
Richard Smith2331bbf2012-05-02 22:22:32 +00002601 // within function bodies, default arguments, and
Douglas Gregor433e0532012-04-16 18:27:27 +00002602 // brace-or-equal-initializers for non-static data members (including such
2603 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00002604 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002605 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00002606 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002607 // declarations and the lexed inline method definitions, along with any
2608 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00002609 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002610 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002611 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00002612
2613 // We've finished with all pending member declarations.
2614 Actions.ActOnFinishCXXMemberDecls();
2615
Richard Smith938f40b2011-06-11 17:19:42 +00002616 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002617 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00002618 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002619 }
2620
John McCall08bede42010-05-28 08:11:17 +00002621 if (TagDecl)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002622 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2623 T.getCloseLocation());
John McCall2ff380a2010-03-17 00:38:33 +00002624
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002625 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002626 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002627 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002628}
Douglas Gregore8381c02008-11-05 04:29:56 +00002629
2630/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2631/// which explicitly initializes the members or base classes of a
2632/// class (C++ [class.base.init]). For example, the three initializers
2633/// after the ':' in the Derived constructor below:
2634///
2635/// @code
2636/// class Base { };
2637/// class Derived : Base {
2638/// int x;
2639/// float f;
2640/// public:
2641/// Derived(float f) : Base(), x(17), f(f) { }
2642/// };
2643/// @endcode
2644///
Mike Stump11289f42009-09-09 15:08:12 +00002645/// [C++] ctor-initializer:
2646/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00002647///
Mike Stump11289f42009-09-09 15:08:12 +00002648/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00002649/// mem-initializer ...[opt]
2650/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00002651void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00002652 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2653
John Wiegley1c0675e2011-04-28 01:08:34 +00002654 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2655 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00002656 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002657
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002658 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002659 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002660
Douglas Gregore8381c02008-11-05 04:29:56 +00002661 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002662 if (Tok.is(tok::code_completion)) {
2663 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2664 MemInitializers.data(),
2665 MemInitializers.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002666 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002667 } else {
2668 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2669 if (!MemInit.isInvalid())
2670 MemInitializers.push_back(MemInit.get());
2671 else
2672 AnyErrors = true;
2673 }
2674
Douglas Gregore8381c02008-11-05 04:29:56 +00002675 if (Tok.is(tok::comma))
2676 ConsumeToken();
2677 else if (Tok.is(tok::l_brace))
2678 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00002679 // If the next token looks like a base or member initializer, assume that
2680 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00002681 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2682 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2683 Diag(Loc, diag::err_ctor_init_missing_comma)
2684 << FixItHint::CreateInsertion(Loc, ", ");
2685 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00002686 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redla7b98a72009-04-26 20:35:05 +00002687 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregore8381c02008-11-05 04:29:56 +00002688 SkipUntil(tok::l_brace, true, true);
2689 break;
2690 }
2691 } while (true);
2692
David Blaikie3fc2f912013-01-17 05:26:25 +00002693 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002694 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00002695}
2696
2697/// ParseMemInitializer - Parse a C++ member initializer, which is
2698/// part of a constructor initializer that explicitly initializes one
2699/// member or base class (C++ [class.base.init]). See
2700/// ParseConstructorInitializer for an example.
2701///
2702/// [C++] mem-initializer:
2703/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002704/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00002705///
Douglas Gregore8381c02008-11-05 04:29:56 +00002706/// [C++] mem-initializer-id:
2707/// '::'[opt] nested-name-specifier[opt] class-name
2708/// identifier
John McCall48871652010-08-21 09:40:31 +00002709Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00002710 // parse '::'[opt] nested-name-specifier[opt]
2711 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002712 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00002713 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002714 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002715 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00002716 if (TemplateId->Kind == TNK_Type_template ||
2717 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00002718 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002719 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00002720 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002721 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002722 }
David Blaikie186a8892012-01-24 06:03:59 +00002723 // Uses of decltype will already have been converted to annot_decltype by
2724 // ParseOptionalCXXScopeSpecifier at this point.
2725 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2726 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002727 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00002728 return true;
2729 }
Mike Stump11289f42009-09-09 15:08:12 +00002730
David Blaikie186a8892012-01-24 06:03:59 +00002731 IdentifierInfo *II = 0;
2732 DeclSpec DS(AttrFactory);
2733 SourceLocation IdLoc = Tok.getLocation();
2734 if (Tok.is(tok::annot_decltype)) {
2735 // Get the decltype expression, if there is one.
2736 ParseDecltypeSpecifier(DS);
2737 } else {
2738 if (Tok.is(tok::identifier))
2739 // Get the identifier. This may be a member name or a class name,
2740 // but we'll let the semantic analysis determine which it is.
2741 II = Tok.getIdentifierInfo();
2742 ConsumeToken();
2743 }
2744
Douglas Gregore8381c02008-11-05 04:29:56 +00002745
2746 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002747 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002748 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2749
Sebastian Redla74948d2011-09-24 17:48:25 +00002750 ExprResult InitList = ParseBraceInitializer();
2751 if (InitList.isInvalid())
2752 return true;
2753
2754 SourceLocation EllipsisLoc;
2755 if (Tok.is(tok::ellipsis))
2756 EllipsisLoc = ConsumeToken();
2757
2758 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002759 TemplateTypeTy, DS, IdLoc,
2760 InitList.take(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00002761 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002762 BalancedDelimiterTracker T(*this, tok::l_paren);
2763 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00002764
Sebastian Redl3da34892011-06-05 12:23:16 +00002765 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00002766 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00002767 CommaLocsTy CommaLocs;
2768 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2769 SkipUntil(tok::r_paren);
2770 return true;
2771 }
2772
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002773 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00002774
2775 SourceLocation EllipsisLoc;
2776 if (Tok.is(tok::ellipsis))
2777 EllipsisLoc = ConsumeToken();
2778
2779 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002780 TemplateTypeTy, DS, IdLoc,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002781 T.getOpenLocation(), ArgExprs.data(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002782 ArgExprs.size(), T.getCloseLocation(),
Sebastian Redl3da34892011-06-05 12:23:16 +00002783 EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00002784 }
2785
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002786 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace
Sebastian Redl3da34892011-06-05 12:23:16 +00002787 : diag::err_expected_lparen);
2788 return true;
Douglas Gregore8381c02008-11-05 04:29:56 +00002789}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002790
Sebastian Redl965b0e32011-03-05 14:45:16 +00002791/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002792///
Douglas Gregor356513d2008-12-01 18:00:20 +00002793/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00002794/// dynamic-exception-specification
2795/// noexcept-specification
2796///
2797/// noexcept-specification:
2798/// 'noexcept'
2799/// 'noexcept' '(' constant-expression ')'
2800ExceptionSpecificationType
Richard Smith2331bbf2012-05-02 22:22:32 +00002801Parser::tryParseExceptionSpecification(
Douglas Gregor433e0532012-04-16 18:27:27 +00002802 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002803 SmallVectorImpl<ParsedType> &DynamicExceptions,
2804 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00002805 ExprResult &NoexceptExpr) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002806 ExceptionSpecificationType Result = EST_None;
2807
2808 // See if there's a dynamic specification.
2809 if (Tok.is(tok::kw_throw)) {
2810 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2811 DynamicExceptions,
2812 DynamicExceptionRanges);
2813 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2814 "Produced different number of exception types and ranges.");
2815 }
2816
2817 // If there's no noexcept specification, we're done.
2818 if (Tok.isNot(tok::kw_noexcept))
2819 return Result;
2820
Richard Smithb15c11c2011-10-17 23:06:20 +00002821 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2822
Sebastian Redl965b0e32011-03-05 14:45:16 +00002823 // If we already had a dynamic specification, parse the noexcept for,
2824 // recovery, but emit a diagnostic and don't store the results.
2825 SourceRange NoexceptRange;
2826 ExceptionSpecificationType NoexceptType = EST_None;
2827
2828 SourceLocation KeywordLoc = ConsumeToken();
2829 if (Tok.is(tok::l_paren)) {
2830 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002831 BalancedDelimiterTracker T(*this, tok::l_paren);
2832 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00002833 NoexceptType = EST_ComputedNoexcept;
2834 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002835 // The argument must be contextually convertible to bool. We use
2836 // ActOnBooleanCondition for this purpose.
2837 if (!NoexceptExpr.isInvalid())
2838 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2839 NoexceptExpr.get());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002840 T.consumeClose();
2841 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl965b0e32011-03-05 14:45:16 +00002842 } else {
2843 // There is no argument.
2844 NoexceptType = EST_BasicNoexcept;
2845 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2846 }
2847
2848 if (Result == EST_None) {
2849 SpecificationRange = NoexceptRange;
2850 Result = NoexceptType;
2851
2852 // If there's a dynamic specification after a noexcept specification,
2853 // parse that and ignore the results.
2854 if (Tok.is(tok::kw_throw)) {
2855 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2856 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2857 DynamicExceptionRanges);
2858 }
2859 } else {
2860 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2861 }
2862
2863 return Result;
2864}
2865
2866/// ParseDynamicExceptionSpecification - Parse a C++
2867/// dynamic-exception-specification (C++ [except.spec]).
2868///
2869/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00002870/// 'throw' '(' type-id-list [opt] ')'
2871/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00002872///
Douglas Gregor356513d2008-12-01 18:00:20 +00002873/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00002874/// type-id ... [opt]
2875/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002876///
Sebastian Redl965b0e32011-03-05 14:45:16 +00002877ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2878 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002879 SmallVectorImpl<ParsedType> &Exceptions,
2880 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002881 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00002882
Sebastian Redl965b0e32011-03-05 14:45:16 +00002883 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002884 BalancedDelimiterTracker T(*this, tok::l_paren);
2885 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002886 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2887 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002888 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002889 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002890
Douglas Gregor356513d2008-12-01 18:00:20 +00002891 // Parse throw(...), a Microsoft extension that means "this function
2892 // can throw anything".
2893 if (Tok.is(tok::ellipsis)) {
2894 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00002895 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00002896 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002897 T.consumeClose();
2898 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002899 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00002900 }
2901
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002902 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00002903 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002904 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00002905 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00002906
Douglas Gregor830837d2010-12-20 23:57:46 +00002907 if (Tok.is(tok::ellipsis)) {
2908 // C++0x [temp.variadic]p5:
2909 // - In a dynamic-exception-specification (15.4); the pattern is a
2910 // type-id.
2911 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00002912 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00002913 if (!Res.isInvalid())
2914 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2915 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00002916
Sebastian Redld6434562009-05-29 18:02:33 +00002917 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002918 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00002919 Ranges.push_back(Range);
2920 }
Douglas Gregor830837d2010-12-20 23:57:46 +00002921
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002922 if (Tok.is(tok::comma))
2923 ConsumeToken();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002924 else
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002925 break;
2926 }
2927
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002928 T.consumeClose();
2929 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002930 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002931}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002932
Douglas Gregor7fb25412010-10-01 18:44:50 +00002933/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2934/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00002935TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00002936 assert(Tok.is(tok::arrow) && "expected arrow");
2937
2938 ConsumeToken();
2939
Richard Smithbfdb1082012-03-12 08:56:40 +00002940 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00002941}
2942
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002943/// \brief We have just started parsing the definition of a new class,
2944/// so push that class onto our stack of classes that is currently
2945/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00002946Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00002947Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
2948 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002949 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002950 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00002951 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00002952 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002953}
2954
2955/// \brief Deallocate the given parsed class and all of its nested
2956/// classes.
2957void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00002958 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2959 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002960 delete Class;
2961}
2962
2963/// \brief Pop the top class of the stack of classes that are
2964/// currently being parsed.
2965///
2966/// This routine should be called when we have finished parsing the
2967/// definition of a class, but have not yet popped the Scope
2968/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00002969void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002970 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00002971
John McCallc1465822011-02-14 07:13:47 +00002972 Actions.PopParsingClass(state);
2973
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002974 ParsingClass *Victim = ClassStack.top();
2975 ClassStack.pop();
2976 if (Victim->TopLevelClass) {
2977 // Deallocate all of the nested classes of this class,
2978 // recursively: we don't need to keep any of this information.
2979 DeallocateParsedClasses(Victim);
2980 return;
Mike Stump11289f42009-09-09 15:08:12 +00002981 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002982 assert(!ClassStack.empty() && "Missing top-level class?");
2983
Douglas Gregorefc46952010-10-12 16:25:54 +00002984 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002985 // The victim is a nested class, but we will not need to perform
2986 // any processing after the definition of this class since it has
2987 // no members whose handling was delayed. Therefore, we can just
2988 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00002989 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002990 return;
2991 }
2992
2993 // This nested class has some members that will need to be processed
2994 // after the top-level class is completely defined. Therefore, add
2995 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002996 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00002997 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00002998 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002999}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003000
Richard Smith3dff2512012-04-10 03:25:07 +00003001/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3002///
3003/// \return the parsed identifier on success, and 0 if the next token is not an
3004/// attribute-token.
3005///
3006/// C++11 [dcl.attr.grammar]p3:
3007/// If a keyword or an alternative token that satisfies the syntactic
3008/// requirements of an identifier is contained in an attribute-token,
3009/// it is considered an identifier.
3010IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3011 switch (Tok.getKind()) {
3012 default:
3013 // Identifiers and keywords have identifier info attached.
3014 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3015 Loc = ConsumeToken();
3016 return II;
3017 }
3018 return 0;
3019
3020 case tok::ampamp: // 'and'
3021 case tok::pipe: // 'bitor'
3022 case tok::pipepipe: // 'or'
3023 case tok::caret: // 'xor'
3024 case tok::tilde: // 'compl'
3025 case tok::amp: // 'bitand'
3026 case tok::ampequal: // 'and_eq'
3027 case tok::pipeequal: // 'or_eq'
3028 case tok::caretequal: // 'xor_eq'
3029 case tok::exclaim: // 'not'
3030 case tok::exclaimequal: // 'not_eq'
3031 // Alternative tokens do not have identifier info, but their spelling
3032 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003033 SmallString<8> SpellingBuf;
Richard Smith3dff2512012-04-10 03:25:07 +00003034 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
3035 if (std::isalpha(Spelling[0])) {
3036 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003037 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003038 }
3039 return 0;
3040 }
3041}
3042
Michael Han23214e52012-10-03 01:56:22 +00003043static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3044 IdentifierInfo *ScopeName) {
3045 switch (AttributeList::getKind(AttrName, ScopeName,
3046 AttributeList::AS_CXX11)) {
3047 case AttributeList::AT_CarriesDependency:
3048 case AttributeList::AT_FallThrough:
Richard Smith10876ef2013-01-17 01:30:42 +00003049 case AttributeList::AT_CXX11NoReturn: {
Michael Han23214e52012-10-03 01:56:22 +00003050 return true;
3051 }
3052
3053 default:
3054 return false;
3055 }
3056}
3057
Richard Smith3dff2512012-04-10 03:25:07 +00003058/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003059/// only parses standard attributes.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003060///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003061/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003062/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003063/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003064///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003065/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003066/// attribute[opt]
3067/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003068/// attribute '...'
3069/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003070///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003071/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003072/// attribute-token attribute-argument-clause[opt]
3073///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003074/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003075/// identifier
3076/// attribute-scoped-token
3077///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003078/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003079/// attribute-namespace '::' identifier
3080///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003081/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003082/// identifier
3083///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003084/// [C++11] attribute-argument-clause:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003085/// '(' balanced-token-seq ')'
3086///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003087/// [C++11] balanced-token-seq:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003088/// balanced-token
3089/// balanced-token-seq balanced-token
3090///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003091/// [C++11] balanced-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003092/// '(' balanced-token-seq ')'
3093/// '[' balanced-token-seq ']'
3094/// '{' balanced-token-seq '}'
3095/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smith3dff2512012-04-10 03:25:07 +00003096void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003097 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003098 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003099 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003100 ParseAlignmentSpecifier(attrs, endLoc);
3101 return;
3102 }
3103
Alexis Hunt96d5c762009-11-21 08:43:09 +00003104 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003105 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003106
Richard Smithf679b5b2011-10-14 20:48:27 +00003107 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3108
Alexis Hunt96d5c762009-11-21 08:43:09 +00003109 ConsumeBracket();
3110 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003111
Richard Smith10876ef2013-01-17 01:30:42 +00003112 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3113
Richard Smith3dff2512012-04-10 03:25:07 +00003114 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003115 // attribute not present
3116 if (Tok.is(tok::comma)) {
3117 ConsumeToken();
3118 continue;
3119 }
3120
Richard Smith3dff2512012-04-10 03:25:07 +00003121 SourceLocation ScopeLoc, AttrLoc;
3122 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3123
3124 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3125 if (!AttrName)
3126 // Break out to the "expected ']'" diagnostic.
3127 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003128
Alexis Hunt96d5c762009-11-21 08:43:09 +00003129 // scoped attribute
3130 if (Tok.is(tok::coloncolon)) {
3131 ConsumeToken();
3132
Richard Smith3dff2512012-04-10 03:25:07 +00003133 ScopeName = AttrName;
3134 ScopeLoc = AttrLoc;
3135
3136 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3137 if (!AttrName) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003138 Diag(Tok.getLocation(), diag::err_expected_ident);
3139 SkipUntil(tok::r_square, tok::comma, true, true);
3140 continue;
3141 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003142 }
3143
Michael Han23214e52012-10-03 01:56:22 +00003144 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003145 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003146
Richard Smith10876ef2013-01-17 01:30:42 +00003147 if (StandardAttr &&
3148 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3149 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3150 << AttrName << SourceRange(SeenAttrs[AttrName]);
3151
Michael Han23214e52012-10-03 01:56:22 +00003152 // Parse attribute arguments
3153 if (Tok.is(tok::l_paren)) {
3154 if (ScopeName && ScopeName->getName() == "gnu") {
3155 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3156 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3157 AttrParsed = true;
3158 } else {
3159 if (StandardAttr)
3160 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3161 << AttrName->getName();
3162
3163 // FIXME: handle other formats of c++11 attribute arguments
3164 ConsumeParen();
3165 SkipUntil(tok::r_paren, false);
3166 }
3167 }
3168
3169 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003170 attrs.addNew(AttrName,
3171 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3172 AttrLoc),
3173 ScopeName, ScopeLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00003174 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003175
Richard Smith3dff2512012-04-10 03:25:07 +00003176 if (Tok.is(tok::ellipsis)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003177 ConsumeToken();
Michael Han23214e52012-10-03 01:56:22 +00003178
3179 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3180 << AttrName->getName();
Richard Smith3dff2512012-04-10 03:25:07 +00003181 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003182 }
3183
3184 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3185 SkipUntil(tok::r_square, false);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003186 if (endLoc)
3187 *endLoc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00003188 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3189 SkipUntil(tok::r_square, false);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003190}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003191
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003192/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003193///
3194/// attribute-specifier-seq:
3195/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003196void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003197 SourceLocation *endLoc) {
3198 SourceLocation StartLoc = Tok.getLocation(), Loc;
3199 if (!endLoc)
3200 endLoc = &Loc;
3201
Douglas Gregor6f981002011-10-07 20:35:25 +00003202 do {
Richard Smith3dff2512012-04-10 03:25:07 +00003203 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003204 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003205
3206 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003207}
3208
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003209/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3210///
3211/// [MS] ms-attribute:
3212/// '[' token-seq ']'
3213///
3214/// [MS] ms-attribute-seq:
3215/// ms-attribute[opt]
3216/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00003217void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3218 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003219 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3220
3221 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003222 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003223 ConsumeBracket();
3224 SkipUntil(tok::r_square, true, true);
John McCall53fa7142010-12-24 02:08:15 +00003225 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003226 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3227 }
3228}
Francois Pichet8f981d52011-05-25 10:19:49 +00003229
3230void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3231 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00003232 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00003233 if (ParseMicrosoftIfExistsCondition(Result))
3234 return;
3235
Douglas Gregor43edb322011-10-24 22:31:10 +00003236 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3237 if (Braces.consumeOpen()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003238 Diag(Tok, diag::err_expected_lbrace);
3239 return;
3240 }
Francois Pichet8f981d52011-05-25 10:19:49 +00003241
Douglas Gregor43edb322011-10-24 22:31:10 +00003242 switch (Result.Behavior) {
3243 case IEB_Parse:
3244 // Parse the declarations below.
3245 break;
3246
3247 case IEB_Dependent:
3248 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3249 << Result.IsIfExists;
3250 // Fall through to skip.
3251
3252 case IEB_Skip:
3253 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00003254 return;
3255 }
3256
Douglas Gregor43edb322011-10-24 22:31:10 +00003257 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003258 // __if_exists, __if_not_exists can nest.
3259 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3260 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3261 continue;
3262 }
3263
3264 // Check for extraneous top-level semicolon.
3265 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003266 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00003267 continue;
3268 }
3269
3270 AccessSpecifier AS = getAccessSpecifierIfPresent();
3271 if (AS != AS_none) {
3272 // Current token is a C++ access specifier.
3273 CurAS = AS;
3274 SourceLocation ASLoc = Tok.getLocation();
3275 ConsumeToken();
3276 if (Tok.is(tok::colon))
3277 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3278 else
3279 Diag(Tok, diag::err_expected_colon);
3280 ConsumeToken();
3281 continue;
3282 }
3283
3284 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00003285 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet8f981d52011-05-25 10:19:49 +00003286 }
Douglas Gregor43edb322011-10-24 22:31:10 +00003287
3288 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00003289}