blob: 914bfc9db89d9aefa2c1fe2a1bfddec2e989c5ef [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;
John McCall7ba107a2009-11-18 02:36:19 +0000286 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000287 bool IsTypeName;
288
289 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000290 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000291 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000292 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000293 ConsumeToken();
294 IsTypeName = true;
295 }
296 else
297 IsTypeName = false;
298
299 // Parse nested-name-specifier.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000300 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000301
302 AttributeList *AttrList = 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000303
304 // Check nested-name specifier.
305 if (SS.isInvalid()) {
306 SkipUntil(tok::semi);
307 return DeclPtrTy();
308 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000309
310 // Parse the unqualified-id. We allow parsing of both constructor and
311 // destructor names and allow the action module to diagnose any semantic
312 // errors.
313 UnqualifiedId Name;
314 if (ParseUnqualifiedId(SS,
315 /*EnteringContext=*/false,
316 /*AllowDestructorName=*/true,
317 /*AllowConstructorName=*/true,
318 /*ObjectType=*/0,
319 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000320 SkipUntil(tok::semi);
321 return DeclPtrTy();
322 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000323
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000324 // Parse (optional) attributes (most likely GNU strong-using extension).
325 if (Tok.is(tok::kw___attribute))
326 AttrList = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000328 // Eat ';'.
329 DeclEnd = Tok.getLocation();
330 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000331 AttrList ? "attributes list" : "using declaration",
332 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000333
Douglas Gregor12c118a2009-11-04 16:30:06 +0000334 return Actions.ActOnUsingDeclaration(CurScope, AS, UsingLoc, SS, Name,
John McCall7ba107a2009-11-18 02:36:19 +0000335 AttrList, IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000336}
337
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000338/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
339///
340/// static_assert-declaration:
341/// static_assert ( constant-expression , string-literal ) ;
342///
Chris Lattner97144fc2009-04-02 04:16:50 +0000343Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000344 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
345 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000347 if (Tok.isNot(tok::l_paren)) {
348 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000349 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000350 }
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000352 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000353
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000354 OwningExprResult AssertExpr(ParseConstantExpression());
355 if (AssertExpr.isInvalid()) {
356 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000357 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Anders Carlssonad5f9602009-03-13 23:29:20 +0000360 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000361 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000362
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000363 if (Tok.isNot(tok::string_literal)) {
364 Diag(Tok, diag::err_expected_string_literal);
365 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000366 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000367 }
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000369 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000370 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000371 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000372
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000373 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Chris Lattner97144fc2009-04-02 04:16:50 +0000375 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000376 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
377
Mike Stump1eb44332009-09-09 15:08:12 +0000378 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000379 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000380}
381
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000382/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
383///
384/// 'decltype' ( expression )
385///
386void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
387 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
388
389 SourceLocation StartLoc = ConsumeToken();
390 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000391
392 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000393 "decltype")) {
394 SkipUntil(tok::r_paren);
395 return;
396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000398 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000400 // C++0x [dcl.type.simple]p4:
401 // The operand of the decltype specifier is an unevaluated operand.
402 EnterExpressionEvaluationContext Unevaluated(Actions,
403 Action::Unevaluated);
404 OwningExprResult Result = ParseExpression();
405 if (Result.isInvalid()) {
406 SkipUntil(tok::r_paren);
407 return;
408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000410 // Match the ')'
411 SourceLocation RParenLoc;
412 if (Tok.is(tok::r_paren))
413 RParenLoc = ConsumeParen();
414 else
415 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000417 if (RParenLoc.isInvalid())
418 return;
419
420 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000421 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000422 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000423 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000424 DiagID, Result.release()))
425 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000426}
427
Douglas Gregor42a552f2008-11-05 20:51:48 +0000428/// ParseClassName - Parse a C++ class-name, which names a class. Note
429/// that we only check that the result names a type; semantic analysis
430/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000431/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000432/// found.
433///
434/// class-name: [C++ 9.1]
435/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000436/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000437///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000438Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000439 const CXXScopeSpec *SS,
440 bool DestrExpected) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000441 // Check whether we have a template-id that names a type.
442 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000443 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000444 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000445 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000446 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000447
448 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
449 TypeTy *Type = Tok.getAnnotationValue();
450 EndLocation = Tok.getAnnotationEndLoc();
451 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000452
453 if (Type)
454 return Type;
455 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000456 }
457
458 // Fall through to produce an error below.
459 }
460
Douglas Gregor42a552f2008-11-05 20:51:48 +0000461 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000462 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000463 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000464 }
465
466 // We have an identifier; check whether it is actually a type.
Mike Stump1eb44332009-09-09 15:08:12 +0000467 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor42c39f32009-08-26 18:27:52 +0000468 Tok.getLocation(), CurScope, SS,
469 true);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000470 if (!Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000471 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000472 : diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000473 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000474 }
475
476 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000477 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000478 return Type;
479}
480
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000481/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
482/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
483/// until we reach the start of a definition or see a token that
484/// cannot start a definition.
485///
486/// class-specifier: [C++ class]
487/// class-head '{' member-specification[opt] '}'
488/// class-head '{' member-specification[opt] '}' attributes[opt]
489/// class-head:
490/// class-key identifier[opt] base-clause[opt]
491/// class-key nested-name-specifier identifier base-clause[opt]
492/// class-key nested-name-specifier[opt] simple-template-id
493/// base-clause[opt]
494/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000495/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000496/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000497/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000498/// simple-template-id base-clause[opt]
499/// class-key:
500/// 'class'
501/// 'struct'
502/// 'union'
503///
504/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000505/// class-key ::[opt] nested-name-specifier[opt] identifier
506/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
507/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000508///
509/// Note that the C++ class-specifier and elaborated-type-specifier,
510/// together, subsume the C99 struct-or-union-specifier:
511///
512/// struct-or-union-specifier: [C99 6.7.2.1]
513/// struct-or-union identifier[opt] '{' struct-contents '}'
514/// struct-or-union identifier
515/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
516/// '}' attributes[opt]
517/// [GNU] struct-or-union attributes[opt] identifier
518/// struct-or-union:
519/// 'struct'
520/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000521void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
522 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000523 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000524 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000525 DeclSpec::TST TagType;
526 if (TagTokKind == tok::kw_struct)
527 TagType = DeclSpec::TST_struct;
528 else if (TagTokKind == tok::kw_class)
529 TagType = DeclSpec::TST_class;
530 else {
531 assert(TagTokKind == tok::kw_union && "Not a class specifier");
532 TagType = DeclSpec::TST_union;
533 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000534
Douglas Gregor374929f2009-09-18 15:37:17 +0000535 if (Tok.is(tok::code_completion)) {
536 // Code completion for a struct, class, or union name.
537 Actions.CodeCompleteTag(CurScope, TagType);
538 ConsumeToken();
539 }
540
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000541 AttributeList *Attr = 0;
542 // If attributes exist after tag, parse them.
543 if (Tok.is(tok::kw___attribute))
544 Attr = ParseAttributes();
545
Steve Narofff59e17e2008-12-24 20:59:21 +0000546 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000547 if (Tok.is(tok::kw___declspec))
548 Attr = ParseMicrosoftDeclSpec(Attr);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Douglas Gregorb117a602009-09-04 05:53:02 +0000550 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
551 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
552 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000553 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000554 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
555 // properly.
556 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
557 Tok.setKind(tok::identifier);
558 }
559
560 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
561 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
562 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000563 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000564 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
565 // properly.
566 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
567 Tok.setKind(tok::identifier);
568 }
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000570 // Parse the (optional) nested-name-specifier.
571 CXXScopeSpec SS;
Mike Stump1eb44332009-09-09 15:08:12 +0000572 if (getLang().CPlusPlus &&
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000573 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
Douglas Gregor39a8de12009-02-25 19:37:18 +0000574 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000575 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000576
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000577 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
578
Douglas Gregorcc636682009-02-17 23:15:12 +0000579 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000580 IdentifierInfo *Name = 0;
581 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000582 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000583 if (Tok.is(tok::identifier)) {
584 Name = Tok.getIdentifierInfo();
585 NameLoc = ConsumeToken();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000586
587 if (Tok.is(tok::less)) {
588 // The name was supposed to refer to a template, but didn't.
589 // Eat the template argument list and try to continue parsing this as
590 // a class (or template thereof).
591 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000592 SourceLocation LAngleLoc, RAngleLoc;
593 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
594 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000595 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000596 // We couldn't parse the template argument list at all, so don't
597 // try to give any location information for the list.
598 LAngleLoc = RAngleLoc = SourceLocation();
599 }
600
601 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000602 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000603 << (TagType == DeclSpec::TST_class? 0
604 : TagType == DeclSpec::TST_struct? 1
605 : 2)
606 << Name
607 << SourceRange(LAngleLoc, RAngleLoc);
608
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000609 // Strip off the last template parameter list if it was empty, since
610 // we've removed its template argument list.
611 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
612 if (TemplateParams && TemplateParams->size() > 1) {
613 TemplateParams->pop_back();
614 } else {
615 TemplateParams = 0;
616 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
617 = ParsedTemplateInfo::NonTemplate;
618 }
619 } else if (TemplateInfo.Kind
620 == ParsedTemplateInfo::ExplicitInstantiation) {
621 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000622 TemplateParams = 0;
623 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
624 = ParsedTemplateInfo::NonTemplate;
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000625 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
626 = SourceLocation();
627 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
628 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000629 }
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000630
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000631
632 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000633 } else if (Tok.is(tok::annot_template_id)) {
634 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
635 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000636
Douglas Gregorc45c2322009-03-31 00:43:58 +0000637 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000638 // The template-name in the simple-template-id refers to
639 // something other than a class template. Give an appropriate
640 // error message and skip to the ';'.
641 SourceRange Range(NameLoc);
642 if (SS.isNotEmpty())
643 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000644
Douglas Gregor39a8de12009-02-25 19:37:18 +0000645 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
646 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Douglas Gregor39a8de12009-02-25 19:37:18 +0000648 DS.SetTypeSpecError();
649 SkipUntil(tok::semi, false, true);
650 TemplateId->Destroy();
651 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000652 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000653 }
654
John McCall67d1a672009-08-06 02:15:43 +0000655 // There are four options here. If we have 'struct foo;', then this
656 // is either a forward declaration or a friend declaration, which
657 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000658 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000659 // something like 'struct foo xyz', a reference.
John McCall0f434ec2009-07-31 02:45:11 +0000660 Action::TagUseKind TUK;
Douglas Gregord85bea22009-09-26 06:47:28 +0000661 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) {
662 if (DS.isFriendSpecified()) {
663 // C++ [class.friend]p2:
664 // A class shall not be defined in a friend declaration.
665 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
666 << SourceRange(DS.getFriendSpecLoc());
667
668 // Skip everything up to the semicolon, so that this looks like a proper
669 // friend class (or template thereof) declaration.
670 SkipUntil(tok::semi, true, true);
671 TUK = Action::TUK_Friend;
672 } else {
673 // Okay, this is a class definition.
674 TUK = Action::TUK_Definition;
675 }
676 } else if (Tok.is(tok::semi))
John McCall67d1a672009-08-06 02:15:43 +0000677 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000678 else
John McCall0f434ec2009-07-31 02:45:11 +0000679 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000680
John McCall0f434ec2009-07-31 02:45:11 +0000681 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000682 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000683 Diag(StartLoc, diag::err_anon_type_definition)
684 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000685
686 // Skip the rest of this declarator, up until the comma or semicolon.
687 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000688
689 if (TemplateId)
690 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000691 return;
692 }
693
Douglas Gregorddc29e12009-02-06 22:42:48 +0000694 // Create the tag portion of the class or class template.
John McCallc4e70192009-09-11 04:59:25 +0000695 Action::DeclResult TagOrTempResult = true; // invalid
696 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000697
John McCall0f434ec2009-07-31 02:45:11 +0000698 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000699 // to turn that template-id into a type.
700
Douglas Gregor402abb52009-05-28 23:31:59 +0000701 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000702 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000703 // Explicit specialization, class template partial specialization,
704 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000705 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000706 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000707 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000708 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000709 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000710 // This is an explicit instantiation of a class template.
711 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000712 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000713 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000714 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000715 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000716 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000717 SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000718 TemplateTy::make(TemplateId->Template),
719 TemplateId->TemplateNameLoc,
720 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000721 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000722 TemplateId->RAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000723 Attr);
Douglas Gregorfc9cd612009-09-26 20:57:03 +0000724 } else if (TUK == Action::TUK_Reference) {
John McCallc4e70192009-09-11 04:59:25 +0000725 TypeResult
John McCall6b2becf2009-09-08 17:47:29 +0000726 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
727 TemplateId->TemplateNameLoc,
728 TemplateId->LAngleLoc,
729 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000730 TemplateId->RAngleLoc);
731
John McCallc4e70192009-09-11 04:59:25 +0000732 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
733 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000734 } else {
735 // This is an explicit specialization or a class template
736 // partial specialization.
737 TemplateParameterLists FakedParamLists;
738
739 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
740 // This looks like an explicit instantiation, because we have
741 // something like
742 //
743 // template class Foo<X>
744 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000745 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000746 // meant to be an explicit specialization, but the user forgot
747 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000748 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000749
Mike Stump1eb44332009-09-09 15:08:12 +0000750 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000751 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000752 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000753 diag::err_explicit_instantiation_with_definition)
754 << SourceRange(TemplateInfo.TemplateLoc)
755 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
756
757 // Create a fake template parameter list that contains only
758 // "template<>", so that we treat this construct as a class
759 // template specialization.
760 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000761 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000762 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000763 LAngleLoc,
764 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000765 LAngleLoc));
766 TemplateParams = &FakedParamLists;
767 }
768
769 // Build the class template specialization.
770 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000771 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000772 StartLoc, SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000773 TemplateTy::make(TemplateId->Template),
774 TemplateId->TemplateNameLoc,
775 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000776 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000777 TemplateId->RAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000778 Attr,
Mike Stump1eb44332009-09-09 15:08:12 +0000779 Action::MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000780 TemplateParams? &(*TemplateParams)[0] : 0,
781 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000782 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000783 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000784 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000785 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000786 // Explicit instantiation of a member of a class template
787 // specialization, e.g.,
788 //
789 // template struct Outer<int>::Inner;
790 //
791 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000792 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000793 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000794 TemplateInfo.TemplateLoc,
795 TagType, StartLoc, SS, Name,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000796 NameLoc, Attr);
797 } else {
798 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000799 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000800 // FIXME: Diagnose this particular error.
801 }
802
John McCallc4e70192009-09-11 04:59:25 +0000803 bool IsDependent = false;
804
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000805 // Declaration or definition of a class type
Mike Stump1eb44332009-09-09 15:08:12 +0000806 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000807 Name, NameLoc, Attr, AS,
Mike Stump1eb44332009-09-09 15:08:12 +0000808 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000809 TemplateParams? &(*TemplateParams)[0] : 0,
810 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000811 Owned, IsDependent);
812
813 // If ActOnTag said the type was dependent, try again with the
814 // less common call.
815 if (IsDependent)
816 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
817 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000818 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000819
820 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000821 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000822 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000823
824 // If there is a body, parse it and inform the actions module.
825 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000826 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000827 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000828 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000829 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall0f434ec2009-07-31 02:45:11 +0000830 else if (TUK == Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000831 // FIXME: Complain that we have a base-specifier list but no
832 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000833 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000834 }
835
John McCallc4e70192009-09-11 04:59:25 +0000836 void *Result;
837 if (!TypeResult.isInvalid()) {
838 TagType = DeclSpec::TST_typename;
839 Result = TypeResult.get();
840 Owned = false;
841 } else if (!TagOrTempResult.isInvalid()) {
842 Result = TagOrTempResult.get().getAs<void>();
843 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000844 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000845 return;
846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
John McCallfec54012009-08-03 20:12:06 +0000848 const char *PrevSpec = 0;
849 unsigned DiagID;
John McCallc4e70192009-09-11 04:59:25 +0000850
John McCallfec54012009-08-03 20:12:06 +0000851 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
John McCallc4e70192009-09-11 04:59:25 +0000852 Result, Owned))
John McCallfec54012009-08-03 20:12:06 +0000853 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000854}
855
Mike Stump1eb44332009-09-09 15:08:12 +0000856/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000857///
858/// base-clause : [C++ class.derived]
859/// ':' base-specifier-list
860/// base-specifier-list:
861/// base-specifier '...'[opt]
862/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000863void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000864 assert(Tok.is(tok::colon) && "Not a base clause");
865 ConsumeToken();
866
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000867 // Build up an array of parsed base specifiers.
868 llvm::SmallVector<BaseTy *, 8> BaseInfo;
869
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000870 while (true) {
871 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000872 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000873 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000874 // Skip the rest of this base specifier, up until the comma or
875 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000876 SkipUntil(tok::comma, tok::l_brace, true, true);
877 } else {
878 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000879 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000880 }
881
882 // If the next token is a comma, consume it and keep reading
883 // base-specifiers.
884 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000886 // Consume the comma.
887 ConsumeToken();
888 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000889
890 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000891 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000892}
893
894/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
895/// one entry in the base class list of a class specifier, for example:
896/// class foo : public bar, virtual private baz {
897/// 'public bar' and 'virtual private baz' are each base-specifiers.
898///
899/// base-specifier: [C++ class.derived]
900/// ::[opt] nested-name-specifier[opt] class-name
901/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
902/// class-name
903/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
904/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000905Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000906 bool IsVirtual = false;
907 SourceLocation StartLoc = Tok.getLocation();
908
909 // Parse the 'virtual' keyword.
910 if (Tok.is(tok::kw_virtual)) {
911 ConsumeToken();
912 IsVirtual = true;
913 }
914
915 // Parse an (optional) access specifier.
916 AccessSpecifier Access = getAccessSpecifierIfPresent();
917 if (Access)
918 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000920 // Parse the 'virtual' keyword (again!), in case it came after the
921 // access specifier.
922 if (Tok.is(tok::kw_virtual)) {
923 SourceLocation VirtualLoc = ConsumeToken();
924 if (IsVirtual) {
925 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000926 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000927 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000928 }
929
930 IsVirtual = true;
931 }
932
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000933 // Parse optional '::' and optional nested-name-specifier.
934 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000935 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000936
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000937 // The location of the base class itself.
938 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000939
940 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000941 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000942 TypeResult BaseType = ParseClassName(EndLocation, &SS);
943 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000944 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000945
946 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000947 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000949 // Notify semantic analysis that we have parsed a complete
950 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000951 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000952 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000953}
954
955/// getAccessSpecifierIfPresent - Determine whether the next token is
956/// a C++ access-specifier.
957///
958/// access-specifier: [C++ class.derived]
959/// 'private'
960/// 'protected'
961/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +0000962AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000963 switch (Tok.getKind()) {
964 default: return AS_none;
965 case tok::kw_private: return AS_private;
966 case tok::kw_protected: return AS_protected;
967 case tok::kw_public: return AS_public;
968 }
969}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000970
Eli Friedmand33133c2009-07-22 21:45:50 +0000971void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
972 DeclPtrTy ThisDecl) {
973 // We just declared a member function. If this member function
974 // has any default arguments, we'll need to parse them later.
975 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000976 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +0000977 = DeclaratorInfo.getTypeObject(0).Fun;
978 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
979 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
980 if (!LateMethod) {
981 // Push this method onto the stack of late-parsed method
982 // declarations.
983 getCurrentClass().MethodDecls.push_back(
984 LateParsedMethodDeclaration(ThisDecl));
985 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +0000986 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +0000987
988 // Add all of the parameters prior to this one (they don't
989 // have default arguments).
990 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
991 for (unsigned I = 0; I < ParamIdx; ++I)
992 LateMethod->DefaultArgs.push_back(
993 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
994 }
995
996 // Add this parameter to the list of parameters (it or may
997 // not have a default argument).
998 LateMethod->DefaultArgs.push_back(
999 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1000 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1001 }
1002 }
1003}
1004
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001005/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1006///
1007/// member-declaration:
1008/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1009/// function-definition ';'[opt]
1010/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1011/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001012/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001013/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001014/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001015///
1016/// member-declarator-list:
1017/// member-declarator
1018/// member-declarator-list ',' member-declarator
1019///
1020/// member-declarator:
1021/// declarator pure-specifier[opt]
1022/// declarator constant-initializer[opt]
1023/// identifier[opt] ':' constant-expression
1024///
Sebastian Redle2b68332009-04-12 17:16:29 +00001025/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001026/// '= 0'
1027///
1028/// constant-initializer:
1029/// '=' constant-expression
1030///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001031void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
1032 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001033 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001034 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001035 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001036 SourceLocation DeclEnd;
1037 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001038 return;
1039 }
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Chris Lattner682bf922009-03-29 16:50:03 +00001041 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001042 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001043 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001044 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001045 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001046 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001047 return;
1048 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001049
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001050 // Handle: member-declaration ::= '__extension__' member-declaration
1051 if (Tok.is(tok::kw___extension__)) {
1052 // __extension__ silences extension warnings in the subexpression.
1053 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1054 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +00001055 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001056 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001057
1058 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001059 // FIXME: Check for template aliases
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001061 // Eat 'using'.
1062 SourceLocation UsingLoc = ConsumeToken();
1063
1064 if (Tok.is(tok::kw_namespace)) {
1065 Diag(UsingLoc, diag::err_using_namespace_in_class);
1066 SkipUntil(tok::semi, true, true);
1067 }
1068 else {
1069 SourceLocation DeclEnd;
1070 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001071 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001072 }
1073 return;
1074 }
1075
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001076 SourceLocation DSStart = Tok.getLocation();
1077 // decl-specifier-seq:
1078 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001079 ParsingDeclSpec DS(*this);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001080 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001081
John McCalldd4a3b02009-09-16 22:47:08 +00001082 Action::MultiTemplateParamsArg TemplateParams(Actions,
1083 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1084 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1085
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001086 if (Tok.is(tok::semi)) {
1087 ConsumeToken();
Douglas Gregord85bea22009-09-26 06:47:28 +00001088 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +00001089 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001090 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001091
John McCall54abf7d2009-11-04 02:18:39 +00001092 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001093
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001094 if (Tok.isNot(tok::colon)) {
1095 // Parse the first declarator.
1096 ParseDeclarator(DeclaratorInfo);
1097 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001098 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001099 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001100 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001101 if (Tok.is(tok::semi))
1102 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001103 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001104 }
1105
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001106 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001107 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001108 || (DeclaratorInfo.isFunctionDeclarator() &&
1109 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001110 if (!DeclaratorInfo.isFunctionDeclarator()) {
1111 Diag(Tok, diag::err_func_def_no_params);
1112 ConsumeBrace();
1113 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001114 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001115 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001116
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001117 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1118 Diag(Tok, diag::err_function_declared_typedef);
1119 // This recovery skips the entire function body. It would be nice
1120 // to simply call ParseCXXInlineMethodDef() below, however Sema
1121 // assumes the declarator represents a function, not a typedef.
1122 ConsumeBrace();
1123 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001124 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001125 }
1126
Douglas Gregor37b372b2009-08-20 22:52:58 +00001127 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001128 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001129 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001130 }
1131
1132 // member-declarator-list:
1133 // member-declarator
1134 // member-declarator-list ',' member-declarator
1135
Chris Lattner682bf922009-03-29 16:50:03 +00001136 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001137 OwningExprResult BitfieldSize(Actions);
1138 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001139 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001140
1141 while (1) {
1142
1143 // member-declarator:
1144 // declarator pure-specifier[opt]
1145 // declarator constant-initializer[opt]
1146 // identifier[opt] ':' constant-expression
1147
1148 if (Tok.is(tok::colon)) {
1149 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001150 BitfieldSize = ParseConstantExpression();
1151 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001152 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001153 }
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001155 // pure-specifier:
1156 // '= 0'
1157 //
1158 // constant-initializer:
1159 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001160 //
1161 // defaulted/deleted function-definition:
1162 // '=' 'default' [TODO]
1163 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001164
1165 if (Tok.is(tok::equal)) {
1166 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001167 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1168 ConsumeToken();
1169 Deleted = true;
1170 } else {
1171 Init = ParseInitializer();
1172 if (Init.isInvalid())
1173 SkipUntil(tok::comma, true, true);
1174 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001175 }
1176
1177 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001178 if (Tok.is(tok::kw___attribute)) {
1179 SourceLocation Loc;
1180 AttributeList *AttrList = ParseAttributes(&Loc);
1181 DeclaratorInfo.AddAttributes(AttrList, Loc);
1182 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001183
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001184 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001185 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001186 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001187
1188 DeclPtrTy ThisDecl;
1189 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001190 // TODO: handle initializers, bitfields, 'delete'
1191 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1192 /*IsDefinition*/ false,
1193 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001194 } else {
John McCall67d1a672009-08-06 02:15:43 +00001195 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1196 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001197 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001198 BitfieldSize.release(),
1199 Init.release(),
1200 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001201 }
Chris Lattner682bf922009-03-29 16:50:03 +00001202 if (ThisDecl)
1203 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001204
Douglas Gregor72b505b2008-12-16 21:30:33 +00001205 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001206 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001207 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001208 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001209 }
1210
John McCall54abf7d2009-11-04 02:18:39 +00001211 DeclaratorInfo.complete(ThisDecl);
1212
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001213 // If we don't have a comma, it is either the end of the list (a ';')
1214 // or an error, bail out.
1215 if (Tok.isNot(tok::comma))
1216 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001218 // Consume the comma.
1219 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001221 // Parse the next declarator.
1222 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001223 BitfieldSize = 0;
1224 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001225 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001227 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001228 if (Tok.is(tok::kw___attribute)) {
1229 SourceLocation Loc;
1230 AttributeList *AttrList = ParseAttributes(&Loc);
1231 DeclaratorInfo.AddAttributes(AttrList, Loc);
1232 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001233
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001234 if (Tok.isNot(tok::colon))
1235 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001236 }
1237
1238 if (Tok.is(tok::semi)) {
1239 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +00001240 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +00001241 DeclsInGroup.size());
1242 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001243 }
1244
1245 Diag(Tok, diag::err_expected_semi_decl_list);
1246 // Skip to end of block or statement
1247 SkipUntil(tok::r_brace, true, true);
1248 if (Tok.is(tok::semi))
1249 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001250 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001251}
1252
1253/// ParseCXXMemberSpecification - Parse the class definition.
1254///
1255/// member-specification:
1256/// member-declaration member-specification[opt]
1257/// access-specifier ':' member-specification[opt]
1258///
1259void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001260 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001261 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001262 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001263 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001264
Chris Lattner49f28ca2009-03-05 08:00:35 +00001265 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1266 PP.getSourceManager(),
1267 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001269 SourceLocation LBraceLoc = ConsumeBrace();
1270
Douglas Gregor6569d682009-05-27 23:11:45 +00001271 // Determine whether this is a top-level (non-nested) class.
Mike Stump1eb44332009-09-09 15:08:12 +00001272 bool TopLevelClass = ClassStack.empty() ||
Douglas Gregor6569d682009-05-27 23:11:45 +00001273 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001274
1275 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001276 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001277
Douglas Gregor6569d682009-05-27 23:11:45 +00001278 // Note that we are parsing a new (potentially-nested) class definition.
1279 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1280
Douglas Gregorddc29e12009-02-06 22:42:48 +00001281 if (TagDecl)
1282 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1283 else {
1284 SkipUntil(tok::r_brace, false, false);
1285 return;
1286 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001287
1288 // C++ 11p3: Members of a class defined with the keyword class are private
1289 // by default. Members of a class defined with the keywords struct or union
1290 // are public by default.
1291 AccessSpecifier CurAS;
1292 if (TagType == DeclSpec::TST_class)
1293 CurAS = AS_private;
1294 else
1295 CurAS = AS_public;
1296
1297 // While we still have something to read, read the member-declarations.
1298 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1299 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001301 // Check for extraneous top-level semicolon.
1302 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +00001303 Diag(Tok, diag::ext_extra_struct_semi)
1304 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001305 ConsumeToken();
1306 continue;
1307 }
1308
1309 AccessSpecifier AS = getAccessSpecifierIfPresent();
1310 if (AS != AS_none) {
1311 // Current token is a C++ access specifier.
1312 CurAS = AS;
1313 ConsumeToken();
1314 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1315 continue;
1316 }
1317
Douglas Gregor37b372b2009-08-20 22:52:58 +00001318 // FIXME: Make sure we don't have a template here.
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001320 // Parse all the comma separated declarators.
1321 ParseCXXClassMemberDeclaration(CurAS);
1322 }
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001324 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001326 AttributeList *AttrList = 0;
1327 // If attributes exist after class contents, parse them.
1328 if (Tok.is(tok::kw___attribute))
1329 AttrList = ParseAttributes(); // FIXME: where should I put them?
1330
1331 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1332 LBraceLoc, RBraceLoc);
1333
1334 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1335 // complete within function bodies, default arguments,
1336 // exception-specifications, and constructor ctor-initializers (including
1337 // such things in nested classes).
1338 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001339 // FIXME: Only function bodies and constructor ctor-initializers are
1340 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001341 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001342 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001343 // are complete and we can parse the delayed portions of method
1344 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001345 ParseLexedMethodDeclarations(getCurrentClass());
1346 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001347 }
1348
1349 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001350 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001351 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001352
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001353 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001354}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001355
1356/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1357/// which explicitly initializes the members or base classes of a
1358/// class (C++ [class.base.init]). For example, the three initializers
1359/// after the ':' in the Derived constructor below:
1360///
1361/// @code
1362/// class Base { };
1363/// class Derived : Base {
1364/// int x;
1365/// float f;
1366/// public:
1367/// Derived(float f) : Base(), x(17), f(f) { }
1368/// };
1369/// @endcode
1370///
Mike Stump1eb44332009-09-09 15:08:12 +00001371/// [C++] ctor-initializer:
1372/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001373///
Mike Stump1eb44332009-09-09 15:08:12 +00001374/// [C++] mem-initializer-list:
1375/// mem-initializer
1376/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001377void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001378 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1379
1380 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Douglas Gregor7ad83902008-11-05 04:29:56 +00001382 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Douglas Gregor7ad83902008-11-05 04:29:56 +00001384 do {
1385 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001386 if (!MemInit.isInvalid())
1387 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001388
1389 if (Tok.is(tok::comma))
1390 ConsumeToken();
1391 else if (Tok.is(tok::l_brace))
1392 break;
1393 else {
1394 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001395 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001396 SkipUntil(tok::l_brace, true, true);
1397 break;
1398 }
1399 } while (true);
1400
Mike Stump1eb44332009-09-09 15:08:12 +00001401 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001402 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001403}
1404
1405/// ParseMemInitializer - Parse a C++ member initializer, which is
1406/// part of a constructor initializer that explicitly initializes one
1407/// member or base class (C++ [class.base.init]). See
1408/// ParseConstructorInitializer for an example.
1409///
1410/// [C++] mem-initializer:
1411/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001412///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001413/// [C++] mem-initializer-id:
1414/// '::'[opt] nested-name-specifier[opt] class-name
1415/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001416Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001417 // parse '::'[opt] nested-name-specifier[opt]
1418 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001419 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001420 TypeTy *TemplateTypeTy = 0;
1421 if (Tok.is(tok::annot_template_id)) {
1422 TemplateIdAnnotation *TemplateId
1423 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1424 if (TemplateId->Kind == TNK_Type_template) {
1425 AnnotateTemplateIdTokenAsType(&SS);
1426 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1427 TemplateTypeTy = Tok.getAnnotationValue();
1428 }
1429 // FIXME. May need to check for TNK_Dependent_template as well.
1430 }
1431 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001432 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001433 return true;
1434 }
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Douglas Gregor7ad83902008-11-05 04:29:56 +00001436 // Get the identifier. This may be a member name or a class name,
1437 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001438 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001439 SourceLocation IdLoc = ConsumeToken();
1440
1441 // Parse the '('.
1442 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001443 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001444 return true;
1445 }
1446 SourceLocation LParenLoc = ConsumeParen();
1447
1448 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001449 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001450 CommaLocsTy CommaLocs;
1451 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1452 SkipUntil(tok::r_paren);
1453 return true;
1454 }
1455
1456 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1457
Fariborz Jahanian96174332009-07-01 19:21:19 +00001458 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1459 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001460 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001461 ArgExprs.size(), CommaLocs.data(),
1462 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001463}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001464
1465/// ParseExceptionSpecification - Parse a C++ exception-specification
1466/// (C++ [except.spec]).
1467///
Douglas Gregora4745612008-12-01 18:00:20 +00001468/// exception-specification:
1469/// 'throw' '(' type-id-list [opt] ')'
1470/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001471///
Douglas Gregora4745612008-12-01 18:00:20 +00001472/// type-id-list:
1473/// type-id
1474/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001475///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001476bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001477 llvm::SmallVector<TypeTy*, 2>
1478 &Exceptions,
1479 llvm::SmallVector<SourceRange, 2>
1480 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001481 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001482 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001484 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001486 if (!Tok.is(tok::l_paren)) {
1487 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1488 }
1489 SourceLocation LParenLoc = ConsumeParen();
1490
Douglas Gregora4745612008-12-01 18:00:20 +00001491 // Parse throw(...), a Microsoft extension that means "this function
1492 // can throw anything".
1493 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001494 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001495 SourceLocation EllipsisLoc = ConsumeToken();
1496 if (!getLang().Microsoft)
1497 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001498 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001499 return false;
1500 }
1501
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001502 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001503 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001504 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001505 TypeResult Res(ParseTypeName(&Range));
1506 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001507 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001508 Ranges.push_back(Range);
1509 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001510 if (Tok.is(tok::comma))
1511 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001512 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001513 break;
1514 }
1515
Sebastian Redlab197ba2009-02-09 18:23:29 +00001516 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001517 return false;
1518}
Douglas Gregor6569d682009-05-27 23:11:45 +00001519
1520/// \brief We have just started parsing the definition of a new class,
1521/// so push that class onto our stack of classes that is currently
1522/// being parsed.
1523void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001524 assert((TopLevelClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001525 "Nested class without outer class");
1526 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1527}
1528
1529/// \brief Deallocate the given parsed class and all of its nested
1530/// classes.
1531void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1532 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1533 DeallocateParsedClasses(Class->NestedClasses[I]);
1534 delete Class;
1535}
1536
1537/// \brief Pop the top class of the stack of classes that are
1538/// currently being parsed.
1539///
1540/// This routine should be called when we have finished parsing the
1541/// definition of a class, but have not yet popped the Scope
1542/// associated with the class's definition.
1543///
1544/// \returns true if the class we've popped is a top-level class,
1545/// false otherwise.
1546void Parser::PopParsingClass() {
1547 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Douglas Gregor6569d682009-05-27 23:11:45 +00001549 ParsingClass *Victim = ClassStack.top();
1550 ClassStack.pop();
1551 if (Victim->TopLevelClass) {
1552 // Deallocate all of the nested classes of this class,
1553 // recursively: we don't need to keep any of this information.
1554 DeallocateParsedClasses(Victim);
1555 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001556 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001557 assert(!ClassStack.empty() && "Missing top-level class?");
1558
1559 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1560 Victim->NestedClasses.empty()) {
1561 // The victim is a nested class, but we will not need to perform
1562 // any processing after the definition of this class since it has
1563 // no members whose handling was delayed. Therefore, we can just
1564 // remove this nested class.
1565 delete Victim;
1566 return;
1567 }
1568
1569 // This nested class has some members that will need to be processed
1570 // after the top-level class is completely defined. Therefore, add
1571 // it to the list of nested classes within its parent.
1572 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1573 ClassStack.top()->NestedClasses.push_back(Victim);
1574 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1575}