blob: 5d49f4dd59af6d07e6f3982423809a634ce1f7de [file] [log] [blame]
Chris Lattnerf7b2e552007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattnerf7b2e552007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor696be932008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Douglas Gregorec93f442008-04-13 21:30:24 +000015#include "clang/Basic/Diagnostic.h"
16#include "clang/Parse/DeclSpec.h"
Chris Lattnerf7b2e552007-08-25 06:57:03 +000017#include "clang/Parse/Scope.h"
Chris Lattnerf7b2e552007-08-25 06:57:03 +000018using namespace clang;
19
20/// ParseNamespace - We know that the current token is a namespace keyword. This
21/// may either be a top level namespace or a block-level namespace alias.
22///
23/// namespace-definition: [C++ 7.3: basic.namespace]
24/// named-namespace-definition
25/// unnamed-namespace-definition
26///
27/// unnamed-namespace-definition:
28/// 'namespace' attributes[opt] '{' namespace-body '}'
29///
30/// named-namespace-definition:
31/// original-namespace-definition
32/// extension-namespace-definition
33///
34/// original-namespace-definition:
35/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
36///
37/// extension-namespace-definition:
38/// 'namespace' original-namespace-name '{' namespace-body '}'
39///
40/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
41/// 'namespace' identifier '=' qualified-namespace-specifier ';'
42///
43Parser::DeclTy *Parser::ParseNamespace(unsigned Context) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000044 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnerf7b2e552007-08-25 06:57:03 +000045 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
46
47 SourceLocation IdentLoc;
48 IdentifierInfo *Ident = 0;
49
Chris Lattner34a01ad2007-10-09 17:33:22 +000050 if (Tok.is(tok::identifier)) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +000051 Ident = Tok.getIdentifierInfo();
52 IdentLoc = ConsumeToken(); // eat the identifier.
53 }
54
55 // Read label attributes, if present.
56 DeclTy *AttrList = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +000057 if (Tok.is(tok::kw___attribute))
Chris Lattnerf7b2e552007-08-25 06:57:03 +000058 // FIXME: save these somewhere.
59 AttrList = ParseAttributes();
60
Chris Lattner34a01ad2007-10-09 17:33:22 +000061 if (Tok.is(tok::equal)) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +000062 // FIXME: Verify no attributes were present.
63 // FIXME: parse this.
Chris Lattner34a01ad2007-10-09 17:33:22 +000064 } else if (Tok.is(tok::l_brace)) {
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000065
Chris Lattnerf7b2e552007-08-25 06:57:03 +000066 SourceLocation LBrace = ConsumeBrace();
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000067
68 // Enter a scope for the namespace.
69 EnterScope(Scope::DeclScope);
70
71 DeclTy *NamespcDecl =
72 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
73
74 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
Chris Lattner9c135722007-08-25 18:15:16 +000075 ParseExternalDeclaration();
Chris Lattnerf7b2e552007-08-25 06:57:03 +000076
77 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000078 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBrace);
79
80 ExitScope();
81
82 return NamespcDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +000083
Chris Lattnerf7b2e552007-08-25 06:57:03 +000084 } else {
85 unsigned D = Ident ? diag::err_expected_lbrace :
86 diag::err_expected_ident_lbrace;
87 Diag(Tok.getLocation(), D);
88 }
89
90 return 0;
91}
Chris Lattner806a5f52008-01-12 07:05:38 +000092
93/// ParseLinkage - We know that the current token is a string_literal
94/// and just before that, that extern was seen.
95///
96/// linkage-specification: [C++ 7.5p2: dcl.link]
97/// 'extern' string-literal '{' declaration-seq[opt] '}'
98/// 'extern' string-literal declaration
99///
100Parser::DeclTy *Parser::ParseLinkage(unsigned Context) {
101 assert(Tok.is(tok::string_literal) && "Not a stringliteral!");
102 llvm::SmallVector<char, 8> LangBuffer;
103 // LangBuffer is guaranteed to be big enough.
104 LangBuffer.resize(Tok.getLength());
105 const char *LangBufPtr = &LangBuffer[0];
106 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
107
108 SourceLocation Loc = ConsumeStringToken();
109 DeclTy *D = 0;
110 SourceLocation LBrace, RBrace;
111
112 if (Tok.isNot(tok::l_brace)) {
113 D = ParseDeclaration(Context);
114 } else {
115 LBrace = ConsumeBrace();
116 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
117 // FIXME capture the decls.
118 D = ParseExternalDeclaration();
119 }
120
121 RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
122 }
123
124 if (!D)
125 return 0;
126
127 return Actions.ActOnLinkageSpec(Loc, LBrace, RBrace, LangBufPtr, StrSize, D);
128}
Douglas Gregorec93f442008-04-13 21:30:24 +0000129
130/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
131/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
132/// until we reach the start of a definition or see a token that
133/// cannot start a definition.
134///
135/// class-specifier: [C++ class]
136/// class-head '{' member-specification[opt] '}'
137/// class-head '{' member-specification[opt] '}' attributes[opt]
138/// class-head:
139/// class-key identifier[opt] base-clause[opt]
140/// class-key nested-name-specifier identifier base-clause[opt]
141/// class-key nested-name-specifier[opt] simple-template-id
142/// base-clause[opt]
143/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
144/// [GNU] class-key attributes[opt] nested-name-specifier
145/// identifier base-clause[opt]
146/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
147/// simple-template-id base-clause[opt]
148/// class-key:
149/// 'class'
150/// 'struct'
151/// 'union'
152///
153/// elaborated-type-specifier: [C++ dcl.type.elab]
154/// class-key ::[opt] nested-name-specifier[opt] identifier
155/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
156/// simple-template-id
157///
158/// Note that the C++ class-specifier and elaborated-type-specifier,
159/// together, subsume the C99 struct-or-union-specifier:
160///
161/// struct-or-union-specifier: [C99 6.7.2.1]
162/// struct-or-union identifier[opt] '{' struct-contents '}'
163/// struct-or-union identifier
164/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
165/// '}' attributes[opt]
166/// [GNU] struct-or-union attributes[opt] identifier
167/// struct-or-union:
168/// 'struct'
169/// 'union'
170void Parser::ParseClassSpecifier(DeclSpec &DS) {
171 assert((Tok.is(tok::kw_class) ||
172 Tok.is(tok::kw_struct) ||
173 Tok.is(tok::kw_union)) &&
174 "Not a class specifier");
175 DeclSpec::TST TagType =
176 Tok.is(tok::kw_class) ? DeclSpec::TST_class :
177 Tok.is(tok::kw_struct) ? DeclSpec::TST_struct :
178 DeclSpec::TST_union;
179
180 SourceLocation StartLoc = ConsumeToken();
181
182 AttributeList *Attr = 0;
183 // If attributes exist after tag, parse them.
184 if (Tok.is(tok::kw___attribute))
185 Attr = ParseAttributes();
186
187 // FIXME: Parse the (optional) nested-name-specifier.
188
189 // Parse the (optional) class name.
190 // FIXME: Alternatively, parse a simple-template-id.
191 IdentifierInfo *Name = 0;
192 SourceLocation NameLoc;
193 if (Tok.is(tok::identifier)) {
194 Name = Tok.getIdentifierInfo();
195 NameLoc = ConsumeToken();
196 }
197
198 // There are three options here. If we have 'struct foo;', then
199 // this is a forward declaration. If we have 'struct foo {...' or
200 // 'struct fo :...' then this is a definition. Otherwise we have
201 // something like 'struct foo xyz', a reference.
202 Action::TagKind TK;
203 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
204 TK = Action::TK_Definition;
205 else if (Tok.is(tok::semi))
206 TK = Action::TK_Declaration;
207 else
208 TK = Action::TK_Reference;
209
210 if (!Name && TK != Action::TK_Definition) {
211 // We have a declaration or reference to an anonymous class.
212 Diag(StartLoc, diag::err_anon_type_definition,
213 DeclSpec::getSpecifierName(TagType));
214
215 // Skip the rest of this declarator, up until the comma or semicolon.
216 SkipUntil(tok::comma, true);
217 return;
218 }
219
220 // Parse the tag portion of this.
221 DeclTy *TagDecl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name,
222 NameLoc, Attr);
223
224 // Parse the optional base clause (C++ only).
225 if (getLang().CPlusPlus && Tok.is(tok::colon)) {
226 ParseBaseClause(TagDecl);
227 }
228
229 // If there is a body, parse it and inform the actions module.
230 if (Tok.is(tok::l_brace))
231 ParseStructUnionBody(StartLoc, TagType, TagDecl);
232 else if (TK == Action::TK_Definition) {
233 // FIXME: Complain that we have a base-specifier list but no
234 // definition.
235 Diag(Tok.getLocation(), diag::err_expected_lbrace);
236 }
237
238 const char *PrevSpec = 0;
239 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
240 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
241}
242
243/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
244///
245/// base-clause : [C++ class.derived]
246/// ':' base-specifier-list
247/// base-specifier-list:
248/// base-specifier '...'[opt]
249/// base-specifier-list ',' base-specifier '...'[opt]
250void Parser::ParseBaseClause(DeclTy *ClassDecl)
251{
252 assert(Tok.is(tok::colon) && "Not a base clause");
253 ConsumeToken();
254
255 while (true) {
256 // Parse a base-specifier.
257 if (ParseBaseSpecifier(ClassDecl)) {
258 // Skip the rest of this base specifier, up until the comma or
259 // opening brace.
260 SkipUntil(tok::comma, tok::l_brace);
261 }
262
263 // If the next token is a comma, consume it and keep reading
264 // base-specifiers.
265 if (Tok.isNot(tok::comma)) break;
266
267 // Consume the comma.
268 ConsumeToken();
269 }
270}
271
272/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
273/// one entry in the base class list of a class specifier, for example:
274/// class foo : public bar, virtual private baz {
275/// 'public bar' and 'virtual private baz' are each base-specifiers.
276///
277/// base-specifier: [C++ class.derived]
278/// ::[opt] nested-name-specifier[opt] class-name
279/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
280/// class-name
281/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
282/// class-name
283bool Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
284{
285 bool IsVirtual = false;
286 SourceLocation StartLoc = Tok.getLocation();
287
288 // Parse the 'virtual' keyword.
289 if (Tok.is(tok::kw_virtual)) {
290 ConsumeToken();
291 IsVirtual = true;
292 }
293
294 // Parse an (optional) access specifier.
295 AccessSpecifier Access = getAccessSpecifierIfPresent();
296 if (Access)
297 ConsumeToken();
298
299 // Parse the 'virtual' keyword (again!), in case it came after the
300 // access specifier.
301 if (Tok.is(tok::kw_virtual)) {
302 SourceLocation VirtualLoc = ConsumeToken();
303 if (IsVirtual) {
304 // Complain about duplicate 'virtual'
305 Diag(VirtualLoc, diag::err_dup_virtual);
306 }
307
308 IsVirtual = true;
309 }
310
311 // FIXME: Parse optional '::' and optional nested-name-specifier.
312
313 // Parse the class-name.
314 // FIXME: Alternatively, parse a simple-template-id.
315 if (Tok.isNot(tok::identifier)) {
316 Diag(Tok.getLocation(), diag::err_expected_class_name);
317 return true;
318 }
319
320 // We have an identifier; check whether it is actually a type.
321 DeclTy *BaseType = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
322 if (!BaseType) {
323 Diag(Tok.getLocation(), diag::err_expected_class_name);
324 return true;
325 }
326
327 // The location of the base class itself.
328 SourceLocation BaseLoc = Tok.getLocation();
329
330 // Find the complete source range for the base-specifier.
331 SourceRange Range(StartLoc, BaseLoc);
332
333 // Consume the identifier token (finally!).
334 ConsumeToken();
335
336 // Notify semantic analysis that we have parsed a complete
337 // base-specifier.
338 Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, BaseType,
339 BaseLoc);
340 return false;
341}
342
343/// getAccessSpecifierIfPresent - Determine whether the next token is
344/// a C++ access-specifier.
345///
346/// access-specifier: [C++ class.derived]
347/// 'private'
348/// 'protected'
349/// 'public'
Douglas Gregor696be932008-04-14 00:13:42 +0000350AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregorec93f442008-04-13 21:30:24 +0000351{
352 switch (Tok.getKind()) {
353 default: return AS_none;
354 case tok::kw_private: return AS_private;
355 case tok::kw_protected: return AS_protected;
356 case tok::kw_public: return AS_public;
357 }
358}