blob: baf9e4ba5996f2e83fcf71e1696eca7443a9fd9f [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.
72 AttrList = ParseAttributes();
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
Chris Lattner51448322009-03-29 14:02:43 +0000100 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
101 ParseExternalDeclaration();
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattner51448322009-03-29 14:02:43 +0000103 // Leave the namespace scope.
104 NamespaceScope.Exit();
105
Chris Lattner97144fc2009-04-02 04:16:50 +0000106 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
107 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000108
Chris Lattner97144fc2009-04-02 04:16:50 +0000109 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000110 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000111}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000112
Anders Carlssonf67606a2009-03-28 04:07:16 +0000113/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
114/// alias definition.
115///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000116Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000117 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000118 IdentifierInfo *Alias,
119 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000120 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Anders Carlssonf67606a2009-03-28 04:07:16 +0000122 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000124 if (Tok.is(tok::code_completion)) {
125 Actions.CodeCompleteNamespaceAliasDecl(CurScope);
126 ConsumeToken();
127 }
128
Anders Carlssonf67606a2009-03-28 04:07:16 +0000129 CXXScopeSpec SS;
130 // Parse (optional) nested-name-specifier.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000131 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000132
133 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
134 Diag(Tok, diag::err_expected_namespace_name);
135 // Skip to end of the definition and eat the ';'.
136 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000137 return DeclPtrTy();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000138 }
139
140 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000141 IdentifierInfo *Ident = Tok.getIdentifierInfo();
142 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Anders Carlssonf67606a2009-03-28 04:07:16 +0000144 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000145 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000146 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
147 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000148
149 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000150 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000151}
152
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000153/// ParseLinkage - We know that the current token is a string_literal
154/// and just before that, that extern was seen.
155///
156/// linkage-specification: [C++ 7.5p2: dcl.link]
157/// 'extern' string-literal '{' declaration-seq[opt] '}'
158/// 'extern' string-literal declaration
159///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000160Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000161 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000162 llvm::SmallVector<char, 8> LangBuffer;
163 // LangBuffer is guaranteed to be big enough.
164 LangBuffer.resize(Tok.getLength());
165 const char *LangBufPtr = &LangBuffer[0];
166 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
167
168 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000169
Douglas Gregor074149e2009-01-05 19:45:36 +0000170 ParseScope LinkageScope(this, Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000171 DeclPtrTy LinkageSpec
172 = Actions.ActOnStartLinkageSpecification(CurScope,
Douglas Gregor074149e2009-01-05 19:45:36 +0000173 /*FIXME: */SourceLocation(),
174 Loc, LangBufPtr, StrSize,
Mike Stump1eb44332009-09-09 15:08:12 +0000175 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000176 : SourceLocation());
177
178 if (Tok.isNot(tok::l_brace)) {
179 ParseDeclarationOrFunctionDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +0000180 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000181 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000182 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000183
184 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000185 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000186 ParseExternalDeclaration();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000187 }
188
Douglas Gregorf44515a2008-12-16 22:23:02 +0000189 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000190 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000191}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000192
Douglas Gregorf780abc2008-12-30 03:27:21 +0000193/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
194/// using-directive. Assumes that current token is 'using'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000195Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
196 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000197 assert(Tok.is(tok::kw_using) && "Not using token");
198
199 // Eat 'using'.
200 SourceLocation UsingLoc = ConsumeToken();
201
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000202 if (Tok.is(tok::code_completion)) {
203 Actions.CodeCompleteUsing(CurScope);
204 ConsumeToken();
205 }
206
Chris Lattner2f274772009-01-06 06:55:51 +0000207 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000208 // Next token after 'using' is 'namespace' so it must be using-directive
Chris Lattner97144fc2009-04-02 04:16:50 +0000209 return ParseUsingDirective(Context, UsingLoc, DeclEnd);
Chris Lattner2f274772009-01-06 06:55:51 +0000210
211 // Otherwise, it must be using-declaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000212 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000213}
214
215/// ParseUsingDirective - Parse C++ using-directive, assumes
216/// that current token is 'namespace' and 'using' was already parsed.
217///
218/// using-directive: [C++ 7.3.p4: namespace.udir]
219/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
220/// namespace-name ;
221/// [GNU] using-directive:
222/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
223/// namespace-name attributes[opt] ;
224///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000225Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000226 SourceLocation UsingLoc,
227 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000228 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
229
230 // Eat 'namespace'.
231 SourceLocation NamespcLoc = ConsumeToken();
232
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000233 if (Tok.is(tok::code_completion)) {
234 Actions.CodeCompleteUsingDirective(CurScope);
235 ConsumeToken();
236 }
237
Douglas Gregorf780abc2008-12-30 03:27:21 +0000238 CXXScopeSpec SS;
239 // Parse (optional) nested-name-specifier.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000240 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000241
242 AttributeList *AttrList = 0;
243 IdentifierInfo *NamespcName = 0;
244 SourceLocation IdentLoc = SourceLocation();
245
246 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000247 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000248 Diag(Tok, diag::err_expected_namespace_name);
249 // If there was invalid namespace name, skip to end of decl, and eat ';'.
250 SkipUntil(tok::semi);
251 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattnerb28317a2009-03-28 19:18:32 +0000252 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000253 }
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Chris Lattner823c44e2009-01-06 07:27:21 +0000255 // Parse identifier.
256 NamespcName = Tok.getIdentifierInfo();
257 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Chris Lattner823c44e2009-01-06 07:27:21 +0000259 // Parse (optional) attributes (most likely GNU strong-using extension).
260 if (Tok.is(tok::kw___attribute))
261 AttrList = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Chris Lattner823c44e2009-01-06 07:27:21 +0000263 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000264 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000265 ExpectAndConsume(tok::semi,
266 AttrList ? diag::err_expected_semi_after_attribute_list :
267 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000268
269 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner823c44e2009-01-06 07:27:21 +0000270 IdentLoc, NamespcName, AttrList);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000271}
272
273/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
274/// 'using' was already seen.
275///
276/// using-declaration: [C++ 7.3.p3: namespace.udecl]
277/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000278/// unqualified-id
279/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000280///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000281Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000282 SourceLocation UsingLoc,
Anders Carlsson595adc12009-08-29 19:54:19 +0000283 SourceLocation &DeclEnd,
284 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000285 CXXScopeSpec SS;
286 bool IsTypeName;
287
288 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000289 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000290 if (Tok.is(tok::kw_typename)) {
291 ConsumeToken();
292 IsTypeName = true;
293 }
294 else
295 IsTypeName = false;
296
297 // Parse nested-name-specifier.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000298 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000299
300 AttributeList *AttrList = 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000301
302 // Check nested-name specifier.
303 if (SS.isInvalid()) {
304 SkipUntil(tok::semi);
305 return DeclPtrTy();
306 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000307
308 // Parse the unqualified-id. We allow parsing of both constructor and
309 // destructor names and allow the action module to diagnose any semantic
310 // errors.
311 UnqualifiedId Name;
312 if (ParseUnqualifiedId(SS,
313 /*EnteringContext=*/false,
314 /*AllowDestructorName=*/true,
315 /*AllowConstructorName=*/true,
316 /*ObjectType=*/0,
317 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000318 SkipUntil(tok::semi);
319 return DeclPtrTy();
320 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000321
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000322 // Parse (optional) attributes (most likely GNU strong-using extension).
323 if (Tok.is(tok::kw___attribute))
324 AttrList = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000326 // Eat ';'.
327 DeclEnd = Tok.getLocation();
328 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000329 AttrList ? "attributes list" : "using declaration",
330 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000331
Douglas Gregor12c118a2009-11-04 16:30:06 +0000332 return Actions.ActOnUsingDeclaration(CurScope, AS, UsingLoc, SS, Name,
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000333 AttrList, IsTypeName);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000334}
335
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000336/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
337///
338/// static_assert-declaration:
339/// static_assert ( constant-expression , string-literal ) ;
340///
Chris Lattner97144fc2009-04-02 04:16:50 +0000341Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000342 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
343 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000345 if (Tok.isNot(tok::l_paren)) {
346 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000347 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000348 }
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000350 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000351
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000352 OwningExprResult AssertExpr(ParseConstantExpression());
353 if (AssertExpr.isInvalid()) {
354 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000355 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Anders Carlssonad5f9602009-03-13 23:29:20 +0000358 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000359 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000360
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000361 if (Tok.isNot(tok::string_literal)) {
362 Diag(Tok, diag::err_expected_string_literal);
363 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000364 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000365 }
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000367 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000368 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000369 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000370
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000371 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Chris Lattner97144fc2009-04-02 04:16:50 +0000373 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000374 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
375
Mike Stump1eb44332009-09-09 15:08:12 +0000376 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000377 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000378}
379
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000380/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
381///
382/// 'decltype' ( expression )
383///
384void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
385 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
386
387 SourceLocation StartLoc = ConsumeToken();
388 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000389
390 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000391 "decltype")) {
392 SkipUntil(tok::r_paren);
393 return;
394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000396 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000398 // C++0x [dcl.type.simple]p4:
399 // The operand of the decltype specifier is an unevaluated operand.
400 EnterExpressionEvaluationContext Unevaluated(Actions,
401 Action::Unevaluated);
402 OwningExprResult Result = ParseExpression();
403 if (Result.isInvalid()) {
404 SkipUntil(tok::r_paren);
405 return;
406 }
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000408 // Match the ')'
409 SourceLocation RParenLoc;
410 if (Tok.is(tok::r_paren))
411 RParenLoc = ConsumeParen();
412 else
413 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000415 if (RParenLoc.isInvalid())
416 return;
417
418 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000419 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000420 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000421 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000422 DiagID, Result.release()))
423 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000424}
425
Douglas Gregor42a552f2008-11-05 20:51:48 +0000426/// ParseClassName - Parse a C++ class-name, which names a class. Note
427/// that we only check that the result names a type; semantic analysis
428/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000429/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000430/// found.
431///
432/// class-name: [C++ 9.1]
433/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000434/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000435///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000436Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000437 const CXXScopeSpec *SS,
438 bool DestrExpected) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000439 // Check whether we have a template-id that names a type.
440 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000441 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000442 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000443 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000444 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000445
446 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
447 TypeTy *Type = Tok.getAnnotationValue();
448 EndLocation = Tok.getAnnotationEndLoc();
449 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000450
451 if (Type)
452 return Type;
453 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000454 }
455
456 // Fall through to produce an error below.
457 }
458
Douglas Gregor42a552f2008-11-05 20:51:48 +0000459 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000460 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000461 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000462 }
463
464 // We have an identifier; check whether it is actually a type.
Mike Stump1eb44332009-09-09 15:08:12 +0000465 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor42c39f32009-08-26 18:27:52 +0000466 Tok.getLocation(), CurScope, SS,
467 true);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000468 if (!Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000469 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000470 : diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000471 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000472 }
473
474 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000475 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000476 return Type;
477}
478
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000479/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
480/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
481/// until we reach the start of a definition or see a token that
482/// cannot start a definition.
483///
484/// class-specifier: [C++ class]
485/// class-head '{' member-specification[opt] '}'
486/// class-head '{' member-specification[opt] '}' attributes[opt]
487/// class-head:
488/// class-key identifier[opt] base-clause[opt]
489/// class-key nested-name-specifier identifier base-clause[opt]
490/// class-key nested-name-specifier[opt] simple-template-id
491/// base-clause[opt]
492/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000493/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000494/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000495/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000496/// simple-template-id base-clause[opt]
497/// class-key:
498/// 'class'
499/// 'struct'
500/// 'union'
501///
502/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000503/// class-key ::[opt] nested-name-specifier[opt] identifier
504/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
505/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000506///
507/// Note that the C++ class-specifier and elaborated-type-specifier,
508/// together, subsume the C99 struct-or-union-specifier:
509///
510/// struct-or-union-specifier: [C99 6.7.2.1]
511/// struct-or-union identifier[opt] '{' struct-contents '}'
512/// struct-or-union identifier
513/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
514/// '}' attributes[opt]
515/// [GNU] struct-or-union attributes[opt] identifier
516/// struct-or-union:
517/// 'struct'
518/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000519void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
520 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000521 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000522 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000523 DeclSpec::TST TagType;
524 if (TagTokKind == tok::kw_struct)
525 TagType = DeclSpec::TST_struct;
526 else if (TagTokKind == tok::kw_class)
527 TagType = DeclSpec::TST_class;
528 else {
529 assert(TagTokKind == tok::kw_union && "Not a class specifier");
530 TagType = DeclSpec::TST_union;
531 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000532
Douglas Gregor374929f2009-09-18 15:37:17 +0000533 if (Tok.is(tok::code_completion)) {
534 // Code completion for a struct, class, or union name.
535 Actions.CodeCompleteTag(CurScope, TagType);
536 ConsumeToken();
537 }
538
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000539 AttributeList *Attr = 0;
540 // If attributes exist after tag, parse them.
541 if (Tok.is(tok::kw___attribute))
542 Attr = ParseAttributes();
543
Steve Narofff59e17e2008-12-24 20:59:21 +0000544 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000545 if (Tok.is(tok::kw___declspec))
546 Attr = ParseMicrosoftDeclSpec(Attr);
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Douglas Gregorb117a602009-09-04 05:53:02 +0000548 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
549 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
550 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000551 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000552 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
553 // properly.
554 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
555 Tok.setKind(tok::identifier);
556 }
557
558 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
559 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
560 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000561 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000562 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
563 // properly.
564 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
565 Tok.setKind(tok::identifier);
566 }
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000568 // Parse the (optional) nested-name-specifier.
569 CXXScopeSpec SS;
Mike Stump1eb44332009-09-09 15:08:12 +0000570 if (getLang().CPlusPlus &&
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000571 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
Douglas Gregor39a8de12009-02-25 19:37:18 +0000572 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000573 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000574
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000575 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
576
Douglas Gregorcc636682009-02-17 23:15:12 +0000577 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000578 IdentifierInfo *Name = 0;
579 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000580 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000581 if (Tok.is(tok::identifier)) {
582 Name = Tok.getIdentifierInfo();
583 NameLoc = ConsumeToken();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000584
585 if (Tok.is(tok::less)) {
586 // The name was supposed to refer to a template, but didn't.
587 // Eat the template argument list and try to continue parsing this as
588 // a class (or template thereof).
589 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000590 SourceLocation LAngleLoc, RAngleLoc;
591 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
592 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000593 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000594 // We couldn't parse the template argument list at all, so don't
595 // try to give any location information for the list.
596 LAngleLoc = RAngleLoc = SourceLocation();
597 }
598
599 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000600 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000601 << (TagType == DeclSpec::TST_class? 0
602 : TagType == DeclSpec::TST_struct? 1
603 : 2)
604 << Name
605 << SourceRange(LAngleLoc, RAngleLoc);
606
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000607 // Strip off the last template parameter list if it was empty, since
608 // we've removed its template argument list.
609 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
610 if (TemplateParams && TemplateParams->size() > 1) {
611 TemplateParams->pop_back();
612 } else {
613 TemplateParams = 0;
614 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
615 = ParsedTemplateInfo::NonTemplate;
616 }
617 } else if (TemplateInfo.Kind
618 == ParsedTemplateInfo::ExplicitInstantiation) {
619 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000620 TemplateParams = 0;
621 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
622 = ParsedTemplateInfo::NonTemplate;
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000623 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
624 = SourceLocation();
625 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
626 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000627 }
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000628
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000629
630 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000631 } else if (Tok.is(tok::annot_template_id)) {
632 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
633 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000634
Douglas Gregorc45c2322009-03-31 00:43:58 +0000635 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000636 // The template-name in the simple-template-id refers to
637 // something other than a class template. Give an appropriate
638 // error message and skip to the ';'.
639 SourceRange Range(NameLoc);
640 if (SS.isNotEmpty())
641 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000642
Douglas Gregor39a8de12009-02-25 19:37:18 +0000643 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
644 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Douglas Gregor39a8de12009-02-25 19:37:18 +0000646 DS.SetTypeSpecError();
647 SkipUntil(tok::semi, false, true);
648 TemplateId->Destroy();
649 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000650 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000651 }
652
John McCall67d1a672009-08-06 02:15:43 +0000653 // There are four options here. If we have 'struct foo;', then this
654 // is either a forward declaration or a friend declaration, which
655 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000656 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000657 // something like 'struct foo xyz', a reference.
John McCall0f434ec2009-07-31 02:45:11 +0000658 Action::TagUseKind TUK;
Douglas Gregord85bea22009-09-26 06:47:28 +0000659 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) {
660 if (DS.isFriendSpecified()) {
661 // C++ [class.friend]p2:
662 // A class shall not be defined in a friend declaration.
663 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
664 << SourceRange(DS.getFriendSpecLoc());
665
666 // Skip everything up to the semicolon, so that this looks like a proper
667 // friend class (or template thereof) declaration.
668 SkipUntil(tok::semi, true, true);
669 TUK = Action::TUK_Friend;
670 } else {
671 // Okay, this is a class definition.
672 TUK = Action::TUK_Definition;
673 }
674 } else if (Tok.is(tok::semi))
John McCall67d1a672009-08-06 02:15:43 +0000675 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000676 else
John McCall0f434ec2009-07-31 02:45:11 +0000677 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000678
John McCall0f434ec2009-07-31 02:45:11 +0000679 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000680 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000681 Diag(StartLoc, diag::err_anon_type_definition)
682 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000683
684 // Skip the rest of this declarator, up until the comma or semicolon.
685 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000686
687 if (TemplateId)
688 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000689 return;
690 }
691
Douglas Gregorddc29e12009-02-06 22:42:48 +0000692 // Create the tag portion of the class or class template.
John McCallc4e70192009-09-11 04:59:25 +0000693 Action::DeclResult TagOrTempResult = true; // invalid
694 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000695
John McCall0f434ec2009-07-31 02:45:11 +0000696 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000697 // to turn that template-id into a type.
698
Douglas Gregor402abb52009-05-28 23:31:59 +0000699 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000700 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000701 // Explicit specialization, class template partial specialization,
702 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000703 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000704 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000705 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000706 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000707 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000708 // This is an explicit instantiation of a class template.
709 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000710 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000711 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000712 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000713 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000714 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000715 SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000716 TemplateTy::make(TemplateId->Template),
717 TemplateId->TemplateNameLoc,
718 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000719 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000720 TemplateId->RAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000721 Attr);
Douglas Gregorfc9cd612009-09-26 20:57:03 +0000722 } else if (TUK == Action::TUK_Reference) {
John McCallc4e70192009-09-11 04:59:25 +0000723 TypeResult
John McCall6b2becf2009-09-08 17:47:29 +0000724 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
725 TemplateId->TemplateNameLoc,
726 TemplateId->LAngleLoc,
727 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000728 TemplateId->RAngleLoc);
729
John McCallc4e70192009-09-11 04:59:25 +0000730 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
731 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000732 } else {
733 // This is an explicit specialization or a class template
734 // partial specialization.
735 TemplateParameterLists FakedParamLists;
736
737 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
738 // This looks like an explicit instantiation, because we have
739 // something like
740 //
741 // template class Foo<X>
742 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000743 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000744 // meant to be an explicit specialization, but the user forgot
745 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000746 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000747
Mike Stump1eb44332009-09-09 15:08:12 +0000748 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000749 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000750 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000751 diag::err_explicit_instantiation_with_definition)
752 << SourceRange(TemplateInfo.TemplateLoc)
753 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
754
755 // Create a fake template parameter list that contains only
756 // "template<>", so that we treat this construct as a class
757 // template specialization.
758 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000759 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000760 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000761 LAngleLoc,
762 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000763 LAngleLoc));
764 TemplateParams = &FakedParamLists;
765 }
766
767 // Build the class template specialization.
768 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000769 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000770 StartLoc, SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000771 TemplateTy::make(TemplateId->Template),
772 TemplateId->TemplateNameLoc,
773 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000774 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000775 TemplateId->RAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000776 Attr,
Mike Stump1eb44332009-09-09 15:08:12 +0000777 Action::MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000778 TemplateParams? &(*TemplateParams)[0] : 0,
779 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000780 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000781 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000782 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000783 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000784 // Explicit instantiation of a member of a class template
785 // specialization, e.g.,
786 //
787 // template struct Outer<int>::Inner;
788 //
789 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000790 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000791 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000792 TemplateInfo.TemplateLoc,
793 TagType, StartLoc, SS, Name,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000794 NameLoc, Attr);
795 } else {
796 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000797 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000798 // FIXME: Diagnose this particular error.
799 }
800
John McCallc4e70192009-09-11 04:59:25 +0000801 bool IsDependent = false;
802
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000803 // Declaration or definition of a class type
Mike Stump1eb44332009-09-09 15:08:12 +0000804 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000805 Name, NameLoc, Attr, AS,
Mike Stump1eb44332009-09-09 15:08:12 +0000806 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000807 TemplateParams? &(*TemplateParams)[0] : 0,
808 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000809 Owned, IsDependent);
810
811 // If ActOnTag said the type was dependent, try again with the
812 // less common call.
813 if (IsDependent)
814 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
815 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000816 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000817
818 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000819 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000820 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000821
822 // If there is a body, parse it and inform the actions module.
823 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000824 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000825 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000826 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000827 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall0f434ec2009-07-31 02:45:11 +0000828 else if (TUK == Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000829 // FIXME: Complain that we have a base-specifier list but no
830 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000831 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000832 }
833
John McCallc4e70192009-09-11 04:59:25 +0000834 void *Result;
835 if (!TypeResult.isInvalid()) {
836 TagType = DeclSpec::TST_typename;
837 Result = TypeResult.get();
838 Owned = false;
839 } else if (!TagOrTempResult.isInvalid()) {
840 Result = TagOrTempResult.get().getAs<void>();
841 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000842 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000843 return;
844 }
Mike Stump1eb44332009-09-09 15:08:12 +0000845
John McCallfec54012009-08-03 20:12:06 +0000846 const char *PrevSpec = 0;
847 unsigned DiagID;
John McCallc4e70192009-09-11 04:59:25 +0000848
John McCallfec54012009-08-03 20:12:06 +0000849 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
John McCallc4e70192009-09-11 04:59:25 +0000850 Result, Owned))
John McCallfec54012009-08-03 20:12:06 +0000851 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000852}
853
Mike Stump1eb44332009-09-09 15:08:12 +0000854/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000855///
856/// base-clause : [C++ class.derived]
857/// ':' base-specifier-list
858/// base-specifier-list:
859/// base-specifier '...'[opt]
860/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000861void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000862 assert(Tok.is(tok::colon) && "Not a base clause");
863 ConsumeToken();
864
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000865 // Build up an array of parsed base specifiers.
866 llvm::SmallVector<BaseTy *, 8> BaseInfo;
867
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000868 while (true) {
869 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000870 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000871 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000872 // Skip the rest of this base specifier, up until the comma or
873 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000874 SkipUntil(tok::comma, tok::l_brace, true, true);
875 } else {
876 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000877 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000878 }
879
880 // If the next token is a comma, consume it and keep reading
881 // base-specifiers.
882 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000884 // Consume the comma.
885 ConsumeToken();
886 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000887
888 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000889 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000890}
891
892/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
893/// one entry in the base class list of a class specifier, for example:
894/// class foo : public bar, virtual private baz {
895/// 'public bar' and 'virtual private baz' are each base-specifiers.
896///
897/// base-specifier: [C++ class.derived]
898/// ::[opt] nested-name-specifier[opt] class-name
899/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
900/// class-name
901/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
902/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000903Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000904 bool IsVirtual = false;
905 SourceLocation StartLoc = Tok.getLocation();
906
907 // Parse the 'virtual' keyword.
908 if (Tok.is(tok::kw_virtual)) {
909 ConsumeToken();
910 IsVirtual = true;
911 }
912
913 // Parse an (optional) access specifier.
914 AccessSpecifier Access = getAccessSpecifierIfPresent();
915 if (Access)
916 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000918 // Parse the 'virtual' keyword (again!), in case it came after the
919 // access specifier.
920 if (Tok.is(tok::kw_virtual)) {
921 SourceLocation VirtualLoc = ConsumeToken();
922 if (IsVirtual) {
923 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000924 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000925 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000926 }
927
928 IsVirtual = true;
929 }
930
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000931 // Parse optional '::' and optional nested-name-specifier.
932 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000933 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000934
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000935 // The location of the base class itself.
936 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000937
938 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000939 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000940 TypeResult BaseType = ParseClassName(EndLocation, &SS);
941 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000942 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000943
944 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000945 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000947 // Notify semantic analysis that we have parsed a complete
948 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000949 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000950 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000951}
952
953/// getAccessSpecifierIfPresent - Determine whether the next token is
954/// a C++ access-specifier.
955///
956/// access-specifier: [C++ class.derived]
957/// 'private'
958/// 'protected'
959/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +0000960AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000961 switch (Tok.getKind()) {
962 default: return AS_none;
963 case tok::kw_private: return AS_private;
964 case tok::kw_protected: return AS_protected;
965 case tok::kw_public: return AS_public;
966 }
967}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000968
Eli Friedmand33133c2009-07-22 21:45:50 +0000969void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
970 DeclPtrTy ThisDecl) {
971 // We just declared a member function. If this member function
972 // has any default arguments, we'll need to parse them later.
973 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000974 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +0000975 = DeclaratorInfo.getTypeObject(0).Fun;
976 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
977 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
978 if (!LateMethod) {
979 // Push this method onto the stack of late-parsed method
980 // declarations.
981 getCurrentClass().MethodDecls.push_back(
982 LateParsedMethodDeclaration(ThisDecl));
983 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +0000984 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +0000985
986 // Add all of the parameters prior to this one (they don't
987 // have default arguments).
988 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
989 for (unsigned I = 0; I < ParamIdx; ++I)
990 LateMethod->DefaultArgs.push_back(
991 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
992 }
993
994 // Add this parameter to the list of parameters (it or may
995 // not have a default argument).
996 LateMethod->DefaultArgs.push_back(
997 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
998 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
999 }
1000 }
1001}
1002
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001003/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1004///
1005/// member-declaration:
1006/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1007/// function-definition ';'[opt]
1008/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1009/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001010/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001011/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001012/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001013///
1014/// member-declarator-list:
1015/// member-declarator
1016/// member-declarator-list ',' member-declarator
1017///
1018/// member-declarator:
1019/// declarator pure-specifier[opt]
1020/// declarator constant-initializer[opt]
1021/// identifier[opt] ':' constant-expression
1022///
Sebastian Redle2b68332009-04-12 17:16:29 +00001023/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001024/// '= 0'
1025///
1026/// constant-initializer:
1027/// '=' constant-expression
1028///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001029void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
1030 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001031 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001032 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001033 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001034 SourceLocation DeclEnd;
1035 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001036 return;
1037 }
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Chris Lattner682bf922009-03-29 16:50:03 +00001039 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001040 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001041 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001042 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001043 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001044 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001045 return;
1046 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001047
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001048 // Handle: member-declaration ::= '__extension__' member-declaration
1049 if (Tok.is(tok::kw___extension__)) {
1050 // __extension__ silences extension warnings in the subexpression.
1051 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1052 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +00001053 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001054 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001055
1056 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001057 // FIXME: Check for template aliases
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001059 // Eat 'using'.
1060 SourceLocation UsingLoc = ConsumeToken();
1061
1062 if (Tok.is(tok::kw_namespace)) {
1063 Diag(UsingLoc, diag::err_using_namespace_in_class);
1064 SkipUntil(tok::semi, true, true);
1065 }
1066 else {
1067 SourceLocation DeclEnd;
1068 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001069 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001070 }
1071 return;
1072 }
1073
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001074 SourceLocation DSStart = Tok.getLocation();
1075 // decl-specifier-seq:
1076 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001077 ParsingDeclSpec DS(*this);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001078 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001079
John McCalldd4a3b02009-09-16 22:47:08 +00001080 Action::MultiTemplateParamsArg TemplateParams(Actions,
1081 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1082 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1083
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001084 if (Tok.is(tok::semi)) {
1085 ConsumeToken();
Douglas Gregord85bea22009-09-26 06:47:28 +00001086 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +00001087 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001088 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001089
John McCall54abf7d2009-11-04 02:18:39 +00001090 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001091
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001092 if (Tok.isNot(tok::colon)) {
1093 // Parse the first declarator.
1094 ParseDeclarator(DeclaratorInfo);
1095 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001096 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001097 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001098 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001099 if (Tok.is(tok::semi))
1100 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001101 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001102 }
1103
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001104 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001105 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001106 || (DeclaratorInfo.isFunctionDeclarator() &&
1107 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001108 if (!DeclaratorInfo.isFunctionDeclarator()) {
1109 Diag(Tok, diag::err_func_def_no_params);
1110 ConsumeBrace();
1111 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001112 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001113 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001114
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001115 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1116 Diag(Tok, diag::err_function_declared_typedef);
1117 // This recovery skips the entire function body. It would be nice
1118 // to simply call ParseCXXInlineMethodDef() below, however Sema
1119 // assumes the declarator represents a function, not a typedef.
1120 ConsumeBrace();
1121 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001122 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001123 }
1124
Douglas Gregor37b372b2009-08-20 22:52:58 +00001125 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001126 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001127 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001128 }
1129
1130 // member-declarator-list:
1131 // member-declarator
1132 // member-declarator-list ',' member-declarator
1133
Chris Lattner682bf922009-03-29 16:50:03 +00001134 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001135 OwningExprResult BitfieldSize(Actions);
1136 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001137 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001138
1139 while (1) {
1140
1141 // member-declarator:
1142 // declarator pure-specifier[opt]
1143 // declarator constant-initializer[opt]
1144 // identifier[opt] ':' constant-expression
1145
1146 if (Tok.is(tok::colon)) {
1147 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001148 BitfieldSize = ParseConstantExpression();
1149 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001150 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001151 }
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001153 // pure-specifier:
1154 // '= 0'
1155 //
1156 // constant-initializer:
1157 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001158 //
1159 // defaulted/deleted function-definition:
1160 // '=' 'default' [TODO]
1161 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001162
1163 if (Tok.is(tok::equal)) {
1164 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001165 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1166 ConsumeToken();
1167 Deleted = true;
1168 } else {
1169 Init = ParseInitializer();
1170 if (Init.isInvalid())
1171 SkipUntil(tok::comma, true, true);
1172 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001173 }
1174
1175 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001176 if (Tok.is(tok::kw___attribute)) {
1177 SourceLocation Loc;
1178 AttributeList *AttrList = ParseAttributes(&Loc);
1179 DeclaratorInfo.AddAttributes(AttrList, Loc);
1180 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001181
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001182 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001183 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001184 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001185
1186 DeclPtrTy ThisDecl;
1187 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001188 // TODO: handle initializers, bitfields, 'delete'
1189 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1190 /*IsDefinition*/ false,
1191 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001192 } else {
John McCall67d1a672009-08-06 02:15:43 +00001193 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1194 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001195 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001196 BitfieldSize.release(),
1197 Init.release(),
1198 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001199 }
Chris Lattner682bf922009-03-29 16:50:03 +00001200 if (ThisDecl)
1201 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001202
Douglas Gregor72b505b2008-12-16 21:30:33 +00001203 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001204 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001205 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001206 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001207 }
1208
John McCall54abf7d2009-11-04 02:18:39 +00001209 DeclaratorInfo.complete(ThisDecl);
1210
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001211 // If we don't have a comma, it is either the end of the list (a ';')
1212 // or an error, bail out.
1213 if (Tok.isNot(tok::comma))
1214 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001216 // Consume the comma.
1217 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001219 // Parse the next declarator.
1220 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001221 BitfieldSize = 0;
1222 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001223 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001225 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001226 if (Tok.is(tok::kw___attribute)) {
1227 SourceLocation Loc;
1228 AttributeList *AttrList = ParseAttributes(&Loc);
1229 DeclaratorInfo.AddAttributes(AttrList, Loc);
1230 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001231
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001232 if (Tok.isNot(tok::colon))
1233 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001234 }
1235
1236 if (Tok.is(tok::semi)) {
1237 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +00001238 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +00001239 DeclsInGroup.size());
1240 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001241 }
1242
1243 Diag(Tok, diag::err_expected_semi_decl_list);
1244 // Skip to end of block or statement
1245 SkipUntil(tok::r_brace, true, true);
1246 if (Tok.is(tok::semi))
1247 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001248 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001249}
1250
1251/// ParseCXXMemberSpecification - Parse the class definition.
1252///
1253/// member-specification:
1254/// member-declaration member-specification[opt]
1255/// access-specifier ':' member-specification[opt]
1256///
1257void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001258 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001259 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001260 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001261 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001262
Chris Lattner49f28ca2009-03-05 08:00:35 +00001263 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1264 PP.getSourceManager(),
1265 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001267 SourceLocation LBraceLoc = ConsumeBrace();
1268
Douglas Gregor6569d682009-05-27 23:11:45 +00001269 // Determine whether this is a top-level (non-nested) class.
Mike Stump1eb44332009-09-09 15:08:12 +00001270 bool TopLevelClass = ClassStack.empty() ||
Douglas Gregor6569d682009-05-27 23:11:45 +00001271 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001272
1273 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001274 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001275
Douglas Gregor6569d682009-05-27 23:11:45 +00001276 // Note that we are parsing a new (potentially-nested) class definition.
1277 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1278
Douglas Gregorddc29e12009-02-06 22:42:48 +00001279 if (TagDecl)
1280 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1281 else {
1282 SkipUntil(tok::r_brace, false, false);
1283 return;
1284 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001285
1286 // C++ 11p3: Members of a class defined with the keyword class are private
1287 // by default. Members of a class defined with the keywords struct or union
1288 // are public by default.
1289 AccessSpecifier CurAS;
1290 if (TagType == DeclSpec::TST_class)
1291 CurAS = AS_private;
1292 else
1293 CurAS = AS_public;
1294
1295 // While we still have something to read, read the member-declarations.
1296 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1297 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001299 // Check for extraneous top-level semicolon.
1300 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +00001301 Diag(Tok, diag::ext_extra_struct_semi)
1302 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001303 ConsumeToken();
1304 continue;
1305 }
1306
1307 AccessSpecifier AS = getAccessSpecifierIfPresent();
1308 if (AS != AS_none) {
1309 // Current token is a C++ access specifier.
1310 CurAS = AS;
1311 ConsumeToken();
1312 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1313 continue;
1314 }
1315
Douglas Gregor37b372b2009-08-20 22:52:58 +00001316 // FIXME: Make sure we don't have a template here.
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001318 // Parse all the comma separated declarators.
1319 ParseCXXClassMemberDeclaration(CurAS);
1320 }
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001322 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001324 AttributeList *AttrList = 0;
1325 // If attributes exist after class contents, parse them.
1326 if (Tok.is(tok::kw___attribute))
1327 AttrList = ParseAttributes(); // FIXME: where should I put them?
1328
1329 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1330 LBraceLoc, RBraceLoc);
1331
1332 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1333 // complete within function bodies, default arguments,
1334 // exception-specifications, and constructor ctor-initializers (including
1335 // such things in nested classes).
1336 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001337 // FIXME: Only function bodies and constructor ctor-initializers are
1338 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001339 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001340 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001341 // are complete and we can parse the delayed portions of method
1342 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001343 ParseLexedMethodDeclarations(getCurrentClass());
1344 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001345 }
1346
1347 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001348 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001349 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001350
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001351 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001352}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001353
1354/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1355/// which explicitly initializes the members or base classes of a
1356/// class (C++ [class.base.init]). For example, the three initializers
1357/// after the ':' in the Derived constructor below:
1358///
1359/// @code
1360/// class Base { };
1361/// class Derived : Base {
1362/// int x;
1363/// float f;
1364/// public:
1365/// Derived(float f) : Base(), x(17), f(f) { }
1366/// };
1367/// @endcode
1368///
Mike Stump1eb44332009-09-09 15:08:12 +00001369/// [C++] ctor-initializer:
1370/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001371///
Mike Stump1eb44332009-09-09 15:08:12 +00001372/// [C++] mem-initializer-list:
1373/// mem-initializer
1374/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001375void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001376 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1377
1378 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Douglas Gregor7ad83902008-11-05 04:29:56 +00001380 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Douglas Gregor7ad83902008-11-05 04:29:56 +00001382 do {
1383 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001384 if (!MemInit.isInvalid())
1385 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001386
1387 if (Tok.is(tok::comma))
1388 ConsumeToken();
1389 else if (Tok.is(tok::l_brace))
1390 break;
1391 else {
1392 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001393 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001394 SkipUntil(tok::l_brace, true, true);
1395 break;
1396 }
1397 } while (true);
1398
Mike Stump1eb44332009-09-09 15:08:12 +00001399 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001400 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001401}
1402
1403/// ParseMemInitializer - Parse a C++ member initializer, which is
1404/// part of a constructor initializer that explicitly initializes one
1405/// member or base class (C++ [class.base.init]). See
1406/// ParseConstructorInitializer for an example.
1407///
1408/// [C++] mem-initializer:
1409/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001410///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001411/// [C++] mem-initializer-id:
1412/// '::'[opt] nested-name-specifier[opt] class-name
1413/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001414Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001415 // parse '::'[opt] nested-name-specifier[opt]
1416 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001417 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001418 TypeTy *TemplateTypeTy = 0;
1419 if (Tok.is(tok::annot_template_id)) {
1420 TemplateIdAnnotation *TemplateId
1421 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1422 if (TemplateId->Kind == TNK_Type_template) {
1423 AnnotateTemplateIdTokenAsType(&SS);
1424 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1425 TemplateTypeTy = Tok.getAnnotationValue();
1426 }
1427 // FIXME. May need to check for TNK_Dependent_template as well.
1428 }
1429 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001430 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001431 return true;
1432 }
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Douglas Gregor7ad83902008-11-05 04:29:56 +00001434 // Get the identifier. This may be a member name or a class name,
1435 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001436 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001437 SourceLocation IdLoc = ConsumeToken();
1438
1439 // Parse the '('.
1440 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001441 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001442 return true;
1443 }
1444 SourceLocation LParenLoc = ConsumeParen();
1445
1446 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001447 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001448 CommaLocsTy CommaLocs;
1449 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1450 SkipUntil(tok::r_paren);
1451 return true;
1452 }
1453
1454 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1455
Fariborz Jahanian96174332009-07-01 19:21:19 +00001456 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1457 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001458 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001459 ArgExprs.size(), CommaLocs.data(),
1460 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001461}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001462
1463/// ParseExceptionSpecification - Parse a C++ exception-specification
1464/// (C++ [except.spec]).
1465///
Douglas Gregora4745612008-12-01 18:00:20 +00001466/// exception-specification:
1467/// 'throw' '(' type-id-list [opt] ')'
1468/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001469///
Douglas Gregora4745612008-12-01 18:00:20 +00001470/// type-id-list:
1471/// type-id
1472/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001473///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001474bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001475 llvm::SmallVector<TypeTy*, 2>
1476 &Exceptions,
1477 llvm::SmallVector<SourceRange, 2>
1478 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001479 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001480 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001482 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001484 if (!Tok.is(tok::l_paren)) {
1485 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1486 }
1487 SourceLocation LParenLoc = ConsumeParen();
1488
Douglas Gregora4745612008-12-01 18:00:20 +00001489 // Parse throw(...), a Microsoft extension that means "this function
1490 // can throw anything".
1491 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001492 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001493 SourceLocation EllipsisLoc = ConsumeToken();
1494 if (!getLang().Microsoft)
1495 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001496 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001497 return false;
1498 }
1499
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001500 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001501 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001502 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001503 TypeResult Res(ParseTypeName(&Range));
1504 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001505 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001506 Ranges.push_back(Range);
1507 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001508 if (Tok.is(tok::comma))
1509 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001510 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001511 break;
1512 }
1513
Sebastian Redlab197ba2009-02-09 18:23:29 +00001514 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001515 return false;
1516}
Douglas Gregor6569d682009-05-27 23:11:45 +00001517
1518/// \brief We have just started parsing the definition of a new class,
1519/// so push that class onto our stack of classes that is currently
1520/// being parsed.
1521void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001522 assert((TopLevelClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001523 "Nested class without outer class");
1524 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1525}
1526
1527/// \brief Deallocate the given parsed class and all of its nested
1528/// classes.
1529void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1530 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1531 DeallocateParsedClasses(Class->NestedClasses[I]);
1532 delete Class;
1533}
1534
1535/// \brief Pop the top class of the stack of classes that are
1536/// currently being parsed.
1537///
1538/// This routine should be called when we have finished parsing the
1539/// definition of a class, but have not yet popped the Scope
1540/// associated with the class's definition.
1541///
1542/// \returns true if the class we've popped is a top-level class,
1543/// false otherwise.
1544void Parser::PopParsingClass() {
1545 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Douglas Gregor6569d682009-05-27 23:11:45 +00001547 ParsingClass *Victim = ClassStack.top();
1548 ClassStack.pop();
1549 if (Victim->TopLevelClass) {
1550 // Deallocate all of the nested classes of this class,
1551 // recursively: we don't need to keep any of this information.
1552 DeallocateParsedClasses(Victim);
1553 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001554 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001555 assert(!ClassStack.empty() && "Missing top-level class?");
1556
1557 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1558 Victim->NestedClasses.empty()) {
1559 // The victim is a nested class, but we will not need to perform
1560 // any processing after the definition of this class since it has
1561 // no members whose handling was delayed. Therefore, we can just
1562 // remove this nested class.
1563 delete Victim;
1564 return;
1565 }
1566
1567 // This nested class has some members that will need to be processed
1568 // after the top-level class is completely defined. Therefore, add
1569 // it to the list of nested classes within its parent.
1570 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1571 ClassStack.top()->NestedClasses.push_back(Victim);
1572 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1573}