blob: dac40d87139ff65df2a2dcc4d083e6b0106f4b6b [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson0c6139d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor1b7f8982008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000022using namespace clang;
23
24/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000025/// may either be a top level namespace or a block-level namespace alias. If
26/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000027///
28/// namespace-definition: [C++ 7.3: basic.namespace]
29/// named-namespace-definition
30/// unnamed-namespace-definition
31///
32/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000033/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000034///
35/// named-namespace-definition:
36/// original-namespace-definition
37/// extension-namespace-definition
38///
39/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000040/// 'inline'[opt] 'namespace' identifier attributes[opt]
41/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000042///
43/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000044/// 'inline'[opt] 'namespace' original-namespace-name
45/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000046///
Chris Lattner8f08cb72007-08-25 06:57:03 +000047/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
48/// 'namespace' identifier '=' qualified-namespace-specifier ';'
49///
John McCalld226f652010-08-21 09:40:31 +000050Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000051 SourceLocation &DeclEnd,
52 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000053 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000054 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump1eb44332009-09-09 15:08:12 +000055
Douglas Gregor49f40bd2009-09-18 19:03:04 +000056 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000057 Actions.CodeCompleteNamespaceDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +000058 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +000059 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000060
Chris Lattner8f08cb72007-08-25 06:57:03 +000061 SourceLocation IdentLoc;
62 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000063
64 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner04d66662007-10-09 17:33:22 +000066 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000067 Ident = Tok.getIdentifierInfo();
68 IdentLoc = ConsumeToken(); // eat the identifier.
69 }
Mike Stump1eb44332009-09-09 15:08:12 +000070
Chris Lattner8f08cb72007-08-25 06:57:03 +000071 // Read label attributes, if present.
John McCall0b7e6782011-03-24 11:26:52 +000072 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000073 if (Tok.is(tok::kw___attribute)) {
74 attrTok = Tok;
John McCall7f040a92010-12-24 02:08:15 +000075 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000076 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Douglas Gregor6a588dd2009-06-17 19:49:00 +000078 if (Tok.is(tok::equal)) {
John McCall7f040a92010-12-24 02:08:15 +000079 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +000080 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +000081 if (InlineLoc.isValid())
82 Diag(InlineLoc, diag::err_inline_namespace_alias)
83 << FixItHint::CreateRemoval(InlineLoc);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000084
Chris Lattner97144fc2009-04-02 04:16:50 +000085 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000086 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattner51448322009-03-29 14:02:43 +000088 if (Tok.isNot(tok::l_brace)) {
Mike Stump1eb44332009-09-09 15:08:12 +000089 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000090 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +000091 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +000092 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattner51448322009-03-29 14:02:43 +000094 SourceLocation LBrace = ConsumeBrace();
95
Douglas Gregor23c94db2010-07-02 17:43:08 +000096 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
97 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
98 getCurScope()->getFnParent()) {
Douglas Gregor95f1b152010-05-14 05:08:22 +000099 Diag(LBrace, diag::err_namespace_nonnamespace_scope);
100 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000101 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000102 }
103
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000104 // If we're still good, complain about inline namespaces in non-C++0x now.
105 if (!getLang().CPlusPlus0x && InlineLoc.isValid())
106 Diag(InlineLoc, diag::ext_inline_namespace);
107
Chris Lattner51448322009-03-29 14:02:43 +0000108 // Enter a scope for the namespace.
109 ParseScope NamespaceScope(this, Scope::DeclScope);
110
John McCalld226f652010-08-21 09:40:31 +0000111 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000112 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
113 IdentLoc, Ident, LBrace, attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000114
John McCallf312b1e2010-08-26 23:41:50 +0000115 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
116 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Sean Huntbbd37c62009-11-21 08:43:09 +0000118 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000119 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000120 MaybeParseCXX0XAttributes(attrs);
121 MaybeParseMicrosoftAttributes(attrs);
122 ParseExternalDeclaration(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000123 }
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattner51448322009-03-29 14:02:43 +0000125 // Leave the namespace scope.
126 NamespaceScope.Exit();
127
Chris Lattner97144fc2009-04-02 04:16:50 +0000128 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
129 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000130
Chris Lattner97144fc2009-04-02 04:16:50 +0000131 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000132 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000133}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000134
Anders Carlssonf67606a2009-03-28 04:07:16 +0000135/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
136/// alias definition.
137///
John McCalld226f652010-08-21 09:40:31 +0000138Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000139 SourceLocation AliasLoc,
140 IdentifierInfo *Alias,
141 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000142 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Anders Carlssonf67606a2009-03-28 04:07:16 +0000144 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000146 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000147 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000148 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000149 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000150
Anders Carlssonf67606a2009-03-28 04:07:16 +0000151 CXXScopeSpec SS;
152 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000153 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000154
155 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
156 Diag(Tok, diag::err_expected_namespace_name);
157 // Skip to end of the definition and eat the ';'.
158 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000159 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000160 }
161
162 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000163 IdentifierInfo *Ident = Tok.getIdentifierInfo();
164 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Anders Carlssonf67606a2009-03-28 04:07:16 +0000166 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000167 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000168 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
169 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Douglas Gregor23c94db2010-07-02 17:43:08 +0000171 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000172 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000173}
174
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000175/// ParseLinkage - We know that the current token is a string_literal
176/// and just before that, that extern was seen.
177///
178/// linkage-specification: [C++ 7.5p2: dcl.link]
179/// 'extern' string-literal '{' declaration-seq[opt] '}'
180/// 'extern' string-literal declaration
181///
Chris Lattner7d642712010-11-09 20:15:55 +0000182Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000183 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000184 llvm::SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000185 bool Invalid = false;
186 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
187 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000188 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000189
190 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000191
Douglas Gregor074149e2009-01-05 19:45:36 +0000192 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000193 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000194 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000195 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000196 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000197 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000198 : SourceLocation());
199
John McCall0b7e6782011-03-24 11:26:52 +0000200 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000201 MaybeParseCXX0XAttributes(attrs);
202 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000203
Douglas Gregor074149e2009-01-05 19:45:36 +0000204 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000205 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000206 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000207 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000208 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000209 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000210
Douglas Gregor63a01132010-02-07 08:38:28 +0000211 DS.abort();
212
John McCall7f040a92010-12-24 02:08:15 +0000213 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000214
Douglas Gregorf44515a2008-12-16 22:23:02 +0000215 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000216 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000217 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000218 MaybeParseCXX0XAttributes(attrs);
219 MaybeParseMicrosoftAttributes(attrs);
220 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000221 }
222
Douglas Gregorf44515a2008-12-16 22:23:02 +0000223 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Chris Lattner7d642712010-11-09 20:15:55 +0000224 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
225 RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000226}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000227
Douglas Gregorf780abc2008-12-30 03:27:21 +0000228/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
229/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000230Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000231 const ParsedTemplateInfo &TemplateInfo,
232 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000233 ParsedAttributesWithRange &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000234 assert(Tok.is(tok::kw_using) && "Not using token");
235
236 // Eat 'using'.
237 SourceLocation UsingLoc = ConsumeToken();
238
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000239 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000240 Actions.CodeCompleteUsing(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000241 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000242 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000243
John McCall78b81052010-11-10 02:40:36 +0000244 // 'using namespace' means this is a using-directive.
245 if (Tok.is(tok::kw_namespace)) {
246 // Template parameters are always an error here.
247 if (TemplateInfo.Kind) {
248 SourceRange R = TemplateInfo.getSourceRange();
249 Diag(UsingLoc, diag::err_templated_using_directive)
250 << R << FixItHint::CreateRemoval(R);
251 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000252
John McCall7f040a92010-12-24 02:08:15 +0000253 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000254 }
255
256 // Otherwise, it must be a using-declaration.
257
258 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000259 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000260
John McCall78b81052010-11-10 02:40:36 +0000261 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000262}
263
264/// ParseUsingDirective - Parse C++ using-directive, assumes
265/// that current token is 'namespace' and 'using' was already parsed.
266///
267/// using-directive: [C++ 7.3.p4: namespace.udir]
268/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
269/// namespace-name ;
270/// [GNU] using-directive:
271/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
272/// namespace-name attributes[opt] ;
273///
John McCalld226f652010-08-21 09:40:31 +0000274Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000275 SourceLocation UsingLoc,
276 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000277 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000278 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
279
280 // Eat 'namespace'.
281 SourceLocation NamespcLoc = ConsumeToken();
282
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000283 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000284 Actions.CodeCompleteUsingDirective(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000285 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000286 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000287
Douglas Gregorf780abc2008-12-30 03:27:21 +0000288 CXXScopeSpec SS;
289 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000290 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000291
Douglas Gregorf780abc2008-12-30 03:27:21 +0000292 IdentifierInfo *NamespcName = 0;
293 SourceLocation IdentLoc = SourceLocation();
294
295 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000296 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000297 Diag(Tok, diag::err_expected_namespace_name);
298 // If there was invalid namespace name, skip to end of decl, and eat ';'.
299 SkipUntil(tok::semi);
300 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000301 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000302 }
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Chris Lattner823c44e2009-01-06 07:27:21 +0000304 // Parse identifier.
305 NamespcName = Tok.getIdentifierInfo();
306 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Chris Lattner823c44e2009-01-06 07:27:21 +0000308 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000309 bool GNUAttr = false;
310 if (Tok.is(tok::kw___attribute)) {
311 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000312 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000313 }
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Chris Lattner823c44e2009-01-06 07:27:21 +0000315 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000316 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000317 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000318 GNUAttr ? diag::err_expected_semi_after_attribute_list
319 : diag::err_expected_semi_after_namespace_name,
320 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000321
Douglas Gregor23c94db2010-07-02 17:43:08 +0000322 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000323 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000324}
325
326/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
327/// 'using' was already seen.
328///
329/// using-declaration: [C++ 7.3.p3: namespace.udecl]
330/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000331/// unqualified-id
332/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000333///
John McCalld226f652010-08-21 09:40:31 +0000334Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000335 const ParsedTemplateInfo &TemplateInfo,
336 SourceLocation UsingLoc,
337 SourceLocation &DeclEnd,
338 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000339 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000340 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000341 bool IsTypeName;
342
John McCall78b81052010-11-10 02:40:36 +0000343 // TODO: in C++0x, if we have template parameters this must be a
344 // template alias:
345 // template <...> using id = type;
346
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000347 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000348 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000349 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000350 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000351 ConsumeToken();
352 IsTypeName = true;
353 }
354 else
355 IsTypeName = false;
356
357 // Parse nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000358 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000359
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000360 // Check nested-name specifier.
361 if (SS.isInvalid()) {
362 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000363 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000364 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000365
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000366 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000367 // destructor names and allow the action module to diagnose any semantic
368 // errors.
369 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000370 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000371 /*EnteringContext=*/false,
372 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000373 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000374 ParsedType(),
Douglas Gregor12c118a2009-11-04 16:30:06 +0000375 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000376 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000377 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000378 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000379
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000380 // Parse (optional) attributes (most likely GNU strong-using extension).
John McCall0b7e6782011-03-24 11:26:52 +0000381 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000382 MaybeParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000384 // Eat ';'.
385 DeclEnd = Tok.getLocation();
386 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
John McCall7f040a92010-12-24 02:08:15 +0000387 !attrs.empty() ? "attributes list" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000388 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000389
John McCall78b81052010-11-10 02:40:36 +0000390 // Diagnose an attempt to declare a templated using-declaration.
391 if (TemplateInfo.Kind) {
392 SourceRange R = TemplateInfo.getSourceRange();
393 Diag(UsingLoc, diag::err_templated_using_declaration)
394 << R << FixItHint::CreateRemoval(R);
395
396 // Unfortunately, we have to bail out instead of recovering by
397 // ignoring the parameters, just in case the nested name specifier
398 // depends on the parameters.
399 return 0;
400 }
401
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000402 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000403 Name, attrs.getList(),
404 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000405}
406
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000407/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
408///
409/// static_assert-declaration:
410/// static_assert ( constant-expression , string-literal ) ;
411///
John McCalld226f652010-08-21 09:40:31 +0000412Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000413 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
414 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000416 if (Tok.isNot(tok::l_paren)) {
417 Diag(Tok, diag::err_expected_lparen);
John McCalld226f652010-08-21 09:40:31 +0000418 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000419 }
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000421 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000422
John McCall60d7b3a2010-08-24 06:29:42 +0000423 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000424 if (AssertExpr.isInvalid()) {
425 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000426 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000427 }
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Anders Carlssonad5f9602009-03-13 23:29:20 +0000429 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000430 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000431
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000432 if (Tok.isNot(tok::string_literal)) {
433 Diag(Tok, diag::err_expected_string_literal);
434 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000435 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000436 }
Mike Stump1eb44332009-09-09 15:08:12 +0000437
John McCall60d7b3a2010-08-24 06:29:42 +0000438 ExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000439 if (AssertMessage.isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000440 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000441
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000442 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Chris Lattner97144fc2009-04-02 04:16:50 +0000444 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000445 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000446
John McCall9ae2f072010-08-23 23:25:46 +0000447 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
448 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000449 AssertMessage.take(),
450 RParenLoc);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000451}
452
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000453/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
454///
455/// 'decltype' ( expression )
456///
457void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
458 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
459
460 SourceLocation StartLoc = ConsumeToken();
461 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000462
463 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000464 "decltype")) {
465 SkipUntil(tok::r_paren);
466 return;
467 }
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000469 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000471 // C++0x [dcl.type.simple]p4:
472 // The operand of the decltype specifier is an unevaluated operand.
473 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000474 Sema::Unevaluated);
John McCall60d7b3a2010-08-24 06:29:42 +0000475 ExprResult Result = ParseExpression();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000476 if (Result.isInvalid()) {
477 SkipUntil(tok::r_paren);
478 return;
479 }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000481 // Match the ')'
482 SourceLocation RParenLoc;
483 if (Tok.is(tok::r_paren))
484 RParenLoc = ConsumeParen();
485 else
486 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000488 if (RParenLoc.isInvalid())
489 return;
490
491 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000492 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000493 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000494 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000495 DiagID, Result.release()))
496 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000497}
498
Douglas Gregor42a552f2008-11-05 20:51:48 +0000499/// ParseClassName - Parse a C++ class-name, which names a class. Note
500/// that we only check that the result names a type; semantic analysis
501/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000502/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000503/// found.
504///
505/// class-name: [C++ 9.1]
506/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000507/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000508///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000509Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +0000510 CXXScopeSpec &SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000511 // Check whether we have a template-id that names a type.
512 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000513 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000514 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000515 if (TemplateId->Kind == TNK_Type_template ||
516 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000517 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000518
519 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000520 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000521 EndLocation = Tok.getAnnotationEndLoc();
522 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000523
524 if (Type)
525 return Type;
526 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000527 }
528
529 // Fall through to produce an error below.
530 }
531
Douglas Gregor42a552f2008-11-05 20:51:48 +0000532 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000533 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000534 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000535 }
536
Douglas Gregor84d0a192010-01-12 21:28:44 +0000537 IdentifierInfo *Id = Tok.getIdentifierInfo();
538 SourceLocation IdLoc = ConsumeToken();
539
540 if (Tok.is(tok::less)) {
541 // It looks the user intended to write a template-id here, but the
542 // template-name was wrong. Try to fix that.
543 TemplateNameKind TNK = TNK_Type_template;
544 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000545 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000546 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000547 Diag(IdLoc, diag::err_unknown_template_name)
548 << Id;
549 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000550
Douglas Gregor84d0a192010-01-12 21:28:44 +0000551 if (!Template)
552 return true;
553
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000554 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000555 UnqualifiedId TemplateName;
556 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000557
Douglas Gregor84d0a192010-01-12 21:28:44 +0000558 // Parse the full template-id, then turn it into a type.
559 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
560 SourceLocation(), true))
561 return true;
562 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000563 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000564
Douglas Gregor84d0a192010-01-12 21:28:44 +0000565 // If we didn't end up with a typename token, there's nothing more we
566 // can do.
567 if (Tok.isNot(tok::annot_typename))
568 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000569
Douglas Gregor84d0a192010-01-12 21:28:44 +0000570 // Retrieve the type from the annotation token, consume that token, and
571 // return.
572 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000573 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000574 ConsumeToken();
575 return Type;
576 }
577
Douglas Gregor42a552f2008-11-05 20:51:48 +0000578 // We have an identifier; check whether it is actually a type.
Douglas Gregor059101f2011-03-02 00:47:37 +0000579 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000580 false, ParsedType(),
581 /*NonTrivialTypeSourceInfo=*/true);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000582 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000583 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000584 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000585 }
586
587 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000588 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000589
590 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000591 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000592 DS.SetRangeStart(IdLoc);
593 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000594 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000595
596 const char *PrevSpec = 0;
597 unsigned DiagID;
598 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
599
600 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
601 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000602}
603
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000604/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
605/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
606/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000607/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000608///
609/// class-specifier: [C++ class]
610/// class-head '{' member-specification[opt] '}'
611/// class-head '{' member-specification[opt] '}' attributes[opt]
612/// class-head:
613/// class-key identifier[opt] base-clause[opt]
614/// class-key nested-name-specifier identifier base-clause[opt]
615/// class-key nested-name-specifier[opt] simple-template-id
616/// base-clause[opt]
617/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000618/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000619/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000620/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000621/// simple-template-id base-clause[opt]
622/// class-key:
623/// 'class'
624/// 'struct'
625/// 'union'
626///
627/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000628/// class-key ::[opt] nested-name-specifier[opt] identifier
629/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
630/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000631///
632/// Note that the C++ class-specifier and elaborated-type-specifier,
633/// together, subsume the C99 struct-or-union-specifier:
634///
635/// struct-or-union-specifier: [C99 6.7.2.1]
636/// struct-or-union identifier[opt] '{' struct-contents '}'
637/// struct-or-union identifier
638/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
639/// '}' attributes[opt]
640/// [GNU] struct-or-union attributes[opt] identifier
641/// struct-or-union:
642/// 'struct'
643/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000644void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
645 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000646 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redld9bafa72010-02-03 21:21:43 +0000647 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000648 DeclSpec::TST TagType;
649 if (TagTokKind == tok::kw_struct)
650 TagType = DeclSpec::TST_struct;
651 else if (TagTokKind == tok::kw_class)
652 TagType = DeclSpec::TST_class;
653 else {
654 assert(TagTokKind == tok::kw_union && "Not a class specifier");
655 TagType = DeclSpec::TST_union;
656 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000657
Douglas Gregor374929f2009-09-18 15:37:17 +0000658 if (Tok.is(tok::code_completion)) {
659 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000660 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregordc845342010-05-25 05:58:43 +0000661 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +0000662 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000663
Chandler Carruth926c4b42010-06-28 08:39:25 +0000664 // C++03 [temp.explicit] 14.7.2/8:
665 // The usual access checking rules do not apply to names used to specify
666 // explicit instantiations.
667 //
668 // As an extension we do not perform access checking on the names used to
669 // specify explicit specializations either. This is important to allow
670 // specializing traits classes for private types.
671 bool SuppressingAccessChecks = false;
672 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
673 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
674 Actions.ActOnStartSuppressingAccessChecks();
675 SuppressingAccessChecks = true;
676 }
677
John McCall0b7e6782011-03-24 11:26:52 +0000678 ParsedAttributes attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000679 // If attributes exist after tag, parse them.
680 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +0000681 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000682
Steve Narofff59e17e2008-12-24 20:59:21 +0000683 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +0000684 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +0000685 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000686
Sean Huntbbd37c62009-11-21 08:43:09 +0000687 // If C++0x attributes exist here, parse them.
688 // FIXME: Are we consistent with the ordering of parsing of different
689 // styles of attributes?
John McCall7f040a92010-12-24 02:08:15 +0000690 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Douglas Gregorb117a602009-09-04 05:53:02 +0000692 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
693 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
694 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000695 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000696 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
697 // properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000698 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000699 Tok.setKind(tok::identifier);
700 }
701
702 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
703 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
704 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000705 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000706 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
707 // properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000708 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000709 Tok.setKind(tok::identifier);
710 }
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000712 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000713 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000714 if (getLang().CPlusPlus) {
715 // "FOO : BAR" is not a potential typo for "FOO::BAR".
716 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000717
John McCallb3d87482010-08-24 05:47:05 +0000718 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true))
John McCall207014e2010-07-30 06:26:29 +0000719 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +0000720 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +0000721 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
722 Diag(Tok, diag::err_expected_ident);
723 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000724
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000725 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
726
Douglas Gregorcc636682009-02-17 23:15:12 +0000727 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000728 IdentifierInfo *Name = 0;
729 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000730 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000731 if (Tok.is(tok::identifier)) {
732 Name = Tok.getIdentifierInfo();
733 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000734
Douglas Gregor5ee37342010-05-30 22:30:21 +0000735 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000736 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000737 // Eat the template argument list and try to continue parsing this as
738 // a class (or template thereof).
739 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000740 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +0000741 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000742 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000743 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000744 // We couldn't parse the template argument list at all, so don't
745 // try to give any location information for the list.
746 LAngleLoc = RAngleLoc = SourceLocation();
747 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000748
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000749 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000750 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000751 << (TagType == DeclSpec::TST_class? 0
752 : TagType == DeclSpec::TST_struct? 1
753 : 2)
754 << Name
755 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000756
757 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000758 // we've removed its template argument list.
759 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
760 if (TemplateParams && TemplateParams->size() > 1) {
761 TemplateParams->pop_back();
762 } else {
763 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000764 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000765 = ParsedTemplateInfo::NonTemplate;
766 }
767 } else if (TemplateInfo.Kind
768 == ParsedTemplateInfo::ExplicitInstantiation) {
769 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000770 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000771 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000772 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000773 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000774 = SourceLocation();
775 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
776 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000777 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000778 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000779 } else if (Tok.is(tok::annot_template_id)) {
780 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
781 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000782
Douglas Gregor059101f2011-03-02 00:47:37 +0000783 if (TemplateId->Kind != TNK_Type_template &&
784 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000785 // The template-name in the simple-template-id refers to
786 // something other than a class template. Give an appropriate
787 // error message and skip to the ';'.
788 SourceRange Range(NameLoc);
789 if (SS.isNotEmpty())
790 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000791
Douglas Gregor39a8de12009-02-25 19:37:18 +0000792 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
793 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Douglas Gregor39a8de12009-02-25 19:37:18 +0000795 DS.SetTypeSpecError();
796 SkipUntil(tok::semi, false, true);
797 TemplateId->Destroy();
Chandler Carruth926c4b42010-06-28 08:39:25 +0000798 if (SuppressingAccessChecks)
799 Actions.ActOnStopSuppressingAccessChecks();
800
Douglas Gregor39a8de12009-02-25 19:37:18 +0000801 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000802 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000803 }
804
Chandler Carruth926c4b42010-06-28 08:39:25 +0000805 // As soon as we're finished parsing the class's template-id, turn access
806 // checking back on.
807 if (SuppressingAccessChecks)
808 Actions.ActOnStopSuppressingAccessChecks();
809
John McCall67d1a672009-08-06 02:15:43 +0000810 // There are four options here. If we have 'struct foo;', then this
811 // is either a forward declaration or a friend declaration, which
Anders Carlssoncc54d592011-01-22 16:56:46 +0000812 // have to be treated differently. If we have 'struct foo {...',
Anders Carlsson1d209272011-03-25 14:55:14 +0000813 // 'struct foo :...' or 'struct foo final[opt]' then this is a
Anders Carlssoncc54d592011-01-22 16:56:46 +0000814 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000815 // However, in some contexts, things look like declarations but are just
816 // references, e.g.
817 // new struct s;
818 // or
819 // &T::operator struct s;
820 // For these, SuppressDeclarations is true.
John McCallf312b1e2010-08-26 23:41:50 +0000821 Sema::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000822 if (SuppressDeclarations)
John McCallf312b1e2010-08-26 23:41:50 +0000823 TUK = Sema::TUK_Reference;
Anders Carlssoncc54d592011-01-22 16:56:46 +0000824 else if (Tok.is(tok::l_brace) ||
825 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +0000826 isCXX0XFinalKeyword()) {
Douglas Gregord85bea22009-09-26 06:47:28 +0000827 if (DS.isFriendSpecified()) {
828 // C++ [class.friend]p2:
829 // A class shall not be defined in a friend declaration.
830 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
831 << SourceRange(DS.getFriendSpecLoc());
832
833 // Skip everything up to the semicolon, so that this looks like a proper
834 // friend class (or template thereof) declaration.
835 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +0000836 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +0000837 } else {
838 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +0000839 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +0000840 }
841 } else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +0000842 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000843 else
John McCallf312b1e2010-08-26 23:41:50 +0000844 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000845
John McCall207014e2010-07-30 06:26:29 +0000846 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +0000847 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +0000848 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
849 // We have a declaration or reference to an anonymous class.
850 Diag(StartLoc, diag::err_anon_type_definition)
851 << DeclSpec::getSpecifierName(TagType);
852 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000853
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000854 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000855
856 if (TemplateId)
857 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000858 return;
859 }
860
Douglas Gregorddc29e12009-02-06 22:42:48 +0000861 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +0000862 DeclResult TagOrTempResult = true; // invalid
863 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000864
Douglas Gregor402abb52009-05-28 23:31:59 +0000865 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000866 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000867 // Explicit specialization, class template partial specialization,
868 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000869 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000870 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000871 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000872 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000873 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000874 // This is an explicit instantiation of a class template.
875 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000876 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000877 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000878 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000879 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000880 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000881 SS,
John McCall2b5289b2010-08-23 07:28:44 +0000882 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000883 TemplateId->TemplateNameLoc,
884 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000885 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000886 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +0000887 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +0000888
889 // Friend template-ids are treated as references unless
890 // they have template headers, in which case they're ill-formed
891 // (FIXME: "template <class T> friend class A<T>::B<int>;").
892 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +0000893 } else if (TUK == Sema::TUK_Reference ||
894 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +0000895 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000896 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType,
897 StartLoc,
898 TemplateId->SS,
899 TemplateId->Template,
900 TemplateId->TemplateNameLoc,
901 TemplateId->LAngleLoc,
902 TemplateArgsPtr,
903 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000904 } else {
905 // This is an explicit specialization or a class template
906 // partial specialization.
907 TemplateParameterLists FakedParamLists;
908
909 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
910 // This looks like an explicit instantiation, because we have
911 // something like
912 //
913 // template class Foo<X>
914 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000915 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000916 // meant to be an explicit specialization, but the user forgot
917 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +0000918 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000919
Mike Stump1eb44332009-09-09 15:08:12 +0000920 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000921 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000922 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000923 diag::err_explicit_instantiation_with_definition)
924 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +0000925 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000926
927 // Create a fake template parameter list that contains only
928 // "template<>", so that we treat this construct as a class
929 // template specialization.
930 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000931 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000932 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000933 LAngleLoc,
934 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000935 LAngleLoc));
936 TemplateParams = &FakedParamLists;
937 }
938
939 // Build the class template specialization.
940 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000941 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000942 StartLoc, SS,
John McCall2b5289b2010-08-23 07:28:44 +0000943 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000944 TemplateId->TemplateNameLoc,
945 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000946 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000947 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +0000948 attrs.getList(),
John McCallf312b1e2010-08-26 23:41:50 +0000949 MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000950 TemplateParams? &(*TemplateParams)[0] : 0,
951 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000952 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000953 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000954 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000955 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000956 // Explicit instantiation of a member of a class template
957 // specialization, e.g.,
958 //
959 // template struct Outer<int>::Inner;
960 //
961 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000962 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000963 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000964 TemplateInfo.TemplateLoc,
965 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +0000966 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +0000967 } else if (TUK == Sema::TUK_Friend &&
968 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
969 TagOrTempResult =
970 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
971 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000972 Name, NameLoc, attrs.getList(),
John McCall9a34edb2010-10-19 01:40:49 +0000973 MultiTemplateParamsArg(Actions,
974 TemplateParams? &(*TemplateParams)[0] : 0,
975 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000976 } else {
977 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000978 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000979 // FIXME: Diagnose this particular error.
980 }
981
John McCallc4e70192009-09-11 04:59:25 +0000982 bool IsDependent = false;
983
John McCalla25c4082010-10-19 18:40:57 +0000984 // Don't pass down template parameter lists if this is just a tag
985 // reference. For example, we don't need the template parameters here:
986 // template <class T> class A *makeA(T t);
987 MultiTemplateParamsArg TParams;
988 if (TUK != Sema::TUK_Reference && TemplateParams)
989 TParams =
990 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
991
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000992 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +0000993 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +0000994 SS, Name, NameLoc, attrs.getList(), AS,
John McCalla25c4082010-10-19 18:40:57 +0000995 TParams, Owned, IsDependent, false,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000996 false, clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +0000997
998 // If ActOnTag said the type was dependent, try again with the
999 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001000 if (IsDependent) {
1001 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001002 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001003 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001004 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001005 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001006
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001007 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001008 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001009 assert(Tok.is(tok::l_brace) ||
Anders Carlssoncc54d592011-01-22 16:56:46 +00001010 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001011 isCXX0XFinalKeyword());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001012 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +00001013 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001014 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001015 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001016 }
1017
John McCallb3d87482010-08-24 05:47:05 +00001018 const char *PrevSpec = 0;
1019 unsigned DiagID;
1020 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001021 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001022 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1023 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001024 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001025 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001026 Result = DS.SetTypeSpecType(TagType, StartLoc,
1027 NameLoc.isValid() ? NameLoc : StartLoc,
1028 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001029 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001030 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001031 return;
1032 }
Mike Stump1eb44332009-09-09 15:08:12 +00001033
John McCallb3d87482010-08-24 05:47:05 +00001034 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001035 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001036
Chris Lattner4ed5d912010-02-02 01:23:29 +00001037 // At this point, we've successfully parsed a class-specifier in 'definition'
1038 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1039 // going to look at what comes after it to improve error recovery. If an
1040 // impossible token occurs next, we assume that the programmer forgot a ; at
1041 // the end of the declaration and recover that way.
1042 //
1043 // This switch enumerates the valid "follow" set for definition.
John McCallf312b1e2010-08-26 23:41:50 +00001044 if (TUK == Sema::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001045 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001046 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001047 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001048 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +00001049 case tok::star: // struct foo {...} * P;
1050 case tok::amp: // struct foo {...} & R = ...
1051 case tok::identifier: // struct foo {...} V ;
1052 case tok::r_paren: //(struct foo {...} ) {4}
1053 case tok::annot_cxxscope: // struct foo {...} a:: b;
1054 case tok::annot_typename: // struct foo {...} a ::b;
1055 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +00001056 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +00001057 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001058 ExpectedSemi = false;
1059 break;
1060 // Type qualifiers
1061 case tok::kw_const: // struct foo {...} const x;
1062 case tok::kw_volatile: // struct foo {...} volatile x;
1063 case tok::kw_restrict: // struct foo {...} restrict x;
1064 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +00001065 // Storage-class specifiers
1066 case tok::kw_static: // struct foo {...} static x;
1067 case tok::kw_extern: // struct foo {...} extern x;
1068 case tok::kw_typedef: // struct foo {...} typedef x;
1069 case tok::kw_register: // struct foo {...} register x;
1070 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregor33f99242010-05-17 18:19:56 +00001071 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001072 // As shown above, type qualifiers and storage class specifiers absolutely
1073 // can occur after class specifiers according to the grammar. However,
1074 // almost noone actually writes code like this. If we see one of these,
1075 // it is much more likely that someone missed a semi colon and the
1076 // type/storage class specifier we're seeing is part of the *next*
1077 // intended declaration, as in:
1078 //
1079 // struct foo { ... }
1080 // typedef int X;
1081 //
1082 // We'd really like to emit a missing semicolon error instead of emitting
1083 // an error on the 'int' saying that you can't have two type specifiers in
1084 // the same declaration of X. Because of this, we look ahead past this
1085 // token to see if it's a type specifier. If so, we know the code is
1086 // otherwise invalid, so we can produce the expected semi error.
1087 if (!isKnownToBeTypeSpecifier(NextToken()))
1088 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001089 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001090
1091 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001092 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001093 if (!getLang().CPlusPlus)
1094 ExpectedSemi = false;
1095 break;
1096 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001097
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001098 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001099 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1100 TagType == DeclSpec::TST_class ? "class"
1101 : TagType == DeclSpec::TST_struct? "struct" : "union");
1102 // Push this token back into the preprocessor and change our current token
1103 // to ';' so that the rest of the code recovers as though there were an
1104 // ';' after the definition.
1105 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001106 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001107 }
1108 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001109}
1110
Mike Stump1eb44332009-09-09 15:08:12 +00001111/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001112///
1113/// base-clause : [C++ class.derived]
1114/// ':' base-specifier-list
1115/// base-specifier-list:
1116/// base-specifier '...'[opt]
1117/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001118void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001119 assert(Tok.is(tok::colon) && "Not a base clause");
1120 ConsumeToken();
1121
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001122 // Build up an array of parsed base specifiers.
John McCallca0408f2010-08-23 06:44:23 +00001123 llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001124
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001125 while (true) {
1126 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001127 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001128 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001129 // Skip the rest of this base specifier, up until the comma or
1130 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001131 SkipUntil(tok::comma, tok::l_brace, true, true);
1132 } else {
1133 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001134 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001135 }
1136
1137 // If the next token is a comma, consume it and keep reading
1138 // base-specifiers.
1139 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001141 // Consume the comma.
1142 ConsumeToken();
1143 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001144
1145 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001146 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001147}
1148
1149/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1150/// one entry in the base class list of a class specifier, for example:
1151/// class foo : public bar, virtual private baz {
1152/// 'public bar' and 'virtual private baz' are each base-specifiers.
1153///
1154/// base-specifier: [C++ class.derived]
1155/// ::[opt] nested-name-specifier[opt] class-name
1156/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1157/// class-name
1158/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1159/// class-name
John McCalld226f652010-08-21 09:40:31 +00001160Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001161 bool IsVirtual = false;
1162 SourceLocation StartLoc = Tok.getLocation();
1163
1164 // Parse the 'virtual' keyword.
1165 if (Tok.is(tok::kw_virtual)) {
1166 ConsumeToken();
1167 IsVirtual = true;
1168 }
1169
1170 // Parse an (optional) access specifier.
1171 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001172 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001173 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001175 // Parse the 'virtual' keyword (again!), in case it came after the
1176 // access specifier.
1177 if (Tok.is(tok::kw_virtual)) {
1178 SourceLocation VirtualLoc = ConsumeToken();
1179 if (IsVirtual) {
1180 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001181 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001182 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001183 }
1184
1185 IsVirtual = true;
1186 }
1187
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001188 // Parse optional '::' and optional nested-name-specifier.
1189 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001190 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001191
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001192 // The location of the base class itself.
1193 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001194
1195 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001196 SourceLocation EndLocation;
Douglas Gregor059101f2011-03-02 00:47:37 +00001197 TypeResult BaseType = ParseClassName(EndLocation, SS);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001198 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001199 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001201 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1202 // actually part of the base-specifier-list grammar productions, but we
1203 // parse it here for convenience.
1204 SourceLocation EllipsisLoc;
1205 if (Tok.is(tok::ellipsis))
1206 EllipsisLoc = ConsumeToken();
1207
Mike Stump1eb44332009-09-09 15:08:12 +00001208 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001209 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001211 // Notify semantic analysis that we have parsed a complete
1212 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001213 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001214 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001215}
1216
1217/// getAccessSpecifierIfPresent - Determine whether the next token is
1218/// a C++ access-specifier.
1219///
1220/// access-specifier: [C++ class.derived]
1221/// 'private'
1222/// 'protected'
1223/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001224AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001225 switch (Tok.getKind()) {
1226 default: return AS_none;
1227 case tok::kw_private: return AS_private;
1228 case tok::kw_protected: return AS_protected;
1229 case tok::kw_public: return AS_public;
1230 }
1231}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001232
Eli Friedmand33133c2009-07-22 21:45:50 +00001233void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
John McCalld226f652010-08-21 09:40:31 +00001234 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001235 // We just declared a member function. If this member function
1236 // has any default arguments, we'll need to parse them later.
1237 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001238 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001239 = DeclaratorInfo.getFunctionTypeInfo();
Eli Friedmand33133c2009-07-22 21:45:50 +00001240 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1241 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1242 if (!LateMethod) {
1243 // Push this method onto the stack of late-parsed method
1244 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001245 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1246 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001247 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001248
1249 // Add all of the parameters prior to this one (they don't
1250 // have default arguments).
1251 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1252 for (unsigned I = 0; I < ParamIdx; ++I)
1253 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001254 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001255 }
1256
1257 // Add this parameter to the list of parameters (it or may
1258 // not have a default argument).
1259 LateMethod->DefaultArgs.push_back(
1260 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1261 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1262 }
1263 }
1264}
1265
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001266/// isCXX0XVirtSpecifier - Determine whether the next token is a C++0x
1267/// virt-specifier.
1268///
1269/// virt-specifier:
1270/// override
1271/// final
Anders Carlssoncc54d592011-01-22 16:56:46 +00001272VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier() const {
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001273 if (!getLang().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001274 return VirtSpecifiers::VS_None;
1275
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001276 if (Tok.is(tok::identifier)) {
1277 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001278
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001279 // Initialize the contextual keywords.
1280 if (!Ident_final) {
1281 Ident_final = &PP.getIdentifierTable().get("final");
1282 Ident_override = &PP.getIdentifierTable().get("override");
1283 }
1284
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001285 if (II == Ident_override)
1286 return VirtSpecifiers::VS_Override;
1287
1288 if (II == Ident_final)
1289 return VirtSpecifiers::VS_Final;
1290 }
1291
1292 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001293}
1294
1295/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1296///
1297/// virt-specifier-seq:
1298/// virt-specifier
1299/// virt-specifier-seq virt-specifier
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001300void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001301 while (true) {
Anders Carlssoncc54d592011-01-22 16:56:46 +00001302 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001303 if (Specifier == VirtSpecifiers::VS_None)
1304 return;
1305
1306 // C++ [class.mem]p8:
1307 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001308 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001309 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001310 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1311 << PrevSpec
1312 << FixItHint::CreateRemoval(Tok.getLocation());
1313
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001314 if (!getLang().CPlusPlus0x)
1315 Diag(Tok.getLocation(), diag::ext_override_control_keyword)
1316 << VirtSpecifiers::getSpecifierName(Specifier);
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001317 ConsumeToken();
1318 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001319}
1320
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001321/// isCXX0XFinalKeyword - Determine whether the next token is a C++0x
1322/// contextual 'final' keyword.
1323bool Parser::isCXX0XFinalKeyword() const {
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001324 if (!getLang().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001325 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001326
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001327 if (!Tok.is(tok::identifier))
1328 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001329
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001330 // Initialize the contextual keywords.
1331 if (!Ident_final) {
1332 Ident_final = &PP.getIdentifierTable().get("final");
1333 Ident_override = &PP.getIdentifierTable().get("override");
1334 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001335
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001336 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001337}
1338
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001339/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1340///
1341/// member-declaration:
1342/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1343/// function-definition ';'[opt]
1344/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1345/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001346/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001347/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001348/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001349///
1350/// member-declarator-list:
1351/// member-declarator
1352/// member-declarator-list ',' member-declarator
1353///
1354/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001355/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001356/// declarator constant-initializer[opt]
1357/// identifier[opt] ':' constant-expression
1358///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001359/// virt-specifier-seq:
1360/// virt-specifier
1361/// virt-specifier-seq virt-specifier
1362///
1363/// virt-specifier:
1364/// override
1365/// final
1366/// new
1367///
Sebastian Redle2b68332009-04-12 17:16:29 +00001368/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001369/// '= 0'
1370///
1371/// constant-initializer:
1372/// '=' constant-expression
1373///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001374void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCallc9068d72010-07-16 08:13:16 +00001375 const ParsedTemplateInfo &TemplateInfo,
1376 ParsingDeclRAIIObject *TemplateDiags) {
John McCall60fa3cf2009-12-11 02:10:03 +00001377 // Access declarations.
1378 if (!TemplateInfo.Kind &&
1379 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001380 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001381 Tok.is(tok::annot_cxxscope)) {
1382 bool isAccessDecl = false;
1383 if (NextToken().is(tok::identifier))
1384 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1385 else
1386 isAccessDecl = NextToken().is(tok::kw_operator);
1387
1388 if (isAccessDecl) {
1389 // Collect the scope specifier token we annotated earlier.
1390 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001391 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
John McCall60fa3cf2009-12-11 02:10:03 +00001392
1393 // Try to parse an unqualified-id.
1394 UnqualifiedId Name;
John McCallb3d87482010-08-24 05:47:05 +00001395 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001396 SkipUntil(tok::semi);
1397 return;
1398 }
1399
1400 // TODO: recover from mistakenly-qualified operator declarations.
1401 if (ExpectAndConsume(tok::semi,
1402 diag::err_expected_semi_after,
1403 "access declaration",
1404 tok::semi))
1405 return;
1406
Douglas Gregor23c94db2010-07-02 17:43:08 +00001407 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001408 false, SourceLocation(),
1409 SS, Name,
1410 /* AttrList */ 0,
1411 /* IsTypeName */ false,
1412 SourceLocation());
1413 return;
1414 }
1415 }
1416
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001417 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001418 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001419 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001420 SourceLocation DeclEnd;
1421 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001422 return;
1423 }
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Chris Lattner682bf922009-03-29 16:50:03 +00001425 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001426 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001427 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001428 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001429 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001430 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001431 return;
1432 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001433
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001434 // Handle: member-declaration ::= '__extension__' member-declaration
1435 if (Tok.is(tok::kw___extension__)) {
1436 // __extension__ silences extension warnings in the subexpression.
1437 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1438 ConsumeToken();
John McCallc9068d72010-07-16 08:13:16 +00001439 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001440 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001441
Chris Lattner4ed5d912010-02-02 01:23:29 +00001442 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1443 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001444 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001445
John McCall0b7e6782011-03-24 11:26:52 +00001446 ParsedAttributesWithRange attrs(AttrFactory);
Sean Huntbbd37c62009-11-21 08:43:09 +00001447 // Optional C++0x attribute-specifier
John McCall7f040a92010-12-24 02:08:15 +00001448 MaybeParseCXX0XAttributes(attrs);
1449 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001450
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001451 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001452 // FIXME: Check for template aliases
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001453
John McCall7f040a92010-12-24 02:08:15 +00001454 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001456 // Eat 'using'.
1457 SourceLocation UsingLoc = ConsumeToken();
1458
1459 if (Tok.is(tok::kw_namespace)) {
1460 Diag(UsingLoc, diag::err_using_namespace_in_class);
1461 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001462 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001463 SourceLocation DeclEnd;
1464 // Otherwise, it must be using-declaration.
John McCall78b81052010-11-10 02:40:36 +00001465 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1466 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001467 }
1468 return;
1469 }
1470
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001471 // decl-specifier-seq:
1472 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001473 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00001474 DS.takeAttributesFrom(attrs);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001475 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001476
John McCallf312b1e2010-08-26 23:41:50 +00001477 MultiTemplateParamsArg TemplateParams(Actions,
John McCalldd4a3b02009-09-16 22:47:08 +00001478 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1479 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1480
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001481 if (Tok.is(tok::semi)) {
1482 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001483 Decl *TheDecl =
John McCallc9068d72010-07-16 08:13:16 +00001484 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
1485 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001486 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001487 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001488
John McCall54abf7d2009-11-04 02:18:39 +00001489 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00001490 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001491
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001492 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001493 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1494 ColonProtectionRAIIObject X(*this);
1495
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001496 // Parse the first declarator.
1497 ParseDeclarator(DeclaratorInfo);
1498 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001499 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001500 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001501 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001502 if (Tok.is(tok::semi))
1503 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001504 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001505 }
1506
Nico Weber48673472011-01-28 06:07:34 +00001507 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1508
John Thompson1b2fc0f2009-11-25 22:58:06 +00001509 // If attributes exist after the declarator, but before an '{', parse them.
John McCall7f040a92010-12-24 02:08:15 +00001510 MaybeParseGNUAttributes(DeclaratorInfo);
John Thompson1b2fc0f2009-11-25 22:58:06 +00001511
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001512 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001513 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001514 || (DeclaratorInfo.isFunctionDeclarator() &&
1515 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001516 if (!DeclaratorInfo.isFunctionDeclarator()) {
1517 Diag(Tok, diag::err_func_def_no_params);
1518 ConsumeBrace();
1519 SkipUntil(tok::r_brace, true);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001520
1521 // Consume the optional ';'
1522 if (Tok.is(tok::semi))
1523 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001524 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001525 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001526
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001527 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1528 Diag(Tok, diag::err_function_declared_typedef);
1529 // This recovery skips the entire function body. It would be nice
1530 // to simply call ParseCXXInlineMethodDef() below, however Sema
1531 // assumes the declarator represents a function, not a typedef.
1532 ConsumeBrace();
1533 SkipUntil(tok::r_brace, true);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001534
1535 // Consume the optional ';'
1536 if (Tok.is(tok::semi))
1537 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001538 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001539 }
1540
Nico Weber48673472011-01-28 06:07:34 +00001541 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo, VS);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001542 // Consume the optional ';'
1543 if (Tok.is(tok::semi))
1544 ConsumeToken();
1545
Chris Lattner682bf922009-03-29 16:50:03 +00001546 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001547 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001548 }
1549
1550 // member-declarator-list:
1551 // member-declarator
1552 // member-declarator-list ',' member-declarator
1553
John McCalld226f652010-08-21 09:40:31 +00001554 llvm::SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00001555 ExprResult BitfieldSize;
1556 ExprResult Init;
Sebastian Redle2b68332009-04-12 17:16:29 +00001557 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001558
1559 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001560 // member-declarator:
1561 // declarator pure-specifier[opt]
1562 // declarator constant-initializer[opt]
1563 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001564 if (Tok.is(tok::colon)) {
1565 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001566 BitfieldSize = ParseConstantExpression();
1567 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001568 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001569 }
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001571 ParseOptionalCXX0XVirtSpecifierSeq(VS);
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001572
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001573 // pure-specifier:
1574 // '= 0'
1575 //
1576 // constant-initializer:
1577 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001578 //
1579 // defaulted/deleted function-definition:
1580 // '=' 'default' [TODO]
1581 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001582 if (Tok.is(tok::equal)) {
1583 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001584 if (Tok.is(tok::kw_delete)) {
1585 if (!getLang().CPlusPlus0x)
1586 Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
Sebastian Redle2b68332009-04-12 17:16:29 +00001587 ConsumeToken();
1588 Deleted = true;
1589 } else {
1590 Init = ParseInitializer();
1591 if (Init.isInvalid())
1592 SkipUntil(tok::comma, true, true);
1593 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001594 }
1595
Chris Lattnere6563252010-06-13 05:34:18 +00001596 // If a simple-asm-expr is present, parse it.
1597 if (Tok.is(tok::kw_asm)) {
1598 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001599 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00001600 if (AsmLabel.isInvalid())
1601 SkipUntil(tok::comma, true, true);
1602
1603 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1604 DeclaratorInfo.SetRangeEnd(Loc);
1605 }
1606
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001607 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001608 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001609
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001610 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001611 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001612 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001613
John McCalld226f652010-08-21 09:40:31 +00001614 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00001615 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001616 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00001617 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCallbbbcdd92009-09-11 21:02:39 +00001618 /*IsDefinition*/ false,
1619 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001620 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001621 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00001622 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001623 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001624 BitfieldSize.release(),
Anders Carlsson69a87352011-01-20 03:57:25 +00001625 VS, Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001626 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001627 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001628 }
Chris Lattner682bf922009-03-29 16:50:03 +00001629 if (ThisDecl)
1630 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001631
Douglas Gregor72b505b2008-12-16 21:30:33 +00001632 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001633 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001634 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001635 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001636 }
1637
John McCall54abf7d2009-11-04 02:18:39 +00001638 DeclaratorInfo.complete(ThisDecl);
1639
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001640 // If we don't have a comma, it is either the end of the list (a ';')
1641 // or an error, bail out.
1642 if (Tok.isNot(tok::comma))
1643 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001645 // Consume the comma.
1646 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001648 // Parse the next declarator.
1649 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00001650 VS.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001651 BitfieldSize = 0;
1652 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001653 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001655 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00001656 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001657
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001658 if (Tok.isNot(tok::colon))
1659 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001660 }
1661
Chris Lattnerae50d502010-02-02 00:43:15 +00001662 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1663 // Skip to end of block or statement.
1664 SkipUntil(tok::r_brace, true, true);
1665 // If we stopped at a ';', eat it.
1666 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001667 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001668 }
1669
Douglas Gregor23c94db2010-07-02 17:43:08 +00001670 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00001671 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001672}
1673
1674/// ParseCXXMemberSpecification - Parse the class definition.
1675///
1676/// member-specification:
1677/// member-declaration member-specification[opt]
1678/// access-specifier ':' member-specification[opt]
1679///
1680void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001681 unsigned TagType, Decl *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001682 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001683 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001684 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001685
John McCallf312b1e2010-08-26 23:41:50 +00001686 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1687 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Douglas Gregor26997fd2010-01-16 20:52:59 +00001689 // Determine whether this is a non-nested class. Note that local
1690 // classes are *not* considered to be nested classes.
1691 bool NonNestedClass = true;
1692 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001693 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001694 if (S->isClassScope()) {
1695 // We're inside a class scope, so this is a nested class.
1696 NonNestedClass = false;
1697 break;
1698 }
1699
1700 if ((S->getFlags() & Scope::FnScope)) {
1701 // If we're in a function or function template declared in the
1702 // body of a class, then this is a local class rather than a
1703 // nested class.
1704 const Scope *Parent = S->getParent();
1705 if (Parent->isTemplateParamScope())
1706 Parent = Parent->getParent();
1707 if (Parent->isClassScope())
1708 break;
1709 }
1710 }
1711 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001712
1713 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001714 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001715
Douglas Gregor6569d682009-05-27 23:11:45 +00001716 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001717 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001718
Douglas Gregorddc29e12009-02-06 22:42:48 +00001719 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001720 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001721
Anders Carlssonb184a182011-03-25 14:46:08 +00001722 SourceLocation FinalLoc;
1723
1724 // Parse the optional 'final' keyword.
1725 if (getLang().CPlusPlus && Tok.is(tok::identifier)) {
1726 IdentifierInfo *II = Tok.getIdentifierInfo();
1727
1728 // Initialize the contextual keywords.
1729 if (!Ident_final) {
1730 Ident_final = &PP.getIdentifierTable().get("final");
1731 Ident_override = &PP.getIdentifierTable().get("override");
1732 }
1733
1734 if (II == Ident_final)
1735 FinalLoc = ConsumeToken();
1736
1737 if (!getLang().CPlusPlus0x)
1738 Diag(FinalLoc, diag::ext_override_control_keyword) << "final";
1739 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001740
John McCallbd0dfa52009-12-19 21:48:58 +00001741 if (Tok.is(tok::colon)) {
1742 ParseBaseClause(TagDecl);
1743
1744 if (!Tok.is(tok::l_brace)) {
1745 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00001746
1747 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001748 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001749 return;
1750 }
1751 }
1752
1753 assert(Tok.is(tok::l_brace));
1754
1755 SourceLocation LBraceLoc = ConsumeBrace();
1756
John McCall42a4f662010-05-28 08:11:17 +00001757 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00001758 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Anders Carlssondfc2f102011-01-22 17:51:53 +00001759 LBraceLoc);
John McCallf9368152009-12-20 07:58:13 +00001760
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001761 // C++ 11p3: Members of a class defined with the keyword class are private
1762 // by default. Members of a class defined with the keywords struct or union
1763 // are public by default.
1764 AccessSpecifier CurAS;
1765 if (TagType == DeclSpec::TST_class)
1766 CurAS = AS_private;
1767 else
1768 CurAS = AS_public;
1769
Douglas Gregor07976d22010-06-21 22:31:09 +00001770 SourceLocation RBraceLoc;
1771 if (TagDecl) {
1772 // While we still have something to read, read the member-declarations.
1773 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1774 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001775
Douglas Gregor07976d22010-06-21 22:31:09 +00001776 // Check for extraneous top-level semicolon.
1777 if (Tok.is(tok::semi)) {
1778 Diag(Tok, diag::ext_extra_struct_semi)
1779 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1780 << FixItHint::CreateRemoval(Tok.getLocation());
1781 ConsumeToken();
1782 continue;
1783 }
1784
1785 AccessSpecifier AS = getAccessSpecifierIfPresent();
1786 if (AS != AS_none) {
1787 // Current token is a C++ access specifier.
1788 CurAS = AS;
1789 SourceLocation ASLoc = Tok.getLocation();
1790 ConsumeToken();
1791 if (Tok.is(tok::colon))
1792 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1793 else
1794 Diag(Tok, diag::err_expected_colon);
1795 ConsumeToken();
1796 continue;
1797 }
1798
1799 // FIXME: Make sure we don't have a template here.
1800
1801 // Parse all the comma separated declarators.
1802 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001803 }
1804
Douglas Gregor07976d22010-06-21 22:31:09 +00001805 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1806 } else {
1807 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001808 }
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001810 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00001811 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001812 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001813
John McCall42a4f662010-05-28 08:11:17 +00001814 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001815 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall42a4f662010-05-28 08:11:17 +00001816 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00001817 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001818
1819 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1820 // complete within function bodies, default arguments,
1821 // exception-specifications, and constructor ctor-initializers (including
1822 // such things in nested classes).
1823 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001824 // FIXME: Only function bodies and constructor ctor-initializers are
1825 // parsed correctly, fix the rest.
Douglas Gregor07976d22010-06-21 22:31:09 +00001826 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001827 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001828 // are complete and we can parse the delayed portions of method
1829 // declarations and the lexed inline method definitions.
Douglas Gregore0cc0472010-06-16 23:45:56 +00001830 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregor6569d682009-05-27 23:11:45 +00001831 ParseLexedMethodDeclarations(getCurrentClass());
1832 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00001833 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001834 }
1835
John McCall42a4f662010-05-28 08:11:17 +00001836 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001837 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCalldb7bb4a2010-03-17 00:38:33 +00001838
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001839 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001840 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001841 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001842}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001843
1844/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1845/// which explicitly initializes the members or base classes of a
1846/// class (C++ [class.base.init]). For example, the three initializers
1847/// after the ':' in the Derived constructor below:
1848///
1849/// @code
1850/// class Base { };
1851/// class Derived : Base {
1852/// int x;
1853/// float f;
1854/// public:
1855/// Derived(float f) : Base(), x(17), f(f) { }
1856/// };
1857/// @endcode
1858///
Mike Stump1eb44332009-09-09 15:08:12 +00001859/// [C++] ctor-initializer:
1860/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001861///
Mike Stump1eb44332009-09-09 15:08:12 +00001862/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00001863/// mem-initializer ...[opt]
1864/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00001865void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001866 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1867
1868 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Sean Huntcbb67482011-01-08 20:30:50 +00001870 llvm::SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001871 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001872
Douglas Gregor7ad83902008-11-05 04:29:56 +00001873 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00001874 if (Tok.is(tok::code_completion)) {
1875 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
1876 MemInitializers.data(),
1877 MemInitializers.size());
1878 ConsumeCodeCompletionToken();
1879 } else {
1880 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
1881 if (!MemInit.isInvalid())
1882 MemInitializers.push_back(MemInit.get());
1883 else
1884 AnyErrors = true;
1885 }
1886
Douglas Gregor7ad83902008-11-05 04:29:56 +00001887 if (Tok.is(tok::comma))
1888 ConsumeToken();
1889 else if (Tok.is(tok::l_brace))
1890 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00001891 // If the next token looks like a base or member initializer, assume that
1892 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00001893 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
1894 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
1895 Diag(Loc, diag::err_ctor_init_missing_comma)
1896 << FixItHint::CreateInsertion(Loc, ", ");
1897 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001898 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001899 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001900 SkipUntil(tok::l_brace, true, true);
1901 break;
1902 }
1903 } while (true);
1904
Mike Stump1eb44332009-09-09 15:08:12 +00001905 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001906 MemInitializers.data(), MemInitializers.size(),
1907 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001908}
1909
1910/// ParseMemInitializer - Parse a C++ member initializer, which is
1911/// part of a constructor initializer that explicitly initializes one
1912/// member or base class (C++ [class.base.init]). See
1913/// ParseConstructorInitializer for an example.
1914///
1915/// [C++] mem-initializer:
1916/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001917///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001918/// [C++] mem-initializer-id:
1919/// '::'[opt] nested-name-specifier[opt] class-name
1920/// identifier
John McCalld226f652010-08-21 09:40:31 +00001921Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001922 // parse '::'[opt] nested-name-specifier[opt]
1923 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001924 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
1925 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00001926 if (Tok.is(tok::annot_template_id)) {
1927 TemplateIdAnnotation *TemplateId
1928 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001929 if (TemplateId->Kind == TNK_Type_template ||
1930 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001931 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00001932 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00001933 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001934 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001935 }
1936 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001937 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001938 return true;
1939 }
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Douglas Gregor7ad83902008-11-05 04:29:56 +00001941 // Get the identifier. This may be a member name or a class name,
1942 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001943 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001944 SourceLocation IdLoc = ConsumeToken();
1945
1946 // Parse the '('.
1947 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001948 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001949 return true;
1950 }
1951 SourceLocation LParenLoc = ConsumeParen();
1952
1953 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001954 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001955 CommaLocsTy CommaLocs;
1956 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1957 SkipUntil(tok::r_paren);
1958 return true;
1959 }
1960
1961 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1962
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00001963 SourceLocation EllipsisLoc;
1964 if (Tok.is(tok::ellipsis))
1965 EllipsisLoc = ConsumeToken();
1966
Douglas Gregor23c94db2010-07-02 17:43:08 +00001967 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanian96174332009-07-01 19:21:19 +00001968 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001969 LParenLoc, ArgExprs.take(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00001970 ArgExprs.size(), RParenLoc,
1971 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001972}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001973
Sebastian Redl7acafd02011-03-05 14:45:16 +00001974/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001975///
Douglas Gregora4745612008-12-01 18:00:20 +00001976/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00001977/// dynamic-exception-specification
1978/// noexcept-specification
1979///
1980/// noexcept-specification:
1981/// 'noexcept'
1982/// 'noexcept' '(' constant-expression ')'
1983ExceptionSpecificationType
1984Parser::MaybeParseExceptionSpecification(SourceRange &SpecificationRange,
1985 llvm::SmallVectorImpl<ParsedType> &DynamicExceptions,
1986 llvm::SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
1987 ExprResult &NoexceptExpr) {
1988 ExceptionSpecificationType Result = EST_None;
1989
1990 // See if there's a dynamic specification.
1991 if (Tok.is(tok::kw_throw)) {
1992 Result = ParseDynamicExceptionSpecification(SpecificationRange,
1993 DynamicExceptions,
1994 DynamicExceptionRanges);
1995 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
1996 "Produced different number of exception types and ranges.");
1997 }
1998
1999 // If there's no noexcept specification, we're done.
2000 if (Tok.isNot(tok::kw_noexcept))
2001 return Result;
2002
2003 // If we already had a dynamic specification, parse the noexcept for,
2004 // recovery, but emit a diagnostic and don't store the results.
2005 SourceRange NoexceptRange;
2006 ExceptionSpecificationType NoexceptType = EST_None;
2007
2008 SourceLocation KeywordLoc = ConsumeToken();
2009 if (Tok.is(tok::l_paren)) {
2010 // There is an argument.
2011 SourceLocation LParenLoc = ConsumeParen();
2012 NoexceptType = EST_ComputedNoexcept;
2013 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002014 // The argument must be contextually convertible to bool. We use
2015 // ActOnBooleanCondition for this purpose.
2016 if (!NoexceptExpr.isInvalid())
2017 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2018 NoexceptExpr.get());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002019 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2020 NoexceptRange = SourceRange(KeywordLoc, RParenLoc);
2021 } else {
2022 // There is no argument.
2023 NoexceptType = EST_BasicNoexcept;
2024 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2025 }
2026
2027 if (Result == EST_None) {
2028 SpecificationRange = NoexceptRange;
2029 Result = NoexceptType;
2030
2031 // If there's a dynamic specification after a noexcept specification,
2032 // parse that and ignore the results.
2033 if (Tok.is(tok::kw_throw)) {
2034 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2035 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2036 DynamicExceptionRanges);
2037 }
2038 } else {
2039 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2040 }
2041
2042 return Result;
2043}
2044
2045/// ParseDynamicExceptionSpecification - Parse a C++
2046/// dynamic-exception-specification (C++ [except.spec]).
2047///
2048/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002049/// 'throw' '(' type-id-list [opt] ')'
2050/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002051///
Douglas Gregora4745612008-12-01 18:00:20 +00002052/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002053/// type-id ... [opt]
2054/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002055///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002056ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2057 SourceRange &SpecificationRange,
2058 llvm::SmallVectorImpl<ParsedType> &Exceptions,
2059 llvm::SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002060 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002061
Sebastian Redl7acafd02011-03-05 14:45:16 +00002062 SpecificationRange.setBegin(ConsumeToken());
Mike Stump1eb44332009-09-09 15:08:12 +00002063
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002064 if (!Tok.is(tok::l_paren)) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002065 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2066 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002067 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002068 }
2069 SourceLocation LParenLoc = ConsumeParen();
2070
Douglas Gregora4745612008-12-01 18:00:20 +00002071 // Parse throw(...), a Microsoft extension that means "this function
2072 // can throw anything".
2073 if (Tok.is(tok::ellipsis)) {
2074 SourceLocation EllipsisLoc = ConsumeToken();
2075 if (!getLang().Microsoft)
2076 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redl7acafd02011-03-05 14:45:16 +00002077 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2078 SpecificationRange.setEnd(RParenLoc);
Sebastian Redl60618fa2011-03-12 11:50:43 +00002079 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002080 }
2081
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002082 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002083 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002084 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002085 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002086
Douglas Gregora04426c2010-12-20 23:57:46 +00002087 if (Tok.is(tok::ellipsis)) {
2088 // C++0x [temp.variadic]p5:
2089 // - In a dynamic-exception-specification (15.4); the pattern is a
2090 // type-id.
2091 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002092 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002093 if (!Res.isInvalid())
2094 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2095 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002096
Sebastian Redlef65f062009-05-29 18:02:33 +00002097 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002098 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002099 Ranges.push_back(Range);
2100 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002101
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002102 if (Tok.is(tok::comma))
2103 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002104 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002105 break;
2106 }
2107
Sebastian Redl7acafd02011-03-05 14:45:16 +00002108 SpecificationRange.setEnd(MatchRHSPunctuation(tok::r_paren, LParenLoc));
Sebastian Redl60618fa2011-03-12 11:50:43 +00002109 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002110}
Douglas Gregor6569d682009-05-27 23:11:45 +00002111
Douglas Gregordab60ad2010-10-01 18:44:50 +00002112/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2113/// function declaration.
2114TypeResult Parser::ParseTrailingReturnType() {
2115 assert(Tok.is(tok::arrow) && "expected arrow");
2116
2117 ConsumeToken();
2118
2119 // FIXME: Need to suppress declarations when parsing this typename.
2120 // Otherwise in this function definition:
2121 //
2122 // auto f() -> struct X {}
2123 //
2124 // struct X is parsed as class definition because of the trailing
2125 // brace.
2126
2127 SourceRange Range;
2128 return ParseTypeName(&Range);
2129}
2130
Douglas Gregor6569d682009-05-27 23:11:45 +00002131/// \brief We have just started parsing the definition of a new class,
2132/// so push that class onto our stack of classes that is currently
2133/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002134Sema::ParsingClassState
2135Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002136 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002137 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00002138 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
John McCalleee1d542011-02-14 07:13:47 +00002139 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002140}
2141
2142/// \brief Deallocate the given parsed class and all of its nested
2143/// classes.
2144void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002145 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2146 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002147 delete Class;
2148}
2149
2150/// \brief Pop the top class of the stack of classes that are
2151/// currently being parsed.
2152///
2153/// This routine should be called when we have finished parsing the
2154/// definition of a class, but have not yet popped the Scope
2155/// associated with the class's definition.
2156///
2157/// \returns true if the class we've popped is a top-level class,
2158/// false otherwise.
John McCalleee1d542011-02-14 07:13:47 +00002159void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002160 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002161
John McCalleee1d542011-02-14 07:13:47 +00002162 Actions.PopParsingClass(state);
2163
Douglas Gregor6569d682009-05-27 23:11:45 +00002164 ParsingClass *Victim = ClassStack.top();
2165 ClassStack.pop();
2166 if (Victim->TopLevelClass) {
2167 // Deallocate all of the nested classes of this class,
2168 // recursively: we don't need to keep any of this information.
2169 DeallocateParsedClasses(Victim);
2170 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002171 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002172 assert(!ClassStack.empty() && "Missing top-level class?");
2173
Douglas Gregord54eb442010-10-12 16:25:54 +00002174 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002175 // The victim is a nested class, but we will not need to perform
2176 // any processing after the definition of this class since it has
2177 // no members whose handling was delayed. Therefore, we can just
2178 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002179 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002180 return;
2181 }
2182
2183 // This nested class has some members that will need to be processed
2184 // after the top-level class is completely defined. Therefore, add
2185 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002186 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002187 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002188 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00002189}
Sean Huntbbd37c62009-11-21 08:43:09 +00002190
2191/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
2192/// parses standard attributes.
2193///
2194/// [C++0x] attribute-specifier:
2195/// '[' '[' attribute-list ']' ']'
2196///
2197/// [C++0x] attribute-list:
2198/// attribute[opt]
2199/// attribute-list ',' attribute[opt]
2200///
2201/// [C++0x] attribute:
2202/// attribute-token attribute-argument-clause[opt]
2203///
2204/// [C++0x] attribute-token:
2205/// identifier
2206/// attribute-scoped-token
2207///
2208/// [C++0x] attribute-scoped-token:
2209/// attribute-namespace '::' identifier
2210///
2211/// [C++0x] attribute-namespace:
2212/// identifier
2213///
2214/// [C++0x] attribute-argument-clause:
2215/// '(' balanced-token-seq ')'
2216///
2217/// [C++0x] balanced-token-seq:
2218/// balanced-token
2219/// balanced-token-seq balanced-token
2220///
2221/// [C++0x] balanced-token:
2222/// '(' balanced-token-seq ')'
2223/// '[' balanced-token-seq ']'
2224/// '{' balanced-token-seq '}'
2225/// any token but '(', ')', '[', ']', '{', or '}'
John McCall7f040a92010-12-24 02:08:15 +00002226void Parser::ParseCXX0XAttributes(ParsedAttributesWithRange &attrs,
2227 SourceLocation *endLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002228 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
2229 && "Not a C++0x attribute list");
2230
2231 SourceLocation StartLoc = Tok.getLocation(), Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00002232
2233 ConsumeBracket();
2234 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002235
Sean Huntbbd37c62009-11-21 08:43:09 +00002236 if (Tok.is(tok::comma)) {
2237 Diag(Tok.getLocation(), diag::err_expected_ident);
2238 ConsumeToken();
2239 }
2240
2241 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
2242 // attribute not present
2243 if (Tok.is(tok::comma)) {
2244 ConsumeToken();
2245 continue;
2246 }
2247
2248 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
2249 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002250
Sean Huntbbd37c62009-11-21 08:43:09 +00002251 // scoped attribute
2252 if (Tok.is(tok::coloncolon)) {
2253 ConsumeToken();
2254
2255 if (!Tok.is(tok::identifier)) {
2256 Diag(Tok.getLocation(), diag::err_expected_ident);
2257 SkipUntil(tok::r_square, tok::comma, true, true);
2258 continue;
2259 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002260
Sean Huntbbd37c62009-11-21 08:43:09 +00002261 ScopeName = AttrName;
2262 ScopeLoc = AttrLoc;
2263
2264 AttrName = Tok.getIdentifierInfo();
2265 AttrLoc = ConsumeToken();
2266 }
2267
2268 bool AttrParsed = false;
2269 // No scoped names are supported; ideally we could put all non-standard
2270 // attributes into namespaces.
2271 if (!ScopeName) {
2272 switch(AttributeList::getKind(AttrName))
2273 {
2274 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00002275 case AttributeList::AT_carries_dependency:
Anders Carlsson15e14a22011-01-23 21:33:18 +00002276 case AttributeList::AT_noreturn: {
Sean Huntbbd37c62009-11-21 08:43:09 +00002277 if (Tok.is(tok::l_paren)) {
2278 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
2279 << AttrName->getName();
2280 break;
2281 }
2282
John McCall0b7e6782011-03-24 11:26:52 +00002283 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc, 0,
2284 SourceLocation(), 0, 0, false, true);
Sean Huntbbd37c62009-11-21 08:43:09 +00002285 AttrParsed = true;
2286 break;
2287 }
2288
2289 // One argument; must be a type-id or assignment-expression
2290 case AttributeList::AT_aligned: {
2291 if (Tok.isNot(tok::l_paren)) {
2292 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
2293 << AttrName->getName();
2294 break;
2295 }
2296 SourceLocation ParamLoc = ConsumeParen();
2297
John McCall60d7b3a2010-08-24 06:29:42 +00002298 ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002299
2300 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2301
2302 ExprVector ArgExprs(Actions);
2303 ArgExprs.push_back(ArgExpr.release());
John McCall0b7e6782011-03-24 11:26:52 +00002304 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc,
2305 0, ParamLoc, ArgExprs.take(), 1,
2306 false, true);
Sean Huntbbd37c62009-11-21 08:43:09 +00002307
2308 AttrParsed = true;
2309 break;
2310 }
2311
2312 // Silence warnings
2313 default: break;
2314 }
2315 }
2316
2317 // Skip the entire parameter clause, if any
2318 if (!AttrParsed && Tok.is(tok::l_paren)) {
2319 ConsumeParen();
2320 // SkipUntil maintains the balancedness of tokens.
2321 SkipUntil(tok::r_paren, false);
2322 }
2323 }
2324
2325 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2326 SkipUntil(tok::r_square, false);
2327 Loc = Tok.getLocation();
2328 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2329 SkipUntil(tok::r_square, false);
2330
John McCall7f040a92010-12-24 02:08:15 +00002331 attrs.Range = SourceRange(StartLoc, Loc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002332}
2333
2334/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2335/// attribute.
2336///
2337/// FIXME: Simply returns an alignof() expression if the argument is a
2338/// type. Ideally, the type should be propagated directly into Sema.
2339///
2340/// [C++0x] 'align' '(' type-id ')'
2341/// [C++0x] 'align' '(' assignment-expression ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002342ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002343 if (isTypeIdInParens()) {
John McCallf312b1e2010-08-26 23:41:50 +00002344 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sean Huntbbd37c62009-11-21 08:43:09 +00002345 SourceLocation TypeLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002346 ParsedType Ty = ParseTypeName().get();
Sean Huntbbd37c62009-11-21 08:43:09 +00002347 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002348 return Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2349 Ty.getAsOpaquePtr(), TypeRange);
Sean Huntbbd37c62009-11-21 08:43:09 +00002350 } else
2351 return ParseConstantExpression();
2352}
Francois Pichet334d47e2010-10-11 12:59:39 +00002353
2354/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
2355///
2356/// [MS] ms-attribute:
2357/// '[' token-seq ']'
2358///
2359/// [MS] ms-attribute-seq:
2360/// ms-attribute[opt]
2361/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00002362void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
2363 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00002364 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
2365
2366 while (Tok.is(tok::l_square)) {
2367 ConsumeBracket();
2368 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00002369 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00002370 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
2371 }
2372}