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