blob: 72c9f33cd8db9987d0aaf4a12549dfcbea247b12 [file] [log] [blame]
Chris Lattnera5235172007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnera5235172007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson74d7f0d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor423984d2008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregor556877c2008-04-13 21:30:24 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattnera5235172007-08-25 06:57:03 +000018#include "clang/Parse/Scope.h"
Chris Lattnerd19c1c02008-12-18 01:12:00 +000019#include "ExtensionRAIIObject.h"
Chris Lattnera5235172007-08-25 06:57:03 +000020using namespace clang;
21
22/// ParseNamespace - We know that the current token is a namespace keyword. This
23/// may either be a top level namespace or a block-level namespace alias.
24///
25/// namespace-definition: [C++ 7.3: basic.namespace]
26/// named-namespace-definition
27/// unnamed-namespace-definition
28///
29/// unnamed-namespace-definition:
30/// 'namespace' attributes[opt] '{' namespace-body '}'
31///
32/// named-namespace-definition:
33/// original-namespace-definition
34/// extension-namespace-definition
35///
36/// original-namespace-definition:
37/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
38///
39/// extension-namespace-definition:
40/// 'namespace' original-namespace-name '{' namespace-body '}'
Mike Stump11289f42009-09-09 15:08:12 +000041///
Chris Lattnera5235172007-08-25 06:57:03 +000042/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
43/// 'namespace' identifier '=' qualified-namespace-specifier ';'
44///
Chris Lattner49836b42009-04-02 04:16:50 +000045Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
46 SourceLocation &DeclEnd) {
Chris Lattner76c72282007-10-09 17:33:22 +000047 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000048 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump11289f42009-09-09 15:08:12 +000049
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000050 if (Tok.is(tok::code_completion)) {
51 Actions.CodeCompleteNamespaceDecl(CurScope);
52 ConsumeToken();
53 }
54
Chris Lattnera5235172007-08-25 06:57:03 +000055 SourceLocation IdentLoc;
56 IdentifierInfo *Ident = 0;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000057
58 Token attrTok;
Mike Stump11289f42009-09-09 15:08:12 +000059
Chris Lattner76c72282007-10-09 17:33:22 +000060 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000061 Ident = Tok.getIdentifierInfo();
62 IdentLoc = ConsumeToken(); // eat the identifier.
63 }
Mike Stump11289f42009-09-09 15:08:12 +000064
Chris Lattnera5235172007-08-25 06:57:03 +000065 // Read label attributes, if present.
Chris Lattner83f095c2009-03-28 19:18:32 +000066 Action::AttrTy *AttrList = 0;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000067 if (Tok.is(tok::kw___attribute)) {
68 attrTok = Tok;
69
Chris Lattnera5235172007-08-25 06:57:03 +000070 // FIXME: save these somewhere.
71 AttrList = ParseAttributes();
Douglas Gregor6b6bba42009-06-17 19:49:00 +000072 }
Mike Stump11289f42009-09-09 15:08:12 +000073
Douglas Gregor6b6bba42009-06-17 19:49:00 +000074 if (Tok.is(tok::equal)) {
75 if (AttrList)
76 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
77
Chris Lattner49836b42009-04-02 04:16:50 +000078 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000079 }
Mike Stump11289f42009-09-09 15:08:12 +000080
Chris Lattner4de55aa2009-03-29 14:02:43 +000081 if (Tok.isNot(tok::l_brace)) {
Mike Stump11289f42009-09-09 15:08:12 +000082 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner4de55aa2009-03-29 14:02:43 +000083 diag::err_expected_ident_lbrace);
84 return DeclPtrTy();
Chris Lattnera5235172007-08-25 06:57:03 +000085 }
Mike Stump11289f42009-09-09 15:08:12 +000086
Chris Lattner4de55aa2009-03-29 14:02:43 +000087 SourceLocation LBrace = ConsumeBrace();
88
89 // Enter a scope for the namespace.
90 ParseScope NamespaceScope(this, Scope::DeclScope);
91
92 DeclPtrTy NamespcDecl =
93 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
94
95 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
96 PP.getSourceManager(),
97 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +000098
Chris Lattner4de55aa2009-03-29 14:02:43 +000099 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
100 ParseExternalDeclaration();
Mike Stump11289f42009-09-09 15:08:12 +0000101
Chris Lattner4de55aa2009-03-29 14:02:43 +0000102 // Leave the namespace scope.
103 NamespaceScope.Exit();
104
Chris Lattner49836b42009-04-02 04:16:50 +0000105 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
106 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000107
Chris Lattner49836b42009-04-02 04:16:50 +0000108 DeclEnd = RBraceLoc;
Chris Lattner4de55aa2009-03-29 14:02:43 +0000109 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000110}
Chris Lattner38376f12008-01-12 07:05:38 +0000111
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000112/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
113/// alias definition.
114///
Anders Carlsson47952ae2009-03-28 22:53:22 +0000115Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000116 SourceLocation AliasLoc,
Chris Lattner49836b42009-04-02 04:16:50 +0000117 IdentifierInfo *Alias,
118 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000119 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000120
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000121 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000122
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000123 if (Tok.is(tok::code_completion)) {
124 Actions.CodeCompleteNamespaceAliasDecl(CurScope);
125 ConsumeToken();
126 }
127
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000128 CXXScopeSpec SS;
129 // Parse (optional) nested-name-specifier.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000130 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000131
132 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
133 Diag(Tok, diag::err_expected_namespace_name);
134 // Skip to end of the definition and eat the ';'.
135 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +0000136 return DeclPtrTy();
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000137 }
138
139 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000140 IdentifierInfo *Ident = Tok.getIdentifierInfo();
141 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000142
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000143 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000144 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000145 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
146 "", tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000147
148 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000149 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000150}
151
Chris Lattner38376f12008-01-12 07:05:38 +0000152/// ParseLinkage - We know that the current token is a string_literal
153/// and just before that, that extern was seen.
154///
155/// linkage-specification: [C++ 7.5p2: dcl.link]
156/// 'extern' string-literal '{' declaration-seq[opt] '}'
157/// 'extern' string-literal declaration
158///
Chris Lattner83f095c2009-03-28 19:18:32 +0000159Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) {
Douglas Gregor15799fd2008-11-21 16:10:08 +0000160 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattner38376f12008-01-12 07:05:38 +0000161 llvm::SmallVector<char, 8> LangBuffer;
162 // LangBuffer is guaranteed to be big enough.
163 LangBuffer.resize(Tok.getLength());
164 const char *LangBufPtr = &LangBuffer[0];
165 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
166
167 SourceLocation Loc = ConsumeStringToken();
Chris Lattner38376f12008-01-12 07:05:38 +0000168
Douglas Gregor07665a62009-01-05 19:45:36 +0000169 ParseScope LinkageScope(this, Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +0000170 DeclPtrTy LinkageSpec
171 = Actions.ActOnStartLinkageSpecification(CurScope,
Douglas Gregor07665a62009-01-05 19:45:36 +0000172 /*FIXME: */SourceLocation(),
173 Loc, LangBufPtr, StrSize,
Mike Stump11289f42009-09-09 15:08:12 +0000174 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor07665a62009-01-05 19:45:36 +0000175 : SourceLocation());
176
177 if (Tok.isNot(tok::l_brace)) {
178 ParseDeclarationOrFunctionDefinition();
Mike Stump11289f42009-09-09 15:08:12 +0000179 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
Douglas Gregor07665a62009-01-05 19:45:36 +0000180 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000181 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000182
183 SourceLocation LBrace = ConsumeBrace();
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000184 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregor07665a62009-01-05 19:45:36 +0000185 ParseExternalDeclaration();
Chris Lattner38376f12008-01-12 07:05:38 +0000186 }
187
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000188 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor07665a62009-01-05 19:45:36 +0000189 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattner38376f12008-01-12 07:05:38 +0000190}
Douglas Gregor556877c2008-04-13 21:30:24 +0000191
Douglas Gregord7c4d982008-12-30 03:27:21 +0000192/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
193/// using-directive. Assumes that current token is 'using'.
Chris Lattner49836b42009-04-02 04:16:50 +0000194Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
195 SourceLocation &DeclEnd) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000196 assert(Tok.is(tok::kw_using) && "Not using token");
197
198 // Eat 'using'.
199 SourceLocation UsingLoc = ConsumeToken();
200
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000201 if (Tok.is(tok::code_completion)) {
202 Actions.CodeCompleteUsing(CurScope);
203 ConsumeToken();
204 }
205
Chris Lattner9b01ca12009-01-06 06:55:51 +0000206 if (Tok.is(tok::kw_namespace))
Douglas Gregord7c4d982008-12-30 03:27:21 +0000207 // Next token after 'using' is 'namespace' so it must be using-directive
Chris Lattner49836b42009-04-02 04:16:50 +0000208 return ParseUsingDirective(Context, UsingLoc, DeclEnd);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000209
210 // Otherwise, it must be using-declaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000211 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000212}
213
214/// ParseUsingDirective - Parse C++ using-directive, assumes
215/// that current token is 'namespace' and 'using' was already parsed.
216///
217/// using-directive: [C++ 7.3.p4: namespace.udir]
218/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
219/// namespace-name ;
220/// [GNU] using-directive:
221/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
222/// namespace-name attributes[opt] ;
223///
Chris Lattner83f095c2009-03-28 19:18:32 +0000224Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner49836b42009-04-02 04:16:50 +0000225 SourceLocation UsingLoc,
226 SourceLocation &DeclEnd) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000227 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
228
229 // Eat 'namespace'.
230 SourceLocation NamespcLoc = ConsumeToken();
231
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000232 if (Tok.is(tok::code_completion)) {
233 Actions.CodeCompleteUsingDirective(CurScope);
234 ConsumeToken();
235 }
236
Douglas Gregord7c4d982008-12-30 03:27:21 +0000237 CXXScopeSpec SS;
238 // Parse (optional) nested-name-specifier.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000239 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000240
241 AttributeList *AttrList = 0;
242 IdentifierInfo *NamespcName = 0;
243 SourceLocation IdentLoc = SourceLocation();
244
245 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000246 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000247 Diag(Tok, diag::err_expected_namespace_name);
248 // If there was invalid namespace name, skip to end of decl, and eat ';'.
249 SkipUntil(tok::semi);
250 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattner83f095c2009-03-28 19:18:32 +0000251 return DeclPtrTy();
Douglas Gregord7c4d982008-12-30 03:27:21 +0000252 }
Mike Stump11289f42009-09-09 15:08:12 +0000253
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000254 // Parse identifier.
255 NamespcName = Tok.getIdentifierInfo();
256 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000257
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000258 // Parse (optional) attributes (most likely GNU strong-using extension).
259 if (Tok.is(tok::kw___attribute))
260 AttrList = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000261
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000262 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000263 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000264 ExpectAndConsume(tok::semi,
265 AttrList ? diag::err_expected_semi_after_attribute_list :
266 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000267
268 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000269 IdentLoc, NamespcName, AttrList);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000270}
271
272/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
273/// 'using' was already seen.
274///
275/// using-declaration: [C++ 7.3.p3: namespace.udecl]
276/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000277/// unqualified-id
278/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000279///
Chris Lattner83f095c2009-03-28 19:18:32 +0000280Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner49836b42009-04-02 04:16:50 +0000281 SourceLocation UsingLoc,
Anders Carlsson7b194b72009-08-29 19:54:19 +0000282 SourceLocation &DeclEnd,
283 AccessSpecifier AS) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000284 CXXScopeSpec SS;
285 bool IsTypeName;
286
287 // Ignore optional 'typename'.
288 if (Tok.is(tok::kw_typename)) {
289 ConsumeToken();
290 IsTypeName = true;
291 }
292 else
293 IsTypeName = false;
294
295 // Parse nested-name-specifier.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000296 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregorfec52632009-06-20 00:51:54 +0000297
298 AttributeList *AttrList = 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000299
300 // Check nested-name specifier.
301 if (SS.isInvalid()) {
302 SkipUntil(tok::semi);
303 return DeclPtrTy();
304 }
305 if (Tok.is(tok::annot_template_id)) {
Anders Carlsson5a9c5ac2009-08-28 03:35:18 +0000306 // C++0x N2914 [namespace.udecl]p5:
Mike Stump11289f42009-09-09 15:08:12 +0000307 // A using-declaration shall not name a template-id.
Anders Carlsson5a9c5ac2009-08-28 03:35:18 +0000308 Diag(Tok, diag::err_using_decl_can_not_refer_to_template_spec);
Douglas Gregorfec52632009-06-20 00:51:54 +0000309 SkipUntil(tok::semi);
310 return DeclPtrTy();
311 }
Mike Stump11289f42009-09-09 15:08:12 +0000312
Anders Carlsson74d7f0d2009-06-27 00:27:47 +0000313 IdentifierInfo *TargetName = 0;
314 OverloadedOperatorKind Op = OO_None;
315 SourceLocation IdentLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000316
Anders Carlsson74d7f0d2009-06-27 00:27:47 +0000317 if (Tok.is(tok::kw_operator)) {
318 IdentLoc = Tok.getLocation();
319
320 Op = TryParseOperatorFunctionId();
321 if (!Op) {
322 // If there was an invalid operator, skip to end of decl, and eat ';'.
323 SkipUntil(tok::semi);
324 return DeclPtrTy();
325 }
326 } else if (Tok.is(tok::identifier)) {
327 // Parse identifier.
328 TargetName = Tok.getIdentifierInfo();
329 IdentLoc = ConsumeToken();
330 } else {
331 // FIXME: Use a better diagnostic here.
Douglas Gregorfec52632009-06-20 00:51:54 +0000332 Diag(Tok, diag::err_expected_ident_in_using);
Anders Carlsson74d7f0d2009-06-27 00:27:47 +0000333
Douglas Gregorfec52632009-06-20 00:51:54 +0000334 // If there was invalid identifier, skip to end of decl, and eat ';'.
335 SkipUntil(tok::semi);
336 return DeclPtrTy();
337 }
Mike Stump11289f42009-09-09 15:08:12 +0000338
Douglas Gregorfec52632009-06-20 00:51:54 +0000339 // Parse (optional) attributes (most likely GNU strong-using extension).
340 if (Tok.is(tok::kw___attribute))
341 AttrList = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000342
Douglas Gregorfec52632009-06-20 00:51:54 +0000343 // Eat ';'.
344 DeclEnd = Tok.getLocation();
345 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
346 AttrList ? "attributes list" : "namespace name", tok::semi);
347
Anders Carlsson7b194b72009-08-29 19:54:19 +0000348 return Actions.ActOnUsingDeclaration(CurScope, AS, UsingLoc, SS,
Anders Carlsson74d7f0d2009-06-27 00:27:47 +0000349 IdentLoc, TargetName, Op,
350 AttrList, IsTypeName);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000351}
352
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000353/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
354///
355/// static_assert-declaration:
356/// static_assert ( constant-expression , string-literal ) ;
357///
Chris Lattner49836b42009-04-02 04:16:50 +0000358Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000359 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
360 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000361
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000362 if (Tok.isNot(tok::l_paren)) {
363 Diag(Tok, diag::err_expected_lparen);
Chris Lattner83f095c2009-03-28 19:18:32 +0000364 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000365 }
Mike Stump11289f42009-09-09 15:08:12 +0000366
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000367 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000368
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000369 OwningExprResult AssertExpr(ParseConstantExpression());
370 if (AssertExpr.isInvalid()) {
371 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +0000372 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000373 }
Mike Stump11289f42009-09-09 15:08:12 +0000374
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000375 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattner83f095c2009-03-28 19:18:32 +0000376 return DeclPtrTy();
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000377
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000378 if (Tok.isNot(tok::string_literal)) {
379 Diag(Tok, diag::err_expected_string_literal);
380 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +0000381 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000382 }
Mike Stump11289f42009-09-09 15:08:12 +0000383
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000384 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump11289f42009-09-09 15:08:12 +0000385 if (AssertMessage.isInvalid())
Chris Lattner83f095c2009-03-28 19:18:32 +0000386 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000387
Anders Carlsson27de6a52009-03-15 18:44:04 +0000388 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000389
Chris Lattner49836b42009-04-02 04:16:50 +0000390 DeclEnd = Tok.getLocation();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000391 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
392
Mike Stump11289f42009-09-09 15:08:12 +0000393 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson27de6a52009-03-15 18:44:04 +0000394 move(AssertMessage));
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000395}
396
Anders Carlsson74948d02009-06-24 17:47:40 +0000397/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
398///
399/// 'decltype' ( expression )
400///
401void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
402 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
403
404 SourceLocation StartLoc = ConsumeToken();
405 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000406
407 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson74948d02009-06-24 17:47:40 +0000408 "decltype")) {
409 SkipUntil(tok::r_paren);
410 return;
411 }
Mike Stump11289f42009-09-09 15:08:12 +0000412
Anders Carlsson74948d02009-06-24 17:47:40 +0000413 // Parse the expression
Mike Stump11289f42009-09-09 15:08:12 +0000414
Anders Carlsson74948d02009-06-24 17:47:40 +0000415 // C++0x [dcl.type.simple]p4:
416 // The operand of the decltype specifier is an unevaluated operand.
417 EnterExpressionEvaluationContext Unevaluated(Actions,
418 Action::Unevaluated);
419 OwningExprResult Result = ParseExpression();
420 if (Result.isInvalid()) {
421 SkipUntil(tok::r_paren);
422 return;
423 }
Mike Stump11289f42009-09-09 15:08:12 +0000424
Anders Carlsson74948d02009-06-24 17:47:40 +0000425 // Match the ')'
426 SourceLocation RParenLoc;
427 if (Tok.is(tok::r_paren))
428 RParenLoc = ConsumeParen();
429 else
430 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000431
Anders Carlsson74948d02009-06-24 17:47:40 +0000432 if (RParenLoc.isInvalid())
433 return;
434
435 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000436 unsigned DiagID;
Anders Carlsson74948d02009-06-24 17:47:40 +0000437 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump11289f42009-09-09 15:08:12 +0000438 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000439 DiagID, Result.release()))
440 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson74948d02009-06-24 17:47:40 +0000441}
442
Douglas Gregor831c93f2008-11-05 20:51:48 +0000443/// ParseClassName - Parse a C++ class-name, which names a class. Note
444/// that we only check that the result names a type; semantic analysis
445/// will need to verify that the type names a class. The result is
Douglas Gregord54dfb82009-02-25 23:52:28 +0000446/// either a type or NULL, depending on whether a type name was
Douglas Gregor831c93f2008-11-05 20:51:48 +0000447/// found.
448///
449/// class-name: [C++ 9.1]
450/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000451/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000452///
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000453Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahanian4041dfc2009-07-20 17:43:15 +0000454 const CXXScopeSpec *SS,
455 bool DestrExpected) {
Douglas Gregord54dfb82009-02-25 23:52:28 +0000456 // Check whether we have a template-id that names a type.
457 if (Tok.is(tok::annot_template_id)) {
Mike Stump11289f42009-09-09 15:08:12 +0000458 TemplateIdAnnotation *TemplateId
Douglas Gregord54dfb82009-02-25 23:52:28 +0000459 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +0000460 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000461 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000462
463 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
464 TypeTy *Type = Tok.getAnnotationValue();
465 EndLocation = Tok.getAnnotationEndLoc();
466 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000467
468 if (Type)
469 return Type;
470 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000471 }
472
473 // Fall through to produce an error below.
474 }
475
Douglas Gregor831c93f2008-11-05 20:51:48 +0000476 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000477 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000478 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000479 }
480
481 // We have an identifier; check whether it is actually a type.
Mike Stump11289f42009-09-09 15:08:12 +0000482 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor5e0962f2009-08-26 18:27:52 +0000483 Tok.getLocation(), CurScope, SS,
484 true);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000485 if (!Type) {
Mike Stump11289f42009-09-09 15:08:12 +0000486 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
Fariborz Jahanian4041dfc2009-07-20 17:43:15 +0000487 : diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000488 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000489 }
490
491 // Consume the identifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +0000492 EndLocation = ConsumeToken();
Douglas Gregor831c93f2008-11-05 20:51:48 +0000493 return Type;
494}
495
Douglas Gregor556877c2008-04-13 21:30:24 +0000496/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
497/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
498/// until we reach the start of a definition or see a token that
499/// cannot start a definition.
500///
501/// class-specifier: [C++ class]
502/// class-head '{' member-specification[opt] '}'
503/// class-head '{' member-specification[opt] '}' attributes[opt]
504/// class-head:
505/// class-key identifier[opt] base-clause[opt]
506/// class-key nested-name-specifier identifier base-clause[opt]
507/// class-key nested-name-specifier[opt] simple-template-id
508/// base-clause[opt]
509/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000510/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +0000511/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000512/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +0000513/// simple-template-id base-clause[opt]
514/// class-key:
515/// 'class'
516/// 'struct'
517/// 'union'
518///
519/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +0000520/// class-key ::[opt] nested-name-specifier[opt] identifier
521/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
522/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +0000523///
524/// Note that the C++ class-specifier and elaborated-type-specifier,
525/// together, subsume the C99 struct-or-union-specifier:
526///
527/// struct-or-union-specifier: [C99 6.7.2.1]
528/// struct-or-union identifier[opt] '{' struct-contents '}'
529/// struct-or-union identifier
530/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
531/// '}' attributes[opt]
532/// [GNU] struct-or-union attributes[opt] identifier
533/// struct-or-union:
534/// 'struct'
535/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000536void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
537 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000538 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor6c2adff2009-03-25 22:00:53 +0000539 AccessSpecifier AS) {
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000540 DeclSpec::TST TagType;
541 if (TagTokKind == tok::kw_struct)
542 TagType = DeclSpec::TST_struct;
543 else if (TagTokKind == tok::kw_class)
544 TagType = DeclSpec::TST_class;
545 else {
546 assert(TagTokKind == tok::kw_union && "Not a class specifier");
547 TagType = DeclSpec::TST_union;
548 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000549
Douglas Gregorf45b0cf2009-09-18 15:37:17 +0000550 if (Tok.is(tok::code_completion)) {
551 // Code completion for a struct, class, or union name.
552 Actions.CodeCompleteTag(CurScope, TagType);
553 ConsumeToken();
554 }
555
Douglas Gregor556877c2008-04-13 21:30:24 +0000556 AttributeList *Attr = 0;
557 // If attributes exist after tag, parse them.
558 if (Tok.is(tok::kw___attribute))
559 Attr = ParseAttributes();
560
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000561 // If declspecs exist after tag, parse them.
Eli Friedman53339e02009-06-08 23:27:34 +0000562 if (Tok.is(tok::kw___declspec))
563 Attr = ParseMicrosoftDeclSpec(Attr);
Mike Stump11289f42009-09-09 15:08:12 +0000564
Douglas Gregor119b0c72009-09-04 05:53:02 +0000565 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
566 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
567 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump11289f42009-09-09 15:08:12 +0000568 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregor119b0c72009-09-04 05:53:02 +0000569 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
570 // properly.
571 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
572 Tok.setKind(tok::identifier);
573 }
574
575 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
576 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
577 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump11289f42009-09-09 15:08:12 +0000578 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregor119b0c72009-09-04 05:53:02 +0000579 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
580 // properly.
581 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
582 Tok.setKind(tok::identifier);
583 }
Mike Stump11289f42009-09-09 15:08:12 +0000584
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000585 // Parse the (optional) nested-name-specifier.
586 CXXScopeSpec SS;
Mike Stump11289f42009-09-09 15:08:12 +0000587 if (getLang().CPlusPlus &&
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000588 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
Douglas Gregor7f741122009-02-25 19:37:18 +0000589 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000590 Diag(Tok, diag::err_expected_ident);
Douglas Gregor67a65642009-02-17 23:15:12 +0000591
Douglas Gregor916462b2009-10-30 21:46:58 +0000592 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
593
Douglas Gregor67a65642009-02-17 23:15:12 +0000594 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +0000595 IdentifierInfo *Name = 0;
596 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +0000597 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +0000598 if (Tok.is(tok::identifier)) {
599 Name = Tok.getIdentifierInfo();
600 NameLoc = ConsumeToken();
Douglas Gregor916462b2009-10-30 21:46:58 +0000601
602 if (Tok.is(tok::less)) {
603 // The name was supposed to refer to a template, but didn't.
604 // Eat the template argument list and try to continue parsing this as
605 // a class (or template thereof).
606 TemplateArgList TemplateArgs;
607 TemplateArgIsTypeList TemplateArgIsType;
608 TemplateArgLocationList TemplateArgLocations;
609 SourceLocation LAngleLoc, RAngleLoc;
610 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
611 true, LAngleLoc,
612 TemplateArgs, TemplateArgIsType,
613 TemplateArgLocations, RAngleLoc)) {
614 // We couldn't parse the template argument list at all, so don't
615 // try to give any location information for the list.
616 LAngleLoc = RAngleLoc = SourceLocation();
617 }
618
619 Diag(NameLoc, diag::err_explicit_spec_non_template)
620 << (TagType == DeclSpec::TST_class? 0
621 : TagType == DeclSpec::TST_struct? 1
622 : 2)
623 << Name
624 << SourceRange(LAngleLoc, RAngleLoc);
625
626 // If this is an explicit specialization, strip off the last template
627 // parameter list, since we've removed its template arguments.
628 if (TemplateParams && TemplateParams->size() > 1) {
629 TemplateParams->pop_back();
630 } else {
631 TemplateParams = 0;
632 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
633 = ParsedTemplateInfo::NonTemplate;
634 }
635
636 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000637 } else if (Tok.is(tok::annot_template_id)) {
638 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
639 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +0000640
Douglas Gregorb67535d2009-03-31 00:43:58 +0000641 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000642 // The template-name in the simple-template-id refers to
643 // something other than a class template. Give an appropriate
644 // error message and skip to the ';'.
645 SourceRange Range(NameLoc);
646 if (SS.isNotEmpty())
647 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +0000648
Douglas Gregor7f741122009-02-25 19:37:18 +0000649 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
650 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +0000651
Douglas Gregor7f741122009-02-25 19:37:18 +0000652 DS.SetTypeSpecError();
653 SkipUntil(tok::semi, false, true);
654 TemplateId->Destroy();
655 return;
Douglas Gregor67a65642009-02-17 23:15:12 +0000656 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000657 }
658
John McCall07e91c02009-08-06 02:15:43 +0000659 // There are four options here. If we have 'struct foo;', then this
660 // is either a forward declaration or a friend declaration, which
661 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor7f741122009-02-25 19:37:18 +0000662 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregor556877c2008-04-13 21:30:24 +0000663 // something like 'struct foo xyz', a reference.
John McCall9bb74a52009-07-31 02:45:11 +0000664 Action::TagUseKind TUK;
Douglas Gregor3dad8422009-09-26 06:47:28 +0000665 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) {
666 if (DS.isFriendSpecified()) {
667 // C++ [class.friend]p2:
668 // A class shall not be defined in a friend declaration.
669 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
670 << SourceRange(DS.getFriendSpecLoc());
671
672 // Skip everything up to the semicolon, so that this looks like a proper
673 // friend class (or template thereof) declaration.
674 SkipUntil(tok::semi, true, true);
675 TUK = Action::TUK_Friend;
676 } else {
677 // Okay, this is a class definition.
678 TUK = Action::TUK_Definition;
679 }
680 } else if (Tok.is(tok::semi))
John McCall07e91c02009-08-06 02:15:43 +0000681 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregor556877c2008-04-13 21:30:24 +0000682 else
John McCall9bb74a52009-07-31 02:45:11 +0000683 TUK = Action::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +0000684
John McCall9bb74a52009-07-31 02:45:11 +0000685 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000686 // We have a declaration or reference to an anonymous class.
Chris Lattner6d29c102008-11-18 07:48:38 +0000687 Diag(StartLoc, diag::err_anon_type_definition)
688 << DeclSpec::getSpecifierName(TagType);
Douglas Gregor556877c2008-04-13 21:30:24 +0000689
690 // Skip the rest of this declarator, up until the comma or semicolon.
691 SkipUntil(tok::comma, true);
Douglas Gregor7f741122009-02-25 19:37:18 +0000692
693 if (TemplateId)
694 TemplateId->Destroy();
Douglas Gregor556877c2008-04-13 21:30:24 +0000695 return;
696 }
697
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000698 // Create the tag portion of the class or class template.
John McCall7f41d982009-09-11 04:59:25 +0000699 Action::DeclResult TagOrTempResult = true; // invalid
700 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000701
John McCall9bb74a52009-07-31 02:45:11 +0000702 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000703 // to turn that template-id into a type.
704
Douglas Gregord6ab8742009-05-28 23:31:59 +0000705 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +0000706 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000707 // Explicit specialization, class template partial specialization,
708 // or explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +0000709 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor7f741122009-02-25 19:37:18 +0000710 TemplateId->getTemplateArgs(),
711 TemplateId->getTemplateArgIsType(),
712 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000713 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000714 TUK == Action::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000715 // This is an explicit instantiation of a class template.
716 TagOrTempResult
Mike Stump11289f42009-09-09 15:08:12 +0000717 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor43e75172009-09-04 06:33:52 +0000718 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000719 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000720 TagType,
Mike Stump11289f42009-09-09 15:08:12 +0000721 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000722 SS,
Mike Stump11289f42009-09-09 15:08:12 +0000723 TemplateTy::make(TemplateId->Template),
724 TemplateId->TemplateNameLoc,
725 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000726 TemplateArgsPtr,
727 TemplateId->getTemplateArgLocations(),
Mike Stump11289f42009-09-09 15:08:12 +0000728 TemplateId->RAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000729 Attr);
Douglas Gregor2208a292009-09-26 20:57:03 +0000730 } else if (TUK == Action::TUK_Reference) {
John McCall7f41d982009-09-11 04:59:25 +0000731 TypeResult
John McCalld8fe9af2009-09-08 17:47:29 +0000732 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
733 TemplateId->TemplateNameLoc,
734 TemplateId->LAngleLoc,
735 TemplateArgsPtr,
736 TemplateId->getTemplateArgLocations(),
737 TemplateId->RAngleLoc);
738
John McCall7f41d982009-09-11 04:59:25 +0000739 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
740 TagType, StartLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000741 } else {
742 // This is an explicit specialization or a class template
743 // partial specialization.
744 TemplateParameterLists FakedParamLists;
745
746 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
747 // This looks like an explicit instantiation, because we have
748 // something like
749 //
750 // template class Foo<X>
751 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000752 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000753 // meant to be an explicit specialization, but the user forgot
754 // the '<>' after 'template'.
John McCall9bb74a52009-07-31 02:45:11 +0000755 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000756
Mike Stump11289f42009-09-09 15:08:12 +0000757 SourceLocation LAngleLoc
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000758 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000759 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000760 diag::err_explicit_instantiation_with_definition)
761 << SourceRange(TemplateInfo.TemplateLoc)
762 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
763
764 // Create a fake template parameter list that contains only
765 // "template<>", so that we treat this construct as a class
766 // template specialization.
767 FakedParamLists.push_back(
Mike Stump11289f42009-09-09 15:08:12 +0000768 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000769 TemplateInfo.TemplateLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000770 LAngleLoc,
771 0, 0,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000772 LAngleLoc));
773 TemplateParams = &FakedParamLists;
774 }
775
776 // Build the class template specialization.
777 TagOrTempResult
John McCall9bb74a52009-07-31 02:45:11 +0000778 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor7f741122009-02-25 19:37:18 +0000779 StartLoc, SS,
Mike Stump11289f42009-09-09 15:08:12 +0000780 TemplateTy::make(TemplateId->Template),
781 TemplateId->TemplateNameLoc,
782 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +0000783 TemplateArgsPtr,
784 TemplateId->getTemplateArgLocations(),
Mike Stump11289f42009-09-09 15:08:12 +0000785 TemplateId->RAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +0000786 Attr,
Mike Stump11289f42009-09-09 15:08:12 +0000787 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor67a65642009-02-17 23:15:12 +0000788 TemplateParams? &(*TemplateParams)[0] : 0,
789 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000790 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000791 TemplateId->Destroy();
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000792 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000793 TUK == Action::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000794 // Explicit instantiation of a member of a class template
795 // specialization, e.g.,
796 //
797 // template struct Outer<int>::Inner;
798 //
799 TagOrTempResult
Mike Stump11289f42009-09-09 15:08:12 +0000800 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor43e75172009-09-04 06:33:52 +0000801 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000802 TemplateInfo.TemplateLoc,
803 TagType, StartLoc, SS, Name,
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000804 NameLoc, Attr);
805 } else {
806 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000807 TUK == Action::TUK_Definition) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000808 // FIXME: Diagnose this particular error.
809 }
810
John McCall7f41d982009-09-11 04:59:25 +0000811 bool IsDependent = false;
812
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000813 // Declaration or definition of a class type
Mike Stump11289f42009-09-09 15:08:12 +0000814 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000815 Name, NameLoc, Attr, AS,
Mike Stump11289f42009-09-09 15:08:12 +0000816 Action::MultiTemplateParamsArg(Actions,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000817 TemplateParams? &(*TemplateParams)[0] : 0,
818 TemplateParams? TemplateParams->size() : 0),
John McCall7f41d982009-09-11 04:59:25 +0000819 Owned, IsDependent);
820
821 // If ActOnTag said the type was dependent, try again with the
822 // less common call.
823 if (IsDependent)
824 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
825 SS, Name, StartLoc, NameLoc);
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000826 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000827
828 // Parse the optional base clause (C++ only).
Chris Lattnera3778332009-02-16 22:07:16 +0000829 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregorc08f4892009-03-25 00:13:59 +0000830 ParseBaseClause(TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +0000831
832 // If there is a body, parse it and inform the actions module.
833 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000834 if (getLang().CPlusPlus)
Douglas Gregorc08f4892009-03-25 00:13:59 +0000835 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000836 else
Douglas Gregorc08f4892009-03-25 00:13:59 +0000837 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall9bb74a52009-07-31 02:45:11 +0000838 else if (TUK == Action::TUK_Definition) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000839 // FIXME: Complain that we have a base-specifier list but no
840 // definition.
Chris Lattner6d29c102008-11-18 07:48:38 +0000841 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregor556877c2008-04-13 21:30:24 +0000842 }
843
John McCall7f41d982009-09-11 04:59:25 +0000844 void *Result;
845 if (!TypeResult.isInvalid()) {
846 TagType = DeclSpec::TST_typename;
847 Result = TypeResult.get();
848 Owned = false;
849 } else if (!TagOrTempResult.isInvalid()) {
850 Result = TagOrTempResult.get().getAs<void>();
851 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000852 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +0000853 return;
854 }
Mike Stump11289f42009-09-09 15:08:12 +0000855
John McCall49bfce42009-08-03 20:12:06 +0000856 const char *PrevSpec = 0;
857 unsigned DiagID;
John McCall7f41d982009-09-11 04:59:25 +0000858
John McCall49bfce42009-08-03 20:12:06 +0000859 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
John McCall7f41d982009-09-11 04:59:25 +0000860 Result, Owned))
John McCall49bfce42009-08-03 20:12:06 +0000861 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregor556877c2008-04-13 21:30:24 +0000862}
863
Mike Stump11289f42009-09-09 15:08:12 +0000864/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +0000865///
866/// base-clause : [C++ class.derived]
867/// ':' base-specifier-list
868/// base-specifier-list:
869/// base-specifier '...'[opt]
870/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattner83f095c2009-03-28 19:18:32 +0000871void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000872 assert(Tok.is(tok::colon) && "Not a base clause");
873 ConsumeToken();
874
Douglas Gregor29a92472008-10-22 17:49:05 +0000875 // Build up an array of parsed base specifiers.
876 llvm::SmallVector<BaseTy *, 8> BaseInfo;
877
Douglas Gregor556877c2008-04-13 21:30:24 +0000878 while (true) {
879 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +0000880 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +0000881 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000882 // Skip the rest of this base specifier, up until the comma or
883 // opening brace.
Douglas Gregor29a92472008-10-22 17:49:05 +0000884 SkipUntil(tok::comma, tok::l_brace, true, true);
885 } else {
886 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +0000887 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +0000888 }
889
890 // If the next token is a comma, consume it and keep reading
891 // base-specifiers.
892 if (Tok.isNot(tok::comma)) break;
Mike Stump11289f42009-09-09 15:08:12 +0000893
Douglas Gregor556877c2008-04-13 21:30:24 +0000894 // Consume the comma.
895 ConsumeToken();
896 }
Douglas Gregor29a92472008-10-22 17:49:05 +0000897
898 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +0000899 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +0000900}
901
902/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
903/// one entry in the base class list of a class specifier, for example:
904/// class foo : public bar, virtual private baz {
905/// 'public bar' and 'virtual private baz' are each base-specifiers.
906///
907/// base-specifier: [C++ class.derived]
908/// ::[opt] nested-name-specifier[opt] class-name
909/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
910/// class-name
911/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
912/// class-name
Chris Lattner83f095c2009-03-28 19:18:32 +0000913Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000914 bool IsVirtual = false;
915 SourceLocation StartLoc = Tok.getLocation();
916
917 // Parse the 'virtual' keyword.
918 if (Tok.is(tok::kw_virtual)) {
919 ConsumeToken();
920 IsVirtual = true;
921 }
922
923 // Parse an (optional) access specifier.
924 AccessSpecifier Access = getAccessSpecifierIfPresent();
925 if (Access)
926 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000927
Douglas Gregor556877c2008-04-13 21:30:24 +0000928 // Parse the 'virtual' keyword (again!), in case it came after the
929 // access specifier.
930 if (Tok.is(tok::kw_virtual)) {
931 SourceLocation VirtualLoc = ConsumeToken();
932 if (IsVirtual) {
933 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +0000934 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000935 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregor556877c2008-04-13 21:30:24 +0000936 }
937
938 IsVirtual = true;
939 }
940
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000941 // Parse optional '::' and optional nested-name-specifier.
942 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000943 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Douglas Gregor556877c2008-04-13 21:30:24 +0000944
Douglas Gregor556877c2008-04-13 21:30:24 +0000945 // The location of the base class itself.
946 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor831c93f2008-11-05 20:51:48 +0000947
948 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +0000949 SourceLocation EndLocation;
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000950 TypeResult BaseType = ParseClassName(EndLocation, &SS);
951 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +0000952 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000953
954 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +0000955 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +0000956
Douglas Gregor556877c2008-04-13 21:30:24 +0000957 // Notify semantic analysis that we have parsed a complete
958 // base-specifier.
Sebastian Redl511ed552008-11-25 22:21:31 +0000959 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000960 BaseType.get(), BaseLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +0000961}
962
963/// getAccessSpecifierIfPresent - Determine whether the next token is
964/// a C++ access-specifier.
965///
966/// access-specifier: [C++ class.derived]
967/// 'private'
968/// 'protected'
969/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +0000970AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +0000971 switch (Tok.getKind()) {
972 default: return AS_none;
973 case tok::kw_private: return AS_private;
974 case tok::kw_protected: return AS_protected;
975 case tok::kw_public: return AS_public;
976 }
977}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000978
Eli Friedman3af2a772009-07-22 21:45:50 +0000979void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
980 DeclPtrTy ThisDecl) {
981 // We just declared a member function. If this member function
982 // has any default arguments, we'll need to parse them later.
983 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000984 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedman3af2a772009-07-22 21:45:50 +0000985 = DeclaratorInfo.getTypeObject(0).Fun;
986 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
987 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
988 if (!LateMethod) {
989 // Push this method onto the stack of late-parsed method
990 // declarations.
991 getCurrentClass().MethodDecls.push_back(
992 LateParsedMethodDeclaration(ThisDecl));
993 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregorc45a40a2009-08-22 00:34:47 +0000994 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +0000995
996 // Add all of the parameters prior to this one (they don't
997 // have default arguments).
998 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
999 for (unsigned I = 0; I < ParamIdx; ++I)
1000 LateMethod->DefaultArgs.push_back(
1001 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
1002 }
1003
1004 // Add this parameter to the list of parameters (it or may
1005 // not have a default argument).
1006 LateMethod->DefaultArgs.push_back(
1007 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1008 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1009 }
1010 }
1011}
1012
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001013/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1014///
1015/// member-declaration:
1016/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1017/// function-definition ';'[opt]
1018/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1019/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001020/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001021/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001022/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001023///
1024/// member-declarator-list:
1025/// member-declarator
1026/// member-declarator-list ',' member-declarator
1027///
1028/// member-declarator:
1029/// declarator pure-specifier[opt]
1030/// declarator constant-initializer[opt]
1031/// identifier[opt] ':' constant-expression
1032///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001033/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001034/// '= 0'
1035///
1036/// constant-initializer:
1037/// '=' constant-expression
1038///
Douglas Gregor3447e762009-08-20 22:52:58 +00001039void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
1040 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001041 // static_assert-declaration
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001042 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001043 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00001044 SourceLocation DeclEnd;
1045 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001046 return;
1047 }
Mike Stump11289f42009-09-09 15:08:12 +00001048
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001049 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00001050 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00001051 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00001052 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00001053 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001054 AS);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001055 return;
1056 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001057
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001058 // Handle: member-declaration ::= '__extension__' member-declaration
1059 if (Tok.is(tok::kw___extension__)) {
1060 // __extension__ silences extension warnings in the subexpression.
1061 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1062 ConsumeToken();
Douglas Gregor3447e762009-08-20 22:52:58 +00001063 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001064 }
Douglas Gregorfec52632009-06-20 00:51:54 +00001065
1066 if (Tok.is(tok::kw_using)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001067 // FIXME: Check for template aliases
Mike Stump11289f42009-09-09 15:08:12 +00001068
Douglas Gregorfec52632009-06-20 00:51:54 +00001069 // Eat 'using'.
1070 SourceLocation UsingLoc = ConsumeToken();
1071
1072 if (Tok.is(tok::kw_namespace)) {
1073 Diag(UsingLoc, diag::err_using_namespace_in_class);
1074 SkipUntil(tok::semi, true, true);
1075 }
1076 else {
1077 SourceLocation DeclEnd;
1078 // Otherwise, it must be using-declaration.
Anders Carlsson7b194b72009-08-29 19:54:19 +00001079 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00001080 }
1081 return;
1082 }
1083
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001084 SourceLocation DSStart = Tok.getLocation();
1085 // decl-specifier-seq:
1086 // Parse the common declaration-specifiers piece.
1087 DeclSpec DS;
Douglas Gregor3447e762009-08-20 22:52:58 +00001088 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001089
John McCall11083da2009-09-16 22:47:08 +00001090 Action::MultiTemplateParamsArg TemplateParams(Actions,
1091 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1092 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1093
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001094 if (Tok.is(tok::semi)) {
1095 ConsumeToken();
Douglas Gregor3dad8422009-09-26 06:47:28 +00001096 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall07e91c02009-08-06 02:15:43 +00001097 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001098 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001099
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001100 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001101
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001102 if (Tok.isNot(tok::colon)) {
1103 // Parse the first declarator.
1104 ParseDeclarator(DeclaratorInfo);
1105 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +00001106 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001107 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001108 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001109 if (Tok.is(tok::semi))
1110 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001111 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001112 }
1113
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001114 // function-definition:
Douglas Gregore8381c02008-11-05 04:29:56 +00001115 if (Tok.is(tok::l_brace)
Sebastian Redla7b98a72009-04-26 20:35:05 +00001116 || (DeclaratorInfo.isFunctionDeclarator() &&
1117 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001118 if (!DeclaratorInfo.isFunctionDeclarator()) {
1119 Diag(Tok, diag::err_func_def_no_params);
1120 ConsumeBrace();
1121 SkipUntil(tok::r_brace, true);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001122 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001123 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001124
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001125 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1126 Diag(Tok, diag::err_function_declared_typedef);
1127 // This recovery skips the entire function body. It would be nice
1128 // to simply call ParseCXXInlineMethodDef() below, however Sema
1129 // assumes the declarator represents a function, not a typedef.
1130 ConsumeBrace();
1131 SkipUntil(tok::r_brace, true);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001132 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001133 }
1134
Douglas Gregor3447e762009-08-20 22:52:58 +00001135 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001136 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001137 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001138 }
1139
1140 // member-declarator-list:
1141 // member-declarator
1142 // member-declarator-list ',' member-declarator
1143
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001144 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redlc13f2682008-12-09 20:22:58 +00001145 OwningExprResult BitfieldSize(Actions);
1146 OwningExprResult Init(Actions);
Sebastian Redl42e92c42009-04-12 17:16:29 +00001147 bool Deleted = false;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001148
1149 while (1) {
1150
1151 // member-declarator:
1152 // declarator pure-specifier[opt]
1153 // declarator constant-initializer[opt]
1154 // identifier[opt] ':' constant-expression
1155
1156 if (Tok.is(tok::colon)) {
1157 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001158 BitfieldSize = ParseConstantExpression();
1159 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001160 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001161 }
Mike Stump11289f42009-09-09 15:08:12 +00001162
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001163 // pure-specifier:
1164 // '= 0'
1165 //
1166 // constant-initializer:
1167 // '=' constant-expression
Sebastian Redl42e92c42009-04-12 17:16:29 +00001168 //
1169 // defaulted/deleted function-definition:
1170 // '=' 'default' [TODO]
1171 // '=' 'delete'
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001172
1173 if (Tok.is(tok::equal)) {
1174 ConsumeToken();
Sebastian Redl42e92c42009-04-12 17:16:29 +00001175 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1176 ConsumeToken();
1177 Deleted = true;
1178 } else {
1179 Init = ParseInitializer();
1180 if (Init.isInvalid())
1181 SkipUntil(tok::comma, true, true);
1182 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001183 }
1184
1185 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001186 if (Tok.is(tok::kw___attribute)) {
1187 SourceLocation Loc;
1188 AttributeList *AttrList = ParseAttributes(&Loc);
1189 DeclaratorInfo.AddAttributes(AttrList, Loc);
1190 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001191
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001192 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001193 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001194 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00001195
1196 DeclPtrTy ThisDecl;
1197 if (DS.isFriendSpecified()) {
John McCall2f212b32009-09-11 21:02:39 +00001198 // TODO: handle initializers, bitfields, 'delete'
1199 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1200 /*IsDefinition*/ false,
1201 move(TemplateParams));
Douglas Gregor3447e762009-08-20 22:52:58 +00001202 } else {
John McCall07e91c02009-08-06 02:15:43 +00001203 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1204 DeclaratorInfo,
Douglas Gregor3447e762009-08-20 22:52:58 +00001205 move(TemplateParams),
John McCall07e91c02009-08-06 02:15:43 +00001206 BitfieldSize.release(),
1207 Init.release(),
1208 Deleted);
Douglas Gregor3447e762009-08-20 22:52:58 +00001209 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001210 if (ThisDecl)
1211 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001212
Douglas Gregor4d87df52008-12-16 21:30:33 +00001213 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump11289f42009-09-09 15:08:12 +00001214 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor4d87df52008-12-16 21:30:33 +00001215 != DeclSpec::SCS_typedef) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001216 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor4d87df52008-12-16 21:30:33 +00001217 }
1218
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001219 // If we don't have a comma, it is either the end of the list (a ';')
1220 // or an error, bail out.
1221 if (Tok.isNot(tok::comma))
1222 break;
Mike Stump11289f42009-09-09 15:08:12 +00001223
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001224 // Consume the comma.
1225 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001226
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001227 // Parse the next declarator.
1228 DeclaratorInfo.clear();
Sebastian Redlc13f2682008-12-09 20:22:58 +00001229 BitfieldSize = 0;
1230 Init = 0;
Sebastian Redl42e92c42009-04-12 17:16:29 +00001231 Deleted = false;
Mike Stump11289f42009-09-09 15:08:12 +00001232
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001233 // Attributes are only allowed on the second declarator.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001234 if (Tok.is(tok::kw___attribute)) {
1235 SourceLocation Loc;
1236 AttributeList *AttrList = ParseAttributes(&Loc);
1237 DeclaratorInfo.AddAttributes(AttrList, Loc);
1238 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001239
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001240 if (Tok.isNot(tok::colon))
1241 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001242 }
1243
1244 if (Tok.is(tok::semi)) {
1245 ConsumeToken();
Eli Friedman55b9ecb2009-05-29 01:49:24 +00001246 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001247 DeclsInGroup.size());
1248 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001249 }
1250
1251 Diag(Tok, diag::err_expected_semi_decl_list);
1252 // Skip to end of block or statement
1253 SkipUntil(tok::r_brace, true, true);
1254 if (Tok.is(tok::semi))
1255 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001256 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001257}
1258
1259/// ParseCXXMemberSpecification - Parse the class definition.
1260///
1261/// member-specification:
1262/// member-declaration member-specification[opt]
1263/// access-specifier ':' member-specification[opt]
1264///
1265void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001266 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Guptad7959242008-10-31 09:52:39 +00001267 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001268 TagType == DeclSpec::TST_union ||
Sanjiv Guptad7959242008-10-31 09:52:39 +00001269 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001270
Chris Lattnereae6cb62009-03-05 08:00:35 +00001271 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1272 PP.getSourceManager(),
1273 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00001274
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001275 SourceLocation LBraceLoc = ConsumeBrace();
1276
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001277 // Determine whether this is a top-level (non-nested) class.
Mike Stump11289f42009-09-09 15:08:12 +00001278 bool TopLevelClass = ClassStack.empty() ||
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001279 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001280
1281 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00001282 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001283
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001284 // Note that we are parsing a new (potentially-nested) class definition.
1285 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1286
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001287 if (TagDecl)
1288 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1289 else {
1290 SkipUntil(tok::r_brace, false, false);
1291 return;
1292 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001293
1294 // C++ 11p3: Members of a class defined with the keyword class are private
1295 // by default. Members of a class defined with the keywords struct or union
1296 // are public by default.
1297 AccessSpecifier CurAS;
1298 if (TagType == DeclSpec::TST_class)
1299 CurAS = AS_private;
1300 else
1301 CurAS = AS_public;
1302
1303 // While we still have something to read, read the member-declarations.
1304 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1305 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001306
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001307 // Check for extraneous top-level semicolon.
1308 if (Tok.is(tok::semi)) {
1309 Diag(Tok, diag::ext_extra_struct_semi);
1310 ConsumeToken();
1311 continue;
1312 }
1313
1314 AccessSpecifier AS = getAccessSpecifierIfPresent();
1315 if (AS != AS_none) {
1316 // Current token is a C++ access specifier.
1317 CurAS = AS;
1318 ConsumeToken();
1319 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1320 continue;
1321 }
1322
Douglas Gregor3447e762009-08-20 22:52:58 +00001323 // FIXME: Make sure we don't have a template here.
Mike Stump11289f42009-09-09 15:08:12 +00001324
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001325 // Parse all the comma separated declarators.
1326 ParseCXXClassMemberDeclaration(CurAS);
1327 }
Mike Stump11289f42009-09-09 15:08:12 +00001328
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001329 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001330
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001331 AttributeList *AttrList = 0;
1332 // If attributes exist after class contents, parse them.
1333 if (Tok.is(tok::kw___attribute))
1334 AttrList = ParseAttributes(); // FIXME: where should I put them?
1335
1336 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1337 LBraceLoc, RBraceLoc);
1338
1339 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1340 // complete within function bodies, default arguments,
1341 // exception-specifications, and constructor ctor-initializers (including
1342 // such things in nested classes).
1343 //
Douglas Gregor4d87df52008-12-16 21:30:33 +00001344 // FIXME: Only function bodies and constructor ctor-initializers are
1345 // parsed correctly, fix the rest.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001346 if (TopLevelClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001347 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00001348 // are complete and we can parse the delayed portions of method
1349 // declarations and the lexed inline method definitions.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001350 ParseLexedMethodDeclarations(getCurrentClass());
1351 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001352 }
1353
1354 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001355 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001356 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001357
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001358 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001359}
Douglas Gregore8381c02008-11-05 04:29:56 +00001360
1361/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1362/// which explicitly initializes the members or base classes of a
1363/// class (C++ [class.base.init]). For example, the three initializers
1364/// after the ':' in the Derived constructor below:
1365///
1366/// @code
1367/// class Base { };
1368/// class Derived : Base {
1369/// int x;
1370/// float f;
1371/// public:
1372/// Derived(float f) : Base(), x(17), f(f) { }
1373/// };
1374/// @endcode
1375///
Mike Stump11289f42009-09-09 15:08:12 +00001376/// [C++] ctor-initializer:
1377/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00001378///
Mike Stump11289f42009-09-09 15:08:12 +00001379/// [C++] mem-initializer-list:
1380/// mem-initializer
1381/// mem-initializer , mem-initializer-list
Chris Lattner83f095c2009-03-28 19:18:32 +00001382void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00001383 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1384
1385 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001386
Douglas Gregore8381c02008-11-05 04:29:56 +00001387 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Mike Stump11289f42009-09-09 15:08:12 +00001388
Douglas Gregore8381c02008-11-05 04:29:56 +00001389 do {
1390 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001391 if (!MemInit.isInvalid())
1392 MemInitializers.push_back(MemInit.get());
Douglas Gregore8381c02008-11-05 04:29:56 +00001393
1394 if (Tok.is(tok::comma))
1395 ConsumeToken();
1396 else if (Tok.is(tok::l_brace))
1397 break;
1398 else {
1399 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redla7b98a72009-04-26 20:35:05 +00001400 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregore8381c02008-11-05 04:29:56 +00001401 SkipUntil(tok::l_brace, true, true);
1402 break;
1403 }
1404 } while (true);
1405
Mike Stump11289f42009-09-09 15:08:12 +00001406 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001407 MemInitializers.data(), MemInitializers.size());
Douglas Gregore8381c02008-11-05 04:29:56 +00001408}
1409
1410/// ParseMemInitializer - Parse a C++ member initializer, which is
1411/// part of a constructor initializer that explicitly initializes one
1412/// member or base class (C++ [class.base.init]). See
1413/// ParseConstructorInitializer for an example.
1414///
1415/// [C++] mem-initializer:
1416/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump11289f42009-09-09 15:08:12 +00001417///
Douglas Gregore8381c02008-11-05 04:29:56 +00001418/// [C++] mem-initializer-id:
1419/// '::'[opt] nested-name-specifier[opt] class-name
1420/// identifier
Chris Lattner83f095c2009-03-28 19:18:32 +00001421Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00001422 // parse '::'[opt] nested-name-specifier[opt]
1423 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001424 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001425 TypeTy *TemplateTypeTy = 0;
1426 if (Tok.is(tok::annot_template_id)) {
1427 TemplateIdAnnotation *TemplateId
1428 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1429 if (TemplateId->Kind == TNK_Type_template) {
1430 AnnotateTemplateIdTokenAsType(&SS);
1431 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1432 TemplateTypeTy = Tok.getAnnotationValue();
1433 }
1434 // FIXME. May need to check for TNK_Dependent_template as well.
1435 }
1436 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001437 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00001438 return true;
1439 }
Mike Stump11289f42009-09-09 15:08:12 +00001440
Douglas Gregore8381c02008-11-05 04:29:56 +00001441 // Get the identifier. This may be a member name or a class name,
1442 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001443 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregore8381c02008-11-05 04:29:56 +00001444 SourceLocation IdLoc = ConsumeToken();
1445
1446 // Parse the '('.
1447 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001448 Diag(Tok, diag::err_expected_lparen);
Douglas Gregore8381c02008-11-05 04:29:56 +00001449 return true;
1450 }
1451 SourceLocation LParenLoc = ConsumeParen();
1452
1453 // Parse the optional expression-list.
Sebastian Redl511ed552008-11-25 22:21:31 +00001454 ExprVector ArgExprs(Actions);
Douglas Gregore8381c02008-11-05 04:29:56 +00001455 CommaLocsTy CommaLocs;
1456 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1457 SkipUntil(tok::r_paren);
1458 return true;
1459 }
1460
1461 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1462
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001463 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1464 TemplateTypeTy, IdLoc,
Sebastian Redl511ed552008-11-25 22:21:31 +00001465 LParenLoc, ArgExprs.take(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001466 ArgExprs.size(), CommaLocs.data(),
1467 RParenLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00001468}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001469
1470/// ParseExceptionSpecification - Parse a C++ exception-specification
1471/// (C++ [except.spec]).
1472///
Douglas Gregor356513d2008-12-01 18:00:20 +00001473/// exception-specification:
1474/// 'throw' '(' type-id-list [opt] ')'
1475/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00001476///
Douglas Gregor356513d2008-12-01 18:00:20 +00001477/// type-id-list:
1478/// type-id
1479/// type-id-list ',' type-id
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001480///
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001481bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redld6434562009-05-29 18:02:33 +00001482 llvm::SmallVector<TypeTy*, 2>
1483 &Exceptions,
1484 llvm::SmallVector<SourceRange, 2>
1485 &Ranges,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001486 bool &hasAnyExceptionSpec) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001487 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00001488
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001489 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001490
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001491 if (!Tok.is(tok::l_paren)) {
1492 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1493 }
1494 SourceLocation LParenLoc = ConsumeParen();
1495
Douglas Gregor356513d2008-12-01 18:00:20 +00001496 // Parse throw(...), a Microsoft extension that means "this function
1497 // can throw anything".
1498 if (Tok.is(tok::ellipsis)) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001499 hasAnyExceptionSpec = true;
Douglas Gregor356513d2008-12-01 18:00:20 +00001500 SourceLocation EllipsisLoc = ConsumeToken();
1501 if (!getLang().Microsoft)
1502 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001503 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor356513d2008-12-01 18:00:20 +00001504 return false;
1505 }
1506
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001507 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00001508 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001509 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00001510 TypeResult Res(ParseTypeName(&Range));
1511 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001512 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00001513 Ranges.push_back(Range);
1514 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001515 if (Tok.is(tok::comma))
1516 ConsumeToken();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001517 else
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001518 break;
1519 }
1520
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001521 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001522 return false;
1523}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001524
1525/// \brief We have just started parsing the definition of a new class,
1526/// so push that class onto our stack of classes that is currently
1527/// being parsed.
1528void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
Mike Stump11289f42009-09-09 15:08:12 +00001529 assert((TopLevelClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001530 "Nested class without outer class");
1531 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1532}
1533
1534/// \brief Deallocate the given parsed class and all of its nested
1535/// classes.
1536void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1537 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1538 DeallocateParsedClasses(Class->NestedClasses[I]);
1539 delete Class;
1540}
1541
1542/// \brief Pop the top class of the stack of classes that are
1543/// currently being parsed.
1544///
1545/// This routine should be called when we have finished parsing the
1546/// definition of a class, but have not yet popped the Scope
1547/// associated with the class's definition.
1548///
1549/// \returns true if the class we've popped is a top-level class,
1550/// false otherwise.
1551void Parser::PopParsingClass() {
1552 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00001553
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001554 ParsingClass *Victim = ClassStack.top();
1555 ClassStack.pop();
1556 if (Victim->TopLevelClass) {
1557 // Deallocate all of the nested classes of this class,
1558 // recursively: we don't need to keep any of this information.
1559 DeallocateParsedClasses(Victim);
1560 return;
Mike Stump11289f42009-09-09 15:08:12 +00001561 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001562 assert(!ClassStack.empty() && "Missing top-level class?");
1563
1564 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1565 Victim->NestedClasses.empty()) {
1566 // The victim is a nested class, but we will not need to perform
1567 // any processing after the definition of this class since it has
1568 // no members whose handling was delayed. Therefore, we can just
1569 // remove this nested class.
1570 delete Victim;
1571 return;
1572 }
1573
1574 // This nested class has some members that will need to be processed
1575 // after the top-level class is completely defined. Therefore, add
1576 // it to the list of nested classes within its parent.
1577 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1578 ClassStack.top()->NestedClasses.push_back(Victim);
1579 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1580}