blob: 56fee66fcdf750abda4f8cb3a344964ea6bef313 [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson0c6139d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor1b7f8982008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000022using namespace clang;
23
24/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000025/// may either be a top level namespace or a block-level namespace alias. If
26/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000027///
28/// namespace-definition: [C++ 7.3: basic.namespace]
29/// named-namespace-definition
30/// unnamed-namespace-definition
31///
32/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000033/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000034///
35/// named-namespace-definition:
36/// original-namespace-definition
37/// extension-namespace-definition
38///
39/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000040/// 'inline'[opt] 'namespace' identifier attributes[opt]
41/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000042///
43/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000044/// 'inline'[opt] 'namespace' original-namespace-name
45/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000046///
Chris Lattner8f08cb72007-08-25 06:57:03 +000047/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
48/// 'namespace' identifier '=' qualified-namespace-specifier ';'
49///
John McCalld226f652010-08-21 09:40:31 +000050Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000051 SourceLocation &DeclEnd,
52 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000053 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000054 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump1eb44332009-09-09 15:08:12 +000055
Douglas Gregor49f40bd2009-09-18 19:03:04 +000056 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000057 Actions.CodeCompleteNamespaceDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +000058 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +000059 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000060
Chris Lattner8f08cb72007-08-25 06:57:03 +000061 SourceLocation IdentLoc;
62 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000063
64 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner04d66662007-10-09 17:33:22 +000066 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000067 Ident = Tok.getIdentifierInfo();
68 IdentLoc = ConsumeToken(); // eat the identifier.
69 }
Mike Stump1eb44332009-09-09 15:08:12 +000070
Chris Lattner8f08cb72007-08-25 06:57:03 +000071 // Read label attributes, if present.
Ted Kremenek1e377652010-02-11 02:19:13 +000072 llvm::OwningPtr<AttributeList> AttrList;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000073 if (Tok.is(tok::kw___attribute)) {
74 attrTok = Tok;
75
Chris Lattner8f08cb72007-08-25 06:57:03 +000076 // FIXME: save these somewhere.
Ted Kremenek1e377652010-02-11 02:19:13 +000077 AttrList.reset(ParseGNUAttributes());
Douglas Gregor6a588dd2009-06-17 19:49:00 +000078 }
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregor6a588dd2009-06-17 19:49:00 +000080 if (Tok.is(tok::equal)) {
81 if (AttrList)
82 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +000083 if (InlineLoc.isValid())
84 Diag(InlineLoc, diag::err_inline_namespace_alias)
85 << FixItHint::CreateRemoval(InlineLoc);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000086
Chris Lattner97144fc2009-04-02 04:16:50 +000087 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000088 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner51448322009-03-29 14:02:43 +000090 if (Tok.isNot(tok::l_brace)) {
Mike Stump1eb44332009-09-09 15:08:12 +000091 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000092 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +000093 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +000094 }
Mike Stump1eb44332009-09-09 15:08:12 +000095
Chris Lattner51448322009-03-29 14:02:43 +000096 SourceLocation LBrace = ConsumeBrace();
97
Douglas Gregor23c94db2010-07-02 17:43:08 +000098 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
99 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
100 getCurScope()->getFnParent()) {
Douglas Gregor95f1b152010-05-14 05:08:22 +0000101 Diag(LBrace, diag::err_namespace_nonnamespace_scope);
102 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000103 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000104 }
105
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000106 // If we're still good, complain about inline namespaces in non-C++0x now.
107 if (!getLang().CPlusPlus0x && InlineLoc.isValid())
108 Diag(InlineLoc, diag::ext_inline_namespace);
109
Chris Lattner51448322009-03-29 14:02:43 +0000110 // Enter a scope for the namespace.
111 ParseScope NamespaceScope(this, Scope::DeclScope);
112
John McCalld226f652010-08-21 09:40:31 +0000113 Decl *NamespcDecl =
Sebastian Redld078e642010-08-27 23:12:46 +0000114 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, IdentLoc, Ident,
115 LBrace, AttrList.get());
Chris Lattner51448322009-03-29 14:02:43 +0000116
John McCallf312b1e2010-08-26 23:41:50 +0000117 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
118 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Sean Huntbbd37c62009-11-21 08:43:09 +0000120 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
121 CXX0XAttributeList Attr;
122 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
123 Attr = ParseCXX0XAttributes();
124 ParseExternalDeclaration(Attr);
125 }
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Chris Lattner51448322009-03-29 14:02:43 +0000127 // Leave the namespace scope.
128 NamespaceScope.Exit();
129
Chris Lattner97144fc2009-04-02 04:16:50 +0000130 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
131 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000132
Chris Lattner97144fc2009-04-02 04:16:50 +0000133 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000134 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000135}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000136
Anders Carlssonf67606a2009-03-28 04:07:16 +0000137/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
138/// alias definition.
139///
John McCalld226f652010-08-21 09:40:31 +0000140Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000141 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000142 IdentifierInfo *Alias,
143 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000144 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Anders Carlssonf67606a2009-03-28 04:07:16 +0000146 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000148 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000149 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000150 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000151 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000152
Anders Carlssonf67606a2009-03-28 04:07:16 +0000153 CXXScopeSpec SS;
154 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000155 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000156
157 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
158 Diag(Tok, diag::err_expected_namespace_name);
159 // Skip to end of the definition and eat the ';'.
160 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000161 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000162 }
163
164 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000165 IdentifierInfo *Ident = Tok.getIdentifierInfo();
166 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Anders Carlssonf67606a2009-03-28 04:07:16 +0000168 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000169 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000170 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
171 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Douglas Gregor23c94db2010-07-02 17:43:08 +0000173 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000174 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000175}
176
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000177/// ParseLinkage - We know that the current token is a string_literal
178/// and just before that, that extern was seen.
179///
180/// linkage-specification: [C++ 7.5p2: dcl.link]
181/// 'extern' string-literal '{' declaration-seq[opt] '}'
182/// 'extern' string-literal declaration
183///
John McCalld226f652010-08-21 09:40:31 +0000184Decl *Parser::ParseLinkage(ParsingDeclSpec &DS,
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000185 unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000186 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000187 llvm::SmallString<8> LangBuffer;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000188 // LangBuffer is guaranteed to be big enough.
Douglas Gregor453091c2010-03-16 22:30:13 +0000189 bool Invalid = false;
190 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
191 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000192 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000193
194 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000195
Douglas Gregor074149e2009-01-05 19:45:36 +0000196 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000197 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000198 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Douglas Gregor074149e2009-01-05 19:45:36 +0000199 /*FIXME: */SourceLocation(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000200 Loc, Lang,
Mike Stump1eb44332009-09-09 15:08:12 +0000201 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000202 : SourceLocation());
203
Sean Huntbbd37c62009-11-21 08:43:09 +0000204 CXX0XAttributeList Attr;
205 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
206 Attr = ParseCXX0XAttributes();
207 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000208
Douglas Gregor074149e2009-01-05 19:45:36 +0000209 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000210 DS.setExternInLinkageSpec(true);
Douglas Gregor09a63c92010-08-24 14:14:45 +0000211 ParseExternalDeclaration(Attr, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000212 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000213 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000214 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000215
Douglas Gregor63a01132010-02-07 08:38:28 +0000216 DS.abort();
217
Sean Huntbbd37c62009-11-21 08:43:09 +0000218 if (Attr.HasAttr)
219 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
220 << Attr.Range;
221
Douglas Gregorf44515a2008-12-16 22:23:02 +0000222 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000223 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000224 CXX0XAttributeList Attr;
225 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
226 Attr = ParseCXX0XAttributes();
227 ParseExternalDeclaration(Attr);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000228 }
229
Douglas Gregorf44515a2008-12-16 22:23:02 +0000230 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000231 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000232}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000233
Douglas Gregorf780abc2008-12-30 03:27:21 +0000234/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
235/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000236Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000237 SourceLocation &DeclEnd,
238 CXX0XAttributeList Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000239 assert(Tok.is(tok::kw_using) && "Not using token");
240
241 // Eat 'using'.
242 SourceLocation UsingLoc = ConsumeToken();
243
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000244 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000245 Actions.CodeCompleteUsing(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000246 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000247 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000248
Chris Lattner2f274772009-01-06 06:55:51 +0000249 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000250 // Next token after 'using' is 'namespace' so it must be using-directive
Sean Huntbbd37c62009-11-21 08:43:09 +0000251 return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList);
252
253 if (Attr.HasAttr)
254 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
255 << Attr.Range;
Chris Lattner2f274772009-01-06 06:55:51 +0000256
257 // Otherwise, it must be using-declaration.
Sean Huntbbd37c62009-11-21 08:43:09 +0000258 // Ignore illegal attributes (the caller should already have issued an error.
Chris Lattner97144fc2009-04-02 04:16:50 +0000259 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000260}
261
262/// ParseUsingDirective - Parse C++ using-directive, assumes
263/// that current token is 'namespace' and 'using' was already parsed.
264///
265/// using-directive: [C++ 7.3.p4: namespace.udir]
266/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
267/// namespace-name ;
268/// [GNU] using-directive:
269/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
270/// namespace-name attributes[opt] ;
271///
John McCalld226f652010-08-21 09:40:31 +0000272Decl *Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000273 SourceLocation UsingLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000274 SourceLocation &DeclEnd,
275 AttributeList *Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000276 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
277
278 // Eat 'namespace'.
279 SourceLocation NamespcLoc = ConsumeToken();
280
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000281 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000282 Actions.CodeCompleteUsingDirective(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000283 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000284 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000285
Douglas Gregorf780abc2008-12-30 03:27:21 +0000286 CXXScopeSpec SS;
287 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000288 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000289
Douglas Gregorf780abc2008-12-30 03:27:21 +0000290 IdentifierInfo *NamespcName = 0;
291 SourceLocation IdentLoc = SourceLocation();
292
293 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000294 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000295 Diag(Tok, diag::err_expected_namespace_name);
296 // If there was invalid namespace name, skip to end of decl, and eat ';'.
297 SkipUntil(tok::semi);
298 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000299 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Chris Lattner823c44e2009-01-06 07:27:21 +0000302 // Parse identifier.
303 NamespcName = Tok.getIdentifierInfo();
304 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Chris Lattner823c44e2009-01-06 07:27:21 +0000306 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000307 bool GNUAttr = false;
308 if (Tok.is(tok::kw___attribute)) {
309 GNUAttr = true;
310 Attr = addAttributeLists(Attr, ParseGNUAttributes());
311 }
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Chris Lattner823c44e2009-01-06 07:27:21 +0000313 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000314 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000315 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000316 GNUAttr ? diag::err_expected_semi_after_attribute_list
317 : diag::err_expected_semi_after_namespace_name,
318 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000319
Douglas Gregor23c94db2010-07-02 17:43:08 +0000320 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000321 IdentLoc, NamespcName, Attr);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000322}
323
324/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
325/// 'using' was already seen.
326///
327/// using-declaration: [C++ 7.3.p3: namespace.udecl]
328/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000329/// unqualified-id
330/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000331///
John McCalld226f652010-08-21 09:40:31 +0000332Decl *Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000333 SourceLocation UsingLoc,
Anders Carlsson595adc12009-08-29 19:54:19 +0000334 SourceLocation &DeclEnd,
335 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000336 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000337 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000338 bool IsTypeName;
339
340 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000341 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000342 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000343 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000344 ConsumeToken();
345 IsTypeName = true;
346 }
347 else
348 IsTypeName = false;
349
350 // Parse nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000351 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000352
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000353 // Check nested-name specifier.
354 if (SS.isInvalid()) {
355 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000356 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000357 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000358
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000359 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000360 // destructor names and allow the action module to diagnose any semantic
361 // errors.
362 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000363 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000364 /*EnteringContext=*/false,
365 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000366 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000367 ParsedType(),
Douglas Gregor12c118a2009-11-04 16:30:06 +0000368 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000369 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000370 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000371 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000372
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000373 // Parse (optional) attributes (most likely GNU strong-using extension).
Ted Kremenek1e377652010-02-11 02:19:13 +0000374 llvm::OwningPtr<AttributeList> AttrList;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000375 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +0000376 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000378 // Eat ';'.
379 DeclEnd = Tok.getLocation();
380 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000381 AttrList ? "attributes list" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000382 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000383
Douglas Gregor23c94db2010-07-02 17:43:08 +0000384 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS, Name,
Ted Kremenek1e377652010-02-11 02:19:13 +0000385 AttrList.get(), IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000386}
387
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000388/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
389///
390/// static_assert-declaration:
391/// static_assert ( constant-expression , string-literal ) ;
392///
John McCalld226f652010-08-21 09:40:31 +0000393Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000394 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
395 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000397 if (Tok.isNot(tok::l_paren)) {
398 Diag(Tok, diag::err_expected_lparen);
John McCalld226f652010-08-21 09:40:31 +0000399 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000400 }
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000402 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000403
John McCall60d7b3a2010-08-24 06:29:42 +0000404 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000405 if (AssertExpr.isInvalid()) {
406 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000407 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Anders Carlssonad5f9602009-03-13 23:29:20 +0000410 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000411 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000412
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000413 if (Tok.isNot(tok::string_literal)) {
414 Diag(Tok, diag::err_expected_string_literal);
415 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000416 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000417 }
Mike Stump1eb44332009-09-09 15:08:12 +0000418
John McCall60d7b3a2010-08-24 06:29:42 +0000419 ExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000420 if (AssertMessage.isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000421 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000422
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000423 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattner97144fc2009-04-02 04:16:50 +0000425 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000426 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000427
John McCall9ae2f072010-08-23 23:25:46 +0000428 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
429 AssertExpr.take(),
430 AssertMessage.take());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000431}
432
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000433/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
434///
435/// 'decltype' ( expression )
436///
437void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
438 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
439
440 SourceLocation StartLoc = ConsumeToken();
441 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000442
443 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000444 "decltype")) {
445 SkipUntil(tok::r_paren);
446 return;
447 }
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000449 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000451 // C++0x [dcl.type.simple]p4:
452 // The operand of the decltype specifier is an unevaluated operand.
453 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000454 Sema::Unevaluated);
John McCall60d7b3a2010-08-24 06:29:42 +0000455 ExprResult Result = ParseExpression();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000456 if (Result.isInvalid()) {
457 SkipUntil(tok::r_paren);
458 return;
459 }
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000461 // Match the ')'
462 SourceLocation RParenLoc;
463 if (Tok.is(tok::r_paren))
464 RParenLoc = ConsumeParen();
465 else
466 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000468 if (RParenLoc.isInvalid())
469 return;
470
471 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000472 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000473 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000474 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000475 DiagID, Result.release()))
476 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000477}
478
Douglas Gregor42a552f2008-11-05 20:51:48 +0000479/// ParseClassName - Parse a C++ class-name, which names a class. Note
480/// that we only check that the result names a type; semantic analysis
481/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000482/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000483/// found.
484///
485/// class-name: [C++ 9.1]
486/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000487/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000488///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000489Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000490 CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000491 // Check whether we have a template-id that names a type.
492 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000493 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000494 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000495 if (TemplateId->Kind == TNK_Type_template ||
496 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000497 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000498
499 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000500 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000501 EndLocation = Tok.getAnnotationEndLoc();
502 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000503
504 if (Type)
505 return Type;
506 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000507 }
508
509 // Fall through to produce an error below.
510 }
511
Douglas Gregor42a552f2008-11-05 20:51:48 +0000512 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000513 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000514 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000515 }
516
Douglas Gregor84d0a192010-01-12 21:28:44 +0000517 IdentifierInfo *Id = Tok.getIdentifierInfo();
518 SourceLocation IdLoc = ConsumeToken();
519
520 if (Tok.is(tok::less)) {
521 // It looks the user intended to write a template-id here, but the
522 // template-name was wrong. Try to fix that.
523 TemplateNameKind TNK = TNK_Type_template;
524 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000525 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor84d0a192010-01-12 21:28:44 +0000526 SS, Template, TNK)) {
527 Diag(IdLoc, diag::err_unknown_template_name)
528 << Id;
529 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000530
Douglas Gregor84d0a192010-01-12 21:28:44 +0000531 if (!Template)
532 return true;
533
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000534 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000535 UnqualifiedId TemplateName;
536 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000537
Douglas Gregor84d0a192010-01-12 21:28:44 +0000538 // Parse the full template-id, then turn it into a type.
539 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
540 SourceLocation(), true))
541 return true;
542 if (TNK == TNK_Dependent_template_name)
543 AnnotateTemplateIdTokenAsType(SS);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000544
Douglas Gregor84d0a192010-01-12 21:28:44 +0000545 // If we didn't end up with a typename token, there's nothing more we
546 // can do.
547 if (Tok.isNot(tok::annot_typename))
548 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000549
Douglas Gregor84d0a192010-01-12 21:28:44 +0000550 // Retrieve the type from the annotation token, consume that token, and
551 // return.
552 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000553 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000554 ConsumeToken();
555 return Type;
556 }
557
Douglas Gregor42a552f2008-11-05 20:51:48 +0000558 // We have an identifier; check whether it is actually a type.
John McCallb3d87482010-08-24 05:47:05 +0000559 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), SS, true);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000560 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000561 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000562 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000563 }
564
565 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000566 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000567
568 // Fake up a Declarator to use with ActOnTypeName.
569 DeclSpec DS;
570 DS.SetRangeStart(IdLoc);
571 DS.SetRangeEnd(EndLocation);
572 DS.getTypeSpecScope() = *SS;
573
574 const char *PrevSpec = 0;
575 unsigned DiagID;
576 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
577
578 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
579 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000580}
581
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000582/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
583/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
584/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000585/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000586///
587/// class-specifier: [C++ class]
588/// class-head '{' member-specification[opt] '}'
589/// class-head '{' member-specification[opt] '}' attributes[opt]
590/// class-head:
591/// class-key identifier[opt] base-clause[opt]
592/// class-key nested-name-specifier identifier base-clause[opt]
593/// class-key nested-name-specifier[opt] simple-template-id
594/// base-clause[opt]
595/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000596/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000597/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000598/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000599/// simple-template-id base-clause[opt]
600/// class-key:
601/// 'class'
602/// 'struct'
603/// 'union'
604///
605/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000606/// class-key ::[opt] nested-name-specifier[opt] identifier
607/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
608/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000609///
610/// Note that the C++ class-specifier and elaborated-type-specifier,
611/// together, subsume the C99 struct-or-union-specifier:
612///
613/// struct-or-union-specifier: [C99 6.7.2.1]
614/// struct-or-union identifier[opt] '{' struct-contents '}'
615/// struct-or-union identifier
616/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
617/// '}' attributes[opt]
618/// [GNU] struct-or-union attributes[opt] identifier
619/// struct-or-union:
620/// 'struct'
621/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000622void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
623 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000624 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redld9bafa72010-02-03 21:21:43 +0000625 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000626 DeclSpec::TST TagType;
627 if (TagTokKind == tok::kw_struct)
628 TagType = DeclSpec::TST_struct;
629 else if (TagTokKind == tok::kw_class)
630 TagType = DeclSpec::TST_class;
631 else {
632 assert(TagTokKind == tok::kw_union && "Not a class specifier");
633 TagType = DeclSpec::TST_union;
634 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000635
Douglas Gregor374929f2009-09-18 15:37:17 +0000636 if (Tok.is(tok::code_completion)) {
637 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000638 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregordc845342010-05-25 05:58:43 +0000639 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +0000640 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000641
Chandler Carruth926c4b42010-06-28 08:39:25 +0000642 // C++03 [temp.explicit] 14.7.2/8:
643 // The usual access checking rules do not apply to names used to specify
644 // explicit instantiations.
645 //
646 // As an extension we do not perform access checking on the names used to
647 // specify explicit specializations either. This is important to allow
648 // specializing traits classes for private types.
649 bool SuppressingAccessChecks = false;
650 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
651 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
652 Actions.ActOnStartSuppressingAccessChecks();
653 SuppressingAccessChecks = true;
654 }
655
Sean Huntbbd37c62009-11-21 08:43:09 +0000656 AttributeList *AttrList = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000657 // If attributes exist after tag, parse them.
658 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000659 AttrList = ParseGNUAttributes();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000660
Steve Narofff59e17e2008-12-24 20:59:21 +0000661 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +0000662 while (Tok.is(tok::kw___declspec))
Sean Huntbbd37c62009-11-21 08:43:09 +0000663 AttrList = ParseMicrosoftDeclSpec(AttrList);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000664
Sean Huntbbd37c62009-11-21 08:43:09 +0000665 // If C++0x attributes exist here, parse them.
666 // FIXME: Are we consistent with the ordering of parsing of different
667 // styles of attributes?
668 if (isCXX0XAttributeSpecifier())
669 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Douglas Gregorb117a602009-09-04 05:53:02 +0000671 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
672 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
673 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000674 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000675 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
676 // properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000677 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000678 Tok.setKind(tok::identifier);
679 }
680
681 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
682 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
683 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000684 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000685 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
686 // properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000687 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000688 Tok.setKind(tok::identifier);
689 }
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000691 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000692 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000693 if (getLang().CPlusPlus) {
694 // "FOO : BAR" is not a potential typo for "FOO::BAR".
695 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000696
John McCallb3d87482010-08-24 05:47:05 +0000697 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true))
John McCall207014e2010-07-30 06:26:29 +0000698 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +0000699 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +0000700 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
701 Diag(Tok, diag::err_expected_ident);
702 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000703
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000704 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
705
Douglas Gregorcc636682009-02-17 23:15:12 +0000706 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000707 IdentifierInfo *Name = 0;
708 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000709 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000710 if (Tok.is(tok::identifier)) {
711 Name = Tok.getIdentifierInfo();
712 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000713
Douglas Gregor5ee37342010-05-30 22:30:21 +0000714 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000715 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000716 // Eat the template argument list and try to continue parsing this as
717 // a class (or template thereof).
718 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000719 SourceLocation LAngleLoc, RAngleLoc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000720 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000721 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000722 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000723 // We couldn't parse the template argument list at all, so don't
724 // try to give any location information for the list.
725 LAngleLoc = RAngleLoc = SourceLocation();
726 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000727
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000728 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000729 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000730 << (TagType == DeclSpec::TST_class? 0
731 : TagType == DeclSpec::TST_struct? 1
732 : 2)
733 << Name
734 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000735
736 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000737 // we've removed its template argument list.
738 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
739 if (TemplateParams && TemplateParams->size() > 1) {
740 TemplateParams->pop_back();
741 } else {
742 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000743 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000744 = ParsedTemplateInfo::NonTemplate;
745 }
746 } else if (TemplateInfo.Kind
747 == ParsedTemplateInfo::ExplicitInstantiation) {
748 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000749 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000750 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000751 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000752 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000753 = SourceLocation();
754 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
755 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000756 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000757 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000758 } else if (Tok.is(tok::annot_template_id)) {
759 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
760 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000761
Douglas Gregorc45c2322009-03-31 00:43:58 +0000762 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000763 // The template-name in the simple-template-id refers to
764 // something other than a class template. Give an appropriate
765 // error message and skip to the ';'.
766 SourceRange Range(NameLoc);
767 if (SS.isNotEmpty())
768 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000769
Douglas Gregor39a8de12009-02-25 19:37:18 +0000770 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
771 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Douglas Gregor39a8de12009-02-25 19:37:18 +0000773 DS.SetTypeSpecError();
774 SkipUntil(tok::semi, false, true);
775 TemplateId->Destroy();
Chandler Carruth926c4b42010-06-28 08:39:25 +0000776 if (SuppressingAccessChecks)
777 Actions.ActOnStopSuppressingAccessChecks();
778
Douglas Gregor39a8de12009-02-25 19:37:18 +0000779 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000780 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000781 }
782
Chandler Carruth926c4b42010-06-28 08:39:25 +0000783 // As soon as we're finished parsing the class's template-id, turn access
784 // checking back on.
785 if (SuppressingAccessChecks)
786 Actions.ActOnStopSuppressingAccessChecks();
787
John McCall67d1a672009-08-06 02:15:43 +0000788 // There are four options here. If we have 'struct foo;', then this
789 // is either a forward declaration or a friend declaration, which
790 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000791 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000792 // something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000793 // However, in some contexts, things look like declarations but are just
794 // references, e.g.
795 // new struct s;
796 // or
797 // &T::operator struct s;
798 // For these, SuppressDeclarations is true.
John McCallf312b1e2010-08-26 23:41:50 +0000799 Sema::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000800 if (SuppressDeclarations)
John McCallf312b1e2010-08-26 23:41:50 +0000801 TUK = Sema::TUK_Reference;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000802 else if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))){
Douglas Gregord85bea22009-09-26 06:47:28 +0000803 if (DS.isFriendSpecified()) {
804 // C++ [class.friend]p2:
805 // A class shall not be defined in a friend declaration.
806 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
807 << SourceRange(DS.getFriendSpecLoc());
808
809 // Skip everything up to the semicolon, so that this looks like a proper
810 // friend class (or template thereof) declaration.
811 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +0000812 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +0000813 } else {
814 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +0000815 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +0000816 }
817 } else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +0000818 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000819 else
John McCallf312b1e2010-08-26 23:41:50 +0000820 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000821
John McCall207014e2010-07-30 06:26:29 +0000822 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +0000823 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +0000824 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
825 // We have a declaration or reference to an anonymous class.
826 Diag(StartLoc, diag::err_anon_type_definition)
827 << DeclSpec::getSpecifierName(TagType);
828 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000829
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000830 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000831
832 if (TemplateId)
833 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000834 return;
835 }
836
Douglas Gregorddc29e12009-02-06 22:42:48 +0000837 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +0000838 DeclResult TagOrTempResult = true; // invalid
839 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000840
Douglas Gregor402abb52009-05-28 23:31:59 +0000841 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000842 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000843 // Explicit specialization, class template partial specialization,
844 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000845 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000846 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000847 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000848 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000849 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000850 // This is an explicit instantiation of a class template.
851 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000852 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000853 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000854 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000855 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000856 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000857 SS,
John McCall2b5289b2010-08-23 07:28:44 +0000858 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000859 TemplateId->TemplateNameLoc,
860 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000861 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000862 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000863 AttrList);
John McCall74256f52010-04-14 00:24:33 +0000864
865 // Friend template-ids are treated as references unless
866 // they have template headers, in which case they're ill-formed
867 // (FIXME: "template <class T> friend class A<T>::B<int>;").
868 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +0000869 } else if (TUK == Sema::TUK_Reference ||
870 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +0000871 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
John McCallc4e70192009-09-11 04:59:25 +0000872 TypeResult
John McCall2b5289b2010-08-23 07:28:44 +0000873 = Actions.ActOnTemplateIdType(TemplateId->Template,
John McCall6b2becf2009-09-08 17:47:29 +0000874 TemplateId->TemplateNameLoc,
875 TemplateId->LAngleLoc,
876 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000877 TemplateId->RAngleLoc);
878
John McCallc4e70192009-09-11 04:59:25 +0000879 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
880 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000881 } else {
882 // This is an explicit specialization or a class template
883 // partial specialization.
884 TemplateParameterLists FakedParamLists;
885
886 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
887 // This looks like an explicit instantiation, because we have
888 // something like
889 //
890 // template class Foo<X>
891 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000892 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000893 // meant to be an explicit specialization, but the user forgot
894 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +0000895 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000896
Mike Stump1eb44332009-09-09 15:08:12 +0000897 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000898 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000899 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000900 diag::err_explicit_instantiation_with_definition)
901 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +0000902 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000903
904 // Create a fake template parameter list that contains only
905 // "template<>", so that we treat this construct as a class
906 // template specialization.
907 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000908 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000909 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000910 LAngleLoc,
911 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000912 LAngleLoc));
913 TemplateParams = &FakedParamLists;
914 }
915
916 // Build the class template specialization.
917 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000918 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000919 StartLoc, SS,
John McCall2b5289b2010-08-23 07:28:44 +0000920 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000921 TemplateId->TemplateNameLoc,
922 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000923 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000924 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000925 AttrList,
John McCallf312b1e2010-08-26 23:41:50 +0000926 MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000927 TemplateParams? &(*TemplateParams)[0] : 0,
928 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000929 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000930 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000931 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000932 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000933 // Explicit instantiation of a member of a class template
934 // specialization, e.g.,
935 //
936 // template struct Outer<int>::Inner;
937 //
938 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000939 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000940 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000941 TemplateInfo.TemplateLoc,
942 TagType, StartLoc, SS, Name,
Sean Huntbbd37c62009-11-21 08:43:09 +0000943 NameLoc, AttrList);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000944 } else {
945 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000946 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000947 // FIXME: Diagnose this particular error.
948 }
949
John McCallc4e70192009-09-11 04:59:25 +0000950 bool IsDependent = false;
951
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000952 // Declaration or definition of a class type
Douglas Gregor23c94db2010-07-02 17:43:08 +0000953 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000954 Name, NameLoc, AttrList, AS,
John McCallf312b1e2010-08-26 23:41:50 +0000955 MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000956 TemplateParams? &(*TemplateParams)[0] : 0,
957 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000958 Owned, IsDependent);
959
960 // If ActOnTag said the type was dependent, try again with the
961 // less common call.
962 if (IsDependent)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000963 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000964 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000965 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000966
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000967 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +0000968 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +0000969 assert(Tok.is(tok::l_brace) ||
970 (getLang().CPlusPlus && Tok.is(tok::colon)));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000971 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000972 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000973 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000974 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000975 }
976
John McCallb3d87482010-08-24 05:47:05 +0000977 // FIXME: The DeclSpec should keep the locations of both the keyword and the
978 // name (if there is one).
979 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
980
981 const char *PrevSpec = 0;
982 unsigned DiagID;
983 bool Result;
John McCallc4e70192009-09-11 04:59:25 +0000984 if (!TypeResult.isInvalid()) {
John McCallb3d87482010-08-24 05:47:05 +0000985 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc,
986 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +0000987 } else if (!TagOrTempResult.isInvalid()) {
John McCallb3d87482010-08-24 05:47:05 +0000988 Result = DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID,
989 TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +0000990 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000991 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000992 return;
993 }
Mike Stump1eb44332009-09-09 15:08:12 +0000994
John McCallb3d87482010-08-24 05:47:05 +0000995 if (Result)
John McCallfec54012009-08-03 20:12:06 +0000996 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000997
Chris Lattner4ed5d912010-02-02 01:23:29 +0000998 // At this point, we've successfully parsed a class-specifier in 'definition'
999 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1000 // going to look at what comes after it to improve error recovery. If an
1001 // impossible token occurs next, we assume that the programmer forgot a ; at
1002 // the end of the declaration and recover that way.
1003 //
1004 // This switch enumerates the valid "follow" set for definition.
John McCallf312b1e2010-08-26 23:41:50 +00001005 if (TUK == Sema::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001006 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001007 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001008 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001009 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +00001010 case tok::star: // struct foo {...} * P;
1011 case tok::amp: // struct foo {...} & R = ...
1012 case tok::identifier: // struct foo {...} V ;
1013 case tok::r_paren: //(struct foo {...} ) {4}
1014 case tok::annot_cxxscope: // struct foo {...} a:: b;
1015 case tok::annot_typename: // struct foo {...} a ::b;
1016 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +00001017 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +00001018 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001019 ExpectedSemi = false;
1020 break;
1021 // Type qualifiers
1022 case tok::kw_const: // struct foo {...} const x;
1023 case tok::kw_volatile: // struct foo {...} volatile x;
1024 case tok::kw_restrict: // struct foo {...} restrict x;
1025 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +00001026 // Storage-class specifiers
1027 case tok::kw_static: // struct foo {...} static x;
1028 case tok::kw_extern: // struct foo {...} extern x;
1029 case tok::kw_typedef: // struct foo {...} typedef x;
1030 case tok::kw_register: // struct foo {...} register x;
1031 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregor33f99242010-05-17 18:19:56 +00001032 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001033 // As shown above, type qualifiers and storage class specifiers absolutely
1034 // can occur after class specifiers according to the grammar. However,
1035 // almost noone actually writes code like this. If we see one of these,
1036 // it is much more likely that someone missed a semi colon and the
1037 // type/storage class specifier we're seeing is part of the *next*
1038 // intended declaration, as in:
1039 //
1040 // struct foo { ... }
1041 // typedef int X;
1042 //
1043 // We'd really like to emit a missing semicolon error instead of emitting
1044 // an error on the 'int' saying that you can't have two type specifiers in
1045 // the same declaration of X. Because of this, we look ahead past this
1046 // token to see if it's a type specifier. If so, we know the code is
1047 // otherwise invalid, so we can produce the expected semi error.
1048 if (!isKnownToBeTypeSpecifier(NextToken()))
1049 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001050 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001051
1052 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001053 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001054 if (!getLang().CPlusPlus)
1055 ExpectedSemi = false;
1056 break;
1057 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001058
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001059 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001060 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1061 TagType == DeclSpec::TST_class ? "class"
1062 : TagType == DeclSpec::TST_struct? "struct" : "union");
1063 // Push this token back into the preprocessor and change our current token
1064 // to ';' so that the rest of the code recovers as though there were an
1065 // ';' after the definition.
1066 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001067 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001068 }
1069 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001070}
1071
Mike Stump1eb44332009-09-09 15:08:12 +00001072/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001073///
1074/// base-clause : [C++ class.derived]
1075/// ':' base-specifier-list
1076/// base-specifier-list:
1077/// base-specifier '...'[opt]
1078/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001079void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001080 assert(Tok.is(tok::colon) && "Not a base clause");
1081 ConsumeToken();
1082
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001083 // Build up an array of parsed base specifiers.
John McCallca0408f2010-08-23 06:44:23 +00001084 llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001085
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001086 while (true) {
1087 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001088 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001089 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001090 // Skip the rest of this base specifier, up until the comma or
1091 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001092 SkipUntil(tok::comma, tok::l_brace, true, true);
1093 } else {
1094 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001095 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001096 }
1097
1098 // If the next token is a comma, consume it and keep reading
1099 // base-specifiers.
1100 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001102 // Consume the comma.
1103 ConsumeToken();
1104 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001105
1106 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001107 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001108}
1109
1110/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1111/// one entry in the base class list of a class specifier, for example:
1112/// class foo : public bar, virtual private baz {
1113/// 'public bar' and 'virtual private baz' are each base-specifiers.
1114///
1115/// base-specifier: [C++ class.derived]
1116/// ::[opt] nested-name-specifier[opt] class-name
1117/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1118/// class-name
1119/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1120/// class-name
John McCalld226f652010-08-21 09:40:31 +00001121Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001122 bool IsVirtual = false;
1123 SourceLocation StartLoc = Tok.getLocation();
1124
1125 // Parse the 'virtual' keyword.
1126 if (Tok.is(tok::kw_virtual)) {
1127 ConsumeToken();
1128 IsVirtual = true;
1129 }
1130
1131 // Parse an (optional) access specifier.
1132 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001133 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001134 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001136 // Parse the 'virtual' keyword (again!), in case it came after the
1137 // access specifier.
1138 if (Tok.is(tok::kw_virtual)) {
1139 SourceLocation VirtualLoc = ConsumeToken();
1140 if (IsVirtual) {
1141 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001142 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001143 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001144 }
1145
1146 IsVirtual = true;
1147 }
1148
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001149 // Parse optional '::' and optional nested-name-specifier.
1150 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001151 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001152
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001153 // The location of the base class itself.
1154 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001155
1156 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001157 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +00001158 TypeResult BaseType = ParseClassName(EndLocation, &SS);
1159 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001160 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001161
1162 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001163 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001165 // Notify semantic analysis that we have parsed a complete
1166 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001167 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +00001168 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001169}
1170
1171/// getAccessSpecifierIfPresent - Determine whether the next token is
1172/// a C++ access-specifier.
1173///
1174/// access-specifier: [C++ class.derived]
1175/// 'private'
1176/// 'protected'
1177/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001178AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001179 switch (Tok.getKind()) {
1180 default: return AS_none;
1181 case tok::kw_private: return AS_private;
1182 case tok::kw_protected: return AS_protected;
1183 case tok::kw_public: return AS_public;
1184 }
1185}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001186
Eli Friedmand33133c2009-07-22 21:45:50 +00001187void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
John McCalld226f652010-08-21 09:40:31 +00001188 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001189 // We just declared a member function. If this member function
1190 // has any default arguments, we'll need to parse them later.
1191 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001192 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +00001193 = DeclaratorInfo.getTypeObject(0).Fun;
1194 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1195 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1196 if (!LateMethod) {
1197 // Push this method onto the stack of late-parsed method
1198 // declarations.
1199 getCurrentClass().MethodDecls.push_back(
1200 LateParsedMethodDeclaration(ThisDecl));
1201 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001202 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001203
1204 // Add all of the parameters prior to this one (they don't
1205 // have default arguments).
1206 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1207 for (unsigned I = 0; I < ParamIdx; ++I)
1208 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001209 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001210 }
1211
1212 // Add this parameter to the list of parameters (it or may
1213 // not have a default argument).
1214 LateMethod->DefaultArgs.push_back(
1215 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1216 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1217 }
1218 }
1219}
1220
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001221/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1222///
1223/// member-declaration:
1224/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1225/// function-definition ';'[opt]
1226/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1227/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001228/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001229/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001230/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001231///
1232/// member-declarator-list:
1233/// member-declarator
1234/// member-declarator-list ',' member-declarator
1235///
1236/// member-declarator:
1237/// declarator pure-specifier[opt]
1238/// declarator constant-initializer[opt]
1239/// identifier[opt] ':' constant-expression
1240///
Sebastian Redle2b68332009-04-12 17:16:29 +00001241/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001242/// '= 0'
1243///
1244/// constant-initializer:
1245/// '=' constant-expression
1246///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001247void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCallc9068d72010-07-16 08:13:16 +00001248 const ParsedTemplateInfo &TemplateInfo,
1249 ParsingDeclRAIIObject *TemplateDiags) {
John McCall60fa3cf2009-12-11 02:10:03 +00001250 // Access declarations.
1251 if (!TemplateInfo.Kind &&
1252 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001253 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001254 Tok.is(tok::annot_cxxscope)) {
1255 bool isAccessDecl = false;
1256 if (NextToken().is(tok::identifier))
1257 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1258 else
1259 isAccessDecl = NextToken().is(tok::kw_operator);
1260
1261 if (isAccessDecl) {
1262 // Collect the scope specifier token we annotated earlier.
1263 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001264 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
John McCall60fa3cf2009-12-11 02:10:03 +00001265
1266 // Try to parse an unqualified-id.
1267 UnqualifiedId Name;
John McCallb3d87482010-08-24 05:47:05 +00001268 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001269 SkipUntil(tok::semi);
1270 return;
1271 }
1272
1273 // TODO: recover from mistakenly-qualified operator declarations.
1274 if (ExpectAndConsume(tok::semi,
1275 diag::err_expected_semi_after,
1276 "access declaration",
1277 tok::semi))
1278 return;
1279
Douglas Gregor23c94db2010-07-02 17:43:08 +00001280 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001281 false, SourceLocation(),
1282 SS, Name,
1283 /* AttrList */ 0,
1284 /* IsTypeName */ false,
1285 SourceLocation());
1286 return;
1287 }
1288 }
1289
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001290 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001291 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001292 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001293 SourceLocation DeclEnd;
1294 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001295 return;
1296 }
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Chris Lattner682bf922009-03-29 16:50:03 +00001298 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001299 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001300 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001301 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001302 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001303 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001304 return;
1305 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001306
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001307 // Handle: member-declaration ::= '__extension__' member-declaration
1308 if (Tok.is(tok::kw___extension__)) {
1309 // __extension__ silences extension warnings in the subexpression.
1310 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1311 ConsumeToken();
John McCallc9068d72010-07-16 08:13:16 +00001312 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001313 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001314
Chris Lattner4ed5d912010-02-02 01:23:29 +00001315 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1316 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001317 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001318
Sean Huntbbd37c62009-11-21 08:43:09 +00001319 CXX0XAttributeList AttrList;
1320 // Optional C++0x attribute-specifier
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001321 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Sean Huntbbd37c62009-11-21 08:43:09 +00001322 AttrList = ParseCXX0XAttributes();
Sean Huntbbd37c62009-11-21 08:43:09 +00001323
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001324 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001325 // FIXME: Check for template aliases
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001326
Sean Huntbbd37c62009-11-21 08:43:09 +00001327 if (AttrList.HasAttr)
1328 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1329 << AttrList.Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001331 // Eat 'using'.
1332 SourceLocation UsingLoc = ConsumeToken();
1333
1334 if (Tok.is(tok::kw_namespace)) {
1335 Diag(UsingLoc, diag::err_using_namespace_in_class);
1336 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001337 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001338 SourceLocation DeclEnd;
1339 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001340 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001341 }
1342 return;
1343 }
1344
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001345 SourceLocation DSStart = Tok.getLocation();
1346 // decl-specifier-seq:
1347 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001348 ParsingDeclSpec DS(*this, TemplateDiags);
Sean Huntbbd37c62009-11-21 08:43:09 +00001349 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001350 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001351
John McCallf312b1e2010-08-26 23:41:50 +00001352 MultiTemplateParamsArg TemplateParams(Actions,
John McCalldd4a3b02009-09-16 22:47:08 +00001353 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1354 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1355
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001356 if (Tok.is(tok::semi)) {
1357 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001358 Decl *TheDecl =
John McCallc9068d72010-07-16 08:13:16 +00001359 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
1360 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001361 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001362 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001363
John McCall54abf7d2009-11-04 02:18:39 +00001364 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001365
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001366 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001367 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1368 ColonProtectionRAIIObject X(*this);
1369
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001370 // Parse the first declarator.
1371 ParseDeclarator(DeclaratorInfo);
1372 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001373 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001374 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001375 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001376 if (Tok.is(tok::semi))
1377 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001378 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001379 }
1380
John Thompson1b2fc0f2009-11-25 22:58:06 +00001381 // If attributes exist after the declarator, but before an '{', parse them.
1382 if (Tok.is(tok::kw___attribute)) {
1383 SourceLocation Loc;
1384 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1385 DeclaratorInfo.AddAttributes(AttrList, Loc);
1386 }
1387
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001388 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001389 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001390 || (DeclaratorInfo.isFunctionDeclarator() &&
1391 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001392 if (!DeclaratorInfo.isFunctionDeclarator()) {
1393 Diag(Tok, diag::err_func_def_no_params);
1394 ConsumeBrace();
1395 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001396 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001397 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001398
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001399 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1400 Diag(Tok, diag::err_function_declared_typedef);
1401 // This recovery skips the entire function body. It would be nice
1402 // to simply call ParseCXXInlineMethodDef() below, however Sema
1403 // assumes the declarator represents a function, not a typedef.
1404 ConsumeBrace();
1405 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001406 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001407 }
1408
Douglas Gregor37b372b2009-08-20 22:52:58 +00001409 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001410 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001411 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001412 }
1413
1414 // member-declarator-list:
1415 // member-declarator
1416 // member-declarator-list ',' member-declarator
1417
John McCalld226f652010-08-21 09:40:31 +00001418 llvm::SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00001419 ExprResult BitfieldSize;
1420 ExprResult Init;
Sebastian Redle2b68332009-04-12 17:16:29 +00001421 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001422
1423 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001424 // member-declarator:
1425 // declarator pure-specifier[opt]
1426 // declarator constant-initializer[opt]
1427 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001428 if (Tok.is(tok::colon)) {
1429 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001430 BitfieldSize = ParseConstantExpression();
1431 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001432 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001433 }
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001435 // pure-specifier:
1436 // '= 0'
1437 //
1438 // constant-initializer:
1439 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001440 //
1441 // defaulted/deleted function-definition:
1442 // '=' 'default' [TODO]
1443 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001444 if (Tok.is(tok::equal)) {
1445 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001446 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1447 ConsumeToken();
1448 Deleted = true;
1449 } else {
1450 Init = ParseInitializer();
1451 if (Init.isInvalid())
1452 SkipUntil(tok::comma, true, true);
1453 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001454 }
1455
Chris Lattnere6563252010-06-13 05:34:18 +00001456 // If a simple-asm-expr is present, parse it.
1457 if (Tok.is(tok::kw_asm)) {
1458 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001459 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00001460 if (AsmLabel.isInvalid())
1461 SkipUntil(tok::comma, true, true);
1462
1463 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1464 DeclaratorInfo.SetRangeEnd(Loc);
1465 }
1466
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001467 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001468 if (Tok.is(tok::kw___attribute)) {
1469 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001470 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001471 DeclaratorInfo.AddAttributes(AttrList, Loc);
1472 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001473
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001474 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001475 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001476 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001477
John McCalld226f652010-08-21 09:40:31 +00001478 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00001479 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001480 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00001481 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCallbbbcdd92009-09-11 21:02:39 +00001482 /*IsDefinition*/ false,
1483 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001484 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001485 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00001486 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001487 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001488 BitfieldSize.release(),
1489 Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001490 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001491 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001492 }
Chris Lattner682bf922009-03-29 16:50:03 +00001493 if (ThisDecl)
1494 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001495
Douglas Gregor72b505b2008-12-16 21:30:33 +00001496 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001497 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001498 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001499 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001500 }
1501
John McCall54abf7d2009-11-04 02:18:39 +00001502 DeclaratorInfo.complete(ThisDecl);
1503
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001504 // If we don't have a comma, it is either the end of the list (a ';')
1505 // or an error, bail out.
1506 if (Tok.isNot(tok::comma))
1507 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001509 // Consume the comma.
1510 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001512 // Parse the next declarator.
1513 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001514 BitfieldSize = 0;
1515 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001516 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001518 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001519 if (Tok.is(tok::kw___attribute)) {
1520 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001521 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001522 DeclaratorInfo.AddAttributes(AttrList, Loc);
1523 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001524
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001525 if (Tok.isNot(tok::colon))
1526 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001527 }
1528
Chris Lattnerae50d502010-02-02 00:43:15 +00001529 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1530 // Skip to end of block or statement.
1531 SkipUntil(tok::r_brace, true, true);
1532 // If we stopped at a ';', eat it.
1533 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001534 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001535 }
1536
Douglas Gregor23c94db2010-07-02 17:43:08 +00001537 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00001538 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001539}
1540
1541/// ParseCXXMemberSpecification - Parse the class definition.
1542///
1543/// member-specification:
1544/// member-declaration member-specification[opt]
1545/// access-specifier ':' member-specification[opt]
1546///
1547void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001548 unsigned TagType, Decl *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001549 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001550 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001551 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001552
John McCallf312b1e2010-08-26 23:41:50 +00001553 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1554 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Douglas Gregor26997fd2010-01-16 20:52:59 +00001556 // Determine whether this is a non-nested class. Note that local
1557 // classes are *not* considered to be nested classes.
1558 bool NonNestedClass = true;
1559 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001560 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001561 if (S->isClassScope()) {
1562 // We're inside a class scope, so this is a nested class.
1563 NonNestedClass = false;
1564 break;
1565 }
1566
1567 if ((S->getFlags() & Scope::FnScope)) {
1568 // If we're in a function or function template declared in the
1569 // body of a class, then this is a local class rather than a
1570 // nested class.
1571 const Scope *Parent = S->getParent();
1572 if (Parent->isTemplateParamScope())
1573 Parent = Parent->getParent();
1574 if (Parent->isClassScope())
1575 break;
1576 }
1577 }
1578 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001579
1580 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001581 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001582
Douglas Gregor6569d682009-05-27 23:11:45 +00001583 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001584 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001585
Douglas Gregorddc29e12009-02-06 22:42:48 +00001586 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001587 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001588
1589 if (Tok.is(tok::colon)) {
1590 ParseBaseClause(TagDecl);
1591
1592 if (!Tok.is(tok::l_brace)) {
1593 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00001594
1595 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001596 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001597 return;
1598 }
1599 }
1600
1601 assert(Tok.is(tok::l_brace));
1602
1603 SourceLocation LBraceLoc = ConsumeBrace();
1604
John McCall42a4f662010-05-28 08:11:17 +00001605 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001606 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, LBraceLoc);
John McCallf9368152009-12-20 07:58:13 +00001607
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001608 // C++ 11p3: Members of a class defined with the keyword class are private
1609 // by default. Members of a class defined with the keywords struct or union
1610 // are public by default.
1611 AccessSpecifier CurAS;
1612 if (TagType == DeclSpec::TST_class)
1613 CurAS = AS_private;
1614 else
1615 CurAS = AS_public;
1616
Douglas Gregor07976d22010-06-21 22:31:09 +00001617 SourceLocation RBraceLoc;
1618 if (TagDecl) {
1619 // While we still have something to read, read the member-declarations.
1620 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1621 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Douglas Gregor07976d22010-06-21 22:31:09 +00001623 // Check for extraneous top-level semicolon.
1624 if (Tok.is(tok::semi)) {
1625 Diag(Tok, diag::ext_extra_struct_semi)
1626 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1627 << FixItHint::CreateRemoval(Tok.getLocation());
1628 ConsumeToken();
1629 continue;
1630 }
1631
1632 AccessSpecifier AS = getAccessSpecifierIfPresent();
1633 if (AS != AS_none) {
1634 // Current token is a C++ access specifier.
1635 CurAS = AS;
1636 SourceLocation ASLoc = Tok.getLocation();
1637 ConsumeToken();
1638 if (Tok.is(tok::colon))
1639 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1640 else
1641 Diag(Tok, diag::err_expected_colon);
1642 ConsumeToken();
1643 continue;
1644 }
1645
1646 // FIXME: Make sure we don't have a template here.
1647
1648 // Parse all the comma separated declarators.
1649 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001650 }
1651
Douglas Gregor07976d22010-06-21 22:31:09 +00001652 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1653 } else {
1654 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001655 }
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001657 // If attributes exist after class contents, parse them.
Ted Kremenek1e377652010-02-11 02:19:13 +00001658 llvm::OwningPtr<AttributeList> AttrList;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001659 if (Tok.is(tok::kw___attribute))
Douglas Gregor0b4c9b52010-03-29 14:42:08 +00001660 AttrList.reset(ParseGNUAttributes());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001661
John McCall42a4f662010-05-28 08:11:17 +00001662 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001663 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall42a4f662010-05-28 08:11:17 +00001664 LBraceLoc, RBraceLoc,
1665 AttrList.get());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001666
1667 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1668 // complete within function bodies, default arguments,
1669 // exception-specifications, and constructor ctor-initializers (including
1670 // such things in nested classes).
1671 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001672 // FIXME: Only function bodies and constructor ctor-initializers are
1673 // parsed correctly, fix the rest.
Douglas Gregor07976d22010-06-21 22:31:09 +00001674 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001675 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001676 // are complete and we can parse the delayed portions of method
1677 // declarations and the lexed inline method definitions.
Douglas Gregore0cc0472010-06-16 23:45:56 +00001678 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregor6569d682009-05-27 23:11:45 +00001679 ParseLexedMethodDeclarations(getCurrentClass());
1680 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00001681 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001682 }
1683
John McCall42a4f662010-05-28 08:11:17 +00001684 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001685 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCalldb7bb4a2010-03-17 00:38:33 +00001686
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001687 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001688 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001689 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001690}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001691
1692/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1693/// which explicitly initializes the members or base classes of a
1694/// class (C++ [class.base.init]). For example, the three initializers
1695/// after the ':' in the Derived constructor below:
1696///
1697/// @code
1698/// class Base { };
1699/// class Derived : Base {
1700/// int x;
1701/// float f;
1702/// public:
1703/// Derived(float f) : Base(), x(17), f(f) { }
1704/// };
1705/// @endcode
1706///
Mike Stump1eb44332009-09-09 15:08:12 +00001707/// [C++] ctor-initializer:
1708/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001709///
Mike Stump1eb44332009-09-09 15:08:12 +00001710/// [C++] mem-initializer-list:
1711/// mem-initializer
1712/// mem-initializer , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00001713void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001714 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1715
1716 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001717
John McCallca0408f2010-08-23 06:44:23 +00001718 llvm::SmallVector<CXXBaseOrMemberInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001719 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001720
Douglas Gregor7ad83902008-11-05 04:29:56 +00001721 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00001722 if (Tok.is(tok::code_completion)) {
1723 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
1724 MemInitializers.data(),
1725 MemInitializers.size());
1726 ConsumeCodeCompletionToken();
1727 } else {
1728 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
1729 if (!MemInit.isInvalid())
1730 MemInitializers.push_back(MemInit.get());
1731 else
1732 AnyErrors = true;
1733 }
1734
Douglas Gregor7ad83902008-11-05 04:29:56 +00001735 if (Tok.is(tok::comma))
1736 ConsumeToken();
1737 else if (Tok.is(tok::l_brace))
1738 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00001739 // If the next token looks like a base or member initializer, assume that
1740 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00001741 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
1742 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
1743 Diag(Loc, diag::err_ctor_init_missing_comma)
1744 << FixItHint::CreateInsertion(Loc, ", ");
1745 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001746 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001747 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001748 SkipUntil(tok::l_brace, true, true);
1749 break;
1750 }
1751 } while (true);
1752
Mike Stump1eb44332009-09-09 15:08:12 +00001753 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001754 MemInitializers.data(), MemInitializers.size(),
1755 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001756}
1757
1758/// ParseMemInitializer - Parse a C++ member initializer, which is
1759/// part of a constructor initializer that explicitly initializes one
1760/// member or base class (C++ [class.base.init]). See
1761/// ParseConstructorInitializer for an example.
1762///
1763/// [C++] mem-initializer:
1764/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001765///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001766/// [C++] mem-initializer-id:
1767/// '::'[opt] nested-name-specifier[opt] class-name
1768/// identifier
John McCalld226f652010-08-21 09:40:31 +00001769Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001770 // parse '::'[opt] nested-name-specifier[opt]
1771 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001772 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
1773 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00001774 if (Tok.is(tok::annot_template_id)) {
1775 TemplateIdAnnotation *TemplateId
1776 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001777 if (TemplateId->Kind == TNK_Type_template ||
1778 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanian96174332009-07-01 19:21:19 +00001779 AnnotateTemplateIdTokenAsType(&SS);
1780 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00001781 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001782 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001783 }
1784 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001785 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001786 return true;
1787 }
Mike Stump1eb44332009-09-09 15:08:12 +00001788
Douglas Gregor7ad83902008-11-05 04:29:56 +00001789 // Get the identifier. This may be a member name or a class name,
1790 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001791 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001792 SourceLocation IdLoc = ConsumeToken();
1793
1794 // Parse the '('.
1795 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001796 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001797 return true;
1798 }
1799 SourceLocation LParenLoc = ConsumeParen();
1800
1801 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001802 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001803 CommaLocsTy CommaLocs;
1804 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1805 SkipUntil(tok::r_paren);
1806 return true;
1807 }
1808
1809 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1810
Douglas Gregor23c94db2010-07-02 17:43:08 +00001811 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanian96174332009-07-01 19:21:19 +00001812 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001813 LParenLoc, ArgExprs.take(),
Douglas Gregora1a04782010-09-09 16:33:13 +00001814 ArgExprs.size(), RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001815}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001816
1817/// ParseExceptionSpecification - Parse a C++ exception-specification
1818/// (C++ [except.spec]).
1819///
Douglas Gregora4745612008-12-01 18:00:20 +00001820/// exception-specification:
1821/// 'throw' '(' type-id-list [opt] ')'
1822/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001823///
Douglas Gregora4745612008-12-01 18:00:20 +00001824/// type-id-list:
1825/// type-id
1826/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001827///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001828bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
John McCallb3d87482010-08-24 05:47:05 +00001829 llvm::SmallVectorImpl<ParsedType>
Sebastian Redlef65f062009-05-29 18:02:33 +00001830 &Exceptions,
John McCallb3d87482010-08-24 05:47:05 +00001831 llvm::SmallVectorImpl<SourceRange>
Sebastian Redlef65f062009-05-29 18:02:33 +00001832 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001833 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001834 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001836 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001838 if (!Tok.is(tok::l_paren)) {
1839 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1840 }
1841 SourceLocation LParenLoc = ConsumeParen();
1842
Douglas Gregora4745612008-12-01 18:00:20 +00001843 // Parse throw(...), a Microsoft extension that means "this function
1844 // can throw anything".
1845 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001846 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001847 SourceLocation EllipsisLoc = ConsumeToken();
1848 if (!getLang().Microsoft)
1849 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001850 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001851 return false;
1852 }
1853
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001854 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001855 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001856 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001857 TypeResult Res(ParseTypeName(&Range));
1858 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001859 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001860 Ranges.push_back(Range);
1861 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001862 if (Tok.is(tok::comma))
1863 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001864 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001865 break;
1866 }
1867
Sebastian Redlab197ba2009-02-09 18:23:29 +00001868 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001869 return false;
1870}
Douglas Gregor6569d682009-05-27 23:11:45 +00001871
1872/// \brief We have just started parsing the definition of a new class,
1873/// so push that class onto our stack of classes that is currently
1874/// being parsed.
John McCalld226f652010-08-21 09:40:31 +00001875void Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001876 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001877 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00001878 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
Douglas Gregor6569d682009-05-27 23:11:45 +00001879}
1880
1881/// \brief Deallocate the given parsed class and all of its nested
1882/// classes.
1883void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1884 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1885 DeallocateParsedClasses(Class->NestedClasses[I]);
1886 delete Class;
1887}
1888
1889/// \brief Pop the top class of the stack of classes that are
1890/// currently being parsed.
1891///
1892/// This routine should be called when we have finished parsing the
1893/// definition of a class, but have not yet popped the Scope
1894/// associated with the class's definition.
1895///
1896/// \returns true if the class we've popped is a top-level class,
1897/// false otherwise.
1898void Parser::PopParsingClass() {
1899 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001900
Douglas Gregor6569d682009-05-27 23:11:45 +00001901 ParsingClass *Victim = ClassStack.top();
1902 ClassStack.pop();
1903 if (Victim->TopLevelClass) {
1904 // Deallocate all of the nested classes of this class,
1905 // recursively: we don't need to keep any of this information.
1906 DeallocateParsedClasses(Victim);
1907 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001908 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001909 assert(!ClassStack.empty() && "Missing top-level class?");
1910
1911 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1912 Victim->NestedClasses.empty()) {
1913 // The victim is a nested class, but we will not need to perform
1914 // any processing after the definition of this class since it has
1915 // no members whose handling was delayed. Therefore, we can just
1916 // remove this nested class.
1917 delete Victim;
1918 return;
1919 }
1920
1921 // This nested class has some members that will need to be processed
1922 // after the top-level class is completely defined. Therefore, add
1923 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001924 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregor6569d682009-05-27 23:11:45 +00001925 ClassStack.top()->NestedClasses.push_back(Victim);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001926 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00001927}
Sean Huntbbd37c62009-11-21 08:43:09 +00001928
1929/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1930/// parses standard attributes.
1931///
1932/// [C++0x] attribute-specifier:
1933/// '[' '[' attribute-list ']' ']'
1934///
1935/// [C++0x] attribute-list:
1936/// attribute[opt]
1937/// attribute-list ',' attribute[opt]
1938///
1939/// [C++0x] attribute:
1940/// attribute-token attribute-argument-clause[opt]
1941///
1942/// [C++0x] attribute-token:
1943/// identifier
1944/// attribute-scoped-token
1945///
1946/// [C++0x] attribute-scoped-token:
1947/// attribute-namespace '::' identifier
1948///
1949/// [C++0x] attribute-namespace:
1950/// identifier
1951///
1952/// [C++0x] attribute-argument-clause:
1953/// '(' balanced-token-seq ')'
1954///
1955/// [C++0x] balanced-token-seq:
1956/// balanced-token
1957/// balanced-token-seq balanced-token
1958///
1959/// [C++0x] balanced-token:
1960/// '(' balanced-token-seq ')'
1961/// '[' balanced-token-seq ']'
1962/// '{' balanced-token-seq '}'
1963/// any token but '(', ')', '[', ']', '{', or '}'
1964CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1965 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1966 && "Not a C++0x attribute list");
1967
1968 SourceLocation StartLoc = Tok.getLocation(), Loc;
1969 AttributeList *CurrAttr = 0;
1970
1971 ConsumeBracket();
1972 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001973
Sean Huntbbd37c62009-11-21 08:43:09 +00001974 if (Tok.is(tok::comma)) {
1975 Diag(Tok.getLocation(), diag::err_expected_ident);
1976 ConsumeToken();
1977 }
1978
1979 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1980 // attribute not present
1981 if (Tok.is(tok::comma)) {
1982 ConsumeToken();
1983 continue;
1984 }
1985
1986 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1987 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001988
Sean Huntbbd37c62009-11-21 08:43:09 +00001989 // scoped attribute
1990 if (Tok.is(tok::coloncolon)) {
1991 ConsumeToken();
1992
1993 if (!Tok.is(tok::identifier)) {
1994 Diag(Tok.getLocation(), diag::err_expected_ident);
1995 SkipUntil(tok::r_square, tok::comma, true, true);
1996 continue;
1997 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001998
Sean Huntbbd37c62009-11-21 08:43:09 +00001999 ScopeName = AttrName;
2000 ScopeLoc = AttrLoc;
2001
2002 AttrName = Tok.getIdentifierInfo();
2003 AttrLoc = ConsumeToken();
2004 }
2005
2006 bool AttrParsed = false;
2007 // No scoped names are supported; ideally we could put all non-standard
2008 // attributes into namespaces.
2009 if (!ScopeName) {
2010 switch(AttributeList::getKind(AttrName))
2011 {
2012 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00002013 case AttributeList::AT_base_check:
2014 case AttributeList::AT_carries_dependency:
Sean Huntbbd37c62009-11-21 08:43:09 +00002015 case AttributeList::AT_final:
Sean Hunt7725e672009-11-25 04:20:27 +00002016 case AttributeList::AT_hiding:
2017 case AttributeList::AT_noreturn:
2018 case AttributeList::AT_override: {
Sean Huntbbd37c62009-11-21 08:43:09 +00002019 if (Tok.is(tok::l_paren)) {
2020 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
2021 << AttrName->getName();
2022 break;
2023 }
2024
2025 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
2026 SourceLocation(), 0, 0, CurrAttr, false,
2027 true);
2028 AttrParsed = true;
2029 break;
2030 }
2031
2032 // One argument; must be a type-id or assignment-expression
2033 case AttributeList::AT_aligned: {
2034 if (Tok.isNot(tok::l_paren)) {
2035 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
2036 << AttrName->getName();
2037 break;
2038 }
2039 SourceLocation ParamLoc = ConsumeParen();
2040
John McCall60d7b3a2010-08-24 06:29:42 +00002041 ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002042
2043 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2044
2045 ExprVector ArgExprs(Actions);
2046 ArgExprs.push_back(ArgExpr.release());
2047 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
2048 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
2049 false, true);
2050
2051 AttrParsed = true;
2052 break;
2053 }
2054
2055 // Silence warnings
2056 default: break;
2057 }
2058 }
2059
2060 // Skip the entire parameter clause, if any
2061 if (!AttrParsed && Tok.is(tok::l_paren)) {
2062 ConsumeParen();
2063 // SkipUntil maintains the balancedness of tokens.
2064 SkipUntil(tok::r_paren, false);
2065 }
2066 }
2067
2068 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2069 SkipUntil(tok::r_square, false);
2070 Loc = Tok.getLocation();
2071 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2072 SkipUntil(tok::r_square, false);
2073
2074 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
2075 return Attr;
2076}
2077
2078/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2079/// attribute.
2080///
2081/// FIXME: Simply returns an alignof() expression if the argument is a
2082/// type. Ideally, the type should be propagated directly into Sema.
2083///
2084/// [C++0x] 'align' '(' type-id ')'
2085/// [C++0x] 'align' '(' assignment-expression ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002086ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002087 if (isTypeIdInParens()) {
John McCallf312b1e2010-08-26 23:41:50 +00002088 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sean Huntbbd37c62009-11-21 08:43:09 +00002089 SourceLocation TypeLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002090 ParsedType Ty = ParseTypeName().get();
Sean Huntbbd37c62009-11-21 08:43:09 +00002091 SourceRange TypeRange(Start, Tok.getLocation());
John McCallb3d87482010-08-24 05:47:05 +00002092 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true,
2093 Ty.getAsOpaquePtr(), TypeRange);
Sean Huntbbd37c62009-11-21 08:43:09 +00002094 } else
2095 return ParseConstantExpression();
2096}