blob: 1b82c06bf8f86f04a299e26809134d21c4d936cc [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson0c6139d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor1b7f8982008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregore37ac4f2008-04-13 21:30:24 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000018#include "clang/Parse/Scope.h"
Chris Lattnerbc8d5642008-12-18 01:12:00 +000019#include "ExtensionRAIIObject.h"
Chris Lattner8f08cb72007-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 Stump1eb44332009-09-09 15:08:12 +000041///
Chris Lattner8f08cb72007-08-25 06:57:03 +000042/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
43/// 'namespace' identifier '=' qualified-namespace-specifier ';'
44///
Chris Lattner97144fc2009-04-02 04:16:50 +000045Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
46 SourceLocation &DeclEnd) {
Chris Lattner04d66662007-10-09 17:33:22 +000047 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000048 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump1eb44332009-09-09 15:08:12 +000049
Chris Lattner8f08cb72007-08-25 06:57:03 +000050 SourceLocation IdentLoc;
51 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000052
53 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000054
Chris Lattner04d66662007-10-09 17:33:22 +000055 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000056 Ident = Tok.getIdentifierInfo();
57 IdentLoc = ConsumeToken(); // eat the identifier.
58 }
Mike Stump1eb44332009-09-09 15:08:12 +000059
Chris Lattner8f08cb72007-08-25 06:57:03 +000060 // Read label attributes, if present.
Chris Lattnerb28317a2009-03-28 19:18:32 +000061 Action::AttrTy *AttrList = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000062 if (Tok.is(tok::kw___attribute)) {
63 attrTok = Tok;
64
Chris Lattner8f08cb72007-08-25 06:57:03 +000065 // FIXME: save these somewhere.
66 AttrList = ParseAttributes();
Douglas Gregor6a588dd2009-06-17 19:49:00 +000067 }
Mike Stump1eb44332009-09-09 15:08:12 +000068
Douglas Gregor6a588dd2009-06-17 19:49:00 +000069 if (Tok.is(tok::equal)) {
70 if (AttrList)
71 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
72
Chris Lattner97144fc2009-04-02 04:16:50 +000073 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000074 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Chris Lattner51448322009-03-29 14:02:43 +000076 if (Tok.isNot(tok::l_brace)) {
Mike Stump1eb44332009-09-09 15:08:12 +000077 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000078 diag::err_expected_ident_lbrace);
79 return DeclPtrTy();
Chris Lattner8f08cb72007-08-25 06:57:03 +000080 }
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattner51448322009-03-29 14:02:43 +000082 SourceLocation LBrace = ConsumeBrace();
83
84 // Enter a scope for the namespace.
85 ParseScope NamespaceScope(this, Scope::DeclScope);
86
87 DeclPtrTy NamespcDecl =
88 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
89
90 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
91 PP.getSourceManager(),
92 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattner51448322009-03-29 14:02:43 +000094 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
95 ParseExternalDeclaration();
Mike Stump1eb44332009-09-09 15:08:12 +000096
Chris Lattner51448322009-03-29 14:02:43 +000097 // Leave the namespace scope.
98 NamespaceScope.Exit();
99
Chris Lattner97144fc2009-04-02 04:16:50 +0000100 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
101 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000102
Chris Lattner97144fc2009-04-02 04:16:50 +0000103 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000104 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000105}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000106
Anders Carlssonf67606a2009-03-28 04:07:16 +0000107/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
108/// alias definition.
109///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000110Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000111 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000112 IdentifierInfo *Alias,
113 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000114 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Anders Carlssonf67606a2009-03-28 04:07:16 +0000116 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Anders Carlssonf67606a2009-03-28 04:07:16 +0000118 CXXScopeSpec SS;
119 // Parse (optional) nested-name-specifier.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000120 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000121
122 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
123 Diag(Tok, diag::err_expected_namespace_name);
124 // Skip to end of the definition and eat the ';'.
125 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000126 return DeclPtrTy();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000127 }
128
129 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000130 IdentifierInfo *Ident = Tok.getIdentifierInfo();
131 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Anders Carlssonf67606a2009-03-28 04:07:16 +0000133 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000134 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000135 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
136 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000137
138 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000139 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000140}
141
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000142/// ParseLinkage - We know that the current token is a string_literal
143/// and just before that, that extern was seen.
144///
145/// linkage-specification: [C++ 7.5p2: dcl.link]
146/// 'extern' string-literal '{' declaration-seq[opt] '}'
147/// 'extern' string-literal declaration
148///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000149Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000150 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000151 llvm::SmallVector<char, 8> LangBuffer;
152 // LangBuffer is guaranteed to be big enough.
153 LangBuffer.resize(Tok.getLength());
154 const char *LangBufPtr = &LangBuffer[0];
155 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
156
157 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000158
Douglas Gregor074149e2009-01-05 19:45:36 +0000159 ParseScope LinkageScope(this, Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000160 DeclPtrTy LinkageSpec
161 = Actions.ActOnStartLinkageSpecification(CurScope,
Douglas Gregor074149e2009-01-05 19:45:36 +0000162 /*FIXME: */SourceLocation(),
163 Loc, LangBufPtr, StrSize,
Mike Stump1eb44332009-09-09 15:08:12 +0000164 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000165 : SourceLocation());
166
167 if (Tok.isNot(tok::l_brace)) {
168 ParseDeclarationOrFunctionDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +0000169 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000170 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000171 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000172
173 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000174 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000175 ParseExternalDeclaration();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000176 }
177
Douglas Gregorf44515a2008-12-16 22:23:02 +0000178 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000179 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000180}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000181
Douglas Gregorf780abc2008-12-30 03:27:21 +0000182/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
183/// using-directive. Assumes that current token is 'using'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000184Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
185 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000186 assert(Tok.is(tok::kw_using) && "Not using token");
187
188 // Eat 'using'.
189 SourceLocation UsingLoc = ConsumeToken();
190
Chris Lattner2f274772009-01-06 06:55:51 +0000191 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000192 // Next token after 'using' is 'namespace' so it must be using-directive
Chris Lattner97144fc2009-04-02 04:16:50 +0000193 return ParseUsingDirective(Context, UsingLoc, DeclEnd);
Chris Lattner2f274772009-01-06 06:55:51 +0000194
195 // Otherwise, it must be using-declaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000196 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000197}
198
199/// ParseUsingDirective - Parse C++ using-directive, assumes
200/// that current token is 'namespace' and 'using' was already parsed.
201///
202/// using-directive: [C++ 7.3.p4: namespace.udir]
203/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
204/// namespace-name ;
205/// [GNU] using-directive:
206/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
207/// namespace-name attributes[opt] ;
208///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000209Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000210 SourceLocation UsingLoc,
211 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000212 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
213
214 // Eat 'namespace'.
215 SourceLocation NamespcLoc = ConsumeToken();
216
217 CXXScopeSpec SS;
218 // Parse (optional) nested-name-specifier.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000219 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000220
221 AttributeList *AttrList = 0;
222 IdentifierInfo *NamespcName = 0;
223 SourceLocation IdentLoc = SourceLocation();
224
225 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000226 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000227 Diag(Tok, diag::err_expected_namespace_name);
228 // If there was invalid namespace name, skip to end of decl, and eat ';'.
229 SkipUntil(tok::semi);
230 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattnerb28317a2009-03-28 19:18:32 +0000231 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000232 }
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Chris Lattner823c44e2009-01-06 07:27:21 +0000234 // Parse identifier.
235 NamespcName = Tok.getIdentifierInfo();
236 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Chris Lattner823c44e2009-01-06 07:27:21 +0000238 // Parse (optional) attributes (most likely GNU strong-using extension).
239 if (Tok.is(tok::kw___attribute))
240 AttrList = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattner823c44e2009-01-06 07:27:21 +0000242 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000243 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000244 ExpectAndConsume(tok::semi,
245 AttrList ? diag::err_expected_semi_after_attribute_list :
246 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000247
248 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner823c44e2009-01-06 07:27:21 +0000249 IdentLoc, NamespcName, AttrList);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000250}
251
252/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
253/// 'using' was already seen.
254///
255/// using-declaration: [C++ 7.3.p3: namespace.udecl]
256/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000257/// unqualified-id
258/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000259///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000260Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000261 SourceLocation UsingLoc,
Anders Carlsson595adc12009-08-29 19:54:19 +0000262 SourceLocation &DeclEnd,
263 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000264 CXXScopeSpec SS;
265 bool IsTypeName;
266
267 // Ignore optional 'typename'.
268 if (Tok.is(tok::kw_typename)) {
269 ConsumeToken();
270 IsTypeName = true;
271 }
272 else
273 IsTypeName = false;
274
275 // Parse nested-name-specifier.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000276 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000277
278 AttributeList *AttrList = 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000279
280 // Check nested-name specifier.
281 if (SS.isInvalid()) {
282 SkipUntil(tok::semi);
283 return DeclPtrTy();
284 }
285 if (Tok.is(tok::annot_template_id)) {
Anders Carlsson73b39cf2009-08-28 03:35:18 +0000286 // C++0x N2914 [namespace.udecl]p5:
Mike Stump1eb44332009-09-09 15:08:12 +0000287 // A using-declaration shall not name a template-id.
Anders Carlsson73b39cf2009-08-28 03:35:18 +0000288 Diag(Tok, diag::err_using_decl_can_not_refer_to_template_spec);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000289 SkipUntil(tok::semi);
290 return DeclPtrTy();
291 }
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000293 IdentifierInfo *TargetName = 0;
294 OverloadedOperatorKind Op = OO_None;
295 SourceLocation IdentLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000297 if (Tok.is(tok::kw_operator)) {
298 IdentLoc = Tok.getLocation();
299
300 Op = TryParseOperatorFunctionId();
301 if (!Op) {
302 // If there was an invalid operator, skip to end of decl, and eat ';'.
303 SkipUntil(tok::semi);
304 return DeclPtrTy();
305 }
306 } else if (Tok.is(tok::identifier)) {
307 // Parse identifier.
308 TargetName = Tok.getIdentifierInfo();
309 IdentLoc = ConsumeToken();
310 } else {
311 // FIXME: Use a better diagnostic here.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000312 Diag(Tok, diag::err_expected_ident_in_using);
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000313
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000314 // If there was invalid identifier, skip to end of decl, and eat ';'.
315 SkipUntil(tok::semi);
316 return DeclPtrTy();
317 }
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000319 // Parse (optional) attributes (most likely GNU strong-using extension).
320 if (Tok.is(tok::kw___attribute))
321 AttrList = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000323 // Eat ';'.
324 DeclEnd = Tok.getLocation();
325 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
326 AttrList ? "attributes list" : "namespace name", tok::semi);
327
Anders Carlsson595adc12009-08-29 19:54:19 +0000328 return Actions.ActOnUsingDeclaration(CurScope, AS, UsingLoc, SS,
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000329 IdentLoc, TargetName, Op,
330 AttrList, IsTypeName);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000331}
332
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000333/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
334///
335/// static_assert-declaration:
336/// static_assert ( constant-expression , string-literal ) ;
337///
Chris Lattner97144fc2009-04-02 04:16:50 +0000338Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000339 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
340 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000342 if (Tok.isNot(tok::l_paren)) {
343 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000344 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000347 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000348
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000349 OwningExprResult AssertExpr(ParseConstantExpression());
350 if (AssertExpr.isInvalid()) {
351 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000352 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000353 }
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Anders Carlssonad5f9602009-03-13 23:29:20 +0000355 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000356 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000357
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000358 if (Tok.isNot(tok::string_literal)) {
359 Diag(Tok, diag::err_expected_string_literal);
360 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000361 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000362 }
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000364 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000365 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000366 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000367
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000368 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Chris Lattner97144fc2009-04-02 04:16:50 +0000370 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000371 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
372
Mike Stump1eb44332009-09-09 15:08:12 +0000373 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000374 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000375}
376
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000377/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
378///
379/// 'decltype' ( expression )
380///
381void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
382 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
383
384 SourceLocation StartLoc = ConsumeToken();
385 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000386
387 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000388 "decltype")) {
389 SkipUntil(tok::r_paren);
390 return;
391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000393 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000395 // C++0x [dcl.type.simple]p4:
396 // The operand of the decltype specifier is an unevaluated operand.
397 EnterExpressionEvaluationContext Unevaluated(Actions,
398 Action::Unevaluated);
399 OwningExprResult Result = ParseExpression();
400 if (Result.isInvalid()) {
401 SkipUntil(tok::r_paren);
402 return;
403 }
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000405 // Match the ')'
406 SourceLocation RParenLoc;
407 if (Tok.is(tok::r_paren))
408 RParenLoc = ConsumeParen();
409 else
410 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000412 if (RParenLoc.isInvalid())
413 return;
414
415 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000416 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000417 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000418 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000419 DiagID, Result.release()))
420 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000421}
422
Douglas Gregor42a552f2008-11-05 20:51:48 +0000423/// ParseClassName - Parse a C++ class-name, which names a class. Note
424/// that we only check that the result names a type; semantic analysis
425/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000426/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000427/// found.
428///
429/// class-name: [C++ 9.1]
430/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000431/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000432///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000433Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000434 const CXXScopeSpec *SS,
435 bool DestrExpected) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000436 // Check whether we have a template-id that names a type.
437 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000438 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000439 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000440 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000441 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000442
443 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
444 TypeTy *Type = Tok.getAnnotationValue();
445 EndLocation = Tok.getAnnotationEndLoc();
446 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000447
448 if (Type)
449 return Type;
450 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000451 }
452
453 // Fall through to produce an error below.
454 }
455
Douglas Gregor42a552f2008-11-05 20:51:48 +0000456 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000457 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000458 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000459 }
460
461 // We have an identifier; check whether it is actually a type.
Mike Stump1eb44332009-09-09 15:08:12 +0000462 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor42c39f32009-08-26 18:27:52 +0000463 Tok.getLocation(), CurScope, SS,
464 true);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000465 if (!Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000466 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000467 : diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000468 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000469 }
470
471 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000472 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000473 return Type;
474}
475
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000476/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
477/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
478/// until we reach the start of a definition or see a token that
479/// cannot start a definition.
480///
481/// class-specifier: [C++ class]
482/// class-head '{' member-specification[opt] '}'
483/// class-head '{' member-specification[opt] '}' attributes[opt]
484/// class-head:
485/// class-key identifier[opt] base-clause[opt]
486/// class-key nested-name-specifier identifier base-clause[opt]
487/// class-key nested-name-specifier[opt] simple-template-id
488/// base-clause[opt]
489/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000490/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000491/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000492/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000493/// simple-template-id base-clause[opt]
494/// class-key:
495/// 'class'
496/// 'struct'
497/// 'union'
498///
499/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000500/// class-key ::[opt] nested-name-specifier[opt] identifier
501/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
502/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000503///
504/// Note that the C++ class-specifier and elaborated-type-specifier,
505/// together, subsume the C99 struct-or-union-specifier:
506///
507/// struct-or-union-specifier: [C99 6.7.2.1]
508/// struct-or-union identifier[opt] '{' struct-contents '}'
509/// struct-or-union identifier
510/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
511/// '}' attributes[opt]
512/// [GNU] struct-or-union attributes[opt] identifier
513/// struct-or-union:
514/// 'struct'
515/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000516void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
517 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000518 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000519 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000520 DeclSpec::TST TagType;
521 if (TagTokKind == tok::kw_struct)
522 TagType = DeclSpec::TST_struct;
523 else if (TagTokKind == tok::kw_class)
524 TagType = DeclSpec::TST_class;
525 else {
526 assert(TagTokKind == tok::kw_union && "Not a class specifier");
527 TagType = DeclSpec::TST_union;
528 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000529
Douglas Gregor374929f2009-09-18 15:37:17 +0000530 if (Tok.is(tok::code_completion)) {
531 // Code completion for a struct, class, or union name.
532 Actions.CodeCompleteTag(CurScope, TagType);
533 ConsumeToken();
534 }
535
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000536 AttributeList *Attr = 0;
537 // If attributes exist after tag, parse them.
538 if (Tok.is(tok::kw___attribute))
539 Attr = ParseAttributes();
540
Steve Narofff59e17e2008-12-24 20:59:21 +0000541 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000542 if (Tok.is(tok::kw___declspec))
543 Attr = ParseMicrosoftDeclSpec(Attr);
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Douglas Gregorb117a602009-09-04 05:53:02 +0000545 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
546 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
547 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000548 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000549 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
550 // properly.
551 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
552 Tok.setKind(tok::identifier);
553 }
554
555 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
556 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
557 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000558 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000559 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
560 // properly.
561 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
562 Tok.setKind(tok::identifier);
563 }
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000565 // Parse the (optional) nested-name-specifier.
566 CXXScopeSpec SS;
Mike Stump1eb44332009-09-09 15:08:12 +0000567 if (getLang().CPlusPlus &&
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000568 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
Douglas Gregor39a8de12009-02-25 19:37:18 +0000569 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000570 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000571
572 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000573 IdentifierInfo *Name = 0;
574 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000575 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000576 if (Tok.is(tok::identifier)) {
577 Name = Tok.getIdentifierInfo();
578 NameLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000579 } else if (Tok.is(tok::annot_template_id)) {
580 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
581 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000582
Douglas Gregorc45c2322009-03-31 00:43:58 +0000583 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000584 // The template-name in the simple-template-id refers to
585 // something other than a class template. Give an appropriate
586 // error message and skip to the ';'.
587 SourceRange Range(NameLoc);
588 if (SS.isNotEmpty())
589 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000590
Douglas Gregor39a8de12009-02-25 19:37:18 +0000591 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
592 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Douglas Gregor39a8de12009-02-25 19:37:18 +0000594 DS.SetTypeSpecError();
595 SkipUntil(tok::semi, false, true);
596 TemplateId->Destroy();
597 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000598 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000599 }
600
John McCall67d1a672009-08-06 02:15:43 +0000601 // There are four options here. If we have 'struct foo;', then this
602 // is either a forward declaration or a friend declaration, which
603 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000604 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000605 // something like 'struct foo xyz', a reference.
John McCall0f434ec2009-07-31 02:45:11 +0000606 Action::TagUseKind TUK;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000607 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
John McCall0f434ec2009-07-31 02:45:11 +0000608 TUK = Action::TUK_Definition;
John McCall67d1a672009-08-06 02:15:43 +0000609 else if (Tok.is(tok::semi))
610 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000611 else
John McCall0f434ec2009-07-31 02:45:11 +0000612 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000613
John McCall0f434ec2009-07-31 02:45:11 +0000614 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000615 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000616 Diag(StartLoc, diag::err_anon_type_definition)
617 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000618
619 // Skip the rest of this declarator, up until the comma or semicolon.
620 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000621
622 if (TemplateId)
623 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000624 return;
625 }
626
Douglas Gregorddc29e12009-02-06 22:42:48 +0000627 // Create the tag portion of the class or class template.
John McCallc4e70192009-09-11 04:59:25 +0000628 Action::DeclResult TagOrTempResult = true; // invalid
629 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000630 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
631
John McCall0f434ec2009-07-31 02:45:11 +0000632 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000633 // to turn that template-id into a type.
634
Douglas Gregor402abb52009-05-28 23:31:59 +0000635 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000636 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000637 // Explicit specialization, class template partial specialization,
638 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000639 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000640 TemplateId->getTemplateArgs(),
641 TemplateId->getTemplateArgIsType(),
642 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000643 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000644 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000645 // This is an explicit instantiation of a class template.
646 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000647 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000648 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000649 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000650 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000651 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000652 SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000653 TemplateTy::make(TemplateId->Template),
654 TemplateId->TemplateNameLoc,
655 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000656 TemplateArgsPtr,
657 TemplateId->getTemplateArgLocations(),
Mike Stump1eb44332009-09-09 15:08:12 +0000658 TemplateId->RAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000659 Attr);
John McCallf1bbbb42009-09-04 01:14:41 +0000660 } else if (TUK == Action::TUK_Reference || TUK == Action::TUK_Friend) {
John McCallc4e70192009-09-11 04:59:25 +0000661 TypeResult
John McCall6b2becf2009-09-08 17:47:29 +0000662 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
663 TemplateId->TemplateNameLoc,
664 TemplateId->LAngleLoc,
665 TemplateArgsPtr,
666 TemplateId->getTemplateArgLocations(),
667 TemplateId->RAngleLoc);
668
John McCallc4e70192009-09-11 04:59:25 +0000669 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
670 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000671 } else {
672 // This is an explicit specialization or a class template
673 // partial specialization.
674 TemplateParameterLists FakedParamLists;
675
676 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
677 // This looks like an explicit instantiation, because we have
678 // something like
679 //
680 // template class Foo<X>
681 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000682 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000683 // meant to be an explicit specialization, but the user forgot
684 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000685 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000686
Mike Stump1eb44332009-09-09 15:08:12 +0000687 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000688 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000689 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000690 diag::err_explicit_instantiation_with_definition)
691 << SourceRange(TemplateInfo.TemplateLoc)
692 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
693
694 // Create a fake template parameter list that contains only
695 // "template<>", so that we treat this construct as a class
696 // template specialization.
697 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000698 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000699 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000700 LAngleLoc,
701 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000702 LAngleLoc));
703 TemplateParams = &FakedParamLists;
704 }
705
706 // Build the class template specialization.
707 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000708 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000709 StartLoc, SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000710 TemplateTy::make(TemplateId->Template),
711 TemplateId->TemplateNameLoc,
712 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000713 TemplateArgsPtr,
714 TemplateId->getTemplateArgLocations(),
Mike Stump1eb44332009-09-09 15:08:12 +0000715 TemplateId->RAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000716 Attr,
Mike Stump1eb44332009-09-09 15:08:12 +0000717 Action::MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000718 TemplateParams? &(*TemplateParams)[0] : 0,
719 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000720 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000721 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000722 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000723 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000724 // Explicit instantiation of a member of a class template
725 // specialization, e.g.,
726 //
727 // template struct Outer<int>::Inner;
728 //
729 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000730 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000731 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000732 TemplateInfo.TemplateLoc,
733 TagType, StartLoc, SS, Name,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000734 NameLoc, Attr);
735 } else {
736 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000737 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000738 // FIXME: Diagnose this particular error.
739 }
740
John McCallc4e70192009-09-11 04:59:25 +0000741 bool IsDependent = false;
742
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000743 // Declaration or definition of a class type
Mike Stump1eb44332009-09-09 15:08:12 +0000744 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000745 Name, NameLoc, Attr, AS,
Mike Stump1eb44332009-09-09 15:08:12 +0000746 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000747 TemplateParams? &(*TemplateParams)[0] : 0,
748 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000749 Owned, IsDependent);
750
751 // If ActOnTag said the type was dependent, try again with the
752 // less common call.
753 if (IsDependent)
754 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
755 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000756 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000757
758 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000759 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000760 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000761
762 // If there is a body, parse it and inform the actions module.
763 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000764 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000765 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000766 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000767 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall0f434ec2009-07-31 02:45:11 +0000768 else if (TUK == Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000769 // FIXME: Complain that we have a base-specifier list but no
770 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000771 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000772 }
773
John McCallc4e70192009-09-11 04:59:25 +0000774 void *Result;
775 if (!TypeResult.isInvalid()) {
776 TagType = DeclSpec::TST_typename;
777 Result = TypeResult.get();
778 Owned = false;
779 } else if (!TagOrTempResult.isInvalid()) {
780 Result = TagOrTempResult.get().getAs<void>();
781 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000782 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000783 return;
784 }
Mike Stump1eb44332009-09-09 15:08:12 +0000785
John McCallfec54012009-08-03 20:12:06 +0000786 const char *PrevSpec = 0;
787 unsigned DiagID;
John McCallc4e70192009-09-11 04:59:25 +0000788
John McCallfec54012009-08-03 20:12:06 +0000789 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
John McCallc4e70192009-09-11 04:59:25 +0000790 Result, Owned))
John McCallfec54012009-08-03 20:12:06 +0000791 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000792}
793
Mike Stump1eb44332009-09-09 15:08:12 +0000794/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000795///
796/// base-clause : [C++ class.derived]
797/// ':' base-specifier-list
798/// base-specifier-list:
799/// base-specifier '...'[opt]
800/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000801void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000802 assert(Tok.is(tok::colon) && "Not a base clause");
803 ConsumeToken();
804
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000805 // Build up an array of parsed base specifiers.
806 llvm::SmallVector<BaseTy *, 8> BaseInfo;
807
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000808 while (true) {
809 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000810 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000811 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000812 // Skip the rest of this base specifier, up until the comma or
813 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000814 SkipUntil(tok::comma, tok::l_brace, true, true);
815 } else {
816 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000817 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000818 }
819
820 // If the next token is a comma, consume it and keep reading
821 // base-specifiers.
822 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000824 // Consume the comma.
825 ConsumeToken();
826 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000827
828 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000829 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000830}
831
832/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
833/// one entry in the base class list of a class specifier, for example:
834/// class foo : public bar, virtual private baz {
835/// 'public bar' and 'virtual private baz' are each base-specifiers.
836///
837/// base-specifier: [C++ class.derived]
838/// ::[opt] nested-name-specifier[opt] class-name
839/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
840/// class-name
841/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
842/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000843Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000844 bool IsVirtual = false;
845 SourceLocation StartLoc = Tok.getLocation();
846
847 // Parse the 'virtual' keyword.
848 if (Tok.is(tok::kw_virtual)) {
849 ConsumeToken();
850 IsVirtual = true;
851 }
852
853 // Parse an (optional) access specifier.
854 AccessSpecifier Access = getAccessSpecifierIfPresent();
855 if (Access)
856 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000858 // Parse the 'virtual' keyword (again!), in case it came after the
859 // access specifier.
860 if (Tok.is(tok::kw_virtual)) {
861 SourceLocation VirtualLoc = ConsumeToken();
862 if (IsVirtual) {
863 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000864 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000865 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000866 }
867
868 IsVirtual = true;
869 }
870
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000871 // Parse optional '::' and optional nested-name-specifier.
872 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000873 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000874
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000875 // The location of the base class itself.
876 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000877
878 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000879 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000880 TypeResult BaseType = ParseClassName(EndLocation, &SS);
881 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000882 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000883
884 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000885 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000887 // Notify semantic analysis that we have parsed a complete
888 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000889 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000890 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000891}
892
893/// getAccessSpecifierIfPresent - Determine whether the next token is
894/// a C++ access-specifier.
895///
896/// access-specifier: [C++ class.derived]
897/// 'private'
898/// 'protected'
899/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +0000900AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000901 switch (Tok.getKind()) {
902 default: return AS_none;
903 case tok::kw_private: return AS_private;
904 case tok::kw_protected: return AS_protected;
905 case tok::kw_public: return AS_public;
906 }
907}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000908
Eli Friedmand33133c2009-07-22 21:45:50 +0000909void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
910 DeclPtrTy ThisDecl) {
911 // We just declared a member function. If this member function
912 // has any default arguments, we'll need to parse them later.
913 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000914 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +0000915 = DeclaratorInfo.getTypeObject(0).Fun;
916 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
917 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
918 if (!LateMethod) {
919 // Push this method onto the stack of late-parsed method
920 // declarations.
921 getCurrentClass().MethodDecls.push_back(
922 LateParsedMethodDeclaration(ThisDecl));
923 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +0000924 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +0000925
926 // Add all of the parameters prior to this one (they don't
927 // have default arguments).
928 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
929 for (unsigned I = 0; I < ParamIdx; ++I)
930 LateMethod->DefaultArgs.push_back(
931 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
932 }
933
934 // Add this parameter to the list of parameters (it or may
935 // not have a default argument).
936 LateMethod->DefaultArgs.push_back(
937 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
938 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
939 }
940 }
941}
942
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000943/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
944///
945/// member-declaration:
946/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
947/// function-definition ';'[opt]
948/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
949/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000950/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000951/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000952/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000953///
954/// member-declarator-list:
955/// member-declarator
956/// member-declarator-list ',' member-declarator
957///
958/// member-declarator:
959/// declarator pure-specifier[opt]
960/// declarator constant-initializer[opt]
961/// identifier[opt] ':' constant-expression
962///
Sebastian Redle2b68332009-04-12 17:16:29 +0000963/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000964/// '= 0'
965///
966/// constant-initializer:
967/// '=' constant-expression
968///
Douglas Gregor37b372b2009-08-20 22:52:58 +0000969void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
970 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000971 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +0000972 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +0000973 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +0000974 SourceLocation DeclEnd;
975 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000976 return;
977 }
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Chris Lattner682bf922009-03-29 16:50:03 +0000979 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000980 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +0000981 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +0000982 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +0000983 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000984 AS);
Chris Lattner682bf922009-03-29 16:50:03 +0000985 return;
986 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000987
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000988 // Handle: member-declaration ::= '__extension__' member-declaration
989 if (Tok.is(tok::kw___extension__)) {
990 // __extension__ silences extension warnings in the subexpression.
991 ExtensionRAIIObject O(Diags); // Use RAII to do this.
992 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +0000993 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000994 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000995
996 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +0000997 // FIXME: Check for template aliases
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000999 // Eat 'using'.
1000 SourceLocation UsingLoc = ConsumeToken();
1001
1002 if (Tok.is(tok::kw_namespace)) {
1003 Diag(UsingLoc, diag::err_using_namespace_in_class);
1004 SkipUntil(tok::semi, true, true);
1005 }
1006 else {
1007 SourceLocation DeclEnd;
1008 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001009 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001010 }
1011 return;
1012 }
1013
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001014 SourceLocation DSStart = Tok.getLocation();
1015 // decl-specifier-seq:
1016 // Parse the common declaration-specifiers piece.
1017 DeclSpec DS;
Douglas Gregor37b372b2009-08-20 22:52:58 +00001018 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001019
John McCalldd4a3b02009-09-16 22:47:08 +00001020 Action::MultiTemplateParamsArg TemplateParams(Actions,
1021 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1022 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1023
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001024 if (Tok.is(tok::semi)) {
1025 ConsumeToken();
John McCall67d1a672009-08-06 02:15:43 +00001026
Anders Carlsson41111812009-09-11 17:54:14 +00001027 if (DS.isFriendSpecified()) {
John McCalldd4a3b02009-09-16 22:47:08 +00001028 Actions.ActOnFriendTypeDecl(CurScope, DS, move(TemplateParams));
Anders Carlsson41111812009-09-11 17:54:14 +00001029 } else
Chris Lattner682bf922009-03-29 16:50:03 +00001030 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +00001031
1032 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001033 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001034
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001035 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001036
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001037 if (Tok.isNot(tok::colon)) {
1038 // Parse the first declarator.
1039 ParseDeclarator(DeclaratorInfo);
1040 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001041 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001042 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001043 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001044 if (Tok.is(tok::semi))
1045 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001046 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001047 }
1048
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001049 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001050 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001051 || (DeclaratorInfo.isFunctionDeclarator() &&
1052 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001053 if (!DeclaratorInfo.isFunctionDeclarator()) {
1054 Diag(Tok, diag::err_func_def_no_params);
1055 ConsumeBrace();
1056 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001057 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001058 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001059
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001060 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1061 Diag(Tok, diag::err_function_declared_typedef);
1062 // This recovery skips the entire function body. It would be nice
1063 // to simply call ParseCXXInlineMethodDef() below, however Sema
1064 // assumes the declarator represents a function, not a typedef.
1065 ConsumeBrace();
1066 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001067 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001068 }
1069
Douglas Gregor37b372b2009-08-20 22:52:58 +00001070 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001071 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001072 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001073 }
1074
1075 // member-declarator-list:
1076 // member-declarator
1077 // member-declarator-list ',' member-declarator
1078
Chris Lattner682bf922009-03-29 16:50:03 +00001079 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001080 OwningExprResult BitfieldSize(Actions);
1081 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001082 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001083
1084 while (1) {
1085
1086 // member-declarator:
1087 // declarator pure-specifier[opt]
1088 // declarator constant-initializer[opt]
1089 // identifier[opt] ':' constant-expression
1090
1091 if (Tok.is(tok::colon)) {
1092 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001093 BitfieldSize = ParseConstantExpression();
1094 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001095 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001096 }
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001098 // pure-specifier:
1099 // '= 0'
1100 //
1101 // constant-initializer:
1102 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001103 //
1104 // defaulted/deleted function-definition:
1105 // '=' 'default' [TODO]
1106 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001107
1108 if (Tok.is(tok::equal)) {
1109 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001110 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1111 ConsumeToken();
1112 Deleted = true;
1113 } else {
1114 Init = ParseInitializer();
1115 if (Init.isInvalid())
1116 SkipUntil(tok::comma, true, true);
1117 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001118 }
1119
1120 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001121 if (Tok.is(tok::kw___attribute)) {
1122 SourceLocation Loc;
1123 AttributeList *AttrList = ParseAttributes(&Loc);
1124 DeclaratorInfo.AddAttributes(AttrList, Loc);
1125 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001126
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001127 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001128 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001129 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001130
1131 DeclPtrTy ThisDecl;
1132 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001133 // TODO: handle initializers, bitfields, 'delete'
1134 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1135 /*IsDefinition*/ false,
1136 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001137 } else {
John McCall67d1a672009-08-06 02:15:43 +00001138 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1139 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001140 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001141 BitfieldSize.release(),
1142 Init.release(),
1143 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001144 }
Chris Lattner682bf922009-03-29 16:50:03 +00001145 if (ThisDecl)
1146 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001147
Douglas Gregor72b505b2008-12-16 21:30:33 +00001148 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001149 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001150 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001151 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001152 }
1153
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001154 // If we don't have a comma, it is either the end of the list (a ';')
1155 // or an error, bail out.
1156 if (Tok.isNot(tok::comma))
1157 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001159 // Consume the comma.
1160 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001162 // Parse the next declarator.
1163 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001164 BitfieldSize = 0;
1165 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001166 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001168 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001169 if (Tok.is(tok::kw___attribute)) {
1170 SourceLocation Loc;
1171 AttributeList *AttrList = ParseAttributes(&Loc);
1172 DeclaratorInfo.AddAttributes(AttrList, Loc);
1173 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001174
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001175 if (Tok.isNot(tok::colon))
1176 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001177 }
1178
1179 if (Tok.is(tok::semi)) {
1180 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +00001181 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +00001182 DeclsInGroup.size());
1183 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001184 }
1185
1186 Diag(Tok, diag::err_expected_semi_decl_list);
1187 // Skip to end of block or statement
1188 SkipUntil(tok::r_brace, true, true);
1189 if (Tok.is(tok::semi))
1190 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001191 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001192}
1193
1194/// ParseCXXMemberSpecification - Parse the class definition.
1195///
1196/// member-specification:
1197/// member-declaration member-specification[opt]
1198/// access-specifier ':' member-specification[opt]
1199///
1200void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001201 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001202 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001203 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001204 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001205
Chris Lattner49f28ca2009-03-05 08:00:35 +00001206 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1207 PP.getSourceManager(),
1208 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001210 SourceLocation LBraceLoc = ConsumeBrace();
1211
Douglas Gregor6569d682009-05-27 23:11:45 +00001212 // Determine whether this is a top-level (non-nested) class.
Mike Stump1eb44332009-09-09 15:08:12 +00001213 bool TopLevelClass = ClassStack.empty() ||
Douglas Gregor6569d682009-05-27 23:11:45 +00001214 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001215
1216 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001217 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001218
Douglas Gregor6569d682009-05-27 23:11:45 +00001219 // Note that we are parsing a new (potentially-nested) class definition.
1220 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1221
Douglas Gregorddc29e12009-02-06 22:42:48 +00001222 if (TagDecl)
1223 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1224 else {
1225 SkipUntil(tok::r_brace, false, false);
1226 return;
1227 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001228
1229 // C++ 11p3: Members of a class defined with the keyword class are private
1230 // by default. Members of a class defined with the keywords struct or union
1231 // are public by default.
1232 AccessSpecifier CurAS;
1233 if (TagType == DeclSpec::TST_class)
1234 CurAS = AS_private;
1235 else
1236 CurAS = AS_public;
1237
1238 // While we still have something to read, read the member-declarations.
1239 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1240 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001242 // Check for extraneous top-level semicolon.
1243 if (Tok.is(tok::semi)) {
1244 Diag(Tok, diag::ext_extra_struct_semi);
1245 ConsumeToken();
1246 continue;
1247 }
1248
1249 AccessSpecifier AS = getAccessSpecifierIfPresent();
1250 if (AS != AS_none) {
1251 // Current token is a C++ access specifier.
1252 CurAS = AS;
1253 ConsumeToken();
1254 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1255 continue;
1256 }
1257
Douglas Gregor37b372b2009-08-20 22:52:58 +00001258 // FIXME: Make sure we don't have a template here.
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001260 // Parse all the comma separated declarators.
1261 ParseCXXClassMemberDeclaration(CurAS);
1262 }
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001264 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001266 AttributeList *AttrList = 0;
1267 // If attributes exist after class contents, parse them.
1268 if (Tok.is(tok::kw___attribute))
1269 AttrList = ParseAttributes(); // FIXME: where should I put them?
1270
1271 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1272 LBraceLoc, RBraceLoc);
1273
1274 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1275 // complete within function bodies, default arguments,
1276 // exception-specifications, and constructor ctor-initializers (including
1277 // such things in nested classes).
1278 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001279 // FIXME: Only function bodies and constructor ctor-initializers are
1280 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001281 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001282 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001283 // are complete and we can parse the delayed portions of method
1284 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001285 ParseLexedMethodDeclarations(getCurrentClass());
1286 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001287 }
1288
1289 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001290 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001291 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001292
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001293 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001294}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001295
1296/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1297/// which explicitly initializes the members or base classes of a
1298/// class (C++ [class.base.init]). For example, the three initializers
1299/// after the ':' in the Derived constructor below:
1300///
1301/// @code
1302/// class Base { };
1303/// class Derived : Base {
1304/// int x;
1305/// float f;
1306/// public:
1307/// Derived(float f) : Base(), x(17), f(f) { }
1308/// };
1309/// @endcode
1310///
Mike Stump1eb44332009-09-09 15:08:12 +00001311/// [C++] ctor-initializer:
1312/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001313///
Mike Stump1eb44332009-09-09 15:08:12 +00001314/// [C++] mem-initializer-list:
1315/// mem-initializer
1316/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001317void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001318 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1319
1320 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Douglas Gregor7ad83902008-11-05 04:29:56 +00001322 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Douglas Gregor7ad83902008-11-05 04:29:56 +00001324 do {
1325 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001326 if (!MemInit.isInvalid())
1327 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001328
1329 if (Tok.is(tok::comma))
1330 ConsumeToken();
1331 else if (Tok.is(tok::l_brace))
1332 break;
1333 else {
1334 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001335 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001336 SkipUntil(tok::l_brace, true, true);
1337 break;
1338 }
1339 } while (true);
1340
Mike Stump1eb44332009-09-09 15:08:12 +00001341 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001342 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001343}
1344
1345/// ParseMemInitializer - Parse a C++ member initializer, which is
1346/// part of a constructor initializer that explicitly initializes one
1347/// member or base class (C++ [class.base.init]). See
1348/// ParseConstructorInitializer for an example.
1349///
1350/// [C++] mem-initializer:
1351/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001352///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001353/// [C++] mem-initializer-id:
1354/// '::'[opt] nested-name-specifier[opt] class-name
1355/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001356Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001357 // parse '::'[opt] nested-name-specifier[opt]
1358 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001359 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001360 TypeTy *TemplateTypeTy = 0;
1361 if (Tok.is(tok::annot_template_id)) {
1362 TemplateIdAnnotation *TemplateId
1363 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1364 if (TemplateId->Kind == TNK_Type_template) {
1365 AnnotateTemplateIdTokenAsType(&SS);
1366 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1367 TemplateTypeTy = Tok.getAnnotationValue();
1368 }
1369 // FIXME. May need to check for TNK_Dependent_template as well.
1370 }
1371 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001372 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001373 return true;
1374 }
Mike Stump1eb44332009-09-09 15:08:12 +00001375
Douglas Gregor7ad83902008-11-05 04:29:56 +00001376 // Get the identifier. This may be a member name or a class name,
1377 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001378 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001379 SourceLocation IdLoc = ConsumeToken();
1380
1381 // Parse the '('.
1382 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001383 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001384 return true;
1385 }
1386 SourceLocation LParenLoc = ConsumeParen();
1387
1388 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001389 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001390 CommaLocsTy CommaLocs;
1391 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1392 SkipUntil(tok::r_paren);
1393 return true;
1394 }
1395
1396 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1397
Fariborz Jahanian96174332009-07-01 19:21:19 +00001398 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1399 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001400 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001401 ArgExprs.size(), CommaLocs.data(),
1402 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001403}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001404
1405/// ParseExceptionSpecification - Parse a C++ exception-specification
1406/// (C++ [except.spec]).
1407///
Douglas Gregora4745612008-12-01 18:00:20 +00001408/// exception-specification:
1409/// 'throw' '(' type-id-list [opt] ')'
1410/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001411///
Douglas Gregora4745612008-12-01 18:00:20 +00001412/// type-id-list:
1413/// type-id
1414/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001415///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001416bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001417 llvm::SmallVector<TypeTy*, 2>
1418 &Exceptions,
1419 llvm::SmallVector<SourceRange, 2>
1420 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001421 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001422 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001424 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001426 if (!Tok.is(tok::l_paren)) {
1427 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1428 }
1429 SourceLocation LParenLoc = ConsumeParen();
1430
Douglas Gregora4745612008-12-01 18:00:20 +00001431 // Parse throw(...), a Microsoft extension that means "this function
1432 // can throw anything".
1433 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001434 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001435 SourceLocation EllipsisLoc = ConsumeToken();
1436 if (!getLang().Microsoft)
1437 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001438 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001439 return false;
1440 }
1441
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001442 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001443 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001444 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001445 TypeResult Res(ParseTypeName(&Range));
1446 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001447 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001448 Ranges.push_back(Range);
1449 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001450 if (Tok.is(tok::comma))
1451 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001452 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001453 break;
1454 }
1455
Sebastian Redlab197ba2009-02-09 18:23:29 +00001456 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001457 return false;
1458}
Douglas Gregor6569d682009-05-27 23:11:45 +00001459
1460/// \brief We have just started parsing the definition of a new class,
1461/// so push that class onto our stack of classes that is currently
1462/// being parsed.
1463void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001464 assert((TopLevelClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001465 "Nested class without outer class");
1466 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1467}
1468
1469/// \brief Deallocate the given parsed class and all of its nested
1470/// classes.
1471void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1472 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1473 DeallocateParsedClasses(Class->NestedClasses[I]);
1474 delete Class;
1475}
1476
1477/// \brief Pop the top class of the stack of classes that are
1478/// currently being parsed.
1479///
1480/// This routine should be called when we have finished parsing the
1481/// definition of a class, but have not yet popped the Scope
1482/// associated with the class's definition.
1483///
1484/// \returns true if the class we've popped is a top-level class,
1485/// false otherwise.
1486void Parser::PopParsingClass() {
1487 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Douglas Gregor6569d682009-05-27 23:11:45 +00001489 ParsingClass *Victim = ClassStack.top();
1490 ClassStack.pop();
1491 if (Victim->TopLevelClass) {
1492 // Deallocate all of the nested classes of this class,
1493 // recursively: we don't need to keep any of this information.
1494 DeallocateParsedClasses(Victim);
1495 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001496 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001497 assert(!ClassStack.empty() && "Missing top-level class?");
1498
1499 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1500 Victim->NestedClasses.empty()) {
1501 // The victim is a nested class, but we will not need to perform
1502 // any processing after the definition of this class since it has
1503 // no members whose handling was delayed. Therefore, we can just
1504 // remove this nested class.
1505 delete Victim;
1506 return;
1507 }
1508
1509 // This nested class has some members that will need to be processed
1510 // after the top-level class is completely defined. Therefore, add
1511 // it to the list of nested classes within its parent.
1512 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1513 ClassStack.top()->NestedClasses.push_back(Victim);
1514 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1515}