blob: c6a373dc5c4d835751b469bd6009bb5faa83d654 [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///
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;
52
Chris Lattner04d66662007-10-09 17:33:22 +000053 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000054 Ident = Tok.getIdentifierInfo();
55 IdentLoc = ConsumeToken(); // eat the identifier.
56 }
57
58 // Read label attributes, if present.
Chris Lattnerb28317a2009-03-28 19:18:32 +000059 Action::AttrTy *AttrList = 0;
Chris Lattner04d66662007-10-09 17:33:22 +000060 if (Tok.is(tok::kw___attribute))
Chris Lattner8f08cb72007-08-25 06:57:03 +000061 // FIXME: save these somewhere.
62 AttrList = ParseAttributes();
63
Anders Carlssonf67606a2009-03-28 04:07:16 +000064 if (Tok.is(tok::equal))
Chris Lattner8f08cb72007-08-25 06:57:03 +000065 // FIXME: Verify no attributes were present.
Chris Lattner97144fc2009-04-02 04:16:50 +000066 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Anders Carlssonf67606a2009-03-28 04:07:16 +000067
Chris Lattner51448322009-03-29 14:02:43 +000068 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +000069 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000070 diag::err_expected_ident_lbrace);
71 return DeclPtrTy();
Chris Lattner8f08cb72007-08-25 06:57:03 +000072 }
73
Chris Lattner51448322009-03-29 14:02:43 +000074 SourceLocation LBrace = ConsumeBrace();
75
76 // Enter a scope for the namespace.
77 ParseScope NamespaceScope(this, Scope::DeclScope);
78
79 DeclPtrTy NamespcDecl =
80 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
81
82 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
83 PP.getSourceManager(),
84 "parsing namespace");
85
86 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
87 ParseExternalDeclaration();
88
89 // Leave the namespace scope.
90 NamespaceScope.Exit();
91
Chris Lattner97144fc2009-04-02 04:16:50 +000092 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
93 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +000094
Chris Lattner97144fc2009-04-02 04:16:50 +000095 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +000096 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +000097}
Chris Lattnerc6fdc342008-01-12 07:05:38 +000098
Anders Carlssonf67606a2009-03-28 04:07:16 +000099/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
100/// alias definition.
101///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000102Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
103 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000104 IdentifierInfo *Alias,
105 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000106 assert(Tok.is(tok::equal) && "Not equal token");
107
108 ConsumeToken(); // eat the '='.
109
110 CXXScopeSpec SS;
111 // Parse (optional) nested-name-specifier.
112 ParseOptionalCXXScopeSpecifier(SS);
113
114 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
115 Diag(Tok, diag::err_expected_namespace_name);
116 // Skip to end of the definition and eat the ';'.
117 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000118 return DeclPtrTy();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000119 }
120
121 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000122 IdentifierInfo *Ident = Tok.getIdentifierInfo();
123 SourceLocation IdentLoc = ConsumeToken();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000124
125 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000126 DeclEnd = Tok.getLocation();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000127 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
128 "namespace name", tok::semi);
129
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000130 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
131 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000132}
133
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000134/// ParseLinkage - We know that the current token is a string_literal
135/// and just before that, that extern was seen.
136///
137/// linkage-specification: [C++ 7.5p2: dcl.link]
138/// 'extern' string-literal '{' declaration-seq[opt] '}'
139/// 'extern' string-literal declaration
140///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000141Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000142 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000143 llvm::SmallVector<char, 8> LangBuffer;
144 // LangBuffer is guaranteed to be big enough.
145 LangBuffer.resize(Tok.getLength());
146 const char *LangBufPtr = &LangBuffer[0];
147 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
148
149 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000150
Douglas Gregor074149e2009-01-05 19:45:36 +0000151 ParseScope LinkageScope(this, Scope::DeclScope);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000152 DeclPtrTy LinkageSpec
Douglas Gregor074149e2009-01-05 19:45:36 +0000153 = Actions.ActOnStartLinkageSpecification(CurScope,
154 /*FIXME: */SourceLocation(),
155 Loc, LangBufPtr, StrSize,
156 Tok.is(tok::l_brace)? Tok.getLocation()
157 : SourceLocation());
158
159 if (Tok.isNot(tok::l_brace)) {
160 ParseDeclarationOrFunctionDefinition();
161 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
162 SourceLocation());
Douglas Gregorf44515a2008-12-16 22:23:02 +0000163 }
164
165 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000166 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000167 ParseExternalDeclaration();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000168 }
169
Douglas Gregorf44515a2008-12-16 22:23:02 +0000170 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000171 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000172}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000173
Douglas Gregorf780abc2008-12-30 03:27:21 +0000174/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
175/// using-directive. Assumes that current token is 'using'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000176Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
177 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000178 assert(Tok.is(tok::kw_using) && "Not using token");
179
180 // Eat 'using'.
181 SourceLocation UsingLoc = ConsumeToken();
182
Chris Lattner2f274772009-01-06 06:55:51 +0000183 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000184 // Next token after 'using' is 'namespace' so it must be using-directive
Chris Lattner97144fc2009-04-02 04:16:50 +0000185 return ParseUsingDirective(Context, UsingLoc, DeclEnd);
Chris Lattner2f274772009-01-06 06:55:51 +0000186
187 // Otherwise, it must be using-declaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000188 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000189}
190
191/// ParseUsingDirective - Parse C++ using-directive, assumes
192/// that current token is 'namespace' and 'using' was already parsed.
193///
194/// using-directive: [C++ 7.3.p4: namespace.udir]
195/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
196/// namespace-name ;
197/// [GNU] using-directive:
198/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
199/// namespace-name attributes[opt] ;
200///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000201Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000202 SourceLocation UsingLoc,
203 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000204 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
205
206 // Eat 'namespace'.
207 SourceLocation NamespcLoc = ConsumeToken();
208
209 CXXScopeSpec SS;
210 // Parse (optional) nested-name-specifier.
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000211 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000212
213 AttributeList *AttrList = 0;
214 IdentifierInfo *NamespcName = 0;
215 SourceLocation IdentLoc = SourceLocation();
216
217 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000218 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000219 Diag(Tok, diag::err_expected_namespace_name);
220 // If there was invalid namespace name, skip to end of decl, and eat ';'.
221 SkipUntil(tok::semi);
222 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattnerb28317a2009-03-28 19:18:32 +0000223 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000224 }
Chris Lattner823c44e2009-01-06 07:27:21 +0000225
226 // Parse identifier.
227 NamespcName = Tok.getIdentifierInfo();
228 IdentLoc = ConsumeToken();
229
230 // Parse (optional) attributes (most likely GNU strong-using extension).
231 if (Tok.is(tok::kw___attribute))
232 AttrList = ParseAttributes();
233
234 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000235 DeclEnd = Tok.getLocation();
Chris Lattner823c44e2009-01-06 07:27:21 +0000236 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
237 AttrList ? "attributes list" : "namespace name", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000238
239 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner823c44e2009-01-06 07:27:21 +0000240 IdentLoc, NamespcName, AttrList);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000241}
242
243/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
244/// 'using' was already seen.
245///
246/// using-declaration: [C++ 7.3.p3: namespace.udecl]
247/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
248/// unqualified-id [TODO]
249/// 'using' :: unqualified-id [TODO]
250///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000251Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000252 SourceLocation UsingLoc,
253 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000254 assert(false && "Not implemented");
255 // FIXME: Implement parsing.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000256 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000257}
258
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000259/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
260///
261/// static_assert-declaration:
262/// static_assert ( constant-expression , string-literal ) ;
263///
Chris Lattner97144fc2009-04-02 04:16:50 +0000264Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000265 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
266 SourceLocation StaticAssertLoc = ConsumeToken();
267
268 if (Tok.isNot(tok::l_paren)) {
269 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000270 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000271 }
272
273 SourceLocation LParenLoc = ConsumeParen();
274
275 OwningExprResult AssertExpr(ParseConstantExpression());
276 if (AssertExpr.isInvalid()) {
277 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000278 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000279 }
280
Anders Carlssonad5f9602009-03-13 23:29:20 +0000281 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000282 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000283
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000284 if (Tok.isNot(tok::string_literal)) {
285 Diag(Tok, diag::err_expected_string_literal);
286 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000287 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000288 }
289
290 OwningExprResult AssertMessage(ParseStringLiteralExpression());
291 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000292 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000293
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000294 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000295
Chris Lattner97144fc2009-04-02 04:16:50 +0000296 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000297 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
298
Anders Carlssonad5f9602009-03-13 23:29:20 +0000299 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000300 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000301}
302
Douglas Gregor42a552f2008-11-05 20:51:48 +0000303/// ParseClassName - Parse a C++ class-name, which names a class. Note
304/// that we only check that the result names a type; semantic analysis
305/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000306/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000307/// found.
308///
309/// class-name: [C++ 9.1]
310/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000311/// simple-template-id
Douglas Gregor42a552f2008-11-05 20:51:48 +0000312///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000313Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
314 const CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000315 // Check whether we have a template-id that names a type.
316 if (Tok.is(tok::annot_template_id)) {
317 TemplateIdAnnotation *TemplateId
318 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000319 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000320 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000321
322 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
323 TypeTy *Type = Tok.getAnnotationValue();
324 EndLocation = Tok.getAnnotationEndLoc();
325 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000326
327 if (Type)
328 return Type;
329 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000330 }
331
332 // Fall through to produce an error below.
333 }
334
Douglas Gregor42a552f2008-11-05 20:51:48 +0000335 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000336 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000337 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000338 }
339
340 // We have an identifier; check whether it is actually a type.
Douglas Gregorb696ea32009-02-04 17:00:24 +0000341 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
342 Tok.getLocation(), CurScope, SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000343 if (!Type) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000344 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000345 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000346 }
347
348 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000349 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000350 return Type;
351}
352
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000353/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
354/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
355/// until we reach the start of a definition or see a token that
356/// cannot start a definition.
357///
358/// class-specifier: [C++ class]
359/// class-head '{' member-specification[opt] '}'
360/// class-head '{' member-specification[opt] '}' attributes[opt]
361/// class-head:
362/// class-key identifier[opt] base-clause[opt]
363/// class-key nested-name-specifier identifier base-clause[opt]
364/// class-key nested-name-specifier[opt] simple-template-id
365/// base-clause[opt]
366/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
367/// [GNU] class-key attributes[opt] nested-name-specifier
368/// identifier base-clause[opt]
369/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
370/// simple-template-id base-clause[opt]
371/// class-key:
372/// 'class'
373/// 'struct'
374/// 'union'
375///
376/// elaborated-type-specifier: [C++ dcl.type.elab]
377/// class-key ::[opt] nested-name-specifier[opt] identifier
378/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
379/// simple-template-id
380///
381/// Note that the C++ class-specifier and elaborated-type-specifier,
382/// together, subsume the C99 struct-or-union-specifier:
383///
384/// struct-or-union-specifier: [C99 6.7.2.1]
385/// struct-or-union identifier[opt] '{' struct-contents '}'
386/// struct-or-union identifier
387/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
388/// '}' attributes[opt]
389/// [GNU] struct-or-union attributes[opt] identifier
390/// struct-or-union:
391/// 'struct'
392/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000393void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
394 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000395 TemplateParameterLists *TemplateParams,
396 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000397 DeclSpec::TST TagType;
398 if (TagTokKind == tok::kw_struct)
399 TagType = DeclSpec::TST_struct;
400 else if (TagTokKind == tok::kw_class)
401 TagType = DeclSpec::TST_class;
402 else {
403 assert(TagTokKind == tok::kw_union && "Not a class specifier");
404 TagType = DeclSpec::TST_union;
405 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000406
407 AttributeList *Attr = 0;
408 // If attributes exist after tag, parse them.
409 if (Tok.is(tok::kw___attribute))
410 Attr = ParseAttributes();
411
Steve Narofff59e17e2008-12-24 20:59:21 +0000412 // If declspecs exist after tag, parse them.
413 if (Tok.is(tok::kw___declspec) && PP.getLangOptions().Microsoft)
414 FuzzyParseMicrosoftDeclSpec();
415
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000416 // Parse the (optional) nested-name-specifier.
417 CXXScopeSpec SS;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000418 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS))
419 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000420 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000421
422 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000423 IdentifierInfo *Name = 0;
424 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000425 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000426 if (Tok.is(tok::identifier)) {
427 Name = Tok.getIdentifierInfo();
428 NameLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000429 } else if (Tok.is(tok::annot_template_id)) {
430 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
431 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000432
Douglas Gregorc45c2322009-03-31 00:43:58 +0000433 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000434 // The template-name in the simple-template-id refers to
435 // something other than a class template. Give an appropriate
436 // error message and skip to the ';'.
437 SourceRange Range(NameLoc);
438 if (SS.isNotEmpty())
439 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000440
Douglas Gregor39a8de12009-02-25 19:37:18 +0000441 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
442 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +0000443
Douglas Gregor39a8de12009-02-25 19:37:18 +0000444 DS.SetTypeSpecError();
445 SkipUntil(tok::semi, false, true);
446 TemplateId->Destroy();
447 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000448 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000449 }
450
451 // There are three options here. If we have 'struct foo;', then
452 // this is a forward declaration. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000453 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000454 // something like 'struct foo xyz', a reference.
455 Action::TagKind TK;
456 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
457 TK = Action::TK_Definition;
458 else if (Tok.is(tok::semi))
459 TK = Action::TK_Declaration;
460 else
461 TK = Action::TK_Reference;
462
Douglas Gregor39a8de12009-02-25 19:37:18 +0000463 if (!Name && !TemplateId && TK != Action::TK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000464 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000465 Diag(StartLoc, diag::err_anon_type_definition)
466 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000467
468 // Skip the rest of this declarator, up until the comma or semicolon.
469 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000470
471 if (TemplateId)
472 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000473 return;
474 }
475
Douglas Gregorddc29e12009-02-06 22:42:48 +0000476 // Create the tag portion of the class or class template.
Douglas Gregor212e81c2009-03-25 00:13:59 +0000477 Action::DeclResult TagOrTempResult;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000478 if (TemplateId && TK != Action::TK_Reference) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000479 // Explicit specialization or class template partial
480 // specialization. Let semantic analysis decide.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000481 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
482 TemplateId->getTemplateArgs(),
483 TemplateId->getTemplateArgIsType(),
484 TemplateId->NumArgs);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000485 TagOrTempResult
Douglas Gregorcc636682009-02-17 23:15:12 +0000486 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000487 StartLoc, SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000488 TemplateTy::make(TemplateId->Template),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000489 TemplateId->TemplateNameLoc,
490 TemplateId->LAngleLoc,
491 TemplateArgsPtr,
492 TemplateId->getTemplateArgLocations(),
493 TemplateId->RAngleLoc,
494 Attr,
Douglas Gregorcc636682009-02-17 23:15:12 +0000495 Action::MultiTemplateParamsArg(Actions,
496 TemplateParams? &(*TemplateParams)[0] : 0,
497 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor39a8de12009-02-25 19:37:18 +0000498 TemplateId->Destroy();
499 } else if (TemplateParams && TK != Action::TK_Reference)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000500 TagOrTempResult = Actions.ActOnClassTemplate(CurScope, TagType, TK,
501 StartLoc, SS, Name, NameLoc,
502 Attr,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000503 Action::MultiTemplateParamsArg(Actions,
504 &(*TemplateParams)[0],
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000505 TemplateParams->size()),
506 AS);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000507 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000508 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000509 NameLoc, Attr, AS);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000510
511 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000512 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000513 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000514
515 // If there is a body, parse it and inform the actions module.
516 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000517 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000518 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000519 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000520 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000521 else if (TK == Action::TK_Definition) {
522 // FIXME: Complain that we have a base-specifier list but no
523 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000524 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000525 }
526
527 const char *PrevSpec = 0;
Douglas Gregor212e81c2009-03-25 00:13:59 +0000528 if (TagOrTempResult.isInvalid())
Douglas Gregorddc29e12009-02-06 22:42:48 +0000529 DS.SetTypeSpecError();
Douglas Gregor212e81c2009-03-25 00:13:59 +0000530 else if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000531 TagOrTempResult.get().getAs<void>()))
Chris Lattner1ab3b962008-11-18 07:48:38 +0000532 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000533}
534
535/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
536///
537/// base-clause : [C++ class.derived]
538/// ':' base-specifier-list
539/// base-specifier-list:
540/// base-specifier '...'[opt]
541/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000542void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000543 assert(Tok.is(tok::colon) && "Not a base clause");
544 ConsumeToken();
545
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000546 // Build up an array of parsed base specifiers.
547 llvm::SmallVector<BaseTy *, 8> BaseInfo;
548
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000549 while (true) {
550 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000551 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000552 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000553 // Skip the rest of this base specifier, up until the comma or
554 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000555 SkipUntil(tok::comma, tok::l_brace, true, true);
556 } else {
557 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000558 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000559 }
560
561 // If the next token is a comma, consume it and keep reading
562 // base-specifiers.
563 if (Tok.isNot(tok::comma)) break;
564
565 // Consume the comma.
566 ConsumeToken();
567 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000568
569 // Attach the base specifiers
570 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000571}
572
573/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
574/// one entry in the base class list of a class specifier, for example:
575/// class foo : public bar, virtual private baz {
576/// 'public bar' and 'virtual private baz' are each base-specifiers.
577///
578/// base-specifier: [C++ class.derived]
579/// ::[opt] nested-name-specifier[opt] class-name
580/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
581/// class-name
582/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
583/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000584Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000585 bool IsVirtual = false;
586 SourceLocation StartLoc = Tok.getLocation();
587
588 // Parse the 'virtual' keyword.
589 if (Tok.is(tok::kw_virtual)) {
590 ConsumeToken();
591 IsVirtual = true;
592 }
593
594 // Parse an (optional) access specifier.
595 AccessSpecifier Access = getAccessSpecifierIfPresent();
596 if (Access)
597 ConsumeToken();
598
599 // Parse the 'virtual' keyword (again!), in case it came after the
600 // access specifier.
601 if (Tok.is(tok::kw_virtual)) {
602 SourceLocation VirtualLoc = ConsumeToken();
603 if (IsVirtual) {
604 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000605 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000606 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000607 }
608
609 IsVirtual = true;
610 }
611
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000612 // Parse optional '::' and optional nested-name-specifier.
613 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000614 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000615
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000616 // The location of the base class itself.
617 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000618
619 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000620 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000621 TypeResult BaseType = ParseClassName(EndLocation, &SS);
622 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000623 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000624
625 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000626 SourceRange Range(StartLoc, EndLocation);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000627
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000628 // Notify semantic analysis that we have parsed a complete
629 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000630 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000631 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000632}
633
634/// getAccessSpecifierIfPresent - Determine whether the next token is
635/// a C++ access-specifier.
636///
637/// access-specifier: [C++ class.derived]
638/// 'private'
639/// 'protected'
640/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000641AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000642{
643 switch (Tok.getKind()) {
644 default: return AS_none;
645 case tok::kw_private: return AS_private;
646 case tok::kw_protected: return AS_protected;
647 case tok::kw_public: return AS_public;
648 }
649}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000650
651/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
652///
653/// member-declaration:
654/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
655/// function-definition ';'[opt]
656/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
657/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000658/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000659/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000660/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000661///
662/// member-declarator-list:
663/// member-declarator
664/// member-declarator-list ',' member-declarator
665///
666/// member-declarator:
667/// declarator pure-specifier[opt]
668/// declarator constant-initializer[opt]
669/// identifier[opt] ':' constant-expression
670///
Sebastian Redle2b68332009-04-12 17:16:29 +0000671/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000672/// '= 0'
673///
674/// constant-initializer:
675/// '=' constant-expression
676///
Chris Lattner682bf922009-03-29 16:50:03 +0000677void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000678 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +0000679 if (Tok.is(tok::kw_static_assert)) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000680 SourceLocation DeclEnd;
681 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000682 return;
683 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000684
Chris Lattner682bf922009-03-29 16:50:03 +0000685 if (Tok.is(tok::kw_template)) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000686 SourceLocation DeclEnd;
687 ParseTemplateDeclarationOrSpecialization(Declarator::MemberContext, DeclEnd,
688 AS);
Chris Lattner682bf922009-03-29 16:50:03 +0000689 return;
690 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000691
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000692 // Handle: member-declaration ::= '__extension__' member-declaration
693 if (Tok.is(tok::kw___extension__)) {
694 // __extension__ silences extension warnings in the subexpression.
695 ExtensionRAIIObject O(Diags); // Use RAII to do this.
696 ConsumeToken();
697 return ParseCXXClassMemberDeclaration(AS);
698 }
699
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000700 SourceLocation DSStart = Tok.getLocation();
701 // decl-specifier-seq:
702 // Parse the common declaration-specifiers piece.
703 DeclSpec DS;
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000704 ParseDeclarationSpecifiers(DS, 0, AS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000705
706 if (Tok.is(tok::semi)) {
707 ConsumeToken();
708 // C++ 9.2p7: The member-declarator-list can be omitted only after a
709 // class-specifier or an enum-specifier or in a friend declaration.
710 // FIXME: Friend declarations.
711 switch (DS.getTypeSpecType()) {
Chris Lattner682bf922009-03-29 16:50:03 +0000712 case DeclSpec::TST_struct:
713 case DeclSpec::TST_union:
714 case DeclSpec::TST_class:
715 case DeclSpec::TST_enum:
716 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
717 return;
718 default:
719 Diag(DSStart, diag::err_no_declarators);
720 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000721 }
722 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000723
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000724 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000725
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000726 if (Tok.isNot(tok::colon)) {
727 // Parse the first declarator.
728 ParseDeclarator(DeclaratorInfo);
729 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +0000730 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000731 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000732 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000733 if (Tok.is(tok::semi))
734 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000735 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000736 }
737
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000738 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000739 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +0000740 || (DeclaratorInfo.isFunctionDeclarator() &&
741 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000742 if (!DeclaratorInfo.isFunctionDeclarator()) {
743 Diag(Tok, diag::err_func_def_no_params);
744 ConsumeBrace();
745 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000746 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000747 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000748
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000749 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
750 Diag(Tok, diag::err_function_declared_typedef);
751 // This recovery skips the entire function body. It would be nice
752 // to simply call ParseCXXInlineMethodDef() below, however Sema
753 // assumes the declarator represents a function, not a typedef.
754 ConsumeBrace();
755 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000756 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000757 }
758
Chris Lattner682bf922009-03-29 16:50:03 +0000759 ParseCXXInlineMethodDef(AS, DeclaratorInfo);
760 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000761 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000762 }
763
764 // member-declarator-list:
765 // member-declarator
766 // member-declarator-list ',' member-declarator
767
Chris Lattner682bf922009-03-29 16:50:03 +0000768 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000769 OwningExprResult BitfieldSize(Actions);
770 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +0000771 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000772
773 while (1) {
774
775 // member-declarator:
776 // declarator pure-specifier[opt]
777 // declarator constant-initializer[opt]
778 // identifier[opt] ':' constant-expression
779
780 if (Tok.is(tok::colon)) {
781 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000782 BitfieldSize = ParseConstantExpression();
783 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000784 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000785 }
786
787 // pure-specifier:
788 // '= 0'
789 //
790 // constant-initializer:
791 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +0000792 //
793 // defaulted/deleted function-definition:
794 // '=' 'default' [TODO]
795 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000796
797 if (Tok.is(tok::equal)) {
798 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +0000799 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
800 ConsumeToken();
801 Deleted = true;
802 } else {
803 Init = ParseInitializer();
804 if (Init.isInvalid())
805 SkipUntil(tok::comma, true, true);
806 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000807 }
808
809 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000810 if (Tok.is(tok::kw___attribute)) {
811 SourceLocation Loc;
812 AttributeList *AttrList = ParseAttributes(&Loc);
813 DeclaratorInfo.AddAttributes(AttrList, Loc);
814 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000815
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000816 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +0000817 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000818 // See Sema::ActOnCXXMemberDeclarator for details.
Chris Lattner682bf922009-03-29 16:50:03 +0000819 DeclPtrTy ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
820 DeclaratorInfo,
821 BitfieldSize.release(),
Sebastian Redle2b68332009-04-12 17:16:29 +0000822 Init.release(),
823 Deleted);
Chris Lattner682bf922009-03-29 16:50:03 +0000824 if (ThisDecl)
825 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000826
Douglas Gregor72b505b2008-12-16 21:30:33 +0000827 if (DeclaratorInfo.isFunctionDeclarator() &&
828 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
829 != DeclSpec::SCS_typedef) {
830 // We just declared a member function. If this member function
831 // has any default arguments, we'll need to parse them later.
832 LateParsedMethodDeclaration *LateMethod = 0;
833 DeclaratorChunk::FunctionTypeInfo &FTI
834 = DeclaratorInfo.getTypeObject(0).Fun;
835 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
836 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
837 if (!LateMethod) {
838 // Push this method onto the stack of late-parsed method
839 // declarations.
840 getCurTopClassStack().MethodDecls.push_back(
Chris Lattner682bf922009-03-29 16:50:03 +0000841 LateParsedMethodDeclaration(ThisDecl));
Douglas Gregor72b505b2008-12-16 21:30:33 +0000842 LateMethod = &getCurTopClassStack().MethodDecls.back();
843
844 // Add all of the parameters prior to this one (they don't
845 // have default arguments).
846 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
847 for (unsigned I = 0; I < ParamIdx; ++I)
848 LateMethod->DefaultArgs.push_back(
849 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
850 }
851
852 // Add this parameter to the list of parameters (it or may
853 // not have a default argument).
854 LateMethod->DefaultArgs.push_back(
855 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
856 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
857 }
858 }
859 }
860
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000861 // If we don't have a comma, it is either the end of the list (a ';')
862 // or an error, bail out.
863 if (Tok.isNot(tok::comma))
864 break;
865
866 // Consume the comma.
867 ConsumeToken();
868
869 // Parse the next declarator.
870 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000871 BitfieldSize = 0;
872 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +0000873 Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000874
875 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000876 if (Tok.is(tok::kw___attribute)) {
877 SourceLocation Loc;
878 AttributeList *AttrList = ParseAttributes(&Loc);
879 DeclaratorInfo.AddAttributes(AttrList, Loc);
880 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000881
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000882 if (Tok.isNot(tok::colon))
883 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000884 }
885
886 if (Tok.is(tok::semi)) {
887 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000888 Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
889 DeclsInGroup.size());
890 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000891 }
892
893 Diag(Tok, diag::err_expected_semi_decl_list);
894 // Skip to end of block or statement
895 SkipUntil(tok::r_brace, true, true);
896 if (Tok.is(tok::semi))
897 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000898 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000899}
900
901/// ParseCXXMemberSpecification - Parse the class definition.
902///
903/// member-specification:
904/// member-declaration member-specification[opt]
905/// access-specifier ':' member-specification[opt]
906///
907void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000908 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000909 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000910 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000911 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000912
Chris Lattner49f28ca2009-03-05 08:00:35 +0000913 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
914 PP.getSourceManager(),
915 "parsing struct/union/class body");
Chris Lattner27b7f102009-03-05 02:25:03 +0000916
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000917 SourceLocation LBraceLoc = ConsumeBrace();
918
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000919 if (!CurScope->isClassScope() && // Not about to define a nested class.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000920 CurScope->isInCXXInlineMethodScope()) {
921 // We will define a local class of an inline method.
922 // Push a new LexedMethodsForTopClass for its inline methods.
923 PushTopClassStack();
924 }
925
926 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000927 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000928
Douglas Gregorddc29e12009-02-06 22:42:48 +0000929 if (TagDecl)
930 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
931 else {
932 SkipUntil(tok::r_brace, false, false);
933 return;
934 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000935
936 // C++ 11p3: Members of a class defined with the keyword class are private
937 // by default. Members of a class defined with the keywords struct or union
938 // are public by default.
939 AccessSpecifier CurAS;
940 if (TagType == DeclSpec::TST_class)
941 CurAS = AS_private;
942 else
943 CurAS = AS_public;
944
945 // While we still have something to read, read the member-declarations.
946 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
947 // Each iteration of this loop reads one member-declaration.
948
949 // Check for extraneous top-level semicolon.
950 if (Tok.is(tok::semi)) {
951 Diag(Tok, diag::ext_extra_struct_semi);
952 ConsumeToken();
953 continue;
954 }
955
956 AccessSpecifier AS = getAccessSpecifierIfPresent();
957 if (AS != AS_none) {
958 // Current token is a C++ access specifier.
959 CurAS = AS;
960 ConsumeToken();
961 ExpectAndConsume(tok::colon, diag::err_expected_colon);
962 continue;
963 }
964
965 // Parse all the comma separated declarators.
966 ParseCXXClassMemberDeclaration(CurAS);
967 }
968
969 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
970
971 AttributeList *AttrList = 0;
972 // If attributes exist after class contents, parse them.
973 if (Tok.is(tok::kw___attribute))
974 AttrList = ParseAttributes(); // FIXME: where should I put them?
975
976 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
977 LBraceLoc, RBraceLoc);
978
979 // C++ 9.2p2: Within the class member-specification, the class is regarded as
980 // complete within function bodies, default arguments,
981 // exception-specifications, and constructor ctor-initializers (including
982 // such things in nested classes).
983 //
Douglas Gregor72b505b2008-12-16 21:30:33 +0000984 // FIXME: Only function bodies and constructor ctor-initializers are
985 // parsed correctly, fix the rest.
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000986 if (!CurScope->getParent()->isClassScope()) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000987 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +0000988 // are complete and we can parse the delayed portions of method
989 // declarations and the lexed inline method definitions.
990 ParseLexedMethodDeclarations();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000991 ParseLexedMethodDefs();
992
993 // For a local class of inline method, pop the LexedMethodsForTopClass that
994 // was previously pushed.
995
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000996 assert((CurScope->isInCXXInlineMethodScope() ||
997 TopClassStacks.size() == 1) &&
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000998 "MethodLexers not getting popped properly!");
999 if (CurScope->isInCXXInlineMethodScope())
1000 PopTopClassStack();
1001 }
1002
1003 // Leave the class scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001004 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001005
Douglas Gregor72de6672009-01-08 20:45:30 +00001006 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001007}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001008
1009/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1010/// which explicitly initializes the members or base classes of a
1011/// class (C++ [class.base.init]). For example, the three initializers
1012/// after the ':' in the Derived constructor below:
1013///
1014/// @code
1015/// class Base { };
1016/// class Derived : Base {
1017/// int x;
1018/// float f;
1019/// public:
1020/// Derived(float f) : Base(), x(17), f(f) { }
1021/// };
1022/// @endcode
1023///
1024/// [C++] ctor-initializer:
1025/// ':' mem-initializer-list
1026///
1027/// [C++] mem-initializer-list:
1028/// mem-initializer
1029/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001030void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001031 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1032
1033 SourceLocation ColonLoc = ConsumeToken();
1034
1035 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
1036
1037 do {
1038 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001039 if (!MemInit.isInvalid())
1040 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001041
1042 if (Tok.is(tok::comma))
1043 ConsumeToken();
1044 else if (Tok.is(tok::l_brace))
1045 break;
1046 else {
1047 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001048 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001049 SkipUntil(tok::l_brace, true, true);
1050 break;
1051 }
1052 } while (true);
1053
1054 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
1055 &MemInitializers[0], MemInitializers.size());
1056}
1057
1058/// ParseMemInitializer - Parse a C++ member initializer, which is
1059/// part of a constructor initializer that explicitly initializes one
1060/// member or base class (C++ [class.base.init]). See
1061/// ParseConstructorInitializer for an example.
1062///
1063/// [C++] mem-initializer:
1064/// mem-initializer-id '(' expression-list[opt] ')'
1065///
1066/// [C++] mem-initializer-id:
1067/// '::'[opt] nested-name-specifier[opt] class-name
1068/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001069Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001070 // FIXME: parse '::'[opt] nested-name-specifier[opt]
1071
1072 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001073 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001074 return true;
1075 }
1076
1077 // Get the identifier. This may be a member name or a class name,
1078 // but we'll let the semantic analysis determine which it is.
1079 IdentifierInfo *II = Tok.getIdentifierInfo();
1080 SourceLocation IdLoc = ConsumeToken();
1081
1082 // Parse the '('.
1083 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001084 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001085 return true;
1086 }
1087 SourceLocation LParenLoc = ConsumeParen();
1088
1089 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001090 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001091 CommaLocsTy CommaLocs;
1092 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1093 SkipUntil(tok::r_paren);
1094 return true;
1095 }
1096
1097 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1098
Sebastian Redla55e52c2008-11-25 22:21:31 +00001099 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
1100 LParenLoc, ArgExprs.take(),
1101 ArgExprs.size(), &CommaLocs[0], RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001102}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001103
1104/// ParseExceptionSpecification - Parse a C++ exception-specification
1105/// (C++ [except.spec]).
1106///
Douglas Gregora4745612008-12-01 18:00:20 +00001107/// exception-specification:
1108/// 'throw' '(' type-id-list [opt] ')'
1109/// [MS] 'throw' '(' '...' ')'
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001110///
Douglas Gregora4745612008-12-01 18:00:20 +00001111/// type-id-list:
1112/// type-id
1113/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001114///
Sebastian Redlab197ba2009-02-09 18:23:29 +00001115bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001116 assert(Tok.is(tok::kw_throw) && "expected throw");
1117
1118 SourceLocation ThrowLoc = ConsumeToken();
1119
1120 if (!Tok.is(tok::l_paren)) {
1121 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1122 }
1123 SourceLocation LParenLoc = ConsumeParen();
1124
Douglas Gregora4745612008-12-01 18:00:20 +00001125 // Parse throw(...), a Microsoft extension that means "this function
1126 // can throw anything".
1127 if (Tok.is(tok::ellipsis)) {
1128 SourceLocation EllipsisLoc = ConsumeToken();
1129 if (!getLang().Microsoft)
1130 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001131 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001132 return false;
1133 }
1134
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001135 // Parse the sequence of type-ids.
1136 while (Tok.isNot(tok::r_paren)) {
1137 ParseTypeName();
1138 if (Tok.is(tok::comma))
1139 ConsumeToken();
1140 else
1141 break;
1142 }
1143
Sebastian Redlab197ba2009-02-09 18:23:29 +00001144 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001145 return false;
1146}