blob: 26d460c6ffbf593e93a336bfe572939b99405e0c [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
Chris Lattner51448322009-03-29 14:02:43 +0000106 // Enter a scope for the namespace.
107 ParseScope NamespaceScope(this, Scope::DeclScope);
108
John McCalld226f652010-08-21 09:40:31 +0000109 Decl *NamespcDecl =
Sebastian Redld078e642010-08-27 23:12:46 +0000110 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, IdentLoc, Ident,
111 LBrace, AttrList.get());
Chris Lattner51448322009-03-29 14:02:43 +0000112
John McCallf312b1e2010-08-26 23:41:50 +0000113 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
114 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Sean Huntbbd37c62009-11-21 08:43:09 +0000116 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
117 CXX0XAttributeList Attr;
118 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
119 Attr = ParseCXX0XAttributes();
120 ParseExternalDeclaration(Attr);
121 }
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Chris Lattner51448322009-03-29 14:02:43 +0000123 // Leave the namespace scope.
124 NamespaceScope.Exit();
125
Chris Lattner97144fc2009-04-02 04:16:50 +0000126 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
127 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000128
Chris Lattner97144fc2009-04-02 04:16:50 +0000129 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000130 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000131}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000132
Anders Carlssonf67606a2009-03-28 04:07:16 +0000133/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
134/// alias definition.
135///
John McCalld226f652010-08-21 09:40:31 +0000136Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000137 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000138 IdentifierInfo *Alias,
139 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000140 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Anders Carlssonf67606a2009-03-28 04:07:16 +0000142 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000144 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000145 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000146 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000147 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000148
Anders Carlssonf67606a2009-03-28 04:07:16 +0000149 CXXScopeSpec SS;
150 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000151 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000152
153 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
154 Diag(Tok, diag::err_expected_namespace_name);
155 // Skip to end of the definition and eat the ';'.
156 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000157 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000158 }
159
160 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000161 IdentifierInfo *Ident = Tok.getIdentifierInfo();
162 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Anders Carlssonf67606a2009-03-28 04:07:16 +0000164 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000165 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000166 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
167 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Douglas Gregor23c94db2010-07-02 17:43:08 +0000169 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000170 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000171}
172
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000173/// ParseLinkage - We know that the current token is a string_literal
174/// and just before that, that extern was seen.
175///
176/// linkage-specification: [C++ 7.5p2: dcl.link]
177/// 'extern' string-literal '{' declaration-seq[opt] '}'
178/// 'extern' string-literal declaration
179///
John McCalld226f652010-08-21 09:40:31 +0000180Decl *Parser::ParseLinkage(ParsingDeclSpec &DS,
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000181 unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000182 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000183 llvm::SmallString<8> LangBuffer;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000184 // LangBuffer is guaranteed to be big enough.
Douglas Gregor453091c2010-03-16 22:30:13 +0000185 bool Invalid = false;
186 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
187 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000188 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000189
190 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000191
Douglas Gregor074149e2009-01-05 19:45:36 +0000192 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000193 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000194 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Douglas Gregor074149e2009-01-05 19:45:36 +0000195 /*FIXME: */SourceLocation(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000196 Loc, Lang,
Mike Stump1eb44332009-09-09 15:08:12 +0000197 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000198 : SourceLocation());
199
Sean Huntbbd37c62009-11-21 08:43:09 +0000200 CXX0XAttributeList Attr;
201 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
202 Attr = ParseCXX0XAttributes();
203 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000204
Douglas Gregor074149e2009-01-05 19:45:36 +0000205 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000206 DS.setExternInLinkageSpec(true);
Douglas Gregor09a63c92010-08-24 14:14:45 +0000207 ParseExternalDeclaration(Attr, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000208 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000209 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000210 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000211
Douglas Gregor63a01132010-02-07 08:38:28 +0000212 DS.abort();
213
Sean Huntbbd37c62009-11-21 08:43:09 +0000214 if (Attr.HasAttr)
215 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
216 << Attr.Range;
217
Douglas Gregorf44515a2008-12-16 22:23:02 +0000218 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000219 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000220 CXX0XAttributeList Attr;
221 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
222 Attr = ParseCXX0XAttributes();
223 ParseExternalDeclaration(Attr);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000224 }
225
Douglas Gregorf44515a2008-12-16 22:23:02 +0000226 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000227 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000228}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000229
Douglas Gregorf780abc2008-12-30 03:27:21 +0000230/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
231/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000232Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000233 SourceLocation &DeclEnd,
234 CXX0XAttributeList Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000235 assert(Tok.is(tok::kw_using) && "Not using token");
236
237 // Eat 'using'.
238 SourceLocation UsingLoc = ConsumeToken();
239
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000240 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000241 Actions.CodeCompleteUsing(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000242 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000243 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000244
Chris Lattner2f274772009-01-06 06:55:51 +0000245 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000246 // Next token after 'using' is 'namespace' so it must be using-directive
Sean Huntbbd37c62009-11-21 08:43:09 +0000247 return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList);
248
249 if (Attr.HasAttr)
250 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
251 << Attr.Range;
Chris Lattner2f274772009-01-06 06:55:51 +0000252
253 // Otherwise, it must be using-declaration.
Sean Huntbbd37c62009-11-21 08:43:09 +0000254 // Ignore illegal attributes (the caller should already have issued an error.
Chris Lattner97144fc2009-04-02 04:16:50 +0000255 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000256}
257
258/// ParseUsingDirective - Parse C++ using-directive, assumes
259/// that current token is 'namespace' and 'using' was already parsed.
260///
261/// using-directive: [C++ 7.3.p4: namespace.udir]
262/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
263/// namespace-name ;
264/// [GNU] using-directive:
265/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
266/// namespace-name attributes[opt] ;
267///
John McCalld226f652010-08-21 09:40:31 +0000268Decl *Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000269 SourceLocation UsingLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000270 SourceLocation &DeclEnd,
271 AttributeList *Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000272 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
273
274 // Eat 'namespace'.
275 SourceLocation NamespcLoc = ConsumeToken();
276
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000277 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000278 Actions.CodeCompleteUsingDirective(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000279 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000280 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000281
Douglas Gregorf780abc2008-12-30 03:27:21 +0000282 CXXScopeSpec SS;
283 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000284 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000285
Douglas Gregorf780abc2008-12-30 03:27:21 +0000286 IdentifierInfo *NamespcName = 0;
287 SourceLocation IdentLoc = SourceLocation();
288
289 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000290 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000291 Diag(Tok, diag::err_expected_namespace_name);
292 // If there was invalid namespace name, skip to end of decl, and eat ';'.
293 SkipUntil(tok::semi);
294 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000295 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000296 }
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Chris Lattner823c44e2009-01-06 07:27:21 +0000298 // Parse identifier.
299 NamespcName = Tok.getIdentifierInfo();
300 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Chris Lattner823c44e2009-01-06 07:27:21 +0000302 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000303 bool GNUAttr = false;
304 if (Tok.is(tok::kw___attribute)) {
305 GNUAttr = true;
306 Attr = addAttributeLists(Attr, ParseGNUAttributes());
307 }
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Chris Lattner823c44e2009-01-06 07:27:21 +0000309 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000310 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000311 ExpectAndConsume(tok::semi,
Sean Huntbbd37c62009-11-21 08:43:09 +0000312 GNUAttr ? diag::err_expected_semi_after_attribute_list :
Chris Lattner6869d8e2009-06-14 00:07:48 +0000313 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000314
Douglas Gregor23c94db2010-07-02 17:43:08 +0000315 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000316 IdentLoc, NamespcName, Attr);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000317}
318
319/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
320/// 'using' was already seen.
321///
322/// using-declaration: [C++ 7.3.p3: namespace.udecl]
323/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000324/// unqualified-id
325/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000326///
John McCalld226f652010-08-21 09:40:31 +0000327Decl *Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000328 SourceLocation UsingLoc,
Anders Carlsson595adc12009-08-29 19:54:19 +0000329 SourceLocation &DeclEnd,
330 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000331 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000332 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000333 bool IsTypeName;
334
335 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000336 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000337 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000338 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000339 ConsumeToken();
340 IsTypeName = true;
341 }
342 else
343 IsTypeName = false;
344
345 // Parse nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000346 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000347
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000348 // Check nested-name specifier.
349 if (SS.isInvalid()) {
350 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000351 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000352 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000353
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000354 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000355 // destructor names and allow the action module to diagnose any semantic
356 // errors.
357 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000358 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000359 /*EnteringContext=*/false,
360 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000361 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000362 ParsedType(),
Douglas Gregor12c118a2009-11-04 16:30:06 +0000363 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000364 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000365 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000366 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000367
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000368 // Parse (optional) attributes (most likely GNU strong-using extension).
Ted Kremenek1e377652010-02-11 02:19:13 +0000369 llvm::OwningPtr<AttributeList> AttrList;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000370 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +0000371 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000373 // Eat ';'.
374 DeclEnd = Tok.getLocation();
375 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000376 AttrList ? "attributes list" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000377 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000378
Douglas Gregor23c94db2010-07-02 17:43:08 +0000379 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS, Name,
Ted Kremenek1e377652010-02-11 02:19:13 +0000380 AttrList.get(), IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000381}
382
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000383/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
384///
385/// static_assert-declaration:
386/// static_assert ( constant-expression , string-literal ) ;
387///
John McCalld226f652010-08-21 09:40:31 +0000388Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000389 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
390 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000392 if (Tok.isNot(tok::l_paren)) {
393 Diag(Tok, diag::err_expected_lparen);
John McCalld226f652010-08-21 09:40:31 +0000394 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000395 }
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000397 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000398
John McCall60d7b3a2010-08-24 06:29:42 +0000399 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000400 if (AssertExpr.isInvalid()) {
401 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000402 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000403 }
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Anders Carlssonad5f9602009-03-13 23:29:20 +0000405 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000406 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000407
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000408 if (Tok.isNot(tok::string_literal)) {
409 Diag(Tok, diag::err_expected_string_literal);
410 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000411 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000412 }
Mike Stump1eb44332009-09-09 15:08:12 +0000413
John McCall60d7b3a2010-08-24 06:29:42 +0000414 ExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000415 if (AssertMessage.isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000416 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000417
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000418 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Chris Lattner97144fc2009-04-02 04:16:50 +0000420 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000421 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
422
John McCall9ae2f072010-08-23 23:25:46 +0000423 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
424 AssertExpr.take(),
425 AssertMessage.take());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000426}
427
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000428/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
429///
430/// 'decltype' ( expression )
431///
432void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
433 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
434
435 SourceLocation StartLoc = ConsumeToken();
436 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000437
438 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000439 "decltype")) {
440 SkipUntil(tok::r_paren);
441 return;
442 }
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000444 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000446 // C++0x [dcl.type.simple]p4:
447 // The operand of the decltype specifier is an unevaluated operand.
448 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000449 Sema::Unevaluated);
John McCall60d7b3a2010-08-24 06:29:42 +0000450 ExprResult Result = ParseExpression();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000451 if (Result.isInvalid()) {
452 SkipUntil(tok::r_paren);
453 return;
454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000456 // Match the ')'
457 SourceLocation RParenLoc;
458 if (Tok.is(tok::r_paren))
459 RParenLoc = ConsumeParen();
460 else
461 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000463 if (RParenLoc.isInvalid())
464 return;
465
466 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000467 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000468 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000469 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000470 DiagID, Result.release()))
471 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000472}
473
Douglas Gregor42a552f2008-11-05 20:51:48 +0000474/// ParseClassName - Parse a C++ class-name, which names a class. Note
475/// that we only check that the result names a type; semantic analysis
476/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000477/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000478/// found.
479///
480/// class-name: [C++ 9.1]
481/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000482/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000483///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000484Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000485 CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000486 // Check whether we have a template-id that names a type.
487 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000488 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000489 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000490 if (TemplateId->Kind == TNK_Type_template ||
491 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000492 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000493
494 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000495 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000496 EndLocation = Tok.getAnnotationEndLoc();
497 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000498
499 if (Type)
500 return Type;
501 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000502 }
503
504 // Fall through to produce an error below.
505 }
506
Douglas Gregor42a552f2008-11-05 20:51:48 +0000507 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000508 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000509 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000510 }
511
Douglas Gregor84d0a192010-01-12 21:28:44 +0000512 IdentifierInfo *Id = Tok.getIdentifierInfo();
513 SourceLocation IdLoc = ConsumeToken();
514
515 if (Tok.is(tok::less)) {
516 // It looks the user intended to write a template-id here, but the
517 // template-name was wrong. Try to fix that.
518 TemplateNameKind TNK = TNK_Type_template;
519 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000520 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor84d0a192010-01-12 21:28:44 +0000521 SS, Template, TNK)) {
522 Diag(IdLoc, diag::err_unknown_template_name)
523 << Id;
524 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000525
Douglas Gregor84d0a192010-01-12 21:28:44 +0000526 if (!Template)
527 return true;
528
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000529 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000530 UnqualifiedId TemplateName;
531 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000532
Douglas Gregor84d0a192010-01-12 21:28:44 +0000533 // Parse the full template-id, then turn it into a type.
534 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
535 SourceLocation(), true))
536 return true;
537 if (TNK == TNK_Dependent_template_name)
538 AnnotateTemplateIdTokenAsType(SS);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000539
Douglas Gregor84d0a192010-01-12 21:28:44 +0000540 // If we didn't end up with a typename token, there's nothing more we
541 // can do.
542 if (Tok.isNot(tok::annot_typename))
543 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000544
Douglas Gregor84d0a192010-01-12 21:28:44 +0000545 // Retrieve the type from the annotation token, consume that token, and
546 // return.
547 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000548 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000549 ConsumeToken();
550 return Type;
551 }
552
Douglas Gregor42a552f2008-11-05 20:51:48 +0000553 // We have an identifier; check whether it is actually a type.
John McCallb3d87482010-08-24 05:47:05 +0000554 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), SS, true);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000555 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000556 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000557 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000558 }
559
560 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000561 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000562
563 // Fake up a Declarator to use with ActOnTypeName.
564 DeclSpec DS;
565 DS.SetRangeStart(IdLoc);
566 DS.SetRangeEnd(EndLocation);
567 DS.getTypeSpecScope() = *SS;
568
569 const char *PrevSpec = 0;
570 unsigned DiagID;
571 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
572
573 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
574 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000575}
576
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000577/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
578/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
579/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000580/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000581///
582/// class-specifier: [C++ class]
583/// class-head '{' member-specification[opt] '}'
584/// class-head '{' member-specification[opt] '}' attributes[opt]
585/// class-head:
586/// class-key identifier[opt] base-clause[opt]
587/// class-key nested-name-specifier identifier base-clause[opt]
588/// class-key nested-name-specifier[opt] simple-template-id
589/// base-clause[opt]
590/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000591/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000592/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000593/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000594/// simple-template-id base-clause[opt]
595/// class-key:
596/// 'class'
597/// 'struct'
598/// 'union'
599///
600/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000601/// class-key ::[opt] nested-name-specifier[opt] identifier
602/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
603/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000604///
605/// Note that the C++ class-specifier and elaborated-type-specifier,
606/// together, subsume the C99 struct-or-union-specifier:
607///
608/// struct-or-union-specifier: [C99 6.7.2.1]
609/// struct-or-union identifier[opt] '{' struct-contents '}'
610/// struct-or-union identifier
611/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
612/// '}' attributes[opt]
613/// [GNU] struct-or-union attributes[opt] identifier
614/// struct-or-union:
615/// 'struct'
616/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000617void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
618 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000619 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redld9bafa72010-02-03 21:21:43 +0000620 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000621 DeclSpec::TST TagType;
622 if (TagTokKind == tok::kw_struct)
623 TagType = DeclSpec::TST_struct;
624 else if (TagTokKind == tok::kw_class)
625 TagType = DeclSpec::TST_class;
626 else {
627 assert(TagTokKind == tok::kw_union && "Not a class specifier");
628 TagType = DeclSpec::TST_union;
629 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000630
Douglas Gregor374929f2009-09-18 15:37:17 +0000631 if (Tok.is(tok::code_completion)) {
632 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000633 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregordc845342010-05-25 05:58:43 +0000634 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +0000635 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000636
Chandler Carruth926c4b42010-06-28 08:39:25 +0000637 // C++03 [temp.explicit] 14.7.2/8:
638 // The usual access checking rules do not apply to names used to specify
639 // explicit instantiations.
640 //
641 // As an extension we do not perform access checking on the names used to
642 // specify explicit specializations either. This is important to allow
643 // specializing traits classes for private types.
644 bool SuppressingAccessChecks = false;
645 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
646 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
647 Actions.ActOnStartSuppressingAccessChecks();
648 SuppressingAccessChecks = true;
649 }
650
Sean Huntbbd37c62009-11-21 08:43:09 +0000651 AttributeList *AttrList = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000652 // If attributes exist after tag, parse them.
653 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000654 AttrList = ParseGNUAttributes();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000655
Steve Narofff59e17e2008-12-24 20:59:21 +0000656 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +0000657 while (Tok.is(tok::kw___declspec))
Sean Huntbbd37c62009-11-21 08:43:09 +0000658 AttrList = ParseMicrosoftDeclSpec(AttrList);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000659
Sean Huntbbd37c62009-11-21 08:43:09 +0000660 // If C++0x attributes exist here, parse them.
661 // FIXME: Are we consistent with the ordering of parsing of different
662 // styles of attributes?
663 if (isCXX0XAttributeSpecifier())
664 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Douglas Gregorb117a602009-09-04 05:53:02 +0000666 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
667 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
668 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000669 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000670 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
671 // properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000672 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000673 Tok.setKind(tok::identifier);
674 }
675
676 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
677 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
678 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000679 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000680 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
681 // properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000682 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000683 Tok.setKind(tok::identifier);
684 }
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000686 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000687 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000688 if (getLang().CPlusPlus) {
689 // "FOO : BAR" is not a potential typo for "FOO::BAR".
690 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000691
John McCallb3d87482010-08-24 05:47:05 +0000692 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true))
John McCall207014e2010-07-30 06:26:29 +0000693 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +0000694 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +0000695 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
696 Diag(Tok, diag::err_expected_ident);
697 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000698
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000699 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
700
Douglas Gregorcc636682009-02-17 23:15:12 +0000701 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000702 IdentifierInfo *Name = 0;
703 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000704 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000705 if (Tok.is(tok::identifier)) {
706 Name = Tok.getIdentifierInfo();
707 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000708
Douglas Gregor5ee37342010-05-30 22:30:21 +0000709 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000710 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000711 // Eat the template argument list and try to continue parsing this as
712 // a class (or template thereof).
713 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000714 SourceLocation LAngleLoc, RAngleLoc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000715 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000716 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000717 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000718 // We couldn't parse the template argument list at all, so don't
719 // try to give any location information for the list.
720 LAngleLoc = RAngleLoc = SourceLocation();
721 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000722
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000723 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000724 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000725 << (TagType == DeclSpec::TST_class? 0
726 : TagType == DeclSpec::TST_struct? 1
727 : 2)
728 << Name
729 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000730
731 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000732 // we've removed its template argument list.
733 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
734 if (TemplateParams && TemplateParams->size() > 1) {
735 TemplateParams->pop_back();
736 } else {
737 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000738 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000739 = ParsedTemplateInfo::NonTemplate;
740 }
741 } else if (TemplateInfo.Kind
742 == ParsedTemplateInfo::ExplicitInstantiation) {
743 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000744 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000745 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000746 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000747 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000748 = SourceLocation();
749 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
750 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000751 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000752 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000753 } else if (Tok.is(tok::annot_template_id)) {
754 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
755 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000756
Douglas Gregorc45c2322009-03-31 00:43:58 +0000757 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000758 // The template-name in the simple-template-id refers to
759 // something other than a class template. Give an appropriate
760 // error message and skip to the ';'.
761 SourceRange Range(NameLoc);
762 if (SS.isNotEmpty())
763 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000764
Douglas Gregor39a8de12009-02-25 19:37:18 +0000765 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
766 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Douglas Gregor39a8de12009-02-25 19:37:18 +0000768 DS.SetTypeSpecError();
769 SkipUntil(tok::semi, false, true);
770 TemplateId->Destroy();
Chandler Carruth926c4b42010-06-28 08:39:25 +0000771 if (SuppressingAccessChecks)
772 Actions.ActOnStopSuppressingAccessChecks();
773
Douglas Gregor39a8de12009-02-25 19:37:18 +0000774 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000775 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000776 }
777
Chandler Carruth926c4b42010-06-28 08:39:25 +0000778 // As soon as we're finished parsing the class's template-id, turn access
779 // checking back on.
780 if (SuppressingAccessChecks)
781 Actions.ActOnStopSuppressingAccessChecks();
782
John McCall67d1a672009-08-06 02:15:43 +0000783 // There are four options here. If we have 'struct foo;', then this
784 // is either a forward declaration or a friend declaration, which
785 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000786 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000787 // something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000788 // However, in some contexts, things look like declarations but are just
789 // references, e.g.
790 // new struct s;
791 // or
792 // &T::operator struct s;
793 // For these, SuppressDeclarations is true.
John McCallf312b1e2010-08-26 23:41:50 +0000794 Sema::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000795 if (SuppressDeclarations)
John McCallf312b1e2010-08-26 23:41:50 +0000796 TUK = Sema::TUK_Reference;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000797 else if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))){
Douglas Gregord85bea22009-09-26 06:47:28 +0000798 if (DS.isFriendSpecified()) {
799 // C++ [class.friend]p2:
800 // A class shall not be defined in a friend declaration.
801 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
802 << SourceRange(DS.getFriendSpecLoc());
803
804 // Skip everything up to the semicolon, so that this looks like a proper
805 // friend class (or template thereof) declaration.
806 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +0000807 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +0000808 } else {
809 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +0000810 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +0000811 }
812 } else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +0000813 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000814 else
John McCallf312b1e2010-08-26 23:41:50 +0000815 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000816
John McCall207014e2010-07-30 06:26:29 +0000817 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +0000818 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +0000819 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
820 // We have a declaration or reference to an anonymous class.
821 Diag(StartLoc, diag::err_anon_type_definition)
822 << DeclSpec::getSpecifierName(TagType);
823 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000824
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000825 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000826
827 if (TemplateId)
828 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000829 return;
830 }
831
Douglas Gregorddc29e12009-02-06 22:42:48 +0000832 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +0000833 DeclResult TagOrTempResult = true; // invalid
834 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000835
Douglas Gregor402abb52009-05-28 23:31:59 +0000836 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000837 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000838 // Explicit specialization, class template partial specialization,
839 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000840 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000841 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000842 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000843 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000844 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000845 // This is an explicit instantiation of a class template.
846 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000847 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000848 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000849 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000850 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000851 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000852 SS,
John McCall2b5289b2010-08-23 07:28:44 +0000853 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000854 TemplateId->TemplateNameLoc,
855 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000856 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000857 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000858 AttrList);
John McCall74256f52010-04-14 00:24:33 +0000859
860 // Friend template-ids are treated as references unless
861 // they have template headers, in which case they're ill-formed
862 // (FIXME: "template <class T> friend class A<T>::B<int>;").
863 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +0000864 } else if (TUK == Sema::TUK_Reference ||
865 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +0000866 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
John McCallc4e70192009-09-11 04:59:25 +0000867 TypeResult
John McCall2b5289b2010-08-23 07:28:44 +0000868 = Actions.ActOnTemplateIdType(TemplateId->Template,
John McCall6b2becf2009-09-08 17:47:29 +0000869 TemplateId->TemplateNameLoc,
870 TemplateId->LAngleLoc,
871 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000872 TemplateId->RAngleLoc);
873
John McCallc4e70192009-09-11 04:59:25 +0000874 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
875 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000876 } else {
877 // This is an explicit specialization or a class template
878 // partial specialization.
879 TemplateParameterLists FakedParamLists;
880
881 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
882 // This looks like an explicit instantiation, because we have
883 // something like
884 //
885 // template class Foo<X>
886 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000887 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000888 // meant to be an explicit specialization, but the user forgot
889 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +0000890 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000891
Mike Stump1eb44332009-09-09 15:08:12 +0000892 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000893 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000894 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000895 diag::err_explicit_instantiation_with_definition)
896 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +0000897 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000898
899 // Create a fake template parameter list that contains only
900 // "template<>", so that we treat this construct as a class
901 // template specialization.
902 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000903 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000904 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000905 LAngleLoc,
906 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000907 LAngleLoc));
908 TemplateParams = &FakedParamLists;
909 }
910
911 // Build the class template specialization.
912 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000913 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000914 StartLoc, SS,
John McCall2b5289b2010-08-23 07:28:44 +0000915 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000916 TemplateId->TemplateNameLoc,
917 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000918 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000919 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000920 AttrList,
John McCallf312b1e2010-08-26 23:41:50 +0000921 MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000922 TemplateParams? &(*TemplateParams)[0] : 0,
923 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000924 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000925 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000926 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000927 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000928 // Explicit instantiation of a member of a class template
929 // specialization, e.g.,
930 //
931 // template struct Outer<int>::Inner;
932 //
933 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000934 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000935 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000936 TemplateInfo.TemplateLoc,
937 TagType, StartLoc, SS, Name,
Sean Huntbbd37c62009-11-21 08:43:09 +0000938 NameLoc, AttrList);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000939 } else {
940 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000941 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000942 // FIXME: Diagnose this particular error.
943 }
944
John McCallc4e70192009-09-11 04:59:25 +0000945 bool IsDependent = false;
946
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000947 // Declaration or definition of a class type
Douglas Gregor23c94db2010-07-02 17:43:08 +0000948 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000949 Name, NameLoc, AttrList, AS,
John McCallf312b1e2010-08-26 23:41:50 +0000950 MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000951 TemplateParams? &(*TemplateParams)[0] : 0,
952 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000953 Owned, IsDependent);
954
955 // If ActOnTag said the type was dependent, try again with the
956 // less common call.
957 if (IsDependent)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000958 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000959 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000960 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000961
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000962 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +0000963 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +0000964 assert(Tok.is(tok::l_brace) ||
965 (getLang().CPlusPlus && Tok.is(tok::colon)));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000966 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000967 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000968 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000969 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000970 }
971
John McCallb3d87482010-08-24 05:47:05 +0000972 // FIXME: The DeclSpec should keep the locations of both the keyword and the
973 // name (if there is one).
974 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
975
976 const char *PrevSpec = 0;
977 unsigned DiagID;
978 bool Result;
John McCallc4e70192009-09-11 04:59:25 +0000979 if (!TypeResult.isInvalid()) {
John McCallb3d87482010-08-24 05:47:05 +0000980 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc,
981 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +0000982 } else if (!TagOrTempResult.isInvalid()) {
John McCallb3d87482010-08-24 05:47:05 +0000983 Result = DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID,
984 TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +0000985 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000986 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000987 return;
988 }
Mike Stump1eb44332009-09-09 15:08:12 +0000989
John McCallb3d87482010-08-24 05:47:05 +0000990 if (Result)
John McCallfec54012009-08-03 20:12:06 +0000991 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000992
Chris Lattner4ed5d912010-02-02 01:23:29 +0000993 // At this point, we've successfully parsed a class-specifier in 'definition'
994 // form (e.g. "struct foo { int x; }". While we could just return here, we're
995 // going to look at what comes after it to improve error recovery. If an
996 // impossible token occurs next, we assume that the programmer forgot a ; at
997 // the end of the declaration and recover that way.
998 //
999 // This switch enumerates the valid "follow" set for definition.
John McCallf312b1e2010-08-26 23:41:50 +00001000 if (TUK == Sema::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001001 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001002 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001003 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001004 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +00001005 case tok::star: // struct foo {...} * P;
1006 case tok::amp: // struct foo {...} & R = ...
1007 case tok::identifier: // struct foo {...} V ;
1008 case tok::r_paren: //(struct foo {...} ) {4}
1009 case tok::annot_cxxscope: // struct foo {...} a:: b;
1010 case tok::annot_typename: // struct foo {...} a ::b;
1011 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +00001012 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +00001013 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001014 ExpectedSemi = false;
1015 break;
1016 // Type qualifiers
1017 case tok::kw_const: // struct foo {...} const x;
1018 case tok::kw_volatile: // struct foo {...} volatile x;
1019 case tok::kw_restrict: // struct foo {...} restrict x;
1020 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +00001021 // Storage-class specifiers
1022 case tok::kw_static: // struct foo {...} static x;
1023 case tok::kw_extern: // struct foo {...} extern x;
1024 case tok::kw_typedef: // struct foo {...} typedef x;
1025 case tok::kw_register: // struct foo {...} register x;
1026 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregor33f99242010-05-17 18:19:56 +00001027 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001028 // As shown above, type qualifiers and storage class specifiers absolutely
1029 // can occur after class specifiers according to the grammar. However,
1030 // almost noone actually writes code like this. If we see one of these,
1031 // it is much more likely that someone missed a semi colon and the
1032 // type/storage class specifier we're seeing is part of the *next*
1033 // intended declaration, as in:
1034 //
1035 // struct foo { ... }
1036 // typedef int X;
1037 //
1038 // We'd really like to emit a missing semicolon error instead of emitting
1039 // an error on the 'int' saying that you can't have two type specifiers in
1040 // the same declaration of X. Because of this, we look ahead past this
1041 // token to see if it's a type specifier. If so, we know the code is
1042 // otherwise invalid, so we can produce the expected semi error.
1043 if (!isKnownToBeTypeSpecifier(NextToken()))
1044 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001045 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001046
1047 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001048 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001049 if (!getLang().CPlusPlus)
1050 ExpectedSemi = false;
1051 break;
1052 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001053
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001054 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001055 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1056 TagType == DeclSpec::TST_class ? "class"
1057 : TagType == DeclSpec::TST_struct? "struct" : "union");
1058 // Push this token back into the preprocessor and change our current token
1059 // to ';' so that the rest of the code recovers as though there were an
1060 // ';' after the definition.
1061 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001062 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001063 }
1064 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001065}
1066
Mike Stump1eb44332009-09-09 15:08:12 +00001067/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001068///
1069/// base-clause : [C++ class.derived]
1070/// ':' base-specifier-list
1071/// base-specifier-list:
1072/// base-specifier '...'[opt]
1073/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001074void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001075 assert(Tok.is(tok::colon) && "Not a base clause");
1076 ConsumeToken();
1077
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001078 // Build up an array of parsed base specifiers.
John McCallca0408f2010-08-23 06:44:23 +00001079 llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001080
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001081 while (true) {
1082 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001083 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001084 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001085 // Skip the rest of this base specifier, up until the comma or
1086 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001087 SkipUntil(tok::comma, tok::l_brace, true, true);
1088 } else {
1089 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001090 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001091 }
1092
1093 // If the next token is a comma, consume it and keep reading
1094 // base-specifiers.
1095 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001097 // Consume the comma.
1098 ConsumeToken();
1099 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001100
1101 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001102 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001103}
1104
1105/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1106/// one entry in the base class list of a class specifier, for example:
1107/// class foo : public bar, virtual private baz {
1108/// 'public bar' and 'virtual private baz' are each base-specifiers.
1109///
1110/// base-specifier: [C++ class.derived]
1111/// ::[opt] nested-name-specifier[opt] class-name
1112/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1113/// class-name
1114/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1115/// class-name
John McCalld226f652010-08-21 09:40:31 +00001116Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001117 bool IsVirtual = false;
1118 SourceLocation StartLoc = Tok.getLocation();
1119
1120 // Parse the 'virtual' keyword.
1121 if (Tok.is(tok::kw_virtual)) {
1122 ConsumeToken();
1123 IsVirtual = true;
1124 }
1125
1126 // Parse an (optional) access specifier.
1127 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001128 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001129 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001131 // Parse the 'virtual' keyword (again!), in case it came after the
1132 // access specifier.
1133 if (Tok.is(tok::kw_virtual)) {
1134 SourceLocation VirtualLoc = ConsumeToken();
1135 if (IsVirtual) {
1136 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001137 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001138 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001139 }
1140
1141 IsVirtual = true;
1142 }
1143
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001144 // Parse optional '::' and optional nested-name-specifier.
1145 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001146 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001147
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001148 // The location of the base class itself.
1149 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001150
1151 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001152 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +00001153 TypeResult BaseType = ParseClassName(EndLocation, &SS);
1154 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001155 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001156
1157 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001158 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001160 // Notify semantic analysis that we have parsed a complete
1161 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001162 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +00001163 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001164}
1165
1166/// getAccessSpecifierIfPresent - Determine whether the next token is
1167/// a C++ access-specifier.
1168///
1169/// access-specifier: [C++ class.derived]
1170/// 'private'
1171/// 'protected'
1172/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001173AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001174 switch (Tok.getKind()) {
1175 default: return AS_none;
1176 case tok::kw_private: return AS_private;
1177 case tok::kw_protected: return AS_protected;
1178 case tok::kw_public: return AS_public;
1179 }
1180}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001181
Eli Friedmand33133c2009-07-22 21:45:50 +00001182void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
John McCalld226f652010-08-21 09:40:31 +00001183 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001184 // We just declared a member function. If this member function
1185 // has any default arguments, we'll need to parse them later.
1186 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001187 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +00001188 = DeclaratorInfo.getTypeObject(0).Fun;
1189 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1190 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1191 if (!LateMethod) {
1192 // Push this method onto the stack of late-parsed method
1193 // declarations.
1194 getCurrentClass().MethodDecls.push_back(
1195 LateParsedMethodDeclaration(ThisDecl));
1196 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001197 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001198
1199 // Add all of the parameters prior to this one (they don't
1200 // have default arguments).
1201 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1202 for (unsigned I = 0; I < ParamIdx; ++I)
1203 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001204 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001205 }
1206
1207 // Add this parameter to the list of parameters (it or may
1208 // not have a default argument).
1209 LateMethod->DefaultArgs.push_back(
1210 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1211 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1212 }
1213 }
1214}
1215
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001216/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1217///
1218/// member-declaration:
1219/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1220/// function-definition ';'[opt]
1221/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1222/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001223/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001224/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001225/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001226///
1227/// member-declarator-list:
1228/// member-declarator
1229/// member-declarator-list ',' member-declarator
1230///
1231/// member-declarator:
1232/// declarator pure-specifier[opt]
1233/// declarator constant-initializer[opt]
1234/// identifier[opt] ':' constant-expression
1235///
Sebastian Redle2b68332009-04-12 17:16:29 +00001236/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001237/// '= 0'
1238///
1239/// constant-initializer:
1240/// '=' constant-expression
1241///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001242void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCallc9068d72010-07-16 08:13:16 +00001243 const ParsedTemplateInfo &TemplateInfo,
1244 ParsingDeclRAIIObject *TemplateDiags) {
John McCall60fa3cf2009-12-11 02:10:03 +00001245 // Access declarations.
1246 if (!TemplateInfo.Kind &&
1247 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001248 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001249 Tok.is(tok::annot_cxxscope)) {
1250 bool isAccessDecl = false;
1251 if (NextToken().is(tok::identifier))
1252 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1253 else
1254 isAccessDecl = NextToken().is(tok::kw_operator);
1255
1256 if (isAccessDecl) {
1257 // Collect the scope specifier token we annotated earlier.
1258 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001259 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
John McCall60fa3cf2009-12-11 02:10:03 +00001260
1261 // Try to parse an unqualified-id.
1262 UnqualifiedId Name;
John McCallb3d87482010-08-24 05:47:05 +00001263 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001264 SkipUntil(tok::semi);
1265 return;
1266 }
1267
1268 // TODO: recover from mistakenly-qualified operator declarations.
1269 if (ExpectAndConsume(tok::semi,
1270 diag::err_expected_semi_after,
1271 "access declaration",
1272 tok::semi))
1273 return;
1274
Douglas Gregor23c94db2010-07-02 17:43:08 +00001275 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001276 false, SourceLocation(),
1277 SS, Name,
1278 /* AttrList */ 0,
1279 /* IsTypeName */ false,
1280 SourceLocation());
1281 return;
1282 }
1283 }
1284
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001285 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001286 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001287 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001288 SourceLocation DeclEnd;
1289 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001290 return;
1291 }
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Chris Lattner682bf922009-03-29 16:50:03 +00001293 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001294 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001295 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001296 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001297 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001298 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001299 return;
1300 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001301
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001302 // Handle: member-declaration ::= '__extension__' member-declaration
1303 if (Tok.is(tok::kw___extension__)) {
1304 // __extension__ silences extension warnings in the subexpression.
1305 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1306 ConsumeToken();
John McCallc9068d72010-07-16 08:13:16 +00001307 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001308 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001309
Chris Lattner4ed5d912010-02-02 01:23:29 +00001310 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1311 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001312 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001313
Sean Huntbbd37c62009-11-21 08:43:09 +00001314 CXX0XAttributeList AttrList;
1315 // Optional C++0x attribute-specifier
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001316 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Sean Huntbbd37c62009-11-21 08:43:09 +00001317 AttrList = ParseCXX0XAttributes();
Sean Huntbbd37c62009-11-21 08:43:09 +00001318
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001319 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001320 // FIXME: Check for template aliases
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001321
Sean Huntbbd37c62009-11-21 08:43:09 +00001322 if (AttrList.HasAttr)
1323 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1324 << AttrList.Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001326 // Eat 'using'.
1327 SourceLocation UsingLoc = ConsumeToken();
1328
1329 if (Tok.is(tok::kw_namespace)) {
1330 Diag(UsingLoc, diag::err_using_namespace_in_class);
1331 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001332 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001333 SourceLocation DeclEnd;
1334 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001335 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001336 }
1337 return;
1338 }
1339
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001340 SourceLocation DSStart = Tok.getLocation();
1341 // decl-specifier-seq:
1342 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001343 ParsingDeclSpec DS(*this, TemplateDiags);
Sean Huntbbd37c62009-11-21 08:43:09 +00001344 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001345 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001346
John McCallf312b1e2010-08-26 23:41:50 +00001347 MultiTemplateParamsArg TemplateParams(Actions,
John McCalldd4a3b02009-09-16 22:47:08 +00001348 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1349 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1350
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001351 if (Tok.is(tok::semi)) {
1352 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001353 Decl *TheDecl =
John McCallc9068d72010-07-16 08:13:16 +00001354 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
1355 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001356 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001357 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001358
John McCall54abf7d2009-11-04 02:18:39 +00001359 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001360
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001361 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001362 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1363 ColonProtectionRAIIObject X(*this);
1364
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001365 // Parse the first declarator.
1366 ParseDeclarator(DeclaratorInfo);
1367 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001368 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001369 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001370 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001371 if (Tok.is(tok::semi))
1372 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001373 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001374 }
1375
John Thompson1b2fc0f2009-11-25 22:58:06 +00001376 // If attributes exist after the declarator, but before an '{', parse them.
1377 if (Tok.is(tok::kw___attribute)) {
1378 SourceLocation Loc;
1379 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1380 DeclaratorInfo.AddAttributes(AttrList, Loc);
1381 }
1382
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001383 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001384 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001385 || (DeclaratorInfo.isFunctionDeclarator() &&
1386 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001387 if (!DeclaratorInfo.isFunctionDeclarator()) {
1388 Diag(Tok, diag::err_func_def_no_params);
1389 ConsumeBrace();
1390 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001391 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001392 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001393
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001394 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1395 Diag(Tok, diag::err_function_declared_typedef);
1396 // This recovery skips the entire function body. It would be nice
1397 // to simply call ParseCXXInlineMethodDef() below, however Sema
1398 // assumes the declarator represents a function, not a typedef.
1399 ConsumeBrace();
1400 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001401 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001402 }
1403
Douglas Gregor37b372b2009-08-20 22:52:58 +00001404 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001405 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001406 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001407 }
1408
1409 // member-declarator-list:
1410 // member-declarator
1411 // member-declarator-list ',' member-declarator
1412
John McCalld226f652010-08-21 09:40:31 +00001413 llvm::SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00001414 ExprResult BitfieldSize;
1415 ExprResult Init;
Sebastian Redle2b68332009-04-12 17:16:29 +00001416 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001417
1418 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001419 // member-declarator:
1420 // declarator pure-specifier[opt]
1421 // declarator constant-initializer[opt]
1422 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001423 if (Tok.is(tok::colon)) {
1424 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001425 BitfieldSize = ParseConstantExpression();
1426 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001427 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001428 }
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001430 // pure-specifier:
1431 // '= 0'
1432 //
1433 // constant-initializer:
1434 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001435 //
1436 // defaulted/deleted function-definition:
1437 // '=' 'default' [TODO]
1438 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001439 if (Tok.is(tok::equal)) {
1440 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001441 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1442 ConsumeToken();
1443 Deleted = true;
1444 } else {
1445 Init = ParseInitializer();
1446 if (Init.isInvalid())
1447 SkipUntil(tok::comma, true, true);
1448 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001449 }
1450
Chris Lattnere6563252010-06-13 05:34:18 +00001451 // If a simple-asm-expr is present, parse it.
1452 if (Tok.is(tok::kw_asm)) {
1453 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001454 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00001455 if (AsmLabel.isInvalid())
1456 SkipUntil(tok::comma, true, true);
1457
1458 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1459 DeclaratorInfo.SetRangeEnd(Loc);
1460 }
1461
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001462 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001463 if (Tok.is(tok::kw___attribute)) {
1464 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001465 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001466 DeclaratorInfo.AddAttributes(AttrList, Loc);
1467 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001468
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001469 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001470 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001471 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001472
John McCalld226f652010-08-21 09:40:31 +00001473 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00001474 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001475 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00001476 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCallbbbcdd92009-09-11 21:02:39 +00001477 /*IsDefinition*/ false,
1478 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001479 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001480 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00001481 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001482 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001483 BitfieldSize.release(),
1484 Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001485 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001486 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001487 }
Chris Lattner682bf922009-03-29 16:50:03 +00001488 if (ThisDecl)
1489 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001490
Douglas Gregor72b505b2008-12-16 21:30:33 +00001491 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001492 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001493 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001494 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001495 }
1496
John McCall54abf7d2009-11-04 02:18:39 +00001497 DeclaratorInfo.complete(ThisDecl);
1498
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001499 // If we don't have a comma, it is either the end of the list (a ';')
1500 // or an error, bail out.
1501 if (Tok.isNot(tok::comma))
1502 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001504 // Consume the comma.
1505 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001507 // Parse the next declarator.
1508 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001509 BitfieldSize = 0;
1510 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001511 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001513 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001514 if (Tok.is(tok::kw___attribute)) {
1515 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001516 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001517 DeclaratorInfo.AddAttributes(AttrList, Loc);
1518 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001519
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001520 if (Tok.isNot(tok::colon))
1521 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001522 }
1523
Chris Lattnerae50d502010-02-02 00:43:15 +00001524 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1525 // Skip to end of block or statement.
1526 SkipUntil(tok::r_brace, true, true);
1527 // If we stopped at a ';', eat it.
1528 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001529 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001530 }
1531
Douglas Gregor23c94db2010-07-02 17:43:08 +00001532 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00001533 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001534}
1535
1536/// ParseCXXMemberSpecification - Parse the class definition.
1537///
1538/// member-specification:
1539/// member-declaration member-specification[opt]
1540/// access-specifier ':' member-specification[opt]
1541///
1542void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001543 unsigned TagType, Decl *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001544 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001545 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001546 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001547
John McCallf312b1e2010-08-26 23:41:50 +00001548 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1549 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Douglas Gregor26997fd2010-01-16 20:52:59 +00001551 // Determine whether this is a non-nested class. Note that local
1552 // classes are *not* considered to be nested classes.
1553 bool NonNestedClass = true;
1554 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001555 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001556 if (S->isClassScope()) {
1557 // We're inside a class scope, so this is a nested class.
1558 NonNestedClass = false;
1559 break;
1560 }
1561
1562 if ((S->getFlags() & Scope::FnScope)) {
1563 // If we're in a function or function template declared in the
1564 // body of a class, then this is a local class rather than a
1565 // nested class.
1566 const Scope *Parent = S->getParent();
1567 if (Parent->isTemplateParamScope())
1568 Parent = Parent->getParent();
1569 if (Parent->isClassScope())
1570 break;
1571 }
1572 }
1573 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001574
1575 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001576 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001577
Douglas Gregor6569d682009-05-27 23:11:45 +00001578 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001579 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001580
Douglas Gregorddc29e12009-02-06 22:42:48 +00001581 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001582 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001583
1584 if (Tok.is(tok::colon)) {
1585 ParseBaseClause(TagDecl);
1586
1587 if (!Tok.is(tok::l_brace)) {
1588 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00001589
1590 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001591 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001592 return;
1593 }
1594 }
1595
1596 assert(Tok.is(tok::l_brace));
1597
1598 SourceLocation LBraceLoc = ConsumeBrace();
1599
John McCall42a4f662010-05-28 08:11:17 +00001600 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001601 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, LBraceLoc);
John McCallf9368152009-12-20 07:58:13 +00001602
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001603 // C++ 11p3: Members of a class defined with the keyword class are private
1604 // by default. Members of a class defined with the keywords struct or union
1605 // are public by default.
1606 AccessSpecifier CurAS;
1607 if (TagType == DeclSpec::TST_class)
1608 CurAS = AS_private;
1609 else
1610 CurAS = AS_public;
1611
Douglas Gregor07976d22010-06-21 22:31:09 +00001612 SourceLocation RBraceLoc;
1613 if (TagDecl) {
1614 // While we still have something to read, read the member-declarations.
1615 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1616 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Douglas Gregor07976d22010-06-21 22:31:09 +00001618 // Check for extraneous top-level semicolon.
1619 if (Tok.is(tok::semi)) {
1620 Diag(Tok, diag::ext_extra_struct_semi)
1621 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1622 << FixItHint::CreateRemoval(Tok.getLocation());
1623 ConsumeToken();
1624 continue;
1625 }
1626
1627 AccessSpecifier AS = getAccessSpecifierIfPresent();
1628 if (AS != AS_none) {
1629 // Current token is a C++ access specifier.
1630 CurAS = AS;
1631 SourceLocation ASLoc = Tok.getLocation();
1632 ConsumeToken();
1633 if (Tok.is(tok::colon))
1634 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1635 else
1636 Diag(Tok, diag::err_expected_colon);
1637 ConsumeToken();
1638 continue;
1639 }
1640
1641 // FIXME: Make sure we don't have a template here.
1642
1643 // Parse all the comma separated declarators.
1644 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001645 }
1646
Douglas Gregor07976d22010-06-21 22:31:09 +00001647 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1648 } else {
1649 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001650 }
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001652 // If attributes exist after class contents, parse them.
Ted Kremenek1e377652010-02-11 02:19:13 +00001653 llvm::OwningPtr<AttributeList> AttrList;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001654 if (Tok.is(tok::kw___attribute))
Douglas Gregor0b4c9b52010-03-29 14:42:08 +00001655 AttrList.reset(ParseGNUAttributes());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001656
John McCall42a4f662010-05-28 08:11:17 +00001657 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001658 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall42a4f662010-05-28 08:11:17 +00001659 LBraceLoc, RBraceLoc,
1660 AttrList.get());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001661
1662 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1663 // complete within function bodies, default arguments,
1664 // exception-specifications, and constructor ctor-initializers (including
1665 // such things in nested classes).
1666 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001667 // FIXME: Only function bodies and constructor ctor-initializers are
1668 // parsed correctly, fix the rest.
Douglas Gregor07976d22010-06-21 22:31:09 +00001669 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001670 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001671 // are complete and we can parse the delayed portions of method
1672 // declarations and the lexed inline method definitions.
Douglas Gregore0cc0472010-06-16 23:45:56 +00001673 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregor6569d682009-05-27 23:11:45 +00001674 ParseLexedMethodDeclarations(getCurrentClass());
1675 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00001676 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001677 }
1678
John McCall42a4f662010-05-28 08:11:17 +00001679 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001680 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCalldb7bb4a2010-03-17 00:38:33 +00001681
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001682 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001683 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001684 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001685}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001686
1687/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1688/// which explicitly initializes the members or base classes of a
1689/// class (C++ [class.base.init]). For example, the three initializers
1690/// after the ':' in the Derived constructor below:
1691///
1692/// @code
1693/// class Base { };
1694/// class Derived : Base {
1695/// int x;
1696/// float f;
1697/// public:
1698/// Derived(float f) : Base(), x(17), f(f) { }
1699/// };
1700/// @endcode
1701///
Mike Stump1eb44332009-09-09 15:08:12 +00001702/// [C++] ctor-initializer:
1703/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001704///
Mike Stump1eb44332009-09-09 15:08:12 +00001705/// [C++] mem-initializer-list:
1706/// mem-initializer
1707/// mem-initializer , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00001708void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001709 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1710
1711 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001712
John McCallca0408f2010-08-23 06:44:23 +00001713 llvm::SmallVector<CXXBaseOrMemberInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001714 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001715
Douglas Gregor7ad83902008-11-05 04:29:56 +00001716 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00001717 if (Tok.is(tok::code_completion)) {
1718 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
1719 MemInitializers.data(),
1720 MemInitializers.size());
1721 ConsumeCodeCompletionToken();
1722 } else {
1723 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
1724 if (!MemInit.isInvalid())
1725 MemInitializers.push_back(MemInit.get());
1726 else
1727 AnyErrors = true;
1728 }
1729
Douglas Gregor7ad83902008-11-05 04:29:56 +00001730 if (Tok.is(tok::comma))
1731 ConsumeToken();
1732 else if (Tok.is(tok::l_brace))
1733 break;
1734 else {
1735 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001736 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001737 SkipUntil(tok::l_brace, true, true);
1738 break;
1739 }
1740 } while (true);
1741
Mike Stump1eb44332009-09-09 15:08:12 +00001742 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001743 MemInitializers.data(), MemInitializers.size(),
1744 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001745}
1746
1747/// ParseMemInitializer - Parse a C++ member initializer, which is
1748/// part of a constructor initializer that explicitly initializes one
1749/// member or base class (C++ [class.base.init]). See
1750/// ParseConstructorInitializer for an example.
1751///
1752/// [C++] mem-initializer:
1753/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001754///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001755/// [C++] mem-initializer-id:
1756/// '::'[opt] nested-name-specifier[opt] class-name
1757/// identifier
John McCalld226f652010-08-21 09:40:31 +00001758Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001759 // parse '::'[opt] nested-name-specifier[opt]
1760 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001761 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
1762 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00001763 if (Tok.is(tok::annot_template_id)) {
1764 TemplateIdAnnotation *TemplateId
1765 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001766 if (TemplateId->Kind == TNK_Type_template ||
1767 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanian96174332009-07-01 19:21:19 +00001768 AnnotateTemplateIdTokenAsType(&SS);
1769 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00001770 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001771 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001772 }
1773 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001774 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001775 return true;
1776 }
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Douglas Gregor7ad83902008-11-05 04:29:56 +00001778 // Get the identifier. This may be a member name or a class name,
1779 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001780 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001781 SourceLocation IdLoc = ConsumeToken();
1782
1783 // Parse the '('.
1784 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001785 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001786 return true;
1787 }
1788 SourceLocation LParenLoc = ConsumeParen();
1789
1790 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001791 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001792 CommaLocsTy CommaLocs;
1793 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1794 SkipUntil(tok::r_paren);
1795 return true;
1796 }
1797
1798 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1799
Douglas Gregor23c94db2010-07-02 17:43:08 +00001800 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanian96174332009-07-01 19:21:19 +00001801 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001802 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001803 ArgExprs.size(), CommaLocs.data(),
1804 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001805}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001806
1807/// ParseExceptionSpecification - Parse a C++ exception-specification
1808/// (C++ [except.spec]).
1809///
Douglas Gregora4745612008-12-01 18:00:20 +00001810/// exception-specification:
1811/// 'throw' '(' type-id-list [opt] ')'
1812/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001813///
Douglas Gregora4745612008-12-01 18:00:20 +00001814/// type-id-list:
1815/// type-id
1816/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001817///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001818bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
John McCallb3d87482010-08-24 05:47:05 +00001819 llvm::SmallVectorImpl<ParsedType>
Sebastian Redlef65f062009-05-29 18:02:33 +00001820 &Exceptions,
John McCallb3d87482010-08-24 05:47:05 +00001821 llvm::SmallVectorImpl<SourceRange>
Sebastian Redlef65f062009-05-29 18:02:33 +00001822 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001823 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001824 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001825
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001826 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001827
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001828 if (!Tok.is(tok::l_paren)) {
1829 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1830 }
1831 SourceLocation LParenLoc = ConsumeParen();
1832
Douglas Gregora4745612008-12-01 18:00:20 +00001833 // Parse throw(...), a Microsoft extension that means "this function
1834 // can throw anything".
1835 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001836 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001837 SourceLocation EllipsisLoc = ConsumeToken();
1838 if (!getLang().Microsoft)
1839 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001840 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001841 return false;
1842 }
1843
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001844 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001845 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001846 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001847 TypeResult Res(ParseTypeName(&Range));
1848 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001849 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001850 Ranges.push_back(Range);
1851 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001852 if (Tok.is(tok::comma))
1853 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001854 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001855 break;
1856 }
1857
Sebastian Redlab197ba2009-02-09 18:23:29 +00001858 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001859 return false;
1860}
Douglas Gregor6569d682009-05-27 23:11:45 +00001861
1862/// \brief We have just started parsing the definition of a new class,
1863/// so push that class onto our stack of classes that is currently
1864/// being parsed.
John McCalld226f652010-08-21 09:40:31 +00001865void Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001866 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001867 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00001868 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
Douglas Gregor6569d682009-05-27 23:11:45 +00001869}
1870
1871/// \brief Deallocate the given parsed class and all of its nested
1872/// classes.
1873void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1874 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1875 DeallocateParsedClasses(Class->NestedClasses[I]);
1876 delete Class;
1877}
1878
1879/// \brief Pop the top class of the stack of classes that are
1880/// currently being parsed.
1881///
1882/// This routine should be called when we have finished parsing the
1883/// definition of a class, but have not yet popped the Scope
1884/// associated with the class's definition.
1885///
1886/// \returns true if the class we've popped is a top-level class,
1887/// false otherwise.
1888void Parser::PopParsingClass() {
1889 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001890
Douglas Gregor6569d682009-05-27 23:11:45 +00001891 ParsingClass *Victim = ClassStack.top();
1892 ClassStack.pop();
1893 if (Victim->TopLevelClass) {
1894 // Deallocate all of the nested classes of this class,
1895 // recursively: we don't need to keep any of this information.
1896 DeallocateParsedClasses(Victim);
1897 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001898 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001899 assert(!ClassStack.empty() && "Missing top-level class?");
1900
1901 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1902 Victim->NestedClasses.empty()) {
1903 // The victim is a nested class, but we will not need to perform
1904 // any processing after the definition of this class since it has
1905 // no members whose handling was delayed. Therefore, we can just
1906 // remove this nested class.
1907 delete Victim;
1908 return;
1909 }
1910
1911 // This nested class has some members that will need to be processed
1912 // after the top-level class is completely defined. Therefore, add
1913 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001914 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregor6569d682009-05-27 23:11:45 +00001915 ClassStack.top()->NestedClasses.push_back(Victim);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001916 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00001917}
Sean Huntbbd37c62009-11-21 08:43:09 +00001918
1919/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1920/// parses standard attributes.
1921///
1922/// [C++0x] attribute-specifier:
1923/// '[' '[' attribute-list ']' ']'
1924///
1925/// [C++0x] attribute-list:
1926/// attribute[opt]
1927/// attribute-list ',' attribute[opt]
1928///
1929/// [C++0x] attribute:
1930/// attribute-token attribute-argument-clause[opt]
1931///
1932/// [C++0x] attribute-token:
1933/// identifier
1934/// attribute-scoped-token
1935///
1936/// [C++0x] attribute-scoped-token:
1937/// attribute-namespace '::' identifier
1938///
1939/// [C++0x] attribute-namespace:
1940/// identifier
1941///
1942/// [C++0x] attribute-argument-clause:
1943/// '(' balanced-token-seq ')'
1944///
1945/// [C++0x] balanced-token-seq:
1946/// balanced-token
1947/// balanced-token-seq balanced-token
1948///
1949/// [C++0x] balanced-token:
1950/// '(' balanced-token-seq ')'
1951/// '[' balanced-token-seq ']'
1952/// '{' balanced-token-seq '}'
1953/// any token but '(', ')', '[', ']', '{', or '}'
1954CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1955 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1956 && "Not a C++0x attribute list");
1957
1958 SourceLocation StartLoc = Tok.getLocation(), Loc;
1959 AttributeList *CurrAttr = 0;
1960
1961 ConsumeBracket();
1962 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001963
Sean Huntbbd37c62009-11-21 08:43:09 +00001964 if (Tok.is(tok::comma)) {
1965 Diag(Tok.getLocation(), diag::err_expected_ident);
1966 ConsumeToken();
1967 }
1968
1969 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1970 // attribute not present
1971 if (Tok.is(tok::comma)) {
1972 ConsumeToken();
1973 continue;
1974 }
1975
1976 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1977 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001978
Sean Huntbbd37c62009-11-21 08:43:09 +00001979 // scoped attribute
1980 if (Tok.is(tok::coloncolon)) {
1981 ConsumeToken();
1982
1983 if (!Tok.is(tok::identifier)) {
1984 Diag(Tok.getLocation(), diag::err_expected_ident);
1985 SkipUntil(tok::r_square, tok::comma, true, true);
1986 continue;
1987 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001988
Sean Huntbbd37c62009-11-21 08:43:09 +00001989 ScopeName = AttrName;
1990 ScopeLoc = AttrLoc;
1991
1992 AttrName = Tok.getIdentifierInfo();
1993 AttrLoc = ConsumeToken();
1994 }
1995
1996 bool AttrParsed = false;
1997 // No scoped names are supported; ideally we could put all non-standard
1998 // attributes into namespaces.
1999 if (!ScopeName) {
2000 switch(AttributeList::getKind(AttrName))
2001 {
2002 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00002003 case AttributeList::AT_base_check:
2004 case AttributeList::AT_carries_dependency:
Sean Huntbbd37c62009-11-21 08:43:09 +00002005 case AttributeList::AT_final:
Sean Hunt7725e672009-11-25 04:20:27 +00002006 case AttributeList::AT_hiding:
2007 case AttributeList::AT_noreturn:
2008 case AttributeList::AT_override: {
Sean Huntbbd37c62009-11-21 08:43:09 +00002009 if (Tok.is(tok::l_paren)) {
2010 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
2011 << AttrName->getName();
2012 break;
2013 }
2014
2015 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
2016 SourceLocation(), 0, 0, CurrAttr, false,
2017 true);
2018 AttrParsed = true;
2019 break;
2020 }
2021
2022 // One argument; must be a type-id or assignment-expression
2023 case AttributeList::AT_aligned: {
2024 if (Tok.isNot(tok::l_paren)) {
2025 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
2026 << AttrName->getName();
2027 break;
2028 }
2029 SourceLocation ParamLoc = ConsumeParen();
2030
John McCall60d7b3a2010-08-24 06:29:42 +00002031 ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002032
2033 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2034
2035 ExprVector ArgExprs(Actions);
2036 ArgExprs.push_back(ArgExpr.release());
2037 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
2038 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
2039 false, true);
2040
2041 AttrParsed = true;
2042 break;
2043 }
2044
2045 // Silence warnings
2046 default: break;
2047 }
2048 }
2049
2050 // Skip the entire parameter clause, if any
2051 if (!AttrParsed && Tok.is(tok::l_paren)) {
2052 ConsumeParen();
2053 // SkipUntil maintains the balancedness of tokens.
2054 SkipUntil(tok::r_paren, false);
2055 }
2056 }
2057
2058 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2059 SkipUntil(tok::r_square, false);
2060 Loc = Tok.getLocation();
2061 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2062 SkipUntil(tok::r_square, false);
2063
2064 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
2065 return Attr;
2066}
2067
2068/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2069/// attribute.
2070///
2071/// FIXME: Simply returns an alignof() expression if the argument is a
2072/// type. Ideally, the type should be propagated directly into Sema.
2073///
2074/// [C++0x] 'align' '(' type-id ')'
2075/// [C++0x] 'align' '(' assignment-expression ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002076ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002077 if (isTypeIdInParens()) {
John McCallf312b1e2010-08-26 23:41:50 +00002078 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sean Huntbbd37c62009-11-21 08:43:09 +00002079 SourceLocation TypeLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002080 ParsedType Ty = ParseTypeName().get();
Sean Huntbbd37c62009-11-21 08:43:09 +00002081 SourceRange TypeRange(Start, Tok.getLocation());
John McCallb3d87482010-08-24 05:47:05 +00002082 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true,
2083 Ty.getAsOpaquePtr(), TypeRange);
Sean Huntbbd37c62009-11-21 08:43:09 +00002084 } else
2085 return ParseConstantExpression();
2086}