blob: ea7a5a103b6d6381351f3311c4e1a4c82832b2f6 [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"
Douglas Gregore37ac4f2008-04-13 21:30:24 +000015#include "clang/Basic/Diagnostic.h"
16#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 Lattner8f08cb72007-08-25 06:57:03 +000019using namespace clang;
20
21/// ParseNamespace - We know that the current token is a namespace keyword. This
22/// may either be a top level namespace or a block-level namespace alias.
23///
24/// namespace-definition: [C++ 7.3: basic.namespace]
25/// named-namespace-definition
26/// unnamed-namespace-definition
27///
28/// unnamed-namespace-definition:
29/// 'namespace' attributes[opt] '{' namespace-body '}'
30///
31/// named-namespace-definition:
32/// original-namespace-definition
33/// extension-namespace-definition
34///
35/// original-namespace-definition:
36/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
37///
38/// extension-namespace-definition:
39/// 'namespace' original-namespace-name '{' namespace-body '}'
40///
41/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
42/// 'namespace' identifier '=' qualified-namespace-specifier ';'
43///
44Parser::DeclTy *Parser::ParseNamespace(unsigned Context) {
Chris Lattner04d66662007-10-09 17:33:22 +000045 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000046 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
47
48 SourceLocation IdentLoc;
49 IdentifierInfo *Ident = 0;
50
Chris Lattner04d66662007-10-09 17:33:22 +000051 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000052 Ident = Tok.getIdentifierInfo();
53 IdentLoc = ConsumeToken(); // eat the identifier.
54 }
55
56 // Read label attributes, if present.
57 DeclTy *AttrList = 0;
Chris Lattner04d66662007-10-09 17:33:22 +000058 if (Tok.is(tok::kw___attribute))
Chris Lattner8f08cb72007-08-25 06:57:03 +000059 // FIXME: save these somewhere.
60 AttrList = ParseAttributes();
61
Chris Lattner04d66662007-10-09 17:33:22 +000062 if (Tok.is(tok::equal)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000063 // FIXME: Verify no attributes were present.
64 // FIXME: parse this.
Chris Lattner04d66662007-10-09 17:33:22 +000065 } else if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000066
Chris Lattner8f08cb72007-08-25 06:57:03 +000067 SourceLocation LBrace = ConsumeBrace();
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000068
69 // Enter a scope for the namespace.
70 EnterScope(Scope::DeclScope);
71
72 DeclTy *NamespcDecl =
73 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
74
75 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
Chris Lattnerbae35112007-08-25 18:15:16 +000076 ParseExternalDeclaration();
Chris Lattner8f08cb72007-08-25 06:57:03 +000077
Argyrios Kyrtzidis8ba5d792008-05-01 21:44:34 +000078 // Leave the namespace scope.
79 ExitScope();
80
Chris Lattner8f08cb72007-08-25 06:57:03 +000081 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000082 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBrace);
83
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000084 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +000085
Chris Lattner8f08cb72007-08-25 06:57:03 +000086 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +000087 Diag(Tok, Ident ? diag::err_expected_lbrace :
88 diag::err_expected_ident_lbrace);
Chris Lattner8f08cb72007-08-25 06:57:03 +000089 }
90
91 return 0;
92}
Chris Lattnerc6fdc342008-01-12 07:05:38 +000093
94/// ParseLinkage - We know that the current token is a string_literal
95/// and just before that, that extern was seen.
96///
97/// linkage-specification: [C++ 7.5p2: dcl.link]
98/// 'extern' string-literal '{' declaration-seq[opt] '}'
99/// 'extern' string-literal declaration
100///
101Parser::DeclTy *Parser::ParseLinkage(unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000102 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000103 llvm::SmallVector<char, 8> LangBuffer;
104 // LangBuffer is guaranteed to be big enough.
105 LangBuffer.resize(Tok.getLength());
106 const char *LangBufPtr = &LangBuffer[0];
107 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
108
109 SourceLocation Loc = ConsumeStringToken();
110 DeclTy *D = 0;
111 SourceLocation LBrace, RBrace;
112
113 if (Tok.isNot(tok::l_brace)) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000114 D = ParseDeclarationOrFunctionDefinition();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000115 } else {
116 LBrace = ConsumeBrace();
117 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
118 // FIXME capture the decls.
119 D = ParseExternalDeclaration();
120 }
121
122 RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
123 }
124
125 if (!D)
126 return 0;
127
128 return Actions.ActOnLinkageSpec(Loc, LBrace, RBrace, LangBufPtr, StrSize, D);
129}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000130
Douglas Gregor42a552f2008-11-05 20:51:48 +0000131/// ParseClassName - Parse a C++ class-name, which names a class. Note
132/// that we only check that the result names a type; semantic analysis
133/// will need to verify that the type names a class. The result is
134/// either a type or NULL, dependending on whether a type name was
135/// found.
136///
137/// class-name: [C++ 9.1]
138/// identifier
139/// template-id [TODO]
140///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000141Parser::TypeTy *Parser::ParseClassName(const CXXScopeSpec *SS) {
Douglas Gregor42a552f2008-11-05 20:51:48 +0000142 // Parse the class-name.
143 // FIXME: Alternatively, parse a simple-template-id.
144 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000145 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000146 return 0;
147 }
148
149 // We have an identifier; check whether it is actually a type.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000150 TypeTy *Type = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope, SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000151 if (!Type) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000152 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000153 return 0;
154 }
155
156 // Consume the identifier.
157 ConsumeToken();
158
159 return Type;
160}
161
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000162/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
163/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
164/// until we reach the start of a definition or see a token that
165/// cannot start a definition.
166///
167/// class-specifier: [C++ class]
168/// class-head '{' member-specification[opt] '}'
169/// class-head '{' member-specification[opt] '}' attributes[opt]
170/// class-head:
171/// class-key identifier[opt] base-clause[opt]
172/// class-key nested-name-specifier identifier base-clause[opt]
173/// class-key nested-name-specifier[opt] simple-template-id
174/// base-clause[opt]
175/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
176/// [GNU] class-key attributes[opt] nested-name-specifier
177/// identifier base-clause[opt]
178/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
179/// simple-template-id base-clause[opt]
180/// class-key:
181/// 'class'
182/// 'struct'
183/// 'union'
184///
185/// elaborated-type-specifier: [C++ dcl.type.elab]
186/// class-key ::[opt] nested-name-specifier[opt] identifier
187/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
188/// simple-template-id
189///
190/// Note that the C++ class-specifier and elaborated-type-specifier,
191/// together, subsume the C99 struct-or-union-specifier:
192///
193/// struct-or-union-specifier: [C99 6.7.2.1]
194/// struct-or-union identifier[opt] '{' struct-contents '}'
195/// struct-or-union identifier
196/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
197/// '}' attributes[opt]
198/// [GNU] struct-or-union attributes[opt] identifier
199/// struct-or-union:
200/// 'struct'
201/// 'union'
202void Parser::ParseClassSpecifier(DeclSpec &DS) {
203 assert((Tok.is(tok::kw_class) ||
204 Tok.is(tok::kw_struct) ||
205 Tok.is(tok::kw_union)) &&
206 "Not a class specifier");
207 DeclSpec::TST TagType =
208 Tok.is(tok::kw_class) ? DeclSpec::TST_class :
209 Tok.is(tok::kw_struct) ? DeclSpec::TST_struct :
210 DeclSpec::TST_union;
211
212 SourceLocation StartLoc = ConsumeToken();
213
214 AttributeList *Attr = 0;
215 // If attributes exist after tag, parse them.
216 if (Tok.is(tok::kw___attribute))
217 Attr = ParseAttributes();
218
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000219 // Parse the (optional) nested-name-specifier.
220 CXXScopeSpec SS;
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +0000221 if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000222 if (Tok.isNot(tok::identifier))
223 Diag(Tok, diag::err_expected_ident);
224 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000225
226 // Parse the (optional) class name.
227 // FIXME: Alternatively, parse a simple-template-id.
228 IdentifierInfo *Name = 0;
229 SourceLocation NameLoc;
230 if (Tok.is(tok::identifier)) {
231 Name = Tok.getIdentifierInfo();
232 NameLoc = ConsumeToken();
233 }
234
235 // There are three options here. If we have 'struct foo;', then
236 // this is a forward declaration. If we have 'struct foo {...' or
237 // 'struct fo :...' then this is a definition. Otherwise we have
238 // something like 'struct foo xyz', a reference.
239 Action::TagKind TK;
240 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
241 TK = Action::TK_Definition;
242 else if (Tok.is(tok::semi))
243 TK = Action::TK_Declaration;
244 else
245 TK = Action::TK_Reference;
246
247 if (!Name && TK != Action::TK_Definition) {
248 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000249 Diag(StartLoc, diag::err_anon_type_definition)
250 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000251
252 // Skip the rest of this declarator, up until the comma or semicolon.
253 SkipUntil(tok::comma, true);
254 return;
255 }
256
257 // Parse the tag portion of this.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000258 DeclTy *TagDecl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name,
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000259 NameLoc, Attr);
260
261 // Parse the optional base clause (C++ only).
262 if (getLang().CPlusPlus && Tok.is(tok::colon)) {
263 ParseBaseClause(TagDecl);
264 }
265
266 // If there is a body, parse it and inform the actions module.
267 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000268 if (getLang().CPlusPlus)
269 ParseCXXMemberSpecification(StartLoc, TagType, TagDecl);
270 else
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000271 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000272 else if (TK == Action::TK_Definition) {
273 // FIXME: Complain that we have a base-specifier list but no
274 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000275 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000276 }
277
278 const char *PrevSpec = 0;
279 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
Chris Lattner1ab3b962008-11-18 07:48:38 +0000280 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000281}
282
283/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
284///
285/// base-clause : [C++ class.derived]
286/// ':' base-specifier-list
287/// base-specifier-list:
288/// base-specifier '...'[opt]
289/// base-specifier-list ',' base-specifier '...'[opt]
290void Parser::ParseBaseClause(DeclTy *ClassDecl)
291{
292 assert(Tok.is(tok::colon) && "Not a base clause");
293 ConsumeToken();
294
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000295 // Build up an array of parsed base specifiers.
296 llvm::SmallVector<BaseTy *, 8> BaseInfo;
297
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000298 while (true) {
299 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000300 BaseResult Result = ParseBaseSpecifier(ClassDecl);
301 if (Result.isInvalid) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000302 // Skip the rest of this base specifier, up until the comma or
303 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000304 SkipUntil(tok::comma, tok::l_brace, true, true);
305 } else {
306 // Add this to our array of base specifiers.
307 BaseInfo.push_back(Result.Val);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000308 }
309
310 // If the next token is a comma, consume it and keep reading
311 // base-specifiers.
312 if (Tok.isNot(tok::comma)) break;
313
314 // Consume the comma.
315 ConsumeToken();
316 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000317
318 // Attach the base specifiers
319 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000320}
321
322/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
323/// one entry in the base class list of a class specifier, for example:
324/// class foo : public bar, virtual private baz {
325/// 'public bar' and 'virtual private baz' are each base-specifiers.
326///
327/// base-specifier: [C++ class.derived]
328/// ::[opt] nested-name-specifier[opt] class-name
329/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
330/// class-name
331/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
332/// class-name
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000333Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000334{
335 bool IsVirtual = false;
336 SourceLocation StartLoc = Tok.getLocation();
337
338 // Parse the 'virtual' keyword.
339 if (Tok.is(tok::kw_virtual)) {
340 ConsumeToken();
341 IsVirtual = true;
342 }
343
344 // Parse an (optional) access specifier.
345 AccessSpecifier Access = getAccessSpecifierIfPresent();
346 if (Access)
347 ConsumeToken();
348
349 // Parse the 'virtual' keyword (again!), in case it came after the
350 // access specifier.
351 if (Tok.is(tok::kw_virtual)) {
352 SourceLocation VirtualLoc = ConsumeToken();
353 if (IsVirtual) {
354 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000355 Diag(VirtualLoc, diag::err_dup_virtual)
356 << SourceRange(VirtualLoc, VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000357 }
358
359 IsVirtual = true;
360 }
361
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000362 // Parse optional '::' and optional nested-name-specifier.
363 CXXScopeSpec SS;
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +0000364 MaybeParseCXXScopeSpecifier(SS);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000365
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000366 // The location of the base class itself.
367 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000368
369 // Parse the class-name.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000370 TypeTy *BaseType = ParseClassName(&SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000371 if (!BaseType)
372 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000373
374 // Find the complete source range for the base-specifier.
375 SourceRange Range(StartLoc, BaseLoc);
376
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000377 // Notify semantic analysis that we have parsed a complete
378 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000379 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
380 BaseType, BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000381}
382
383/// getAccessSpecifierIfPresent - Determine whether the next token is
384/// a C++ access-specifier.
385///
386/// access-specifier: [C++ class.derived]
387/// 'private'
388/// 'protected'
389/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000390AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000391{
392 switch (Tok.getKind()) {
393 default: return AS_none;
394 case tok::kw_private: return AS_private;
395 case tok::kw_protected: return AS_protected;
396 case tok::kw_public: return AS_public;
397 }
398}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000399
400/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
401///
402/// member-declaration:
403/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
404/// function-definition ';'[opt]
405/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
406/// using-declaration [TODO]
407/// [C++0x] static_assert-declaration [TODO]
408/// template-declaration [TODO]
409///
410/// member-declarator-list:
411/// member-declarator
412/// member-declarator-list ',' member-declarator
413///
414/// member-declarator:
415/// declarator pure-specifier[opt]
416/// declarator constant-initializer[opt]
417/// identifier[opt] ':' constant-expression
418///
419/// pure-specifier: [TODO]
420/// '= 0'
421///
422/// constant-initializer:
423/// '=' constant-expression
424///
425Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
426 SourceLocation DSStart = Tok.getLocation();
427 // decl-specifier-seq:
428 // Parse the common declaration-specifiers piece.
429 DeclSpec DS;
430 ParseDeclarationSpecifiers(DS);
431
432 if (Tok.is(tok::semi)) {
433 ConsumeToken();
434 // C++ 9.2p7: The member-declarator-list can be omitted only after a
435 // class-specifier or an enum-specifier or in a friend declaration.
436 // FIXME: Friend declarations.
437 switch (DS.getTypeSpecType()) {
438 case DeclSpec::TST_struct:
439 case DeclSpec::TST_union:
440 case DeclSpec::TST_class:
441 case DeclSpec::TST_enum:
442 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
443 default:
444 Diag(DSStart, diag::err_no_declarators);
445 return 0;
446 }
447 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000448
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000449 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000450
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000451 if (Tok.isNot(tok::colon)) {
452 // Parse the first declarator.
453 ParseDeclarator(DeclaratorInfo);
454 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +0000455 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000456 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000457 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000458 if (Tok.is(tok::semi))
459 ConsumeToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000460 return 0;
461 }
462
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000463 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000464 if (Tok.is(tok::l_brace)
465 || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000466 if (!DeclaratorInfo.isFunctionDeclarator()) {
467 Diag(Tok, diag::err_func_def_no_params);
468 ConsumeBrace();
469 SkipUntil(tok::r_brace, true);
470 return 0;
471 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000472
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000473 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
474 Diag(Tok, diag::err_function_declared_typedef);
475 // This recovery skips the entire function body. It would be nice
476 // to simply call ParseCXXInlineMethodDef() below, however Sema
477 // assumes the declarator represents a function, not a typedef.
478 ConsumeBrace();
479 SkipUntil(tok::r_brace, true);
480 return 0;
481 }
482
483 return ParseCXXInlineMethodDef(AS, DeclaratorInfo);
484 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000485 }
486
487 // member-declarator-list:
488 // member-declarator
489 // member-declarator-list ',' member-declarator
490
491 DeclTy *LastDeclInGroup = 0;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000492 ExprOwner BitfieldSize(Actions);
493 ExprOwner Init(Actions);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000494
495 while (1) {
496
497 // member-declarator:
498 // declarator pure-specifier[opt]
499 // declarator constant-initializer[opt]
500 // identifier[opt] ':' constant-expression
501
502 if (Tok.is(tok::colon)) {
503 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000504 BitfieldSize = ParseConstantExpression();
505 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000506 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000507 }
508
509 // pure-specifier:
510 // '= 0'
511 //
512 // constant-initializer:
513 // '=' constant-expression
514
515 if (Tok.is(tok::equal)) {
516 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000517 Init = ParseInitializer();
518 if (Init.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000519 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000520 }
521
522 // If attributes exist after the declarator, parse them.
523 if (Tok.is(tok::kw___attribute))
524 DeclaratorInfo.AddAttributes(ParseAttributes());
525
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000526 // NOTE: If Sema is the Action module and declarator is an instance field,
527 // this call will *not* return the created decl; LastDeclInGroup will be
528 // returned instead.
529 // See Sema::ActOnCXXMemberDeclarator for details.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000530 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
531 DeclaratorInfo,
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000532 BitfieldSize.move(),
533 Init.move(),
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000534 LastDeclInGroup);
535
536 // If we don't have a comma, it is either the end of the list (a ';')
537 // or an error, bail out.
538 if (Tok.isNot(tok::comma))
539 break;
540
541 // Consume the comma.
542 ConsumeToken();
543
544 // Parse the next declarator.
545 DeclaratorInfo.clear();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000546 BitfieldSize.reset();
547 Init.reset();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000548
549 // Attributes are only allowed on the second declarator.
550 if (Tok.is(tok::kw___attribute))
551 DeclaratorInfo.AddAttributes(ParseAttributes());
552
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000553 if (Tok.isNot(tok::colon))
554 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000555 }
556
557 if (Tok.is(tok::semi)) {
558 ConsumeToken();
559 // Reverse the chain list.
560 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
561 }
562
563 Diag(Tok, diag::err_expected_semi_decl_list);
564 // Skip to end of block or statement
565 SkipUntil(tok::r_brace, true, true);
566 if (Tok.is(tok::semi))
567 ConsumeToken();
568 return 0;
569}
570
571/// ParseCXXMemberSpecification - Parse the class definition.
572///
573/// member-specification:
574/// member-declaration member-specification[opt]
575/// access-specifier ':' member-specification[opt]
576///
577void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
578 unsigned TagType, DeclTy *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000579 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000580 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000581 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000582
583 SourceLocation LBraceLoc = ConsumeBrace();
584
585 if (!CurScope->isCXXClassScope() && // Not about to define a nested class.
586 CurScope->isInCXXInlineMethodScope()) {
587 // We will define a local class of an inline method.
588 // Push a new LexedMethodsForTopClass for its inline methods.
589 PushTopClassStack();
590 }
591
592 // Enter a scope for the class.
593 EnterScope(Scope::CXXClassScope|Scope::DeclScope);
594
595 Actions.ActOnStartCXXClassDef(CurScope, TagDecl, LBraceLoc);
596
597 // C++ 11p3: Members of a class defined with the keyword class are private
598 // by default. Members of a class defined with the keywords struct or union
599 // are public by default.
600 AccessSpecifier CurAS;
601 if (TagType == DeclSpec::TST_class)
602 CurAS = AS_private;
603 else
604 CurAS = AS_public;
605
606 // While we still have something to read, read the member-declarations.
607 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
608 // Each iteration of this loop reads one member-declaration.
609
610 // Check for extraneous top-level semicolon.
611 if (Tok.is(tok::semi)) {
612 Diag(Tok, diag::ext_extra_struct_semi);
613 ConsumeToken();
614 continue;
615 }
616
617 AccessSpecifier AS = getAccessSpecifierIfPresent();
618 if (AS != AS_none) {
619 // Current token is a C++ access specifier.
620 CurAS = AS;
621 ConsumeToken();
622 ExpectAndConsume(tok::colon, diag::err_expected_colon);
623 continue;
624 }
625
626 // Parse all the comma separated declarators.
627 ParseCXXClassMemberDeclaration(CurAS);
628 }
629
630 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
631
632 AttributeList *AttrList = 0;
633 // If attributes exist after class contents, parse them.
634 if (Tok.is(tok::kw___attribute))
635 AttrList = ParseAttributes(); // FIXME: where should I put them?
636
637 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
638 LBraceLoc, RBraceLoc);
639
640 // C++ 9.2p2: Within the class member-specification, the class is regarded as
641 // complete within function bodies, default arguments,
642 // exception-specifications, and constructor ctor-initializers (including
643 // such things in nested classes).
644 //
645 // FIXME: Only function bodies are parsed correctly, fix the rest.
646 if (!CurScope->getParent()->isCXXClassScope()) {
647 // We are not inside a nested class. This class and its nested classes
648 // are complete and we can parse the lexed inline method definitions.
649 ParseLexedMethodDefs();
650
651 // For a local class of inline method, pop the LexedMethodsForTopClass that
652 // was previously pushed.
653
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000654 assert((CurScope->isInCXXInlineMethodScope() ||
655 TopClassStacks.size() == 1) &&
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000656 "MethodLexers not getting popped properly!");
657 if (CurScope->isInCXXInlineMethodScope())
658 PopTopClassStack();
659 }
660
661 // Leave the class scope.
662 ExitScope();
663
Argyrios Kyrtzidis5b7f0c82008-08-09 00:39:29 +0000664 Actions.ActOnFinishCXXClassDef(TagDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000665}
Douglas Gregor7ad83902008-11-05 04:29:56 +0000666
667/// ParseConstructorInitializer - Parse a C++ constructor initializer,
668/// which explicitly initializes the members or base classes of a
669/// class (C++ [class.base.init]). For example, the three initializers
670/// after the ':' in the Derived constructor below:
671///
672/// @code
673/// class Base { };
674/// class Derived : Base {
675/// int x;
676/// float f;
677/// public:
678/// Derived(float f) : Base(), x(17), f(f) { }
679/// };
680/// @endcode
681///
682/// [C++] ctor-initializer:
683/// ':' mem-initializer-list
684///
685/// [C++] mem-initializer-list:
686/// mem-initializer
687/// mem-initializer , mem-initializer-list
688void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) {
689 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
690
691 SourceLocation ColonLoc = ConsumeToken();
692
693 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
694
695 do {
696 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
697 if (!MemInit.isInvalid)
698 MemInitializers.push_back(MemInit.Val);
699
700 if (Tok.is(tok::comma))
701 ConsumeToken();
702 else if (Tok.is(tok::l_brace))
703 break;
704 else {
705 // Skip over garbage, until we get to '{'. Don't eat the '{'.
706 SkipUntil(tok::l_brace, true, true);
707 break;
708 }
709 } while (true);
710
711 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
712 &MemInitializers[0], MemInitializers.size());
713}
714
715/// ParseMemInitializer - Parse a C++ member initializer, which is
716/// part of a constructor initializer that explicitly initializes one
717/// member or base class (C++ [class.base.init]). See
718/// ParseConstructorInitializer for an example.
719///
720/// [C++] mem-initializer:
721/// mem-initializer-id '(' expression-list[opt] ')'
722///
723/// [C++] mem-initializer-id:
724/// '::'[opt] nested-name-specifier[opt] class-name
725/// identifier
726Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) {
727 // FIXME: parse '::'[opt] nested-name-specifier[opt]
728
729 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000730 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000731 return true;
732 }
733
734 // Get the identifier. This may be a member name or a class name,
735 // but we'll let the semantic analysis determine which it is.
736 IdentifierInfo *II = Tok.getIdentifierInfo();
737 SourceLocation IdLoc = ConsumeToken();
738
739 // Parse the '('.
740 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000741 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000742 return true;
743 }
744 SourceLocation LParenLoc = ConsumeParen();
745
746 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000747 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000748 CommaLocsTy CommaLocs;
749 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
750 SkipUntil(tok::r_paren);
751 return true;
752 }
753
754 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
755
Sebastian Redla55e52c2008-11-25 22:21:31 +0000756 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
757 LParenLoc, ArgExprs.take(),
758 ArgExprs.size(), &CommaLocs[0], RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000759}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +0000760
761/// ParseExceptionSpecification - Parse a C++ exception-specification
762/// (C++ [except.spec]).
763///
Douglas Gregora4745612008-12-01 18:00:20 +0000764/// exception-specification:
765/// 'throw' '(' type-id-list [opt] ')'
766/// [MS] 'throw' '(' '...' ')'
Douglas Gregor0fe7bea2008-11-25 03:22:00 +0000767///
Douglas Gregora4745612008-12-01 18:00:20 +0000768/// type-id-list:
769/// type-id
770/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +0000771///
772bool Parser::ParseExceptionSpecification() {
773 assert(Tok.is(tok::kw_throw) && "expected throw");
774
775 SourceLocation ThrowLoc = ConsumeToken();
776
777 if (!Tok.is(tok::l_paren)) {
778 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
779 }
780 SourceLocation LParenLoc = ConsumeParen();
781
Douglas Gregora4745612008-12-01 18:00:20 +0000782 // Parse throw(...), a Microsoft extension that means "this function
783 // can throw anything".
784 if (Tok.is(tok::ellipsis)) {
785 SourceLocation EllipsisLoc = ConsumeToken();
786 if (!getLang().Microsoft)
787 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
788 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
789 return false;
790 }
791
Douglas Gregor0fe7bea2008-11-25 03:22:00 +0000792 // Parse the sequence of type-ids.
793 while (Tok.isNot(tok::r_paren)) {
794 ParseTypeName();
795 if (Tok.is(tok::comma))
796 ConsumeToken();
797 else
798 break;
799 }
800
801 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
802 return false;
803}