blob: a384fab93504f6ee26f08ef31285c067e3b3b812 [file] [log] [blame]
Chris Lattnera5235172007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera5235172007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson74d7f0d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor423984d2008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Chris Lattnera5235172007-08-25 06:57:03 +000022using namespace clang;
23
24/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redl67667942010-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 Lattnera5235172007-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 Redl67667942010-08-27 23:12:46 +000033/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000034///
35/// named-namespace-definition:
36/// original-namespace-definition
37/// extension-namespace-definition
38///
39/// original-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000040/// 'inline'[opt] 'namespace' identifier attributes[opt]
41/// '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000042///
43/// extension-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000044/// 'inline'[opt] 'namespace' original-namespace-name
45/// '{' namespace-body '}'
Mike Stump11289f42009-09-09 15:08:12 +000046///
Chris Lattnera5235172007-08-25 06:57:03 +000047/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
48/// 'namespace' identifier '=' qualified-namespace-specifier ';'
49///
John McCall48871652010-08-21 09:40:31 +000050Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redl67667942010-08-27 23:12:46 +000051 SourceLocation &DeclEnd,
52 SourceLocation InlineLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000053 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000054 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump11289f42009-09-09 15:08:12 +000055
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000056 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000057 Actions.CodeCompleteNamespaceDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +000058 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000059 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000060
Chris Lattnera5235172007-08-25 06:57:03 +000061 SourceLocation IdentLoc;
62 IdentifierInfo *Ident = 0;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000063
64 Token attrTok;
Mike Stump11289f42009-09-09 15:08:12 +000065
Chris Lattner76c72282007-10-09 17:33:22 +000066 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000067 Ident = Tok.getIdentifierInfo();
68 IdentLoc = ConsumeToken(); // eat the identifier.
69 }
Mike Stump11289f42009-09-09 15:08:12 +000070
Chris Lattnera5235172007-08-25 06:57:03 +000071 // Read label attributes, if present.
John McCall084e83d2011-03-24 11:26:52 +000072 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000073 if (Tok.is(tok::kw___attribute)) {
74 attrTok = Tok;
John McCall53fa7142010-12-24 02:08:15 +000075 ParseGNUAttributes(attrs);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000076 }
Mike Stump11289f42009-09-09 15:08:12 +000077
Douglas Gregor6b6bba42009-06-17 19:49:00 +000078 if (Tok.is(tok::equal)) {
John McCall53fa7142010-12-24 02:08:15 +000079 if (!attrs.empty())
Douglas Gregor6b6bba42009-06-17 19:49:00 +000080 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redl67667942010-08-27 23:12:46 +000081 if (InlineLoc.isValid())
82 Diag(InlineLoc, diag::err_inline_namespace_alias)
83 << FixItHint::CreateRemoval(InlineLoc);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000084
Chris Lattner49836b42009-04-02 04:16:50 +000085 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000086 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattner4de55aa2009-03-29 14:02:43 +000088 if (Tok.isNot(tok::l_brace)) {
Mike Stump11289f42009-09-09 15:08:12 +000089 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner4de55aa2009-03-29 14:02:43 +000090 diag::err_expected_ident_lbrace);
John McCall48871652010-08-21 09:40:31 +000091 return 0;
Chris Lattnera5235172007-08-25 06:57:03 +000092 }
Mike Stump11289f42009-09-09 15:08:12 +000093
Chris Lattner4de55aa2009-03-29 14:02:43 +000094 SourceLocation LBrace = ConsumeBrace();
95
Douglas Gregor0be31a22010-07-02 17:43:08 +000096 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
97 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
98 getCurScope()->getFnParent()) {
Douglas Gregor05cfc292010-05-14 05:08:22 +000099 Diag(LBrace, diag::err_namespace_nonnamespace_scope);
100 SkipUntil(tok::r_brace, false);
John McCall48871652010-08-21 09:40:31 +0000101 return 0;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000102 }
103
Sebastian Redl5a5f2c72010-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 Lattner4de55aa2009-03-29 14:02:43 +0000108 // Enter a scope for the namespace.
109 ParseScope NamespaceScope(this, Scope::DeclScope);
110
John McCall48871652010-08-21 09:40:31 +0000111 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000112 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
113 IdentLoc, Ident, LBrace, attrs.getList());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000114
John McCallfaf5fb42010-08-26 23:41:50 +0000115 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
116 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000117
Alexis Hunt96d5c762009-11-21 08:43:09 +0000118 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall084e83d2011-03-24 11:26:52 +0000119 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000120 MaybeParseCXX0XAttributes(attrs);
121 MaybeParseMicrosoftAttributes(attrs);
122 ParseExternalDeclaration(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000123 }
Mike Stump11289f42009-09-09 15:08:12 +0000124
Chris Lattner4de55aa2009-03-29 14:02:43 +0000125 // Leave the namespace scope.
126 NamespaceScope.Exit();
127
Chris Lattner49836b42009-04-02 04:16:50 +0000128 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
129 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000130
Chris Lattner49836b42009-04-02 04:16:50 +0000131 DeclEnd = RBraceLoc;
Chris Lattner4de55aa2009-03-29 14:02:43 +0000132 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000133}
Chris Lattner38376f12008-01-12 07:05:38 +0000134
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000135/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
136/// alias definition.
137///
John McCall48871652010-08-21 09:40:31 +0000138Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall084e83d2011-03-24 11:26:52 +0000139 SourceLocation AliasLoc,
140 IdentifierInfo *Alias,
141 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000142 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000143
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000144 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000145
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000146 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000147 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000148 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000149 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000150
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000151 CXXScopeSpec SS;
152 // Parse (optional) nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000153 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Anders Carlsson1894f0d42009-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 McCall48871652010-08-21 09:40:31 +0000159 return 0;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000160 }
161
162 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000163 IdentifierInfo *Ident = Tok.getIdentifierInfo();
164 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000165
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000166 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000167 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000168 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
169 "", tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000170
Douglas Gregor0be31a22010-07-02 17:43:08 +0000171 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000172 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000173}
174
Chris Lattner38376f12008-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 Lattner8ea64422010-11-09 20:15:55 +0000182Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregor15799fd2008-11-21 16:10:08 +0000183 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000184 llvm::SmallString<8> LangBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000185 bool Invalid = false;
186 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
187 if (Invalid)
John McCall48871652010-08-21 09:40:31 +0000188 return 0;
Chris Lattner38376f12008-01-12 07:05:38 +0000189
190 SourceLocation Loc = ConsumeStringToken();
Chris Lattner38376f12008-01-12 07:05:38 +0000191
Douglas Gregor07665a62009-01-05 19:45:36 +0000192 ParseScope LinkageScope(this, Scope::DeclScope);
John McCall48871652010-08-21 09:40:31 +0000193 Decl *LinkageSpec
Douglas Gregor0be31a22010-07-02 17:43:08 +0000194 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000195 DS.getSourceRange().getBegin(),
Benjamin Kramerbebee842010-05-03 13:08:54 +0000196 Loc, Lang,
Abramo Bagnaraea947882011-03-08 16:41:52 +0000197 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor07665a62009-01-05 19:45:36 +0000198 : SourceLocation());
199
John McCall084e83d2011-03-24 11:26:52 +0000200 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000201 MaybeParseCXX0XAttributes(attrs);
202 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000203
Douglas Gregor07665a62009-01-05 19:45:36 +0000204 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000205 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000206 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000207 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor07665a62009-01-05 19:45:36 +0000208 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000209 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000210
Douglas Gregorb65a9132010-02-07 08:38:28 +0000211 DS.abort();
212
John McCall53fa7142010-12-24 02:08:15 +0000213 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000214
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000215 SourceLocation LBrace = ConsumeBrace();
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000216 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall084e83d2011-03-24 11:26:52 +0000217 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000218 MaybeParseCXX0XAttributes(attrs);
219 MaybeParseMicrosoftAttributes(attrs);
220 ParseExternalDeclaration(attrs);
Chris Lattner38376f12008-01-12 07:05:38 +0000221 }
222
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000223 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Chris Lattner8ea64422010-11-09 20:15:55 +0000224 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
225 RBrace);
Chris Lattner38376f12008-01-12 07:05:38 +0000226}
Douglas Gregor556877c2008-04-13 21:30:24 +0000227
Douglas Gregord7c4d982008-12-30 03:27:21 +0000228/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
229/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000230Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000231 const ParsedTemplateInfo &TemplateInfo,
232 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000233 ParsedAttributesWithRange &attrs) {
Douglas Gregord7c4d982008-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 Gregor7e90c6d2009-09-18 19:03:04 +0000239 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000240 Actions.CodeCompleteUsing(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000241 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000242 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000243
John McCall9b72f892010-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 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000252
John McCall53fa7142010-12-24 02:08:15 +0000253 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000254 }
255
256 // Otherwise, it must be a using-declaration.
257
258 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000259 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000260
John McCall9b72f892010-11-10 02:40:36 +0000261 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd);
Douglas Gregord7c4d982008-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 McCall48871652010-08-21 09:40:31 +0000274Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000275 SourceLocation UsingLoc,
276 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000277 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-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 Gregor7e90c6d2009-09-18 19:03:04 +0000283 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000284 Actions.CodeCompleteUsingDirective(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000285 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000286 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000287
Douglas Gregord7c4d982008-12-30 03:27:21 +0000288 CXXScopeSpec SS;
289 // Parse (optional) nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000290 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000291
Douglas Gregord7c4d982008-12-30 03:27:21 +0000292 IdentifierInfo *NamespcName = 0;
293 SourceLocation IdentLoc = SourceLocation();
294
295 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000296 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-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 McCall48871652010-08-21 09:40:31 +0000301 return 0;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000302 }
Mike Stump11289f42009-09-09 15:08:12 +0000303
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000304 // Parse identifier.
305 NamespcName = Tok.getIdentifierInfo();
306 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000307
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000308 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000309 bool GNUAttr = false;
310 if (Tok.is(tok::kw___attribute)) {
311 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000312 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000313 }
Mike Stump11289f42009-09-09 15:08:12 +0000314
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000315 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000316 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000317 ExpectAndConsume(tok::semi,
Douglas Gregor45d6bdf2010-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 Gregord7c4d982008-12-30 03:27:21 +0000321
Douglas Gregor0be31a22010-07-02 17:43:08 +0000322 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000323 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-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 Gregorfec52632009-06-20 00:51:54 +0000331/// unqualified-id
332/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000333///
John McCall48871652010-08-21 09:40:31 +0000334Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000335 const ParsedTemplateInfo &TemplateInfo,
336 SourceLocation UsingLoc,
337 SourceLocation &DeclEnd,
338 AccessSpecifier AS) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000339 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000340 SourceLocation TypenameLoc;
Douglas Gregorfec52632009-06-20 00:51:54 +0000341 bool IsTypeName;
342
John McCall9b72f892010-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 Gregorfec52632009-06-20 00:51:54 +0000347 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000348 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregorfec52632009-06-20 00:51:54 +0000349 if (Tok.is(tok::kw_typename)) {
John McCalle61f2ba2009-11-18 02:36:19 +0000350 TypenameLoc = Tok.getLocation();
Douglas Gregorfec52632009-06-20 00:51:54 +0000351 ConsumeToken();
352 IsTypeName = true;
353 }
354 else
355 IsTypeName = false;
356
357 // Parse nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000358 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregorfec52632009-06-20 00:51:54 +0000359
Douglas Gregorfec52632009-06-20 00:51:54 +0000360 // Check nested-name specifier.
361 if (SS.isInvalid()) {
362 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000363 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000364 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000365
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000366 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000367 // destructor names and allow the action module to diagnose any semantic
368 // errors.
369 UnqualifiedId Name;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000370 if (ParseUnqualifiedId(SS,
Douglas Gregor220f4272009-11-04 16:30:06 +0000371 /*EnteringContext=*/false,
372 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000373 /*AllowConstructorName=*/true,
John McCallba7bf592010-08-24 05:47:05 +0000374 ParsedType(),
Douglas Gregor220f4272009-11-04 16:30:06 +0000375 Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000376 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000377 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000378 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000379
Douglas Gregorfec52632009-06-20 00:51:54 +0000380 // Parse (optional) attributes (most likely GNU strong-using extension).
John McCall084e83d2011-03-24 11:26:52 +0000381 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000382 MaybeParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +0000383
Douglas Gregorfec52632009-06-20 00:51:54 +0000384 // Eat ';'.
385 DeclEnd = Tok.getLocation();
386 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
John McCall53fa7142010-12-24 02:08:15 +0000387 !attrs.empty() ? "attributes list" : "using declaration",
Douglas Gregor220f4272009-11-04 16:30:06 +0000388 tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000389
John McCall9b72f892010-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 Kremenek5eec2b02010-11-10 05:59:39 +0000402 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000403 Name, attrs.getList(),
404 IsTypeName, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000405}
406
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000407/// ParseStaticAssertDeclaration - Parse C++0x or C1X static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000408///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000409/// [C++0x] static_assert-declaration:
410/// static_assert ( constant-expression , string-literal ) ;
411///
412/// [C1X] static_assert-declaration:
413/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000414///
John McCall48871652010-08-21 09:40:31 +0000415Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000416 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
417 "Not a static_assert declaration");
418
419 if (Tok.is(tok::kw__Static_assert) && !getLang().C1X)
420 Diag(Tok, diag::ext_c1x_static_assert);
421
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000422 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000423
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000424 if (Tok.isNot(tok::l_paren)) {
425 Diag(Tok, diag::err_expected_lparen);
John McCall48871652010-08-21 09:40:31 +0000426 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000427 }
Mike Stump11289f42009-09-09 15:08:12 +0000428
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000429 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000430
John McCalldadc5752010-08-24 06:29:42 +0000431 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000432 if (AssertExpr.isInvalid()) {
433 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000434 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000435 }
Mike Stump11289f42009-09-09 15:08:12 +0000436
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000437 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCall48871652010-08-21 09:40:31 +0000438 return 0;
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000439
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000440 if (Tok.isNot(tok::string_literal)) {
441 Diag(Tok, diag::err_expected_string_literal);
442 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000443 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000444 }
Mike Stump11289f42009-09-09 15:08:12 +0000445
John McCalldadc5752010-08-24 06:29:42 +0000446 ExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump11289f42009-09-09 15:08:12 +0000447 if (AssertMessage.isInvalid())
John McCall48871652010-08-21 09:40:31 +0000448 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000449
Abramo Bagnaraea947882011-03-08 16:41:52 +0000450 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000451
Chris Lattner49836b42009-04-02 04:16:50 +0000452 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000453 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000454
John McCallb268a282010-08-23 23:25:46 +0000455 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
456 AssertExpr.take(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000457 AssertMessage.take(),
458 RParenLoc);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000459}
460
Anders Carlsson74948d02009-06-24 17:47:40 +0000461/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
462///
463/// 'decltype' ( expression )
464///
465void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
466 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
467
468 SourceLocation StartLoc = ConsumeToken();
469 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000470
471 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson74948d02009-06-24 17:47:40 +0000472 "decltype")) {
473 SkipUntil(tok::r_paren);
474 return;
475 }
Mike Stump11289f42009-09-09 15:08:12 +0000476
Anders Carlsson74948d02009-06-24 17:47:40 +0000477 // Parse the expression
Mike Stump11289f42009-09-09 15:08:12 +0000478
Anders Carlsson74948d02009-06-24 17:47:40 +0000479 // C++0x [dcl.type.simple]p4:
480 // The operand of the decltype specifier is an unevaluated operand.
481 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallfaf5fb42010-08-26 23:41:50 +0000482 Sema::Unevaluated);
John McCalldadc5752010-08-24 06:29:42 +0000483 ExprResult Result = ParseExpression();
Anders Carlsson74948d02009-06-24 17:47:40 +0000484 if (Result.isInvalid()) {
485 SkipUntil(tok::r_paren);
486 return;
487 }
Mike Stump11289f42009-09-09 15:08:12 +0000488
Anders Carlsson74948d02009-06-24 17:47:40 +0000489 // Match the ')'
490 SourceLocation RParenLoc;
491 if (Tok.is(tok::r_paren))
492 RParenLoc = ConsumeParen();
493 else
494 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000495
Anders Carlsson74948d02009-06-24 17:47:40 +0000496 if (RParenLoc.isInvalid())
497 return;
498
499 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000500 unsigned DiagID;
Anders Carlsson74948d02009-06-24 17:47:40 +0000501 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump11289f42009-09-09 15:08:12 +0000502 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000503 DiagID, Result.release()))
504 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson74948d02009-06-24 17:47:40 +0000505}
506
Douglas Gregor831c93f2008-11-05 20:51:48 +0000507/// ParseClassName - Parse a C++ class-name, which names a class. Note
508/// that we only check that the result names a type; semantic analysis
509/// will need to verify that the type names a class. The result is
Douglas Gregord54dfb82009-02-25 23:52:28 +0000510/// either a type or NULL, depending on whether a type name was
Douglas Gregor831c93f2008-11-05 20:51:48 +0000511/// found.
512///
513/// class-name: [C++ 9.1]
514/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000515/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000516///
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000517Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Douglas Gregore7c20652011-03-02 00:47:37 +0000518 CXXScopeSpec &SS) {
Douglas Gregord54dfb82009-02-25 23:52:28 +0000519 // Check whether we have a template-id that names a type.
520 if (Tok.is(tok::annot_template_id)) {
Mike Stump11289f42009-09-09 15:08:12 +0000521 TemplateIdAnnotation *TemplateId
Douglas Gregord54dfb82009-02-25 23:52:28 +0000522 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregor46c59612010-01-12 17:52:59 +0000523 if (TemplateId->Kind == TNK_Type_template ||
524 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000525 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000526
527 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000528 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000529 EndLocation = Tok.getAnnotationEndLoc();
530 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000531
532 if (Type)
533 return Type;
534 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000535 }
536
537 // Fall through to produce an error below.
538 }
539
Douglas Gregor831c93f2008-11-05 20:51:48 +0000540 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000541 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000542 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000543 }
544
Douglas Gregor18473f32010-01-12 21:28:44 +0000545 IdentifierInfo *Id = Tok.getIdentifierInfo();
546 SourceLocation IdLoc = ConsumeToken();
547
548 if (Tok.is(tok::less)) {
549 // It looks the user intended to write a template-id here, but the
550 // template-name was wrong. Try to fix that.
551 TemplateNameKind TNK = TNK_Type_template;
552 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000553 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000554 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000555 Diag(IdLoc, diag::err_unknown_template_name)
556 << Id;
557 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000558
Douglas Gregor18473f32010-01-12 21:28:44 +0000559 if (!Template)
560 return true;
561
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000562 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000563 UnqualifiedId TemplateName;
564 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000565
Douglas Gregor18473f32010-01-12 21:28:44 +0000566 // Parse the full template-id, then turn it into a type.
567 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
568 SourceLocation(), true))
569 return true;
570 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +0000571 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000572
Douglas Gregor18473f32010-01-12 21:28:44 +0000573 // If we didn't end up with a typename token, there's nothing more we
574 // can do.
575 if (Tok.isNot(tok::annot_typename))
576 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000577
Douglas Gregor18473f32010-01-12 21:28:44 +0000578 // Retrieve the type from the annotation token, consume that token, and
579 // return.
580 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +0000581 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +0000582 ConsumeToken();
583 return Type;
584 }
585
Douglas Gregor831c93f2008-11-05 20:51:48 +0000586 // We have an identifier; check whether it is actually a type.
Douglas Gregore7c20652011-03-02 00:47:37 +0000587 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +0000588 false, ParsedType(),
589 /*NonTrivialTypeSourceInfo=*/true);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000590 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +0000591 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000592 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000593 }
594
595 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +0000596 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000597
598 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000599 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +0000600 DS.SetRangeStart(IdLoc);
601 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +0000602 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000603
604 const char *PrevSpec = 0;
605 unsigned DiagID;
606 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
607
608 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
609 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000610}
611
Douglas Gregor556877c2008-04-13 21:30:24 +0000612/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
613/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
614/// until we reach the start of a definition or see a token that
Sebastian Redl2b372722010-02-03 21:21:43 +0000615/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregor556877c2008-04-13 21:30:24 +0000616///
617/// class-specifier: [C++ class]
618/// class-head '{' member-specification[opt] '}'
619/// class-head '{' member-specification[opt] '}' attributes[opt]
620/// class-head:
621/// class-key identifier[opt] base-clause[opt]
622/// class-key nested-name-specifier identifier base-clause[opt]
623/// class-key nested-name-specifier[opt] simple-template-id
624/// base-clause[opt]
625/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000626/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +0000627/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000628/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +0000629/// simple-template-id base-clause[opt]
630/// class-key:
631/// 'class'
632/// 'struct'
633/// 'union'
634///
635/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +0000636/// class-key ::[opt] nested-name-specifier[opt] identifier
637/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
638/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +0000639///
640/// Note that the C++ class-specifier and elaborated-type-specifier,
641/// together, subsume the C99 struct-or-union-specifier:
642///
643/// struct-or-union-specifier: [C99 6.7.2.1]
644/// struct-or-union identifier[opt] '{' struct-contents '}'
645/// struct-or-union identifier
646/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
647/// '}' attributes[opt]
648/// [GNU] struct-or-union attributes[opt] identifier
649/// struct-or-union:
650/// 'struct'
651/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000652void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
653 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000654 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redl2b372722010-02-03 21:21:43 +0000655 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000656 DeclSpec::TST TagType;
657 if (TagTokKind == tok::kw_struct)
658 TagType = DeclSpec::TST_struct;
659 else if (TagTokKind == tok::kw_class)
660 TagType = DeclSpec::TST_class;
661 else {
662 assert(TagTokKind == tok::kw_union && "Not a class specifier");
663 TagType = DeclSpec::TST_union;
664 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000665
Douglas Gregorf45b0cf2009-09-18 15:37:17 +0000666 if (Tok.is(tok::code_completion)) {
667 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000668 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000669 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +0000670 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000671
Chandler Carruth2d69ec72010-06-28 08:39:25 +0000672 // C++03 [temp.explicit] 14.7.2/8:
673 // The usual access checking rules do not apply to names used to specify
674 // explicit instantiations.
675 //
676 // As an extension we do not perform access checking on the names used to
677 // specify explicit specializations either. This is important to allow
678 // specializing traits classes for private types.
679 bool SuppressingAccessChecks = false;
680 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
681 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
682 Actions.ActOnStartSuppressingAccessChecks();
683 SuppressingAccessChecks = true;
684 }
685
John McCall084e83d2011-03-24 11:26:52 +0000686 ParsedAttributes attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +0000687 // If attributes exist after tag, parse them.
688 if (Tok.is(tok::kw___attribute))
John McCall53fa7142010-12-24 02:08:15 +0000689 ParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +0000690
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000691 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +0000692 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +0000693 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000694
Alexis Hunt96d5c762009-11-21 08:43:09 +0000695 // If C++0x attributes exist here, parse them.
696 // FIXME: Are we consistent with the ordering of parsing of different
697 // styles of attributes?
John McCall53fa7142010-12-24 02:08:15 +0000698 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +0000699
Douglas Gregor119b0c72009-09-04 05:53:02 +0000700 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
701 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
702 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump11289f42009-09-09 15:08:12 +0000703 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregor119b0c72009-09-04 05:53:02 +0000704 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
705 // properly.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000706 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregor119b0c72009-09-04 05:53:02 +0000707 Tok.setKind(tok::identifier);
708 }
709
710 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
711 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
712 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump11289f42009-09-09 15:08:12 +0000713 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregor119b0c72009-09-04 05:53:02 +0000714 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
715 // properly.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000716 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregor119b0c72009-09-04 05:53:02 +0000717 Tok.setKind(tok::identifier);
718 }
Mike Stump11289f42009-09-09 15:08:12 +0000719
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000720 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +0000721 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000722 if (getLang().CPlusPlus) {
723 // "FOO : BAR" is not a potential typo for "FOO::BAR".
724 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000725
John McCallba7bf592010-08-24 05:47:05 +0000726 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true))
John McCall413021a2010-07-30 06:26:29 +0000727 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +0000728 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000729 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
730 Diag(Tok, diag::err_expected_ident);
731 }
Douglas Gregor67a65642009-02-17 23:15:12 +0000732
Douglas Gregor916462b2009-10-30 21:46:58 +0000733 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
734
Douglas Gregor67a65642009-02-17 23:15:12 +0000735 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +0000736 IdentifierInfo *Name = 0;
737 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +0000738 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +0000739 if (Tok.is(tok::identifier)) {
740 Name = Tok.getIdentifierInfo();
741 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000742
Douglas Gregord5a479c2010-05-30 22:30:21 +0000743 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000744 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +0000745 // Eat the template argument list and try to continue parsing this as
746 // a class (or template thereof).
747 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +0000748 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +0000749 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +0000750 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000751 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +0000752 // We couldn't parse the template argument list at all, so don't
753 // try to give any location information for the list.
754 LAngleLoc = RAngleLoc = SourceLocation();
755 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000756
Douglas Gregor916462b2009-10-30 21:46:58 +0000757 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000758 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor916462b2009-10-30 21:46:58 +0000759 << (TagType == DeclSpec::TST_class? 0
760 : TagType == DeclSpec::TST_struct? 1
761 : 2)
762 << Name
763 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000764
765 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000766 // we've removed its template argument list.
767 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
768 if (TemplateParams && TemplateParams->size() > 1) {
769 TemplateParams->pop_back();
770 } else {
771 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000772 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000773 = ParsedTemplateInfo::NonTemplate;
774 }
775 } else if (TemplateInfo.Kind
776 == ParsedTemplateInfo::ExplicitInstantiation) {
777 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +0000778 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000779 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +0000780 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000781 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000782 = SourceLocation();
783 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
784 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +0000785 }
Douglas Gregor916462b2009-10-30 21:46:58 +0000786 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000787 } else if (Tok.is(tok::annot_template_id)) {
788 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
789 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +0000790
Douglas Gregore7c20652011-03-02 00:47:37 +0000791 if (TemplateId->Kind != TNK_Type_template &&
792 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000793 // The template-name in the simple-template-id refers to
794 // something other than a class template. Give an appropriate
795 // error message and skip to the ';'.
796 SourceRange Range(NameLoc);
797 if (SS.isNotEmpty())
798 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +0000799
Douglas Gregor7f741122009-02-25 19:37:18 +0000800 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
801 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +0000802
Douglas Gregor7f741122009-02-25 19:37:18 +0000803 DS.SetTypeSpecError();
804 SkipUntil(tok::semi, false, true);
805 TemplateId->Destroy();
Chandler Carruth2d69ec72010-06-28 08:39:25 +0000806 if (SuppressingAccessChecks)
807 Actions.ActOnStopSuppressingAccessChecks();
808
Douglas Gregor7f741122009-02-25 19:37:18 +0000809 return;
Douglas Gregor67a65642009-02-17 23:15:12 +0000810 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000811 }
812
Chandler Carruth2d69ec72010-06-28 08:39:25 +0000813 // As soon as we're finished parsing the class's template-id, turn access
814 // checking back on.
815 if (SuppressingAccessChecks)
816 Actions.ActOnStopSuppressingAccessChecks();
817
John McCall07e91c02009-08-06 02:15:43 +0000818 // There are four options here. If we have 'struct foo;', then this
819 // is either a forward declaration or a friend declaration, which
Anders Carlsson4b63d0e2011-01-22 16:56:46 +0000820 // have to be treated differently. If we have 'struct foo {...',
Anders Carlsson65c76d32011-03-25 14:55:14 +0000821 // 'struct foo :...' or 'struct foo final[opt]' then this is a
Anders Carlsson4b63d0e2011-01-22 16:56:46 +0000822 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Sebastian Redl2b372722010-02-03 21:21:43 +0000823 // However, in some contexts, things look like declarations but are just
824 // references, e.g.
825 // new struct s;
826 // or
827 // &T::operator struct s;
828 // For these, SuppressDeclarations is true.
John McCallfaf5fb42010-08-26 23:41:50 +0000829 Sema::TagUseKind TUK;
Sebastian Redl2b372722010-02-03 21:21:43 +0000830 if (SuppressDeclarations)
John McCallfaf5fb42010-08-26 23:41:50 +0000831 TUK = Sema::TUK_Reference;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +0000832 else if (Tok.is(tok::l_brace) ||
833 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlssoncafbab72011-03-25 14:53:29 +0000834 isCXX0XFinalKeyword()) {
Douglas Gregor3dad8422009-09-26 06:47:28 +0000835 if (DS.isFriendSpecified()) {
836 // C++ [class.friend]p2:
837 // A class shall not be defined in a friend declaration.
838 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
839 << SourceRange(DS.getFriendSpecLoc());
840
841 // Skip everything up to the semicolon, so that this looks like a proper
842 // friend class (or template thereof) declaration.
843 SkipUntil(tok::semi, true, true);
John McCallfaf5fb42010-08-26 23:41:50 +0000844 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +0000845 } else {
846 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +0000847 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +0000848 }
849 } else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +0000850 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregor556877c2008-04-13 21:30:24 +0000851 else
John McCallfaf5fb42010-08-26 23:41:50 +0000852 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +0000853
John McCall413021a2010-07-30 06:26:29 +0000854 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +0000855 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +0000856 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
857 // We have a declaration or reference to an anonymous class.
858 Diag(StartLoc, diag::err_anon_type_definition)
859 << DeclSpec::getSpecifierName(TagType);
860 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000861
Douglas Gregor556877c2008-04-13 21:30:24 +0000862 SkipUntil(tok::comma, true);
Douglas Gregor7f741122009-02-25 19:37:18 +0000863
864 if (TemplateId)
865 TemplateId->Destroy();
Douglas Gregor556877c2008-04-13 21:30:24 +0000866 return;
867 }
868
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000869 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +0000870 DeclResult TagOrTempResult = true; // invalid
871 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000872
Douglas Gregord6ab8742009-05-28 23:31:59 +0000873 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +0000874 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000875 // Explicit specialization, class template partial specialization,
876 // or explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +0000877 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor7f741122009-02-25 19:37:18 +0000878 TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +0000879 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000880 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +0000881 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000882 // This is an explicit instantiation of a class template.
883 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +0000884 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +0000885 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000886 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000887 TagType,
Mike Stump11289f42009-09-09 15:08:12 +0000888 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000889 SS,
John McCall3e56fd42010-08-23 07:28:44 +0000890 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +0000891 TemplateId->TemplateNameLoc,
892 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000893 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +0000894 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +0000895 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +0000896
897 // Friend template-ids are treated as references unless
898 // they have template headers, in which case they're ill-formed
899 // (FIXME: "template <class T> friend class A<T>::B<int>;").
900 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +0000901 } else if (TUK == Sema::TUK_Reference ||
902 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +0000903 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000904 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType,
905 StartLoc,
906 TemplateId->SS,
907 TemplateId->Template,
908 TemplateId->TemplateNameLoc,
909 TemplateId->LAngleLoc,
910 TemplateArgsPtr,
911 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000912 } else {
913 // This is an explicit specialization or a class template
914 // partial specialization.
915 TemplateParameterLists FakedParamLists;
916
917 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
918 // This looks like an explicit instantiation, because we have
919 // something like
920 //
921 // template class Foo<X>
922 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000923 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000924 // meant to be an explicit specialization, but the user forgot
925 // the '<>' after 'template'.
John McCallfaf5fb42010-08-26 23:41:50 +0000926 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000927
Mike Stump11289f42009-09-09 15:08:12 +0000928 SourceLocation LAngleLoc
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000929 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000930 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000931 diag::err_explicit_instantiation_with_definition)
932 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregora771f462010-03-31 17:46:05 +0000933 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000934
935 // Create a fake template parameter list that contains only
936 // "template<>", so that we treat this construct as a class
937 // template specialization.
938 FakedParamLists.push_back(
Mike Stump11289f42009-09-09 15:08:12 +0000939 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000940 TemplateInfo.TemplateLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000941 LAngleLoc,
942 0, 0,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000943 LAngleLoc));
944 TemplateParams = &FakedParamLists;
945 }
946
947 // Build the class template specialization.
948 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +0000949 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor7f741122009-02-25 19:37:18 +0000950 StartLoc, SS,
John McCall3e56fd42010-08-23 07:28:44 +0000951 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +0000952 TemplateId->TemplateNameLoc,
953 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +0000954 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +0000955 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +0000956 attrs.getList(),
John McCallfaf5fb42010-08-26 23:41:50 +0000957 MultiTemplateParamsArg(Actions,
Douglas Gregor67a65642009-02-17 23:15:12 +0000958 TemplateParams? &(*TemplateParams)[0] : 0,
959 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000960 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000961 TemplateId->Destroy();
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000962 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +0000963 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000964 // Explicit instantiation of a member of a class template
965 // specialization, e.g.,
966 //
967 // template struct Outer<int>::Inner;
968 //
969 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +0000970 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +0000971 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000972 TemplateInfo.TemplateLoc,
973 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +0000974 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +0000975 } else if (TUK == Sema::TUK_Friend &&
976 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
977 TagOrTempResult =
978 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
979 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000980 Name, NameLoc, attrs.getList(),
John McCallace48cd2010-10-19 01:40:49 +0000981 MultiTemplateParamsArg(Actions,
982 TemplateParams? &(*TemplateParams)[0] : 0,
983 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000984 } else {
985 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +0000986 TUK == Sema::TUK_Definition) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000987 // FIXME: Diagnose this particular error.
988 }
989
John McCall7f41d982009-09-11 04:59:25 +0000990 bool IsDependent = false;
991
John McCall32723e92010-10-19 18:40:57 +0000992 // Don't pass down template parameter lists if this is just a tag
993 // reference. For example, we don't need the template parameters here:
994 // template <class T> class A *makeA(T t);
995 MultiTemplateParamsArg TParams;
996 if (TUK != Sema::TUK_Reference && TemplateParams)
997 TParams =
998 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
999
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001000 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001001 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001002 SS, Name, NameLoc, attrs.getList(), AS,
John McCall32723e92010-10-19 18:40:57 +00001003 TParams, Owned, IsDependent, false,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001004 false, clang::TypeResult());
John McCall7f41d982009-09-11 04:59:25 +00001005
1006 // If ActOnTag said the type was dependent, try again with the
1007 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001008 if (IsDependent) {
1009 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001010 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001011 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001012 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001013 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001014
Douglas Gregor556877c2008-04-13 21:30:24 +00001015 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001016 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001017 assert(Tok.is(tok::l_brace) ||
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001018 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlssoncafbab72011-03-25 14:53:29 +00001019 isCXX0XFinalKeyword());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001020 if (getLang().CPlusPlus)
Douglas Gregorc08f4892009-03-25 00:13:59 +00001021 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001022 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001023 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001024 }
1025
John McCallba7bf592010-08-24 05:47:05 +00001026 const char *PrevSpec = 0;
1027 unsigned DiagID;
1028 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001029 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001030 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1031 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallba7bf592010-08-24 05:47:05 +00001032 PrevSpec, DiagID, TypeResult.get());
John McCall7f41d982009-09-11 04:59:25 +00001033 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001034 Result = DS.SetTypeSpecType(TagType, StartLoc,
1035 NameLoc.isValid() ? NameLoc : StartLoc,
1036 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCall7f41d982009-09-11 04:59:25 +00001037 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001038 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001039 return;
1040 }
Mike Stump11289f42009-09-09 15:08:12 +00001041
John McCallba7bf592010-08-24 05:47:05 +00001042 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001043 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001044
Chris Lattnercf251412010-02-02 01:23:29 +00001045 // At this point, we've successfully parsed a class-specifier in 'definition'
1046 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1047 // going to look at what comes after it to improve error recovery. If an
1048 // impossible token occurs next, we assume that the programmer forgot a ; at
1049 // the end of the declaration and recover that way.
1050 //
1051 // This switch enumerates the valid "follow" set for definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001052 if (TUK == Sema::TUK_Definition) {
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001053 bool ExpectedSemi = true;
Chris Lattnercf251412010-02-02 01:23:29 +00001054 switch (Tok.getKind()) {
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001055 default: break;
Chris Lattnercf251412010-02-02 01:23:29 +00001056 case tok::semi: // struct foo {...} ;
Chris Lattnerafe6a842010-02-02 17:32:27 +00001057 case tok::star: // struct foo {...} * P;
1058 case tok::amp: // struct foo {...} & R = ...
1059 case tok::identifier: // struct foo {...} V ;
1060 case tok::r_paren: //(struct foo {...} ) {4}
1061 case tok::annot_cxxscope: // struct foo {...} a:: b;
1062 case tok::annot_typename: // struct foo {...} a ::b;
1063 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattner5e854b92010-02-03 20:41:24 +00001064 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner35af0ab2010-02-03 01:45:03 +00001065 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001066 ExpectedSemi = false;
1067 break;
1068 // Type qualifiers
1069 case tok::kw_const: // struct foo {...} const x;
1070 case tok::kw_volatile: // struct foo {...} volatile x;
1071 case tok::kw_restrict: // struct foo {...} restrict x;
1072 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattnerafe6a842010-02-02 17:32:27 +00001073 // Storage-class specifiers
1074 case tok::kw_static: // struct foo {...} static x;
1075 case tok::kw_extern: // struct foo {...} extern x;
1076 case tok::kw_typedef: // struct foo {...} typedef x;
1077 case tok::kw_register: // struct foo {...} register x;
1078 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregorc9a99c52010-05-17 18:19:56 +00001079 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001080 // As shown above, type qualifiers and storage class specifiers absolutely
1081 // can occur after class specifiers according to the grammar. However,
Chris Lattner57540c52011-04-15 05:22:18 +00001082 // almost no one actually writes code like this. If we see one of these,
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001083 // it is much more likely that someone missed a semi colon and the
1084 // type/storage class specifier we're seeing is part of the *next*
1085 // intended declaration, as in:
1086 //
1087 // struct foo { ... }
1088 // typedef int X;
1089 //
1090 // We'd really like to emit a missing semicolon error instead of emitting
1091 // an error on the 'int' saying that you can't have two type specifiers in
1092 // the same declaration of X. Because of this, we look ahead past this
1093 // token to see if it's a type specifier. If so, we know the code is
1094 // otherwise invalid, so we can produce the expected semi error.
1095 if (!isKnownToBeTypeSpecifier(NextToken()))
1096 ExpectedSemi = false;
Chris Lattnercf251412010-02-02 01:23:29 +00001097 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001098
1099 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattnercf251412010-02-02 01:23:29 +00001100 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001101 if (!getLang().CPlusPlus)
1102 ExpectedSemi = false;
1103 break;
1104 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001105
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001106 if (ExpectedSemi) {
Chris Lattnercf251412010-02-02 01:23:29 +00001107 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1108 TagType == DeclSpec::TST_class ? "class"
1109 : TagType == DeclSpec::TST_struct? "struct" : "union");
1110 // Push this token back into the preprocessor and change our current token
1111 // to ';' so that the rest of the code recovers as though there were an
1112 // ';' after the definition.
1113 PP.EnterToken(Tok);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001114 Tok.setKind(tok::semi);
Chris Lattnercf251412010-02-02 01:23:29 +00001115 }
1116 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001117}
1118
Mike Stump11289f42009-09-09 15:08:12 +00001119/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001120///
1121/// base-clause : [C++ class.derived]
1122/// ':' base-specifier-list
1123/// base-specifier-list:
1124/// base-specifier '...'[opt]
1125/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001126void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001127 assert(Tok.is(tok::colon) && "Not a base clause");
1128 ConsumeToken();
1129
Douglas Gregor29a92472008-10-22 17:49:05 +00001130 // Build up an array of parsed base specifiers.
John McCall37ad5512010-08-23 06:44:23 +00001131 llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001132
Douglas Gregor556877c2008-04-13 21:30:24 +00001133 while (true) {
1134 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001135 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001136 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001137 // Skip the rest of this base specifier, up until the comma or
1138 // opening brace.
Douglas Gregor29a92472008-10-22 17:49:05 +00001139 SkipUntil(tok::comma, tok::l_brace, true, true);
1140 } else {
1141 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001142 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001143 }
1144
1145 // If the next token is a comma, consume it and keep reading
1146 // base-specifiers.
1147 if (Tok.isNot(tok::comma)) break;
Mike Stump11289f42009-09-09 15:08:12 +00001148
Douglas Gregor556877c2008-04-13 21:30:24 +00001149 // Consume the comma.
1150 ConsumeToken();
1151 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001152
1153 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001154 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001155}
1156
1157/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1158/// one entry in the base class list of a class specifier, for example:
1159/// class foo : public bar, virtual private baz {
1160/// 'public bar' and 'virtual private baz' are each base-specifiers.
1161///
1162/// base-specifier: [C++ class.derived]
1163/// ::[opt] nested-name-specifier[opt] class-name
1164/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1165/// class-name
1166/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1167/// class-name
John McCall48871652010-08-21 09:40:31 +00001168Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001169 bool IsVirtual = false;
1170 SourceLocation StartLoc = Tok.getLocation();
1171
1172 // Parse the 'virtual' keyword.
1173 if (Tok.is(tok::kw_virtual)) {
1174 ConsumeToken();
1175 IsVirtual = true;
1176 }
1177
1178 // Parse an (optional) access specifier.
1179 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001180 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001181 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001182
Douglas Gregor556877c2008-04-13 21:30:24 +00001183 // Parse the 'virtual' keyword (again!), in case it came after the
1184 // access specifier.
1185 if (Tok.is(tok::kw_virtual)) {
1186 SourceLocation VirtualLoc = ConsumeToken();
1187 if (IsVirtual) {
1188 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001189 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001190 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001191 }
1192
1193 IsVirtual = true;
1194 }
1195
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001196 // Parse optional '::' and optional nested-name-specifier.
1197 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001198 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregor556877c2008-04-13 21:30:24 +00001199
Douglas Gregor556877c2008-04-13 21:30:24 +00001200 // The location of the base class itself.
1201 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor831c93f2008-11-05 20:51:48 +00001202
1203 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001204 SourceLocation EndLocation;
Douglas Gregore7c20652011-03-02 00:47:37 +00001205 TypeResult BaseType = ParseClassName(EndLocation, SS);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001206 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001207 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001208
Douglas Gregor752a5952011-01-03 22:36:02 +00001209 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1210 // actually part of the base-specifier-list grammar productions, but we
1211 // parse it here for convenience.
1212 SourceLocation EllipsisLoc;
1213 if (Tok.is(tok::ellipsis))
1214 EllipsisLoc = ConsumeToken();
1215
Mike Stump11289f42009-09-09 15:08:12 +00001216 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001217 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001218
Douglas Gregor556877c2008-04-13 21:30:24 +00001219 // Notify semantic analysis that we have parsed a complete
1220 // base-specifier.
Sebastian Redl511ed552008-11-25 22:21:31 +00001221 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor752a5952011-01-03 22:36:02 +00001222 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001223}
1224
1225/// getAccessSpecifierIfPresent - Determine whether the next token is
1226/// a C++ access-specifier.
1227///
1228/// access-specifier: [C++ class.derived]
1229/// 'private'
1230/// 'protected'
1231/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001232AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001233 switch (Tok.getKind()) {
1234 default: return AS_none;
1235 case tok::kw_private: return AS_private;
1236 case tok::kw_protected: return AS_protected;
1237 case tok::kw_public: return AS_public;
1238 }
1239}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001240
Eli Friedman3af2a772009-07-22 21:45:50 +00001241void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
John McCall48871652010-08-21 09:40:31 +00001242 Decl *ThisDecl) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001243 // We just declared a member function. If this member function
1244 // has any default arguments, we'll need to parse them later.
1245 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001246 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001247 = DeclaratorInfo.getFunctionTypeInfo();
Eli Friedman3af2a772009-07-22 21:45:50 +00001248 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1249 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1250 if (!LateMethod) {
1251 // Push this method onto the stack of late-parsed method
1252 // declarations.
Douglas Gregorefc46952010-10-12 16:25:54 +00001253 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1254 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001255 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001256
1257 // Add all of the parameters prior to this one (they don't
1258 // have default arguments).
1259 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1260 for (unsigned I = 0; I < ParamIdx; ++I)
1261 LateMethod->DefaultArgs.push_back(
Douglas Gregor1d85d292010-03-02 01:29:43 +00001262 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001263 }
1264
1265 // Add this parameter to the list of parameters (it or may
1266 // not have a default argument).
1267 LateMethod->DefaultArgs.push_back(
1268 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1269 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1270 }
1271 }
1272}
1273
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001274/// isCXX0XVirtSpecifier - Determine whether the next token is a C++0x
1275/// virt-specifier.
1276///
1277/// virt-specifier:
1278/// override
1279/// final
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001280VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier() const {
Anders Carlsson5a72fdb2011-01-22 23:01:49 +00001281 if (!getLang().CPlusPlus)
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001282 return VirtSpecifiers::VS_None;
1283
Anders Carlsson56104902011-01-17 03:05:47 +00001284 if (Tok.is(tok::identifier)) {
1285 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001286
Anders Carlsson428803b2011-01-20 03:47:08 +00001287 // Initialize the contextual keywords.
1288 if (!Ident_final) {
1289 Ident_final = &PP.getIdentifierTable().get("final");
1290 Ident_override = &PP.getIdentifierTable().get("override");
1291 }
1292
Anders Carlsson56104902011-01-17 03:05:47 +00001293 if (II == Ident_override)
1294 return VirtSpecifiers::VS_Override;
1295
1296 if (II == Ident_final)
1297 return VirtSpecifiers::VS_Final;
1298 }
1299
1300 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001301}
1302
1303/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1304///
1305/// virt-specifier-seq:
1306/// virt-specifier
1307/// virt-specifier-seq virt-specifier
Anders Carlsson56104902011-01-17 03:05:47 +00001308void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) {
Anders Carlsson56104902011-01-17 03:05:47 +00001309 while (true) {
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001310 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001311 if (Specifier == VirtSpecifiers::VS_None)
1312 return;
1313
1314 // C++ [class.mem]p8:
1315 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001316 const char *PrevSpec = 0;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001317 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001318 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1319 << PrevSpec
1320 << FixItHint::CreateRemoval(Tok.getLocation());
1321
Anders Carlsson5a72fdb2011-01-22 23:01:49 +00001322 if (!getLang().CPlusPlus0x)
1323 Diag(Tok.getLocation(), diag::ext_override_control_keyword)
1324 << VirtSpecifiers::getSpecifierName(Specifier);
Anders Carlsson56104902011-01-17 03:05:47 +00001325 ConsumeToken();
1326 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001327}
1328
Anders Carlssoncafbab72011-03-25 14:53:29 +00001329/// isCXX0XFinalKeyword - Determine whether the next token is a C++0x
1330/// contextual 'final' keyword.
1331bool Parser::isCXX0XFinalKeyword() const {
Anders Carlsson5a72fdb2011-01-22 23:01:49 +00001332 if (!getLang().CPlusPlus)
Anders Carlssoncafbab72011-03-25 14:53:29 +00001333 return false;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001334
Anders Carlssoncafbab72011-03-25 14:53:29 +00001335 if (!Tok.is(tok::identifier))
1336 return false;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001337
Anders Carlssoncafbab72011-03-25 14:53:29 +00001338 // Initialize the contextual keywords.
1339 if (!Ident_final) {
1340 Ident_final = &PP.getIdentifierTable().get("final");
1341 Ident_override = &PP.getIdentifierTable().get("override");
1342 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001343
Anders Carlssoncafbab72011-03-25 14:53:29 +00001344 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001345}
1346
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001347/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1348///
1349/// member-declaration:
1350/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1351/// function-definition ';'[opt]
1352/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1353/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001354/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001355/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001356/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001357///
1358/// member-declarator-list:
1359/// member-declarator
1360/// member-declarator-list ',' member-declarator
1361///
1362/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001363/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001364/// declarator constant-initializer[opt]
1365/// identifier[opt] ':' constant-expression
1366///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001367/// virt-specifier-seq:
1368/// virt-specifier
1369/// virt-specifier-seq virt-specifier
1370///
1371/// virt-specifier:
1372/// override
1373/// final
1374/// new
1375///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001376/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001377/// '= 0'
1378///
1379/// constant-initializer:
1380/// '=' constant-expression
1381///
Douglas Gregor3447e762009-08-20 22:52:58 +00001382void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCall796c2a52010-07-16 08:13:16 +00001383 const ParsedTemplateInfo &TemplateInfo,
1384 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00001385 if (Tok.is(tok::at)) {
1386 if (getLang().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
1387 Diag(Tok, diag::err_at_defs_cxx);
1388 else
1389 Diag(Tok, diag::err_at_in_class);
1390
1391 ConsumeToken();
1392 SkipUntil(tok::r_brace);
1393 return;
1394 }
1395
John McCalla0097262009-12-11 02:10:03 +00001396 // Access declarations.
1397 if (!TemplateInfo.Kind &&
1398 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall1f476a12010-02-26 08:45:28 +00001399 !TryAnnotateCXXScopeToken() &&
John McCalla0097262009-12-11 02:10:03 +00001400 Tok.is(tok::annot_cxxscope)) {
1401 bool isAccessDecl = false;
1402 if (NextToken().is(tok::identifier))
1403 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1404 else
1405 isAccessDecl = NextToken().is(tok::kw_operator);
1406
1407 if (isAccessDecl) {
1408 // Collect the scope specifier token we annotated earlier.
1409 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001410 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
John McCalla0097262009-12-11 02:10:03 +00001411
1412 // Try to parse an unqualified-id.
1413 UnqualifiedId Name;
John McCallba7bf592010-08-24 05:47:05 +00001414 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) {
John McCalla0097262009-12-11 02:10:03 +00001415 SkipUntil(tok::semi);
1416 return;
1417 }
1418
1419 // TODO: recover from mistakenly-qualified operator declarations.
1420 if (ExpectAndConsume(tok::semi,
1421 diag::err_expected_semi_after,
1422 "access declaration",
1423 tok::semi))
1424 return;
1425
Douglas Gregor0be31a22010-07-02 17:43:08 +00001426 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCalla0097262009-12-11 02:10:03 +00001427 false, SourceLocation(),
1428 SS, Name,
1429 /* AttrList */ 0,
1430 /* IsTypeName */ false,
1431 SourceLocation());
1432 return;
1433 }
1434 }
1435
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001436 // static_assert-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001437 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001438 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00001439 SourceLocation DeclEnd;
1440 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001441 return;
1442 }
Mike Stump11289f42009-09-09 15:08:12 +00001443
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001444 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00001445 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00001446 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00001447 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00001448 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001449 AS);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001450 return;
1451 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001452
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001453 // Handle: member-declaration ::= '__extension__' member-declaration
1454 if (Tok.is(tok::kw___extension__)) {
1455 // __extension__ silences extension warnings in the subexpression.
1456 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1457 ConsumeToken();
John McCall796c2a52010-07-16 08:13:16 +00001458 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001459 }
Douglas Gregorfec52632009-06-20 00:51:54 +00001460
Chris Lattnercf251412010-02-02 01:23:29 +00001461 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1462 // is a bitfield.
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001463 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001464
John McCall084e83d2011-03-24 11:26:52 +00001465 ParsedAttributesWithRange attrs(AttrFactory);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001466 // Optional C++0x attribute-specifier
John McCall53fa7142010-12-24 02:08:15 +00001467 MaybeParseCXX0XAttributes(attrs);
1468 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001469
Douglas Gregorfec52632009-06-20 00:51:54 +00001470 if (Tok.is(tok::kw_using)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001471 // FIXME: Check for template aliases
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001472
John McCall53fa7142010-12-24 02:08:15 +00001473 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001474
Douglas Gregorfec52632009-06-20 00:51:54 +00001475 // Eat 'using'.
1476 SourceLocation UsingLoc = ConsumeToken();
1477
1478 if (Tok.is(tok::kw_namespace)) {
1479 Diag(UsingLoc, diag::err_using_namespace_in_class);
1480 SkipUntil(tok::semi, true, true);
Chris Lattner916dbf12010-02-02 00:43:15 +00001481 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00001482 SourceLocation DeclEnd;
1483 // Otherwise, it must be using-declaration.
John McCall9b72f892010-11-10 02:40:36 +00001484 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1485 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00001486 }
1487 return;
1488 }
1489
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001490 // decl-specifier-seq:
1491 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00001492 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00001493 DS.takeAttributesFrom(attrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00001494 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001495
John McCallfaf5fb42010-08-26 23:41:50 +00001496 MultiTemplateParamsArg TemplateParams(Actions,
John McCall11083da2009-09-16 22:47:08 +00001497 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1498 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1499
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001500 if (Tok.is(tok::semi)) {
1501 ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001502 Decl *TheDecl =
John McCall796c2a52010-07-16 08:13:16 +00001503 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
1504 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00001505 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001506 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001507
John McCall28a6aea2009-11-04 02:18:39 +00001508 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00001509 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001510
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001511 if (Tok.isNot(tok::colon)) {
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001512 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1513 ColonProtectionRAIIObject X(*this);
1514
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001515 // Parse the first declarator.
1516 ParseDeclarator(DeclaratorInfo);
1517 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +00001518 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001519 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001520 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001521 if (Tok.is(tok::semi))
1522 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001523 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001524 }
1525
Nico Weber24b2a822011-01-28 06:07:34 +00001526 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1527
John Thompson5bc5cbe2009-11-25 22:58:06 +00001528 // If attributes exist after the declarator, but before an '{', parse them.
John McCall53fa7142010-12-24 02:08:15 +00001529 MaybeParseGNUAttributes(DeclaratorInfo);
John Thompson5bc5cbe2009-11-25 22:58:06 +00001530
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001531 // function-definition:
Douglas Gregore8381c02008-11-05 04:29:56 +00001532 if (Tok.is(tok::l_brace)
Sebastian Redla7b98a72009-04-26 20:35:05 +00001533 || (DeclaratorInfo.isFunctionDeclarator() &&
1534 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001535 if (!DeclaratorInfo.isFunctionDeclarator()) {
1536 Diag(Tok, diag::err_func_def_no_params);
1537 ConsumeBrace();
1538 SkipUntil(tok::r_brace, true);
Douglas Gregor8a4db832011-01-19 16:41:58 +00001539
1540 // Consume the optional ';'
1541 if (Tok.is(tok::semi))
1542 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001543 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001544 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001545
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001546 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1547 Diag(Tok, diag::err_function_declared_typedef);
1548 // This recovery skips the entire function body. It would be nice
1549 // to simply call ParseCXXInlineMethodDef() below, however Sema
1550 // assumes the declarator represents a function, not a typedef.
1551 ConsumeBrace();
1552 SkipUntil(tok::r_brace, true);
Douglas Gregor8a4db832011-01-19 16:41:58 +00001553
1554 // Consume the optional ';'
1555 if (Tok.is(tok::semi))
1556 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001557 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001558 }
1559
Nico Weber24b2a822011-01-28 06:07:34 +00001560 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo, VS);
Douglas Gregor8a4db832011-01-19 16:41:58 +00001561 // Consume the optional ';'
1562 if (Tok.is(tok::semi))
1563 ConsumeToken();
1564
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001565 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001566 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001567 }
1568
1569 // member-declarator-list:
1570 // member-declarator
1571 // member-declarator-list ',' member-declarator
1572
John McCall48871652010-08-21 09:40:31 +00001573 llvm::SmallVector<Decl *, 8> DeclsInGroup;
John McCalldadc5752010-08-24 06:29:42 +00001574 ExprResult BitfieldSize;
1575 ExprResult Init;
Sebastian Redl42e92c42009-04-12 17:16:29 +00001576 bool Deleted = false;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001577
1578 while (1) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001579 // member-declarator:
1580 // declarator pure-specifier[opt]
1581 // declarator constant-initializer[opt]
1582 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001583 if (Tok.is(tok::colon)) {
1584 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001585 BitfieldSize = ParseConstantExpression();
1586 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001587 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001588 }
Mike Stump11289f42009-09-09 15:08:12 +00001589
Anders Carlsson56104902011-01-17 03:05:47 +00001590 ParseOptionalCXX0XVirtSpecifierSeq(VS);
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001591
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001592 // pure-specifier:
1593 // '= 0'
1594 //
1595 // constant-initializer:
1596 // '=' constant-expression
Sebastian Redl42e92c42009-04-12 17:16:29 +00001597 //
1598 // defaulted/deleted function-definition:
1599 // '=' 'default' [TODO]
1600 // '=' 'delete'
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001601 if (Tok.is(tok::equal)) {
1602 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001603 if (Tok.is(tok::kw_delete)) {
1604 if (!getLang().CPlusPlus0x)
1605 Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
Sebastian Redl42e92c42009-04-12 17:16:29 +00001606 ConsumeToken();
1607 Deleted = true;
1608 } else {
1609 Init = ParseInitializer();
1610 if (Init.isInvalid())
1611 SkipUntil(tok::comma, true, true);
1612 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001613 }
1614
Chris Lattnerf3d3b362010-06-13 05:34:18 +00001615 // If a simple-asm-expr is present, parse it.
1616 if (Tok.is(tok::kw_asm)) {
1617 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001618 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnerf3d3b362010-06-13 05:34:18 +00001619 if (AsmLabel.isInvalid())
1620 SkipUntil(tok::comma, true, true);
1621
1622 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1623 DeclaratorInfo.SetRangeEnd(Loc);
1624 }
1625
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001626 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001627 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001628
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001629 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001630 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001631 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00001632
John McCall48871652010-08-21 09:40:31 +00001633 Decl *ThisDecl = 0;
John McCall07e91c02009-08-06 02:15:43 +00001634 if (DS.isFriendSpecified()) {
John McCall2f212b32009-09-11 21:02:39 +00001635 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor0be31a22010-07-02 17:43:08 +00001636 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCall2f212b32009-09-11 21:02:39 +00001637 /*IsDefinition*/ false,
1638 move(TemplateParams));
Douglas Gregor3447e762009-08-20 22:52:58 +00001639 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001640 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00001641 DeclaratorInfo,
Douglas Gregor3447e762009-08-20 22:52:58 +00001642 move(TemplateParams),
John McCall07e91c02009-08-06 02:15:43 +00001643 BitfieldSize.release(),
Anders Carlssondb36b802011-01-20 03:57:25 +00001644 VS, Init.release(),
Sebastian Redld6f78502009-11-24 23:38:44 +00001645 /*IsDefinition*/Deleted,
John McCall07e91c02009-08-06 02:15:43 +00001646 Deleted);
Douglas Gregor3447e762009-08-20 22:52:58 +00001647 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001648 if (ThisDecl)
1649 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001650
Douglas Gregor4d87df52008-12-16 21:30:33 +00001651 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump11289f42009-09-09 15:08:12 +00001652 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor4d87df52008-12-16 21:30:33 +00001653 != DeclSpec::SCS_typedef) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001654 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor4d87df52008-12-16 21:30:33 +00001655 }
1656
John McCall28a6aea2009-11-04 02:18:39 +00001657 DeclaratorInfo.complete(ThisDecl);
1658
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001659 // If we don't have a comma, it is either the end of the list (a ';')
1660 // or an error, bail out.
1661 if (Tok.isNot(tok::comma))
1662 break;
Mike Stump11289f42009-09-09 15:08:12 +00001663
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001664 // Consume the comma.
1665 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001666
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001667 // Parse the next declarator.
1668 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00001669 VS.clear();
Sebastian Redlc13f2682008-12-09 20:22:58 +00001670 BitfieldSize = 0;
1671 Init = 0;
Sebastian Redl42e92c42009-04-12 17:16:29 +00001672 Deleted = false;
Mike Stump11289f42009-09-09 15:08:12 +00001673
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001674 // Attributes are only allowed on the second declarator.
John McCall53fa7142010-12-24 02:08:15 +00001675 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001676
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001677 if (Tok.isNot(tok::colon))
1678 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001679 }
1680
Chris Lattner916dbf12010-02-02 00:43:15 +00001681 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1682 // Skip to end of block or statement.
1683 SkipUntil(tok::r_brace, true, true);
1684 // If we stopped at a ';', eat it.
1685 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001686 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001687 }
1688
Douglas Gregor0be31a22010-07-02 17:43:08 +00001689 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattner916dbf12010-02-02 00:43:15 +00001690 DeclsInGroup.size());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001691}
1692
1693/// ParseCXXMemberSpecification - Parse the class definition.
1694///
1695/// member-specification:
1696/// member-declaration member-specification[opt]
1697/// access-specifier ':' member-specification[opt]
1698///
1699void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00001700 unsigned TagType, Decl *TagDecl) {
Sanjiv Guptad7959242008-10-31 09:52:39 +00001701 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001702 TagType == DeclSpec::TST_union ||
Sanjiv Guptad7959242008-10-31 09:52:39 +00001703 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001704
John McCallfaf5fb42010-08-26 23:41:50 +00001705 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1706 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00001707
Douglas Gregoredf8f392010-01-16 20:52:59 +00001708 // Determine whether this is a non-nested class. Note that local
1709 // classes are *not* considered to be nested classes.
1710 bool NonNestedClass = true;
1711 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001712 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00001713 if (S->isClassScope()) {
1714 // We're inside a class scope, so this is a nested class.
1715 NonNestedClass = false;
1716 break;
1717 }
1718
1719 if ((S->getFlags() & Scope::FnScope)) {
1720 // If we're in a function or function template declared in the
1721 // body of a class, then this is a local class rather than a
1722 // nested class.
1723 const Scope *Parent = S->getParent();
1724 if (Parent->isTemplateParamScope())
1725 Parent = Parent->getParent();
1726 if (Parent->isClassScope())
1727 break;
1728 }
1729 }
1730 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001731
1732 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00001733 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001734
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001735 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregoredf8f392010-01-16 20:52:59 +00001736 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001737
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001738 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001739 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00001740
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00001741 SourceLocation FinalLoc;
1742
1743 // Parse the optional 'final' keyword.
1744 if (getLang().CPlusPlus && Tok.is(tok::identifier)) {
1745 IdentifierInfo *II = Tok.getIdentifierInfo();
1746
1747 // Initialize the contextual keywords.
1748 if (!Ident_final) {
1749 Ident_final = &PP.getIdentifierTable().get("final");
1750 Ident_override = &PP.getIdentifierTable().get("override");
1751 }
1752
1753 if (II == Ident_final)
1754 FinalLoc = ConsumeToken();
1755
1756 if (!getLang().CPlusPlus0x)
1757 Diag(FinalLoc, diag::ext_override_control_keyword) << "final";
1758 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001759
John McCall2d814c32009-12-19 21:48:58 +00001760 if (Tok.is(tok::colon)) {
1761 ParseBaseClause(TagDecl);
1762
1763 if (!Tok.is(tok::l_brace)) {
1764 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCall2ff380a2010-03-17 00:38:33 +00001765
1766 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001767 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00001768 return;
1769 }
1770 }
1771
1772 assert(Tok.is(tok::l_brace));
1773
1774 SourceLocation LBraceLoc = ConsumeBrace();
1775
John McCall08bede42010-05-28 08:11:17 +00001776 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00001777 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Anders Carlssonfc1eef42011-01-22 17:51:53 +00001778 LBraceLoc);
John McCall1c7e6ec2009-12-20 07:58:13 +00001779
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001780 // C++ 11p3: Members of a class defined with the keyword class are private
1781 // by default. Members of a class defined with the keywords struct or union
1782 // are public by default.
1783 AccessSpecifier CurAS;
1784 if (TagType == DeclSpec::TST_class)
1785 CurAS = AS_private;
1786 else
1787 CurAS = AS_public;
1788
Douglas Gregor9377c822010-06-21 22:31:09 +00001789 SourceLocation RBraceLoc;
1790 if (TagDecl) {
1791 // While we still have something to read, read the member-declarations.
1792 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1793 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001794
Douglas Gregor9377c822010-06-21 22:31:09 +00001795 // Check for extraneous top-level semicolon.
1796 if (Tok.is(tok::semi)) {
1797 Diag(Tok, diag::ext_extra_struct_semi)
1798 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1799 << FixItHint::CreateRemoval(Tok.getLocation());
1800 ConsumeToken();
1801 continue;
1802 }
1803
1804 AccessSpecifier AS = getAccessSpecifierIfPresent();
1805 if (AS != AS_none) {
1806 // Current token is a C++ access specifier.
1807 CurAS = AS;
1808 SourceLocation ASLoc = Tok.getLocation();
1809 ConsumeToken();
1810 if (Tok.is(tok::colon))
1811 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1812 else
1813 Diag(Tok, diag::err_expected_colon);
1814 ConsumeToken();
1815 continue;
1816 }
1817
1818 // FIXME: Make sure we don't have a template here.
1819
1820 // Parse all the comma separated declarators.
1821 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001822 }
1823
Douglas Gregor9377c822010-06-21 22:31:09 +00001824 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1825 } else {
1826 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001827 }
Mike Stump11289f42009-09-09 15:08:12 +00001828
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001829 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001830 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001831 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001832
John McCall08bede42010-05-28 08:11:17 +00001833 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001834 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall08bede42010-05-28 08:11:17 +00001835 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00001836 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001837
1838 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1839 // complete within function bodies, default arguments,
1840 // exception-specifications, and constructor ctor-initializers (including
1841 // such things in nested classes).
1842 //
Douglas Gregor4d87df52008-12-16 21:30:33 +00001843 // FIXME: Only function bodies and constructor ctor-initializers are
1844 // parsed correctly, fix the rest.
Douglas Gregor9377c822010-06-21 22:31:09 +00001845 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001846 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00001847 // are complete and we can parse the delayed portions of method
1848 // declarations and the lexed inline method definitions.
Douglas Gregor428119e2010-06-16 23:45:56 +00001849 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001850 ParseLexedMethodDeclarations(getCurrentClass());
1851 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00001852 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001853 }
1854
John McCall08bede42010-05-28 08:11:17 +00001855 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001856 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCall2ff380a2010-03-17 00:38:33 +00001857
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001858 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001859 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001860 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001861}
Douglas Gregore8381c02008-11-05 04:29:56 +00001862
1863/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1864/// which explicitly initializes the members or base classes of a
1865/// class (C++ [class.base.init]). For example, the three initializers
1866/// after the ':' in the Derived constructor below:
1867///
1868/// @code
1869/// class Base { };
1870/// class Derived : Base {
1871/// int x;
1872/// float f;
1873/// public:
1874/// Derived(float f) : Base(), x(17), f(f) { }
1875/// };
1876/// @endcode
1877///
Mike Stump11289f42009-09-09 15:08:12 +00001878/// [C++] ctor-initializer:
1879/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00001880///
Mike Stump11289f42009-09-09 15:08:12 +00001881/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00001882/// mem-initializer ...[opt]
1883/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00001884void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00001885 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1886
1887 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001888
Alexis Hunt1d792652011-01-08 20:30:50 +00001889 llvm::SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00001890 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001891
Douglas Gregore8381c02008-11-05 04:29:56 +00001892 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00001893 if (Tok.is(tok::code_completion)) {
1894 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
1895 MemInitializers.data(),
1896 MemInitializers.size());
1897 ConsumeCodeCompletionToken();
1898 } else {
1899 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
1900 if (!MemInit.isInvalid())
1901 MemInitializers.push_back(MemInit.get());
1902 else
1903 AnyErrors = true;
1904 }
1905
Douglas Gregore8381c02008-11-05 04:29:56 +00001906 if (Tok.is(tok::comma))
1907 ConsumeToken();
1908 else if (Tok.is(tok::l_brace))
1909 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00001910 // If the next token looks like a base or member initializer, assume that
1911 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00001912 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
1913 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
1914 Diag(Loc, diag::err_ctor_init_missing_comma)
1915 << FixItHint::CreateInsertion(Loc, ", ");
1916 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00001917 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redla7b98a72009-04-26 20:35:05 +00001918 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregore8381c02008-11-05 04:29:56 +00001919 SkipUntil(tok::l_brace, true, true);
1920 break;
1921 }
1922 } while (true);
1923
Mike Stump11289f42009-09-09 15:08:12 +00001924 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00001925 MemInitializers.data(), MemInitializers.size(),
1926 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00001927}
1928
1929/// ParseMemInitializer - Parse a C++ member initializer, which is
1930/// part of a constructor initializer that explicitly initializes one
1931/// member or base class (C++ [class.base.init]). See
1932/// ParseConstructorInitializer for an example.
1933///
1934/// [C++] mem-initializer:
1935/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump11289f42009-09-09 15:08:12 +00001936///
Douglas Gregore8381c02008-11-05 04:29:56 +00001937/// [C++] mem-initializer-id:
1938/// '::'[opt] nested-name-specifier[opt] class-name
1939/// identifier
John McCall48871652010-08-21 09:40:31 +00001940Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00001941 // parse '::'[opt] nested-name-specifier[opt]
1942 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001943 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
1944 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001945 if (Tok.is(tok::annot_template_id)) {
1946 TemplateIdAnnotation *TemplateId
1947 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregor46c59612010-01-12 17:52:59 +00001948 if (TemplateId->Kind == TNK_Type_template ||
1949 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00001950 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001951 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00001952 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001953 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001954 }
1955 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001956 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00001957 return true;
1958 }
Mike Stump11289f42009-09-09 15:08:12 +00001959
Douglas Gregore8381c02008-11-05 04:29:56 +00001960 // Get the identifier. This may be a member name or a class name,
1961 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001962 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregore8381c02008-11-05 04:29:56 +00001963 SourceLocation IdLoc = ConsumeToken();
1964
1965 // Parse the '('.
1966 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001967 Diag(Tok, diag::err_expected_lparen);
Douglas Gregore8381c02008-11-05 04:29:56 +00001968 return true;
1969 }
1970 SourceLocation LParenLoc = ConsumeParen();
1971
1972 // Parse the optional expression-list.
Sebastian Redl511ed552008-11-25 22:21:31 +00001973 ExprVector ArgExprs(Actions);
Douglas Gregore8381c02008-11-05 04:29:56 +00001974 CommaLocsTy CommaLocs;
1975 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1976 SkipUntil(tok::r_paren);
1977 return true;
1978 }
1979
1980 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1981
Douglas Gregor44e7df62011-01-04 00:32:56 +00001982 SourceLocation EllipsisLoc;
1983 if (Tok.is(tok::ellipsis))
1984 EllipsisLoc = ConsumeToken();
1985
Douglas Gregor0be31a22010-07-02 17:43:08 +00001986 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001987 TemplateTypeTy, IdLoc,
Sebastian Redl511ed552008-11-25 22:21:31 +00001988 LParenLoc, ArgExprs.take(),
Douglas Gregor44e7df62011-01-04 00:32:56 +00001989 ArgExprs.size(), RParenLoc,
1990 EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00001991}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001992
Sebastian Redl965b0e32011-03-05 14:45:16 +00001993/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001994///
Douglas Gregor356513d2008-12-01 18:00:20 +00001995/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00001996/// dynamic-exception-specification
1997/// noexcept-specification
1998///
1999/// noexcept-specification:
2000/// 'noexcept'
2001/// 'noexcept' '(' constant-expression ')'
2002ExceptionSpecificationType
2003Parser::MaybeParseExceptionSpecification(SourceRange &SpecificationRange,
2004 llvm::SmallVectorImpl<ParsedType> &DynamicExceptions,
2005 llvm::SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
2006 ExprResult &NoexceptExpr) {
2007 ExceptionSpecificationType Result = EST_None;
2008
2009 // See if there's a dynamic specification.
2010 if (Tok.is(tok::kw_throw)) {
2011 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2012 DynamicExceptions,
2013 DynamicExceptionRanges);
2014 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2015 "Produced different number of exception types and ranges.");
2016 }
2017
2018 // If there's no noexcept specification, we're done.
2019 if (Tok.isNot(tok::kw_noexcept))
2020 return Result;
2021
2022 // If we already had a dynamic specification, parse the noexcept for,
2023 // recovery, but emit a diagnostic and don't store the results.
2024 SourceRange NoexceptRange;
2025 ExceptionSpecificationType NoexceptType = EST_None;
2026
2027 SourceLocation KeywordLoc = ConsumeToken();
2028 if (Tok.is(tok::l_paren)) {
2029 // There is an argument.
2030 SourceLocation LParenLoc = ConsumeParen();
2031 NoexceptType = EST_ComputedNoexcept;
2032 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002033 // The argument must be contextually convertible to bool. We use
2034 // ActOnBooleanCondition for this purpose.
2035 if (!NoexceptExpr.isInvalid())
2036 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2037 NoexceptExpr.get());
Sebastian Redl965b0e32011-03-05 14:45:16 +00002038 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2039 NoexceptRange = SourceRange(KeywordLoc, RParenLoc);
2040 } else {
2041 // There is no argument.
2042 NoexceptType = EST_BasicNoexcept;
2043 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2044 }
2045
2046 if (Result == EST_None) {
2047 SpecificationRange = NoexceptRange;
2048 Result = NoexceptType;
2049
2050 // If there's a dynamic specification after a noexcept specification,
2051 // parse that and ignore the results.
2052 if (Tok.is(tok::kw_throw)) {
2053 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2054 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2055 DynamicExceptionRanges);
2056 }
2057 } else {
2058 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2059 }
2060
2061 return Result;
2062}
2063
2064/// ParseDynamicExceptionSpecification - Parse a C++
2065/// dynamic-exception-specification (C++ [except.spec]).
2066///
2067/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00002068/// 'throw' '(' type-id-list [opt] ')'
2069/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00002070///
Douglas Gregor356513d2008-12-01 18:00:20 +00002071/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00002072/// type-id ... [opt]
2073/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002074///
Sebastian Redl965b0e32011-03-05 14:45:16 +00002075ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2076 SourceRange &SpecificationRange,
2077 llvm::SmallVectorImpl<ParsedType> &Exceptions,
2078 llvm::SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002079 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00002080
Sebastian Redl965b0e32011-03-05 14:45:16 +00002081 SpecificationRange.setBegin(ConsumeToken());
Mike Stump11289f42009-09-09 15:08:12 +00002082
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002083 if (!Tok.is(tok::l_paren)) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002084 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2085 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002086 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002087 }
2088 SourceLocation LParenLoc = ConsumeParen();
2089
Douglas Gregor356513d2008-12-01 18:00:20 +00002090 // Parse throw(...), a Microsoft extension that means "this function
2091 // can throw anything".
2092 if (Tok.is(tok::ellipsis)) {
2093 SourceLocation EllipsisLoc = ConsumeToken();
2094 if (!getLang().Microsoft)
2095 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redl965b0e32011-03-05 14:45:16 +00002096 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2097 SpecificationRange.setEnd(RParenLoc);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002098 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00002099 }
2100
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002101 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00002102 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002103 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00002104 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00002105
Douglas Gregor830837d2010-12-20 23:57:46 +00002106 if (Tok.is(tok::ellipsis)) {
2107 // C++0x [temp.variadic]p5:
2108 // - In a dynamic-exception-specification (15.4); the pattern is a
2109 // type-id.
2110 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00002111 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00002112 if (!Res.isInvalid())
2113 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2114 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00002115
Sebastian Redld6434562009-05-29 18:02:33 +00002116 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002117 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00002118 Ranges.push_back(Range);
2119 }
Douglas Gregor830837d2010-12-20 23:57:46 +00002120
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002121 if (Tok.is(tok::comma))
2122 ConsumeToken();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002123 else
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002124 break;
2125 }
2126
Sebastian Redl965b0e32011-03-05 14:45:16 +00002127 SpecificationRange.setEnd(MatchRHSPunctuation(tok::r_paren, LParenLoc));
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002128 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002129}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002130
Douglas Gregor7fb25412010-10-01 18:44:50 +00002131/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2132/// function declaration.
2133TypeResult Parser::ParseTrailingReturnType() {
2134 assert(Tok.is(tok::arrow) && "expected arrow");
2135
2136 ConsumeToken();
2137
2138 // FIXME: Need to suppress declarations when parsing this typename.
2139 // Otherwise in this function definition:
2140 //
2141 // auto f() -> struct X {}
2142 //
2143 // struct X is parsed as class definition because of the trailing
2144 // brace.
2145
2146 SourceRange Range;
2147 return ParseTypeName(&Range);
2148}
2149
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002150/// \brief We have just started parsing the definition of a new class,
2151/// so push that class onto our stack of classes that is currently
2152/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00002153Sema::ParsingClassState
2154Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002155 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002156 "Nested class without outer class");
Douglas Gregoredf8f392010-01-16 20:52:59 +00002157 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
John McCallc1465822011-02-14 07:13:47 +00002158 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002159}
2160
2161/// \brief Deallocate the given parsed class and all of its nested
2162/// classes.
2163void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00002164 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2165 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002166 delete Class;
2167}
2168
2169/// \brief Pop the top class of the stack of classes that are
2170/// currently being parsed.
2171///
2172/// This routine should be called when we have finished parsing the
2173/// definition of a class, but have not yet popped the Scope
2174/// associated with the class's definition.
2175///
2176/// \returns true if the class we've popped is a top-level class,
2177/// false otherwise.
John McCallc1465822011-02-14 07:13:47 +00002178void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002179 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00002180
John McCallc1465822011-02-14 07:13:47 +00002181 Actions.PopParsingClass(state);
2182
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002183 ParsingClass *Victim = ClassStack.top();
2184 ClassStack.pop();
2185 if (Victim->TopLevelClass) {
2186 // Deallocate all of the nested classes of this class,
2187 // recursively: we don't need to keep any of this information.
2188 DeallocateParsedClasses(Victim);
2189 return;
Mike Stump11289f42009-09-09 15:08:12 +00002190 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002191 assert(!ClassStack.empty() && "Missing top-level class?");
2192
Douglas Gregorefc46952010-10-12 16:25:54 +00002193 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002194 // The victim is a nested class, but we will not need to perform
2195 // any processing after the definition of this class since it has
2196 // no members whose handling was delayed. Therefore, we can just
2197 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00002198 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002199 return;
2200 }
2201
2202 // This nested class has some members that will need to be processed
2203 // after the top-level class is completely defined. Therefore, add
2204 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002205 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00002206 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00002207 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002208}
Alexis Hunt96d5c762009-11-21 08:43:09 +00002209
2210/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
2211/// parses standard attributes.
2212///
2213/// [C++0x] attribute-specifier:
2214/// '[' '[' attribute-list ']' ']'
2215///
2216/// [C++0x] attribute-list:
2217/// attribute[opt]
2218/// attribute-list ',' attribute[opt]
2219///
2220/// [C++0x] attribute:
2221/// attribute-token attribute-argument-clause[opt]
2222///
2223/// [C++0x] attribute-token:
2224/// identifier
2225/// attribute-scoped-token
2226///
2227/// [C++0x] attribute-scoped-token:
2228/// attribute-namespace '::' identifier
2229///
2230/// [C++0x] attribute-namespace:
2231/// identifier
2232///
2233/// [C++0x] attribute-argument-clause:
2234/// '(' balanced-token-seq ')'
2235///
2236/// [C++0x] balanced-token-seq:
2237/// balanced-token
2238/// balanced-token-seq balanced-token
2239///
2240/// [C++0x] balanced-token:
2241/// '(' balanced-token-seq ')'
2242/// '[' balanced-token-seq ']'
2243/// '{' balanced-token-seq '}'
2244/// any token but '(', ')', '[', ']', '{', or '}'
John McCall53fa7142010-12-24 02:08:15 +00002245void Parser::ParseCXX0XAttributes(ParsedAttributesWithRange &attrs,
2246 SourceLocation *endLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002247 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
2248 && "Not a C++0x attribute list");
2249
2250 SourceLocation StartLoc = Tok.getLocation(), Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002251
2252 ConsumeBracket();
2253 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002254
Alexis Hunt96d5c762009-11-21 08:43:09 +00002255 if (Tok.is(tok::comma)) {
2256 Diag(Tok.getLocation(), diag::err_expected_ident);
2257 ConsumeToken();
2258 }
2259
2260 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
2261 // attribute not present
2262 if (Tok.is(tok::comma)) {
2263 ConsumeToken();
2264 continue;
2265 }
2266
2267 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
2268 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002269
Alexis Hunt96d5c762009-11-21 08:43:09 +00002270 // scoped attribute
2271 if (Tok.is(tok::coloncolon)) {
2272 ConsumeToken();
2273
2274 if (!Tok.is(tok::identifier)) {
2275 Diag(Tok.getLocation(), diag::err_expected_ident);
2276 SkipUntil(tok::r_square, tok::comma, true, true);
2277 continue;
2278 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002279
Alexis Hunt96d5c762009-11-21 08:43:09 +00002280 ScopeName = AttrName;
2281 ScopeLoc = AttrLoc;
2282
2283 AttrName = Tok.getIdentifierInfo();
2284 AttrLoc = ConsumeToken();
2285 }
2286
2287 bool AttrParsed = false;
2288 // No scoped names are supported; ideally we could put all non-standard
2289 // attributes into namespaces.
2290 if (!ScopeName) {
2291 switch(AttributeList::getKind(AttrName))
2292 {
2293 // No arguments
Alexis Hunt54a02542009-11-25 04:20:27 +00002294 case AttributeList::AT_carries_dependency:
Anders Carlssone30621b2011-01-23 21:33:18 +00002295 case AttributeList::AT_noreturn: {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002296 if (Tok.is(tok::l_paren)) {
2297 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
2298 << AttrName->getName();
2299 break;
2300 }
2301
John McCall084e83d2011-03-24 11:26:52 +00002302 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc, 0,
2303 SourceLocation(), 0, 0, false, true);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002304 AttrParsed = true;
2305 break;
2306 }
2307
2308 // One argument; must be a type-id or assignment-expression
2309 case AttributeList::AT_aligned: {
2310 if (Tok.isNot(tok::l_paren)) {
2311 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
2312 << AttrName->getName();
2313 break;
2314 }
2315 SourceLocation ParamLoc = ConsumeParen();
2316
John McCalldadc5752010-08-24 06:29:42 +00002317 ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002318
2319 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2320
2321 ExprVector ArgExprs(Actions);
2322 ArgExprs.push_back(ArgExpr.release());
John McCall084e83d2011-03-24 11:26:52 +00002323 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc,
2324 0, ParamLoc, ArgExprs.take(), 1,
2325 false, true);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002326
2327 AttrParsed = true;
2328 break;
2329 }
2330
2331 // Silence warnings
2332 default: break;
2333 }
2334 }
2335
2336 // Skip the entire parameter clause, if any
2337 if (!AttrParsed && Tok.is(tok::l_paren)) {
2338 ConsumeParen();
2339 // SkipUntil maintains the balancedness of tokens.
2340 SkipUntil(tok::r_paren, false);
2341 }
2342 }
2343
2344 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2345 SkipUntil(tok::r_square, false);
2346 Loc = Tok.getLocation();
2347 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2348 SkipUntil(tok::r_square, false);
2349
John McCall53fa7142010-12-24 02:08:15 +00002350 attrs.Range = SourceRange(StartLoc, Loc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002351}
2352
2353/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2354/// attribute.
2355///
2356/// FIXME: Simply returns an alignof() expression if the argument is a
2357/// type. Ideally, the type should be propagated directly into Sema.
2358///
2359/// [C++0x] 'align' '(' type-id ')'
2360/// [C++0x] 'align' '(' assignment-expression ')'
John McCalldadc5752010-08-24 06:29:42 +00002361ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002362 if (isTypeIdInParens()) {
John McCallfaf5fb42010-08-26 23:41:50 +00002363 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002364 SourceLocation TypeLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002365 ParsedType Ty = ParseTypeName().get();
Alexis Hunt96d5c762009-11-21 08:43:09 +00002366 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbournee190dee2011-03-11 19:24:49 +00002367 return Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2368 Ty.getAsOpaquePtr(), TypeRange);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002369 } else
2370 return ParseConstantExpression();
2371}
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00002372
2373/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
2374///
2375/// [MS] ms-attribute:
2376/// '[' token-seq ']'
2377///
2378/// [MS] ms-attribute-seq:
2379/// ms-attribute[opt]
2380/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00002381void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
2382 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00002383 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
2384
2385 while (Tok.is(tok::l_square)) {
2386 ConsumeBracket();
2387 SkipUntil(tok::r_square, true, true);
John McCall53fa7142010-12-24 02:08:15 +00002388 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00002389 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
2390 }
2391}