blob: 9f704d2ea8a5ec70be223acbadb618438d2f1946 [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
Douglas Gregor1b7f8982008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregore37ac4f2008-04-13 21:30:24 +000016#include "clang/Parse/DeclSpec.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000017#include "clang/Parse/Scope.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000018#include "AstGuard.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///
45Parser::DeclTy *Parser::ParseNamespace(unsigned Context) {
Chris Lattner04d66662007-10-09 17:33:22 +000046 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000047 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
48
49 SourceLocation IdentLoc;
50 IdentifierInfo *Ident = 0;
51
Chris Lattner04d66662007-10-09 17:33:22 +000052 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000053 Ident = Tok.getIdentifierInfo();
54 IdentLoc = ConsumeToken(); // eat the identifier.
55 }
56
57 // Read label attributes, if present.
58 DeclTy *AttrList = 0;
Chris Lattner04d66662007-10-09 17:33:22 +000059 if (Tok.is(tok::kw___attribute))
Chris Lattner8f08cb72007-08-25 06:57:03 +000060 // FIXME: save these somewhere.
61 AttrList = ParseAttributes();
62
Chris Lattner04d66662007-10-09 17:33:22 +000063 if (Tok.is(tok::equal)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000064 // FIXME: Verify no attributes were present.
65 // FIXME: parse this.
Chris Lattner04d66662007-10-09 17:33:22 +000066 } else if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000067
Chris Lattner8f08cb72007-08-25 06:57:03 +000068 SourceLocation LBrace = ConsumeBrace();
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000069
70 // Enter a scope for the namespace.
Douglas Gregor8935b8b2008-12-10 06:34:36 +000071 ParseScope NamespaceScope(this, Scope::DeclScope);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000072
73 DeclTy *NamespcDecl =
74 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
75
76 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
Chris Lattnerbae35112007-08-25 18:15:16 +000077 ParseExternalDeclaration();
Chris Lattner8f08cb72007-08-25 06:57:03 +000078
Argyrios Kyrtzidis8ba5d792008-05-01 21:44:34 +000079 // Leave the namespace scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +000080 NamespaceScope.Exit();
Argyrios Kyrtzidis8ba5d792008-05-01 21:44:34 +000081
Chris Lattner8f08cb72007-08-25 06:57:03 +000082 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000083 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBrace);
84
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000085 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +000086
Chris Lattner8f08cb72007-08-25 06:57:03 +000087 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +000088 Diag(Tok, Ident ? diag::err_expected_lbrace :
89 diag::err_expected_ident_lbrace);
Chris Lattner8f08cb72007-08-25 06:57:03 +000090 }
91
92 return 0;
93}
Chris Lattnerc6fdc342008-01-12 07:05:38 +000094
95/// ParseLinkage - We know that the current token is a string_literal
96/// and just before that, that extern was seen.
97///
98/// linkage-specification: [C++ 7.5p2: dcl.link]
99/// 'extern' string-literal '{' declaration-seq[opt] '}'
100/// 'extern' string-literal declaration
101///
102Parser::DeclTy *Parser::ParseLinkage(unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000103 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000104 llvm::SmallVector<char, 8> LangBuffer;
105 // LangBuffer is guaranteed to be big enough.
106 LangBuffer.resize(Tok.getLength());
107 const char *LangBufPtr = &LangBuffer[0];
108 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
109
110 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000111
Douglas Gregor074149e2009-01-05 19:45:36 +0000112 ParseScope LinkageScope(this, Scope::DeclScope);
113 DeclTy *LinkageSpec
114 = Actions.ActOnStartLinkageSpecification(CurScope,
115 /*FIXME: */SourceLocation(),
116 Loc, LangBufPtr, StrSize,
117 Tok.is(tok::l_brace)? Tok.getLocation()
118 : SourceLocation());
119
120 if (Tok.isNot(tok::l_brace)) {
121 ParseDeclarationOrFunctionDefinition();
122 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
123 SourceLocation());
Douglas Gregorf44515a2008-12-16 22:23:02 +0000124 }
125
126 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000127 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000128 ParseExternalDeclaration();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000129 }
130
Douglas Gregorf44515a2008-12-16 22:23:02 +0000131 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000132 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000133}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000134
Douglas Gregorf780abc2008-12-30 03:27:21 +0000135/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
136/// using-directive. Assumes that current token is 'using'.
Chris Lattner2f274772009-01-06 06:55:51 +0000137Parser::DeclTy *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000138 assert(Tok.is(tok::kw_using) && "Not using token");
139
140 // Eat 'using'.
141 SourceLocation UsingLoc = ConsumeToken();
142
Chris Lattner2f274772009-01-06 06:55:51 +0000143 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000144 // Next token after 'using' is 'namespace' so it must be using-directive
145 return ParseUsingDirective(Context, UsingLoc);
Chris Lattner2f274772009-01-06 06:55:51 +0000146
147 // Otherwise, it must be using-declaration.
148 return ParseUsingDeclaration(Context, UsingLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000149}
150
151/// ParseUsingDirective - Parse C++ using-directive, assumes
152/// that current token is 'namespace' and 'using' was already parsed.
153///
154/// using-directive: [C++ 7.3.p4: namespace.udir]
155/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
156/// namespace-name ;
157/// [GNU] using-directive:
158/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
159/// namespace-name attributes[opt] ;
160///
161Parser::DeclTy *Parser::ParseUsingDirective(unsigned Context,
162 SourceLocation UsingLoc) {
163 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
164
165 // Eat 'namespace'.
166 SourceLocation NamespcLoc = ConsumeToken();
167
168 CXXScopeSpec SS;
169 // Parse (optional) nested-name-specifier.
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000170 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000171
172 AttributeList *AttrList = 0;
173 IdentifierInfo *NamespcName = 0;
174 SourceLocation IdentLoc = SourceLocation();
175
176 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000177 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000178 Diag(Tok, diag::err_expected_namespace_name);
179 // If there was invalid namespace name, skip to end of decl, and eat ';'.
180 SkipUntil(tok::semi);
181 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
182 return 0;
183 }
Chris Lattner823c44e2009-01-06 07:27:21 +0000184
185 // Parse identifier.
186 NamespcName = Tok.getIdentifierInfo();
187 IdentLoc = ConsumeToken();
188
189 // Parse (optional) attributes (most likely GNU strong-using extension).
190 if (Tok.is(tok::kw___attribute))
191 AttrList = ParseAttributes();
192
193 // Eat ';'.
194 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
195 AttrList ? "attributes list" : "namespace name", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000196
197 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner823c44e2009-01-06 07:27:21 +0000198 IdentLoc, NamespcName, AttrList);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000199}
200
201/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
202/// 'using' was already seen.
203///
204/// using-declaration: [C++ 7.3.p3: namespace.udecl]
205/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
206/// unqualified-id [TODO]
207/// 'using' :: unqualified-id [TODO]
208///
209Parser::DeclTy *Parser::ParseUsingDeclaration(unsigned Context,
210 SourceLocation UsingLoc) {
211 assert(false && "Not implemented");
212 // FIXME: Implement parsing.
213 return 0;
214}
215
Douglas Gregor42a552f2008-11-05 20:51:48 +0000216/// ParseClassName - Parse a C++ class-name, which names a class. Note
217/// that we only check that the result names a type; semantic analysis
218/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000219/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000220/// found.
221///
222/// class-name: [C++ 9.1]
223/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000224/// simple-template-id
Douglas Gregor42a552f2008-11-05 20:51:48 +0000225///
Douglas Gregor7f43d672009-02-25 23:52:28 +0000226Parser::TypeTy *Parser::ParseClassName(SourceLocation &EndLocation,
227 const CXXScopeSpec *SS) {
228 // Check whether we have a template-id that names a type.
229 if (Tok.is(tok::annot_template_id)) {
230 TemplateIdAnnotation *TemplateId
231 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
232 if (TemplateId->Kind == TNK_Class_template) {
233 if (AnnotateTemplateIdTokenAsType(SS))
234 return 0;
235
236 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
237 TypeTy *Type = Tok.getAnnotationValue();
238 EndLocation = Tok.getAnnotationEndLoc();
239 ConsumeToken();
240 return Type;
241 }
242
243 // Fall through to produce an error below.
244 }
245
Douglas Gregor42a552f2008-11-05 20:51:48 +0000246 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000247 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000248 return 0;
249 }
250
251 // We have an identifier; check whether it is actually a type.
Douglas Gregorb696ea32009-02-04 17:00:24 +0000252 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
253 Tok.getLocation(), CurScope, SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000254 if (!Type) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000255 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000256 return 0;
257 }
258
259 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000260 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000261 return Type;
262}
263
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000264/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
265/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
266/// until we reach the start of a definition or see a token that
267/// cannot start a definition.
268///
269/// class-specifier: [C++ class]
270/// class-head '{' member-specification[opt] '}'
271/// class-head '{' member-specification[opt] '}' attributes[opt]
272/// class-head:
273/// class-key identifier[opt] base-clause[opt]
274/// class-key nested-name-specifier identifier base-clause[opt]
275/// class-key nested-name-specifier[opt] simple-template-id
276/// base-clause[opt]
277/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
278/// [GNU] class-key attributes[opt] nested-name-specifier
279/// identifier base-clause[opt]
280/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
281/// simple-template-id base-clause[opt]
282/// class-key:
283/// 'class'
284/// 'struct'
285/// 'union'
286///
287/// elaborated-type-specifier: [C++ dcl.type.elab]
288/// class-key ::[opt] nested-name-specifier[opt] identifier
289/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
290/// simple-template-id
291///
292/// Note that the C++ class-specifier and elaborated-type-specifier,
293/// together, subsume the C99 struct-or-union-specifier:
294///
295/// struct-or-union-specifier: [C99 6.7.2.1]
296/// struct-or-union identifier[opt] '{' struct-contents '}'
297/// struct-or-union identifier
298/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
299/// '}' attributes[opt]
300/// [GNU] struct-or-union attributes[opt] identifier
301/// struct-or-union:
302/// 'struct'
303/// 'union'
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000304void Parser::ParseClassSpecifier(DeclSpec &DS,
305 TemplateParameterLists *TemplateParams) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000306 assert((Tok.is(tok::kw_class) ||
307 Tok.is(tok::kw_struct) ||
308 Tok.is(tok::kw_union)) &&
309 "Not a class specifier");
310 DeclSpec::TST TagType =
311 Tok.is(tok::kw_class) ? DeclSpec::TST_class :
312 Tok.is(tok::kw_struct) ? DeclSpec::TST_struct :
313 DeclSpec::TST_union;
314
315 SourceLocation StartLoc = ConsumeToken();
316
317 AttributeList *Attr = 0;
318 // If attributes exist after tag, parse them.
319 if (Tok.is(tok::kw___attribute))
320 Attr = ParseAttributes();
321
Steve Narofff59e17e2008-12-24 20:59:21 +0000322 // If declspecs exist after tag, parse them.
323 if (Tok.is(tok::kw___declspec) && PP.getLangOptions().Microsoft)
324 FuzzyParseMicrosoftDeclSpec();
325
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000326 // Parse the (optional) nested-name-specifier.
327 CXXScopeSpec SS;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000328 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS))
329 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000330 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000331
332 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000333 IdentifierInfo *Name = 0;
334 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000335 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000336 if (Tok.is(tok::identifier)) {
337 Name = Tok.getIdentifierInfo();
338 NameLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000339 } else if (Tok.is(tok::annot_template_id)) {
340 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
341 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000342
Douglas Gregor39a8de12009-02-25 19:37:18 +0000343 if (TemplateId->Kind != TNK_Class_template) {
344 // The template-name in the simple-template-id refers to
345 // something other than a class template. Give an appropriate
346 // error message and skip to the ';'.
347 SourceRange Range(NameLoc);
348 if (SS.isNotEmpty())
349 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000350
Douglas Gregor39a8de12009-02-25 19:37:18 +0000351 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
352 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +0000353
Douglas Gregor39a8de12009-02-25 19:37:18 +0000354 DS.SetTypeSpecError();
355 SkipUntil(tok::semi, false, true);
356 TemplateId->Destroy();
357 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000358 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000359 }
360
361 // There are three options here. If we have 'struct foo;', then
362 // this is a forward declaration. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000363 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000364 // something like 'struct foo xyz', a reference.
365 Action::TagKind TK;
366 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
367 TK = Action::TK_Definition;
368 else if (Tok.is(tok::semi))
369 TK = Action::TK_Declaration;
370 else
371 TK = Action::TK_Reference;
372
Douglas Gregor39a8de12009-02-25 19:37:18 +0000373 if (!Name && !TemplateId && TK != Action::TK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000374 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000375 Diag(StartLoc, diag::err_anon_type_definition)
376 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000377
378 // Skip the rest of this declarator, up until the comma or semicolon.
379 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000380
381 if (TemplateId)
382 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000383 return;
384 }
385
Douglas Gregorddc29e12009-02-06 22:42:48 +0000386 // Create the tag portion of the class or class template.
387 DeclTy *TagOrTempDecl;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000388 if (TemplateId && TK != Action::TK_Reference) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000389 // Explicit specialization or class template partial
390 // specialization. Let semantic analysis decide.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000391 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
392 TemplateId->getTemplateArgs(),
393 TemplateId->getTemplateArgIsType(),
394 TemplateId->NumArgs);
Douglas Gregorcc636682009-02-17 23:15:12 +0000395 TagOrTempDecl
396 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000397 StartLoc, SS,
398 TemplateId->Template,
399 TemplateId->TemplateNameLoc,
400 TemplateId->LAngleLoc,
401 TemplateArgsPtr,
402 TemplateId->getTemplateArgLocations(),
403 TemplateId->RAngleLoc,
404 Attr,
Douglas Gregorcc636682009-02-17 23:15:12 +0000405 Action::MultiTemplateParamsArg(Actions,
406 TemplateParams? &(*TemplateParams)[0] : 0,
407 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor39a8de12009-02-25 19:37:18 +0000408 TemplateId->Destroy();
409 } else if (TemplateParams && TK != Action::TK_Reference)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000410 TagOrTempDecl = Actions.ActOnClassTemplate(CurScope, TagType, TK, StartLoc,
411 SS, Name, NameLoc, Attr,
412 Action::MultiTemplateParamsArg(Actions,
413 &(*TemplateParams)[0],
414 TemplateParams->size()));
415 else
416 TagOrTempDecl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name,
417 NameLoc, Attr);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000418
419 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000420 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000421 ParseBaseClause(TagOrTempDecl);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000422
423 // If there is a body, parse it and inform the actions module.
424 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000425 if (getLang().CPlusPlus)
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000426 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempDecl);
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000427 else
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000428 ParseStructUnionBody(StartLoc, TagType, TagOrTempDecl);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000429 else if (TK == Action::TK_Definition) {
430 // FIXME: Complain that we have a base-specifier list but no
431 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000432 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000433 }
434
435 const char *PrevSpec = 0;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000436 if (!TagOrTempDecl)
437 DS.SetTypeSpecError();
438 else if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagOrTempDecl))
Chris Lattner1ab3b962008-11-18 07:48:38 +0000439 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000440}
441
442/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
443///
444/// base-clause : [C++ class.derived]
445/// ':' base-specifier-list
446/// base-specifier-list:
447/// base-specifier '...'[opt]
448/// base-specifier-list ',' base-specifier '...'[opt]
449void Parser::ParseBaseClause(DeclTy *ClassDecl)
450{
451 assert(Tok.is(tok::colon) && "Not a base clause");
452 ConsumeToken();
453
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000454 // Build up an array of parsed base specifiers.
455 llvm::SmallVector<BaseTy *, 8> BaseInfo;
456
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000457 while (true) {
458 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000459 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000460 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000461 // Skip the rest of this base specifier, up until the comma or
462 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000463 SkipUntil(tok::comma, tok::l_brace, true, true);
464 } else {
465 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000466 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000467 }
468
469 // If the next token is a comma, consume it and keep reading
470 // base-specifiers.
471 if (Tok.isNot(tok::comma)) break;
472
473 // Consume the comma.
474 ConsumeToken();
475 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000476
477 // Attach the base specifiers
478 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000479}
480
481/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
482/// one entry in the base class list of a class specifier, for example:
483/// class foo : public bar, virtual private baz {
484/// 'public bar' and 'virtual private baz' are each base-specifiers.
485///
486/// base-specifier: [C++ class.derived]
487/// ::[opt] nested-name-specifier[opt] class-name
488/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
489/// class-name
490/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
491/// class-name
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000492Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000493{
494 bool IsVirtual = false;
495 SourceLocation StartLoc = Tok.getLocation();
496
497 // Parse the 'virtual' keyword.
498 if (Tok.is(tok::kw_virtual)) {
499 ConsumeToken();
500 IsVirtual = true;
501 }
502
503 // Parse an (optional) access specifier.
504 AccessSpecifier Access = getAccessSpecifierIfPresent();
505 if (Access)
506 ConsumeToken();
507
508 // Parse the 'virtual' keyword (again!), in case it came after the
509 // access specifier.
510 if (Tok.is(tok::kw_virtual)) {
511 SourceLocation VirtualLoc = ConsumeToken();
512 if (IsVirtual) {
513 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000514 Diag(VirtualLoc, diag::err_dup_virtual)
515 << SourceRange(VirtualLoc, VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000516 }
517
518 IsVirtual = true;
519 }
520
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000521 // Parse optional '::' and optional nested-name-specifier.
522 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000523 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000524
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000525 // The location of the base class itself.
526 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000527
528 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000529 SourceLocation EndLocation;
530 TypeTy *BaseType = ParseClassName(EndLocation, &SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000531 if (!BaseType)
532 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000533
534 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000535 SourceRange Range(StartLoc, EndLocation);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000536
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000537 // Notify semantic analysis that we have parsed a complete
538 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000539 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
540 BaseType, BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000541}
542
543/// getAccessSpecifierIfPresent - Determine whether the next token is
544/// a C++ access-specifier.
545///
546/// access-specifier: [C++ class.derived]
547/// 'private'
548/// 'protected'
549/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000550AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000551{
552 switch (Tok.getKind()) {
553 default: return AS_none;
554 case tok::kw_private: return AS_private;
555 case tok::kw_protected: return AS_protected;
556 case tok::kw_public: return AS_public;
557 }
558}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000559
560/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
561///
562/// member-declaration:
563/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
564/// function-definition ';'[opt]
565/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
566/// using-declaration [TODO]
567/// [C++0x] static_assert-declaration [TODO]
568/// template-declaration [TODO]
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000569/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000570///
571/// member-declarator-list:
572/// member-declarator
573/// member-declarator-list ',' member-declarator
574///
575/// member-declarator:
576/// declarator pure-specifier[opt]
577/// declarator constant-initializer[opt]
578/// identifier[opt] ':' constant-expression
579///
580/// pure-specifier: [TODO]
581/// '= 0'
582///
583/// constant-initializer:
584/// '=' constant-expression
585///
586Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000587 // Handle: member-declaration ::= '__extension__' member-declaration
588 if (Tok.is(tok::kw___extension__)) {
589 // __extension__ silences extension warnings in the subexpression.
590 ExtensionRAIIObject O(Diags); // Use RAII to do this.
591 ConsumeToken();
592 return ParseCXXClassMemberDeclaration(AS);
593 }
594
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000595 SourceLocation DSStart = Tok.getLocation();
596 // decl-specifier-seq:
597 // Parse the common declaration-specifiers piece.
598 DeclSpec DS;
599 ParseDeclarationSpecifiers(DS);
600
601 if (Tok.is(tok::semi)) {
602 ConsumeToken();
603 // C++ 9.2p7: The member-declarator-list can be omitted only after a
604 // class-specifier or an enum-specifier or in a friend declaration.
605 // FIXME: Friend declarations.
606 switch (DS.getTypeSpecType()) {
607 case DeclSpec::TST_struct:
608 case DeclSpec::TST_union:
609 case DeclSpec::TST_class:
610 case DeclSpec::TST_enum:
611 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
612 default:
613 Diag(DSStart, diag::err_no_declarators);
614 return 0;
615 }
616 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000617
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000618 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000619
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000620 if (Tok.isNot(tok::colon)) {
621 // Parse the first declarator.
622 ParseDeclarator(DeclaratorInfo);
623 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +0000624 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000625 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000626 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000627 if (Tok.is(tok::semi))
628 ConsumeToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000629 return 0;
630 }
631
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000632 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000633 if (Tok.is(tok::l_brace)
634 || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000635 if (!DeclaratorInfo.isFunctionDeclarator()) {
636 Diag(Tok, diag::err_func_def_no_params);
637 ConsumeBrace();
638 SkipUntil(tok::r_brace, true);
639 return 0;
640 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000641
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000642 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
643 Diag(Tok, diag::err_function_declared_typedef);
644 // This recovery skips the entire function body. It would be nice
645 // to simply call ParseCXXInlineMethodDef() below, however Sema
646 // assumes the declarator represents a function, not a typedef.
647 ConsumeBrace();
648 SkipUntil(tok::r_brace, true);
649 return 0;
650 }
651
652 return ParseCXXInlineMethodDef(AS, DeclaratorInfo);
653 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000654 }
655
656 // member-declarator-list:
657 // member-declarator
658 // member-declarator-list ',' member-declarator
659
660 DeclTy *LastDeclInGroup = 0;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000661 OwningExprResult BitfieldSize(Actions);
662 OwningExprResult Init(Actions);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000663
664 while (1) {
665
666 // member-declarator:
667 // declarator pure-specifier[opt]
668 // declarator constant-initializer[opt]
669 // identifier[opt] ':' constant-expression
670
671 if (Tok.is(tok::colon)) {
672 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000673 BitfieldSize = ParseConstantExpression();
674 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000675 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000676 }
677
678 // pure-specifier:
679 // '= 0'
680 //
681 // constant-initializer:
682 // '=' constant-expression
683
684 if (Tok.is(tok::equal)) {
685 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000686 Init = ParseInitializer();
687 if (Init.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000688 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000689 }
690
691 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000692 if (Tok.is(tok::kw___attribute)) {
693 SourceLocation Loc;
694 AttributeList *AttrList = ParseAttributes(&Loc);
695 DeclaratorInfo.AddAttributes(AttrList, Loc);
696 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000697
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000698 // NOTE: If Sema is the Action module and declarator is an instance field,
699 // this call will *not* return the created decl; LastDeclInGroup will be
700 // returned instead.
701 // See Sema::ActOnCXXMemberDeclarator for details.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000702 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
703 DeclaratorInfo,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000704 BitfieldSize.release(),
705 Init.release(),
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000706 LastDeclInGroup);
707
Douglas Gregor72b505b2008-12-16 21:30:33 +0000708 if (DeclaratorInfo.isFunctionDeclarator() &&
709 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
710 != DeclSpec::SCS_typedef) {
711 // We just declared a member function. If this member function
712 // has any default arguments, we'll need to parse them later.
713 LateParsedMethodDeclaration *LateMethod = 0;
714 DeclaratorChunk::FunctionTypeInfo &FTI
715 = DeclaratorInfo.getTypeObject(0).Fun;
716 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
717 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
718 if (!LateMethod) {
719 // Push this method onto the stack of late-parsed method
720 // declarations.
721 getCurTopClassStack().MethodDecls.push_back(
722 LateParsedMethodDeclaration(LastDeclInGroup));
723 LateMethod = &getCurTopClassStack().MethodDecls.back();
724
725 // Add all of the parameters prior to this one (they don't
726 // have default arguments).
727 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
728 for (unsigned I = 0; I < ParamIdx; ++I)
729 LateMethod->DefaultArgs.push_back(
730 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
731 }
732
733 // Add this parameter to the list of parameters (it or may
734 // not have a default argument).
735 LateMethod->DefaultArgs.push_back(
736 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
737 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
738 }
739 }
740 }
741
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000742 // If we don't have a comma, it is either the end of the list (a ';')
743 // or an error, bail out.
744 if (Tok.isNot(tok::comma))
745 break;
746
747 // Consume the comma.
748 ConsumeToken();
749
750 // Parse the next declarator.
751 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000752 BitfieldSize = 0;
753 Init = 0;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000754
755 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000756 if (Tok.is(tok::kw___attribute)) {
757 SourceLocation Loc;
758 AttributeList *AttrList = ParseAttributes(&Loc);
759 DeclaratorInfo.AddAttributes(AttrList, Loc);
760 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000761
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000762 if (Tok.isNot(tok::colon))
763 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000764 }
765
766 if (Tok.is(tok::semi)) {
767 ConsumeToken();
768 // Reverse the chain list.
769 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
770 }
771
772 Diag(Tok, diag::err_expected_semi_decl_list);
773 // Skip to end of block or statement
774 SkipUntil(tok::r_brace, true, true);
775 if (Tok.is(tok::semi))
776 ConsumeToken();
777 return 0;
778}
779
780/// ParseCXXMemberSpecification - Parse the class definition.
781///
782/// member-specification:
783/// member-declaration member-specification[opt]
784/// access-specifier ':' member-specification[opt]
785///
786void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
787 unsigned TagType, DeclTy *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000788 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000789 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000790 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000791
792 SourceLocation LBraceLoc = ConsumeBrace();
793
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000794 if (!CurScope->isClassScope() && // Not about to define a nested class.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000795 CurScope->isInCXXInlineMethodScope()) {
796 // We will define a local class of an inline method.
797 // Push a new LexedMethodsForTopClass for its inline methods.
798 PushTopClassStack();
799 }
800
801 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000802 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000803
Douglas Gregorddc29e12009-02-06 22:42:48 +0000804 if (TagDecl)
805 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
806 else {
807 SkipUntil(tok::r_brace, false, false);
808 return;
809 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000810
811 // C++ 11p3: Members of a class defined with the keyword class are private
812 // by default. Members of a class defined with the keywords struct or union
813 // are public by default.
814 AccessSpecifier CurAS;
815 if (TagType == DeclSpec::TST_class)
816 CurAS = AS_private;
817 else
818 CurAS = AS_public;
819
820 // While we still have something to read, read the member-declarations.
821 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
822 // Each iteration of this loop reads one member-declaration.
823
824 // Check for extraneous top-level semicolon.
825 if (Tok.is(tok::semi)) {
826 Diag(Tok, diag::ext_extra_struct_semi);
827 ConsumeToken();
828 continue;
829 }
830
831 AccessSpecifier AS = getAccessSpecifierIfPresent();
832 if (AS != AS_none) {
833 // Current token is a C++ access specifier.
834 CurAS = AS;
835 ConsumeToken();
836 ExpectAndConsume(tok::colon, diag::err_expected_colon);
837 continue;
838 }
839
840 // Parse all the comma separated declarators.
841 ParseCXXClassMemberDeclaration(CurAS);
842 }
843
844 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
845
846 AttributeList *AttrList = 0;
847 // If attributes exist after class contents, parse them.
848 if (Tok.is(tok::kw___attribute))
849 AttrList = ParseAttributes(); // FIXME: where should I put them?
850
851 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
852 LBraceLoc, RBraceLoc);
853
854 // C++ 9.2p2: Within the class member-specification, the class is regarded as
855 // complete within function bodies, default arguments,
856 // exception-specifications, and constructor ctor-initializers (including
857 // such things in nested classes).
858 //
Douglas Gregor72b505b2008-12-16 21:30:33 +0000859 // FIXME: Only function bodies and constructor ctor-initializers are
860 // parsed correctly, fix the rest.
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000861 if (!CurScope->getParent()->isClassScope()) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000862 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +0000863 // are complete and we can parse the delayed portions of method
864 // declarations and the lexed inline method definitions.
865 ParseLexedMethodDeclarations();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000866 ParseLexedMethodDefs();
867
868 // For a local class of inline method, pop the LexedMethodsForTopClass that
869 // was previously pushed.
870
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000871 assert((CurScope->isInCXXInlineMethodScope() ||
872 TopClassStacks.size() == 1) &&
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000873 "MethodLexers not getting popped properly!");
874 if (CurScope->isInCXXInlineMethodScope())
875 PopTopClassStack();
876 }
877
878 // Leave the class scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000879 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000880
Douglas Gregor72de6672009-01-08 20:45:30 +0000881 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000882}
Douglas Gregor7ad83902008-11-05 04:29:56 +0000883
884/// ParseConstructorInitializer - Parse a C++ constructor initializer,
885/// which explicitly initializes the members or base classes of a
886/// class (C++ [class.base.init]). For example, the three initializers
887/// after the ':' in the Derived constructor below:
888///
889/// @code
890/// class Base { };
891/// class Derived : Base {
892/// int x;
893/// float f;
894/// public:
895/// Derived(float f) : Base(), x(17), f(f) { }
896/// };
897/// @endcode
898///
899/// [C++] ctor-initializer:
900/// ':' mem-initializer-list
901///
902/// [C++] mem-initializer-list:
903/// mem-initializer
904/// mem-initializer , mem-initializer-list
905void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) {
906 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
907
908 SourceLocation ColonLoc = ConsumeToken();
909
910 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
911
912 do {
913 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000914 if (!MemInit.isInvalid())
915 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000916
917 if (Tok.is(tok::comma))
918 ConsumeToken();
919 else if (Tok.is(tok::l_brace))
920 break;
921 else {
922 // Skip over garbage, until we get to '{'. Don't eat the '{'.
923 SkipUntil(tok::l_brace, true, true);
924 break;
925 }
926 } while (true);
927
928 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
929 &MemInitializers[0], MemInitializers.size());
930}
931
932/// ParseMemInitializer - Parse a C++ member initializer, which is
933/// part of a constructor initializer that explicitly initializes one
934/// member or base class (C++ [class.base.init]). See
935/// ParseConstructorInitializer for an example.
936///
937/// [C++] mem-initializer:
938/// mem-initializer-id '(' expression-list[opt] ')'
939///
940/// [C++] mem-initializer-id:
941/// '::'[opt] nested-name-specifier[opt] class-name
942/// identifier
943Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) {
944 // FIXME: parse '::'[opt] nested-name-specifier[opt]
945
946 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000947 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000948 return true;
949 }
950
951 // Get the identifier. This may be a member name or a class name,
952 // but we'll let the semantic analysis determine which it is.
953 IdentifierInfo *II = Tok.getIdentifierInfo();
954 SourceLocation IdLoc = ConsumeToken();
955
956 // Parse the '('.
957 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000958 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000959 return true;
960 }
961 SourceLocation LParenLoc = ConsumeParen();
962
963 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000964 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000965 CommaLocsTy CommaLocs;
966 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
967 SkipUntil(tok::r_paren);
968 return true;
969 }
970
971 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
972
Sebastian Redla55e52c2008-11-25 22:21:31 +0000973 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
974 LParenLoc, ArgExprs.take(),
975 ArgExprs.size(), &CommaLocs[0], RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000976}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +0000977
978/// ParseExceptionSpecification - Parse a C++ exception-specification
979/// (C++ [except.spec]).
980///
Douglas Gregora4745612008-12-01 18:00:20 +0000981/// exception-specification:
982/// 'throw' '(' type-id-list [opt] ')'
983/// [MS] 'throw' '(' '...' ')'
Douglas Gregor0fe7bea2008-11-25 03:22:00 +0000984///
Douglas Gregora4745612008-12-01 18:00:20 +0000985/// type-id-list:
986/// type-id
987/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +0000988///
Sebastian Redlab197ba2009-02-09 18:23:29 +0000989bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +0000990 assert(Tok.is(tok::kw_throw) && "expected throw");
991
992 SourceLocation ThrowLoc = ConsumeToken();
993
994 if (!Tok.is(tok::l_paren)) {
995 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
996 }
997 SourceLocation LParenLoc = ConsumeParen();
998
Douglas Gregora4745612008-12-01 18:00:20 +0000999 // Parse throw(...), a Microsoft extension that means "this function
1000 // can throw anything".
1001 if (Tok.is(tok::ellipsis)) {
1002 SourceLocation EllipsisLoc = ConsumeToken();
1003 if (!getLang().Microsoft)
1004 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001005 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001006 return false;
1007 }
1008
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001009 // Parse the sequence of type-ids.
1010 while (Tok.isNot(tok::r_paren)) {
1011 ParseTypeName();
1012 if (Tok.is(tok::comma))
1013 ConsumeToken();
1014 else
1015 break;
1016 }
1017
Sebastian Redlab197ba2009-02-09 18:23:29 +00001018 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001019 return false;
1020}