blob: 2361bc1774e85b65d00c3b826b8ba8b3a2fe4a3d [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"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000016#include "clang/AST/DeclTemplate.h"
Erik Verbruggen888d52a2014-01-15 09:15:43 +000017#include "clang/AST/ASTContext.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000018#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Basic/OperatorKinds.h"
Chris Lattner60f36222009-01-29 05:15:15 +000020#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/DeclSpec.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Sema/Scope.h"
John McCalldb632ac2012-09-25 07:32:39 +000025#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000026#include "llvm/ADT/SmallString.h"
Chris Lattnera5235172007-08-25 06:57:03 +000027using namespace clang;
28
29/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redl67667942010-08-27 23:12:46 +000030/// may either be a top level namespace or a block-level namespace alias. If
31/// there was an inline keyword, it has already been parsed.
Chris Lattnera5235172007-08-25 06:57:03 +000032///
33/// namespace-definition: [C++ 7.3: basic.namespace]
34/// named-namespace-definition
35/// unnamed-namespace-definition
36///
37/// unnamed-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000038/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000039///
40/// named-namespace-definition:
41/// original-namespace-definition
42/// extension-namespace-definition
43///
44/// original-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000045/// 'inline'[opt] 'namespace' identifier attributes[opt]
46/// '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000047///
48/// extension-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000049/// 'inline'[opt] 'namespace' original-namespace-name
50/// '{' namespace-body '}'
Mike Stump11289f42009-09-09 15:08:12 +000051///
Chris Lattnera5235172007-08-25 06:57:03 +000052/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
53/// 'namespace' identifier '=' qualified-namespace-specifier ';'
54///
John McCall48871652010-08-21 09:40:31 +000055Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redl67667942010-08-27 23:12:46 +000056 SourceLocation &DeclEnd,
57 SourceLocation InlineLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000058 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000059 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian4bf82622011-08-22 17:59:19 +000060 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000061
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000062 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000063 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000064 cutOffParsing();
65 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000066 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000067
Chris Lattnera5235172007-08-25 06:57:03 +000068 SourceLocation IdentLoc;
69 IdentifierInfo *Ident = 0;
Richard Trieu61384cb2011-05-26 20:11:09 +000070 std::vector<SourceLocation> ExtraIdentLoc;
71 std::vector<IdentifierInfo*> ExtraIdent;
72 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000073
74 Token attrTok;
Mike Stump11289f42009-09-09 15:08:12 +000075
Chris Lattner76c72282007-10-09 17:33:22 +000076 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000077 Ident = Tok.getIdentifierInfo();
78 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieu61384cb2011-05-26 20:11:09 +000079 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
80 ExtraNamespaceLoc.push_back(ConsumeToken());
81 ExtraIdent.push_back(Tok.getIdentifierInfo());
82 ExtraIdentLoc.push_back(ConsumeToken());
83 }
Chris Lattnera5235172007-08-25 06:57:03 +000084 }
Mike Stump11289f42009-09-09 15:08:12 +000085
Chris Lattnera5235172007-08-25 06:57:03 +000086 // Read label attributes, if present.
John McCall084e83d2011-03-24 11:26:52 +000087 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000088 if (Tok.is(tok::kw___attribute)) {
89 attrTok = Tok;
John McCall53fa7142010-12-24 02:08:15 +000090 ParseGNUAttributes(attrs);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000091 }
Mike Stump11289f42009-09-09 15:08:12 +000092
Douglas Gregor6b6bba42009-06-17 19:49:00 +000093 if (Tok.is(tok::equal)) {
Nico Weber729f1e22012-10-27 23:44:27 +000094 if (Ident == 0) {
Alp Tokerec543272013-12-24 09:48:30 +000095 Diag(Tok, diag::err_expected) << tok::identifier;
Nico Weber729f1e22012-10-27 23:44:27 +000096 // Skip to end of the definition and eat the ';'.
97 SkipUntil(tok::semi);
98 return 0;
99 }
John McCall53fa7142010-12-24 02:08:15 +0000100 if (!attrs.empty())
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000101 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redl67667942010-08-27 23:12:46 +0000102 if (InlineLoc.isValid())
103 Diag(InlineLoc, diag::err_inline_namespace_alias)
104 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000105 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000106 }
Mike Stump11289f42009-09-09 15:08:12 +0000107
Richard Trieu61384cb2011-05-26 20:11:09 +0000108
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000109 BalancedDelimiterTracker T(*this, tok::l_brace);
110 if (T.consumeOpen()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000111 if (!ExtraIdent.empty()) {
112 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
113 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
114 }
Alp Tokerec543272013-12-24 09:48:30 +0000115
116 if (Ident)
117 Diag(Tok, diag::err_expected) << tok::l_brace;
118 else
119 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
120
John McCall48871652010-08-21 09:40:31 +0000121 return 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000122 }
Mike Stump11289f42009-09-09 15:08:12 +0000123
Douglas Gregor0be31a22010-07-02 17:43:08 +0000124 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
125 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
126 getCurScope()->getFnParent()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000127 if (!ExtraIdent.empty()) {
128 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
129 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
130 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000131 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000132 SkipUntil(tok::r_brace);
John McCall48871652010-08-21 09:40:31 +0000133 return 0;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000134 }
135
Richard Trieu61384cb2011-05-26 20:11:09 +0000136 if (!ExtraIdent.empty()) {
137 TentativeParsingAction TPA(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000138 SkipUntil(tok::r_brace, StopBeforeMatch);
Richard Trieu61384cb2011-05-26 20:11:09 +0000139 Token rBraceToken = Tok;
140 TPA.Revert();
141
142 if (!rBraceToken.is(tok::r_brace)) {
143 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
144 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
145 } else {
Benjamin Kramerf546f412011-05-26 21:32:30 +0000146 std::string NamespaceFix;
Richard Trieu61384cb2011-05-26 20:11:09 +0000147 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
148 E = ExtraIdent.end(); I != E; ++I) {
149 NamespaceFix += " { namespace ";
150 NamespaceFix += (*I)->getName();
151 }
Benjamin Kramerf546f412011-05-26 21:32:30 +0000152
Richard Trieu61384cb2011-05-26 20:11:09 +0000153 std::string RBraces;
Benjamin Kramerf546f412011-05-26 21:32:30 +0000154 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieu61384cb2011-05-26 20:11:09 +0000155 RBraces += "} ";
Benjamin Kramerf546f412011-05-26 21:32:30 +0000156
Richard Trieu61384cb2011-05-26 20:11:09 +0000157 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
158 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
159 ExtraIdentLoc.back()),
160 NamespaceFix)
161 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
162 }
163 }
164
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000165 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith5d164bc2011-10-15 05:09:34 +0000166 if (InlineLoc.isValid())
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000167 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000168 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000169
Chris Lattner4de55aa2009-03-29 14:02:43 +0000170 // Enter a scope for the namespace.
171 ParseScope NamespaceScope(this, Scope::DeclScope);
172
John McCall48871652010-08-21 09:40:31 +0000173 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000174 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000175 IdentLoc, Ident, T.getOpenLocation(),
176 attrs.getList());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000177
John McCallfaf5fb42010-08-26 23:41:50 +0000178 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
179 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000180
Richard Trieu61384cb2011-05-26 20:11:09 +0000181 // Parse the contents of the namespace. This includes parsing recovery on
182 // any improperly nested namespaces.
183 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000184 InlineLoc, attrs, T);
Mike Stump11289f42009-09-09 15:08:12 +0000185
Chris Lattner4de55aa2009-03-29 14:02:43 +0000186 // Leave the namespace scope.
187 NamespaceScope.Exit();
188
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000189 DeclEnd = T.getCloseLocation();
190 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000191
192 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000193}
Chris Lattner38376f12008-01-12 07:05:38 +0000194
Richard Trieu61384cb2011-05-26 20:11:09 +0000195/// ParseInnerNamespace - Parse the contents of a namespace.
196void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
197 std::vector<IdentifierInfo*>& Ident,
198 std::vector<SourceLocation>& NamespaceLoc,
199 unsigned int index, SourceLocation& InlineLoc,
Richard Trieu61384cb2011-05-26 20:11:09 +0000200 ParsedAttributes& attrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000201 BalancedDelimiterTracker &Tracker) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000202 if (index == Ident.size()) {
Richard Smith34f30512013-11-23 04:06:09 +0000203 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000204 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000205 MaybeParseCXX11Attributes(attrs);
Richard Trieu61384cb2011-05-26 20:11:09 +0000206 MaybeParseMicrosoftAttributes(attrs);
207 ParseExternalDeclaration(attrs);
208 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000209
210 // The caller is what called check -- we are simply calling
211 // the close for it.
212 Tracker.consumeClose();
Richard Trieu61384cb2011-05-26 20:11:09 +0000213
214 return;
215 }
216
217 // Parse improperly nested namespaces.
218 ParseScope NamespaceScope(this, Scope::DeclScope);
219 Decl *NamespcDecl =
220 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
221 NamespaceLoc[index], IdentLoc[index],
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000222 Ident[index], Tracker.getOpenLocation(),
223 attrs.getList());
Richard Trieu61384cb2011-05-26 20:11:09 +0000224
225 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000226 attrs, Tracker);
Richard Trieu61384cb2011-05-26 20:11:09 +0000227
228 NamespaceScope.Exit();
229
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000230 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieu61384cb2011-05-26 20:11:09 +0000231}
232
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000233/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
234/// alias definition.
235///
John McCall48871652010-08-21 09:40:31 +0000236Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall084e83d2011-03-24 11:26:52 +0000237 SourceLocation AliasLoc,
238 IdentifierInfo *Alias,
239 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000240 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000241
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000242 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000243
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000244 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000245 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000246 cutOffParsing();
247 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000248 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000249
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000250 CXXScopeSpec SS;
251 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000252 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000253
254 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
255 Diag(Tok, diag::err_expected_namespace_name);
256 // Skip to end of the definition and eat the ';'.
257 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000258 return 0;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000259 }
260
261 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000262 IdentifierInfo *Ident = Tok.getIdentifierInfo();
263 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000264
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000265 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000266 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000267 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
268 SkipUntil(tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000269
Douglas Gregor0be31a22010-07-02 17:43:08 +0000270 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000271 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000272}
273
Chris Lattner38376f12008-01-12 07:05:38 +0000274/// ParseLinkage - We know that the current token is a string_literal
275/// and just before that, that extern was seen.
276///
277/// linkage-specification: [C++ 7.5p2: dcl.link]
278/// 'extern' string-literal '{' declaration-seq[opt] '}'
279/// 'extern' string-literal declaration
280///
Chris Lattner8ea64422010-11-09 20:15:55 +0000281Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregor15799fd2008-11-21 16:10:08 +0000282 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000283 SmallString<8> LangBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000284 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000285 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000286 if (Invalid)
John McCall48871652010-08-21 09:40:31 +0000287 return 0;
Chris Lattner38376f12008-01-12 07:05:38 +0000288
Richard Smithd67aea22012-03-06 03:21:47 +0000289 // FIXME: This is incorrect: linkage-specifiers are parsed in translation
290 // phase 7, so string-literal concatenation is supposed to occur.
291 // extern "" "C" "" "+" "+" { } is legal.
292 if (Tok.hasUDSuffix())
293 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattner38376f12008-01-12 07:05:38 +0000294 SourceLocation Loc = ConsumeStringToken();
Chris Lattner38376f12008-01-12 07:05:38 +0000295
Douglas Gregor07665a62009-01-05 19:45:36 +0000296 ParseScope LinkageScope(this, Scope::DeclScope);
John McCall48871652010-08-21 09:40:31 +0000297 Decl *LinkageSpec
Douglas Gregor0be31a22010-07-02 17:43:08 +0000298 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000299 DS.getSourceRange().getBegin(),
Benjamin Kramerbebee842010-05-03 13:08:54 +0000300 Loc, Lang,
Abramo Bagnaraea947882011-03-08 16:41:52 +0000301 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor07665a62009-01-05 19:45:36 +0000302 : SourceLocation());
303
John McCall084e83d2011-03-24 11:26:52 +0000304 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000305 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000306 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000307
Douglas Gregor07665a62009-01-05 19:45:36 +0000308 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara4d423992011-05-01 16:25:54 +0000309 // Reset the source range in DS, as the leading "extern"
310 // does not really belong to the inner declaration ...
311 DS.SetRangeStart(SourceLocation());
312 DS.SetRangeEnd(SourceLocation());
313 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000314 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000315 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000316 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor07665a62009-01-05 19:45:36 +0000317 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000318 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000319
Douglas Gregorb65a9132010-02-07 08:38:28 +0000320 DS.abort();
321
John McCall53fa7142010-12-24 02:08:15 +0000322 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000323
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000324 BalancedDelimiterTracker T(*this, tok::l_brace);
325 T.consumeOpen();
Richard Smith34f30512013-11-23 04:06:09 +0000326 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
John McCall084e83d2011-03-24 11:26:52 +0000327 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000328 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000329 MaybeParseMicrosoftAttributes(attrs);
330 ParseExternalDeclaration(attrs);
Chris Lattner38376f12008-01-12 07:05:38 +0000331 }
332
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000333 T.consumeClose();
Chris Lattner8ea64422010-11-09 20:15:55 +0000334 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000335 T.getCloseLocation());
Chris Lattner38376f12008-01-12 07:05:38 +0000336}
Douglas Gregor556877c2008-04-13 21:30:24 +0000337
Douglas Gregord7c4d982008-12-30 03:27:21 +0000338/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
339/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000340Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000341 const ParsedTemplateInfo &TemplateInfo,
342 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000343 ParsedAttributesWithRange &attrs,
344 Decl **OwnedType) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000345 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000346 ObjCDeclContextSwitch ObjCDC(*this);
347
Douglas Gregord7c4d982008-12-30 03:27:21 +0000348 // Eat 'using'.
349 SourceLocation UsingLoc = ConsumeToken();
350
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000351 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000352 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000353 cutOffParsing();
354 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000355 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000356
John McCall9b72f892010-11-10 02:40:36 +0000357 // 'using namespace' means this is a using-directive.
358 if (Tok.is(tok::kw_namespace)) {
359 // Template parameters are always an error here.
360 if (TemplateInfo.Kind) {
361 SourceRange R = TemplateInfo.getSourceRange();
362 Diag(UsingLoc, diag::err_templated_using_directive)
363 << R << FixItHint::CreateRemoval(R);
364 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000365
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000366 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000367 }
368
Richard Smithdda56e42011-04-15 14:24:37 +0000369 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +0000370
371 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000372 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000373
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000374 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000375 AS_none, OwnedType);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000376}
377
378/// ParseUsingDirective - Parse C++ using-directive, assumes
379/// that current token is 'namespace' and 'using' was already parsed.
380///
381/// using-directive: [C++ 7.3.p4: namespace.udir]
382/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
383/// namespace-name ;
384/// [GNU] using-directive:
385/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
386/// namespace-name attributes[opt] ;
387///
John McCall48871652010-08-21 09:40:31 +0000388Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000389 SourceLocation UsingLoc,
390 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000391 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000392 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
393
394 // Eat 'namespace'.
395 SourceLocation NamespcLoc = ConsumeToken();
396
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000397 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000398 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000399 cutOffParsing();
400 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000401 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000402
Douglas Gregord7c4d982008-12-30 03:27:21 +0000403 CXXScopeSpec SS;
404 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000405 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000406
Douglas Gregord7c4d982008-12-30 03:27:21 +0000407 IdentifierInfo *NamespcName = 0;
408 SourceLocation IdentLoc = SourceLocation();
409
410 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000411 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000412 Diag(Tok, diag::err_expected_namespace_name);
413 // If there was invalid namespace name, skip to end of decl, and eat ';'.
414 SkipUntil(tok::semi);
415 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCall48871652010-08-21 09:40:31 +0000416 return 0;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000417 }
Mike Stump11289f42009-09-09 15:08:12 +0000418
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000419 // Parse identifier.
420 NamespcName = Tok.getIdentifierInfo();
421 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000422
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000423 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000424 bool GNUAttr = false;
425 if (Tok.is(tok::kw___attribute)) {
426 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000427 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000428 }
Mike Stump11289f42009-09-09 15:08:12 +0000429
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000430 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000431 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000432 if (ExpectAndConsume(tok::semi,
433 GNUAttr ? diag::err_expected_semi_after_attribute_list
434 : diag::err_expected_semi_after_namespace_name))
435 SkipUntil(tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000436
Douglas Gregor0be31a22010-07-02 17:43:08 +0000437 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000438 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000439}
440
Richard Smithdda56e42011-04-15 14:24:37 +0000441/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
442/// Assumes that 'using' was already seen.
Douglas Gregord7c4d982008-12-30 03:27:21 +0000443///
444/// using-declaration: [C++ 7.3.p3: namespace.udecl]
445/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000446/// unqualified-id
447/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000448///
Richard Smith810ad3e2013-01-29 10:02:16 +0000449/// alias-declaration: C++11 [dcl.dcl]p1
450/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smithdda56e42011-04-15 14:24:37 +0000451///
John McCall48871652010-08-21 09:40:31 +0000452Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000453 const ParsedTemplateInfo &TemplateInfo,
454 SourceLocation UsingLoc,
455 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000456 AccessSpecifier AS,
457 Decl **OwnedType) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000458 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000459 SourceLocation TypenameLoc;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000460 bool HasTypenameKeyword = false;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000461
Richard Smithc2c8bb82013-10-15 01:34:54 +0000462 // Check for misplaced attributes before the identifier in an
463 // alias-declaration.
464 ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
465 MaybeParseCXX11Attributes(MisplacedAttrs);
Douglas Gregorfec52632009-06-20 00:51:54 +0000466
467 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000468 // FIXME: This is wrong; we should parse this as a typename-specifier.
Alp Toker97650562014-01-10 11:19:30 +0000469 if (TryConsumeToken(tok::kw_typename, TypenameLoc))
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000470 HasTypenameKeyword = true;
Douglas Gregorfec52632009-06-20 00:51:54 +0000471
472 // Parse nested-name-specifier.
Richard Smith7447af42013-03-26 01:15:19 +0000473 IdentifierInfo *LastII = 0;
474 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
475 /*MayBePseudoDtor=*/0, /*IsTypename=*/false,
476 /*LastII=*/&LastII);
Douglas Gregorfec52632009-06-20 00:51:54 +0000477
Douglas Gregorfec52632009-06-20 00:51:54 +0000478 // Check nested-name specifier.
479 if (SS.isInvalid()) {
480 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000481 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000482 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000483
Richard Smith7447af42013-03-26 01:15:19 +0000484 SourceLocation TemplateKWLoc;
485 UnqualifiedId Name;
486
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000487 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000488 // destructor names and allow the action module to diagnose any semantic
489 // errors.
Richard Smith7447af42013-03-26 01:15:19 +0000490 //
491 // C++11 [class.qual]p2:
492 // [...] in a using-declaration that is a member-declaration, if the name
493 // specified after the nested-name-specifier is the same as the identifier
494 // or the simple-template-id's template-name in the last component of the
495 // nested-name-specifier, the name is [...] considered to name the
496 // constructor.
497 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
498 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
499 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
500 !SS.getScopeRep()->getAsNamespace() &&
501 !SS.getScopeRep()->getAsNamespaceAlias()) {
502 SourceLocation IdLoc = ConsumeToken();
503 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
504 Name.setConstructorName(Type, IdLoc, IdLoc);
505 } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
506 /*AllowDestructorName=*/ true,
507 /*AllowConstructorName=*/ true, ParsedType(),
508 TemplateKWLoc, Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000509 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000510 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000511 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000512
Richard Smithc2c8bb82013-10-15 01:34:54 +0000513 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smith37a45dd2013-10-24 01:21:09 +0000514 MaybeParseGNUAttributes(Attrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000515 MaybeParseCXX11Attributes(Attrs);
Richard Smithdda56e42011-04-15 14:24:37 +0000516
517 // Maybe this is an alias-declaration.
Richard Smithdda56e42011-04-15 14:24:37 +0000518 TypeResult TypeAlias;
Richard Smithc2c8bb82013-10-15 01:34:54 +0000519 bool IsAliasDecl = Tok.is(tok::equal);
Richard Smithdda56e42011-04-15 14:24:37 +0000520 if (IsAliasDecl) {
Richard Smithc2c8bb82013-10-15 01:34:54 +0000521 // If we had any misplaced attributes from earlier, this is where they
522 // should have been written.
523 if (MisplacedAttrs.Range.isValid()) {
524 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
525 << FixItHint::CreateInsertionFromRange(
526 Tok.getLocation(),
527 CharSourceRange::getTokenRange(MisplacedAttrs.Range))
528 << FixItHint::CreateRemoval(MisplacedAttrs.Range);
529 Attrs.takeAllFrom(MisplacedAttrs);
530 }
531
Richard Smithdda56e42011-04-15 14:24:37 +0000532 ConsumeToken();
533
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000534 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000535 diag::warn_cxx98_compat_alias_declaration :
536 diag::ext_alias_declaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000537
Richard Smith3f1b5d02011-05-05 21:57:07 +0000538 // Type alias templates cannot be specialized.
539 int SpecKind = -1;
Richard Smith14034022011-05-05 22:36:10 +0000540 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
541 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000542 SpecKind = 0;
543 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
544 SpecKind = 1;
545 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
546 SpecKind = 2;
547 if (SpecKind != -1) {
548 SourceRange Range;
549 if (SpecKind == 0)
550 Range = SourceRange(Name.TemplateId->LAngleLoc,
551 Name.TemplateId->RAngleLoc);
552 else
553 Range = TemplateInfo.getSourceRange();
554 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
555 << SpecKind << Range;
556 SkipUntil(tok::semi);
557 return 0;
558 }
559
Richard Smithdda56e42011-04-15 14:24:37 +0000560 // Name must be an identifier.
561 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
562 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
563 // No removal fixit: can't recover from this.
564 SkipUntil(tok::semi);
565 return 0;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000566 } else if (HasTypenameKeyword)
Richard Smithdda56e42011-04-15 14:24:37 +0000567 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
568 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
569 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
570 else if (SS.isNotEmpty())
571 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
572 << FixItHint::CreateRemoval(SS.getRange());
573
Richard Smith3f1b5d02011-05-05 21:57:07 +0000574 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
575 Declarator::AliasTemplateContext :
Richard Smith54ecd982013-02-20 19:22:51 +0000576 Declarator::AliasDeclContext, AS, OwnedType,
577 &Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000578 } else {
579 // C++11 attributes are not allowed on a using-declaration, but GNU ones
580 // are.
Richard Smithc2c8bb82013-10-15 01:34:54 +0000581 ProhibitAttributes(MisplacedAttrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000582 ProhibitAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000583
Richard Smithdda56e42011-04-15 14:24:37 +0000584 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith54ecd982013-02-20 19:22:51 +0000585 MaybeParseGNUAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000586 }
Mike Stump11289f42009-09-09 15:08:12 +0000587
Douglas Gregorfec52632009-06-20 00:51:54 +0000588 // Eat ';'.
589 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000590 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
591 !Attrs.empty() ? "attributes list"
592 : IsAliasDecl ? "alias declaration"
593 : "using declaration"))
594 SkipUntil(tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000595
John McCall9b72f892010-11-10 02:40:36 +0000596 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith810ad3e2013-01-29 10:02:16 +0000597 // In C++11, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000598 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000599 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-11-10 02:40:36 +0000600 SourceRange R = TemplateInfo.getSourceRange();
601 Diag(UsingLoc, diag::err_templated_using_declaration)
602 << R << FixItHint::CreateRemoval(R);
603
604 // Unfortunately, we have to bail out instead of recovering by
605 // ignoring the parameters, just in case the nested name specifier
606 // depends on the parameters.
607 return 0;
608 }
609
Douglas Gregor882a61a2011-09-26 14:30:28 +0000610 // "typename" keyword is allowed for identifiers only,
611 // because it may be a type definition.
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000612 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor882a61a2011-09-26 14:30:28 +0000613 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
614 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000615 // Proceed parsing, but reset the HasTypenameKeyword flag.
616 HasTypenameKeyword = false;
Douglas Gregor882a61a2011-09-26 14:30:28 +0000617 }
618
Richard Smith3f1b5d02011-05-05 21:57:07 +0000619 if (IsAliasDecl) {
620 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000621 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3f1b5d02011-05-05 21:57:07 +0000622 TemplateParams ? TemplateParams->data() : 0,
623 TemplateParams ? TemplateParams->size() : 0);
624 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith54ecd982013-02-20 19:22:51 +0000625 UsingLoc, Name, Attrs.getList(),
626 TypeAlias);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000627 }
Richard Smithdda56e42011-04-15 14:24:37 +0000628
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000629 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
630 /* HasUsingKeyword */ true, UsingLoc,
631 SS, Name, Attrs.getList(),
632 HasTypenameKeyword, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000633}
634
Benjamin Kramere56f3932011-12-23 17:00:35 +0000635/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000636///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000637/// [C++0x] static_assert-declaration:
638/// static_assert ( constant-expression , string-literal ) ;
639///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000640/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000641/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000642///
John McCall48871652010-08-21 09:40:31 +0000643Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000644 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
645 "Not a static_assert declaration");
646
David Blaikiebbafb8a2012-03-11 07:00:24 +0000647 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000648 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000649 if (Tok.is(tok::kw_static_assert))
650 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000651
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000652 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000653
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000654 BalancedDelimiterTracker T(*this, tok::l_paren);
655 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000656 Diag(Tok, diag::err_expected) << tok::l_paren;
Richard Smith76965712012-09-13 19:12:50 +0000657 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000658 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000659 }
Mike Stump11289f42009-09-09 15:08:12 +0000660
John McCalldadc5752010-08-24 06:29:42 +0000661 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000662 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000663 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000664 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000665 }
Mike Stump11289f42009-09-09 15:08:12 +0000666
Alp Toker383d2c42014-01-01 03:08:43 +0000667 if (ExpectAndConsume(tok::comma)) {
668 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000669 return 0;
Alp Toker383d2c42014-01-01 03:08:43 +0000670 }
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000671
Richard Smithf506eaf2012-03-05 23:20:05 +0000672 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000673 Diag(Tok, diag::err_expected_string_literal)
674 << /*Source='static_assert'*/1;
Richard Smith76965712012-09-13 19:12:50 +0000675 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000676 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000677 }
Mike Stump11289f42009-09-09 15:08:12 +0000678
John McCalldadc5752010-08-24 06:29:42 +0000679 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smithd67aea22012-03-06 03:21:47 +0000680 if (AssertMessage.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000681 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000682 return 0;
Richard Smithd67aea22012-03-06 03:21:47 +0000683 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000684
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000685 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000686
Chris Lattner49836b42009-04-02 04:16:50 +0000687 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000688 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000689
John McCallb268a282010-08-23 23:25:46 +0000690 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
691 AssertExpr.take(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000692 AssertMessage.take(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000693 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000694}
695
Richard Smith74aeef52013-04-26 16:15:35 +0000696/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson74948d02009-06-24 17:47:40 +0000697///
698/// 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +0000699/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson74948d02009-06-24 17:47:40 +0000700///
David Blaikie15a430a2011-12-04 05:04:18 +0000701SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
702 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
703 && "Not a decltype specifier");
704
David Blaikie15a430a2011-12-04 05:04:18 +0000705 ExprResult Result;
706 SourceLocation StartLoc = Tok.getLocation();
707 SourceLocation EndLoc;
708
709 if (Tok.is(tok::annot_decltype)) {
710 Result = getExprAnnotation(Tok);
711 EndLoc = Tok.getAnnotationEndLoc();
712 ConsumeToken();
713 if (Result.isInvalid()) {
714 DS.SetTypeSpecError();
715 return EndLoc;
716 }
717 } else {
Richard Smith324df552012-02-24 22:30:04 +0000718 if (Tok.getIdentifierInfo()->isStr("decltype"))
719 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000720
David Blaikie15a430a2011-12-04 05:04:18 +0000721 ConsumeToken();
722
723 BalancedDelimiterTracker T(*this, tok::l_paren);
724 if (T.expectAndConsume(diag::err_expected_lparen_after,
725 "decltype", tok::r_paren)) {
726 DS.SetTypeSpecError();
727 return T.getOpenLocation() == Tok.getLocation() ?
728 StartLoc : T.getOpenLocation();
729 }
730
Richard Smith74aeef52013-04-26 16:15:35 +0000731 // Check for C++1y 'decltype(auto)'.
732 if (Tok.is(tok::kw_auto)) {
733 // No need to disambiguate here: an expression can't start with 'auto',
734 // because the typename-specifier in a function-style cast operation can't
735 // be 'auto'.
736 Diag(Tok.getLocation(),
737 getLangOpts().CPlusPlus1y
738 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
739 : diag::ext_decltype_auto_type_specifier);
740 ConsumeToken();
741 } else {
742 // Parse the expression
David Blaikie15a430a2011-12-04 05:04:18 +0000743
Richard Smith74aeef52013-04-26 16:15:35 +0000744 // C++11 [dcl.type.simple]p4:
745 // The operand of the decltype specifier is an unevaluated operand.
746 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
747 0, /*IsDecltype=*/true);
748 Result = ParseExpression();
749 if (Result.isInvalid()) {
750 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000751 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smith74aeef52013-04-26 16:15:35 +0000752 EndLoc = ConsumeParen();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000753 } else {
Richard Smith74aeef52013-04-26 16:15:35 +0000754 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
755 // Backtrack to get the location of the last token before the semi.
756 PP.RevertCachedTokens(2);
757 ConsumeToken(); // the semi.
758 EndLoc = ConsumeAnyToken();
759 assert(Tok.is(tok::semi));
760 } else {
761 EndLoc = Tok.getLocation();
762 }
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000763 }
Richard Smith74aeef52013-04-26 16:15:35 +0000764 return EndLoc;
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000765 }
Richard Smith74aeef52013-04-26 16:15:35 +0000766
767 Result = Actions.ActOnDecltypeExpression(Result.take());
David Blaikie15a430a2011-12-04 05:04:18 +0000768 }
769
770 // Match the ')'
771 T.consumeClose();
772 if (T.getCloseLocation().isInvalid()) {
773 DS.SetTypeSpecError();
774 // FIXME: this should return the location of the last token
775 // that was consumed (by "consumeClose()")
776 return T.getCloseLocation();
777 }
778
Richard Smithfd555f62012-02-22 02:04:18 +0000779 if (Result.isInvalid()) {
780 DS.SetTypeSpecError();
781 return T.getCloseLocation();
782 }
783
David Blaikie15a430a2011-12-04 05:04:18 +0000784 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000785 }
Richard Smith74aeef52013-04-26 16:15:35 +0000786 assert(!Result.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000787
Anders Carlsson74948d02009-06-24 17:47:40 +0000788 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000789 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000790 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Anders Carlsson74948d02009-06-24 17:47:40 +0000791 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smith74aeef52013-04-26 16:15:35 +0000792 if (Result.get()
793 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000794 DiagID, Result.release(), Policy)
Richard Smith74aeef52013-04-26 16:15:35 +0000795 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000796 DiagID, Policy)) {
John McCall49bfce42009-08-03 20:12:06 +0000797 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-12-04 05:04:18 +0000798 DS.SetTypeSpecError();
799 }
800 return EndLoc;
801}
802
803void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
804 SourceLocation StartLoc,
805 SourceLocation EndLoc) {
806 // make sure we have a token we can turn into an annotation token
807 if (PP.isBacktrackEnabled())
808 PP.RevertCachedTokens(1);
809 else
810 PP.EnterToken(Tok);
811
812 Tok.setKind(tok::annot_decltype);
Richard Smith74aeef52013-04-26 16:15:35 +0000813 setExprAnnotation(Tok,
814 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
815 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
816 ExprError());
David Blaikie15a430a2011-12-04 05:04:18 +0000817 Tok.setAnnotationEndLoc(EndLoc);
818 Tok.setLocation(StartLoc);
819 PP.AnnotateCachedTokens(Tok);
Anders Carlsson74948d02009-06-24 17:47:40 +0000820}
821
Alexis Hunt4a257072011-05-19 05:37:45 +0000822void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
823 assert(Tok.is(tok::kw___underlying_type) &&
824 "Not an underlying type specifier");
825
826 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000827 BalancedDelimiterTracker T(*this, tok::l_paren);
828 if (T.expectAndConsume(diag::err_expected_lparen_after,
829 "__underlying_type", tok::r_paren)) {
Alexis Hunt4a257072011-05-19 05:37:45 +0000830 return;
831 }
832
833 TypeResult Result = ParseTypeName();
834 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000835 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt4a257072011-05-19 05:37:45 +0000836 return;
837 }
838
839 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000840 T.consumeClose();
841 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +0000842 return;
843
844 const char *PrevSpec = 0;
845 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000846 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000847 DiagID, Result.release(),
848 Actions.getASTContext().getPrintingPolicy()))
Alexis Hunt4a257072011-05-19 05:37:45 +0000849 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanellaa90af722013-07-06 18:54:58 +0000850 DS.setTypeofParensRange(T.getRange());
Alexis Hunt4a257072011-05-19 05:37:45 +0000851}
852
David Blaikie00ee7a082011-10-25 15:01:20 +0000853/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
854/// class name or decltype-specifier. Note that we only check that the result
855/// names a type; semantic analysis will need to verify that the type names a
856/// class. The result is either a type or null, depending on whether a type
857/// name was found.
Douglas Gregor831c93f2008-11-05 20:51:48 +0000858///
Richard Smith4c96e992013-02-19 23:47:15 +0000859/// base-type-specifier: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000860/// class-or-decltype
Richard Smith4c96e992013-02-19 23:47:15 +0000861/// class-or-decltype: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000862/// nested-name-specifier[opt] class-name
863/// decltype-specifier
Richard Smith4c96e992013-02-19 23:47:15 +0000864/// class-name: [C++ class.name]
Douglas Gregor831c93f2008-11-05 20:51:48 +0000865/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000866/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000867///
Richard Smith4c96e992013-02-19 23:47:15 +0000868/// In C++98, instead of base-type-specifier, we have:
869///
870/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie1cd50022011-10-25 17:10:12 +0000871Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
872 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-10-25 18:46:41 +0000873 // Ignore attempts to use typename
874 if (Tok.is(tok::kw_typename)) {
875 Diag(Tok, diag::err_expected_class_name_not_template)
876 << FixItHint::CreateRemoval(Tok.getLocation());
877 ConsumeToken();
878 }
879
David Blaikieafa155f2011-10-25 18:17:58 +0000880 // Parse optional nested-name-specifier
881 CXXScopeSpec SS;
882 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
883
884 BaseLoc = Tok.getLocation();
885
David Blaikie1cd50022011-10-25 17:10:12 +0000886 // Parse decltype-specifier
David Blaikie15a430a2011-12-04 05:04:18 +0000887 // tok == kw_decltype is just error recovery, it can only happen when SS
888 // isn't empty
889 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikieafa155f2011-10-25 18:17:58 +0000890 if (SS.isNotEmpty())
891 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
892 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie1cd50022011-10-25 17:10:12 +0000893 // Fake up a Declarator to use with ActOnTypeName.
894 DeclSpec DS(AttrFactory);
895
David Blaikie7491e732011-12-08 04:53:15 +0000896 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +0000897
898 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
899 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
900 }
901
Douglas Gregord54dfb82009-02-25 23:52:28 +0000902 // Check whether we have a template-id that names a type.
903 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000904 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +0000905 if (TemplateId->Kind == TNK_Type_template ||
906 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000907 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000908
909 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000910 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000911 EndLocation = Tok.getAnnotationEndLoc();
912 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000913
914 if (Type)
915 return Type;
916 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000917 }
918
919 // Fall through to produce an error below.
920 }
921
Douglas Gregor831c93f2008-11-05 20:51:48 +0000922 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000923 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000924 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000925 }
926
Douglas Gregor18473f32010-01-12 21:28:44 +0000927 IdentifierInfo *Id = Tok.getIdentifierInfo();
928 SourceLocation IdLoc = ConsumeToken();
929
930 if (Tok.is(tok::less)) {
931 // It looks the user intended to write a template-id here, but the
932 // template-name was wrong. Try to fix that.
933 TemplateNameKind TNK = TNK_Type_template;
934 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000935 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000936 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000937 Diag(IdLoc, diag::err_unknown_template_name)
938 << Id;
939 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000940
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000941 if (!Template) {
942 TemplateArgList TemplateArgs;
943 SourceLocation LAngleLoc, RAngleLoc;
944 ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
945 true, LAngleLoc, TemplateArgs, RAngleLoc);
Douglas Gregor18473f32010-01-12 21:28:44 +0000946 return true;
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000947 }
Douglas Gregor18473f32010-01-12 21:28:44 +0000948
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000949 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000950 UnqualifiedId TemplateName;
951 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000952
Douglas Gregor18473f32010-01-12 21:28:44 +0000953 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +0000954 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
955 TemplateName, true))
Douglas Gregor18473f32010-01-12 21:28:44 +0000956 return true;
957 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +0000958 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000959
Douglas Gregor18473f32010-01-12 21:28:44 +0000960 // If we didn't end up with a typename token, there's nothing more we
961 // can do.
962 if (Tok.isNot(tok::annot_typename))
963 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000964
Douglas Gregor18473f32010-01-12 21:28:44 +0000965 // Retrieve the type from the annotation token, consume that token, and
966 // return.
967 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +0000968 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +0000969 ConsumeToken();
970 return Type;
971 }
972
Douglas Gregor831c93f2008-11-05 20:51:48 +0000973 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +0000974 IdentifierInfo *CorrectedII = 0;
Douglas Gregore7c20652011-03-02 00:47:37 +0000975 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +0000976 false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +0000977 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +0000978 /*NonTrivialTypeSourceInfo=*/true,
979 &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000980 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +0000981 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000982 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000983 }
984
985 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +0000986 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000987
988 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000989 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +0000990 DS.SetRangeStart(IdLoc);
991 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +0000992 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000993
994 const char *PrevSpec = 0;
995 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000996 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
997 Actions.getASTContext().getPrintingPolicy());
Nick Lewycky19b9f952010-07-26 16:56:01 +0000998
999 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1000 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001001}
1002
John McCall8d32c052012-05-22 21:28:12 +00001003void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
1004 while (Tok.is(tok::kw___single_inheritance) ||
1005 Tok.is(tok::kw___multiple_inheritance) ||
1006 Tok.is(tok::kw___virtual_inheritance)) {
1007 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1008 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +00001009 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
Aaron Ballman8edb5c22013-12-18 23:44:18 +00001010 AttributeList::AS_Keyword);
John McCall8d32c052012-05-22 21:28:12 +00001011 }
1012}
1013
Richard Smith369b9f92012-06-25 21:37:02 +00001014/// Determine whether the following tokens are valid after a type-specifier
1015/// which could be a standalone declaration. This will conservatively return
1016/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith200f47c2012-07-02 19:14:01 +00001017bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-06-25 21:37:02 +00001018 // This switch enumerates the valid "follow" set for type-specifiers.
1019 switch (Tok.getKind()) {
1020 default: break;
1021 case tok::semi: // struct foo {...} ;
1022 case tok::star: // struct foo {...} * P;
1023 case tok::amp: // struct foo {...} & R = ...
Richard Smith1ac67d12013-01-19 03:48:05 +00001024 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +00001025 case tok::identifier: // struct foo {...} V ;
1026 case tok::r_paren: //(struct foo {...} ) {4}
1027 case tok::annot_cxxscope: // struct foo {...} a:: b;
1028 case tok::annot_typename: // struct foo {...} a ::b;
1029 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1030 case tok::l_paren: // struct foo {...} ( x);
1031 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +00001032 case tok::kw_operator: // struct foo operator ++() {...}
Alp Tokerd3f79c52013-11-24 20:24:54 +00001033 case tok::kw___declspec: // struct foo {...} __declspec(...)
Richard Smith369b9f92012-06-25 21:37:02 +00001034 return true;
Richard Smith200f47c2012-07-02 19:14:01 +00001035 case tok::colon:
1036 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smith369b9f92012-06-25 21:37:02 +00001037 // Type qualifiers
1038 case tok::kw_const: // struct foo {...} const x;
1039 case tok::kw_volatile: // struct foo {...} volatile x;
1040 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001041 // Function specifiers
1042 // Note, no 'explicit'. An explicit function must be either a conversion
1043 // operator or a constructor. Either way, it can't have a return type.
1044 case tok::kw_inline: // struct foo inline f();
1045 case tok::kw_virtual: // struct foo virtual f();
1046 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +00001047 // Storage-class specifiers
1048 case tok::kw_static: // struct foo {...} static x;
1049 case tok::kw_extern: // struct foo {...} extern x;
1050 case tok::kw_typedef: // struct foo {...} typedef x;
1051 case tok::kw_register: // struct foo {...} register x;
1052 case tok::kw_auto: // struct foo {...} auto x;
1053 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001054 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +00001055 case tok::kw_constexpr: // struct foo {...} constexpr x;
1056 // As shown above, type qualifiers and storage class specifiers absolutely
1057 // can occur after class specifiers according to the grammar. However,
1058 // almost no one actually writes code like this. If we see one of these,
1059 // it is much more likely that someone missed a semi colon and the
1060 // type/storage class specifier we're seeing is part of the *next*
1061 // intended declaration, as in:
1062 //
1063 // struct foo { ... }
1064 // typedef int X;
1065 //
1066 // We'd really like to emit a missing semicolon error instead of emitting
1067 // an error on the 'int' saying that you can't have two type specifiers in
1068 // the same declaration of X. Because of this, we look ahead past this
1069 // token to see if it's a type specifier. If so, we know the code is
1070 // otherwise invalid, so we can produce the expected semi error.
1071 if (!isKnownToBeTypeSpecifier(NextToken()))
1072 return true;
1073 break;
1074 case tok::r_brace: // struct bar { struct foo {...} }
1075 // Missing ';' at end of struct is accepted as an extension in C mode.
1076 if (!getLangOpts().CPlusPlus)
1077 return true;
1078 break;
Richard Smith1ac67d12013-01-19 03:48:05 +00001079 // C++11 attributes
1080 case tok::l_square: // enum E [[]] x
1081 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1082 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith52c5b872013-01-29 04:13:32 +00001083 case tok::greater:
1084 // template<class T = class X>
1085 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001086 }
1087 return false;
1088}
1089
Douglas Gregor556877c2008-04-13 21:30:24 +00001090/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1091/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1092/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001093/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001094///
1095/// class-specifier: [C++ class]
1096/// class-head '{' member-specification[opt] '}'
1097/// class-head '{' member-specification[opt] '}' attributes[opt]
1098/// class-head:
1099/// class-key identifier[opt] base-clause[opt]
1100/// class-key nested-name-specifier identifier base-clause[opt]
1101/// class-key nested-name-specifier[opt] simple-template-id
1102/// base-clause[opt]
1103/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001104/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001105/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001106/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001107/// simple-template-id base-clause[opt]
1108/// class-key:
1109/// 'class'
1110/// 'struct'
1111/// 'union'
1112///
1113/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001114/// class-key ::[opt] nested-name-specifier[opt] identifier
1115/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1116/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001117///
1118/// Note that the C++ class-specifier and elaborated-type-specifier,
1119/// together, subsume the C99 struct-or-union-specifier:
1120///
1121/// struct-or-union-specifier: [C99 6.7.2.1]
1122/// struct-or-union identifier[opt] '{' struct-contents '}'
1123/// struct-or-union identifier
1124/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1125/// '}' attributes[opt]
1126/// [GNU] struct-or-union attributes[opt] identifier
1127/// struct-or-union:
1128/// 'struct'
1129/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001130void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1131 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001132 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001133 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001134 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001135 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001136 DeclSpec::TST TagType;
1137 if (TagTokKind == tok::kw_struct)
1138 TagType = DeclSpec::TST_struct;
1139 else if (TagTokKind == tok::kw___interface)
1140 TagType = DeclSpec::TST_interface;
1141 else if (TagTokKind == tok::kw_class)
1142 TagType = DeclSpec::TST_class;
1143 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001144 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1145 TagType = DeclSpec::TST_union;
1146 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001147
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001148 if (Tok.is(tok::code_completion)) {
1149 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001150 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001151 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001152 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001153
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001154 // C++03 [temp.explicit] 14.7.2/8:
1155 // The usual access checking rules do not apply to names used to specify
1156 // explicit instantiations.
1157 //
1158 // As an extension we do not perform access checking on the names used to
1159 // specify explicit specializations either. This is important to allow
1160 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001161 //
1162 // Note that we don't suppress if this turns out to be an elaborated
1163 // type specifier.
1164 bool shouldDelayDiagsInTag =
1165 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1166 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1167 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001168
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001169 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001170 // If attributes exist after tag, parse them.
Richard Smith37a45dd2013-10-24 01:21:09 +00001171 MaybeParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +00001172
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001173 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +00001174 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +00001175 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001176
John McCall8d32c052012-05-22 21:28:12 +00001177 // Parse inheritance specifiers.
1178 if (Tok.is(tok::kw___single_inheritance) ||
1179 Tok.is(tok::kw___multiple_inheritance) ||
1180 Tok.is(tok::kw___virtual_inheritance))
Richard Smith37a45dd2013-10-24 01:21:09 +00001181 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCall8d32c052012-05-22 21:28:12 +00001182
Alexis Hunt96d5c762009-11-21 08:43:09 +00001183 // If C++0x attributes exist here, parse them.
1184 // FIXME: Are we consistent with the ordering of parsing of different
1185 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001186 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001187
Michael Han309af292013-01-07 16:57:11 +00001188 // Source location used by FIXIT to insert misplaced
1189 // C++11 attributes
1190 SourceLocation AttrFixitLoc = Tok.getLocation();
1191
Alp Toker53358e42013-12-17 14:12:30 +00001192 // GNU libstdc++ and libc++ use certain intrinsic names as the
1193 // name of struct templates, but some are keywords in GCC >= 4.3
1194 // MSVC and Clang. For compatibility, convert the token to an identifier
1195 // and issue a warning diagnostic.
1196 if (TagType == DeclSpec::TST_struct && !Tok.is(tok::identifier) &&
1197 !Tok.isAnnotation()) {
1198 const IdentifierInfo *II = Tok.getIdentifierInfo();
1199 // We rarely end up here so the following check is efficient.
1200 if (II && II->getName().startswith("__is_"))
1201 TryKeywordIdentFallback(true);
1202 }
Mike Stump11289f42009-09-09 15:08:12 +00001203
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001204 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001205 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001206 if (getLangOpts().CPlusPlus) {
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001207 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1208 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001209
Douglas Gregordf593fb2011-11-07 17:33:42 +00001210 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall413021a2010-07-30 06:26:29 +00001211 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +00001212 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001213 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Alp Tokerec543272013-12-24 09:48:30 +00001214 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001215 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001216
Douglas Gregor916462b2009-10-30 21:46:58 +00001217 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1218
Douglas Gregor67a65642009-02-17 23:15:12 +00001219 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +00001220 IdentifierInfo *Name = 0;
1221 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +00001222 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +00001223 if (Tok.is(tok::identifier)) {
1224 Name = Tok.getIdentifierInfo();
1225 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001226
David Blaikiebbafb8a2012-03-11 07:00:24 +00001227 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001228 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001229 // Eat the template argument list and try to continue parsing this as
1230 // a class (or template thereof).
1231 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001232 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +00001233 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +00001234 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001235 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001236 // We couldn't parse the template argument list at all, so don't
1237 // try to give any location information for the list.
1238 LAngleLoc = RAngleLoc = SourceLocation();
1239 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001240
Douglas Gregor916462b2009-10-30 21:46:58 +00001241 Diag(NameLoc, diag::err_explicit_spec_non_template)
Alp Toker01d65e12014-01-06 12:54:41 +00001242 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1243 << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
Joao Matose9a3ed42012-08-31 22:18:20 +00001244
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001245 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001246 // we've removed its template argument list.
1247 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1248 if (TemplateParams && TemplateParams->size() > 1) {
1249 TemplateParams->pop_back();
1250 } else {
1251 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001252 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001253 = ParsedTemplateInfo::NonTemplate;
1254 }
1255 } else if (TemplateInfo.Kind
1256 == ParsedTemplateInfo::ExplicitInstantiation) {
1257 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +00001258 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001259 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001260 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001261 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001262 = SourceLocation();
1263 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1264 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001265 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001266 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001267 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001268 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001269 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001270
Douglas Gregore7c20652011-03-02 00:47:37 +00001271 if (TemplateId->Kind != TNK_Type_template &&
1272 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001273 // The template-name in the simple-template-id refers to
1274 // something other than a class template. Give an appropriate
1275 // error message and skip to the ';'.
1276 SourceRange Range(NameLoc);
1277 if (SS.isNotEmpty())
1278 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001279
Richard Smith72bfbd82013-12-04 00:28:23 +00001280 // FIXME: Name may be null here.
Douglas Gregor7f741122009-02-25 19:37:18 +00001281 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu30f93852013-06-19 22:25:01 +00001282 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001283
Douglas Gregor7f741122009-02-25 19:37:18 +00001284 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001285 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor7f741122009-02-25 19:37:18 +00001286 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001287 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001288 }
1289
Richard Smithbfdb1082012-03-12 08:56:40 +00001290 // There are four options here.
1291 // - If we are in a trailing return type, this is always just a reference,
1292 // and we must not try to parse a definition. For instance,
1293 // [] () -> struct S { };
1294 // does not define a type.
1295 // - If we have 'struct foo {...', 'struct foo :...',
1296 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1297 // - If we have 'struct foo;', then this is either a forward declaration
1298 // or a friend declaration, which have to be treated differently.
1299 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001300 //
1301 // We also detect these erroneous cases to provide better diagnostic for
1302 // C++11 attributes parsing.
1303 // - attributes follow class name:
1304 // struct foo [[]] {};
1305 // - attributes appear before or after 'final':
1306 // struct foo [[]] final [[]] {};
1307 //
Richard Smithc5b05522012-03-12 07:56:15 +00001308 // However, in type-specifier-seq's, things look like declarations but are
1309 // just references, e.g.
1310 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001311 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001312 // &T::operator struct s;
Richard Smith649c7b062014-01-08 00:56:48 +00001313 // For these, DSC is DSC_type_specifier or DSC_alias_declaration.
Michael Han9407e502012-11-26 22:54:45 +00001314
1315 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001316 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001317
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001318 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
John McCallfaf5fb42010-08-26 23:41:50 +00001319 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001320 if (DSC == DSC_trailing)
1321 TUK = Sema::TUK_Reference;
1322 else if (Tok.is(tok::l_brace) ||
1323 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001324 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001325 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001326 if (DS.isFriendSpecified()) {
1327 // C++ [class.friend]p2:
1328 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001329 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001330 << SourceRange(DS.getFriendSpecLoc());
1331
1332 // Skip everything up to the semicolon, so that this looks like a proper
1333 // friend class (or template thereof) declaration.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001334 SkipUntil(tok::semi, StopBeforeMatch);
John McCallfaf5fb42010-08-26 23:41:50 +00001335 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001336 } else {
1337 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001338 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001339 }
Richard Smith434516c2013-02-22 06:46:23 +00001340 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1341 NextToken().is(tok::kw_alignas))) {
Michael Han9407e502012-11-26 22:54:45 +00001342 // We can't tell if this is a definition or reference
1343 // until we skipped the 'final' and C++11 attribute specifiers.
1344 TentativeParsingAction PA(*this);
1345
1346 // Skip the 'final' keyword.
1347 ConsumeToken();
1348
1349 // Skip C++11 attribute specifiers.
1350 while (true) {
1351 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1352 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001353 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001354 break;
Richard Smith434516c2013-02-22 06:46:23 +00001355 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han9407e502012-11-26 22:54:45 +00001356 ConsumeToken();
1357 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001358 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001359 break;
1360 } else {
1361 break;
1362 }
1363 }
1364
1365 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1366 TUK = Sema::TUK_Definition;
1367 else
1368 TUK = Sema::TUK_Reference;
1369
1370 PA.Revert();
Richard Smith649c7b062014-01-08 00:56:48 +00001371 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00001372 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001373 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001374 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001375 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001376 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Joao Matose9a3ed42012-08-31 22:18:20 +00001377 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00001378 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001379 DeclSpec::getSpecifierName(TagType, PPol));
Joao Matose9a3ed42012-08-31 22:18:20 +00001380 PP.EnterToken(Tok);
1381 Tok.setKind(tok::semi);
1382 }
Richard Smith369b9f92012-06-25 21:37:02 +00001383 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001384 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001385
Michael Han9407e502012-11-26 22:54:45 +00001386 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1387 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001388 if (TUK != Sema::TUK_Reference) {
1389 // If this is not a reference, then the only possible
1390 // valid place for C++11 attributes to appear here
1391 // is between class-key and class-name. If there are
1392 // any attributes after class-name, we try a fixit to move
1393 // them to the right place.
1394 SourceRange AttrRange = Attributes.Range;
1395 if (AttrRange.isValid()) {
1396 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1397 << AttrRange
1398 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1399 CharSourceRange(AttrRange, true))
1400 << FixItHint::CreateRemoval(AttrRange);
1401
1402 // Recover by adding misplaced attributes to the attribute list
1403 // of the class so they can be applied on the class later.
1404 attrs.takeAllFrom(Attributes);
1405 }
1406 }
Michael Han9407e502012-11-26 22:54:45 +00001407
John McCall6347b682012-05-07 06:16:58 +00001408 // If this is an elaborated type specifier, and we delayed
1409 // diagnostics before, just merge them into the current pool.
1410 if (shouldDelayDiagsInTag) {
1411 diagsFromTag.done();
1412 if (TUK == Sema::TUK_Reference)
1413 diagsFromTag.redelay();
1414 }
1415
John McCall413021a2010-07-30 06:26:29 +00001416 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001417 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001418 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1419 // We have a declaration or reference to an anonymous class.
1420 Diag(StartLoc, diag::err_anon_type_definition)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001421 << DeclSpec::getSpecifierName(TagType, Policy);
John McCall413021a2010-07-30 06:26:29 +00001422 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001423
David Majnemer3252fd02013-12-05 01:36:53 +00001424 // If we are parsing a definition and stop at a base-clause, continue on
1425 // until the semicolon. Continuing from the comma will just trick us into
1426 // thinking we are seeing a variable declaration.
1427 if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
1428 SkipUntil(tok::semi, StopBeforeMatch);
1429 else
1430 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor556877c2008-04-13 21:30:24 +00001431 return;
1432 }
1433
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001434 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001435 DeclResult TagOrTempResult = true; // invalid
1436 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001437
Douglas Gregord6ab8742009-05-28 23:31:59 +00001438 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +00001439 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001440 // Explicit specialization, class template partial specialization,
1441 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001442 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001443 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001444 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001445 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001446 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001447 ProhibitAttributes(attrs);
1448
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001449 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001450 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001451 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001452 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001453 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001454 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001455 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001456 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001457 TemplateId->TemplateNameLoc,
1458 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001459 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001460 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001461 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001462
1463 // Friend template-ids are treated as references unless
1464 // they have template headers, in which case they're ill-formed
1465 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1466 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001467 } else if (TUK == Sema::TUK_Reference ||
1468 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001469 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001470 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001471 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001472 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001473 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001474 TemplateId->Template,
1475 TemplateId->TemplateNameLoc,
1476 TemplateId->LAngleLoc,
1477 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001478 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001479 } else {
1480 // This is an explicit specialization or a class template
1481 // partial specialization.
1482 TemplateParameterLists FakedParamLists;
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001483 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1484 // This looks like an explicit instantiation, because we have
1485 // something like
1486 //
1487 // template class Foo<X>
1488 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001489 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001490 // meant to be an explicit specialization, but the user forgot
1491 // the '<>' after 'template'.
Richard Smith003c5e12013-11-08 19:03:29 +00001492 // It this is friend declaration however, since it cannot have a
1493 // template header, it is most likely that the user meant to
1494 // remove the 'template' keyword.
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001495 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith003c5e12013-11-08 19:03:29 +00001496 "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001497
Richard Smith003c5e12013-11-08 19:03:29 +00001498 if (TUK == Sema::TUK_Friend) {
1499 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
1500 TemplateParams = 0;
1501 } else {
1502 SourceLocation LAngleLoc =
1503 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1504 Diag(TemplateId->TemplateNameLoc,
1505 diag::err_explicit_instantiation_with_definition)
1506 << SourceRange(TemplateInfo.TemplateLoc)
1507 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1508
1509 // Create a fake template parameter list that contains only
1510 // "template<>", so that we treat this construct as a class
1511 // template specialization.
1512 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1513 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1514 LAngleLoc));
1515 TemplateParams = &FakedParamLists;
1516 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001517 }
1518
1519 // Build the class template specialization.
1520 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001521 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor3c7cd6a2011-09-09 20:53:38 +00001522 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall3e56fd42010-08-23 07:28:44 +00001523 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001524 TemplateId->TemplateNameLoc,
1525 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +00001526 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001527 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001528 attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001529 MultiTemplateParamsArg(
Douglas Gregor67a65642009-02-17 23:15:12 +00001530 TemplateParams? &(*TemplateParams)[0] : 0,
1531 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001532 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001533 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001534 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001535 // Explicit instantiation of a member of a class template
1536 // specialization, e.g.,
1537 //
1538 // template struct Outer<int>::Inner;
1539 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001540 ProhibitAttributes(attrs);
1541
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001542 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001543 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001544 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001545 TemplateInfo.TemplateLoc,
1546 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001547 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001548 } else if (TUK == Sema::TUK_Friend &&
1549 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001550 ProhibitAttributes(attrs);
1551
John McCallace48cd2010-10-19 01:40:49 +00001552 TagOrTempResult =
1553 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1554 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001555 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001556 MultiTemplateParamsArg(
John McCallace48cd2010-10-19 01:40:49 +00001557 TemplateParams? &(*TemplateParams)[0] : 0,
1558 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001559 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001560 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1561 ProhibitAttributes(attrs);
Richard Smith003c5e12013-11-08 19:03:29 +00001562
Larisse Voufo725de3e2013-06-21 00:08:46 +00001563 if (TUK == Sema::TUK_Definition &&
1564 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1565 // If the declarator-id is not a template-id, issue a diagnostic and
1566 // recover by ignoring the 'template' keyword.
1567 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1568 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001569 TemplateParams = 0;
Larisse Voufo725de3e2013-06-21 00:08:46 +00001570 }
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001571
John McCall7f41d982009-09-11 04:59:25 +00001572 bool IsDependent = false;
1573
John McCall32723e92010-10-19 18:40:57 +00001574 // Don't pass down template parameter lists if this is just a tag
1575 // reference. For example, we don't need the template parameters here:
1576 // template <class T> class A *makeA(T t);
1577 MultiTemplateParamsArg TParams;
1578 if (TUK != Sema::TUK_Reference && TemplateParams)
1579 TParams =
1580 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1581
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001582 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001583 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001584 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001585 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001586 TParams, Owned, IsDependent,
1587 SourceLocation(), false,
Richard Smith649c7b062014-01-08 00:56:48 +00001588 clang::TypeResult(),
1589 DSC == DSC_type_specifier);
John McCall7f41d982009-09-11 04:59:25 +00001590
1591 // If ActOnTag said the type was dependent, try again with the
1592 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001593 if (IsDependent) {
1594 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001595 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001596 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001597 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001598 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001599
Douglas Gregor556877c2008-04-13 21:30:24 +00001600 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001601 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001602 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001603 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001604 isCXX11FinalKeyword());
David Blaikiebbafb8a2012-03-11 07:00:24 +00001605 if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001606 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1607 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001608 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001609 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001610 }
1611
John McCallba7bf592010-08-24 05:47:05 +00001612 const char *PrevSpec = 0;
1613 unsigned DiagID;
1614 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001615 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001616 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1617 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001618 PrevSpec, DiagID, TypeResult.get(), Policy);
John McCall7f41d982009-09-11 04:59:25 +00001619 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001620 Result = DS.SetTypeSpecType(TagType, StartLoc,
1621 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001622 PrevSpec, DiagID, TagOrTempResult.get(), Owned,
1623 Policy);
John McCall7f41d982009-09-11 04:59:25 +00001624 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001625 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001626 return;
1627 }
Mike Stump11289f42009-09-09 15:08:12 +00001628
John McCallba7bf592010-08-24 05:47:05 +00001629 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001630 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001631
Chris Lattnercf251412010-02-02 01:23:29 +00001632 // At this point, we've successfully parsed a class-specifier in 'definition'
1633 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1634 // going to look at what comes after it to improve error recovery. If an
1635 // impossible token occurs next, we assume that the programmer forgot a ; at
1636 // the end of the declaration and recover that way.
1637 //
Richard Smith369b9f92012-06-25 21:37:02 +00001638 // Also enforce C++ [temp]p3:
1639 // In a template-declaration which defines a class, no declarator
1640 // is permitted.
Joao Matose9a3ed42012-08-31 22:18:20 +00001641 if (TUK == Sema::TUK_Definition &&
1642 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001643 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001644 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Alp Toker383d2c42014-01-01 03:08:43 +00001645 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001646 DeclSpec::getSpecifierName(TagType, PPol));
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001647 // Push this token back into the preprocessor and change our current token
1648 // to ';' so that the rest of the code recovers as though there were an
1649 // ';' after the definition.
1650 PP.EnterToken(Tok);
1651 Tok.setKind(tok::semi);
1652 }
Chris Lattnercf251412010-02-02 01:23:29 +00001653 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001654}
1655
Mike Stump11289f42009-09-09 15:08:12 +00001656/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001657///
1658/// base-clause : [C++ class.derived]
1659/// ':' base-specifier-list
1660/// base-specifier-list:
1661/// base-specifier '...'[opt]
1662/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001663void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001664 assert(Tok.is(tok::colon) && "Not a base clause");
1665 ConsumeToken();
1666
Douglas Gregor29a92472008-10-22 17:49:05 +00001667 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001668 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001669
Douglas Gregor556877c2008-04-13 21:30:24 +00001670 while (true) {
1671 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001672 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001673 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001674 // Skip the rest of this base specifier, up until the comma or
1675 // opening brace.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001676 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor29a92472008-10-22 17:49:05 +00001677 } else {
1678 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001679 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001680 }
1681
1682 // If the next token is a comma, consume it and keep reading
1683 // base-specifiers.
Alp Toker97650562014-01-10 11:19:30 +00001684 if (!TryConsumeToken(tok::comma))
1685 break;
Douglas Gregor556877c2008-04-13 21:30:24 +00001686 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001687
1688 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001689 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001690}
1691
1692/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1693/// one entry in the base class list of a class specifier, for example:
1694/// class foo : public bar, virtual private baz {
1695/// 'public bar' and 'virtual private baz' are each base-specifiers.
1696///
1697/// base-specifier: [C++ class.derived]
Richard Smith4c96e992013-02-19 23:47:15 +00001698/// attribute-specifier-seq[opt] base-type-specifier
1699/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1700/// base-type-specifier
1701/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1702/// base-type-specifier
John McCall48871652010-08-21 09:40:31 +00001703Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001704 bool IsVirtual = false;
1705 SourceLocation StartLoc = Tok.getLocation();
1706
Richard Smith4c96e992013-02-19 23:47:15 +00001707 ParsedAttributesWithRange Attributes(AttrFactory);
1708 MaybeParseCXX11Attributes(Attributes);
1709
Douglas Gregor556877c2008-04-13 21:30:24 +00001710 // Parse the 'virtual' keyword.
Alp Toker97650562014-01-10 11:19:30 +00001711 if (TryConsumeToken(tok::kw_virtual))
Douglas Gregor556877c2008-04-13 21:30:24 +00001712 IsVirtual = true;
Douglas Gregor556877c2008-04-13 21:30:24 +00001713
Richard Smith4c96e992013-02-19 23:47:15 +00001714 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1715
Douglas Gregor556877c2008-04-13 21:30:24 +00001716 // Parse an (optional) access specifier.
1717 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001718 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001719 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001720
Richard Smith4c96e992013-02-19 23:47:15 +00001721 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1722
Douglas Gregor556877c2008-04-13 21:30:24 +00001723 // Parse the 'virtual' keyword (again!), in case it came after the
1724 // access specifier.
1725 if (Tok.is(tok::kw_virtual)) {
1726 SourceLocation VirtualLoc = ConsumeToken();
1727 if (IsVirtual) {
1728 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001729 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001730 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001731 }
1732
1733 IsVirtual = true;
1734 }
1735
Richard Smith4c96e992013-02-19 23:47:15 +00001736 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1737
Douglas Gregor831c93f2008-11-05 20:51:48 +00001738 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001739 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00001740 SourceLocation BaseLoc;
1741 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001742 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001743 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001744
Douglas Gregor752a5952011-01-03 22:36:02 +00001745 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1746 // actually part of the base-specifier-list grammar productions, but we
1747 // parse it here for convenience.
1748 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00001749 TryConsumeToken(tok::ellipsis, EllipsisLoc);
1750
Mike Stump11289f42009-09-09 15:08:12 +00001751 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001752 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001753
Douglas Gregor556877c2008-04-13 21:30:24 +00001754 // Notify semantic analysis that we have parsed a complete
1755 // base-specifier.
Richard Smith4c96e992013-02-19 23:47:15 +00001756 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1757 Access, BaseType.get(), BaseLoc,
1758 EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001759}
1760
1761/// getAccessSpecifierIfPresent - Determine whether the next token is
1762/// a C++ access-specifier.
1763///
1764/// access-specifier: [C++ class.derived]
1765/// 'private'
1766/// 'protected'
1767/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001768AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001769 switch (Tok.getKind()) {
1770 default: return AS_none;
1771 case tok::kw_private: return AS_private;
1772 case tok::kw_protected: return AS_protected;
1773 case tok::kw_public: return AS_public;
1774 }
1775}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001776
Douglas Gregor433e0532012-04-16 18:27:27 +00001777/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith2331bbf2012-05-02 22:22:32 +00001778/// delayed, e.g., default arguments, create a late-parsed method declaration
1779/// record to handle the parsing at the end of the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00001780void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1781 Decl *ThisDecl) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001782 // We just declared a member function. If this member function
Richard Smith2331bbf2012-05-02 22:22:32 +00001783 // has any default arguments, we'll need to parse them later.
Eli Friedman3af2a772009-07-22 21:45:50 +00001784 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001785 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001786 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor433e0532012-04-16 18:27:27 +00001787
Eli Friedman3af2a772009-07-22 21:45:50 +00001788 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1789 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1790 if (!LateMethod) {
1791 // Push this method onto the stack of late-parsed method
1792 // declarations.
Douglas Gregorefc46952010-10-12 16:25:54 +00001793 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1794 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001795 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001796
1797 // Add all of the parameters prior to this one (they don't
1798 // have default arguments).
1799 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1800 for (unsigned I = 0; I < ParamIdx; ++I)
1801 LateMethod->DefaultArgs.push_back(
Douglas Gregor1d85d292010-03-02 01:29:43 +00001802 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001803 }
1804
Douglas Gregor433e0532012-04-16 18:27:27 +00001805 // Add this parameter to the list of parameters (it may or may
Eli Friedman3af2a772009-07-22 21:45:50 +00001806 // not have a default argument).
1807 LateMethod->DefaultArgs.push_back(
1808 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1809 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1810 }
1811 }
1812}
1813
Richard Smith89645bc2013-01-02 12:01:23 +00001814/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001815/// virt-specifier.
1816///
1817/// virt-specifier:
1818/// override
1819/// final
Richard Smith89645bc2013-01-02 12:01:23 +00001820VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001821 if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001822 return VirtSpecifiers::VS_None;
1823
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001824 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001825
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001826 // Initialize the contextual keywords.
1827 if (!Ident_final) {
1828 Ident_final = &PP.getIdentifierTable().get("final");
1829 if (getLangOpts().MicrosoftExt)
1830 Ident_sealed = &PP.getIdentifierTable().get("sealed");
1831 Ident_override = &PP.getIdentifierTable().get("override");
Anders Carlsson56104902011-01-17 03:05:47 +00001832 }
1833
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001834 if (II == Ident_override)
1835 return VirtSpecifiers::VS_Override;
1836
1837 if (II == Ident_sealed)
1838 return VirtSpecifiers::VS_Sealed;
1839
1840 if (II == Ident_final)
1841 return VirtSpecifiers::VS_Final;
1842
Anders Carlsson56104902011-01-17 03:05:47 +00001843 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001844}
1845
Richard Smith89645bc2013-01-02 12:01:23 +00001846/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001847///
1848/// virt-specifier-seq:
1849/// virt-specifier
1850/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00001851void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalldb632ac2012-09-25 07:32:39 +00001852 bool IsInterface) {
Anders Carlsson56104902011-01-17 03:05:47 +00001853 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00001854 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001855 if (Specifier == VirtSpecifiers::VS_None)
1856 return;
1857
1858 // C++ [class.mem]p8:
1859 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001860 const char *PrevSpec = 0;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001861 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001862 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1863 << PrevSpec
1864 << FixItHint::CreateRemoval(Tok.getLocation());
1865
David Majnemera5433082013-10-18 00:33:31 +00001866 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
1867 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalldb632ac2012-09-25 07:32:39 +00001868 Diag(Tok.getLocation(), diag::err_override_control_interface)
1869 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemera5433082013-10-18 00:33:31 +00001870 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
1871 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
John McCalldb632ac2012-09-25 07:32:39 +00001872 } else {
David Majnemera5433082013-10-18 00:33:31 +00001873 Diag(Tok.getLocation(),
1874 getLangOpts().CPlusPlus11
1875 ? diag::warn_cxx98_compat_override_control_keyword
1876 : diag::ext_override_control_keyword)
1877 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalldb632ac2012-09-25 07:32:39 +00001878 }
Anders Carlsson56104902011-01-17 03:05:47 +00001879 ConsumeToken();
1880 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001881}
1882
Richard Smith89645bc2013-01-02 12:01:23 +00001883/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001884/// 'final' or Microsoft 'sealed' contextual keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00001885bool Parser::isCXX11FinalKeyword() const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001886 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
1887 return Specifier == VirtSpecifiers::VS_Final ||
1888 Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001889}
1890
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001891/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1892///
1893/// member-declaration:
1894/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1895/// function-definition ';'[opt]
1896/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1897/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001898/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001899/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001900/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001901///
1902/// member-declarator-list:
1903/// member-declarator
1904/// member-declarator-list ',' member-declarator
1905///
1906/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001907/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001908/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00001909/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001910/// identifier[opt] ':' constant-expression
1911///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001912/// virt-specifier-seq:
1913/// virt-specifier
1914/// virt-specifier-seq virt-specifier
1915///
1916/// virt-specifier:
1917/// override
1918/// final
David Majnemera5433082013-10-18 00:33:31 +00001919/// [MS] sealed
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001920///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001921/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001922/// '= 0'
1923///
1924/// constant-initializer:
1925/// '=' constant-expression
1926///
Douglas Gregor3447e762009-08-20 22:52:58 +00001927void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00001928 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00001929 const ParsedTemplateInfo &TemplateInfo,
1930 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00001931 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001932 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00001933 Diag(Tok, diag::err_at_defs_cxx);
1934 else
1935 Diag(Tok, diag::err_at_in_class);
Richard Smithda35e962013-11-09 04:52:51 +00001936
Douglas Gregor23c84762011-04-14 17:21:19 +00001937 ConsumeToken();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001938 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregor23c84762011-04-14 17:21:19 +00001939 return;
1940 }
Richard Smithda35e962013-11-09 04:52:51 +00001941
John McCalla0097262009-12-11 02:10:03 +00001942 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00001943 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00001944 if (!TemplateInfo.Kind &&
Richard Smith45855df2012-05-09 08:23:23 +00001945 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1946 if (TryAnnotateCXXScopeToken())
1947 MalformedTypeSpec = true;
1948
1949 bool isAccessDecl;
1950 if (Tok.isNot(tok::annot_cxxscope))
1951 isAccessDecl = false;
1952 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00001953 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1954 else
1955 isAccessDecl = NextToken().is(tok::kw_operator);
1956
1957 if (isAccessDecl) {
1958 // Collect the scope specifier token we annotated earlier.
1959 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00001960 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1961 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00001962
1963 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001964 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00001965 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001966 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1967 TemplateKWLoc, Name)) {
John McCalla0097262009-12-11 02:10:03 +00001968 SkipUntil(tok::semi);
1969 return;
1970 }
1971
1972 // TODO: recover from mistakenly-qualified operator declarations.
Alp Toker383d2c42014-01-01 03:08:43 +00001973 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
1974 "access declaration")) {
1975 SkipUntil(tok::semi);
John McCalla0097262009-12-11 02:10:03 +00001976 return;
Alp Toker383d2c42014-01-01 03:08:43 +00001977 }
John McCalla0097262009-12-11 02:10:03 +00001978
Douglas Gregor0be31a22010-07-02 17:43:08 +00001979 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001980 /* HasUsingKeyword */ false,
1981 SourceLocation(),
John McCalla0097262009-12-11 02:10:03 +00001982 SS, Name,
1983 /* AttrList */ 0,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001984 /* HasTypenameKeyword */ false,
John McCalla0097262009-12-11 02:10:03 +00001985 SourceLocation());
1986 return;
1987 }
1988 }
1989
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001990 // static_assert-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001991 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001992 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00001993 SourceLocation DeclEnd;
1994 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001995 return;
1996 }
Mike Stump11289f42009-09-09 15:08:12 +00001997
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001998 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00001999 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00002000 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00002001 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00002002 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002003 AS, AccessAttrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002004 return;
2005 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002006
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002007 // Handle: member-declaration ::= '__extension__' member-declaration
2008 if (Tok.is(tok::kw___extension__)) {
2009 // __extension__ silences extension warnings in the subexpression.
2010 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2011 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002012 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2013 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002014 }
Douglas Gregorfec52632009-06-20 00:51:54 +00002015
Chris Lattnercf251412010-02-02 01:23:29 +00002016 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
2017 // is a bitfield.
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002018 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002019
John McCall084e83d2011-03-24 11:26:52 +00002020 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00002021 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002022 // Optional C++11 attribute-specifier
2023 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00002024 // We need to keep these attributes for future diagnostic
2025 // before they are taken over by declaration specifier.
2026 FnAttrs.addAll(attrs.getList());
2027 FnAttrs.Range = attrs.Range;
2028
John McCall53fa7142010-12-24 02:08:15 +00002029 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002030
Douglas Gregorfec52632009-06-20 00:51:54 +00002031 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00002032 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002033
Douglas Gregorfec52632009-06-20 00:51:54 +00002034 // Eat 'using'.
2035 SourceLocation UsingLoc = ConsumeToken();
2036
2037 if (Tok.is(tok::kw_namespace)) {
2038 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002039 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002040 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00002041 SourceLocation DeclEnd;
Richard Smith3f1b5d02011-05-05 21:57:07 +00002042 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +00002043 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2044 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00002045 }
2046 return;
2047 }
2048
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002049 // Hold late-parsed attributes so we can attach a Decl to them later.
2050 LateParsedAttrList CommonLateParsedAttrs;
2051
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002052 // decl-specifier-seq:
2053 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00002054 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00002055 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00002056 if (MalformedTypeSpec)
2057 DS.SetTypeSpecError();
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002058 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2059 &CommonLateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002060
Richard Smith404dfb42013-11-19 22:47:36 +00002061 // If we had a free-standing type definition with a missing semicolon, we
2062 // may get this far before the problem becomes obvious.
2063 if (DS.hasTagDefinition() &&
2064 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2065 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2066 &CommonLateParsedAttrs))
2067 return;
2068
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002069 MultiTemplateParamsArg TemplateParams(
John McCall11083da2009-09-16 22:47:08 +00002070 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2071 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2072
Alp Toker35d87032013-12-30 23:29:50 +00002073 if (TryConsumeToken(tok::semi)) {
Michael Handdc016d2012-11-28 23:17:40 +00002074 if (DS.isFriendSpecified())
2075 ProhibitAttributes(FnAttrs);
2076
John McCall48871652010-08-21 09:40:31 +00002077 Decl *TheDecl =
Chandler Carruth7c9856d2011-05-03 18:35:10 +00002078 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCall796c2a52010-07-16 08:13:16 +00002079 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00002080 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002081 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002082
John McCall28a6aea2009-11-04 02:18:39 +00002083 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002084 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002085
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002086 // Hold late-parsed attributes so we can attach a Decl to them later.
2087 LateParsedAttrList LateParsedAttrs;
2088
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002089 SourceLocation EqualLoc;
2090 bool HasInitializer = false;
2091 ExprResult Init;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002092 if (Tok.isNot(tok::colon)) {
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002093 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2094 ColonProtectionRAIIObject X(*this);
2095
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002096 // Parse the first declarator.
2097 ParseDeclarator(DeclaratorInfo);
Richard Smith2331bbf2012-05-02 22:22:32 +00002098 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +00002099 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002100 // If so, skip until the semi-colon or a }.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002101 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Alp Toker97650562014-01-10 11:19:30 +00002102 TryConsumeToken(tok::semi);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002103 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002104 }
2105
Richard Smith89645bc2013-01-02 12:01:23 +00002106 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber24b2a822011-01-28 06:07:34 +00002107
John Thompson5bc5cbe2009-11-25 22:58:06 +00002108 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002109 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson5bc5cbe2009-11-25 22:58:06 +00002110
Francois Pichet3abc9b82011-05-11 02:14:46 +00002111 // MSVC permits pure specifier on inline functions declared at class scope.
2112 // Hence check for =0 before checking for function definition.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002113 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet3abc9b82011-05-11 02:14:46 +00002114 DeclaratorInfo.isFunctionDeclarator() &&
2115 NextToken().is(tok::numeric_constant)) {
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002116 EqualLoc = ConsumeToken();
Francois Pichet3abc9b82011-05-11 02:14:46 +00002117 Init = ParseInitializer();
2118 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002119 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002120 else
2121 HasInitializer = true;
Francois Pichet3abc9b82011-05-11 02:14:46 +00002122 }
2123
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002124 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002125 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002126 //
2127 // In C++11, a non-function declarator followed by an open brace is a
2128 // braced-init-list for an in-class member initialization, not an
2129 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002130 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002131 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002132 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002133 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002134 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002135 } else if (Tok.is(tok::equal)) {
2136 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002137 if (KW.is(tok::kw_default))
2138 DefinitionKind = FDK_Defaulted;
2139 else if (KW.is(tok::kw_delete))
2140 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002141 }
2142 }
2143
Michael Handdc016d2012-11-28 23:17:40 +00002144 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2145 // to a friend declaration, that declaration shall be a definition.
2146 if (DeclaratorInfo.isFunctionDeclarator() &&
2147 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2148 // Diagnose attributes that appear before decl specifier:
2149 // [[]] friend int foo();
2150 ProhibitAttributes(FnAttrs);
2151 }
2152
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002153 if (DefinitionKind) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002154 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002155 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002156 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002157 SkipUntil(tok::r_brace);
Michael Handdc016d2012-11-28 23:17:40 +00002158
Douglas Gregor8a4db832011-01-19 16:41:58 +00002159 // Consume the optional ';'
Alp Toker35d87032013-12-30 23:29:50 +00002160 TryConsumeToken(tok::semi);
2161
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002162 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002163 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002164
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002165 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002166 Diag(DeclaratorInfo.getIdentifierLoc(),
2167 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002168
Richard Smith2603b092012-11-15 22:54:20 +00002169 // Recover by treating the 'typedef' as spurious.
2170 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002171 }
2172
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002173 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002174 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002175 VS, DefinitionKind, Init);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002176
David Majnemer23252a32013-08-01 04:22:55 +00002177 if (FunDecl) {
2178 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2179 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2180 }
2181 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2182 LateParsedAttrs[i]->addDecl(FunDecl);
2183 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002184 }
2185 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002186
2187 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002188 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002189 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002190
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002191 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002192 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002193 }
2194
2195 // member-declarator-list:
2196 // member-declarator
2197 // member-declarator-list ',' member-declarator
2198
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002199 SmallVector<Decl *, 8> DeclsInGroup;
John McCalldadc5752010-08-24 06:29:42 +00002200 ExprResult BitfieldSize;
Richard Smithc8a79032012-01-09 22:31:44 +00002201 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002202
2203 while (1) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002204 // member-declarator:
2205 // declarator pure-specifier[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00002206 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002207 // identifier[opt] ':' constant-expression
Alp Toker97650562014-01-10 11:19:30 +00002208 if (TryConsumeToken(tok::colon)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002209 BitfieldSize = ParseConstantExpression();
2210 if (BitfieldSize.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002211 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002212 }
Mike Stump11289f42009-09-09 15:08:12 +00002213
Chris Lattnerf3d3b362010-06-13 05:34:18 +00002214 // If a simple-asm-expr is present, parse it.
2215 if (Tok.is(tok::kw_asm)) {
2216 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00002217 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnerf3d3b362010-06-13 05:34:18 +00002218 if (AsmLabel.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002219 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Chris Lattnerf3d3b362010-06-13 05:34:18 +00002220
2221 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2222 DeclaratorInfo.SetRangeEnd(Loc);
2223 }
2224
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002225 // If attributes exist after the declarator, parse them.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002226 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002227
Richard Smith938f40b2011-06-11 17:19:42 +00002228 // FIXME: When g++ adds support for this, we'll need to check whether it
2229 // goes before or after the GNU attributes and __asm__.
Richard Smith89645bc2013-01-02 12:01:23 +00002230 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith938f40b2011-06-11 17:19:42 +00002231
Richard Smith2b013182012-06-10 03:12:00 +00002232 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002233 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith938f40b2011-06-11 17:19:42 +00002234 if (BitfieldSize.get()) {
2235 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002236 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith938f40b2011-06-11 17:19:42 +00002237 } else {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002238 HasInitializer = true;
Richard Smith2b013182012-06-10 03:12:00 +00002239 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2240 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smith2b013182012-06-10 03:12:00 +00002241 != DeclSpec::SCS_typedef)
2242 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002243 }
2244 }
2245
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002246 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002247 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002248 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002249
Rafael Espindola0a67e2f2013-01-08 20:44:06 +00002250 NamedDecl *ThisDecl = 0;
John McCall07e91c02009-08-06 02:15:43 +00002251 if (DS.isFriendSpecified()) {
Michael Handdc016d2012-11-28 23:17:40 +00002252 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2253 // to a friend declaration, that declaration shall be a definition.
2254 //
2255 // Diagnose attributes appear after friend member function declarator:
2256 // foo [[]] ();
2257 SmallVector<SourceRange, 4> Ranges;
2258 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2259 if (!Ranges.empty()) {
Craig Topper2341c0d2013-07-04 03:08:24 +00002260 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
Michael Handdc016d2012-11-28 23:17:40 +00002261 E = Ranges.end(); I != E; ++I) {
2262 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2263 << *I;
2264 }
2265 }
2266
John McCall2f212b32009-09-11 21:02:39 +00002267 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor0be31a22010-07-02 17:43:08 +00002268 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002269 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002270 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002271 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002272 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002273 TemplateParams,
John McCall07e91c02009-08-06 02:15:43 +00002274 BitfieldSize.release(),
Richard Smith2b013182012-06-10 03:12:00 +00002275 VS, HasInClassInit);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002276
2277 if (VarTemplateDecl *VT =
2278 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : 0)
2279 // Re-direct this decl to refer to the templated decl so that we can
2280 // initialize it.
2281 ThisDecl = VT->getTemplatedDecl();
2282
David Majnemer23252a32013-08-01 04:22:55 +00002283 if (ThisDecl && AccessAttrs)
Richard Smithf8a75c32013-08-29 00:47:48 +00002284 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00002285 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002286
Douglas Gregor728d00b2011-10-10 14:49:18 +00002287 // Handle the initializer.
David Blaikie35506f82013-01-30 01:22:18 +00002288 if (HasInClassInit != ICIS_NoInit &&
2289 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2290 DeclSpec::SCS_static) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002291 // The initializer was deferred; parse it and cache the tokens.
David Majnemer23252a32013-08-01 04:22:55 +00002292 Diag(Tok, getLangOpts().CPlusPlus11
2293 ? diag::warn_cxx98_compat_nonstatic_member_init
2294 : diag::ext_nonstatic_member_init);
Richard Smith5d164bc2011-10-15 05:09:34 +00002295
Richard Smith938f40b2011-06-11 17:19:42 +00002296 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002297 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2298 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002299 //
2300 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002301 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002302 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002303 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemer23252a32013-08-01 04:22:55 +00002304
2305 // Avoid later warnings about a class member of incomplete type.
David Blaikiecdd91db2012-02-14 09:00:46 +00002306 if (ThisDecl)
David Blaikiecdd91db2012-02-14 09:00:46 +00002307 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002308 } else
2309 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002310 } else if (HasInitializer) {
2311 // Normal initializer.
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002312 if (!Init.isUsable())
David Majnemer23252a32013-08-01 04:22:55 +00002313 Init = ParseCXXMemberInitializer(
2314 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2315
Douglas Gregor728d00b2011-10-10 14:49:18 +00002316 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002317 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002318 else if (ThisDecl)
Sebastian Redleef474c2012-02-22 10:50:08 +00002319 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smith74aeef52013-04-26 16:15:35 +00002320 DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002321 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor728d00b2011-10-10 14:49:18 +00002322 // No initializer.
Richard Smith74aeef52013-04-26 16:15:35 +00002323 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002324
Douglas Gregor728d00b2011-10-10 14:49:18 +00002325 if (ThisDecl) {
David Majnemer23252a32013-08-01 04:22:55 +00002326 if (!ThisDecl->isInvalidDecl()) {
2327 // Set the Decl for any late parsed attributes
2328 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2329 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2330
2331 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2332 LateParsedAttrs[i]->addDecl(ThisDecl);
2333 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002334 Actions.FinalizeDeclaration(ThisDecl);
2335 DeclsInGroup.push_back(ThisDecl);
David Majnemer23252a32013-08-01 04:22:55 +00002336
2337 if (DeclaratorInfo.isFunctionDeclarator() &&
2338 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2339 DeclSpec::SCS_typedef)
2340 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002341 }
David Majnemer23252a32013-08-01 04:22:55 +00002342 LateParsedAttrs.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002343
2344 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002345
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002346 // If we don't have a comma, it is either the end of the list (a ';')
2347 // or an error, bail out.
Alp Toker094e5212014-01-05 03:27:11 +00002348 SourceLocation CommaLoc;
2349 if (!TryConsumeToken(tok::comma, CommaLoc))
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002350 break;
Mike Stump11289f42009-09-09 15:08:12 +00002351
Richard Smithc8a79032012-01-09 22:31:44 +00002352 if (Tok.isAtStartOfLine() &&
2353 !MightBeDeclarator(Declarator::MemberContext)) {
2354 // This comma was followed by a line-break and something which can't be
2355 // the start of a declarator. The comma was probably a typo for a
2356 // semicolon.
2357 Diag(CommaLoc, diag::err_expected_semi_declaration)
2358 << FixItHint::CreateReplacement(CommaLoc, ";");
2359 ExpectSemi = false;
2360 break;
2361 }
Mike Stump11289f42009-09-09 15:08:12 +00002362
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002363 // Parse the next declarator.
2364 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002365 VS.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002366 BitfieldSize = true;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002367 Init = true;
2368 HasInitializer = false;
Richard Smith8d06f422012-01-12 23:53:29 +00002369 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002370
Bill Wendling44426052012-12-20 19:22:21 +00002371 // Attributes are only allowed on the second declarator.
John McCall53fa7142010-12-24 02:08:15 +00002372 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002373
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002374 if (Tok.isNot(tok::colon))
2375 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002376 }
2377
Richard Smithc8a79032012-01-09 22:31:44 +00002378 if (ExpectSemi &&
2379 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002380 // Skip to end of block or statement.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002381 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002382 // If we stopped at a ';', eat it.
Alp Toker35d87032013-12-30 23:29:50 +00002383 TryConsumeToken(tok::semi);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002384 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002385 }
2386
Rafael Espindolaab417692013-07-09 12:05:01 +00002387 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002388}
2389
Richard Smith938f40b2011-06-11 17:19:42 +00002390/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2391/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2392/// function definition. The location of the '=', if any, will be placed in
2393/// EqualLoc.
2394///
2395/// pure-specifier:
2396/// '= 0'
Sebastian Redleef474c2012-02-22 10:50:08 +00002397///
Richard Smith938f40b2011-06-11 17:19:42 +00002398/// brace-or-equal-initializer:
2399/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002400/// braced-init-list
2401///
Richard Smith938f40b2011-06-11 17:19:42 +00002402/// initializer-clause:
2403/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002404/// braced-init-list
2405///
Richard Smithda35e962013-11-09 04:52:51 +00002406/// defaulted/deleted function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002407/// '=' 'default'
2408/// '=' 'delete'
2409///
2410/// Prior to C++0x, the assignment-expression in an initializer-clause must
2411/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002412ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002413 SourceLocation &EqualLoc) {
2414 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2415 && "Data member initializer not starting with '=' or '{'");
2416
Douglas Gregor926410d2012-02-21 02:22:07 +00002417 EnterExpressionEvaluationContext Context(Actions,
2418 Sema::PotentiallyEvaluated,
2419 D);
Alp Toker094e5212014-01-05 03:27:11 +00002420 if (TryConsumeToken(tok::equal, EqualLoc)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002421 if (Tok.is(tok::kw_delete)) {
2422 // In principle, an initializer of '= delete p;' is legal, but it will
2423 // never type-check. It's better to diagnose it as an ill-formed expression
2424 // than as an ill-formed deleted non-function member.
2425 // An initializer of '= delete p, foo' will never be parsed, because
2426 // a top-level comma always ends the initializer expression.
2427 const Token &Next = NextToken();
2428 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
Richard Smith34f30512013-11-23 04:06:09 +00002429 Next.is(tok::eof)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002430 if (IsFunction)
2431 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2432 << 1 /* delete */;
2433 else
2434 Diag(ConsumeToken(), diag::err_deleted_non_function);
2435 return ExprResult();
2436 }
2437 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002438 if (IsFunction)
2439 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2440 << 0 /* default */;
2441 else
2442 Diag(ConsumeToken(), diag::err_default_special_members);
2443 return ExprResult();
2444 }
2445
Sebastian Redleef474c2012-02-22 10:50:08 +00002446 }
2447 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002448}
2449
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002450/// ParseCXXMemberSpecification - Parse the class definition.
2451///
2452/// member-specification:
2453/// member-declaration member-specification[opt]
2454/// access-specifier ':' member-specification[opt]
2455///
Joao Matose9a3ed42012-08-31 22:18:20 +00002456void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00002457 SourceLocation AttrFixitLoc,
Richard Smith4c96e992013-02-19 23:47:15 +00002458 ParsedAttributesWithRange &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00002459 unsigned TagType, Decl *TagDecl) {
2460 assert((TagType == DeclSpec::TST_struct ||
2461 TagType == DeclSpec::TST_interface ||
2462 TagType == DeclSpec::TST_union ||
2463 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2464
John McCallfaf5fb42010-08-26 23:41:50 +00002465 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2466 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00002467
Douglas Gregoredf8f392010-01-16 20:52:59 +00002468 // Determine whether this is a non-nested class. Note that local
2469 // classes are *not* considered to be nested classes.
2470 bool NonNestedClass = true;
2471 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002472 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002473 if (S->isClassScope()) {
2474 // We're inside a class scope, so this is a nested class.
2475 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00002476
2477 // The Microsoft extension __interface does not permit nested classes.
2478 if (getCurrentClass().IsInterface) {
2479 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2480 << /*ErrorType=*/6
2481 << (isa<NamedDecl>(TagDecl)
2482 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2483 : "<anonymous>");
2484 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00002485 break;
2486 }
2487
2488 if ((S->getFlags() & Scope::FnScope)) {
2489 // If we're in a function or function template declared in the
2490 // body of a class, then this is a local class rather than a
2491 // nested class.
2492 const Scope *Parent = S->getParent();
2493 if (Parent->isTemplateParamScope())
2494 Parent = Parent->getParent();
2495 if (Parent->isClassScope())
2496 break;
2497 }
2498 }
2499 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002500
2501 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00002502 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002503
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002504 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00002505 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2506 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002507
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002508 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002509 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002510
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002511 SourceLocation FinalLoc;
David Majnemera5433082013-10-18 00:33:31 +00002512 bool IsFinalSpelledSealed = false;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002513
2514 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002515 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemera5433082013-10-18 00:33:31 +00002516 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2517 assert((Specifier == VirtSpecifiers::VS_Final ||
2518 Specifier == VirtSpecifiers::VS_Sealed) &&
2519 "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00002520 FinalLoc = ConsumeToken();
David Majnemera5433082013-10-18 00:33:31 +00002521 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002522
David Majnemera5433082013-10-18 00:33:31 +00002523 if (TagType == DeclSpec::TST_interface)
John McCalldb632ac2012-09-25 07:32:39 +00002524 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemera5433082013-10-18 00:33:31 +00002525 << VirtSpecifiers::getSpecifierName(Specifier);
2526 else if (Specifier == VirtSpecifiers::VS_Final)
2527 Diag(FinalLoc, getLangOpts().CPlusPlus11
2528 ? diag::warn_cxx98_compat_override_control_keyword
2529 : diag::ext_override_control_keyword)
2530 << VirtSpecifiers::getSpecifierName(Specifier);
2531 else if (Specifier == VirtSpecifiers::VS_Sealed)
2532 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Michael Han9407e502012-11-26 22:54:45 +00002533
Michael Han309af292013-01-07 16:57:11 +00002534 // Parse any C++11 attributes after 'final' keyword.
2535 // These attributes are not allowed to appear here,
2536 // and the only possible place for them to appertain
2537 // to the class would be between class-key and class-name.
Richard Smith4c96e992013-02-19 23:47:15 +00002538 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002539 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002540
John McCall2d814c32009-12-19 21:48:58 +00002541 if (Tok.is(tok::colon)) {
2542 ParseBaseClause(TagDecl);
2543
2544 if (!Tok.is(tok::l_brace)) {
2545 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCall2ff380a2010-03-17 00:38:33 +00002546
2547 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002548 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002549 return;
2550 }
2551 }
2552
2553 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002554 BalancedDelimiterTracker T(*this, tok::l_brace);
2555 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00002556
John McCall08bede42010-05-28 08:11:17 +00002557 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00002558 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemera5433082013-10-18 00:33:31 +00002559 IsFinalSpelledSealed,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002560 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00002561
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002562 // C++ 11p3: Members of a class defined with the keyword class are private
2563 // by default. Members of a class defined with the keywords struct or union
2564 // are public by default.
2565 AccessSpecifier CurAS;
2566 if (TagType == DeclSpec::TST_class)
2567 CurAS = AS_private;
2568 else
2569 CurAS = AS_public;
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002570 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002571
Douglas Gregor9377c822010-06-21 22:31:09 +00002572 if (TagDecl) {
2573 // While we still have something to read, read the member-declarations.
Richard Smith34f30512013-11-23 04:06:09 +00002574 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Douglas Gregor9377c822010-06-21 22:31:09 +00002575 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002576
David Blaikiebbafb8a2012-03-11 07:00:24 +00002577 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet8f981d52011-05-25 10:19:49 +00002578 Tok.is(tok::kw___if_not_exists))) {
2579 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2580 continue;
2581 }
2582
Douglas Gregor9377c822010-06-21 22:31:09 +00002583 // Check for extraneous top-level semicolon.
2584 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002585 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor9377c822010-06-21 22:31:09 +00002586 continue;
2587 }
2588
Eli Friedmanec52f922012-02-23 23:47:16 +00002589 if (Tok.is(tok::annot_pragma_vis)) {
2590 HandlePragmaVisibility();
2591 continue;
2592 }
2593
2594 if (Tok.is(tok::annot_pragma_pack)) {
2595 HandlePragmaPack();
2596 continue;
2597 }
2598
Argyrios Kyrtzidis5c2021b2012-10-12 17:39:59 +00002599 if (Tok.is(tok::annot_pragma_align)) {
2600 HandlePragmaAlign();
2601 continue;
2602 }
2603
Alexey Bataeva769e072013-03-22 06:34:35 +00002604 if (Tok.is(tok::annot_pragma_openmp)) {
2605 ParseOpenMPDeclarativeDirective();
2606 continue;
2607 }
2608
Richard Smithda35e962013-11-09 04:52:51 +00002609 // If we see a namespace here, a close brace was missing somewhere.
2610 if (Tok.is(tok::kw_namespace)) {
Richard Smith2ac43ad2013-11-15 23:00:02 +00002611 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
Richard Smithda35e962013-11-09 04:52:51 +00002612 break;
2613 }
2614
Douglas Gregor9377c822010-06-21 22:31:09 +00002615 AccessSpecifier AS = getAccessSpecifierIfPresent();
2616 if (AS != AS_none) {
2617 // Current token is a C++ access specifier.
2618 CurAS = AS;
2619 SourceLocation ASLoc = Tok.getLocation();
David Blaikieeba32c22011-10-13 06:08:43 +00002620 unsigned TokLength = Tok.getLength();
Douglas Gregor9377c822010-06-21 22:31:09 +00002621 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002622 AccessAttrs.clear();
2623 MaybeParseGNUAttributes(AccessAttrs);
2624
David Blaikieeba32c22011-10-13 06:08:43 +00002625 SourceLocation EndLoc;
Alp Toker35d87032013-12-30 23:29:50 +00002626 if (TryConsumeToken(tok::colon, EndLoc)) {
2627 } else if (TryConsumeToken(tok::semi, EndLoc)) {
2628 Diag(EndLoc, diag::err_expected)
2629 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002630 } else {
2631 EndLoc = ASLoc.getLocWithOffset(TokLength);
Alp Toker35d87032013-12-30 23:29:50 +00002632 Diag(EndLoc, diag::err_expected)
2633 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002634 }
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002635
John McCalldb632ac2012-09-25 07:32:39 +00002636 // The Microsoft extension __interface does not permit non-public
2637 // access specifiers.
2638 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2639 Diag(ASLoc, diag::err_access_specifier_interface)
2640 << (CurAS == AS_protected);
2641 }
2642
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002643 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2644 AccessAttrs.getList())) {
2645 // found another attribute than only annotations
2646 AccessAttrs.clear();
2647 }
2648
Douglas Gregor9377c822010-06-21 22:31:09 +00002649 continue;
2650 }
2651
Douglas Gregor9377c822010-06-21 22:31:09 +00002652 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002653 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002654 }
2655
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002656 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00002657 } else {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002658 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002659 }
Mike Stump11289f42009-09-09 15:08:12 +00002660
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002661 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002662 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002663 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002664
John McCall08bede42010-05-28 08:11:17 +00002665 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002666 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002667 T.getOpenLocation(),
2668 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002669 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002670
Douglas Gregor433e0532012-04-16 18:27:27 +00002671 // C++11 [class.mem]p2:
2672 // Within the class member-specification, the class is regarded as complete
Richard Smith2331bbf2012-05-02 22:22:32 +00002673 // within function bodies, default arguments, and
Douglas Gregor433e0532012-04-16 18:27:27 +00002674 // brace-or-equal-initializers for non-static data members (including such
2675 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00002676 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002677 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00002678 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002679 // declarations and the lexed inline method definitions, along with any
2680 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00002681 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002682 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002683 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00002684
2685 // We've finished with all pending member declarations.
2686 Actions.ActOnFinishCXXMemberDecls();
2687
Richard Smith938f40b2011-06-11 17:19:42 +00002688 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002689 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00002690 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002691 }
2692
John McCall08bede42010-05-28 08:11:17 +00002693 if (TagDecl)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002694 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2695 T.getCloseLocation());
John McCall2ff380a2010-03-17 00:38:33 +00002696
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002697 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002698 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002699 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002700}
Douglas Gregore8381c02008-11-05 04:29:56 +00002701
Richard Smith2ac43ad2013-11-15 23:00:02 +00002702void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithda35e962013-11-09 04:52:51 +00002703 assert(Tok.is(tok::kw_namespace));
2704
2705 // FIXME: Suggest where the close brace should have gone by looking
2706 // at indentation changes within the definition body.
Richard Smith2ac43ad2013-11-15 23:00:02 +00002707 Diag(D->getLocation(),
2708 diag::err_missing_end_of_definition) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002709 Diag(Tok.getLocation(),
Richard Smith2ac43ad2013-11-15 23:00:02 +00002710 diag::note_missing_end_of_definition_before) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002711
2712 // Push '};' onto the token stream to recover.
2713 PP.EnterToken(Tok);
2714
2715 Tok.startToken();
2716 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
2717 Tok.setKind(tok::semi);
2718 PP.EnterToken(Tok);
2719
2720 Tok.setKind(tok::r_brace);
2721}
2722
Douglas Gregore8381c02008-11-05 04:29:56 +00002723/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2724/// which explicitly initializes the members or base classes of a
2725/// class (C++ [class.base.init]). For example, the three initializers
2726/// after the ':' in the Derived constructor below:
2727///
2728/// @code
2729/// class Base { };
2730/// class Derived : Base {
2731/// int x;
2732/// float f;
2733/// public:
2734/// Derived(float f) : Base(), x(17), f(f) { }
2735/// };
2736/// @endcode
2737///
Mike Stump11289f42009-09-09 15:08:12 +00002738/// [C++] ctor-initializer:
2739/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00002740///
Mike Stump11289f42009-09-09 15:08:12 +00002741/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00002742/// mem-initializer ...[opt]
2743/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00002744void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00002745 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2746
John Wiegley1c0675e2011-04-28 01:08:34 +00002747 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2748 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00002749 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002750
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002751 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002752 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002753
Douglas Gregore8381c02008-11-05 04:29:56 +00002754 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002755 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00002756 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2757 MemInitializers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002758 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002759 } else {
2760 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2761 if (!MemInit.isInvalid())
2762 MemInitializers.push_back(MemInit.get());
2763 else
2764 AnyErrors = true;
2765 }
2766
Douglas Gregore8381c02008-11-05 04:29:56 +00002767 if (Tok.is(tok::comma))
2768 ConsumeToken();
2769 else if (Tok.is(tok::l_brace))
2770 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00002771 // If the next token looks like a base or member initializer, assume that
2772 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00002773 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2774 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2775 Diag(Loc, diag::err_ctor_init_missing_comma)
2776 << FixItHint::CreateInsertion(Loc, ", ");
2777 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00002778 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alp Tokerec543272013-12-24 09:48:30 +00002779 Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
2780 << tok::comma;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002781 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregore8381c02008-11-05 04:29:56 +00002782 break;
2783 }
2784 } while (true);
2785
David Blaikie3fc2f912013-01-17 05:26:25 +00002786 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002787 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00002788}
2789
2790/// ParseMemInitializer - Parse a C++ member initializer, which is
2791/// part of a constructor initializer that explicitly initializes one
2792/// member or base class (C++ [class.base.init]). See
2793/// ParseConstructorInitializer for an example.
2794///
2795/// [C++] mem-initializer:
2796/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002797/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00002798///
Douglas Gregore8381c02008-11-05 04:29:56 +00002799/// [C++] mem-initializer-id:
2800/// '::'[opt] nested-name-specifier[opt] class-name
2801/// identifier
John McCall48871652010-08-21 09:40:31 +00002802Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00002803 // parse '::'[opt] nested-name-specifier[opt]
2804 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002805 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00002806 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002807 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002808 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00002809 if (TemplateId->Kind == TNK_Type_template ||
2810 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00002811 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002812 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00002813 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002814 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002815 }
David Blaikie186a8892012-01-24 06:03:59 +00002816 // Uses of decltype will already have been converted to annot_decltype by
2817 // ParseOptionalCXXScopeSpecifier at this point.
2818 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2819 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002820 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00002821 return true;
2822 }
Mike Stump11289f42009-09-09 15:08:12 +00002823
David Blaikie186a8892012-01-24 06:03:59 +00002824 IdentifierInfo *II = 0;
2825 DeclSpec DS(AttrFactory);
2826 SourceLocation IdLoc = Tok.getLocation();
2827 if (Tok.is(tok::annot_decltype)) {
2828 // Get the decltype expression, if there is one.
2829 ParseDecltypeSpecifier(DS);
2830 } else {
2831 if (Tok.is(tok::identifier))
2832 // Get the identifier. This may be a member name or a class name,
2833 // but we'll let the semantic analysis determine which it is.
2834 II = Tok.getIdentifierInfo();
2835 ConsumeToken();
2836 }
2837
Douglas Gregore8381c02008-11-05 04:29:56 +00002838
2839 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002840 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002841 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2842
Sebastian Redla74948d2011-09-24 17:48:25 +00002843 ExprResult InitList = ParseBraceInitializer();
2844 if (InitList.isInvalid())
2845 return true;
2846
2847 SourceLocation EllipsisLoc;
Alp Toker094e5212014-01-05 03:27:11 +00002848 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redla74948d2011-09-24 17:48:25 +00002849
2850 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002851 TemplateTypeTy, DS, IdLoc,
2852 InitList.take(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00002853 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002854 BalancedDelimiterTracker T(*this, tok::l_paren);
2855 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00002856
Sebastian Redl3da34892011-06-05 12:23:16 +00002857 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00002858 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00002859 CommaLocsTy CommaLocs;
2860 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002861 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00002862 return true;
2863 }
2864
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002865 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00002866
2867 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00002868 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00002869
2870 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002871 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenko139474d2013-05-09 23:51:52 +00002872 T.getOpenLocation(), ArgExprs,
2873 T.getCloseLocation(), EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00002874 }
2875
Alp Tokerec543272013-12-24 09:48:30 +00002876 if (getLangOpts().CPlusPlus11)
2877 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
2878 else
2879 return Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregore8381c02008-11-05 04:29:56 +00002880}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002881
Sebastian Redl965b0e32011-03-05 14:45:16 +00002882/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002883///
Douglas Gregor356513d2008-12-01 18:00:20 +00002884/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00002885/// dynamic-exception-specification
2886/// noexcept-specification
2887///
2888/// noexcept-specification:
2889/// 'noexcept'
2890/// 'noexcept' '(' constant-expression ')'
2891ExceptionSpecificationType
Richard Smith2331bbf2012-05-02 22:22:32 +00002892Parser::tryParseExceptionSpecification(
Douglas Gregor433e0532012-04-16 18:27:27 +00002893 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002894 SmallVectorImpl<ParsedType> &DynamicExceptions,
2895 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00002896 ExprResult &NoexceptExpr) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002897 ExceptionSpecificationType Result = EST_None;
2898
2899 // See if there's a dynamic specification.
2900 if (Tok.is(tok::kw_throw)) {
2901 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2902 DynamicExceptions,
2903 DynamicExceptionRanges);
2904 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2905 "Produced different number of exception types and ranges.");
2906 }
2907
2908 // If there's no noexcept specification, we're done.
2909 if (Tok.isNot(tok::kw_noexcept))
2910 return Result;
2911
Richard Smithb15c11c2011-10-17 23:06:20 +00002912 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2913
Sebastian Redl965b0e32011-03-05 14:45:16 +00002914 // If we already had a dynamic specification, parse the noexcept for,
2915 // recovery, but emit a diagnostic and don't store the results.
2916 SourceRange NoexceptRange;
2917 ExceptionSpecificationType NoexceptType = EST_None;
2918
2919 SourceLocation KeywordLoc = ConsumeToken();
2920 if (Tok.is(tok::l_paren)) {
2921 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002922 BalancedDelimiterTracker T(*this, tok::l_paren);
2923 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00002924 NoexceptType = EST_ComputedNoexcept;
2925 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002926 // The argument must be contextually convertible to bool. We use
2927 // ActOnBooleanCondition for this purpose.
2928 if (!NoexceptExpr.isInvalid())
2929 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2930 NoexceptExpr.get());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002931 T.consumeClose();
2932 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl965b0e32011-03-05 14:45:16 +00002933 } else {
2934 // There is no argument.
2935 NoexceptType = EST_BasicNoexcept;
2936 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2937 }
2938
2939 if (Result == EST_None) {
2940 SpecificationRange = NoexceptRange;
2941 Result = NoexceptType;
2942
2943 // If there's a dynamic specification after a noexcept specification,
2944 // parse that and ignore the results.
2945 if (Tok.is(tok::kw_throw)) {
2946 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2947 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2948 DynamicExceptionRanges);
2949 }
2950 } else {
2951 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2952 }
2953
2954 return Result;
2955}
2956
Richard Smith8ca78a12013-06-13 02:02:51 +00002957static void diagnoseDynamicExceptionSpecification(
2958 Parser &P, const SourceRange &Range, bool IsNoexcept) {
2959 if (P.getLangOpts().CPlusPlus11) {
2960 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
2961 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
2962 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
2963 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
2964 }
2965}
2966
Sebastian Redl965b0e32011-03-05 14:45:16 +00002967/// ParseDynamicExceptionSpecification - Parse a C++
2968/// dynamic-exception-specification (C++ [except.spec]).
2969///
2970/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00002971/// 'throw' '(' type-id-list [opt] ')'
2972/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00002973///
Douglas Gregor356513d2008-12-01 18:00:20 +00002974/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00002975/// type-id ... [opt]
2976/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002977///
Sebastian Redl965b0e32011-03-05 14:45:16 +00002978ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2979 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002980 SmallVectorImpl<ParsedType> &Exceptions,
2981 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002982 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00002983
Sebastian Redl965b0e32011-03-05 14:45:16 +00002984 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002985 BalancedDelimiterTracker T(*this, tok::l_paren);
2986 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002987 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2988 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002989 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002990 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002991
Douglas Gregor356513d2008-12-01 18:00:20 +00002992 // Parse throw(...), a Microsoft extension that means "this function
2993 // can throw anything".
2994 if (Tok.is(tok::ellipsis)) {
2995 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00002996 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00002997 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002998 T.consumeClose();
2999 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003000 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003001 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00003002 }
3003
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003004 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00003005 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003006 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00003007 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00003008
Douglas Gregor830837d2010-12-20 23:57:46 +00003009 if (Tok.is(tok::ellipsis)) {
3010 // C++0x [temp.variadic]p5:
3011 // - In a dynamic-exception-specification (15.4); the pattern is a
3012 // type-id.
3013 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003014 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00003015 if (!Res.isInvalid())
3016 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3017 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003018
Sebastian Redld6434562009-05-29 18:02:33 +00003019 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003020 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00003021 Ranges.push_back(Range);
3022 }
Alp Toker97650562014-01-10 11:19:30 +00003023
3024 if (!TryConsumeToken(tok::comma))
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003025 break;
3026 }
3027
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003028 T.consumeClose();
3029 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003030 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3031 Exceptions.empty());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003032 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003033}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003034
Douglas Gregor7fb25412010-10-01 18:44:50 +00003035/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3036/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003037TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00003038 assert(Tok.is(tok::arrow) && "expected arrow");
3039
3040 ConsumeToken();
3041
Richard Smithbfdb1082012-03-12 08:56:40 +00003042 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00003043}
3044
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003045/// \brief We have just started parsing the definition of a new class,
3046/// so push that class onto our stack of classes that is currently
3047/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00003048Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00003049Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3050 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003051 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003052 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00003053 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00003054 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003055}
3056
3057/// \brief Deallocate the given parsed class and all of its nested
3058/// classes.
3059void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00003060 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3061 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003062 delete Class;
3063}
3064
3065/// \brief Pop the top class of the stack of classes that are
3066/// currently being parsed.
3067///
3068/// This routine should be called when we have finished parsing the
3069/// definition of a class, but have not yet popped the Scope
3070/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00003071void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003072 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00003073
John McCallc1465822011-02-14 07:13:47 +00003074 Actions.PopParsingClass(state);
3075
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003076 ParsingClass *Victim = ClassStack.top();
3077 ClassStack.pop();
3078 if (Victim->TopLevelClass) {
3079 // Deallocate all of the nested classes of this class,
3080 // recursively: we don't need to keep any of this information.
3081 DeallocateParsedClasses(Victim);
3082 return;
Mike Stump11289f42009-09-09 15:08:12 +00003083 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003084 assert(!ClassStack.empty() && "Missing top-level class?");
3085
Douglas Gregorefc46952010-10-12 16:25:54 +00003086 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003087 // The victim is a nested class, but we will not need to perform
3088 // any processing after the definition of this class since it has
3089 // no members whose handling was delayed. Therefore, we can just
3090 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00003091 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003092 return;
3093 }
3094
3095 // This nested class has some members that will need to be processed
3096 // after the top-level class is completely defined. Therefore, add
3097 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003098 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00003099 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00003100 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003101}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003102
Richard Smith3dff2512012-04-10 03:25:07 +00003103/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3104///
3105/// \return the parsed identifier on success, and 0 if the next token is not an
3106/// attribute-token.
3107///
3108/// C++11 [dcl.attr.grammar]p3:
3109/// If a keyword or an alternative token that satisfies the syntactic
3110/// requirements of an identifier is contained in an attribute-token,
3111/// it is considered an identifier.
3112IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3113 switch (Tok.getKind()) {
3114 default:
3115 // Identifiers and keywords have identifier info attached.
3116 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3117 Loc = ConsumeToken();
3118 return II;
3119 }
3120 return 0;
3121
3122 case tok::ampamp: // 'and'
3123 case tok::pipe: // 'bitor'
3124 case tok::pipepipe: // 'or'
3125 case tok::caret: // 'xor'
3126 case tok::tilde: // 'compl'
3127 case tok::amp: // 'bitand'
3128 case tok::ampequal: // 'and_eq'
3129 case tok::pipeequal: // 'or_eq'
3130 case tok::caretequal: // 'xor_eq'
3131 case tok::exclaim: // 'not'
3132 case tok::exclaimequal: // 'not_eq'
3133 // Alternative tokens do not have identifier info, but their spelling
3134 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003135 SmallString<8> SpellingBuf;
Richard Smith3dff2512012-04-10 03:25:07 +00003136 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rosea7d03842013-02-08 22:30:41 +00003137 if (isLetter(Spelling[0])) {
Richard Smith3dff2512012-04-10 03:25:07 +00003138 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003139 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003140 }
3141 return 0;
3142 }
3143}
3144
Michael Han23214e52012-10-03 01:56:22 +00003145static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3146 IdentifierInfo *ScopeName) {
3147 switch (AttributeList::getKind(AttrName, ScopeName,
3148 AttributeList::AS_CXX11)) {
3149 case AttributeList::AT_CarriesDependency:
3150 case AttributeList::AT_FallThrough:
Richard Smith10876ef2013-01-17 01:30:42 +00003151 case AttributeList::AT_CXX11NoReturn: {
Michael Han23214e52012-10-03 01:56:22 +00003152 return true;
3153 }
3154
3155 default:
3156 return false;
3157 }
3158}
3159
Richard Smith3dff2512012-04-10 03:25:07 +00003160/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003161/// only parses standard attributes.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003162///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003163/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003164/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003165/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003166///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003167/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003168/// attribute[opt]
3169/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003170/// attribute '...'
3171/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003172///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003173/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003174/// attribute-token attribute-argument-clause[opt]
3175///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003176/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003177/// identifier
3178/// attribute-scoped-token
3179///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003180/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003181/// attribute-namespace '::' identifier
3182///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003183/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003184/// identifier
3185///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003186/// [C++11] attribute-argument-clause:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003187/// '(' balanced-token-seq ')'
3188///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003189/// [C++11] balanced-token-seq:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003190/// balanced-token
3191/// balanced-token-seq balanced-token
3192///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003193/// [C++11] balanced-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003194/// '(' balanced-token-seq ')'
3195/// '[' balanced-token-seq ']'
3196/// '{' balanced-token-seq '}'
3197/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smith3dff2512012-04-10 03:25:07 +00003198void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003199 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003200 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003201 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003202 ParseAlignmentSpecifier(attrs, endLoc);
3203 return;
3204 }
3205
Alexis Hunt96d5c762009-11-21 08:43:09 +00003206 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003207 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003208
Richard Smithf679b5b2011-10-14 20:48:27 +00003209 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3210
Alexis Hunt96d5c762009-11-21 08:43:09 +00003211 ConsumeBracket();
3212 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003213
Richard Smith10876ef2013-01-17 01:30:42 +00003214 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3215
Richard Smith3dff2512012-04-10 03:25:07 +00003216 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003217 // attribute not present
Alp Toker97650562014-01-10 11:19:30 +00003218 if (TryConsumeToken(tok::comma))
Alexis Hunt96d5c762009-11-21 08:43:09 +00003219 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003220
Richard Smith3dff2512012-04-10 03:25:07 +00003221 SourceLocation ScopeLoc, AttrLoc;
3222 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3223
3224 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3225 if (!AttrName)
3226 // Break out to the "expected ']'" diagnostic.
3227 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003228
Alexis Hunt96d5c762009-11-21 08:43:09 +00003229 // scoped attribute
Alp Toker97650562014-01-10 11:19:30 +00003230 if (TryConsumeToken(tok::coloncolon)) {
Richard Smith3dff2512012-04-10 03:25:07 +00003231 ScopeName = AttrName;
3232 ScopeLoc = AttrLoc;
3233
3234 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3235 if (!AttrName) {
Alp Tokerec543272013-12-24 09:48:30 +00003236 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003237 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003238 continue;
3239 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003240 }
3241
Michael Han23214e52012-10-03 01:56:22 +00003242 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003243 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003244
Richard Smith10876ef2013-01-17 01:30:42 +00003245 if (StandardAttr &&
3246 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3247 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3248 << AttrName << SourceRange(SeenAttrs[AttrName]);
3249
Michael Han23214e52012-10-03 01:56:22 +00003250 // Parse attribute arguments
3251 if (Tok.is(tok::l_paren)) {
3252 if (ScopeName && ScopeName->getName() == "gnu") {
3253 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +00003254 ScopeName, ScopeLoc, AttributeList::AS_CXX11, 0);
Michael Han23214e52012-10-03 01:56:22 +00003255 AttrParsed = true;
3256 } else {
3257 if (StandardAttr)
3258 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3259 << AttrName->getName();
3260
3261 // FIXME: handle other formats of c++11 attribute arguments
3262 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003263 SkipUntil(tok::r_paren);
Michael Han23214e52012-10-03 01:56:22 +00003264 }
3265 }
3266
3267 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003268 attrs.addNew(AttrName,
3269 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3270 AttrLoc),
Aaron Ballman00e99962013-08-31 01:11:41 +00003271 ScopeName, ScopeLoc, 0, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003272
Alp Toker97650562014-01-10 11:19:30 +00003273 if (TryConsumeToken(tok::ellipsis))
Michael Han23214e52012-10-03 01:56:22 +00003274 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3275 << AttrName->getName();
Alexis Hunt96d5c762009-11-21 08:43:09 +00003276 }
3277
Alp Toker383d2c42014-01-01 03:08:43 +00003278 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003279 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003280 if (endLoc)
3281 *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003282 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003283 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003284}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003285
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003286/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003287///
3288/// attribute-specifier-seq:
3289/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003290void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003291 SourceLocation *endLoc) {
Richard Smith4cabd042013-02-22 09:15:49 +00003292 assert(getLangOpts().CPlusPlus11);
3293
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003294 SourceLocation StartLoc = Tok.getLocation(), Loc;
3295 if (!endLoc)
3296 endLoc = &Loc;
3297
Douglas Gregor6f981002011-10-07 20:35:25 +00003298 do {
Richard Smith3dff2512012-04-10 03:25:07 +00003299 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003300 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003301
3302 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003303}
3304
Richard Smithc2c8bb82013-10-15 01:34:54 +00003305void Parser::DiagnoseAndSkipCXX11Attributes() {
3306 if (!isCXX11AttributeSpecifier())
3307 return;
3308
3309 // Start and end location of an attribute or an attribute list.
3310 SourceLocation StartLoc = Tok.getLocation();
3311 SourceLocation EndLoc;
3312
3313 do {
3314 if (Tok.is(tok::l_square)) {
3315 BalancedDelimiterTracker T(*this, tok::l_square);
3316 T.consumeOpen();
3317 T.skipToEnd();
3318 EndLoc = T.getCloseLocation();
3319 } else {
3320 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3321 ConsumeToken();
3322 BalancedDelimiterTracker T(*this, tok::l_paren);
3323 if (!T.consumeOpen())
3324 T.skipToEnd();
3325 EndLoc = T.getCloseLocation();
3326 }
3327 } while (isCXX11AttributeSpecifier());
3328
3329 if (EndLoc.isValid()) {
3330 SourceRange Range(StartLoc, EndLoc);
3331 Diag(StartLoc, diag::err_attributes_not_allowed)
3332 << Range;
3333 }
3334}
3335
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003336/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3337///
3338/// [MS] ms-attribute:
3339/// '[' token-seq ']'
3340///
3341/// [MS] ms-attribute-seq:
3342/// ms-attribute[opt]
3343/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00003344void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3345 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003346 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3347
3348 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003349 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003350 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003351 SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
John McCall53fa7142010-12-24 02:08:15 +00003352 if (endLoc) *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003353 ExpectAndConsume(tok::r_square);
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003354 }
3355}
Francois Pichet8f981d52011-05-25 10:19:49 +00003356
3357void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3358 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00003359 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00003360 if (ParseMicrosoftIfExistsCondition(Result))
3361 return;
3362
Douglas Gregor43edb322011-10-24 22:31:10 +00003363 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3364 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00003365 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet8f981d52011-05-25 10:19:49 +00003366 return;
3367 }
Francois Pichet8f981d52011-05-25 10:19:49 +00003368
Douglas Gregor43edb322011-10-24 22:31:10 +00003369 switch (Result.Behavior) {
3370 case IEB_Parse:
3371 // Parse the declarations below.
3372 break;
3373
3374 case IEB_Dependent:
3375 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3376 << Result.IsIfExists;
3377 // Fall through to skip.
3378
3379 case IEB_Skip:
3380 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00003381 return;
3382 }
3383
Richard Smith34f30512013-11-23 04:06:09 +00003384 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003385 // __if_exists, __if_not_exists can nest.
3386 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3387 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3388 continue;
3389 }
3390
3391 // Check for extraneous top-level semicolon.
3392 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003393 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00003394 continue;
3395 }
3396
3397 AccessSpecifier AS = getAccessSpecifierIfPresent();
3398 if (AS != AS_none) {
3399 // Current token is a C++ access specifier.
3400 CurAS = AS;
3401 SourceLocation ASLoc = Tok.getLocation();
3402 ConsumeToken();
3403 if (Tok.is(tok::colon))
3404 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3405 else
Alp Toker35d87032013-12-30 23:29:50 +00003406 Diag(Tok, diag::err_expected) << tok::colon;
Francois Pichet8f981d52011-05-25 10:19:49 +00003407 ConsumeToken();
3408 continue;
3409 }
3410
3411 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00003412 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet8f981d52011-05-25 10:19:49 +00003413 }
Douglas Gregor43edb322011-10-24 22:31:10 +00003414
3415 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00003416}