blob: 59359530e3963f0037b13598b745e52ac1608462 [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.
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();
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.
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 }
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,
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:
287 // A using-declaration shall not name a template-id.
288 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 }
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000292
293 IdentifierInfo *TargetName = 0;
294 OverloadedOperatorKind Op = OO_None;
295 SourceLocation IdentLoc;
296
297 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 }
318
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();
322
323 // 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();
341
342 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 }
346
347 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 }
354
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 }
363
364 OwningExprResult AssertMessage(ParseStringLiteralExpression());
365 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);
Anders Carlsson511d7ab2009-03-11 16:27:10 +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
Anders Carlssonad5f9602009-03-13 23:29:20 +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();
386
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000387 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
388 "decltype")) {
389 SkipUntil(tok::r_paren);
390 return;
391 }
392
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000393 // Parse the expression
394
395 // 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 }
404
405 // Match the ')'
406 SourceLocation RParenLoc;
407 if (Tok.is(tok::r_paren))
408 RParenLoc = ConsumeParen();
409 else
410 MatchRHSPunctuation(tok::r_paren, LParenLoc);
411
412 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)").
418 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
Douglas Gregor42a552f2008-11-05 20:51:48 +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)) {
438 TemplateIdAnnotation *TemplateId
439 = 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.
Douglas Gregorb696ea32009-02-04 17:00:24 +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) {
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000466 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
467 : 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]
490/// [GNU] class-key attributes[opt] nested-name-specifier
491/// identifier base-clause[opt]
492/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
493/// simple-template-id base-clause[opt]
494/// class-key:
495/// 'class'
496/// 'struct'
497/// 'union'
498///
499/// elaborated-type-specifier: [C++ dcl.type.elab]
500/// class-key ::[opt] nested-name-specifier[opt] identifier
501/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
502/// simple-template-id
503///
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
530 AttributeList *Attr = 0;
531 // If attributes exist after tag, parse them.
532 if (Tok.is(tok::kw___attribute))
533 Attr = ParseAttributes();
534
Steve Narofff59e17e2008-12-24 20:59:21 +0000535 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000536 if (Tok.is(tok::kw___declspec))
537 Attr = ParseMicrosoftDeclSpec(Attr);
Steve Narofff59e17e2008-12-24 20:59:21 +0000538
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000539 // Parse the (optional) nested-name-specifier.
540 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000541 if (getLang().CPlusPlus &&
542 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
Douglas Gregor39a8de12009-02-25 19:37:18 +0000543 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000544 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000545
546 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000547 IdentifierInfo *Name = 0;
548 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000549 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000550 if (Tok.is(tok::identifier)) {
551 Name = Tok.getIdentifierInfo();
552 NameLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000553 } else if (Tok.is(tok::annot_template_id)) {
554 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
555 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000556
Douglas Gregorc45c2322009-03-31 00:43:58 +0000557 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000558 // The template-name in the simple-template-id refers to
559 // something other than a class template. Give an appropriate
560 // error message and skip to the ';'.
561 SourceRange Range(NameLoc);
562 if (SS.isNotEmpty())
563 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000564
Douglas Gregor39a8de12009-02-25 19:37:18 +0000565 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
566 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +0000567
Douglas Gregor39a8de12009-02-25 19:37:18 +0000568 DS.SetTypeSpecError();
569 SkipUntil(tok::semi, false, true);
570 TemplateId->Destroy();
571 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000572 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000573 }
574
John McCall67d1a672009-08-06 02:15:43 +0000575 // There are four options here. If we have 'struct foo;', then this
576 // is either a forward declaration or a friend declaration, which
577 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000578 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000579 // something like 'struct foo xyz', a reference.
John McCall0f434ec2009-07-31 02:45:11 +0000580 Action::TagUseKind TUK;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000581 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
John McCall0f434ec2009-07-31 02:45:11 +0000582 TUK = Action::TUK_Definition;
John McCall67d1a672009-08-06 02:15:43 +0000583 else if (Tok.is(tok::semi))
584 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000585 else
John McCall0f434ec2009-07-31 02:45:11 +0000586 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000587
John McCall0f434ec2009-07-31 02:45:11 +0000588 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000589 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000590 Diag(StartLoc, diag::err_anon_type_definition)
591 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000592
593 // Skip the rest of this declarator, up until the comma or semicolon.
594 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000595
596 if (TemplateId)
597 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000598 return;
599 }
600
Douglas Gregorddc29e12009-02-06 22:42:48 +0000601 // Create the tag portion of the class or class template.
Douglas Gregor212e81c2009-03-25 00:13:59 +0000602 Action::DeclResult TagOrTempResult;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000603 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
604
John McCall0f434ec2009-07-31 02:45:11 +0000605 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000606 // to turn that template-id into a type.
607
Douglas Gregor402abb52009-05-28 23:31:59 +0000608 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000609 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000610 // Explicit specialization, class template partial specialization,
611 // or explicit instantiation.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000612 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
613 TemplateId->getTemplateArgs(),
614 TemplateId->getTemplateArgIsType(),
615 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000616 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000617 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000618 // This is an explicit instantiation of a class template.
619 TagOrTempResult
620 = Actions.ActOnExplicitInstantiation(CurScope,
621 TemplateInfo.TemplateLoc,
622 TagType,
623 StartLoc,
624 SS,
625 TemplateTy::make(TemplateId->Template),
626 TemplateId->TemplateNameLoc,
627 TemplateId->LAngleLoc,
628 TemplateArgsPtr,
629 TemplateId->getTemplateArgLocations(),
630 TemplateId->RAngleLoc,
631 Attr);
John McCallf1bbbb42009-09-04 01:14:41 +0000632 } else if (TUK == Action::TUK_Reference || TUK == Action::TUK_Friend) {
633 Action::TypeResult TypeResult =
634 Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
635 TemplateId->TemplateNameLoc,
636 TemplateId->LAngleLoc,
637 TemplateArgsPtr,
638 TemplateId->getTemplateArgLocations(),
639 TemplateId->RAngleLoc,
640 TagType, StartLoc);
641
642 TemplateId->Destroy();
643
644 if (TypeResult.isInvalid()) {
645 DS.SetTypeSpecError();
646 return;
647 }
648
649 const char *PrevSpec = 0;
650 unsigned DiagID;
651 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, PrevSpec,
652 DiagID, TypeResult.get()))
653 Diag(StartLoc, DiagID) << PrevSpec;
654
655 return;
656
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000657 } else {
658 // This is an explicit specialization or a class template
659 // partial specialization.
660 TemplateParameterLists FakedParamLists;
661
662 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
663 // This looks like an explicit instantiation, because we have
664 // something like
665 //
666 // template class Foo<X>
667 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000668 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000669 // meant to be an explicit specialization, but the user forgot
670 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000671 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000672
673 SourceLocation LAngleLoc
674 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
675 Diag(TemplateId->TemplateNameLoc,
676 diag::err_explicit_instantiation_with_definition)
677 << SourceRange(TemplateInfo.TemplateLoc)
678 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
679
680 // Create a fake template parameter list that contains only
681 // "template<>", so that we treat this construct as a class
682 // template specialization.
683 FakedParamLists.push_back(
684 Actions.ActOnTemplateParameterList(0, SourceLocation(),
685 TemplateInfo.TemplateLoc,
686 LAngleLoc,
687 0, 0,
688 LAngleLoc));
689 TemplateParams = &FakedParamLists;
690 }
691
692 // Build the class template specialization.
693 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000694 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000695 StartLoc, SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000696 TemplateTy::make(TemplateId->Template),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000697 TemplateId->TemplateNameLoc,
698 TemplateId->LAngleLoc,
699 TemplateArgsPtr,
700 TemplateId->getTemplateArgLocations(),
701 TemplateId->RAngleLoc,
702 Attr,
Douglas Gregorcc636682009-02-17 23:15:12 +0000703 Action::MultiTemplateParamsArg(Actions,
704 TemplateParams? &(*TemplateParams)[0] : 0,
705 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000706 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000707 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000708 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000709 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000710 // Explicit instantiation of a member of a class template
711 // specialization, e.g.,
712 //
713 // template struct Outer<int>::Inner;
714 //
715 TagOrTempResult
716 = Actions.ActOnExplicitInstantiation(CurScope,
717 TemplateInfo.TemplateLoc,
718 TagType, StartLoc, SS, Name,
719 NameLoc, Attr);
720 } else {
721 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000722 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000723 // FIXME: Diagnose this particular error.
724 }
725
726 // Declaration or definition of a class type
John McCall0f434ec2009-07-31 02:45:11 +0000727 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000728 Name, NameLoc, Attr, AS,
729 Action::MultiTemplateParamsArg(Actions,
730 TemplateParams? &(*TemplateParams)[0] : 0,
731 TemplateParams? TemplateParams->size() : 0),
732 Owned);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000733 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000734
735 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000736 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000737 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000738
739 // If there is a body, parse it and inform the actions module.
740 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000741 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000742 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000743 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000744 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall0f434ec2009-07-31 02:45:11 +0000745 else if (TUK == Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000746 // FIXME: Complain that we have a base-specifier list but no
747 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000748 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000749 }
750
Anders Carlsson66e99772009-05-11 22:27:47 +0000751 if (TagOrTempResult.isInvalid()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000752 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000753 return;
754 }
755
John McCallfec54012009-08-03 20:12:06 +0000756 const char *PrevSpec = 0;
757 unsigned DiagID;
758 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +0000759 TagOrTempResult.get().getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +0000760 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000761}
762
763/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
764///
765/// base-clause : [C++ class.derived]
766/// ':' base-specifier-list
767/// base-specifier-list:
768/// base-specifier '...'[opt]
769/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000770void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000771 assert(Tok.is(tok::colon) && "Not a base clause");
772 ConsumeToken();
773
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000774 // Build up an array of parsed base specifiers.
775 llvm::SmallVector<BaseTy *, 8> BaseInfo;
776
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000777 while (true) {
778 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000779 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000780 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000781 // Skip the rest of this base specifier, up until the comma or
782 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000783 SkipUntil(tok::comma, tok::l_brace, true, true);
784 } else {
785 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000786 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000787 }
788
789 // If the next token is a comma, consume it and keep reading
790 // base-specifiers.
791 if (Tok.isNot(tok::comma)) break;
792
793 // Consume the comma.
794 ConsumeToken();
795 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000796
797 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000798 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000799}
800
801/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
802/// one entry in the base class list of a class specifier, for example:
803/// class foo : public bar, virtual private baz {
804/// 'public bar' and 'virtual private baz' are each base-specifiers.
805///
806/// base-specifier: [C++ class.derived]
807/// ::[opt] nested-name-specifier[opt] class-name
808/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
809/// class-name
810/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
811/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000812Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000813 bool IsVirtual = false;
814 SourceLocation StartLoc = Tok.getLocation();
815
816 // Parse the 'virtual' keyword.
817 if (Tok.is(tok::kw_virtual)) {
818 ConsumeToken();
819 IsVirtual = true;
820 }
821
822 // Parse an (optional) access specifier.
823 AccessSpecifier Access = getAccessSpecifierIfPresent();
824 if (Access)
825 ConsumeToken();
826
827 // Parse the 'virtual' keyword (again!), in case it came after the
828 // access specifier.
829 if (Tok.is(tok::kw_virtual)) {
830 SourceLocation VirtualLoc = ConsumeToken();
831 if (IsVirtual) {
832 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000833 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000834 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000835 }
836
837 IsVirtual = true;
838 }
839
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000840 // Parse optional '::' and optional nested-name-specifier.
841 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000842 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000843
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000844 // The location of the base class itself.
845 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000846
847 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000848 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000849 TypeResult BaseType = ParseClassName(EndLocation, &SS);
850 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000851 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000852
853 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000854 SourceRange Range(StartLoc, EndLocation);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000855
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000856 // Notify semantic analysis that we have parsed a complete
857 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000858 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000859 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000860}
861
862/// getAccessSpecifierIfPresent - Determine whether the next token is
863/// a C++ access-specifier.
864///
865/// access-specifier: [C++ class.derived]
866/// 'private'
867/// 'protected'
868/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000869AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000870{
871 switch (Tok.getKind()) {
872 default: return AS_none;
873 case tok::kw_private: return AS_private;
874 case tok::kw_protected: return AS_protected;
875 case tok::kw_public: return AS_public;
876 }
877}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000878
Eli Friedmand33133c2009-07-22 21:45:50 +0000879void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
880 DeclPtrTy ThisDecl) {
881 // We just declared a member function. If this member function
882 // has any default arguments, we'll need to parse them later.
883 LateParsedMethodDeclaration *LateMethod = 0;
884 DeclaratorChunk::FunctionTypeInfo &FTI
885 = DeclaratorInfo.getTypeObject(0).Fun;
886 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
887 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
888 if (!LateMethod) {
889 // Push this method onto the stack of late-parsed method
890 // declarations.
891 getCurrentClass().MethodDecls.push_back(
892 LateParsedMethodDeclaration(ThisDecl));
893 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +0000894 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +0000895
896 // Add all of the parameters prior to this one (they don't
897 // have default arguments).
898 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
899 for (unsigned I = 0; I < ParamIdx; ++I)
900 LateMethod->DefaultArgs.push_back(
901 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
902 }
903
904 // Add this parameter to the list of parameters (it or may
905 // not have a default argument).
906 LateMethod->DefaultArgs.push_back(
907 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
908 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
909 }
910 }
911}
912
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000913/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
914///
915/// member-declaration:
916/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
917/// function-definition ';'[opt]
918/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
919/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000920/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000921/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000922/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000923///
924/// member-declarator-list:
925/// member-declarator
926/// member-declarator-list ',' member-declarator
927///
928/// member-declarator:
929/// declarator pure-specifier[opt]
930/// declarator constant-initializer[opt]
931/// identifier[opt] ':' constant-expression
932///
Sebastian Redle2b68332009-04-12 17:16:29 +0000933/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000934/// '= 0'
935///
936/// constant-initializer:
937/// '=' constant-expression
938///
Douglas Gregor37b372b2009-08-20 22:52:58 +0000939void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
940 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000941 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +0000942 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +0000943 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +0000944 SourceLocation DeclEnd;
945 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000946 return;
947 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000948
Chris Lattner682bf922009-03-29 16:50:03 +0000949 if (Tok.is(tok::kw_template)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +0000950 assert(!TemplateInfo.TemplateParams &&
951 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +0000952 SourceLocation DeclEnd;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000953 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
954 AS);
Chris Lattner682bf922009-03-29 16:50:03 +0000955 return;
956 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000957
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000958 // Handle: member-declaration ::= '__extension__' member-declaration
959 if (Tok.is(tok::kw___extension__)) {
960 // __extension__ silences extension warnings in the subexpression.
961 ExtensionRAIIObject O(Diags); // Use RAII to do this.
962 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +0000963 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000964 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000965
966 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +0000967 // FIXME: Check for template aliases
968
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000969 // Eat 'using'.
970 SourceLocation UsingLoc = ConsumeToken();
971
972 if (Tok.is(tok::kw_namespace)) {
973 Diag(UsingLoc, diag::err_using_namespace_in_class);
974 SkipUntil(tok::semi, true, true);
975 }
976 else {
977 SourceLocation DeclEnd;
978 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +0000979 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000980 }
981 return;
982 }
983
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000984 SourceLocation DSStart = Tok.getLocation();
985 // decl-specifier-seq:
986 // Parse the common declaration-specifiers piece.
987 DeclSpec DS;
Douglas Gregor37b372b2009-08-20 22:52:58 +0000988 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000989
990 if (Tok.is(tok::semi)) {
991 ConsumeToken();
John McCall67d1a672009-08-06 02:15:43 +0000992
Douglas Gregor37b372b2009-08-20 22:52:58 +0000993 // FIXME: Friend templates?
John McCall67d1a672009-08-06 02:15:43 +0000994 if (DS.isFriendSpecified())
John McCall3f9a8a62009-08-11 06:59:38 +0000995 Actions.ActOnFriendDecl(CurScope, &DS, /*IsDefinition*/ false);
John McCall67d1a672009-08-06 02:15:43 +0000996 else
Chris Lattner682bf922009-03-29 16:50:03 +0000997 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +0000998
999 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001000 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001001
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001002 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001003
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001004 if (Tok.isNot(tok::colon)) {
1005 // Parse the first declarator.
1006 ParseDeclarator(DeclaratorInfo);
1007 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001008 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001009 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001010 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001011 if (Tok.is(tok::semi))
1012 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001013 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001014 }
1015
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001016 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001017 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001018 || (DeclaratorInfo.isFunctionDeclarator() &&
1019 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001020 if (!DeclaratorInfo.isFunctionDeclarator()) {
1021 Diag(Tok, diag::err_func_def_no_params);
1022 ConsumeBrace();
1023 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001024 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001025 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001026
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001027 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1028 Diag(Tok, diag::err_function_declared_typedef);
1029 // This recovery skips the entire function body. It would be nice
1030 // to simply call ParseCXXInlineMethodDef() below, however Sema
1031 // assumes the declarator represents a function, not a typedef.
1032 ConsumeBrace();
1033 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001034 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001035 }
1036
Douglas Gregor37b372b2009-08-20 22:52:58 +00001037 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001038 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001039 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001040 }
1041
1042 // member-declarator-list:
1043 // member-declarator
1044 // member-declarator-list ',' member-declarator
1045
Chris Lattner682bf922009-03-29 16:50:03 +00001046 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001047 OwningExprResult BitfieldSize(Actions);
1048 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001049 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001050
1051 while (1) {
1052
1053 // member-declarator:
1054 // declarator pure-specifier[opt]
1055 // declarator constant-initializer[opt]
1056 // identifier[opt] ':' constant-expression
1057
1058 if (Tok.is(tok::colon)) {
1059 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001060 BitfieldSize = ParseConstantExpression();
1061 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001062 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001063 }
1064
1065 // pure-specifier:
1066 // '= 0'
1067 //
1068 // constant-initializer:
1069 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001070 //
1071 // defaulted/deleted function-definition:
1072 // '=' 'default' [TODO]
1073 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001074
1075 if (Tok.is(tok::equal)) {
1076 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001077 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1078 ConsumeToken();
1079 Deleted = true;
1080 } else {
1081 Init = ParseInitializer();
1082 if (Init.isInvalid())
1083 SkipUntil(tok::comma, true, true);
1084 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001085 }
1086
1087 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001088 if (Tok.is(tok::kw___attribute)) {
1089 SourceLocation Loc;
1090 AttributeList *AttrList = ParseAttributes(&Loc);
1091 DeclaratorInfo.AddAttributes(AttrList, Loc);
1092 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001093
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001094 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001095 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001096 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001097
1098 DeclPtrTy ThisDecl;
1099 if (DS.isFriendSpecified()) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001100 // TODO: handle initializers, bitfields, 'delete', friend templates
John McCall3f9a8a62009-08-11 06:59:38 +00001101 ThisDecl = Actions.ActOnFriendDecl(CurScope, &DeclaratorInfo,
1102 /*IsDefinition*/ false);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001103 } else {
1104 Action::MultiTemplateParamsArg TemplateParams(Actions,
1105 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1106 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
John McCall67d1a672009-08-06 02:15:43 +00001107 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1108 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001109 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001110 BitfieldSize.release(),
1111 Init.release(),
1112 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001113 }
Chris Lattner682bf922009-03-29 16:50:03 +00001114 if (ThisDecl)
1115 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001116
Douglas Gregor72b505b2008-12-16 21:30:33 +00001117 if (DeclaratorInfo.isFunctionDeclarator() &&
1118 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
1119 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001120 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001121 }
1122
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001123 // If we don't have a comma, it is either the end of the list (a ';')
1124 // or an error, bail out.
1125 if (Tok.isNot(tok::comma))
1126 break;
1127
1128 // Consume the comma.
1129 ConsumeToken();
1130
1131 // Parse the next declarator.
1132 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001133 BitfieldSize = 0;
1134 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001135 Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001136
1137 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001138 if (Tok.is(tok::kw___attribute)) {
1139 SourceLocation Loc;
1140 AttributeList *AttrList = ParseAttributes(&Loc);
1141 DeclaratorInfo.AddAttributes(AttrList, Loc);
1142 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001143
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001144 if (Tok.isNot(tok::colon))
1145 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001146 }
1147
1148 if (Tok.is(tok::semi)) {
1149 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +00001150 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +00001151 DeclsInGroup.size());
1152 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001153 }
1154
1155 Diag(Tok, diag::err_expected_semi_decl_list);
1156 // Skip to end of block or statement
1157 SkipUntil(tok::r_brace, true, true);
1158 if (Tok.is(tok::semi))
1159 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001160 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001161}
1162
1163/// ParseCXXMemberSpecification - Parse the class definition.
1164///
1165/// member-specification:
1166/// member-declaration member-specification[opt]
1167/// access-specifier ':' member-specification[opt]
1168///
1169void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001170 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001171 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001172 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001173 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001174
Chris Lattner49f28ca2009-03-05 08:00:35 +00001175 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1176 PP.getSourceManager(),
1177 "parsing struct/union/class body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001178
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001179 SourceLocation LBraceLoc = ConsumeBrace();
1180
Douglas Gregor6569d682009-05-27 23:11:45 +00001181 // Determine whether this is a top-level (non-nested) class.
1182 bool TopLevelClass = ClassStack.empty() ||
1183 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001184
1185 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001186 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001187
Douglas Gregor6569d682009-05-27 23:11:45 +00001188 // Note that we are parsing a new (potentially-nested) class definition.
1189 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1190
Douglas Gregorddc29e12009-02-06 22:42:48 +00001191 if (TagDecl)
1192 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1193 else {
1194 SkipUntil(tok::r_brace, false, false);
1195 return;
1196 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001197
1198 // C++ 11p3: Members of a class defined with the keyword class are private
1199 // by default. Members of a class defined with the keywords struct or union
1200 // are public by default.
1201 AccessSpecifier CurAS;
1202 if (TagType == DeclSpec::TST_class)
1203 CurAS = AS_private;
1204 else
1205 CurAS = AS_public;
1206
1207 // While we still have something to read, read the member-declarations.
1208 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1209 // Each iteration of this loop reads one member-declaration.
1210
1211 // Check for extraneous top-level semicolon.
1212 if (Tok.is(tok::semi)) {
1213 Diag(Tok, diag::ext_extra_struct_semi);
1214 ConsumeToken();
1215 continue;
1216 }
1217
1218 AccessSpecifier AS = getAccessSpecifierIfPresent();
1219 if (AS != AS_none) {
1220 // Current token is a C++ access specifier.
1221 CurAS = AS;
1222 ConsumeToken();
1223 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1224 continue;
1225 }
1226
Douglas Gregor37b372b2009-08-20 22:52:58 +00001227 // FIXME: Make sure we don't have a template here.
1228
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001229 // Parse all the comma separated declarators.
1230 ParseCXXClassMemberDeclaration(CurAS);
1231 }
1232
1233 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1234
1235 AttributeList *AttrList = 0;
1236 // If attributes exist after class contents, parse them.
1237 if (Tok.is(tok::kw___attribute))
1238 AttrList = ParseAttributes(); // FIXME: where should I put them?
1239
1240 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1241 LBraceLoc, RBraceLoc);
1242
1243 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1244 // complete within function bodies, default arguments,
1245 // exception-specifications, and constructor ctor-initializers (including
1246 // such things in nested classes).
1247 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001248 // FIXME: Only function bodies and constructor ctor-initializers are
1249 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001250 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001251 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001252 // are complete and we can parse the delayed portions of method
1253 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001254 ParseLexedMethodDeclarations(getCurrentClass());
1255 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001256 }
1257
1258 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001259 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001260 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001261
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001262 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001263}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001264
1265/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1266/// which explicitly initializes the members or base classes of a
1267/// class (C++ [class.base.init]). For example, the three initializers
1268/// after the ':' in the Derived constructor below:
1269///
1270/// @code
1271/// class Base { };
1272/// class Derived : Base {
1273/// int x;
1274/// float f;
1275/// public:
1276/// Derived(float f) : Base(), x(17), f(f) { }
1277/// };
1278/// @endcode
1279///
1280/// [C++] ctor-initializer:
1281/// ':' mem-initializer-list
1282///
1283/// [C++] mem-initializer-list:
1284/// mem-initializer
1285/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001286void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001287 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1288
1289 SourceLocation ColonLoc = ConsumeToken();
1290
1291 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
1292
1293 do {
1294 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001295 if (!MemInit.isInvalid())
1296 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001297
1298 if (Tok.is(tok::comma))
1299 ConsumeToken();
1300 else if (Tok.is(tok::l_brace))
1301 break;
1302 else {
1303 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001304 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001305 SkipUntil(tok::l_brace, true, true);
1306 break;
1307 }
1308 } while (true);
1309
1310 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001311 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001312}
1313
1314/// ParseMemInitializer - Parse a C++ member initializer, which is
1315/// part of a constructor initializer that explicitly initializes one
1316/// member or base class (C++ [class.base.init]). See
1317/// ParseConstructorInitializer for an example.
1318///
1319/// [C++] mem-initializer:
1320/// mem-initializer-id '(' expression-list[opt] ')'
1321///
1322/// [C++] mem-initializer-id:
1323/// '::'[opt] nested-name-specifier[opt] class-name
1324/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001325Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001326 // parse '::'[opt] nested-name-specifier[opt]
1327 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001328 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001329 TypeTy *TemplateTypeTy = 0;
1330 if (Tok.is(tok::annot_template_id)) {
1331 TemplateIdAnnotation *TemplateId
1332 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1333 if (TemplateId->Kind == TNK_Type_template) {
1334 AnnotateTemplateIdTokenAsType(&SS);
1335 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1336 TemplateTypeTy = Tok.getAnnotationValue();
1337 }
1338 // FIXME. May need to check for TNK_Dependent_template as well.
1339 }
1340 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001341 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001342 return true;
1343 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001344
Douglas Gregor7ad83902008-11-05 04:29:56 +00001345 // Get the identifier. This may be a member name or a class name,
1346 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001347 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001348 SourceLocation IdLoc = ConsumeToken();
1349
1350 // Parse the '('.
1351 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001352 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001353 return true;
1354 }
1355 SourceLocation LParenLoc = ConsumeParen();
1356
1357 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001358 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001359 CommaLocsTy CommaLocs;
1360 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1361 SkipUntil(tok::r_paren);
1362 return true;
1363 }
1364
1365 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1366
Fariborz Jahanian96174332009-07-01 19:21:19 +00001367 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1368 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001369 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001370 ArgExprs.size(), CommaLocs.data(),
1371 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001372}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001373
1374/// ParseExceptionSpecification - Parse a C++ exception-specification
1375/// (C++ [except.spec]).
1376///
Douglas Gregora4745612008-12-01 18:00:20 +00001377/// exception-specification:
1378/// 'throw' '(' type-id-list [opt] ')'
1379/// [MS] 'throw' '(' '...' ')'
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001380///
Douglas Gregora4745612008-12-01 18:00:20 +00001381/// type-id-list:
1382/// type-id
1383/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001384///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001385bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001386 llvm::SmallVector<TypeTy*, 2>
1387 &Exceptions,
1388 llvm::SmallVector<SourceRange, 2>
1389 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001390 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001391 assert(Tok.is(tok::kw_throw) && "expected throw");
1392
1393 SourceLocation ThrowLoc = ConsumeToken();
1394
1395 if (!Tok.is(tok::l_paren)) {
1396 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1397 }
1398 SourceLocation LParenLoc = ConsumeParen();
1399
Douglas Gregora4745612008-12-01 18:00:20 +00001400 // Parse throw(...), a Microsoft extension that means "this function
1401 // can throw anything".
1402 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001403 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001404 SourceLocation EllipsisLoc = ConsumeToken();
1405 if (!getLang().Microsoft)
1406 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001407 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001408 return false;
1409 }
1410
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001411 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001412 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001413 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001414 TypeResult Res(ParseTypeName(&Range));
1415 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001416 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001417 Ranges.push_back(Range);
1418 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001419 if (Tok.is(tok::comma))
1420 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001421 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001422 break;
1423 }
1424
Sebastian Redlab197ba2009-02-09 18:23:29 +00001425 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001426 return false;
1427}
Douglas Gregor6569d682009-05-27 23:11:45 +00001428
1429/// \brief We have just started parsing the definition of a new class,
1430/// so push that class onto our stack of classes that is currently
1431/// being parsed.
1432void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
1433 assert((TopLevelClass || !ClassStack.empty()) &&
1434 "Nested class without outer class");
1435 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1436}
1437
1438/// \brief Deallocate the given parsed class and all of its nested
1439/// classes.
1440void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1441 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1442 DeallocateParsedClasses(Class->NestedClasses[I]);
1443 delete Class;
1444}
1445
1446/// \brief Pop the top class of the stack of classes that are
1447/// currently being parsed.
1448///
1449/// This routine should be called when we have finished parsing the
1450/// definition of a class, but have not yet popped the Scope
1451/// associated with the class's definition.
1452///
1453/// \returns true if the class we've popped is a top-level class,
1454/// false otherwise.
1455void Parser::PopParsingClass() {
1456 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
1457
1458 ParsingClass *Victim = ClassStack.top();
1459 ClassStack.pop();
1460 if (Victim->TopLevelClass) {
1461 // Deallocate all of the nested classes of this class,
1462 // recursively: we don't need to keep any of this information.
1463 DeallocateParsedClasses(Victim);
1464 return;
1465 }
1466 assert(!ClassStack.empty() && "Missing top-level class?");
1467
1468 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1469 Victim->NestedClasses.empty()) {
1470 // The victim is a nested class, but we will not need to perform
1471 // any processing after the definition of this class since it has
1472 // no members whose handling was delayed. Therefore, we can just
1473 // remove this nested class.
1474 delete Victim;
1475 return;
1476 }
1477
1478 // This nested class has some members that will need to be processed
1479 // after the top-level class is completely defined. Therefore, add
1480 // it to the list of nested classes within its parent.
1481 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1482 ClassStack.top()->NestedClasses.push_back(Victim);
1483 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1484}