blob: c002d48d72192e0056ee6c63536cd5faef3c3e43 [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 '}'
41///
42/// 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'.
49
50 SourceLocation IdentLoc;
51 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000052
53 Token attrTok;
Chris Lattner8f08cb72007-08-25 06:57:03 +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 }
59
60 // 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 }
Chris Lattner8f08cb72007-08-25 06:57:03 +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 }
Anders Carlssonf67606a2009-03-28 04:07:16 +000075
Chris Lattner51448322009-03-29 14:02:43 +000076 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +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 }
81
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");
93
94 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
95 ParseExternalDeclaration();
96
97 // 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,
111 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");
115
116 ConsumeToken(); // eat the '='.
117
118 CXXScopeSpec SS;
119 // Parse (optional) nested-name-specifier.
120 ParseOptionalCXXScopeSpecifier(SS);
121
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();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000132
133 // 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);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000137
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000138 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
139 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);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000160 DeclPtrTy LinkageSpec
Douglas Gregor074149e2009-01-05 19:45:36 +0000161 = Actions.ActOnStartLinkageSpecification(CurScope,
162 /*FIXME: */SourceLocation(),
163 Loc, LangBufPtr, StrSize,
164 Tok.is(tok::l_brace)? Tok.getLocation()
165 : SourceLocation());
166
167 if (Tok.isNot(tok::l_brace)) {
168 ParseDeclarationOrFunctionDefinition();
169 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
170 SourceLocation());
Douglas Gregorf44515a2008-12-16 22:23:02 +0000171 }
172
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.
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000219 ParseOptionalCXXScopeSpecifier(SS);
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 }
Chris Lattner823c44e2009-01-06 07:27:21 +0000233
234 // Parse identifier.
235 NamespcName = Tok.getIdentifierInfo();
236 IdentLoc = ConsumeToken();
237
238 // Parse (optional) attributes (most likely GNU strong-using extension).
239 if (Tok.is(tok::kw___attribute))
240 AttrList = ParseAttributes();
241
242 // 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,
262 SourceLocation &DeclEnd) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000263 CXXScopeSpec SS;
264 bool IsTypeName;
265
266 // Ignore optional 'typename'.
267 if (Tok.is(tok::kw_typename)) {
268 ConsumeToken();
269 IsTypeName = true;
270 }
271 else
272 IsTypeName = false;
273
274 // Parse nested-name-specifier.
275 ParseOptionalCXXScopeSpecifier(SS);
276
277 AttributeList *AttrList = 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000278
279 // Check nested-name specifier.
280 if (SS.isInvalid()) {
281 SkipUntil(tok::semi);
282 return DeclPtrTy();
283 }
284 if (Tok.is(tok::annot_template_id)) {
Anders Carlsson73b39cf2009-08-28 03:35:18 +0000285 // C++0x N2914 [namespace.udecl]p5:
286 // A using-declaration shall not name a template-id.
287 Diag(Tok, diag::err_using_decl_can_not_refer_to_template_spec);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000288 SkipUntil(tok::semi);
289 return DeclPtrTy();
290 }
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000291
292 IdentifierInfo *TargetName = 0;
293 OverloadedOperatorKind Op = OO_None;
294 SourceLocation IdentLoc;
295
296 if (Tok.is(tok::kw_operator)) {
297 IdentLoc = Tok.getLocation();
298
299 Op = TryParseOperatorFunctionId();
300 if (!Op) {
301 // If there was an invalid operator, skip to end of decl, and eat ';'.
302 SkipUntil(tok::semi);
303 return DeclPtrTy();
304 }
305 } else if (Tok.is(tok::identifier)) {
306 // Parse identifier.
307 TargetName = Tok.getIdentifierInfo();
308 IdentLoc = ConsumeToken();
309 } else {
310 // FIXME: Use a better diagnostic here.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000311 Diag(Tok, diag::err_expected_ident_in_using);
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000312
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000313 // If there was invalid identifier, skip to end of decl, and eat ';'.
314 SkipUntil(tok::semi);
315 return DeclPtrTy();
316 }
317
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000318 // Parse (optional) attributes (most likely GNU strong-using extension).
319 if (Tok.is(tok::kw___attribute))
320 AttrList = ParseAttributes();
321
322 // Eat ';'.
323 DeclEnd = Tok.getLocation();
324 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
325 AttrList ? "attributes list" : "namespace name", tok::semi);
326
327 return Actions.ActOnUsingDeclaration(CurScope, UsingLoc, SS,
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000328 IdentLoc, TargetName, Op,
329 AttrList, IsTypeName);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000330}
331
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000332/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
333///
334/// static_assert-declaration:
335/// static_assert ( constant-expression , string-literal ) ;
336///
Chris Lattner97144fc2009-04-02 04:16:50 +0000337Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000338 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
339 SourceLocation StaticAssertLoc = ConsumeToken();
340
341 if (Tok.isNot(tok::l_paren)) {
342 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000343 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000344 }
345
346 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000347
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000348 OwningExprResult AssertExpr(ParseConstantExpression());
349 if (AssertExpr.isInvalid()) {
350 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000351 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000352 }
353
Anders Carlssonad5f9602009-03-13 23:29:20 +0000354 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000355 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000356
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000357 if (Tok.isNot(tok::string_literal)) {
358 Diag(Tok, diag::err_expected_string_literal);
359 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000360 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000361 }
362
363 OwningExprResult AssertMessage(ParseStringLiteralExpression());
364 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000365 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000366
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000367 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000368
Chris Lattner97144fc2009-04-02 04:16:50 +0000369 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000370 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
371
Anders Carlssonad5f9602009-03-13 23:29:20 +0000372 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000373 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000374}
375
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000376/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
377///
378/// 'decltype' ( expression )
379///
380void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
381 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
382
383 SourceLocation StartLoc = ConsumeToken();
384 SourceLocation LParenLoc = Tok.getLocation();
385
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000386 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
387 "decltype")) {
388 SkipUntil(tok::r_paren);
389 return;
390 }
391
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000392 // Parse the expression
393
394 // C++0x [dcl.type.simple]p4:
395 // The operand of the decltype specifier is an unevaluated operand.
396 EnterExpressionEvaluationContext Unevaluated(Actions,
397 Action::Unevaluated);
398 OwningExprResult Result = ParseExpression();
399 if (Result.isInvalid()) {
400 SkipUntil(tok::r_paren);
401 return;
402 }
403
404 // Match the ')'
405 SourceLocation RParenLoc;
406 if (Tok.is(tok::r_paren))
407 RParenLoc = ConsumeParen();
408 else
409 MatchRHSPunctuation(tok::r_paren, LParenLoc);
410
411 if (RParenLoc.isInvalid())
412 return;
413
414 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000415 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000416 // Check for duplicate type specifiers (e.g. "int decltype(a)").
417 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000418 DiagID, Result.release()))
419 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000420}
421
Douglas Gregor42a552f2008-11-05 20:51:48 +0000422/// ParseClassName - Parse a C++ class-name, which names a class. Note
423/// that we only check that the result names a type; semantic analysis
424/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000425/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000426/// found.
427///
428/// class-name: [C++ 9.1]
429/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000430/// simple-template-id
Douglas Gregor42a552f2008-11-05 20:51:48 +0000431///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000432Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000433 const CXXScopeSpec *SS,
434 bool DestrExpected) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000435 // Check whether we have a template-id that names a type.
436 if (Tok.is(tok::annot_template_id)) {
437 TemplateIdAnnotation *TemplateId
438 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000439 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000440 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000441
442 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
443 TypeTy *Type = Tok.getAnnotationValue();
444 EndLocation = Tok.getAnnotationEndLoc();
445 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000446
447 if (Type)
448 return Type;
449 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000450 }
451
452 // Fall through to produce an error below.
453 }
454
Douglas Gregor42a552f2008-11-05 20:51:48 +0000455 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000456 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000457 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000458 }
459
460 // We have an identifier; check whether it is actually a type.
Douglas Gregorb696ea32009-02-04 17:00:24 +0000461 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor42c39f32009-08-26 18:27:52 +0000462 Tok.getLocation(), CurScope, SS,
463 true);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000464 if (!Type) {
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000465 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
466 : diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000467 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000468 }
469
470 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000471 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000472 return Type;
473}
474
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000475/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
476/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
477/// until we reach the start of a definition or see a token that
478/// cannot start a definition.
479///
480/// class-specifier: [C++ class]
481/// class-head '{' member-specification[opt] '}'
482/// class-head '{' member-specification[opt] '}' attributes[opt]
483/// class-head:
484/// class-key identifier[opt] base-clause[opt]
485/// class-key nested-name-specifier identifier base-clause[opt]
486/// class-key nested-name-specifier[opt] simple-template-id
487/// base-clause[opt]
488/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
489/// [GNU] class-key attributes[opt] nested-name-specifier
490/// identifier base-clause[opt]
491/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
492/// simple-template-id base-clause[opt]
493/// class-key:
494/// 'class'
495/// 'struct'
496/// 'union'
497///
498/// elaborated-type-specifier: [C++ dcl.type.elab]
499/// class-key ::[opt] nested-name-specifier[opt] identifier
500/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
501/// simple-template-id
502///
503/// Note that the C++ class-specifier and elaborated-type-specifier,
504/// together, subsume the C99 struct-or-union-specifier:
505///
506/// struct-or-union-specifier: [C99 6.7.2.1]
507/// struct-or-union identifier[opt] '{' struct-contents '}'
508/// struct-or-union identifier
509/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
510/// '}' attributes[opt]
511/// [GNU] struct-or-union attributes[opt] identifier
512/// struct-or-union:
513/// 'struct'
514/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000515void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
516 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000517 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000518 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000519 DeclSpec::TST TagType;
520 if (TagTokKind == tok::kw_struct)
521 TagType = DeclSpec::TST_struct;
522 else if (TagTokKind == tok::kw_class)
523 TagType = DeclSpec::TST_class;
524 else {
525 assert(TagTokKind == tok::kw_union && "Not a class specifier");
526 TagType = DeclSpec::TST_union;
527 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000528
529 AttributeList *Attr = 0;
530 // If attributes exist after tag, parse them.
531 if (Tok.is(tok::kw___attribute))
532 Attr = ParseAttributes();
533
Steve Narofff59e17e2008-12-24 20:59:21 +0000534 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000535 if (Tok.is(tok::kw___declspec))
536 Attr = ParseMicrosoftDeclSpec(Attr);
Steve Narofff59e17e2008-12-24 20:59:21 +0000537
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000538 // Parse the (optional) nested-name-specifier.
539 CXXScopeSpec SS;
Douglas Gregor495c35d2009-08-25 22:51:20 +0000540 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, true))
Douglas Gregor39a8de12009-02-25 19:37:18 +0000541 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000542 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000543
544 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000545 IdentifierInfo *Name = 0;
546 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000547 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000548 if (Tok.is(tok::identifier)) {
549 Name = Tok.getIdentifierInfo();
550 NameLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000551 } else if (Tok.is(tok::annot_template_id)) {
552 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
553 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000554
Douglas Gregorc45c2322009-03-31 00:43:58 +0000555 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000556 // The template-name in the simple-template-id refers to
557 // something other than a class template. Give an appropriate
558 // error message and skip to the ';'.
559 SourceRange Range(NameLoc);
560 if (SS.isNotEmpty())
561 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000562
Douglas Gregor39a8de12009-02-25 19:37:18 +0000563 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
564 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +0000565
Douglas Gregor39a8de12009-02-25 19:37:18 +0000566 DS.SetTypeSpecError();
567 SkipUntil(tok::semi, false, true);
568 TemplateId->Destroy();
569 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000570 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000571 }
572
John McCall67d1a672009-08-06 02:15:43 +0000573 // There are four options here. If we have 'struct foo;', then this
574 // is either a forward declaration or a friend declaration, which
575 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000576 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000577 // something like 'struct foo xyz', a reference.
John McCall0f434ec2009-07-31 02:45:11 +0000578 Action::TagUseKind TUK;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000579 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
John McCall0f434ec2009-07-31 02:45:11 +0000580 TUK = Action::TUK_Definition;
John McCall67d1a672009-08-06 02:15:43 +0000581 else if (Tok.is(tok::semi))
582 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000583 else
John McCall0f434ec2009-07-31 02:45:11 +0000584 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000585
John McCall0f434ec2009-07-31 02:45:11 +0000586 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000587 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000588 Diag(StartLoc, diag::err_anon_type_definition)
589 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000590
591 // Skip the rest of this declarator, up until the comma or semicolon.
592 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000593
594 if (TemplateId)
595 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000596 return;
597 }
598
Douglas Gregorddc29e12009-02-06 22:42:48 +0000599 // Create the tag portion of the class or class template.
Douglas Gregor212e81c2009-03-25 00:13:59 +0000600 Action::DeclResult TagOrTempResult;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000601 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
602
John McCall0f434ec2009-07-31 02:45:11 +0000603 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000604 // to turn that template-id into a type.
605
Douglas Gregor402abb52009-05-28 23:31:59 +0000606 bool Owned = false;
John McCall67d1a672009-08-06 02:15:43 +0000607 if (TemplateId && TUK != Action::TUK_Reference && TUK != Action::TUK_Friend) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000608 // Explicit specialization, class template partial specialization,
609 // or explicit instantiation.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000610 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
611 TemplateId->getTemplateArgs(),
612 TemplateId->getTemplateArgIsType(),
613 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000614 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000615 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000616 // This is an explicit instantiation of a class template.
617 TagOrTempResult
618 = Actions.ActOnExplicitInstantiation(CurScope,
619 TemplateInfo.TemplateLoc,
620 TagType,
621 StartLoc,
622 SS,
623 TemplateTy::make(TemplateId->Template),
624 TemplateId->TemplateNameLoc,
625 TemplateId->LAngleLoc,
626 TemplateArgsPtr,
627 TemplateId->getTemplateArgLocations(),
628 TemplateId->RAngleLoc,
629 Attr);
630 } else {
631 // This is an explicit specialization or a class template
632 // partial specialization.
633 TemplateParameterLists FakedParamLists;
634
635 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
636 // This looks like an explicit instantiation, because we have
637 // something like
638 //
639 // template class Foo<X>
640 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000641 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000642 // meant to be an explicit specialization, but the user forgot
643 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000644 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000645
646 SourceLocation LAngleLoc
647 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
648 Diag(TemplateId->TemplateNameLoc,
649 diag::err_explicit_instantiation_with_definition)
650 << SourceRange(TemplateInfo.TemplateLoc)
651 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
652
653 // Create a fake template parameter list that contains only
654 // "template<>", so that we treat this construct as a class
655 // template specialization.
656 FakedParamLists.push_back(
657 Actions.ActOnTemplateParameterList(0, SourceLocation(),
658 TemplateInfo.TemplateLoc,
659 LAngleLoc,
660 0, 0,
661 LAngleLoc));
662 TemplateParams = &FakedParamLists;
663 }
664
665 // Build the class template specialization.
666 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000667 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000668 StartLoc, SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000669 TemplateTy::make(TemplateId->Template),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000670 TemplateId->TemplateNameLoc,
671 TemplateId->LAngleLoc,
672 TemplateArgsPtr,
673 TemplateId->getTemplateArgLocations(),
674 TemplateId->RAngleLoc,
675 Attr,
Douglas Gregorcc636682009-02-17 23:15:12 +0000676 Action::MultiTemplateParamsArg(Actions,
677 TemplateParams? &(*TemplateParams)[0] : 0,
678 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000679 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000680 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000681 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000682 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000683 // Explicit instantiation of a member of a class template
684 // specialization, e.g.,
685 //
686 // template struct Outer<int>::Inner;
687 //
688 TagOrTempResult
689 = Actions.ActOnExplicitInstantiation(CurScope,
690 TemplateInfo.TemplateLoc,
691 TagType, StartLoc, SS, Name,
692 NameLoc, Attr);
693 } else {
694 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000695 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000696 // FIXME: Diagnose this particular error.
697 }
698
699 // Declaration or definition of a class type
John McCall0f434ec2009-07-31 02:45:11 +0000700 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000701 Name, NameLoc, Attr, AS,
702 Action::MultiTemplateParamsArg(Actions,
703 TemplateParams? &(*TemplateParams)[0] : 0,
704 TemplateParams? TemplateParams->size() : 0),
705 Owned);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000706 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000707
708 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000709 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000710 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000711
712 // If there is a body, parse it and inform the actions module.
713 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000714 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000715 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000716 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000717 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall0f434ec2009-07-31 02:45:11 +0000718 else if (TUK == Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000719 // FIXME: Complain that we have a base-specifier list but no
720 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000721 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000722 }
723
Anders Carlsson66e99772009-05-11 22:27:47 +0000724 if (TagOrTempResult.isInvalid()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000725 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000726 return;
727 }
728
John McCallfec54012009-08-03 20:12:06 +0000729 const char *PrevSpec = 0;
730 unsigned DiagID;
731 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +0000732 TagOrTempResult.get().getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +0000733 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000734}
735
736/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
737///
738/// base-clause : [C++ class.derived]
739/// ':' base-specifier-list
740/// base-specifier-list:
741/// base-specifier '...'[opt]
742/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000743void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000744 assert(Tok.is(tok::colon) && "Not a base clause");
745 ConsumeToken();
746
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000747 // Build up an array of parsed base specifiers.
748 llvm::SmallVector<BaseTy *, 8> BaseInfo;
749
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000750 while (true) {
751 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000752 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000753 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000754 // Skip the rest of this base specifier, up until the comma or
755 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000756 SkipUntil(tok::comma, tok::l_brace, true, true);
757 } else {
758 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000759 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000760 }
761
762 // If the next token is a comma, consume it and keep reading
763 // base-specifiers.
764 if (Tok.isNot(tok::comma)) break;
765
766 // Consume the comma.
767 ConsumeToken();
768 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000769
770 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000771 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000772}
773
774/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
775/// one entry in the base class list of a class specifier, for example:
776/// class foo : public bar, virtual private baz {
777/// 'public bar' and 'virtual private baz' are each base-specifiers.
778///
779/// base-specifier: [C++ class.derived]
780/// ::[opt] nested-name-specifier[opt] class-name
781/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
782/// class-name
783/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
784/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000785Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000786 bool IsVirtual = false;
787 SourceLocation StartLoc = Tok.getLocation();
788
789 // Parse the 'virtual' keyword.
790 if (Tok.is(tok::kw_virtual)) {
791 ConsumeToken();
792 IsVirtual = true;
793 }
794
795 // Parse an (optional) access specifier.
796 AccessSpecifier Access = getAccessSpecifierIfPresent();
797 if (Access)
798 ConsumeToken();
799
800 // Parse the 'virtual' keyword (again!), in case it came after the
801 // access specifier.
802 if (Tok.is(tok::kw_virtual)) {
803 SourceLocation VirtualLoc = ConsumeToken();
804 if (IsVirtual) {
805 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000806 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000807 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000808 }
809
810 IsVirtual = true;
811 }
812
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000813 // Parse optional '::' and optional nested-name-specifier.
814 CXXScopeSpec SS;
Douglas Gregor495c35d2009-08-25 22:51:20 +0000815 ParseOptionalCXXScopeSpecifier(SS, true);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000816
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000817 // The location of the base class itself.
818 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000819
820 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000821 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000822 TypeResult BaseType = ParseClassName(EndLocation, &SS);
823 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000824 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000825
826 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000827 SourceRange Range(StartLoc, EndLocation);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000828
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000829 // Notify semantic analysis that we have parsed a complete
830 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000831 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000832 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000833}
834
835/// getAccessSpecifierIfPresent - Determine whether the next token is
836/// a C++ access-specifier.
837///
838/// access-specifier: [C++ class.derived]
839/// 'private'
840/// 'protected'
841/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000842AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000843{
844 switch (Tok.getKind()) {
845 default: return AS_none;
846 case tok::kw_private: return AS_private;
847 case tok::kw_protected: return AS_protected;
848 case tok::kw_public: return AS_public;
849 }
850}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000851
Eli Friedmand33133c2009-07-22 21:45:50 +0000852void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
853 DeclPtrTy ThisDecl) {
854 // We just declared a member function. If this member function
855 // has any default arguments, we'll need to parse them later.
856 LateParsedMethodDeclaration *LateMethod = 0;
857 DeclaratorChunk::FunctionTypeInfo &FTI
858 = DeclaratorInfo.getTypeObject(0).Fun;
859 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
860 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
861 if (!LateMethod) {
862 // Push this method onto the stack of late-parsed method
863 // declarations.
864 getCurrentClass().MethodDecls.push_back(
865 LateParsedMethodDeclaration(ThisDecl));
866 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +0000867 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +0000868
869 // Add all of the parameters prior to this one (they don't
870 // have default arguments).
871 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
872 for (unsigned I = 0; I < ParamIdx; ++I)
873 LateMethod->DefaultArgs.push_back(
874 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
875 }
876
877 // Add this parameter to the list of parameters (it or may
878 // not have a default argument).
879 LateMethod->DefaultArgs.push_back(
880 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
881 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
882 }
883 }
884}
885
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000886/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
887///
888/// member-declaration:
889/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
890/// function-definition ';'[opt]
891/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
892/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000893/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000894/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000895/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000896///
897/// member-declarator-list:
898/// member-declarator
899/// member-declarator-list ',' member-declarator
900///
901/// member-declarator:
902/// declarator pure-specifier[opt]
903/// declarator constant-initializer[opt]
904/// identifier[opt] ':' constant-expression
905///
Sebastian Redle2b68332009-04-12 17:16:29 +0000906/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000907/// '= 0'
908///
909/// constant-initializer:
910/// '=' constant-expression
911///
Douglas Gregor37b372b2009-08-20 22:52:58 +0000912void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
913 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000914 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +0000915 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +0000916 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +0000917 SourceLocation DeclEnd;
918 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000919 return;
920 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000921
Chris Lattner682bf922009-03-29 16:50:03 +0000922 if (Tok.is(tok::kw_template)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +0000923 assert(!TemplateInfo.TemplateParams &&
924 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +0000925 SourceLocation DeclEnd;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000926 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
927 AS);
Chris Lattner682bf922009-03-29 16:50:03 +0000928 return;
929 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000930
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000931 // Handle: member-declaration ::= '__extension__' member-declaration
932 if (Tok.is(tok::kw___extension__)) {
933 // __extension__ silences extension warnings in the subexpression.
934 ExtensionRAIIObject O(Diags); // Use RAII to do this.
935 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +0000936 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000937 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000938
939 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +0000940 // FIXME: Check for template aliases
941
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000942 // Eat 'using'.
943 SourceLocation UsingLoc = ConsumeToken();
944
945 if (Tok.is(tok::kw_namespace)) {
946 Diag(UsingLoc, diag::err_using_namespace_in_class);
947 SkipUntil(tok::semi, true, true);
948 }
949 else {
950 SourceLocation DeclEnd;
951 // Otherwise, it must be using-declaration.
952 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd);
953 }
954 return;
955 }
956
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000957 SourceLocation DSStart = Tok.getLocation();
958 // decl-specifier-seq:
959 // Parse the common declaration-specifiers piece.
960 DeclSpec DS;
Douglas Gregor37b372b2009-08-20 22:52:58 +0000961 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000962
963 if (Tok.is(tok::semi)) {
964 ConsumeToken();
John McCall67d1a672009-08-06 02:15:43 +0000965
Douglas Gregor37b372b2009-08-20 22:52:58 +0000966 // FIXME: Friend templates?
John McCall67d1a672009-08-06 02:15:43 +0000967 if (DS.isFriendSpecified())
John McCall3f9a8a62009-08-11 06:59:38 +0000968 Actions.ActOnFriendDecl(CurScope, &DS, /*IsDefinition*/ false);
John McCall67d1a672009-08-06 02:15:43 +0000969 else
Chris Lattner682bf922009-03-29 16:50:03 +0000970 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +0000971
972 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000973 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000974
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000975 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000976
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000977 if (Tok.isNot(tok::colon)) {
978 // Parse the first declarator.
979 ParseDeclarator(DeclaratorInfo);
980 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +0000981 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000982 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000983 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000984 if (Tok.is(tok::semi))
985 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000986 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000987 }
988
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000989 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000990 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +0000991 || (DeclaratorInfo.isFunctionDeclarator() &&
992 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000993 if (!DeclaratorInfo.isFunctionDeclarator()) {
994 Diag(Tok, diag::err_func_def_no_params);
995 ConsumeBrace();
996 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000997 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000998 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000999
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001000 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1001 Diag(Tok, diag::err_function_declared_typedef);
1002 // This recovery skips the entire function body. It would be nice
1003 // to simply call ParseCXXInlineMethodDef() below, however Sema
1004 // assumes the declarator represents a function, not a typedef.
1005 ConsumeBrace();
1006 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001007 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001008 }
1009
Douglas Gregor37b372b2009-08-20 22:52:58 +00001010 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001011 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001012 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001013 }
1014
1015 // member-declarator-list:
1016 // member-declarator
1017 // member-declarator-list ',' member-declarator
1018
Chris Lattner682bf922009-03-29 16:50:03 +00001019 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001020 OwningExprResult BitfieldSize(Actions);
1021 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001022 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001023
1024 while (1) {
1025
1026 // member-declarator:
1027 // declarator pure-specifier[opt]
1028 // declarator constant-initializer[opt]
1029 // identifier[opt] ':' constant-expression
1030
1031 if (Tok.is(tok::colon)) {
1032 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001033 BitfieldSize = ParseConstantExpression();
1034 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001035 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001036 }
1037
1038 // pure-specifier:
1039 // '= 0'
1040 //
1041 // constant-initializer:
1042 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001043 //
1044 // defaulted/deleted function-definition:
1045 // '=' 'default' [TODO]
1046 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001047
1048 if (Tok.is(tok::equal)) {
1049 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001050 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1051 ConsumeToken();
1052 Deleted = true;
1053 } else {
1054 Init = ParseInitializer();
1055 if (Init.isInvalid())
1056 SkipUntil(tok::comma, true, true);
1057 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001058 }
1059
1060 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001061 if (Tok.is(tok::kw___attribute)) {
1062 SourceLocation Loc;
1063 AttributeList *AttrList = ParseAttributes(&Loc);
1064 DeclaratorInfo.AddAttributes(AttrList, Loc);
1065 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001066
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001067 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001068 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001069 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001070
1071 DeclPtrTy ThisDecl;
1072 if (DS.isFriendSpecified()) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001073 // TODO: handle initializers, bitfields, 'delete', friend templates
John McCall3f9a8a62009-08-11 06:59:38 +00001074 ThisDecl = Actions.ActOnFriendDecl(CurScope, &DeclaratorInfo,
1075 /*IsDefinition*/ false);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001076 } else {
1077 Action::MultiTemplateParamsArg TemplateParams(Actions,
1078 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1079 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
John McCall67d1a672009-08-06 02:15:43 +00001080 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1081 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001082 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001083 BitfieldSize.release(),
1084 Init.release(),
1085 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001086 }
Chris Lattner682bf922009-03-29 16:50:03 +00001087 if (ThisDecl)
1088 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001089
Douglas Gregor72b505b2008-12-16 21:30:33 +00001090 if (DeclaratorInfo.isFunctionDeclarator() &&
1091 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
1092 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001093 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001094 }
1095
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001096 // If we don't have a comma, it is either the end of the list (a ';')
1097 // or an error, bail out.
1098 if (Tok.isNot(tok::comma))
1099 break;
1100
1101 // Consume the comma.
1102 ConsumeToken();
1103
1104 // Parse the next declarator.
1105 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001106 BitfieldSize = 0;
1107 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001108 Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001109
1110 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001111 if (Tok.is(tok::kw___attribute)) {
1112 SourceLocation Loc;
1113 AttributeList *AttrList = ParseAttributes(&Loc);
1114 DeclaratorInfo.AddAttributes(AttrList, Loc);
1115 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001116
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001117 if (Tok.isNot(tok::colon))
1118 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001119 }
1120
1121 if (Tok.is(tok::semi)) {
1122 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +00001123 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +00001124 DeclsInGroup.size());
1125 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001126 }
1127
1128 Diag(Tok, diag::err_expected_semi_decl_list);
1129 // Skip to end of block or statement
1130 SkipUntil(tok::r_brace, true, true);
1131 if (Tok.is(tok::semi))
1132 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001133 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001134}
1135
1136/// ParseCXXMemberSpecification - Parse the class definition.
1137///
1138/// member-specification:
1139/// member-declaration member-specification[opt]
1140/// access-specifier ':' member-specification[opt]
1141///
1142void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001143 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001144 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001145 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001146 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001147
Chris Lattner49f28ca2009-03-05 08:00:35 +00001148 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1149 PP.getSourceManager(),
1150 "parsing struct/union/class body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001151
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001152 SourceLocation LBraceLoc = ConsumeBrace();
1153
Douglas Gregor6569d682009-05-27 23:11:45 +00001154 // Determine whether this is a top-level (non-nested) class.
1155 bool TopLevelClass = ClassStack.empty() ||
1156 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001157
1158 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001159 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001160
Douglas Gregor6569d682009-05-27 23:11:45 +00001161 // Note that we are parsing a new (potentially-nested) class definition.
1162 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1163
Douglas Gregorddc29e12009-02-06 22:42:48 +00001164 if (TagDecl)
1165 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1166 else {
1167 SkipUntil(tok::r_brace, false, false);
1168 return;
1169 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001170
1171 // C++ 11p3: Members of a class defined with the keyword class are private
1172 // by default. Members of a class defined with the keywords struct or union
1173 // are public by default.
1174 AccessSpecifier CurAS;
1175 if (TagType == DeclSpec::TST_class)
1176 CurAS = AS_private;
1177 else
1178 CurAS = AS_public;
1179
1180 // While we still have something to read, read the member-declarations.
1181 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1182 // Each iteration of this loop reads one member-declaration.
1183
1184 // Check for extraneous top-level semicolon.
1185 if (Tok.is(tok::semi)) {
1186 Diag(Tok, diag::ext_extra_struct_semi);
1187 ConsumeToken();
1188 continue;
1189 }
1190
1191 AccessSpecifier AS = getAccessSpecifierIfPresent();
1192 if (AS != AS_none) {
1193 // Current token is a C++ access specifier.
1194 CurAS = AS;
1195 ConsumeToken();
1196 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1197 continue;
1198 }
1199
Douglas Gregor37b372b2009-08-20 22:52:58 +00001200 // FIXME: Make sure we don't have a template here.
1201
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001202 // Parse all the comma separated declarators.
1203 ParseCXXClassMemberDeclaration(CurAS);
1204 }
1205
1206 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1207
1208 AttributeList *AttrList = 0;
1209 // If attributes exist after class contents, parse them.
1210 if (Tok.is(tok::kw___attribute))
1211 AttrList = ParseAttributes(); // FIXME: where should I put them?
1212
1213 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1214 LBraceLoc, RBraceLoc);
1215
1216 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1217 // complete within function bodies, default arguments,
1218 // exception-specifications, and constructor ctor-initializers (including
1219 // such things in nested classes).
1220 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001221 // FIXME: Only function bodies and constructor ctor-initializers are
1222 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001223 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001224 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001225 // are complete and we can parse the delayed portions of method
1226 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001227 ParseLexedMethodDeclarations(getCurrentClass());
1228 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001229 }
1230
1231 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001232 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001233 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001234
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001235 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001236}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001237
1238/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1239/// which explicitly initializes the members or base classes of a
1240/// class (C++ [class.base.init]). For example, the three initializers
1241/// after the ':' in the Derived constructor below:
1242///
1243/// @code
1244/// class Base { };
1245/// class Derived : Base {
1246/// int x;
1247/// float f;
1248/// public:
1249/// Derived(float f) : Base(), x(17), f(f) { }
1250/// };
1251/// @endcode
1252///
1253/// [C++] ctor-initializer:
1254/// ':' mem-initializer-list
1255///
1256/// [C++] mem-initializer-list:
1257/// mem-initializer
1258/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001259void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001260 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1261
1262 SourceLocation ColonLoc = ConsumeToken();
1263
1264 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
1265
1266 do {
1267 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001268 if (!MemInit.isInvalid())
1269 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001270
1271 if (Tok.is(tok::comma))
1272 ConsumeToken();
1273 else if (Tok.is(tok::l_brace))
1274 break;
1275 else {
1276 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001277 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001278 SkipUntil(tok::l_brace, true, true);
1279 break;
1280 }
1281 } while (true);
1282
1283 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001284 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001285}
1286
1287/// ParseMemInitializer - Parse a C++ member initializer, which is
1288/// part of a constructor initializer that explicitly initializes one
1289/// member or base class (C++ [class.base.init]). See
1290/// ParseConstructorInitializer for an example.
1291///
1292/// [C++] mem-initializer:
1293/// mem-initializer-id '(' expression-list[opt] ')'
1294///
1295/// [C++] mem-initializer-id:
1296/// '::'[opt] nested-name-specifier[opt] class-name
1297/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001298Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001299 // parse '::'[opt] nested-name-specifier[opt]
1300 CXXScopeSpec SS;
1301 ParseOptionalCXXScopeSpecifier(SS);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001302 TypeTy *TemplateTypeTy = 0;
1303 if (Tok.is(tok::annot_template_id)) {
1304 TemplateIdAnnotation *TemplateId
1305 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1306 if (TemplateId->Kind == TNK_Type_template) {
1307 AnnotateTemplateIdTokenAsType(&SS);
1308 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1309 TemplateTypeTy = Tok.getAnnotationValue();
1310 }
1311 // FIXME. May need to check for TNK_Dependent_template as well.
1312 }
1313 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001314 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001315 return true;
1316 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001317
Douglas Gregor7ad83902008-11-05 04:29:56 +00001318 // Get the identifier. This may be a member name or a class name,
1319 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001320 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001321 SourceLocation IdLoc = ConsumeToken();
1322
1323 // Parse the '('.
1324 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001325 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001326 return true;
1327 }
1328 SourceLocation LParenLoc = ConsumeParen();
1329
1330 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001331 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001332 CommaLocsTy CommaLocs;
1333 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1334 SkipUntil(tok::r_paren);
1335 return true;
1336 }
1337
1338 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1339
Fariborz Jahanian96174332009-07-01 19:21:19 +00001340 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1341 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001342 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001343 ArgExprs.size(), CommaLocs.data(),
1344 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001345}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001346
1347/// ParseExceptionSpecification - Parse a C++ exception-specification
1348/// (C++ [except.spec]).
1349///
Douglas Gregora4745612008-12-01 18:00:20 +00001350/// exception-specification:
1351/// 'throw' '(' type-id-list [opt] ')'
1352/// [MS] 'throw' '(' '...' ')'
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001353///
Douglas Gregora4745612008-12-01 18:00:20 +00001354/// type-id-list:
1355/// type-id
1356/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001357///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001358bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001359 llvm::SmallVector<TypeTy*, 2>
1360 &Exceptions,
1361 llvm::SmallVector<SourceRange, 2>
1362 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001363 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001364 assert(Tok.is(tok::kw_throw) && "expected throw");
1365
1366 SourceLocation ThrowLoc = ConsumeToken();
1367
1368 if (!Tok.is(tok::l_paren)) {
1369 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1370 }
1371 SourceLocation LParenLoc = ConsumeParen();
1372
Douglas Gregora4745612008-12-01 18:00:20 +00001373 // Parse throw(...), a Microsoft extension that means "this function
1374 // can throw anything".
1375 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001376 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001377 SourceLocation EllipsisLoc = ConsumeToken();
1378 if (!getLang().Microsoft)
1379 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001380 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001381 return false;
1382 }
1383
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001384 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001385 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001386 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001387 TypeResult Res(ParseTypeName(&Range));
1388 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001389 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001390 Ranges.push_back(Range);
1391 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001392 if (Tok.is(tok::comma))
1393 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001394 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001395 break;
1396 }
1397
Sebastian Redlab197ba2009-02-09 18:23:29 +00001398 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001399 return false;
1400}
Douglas Gregor6569d682009-05-27 23:11:45 +00001401
1402/// \brief We have just started parsing the definition of a new class,
1403/// so push that class onto our stack of classes that is currently
1404/// being parsed.
1405void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
1406 assert((TopLevelClass || !ClassStack.empty()) &&
1407 "Nested class without outer class");
1408 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1409}
1410
1411/// \brief Deallocate the given parsed class and all of its nested
1412/// classes.
1413void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1414 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1415 DeallocateParsedClasses(Class->NestedClasses[I]);
1416 delete Class;
1417}
1418
1419/// \brief Pop the top class of the stack of classes that are
1420/// currently being parsed.
1421///
1422/// This routine should be called when we have finished parsing the
1423/// definition of a class, but have not yet popped the Scope
1424/// associated with the class's definition.
1425///
1426/// \returns true if the class we've popped is a top-level class,
1427/// false otherwise.
1428void Parser::PopParsingClass() {
1429 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
1430
1431 ParsingClass *Victim = ClassStack.top();
1432 ClassStack.pop();
1433 if (Victim->TopLevelClass) {
1434 // Deallocate all of the nested classes of this class,
1435 // recursively: we don't need to keep any of this information.
1436 DeallocateParsedClasses(Victim);
1437 return;
1438 }
1439 assert(!ClassStack.empty() && "Missing top-level class?");
1440
1441 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1442 Victim->NestedClasses.empty()) {
1443 // The victim is a nested class, but we will not need to perform
1444 // any processing after the definition of this class since it has
1445 // no members whose handling was delayed. Therefore, we can just
1446 // remove this nested class.
1447 delete Victim;
1448 return;
1449 }
1450
1451 // This nested class has some members that will need to be processed
1452 // after the top-level class is completely defined. Therefore, add
1453 // it to the list of nested classes within its parent.
1454 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1455 ClassStack.top()->NestedClasses.push_back(Victim);
1456 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1457}