blob: 91f86864f81fd9b86b31c50e1448331e5c8bcd5d [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 }
Douglas Gregor7861a802009-11-03 01:35:08 +0000326 // FIXME: what about conversion functions?
Anders Carlsson74d7f0d2009-06-27 00:27:47 +0000327 } else if (Tok.is(tok::identifier)) {
328 // Parse identifier.
329 TargetName = Tok.getIdentifierInfo();
330 IdentLoc = ConsumeToken();
331 } else {
332 // FIXME: Use a better diagnostic here.
Douglas Gregorfec52632009-06-20 00:51:54 +0000333 Diag(Tok, diag::err_expected_ident_in_using);
Anders Carlsson74d7f0d2009-06-27 00:27:47 +0000334
Douglas Gregorfec52632009-06-20 00:51:54 +0000335 // If there was invalid identifier, skip to end of decl, and eat ';'.
336 SkipUntil(tok::semi);
337 return DeclPtrTy();
338 }
Mike Stump11289f42009-09-09 15:08:12 +0000339
Douglas Gregorfec52632009-06-20 00:51:54 +0000340 // Parse (optional) attributes (most likely GNU strong-using extension).
341 if (Tok.is(tok::kw___attribute))
342 AttrList = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000343
Douglas Gregorfec52632009-06-20 00:51:54 +0000344 // Eat ';'.
345 DeclEnd = Tok.getLocation();
346 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
347 AttrList ? "attributes list" : "namespace name", tok::semi);
348
Anders Carlsson7b194b72009-08-29 19:54:19 +0000349 return Actions.ActOnUsingDeclaration(CurScope, AS, UsingLoc, SS,
Anders Carlsson74d7f0d2009-06-27 00:27:47 +0000350 IdentLoc, TargetName, Op,
351 AttrList, IsTypeName);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000352}
353
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000354/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
355///
356/// static_assert-declaration:
357/// static_assert ( constant-expression , string-literal ) ;
358///
Chris Lattner49836b42009-04-02 04:16:50 +0000359Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000360 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
361 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000362
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000363 if (Tok.isNot(tok::l_paren)) {
364 Diag(Tok, diag::err_expected_lparen);
Chris Lattner83f095c2009-03-28 19:18:32 +0000365 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000366 }
Mike Stump11289f42009-09-09 15:08:12 +0000367
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000368 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000369
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000370 OwningExprResult AssertExpr(ParseConstantExpression());
371 if (AssertExpr.isInvalid()) {
372 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +0000373 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000374 }
Mike Stump11289f42009-09-09 15:08:12 +0000375
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000376 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattner83f095c2009-03-28 19:18:32 +0000377 return DeclPtrTy();
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000378
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000379 if (Tok.isNot(tok::string_literal)) {
380 Diag(Tok, diag::err_expected_string_literal);
381 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +0000382 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000383 }
Mike Stump11289f42009-09-09 15:08:12 +0000384
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000385 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump11289f42009-09-09 15:08:12 +0000386 if (AssertMessage.isInvalid())
Chris Lattner83f095c2009-03-28 19:18:32 +0000387 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000388
Anders Carlsson27de6a52009-03-15 18:44:04 +0000389 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000390
Chris Lattner49836b42009-04-02 04:16:50 +0000391 DeclEnd = Tok.getLocation();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000392 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
393
Mike Stump11289f42009-09-09 15:08:12 +0000394 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson27de6a52009-03-15 18:44:04 +0000395 move(AssertMessage));
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000396}
397
Anders Carlsson74948d02009-06-24 17:47:40 +0000398/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
399///
400/// 'decltype' ( expression )
401///
402void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
403 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
404
405 SourceLocation StartLoc = ConsumeToken();
406 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000407
408 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson74948d02009-06-24 17:47:40 +0000409 "decltype")) {
410 SkipUntil(tok::r_paren);
411 return;
412 }
Mike Stump11289f42009-09-09 15:08:12 +0000413
Anders Carlsson74948d02009-06-24 17:47:40 +0000414 // Parse the expression
Mike Stump11289f42009-09-09 15:08:12 +0000415
Anders Carlsson74948d02009-06-24 17:47:40 +0000416 // C++0x [dcl.type.simple]p4:
417 // The operand of the decltype specifier is an unevaluated operand.
418 EnterExpressionEvaluationContext Unevaluated(Actions,
419 Action::Unevaluated);
420 OwningExprResult Result = ParseExpression();
421 if (Result.isInvalid()) {
422 SkipUntil(tok::r_paren);
423 return;
424 }
Mike Stump11289f42009-09-09 15:08:12 +0000425
Anders Carlsson74948d02009-06-24 17:47:40 +0000426 // Match the ')'
427 SourceLocation RParenLoc;
428 if (Tok.is(tok::r_paren))
429 RParenLoc = ConsumeParen();
430 else
431 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000432
Anders Carlsson74948d02009-06-24 17:47:40 +0000433 if (RParenLoc.isInvalid())
434 return;
435
436 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000437 unsigned DiagID;
Anders Carlsson74948d02009-06-24 17:47:40 +0000438 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump11289f42009-09-09 15:08:12 +0000439 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000440 DiagID, Result.release()))
441 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson74948d02009-06-24 17:47:40 +0000442}
443
Douglas Gregor831c93f2008-11-05 20:51:48 +0000444/// ParseClassName - Parse a C++ class-name, which names a class. Note
445/// that we only check that the result names a type; semantic analysis
446/// will need to verify that the type names a class. The result is
Douglas Gregord54dfb82009-02-25 23:52:28 +0000447/// either a type or NULL, depending on whether a type name was
Douglas Gregor831c93f2008-11-05 20:51:48 +0000448/// found.
449///
450/// class-name: [C++ 9.1]
451/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000452/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000453///
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000454Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahanian4041dfc2009-07-20 17:43:15 +0000455 const CXXScopeSpec *SS,
456 bool DestrExpected) {
Douglas Gregord54dfb82009-02-25 23:52:28 +0000457 // Check whether we have a template-id that names a type.
458 if (Tok.is(tok::annot_template_id)) {
Mike Stump11289f42009-09-09 15:08:12 +0000459 TemplateIdAnnotation *TemplateId
Douglas Gregord54dfb82009-02-25 23:52:28 +0000460 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +0000461 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000462 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000463
464 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
465 TypeTy *Type = Tok.getAnnotationValue();
466 EndLocation = Tok.getAnnotationEndLoc();
467 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000468
469 if (Type)
470 return Type;
471 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000472 }
473
474 // Fall through to produce an error below.
475 }
476
Douglas Gregor831c93f2008-11-05 20:51:48 +0000477 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000478 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000479 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000480 }
481
482 // We have an identifier; check whether it is actually a type.
Mike Stump11289f42009-09-09 15:08:12 +0000483 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor5e0962f2009-08-26 18:27:52 +0000484 Tok.getLocation(), CurScope, SS,
485 true);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000486 if (!Type) {
Mike Stump11289f42009-09-09 15:08:12 +0000487 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
Fariborz Jahanian4041dfc2009-07-20 17:43:15 +0000488 : diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000489 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000490 }
491
492 // Consume the identifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +0000493 EndLocation = ConsumeToken();
Douglas Gregor831c93f2008-11-05 20:51:48 +0000494 return Type;
495}
496
Douglas Gregor556877c2008-04-13 21:30:24 +0000497/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
498/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
499/// until we reach the start of a definition or see a token that
500/// cannot start a definition.
501///
502/// class-specifier: [C++ class]
503/// class-head '{' member-specification[opt] '}'
504/// class-head '{' member-specification[opt] '}' attributes[opt]
505/// class-head:
506/// class-key identifier[opt] base-clause[opt]
507/// class-key nested-name-specifier identifier base-clause[opt]
508/// class-key nested-name-specifier[opt] simple-template-id
509/// base-clause[opt]
510/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000511/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +0000512/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000513/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +0000514/// simple-template-id base-clause[opt]
515/// class-key:
516/// 'class'
517/// 'struct'
518/// 'union'
519///
520/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +0000521/// class-key ::[opt] nested-name-specifier[opt] identifier
522/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
523/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +0000524///
525/// Note that the C++ class-specifier and elaborated-type-specifier,
526/// together, subsume the C99 struct-or-union-specifier:
527///
528/// struct-or-union-specifier: [C99 6.7.2.1]
529/// struct-or-union identifier[opt] '{' struct-contents '}'
530/// struct-or-union identifier
531/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
532/// '}' attributes[opt]
533/// [GNU] struct-or-union attributes[opt] identifier
534/// struct-or-union:
535/// 'struct'
536/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000537void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
538 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000539 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor6c2adff2009-03-25 22:00:53 +0000540 AccessSpecifier AS) {
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000541 DeclSpec::TST TagType;
542 if (TagTokKind == tok::kw_struct)
543 TagType = DeclSpec::TST_struct;
544 else if (TagTokKind == tok::kw_class)
545 TagType = DeclSpec::TST_class;
546 else {
547 assert(TagTokKind == tok::kw_union && "Not a class specifier");
548 TagType = DeclSpec::TST_union;
549 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000550
Douglas Gregorf45b0cf2009-09-18 15:37:17 +0000551 if (Tok.is(tok::code_completion)) {
552 // Code completion for a struct, class, or union name.
553 Actions.CodeCompleteTag(CurScope, TagType);
554 ConsumeToken();
555 }
556
Douglas Gregor556877c2008-04-13 21:30:24 +0000557 AttributeList *Attr = 0;
558 // If attributes exist after tag, parse them.
559 if (Tok.is(tok::kw___attribute))
560 Attr = ParseAttributes();
561
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000562 // If declspecs exist after tag, parse them.
Eli Friedman53339e02009-06-08 23:27:34 +0000563 if (Tok.is(tok::kw___declspec))
564 Attr = ParseMicrosoftDeclSpec(Attr);
Mike Stump11289f42009-09-09 15:08:12 +0000565
Douglas Gregor119b0c72009-09-04 05:53:02 +0000566 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
567 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
568 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump11289f42009-09-09 15:08:12 +0000569 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregor119b0c72009-09-04 05:53:02 +0000570 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
571 // properly.
572 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
573 Tok.setKind(tok::identifier);
574 }
575
576 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
577 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
578 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump11289f42009-09-09 15:08:12 +0000579 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregor119b0c72009-09-04 05:53:02 +0000580 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
581 // properly.
582 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
583 Tok.setKind(tok::identifier);
584 }
Mike Stump11289f42009-09-09 15:08:12 +0000585
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000586 // Parse the (optional) nested-name-specifier.
587 CXXScopeSpec SS;
Mike Stump11289f42009-09-09 15:08:12 +0000588 if (getLang().CPlusPlus &&
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000589 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
Douglas Gregor7f741122009-02-25 19:37:18 +0000590 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000591 Diag(Tok, diag::err_expected_ident);
Douglas Gregor67a65642009-02-17 23:15:12 +0000592
Douglas Gregor916462b2009-10-30 21:46:58 +0000593 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
594
Douglas Gregor67a65642009-02-17 23:15:12 +0000595 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +0000596 IdentifierInfo *Name = 0;
597 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +0000598 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +0000599 if (Tok.is(tok::identifier)) {
600 Name = Tok.getIdentifierInfo();
601 NameLoc = ConsumeToken();
Douglas Gregor916462b2009-10-30 21:46:58 +0000602
603 if (Tok.is(tok::less)) {
604 // The name was supposed to refer to a template, but didn't.
605 // Eat the template argument list and try to continue parsing this as
606 // a class (or template thereof).
607 TemplateArgList TemplateArgs;
608 TemplateArgIsTypeList TemplateArgIsType;
609 TemplateArgLocationList TemplateArgLocations;
610 SourceLocation LAngleLoc, RAngleLoc;
611 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
612 true, LAngleLoc,
613 TemplateArgs, TemplateArgIsType,
614 TemplateArgLocations, RAngleLoc)) {
615 // We couldn't parse the template argument list at all, so don't
616 // try to give any location information for the list.
617 LAngleLoc = RAngleLoc = SourceLocation();
618 }
619
620 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000621 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor916462b2009-10-30 21:46:58 +0000622 << (TagType == DeclSpec::TST_class? 0
623 : TagType == DeclSpec::TST_struct? 1
624 : 2)
625 << Name
626 << SourceRange(LAngleLoc, RAngleLoc);
627
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000628 // Strip off the last template parameter list if it was empty, since
629 // we've removed its template argument list.
630 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
631 if (TemplateParams && TemplateParams->size() > 1) {
632 TemplateParams->pop_back();
633 } else {
634 TemplateParams = 0;
635 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
636 = ParsedTemplateInfo::NonTemplate;
637 }
638 } else if (TemplateInfo.Kind
639 == ParsedTemplateInfo::ExplicitInstantiation) {
640 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +0000641 TemplateParams = 0;
642 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
643 = ParsedTemplateInfo::NonTemplate;
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000644 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
645 = SourceLocation();
646 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
647 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +0000648 }
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000649
Douglas Gregor916462b2009-10-30 21:46:58 +0000650
651 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000652 } else if (Tok.is(tok::annot_template_id)) {
653 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
654 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +0000655
Douglas Gregorb67535d2009-03-31 00:43:58 +0000656 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000657 // The template-name in the simple-template-id refers to
658 // something other than a class template. Give an appropriate
659 // error message and skip to the ';'.
660 SourceRange Range(NameLoc);
661 if (SS.isNotEmpty())
662 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +0000663
Douglas Gregor7f741122009-02-25 19:37:18 +0000664 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
665 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +0000666
Douglas Gregor7f741122009-02-25 19:37:18 +0000667 DS.SetTypeSpecError();
668 SkipUntil(tok::semi, false, true);
669 TemplateId->Destroy();
670 return;
Douglas Gregor67a65642009-02-17 23:15:12 +0000671 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000672 }
673
John McCall07e91c02009-08-06 02:15:43 +0000674 // There are four options here. If we have 'struct foo;', then this
675 // is either a forward declaration or a friend declaration, which
676 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor7f741122009-02-25 19:37:18 +0000677 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregor556877c2008-04-13 21:30:24 +0000678 // something like 'struct foo xyz', a reference.
John McCall9bb74a52009-07-31 02:45:11 +0000679 Action::TagUseKind TUK;
Douglas Gregor3dad8422009-09-26 06:47:28 +0000680 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) {
681 if (DS.isFriendSpecified()) {
682 // C++ [class.friend]p2:
683 // A class shall not be defined in a friend declaration.
684 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
685 << SourceRange(DS.getFriendSpecLoc());
686
687 // Skip everything up to the semicolon, so that this looks like a proper
688 // friend class (or template thereof) declaration.
689 SkipUntil(tok::semi, true, true);
690 TUK = Action::TUK_Friend;
691 } else {
692 // Okay, this is a class definition.
693 TUK = Action::TUK_Definition;
694 }
695 } else if (Tok.is(tok::semi))
John McCall07e91c02009-08-06 02:15:43 +0000696 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregor556877c2008-04-13 21:30:24 +0000697 else
John McCall9bb74a52009-07-31 02:45:11 +0000698 TUK = Action::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +0000699
John McCall9bb74a52009-07-31 02:45:11 +0000700 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000701 // We have a declaration or reference to an anonymous class.
Chris Lattner6d29c102008-11-18 07:48:38 +0000702 Diag(StartLoc, diag::err_anon_type_definition)
703 << DeclSpec::getSpecifierName(TagType);
Douglas Gregor556877c2008-04-13 21:30:24 +0000704
705 // Skip the rest of this declarator, up until the comma or semicolon.
706 SkipUntil(tok::comma, true);
Douglas Gregor7f741122009-02-25 19:37:18 +0000707
708 if (TemplateId)
709 TemplateId->Destroy();
Douglas Gregor556877c2008-04-13 21:30:24 +0000710 return;
711 }
712
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000713 // Create the tag portion of the class or class template.
John McCall7f41d982009-09-11 04:59:25 +0000714 Action::DeclResult TagOrTempResult = true; // invalid
715 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000716
John McCall9bb74a52009-07-31 02:45:11 +0000717 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000718 // to turn that template-id into a type.
719
Douglas Gregord6ab8742009-05-28 23:31:59 +0000720 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +0000721 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000722 // Explicit specialization, class template partial specialization,
723 // or explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +0000724 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor7f741122009-02-25 19:37:18 +0000725 TemplateId->getTemplateArgs(),
726 TemplateId->getTemplateArgIsType(),
727 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000728 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000729 TUK == Action::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000730 // This is an explicit instantiation of a class template.
731 TagOrTempResult
Mike Stump11289f42009-09-09 15:08:12 +0000732 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor43e75172009-09-04 06:33:52 +0000733 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000734 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000735 TagType,
Mike Stump11289f42009-09-09 15:08:12 +0000736 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000737 SS,
Mike Stump11289f42009-09-09 15:08:12 +0000738 TemplateTy::make(TemplateId->Template),
739 TemplateId->TemplateNameLoc,
740 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000741 TemplateArgsPtr,
742 TemplateId->getTemplateArgLocations(),
Mike Stump11289f42009-09-09 15:08:12 +0000743 TemplateId->RAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000744 Attr);
Douglas Gregor2208a292009-09-26 20:57:03 +0000745 } else if (TUK == Action::TUK_Reference) {
John McCall7f41d982009-09-11 04:59:25 +0000746 TypeResult
John McCalld8fe9af2009-09-08 17:47:29 +0000747 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
748 TemplateId->TemplateNameLoc,
749 TemplateId->LAngleLoc,
750 TemplateArgsPtr,
751 TemplateId->getTemplateArgLocations(),
752 TemplateId->RAngleLoc);
753
John McCall7f41d982009-09-11 04:59:25 +0000754 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
755 TagType, StartLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000756 } else {
757 // This is an explicit specialization or a class template
758 // partial specialization.
759 TemplateParameterLists FakedParamLists;
760
761 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
762 // This looks like an explicit instantiation, because we have
763 // something like
764 //
765 // template class Foo<X>
766 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000767 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000768 // meant to be an explicit specialization, but the user forgot
769 // the '<>' after 'template'.
John McCall9bb74a52009-07-31 02:45:11 +0000770 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000771
Mike Stump11289f42009-09-09 15:08:12 +0000772 SourceLocation LAngleLoc
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000773 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000774 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000775 diag::err_explicit_instantiation_with_definition)
776 << SourceRange(TemplateInfo.TemplateLoc)
777 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
778
779 // Create a fake template parameter list that contains only
780 // "template<>", so that we treat this construct as a class
781 // template specialization.
782 FakedParamLists.push_back(
Mike Stump11289f42009-09-09 15:08:12 +0000783 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000784 TemplateInfo.TemplateLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000785 LAngleLoc,
786 0, 0,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000787 LAngleLoc));
788 TemplateParams = &FakedParamLists;
789 }
790
791 // Build the class template specialization.
792 TagOrTempResult
John McCall9bb74a52009-07-31 02:45:11 +0000793 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor7f741122009-02-25 19:37:18 +0000794 StartLoc, SS,
Mike Stump11289f42009-09-09 15:08:12 +0000795 TemplateTy::make(TemplateId->Template),
796 TemplateId->TemplateNameLoc,
797 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +0000798 TemplateArgsPtr,
799 TemplateId->getTemplateArgLocations(),
Mike Stump11289f42009-09-09 15:08:12 +0000800 TemplateId->RAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +0000801 Attr,
Mike Stump11289f42009-09-09 15:08:12 +0000802 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor67a65642009-02-17 23:15:12 +0000803 TemplateParams? &(*TemplateParams)[0] : 0,
804 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000805 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000806 TemplateId->Destroy();
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000807 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000808 TUK == Action::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000809 // Explicit instantiation of a member of a class template
810 // specialization, e.g.,
811 //
812 // template struct Outer<int>::Inner;
813 //
814 TagOrTempResult
Mike Stump11289f42009-09-09 15:08:12 +0000815 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor43e75172009-09-04 06:33:52 +0000816 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000817 TemplateInfo.TemplateLoc,
818 TagType, StartLoc, SS, Name,
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000819 NameLoc, Attr);
820 } else {
821 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000822 TUK == Action::TUK_Definition) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000823 // FIXME: Diagnose this particular error.
824 }
825
John McCall7f41d982009-09-11 04:59:25 +0000826 bool IsDependent = false;
827
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000828 // Declaration or definition of a class type
Mike Stump11289f42009-09-09 15:08:12 +0000829 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000830 Name, NameLoc, Attr, AS,
Mike Stump11289f42009-09-09 15:08:12 +0000831 Action::MultiTemplateParamsArg(Actions,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000832 TemplateParams? &(*TemplateParams)[0] : 0,
833 TemplateParams? TemplateParams->size() : 0),
John McCall7f41d982009-09-11 04:59:25 +0000834 Owned, IsDependent);
835
836 // If ActOnTag said the type was dependent, try again with the
837 // less common call.
838 if (IsDependent)
839 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
840 SS, Name, StartLoc, NameLoc);
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000841 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000842
843 // Parse the optional base clause (C++ only).
Chris Lattnera3778332009-02-16 22:07:16 +0000844 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregorc08f4892009-03-25 00:13:59 +0000845 ParseBaseClause(TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +0000846
847 // If there is a body, parse it and inform the actions module.
848 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000849 if (getLang().CPlusPlus)
Douglas Gregorc08f4892009-03-25 00:13:59 +0000850 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000851 else
Douglas Gregorc08f4892009-03-25 00:13:59 +0000852 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall9bb74a52009-07-31 02:45:11 +0000853 else if (TUK == Action::TUK_Definition) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000854 // FIXME: Complain that we have a base-specifier list but no
855 // definition.
Chris Lattner6d29c102008-11-18 07:48:38 +0000856 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregor556877c2008-04-13 21:30:24 +0000857 }
858
John McCall7f41d982009-09-11 04:59:25 +0000859 void *Result;
860 if (!TypeResult.isInvalid()) {
861 TagType = DeclSpec::TST_typename;
862 Result = TypeResult.get();
863 Owned = false;
864 } else if (!TagOrTempResult.isInvalid()) {
865 Result = TagOrTempResult.get().getAs<void>();
866 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000867 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +0000868 return;
869 }
Mike Stump11289f42009-09-09 15:08:12 +0000870
John McCall49bfce42009-08-03 20:12:06 +0000871 const char *PrevSpec = 0;
872 unsigned DiagID;
John McCall7f41d982009-09-11 04:59:25 +0000873
John McCall49bfce42009-08-03 20:12:06 +0000874 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
John McCall7f41d982009-09-11 04:59:25 +0000875 Result, Owned))
John McCall49bfce42009-08-03 20:12:06 +0000876 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregor556877c2008-04-13 21:30:24 +0000877}
878
Mike Stump11289f42009-09-09 15:08:12 +0000879/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +0000880///
881/// base-clause : [C++ class.derived]
882/// ':' base-specifier-list
883/// base-specifier-list:
884/// base-specifier '...'[opt]
885/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattner83f095c2009-03-28 19:18:32 +0000886void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000887 assert(Tok.is(tok::colon) && "Not a base clause");
888 ConsumeToken();
889
Douglas Gregor29a92472008-10-22 17:49:05 +0000890 // Build up an array of parsed base specifiers.
891 llvm::SmallVector<BaseTy *, 8> BaseInfo;
892
Douglas Gregor556877c2008-04-13 21:30:24 +0000893 while (true) {
894 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +0000895 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +0000896 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000897 // Skip the rest of this base specifier, up until the comma or
898 // opening brace.
Douglas Gregor29a92472008-10-22 17:49:05 +0000899 SkipUntil(tok::comma, tok::l_brace, true, true);
900 } else {
901 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +0000902 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +0000903 }
904
905 // If the next token is a comma, consume it and keep reading
906 // base-specifiers.
907 if (Tok.isNot(tok::comma)) break;
Mike Stump11289f42009-09-09 15:08:12 +0000908
Douglas Gregor556877c2008-04-13 21:30:24 +0000909 // Consume the comma.
910 ConsumeToken();
911 }
Douglas Gregor29a92472008-10-22 17:49:05 +0000912
913 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +0000914 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +0000915}
916
917/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
918/// one entry in the base class list of a class specifier, for example:
919/// class foo : public bar, virtual private baz {
920/// 'public bar' and 'virtual private baz' are each base-specifiers.
921///
922/// base-specifier: [C++ class.derived]
923/// ::[opt] nested-name-specifier[opt] class-name
924/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
925/// class-name
926/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
927/// class-name
Chris Lattner83f095c2009-03-28 19:18:32 +0000928Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000929 bool IsVirtual = false;
930 SourceLocation StartLoc = Tok.getLocation();
931
932 // Parse the 'virtual' keyword.
933 if (Tok.is(tok::kw_virtual)) {
934 ConsumeToken();
935 IsVirtual = true;
936 }
937
938 // Parse an (optional) access specifier.
939 AccessSpecifier Access = getAccessSpecifierIfPresent();
940 if (Access)
941 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000942
Douglas Gregor556877c2008-04-13 21:30:24 +0000943 // Parse the 'virtual' keyword (again!), in case it came after the
944 // access specifier.
945 if (Tok.is(tok::kw_virtual)) {
946 SourceLocation VirtualLoc = ConsumeToken();
947 if (IsVirtual) {
948 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +0000949 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000950 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregor556877c2008-04-13 21:30:24 +0000951 }
952
953 IsVirtual = true;
954 }
955
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000956 // Parse optional '::' and optional nested-name-specifier.
957 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000958 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Douglas Gregor556877c2008-04-13 21:30:24 +0000959
Douglas Gregor556877c2008-04-13 21:30:24 +0000960 // The location of the base class itself.
961 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor831c93f2008-11-05 20:51:48 +0000962
963 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +0000964 SourceLocation EndLocation;
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000965 TypeResult BaseType = ParseClassName(EndLocation, &SS);
966 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +0000967 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000968
969 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +0000970 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +0000971
Douglas Gregor556877c2008-04-13 21:30:24 +0000972 // Notify semantic analysis that we have parsed a complete
973 // base-specifier.
Sebastian Redl511ed552008-11-25 22:21:31 +0000974 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000975 BaseType.get(), BaseLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +0000976}
977
978/// getAccessSpecifierIfPresent - Determine whether the next token is
979/// a C++ access-specifier.
980///
981/// access-specifier: [C++ class.derived]
982/// 'private'
983/// 'protected'
984/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +0000985AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +0000986 switch (Tok.getKind()) {
987 default: return AS_none;
988 case tok::kw_private: return AS_private;
989 case tok::kw_protected: return AS_protected;
990 case tok::kw_public: return AS_public;
991 }
992}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000993
Eli Friedman3af2a772009-07-22 21:45:50 +0000994void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
995 DeclPtrTy ThisDecl) {
996 // We just declared a member function. If this member function
997 // has any default arguments, we'll need to parse them later.
998 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000999 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedman3af2a772009-07-22 21:45:50 +00001000 = DeclaratorInfo.getTypeObject(0).Fun;
1001 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1002 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1003 if (!LateMethod) {
1004 // Push this method onto the stack of late-parsed method
1005 // declarations.
1006 getCurrentClass().MethodDecls.push_back(
1007 LateParsedMethodDeclaration(ThisDecl));
1008 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregorc45a40a2009-08-22 00:34:47 +00001009 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001010
1011 // Add all of the parameters prior to this one (they don't
1012 // have default arguments).
1013 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1014 for (unsigned I = 0; I < ParamIdx; ++I)
1015 LateMethod->DefaultArgs.push_back(
1016 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
1017 }
1018
1019 // Add this parameter to the list of parameters (it or may
1020 // not have a default argument).
1021 LateMethod->DefaultArgs.push_back(
1022 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1023 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1024 }
1025 }
1026}
1027
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001028/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1029///
1030/// member-declaration:
1031/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1032/// function-definition ';'[opt]
1033/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1034/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001035/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001036/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001037/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001038///
1039/// member-declarator-list:
1040/// member-declarator
1041/// member-declarator-list ',' member-declarator
1042///
1043/// member-declarator:
1044/// declarator pure-specifier[opt]
1045/// declarator constant-initializer[opt]
1046/// identifier[opt] ':' constant-expression
1047///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001048/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001049/// '= 0'
1050///
1051/// constant-initializer:
1052/// '=' constant-expression
1053///
Douglas Gregor3447e762009-08-20 22:52:58 +00001054void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
1055 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001056 // static_assert-declaration
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001057 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001058 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00001059 SourceLocation DeclEnd;
1060 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001061 return;
1062 }
Mike Stump11289f42009-09-09 15:08:12 +00001063
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001064 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00001065 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00001066 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00001067 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00001068 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001069 AS);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001070 return;
1071 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001072
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001073 // Handle: member-declaration ::= '__extension__' member-declaration
1074 if (Tok.is(tok::kw___extension__)) {
1075 // __extension__ silences extension warnings in the subexpression.
1076 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1077 ConsumeToken();
Douglas Gregor3447e762009-08-20 22:52:58 +00001078 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001079 }
Douglas Gregorfec52632009-06-20 00:51:54 +00001080
1081 if (Tok.is(tok::kw_using)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001082 // FIXME: Check for template aliases
Mike Stump11289f42009-09-09 15:08:12 +00001083
Douglas Gregorfec52632009-06-20 00:51:54 +00001084 // Eat 'using'.
1085 SourceLocation UsingLoc = ConsumeToken();
1086
1087 if (Tok.is(tok::kw_namespace)) {
1088 Diag(UsingLoc, diag::err_using_namespace_in_class);
1089 SkipUntil(tok::semi, true, true);
1090 }
1091 else {
1092 SourceLocation DeclEnd;
1093 // Otherwise, it must be using-declaration.
Anders Carlsson7b194b72009-08-29 19:54:19 +00001094 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00001095 }
1096 return;
1097 }
1098
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001099 SourceLocation DSStart = Tok.getLocation();
1100 // decl-specifier-seq:
1101 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001102 ParsingDeclSpec DS(*this);
Douglas Gregor3447e762009-08-20 22:52:58 +00001103 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001104
John McCall11083da2009-09-16 22:47:08 +00001105 Action::MultiTemplateParamsArg TemplateParams(Actions,
1106 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1107 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1108
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001109 if (Tok.is(tok::semi)) {
1110 ConsumeToken();
Douglas Gregor3dad8422009-09-26 06:47:28 +00001111 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall07e91c02009-08-06 02:15:43 +00001112 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001113 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001114
John McCall28a6aea2009-11-04 02:18:39 +00001115 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001116
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001117 if (Tok.isNot(tok::colon)) {
1118 // Parse the first declarator.
1119 ParseDeclarator(DeclaratorInfo);
1120 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +00001121 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001122 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001123 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001124 if (Tok.is(tok::semi))
1125 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001126 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001127 }
1128
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001129 // function-definition:
Douglas Gregore8381c02008-11-05 04:29:56 +00001130 if (Tok.is(tok::l_brace)
Sebastian Redla7b98a72009-04-26 20:35:05 +00001131 || (DeclaratorInfo.isFunctionDeclarator() &&
1132 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001133 if (!DeclaratorInfo.isFunctionDeclarator()) {
1134 Diag(Tok, diag::err_func_def_no_params);
1135 ConsumeBrace();
1136 SkipUntil(tok::r_brace, true);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001137 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001138 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001139
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001140 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1141 Diag(Tok, diag::err_function_declared_typedef);
1142 // This recovery skips the entire function body. It would be nice
1143 // to simply call ParseCXXInlineMethodDef() below, however Sema
1144 // assumes the declarator represents a function, not a typedef.
1145 ConsumeBrace();
1146 SkipUntil(tok::r_brace, true);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001147 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001148 }
1149
Douglas Gregor3447e762009-08-20 22:52:58 +00001150 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001151 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001152 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001153 }
1154
1155 // member-declarator-list:
1156 // member-declarator
1157 // member-declarator-list ',' member-declarator
1158
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001159 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redlc13f2682008-12-09 20:22:58 +00001160 OwningExprResult BitfieldSize(Actions);
1161 OwningExprResult Init(Actions);
Sebastian Redl42e92c42009-04-12 17:16:29 +00001162 bool Deleted = false;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001163
1164 while (1) {
1165
1166 // member-declarator:
1167 // declarator pure-specifier[opt]
1168 // declarator constant-initializer[opt]
1169 // identifier[opt] ':' constant-expression
1170
1171 if (Tok.is(tok::colon)) {
1172 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001173 BitfieldSize = ParseConstantExpression();
1174 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001175 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001176 }
Mike Stump11289f42009-09-09 15:08:12 +00001177
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001178 // pure-specifier:
1179 // '= 0'
1180 //
1181 // constant-initializer:
1182 // '=' constant-expression
Sebastian Redl42e92c42009-04-12 17:16:29 +00001183 //
1184 // defaulted/deleted function-definition:
1185 // '=' 'default' [TODO]
1186 // '=' 'delete'
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001187
1188 if (Tok.is(tok::equal)) {
1189 ConsumeToken();
Sebastian Redl42e92c42009-04-12 17:16:29 +00001190 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1191 ConsumeToken();
1192 Deleted = true;
1193 } else {
1194 Init = ParseInitializer();
1195 if (Init.isInvalid())
1196 SkipUntil(tok::comma, true, true);
1197 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001198 }
1199
1200 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001201 if (Tok.is(tok::kw___attribute)) {
1202 SourceLocation Loc;
1203 AttributeList *AttrList = ParseAttributes(&Loc);
1204 DeclaratorInfo.AddAttributes(AttrList, Loc);
1205 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001206
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001207 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001208 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001209 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00001210
1211 DeclPtrTy ThisDecl;
1212 if (DS.isFriendSpecified()) {
John McCall2f212b32009-09-11 21:02:39 +00001213 // TODO: handle initializers, bitfields, 'delete'
1214 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1215 /*IsDefinition*/ false,
1216 move(TemplateParams));
Douglas Gregor3447e762009-08-20 22:52:58 +00001217 } else {
John McCall07e91c02009-08-06 02:15:43 +00001218 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1219 DeclaratorInfo,
Douglas Gregor3447e762009-08-20 22:52:58 +00001220 move(TemplateParams),
John McCall07e91c02009-08-06 02:15:43 +00001221 BitfieldSize.release(),
1222 Init.release(),
1223 Deleted);
Douglas Gregor3447e762009-08-20 22:52:58 +00001224 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001225 if (ThisDecl)
1226 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001227
Douglas Gregor4d87df52008-12-16 21:30:33 +00001228 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump11289f42009-09-09 15:08:12 +00001229 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor4d87df52008-12-16 21:30:33 +00001230 != DeclSpec::SCS_typedef) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001231 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor4d87df52008-12-16 21:30:33 +00001232 }
1233
John McCall28a6aea2009-11-04 02:18:39 +00001234 DeclaratorInfo.complete(ThisDecl);
1235
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001236 // If we don't have a comma, it is either the end of the list (a ';')
1237 // or an error, bail out.
1238 if (Tok.isNot(tok::comma))
1239 break;
Mike Stump11289f42009-09-09 15:08:12 +00001240
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001241 // Consume the comma.
1242 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001243
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001244 // Parse the next declarator.
1245 DeclaratorInfo.clear();
Sebastian Redlc13f2682008-12-09 20:22:58 +00001246 BitfieldSize = 0;
1247 Init = 0;
Sebastian Redl42e92c42009-04-12 17:16:29 +00001248 Deleted = false;
Mike Stump11289f42009-09-09 15:08:12 +00001249
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001250 // Attributes are only allowed on the second declarator.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001251 if (Tok.is(tok::kw___attribute)) {
1252 SourceLocation Loc;
1253 AttributeList *AttrList = ParseAttributes(&Loc);
1254 DeclaratorInfo.AddAttributes(AttrList, Loc);
1255 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001256
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001257 if (Tok.isNot(tok::colon))
1258 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001259 }
1260
1261 if (Tok.is(tok::semi)) {
1262 ConsumeToken();
Eli Friedman55b9ecb2009-05-29 01:49:24 +00001263 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001264 DeclsInGroup.size());
1265 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001266 }
1267
1268 Diag(Tok, diag::err_expected_semi_decl_list);
1269 // Skip to end of block or statement
1270 SkipUntil(tok::r_brace, true, true);
1271 if (Tok.is(tok::semi))
1272 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001273 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001274}
1275
1276/// ParseCXXMemberSpecification - Parse the class definition.
1277///
1278/// member-specification:
1279/// member-declaration member-specification[opt]
1280/// access-specifier ':' member-specification[opt]
1281///
1282void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001283 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Guptad7959242008-10-31 09:52:39 +00001284 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001285 TagType == DeclSpec::TST_union ||
Sanjiv Guptad7959242008-10-31 09:52:39 +00001286 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001287
Chris Lattnereae6cb62009-03-05 08:00:35 +00001288 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1289 PP.getSourceManager(),
1290 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00001291
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001292 SourceLocation LBraceLoc = ConsumeBrace();
1293
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001294 // Determine whether this is a top-level (non-nested) class.
Mike Stump11289f42009-09-09 15:08:12 +00001295 bool TopLevelClass = ClassStack.empty() ||
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001296 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001297
1298 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00001299 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001300
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001301 // Note that we are parsing a new (potentially-nested) class definition.
1302 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1303
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001304 if (TagDecl)
1305 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1306 else {
1307 SkipUntil(tok::r_brace, false, false);
1308 return;
1309 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001310
1311 // C++ 11p3: Members of a class defined with the keyword class are private
1312 // by default. Members of a class defined with the keywords struct or union
1313 // are public by default.
1314 AccessSpecifier CurAS;
1315 if (TagType == DeclSpec::TST_class)
1316 CurAS = AS_private;
1317 else
1318 CurAS = AS_public;
1319
1320 // While we still have something to read, read the member-declarations.
1321 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1322 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001323
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001324 // Check for extraneous top-level semicolon.
1325 if (Tok.is(tok::semi)) {
1326 Diag(Tok, diag::ext_extra_struct_semi);
1327 ConsumeToken();
1328 continue;
1329 }
1330
1331 AccessSpecifier AS = getAccessSpecifierIfPresent();
1332 if (AS != AS_none) {
1333 // Current token is a C++ access specifier.
1334 CurAS = AS;
1335 ConsumeToken();
1336 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1337 continue;
1338 }
1339
Douglas Gregor3447e762009-08-20 22:52:58 +00001340 // FIXME: Make sure we don't have a template here.
Mike Stump11289f42009-09-09 15:08:12 +00001341
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001342 // Parse all the comma separated declarators.
1343 ParseCXXClassMemberDeclaration(CurAS);
1344 }
Mike Stump11289f42009-09-09 15:08:12 +00001345
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001346 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001347
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001348 AttributeList *AttrList = 0;
1349 // If attributes exist after class contents, parse them.
1350 if (Tok.is(tok::kw___attribute))
1351 AttrList = ParseAttributes(); // FIXME: where should I put them?
1352
1353 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1354 LBraceLoc, RBraceLoc);
1355
1356 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1357 // complete within function bodies, default arguments,
1358 // exception-specifications, and constructor ctor-initializers (including
1359 // such things in nested classes).
1360 //
Douglas Gregor4d87df52008-12-16 21:30:33 +00001361 // FIXME: Only function bodies and constructor ctor-initializers are
1362 // parsed correctly, fix the rest.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001363 if (TopLevelClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001364 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00001365 // are complete and we can parse the delayed portions of method
1366 // declarations and the lexed inline method definitions.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001367 ParseLexedMethodDeclarations(getCurrentClass());
1368 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001369 }
1370
1371 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001372 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001373 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001374
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001375 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001376}
Douglas Gregore8381c02008-11-05 04:29:56 +00001377
1378/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1379/// which explicitly initializes the members or base classes of a
1380/// class (C++ [class.base.init]). For example, the three initializers
1381/// after the ':' in the Derived constructor below:
1382///
1383/// @code
1384/// class Base { };
1385/// class Derived : Base {
1386/// int x;
1387/// float f;
1388/// public:
1389/// Derived(float f) : Base(), x(17), f(f) { }
1390/// };
1391/// @endcode
1392///
Mike Stump11289f42009-09-09 15:08:12 +00001393/// [C++] ctor-initializer:
1394/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00001395///
Mike Stump11289f42009-09-09 15:08:12 +00001396/// [C++] mem-initializer-list:
1397/// mem-initializer
1398/// mem-initializer , mem-initializer-list
Chris Lattner83f095c2009-03-28 19:18:32 +00001399void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00001400 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1401
1402 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001403
Douglas Gregore8381c02008-11-05 04:29:56 +00001404 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Mike Stump11289f42009-09-09 15:08:12 +00001405
Douglas Gregore8381c02008-11-05 04:29:56 +00001406 do {
1407 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001408 if (!MemInit.isInvalid())
1409 MemInitializers.push_back(MemInit.get());
Douglas Gregore8381c02008-11-05 04:29:56 +00001410
1411 if (Tok.is(tok::comma))
1412 ConsumeToken();
1413 else if (Tok.is(tok::l_brace))
1414 break;
1415 else {
1416 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redla7b98a72009-04-26 20:35:05 +00001417 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregore8381c02008-11-05 04:29:56 +00001418 SkipUntil(tok::l_brace, true, true);
1419 break;
1420 }
1421 } while (true);
1422
Mike Stump11289f42009-09-09 15:08:12 +00001423 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001424 MemInitializers.data(), MemInitializers.size());
Douglas Gregore8381c02008-11-05 04:29:56 +00001425}
1426
1427/// ParseMemInitializer - Parse a C++ member initializer, which is
1428/// part of a constructor initializer that explicitly initializes one
1429/// member or base class (C++ [class.base.init]). See
1430/// ParseConstructorInitializer for an example.
1431///
1432/// [C++] mem-initializer:
1433/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump11289f42009-09-09 15:08:12 +00001434///
Douglas Gregore8381c02008-11-05 04:29:56 +00001435/// [C++] mem-initializer-id:
1436/// '::'[opt] nested-name-specifier[opt] class-name
1437/// identifier
Chris Lattner83f095c2009-03-28 19:18:32 +00001438Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00001439 // parse '::'[opt] nested-name-specifier[opt]
1440 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001441 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001442 TypeTy *TemplateTypeTy = 0;
1443 if (Tok.is(tok::annot_template_id)) {
1444 TemplateIdAnnotation *TemplateId
1445 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1446 if (TemplateId->Kind == TNK_Type_template) {
1447 AnnotateTemplateIdTokenAsType(&SS);
1448 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1449 TemplateTypeTy = Tok.getAnnotationValue();
1450 }
1451 // FIXME. May need to check for TNK_Dependent_template as well.
1452 }
1453 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001454 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00001455 return true;
1456 }
Mike Stump11289f42009-09-09 15:08:12 +00001457
Douglas Gregore8381c02008-11-05 04:29:56 +00001458 // Get the identifier. This may be a member name or a class name,
1459 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001460 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregore8381c02008-11-05 04:29:56 +00001461 SourceLocation IdLoc = ConsumeToken();
1462
1463 // Parse the '('.
1464 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001465 Diag(Tok, diag::err_expected_lparen);
Douglas Gregore8381c02008-11-05 04:29:56 +00001466 return true;
1467 }
1468 SourceLocation LParenLoc = ConsumeParen();
1469
1470 // Parse the optional expression-list.
Sebastian Redl511ed552008-11-25 22:21:31 +00001471 ExprVector ArgExprs(Actions);
Douglas Gregore8381c02008-11-05 04:29:56 +00001472 CommaLocsTy CommaLocs;
1473 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1474 SkipUntil(tok::r_paren);
1475 return true;
1476 }
1477
1478 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1479
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001480 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1481 TemplateTypeTy, IdLoc,
Sebastian Redl511ed552008-11-25 22:21:31 +00001482 LParenLoc, ArgExprs.take(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001483 ArgExprs.size(), CommaLocs.data(),
1484 RParenLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00001485}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001486
1487/// ParseExceptionSpecification - Parse a C++ exception-specification
1488/// (C++ [except.spec]).
1489///
Douglas Gregor356513d2008-12-01 18:00:20 +00001490/// exception-specification:
1491/// 'throw' '(' type-id-list [opt] ')'
1492/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00001493///
Douglas Gregor356513d2008-12-01 18:00:20 +00001494/// type-id-list:
1495/// type-id
1496/// type-id-list ',' type-id
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001497///
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001498bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redld6434562009-05-29 18:02:33 +00001499 llvm::SmallVector<TypeTy*, 2>
1500 &Exceptions,
1501 llvm::SmallVector<SourceRange, 2>
1502 &Ranges,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001503 bool &hasAnyExceptionSpec) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001504 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00001505
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001506 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001507
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001508 if (!Tok.is(tok::l_paren)) {
1509 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1510 }
1511 SourceLocation LParenLoc = ConsumeParen();
1512
Douglas Gregor356513d2008-12-01 18:00:20 +00001513 // Parse throw(...), a Microsoft extension that means "this function
1514 // can throw anything".
1515 if (Tok.is(tok::ellipsis)) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001516 hasAnyExceptionSpec = true;
Douglas Gregor356513d2008-12-01 18:00:20 +00001517 SourceLocation EllipsisLoc = ConsumeToken();
1518 if (!getLang().Microsoft)
1519 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001520 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor356513d2008-12-01 18:00:20 +00001521 return false;
1522 }
1523
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001524 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00001525 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001526 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00001527 TypeResult Res(ParseTypeName(&Range));
1528 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001529 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00001530 Ranges.push_back(Range);
1531 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001532 if (Tok.is(tok::comma))
1533 ConsumeToken();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001534 else
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001535 break;
1536 }
1537
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001538 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001539 return false;
1540}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001541
1542/// \brief We have just started parsing the definition of a new class,
1543/// so push that class onto our stack of classes that is currently
1544/// being parsed.
1545void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
Mike Stump11289f42009-09-09 15:08:12 +00001546 assert((TopLevelClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001547 "Nested class without outer class");
1548 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1549}
1550
1551/// \brief Deallocate the given parsed class and all of its nested
1552/// classes.
1553void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1554 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1555 DeallocateParsedClasses(Class->NestedClasses[I]);
1556 delete Class;
1557}
1558
1559/// \brief Pop the top class of the stack of classes that are
1560/// currently being parsed.
1561///
1562/// This routine should be called when we have finished parsing the
1563/// definition of a class, but have not yet popped the Scope
1564/// associated with the class's definition.
1565///
1566/// \returns true if the class we've popped is a top-level class,
1567/// false otherwise.
1568void Parser::PopParsingClass() {
1569 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00001570
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001571 ParsingClass *Victim = ClassStack.top();
1572 ClassStack.pop();
1573 if (Victim->TopLevelClass) {
1574 // Deallocate all of the nested classes of this class,
1575 // recursively: we don't need to keep any of this information.
1576 DeallocateParsedClasses(Victim);
1577 return;
Mike Stump11289f42009-09-09 15:08:12 +00001578 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001579 assert(!ClassStack.empty() && "Missing top-level class?");
1580
1581 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1582 Victim->NestedClasses.empty()) {
1583 // The victim is a nested class, but we will not need to perform
1584 // any processing after the definition of this class since it has
1585 // no members whose handling was delayed. Therefore, we can just
1586 // remove this nested class.
1587 delete Victim;
1588 return;
1589 }
1590
1591 // This nested class has some members that will need to be processed
1592 // after the top-level class is completely defined. Therefore, add
1593 // it to the list of nested classes within its parent.
1594 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1595 ClassStack.top()->NestedClasses.push_back(Victim);
1596 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1597}