blob: a21f0344272c9cbe42d63279da6333c9bcf8ca83 [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 Lattnerbc8d5642008-12-18 01:12:00 +000020#include "ExtensionRAIIObject.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)) {
52 Actions.CodeCompleteNamespaceDecl(CurScope);
53 ConsumeToken();
54 }
55
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.
Chris Lattnerb28317a2009-03-28 19:18:32 +000067 Action::AttrTy *AttrList = 0;
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.
Sean Huntbbd37c62009-11-21 08:43:09 +000072 AttrList = 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
90 // Enter a scope for the namespace.
91 ParseScope NamespaceScope(this, Scope::DeclScope);
92
93 DeclPtrTy NamespcDecl =
94 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
95
96 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
97 PP.getSourceManager(),
98 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +000099
Sean Huntbbd37c62009-11-21 08:43:09 +0000100 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
101 CXX0XAttributeList Attr;
102 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
103 Attr = ParseCXX0XAttributes();
104 ParseExternalDeclaration(Attr);
105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Chris Lattner51448322009-03-29 14:02:43 +0000107 // Leave the namespace scope.
108 NamespaceScope.Exit();
109
Chris Lattner97144fc2009-04-02 04:16:50 +0000110 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
111 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000112
Chris Lattner97144fc2009-04-02 04:16:50 +0000113 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000114 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000115}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000116
Anders Carlssonf67606a2009-03-28 04:07:16 +0000117/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
118/// alias definition.
119///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000120Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000121 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000122 IdentifierInfo *Alias,
123 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000124 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Anders Carlssonf67606a2009-03-28 04:07:16 +0000126 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000128 if (Tok.is(tok::code_completion)) {
129 Actions.CodeCompleteNamespaceAliasDecl(CurScope);
130 ConsumeToken();
131 }
132
Anders Carlssonf67606a2009-03-28 04:07:16 +0000133 CXXScopeSpec SS;
134 // Parse (optional) nested-name-specifier.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000135 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000136
137 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
138 Diag(Tok, diag::err_expected_namespace_name);
139 // Skip to end of the definition and eat the ';'.
140 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000141 return DeclPtrTy();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000142 }
143
144 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000145 IdentifierInfo *Ident = Tok.getIdentifierInfo();
146 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Anders Carlssonf67606a2009-03-28 04:07:16 +0000148 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000149 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000150 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
151 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000152
153 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000154 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000155}
156
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000157/// ParseLinkage - We know that the current token is a string_literal
158/// and just before that, that extern was seen.
159///
160/// linkage-specification: [C++ 7.5p2: dcl.link]
161/// 'extern' string-literal '{' declaration-seq[opt] '}'
162/// 'extern' string-literal declaration
163///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000164Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000165 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000166 llvm::SmallVector<char, 8> LangBuffer;
167 // LangBuffer is guaranteed to be big enough.
168 LangBuffer.resize(Tok.getLength());
169 const char *LangBufPtr = &LangBuffer[0];
170 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
171
172 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000173
Douglas Gregor074149e2009-01-05 19:45:36 +0000174 ParseScope LinkageScope(this, Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000175 DeclPtrTy LinkageSpec
176 = Actions.ActOnStartLinkageSpecification(CurScope,
Douglas Gregor074149e2009-01-05 19:45:36 +0000177 /*FIXME: */SourceLocation(),
178 Loc, LangBufPtr, StrSize,
Mike Stump1eb44332009-09-09 15:08:12 +0000179 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000180 : SourceLocation());
181
Sean Huntbbd37c62009-11-21 08:43:09 +0000182 CXX0XAttributeList Attr;
183 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
184 Attr = ParseCXX0XAttributes();
185 }
186
Douglas Gregor074149e2009-01-05 19:45:36 +0000187 if (Tok.isNot(tok::l_brace)) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000188 ParseDeclarationOrFunctionDefinition(Attr.AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000189 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000190 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000191 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000192
Sean Huntbbd37c62009-11-21 08:43:09 +0000193 if (Attr.HasAttr)
194 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
195 << Attr.Range;
196
Douglas Gregorf44515a2008-12-16 22:23:02 +0000197 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000198 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000199 CXX0XAttributeList Attr;
200 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
201 Attr = ParseCXX0XAttributes();
202 ParseExternalDeclaration(Attr);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000203 }
204
Douglas Gregorf44515a2008-12-16 22:23:02 +0000205 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000206 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000207}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000208
Douglas Gregorf780abc2008-12-30 03:27:21 +0000209/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
210/// using-directive. Assumes that current token is 'using'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000211Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000212 SourceLocation &DeclEnd,
213 CXX0XAttributeList Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000214 assert(Tok.is(tok::kw_using) && "Not using token");
215
216 // Eat 'using'.
217 SourceLocation UsingLoc = ConsumeToken();
218
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000219 if (Tok.is(tok::code_completion)) {
220 Actions.CodeCompleteUsing(CurScope);
221 ConsumeToken();
222 }
223
Chris Lattner2f274772009-01-06 06:55:51 +0000224 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000225 // Next token after 'using' is 'namespace' so it must be using-directive
Sean Huntbbd37c62009-11-21 08:43:09 +0000226 return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList);
227
228 if (Attr.HasAttr)
229 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
230 << Attr.Range;
Chris Lattner2f274772009-01-06 06:55:51 +0000231
232 // Otherwise, it must be using-declaration.
Sean Huntbbd37c62009-11-21 08:43:09 +0000233 // Ignore illegal attributes (the caller should already have issued an error.
Chris Lattner97144fc2009-04-02 04:16:50 +0000234 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000235}
236
237/// ParseUsingDirective - Parse C++ using-directive, assumes
238/// that current token is 'namespace' and 'using' was already parsed.
239///
240/// using-directive: [C++ 7.3.p4: namespace.udir]
241/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
242/// namespace-name ;
243/// [GNU] using-directive:
244/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
245/// namespace-name attributes[opt] ;
246///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000247Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000248 SourceLocation UsingLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000249 SourceLocation &DeclEnd,
250 AttributeList *Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000251 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
252
253 // Eat 'namespace'.
254 SourceLocation NamespcLoc = ConsumeToken();
255
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000256 if (Tok.is(tok::code_completion)) {
257 Actions.CodeCompleteUsingDirective(CurScope);
258 ConsumeToken();
259 }
260
Douglas Gregorf780abc2008-12-30 03:27:21 +0000261 CXXScopeSpec SS;
262 // Parse (optional) nested-name-specifier.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000263 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000264
Douglas Gregorf780abc2008-12-30 03:27:21 +0000265 IdentifierInfo *NamespcName = 0;
266 SourceLocation IdentLoc = SourceLocation();
267
268 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000269 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000270 Diag(Tok, diag::err_expected_namespace_name);
271 // If there was invalid namespace name, skip to end of decl, and eat ';'.
272 SkipUntil(tok::semi);
273 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattnerb28317a2009-03-28 19:18:32 +0000274 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000275 }
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Chris Lattner823c44e2009-01-06 07:27:21 +0000277 // Parse identifier.
278 NamespcName = Tok.getIdentifierInfo();
279 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Chris Lattner823c44e2009-01-06 07:27:21 +0000281 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000282 bool GNUAttr = false;
283 if (Tok.is(tok::kw___attribute)) {
284 GNUAttr = true;
285 Attr = addAttributeLists(Attr, ParseGNUAttributes());
286 }
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Chris Lattner823c44e2009-01-06 07:27:21 +0000288 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000289 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000290 ExpectAndConsume(tok::semi,
Sean Huntbbd37c62009-11-21 08:43:09 +0000291 GNUAttr ? diag::err_expected_semi_after_attribute_list :
Chris Lattner6869d8e2009-06-14 00:07:48 +0000292 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000293
294 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000295 IdentLoc, NamespcName, Attr);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000296}
297
298/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
299/// 'using' was already seen.
300///
301/// using-declaration: [C++ 7.3.p3: namespace.udecl]
302/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000303/// unqualified-id
304/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000305///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000306Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000307 SourceLocation UsingLoc,
Anders Carlsson595adc12009-08-29 19:54:19 +0000308 SourceLocation &DeclEnd,
309 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000310 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000311 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000312 bool IsTypeName;
313
314 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000315 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000316 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000317 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000318 ConsumeToken();
319 IsTypeName = true;
320 }
321 else
322 IsTypeName = false;
323
324 // Parse nested-name-specifier.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000325 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000326
327 AttributeList *AttrList = 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000328
329 // Check nested-name specifier.
330 if (SS.isInvalid()) {
331 SkipUntil(tok::semi);
332 return DeclPtrTy();
333 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000334
335 // Parse the unqualified-id. We allow parsing of both constructor and
336 // destructor names and allow the action module to diagnose any semantic
337 // errors.
338 UnqualifiedId Name;
339 if (ParseUnqualifiedId(SS,
340 /*EnteringContext=*/false,
341 /*AllowDestructorName=*/true,
342 /*AllowConstructorName=*/true,
343 /*ObjectType=*/0,
344 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000345 SkipUntil(tok::semi);
346 return DeclPtrTy();
347 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000348
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000349 // Parse (optional) attributes (most likely GNU strong-using extension).
350 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000351 AttrList = ParseGNUAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000353 // Eat ';'.
354 DeclEnd = Tok.getLocation();
355 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000356 AttrList ? "attributes list" : "using declaration",
357 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000358
Douglas Gregor12c118a2009-11-04 16:30:06 +0000359 return Actions.ActOnUsingDeclaration(CurScope, AS, UsingLoc, SS, Name,
John McCall7ba107a2009-11-18 02:36:19 +0000360 AttrList, IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000361}
362
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000363/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
364///
365/// static_assert-declaration:
366/// static_assert ( constant-expression , string-literal ) ;
367///
Chris Lattner97144fc2009-04-02 04:16:50 +0000368Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000369 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
370 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000372 if (Tok.isNot(tok::l_paren)) {
373 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000374 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000377 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000378
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000379 OwningExprResult AssertExpr(ParseConstantExpression());
380 if (AssertExpr.isInvalid()) {
381 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000382 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000383 }
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Anders Carlssonad5f9602009-03-13 23:29:20 +0000385 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000386 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000387
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000388 if (Tok.isNot(tok::string_literal)) {
389 Diag(Tok, diag::err_expected_string_literal);
390 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000391 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000392 }
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000394 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000395 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000396 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000397
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000398 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattner97144fc2009-04-02 04:16:50 +0000400 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000401 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
402
Mike Stump1eb44332009-09-09 15:08:12 +0000403 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000404 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000405}
406
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000407/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
408///
409/// 'decltype' ( expression )
410///
411void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
412 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
413
414 SourceLocation StartLoc = ConsumeToken();
415 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000416
417 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000418 "decltype")) {
419 SkipUntil(tok::r_paren);
420 return;
421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000423 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000425 // C++0x [dcl.type.simple]p4:
426 // The operand of the decltype specifier is an unevaluated operand.
427 EnterExpressionEvaluationContext Unevaluated(Actions,
428 Action::Unevaluated);
429 OwningExprResult Result = ParseExpression();
430 if (Result.isInvalid()) {
431 SkipUntil(tok::r_paren);
432 return;
433 }
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000435 // Match the ')'
436 SourceLocation RParenLoc;
437 if (Tok.is(tok::r_paren))
438 RParenLoc = ConsumeParen();
439 else
440 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000442 if (RParenLoc.isInvalid())
443 return;
444
445 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000446 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000447 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000448 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000449 DiagID, Result.release()))
450 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000451}
452
Douglas Gregor42a552f2008-11-05 20:51:48 +0000453/// ParseClassName - Parse a C++ class-name, which names a class. Note
454/// that we only check that the result names a type; semantic analysis
455/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000456/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000457/// found.
458///
459/// class-name: [C++ 9.1]
460/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000461/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000462///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000463Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000464 const CXXScopeSpec *SS,
465 bool DestrExpected) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000466 // Check whether we have a template-id that names a type.
467 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000468 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000469 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000470 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000471 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000472
473 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
474 TypeTy *Type = Tok.getAnnotationValue();
475 EndLocation = Tok.getAnnotationEndLoc();
476 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000477
478 if (Type)
479 return Type;
480 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000481 }
482
483 // Fall through to produce an error below.
484 }
485
Douglas Gregor42a552f2008-11-05 20:51:48 +0000486 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000487 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000488 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000489 }
490
491 // We have an identifier; check whether it is actually a type.
Mike Stump1eb44332009-09-09 15:08:12 +0000492 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor42c39f32009-08-26 18:27:52 +0000493 Tok.getLocation(), CurScope, SS,
494 true);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000495 if (!Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000496 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000497 : diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000498 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000499 }
500
501 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000502 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000503 return Type;
504}
505
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000506/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
507/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
508/// until we reach the start of a definition or see a token that
509/// cannot start a definition.
510///
511/// class-specifier: [C++ class]
512/// class-head '{' member-specification[opt] '}'
513/// class-head '{' member-specification[opt] '}' attributes[opt]
514/// class-head:
515/// class-key identifier[opt] base-clause[opt]
516/// class-key nested-name-specifier identifier base-clause[opt]
517/// class-key nested-name-specifier[opt] simple-template-id
518/// base-clause[opt]
519/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000520/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000521/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000522/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000523/// simple-template-id base-clause[opt]
524/// class-key:
525/// 'class'
526/// 'struct'
527/// 'union'
528///
529/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000530/// class-key ::[opt] nested-name-specifier[opt] identifier
531/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
532/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000533///
534/// Note that the C++ class-specifier and elaborated-type-specifier,
535/// together, subsume the C99 struct-or-union-specifier:
536///
537/// struct-or-union-specifier: [C99 6.7.2.1]
538/// struct-or-union identifier[opt] '{' struct-contents '}'
539/// struct-or-union identifier
540/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
541/// '}' attributes[opt]
542/// [GNU] struct-or-union attributes[opt] identifier
543/// struct-or-union:
544/// 'struct'
545/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000546void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
547 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000548 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000549 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000550 DeclSpec::TST TagType;
551 if (TagTokKind == tok::kw_struct)
552 TagType = DeclSpec::TST_struct;
553 else if (TagTokKind == tok::kw_class)
554 TagType = DeclSpec::TST_class;
555 else {
556 assert(TagTokKind == tok::kw_union && "Not a class specifier");
557 TagType = DeclSpec::TST_union;
558 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000559
Douglas Gregor374929f2009-09-18 15:37:17 +0000560 if (Tok.is(tok::code_completion)) {
561 // Code completion for a struct, class, or union name.
562 Actions.CodeCompleteTag(CurScope, TagType);
563 ConsumeToken();
564 }
565
Sean Huntbbd37c62009-11-21 08:43:09 +0000566 AttributeList *AttrList = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000567 // If attributes exist after tag, parse them.
568 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000569 AttrList = ParseGNUAttributes();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000570
Steve Narofff59e17e2008-12-24 20:59:21 +0000571 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000572 if (Tok.is(tok::kw___declspec))
Sean Huntbbd37c62009-11-21 08:43:09 +0000573 AttrList = ParseMicrosoftDeclSpec(AttrList);
574
575 // If C++0x attributes exist here, parse them.
576 // FIXME: Are we consistent with the ordering of parsing of different
577 // styles of attributes?
578 if (isCXX0XAttributeSpecifier())
579 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Douglas Gregorb117a602009-09-04 05:53:02 +0000581 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
582 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
583 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000584 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000585 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
586 // properly.
587 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
588 Tok.setKind(tok::identifier);
589 }
590
591 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
592 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
593 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000594 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000595 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
596 // properly.
597 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
598 Tok.setKind(tok::identifier);
599 }
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000601 // Parse the (optional) nested-name-specifier.
602 CXXScopeSpec SS;
Mike Stump1eb44332009-09-09 15:08:12 +0000603 if (getLang().CPlusPlus &&
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000604 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
Douglas Gregor39a8de12009-02-25 19:37:18 +0000605 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000606 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000607
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000608 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
609
Douglas Gregorcc636682009-02-17 23:15:12 +0000610 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000611 IdentifierInfo *Name = 0;
612 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000613 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000614 if (Tok.is(tok::identifier)) {
615 Name = Tok.getIdentifierInfo();
616 NameLoc = ConsumeToken();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000617
618 if (Tok.is(tok::less)) {
619 // The name was supposed to refer to a template, but didn't.
620 // Eat the template argument list and try to continue parsing this as
621 // a class (or template thereof).
622 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000623 SourceLocation LAngleLoc, RAngleLoc;
624 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
625 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000626 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000627 // We couldn't parse the template argument list at all, so don't
628 // try to give any location information for the list.
629 LAngleLoc = RAngleLoc = SourceLocation();
630 }
631
632 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000633 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000634 << (TagType == DeclSpec::TST_class? 0
635 : TagType == DeclSpec::TST_struct? 1
636 : 2)
637 << Name
638 << SourceRange(LAngleLoc, RAngleLoc);
639
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000640 // Strip off the last template parameter list if it was empty, since
641 // we've removed its template argument list.
642 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
643 if (TemplateParams && TemplateParams->size() > 1) {
644 TemplateParams->pop_back();
645 } else {
646 TemplateParams = 0;
647 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
648 = ParsedTemplateInfo::NonTemplate;
649 }
650 } else if (TemplateInfo.Kind
651 == ParsedTemplateInfo::ExplicitInstantiation) {
652 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000653 TemplateParams = 0;
654 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
655 = ParsedTemplateInfo::NonTemplate;
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000656 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
657 = SourceLocation();
658 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
659 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000660 }
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000661
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000662
663 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000664 } else if (Tok.is(tok::annot_template_id)) {
665 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
666 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000667
Douglas Gregorc45c2322009-03-31 00:43:58 +0000668 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000669 // The template-name in the simple-template-id refers to
670 // something other than a class template. Give an appropriate
671 // error message and skip to the ';'.
672 SourceRange Range(NameLoc);
673 if (SS.isNotEmpty())
674 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000675
Douglas Gregor39a8de12009-02-25 19:37:18 +0000676 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
677 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Douglas Gregor39a8de12009-02-25 19:37:18 +0000679 DS.SetTypeSpecError();
680 SkipUntil(tok::semi, false, true);
681 TemplateId->Destroy();
682 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000683 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000684 }
685
John McCall67d1a672009-08-06 02:15:43 +0000686 // There are four options here. If we have 'struct foo;', then this
687 // is either a forward declaration or a friend declaration, which
688 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000689 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000690 // something like 'struct foo xyz', a reference.
John McCall0f434ec2009-07-31 02:45:11 +0000691 Action::TagUseKind TUK;
Douglas Gregord85bea22009-09-26 06:47:28 +0000692 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) {
693 if (DS.isFriendSpecified()) {
694 // C++ [class.friend]p2:
695 // A class shall not be defined in a friend declaration.
696 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
697 << SourceRange(DS.getFriendSpecLoc());
698
699 // Skip everything up to the semicolon, so that this looks like a proper
700 // friend class (or template thereof) declaration.
701 SkipUntil(tok::semi, true, true);
702 TUK = Action::TUK_Friend;
703 } else {
704 // Okay, this is a class definition.
705 TUK = Action::TUK_Definition;
706 }
707 } else if (Tok.is(tok::semi))
John McCall67d1a672009-08-06 02:15:43 +0000708 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000709 else
John McCall0f434ec2009-07-31 02:45:11 +0000710 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000711
John McCall0f434ec2009-07-31 02:45:11 +0000712 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000713 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000714 Diag(StartLoc, diag::err_anon_type_definition)
715 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000716
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000717 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000718
719 if (TemplateId)
720 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000721 return;
722 }
723
Douglas Gregorddc29e12009-02-06 22:42:48 +0000724 // Create the tag portion of the class or class template.
John McCallc4e70192009-09-11 04:59:25 +0000725 Action::DeclResult TagOrTempResult = true; // invalid
726 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000727
John McCall0f434ec2009-07-31 02:45:11 +0000728 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000729 // to turn that template-id into a type.
730
Douglas Gregor402abb52009-05-28 23:31:59 +0000731 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000732 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000733 // Explicit specialization, class template partial specialization,
734 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000735 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000736 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000737 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000738 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000739 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000740 // This is an explicit instantiation of a class template.
741 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000742 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000743 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000744 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000745 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000746 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000747 SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000748 TemplateTy::make(TemplateId->Template),
749 TemplateId->TemplateNameLoc,
750 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000751 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000752 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000753 AttrList);
Douglas Gregorfc9cd612009-09-26 20:57:03 +0000754 } else if (TUK == Action::TUK_Reference) {
John McCallc4e70192009-09-11 04:59:25 +0000755 TypeResult
John McCall6b2becf2009-09-08 17:47:29 +0000756 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
757 TemplateId->TemplateNameLoc,
758 TemplateId->LAngleLoc,
759 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000760 TemplateId->RAngleLoc);
761
John McCallc4e70192009-09-11 04:59:25 +0000762 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
763 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000764 } else {
765 // This is an explicit specialization or a class template
766 // partial specialization.
767 TemplateParameterLists FakedParamLists;
768
769 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
770 // This looks like an explicit instantiation, because we have
771 // something like
772 //
773 // template class Foo<X>
774 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000775 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000776 // meant to be an explicit specialization, but the user forgot
777 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000778 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000779
Mike Stump1eb44332009-09-09 15:08:12 +0000780 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000781 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000782 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000783 diag::err_explicit_instantiation_with_definition)
784 << SourceRange(TemplateInfo.TemplateLoc)
785 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
786
787 // Create a fake template parameter list that contains only
788 // "template<>", so that we treat this construct as a class
789 // template specialization.
790 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000791 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000792 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000793 LAngleLoc,
794 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000795 LAngleLoc));
796 TemplateParams = &FakedParamLists;
797 }
798
799 // Build the class template specialization.
800 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000801 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000802 StartLoc, SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000803 TemplateTy::make(TemplateId->Template),
804 TemplateId->TemplateNameLoc,
805 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000806 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000807 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000808 AttrList,
Mike Stump1eb44332009-09-09 15:08:12 +0000809 Action::MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000810 TemplateParams? &(*TemplateParams)[0] : 0,
811 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000812 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000813 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000814 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000815 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000816 // Explicit instantiation of a member of a class template
817 // specialization, e.g.,
818 //
819 // template struct Outer<int>::Inner;
820 //
821 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000822 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000823 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000824 TemplateInfo.TemplateLoc,
825 TagType, StartLoc, SS, Name,
Sean Huntbbd37c62009-11-21 08:43:09 +0000826 NameLoc, AttrList);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000827 } else {
828 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000829 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000830 // FIXME: Diagnose this particular error.
831 }
832
John McCallc4e70192009-09-11 04:59:25 +0000833 bool IsDependent = false;
834
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000835 // Declaration or definition of a class type
Mike Stump1eb44332009-09-09 15:08:12 +0000836 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000837 Name, NameLoc, AttrList, AS,
Mike Stump1eb44332009-09-09 15:08:12 +0000838 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000839 TemplateParams? &(*TemplateParams)[0] : 0,
840 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000841 Owned, IsDependent);
842
843 // If ActOnTag said the type was dependent, try again with the
844 // less common call.
845 if (IsDependent)
846 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
847 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000848 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000849
850 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000851 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000852 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000853
854 // If there is a body, parse it and inform the actions module.
855 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000856 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000857 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000858 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000859 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall0f434ec2009-07-31 02:45:11 +0000860 else if (TUK == Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000861 // FIXME: Complain that we have a base-specifier list but no
862 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000863 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000864 }
865
John McCallc4e70192009-09-11 04:59:25 +0000866 void *Result;
867 if (!TypeResult.isInvalid()) {
868 TagType = DeclSpec::TST_typename;
869 Result = TypeResult.get();
870 Owned = false;
871 } else if (!TagOrTempResult.isInvalid()) {
872 Result = TagOrTempResult.get().getAs<void>();
873 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000874 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000875 return;
876 }
Mike Stump1eb44332009-09-09 15:08:12 +0000877
John McCallfec54012009-08-03 20:12:06 +0000878 const char *PrevSpec = 0;
879 unsigned DiagID;
John McCallc4e70192009-09-11 04:59:25 +0000880
John McCallfec54012009-08-03 20:12:06 +0000881 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
John McCallc4e70192009-09-11 04:59:25 +0000882 Result, Owned))
John McCallfec54012009-08-03 20:12:06 +0000883 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000884}
885
Mike Stump1eb44332009-09-09 15:08:12 +0000886/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000887///
888/// base-clause : [C++ class.derived]
889/// ':' base-specifier-list
890/// base-specifier-list:
891/// base-specifier '...'[opt]
892/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000893void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000894 assert(Tok.is(tok::colon) && "Not a base clause");
895 ConsumeToken();
896
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000897 // Build up an array of parsed base specifiers.
898 llvm::SmallVector<BaseTy *, 8> BaseInfo;
899
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000900 while (true) {
901 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000902 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000903 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000904 // Skip the rest of this base specifier, up until the comma or
905 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000906 SkipUntil(tok::comma, tok::l_brace, true, true);
907 } else {
908 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000909 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000910 }
911
912 // If the next token is a comma, consume it and keep reading
913 // base-specifiers.
914 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000916 // Consume the comma.
917 ConsumeToken();
918 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000919
920 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000921 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000922}
923
924/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
925/// one entry in the base class list of a class specifier, for example:
926/// class foo : public bar, virtual private baz {
927/// 'public bar' and 'virtual private baz' are each base-specifiers.
928///
929/// base-specifier: [C++ class.derived]
930/// ::[opt] nested-name-specifier[opt] class-name
931/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
932/// class-name
933/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
934/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000935Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000936 bool IsVirtual = false;
937 SourceLocation StartLoc = Tok.getLocation();
938
939 // Parse the 'virtual' keyword.
940 if (Tok.is(tok::kw_virtual)) {
941 ConsumeToken();
942 IsVirtual = true;
943 }
944
945 // Parse an (optional) access specifier.
946 AccessSpecifier Access = getAccessSpecifierIfPresent();
947 if (Access)
948 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000950 // Parse the 'virtual' keyword (again!), in case it came after the
951 // access specifier.
952 if (Tok.is(tok::kw_virtual)) {
953 SourceLocation VirtualLoc = ConsumeToken();
954 if (IsVirtual) {
955 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000956 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000957 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000958 }
959
960 IsVirtual = true;
961 }
962
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000963 // Parse optional '::' and optional nested-name-specifier.
964 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000965 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000966
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000967 // The location of the base class itself.
968 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000969
970 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000971 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000972 TypeResult BaseType = ParseClassName(EndLocation, &SS);
973 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000974 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000975
976 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000977 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000979 // Notify semantic analysis that we have parsed a complete
980 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000981 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000982 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000983}
984
985/// getAccessSpecifierIfPresent - Determine whether the next token is
986/// a C++ access-specifier.
987///
988/// access-specifier: [C++ class.derived]
989/// 'private'
990/// 'protected'
991/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +0000992AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000993 switch (Tok.getKind()) {
994 default: return AS_none;
995 case tok::kw_private: return AS_private;
996 case tok::kw_protected: return AS_protected;
997 case tok::kw_public: return AS_public;
998 }
999}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001000
Eli Friedmand33133c2009-07-22 21:45:50 +00001001void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
1002 DeclPtrTy ThisDecl) {
1003 // We just declared a member function. If this member function
1004 // has any default arguments, we'll need to parse them later.
1005 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001006 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +00001007 = DeclaratorInfo.getTypeObject(0).Fun;
1008 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1009 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1010 if (!LateMethod) {
1011 // Push this method onto the stack of late-parsed method
1012 // declarations.
1013 getCurrentClass().MethodDecls.push_back(
1014 LateParsedMethodDeclaration(ThisDecl));
1015 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +00001016 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001017
1018 // Add all of the parameters prior to this one (they don't
1019 // have default arguments).
1020 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1021 for (unsigned I = 0; I < ParamIdx; ++I)
1022 LateMethod->DefaultArgs.push_back(
1023 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
1024 }
1025
1026 // Add this parameter to the list of parameters (it or may
1027 // not have a default argument).
1028 LateMethod->DefaultArgs.push_back(
1029 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1030 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1031 }
1032 }
1033}
1034
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001035/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1036///
1037/// member-declaration:
1038/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1039/// function-definition ';'[opt]
1040/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1041/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001042/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001043/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001044/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001045///
1046/// member-declarator-list:
1047/// member-declarator
1048/// member-declarator-list ',' member-declarator
1049///
1050/// member-declarator:
1051/// declarator pure-specifier[opt]
1052/// declarator constant-initializer[opt]
1053/// identifier[opt] ':' constant-expression
1054///
Sebastian Redle2b68332009-04-12 17:16:29 +00001055/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001056/// '= 0'
1057///
1058/// constant-initializer:
1059/// '=' constant-expression
1060///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001061void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
1062 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001063 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001064 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001065 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001066 SourceLocation DeclEnd;
1067 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001068 return;
1069 }
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Chris Lattner682bf922009-03-29 16:50:03 +00001071 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001072 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001073 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001074 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001075 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001076 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001077 return;
1078 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001079
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001080 // Handle: member-declaration ::= '__extension__' member-declaration
1081 if (Tok.is(tok::kw___extension__)) {
1082 // __extension__ silences extension warnings in the subexpression.
1083 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1084 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +00001085 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001086 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001087
Sean Huntbbd37c62009-11-21 08:43:09 +00001088 CXX0XAttributeList AttrList;
1089 // Optional C++0x attribute-specifier
1090 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
1091 AttrList = ParseCXX0XAttributes();
1092 }
1093
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001094 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001095 // FIXME: Check for template aliases
Sean Huntbbd37c62009-11-21 08:43:09 +00001096
1097 if (AttrList.HasAttr)
1098 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1099 << AttrList.Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001101 // Eat 'using'.
1102 SourceLocation UsingLoc = ConsumeToken();
1103
1104 if (Tok.is(tok::kw_namespace)) {
1105 Diag(UsingLoc, diag::err_using_namespace_in_class);
1106 SkipUntil(tok::semi, true, true);
1107 }
1108 else {
1109 SourceLocation DeclEnd;
1110 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001111 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001112 }
1113 return;
1114 }
1115
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001116 SourceLocation DSStart = Tok.getLocation();
1117 // decl-specifier-seq:
1118 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001119 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +00001120 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001121 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001122
John McCalldd4a3b02009-09-16 22:47:08 +00001123 Action::MultiTemplateParamsArg TemplateParams(Actions,
1124 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1125 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1126
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001127 if (Tok.is(tok::semi)) {
1128 ConsumeToken();
Douglas Gregord85bea22009-09-26 06:47:28 +00001129 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +00001130 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001131 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001132
John McCall54abf7d2009-11-04 02:18:39 +00001133 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001134
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001135 if (Tok.isNot(tok::colon)) {
1136 // Parse the first declarator.
1137 ParseDeclarator(DeclaratorInfo);
1138 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001139 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001140 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001141 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001142 if (Tok.is(tok::semi))
1143 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001144 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001145 }
1146
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001147 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001148 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001149 || (DeclaratorInfo.isFunctionDeclarator() &&
1150 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001151 if (!DeclaratorInfo.isFunctionDeclarator()) {
1152 Diag(Tok, diag::err_func_def_no_params);
1153 ConsumeBrace();
1154 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001155 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001156 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001157
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001158 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1159 Diag(Tok, diag::err_function_declared_typedef);
1160 // This recovery skips the entire function body. It would be nice
1161 // to simply call ParseCXXInlineMethodDef() below, however Sema
1162 // assumes the declarator represents a function, not a typedef.
1163 ConsumeBrace();
1164 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001165 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001166 }
1167
Douglas Gregor37b372b2009-08-20 22:52:58 +00001168 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001169 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001170 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001171 }
1172
1173 // member-declarator-list:
1174 // member-declarator
1175 // member-declarator-list ',' member-declarator
1176
Chris Lattner682bf922009-03-29 16:50:03 +00001177 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001178 OwningExprResult BitfieldSize(Actions);
1179 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001180 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001181
1182 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001183 // member-declarator:
1184 // declarator pure-specifier[opt]
1185 // declarator constant-initializer[opt]
1186 // identifier[opt] ':' constant-expression
1187
1188 if (Tok.is(tok::colon)) {
1189 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001190 BitfieldSize = ParseConstantExpression();
1191 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001192 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001193 }
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001195 // pure-specifier:
1196 // '= 0'
1197 //
1198 // constant-initializer:
1199 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001200 //
1201 // defaulted/deleted function-definition:
1202 // '=' 'default' [TODO]
1203 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001204
1205 if (Tok.is(tok::equal)) {
1206 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001207 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1208 ConsumeToken();
1209 Deleted = true;
1210 } else {
1211 Init = ParseInitializer();
1212 if (Init.isInvalid())
1213 SkipUntil(tok::comma, true, true);
1214 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001215 }
1216
1217 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001218 if (Tok.is(tok::kw___attribute)) {
1219 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001220 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001221 DeclaratorInfo.AddAttributes(AttrList, Loc);
1222 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001223
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001224 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001225 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001226 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001227
1228 DeclPtrTy ThisDecl;
1229 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001230 // TODO: handle initializers, bitfields, 'delete'
1231 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1232 /*IsDefinition*/ false,
1233 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001234 } else {
John McCall67d1a672009-08-06 02:15:43 +00001235 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1236 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001237 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001238 BitfieldSize.release(),
1239 Init.release(),
1240 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001241 }
Chris Lattner682bf922009-03-29 16:50:03 +00001242 if (ThisDecl)
1243 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001244
Douglas Gregor72b505b2008-12-16 21:30:33 +00001245 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001246 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001247 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001248 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001249 }
1250
John McCall54abf7d2009-11-04 02:18:39 +00001251 DeclaratorInfo.complete(ThisDecl);
1252
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001253 // If we don't have a comma, it is either the end of the list (a ';')
1254 // or an error, bail out.
1255 if (Tok.isNot(tok::comma))
1256 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001258 // Consume the comma.
1259 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001261 // Parse the next declarator.
1262 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001263 BitfieldSize = 0;
1264 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001265 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001267 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001268 if (Tok.is(tok::kw___attribute)) {
1269 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001270 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001271 DeclaratorInfo.AddAttributes(AttrList, Loc);
1272 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001273
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001274 if (Tok.isNot(tok::colon))
1275 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001276 }
1277
1278 if (Tok.is(tok::semi)) {
1279 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +00001280 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +00001281 DeclsInGroup.size());
1282 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001283 }
1284
1285 Diag(Tok, diag::err_expected_semi_decl_list);
1286 // Skip to end of block or statement
1287 SkipUntil(tok::r_brace, true, true);
1288 if (Tok.is(tok::semi))
1289 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001290 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001291}
1292
1293/// ParseCXXMemberSpecification - Parse the class definition.
1294///
1295/// member-specification:
1296/// member-declaration member-specification[opt]
1297/// access-specifier ':' member-specification[opt]
1298///
1299void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001300 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001301 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001302 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001303 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001304
Chris Lattner49f28ca2009-03-05 08:00:35 +00001305 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1306 PP.getSourceManager(),
1307 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001309 SourceLocation LBraceLoc = ConsumeBrace();
1310
Douglas Gregor6569d682009-05-27 23:11:45 +00001311 // Determine whether this is a top-level (non-nested) class.
Mike Stump1eb44332009-09-09 15:08:12 +00001312 bool TopLevelClass = ClassStack.empty() ||
Douglas Gregor6569d682009-05-27 23:11:45 +00001313 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001314
1315 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001316 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001317
Douglas Gregor6569d682009-05-27 23:11:45 +00001318 // Note that we are parsing a new (potentially-nested) class definition.
1319 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1320
Douglas Gregorddc29e12009-02-06 22:42:48 +00001321 if (TagDecl)
1322 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1323 else {
1324 SkipUntil(tok::r_brace, false, false);
1325 return;
1326 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001327
1328 // C++ 11p3: Members of a class defined with the keyword class are private
1329 // by default. Members of a class defined with the keywords struct or union
1330 // are public by default.
1331 AccessSpecifier CurAS;
1332 if (TagType == DeclSpec::TST_class)
1333 CurAS = AS_private;
1334 else
1335 CurAS = AS_public;
1336
1337 // While we still have something to read, read the member-declarations.
1338 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1339 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001341 // Check for extraneous top-level semicolon.
1342 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +00001343 Diag(Tok, diag::ext_extra_struct_semi)
1344 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001345 ConsumeToken();
1346 continue;
1347 }
1348
1349 AccessSpecifier AS = getAccessSpecifierIfPresent();
1350 if (AS != AS_none) {
1351 // Current token is a C++ access specifier.
1352 CurAS = AS;
1353 ConsumeToken();
1354 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1355 continue;
1356 }
1357
Douglas Gregor37b372b2009-08-20 22:52:58 +00001358 // FIXME: Make sure we don't have a template here.
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001360 // Parse all the comma separated declarators.
1361 ParseCXXClassMemberDeclaration(CurAS);
1362 }
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001364 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001366 AttributeList *AttrList = 0;
1367 // If attributes exist after class contents, parse them.
1368 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001369 AttrList = ParseGNUAttributes(); // FIXME: where should I put them?
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001370
1371 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1372 LBraceLoc, RBraceLoc);
1373
1374 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1375 // complete within function bodies, default arguments,
1376 // exception-specifications, and constructor ctor-initializers (including
1377 // such things in nested classes).
1378 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001379 // FIXME: Only function bodies and constructor ctor-initializers are
1380 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001381 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001382 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001383 // are complete and we can parse the delayed portions of method
1384 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001385 ParseLexedMethodDeclarations(getCurrentClass());
1386 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001387 }
1388
1389 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001390 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001391 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001392
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001393 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001394}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001395
1396/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1397/// which explicitly initializes the members or base classes of a
1398/// class (C++ [class.base.init]). For example, the three initializers
1399/// after the ':' in the Derived constructor below:
1400///
1401/// @code
1402/// class Base { };
1403/// class Derived : Base {
1404/// int x;
1405/// float f;
1406/// public:
1407/// Derived(float f) : Base(), x(17), f(f) { }
1408/// };
1409/// @endcode
1410///
Mike Stump1eb44332009-09-09 15:08:12 +00001411/// [C++] ctor-initializer:
1412/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001413///
Mike Stump1eb44332009-09-09 15:08:12 +00001414/// [C++] mem-initializer-list:
1415/// mem-initializer
1416/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001417void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001418 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1419
1420 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Douglas Gregor7ad83902008-11-05 04:29:56 +00001422 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Douglas Gregor7ad83902008-11-05 04:29:56 +00001424 do {
1425 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001426 if (!MemInit.isInvalid())
1427 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001428
1429 if (Tok.is(tok::comma))
1430 ConsumeToken();
1431 else if (Tok.is(tok::l_brace))
1432 break;
1433 else {
1434 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001435 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001436 SkipUntil(tok::l_brace, true, true);
1437 break;
1438 }
1439 } while (true);
1440
Mike Stump1eb44332009-09-09 15:08:12 +00001441 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001442 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001443}
1444
1445/// ParseMemInitializer - Parse a C++ member initializer, which is
1446/// part of a constructor initializer that explicitly initializes one
1447/// member or base class (C++ [class.base.init]). See
1448/// ParseConstructorInitializer for an example.
1449///
1450/// [C++] mem-initializer:
1451/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001452///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001453/// [C++] mem-initializer-id:
1454/// '::'[opt] nested-name-specifier[opt] class-name
1455/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001456Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001457 // parse '::'[opt] nested-name-specifier[opt]
1458 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001459 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001460 TypeTy *TemplateTypeTy = 0;
1461 if (Tok.is(tok::annot_template_id)) {
1462 TemplateIdAnnotation *TemplateId
1463 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1464 if (TemplateId->Kind == TNK_Type_template) {
1465 AnnotateTemplateIdTokenAsType(&SS);
1466 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1467 TemplateTypeTy = Tok.getAnnotationValue();
1468 }
1469 // FIXME. May need to check for TNK_Dependent_template as well.
1470 }
1471 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001472 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001473 return true;
1474 }
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Douglas Gregor7ad83902008-11-05 04:29:56 +00001476 // Get the identifier. This may be a member name or a class name,
1477 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001478 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001479 SourceLocation IdLoc = ConsumeToken();
1480
1481 // Parse the '('.
1482 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001483 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001484 return true;
1485 }
1486 SourceLocation LParenLoc = ConsumeParen();
1487
1488 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001489 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001490 CommaLocsTy CommaLocs;
1491 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1492 SkipUntil(tok::r_paren);
1493 return true;
1494 }
1495
1496 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1497
Fariborz Jahanian96174332009-07-01 19:21:19 +00001498 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1499 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001500 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001501 ArgExprs.size(), CommaLocs.data(),
1502 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001503}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001504
1505/// ParseExceptionSpecification - Parse a C++ exception-specification
1506/// (C++ [except.spec]).
1507///
Douglas Gregora4745612008-12-01 18:00:20 +00001508/// exception-specification:
1509/// 'throw' '(' type-id-list [opt] ')'
1510/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001511///
Douglas Gregora4745612008-12-01 18:00:20 +00001512/// type-id-list:
1513/// type-id
1514/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001515///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001516bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001517 llvm::SmallVector<TypeTy*, 2>
1518 &Exceptions,
1519 llvm::SmallVector<SourceRange, 2>
1520 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001521 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001522 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001524 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001526 if (!Tok.is(tok::l_paren)) {
1527 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1528 }
1529 SourceLocation LParenLoc = ConsumeParen();
1530
Douglas Gregora4745612008-12-01 18:00:20 +00001531 // Parse throw(...), a Microsoft extension that means "this function
1532 // can throw anything".
1533 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001534 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001535 SourceLocation EllipsisLoc = ConsumeToken();
1536 if (!getLang().Microsoft)
1537 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001538 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001539 return false;
1540 }
1541
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001542 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001543 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001544 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001545 TypeResult Res(ParseTypeName(&Range));
1546 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001547 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001548 Ranges.push_back(Range);
1549 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001550 if (Tok.is(tok::comma))
1551 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001552 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001553 break;
1554 }
1555
Sebastian Redlab197ba2009-02-09 18:23:29 +00001556 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001557 return false;
1558}
Douglas Gregor6569d682009-05-27 23:11:45 +00001559
1560/// \brief We have just started parsing the definition of a new class,
1561/// so push that class onto our stack of classes that is currently
1562/// being parsed.
1563void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001564 assert((TopLevelClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001565 "Nested class without outer class");
1566 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1567}
1568
1569/// \brief Deallocate the given parsed class and all of its nested
1570/// classes.
1571void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1572 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1573 DeallocateParsedClasses(Class->NestedClasses[I]);
1574 delete Class;
1575}
1576
1577/// \brief Pop the top class of the stack of classes that are
1578/// currently being parsed.
1579///
1580/// This routine should be called when we have finished parsing the
1581/// definition of a class, but have not yet popped the Scope
1582/// associated with the class's definition.
1583///
1584/// \returns true if the class we've popped is a top-level class,
1585/// false otherwise.
1586void Parser::PopParsingClass() {
1587 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001588
Douglas Gregor6569d682009-05-27 23:11:45 +00001589 ParsingClass *Victim = ClassStack.top();
1590 ClassStack.pop();
1591 if (Victim->TopLevelClass) {
1592 // Deallocate all of the nested classes of this class,
1593 // recursively: we don't need to keep any of this information.
1594 DeallocateParsedClasses(Victim);
1595 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001596 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001597 assert(!ClassStack.empty() && "Missing top-level class?");
1598
1599 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1600 Victim->NestedClasses.empty()) {
1601 // The victim is a nested class, but we will not need to perform
1602 // any processing after the definition of this class since it has
1603 // no members whose handling was delayed. Therefore, we can just
1604 // remove this nested class.
1605 delete Victim;
1606 return;
1607 }
1608
1609 // This nested class has some members that will need to be processed
1610 // after the top-level class is completely defined. Therefore, add
1611 // it to the list of nested classes within its parent.
1612 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1613 ClassStack.top()->NestedClasses.push_back(Victim);
1614 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1615}
Sean Huntbbd37c62009-11-21 08:43:09 +00001616
1617/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1618/// parses standard attributes.
1619///
1620/// [C++0x] attribute-specifier:
1621/// '[' '[' attribute-list ']' ']'
1622///
1623/// [C++0x] attribute-list:
1624/// attribute[opt]
1625/// attribute-list ',' attribute[opt]
1626///
1627/// [C++0x] attribute:
1628/// attribute-token attribute-argument-clause[opt]
1629///
1630/// [C++0x] attribute-token:
1631/// identifier
1632/// attribute-scoped-token
1633///
1634/// [C++0x] attribute-scoped-token:
1635/// attribute-namespace '::' identifier
1636///
1637/// [C++0x] attribute-namespace:
1638/// identifier
1639///
1640/// [C++0x] attribute-argument-clause:
1641/// '(' balanced-token-seq ')'
1642///
1643/// [C++0x] balanced-token-seq:
1644/// balanced-token
1645/// balanced-token-seq balanced-token
1646///
1647/// [C++0x] balanced-token:
1648/// '(' balanced-token-seq ')'
1649/// '[' balanced-token-seq ']'
1650/// '{' balanced-token-seq '}'
1651/// any token but '(', ')', '[', ']', '{', or '}'
1652CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1653 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1654 && "Not a C++0x attribute list");
1655
1656 SourceLocation StartLoc = Tok.getLocation(), Loc;
1657 AttributeList *CurrAttr = 0;
1658
1659 ConsumeBracket();
1660 ConsumeBracket();
1661
1662 if (Tok.is(tok::comma)) {
1663 Diag(Tok.getLocation(), diag::err_expected_ident);
1664 ConsumeToken();
1665 }
1666
1667 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1668 // attribute not present
1669 if (Tok.is(tok::comma)) {
1670 ConsumeToken();
1671 continue;
1672 }
1673
1674 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1675 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
1676
1677 // scoped attribute
1678 if (Tok.is(tok::coloncolon)) {
1679 ConsumeToken();
1680
1681 if (!Tok.is(tok::identifier)) {
1682 Diag(Tok.getLocation(), diag::err_expected_ident);
1683 SkipUntil(tok::r_square, tok::comma, true, true);
1684 continue;
1685 }
1686
1687 ScopeName = AttrName;
1688 ScopeLoc = AttrLoc;
1689
1690 AttrName = Tok.getIdentifierInfo();
1691 AttrLoc = ConsumeToken();
1692 }
1693
1694 bool AttrParsed = false;
1695 // No scoped names are supported; ideally we could put all non-standard
1696 // attributes into namespaces.
1697 if (!ScopeName) {
1698 switch(AttributeList::getKind(AttrName))
1699 {
1700 // No arguments
1701 case AttributeList::AT_noreturn:
1702 case AttributeList::AT_final:
1703 case AttributeList::AT_carries_dependency: {
1704 if (Tok.is(tok::l_paren)) {
1705 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
1706 << AttrName->getName();
1707 break;
1708 }
1709
1710 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
1711 SourceLocation(), 0, 0, CurrAttr, false,
1712 true);
1713 AttrParsed = true;
1714 break;
1715 }
1716
1717 // One argument; must be a type-id or assignment-expression
1718 case AttributeList::AT_aligned: {
1719 if (Tok.isNot(tok::l_paren)) {
1720 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
1721 << AttrName->getName();
1722 break;
1723 }
1724 SourceLocation ParamLoc = ConsumeParen();
1725
1726 OwningExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
1727
1728 MatchRHSPunctuation(tok::r_paren, ParamLoc);
1729
1730 ExprVector ArgExprs(Actions);
1731 ArgExprs.push_back(ArgExpr.release());
1732 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
1733 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
1734 false, true);
1735
1736 AttrParsed = true;
1737 break;
1738 }
1739
1740 // Silence warnings
1741 default: break;
1742 }
1743 }
1744
1745 // Skip the entire parameter clause, if any
1746 if (!AttrParsed && Tok.is(tok::l_paren)) {
1747 ConsumeParen();
1748 // SkipUntil maintains the balancedness of tokens.
1749 SkipUntil(tok::r_paren, false);
1750 }
1751 }
1752
1753 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1754 SkipUntil(tok::r_square, false);
1755 Loc = Tok.getLocation();
1756 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1757 SkipUntil(tok::r_square, false);
1758
1759 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
1760 return Attr;
1761}
1762
1763/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
1764/// attribute.
1765///
1766/// FIXME: Simply returns an alignof() expression if the argument is a
1767/// type. Ideally, the type should be propagated directly into Sema.
1768///
1769/// [C++0x] 'align' '(' type-id ')'
1770/// [C++0x] 'align' '(' assignment-expression ')'
1771Parser::OwningExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
1772 if (isTypeIdInParens()) {
1773 EnterExpressionEvaluationContext Unevaluated(Actions,
1774 Action::Unevaluated);
1775 SourceLocation TypeLoc = Tok.getLocation();
1776 TypeTy *Ty = ParseTypeName().get();
1777 SourceRange TypeRange(Start, Tok.getLocation());
1778 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, Ty,
1779 TypeRange);
1780 } else
1781 return ParseConstantExpression();
1782}