DeclCXX.h revision 1f81230ac57b9bda8bba9c8221652842ca786132
1//===-- DeclCXX.h - Classes for representing C++ declarations -*- C++ -*-=====//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the C++ Decl subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_DECLCXX_H
15#define LLVM_CLANG_AST_DECLCXX_H
16
17#include "clang/AST/Decl.h"
18#include "llvm/ADT/SmallVector.h"
19
20namespace clang {
21class CXXRecordDecl;
22
23/// CXXFieldDecl - Represents an instance field of a C++ struct/union/class.
24class CXXFieldDecl : public FieldDecl {
25  CXXRecordDecl *Parent;
26
27  CXXFieldDecl(CXXRecordDecl *RD, SourceLocation L, IdentifierInfo *Id,
28               QualType T, Expr *BW = NULL)
29    : FieldDecl(CXXField, L, Id, T, BW), Parent(RD) {}
30public:
31  static CXXFieldDecl *Create(ASTContext &C, CXXRecordDecl *RD,SourceLocation L,
32                              IdentifierInfo *Id, QualType T, Expr *BW = NULL);
33
34  void setAccess(AccessSpecifier AS) { Access = AS; }
35  AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
36  CXXRecordDecl *getParent() const { return Parent; }
37
38  // Implement isa/cast/dyncast/etc.
39  static bool classof(const Decl *D) { return D->getKind() == CXXField; }
40  static bool classof(const CXXFieldDecl *D) { return true; }
41};
42
43/// CXXBaseSpecifier - A base class of a C++ class.
44///
45/// Each CXXBaseSpecifier represents a single, direct base class (or
46/// struct) of a C++ class (or struct). It specifies the type of that
47/// base class, whether it is a virtual or non-virtual base, and what
48/// level of access (public, protected, private) is used for the
49/// derivation. For example:
50///
51/// @code
52///   class A { };
53///   class B { };
54///   class C : public virtual A, protected B { };
55/// @endcode
56///
57/// In this code, C will have two CXXBaseSpecifiers, one for "public
58/// virtual A" and the other for "protected B".
59class CXXBaseSpecifier {
60  /// Range - The source code range that covers the full base
61  /// specifier, including the "virtual" (if present) and access
62  /// specifier (if present).
63  SourceRange Range;
64
65  /// Virtual - Whether this is a virtual base class or not.
66  bool Virtual : 1;
67
68  /// BaseOfClass - Whether this is the base of a class (true) or of a
69  /// struct (false). This determines the mapping from the access
70  /// specifier as written in the source code to the access specifier
71  /// used for semantic analysis.
72  bool BaseOfClass : 1;
73
74  /// Access - Access specifier as written in the source code (which
75  /// may be AS_none). The actual type of data stored here is an
76  /// AccessSpecifier, but we use "unsigned" here to work around a
77  /// VC++ bug.
78  unsigned Access : 2;
79
80  /// BaseType - The type of the base class. This will be a class or
81  /// struct (or a typedef of such).
82  QualType BaseType;
83
84public:
85  CXXBaseSpecifier() { }
86
87  CXXBaseSpecifier(SourceRange R, bool V, bool BC, AccessSpecifier A, QualType T)
88    : Range(R), Virtual(V), BaseOfClass(BC), Access(A), BaseType(T) { }
89
90  /// getSourceRange - Retrieves the source range that contains the
91  /// entire base specifier.
92  SourceRange getSourceRange() const { return Range; }
93
94  /// isVirtual - Determines whether the base class is a virtual base
95  /// class (or not).
96  bool isVirtual() const { return Virtual; }
97
98  /// getAccessSpecifier - Returns the access specifier for this base
99  /// specifier. This is the actual base specifier as used for
100  /// semantic analysis, so the result can never be AS_none. To
101  /// retrieve the access specifier as written in the source code, use
102  /// getAccessSpecifierAsWritten().
103  AccessSpecifier getAccessSpecifier() const {
104    if ((AccessSpecifier)Access == AS_none)
105      return BaseOfClass? AS_private : AS_public;
106    else
107      return (AccessSpecifier)Access;
108  }
109
110  /// getAccessSpecifierAsWritten - Retrieves the access specifier as
111  /// written in the source code (which may mean that no access
112  /// specifier was explicitly written). Use getAccessSpecifier() to
113  /// retrieve the access specifier for use in semantic analysis.
114  AccessSpecifier getAccessSpecifierAsWritten() const {
115    return (AccessSpecifier)Access;
116  }
117
118  /// getType - Retrieves the type of the base class. This type will
119  /// always be an unqualified class type.
120  QualType getType() const { return BaseType; }
121};
122
123/// CXXRecordDecl - Represents a C++ struct/union/class.
124/// CXXRecordDecl differs from RecordDecl in several ways. First, it
125/// is a DeclContext, because it can contain other
126/// declarations. Second, it provides additional C++ fields, including
127/// storage for base classes.
128class CXXRecordDecl : public RecordDecl, public DeclContext {
129  /// Bases - Base classes of this class.
130  /// FIXME: This is wasted space for a union.
131  CXXBaseSpecifier *Bases;
132
133  /// NumBases - The number of base class specifiers in Bases.
134  unsigned NumBases;
135
136  CXXRecordDecl(TagKind TK, DeclContext *DC,
137                SourceLocation L, IdentifierInfo *Id)
138    : RecordDecl(CXXRecord, TK, DC, L, Id), DeclContext(CXXRecord),
139      Bases(0), NumBases(0) {}
140
141  ~CXXRecordDecl();
142
143public:
144  /// base_class_iterator - Iterator that traverses the base classes
145  /// of a clas.
146  typedef CXXBaseSpecifier*       base_class_iterator;
147
148  /// base_class_const_iterator - Iterator that traverses the base
149  /// classes of a clas.
150  typedef const CXXBaseSpecifier* base_class_const_iterator;
151
152  static CXXRecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC,
153                               SourceLocation L, IdentifierInfo *Id,
154                               CXXRecordDecl* PrevDecl=0);
155
156  /// setBases - Sets the base classes of this struct or class.
157  void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases);
158
159  /// getNumBases - Retrieves the number of base classes of this
160  /// class.
161  unsigned getNumBases() const { return NumBases; }
162
163  base_class_iterator       bases_begin()       { return Bases; }
164  base_class_const_iterator bases_begin() const { return Bases; }
165  base_class_iterator       bases_end()         { return Bases + NumBases; }
166  base_class_const_iterator bases_end()   const { return Bases + NumBases; }
167
168
169  const CXXFieldDecl *getMember(unsigned i) const {
170    return cast<const CXXFieldDecl>(RecordDecl::getMember(i));
171  }
172  CXXFieldDecl *getMember(unsigned i) {
173    return cast<CXXFieldDecl>(RecordDecl::getMember(i));
174  }
175
176  /// getMember - If the member doesn't exist, or there are no members, this
177  /// function will return 0;
178  CXXFieldDecl *getMember(IdentifierInfo *name) {
179    return cast_or_null<CXXFieldDecl>(RecordDecl::getMember(name));
180  }
181
182  /// viewInheritance - Renders and displays an inheritance diagram
183  /// for this C++ class and all of its base classes (transitively) using
184  /// GraphViz.
185  void viewInheritance(ASTContext& Context) const;
186
187  static bool classof(const Decl *D) { return D->getKind() == CXXRecord; }
188  static bool classof(const CXXRecordDecl *D) { return true; }
189  static DeclContext *castToDeclContext(const CXXRecordDecl *D) {
190    return static_cast<DeclContext *>(const_cast<CXXRecordDecl*>(D));
191  }
192  static CXXRecordDecl *castFromDeclContext(const DeclContext *DC) {
193    return static_cast<CXXRecordDecl *>(const_cast<DeclContext*>(DC));
194  }
195
196protected:
197  /// EmitImpl - Serialize this CXXRecordDecl.  Called by Decl::Emit.
198  // FIXME: Implement this.
199  //virtual void EmitImpl(llvm::Serializer& S) const;
200
201  /// CreateImpl - Deserialize a CXXRecordDecl.  Called by Decl::Create.
202  // FIXME: Implement this.
203  static CXXRecordDecl* CreateImpl(Kind DK, llvm::Deserializer& D, ASTContext& C);
204
205  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
206};
207
208/// CXXMethodDecl - Represents a static or instance method of a
209/// struct/union/class.
210class CXXMethodDecl : public FunctionDecl {
211
212  CXXMethodDecl(CXXRecordDecl *RD, SourceLocation L,
213               IdentifierInfo *Id, QualType T,
214               bool isStatic, bool isInline, ScopedDecl *PrevDecl)
215    : FunctionDecl(CXXMethod, RD, L, Id, T, (isStatic ? Static : None),
216                   isInline, PrevDecl) {}
217public:
218  static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD,
219                              SourceLocation L, IdentifierInfo *Id,
220                              QualType T, bool isStatic = false,
221                              bool isInline = false,  ScopedDecl *PrevDecl = 0);
222
223  bool isStatic() const { return getStorageClass() == Static; }
224  bool isInstance() const { return !isStatic(); }
225
226  void setAccess(AccessSpecifier AS) { Access = AS; }
227  AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
228
229  /// getThisType - Returns the type of 'this' pointer.
230  /// Should only be called for instance methods.
231  QualType getThisType(ASTContext &C) const;
232
233  // Implement isa/cast/dyncast/etc.
234  static bool classof(const Decl *D) { return D->getKind() == CXXMethod; }
235  static bool classof(const CXXMethodDecl *D) { return true; }
236
237protected:
238  /// EmitImpl - Serialize this CXXMethodDecl.  Called by Decl::Emit.
239  // FIXME: Implement this.
240  //virtual void EmitImpl(llvm::Serializer& S) const;
241
242  /// CreateImpl - Deserialize a CXXMethodDecl.  Called by Decl::Create.
243  // FIXME: Implement this.
244  static CXXMethodDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
245
246  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
247};
248
249/// CXXClassVarDecl - Represents a static data member of a struct/union/class.
250class CXXClassVarDecl : public VarDecl {
251
252  CXXClassVarDecl(CXXRecordDecl *RD, SourceLocation L,
253              IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl)
254    : VarDecl(CXXClassVar, RD, L, Id, T, None, PrevDecl) {}
255public:
256  static CXXClassVarDecl *Create(ASTContext &C, CXXRecordDecl *RD,
257                             SourceLocation L,IdentifierInfo *Id,
258                             QualType T, ScopedDecl *PrevDecl);
259
260  void setAccess(AccessSpecifier AS) { Access = AS; }
261  AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
262
263  // Implement isa/cast/dyncast/etc.
264  static bool classof(const Decl *D) { return D->getKind() == CXXClassVar; }
265  static bool classof(const CXXClassVarDecl *D) { return true; }
266
267protected:
268  /// EmitImpl - Serialize this CXXClassVarDecl. Called by Decl::Emit.
269  // FIXME: Implement this.
270  //virtual void EmitImpl(llvm::Serializer& S) const;
271
272  /// CreateImpl - Deserialize a CXXClassVarDecl.  Called by Decl::Create.
273  // FIXME: Implement this.
274  static CXXClassVarDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
275
276  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
277};
278
279
280/// CXXClassMemberWrapper - A wrapper class for C++ class member decls.
281/// Common functions like set/getAccess are included here to avoid bloating
282/// the interface of non-C++ specific decl classes, like NamedDecl.
283class CXXClassMemberWrapper {
284  Decl *MD;
285
286public:
287  CXXClassMemberWrapper(Decl *D) : MD(D) {
288    assert(isMember(D) && "Not a C++ class member!");
289  }
290
291  AccessSpecifier getAccess() const {
292    return AccessSpecifier(MD->Access);
293  }
294
295  void setAccess(AccessSpecifier AS) {
296    assert(AS != AS_none && "Access must be specified.");
297    MD->Access = AS;
298  }
299
300  CXXRecordDecl *getParent() const {
301    if (ScopedDecl *SD = dyn_cast<ScopedDecl>(MD)) {
302      return cast<CXXRecordDecl>(SD->getDeclContext());
303    }
304    return cast<CXXFieldDecl>(MD)->getParent();
305  }
306
307  static bool isMember(Decl *D) {
308    if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
309      return isa<CXXRecordDecl>(SD->getDeclContext());
310    }
311    return isa<CXXFieldDecl>(D);
312  }
313};
314
315/// OverloadedFunctionDecl - An instance of this class represents a
316/// set of overloaded functions. All of the functions have the same
317/// name and occur within the same scope.
318///
319/// An OverloadedFunctionDecl has no ownership over the FunctionDecl
320/// nodes it contains. Rather, the FunctionDecls are owned by the
321/// enclosing scope (which also owns the OverloadedFunctionDecl
322/// node). OverloadedFunctionDecl is used primarily to store a set of
323/// overloaded functions for name lookup.
324class OverloadedFunctionDecl : public NamedDecl {
325protected:
326  OverloadedFunctionDecl(DeclContext *DC, IdentifierInfo *Id)
327    : NamedDecl(OverloadedFunction, SourceLocation(), Id) { }
328
329  /// Functions - the set of overloaded functions contained in this
330  /// overload set.
331  llvm::SmallVector<FunctionDecl *, 4> Functions;
332
333public:
334  typedef llvm::SmallVector<FunctionDecl *, 4>::iterator function_iterator;
335  typedef llvm::SmallVector<FunctionDecl *, 4>::const_iterator
336    function_const_iterator;
337
338  static OverloadedFunctionDecl *Create(ASTContext &C, DeclContext *DC,
339                                        IdentifierInfo *Id);
340
341  /// addOverload - Add an overloaded function FD to this set of
342  /// overloaded functions.
343  void addOverload(FunctionDecl *FD) {
344    assert((!getNumFunctions() || (FD->getDeclContext() == getDeclContext())) &&
345           "Overloaded functions must all be in the same context");
346    assert(FD->getIdentifier() == getIdentifier() &&
347           "Overloaded functions must have the same name.");
348    Functions.push_back(FD);
349  }
350
351  function_iterator function_begin() { return Functions.begin(); }
352  function_iterator function_end() { return Functions.end(); }
353  function_const_iterator function_begin() const { return Functions.begin(); }
354  function_const_iterator function_end() const { return Functions.end(); }
355
356  /// getNumFunctions - the number of overloaded functions stored in
357  /// this set.
358  unsigned getNumFunctions() const { return Functions.size(); }
359
360  /// getFunction - retrieve the ith function in the overload set.
361  const FunctionDecl *getFunction(unsigned i) const {
362    assert(i < getNumFunctions() && "Illegal function #");
363    return Functions[i];
364  }
365  FunctionDecl *getFunction(unsigned i) {
366    assert(i < getNumFunctions() && "Illegal function #");
367    return Functions[i];
368  }
369
370  // getDeclContext - Get the context of these overloaded functions.
371  DeclContext *getDeclContext() {
372    assert(getNumFunctions() > 0 && "Context of an empty overload set");
373    return getFunction(0)->getDeclContext();
374  }
375
376  // Implement isa/cast/dyncast/etc.
377  static bool classof(const Decl *D) {
378    return D->getKind() == OverloadedFunction;
379  }
380  static bool classof(const OverloadedFunctionDecl *D) { return true; }
381
382protected:
383  /// EmitImpl - Serialize this FunctionDecl.  Called by Decl::Emit.
384  virtual void EmitImpl(llvm::Serializer& S) const;
385
386  /// CreateImpl - Deserialize an OverloadedFunctionDecl.  Called by
387  /// Decl::Create.
388  static OverloadedFunctionDecl* CreateImpl(llvm::Deserializer& D,
389                                            ASTContext& C);
390
391  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
392};
393
394} // end namespace clang
395
396#endif
397