blob: 55e677dbaaeb54ffd987bc82f211e9332ad712c5 [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"
Douglas Gregore37ac4f2008-04-13 21:30:24 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000018#include "clang/Parse/Scope.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000019#include "clang/Parse/Template.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000021using namespace clang;
22
23/// ParseNamespace - We know that the current token is a namespace keyword. This
24/// may either be a top level namespace or a block-level namespace alias.
25///
26/// namespace-definition: [C++ 7.3: basic.namespace]
27/// named-namespace-definition
28/// unnamed-namespace-definition
29///
30/// unnamed-namespace-definition:
31/// 'namespace' attributes[opt] '{' namespace-body '}'
32///
33/// named-namespace-definition:
34/// original-namespace-definition
35/// extension-namespace-definition
36///
37/// original-namespace-definition:
38/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
39///
40/// extension-namespace-definition:
41/// 'namespace' original-namespace-name '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000042///
Chris Lattner8f08cb72007-08-25 06:57:03 +000043/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
44/// 'namespace' identifier '=' qualified-namespace-specifier ';'
45///
Chris Lattner97144fc2009-04-02 04:16:50 +000046Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
47 SourceLocation &DeclEnd) {
Chris Lattner04d66662007-10-09 17:33:22 +000048 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000049 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor49f40bd2009-09-18 19:03:04 +000051 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000052 Actions.CodeCompleteNamespaceDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +000053 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +000054 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000055
Chris Lattner8f08cb72007-08-25 06:57:03 +000056 SourceLocation IdentLoc;
57 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000058
59 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000060
Chris Lattner04d66662007-10-09 17:33:22 +000061 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000062 Ident = Tok.getIdentifierInfo();
63 IdentLoc = ConsumeToken(); // eat the identifier.
64 }
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner8f08cb72007-08-25 06:57:03 +000066 // Read label attributes, if present.
Ted Kremenek1e377652010-02-11 02:19:13 +000067 llvm::OwningPtr<AttributeList> AttrList;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000068 if (Tok.is(tok::kw___attribute)) {
69 attrTok = Tok;
70
Chris Lattner8f08cb72007-08-25 06:57:03 +000071 // FIXME: save these somewhere.
Ted Kremenek1e377652010-02-11 02:19:13 +000072 AttrList.reset(ParseGNUAttributes());
Douglas Gregor6a588dd2009-06-17 19:49:00 +000073 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Douglas Gregor6a588dd2009-06-17 19:49:00 +000075 if (Tok.is(tok::equal)) {
76 if (AttrList)
77 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
78
Chris Lattner97144fc2009-04-02 04:16:50 +000079 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000080 }
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattner51448322009-03-29 14:02:43 +000082 if (Tok.isNot(tok::l_brace)) {
Mike Stump1eb44332009-09-09 15:08:12 +000083 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000084 diag::err_expected_ident_lbrace);
85 return DeclPtrTy();
Chris Lattner8f08cb72007-08-25 06:57:03 +000086 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattner51448322009-03-29 14:02:43 +000088 SourceLocation LBrace = ConsumeBrace();
89
Douglas Gregor23c94db2010-07-02 17:43:08 +000090 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
91 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
92 getCurScope()->getFnParent()) {
Douglas Gregor95f1b152010-05-14 05:08:22 +000093 Diag(LBrace, diag::err_namespace_nonnamespace_scope);
94 SkipUntil(tok::r_brace, false);
95 return DeclPtrTy();
96 }
97
Chris Lattner51448322009-03-29 14:02:43 +000098 // Enter a scope for the namespace.
99 ParseScope NamespaceScope(this, Scope::DeclScope);
100
101 DeclPtrTy NamespcDecl =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000102 Actions.ActOnStartNamespaceDef(getCurScope(), IdentLoc, Ident, LBrace,
Ted Kremenek1e377652010-02-11 02:19:13 +0000103 AttrList.get());
Chris Lattner51448322009-03-29 14:02:43 +0000104
105 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
106 PP.getSourceManager(),
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///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000129Parser::DeclPtrTy 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.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000144 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, 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);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000150 return DeclPtrTy();
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///
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000173Parser::DeclPtrTy Parser::ParseLinkage(ParsingDeclSpec &DS,
174 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)
181 return DeclPtrTy();
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);
Mike Stump1eb44332009-09-09 15:08:12 +0000186 DeclPtrTy 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);
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000200 ParseDeclarationOrFunctionDefinition(DS, Attr.AttrList);
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'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000225Parser::DeclPtrTy 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///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000261Parser::DeclPtrTy 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.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000277 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, 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?
Chris Lattnerb28317a2009-03-28 19:18:32 +0000288 return DeclPtrTy();
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///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000320Parser::DeclPtrTy 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.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000339 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, 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);
344 return DeclPtrTy();
345 }
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,
355 /*ObjectType=*/0,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000356 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000357 SkipUntil(tok::semi);
358 return DeclPtrTy();
359 }
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///
Chris Lattner97144fc2009-04-02 04:16:50 +0000381Parser::DeclPtrTy 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);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000387 return DeclPtrTy();
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
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000392 OwningExprResult AssertExpr(ParseConstantExpression());
393 if (AssertExpr.isInvalid()) {
394 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000395 return DeclPtrTy();
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))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000399 return DeclPtrTy();
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);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000404 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000407 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000408 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000409 return DeclPtrTy();
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
Mike Stump1eb44332009-09-09 15:08:12 +0000416 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000417 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000418}
419
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000420/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
421///
422/// 'decltype' ( expression )
423///
424void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
425 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
426
427 SourceLocation StartLoc = ConsumeToken();
428 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000429
430 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000431 "decltype")) {
432 SkipUntil(tok::r_paren);
433 return;
434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000436 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000438 // C++0x [dcl.type.simple]p4:
439 // The operand of the decltype specifier is an unevaluated operand.
440 EnterExpressionEvaluationContext Unevaluated(Actions,
441 Action::Unevaluated);
442 OwningExprResult Result = ParseExpression();
443 if (Result.isInvalid()) {
444 SkipUntil(tok::r_paren);
445 return;
446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000448 // Match the ')'
449 SourceLocation RParenLoc;
450 if (Tok.is(tok::r_paren))
451 RParenLoc = ConsumeParen();
452 else
453 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000455 if (RParenLoc.isInvalid())
456 return;
457
458 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000459 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000460 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000461 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000462 DiagID, Result.release()))
463 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000464}
465
Douglas Gregor42a552f2008-11-05 20:51:48 +0000466/// ParseClassName - Parse a C++ class-name, which names a class. Note
467/// that we only check that the result names a type; semantic analysis
468/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000469/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000470/// found.
471///
472/// class-name: [C++ 9.1]
473/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000474/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000475///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000476Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000477 CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000478 // Check whether we have a template-id that names a type.
479 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000480 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000481 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000482 if (TemplateId->Kind == TNK_Type_template ||
483 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000484 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000485
486 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
487 TypeTy *Type = Tok.getAnnotationValue();
488 EndLocation = Tok.getAnnotationEndLoc();
489 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000490
491 if (Type)
492 return Type;
493 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000494 }
495
496 // Fall through to produce an error below.
497 }
498
Douglas Gregor42a552f2008-11-05 20:51:48 +0000499 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000500 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000501 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000502 }
503
Douglas Gregor84d0a192010-01-12 21:28:44 +0000504 IdentifierInfo *Id = Tok.getIdentifierInfo();
505 SourceLocation IdLoc = ConsumeToken();
506
507 if (Tok.is(tok::less)) {
508 // It looks the user intended to write a template-id here, but the
509 // template-name was wrong. Try to fix that.
510 TemplateNameKind TNK = TNK_Type_template;
511 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000512 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor84d0a192010-01-12 21:28:44 +0000513 SS, Template, TNK)) {
514 Diag(IdLoc, diag::err_unknown_template_name)
515 << Id;
516 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000517
Douglas Gregor84d0a192010-01-12 21:28:44 +0000518 if (!Template)
519 return true;
520
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000521 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000522 UnqualifiedId TemplateName;
523 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000524
Douglas Gregor84d0a192010-01-12 21:28:44 +0000525 // Parse the full template-id, then turn it into a type.
526 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
527 SourceLocation(), true))
528 return true;
529 if (TNK == TNK_Dependent_template_name)
530 AnnotateTemplateIdTokenAsType(SS);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000531
Douglas Gregor84d0a192010-01-12 21:28:44 +0000532 // If we didn't end up with a typename token, there's nothing more we
533 // can do.
534 if (Tok.isNot(tok::annot_typename))
535 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000536
Douglas Gregor84d0a192010-01-12 21:28:44 +0000537 // Retrieve the type from the annotation token, consume that token, and
538 // return.
539 EndLocation = Tok.getAnnotationEndLoc();
540 TypeTy *Type = Tok.getAnnotationValue();
541 ConsumeToken();
542 return Type;
543 }
544
Douglas Gregor42a552f2008-11-05 20:51:48 +0000545 // We have an identifier; check whether it is actually a type.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000546 TypeTy *Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), SS, true);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000547 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000548 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000549 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000550 }
551
552 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000553 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000554
555 // Fake up a Declarator to use with ActOnTypeName.
556 DeclSpec DS;
557 DS.SetRangeStart(IdLoc);
558 DS.SetRangeEnd(EndLocation);
559 DS.getTypeSpecScope() = *SS;
560
561 const char *PrevSpec = 0;
562 unsigned DiagID;
563 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
564
565 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
566 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000567}
568
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000569/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
570/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
571/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000572/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000573///
574/// class-specifier: [C++ class]
575/// class-head '{' member-specification[opt] '}'
576/// class-head '{' member-specification[opt] '}' attributes[opt]
577/// class-head:
578/// class-key identifier[opt] base-clause[opt]
579/// class-key nested-name-specifier identifier base-clause[opt]
580/// class-key nested-name-specifier[opt] simple-template-id
581/// base-clause[opt]
582/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000583/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000584/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000585/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000586/// simple-template-id base-clause[opt]
587/// class-key:
588/// 'class'
589/// 'struct'
590/// 'union'
591///
592/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000593/// class-key ::[opt] nested-name-specifier[opt] identifier
594/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
595/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000596///
597/// Note that the C++ class-specifier and elaborated-type-specifier,
598/// together, subsume the C99 struct-or-union-specifier:
599///
600/// struct-or-union-specifier: [C99 6.7.2.1]
601/// struct-or-union identifier[opt] '{' struct-contents '}'
602/// struct-or-union identifier
603/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
604/// '}' attributes[opt]
605/// [GNU] struct-or-union attributes[opt] identifier
606/// struct-or-union:
607/// 'struct'
608/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000609void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
610 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000611 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redld9bafa72010-02-03 21:21:43 +0000612 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000613 DeclSpec::TST TagType;
614 if (TagTokKind == tok::kw_struct)
615 TagType = DeclSpec::TST_struct;
616 else if (TagTokKind == tok::kw_class)
617 TagType = DeclSpec::TST_class;
618 else {
619 assert(TagTokKind == tok::kw_union && "Not a class specifier");
620 TagType = DeclSpec::TST_union;
621 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000622
Douglas Gregor374929f2009-09-18 15:37:17 +0000623 if (Tok.is(tok::code_completion)) {
624 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000625 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregordc845342010-05-25 05:58:43 +0000626 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +0000627 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000628
Chandler Carruth926c4b42010-06-28 08:39:25 +0000629 // C++03 [temp.explicit] 14.7.2/8:
630 // The usual access checking rules do not apply to names used to specify
631 // explicit instantiations.
632 //
633 // As an extension we do not perform access checking on the names used to
634 // specify explicit specializations either. This is important to allow
635 // specializing traits classes for private types.
636 bool SuppressingAccessChecks = false;
637 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
638 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
639 Actions.ActOnStartSuppressingAccessChecks();
640 SuppressingAccessChecks = true;
641 }
642
Sean Huntbbd37c62009-11-21 08:43:09 +0000643 AttributeList *AttrList = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000644 // If attributes exist after tag, parse them.
645 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000646 AttrList = ParseGNUAttributes();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000647
Steve Narofff59e17e2008-12-24 20:59:21 +0000648 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +0000649 while (Tok.is(tok::kw___declspec))
Sean Huntbbd37c62009-11-21 08:43:09 +0000650 AttrList = ParseMicrosoftDeclSpec(AttrList);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000651
Sean Huntbbd37c62009-11-21 08:43:09 +0000652 // If C++0x attributes exist here, parse them.
653 // FIXME: Are we consistent with the ordering of parsing of different
654 // styles of attributes?
655 if (isCXX0XAttributeSpecifier())
656 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Douglas Gregorb117a602009-09-04 05:53:02 +0000658 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
659 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
660 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000661 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000662 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
663 // properly.
664 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
665 Tok.setKind(tok::identifier);
666 }
667
668 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
669 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
670 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000671 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000672 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
673 // properly.
674 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
675 Tok.setKind(tok::identifier);
676 }
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000678 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000679 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000680 if (getLang().CPlusPlus) {
681 // "FOO : BAR" is not a potential typo for "FOO::BAR".
682 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000683
John McCall207014e2010-07-30 06:26:29 +0000684 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
685 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +0000686 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +0000687 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
688 Diag(Tok, diag::err_expected_ident);
689 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000690
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000691 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
692
Douglas Gregorcc636682009-02-17 23:15:12 +0000693 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000694 IdentifierInfo *Name = 0;
695 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000696 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000697 if (Tok.is(tok::identifier)) {
698 Name = Tok.getIdentifierInfo();
699 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000700
Douglas Gregor5ee37342010-05-30 22:30:21 +0000701 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000702 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000703 // Eat the template argument list and try to continue parsing this as
704 // a class (or template thereof).
705 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000706 SourceLocation LAngleLoc, RAngleLoc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000707 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000708 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000709 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000710 // We couldn't parse the template argument list at all, so don't
711 // try to give any location information for the list.
712 LAngleLoc = RAngleLoc = SourceLocation();
713 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000714
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000715 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000716 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000717 << (TagType == DeclSpec::TST_class? 0
718 : TagType == DeclSpec::TST_struct? 1
719 : 2)
720 << Name
721 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000722
723 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000724 // we've removed its template argument list.
725 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
726 if (TemplateParams && TemplateParams->size() > 1) {
727 TemplateParams->pop_back();
728 } else {
729 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000730 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000731 = ParsedTemplateInfo::NonTemplate;
732 }
733 } else if (TemplateInfo.Kind
734 == ParsedTemplateInfo::ExplicitInstantiation) {
735 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000736 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000737 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000738 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000739 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000740 = SourceLocation();
741 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
742 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000743 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000744 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000745 } else if (Tok.is(tok::annot_template_id)) {
746 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
747 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000748
Douglas Gregorc45c2322009-03-31 00:43:58 +0000749 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000750 // The template-name in the simple-template-id refers to
751 // something other than a class template. Give an appropriate
752 // error message and skip to the ';'.
753 SourceRange Range(NameLoc);
754 if (SS.isNotEmpty())
755 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000756
Douglas Gregor39a8de12009-02-25 19:37:18 +0000757 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
758 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Douglas Gregor39a8de12009-02-25 19:37:18 +0000760 DS.SetTypeSpecError();
761 SkipUntil(tok::semi, false, true);
762 TemplateId->Destroy();
Chandler Carruth926c4b42010-06-28 08:39:25 +0000763 if (SuppressingAccessChecks)
764 Actions.ActOnStopSuppressingAccessChecks();
765
Douglas Gregor39a8de12009-02-25 19:37:18 +0000766 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000767 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000768 }
769
Chandler Carruth926c4b42010-06-28 08:39:25 +0000770 // As soon as we're finished parsing the class's template-id, turn access
771 // checking back on.
772 if (SuppressingAccessChecks)
773 Actions.ActOnStopSuppressingAccessChecks();
774
John McCall67d1a672009-08-06 02:15:43 +0000775 // There are four options here. If we have 'struct foo;', then this
776 // is either a forward declaration or a friend declaration, which
777 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000778 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000779 // something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000780 // However, in some contexts, things look like declarations but are just
781 // references, e.g.
782 // new struct s;
783 // or
784 // &T::operator struct s;
785 // For these, SuppressDeclarations is true.
John McCall0f434ec2009-07-31 02:45:11 +0000786 Action::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000787 if (SuppressDeclarations)
788 TUK = Action::TUK_Reference;
789 else if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))){
Douglas Gregord85bea22009-09-26 06:47:28 +0000790 if (DS.isFriendSpecified()) {
791 // C++ [class.friend]p2:
792 // A class shall not be defined in a friend declaration.
793 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
794 << SourceRange(DS.getFriendSpecLoc());
795
796 // Skip everything up to the semicolon, so that this looks like a proper
797 // friend class (or template thereof) declaration.
798 SkipUntil(tok::semi, true, true);
799 TUK = Action::TUK_Friend;
800 } else {
801 // Okay, this is a class definition.
802 TUK = Action::TUK_Definition;
803 }
804 } else if (Tok.is(tok::semi))
John McCall67d1a672009-08-06 02:15:43 +0000805 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000806 else
John McCall0f434ec2009-07-31 02:45:11 +0000807 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000808
John McCall207014e2010-07-30 06:26:29 +0000809 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
810 TUK != Action::TUK_Definition)) {
811 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
812 // We have a declaration or reference to an anonymous class.
813 Diag(StartLoc, diag::err_anon_type_definition)
814 << DeclSpec::getSpecifierName(TagType);
815 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000816
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000817 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000818
819 if (TemplateId)
820 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000821 return;
822 }
823
Douglas Gregorddc29e12009-02-06 22:42:48 +0000824 // Create the tag portion of the class or class template.
John McCallc4e70192009-09-11 04:59:25 +0000825 Action::DeclResult TagOrTempResult = true; // invalid
826 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000827
Douglas Gregor402abb52009-05-28 23:31:59 +0000828 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000829 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000830 // Explicit specialization, class template partial specialization,
831 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000832 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000833 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000834 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000835 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000836 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000837 // This is an explicit instantiation of a class template.
838 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000839 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000840 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000841 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000842 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000843 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000844 SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000845 TemplateTy::make(TemplateId->Template),
846 TemplateId->TemplateNameLoc,
847 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000848 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000849 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000850 AttrList);
John McCall74256f52010-04-14 00:24:33 +0000851
852 // Friend template-ids are treated as references unless
853 // they have template headers, in which case they're ill-formed
854 // (FIXME: "template <class T> friend class A<T>::B<int>;").
855 // We diagnose this error in ActOnClassTemplateSpecialization.
856 } else if (TUK == Action::TUK_Reference ||
857 (TUK == Action::TUK_Friend &&
858 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
John McCallc4e70192009-09-11 04:59:25 +0000859 TypeResult
John McCall6b2becf2009-09-08 17:47:29 +0000860 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
861 TemplateId->TemplateNameLoc,
862 TemplateId->LAngleLoc,
863 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000864 TemplateId->RAngleLoc);
865
John McCallc4e70192009-09-11 04:59:25 +0000866 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
867 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000868 } else {
869 // This is an explicit specialization or a class template
870 // partial specialization.
871 TemplateParameterLists FakedParamLists;
872
873 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
874 // This looks like an explicit instantiation, because we have
875 // something like
876 //
877 // template class Foo<X>
878 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000879 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000880 // meant to be an explicit specialization, but the user forgot
881 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000882 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000883
Mike Stump1eb44332009-09-09 15:08:12 +0000884 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000885 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000886 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000887 diag::err_explicit_instantiation_with_definition)
888 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +0000889 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000890
891 // Create a fake template parameter list that contains only
892 // "template<>", so that we treat this construct as a class
893 // template specialization.
894 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000895 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000896 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000897 LAngleLoc,
898 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000899 LAngleLoc));
900 TemplateParams = &FakedParamLists;
901 }
902
903 // Build the class template specialization.
904 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000905 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000906 StartLoc, SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000907 TemplateTy::make(TemplateId->Template),
908 TemplateId->TemplateNameLoc,
909 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000910 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000911 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000912 AttrList,
Mike Stump1eb44332009-09-09 15:08:12 +0000913 Action::MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000914 TemplateParams? &(*TemplateParams)[0] : 0,
915 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000916 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000917 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000918 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000919 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000920 // Explicit instantiation of a member of a class template
921 // specialization, e.g.,
922 //
923 // template struct Outer<int>::Inner;
924 //
925 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000926 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000927 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000928 TemplateInfo.TemplateLoc,
929 TagType, StartLoc, SS, Name,
Sean Huntbbd37c62009-11-21 08:43:09 +0000930 NameLoc, AttrList);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000931 } else {
932 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000933 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000934 // FIXME: Diagnose this particular error.
935 }
936
John McCallc4e70192009-09-11 04:59:25 +0000937 bool IsDependent = false;
938
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000939 // Declaration or definition of a class type
Douglas Gregor23c94db2010-07-02 17:43:08 +0000940 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000941 Name, NameLoc, AttrList, AS,
Mike Stump1eb44332009-09-09 15:08:12 +0000942 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000943 TemplateParams? &(*TemplateParams)[0] : 0,
944 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000945 Owned, IsDependent);
946
947 // If ActOnTag said the type was dependent, try again with the
948 // less common call.
949 if (IsDependent)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000950 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000951 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000952 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000953
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000954 // If there is a body, parse it and inform the actions module.
John McCallbd0dfa52009-12-19 21:48:58 +0000955 if (TUK == Action::TUK_Definition) {
956 assert(Tok.is(tok::l_brace) ||
957 (getLang().CPlusPlus && Tok.is(tok::colon)));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000958 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000959 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000960 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000961 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000962 }
963
John McCallc4e70192009-09-11 04:59:25 +0000964 void *Result;
965 if (!TypeResult.isInvalid()) {
966 TagType = DeclSpec::TST_typename;
967 Result = TypeResult.get();
968 Owned = false;
969 } else if (!TagOrTempResult.isInvalid()) {
970 Result = TagOrTempResult.get().getAs<void>();
971 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000972 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000973 return;
974 }
Mike Stump1eb44332009-09-09 15:08:12 +0000975
John McCallfec54012009-08-03 20:12:06 +0000976 const char *PrevSpec = 0;
977 unsigned DiagID;
John McCallc4e70192009-09-11 04:59:25 +0000978
Douglas Gregorb988f9c2010-01-25 16:33:23 +0000979 // FIXME: The DeclSpec should keep the locations of both the keyword and the
980 // name (if there is one).
981 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000982
Douglas Gregorb988f9c2010-01-25 16:33:23 +0000983 if (DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID,
John McCallc4e70192009-09-11 04:59:25 +0000984 Result, Owned))
John McCallfec54012009-08-03 20:12:06 +0000985 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000986
Chris Lattner4ed5d912010-02-02 01:23:29 +0000987 // At this point, we've successfully parsed a class-specifier in 'definition'
988 // form (e.g. "struct foo { int x; }". While we could just return here, we're
989 // going to look at what comes after it to improve error recovery. If an
990 // impossible token occurs next, we assume that the programmer forgot a ; at
991 // the end of the declaration and recover that way.
992 //
993 // This switch enumerates the valid "follow" set for definition.
994 if (TUK == Action::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000995 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000996 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000997 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000998 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +0000999 case tok::star: // struct foo {...} * P;
1000 case tok::amp: // struct foo {...} & R = ...
1001 case tok::identifier: // struct foo {...} V ;
1002 case tok::r_paren: //(struct foo {...} ) {4}
1003 case tok::annot_cxxscope: // struct foo {...} a:: b;
1004 case tok::annot_typename: // struct foo {...} a ::b;
1005 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +00001006 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +00001007 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001008 ExpectedSemi = false;
1009 break;
1010 // Type qualifiers
1011 case tok::kw_const: // struct foo {...} const x;
1012 case tok::kw_volatile: // struct foo {...} volatile x;
1013 case tok::kw_restrict: // struct foo {...} restrict x;
1014 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +00001015 // Storage-class specifiers
1016 case tok::kw_static: // struct foo {...} static x;
1017 case tok::kw_extern: // struct foo {...} extern x;
1018 case tok::kw_typedef: // struct foo {...} typedef x;
1019 case tok::kw_register: // struct foo {...} register x;
1020 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregor33f99242010-05-17 18:19:56 +00001021 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001022 // As shown above, type qualifiers and storage class specifiers absolutely
1023 // can occur after class specifiers according to the grammar. However,
1024 // almost noone actually writes code like this. If we see one of these,
1025 // it is much more likely that someone missed a semi colon and the
1026 // type/storage class specifier we're seeing is part of the *next*
1027 // intended declaration, as in:
1028 //
1029 // struct foo { ... }
1030 // typedef int X;
1031 //
1032 // We'd really like to emit a missing semicolon error instead of emitting
1033 // an error on the 'int' saying that you can't have two type specifiers in
1034 // the same declaration of X. Because of this, we look ahead past this
1035 // token to see if it's a type specifier. If so, we know the code is
1036 // otherwise invalid, so we can produce the expected semi error.
1037 if (!isKnownToBeTypeSpecifier(NextToken()))
1038 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001039 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001040
1041 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001042 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001043 if (!getLang().CPlusPlus)
1044 ExpectedSemi = false;
1045 break;
1046 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001047
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001048 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001049 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1050 TagType == DeclSpec::TST_class ? "class"
1051 : TagType == DeclSpec::TST_struct? "struct" : "union");
1052 // Push this token back into the preprocessor and change our current token
1053 // to ';' so that the rest of the code recovers as though there were an
1054 // ';' after the definition.
1055 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001056 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001057 }
1058 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001059}
1060
Mike Stump1eb44332009-09-09 15:08:12 +00001061/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001062///
1063/// base-clause : [C++ class.derived]
1064/// ':' base-specifier-list
1065/// base-specifier-list:
1066/// base-specifier '...'[opt]
1067/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +00001068void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001069 assert(Tok.is(tok::colon) && "Not a base clause");
1070 ConsumeToken();
1071
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001072 // Build up an array of parsed base specifiers.
1073 llvm::SmallVector<BaseTy *, 8> BaseInfo;
1074
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001075 while (true) {
1076 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001077 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001078 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001079 // Skip the rest of this base specifier, up until the comma or
1080 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001081 SkipUntil(tok::comma, tok::l_brace, true, true);
1082 } else {
1083 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001084 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001085 }
1086
1087 // If the next token is a comma, consume it and keep reading
1088 // base-specifiers.
1089 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001091 // Consume the comma.
1092 ConsumeToken();
1093 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001094
1095 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001096 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001097}
1098
1099/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1100/// one entry in the base class list of a class specifier, for example:
1101/// class foo : public bar, virtual private baz {
1102/// 'public bar' and 'virtual private baz' are each base-specifiers.
1103///
1104/// base-specifier: [C++ class.derived]
1105/// ::[opt] nested-name-specifier[opt] class-name
1106/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1107/// class-name
1108/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1109/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +00001110Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001111 bool IsVirtual = false;
1112 SourceLocation StartLoc = Tok.getLocation();
1113
1114 // Parse the 'virtual' keyword.
1115 if (Tok.is(tok::kw_virtual)) {
1116 ConsumeToken();
1117 IsVirtual = true;
1118 }
1119
1120 // Parse an (optional) access specifier.
1121 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001122 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001123 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001125 // Parse the 'virtual' keyword (again!), in case it came after the
1126 // access specifier.
1127 if (Tok.is(tok::kw_virtual)) {
1128 SourceLocation VirtualLoc = ConsumeToken();
1129 if (IsVirtual) {
1130 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001131 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001132 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001133 }
1134
1135 IsVirtual = true;
1136 }
1137
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001138 // Parse optional '::' and optional nested-name-specifier.
1139 CXXScopeSpec SS;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001140 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0,
Douglas Gregord2b43bf2010-03-02 00:25:00 +00001141 /*EnteringContext=*/false);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001142
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001143 // The location of the base class itself.
1144 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001145
1146 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001147 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +00001148 TypeResult BaseType = ParseClassName(EndLocation, &SS);
1149 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001150 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001151
1152 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001153 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001155 // Notify semantic analysis that we have parsed a complete
1156 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001157 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +00001158 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001159}
1160
1161/// getAccessSpecifierIfPresent - Determine whether the next token is
1162/// a C++ access-specifier.
1163///
1164/// access-specifier: [C++ class.derived]
1165/// 'private'
1166/// 'protected'
1167/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001168AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001169 switch (Tok.getKind()) {
1170 default: return AS_none;
1171 case tok::kw_private: return AS_private;
1172 case tok::kw_protected: return AS_protected;
1173 case tok::kw_public: return AS_public;
1174 }
1175}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001176
Eli Friedmand33133c2009-07-22 21:45:50 +00001177void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
1178 DeclPtrTy ThisDecl) {
1179 // We just declared a member function. If this member function
1180 // has any default arguments, we'll need to parse them later.
1181 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001182 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +00001183 = DeclaratorInfo.getTypeObject(0).Fun;
1184 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1185 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1186 if (!LateMethod) {
1187 // Push this method onto the stack of late-parsed method
1188 // declarations.
1189 getCurrentClass().MethodDecls.push_back(
1190 LateParsedMethodDeclaration(ThisDecl));
1191 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001192 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001193
1194 // Add all of the parameters prior to this one (they don't
1195 // have default arguments).
1196 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1197 for (unsigned I = 0; I < ParamIdx; ++I)
1198 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001199 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001200 }
1201
1202 // Add this parameter to the list of parameters (it or may
1203 // not have a default argument).
1204 LateMethod->DefaultArgs.push_back(
1205 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1206 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1207 }
1208 }
1209}
1210
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001211/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1212///
1213/// member-declaration:
1214/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1215/// function-definition ';'[opt]
1216/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1217/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001218/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001219/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001220/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001221///
1222/// member-declarator-list:
1223/// member-declarator
1224/// member-declarator-list ',' member-declarator
1225///
1226/// member-declarator:
1227/// declarator pure-specifier[opt]
1228/// declarator constant-initializer[opt]
1229/// identifier[opt] ':' constant-expression
1230///
Sebastian Redle2b68332009-04-12 17:16:29 +00001231/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001232/// '= 0'
1233///
1234/// constant-initializer:
1235/// '=' constant-expression
1236///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001237void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCallc9068d72010-07-16 08:13:16 +00001238 const ParsedTemplateInfo &TemplateInfo,
1239 ParsingDeclRAIIObject *TemplateDiags) {
John McCall60fa3cf2009-12-11 02:10:03 +00001240 // Access declarations.
1241 if (!TemplateInfo.Kind &&
1242 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001243 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001244 Tok.is(tok::annot_cxxscope)) {
1245 bool isAccessDecl = false;
1246 if (NextToken().is(tok::identifier))
1247 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1248 else
1249 isAccessDecl = NextToken().is(tok::kw_operator);
1250
1251 if (isAccessDecl) {
1252 // Collect the scope specifier token we annotated earlier.
1253 CXXScopeSpec SS;
1254 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType*/ 0, false);
1255
1256 // Try to parse an unqualified-id.
1257 UnqualifiedId Name;
1258 if (ParseUnqualifiedId(SS, false, true, true, /*ObjectType*/ 0, Name)) {
1259 SkipUntil(tok::semi);
1260 return;
1261 }
1262
1263 // TODO: recover from mistakenly-qualified operator declarations.
1264 if (ExpectAndConsume(tok::semi,
1265 diag::err_expected_semi_after,
1266 "access declaration",
1267 tok::semi))
1268 return;
1269
Douglas Gregor23c94db2010-07-02 17:43:08 +00001270 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001271 false, SourceLocation(),
1272 SS, Name,
1273 /* AttrList */ 0,
1274 /* IsTypeName */ false,
1275 SourceLocation());
1276 return;
1277 }
1278 }
1279
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001280 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001281 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001282 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001283 SourceLocation DeclEnd;
1284 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001285 return;
1286 }
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Chris Lattner682bf922009-03-29 16:50:03 +00001288 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001289 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001290 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001291 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001292 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001293 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001294 return;
1295 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001296
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001297 // Handle: member-declaration ::= '__extension__' member-declaration
1298 if (Tok.is(tok::kw___extension__)) {
1299 // __extension__ silences extension warnings in the subexpression.
1300 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1301 ConsumeToken();
John McCallc9068d72010-07-16 08:13:16 +00001302 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001303 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001304
Chris Lattner4ed5d912010-02-02 01:23:29 +00001305 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1306 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001307 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001308
Sean Huntbbd37c62009-11-21 08:43:09 +00001309 CXX0XAttributeList AttrList;
1310 // Optional C++0x attribute-specifier
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001311 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Sean Huntbbd37c62009-11-21 08:43:09 +00001312 AttrList = ParseCXX0XAttributes();
Sean Huntbbd37c62009-11-21 08:43:09 +00001313
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001314 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001315 // FIXME: Check for template aliases
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001316
Sean Huntbbd37c62009-11-21 08:43:09 +00001317 if (AttrList.HasAttr)
1318 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1319 << AttrList.Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001321 // Eat 'using'.
1322 SourceLocation UsingLoc = ConsumeToken();
1323
1324 if (Tok.is(tok::kw_namespace)) {
1325 Diag(UsingLoc, diag::err_using_namespace_in_class);
1326 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001327 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001328 SourceLocation DeclEnd;
1329 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001330 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001331 }
1332 return;
1333 }
1334
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001335 SourceLocation DSStart = Tok.getLocation();
1336 // decl-specifier-seq:
1337 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001338 ParsingDeclSpec DS(*this, TemplateDiags);
Sean Huntbbd37c62009-11-21 08:43:09 +00001339 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001340 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001341
John McCalldd4a3b02009-09-16 22:47:08 +00001342 Action::MultiTemplateParamsArg TemplateParams(Actions,
1343 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1344 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1345
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001346 if (Tok.is(tok::semi)) {
1347 ConsumeToken();
John McCallc9068d72010-07-16 08:13:16 +00001348 DeclPtrTy TheDecl =
1349 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
1350 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001351 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001352 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001353
John McCall54abf7d2009-11-04 02:18:39 +00001354 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001355
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001356 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001357 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1358 ColonProtectionRAIIObject X(*this);
1359
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001360 // Parse the first declarator.
1361 ParseDeclarator(DeclaratorInfo);
1362 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001363 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001364 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001365 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001366 if (Tok.is(tok::semi))
1367 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001368 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001369 }
1370
John Thompson1b2fc0f2009-11-25 22:58:06 +00001371 // If attributes exist after the declarator, but before an '{', parse them.
1372 if (Tok.is(tok::kw___attribute)) {
1373 SourceLocation Loc;
1374 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1375 DeclaratorInfo.AddAttributes(AttrList, Loc);
1376 }
1377
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001378 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001379 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001380 || (DeclaratorInfo.isFunctionDeclarator() &&
1381 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001382 if (!DeclaratorInfo.isFunctionDeclarator()) {
1383 Diag(Tok, diag::err_func_def_no_params);
1384 ConsumeBrace();
1385 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001386 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001387 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001388
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001389 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1390 Diag(Tok, diag::err_function_declared_typedef);
1391 // This recovery skips the entire function body. It would be nice
1392 // to simply call ParseCXXInlineMethodDef() below, however Sema
1393 // assumes the declarator represents a function, not a typedef.
1394 ConsumeBrace();
1395 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001396 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001397 }
1398
Douglas Gregor37b372b2009-08-20 22:52:58 +00001399 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001400 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001401 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001402 }
1403
1404 // member-declarator-list:
1405 // member-declarator
1406 // member-declarator-list ',' member-declarator
1407
Chris Lattner682bf922009-03-29 16:50:03 +00001408 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001409 OwningExprResult BitfieldSize(Actions);
1410 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001411 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001412
1413 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001414 // member-declarator:
1415 // declarator pure-specifier[opt]
1416 // declarator constant-initializer[opt]
1417 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001418 if (Tok.is(tok::colon)) {
1419 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001420 BitfieldSize = ParseConstantExpression();
1421 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001422 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001423 }
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001425 // pure-specifier:
1426 // '= 0'
1427 //
1428 // constant-initializer:
1429 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001430 //
1431 // defaulted/deleted function-definition:
1432 // '=' 'default' [TODO]
1433 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001434 if (Tok.is(tok::equal)) {
1435 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001436 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1437 ConsumeToken();
1438 Deleted = true;
1439 } else {
1440 Init = ParseInitializer();
1441 if (Init.isInvalid())
1442 SkipUntil(tok::comma, true, true);
1443 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001444 }
1445
Chris Lattnere6563252010-06-13 05:34:18 +00001446 // If a simple-asm-expr is present, parse it.
1447 if (Tok.is(tok::kw_asm)) {
1448 SourceLocation Loc;
1449 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
1450 if (AsmLabel.isInvalid())
1451 SkipUntil(tok::comma, true, true);
1452
1453 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1454 DeclaratorInfo.SetRangeEnd(Loc);
1455 }
1456
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001457 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001458 if (Tok.is(tok::kw___attribute)) {
1459 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001460 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001461 DeclaratorInfo.AddAttributes(AttrList, Loc);
1462 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001463
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001464 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001465 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001466 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001467
1468 DeclPtrTy ThisDecl;
1469 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001470 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00001471 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCallbbbcdd92009-09-11 21:02:39 +00001472 /*IsDefinition*/ false,
1473 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001474 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001475 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00001476 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001477 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001478 BitfieldSize.release(),
1479 Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001480 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001481 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001482 }
Chris Lattner682bf922009-03-29 16:50:03 +00001483 if (ThisDecl)
1484 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001485
Douglas Gregor72b505b2008-12-16 21:30:33 +00001486 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001487 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001488 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001489 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001490 }
1491
John McCall54abf7d2009-11-04 02:18:39 +00001492 DeclaratorInfo.complete(ThisDecl);
1493
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001494 // If we don't have a comma, it is either the end of the list (a ';')
1495 // or an error, bail out.
1496 if (Tok.isNot(tok::comma))
1497 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001499 // Consume the comma.
1500 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001502 // Parse the next declarator.
1503 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001504 BitfieldSize = 0;
1505 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001506 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001508 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001509 if (Tok.is(tok::kw___attribute)) {
1510 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001511 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001512 DeclaratorInfo.AddAttributes(AttrList, Loc);
1513 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001514
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001515 if (Tok.isNot(tok::colon))
1516 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001517 }
1518
Chris Lattnerae50d502010-02-02 00:43:15 +00001519 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1520 // Skip to end of block or statement.
1521 SkipUntil(tok::r_brace, true, true);
1522 // If we stopped at a ';', eat it.
1523 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001524 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001525 }
1526
Douglas Gregor23c94db2010-07-02 17:43:08 +00001527 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00001528 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001529}
1530
1531/// ParseCXXMemberSpecification - Parse the class definition.
1532///
1533/// member-specification:
1534/// member-declaration member-specification[opt]
1535/// access-specifier ':' member-specification[opt]
1536///
1537void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001538 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001539 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001540 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001541 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001542
Chris Lattner49f28ca2009-03-05 08:00:35 +00001543 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1544 PP.getSourceManager(),
1545 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Douglas Gregor26997fd2010-01-16 20:52:59 +00001547 // Determine whether this is a non-nested class. Note that local
1548 // classes are *not* considered to be nested classes.
1549 bool NonNestedClass = true;
1550 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001551 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001552 if (S->isClassScope()) {
1553 // We're inside a class scope, so this is a nested class.
1554 NonNestedClass = false;
1555 break;
1556 }
1557
1558 if ((S->getFlags() & Scope::FnScope)) {
1559 // If we're in a function or function template declared in the
1560 // body of a class, then this is a local class rather than a
1561 // nested class.
1562 const Scope *Parent = S->getParent();
1563 if (Parent->isTemplateParamScope())
1564 Parent = Parent->getParent();
1565 if (Parent->isClassScope())
1566 break;
1567 }
1568 }
1569 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001570
1571 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001572 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001573
Douglas Gregor6569d682009-05-27 23:11:45 +00001574 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001575 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001576
Douglas Gregorddc29e12009-02-06 22:42:48 +00001577 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001578 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001579
1580 if (Tok.is(tok::colon)) {
1581 ParseBaseClause(TagDecl);
1582
1583 if (!Tok.is(tok::l_brace)) {
1584 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00001585
1586 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001587 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001588 return;
1589 }
1590 }
1591
1592 assert(Tok.is(tok::l_brace));
1593
1594 SourceLocation LBraceLoc = ConsumeBrace();
1595
John McCall42a4f662010-05-28 08:11:17 +00001596 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001597 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, LBraceLoc);
John McCallf9368152009-12-20 07:58:13 +00001598
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001599 // C++ 11p3: Members of a class defined with the keyword class are private
1600 // by default. Members of a class defined with the keywords struct or union
1601 // are public by default.
1602 AccessSpecifier CurAS;
1603 if (TagType == DeclSpec::TST_class)
1604 CurAS = AS_private;
1605 else
1606 CurAS = AS_public;
1607
Douglas Gregor07976d22010-06-21 22:31:09 +00001608 SourceLocation RBraceLoc;
1609 if (TagDecl) {
1610 // While we still have something to read, read the member-declarations.
1611 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1612 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Douglas Gregor07976d22010-06-21 22:31:09 +00001614 // Check for extraneous top-level semicolon.
1615 if (Tok.is(tok::semi)) {
1616 Diag(Tok, diag::ext_extra_struct_semi)
1617 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1618 << FixItHint::CreateRemoval(Tok.getLocation());
1619 ConsumeToken();
1620 continue;
1621 }
1622
1623 AccessSpecifier AS = getAccessSpecifierIfPresent();
1624 if (AS != AS_none) {
1625 // Current token is a C++ access specifier.
1626 CurAS = AS;
1627 SourceLocation ASLoc = Tok.getLocation();
1628 ConsumeToken();
1629 if (Tok.is(tok::colon))
1630 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1631 else
1632 Diag(Tok, diag::err_expected_colon);
1633 ConsumeToken();
1634 continue;
1635 }
1636
1637 // FIXME: Make sure we don't have a template here.
1638
1639 // Parse all the comma separated declarators.
1640 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001641 }
1642
Douglas Gregor07976d22010-06-21 22:31:09 +00001643 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1644 } else {
1645 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001646 }
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001648 // If attributes exist after class contents, parse them.
Ted Kremenek1e377652010-02-11 02:19:13 +00001649 llvm::OwningPtr<AttributeList> AttrList;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001650 if (Tok.is(tok::kw___attribute))
Douglas Gregor0b4c9b52010-03-29 14:42:08 +00001651 AttrList.reset(ParseGNUAttributes());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001652
John McCall42a4f662010-05-28 08:11:17 +00001653 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001654 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall42a4f662010-05-28 08:11:17 +00001655 LBraceLoc, RBraceLoc,
1656 AttrList.get());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001657
1658 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1659 // complete within function bodies, default arguments,
1660 // exception-specifications, and constructor ctor-initializers (including
1661 // such things in nested classes).
1662 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001663 // FIXME: Only function bodies and constructor ctor-initializers are
1664 // parsed correctly, fix the rest.
Douglas Gregor07976d22010-06-21 22:31:09 +00001665 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001666 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001667 // are complete and we can parse the delayed portions of method
1668 // declarations and the lexed inline method definitions.
Douglas Gregore0cc0472010-06-16 23:45:56 +00001669 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregor6569d682009-05-27 23:11:45 +00001670 ParseLexedMethodDeclarations(getCurrentClass());
1671 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00001672 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001673 }
1674
John McCall42a4f662010-05-28 08:11:17 +00001675 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001676 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCalldb7bb4a2010-03-17 00:38:33 +00001677
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001678 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001679 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001680 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001681}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001682
1683/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1684/// which explicitly initializes the members or base classes of a
1685/// class (C++ [class.base.init]). For example, the three initializers
1686/// after the ':' in the Derived constructor below:
1687///
1688/// @code
1689/// class Base { };
1690/// class Derived : Base {
1691/// int x;
1692/// float f;
1693/// public:
1694/// Derived(float f) : Base(), x(17), f(f) { }
1695/// };
1696/// @endcode
1697///
Mike Stump1eb44332009-09-09 15:08:12 +00001698/// [C++] ctor-initializer:
1699/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001700///
Mike Stump1eb44332009-09-09 15:08:12 +00001701/// [C++] mem-initializer-list:
1702/// mem-initializer
1703/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001704void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001705 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1706
1707 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Douglas Gregor7ad83902008-11-05 04:29:56 +00001709 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001710 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001711
Douglas Gregor7ad83902008-11-05 04:29:56 +00001712 do {
1713 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001714 if (!MemInit.isInvalid())
1715 MemInitializers.push_back(MemInit.get());
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001716 else
1717 AnyErrors = true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001718
Douglas Gregor7ad83902008-11-05 04:29:56 +00001719 if (Tok.is(tok::comma))
1720 ConsumeToken();
1721 else if (Tok.is(tok::l_brace))
1722 break;
1723 else {
1724 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001725 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001726 SkipUntil(tok::l_brace, true, true);
1727 break;
1728 }
1729 } while (true);
1730
Mike Stump1eb44332009-09-09 15:08:12 +00001731 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001732 MemInitializers.data(), MemInitializers.size(),
1733 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001734}
1735
1736/// ParseMemInitializer - Parse a C++ member initializer, which is
1737/// part of a constructor initializer that explicitly initializes one
1738/// member or base class (C++ [class.base.init]). See
1739/// ParseConstructorInitializer for an example.
1740///
1741/// [C++] mem-initializer:
1742/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001743///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001744/// [C++] mem-initializer-id:
1745/// '::'[opt] nested-name-specifier[opt] class-name
1746/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001747Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001748 // parse '::'[opt] nested-name-specifier[opt]
1749 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001750 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001751 TypeTy *TemplateTypeTy = 0;
1752 if (Tok.is(tok::annot_template_id)) {
1753 TemplateIdAnnotation *TemplateId
1754 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001755 if (TemplateId->Kind == TNK_Type_template ||
1756 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanian96174332009-07-01 19:21:19 +00001757 AnnotateTemplateIdTokenAsType(&SS);
1758 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1759 TemplateTypeTy = Tok.getAnnotationValue();
1760 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001761 }
1762 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001763 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001764 return true;
1765 }
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Douglas Gregor7ad83902008-11-05 04:29:56 +00001767 // Get the identifier. This may be a member name or a class name,
1768 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001769 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001770 SourceLocation IdLoc = ConsumeToken();
1771
1772 // Parse the '('.
1773 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001774 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001775 return true;
1776 }
1777 SourceLocation LParenLoc = ConsumeParen();
1778
1779 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001780 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001781 CommaLocsTy CommaLocs;
1782 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1783 SkipUntil(tok::r_paren);
1784 return true;
1785 }
1786
1787 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1788
Douglas Gregor23c94db2010-07-02 17:43:08 +00001789 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanian96174332009-07-01 19:21:19 +00001790 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001791 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001792 ArgExprs.size(), CommaLocs.data(),
1793 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001794}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001795
1796/// ParseExceptionSpecification - Parse a C++ exception-specification
1797/// (C++ [except.spec]).
1798///
Douglas Gregora4745612008-12-01 18:00:20 +00001799/// exception-specification:
1800/// 'throw' '(' type-id-list [opt] ')'
1801/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001802///
Douglas Gregora4745612008-12-01 18:00:20 +00001803/// type-id-list:
1804/// type-id
1805/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001806///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001807bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001808 llvm::SmallVector<TypeTy*, 2>
1809 &Exceptions,
1810 llvm::SmallVector<SourceRange, 2>
1811 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001812 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001813 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001814
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001815 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001816
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001817 if (!Tok.is(tok::l_paren)) {
1818 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1819 }
1820 SourceLocation LParenLoc = ConsumeParen();
1821
Douglas Gregora4745612008-12-01 18:00:20 +00001822 // Parse throw(...), a Microsoft extension that means "this function
1823 // can throw anything".
1824 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001825 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001826 SourceLocation EllipsisLoc = ConsumeToken();
1827 if (!getLang().Microsoft)
1828 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001829 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001830 return false;
1831 }
1832
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001833 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001834 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001835 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001836 TypeResult Res(ParseTypeName(&Range));
1837 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001838 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001839 Ranges.push_back(Range);
1840 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001841 if (Tok.is(tok::comma))
1842 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001843 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001844 break;
1845 }
1846
Sebastian Redlab197ba2009-02-09 18:23:29 +00001847 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001848 return false;
1849}
Douglas Gregor6569d682009-05-27 23:11:45 +00001850
1851/// \brief We have just started parsing the definition of a new class,
1852/// so push that class onto our stack of classes that is currently
1853/// being parsed.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001854void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool NonNestedClass) {
1855 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001856 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00001857 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
Douglas Gregor6569d682009-05-27 23:11:45 +00001858}
1859
1860/// \brief Deallocate the given parsed class and all of its nested
1861/// classes.
1862void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1863 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1864 DeallocateParsedClasses(Class->NestedClasses[I]);
1865 delete Class;
1866}
1867
1868/// \brief Pop the top class of the stack of classes that are
1869/// currently being parsed.
1870///
1871/// This routine should be called when we have finished parsing the
1872/// definition of a class, but have not yet popped the Scope
1873/// associated with the class's definition.
1874///
1875/// \returns true if the class we've popped is a top-level class,
1876/// false otherwise.
1877void Parser::PopParsingClass() {
1878 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001879
Douglas Gregor6569d682009-05-27 23:11:45 +00001880 ParsingClass *Victim = ClassStack.top();
1881 ClassStack.pop();
1882 if (Victim->TopLevelClass) {
1883 // Deallocate all of the nested classes of this class,
1884 // recursively: we don't need to keep any of this information.
1885 DeallocateParsedClasses(Victim);
1886 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001887 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001888 assert(!ClassStack.empty() && "Missing top-level class?");
1889
1890 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1891 Victim->NestedClasses.empty()) {
1892 // The victim is a nested class, but we will not need to perform
1893 // any processing after the definition of this class since it has
1894 // no members whose handling was delayed. Therefore, we can just
1895 // remove this nested class.
1896 delete Victim;
1897 return;
1898 }
1899
1900 // This nested class has some members that will need to be processed
1901 // after the top-level class is completely defined. Therefore, add
1902 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001903 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregor6569d682009-05-27 23:11:45 +00001904 ClassStack.top()->NestedClasses.push_back(Victim);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001905 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00001906}
Sean Huntbbd37c62009-11-21 08:43:09 +00001907
1908/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1909/// parses standard attributes.
1910///
1911/// [C++0x] attribute-specifier:
1912/// '[' '[' attribute-list ']' ']'
1913///
1914/// [C++0x] attribute-list:
1915/// attribute[opt]
1916/// attribute-list ',' attribute[opt]
1917///
1918/// [C++0x] attribute:
1919/// attribute-token attribute-argument-clause[opt]
1920///
1921/// [C++0x] attribute-token:
1922/// identifier
1923/// attribute-scoped-token
1924///
1925/// [C++0x] attribute-scoped-token:
1926/// attribute-namespace '::' identifier
1927///
1928/// [C++0x] attribute-namespace:
1929/// identifier
1930///
1931/// [C++0x] attribute-argument-clause:
1932/// '(' balanced-token-seq ')'
1933///
1934/// [C++0x] balanced-token-seq:
1935/// balanced-token
1936/// balanced-token-seq balanced-token
1937///
1938/// [C++0x] balanced-token:
1939/// '(' balanced-token-seq ')'
1940/// '[' balanced-token-seq ']'
1941/// '{' balanced-token-seq '}'
1942/// any token but '(', ')', '[', ']', '{', or '}'
1943CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1944 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1945 && "Not a C++0x attribute list");
1946
1947 SourceLocation StartLoc = Tok.getLocation(), Loc;
1948 AttributeList *CurrAttr = 0;
1949
1950 ConsumeBracket();
1951 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001952
Sean Huntbbd37c62009-11-21 08:43:09 +00001953 if (Tok.is(tok::comma)) {
1954 Diag(Tok.getLocation(), diag::err_expected_ident);
1955 ConsumeToken();
1956 }
1957
1958 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1959 // attribute not present
1960 if (Tok.is(tok::comma)) {
1961 ConsumeToken();
1962 continue;
1963 }
1964
1965 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1966 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001967
Sean Huntbbd37c62009-11-21 08:43:09 +00001968 // scoped attribute
1969 if (Tok.is(tok::coloncolon)) {
1970 ConsumeToken();
1971
1972 if (!Tok.is(tok::identifier)) {
1973 Diag(Tok.getLocation(), diag::err_expected_ident);
1974 SkipUntil(tok::r_square, tok::comma, true, true);
1975 continue;
1976 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001977
Sean Huntbbd37c62009-11-21 08:43:09 +00001978 ScopeName = AttrName;
1979 ScopeLoc = AttrLoc;
1980
1981 AttrName = Tok.getIdentifierInfo();
1982 AttrLoc = ConsumeToken();
1983 }
1984
1985 bool AttrParsed = false;
1986 // No scoped names are supported; ideally we could put all non-standard
1987 // attributes into namespaces.
1988 if (!ScopeName) {
1989 switch(AttributeList::getKind(AttrName))
1990 {
1991 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00001992 case AttributeList::AT_base_check:
1993 case AttributeList::AT_carries_dependency:
Sean Huntbbd37c62009-11-21 08:43:09 +00001994 case AttributeList::AT_final:
Sean Hunt7725e672009-11-25 04:20:27 +00001995 case AttributeList::AT_hiding:
1996 case AttributeList::AT_noreturn:
1997 case AttributeList::AT_override: {
Sean Huntbbd37c62009-11-21 08:43:09 +00001998 if (Tok.is(tok::l_paren)) {
1999 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
2000 << AttrName->getName();
2001 break;
2002 }
2003
2004 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
2005 SourceLocation(), 0, 0, CurrAttr, false,
2006 true);
2007 AttrParsed = true;
2008 break;
2009 }
2010
2011 // One argument; must be a type-id or assignment-expression
2012 case AttributeList::AT_aligned: {
2013 if (Tok.isNot(tok::l_paren)) {
2014 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
2015 << AttrName->getName();
2016 break;
2017 }
2018 SourceLocation ParamLoc = ConsumeParen();
2019
2020 OwningExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
2021
2022 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2023
2024 ExprVector ArgExprs(Actions);
2025 ArgExprs.push_back(ArgExpr.release());
2026 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
2027 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
2028 false, true);
2029
2030 AttrParsed = true;
2031 break;
2032 }
2033
2034 // Silence warnings
2035 default: break;
2036 }
2037 }
2038
2039 // Skip the entire parameter clause, if any
2040 if (!AttrParsed && Tok.is(tok::l_paren)) {
2041 ConsumeParen();
2042 // SkipUntil maintains the balancedness of tokens.
2043 SkipUntil(tok::r_paren, false);
2044 }
2045 }
2046
2047 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2048 SkipUntil(tok::r_square, false);
2049 Loc = Tok.getLocation();
2050 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2051 SkipUntil(tok::r_square, false);
2052
2053 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
2054 return Attr;
2055}
2056
2057/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2058/// attribute.
2059///
2060/// FIXME: Simply returns an alignof() expression if the argument is a
2061/// type. Ideally, the type should be propagated directly into Sema.
2062///
2063/// [C++0x] 'align' '(' type-id ')'
2064/// [C++0x] 'align' '(' assignment-expression ')'
2065Parser::OwningExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
2066 if (isTypeIdInParens()) {
2067 EnterExpressionEvaluationContext Unevaluated(Actions,
2068 Action::Unevaluated);
2069 SourceLocation TypeLoc = Tok.getLocation();
2070 TypeTy *Ty = ParseTypeName().get();
2071 SourceRange TypeRange(Start, Tok.getLocation());
Nick Lewycky56062202010-07-26 16:56:01 +00002072 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, Ty, TypeRange);
Sean Huntbbd37c62009-11-21 08:43:09 +00002073 } else
2074 return ParseConstantExpression();
2075}