blob: df707b22e1a8f93e199aa3933cf372f261c3359d [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
25/// may either be a top level namespace or a block-level namespace alias.
26///
27/// namespace-definition: [C++ 7.3: basic.namespace]
28/// named-namespace-definition
29/// unnamed-namespace-definition
30///
31/// unnamed-namespace-definition:
32/// 'namespace' attributes[opt] '{' namespace-body '}'
33///
34/// named-namespace-definition:
35/// original-namespace-definition
36/// extension-namespace-definition
37///
38/// original-namespace-definition:
39/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
40///
41/// extension-namespace-definition:
42/// 'namespace' original-namespace-name '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000043///
Chris Lattner8f08cb72007-08-25 06:57:03 +000044/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
45/// 'namespace' identifier '=' qualified-namespace-specifier ';'
46///
John McCalld226f652010-08-21 09:40:31 +000047Decl *Parser::ParseNamespace(unsigned Context,
48 SourceLocation &DeclEnd) {
Chris Lattner04d66662007-10-09 17:33:22 +000049 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000050 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump1eb44332009-09-09 15:08:12 +000051
Douglas Gregor49f40bd2009-09-18 19:03:04 +000052 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000053 Actions.CodeCompleteNamespaceDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +000054 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +000055 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000056
Chris Lattner8f08cb72007-08-25 06:57:03 +000057 SourceLocation IdentLoc;
58 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000059
60 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000061
Chris Lattner04d66662007-10-09 17:33:22 +000062 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000063 Ident = Tok.getIdentifierInfo();
64 IdentLoc = ConsumeToken(); // eat the identifier.
65 }
Mike Stump1eb44332009-09-09 15:08:12 +000066
Chris Lattner8f08cb72007-08-25 06:57:03 +000067 // Read label attributes, if present.
Ted Kremenek1e377652010-02-11 02:19:13 +000068 llvm::OwningPtr<AttributeList> AttrList;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000069 if (Tok.is(tok::kw___attribute)) {
70 attrTok = Tok;
71
Chris Lattner8f08cb72007-08-25 06:57:03 +000072 // FIXME: save these somewhere.
Ted Kremenek1e377652010-02-11 02:19:13 +000073 AttrList.reset(ParseGNUAttributes());
Douglas Gregor6a588dd2009-06-17 19:49:00 +000074 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Douglas Gregor6a588dd2009-06-17 19:49:00 +000076 if (Tok.is(tok::equal)) {
77 if (AttrList)
78 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
79
Chris Lattner97144fc2009-04-02 04:16:50 +000080 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000081 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Chris Lattner51448322009-03-29 14:02:43 +000083 if (Tok.isNot(tok::l_brace)) {
Mike Stump1eb44332009-09-09 15:08:12 +000084 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000085 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +000086 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +000087 }
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner51448322009-03-29 14:02:43 +000089 SourceLocation LBrace = ConsumeBrace();
90
Douglas Gregor23c94db2010-07-02 17:43:08 +000091 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
92 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
93 getCurScope()->getFnParent()) {
Douglas Gregor95f1b152010-05-14 05:08:22 +000094 Diag(LBrace, diag::err_namespace_nonnamespace_scope);
95 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +000096 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +000097 }
98
Chris Lattner51448322009-03-29 14:02:43 +000099 // Enter a scope for the namespace.
100 ParseScope NamespaceScope(this, Scope::DeclScope);
101
John McCalld226f652010-08-21 09:40:31 +0000102 Decl *NamespcDecl =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000103 Actions.ActOnStartNamespaceDef(getCurScope(), IdentLoc, Ident, LBrace,
Ted Kremenek1e377652010-02-11 02:19:13 +0000104 AttrList.get());
Chris Lattner51448322009-03-29 14:02:43 +0000105
John McCallf312b1e2010-08-26 23:41:50 +0000106 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
107 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Sean Huntbbd37c62009-11-21 08:43:09 +0000109 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
110 CXX0XAttributeList Attr;
111 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
112 Attr = ParseCXX0XAttributes();
113 ParseExternalDeclaration(Attr);
114 }
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Chris Lattner51448322009-03-29 14:02:43 +0000116 // Leave the namespace scope.
117 NamespaceScope.Exit();
118
Chris Lattner97144fc2009-04-02 04:16:50 +0000119 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
120 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000121
Chris Lattner97144fc2009-04-02 04:16:50 +0000122 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000123 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000124}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000125
Anders Carlssonf67606a2009-03-28 04:07:16 +0000126/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
127/// alias definition.
128///
John McCalld226f652010-08-21 09:40:31 +0000129Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000130 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000131 IdentifierInfo *Alias,
132 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000133 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Anders Carlssonf67606a2009-03-28 04:07:16 +0000135 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000137 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000138 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000139 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000140 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000141
Anders Carlssonf67606a2009-03-28 04:07:16 +0000142 CXXScopeSpec SS;
143 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000144 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000145
146 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
147 Diag(Tok, diag::err_expected_namespace_name);
148 // Skip to end of the definition and eat the ';'.
149 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000150 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000151 }
152
153 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000154 IdentifierInfo *Ident = Tok.getIdentifierInfo();
155 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Anders Carlssonf67606a2009-03-28 04:07:16 +0000157 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000158 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000159 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
160 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Douglas Gregor23c94db2010-07-02 17:43:08 +0000162 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000163 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000164}
165
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000166/// ParseLinkage - We know that the current token is a string_literal
167/// and just before that, that extern was seen.
168///
169/// linkage-specification: [C++ 7.5p2: dcl.link]
170/// 'extern' string-literal '{' declaration-seq[opt] '}'
171/// 'extern' string-literal declaration
172///
John McCalld226f652010-08-21 09:40:31 +0000173Decl *Parser::ParseLinkage(ParsingDeclSpec &DS,
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000174 unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000175 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000176 llvm::SmallString<8> LangBuffer;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000177 // LangBuffer is guaranteed to be big enough.
Douglas Gregor453091c2010-03-16 22:30:13 +0000178 bool Invalid = false;
179 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
180 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000181 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000182
183 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000184
Douglas Gregor074149e2009-01-05 19:45:36 +0000185 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000186 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000187 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Douglas Gregor074149e2009-01-05 19:45:36 +0000188 /*FIXME: */SourceLocation(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000189 Loc, Lang,
Mike Stump1eb44332009-09-09 15:08:12 +0000190 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000191 : SourceLocation());
192
Sean Huntbbd37c62009-11-21 08:43:09 +0000193 CXX0XAttributeList Attr;
194 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
195 Attr = ParseCXX0XAttributes();
196 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000197
Douglas Gregor074149e2009-01-05 19:45:36 +0000198 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000199 DS.setExternInLinkageSpec(true);
Douglas Gregor09a63c92010-08-24 14:14:45 +0000200 ParseExternalDeclaration(Attr, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000201 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000202 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000203 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000204
Douglas Gregor63a01132010-02-07 08:38:28 +0000205 DS.abort();
206
Sean Huntbbd37c62009-11-21 08:43:09 +0000207 if (Attr.HasAttr)
208 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
209 << Attr.Range;
210
Douglas Gregorf44515a2008-12-16 22:23:02 +0000211 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000212 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000213 CXX0XAttributeList Attr;
214 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
215 Attr = ParseCXX0XAttributes();
216 ParseExternalDeclaration(Attr);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000217 }
218
Douglas Gregorf44515a2008-12-16 22:23:02 +0000219 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000220 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000221}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000222
Douglas Gregorf780abc2008-12-30 03:27:21 +0000223/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
224/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000225Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000226 SourceLocation &DeclEnd,
227 CXX0XAttributeList Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000228 assert(Tok.is(tok::kw_using) && "Not using token");
229
230 // Eat 'using'.
231 SourceLocation UsingLoc = ConsumeToken();
232
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000233 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000234 Actions.CodeCompleteUsing(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000235 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000236 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000237
Chris Lattner2f274772009-01-06 06:55:51 +0000238 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000239 // Next token after 'using' is 'namespace' so it must be using-directive
Sean Huntbbd37c62009-11-21 08:43:09 +0000240 return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList);
241
242 if (Attr.HasAttr)
243 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
244 << Attr.Range;
Chris Lattner2f274772009-01-06 06:55:51 +0000245
246 // Otherwise, it must be using-declaration.
Sean Huntbbd37c62009-11-21 08:43:09 +0000247 // Ignore illegal attributes (the caller should already have issued an error.
Chris Lattner97144fc2009-04-02 04:16:50 +0000248 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000249}
250
251/// ParseUsingDirective - Parse C++ using-directive, assumes
252/// that current token is 'namespace' and 'using' was already parsed.
253///
254/// using-directive: [C++ 7.3.p4: namespace.udir]
255/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
256/// namespace-name ;
257/// [GNU] using-directive:
258/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
259/// namespace-name attributes[opt] ;
260///
John McCalld226f652010-08-21 09:40:31 +0000261Decl *Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000262 SourceLocation UsingLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000263 SourceLocation &DeclEnd,
264 AttributeList *Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000265 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
266
267 // Eat 'namespace'.
268 SourceLocation NamespcLoc = ConsumeToken();
269
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000270 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000271 Actions.CodeCompleteUsingDirective(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000272 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000273 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000274
Douglas Gregorf780abc2008-12-30 03:27:21 +0000275 CXXScopeSpec SS;
276 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000277 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000278
Douglas Gregorf780abc2008-12-30 03:27:21 +0000279 IdentifierInfo *NamespcName = 0;
280 SourceLocation IdentLoc = SourceLocation();
281
282 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000283 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000284 Diag(Tok, diag::err_expected_namespace_name);
285 // If there was invalid namespace name, skip to end of decl, and eat ';'.
286 SkipUntil(tok::semi);
287 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000288 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000289 }
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Chris Lattner823c44e2009-01-06 07:27:21 +0000291 // Parse identifier.
292 NamespcName = Tok.getIdentifierInfo();
293 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Chris Lattner823c44e2009-01-06 07:27:21 +0000295 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000296 bool GNUAttr = false;
297 if (Tok.is(tok::kw___attribute)) {
298 GNUAttr = true;
299 Attr = addAttributeLists(Attr, ParseGNUAttributes());
300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Chris Lattner823c44e2009-01-06 07:27:21 +0000302 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000303 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000304 ExpectAndConsume(tok::semi,
Sean Huntbbd37c62009-11-21 08:43:09 +0000305 GNUAttr ? diag::err_expected_semi_after_attribute_list :
Chris Lattner6869d8e2009-06-14 00:07:48 +0000306 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000307
Douglas Gregor23c94db2010-07-02 17:43:08 +0000308 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000309 IdentLoc, NamespcName, Attr);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000310}
311
312/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
313/// 'using' was already seen.
314///
315/// using-declaration: [C++ 7.3.p3: namespace.udecl]
316/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000317/// unqualified-id
318/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000319///
John McCalld226f652010-08-21 09:40:31 +0000320Decl *Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000321 SourceLocation UsingLoc,
Anders Carlsson595adc12009-08-29 19:54:19 +0000322 SourceLocation &DeclEnd,
323 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000324 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000325 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000326 bool IsTypeName;
327
328 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000329 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000330 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000331 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000332 ConsumeToken();
333 IsTypeName = true;
334 }
335 else
336 IsTypeName = false;
337
338 // Parse nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000339 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000340
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000341 // Check nested-name specifier.
342 if (SS.isInvalid()) {
343 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000344 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000345 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000346
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000347 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000348 // destructor names and allow the action module to diagnose any semantic
349 // errors.
350 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000351 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000352 /*EnteringContext=*/false,
353 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000354 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000355 ParsedType(),
Douglas Gregor12c118a2009-11-04 16:30:06 +0000356 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000357 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000358 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000359 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000360
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000361 // Parse (optional) attributes (most likely GNU strong-using extension).
Ted Kremenek1e377652010-02-11 02:19:13 +0000362 llvm::OwningPtr<AttributeList> AttrList;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000363 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +0000364 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000366 // Eat ';'.
367 DeclEnd = Tok.getLocation();
368 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000369 AttrList ? "attributes list" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000370 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000371
Douglas Gregor23c94db2010-07-02 17:43:08 +0000372 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS, Name,
Ted Kremenek1e377652010-02-11 02:19:13 +0000373 AttrList.get(), IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000374}
375
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000376/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
377///
378/// static_assert-declaration:
379/// static_assert ( constant-expression , string-literal ) ;
380///
John McCalld226f652010-08-21 09:40:31 +0000381Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000382 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
383 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000385 if (Tok.isNot(tok::l_paren)) {
386 Diag(Tok, diag::err_expected_lparen);
John McCalld226f652010-08-21 09:40:31 +0000387 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000390 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000391
John McCall60d7b3a2010-08-24 06:29:42 +0000392 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000393 if (AssertExpr.isInvalid()) {
394 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000395 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Anders Carlssonad5f9602009-03-13 23:29:20 +0000398 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000399 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000400
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000401 if (Tok.isNot(tok::string_literal)) {
402 Diag(Tok, diag::err_expected_string_literal);
403 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000404 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
John McCall60d7b3a2010-08-24 06:29:42 +0000407 ExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000408 if (AssertMessage.isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000409 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000410
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000411 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Chris Lattner97144fc2009-04-02 04:16:50 +0000413 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000414 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
415
John McCall9ae2f072010-08-23 23:25:46 +0000416 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
417 AssertExpr.take(),
418 AssertMessage.take());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000419}
420
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000421/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
422///
423/// 'decltype' ( expression )
424///
425void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
426 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
427
428 SourceLocation StartLoc = ConsumeToken();
429 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000430
431 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000432 "decltype")) {
433 SkipUntil(tok::r_paren);
434 return;
435 }
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000437 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000439 // C++0x [dcl.type.simple]p4:
440 // The operand of the decltype specifier is an unevaluated operand.
441 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000442 Sema::Unevaluated);
John McCall60d7b3a2010-08-24 06:29:42 +0000443 ExprResult Result = ParseExpression();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000444 if (Result.isInvalid()) {
445 SkipUntil(tok::r_paren);
446 return;
447 }
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000449 // Match the ')'
450 SourceLocation RParenLoc;
451 if (Tok.is(tok::r_paren))
452 RParenLoc = ConsumeParen();
453 else
454 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000456 if (RParenLoc.isInvalid())
457 return;
458
459 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000460 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000461 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000462 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000463 DiagID, Result.release()))
464 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000465}
466
Douglas Gregor42a552f2008-11-05 20:51:48 +0000467/// ParseClassName - Parse a C++ class-name, which names a class. Note
468/// that we only check that the result names a type; semantic analysis
469/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000470/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000471/// found.
472///
473/// class-name: [C++ 9.1]
474/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000475/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000476///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000477Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000478 CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000479 // Check whether we have a template-id that names a type.
480 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000481 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000482 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000483 if (TemplateId->Kind == TNK_Type_template ||
484 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000485 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000486
487 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000488 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000489 EndLocation = Tok.getAnnotationEndLoc();
490 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000491
492 if (Type)
493 return Type;
494 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000495 }
496
497 // Fall through to produce an error below.
498 }
499
Douglas Gregor42a552f2008-11-05 20:51:48 +0000500 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000501 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000502 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000503 }
504
Douglas Gregor84d0a192010-01-12 21:28:44 +0000505 IdentifierInfo *Id = Tok.getIdentifierInfo();
506 SourceLocation IdLoc = ConsumeToken();
507
508 if (Tok.is(tok::less)) {
509 // It looks the user intended to write a template-id here, but the
510 // template-name was wrong. Try to fix that.
511 TemplateNameKind TNK = TNK_Type_template;
512 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000513 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor84d0a192010-01-12 21:28:44 +0000514 SS, Template, TNK)) {
515 Diag(IdLoc, diag::err_unknown_template_name)
516 << Id;
517 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000518
Douglas Gregor84d0a192010-01-12 21:28:44 +0000519 if (!Template)
520 return true;
521
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000522 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000523 UnqualifiedId TemplateName;
524 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000525
Douglas Gregor84d0a192010-01-12 21:28:44 +0000526 // Parse the full template-id, then turn it into a type.
527 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
528 SourceLocation(), true))
529 return true;
530 if (TNK == TNK_Dependent_template_name)
531 AnnotateTemplateIdTokenAsType(SS);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000532
Douglas Gregor84d0a192010-01-12 21:28:44 +0000533 // If we didn't end up with a typename token, there's nothing more we
534 // can do.
535 if (Tok.isNot(tok::annot_typename))
536 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000537
Douglas Gregor84d0a192010-01-12 21:28:44 +0000538 // Retrieve the type from the annotation token, consume that token, and
539 // return.
540 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000541 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000542 ConsumeToken();
543 return Type;
544 }
545
Douglas Gregor42a552f2008-11-05 20:51:48 +0000546 // We have an identifier; check whether it is actually a type.
John McCallb3d87482010-08-24 05:47:05 +0000547 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), SS, true);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000548 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000549 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000550 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000551 }
552
553 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000554 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000555
556 // Fake up a Declarator to use with ActOnTypeName.
557 DeclSpec DS;
558 DS.SetRangeStart(IdLoc);
559 DS.SetRangeEnd(EndLocation);
560 DS.getTypeSpecScope() = *SS;
561
562 const char *PrevSpec = 0;
563 unsigned DiagID;
564 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
565
566 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
567 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000568}
569
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000570/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
571/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
572/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000573/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000574///
575/// class-specifier: [C++ class]
576/// class-head '{' member-specification[opt] '}'
577/// class-head '{' member-specification[opt] '}' attributes[opt]
578/// class-head:
579/// class-key identifier[opt] base-clause[opt]
580/// class-key nested-name-specifier identifier base-clause[opt]
581/// class-key nested-name-specifier[opt] simple-template-id
582/// base-clause[opt]
583/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000584/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000585/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000586/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000587/// simple-template-id base-clause[opt]
588/// class-key:
589/// 'class'
590/// 'struct'
591/// 'union'
592///
593/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000594/// class-key ::[opt] nested-name-specifier[opt] identifier
595/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
596/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000597///
598/// Note that the C++ class-specifier and elaborated-type-specifier,
599/// together, subsume the C99 struct-or-union-specifier:
600///
601/// struct-or-union-specifier: [C99 6.7.2.1]
602/// struct-or-union identifier[opt] '{' struct-contents '}'
603/// struct-or-union identifier
604/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
605/// '}' attributes[opt]
606/// [GNU] struct-or-union attributes[opt] identifier
607/// struct-or-union:
608/// 'struct'
609/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000610void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
611 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000612 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redld9bafa72010-02-03 21:21:43 +0000613 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000614 DeclSpec::TST TagType;
615 if (TagTokKind == tok::kw_struct)
616 TagType = DeclSpec::TST_struct;
617 else if (TagTokKind == tok::kw_class)
618 TagType = DeclSpec::TST_class;
619 else {
620 assert(TagTokKind == tok::kw_union && "Not a class specifier");
621 TagType = DeclSpec::TST_union;
622 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000623
Douglas Gregor374929f2009-09-18 15:37:17 +0000624 if (Tok.is(tok::code_completion)) {
625 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000626 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregordc845342010-05-25 05:58:43 +0000627 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +0000628 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000629
Chandler Carruth926c4b42010-06-28 08:39:25 +0000630 // C++03 [temp.explicit] 14.7.2/8:
631 // The usual access checking rules do not apply to names used to specify
632 // explicit instantiations.
633 //
634 // As an extension we do not perform access checking on the names used to
635 // specify explicit specializations either. This is important to allow
636 // specializing traits classes for private types.
637 bool SuppressingAccessChecks = false;
638 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
639 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
640 Actions.ActOnStartSuppressingAccessChecks();
641 SuppressingAccessChecks = true;
642 }
643
Sean Huntbbd37c62009-11-21 08:43:09 +0000644 AttributeList *AttrList = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000645 // If attributes exist after tag, parse them.
646 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000647 AttrList = ParseGNUAttributes();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000648
Steve Narofff59e17e2008-12-24 20:59:21 +0000649 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +0000650 while (Tok.is(tok::kw___declspec))
Sean Huntbbd37c62009-11-21 08:43:09 +0000651 AttrList = ParseMicrosoftDeclSpec(AttrList);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000652
Sean Huntbbd37c62009-11-21 08:43:09 +0000653 // If C++0x attributes exist here, parse them.
654 // FIXME: Are we consistent with the ordering of parsing of different
655 // styles of attributes?
656 if (isCXX0XAttributeSpecifier())
657 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Douglas Gregorb117a602009-09-04 05:53:02 +0000659 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
660 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
661 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000662 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000663 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
664 // properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000665 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000666 Tok.setKind(tok::identifier);
667 }
668
669 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
670 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
671 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000672 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000673 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
674 // properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000675 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000676 Tok.setKind(tok::identifier);
677 }
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000679 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000680 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000681 if (getLang().CPlusPlus) {
682 // "FOO : BAR" is not a potential typo for "FOO::BAR".
683 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000684
John McCallb3d87482010-08-24 05:47:05 +0000685 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true))
John McCall207014e2010-07-30 06:26:29 +0000686 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +0000687 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +0000688 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
689 Diag(Tok, diag::err_expected_ident);
690 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000691
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000692 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
693
Douglas Gregorcc636682009-02-17 23:15:12 +0000694 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000695 IdentifierInfo *Name = 0;
696 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000697 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000698 if (Tok.is(tok::identifier)) {
699 Name = Tok.getIdentifierInfo();
700 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000701
Douglas Gregor5ee37342010-05-30 22:30:21 +0000702 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000703 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000704 // Eat the template argument list and try to continue parsing this as
705 // a class (or template thereof).
706 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000707 SourceLocation LAngleLoc, RAngleLoc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000708 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000709 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000710 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000711 // We couldn't parse the template argument list at all, so don't
712 // try to give any location information for the list.
713 LAngleLoc = RAngleLoc = SourceLocation();
714 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000715
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000716 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000717 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000718 << (TagType == DeclSpec::TST_class? 0
719 : TagType == DeclSpec::TST_struct? 1
720 : 2)
721 << Name
722 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000723
724 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000725 // we've removed its template argument list.
726 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
727 if (TemplateParams && TemplateParams->size() > 1) {
728 TemplateParams->pop_back();
729 } else {
730 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000731 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000732 = ParsedTemplateInfo::NonTemplate;
733 }
734 } else if (TemplateInfo.Kind
735 == ParsedTemplateInfo::ExplicitInstantiation) {
736 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000737 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000738 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000739 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000740 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000741 = SourceLocation();
742 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
743 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000744 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000745 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000746 } else if (Tok.is(tok::annot_template_id)) {
747 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
748 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000749
Douglas Gregorc45c2322009-03-31 00:43:58 +0000750 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000751 // The template-name in the simple-template-id refers to
752 // something other than a class template. Give an appropriate
753 // error message and skip to the ';'.
754 SourceRange Range(NameLoc);
755 if (SS.isNotEmpty())
756 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000757
Douglas Gregor39a8de12009-02-25 19:37:18 +0000758 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
759 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Douglas Gregor39a8de12009-02-25 19:37:18 +0000761 DS.SetTypeSpecError();
762 SkipUntil(tok::semi, false, true);
763 TemplateId->Destroy();
Chandler Carruth926c4b42010-06-28 08:39:25 +0000764 if (SuppressingAccessChecks)
765 Actions.ActOnStopSuppressingAccessChecks();
766
Douglas Gregor39a8de12009-02-25 19:37:18 +0000767 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000768 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000769 }
770
Chandler Carruth926c4b42010-06-28 08:39:25 +0000771 // As soon as we're finished parsing the class's template-id, turn access
772 // checking back on.
773 if (SuppressingAccessChecks)
774 Actions.ActOnStopSuppressingAccessChecks();
775
John McCall67d1a672009-08-06 02:15:43 +0000776 // There are four options here. If we have 'struct foo;', then this
777 // is either a forward declaration or a friend declaration, which
778 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000779 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000780 // something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000781 // However, in some contexts, things look like declarations but are just
782 // references, e.g.
783 // new struct s;
784 // or
785 // &T::operator struct s;
786 // For these, SuppressDeclarations is true.
John McCallf312b1e2010-08-26 23:41:50 +0000787 Sema::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000788 if (SuppressDeclarations)
John McCallf312b1e2010-08-26 23:41:50 +0000789 TUK = Sema::TUK_Reference;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000790 else if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))){
Douglas Gregord85bea22009-09-26 06:47:28 +0000791 if (DS.isFriendSpecified()) {
792 // C++ [class.friend]p2:
793 // A class shall not be defined in a friend declaration.
794 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
795 << SourceRange(DS.getFriendSpecLoc());
796
797 // Skip everything up to the semicolon, so that this looks like a proper
798 // friend class (or template thereof) declaration.
799 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +0000800 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +0000801 } else {
802 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +0000803 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +0000804 }
805 } else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +0000806 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000807 else
John McCallf312b1e2010-08-26 23:41:50 +0000808 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000809
John McCall207014e2010-07-30 06:26:29 +0000810 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +0000811 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +0000812 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
813 // We have a declaration or reference to an anonymous class.
814 Diag(StartLoc, diag::err_anon_type_definition)
815 << DeclSpec::getSpecifierName(TagType);
816 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000817
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000818 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000819
820 if (TemplateId)
821 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000822 return;
823 }
824
Douglas Gregorddc29e12009-02-06 22:42:48 +0000825 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +0000826 DeclResult TagOrTempResult = true; // invalid
827 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000828
Douglas Gregor402abb52009-05-28 23:31:59 +0000829 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000830 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000831 // Explicit specialization, class template partial specialization,
832 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000833 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000834 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000835 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000836 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000837 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000838 // This is an explicit instantiation of a class template.
839 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000840 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000841 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000842 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000843 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000844 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000845 SS,
John McCall2b5289b2010-08-23 07:28:44 +0000846 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000847 TemplateId->TemplateNameLoc,
848 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000849 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000850 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000851 AttrList);
John McCall74256f52010-04-14 00:24:33 +0000852
853 // Friend template-ids are treated as references unless
854 // they have template headers, in which case they're ill-formed
855 // (FIXME: "template <class T> friend class A<T>::B<int>;").
856 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +0000857 } else if (TUK == Sema::TUK_Reference ||
858 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +0000859 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
John McCallc4e70192009-09-11 04:59:25 +0000860 TypeResult
John McCall2b5289b2010-08-23 07:28:44 +0000861 = Actions.ActOnTemplateIdType(TemplateId->Template,
John McCall6b2becf2009-09-08 17:47:29 +0000862 TemplateId->TemplateNameLoc,
863 TemplateId->LAngleLoc,
864 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000865 TemplateId->RAngleLoc);
866
John McCallc4e70192009-09-11 04:59:25 +0000867 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
868 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000869 } else {
870 // This is an explicit specialization or a class template
871 // partial specialization.
872 TemplateParameterLists FakedParamLists;
873
874 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
875 // This looks like an explicit instantiation, because we have
876 // something like
877 //
878 // template class Foo<X>
879 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000880 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000881 // meant to be an explicit specialization, but the user forgot
882 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +0000883 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000884
Mike Stump1eb44332009-09-09 15:08:12 +0000885 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000886 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000887 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000888 diag::err_explicit_instantiation_with_definition)
889 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +0000890 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000891
892 // Create a fake template parameter list that contains only
893 // "template<>", so that we treat this construct as a class
894 // template specialization.
895 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000896 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000897 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000898 LAngleLoc,
899 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000900 LAngleLoc));
901 TemplateParams = &FakedParamLists;
902 }
903
904 // Build the class template specialization.
905 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000906 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000907 StartLoc, SS,
John McCall2b5289b2010-08-23 07:28:44 +0000908 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000909 TemplateId->TemplateNameLoc,
910 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000911 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000912 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000913 AttrList,
John McCallf312b1e2010-08-26 23:41:50 +0000914 MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000915 TemplateParams? &(*TemplateParams)[0] : 0,
916 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000917 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000918 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000919 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000920 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000921 // Explicit instantiation of a member of a class template
922 // specialization, e.g.,
923 //
924 // template struct Outer<int>::Inner;
925 //
926 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000927 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000928 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000929 TemplateInfo.TemplateLoc,
930 TagType, StartLoc, SS, Name,
Sean Huntbbd37c62009-11-21 08:43:09 +0000931 NameLoc, AttrList);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000932 } else {
933 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000934 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000935 // FIXME: Diagnose this particular error.
936 }
937
John McCallc4e70192009-09-11 04:59:25 +0000938 bool IsDependent = false;
939
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000940 // Declaration or definition of a class type
Douglas Gregor23c94db2010-07-02 17:43:08 +0000941 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000942 Name, NameLoc, AttrList, AS,
John McCallf312b1e2010-08-26 23:41:50 +0000943 MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000944 TemplateParams? &(*TemplateParams)[0] : 0,
945 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000946 Owned, IsDependent);
947
948 // If ActOnTag said the type was dependent, try again with the
949 // less common call.
950 if (IsDependent)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000951 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000952 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000953 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000954
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000955 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +0000956 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +0000957 assert(Tok.is(tok::l_brace) ||
958 (getLang().CPlusPlus && Tok.is(tok::colon)));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000959 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000960 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000961 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000962 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000963 }
964
John McCallb3d87482010-08-24 05:47:05 +0000965 // FIXME: The DeclSpec should keep the locations of both the keyword and the
966 // name (if there is one).
967 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
968
969 const char *PrevSpec = 0;
970 unsigned DiagID;
971 bool Result;
John McCallc4e70192009-09-11 04:59:25 +0000972 if (!TypeResult.isInvalid()) {
John McCallb3d87482010-08-24 05:47:05 +0000973 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc,
974 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +0000975 } else if (!TagOrTempResult.isInvalid()) {
John McCallb3d87482010-08-24 05:47:05 +0000976 Result = DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID,
977 TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +0000978 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000979 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000980 return;
981 }
Mike Stump1eb44332009-09-09 15:08:12 +0000982
John McCallb3d87482010-08-24 05:47:05 +0000983 if (Result)
John McCallfec54012009-08-03 20:12:06 +0000984 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000985
Chris Lattner4ed5d912010-02-02 01:23:29 +0000986 // At this point, we've successfully parsed a class-specifier in 'definition'
987 // form (e.g. "struct foo { int x; }". While we could just return here, we're
988 // going to look at what comes after it to improve error recovery. If an
989 // impossible token occurs next, we assume that the programmer forgot a ; at
990 // the end of the declaration and recover that way.
991 //
992 // This switch enumerates the valid "follow" set for definition.
John McCallf312b1e2010-08-26 23:41:50 +0000993 if (TUK == Sema::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000994 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000995 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000996 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000997 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +0000998 case tok::star: // struct foo {...} * P;
999 case tok::amp: // struct foo {...} & R = ...
1000 case tok::identifier: // struct foo {...} V ;
1001 case tok::r_paren: //(struct foo {...} ) {4}
1002 case tok::annot_cxxscope: // struct foo {...} a:: b;
1003 case tok::annot_typename: // struct foo {...} a ::b;
1004 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +00001005 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +00001006 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001007 ExpectedSemi = false;
1008 break;
1009 // Type qualifiers
1010 case tok::kw_const: // struct foo {...} const x;
1011 case tok::kw_volatile: // struct foo {...} volatile x;
1012 case tok::kw_restrict: // struct foo {...} restrict x;
1013 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +00001014 // Storage-class specifiers
1015 case tok::kw_static: // struct foo {...} static x;
1016 case tok::kw_extern: // struct foo {...} extern x;
1017 case tok::kw_typedef: // struct foo {...} typedef x;
1018 case tok::kw_register: // struct foo {...} register x;
1019 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregor33f99242010-05-17 18:19:56 +00001020 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001021 // As shown above, type qualifiers and storage class specifiers absolutely
1022 // can occur after class specifiers according to the grammar. However,
1023 // almost noone actually writes code like this. If we see one of these,
1024 // it is much more likely that someone missed a semi colon and the
1025 // type/storage class specifier we're seeing is part of the *next*
1026 // intended declaration, as in:
1027 //
1028 // struct foo { ... }
1029 // typedef int X;
1030 //
1031 // We'd really like to emit a missing semicolon error instead of emitting
1032 // an error on the 'int' saying that you can't have two type specifiers in
1033 // the same declaration of X. Because of this, we look ahead past this
1034 // token to see if it's a type specifier. If so, we know the code is
1035 // otherwise invalid, so we can produce the expected semi error.
1036 if (!isKnownToBeTypeSpecifier(NextToken()))
1037 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001038 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001039
1040 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001041 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001042 if (!getLang().CPlusPlus)
1043 ExpectedSemi = false;
1044 break;
1045 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001046
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001047 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001048 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1049 TagType == DeclSpec::TST_class ? "class"
1050 : TagType == DeclSpec::TST_struct? "struct" : "union");
1051 // Push this token back into the preprocessor and change our current token
1052 // to ';' so that the rest of the code recovers as though there were an
1053 // ';' after the definition.
1054 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001055 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001056 }
1057 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001058}
1059
Mike Stump1eb44332009-09-09 15:08:12 +00001060/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001061///
1062/// base-clause : [C++ class.derived]
1063/// ':' base-specifier-list
1064/// base-specifier-list:
1065/// base-specifier '...'[opt]
1066/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001067void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001068 assert(Tok.is(tok::colon) && "Not a base clause");
1069 ConsumeToken();
1070
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001071 // Build up an array of parsed base specifiers.
John McCallca0408f2010-08-23 06:44:23 +00001072 llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001073
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001074 while (true) {
1075 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001076 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001077 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001078 // Skip the rest of this base specifier, up until the comma or
1079 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001080 SkipUntil(tok::comma, tok::l_brace, true, true);
1081 } else {
1082 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001083 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001084 }
1085
1086 // If the next token is a comma, consume it and keep reading
1087 // base-specifiers.
1088 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001090 // Consume the comma.
1091 ConsumeToken();
1092 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001093
1094 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001095 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001096}
1097
1098/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1099/// one entry in the base class list of a class specifier, for example:
1100/// class foo : public bar, virtual private baz {
1101/// 'public bar' and 'virtual private baz' are each base-specifiers.
1102///
1103/// base-specifier: [C++ class.derived]
1104/// ::[opt] nested-name-specifier[opt] class-name
1105/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1106/// class-name
1107/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1108/// class-name
John McCalld226f652010-08-21 09:40:31 +00001109Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001110 bool IsVirtual = false;
1111 SourceLocation StartLoc = Tok.getLocation();
1112
1113 // Parse the 'virtual' keyword.
1114 if (Tok.is(tok::kw_virtual)) {
1115 ConsumeToken();
1116 IsVirtual = true;
1117 }
1118
1119 // Parse an (optional) access specifier.
1120 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001121 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001122 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001124 // Parse the 'virtual' keyword (again!), in case it came after the
1125 // access specifier.
1126 if (Tok.is(tok::kw_virtual)) {
1127 SourceLocation VirtualLoc = ConsumeToken();
1128 if (IsVirtual) {
1129 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001130 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001131 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001132 }
1133
1134 IsVirtual = true;
1135 }
1136
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001137 // Parse optional '::' and optional nested-name-specifier.
1138 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001139 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001140
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001141 // The location of the base class itself.
1142 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001143
1144 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001145 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +00001146 TypeResult BaseType = ParseClassName(EndLocation, &SS);
1147 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001148 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001149
1150 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001151 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001153 // Notify semantic analysis that we have parsed a complete
1154 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001155 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +00001156 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001157}
1158
1159/// getAccessSpecifierIfPresent - Determine whether the next token is
1160/// a C++ access-specifier.
1161///
1162/// access-specifier: [C++ class.derived]
1163/// 'private'
1164/// 'protected'
1165/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001166AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001167 switch (Tok.getKind()) {
1168 default: return AS_none;
1169 case tok::kw_private: return AS_private;
1170 case tok::kw_protected: return AS_protected;
1171 case tok::kw_public: return AS_public;
1172 }
1173}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001174
Eli Friedmand33133c2009-07-22 21:45:50 +00001175void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
John McCalld226f652010-08-21 09:40:31 +00001176 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001177 // We just declared a member function. If this member function
1178 // has any default arguments, we'll need to parse them later.
1179 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001180 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +00001181 = DeclaratorInfo.getTypeObject(0).Fun;
1182 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1183 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1184 if (!LateMethod) {
1185 // Push this method onto the stack of late-parsed method
1186 // declarations.
1187 getCurrentClass().MethodDecls.push_back(
1188 LateParsedMethodDeclaration(ThisDecl));
1189 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001190 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001191
1192 // Add all of the parameters prior to this one (they don't
1193 // have default arguments).
1194 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1195 for (unsigned I = 0; I < ParamIdx; ++I)
1196 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001197 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001198 }
1199
1200 // Add this parameter to the list of parameters (it or may
1201 // not have a default argument).
1202 LateMethod->DefaultArgs.push_back(
1203 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1204 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1205 }
1206 }
1207}
1208
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001209/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1210///
1211/// member-declaration:
1212/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1213/// function-definition ';'[opt]
1214/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1215/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001216/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001217/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001218/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001219///
1220/// member-declarator-list:
1221/// member-declarator
1222/// member-declarator-list ',' member-declarator
1223///
1224/// member-declarator:
1225/// declarator pure-specifier[opt]
1226/// declarator constant-initializer[opt]
1227/// identifier[opt] ':' constant-expression
1228///
Sebastian Redle2b68332009-04-12 17:16:29 +00001229/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001230/// '= 0'
1231///
1232/// constant-initializer:
1233/// '=' constant-expression
1234///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001235void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCallc9068d72010-07-16 08:13:16 +00001236 const ParsedTemplateInfo &TemplateInfo,
1237 ParsingDeclRAIIObject *TemplateDiags) {
John McCall60fa3cf2009-12-11 02:10:03 +00001238 // Access declarations.
1239 if (!TemplateInfo.Kind &&
1240 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001241 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001242 Tok.is(tok::annot_cxxscope)) {
1243 bool isAccessDecl = false;
1244 if (NextToken().is(tok::identifier))
1245 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1246 else
1247 isAccessDecl = NextToken().is(tok::kw_operator);
1248
1249 if (isAccessDecl) {
1250 // Collect the scope specifier token we annotated earlier.
1251 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001252 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
John McCall60fa3cf2009-12-11 02:10:03 +00001253
1254 // Try to parse an unqualified-id.
1255 UnqualifiedId Name;
John McCallb3d87482010-08-24 05:47:05 +00001256 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001257 SkipUntil(tok::semi);
1258 return;
1259 }
1260
1261 // TODO: recover from mistakenly-qualified operator declarations.
1262 if (ExpectAndConsume(tok::semi,
1263 diag::err_expected_semi_after,
1264 "access declaration",
1265 tok::semi))
1266 return;
1267
Douglas Gregor23c94db2010-07-02 17:43:08 +00001268 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001269 false, SourceLocation(),
1270 SS, Name,
1271 /* AttrList */ 0,
1272 /* IsTypeName */ false,
1273 SourceLocation());
1274 return;
1275 }
1276 }
1277
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001278 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001279 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001280 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001281 SourceLocation DeclEnd;
1282 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001283 return;
1284 }
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Chris Lattner682bf922009-03-29 16:50:03 +00001286 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001287 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001288 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001289 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001290 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001291 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001292 return;
1293 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001294
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001295 // Handle: member-declaration ::= '__extension__' member-declaration
1296 if (Tok.is(tok::kw___extension__)) {
1297 // __extension__ silences extension warnings in the subexpression.
1298 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1299 ConsumeToken();
John McCallc9068d72010-07-16 08:13:16 +00001300 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001301 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001302
Chris Lattner4ed5d912010-02-02 01:23:29 +00001303 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1304 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001305 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001306
Sean Huntbbd37c62009-11-21 08:43:09 +00001307 CXX0XAttributeList AttrList;
1308 // Optional C++0x attribute-specifier
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001309 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Sean Huntbbd37c62009-11-21 08:43:09 +00001310 AttrList = ParseCXX0XAttributes();
Sean Huntbbd37c62009-11-21 08:43:09 +00001311
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001312 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001313 // FIXME: Check for template aliases
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001314
Sean Huntbbd37c62009-11-21 08:43:09 +00001315 if (AttrList.HasAttr)
1316 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1317 << AttrList.Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001319 // Eat 'using'.
1320 SourceLocation UsingLoc = ConsumeToken();
1321
1322 if (Tok.is(tok::kw_namespace)) {
1323 Diag(UsingLoc, diag::err_using_namespace_in_class);
1324 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001325 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001326 SourceLocation DeclEnd;
1327 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001328 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001329 }
1330 return;
1331 }
1332
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001333 SourceLocation DSStart = Tok.getLocation();
1334 // decl-specifier-seq:
1335 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001336 ParsingDeclSpec DS(*this, TemplateDiags);
Sean Huntbbd37c62009-11-21 08:43:09 +00001337 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001338 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001339
John McCallf312b1e2010-08-26 23:41:50 +00001340 MultiTemplateParamsArg TemplateParams(Actions,
John McCalldd4a3b02009-09-16 22:47:08 +00001341 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1342 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1343
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001344 if (Tok.is(tok::semi)) {
1345 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001346 Decl *TheDecl =
John McCallc9068d72010-07-16 08:13:16 +00001347 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
1348 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001349 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001350 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001351
John McCall54abf7d2009-11-04 02:18:39 +00001352 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001353
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001354 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001355 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1356 ColonProtectionRAIIObject X(*this);
1357
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001358 // Parse the first declarator.
1359 ParseDeclarator(DeclaratorInfo);
1360 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001361 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001362 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001363 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001364 if (Tok.is(tok::semi))
1365 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001366 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001367 }
1368
John Thompson1b2fc0f2009-11-25 22:58:06 +00001369 // If attributes exist after the declarator, but before an '{', parse them.
1370 if (Tok.is(tok::kw___attribute)) {
1371 SourceLocation Loc;
1372 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1373 DeclaratorInfo.AddAttributes(AttrList, Loc);
1374 }
1375
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001376 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001377 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001378 || (DeclaratorInfo.isFunctionDeclarator() &&
1379 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001380 if (!DeclaratorInfo.isFunctionDeclarator()) {
1381 Diag(Tok, diag::err_func_def_no_params);
1382 ConsumeBrace();
1383 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001384 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001385 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001386
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001387 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1388 Diag(Tok, diag::err_function_declared_typedef);
1389 // This recovery skips the entire function body. It would be nice
1390 // to simply call ParseCXXInlineMethodDef() below, however Sema
1391 // assumes the declarator represents a function, not a typedef.
1392 ConsumeBrace();
1393 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001394 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001395 }
1396
Douglas Gregor37b372b2009-08-20 22:52:58 +00001397 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001398 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001399 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001400 }
1401
1402 // member-declarator-list:
1403 // member-declarator
1404 // member-declarator-list ',' member-declarator
1405
John McCalld226f652010-08-21 09:40:31 +00001406 llvm::SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00001407 ExprResult BitfieldSize;
1408 ExprResult Init;
Sebastian Redle2b68332009-04-12 17:16:29 +00001409 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001410
1411 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001412 // member-declarator:
1413 // declarator pure-specifier[opt]
1414 // declarator constant-initializer[opt]
1415 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001416 if (Tok.is(tok::colon)) {
1417 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001418 BitfieldSize = ParseConstantExpression();
1419 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001420 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001421 }
Mike Stump1eb44332009-09-09 15:08:12 +00001422
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001423 // pure-specifier:
1424 // '= 0'
1425 //
1426 // constant-initializer:
1427 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001428 //
1429 // defaulted/deleted function-definition:
1430 // '=' 'default' [TODO]
1431 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001432 if (Tok.is(tok::equal)) {
1433 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001434 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1435 ConsumeToken();
1436 Deleted = true;
1437 } else {
1438 Init = ParseInitializer();
1439 if (Init.isInvalid())
1440 SkipUntil(tok::comma, true, true);
1441 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001442 }
1443
Chris Lattnere6563252010-06-13 05:34:18 +00001444 // If a simple-asm-expr is present, parse it.
1445 if (Tok.is(tok::kw_asm)) {
1446 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001447 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00001448 if (AsmLabel.isInvalid())
1449 SkipUntil(tok::comma, true, true);
1450
1451 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1452 DeclaratorInfo.SetRangeEnd(Loc);
1453 }
1454
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001455 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001456 if (Tok.is(tok::kw___attribute)) {
1457 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001458 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001459 DeclaratorInfo.AddAttributes(AttrList, Loc);
1460 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001461
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001462 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001463 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001464 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001465
John McCalld226f652010-08-21 09:40:31 +00001466 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00001467 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001468 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00001469 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCallbbbcdd92009-09-11 21:02:39 +00001470 /*IsDefinition*/ false,
1471 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001472 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001473 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00001474 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001475 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001476 BitfieldSize.release(),
1477 Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001478 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001479 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001480 }
Chris Lattner682bf922009-03-29 16:50:03 +00001481 if (ThisDecl)
1482 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001483
Douglas Gregor72b505b2008-12-16 21:30:33 +00001484 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001485 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001486 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001487 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001488 }
1489
John McCall54abf7d2009-11-04 02:18:39 +00001490 DeclaratorInfo.complete(ThisDecl);
1491
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001492 // If we don't have a comma, it is either the end of the list (a ';')
1493 // or an error, bail out.
1494 if (Tok.isNot(tok::comma))
1495 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001497 // Consume the comma.
1498 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001500 // Parse the next declarator.
1501 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001502 BitfieldSize = 0;
1503 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001504 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001506 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001507 if (Tok.is(tok::kw___attribute)) {
1508 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001509 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001510 DeclaratorInfo.AddAttributes(AttrList, Loc);
1511 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001512
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001513 if (Tok.isNot(tok::colon))
1514 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001515 }
1516
Chris Lattnerae50d502010-02-02 00:43:15 +00001517 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1518 // Skip to end of block or statement.
1519 SkipUntil(tok::r_brace, true, true);
1520 // If we stopped at a ';', eat it.
1521 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001522 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001523 }
1524
Douglas Gregor23c94db2010-07-02 17:43:08 +00001525 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00001526 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001527}
1528
1529/// ParseCXXMemberSpecification - Parse the class definition.
1530///
1531/// member-specification:
1532/// member-declaration member-specification[opt]
1533/// access-specifier ':' member-specification[opt]
1534///
1535void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001536 unsigned TagType, Decl *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001537 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001538 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001539 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001540
John McCallf312b1e2010-08-26 23:41:50 +00001541 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1542 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Douglas Gregor26997fd2010-01-16 20:52:59 +00001544 // Determine whether this is a non-nested class. Note that local
1545 // classes are *not* considered to be nested classes.
1546 bool NonNestedClass = true;
1547 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001548 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001549 if (S->isClassScope()) {
1550 // We're inside a class scope, so this is a nested class.
1551 NonNestedClass = false;
1552 break;
1553 }
1554
1555 if ((S->getFlags() & Scope::FnScope)) {
1556 // If we're in a function or function template declared in the
1557 // body of a class, then this is a local class rather than a
1558 // nested class.
1559 const Scope *Parent = S->getParent();
1560 if (Parent->isTemplateParamScope())
1561 Parent = Parent->getParent();
1562 if (Parent->isClassScope())
1563 break;
1564 }
1565 }
1566 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001567
1568 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001569 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001570
Douglas Gregor6569d682009-05-27 23:11:45 +00001571 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001572 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001573
Douglas Gregorddc29e12009-02-06 22:42:48 +00001574 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001575 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001576
1577 if (Tok.is(tok::colon)) {
1578 ParseBaseClause(TagDecl);
1579
1580 if (!Tok.is(tok::l_brace)) {
1581 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00001582
1583 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001584 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001585 return;
1586 }
1587 }
1588
1589 assert(Tok.is(tok::l_brace));
1590
1591 SourceLocation LBraceLoc = ConsumeBrace();
1592
John McCall42a4f662010-05-28 08:11:17 +00001593 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001594 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, LBraceLoc);
John McCallf9368152009-12-20 07:58:13 +00001595
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001596 // C++ 11p3: Members of a class defined with the keyword class are private
1597 // by default. Members of a class defined with the keywords struct or union
1598 // are public by default.
1599 AccessSpecifier CurAS;
1600 if (TagType == DeclSpec::TST_class)
1601 CurAS = AS_private;
1602 else
1603 CurAS = AS_public;
1604
Douglas Gregor07976d22010-06-21 22:31:09 +00001605 SourceLocation RBraceLoc;
1606 if (TagDecl) {
1607 // While we still have something to read, read the member-declarations.
1608 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1609 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Douglas Gregor07976d22010-06-21 22:31:09 +00001611 // Check for extraneous top-level semicolon.
1612 if (Tok.is(tok::semi)) {
1613 Diag(Tok, diag::ext_extra_struct_semi)
1614 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1615 << FixItHint::CreateRemoval(Tok.getLocation());
1616 ConsumeToken();
1617 continue;
1618 }
1619
1620 AccessSpecifier AS = getAccessSpecifierIfPresent();
1621 if (AS != AS_none) {
1622 // Current token is a C++ access specifier.
1623 CurAS = AS;
1624 SourceLocation ASLoc = Tok.getLocation();
1625 ConsumeToken();
1626 if (Tok.is(tok::colon))
1627 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1628 else
1629 Diag(Tok, diag::err_expected_colon);
1630 ConsumeToken();
1631 continue;
1632 }
1633
1634 // FIXME: Make sure we don't have a template here.
1635
1636 // Parse all the comma separated declarators.
1637 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001638 }
1639
Douglas Gregor07976d22010-06-21 22:31:09 +00001640 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1641 } else {
1642 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001643 }
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001645 // If attributes exist after class contents, parse them.
Ted Kremenek1e377652010-02-11 02:19:13 +00001646 llvm::OwningPtr<AttributeList> AttrList;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001647 if (Tok.is(tok::kw___attribute))
Douglas Gregor0b4c9b52010-03-29 14:42:08 +00001648 AttrList.reset(ParseGNUAttributes());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001649
John McCall42a4f662010-05-28 08:11:17 +00001650 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001651 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall42a4f662010-05-28 08:11:17 +00001652 LBraceLoc, RBraceLoc,
1653 AttrList.get());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001654
1655 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1656 // complete within function bodies, default arguments,
1657 // exception-specifications, and constructor ctor-initializers (including
1658 // such things in nested classes).
1659 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001660 // FIXME: Only function bodies and constructor ctor-initializers are
1661 // parsed correctly, fix the rest.
Douglas Gregor07976d22010-06-21 22:31:09 +00001662 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001663 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001664 // are complete and we can parse the delayed portions of method
1665 // declarations and the lexed inline method definitions.
Douglas Gregore0cc0472010-06-16 23:45:56 +00001666 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregor6569d682009-05-27 23:11:45 +00001667 ParseLexedMethodDeclarations(getCurrentClass());
1668 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00001669 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001670 }
1671
John McCall42a4f662010-05-28 08:11:17 +00001672 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001673 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCalldb7bb4a2010-03-17 00:38:33 +00001674
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001675 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001676 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001677 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001678}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001679
1680/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1681/// which explicitly initializes the members or base classes of a
1682/// class (C++ [class.base.init]). For example, the three initializers
1683/// after the ':' in the Derived constructor below:
1684///
1685/// @code
1686/// class Base { };
1687/// class Derived : Base {
1688/// int x;
1689/// float f;
1690/// public:
1691/// Derived(float f) : Base(), x(17), f(f) { }
1692/// };
1693/// @endcode
1694///
Mike Stump1eb44332009-09-09 15:08:12 +00001695/// [C++] ctor-initializer:
1696/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001697///
Mike Stump1eb44332009-09-09 15:08:12 +00001698/// [C++] mem-initializer-list:
1699/// mem-initializer
1700/// mem-initializer , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00001701void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001702 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1703
1704 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001705
John McCallca0408f2010-08-23 06:44:23 +00001706 llvm::SmallVector<CXXBaseOrMemberInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001707 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001708
Douglas Gregor7ad83902008-11-05 04:29:56 +00001709 do {
1710 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001711 if (!MemInit.isInvalid())
1712 MemInitializers.push_back(MemInit.get());
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001713 else
1714 AnyErrors = true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001715
Douglas Gregor7ad83902008-11-05 04:29:56 +00001716 if (Tok.is(tok::comma))
1717 ConsumeToken();
1718 else if (Tok.is(tok::l_brace))
1719 break;
1720 else {
1721 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001722 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001723 SkipUntil(tok::l_brace, true, true);
1724 break;
1725 }
1726 } while (true);
1727
Mike Stump1eb44332009-09-09 15:08:12 +00001728 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001729 MemInitializers.data(), MemInitializers.size(),
1730 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001731}
1732
1733/// ParseMemInitializer - Parse a C++ member initializer, which is
1734/// part of a constructor initializer that explicitly initializes one
1735/// member or base class (C++ [class.base.init]). See
1736/// ParseConstructorInitializer for an example.
1737///
1738/// [C++] mem-initializer:
1739/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001740///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001741/// [C++] mem-initializer-id:
1742/// '::'[opt] nested-name-specifier[opt] class-name
1743/// identifier
John McCalld226f652010-08-21 09:40:31 +00001744Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001745 // parse '::'[opt] nested-name-specifier[opt]
1746 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001747 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
1748 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00001749 if (Tok.is(tok::annot_template_id)) {
1750 TemplateIdAnnotation *TemplateId
1751 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001752 if (TemplateId->Kind == TNK_Type_template ||
1753 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanian96174332009-07-01 19:21:19 +00001754 AnnotateTemplateIdTokenAsType(&SS);
1755 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00001756 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001757 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001758 }
1759 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001760 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001761 return true;
1762 }
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Douglas Gregor7ad83902008-11-05 04:29:56 +00001764 // Get the identifier. This may be a member name or a class name,
1765 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001766 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001767 SourceLocation IdLoc = ConsumeToken();
1768
1769 // Parse the '('.
1770 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001771 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001772 return true;
1773 }
1774 SourceLocation LParenLoc = ConsumeParen();
1775
1776 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001777 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001778 CommaLocsTy CommaLocs;
1779 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1780 SkipUntil(tok::r_paren);
1781 return true;
1782 }
1783
1784 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1785
Douglas Gregor23c94db2010-07-02 17:43:08 +00001786 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanian96174332009-07-01 19:21:19 +00001787 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001788 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001789 ArgExprs.size(), CommaLocs.data(),
1790 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001791}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001792
1793/// ParseExceptionSpecification - Parse a C++ exception-specification
1794/// (C++ [except.spec]).
1795///
Douglas Gregora4745612008-12-01 18:00:20 +00001796/// exception-specification:
1797/// 'throw' '(' type-id-list [opt] ')'
1798/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001799///
Douglas Gregora4745612008-12-01 18:00:20 +00001800/// type-id-list:
1801/// type-id
1802/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001803///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001804bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
John McCallb3d87482010-08-24 05:47:05 +00001805 llvm::SmallVectorImpl<ParsedType>
Sebastian Redlef65f062009-05-29 18:02:33 +00001806 &Exceptions,
John McCallb3d87482010-08-24 05:47:05 +00001807 llvm::SmallVectorImpl<SourceRange>
Sebastian Redlef65f062009-05-29 18:02:33 +00001808 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001809 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001810 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001812 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001813
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001814 if (!Tok.is(tok::l_paren)) {
1815 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1816 }
1817 SourceLocation LParenLoc = ConsumeParen();
1818
Douglas Gregora4745612008-12-01 18:00:20 +00001819 // Parse throw(...), a Microsoft extension that means "this function
1820 // can throw anything".
1821 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001822 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001823 SourceLocation EllipsisLoc = ConsumeToken();
1824 if (!getLang().Microsoft)
1825 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001826 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001827 return false;
1828 }
1829
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001830 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001831 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001832 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001833 TypeResult Res(ParseTypeName(&Range));
1834 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001835 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001836 Ranges.push_back(Range);
1837 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001838 if (Tok.is(tok::comma))
1839 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001840 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001841 break;
1842 }
1843
Sebastian Redlab197ba2009-02-09 18:23:29 +00001844 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001845 return false;
1846}
Douglas Gregor6569d682009-05-27 23:11:45 +00001847
1848/// \brief We have just started parsing the definition of a new class,
1849/// so push that class onto our stack of classes that is currently
1850/// being parsed.
John McCalld226f652010-08-21 09:40:31 +00001851void Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001852 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001853 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00001854 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
Douglas Gregor6569d682009-05-27 23:11:45 +00001855}
1856
1857/// \brief Deallocate the given parsed class and all of its nested
1858/// classes.
1859void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1860 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1861 DeallocateParsedClasses(Class->NestedClasses[I]);
1862 delete Class;
1863}
1864
1865/// \brief Pop the top class of the stack of classes that are
1866/// currently being parsed.
1867///
1868/// This routine should be called when we have finished parsing the
1869/// definition of a class, but have not yet popped the Scope
1870/// associated with the class's definition.
1871///
1872/// \returns true if the class we've popped is a top-level class,
1873/// false otherwise.
1874void Parser::PopParsingClass() {
1875 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Douglas Gregor6569d682009-05-27 23:11:45 +00001877 ParsingClass *Victim = ClassStack.top();
1878 ClassStack.pop();
1879 if (Victim->TopLevelClass) {
1880 // Deallocate all of the nested classes of this class,
1881 // recursively: we don't need to keep any of this information.
1882 DeallocateParsedClasses(Victim);
1883 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001884 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001885 assert(!ClassStack.empty() && "Missing top-level class?");
1886
1887 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1888 Victim->NestedClasses.empty()) {
1889 // The victim is a nested class, but we will not need to perform
1890 // any processing after the definition of this class since it has
1891 // no members whose handling was delayed. Therefore, we can just
1892 // remove this nested class.
1893 delete Victim;
1894 return;
1895 }
1896
1897 // This nested class has some members that will need to be processed
1898 // after the top-level class is completely defined. Therefore, add
1899 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001900 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregor6569d682009-05-27 23:11:45 +00001901 ClassStack.top()->NestedClasses.push_back(Victim);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001902 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00001903}
Sean Huntbbd37c62009-11-21 08:43:09 +00001904
1905/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1906/// parses standard attributes.
1907///
1908/// [C++0x] attribute-specifier:
1909/// '[' '[' attribute-list ']' ']'
1910///
1911/// [C++0x] attribute-list:
1912/// attribute[opt]
1913/// attribute-list ',' attribute[opt]
1914///
1915/// [C++0x] attribute:
1916/// attribute-token attribute-argument-clause[opt]
1917///
1918/// [C++0x] attribute-token:
1919/// identifier
1920/// attribute-scoped-token
1921///
1922/// [C++0x] attribute-scoped-token:
1923/// attribute-namespace '::' identifier
1924///
1925/// [C++0x] attribute-namespace:
1926/// identifier
1927///
1928/// [C++0x] attribute-argument-clause:
1929/// '(' balanced-token-seq ')'
1930///
1931/// [C++0x] balanced-token-seq:
1932/// balanced-token
1933/// balanced-token-seq balanced-token
1934///
1935/// [C++0x] balanced-token:
1936/// '(' balanced-token-seq ')'
1937/// '[' balanced-token-seq ']'
1938/// '{' balanced-token-seq '}'
1939/// any token but '(', ')', '[', ']', '{', or '}'
1940CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1941 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1942 && "Not a C++0x attribute list");
1943
1944 SourceLocation StartLoc = Tok.getLocation(), Loc;
1945 AttributeList *CurrAttr = 0;
1946
1947 ConsumeBracket();
1948 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001949
Sean Huntbbd37c62009-11-21 08:43:09 +00001950 if (Tok.is(tok::comma)) {
1951 Diag(Tok.getLocation(), diag::err_expected_ident);
1952 ConsumeToken();
1953 }
1954
1955 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1956 // attribute not present
1957 if (Tok.is(tok::comma)) {
1958 ConsumeToken();
1959 continue;
1960 }
1961
1962 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1963 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001964
Sean Huntbbd37c62009-11-21 08:43:09 +00001965 // scoped attribute
1966 if (Tok.is(tok::coloncolon)) {
1967 ConsumeToken();
1968
1969 if (!Tok.is(tok::identifier)) {
1970 Diag(Tok.getLocation(), diag::err_expected_ident);
1971 SkipUntil(tok::r_square, tok::comma, true, true);
1972 continue;
1973 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001974
Sean Huntbbd37c62009-11-21 08:43:09 +00001975 ScopeName = AttrName;
1976 ScopeLoc = AttrLoc;
1977
1978 AttrName = Tok.getIdentifierInfo();
1979 AttrLoc = ConsumeToken();
1980 }
1981
1982 bool AttrParsed = false;
1983 // No scoped names are supported; ideally we could put all non-standard
1984 // attributes into namespaces.
1985 if (!ScopeName) {
1986 switch(AttributeList::getKind(AttrName))
1987 {
1988 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00001989 case AttributeList::AT_base_check:
1990 case AttributeList::AT_carries_dependency:
Sean Huntbbd37c62009-11-21 08:43:09 +00001991 case AttributeList::AT_final:
Sean Hunt7725e672009-11-25 04:20:27 +00001992 case AttributeList::AT_hiding:
1993 case AttributeList::AT_noreturn:
1994 case AttributeList::AT_override: {
Sean Huntbbd37c62009-11-21 08:43:09 +00001995 if (Tok.is(tok::l_paren)) {
1996 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
1997 << AttrName->getName();
1998 break;
1999 }
2000
2001 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
2002 SourceLocation(), 0, 0, CurrAttr, false,
2003 true);
2004 AttrParsed = true;
2005 break;
2006 }
2007
2008 // One argument; must be a type-id or assignment-expression
2009 case AttributeList::AT_aligned: {
2010 if (Tok.isNot(tok::l_paren)) {
2011 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
2012 << AttrName->getName();
2013 break;
2014 }
2015 SourceLocation ParamLoc = ConsumeParen();
2016
John McCall60d7b3a2010-08-24 06:29:42 +00002017 ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002018
2019 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2020
2021 ExprVector ArgExprs(Actions);
2022 ArgExprs.push_back(ArgExpr.release());
2023 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
2024 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
2025 false, true);
2026
2027 AttrParsed = true;
2028 break;
2029 }
2030
2031 // Silence warnings
2032 default: break;
2033 }
2034 }
2035
2036 // Skip the entire parameter clause, if any
2037 if (!AttrParsed && Tok.is(tok::l_paren)) {
2038 ConsumeParen();
2039 // SkipUntil maintains the balancedness of tokens.
2040 SkipUntil(tok::r_paren, false);
2041 }
2042 }
2043
2044 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2045 SkipUntil(tok::r_square, false);
2046 Loc = Tok.getLocation();
2047 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2048 SkipUntil(tok::r_square, false);
2049
2050 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
2051 return Attr;
2052}
2053
2054/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2055/// attribute.
2056///
2057/// FIXME: Simply returns an alignof() expression if the argument is a
2058/// type. Ideally, the type should be propagated directly into Sema.
2059///
2060/// [C++0x] 'align' '(' type-id ')'
2061/// [C++0x] 'align' '(' assignment-expression ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002062ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002063 if (isTypeIdInParens()) {
John McCallf312b1e2010-08-26 23:41:50 +00002064 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sean Huntbbd37c62009-11-21 08:43:09 +00002065 SourceLocation TypeLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002066 ParsedType Ty = ParseTypeName().get();
Sean Huntbbd37c62009-11-21 08:43:09 +00002067 SourceRange TypeRange(Start, Tok.getLocation());
John McCallb3d87482010-08-24 05:47:05 +00002068 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true,
2069 Ty.getAsOpaquePtr(), TypeRange);
Sean Huntbbd37c62009-11-21 08:43:09 +00002070 } else
2071 return ParseConstantExpression();
2072}