DeclCXX.h revision f15ac4bc132d609f1df92308612f3d2629bbd480
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;
22class CXXConstructorDecl;
23
24/// OverloadedFunctionDecl - An instance of this class represents a
25/// set of overloaded functions. All of the functions have the same
26/// name and occur within the same scope.
27///
28/// An OverloadedFunctionDecl has no ownership over the FunctionDecl
29/// nodes it contains. Rather, the FunctionDecls are owned by the
30/// enclosing scope (which also owns the OverloadedFunctionDecl
31/// node). OverloadedFunctionDecl is used primarily to store a set of
32/// overloaded functions for name lookup.
33class OverloadedFunctionDecl : public NamedDecl {
34protected:
35  OverloadedFunctionDecl(DeclContext *DC, IdentifierInfo *Id)
36    : NamedDecl(OverloadedFunction, SourceLocation(), Id) { }
37
38  /// Functions - the set of overloaded functions contained in this
39  /// overload set.
40  llvm::SmallVector<FunctionDecl *, 4> Functions;
41
42public:
43  typedef llvm::SmallVector<FunctionDecl *, 4>::iterator function_iterator;
44  typedef llvm::SmallVector<FunctionDecl *, 4>::const_iterator
45    function_const_iterator;
46
47  static OverloadedFunctionDecl *Create(ASTContext &C, DeclContext *DC,
48                                        IdentifierInfo *Id);
49
50  /// addOverload - Add an overloaded function FD to this set of
51  /// overloaded functions.
52  void addOverload(FunctionDecl *FD) {
53    assert((!getNumFunctions() || (FD->getDeclContext() == getDeclContext())) &&
54           "Overloaded functions must all be in the same context");
55    assert(FD->getIdentifier() == getIdentifier() &&
56           "Overloaded functions must have the same name.");
57    Functions.push_back(FD);
58  }
59
60  function_iterator function_begin() { return Functions.begin(); }
61  function_iterator function_end() { return Functions.end(); }
62  function_const_iterator function_begin() const { return Functions.begin(); }
63  function_const_iterator function_end() const { return Functions.end(); }
64
65  /// getNumFunctions - the number of overloaded functions stored in
66  /// this set.
67  unsigned getNumFunctions() const { return Functions.size(); }
68
69  /// getFunction - retrieve the ith function in the overload set.
70  const FunctionDecl *getFunction(unsigned i) const {
71    assert(i < getNumFunctions() && "Illegal function #");
72    return Functions[i];
73  }
74  FunctionDecl *getFunction(unsigned i) {
75    assert(i < getNumFunctions() && "Illegal function #");
76    return Functions[i];
77  }
78
79  // getDeclContext - Get the context of these overloaded functions.
80  DeclContext *getDeclContext() {
81    assert(getNumFunctions() > 0 && "Context of an empty overload set");
82    return getFunction(0)->getDeclContext();
83  }
84
85  // Implement isa/cast/dyncast/etc.
86  static bool classof(const Decl *D) {
87    return D->getKind() == OverloadedFunction;
88  }
89  static bool classof(const OverloadedFunctionDecl *D) { return true; }
90
91protected:
92  /// EmitImpl - Serialize this FunctionDecl.  Called by Decl::Emit.
93  virtual void EmitImpl(llvm::Serializer& S) const;
94
95  /// CreateImpl - Deserialize an OverloadedFunctionDecl.  Called by
96  /// Decl::Create.
97  static OverloadedFunctionDecl* CreateImpl(llvm::Deserializer& D,
98                                            ASTContext& C);
99
100  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
101  friend class CXXRecordDecl;
102};
103
104/// CXXFieldDecl - Represents an instance field of a C++ struct/union/class.
105class CXXFieldDecl : public FieldDecl {
106  CXXRecordDecl *Parent;
107
108  CXXFieldDecl(CXXRecordDecl *RD, SourceLocation L, IdentifierInfo *Id,
109               QualType T, Expr *BW = NULL)
110    : FieldDecl(CXXField, L, Id, T, BW), Parent(RD) {}
111public:
112  static CXXFieldDecl *Create(ASTContext &C, CXXRecordDecl *RD,SourceLocation L,
113                              IdentifierInfo *Id, QualType T, Expr *BW = NULL);
114
115  void setAccess(AccessSpecifier AS) { Access = AS; }
116  AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
117  CXXRecordDecl *getParent() const { return Parent; }
118
119  // Implement isa/cast/dyncast/etc.
120  static bool classof(const Decl *D) { return D->getKind() == CXXField; }
121  static bool classof(const CXXFieldDecl *D) { return true; }
122};
123
124/// CXXBaseSpecifier - A base class of a C++ class.
125///
126/// Each CXXBaseSpecifier represents a single, direct base class (or
127/// struct) of a C++ class (or struct). It specifies the type of that
128/// base class, whether it is a virtual or non-virtual base, and what
129/// level of access (public, protected, private) is used for the
130/// derivation. For example:
131///
132/// @code
133///   class A { };
134///   class B { };
135///   class C : public virtual A, protected B { };
136/// @endcode
137///
138/// In this code, C will have two CXXBaseSpecifiers, one for "public
139/// virtual A" and the other for "protected B".
140class CXXBaseSpecifier {
141  /// Range - The source code range that covers the full base
142  /// specifier, including the "virtual" (if present) and access
143  /// specifier (if present).
144  SourceRange Range;
145
146  /// Virtual - Whether this is a virtual base class or not.
147  bool Virtual : 1;
148
149  /// BaseOfClass - Whether this is the base of a class (true) or of a
150  /// struct (false). This determines the mapping from the access
151  /// specifier as written in the source code to the access specifier
152  /// used for semantic analysis.
153  bool BaseOfClass : 1;
154
155  /// Access - Access specifier as written in the source code (which
156  /// may be AS_none). The actual type of data stored here is an
157  /// AccessSpecifier, but we use "unsigned" here to work around a
158  /// VC++ bug.
159  unsigned Access : 2;
160
161  /// BaseType - The type of the base class. This will be a class or
162  /// struct (or a typedef of such).
163  QualType BaseType;
164
165public:
166  CXXBaseSpecifier() { }
167
168  CXXBaseSpecifier(SourceRange R, bool V, bool BC, AccessSpecifier A, QualType T)
169    : Range(R), Virtual(V), BaseOfClass(BC), Access(A), BaseType(T) { }
170
171  /// getSourceRange - Retrieves the source range that contains the
172  /// entire base specifier.
173  SourceRange getSourceRange() const { return Range; }
174
175  /// isVirtual - Determines whether the base class is a virtual base
176  /// class (or not).
177  bool isVirtual() const { return Virtual; }
178
179  /// getAccessSpecifier - Returns the access specifier for this base
180  /// specifier. This is the actual base specifier as used for
181  /// semantic analysis, so the result can never be AS_none. To
182  /// retrieve the access specifier as written in the source code, use
183  /// getAccessSpecifierAsWritten().
184  AccessSpecifier getAccessSpecifier() const {
185    if ((AccessSpecifier)Access == AS_none)
186      return BaseOfClass? AS_private : AS_public;
187    else
188      return (AccessSpecifier)Access;
189  }
190
191  /// getAccessSpecifierAsWritten - Retrieves the access specifier as
192  /// written in the source code (which may mean that no access
193  /// specifier was explicitly written). Use getAccessSpecifier() to
194  /// retrieve the access specifier for use in semantic analysis.
195  AccessSpecifier getAccessSpecifierAsWritten() const {
196    return (AccessSpecifier)Access;
197  }
198
199  /// getType - Retrieves the type of the base class. This type will
200  /// always be an unqualified class type.
201  QualType getType() const { return BaseType; }
202};
203
204/// CXXRecordDecl - Represents a C++ struct/union/class.
205/// CXXRecordDecl differs from RecordDecl in several ways. First, it
206/// is a DeclContext, because it can contain other
207/// declarations. Second, it provides additional C++ fields, including
208/// storage for base classes and constructors.
209class CXXRecordDecl : public RecordDecl, public DeclContext {
210  /// Bases - Base classes of this class.
211  /// FIXME: This is wasted space for a union.
212  CXXBaseSpecifier *Bases;
213
214  /// NumBases - The number of base class specifiers in Bases.
215  unsigned NumBases;
216
217  /// Constructors - Overload set containing the constructors of this
218  /// C++ class. Each of the entries in this overload set is a
219  /// CXXConstructorDecl.
220  OverloadedFunctionDecl Constructors;
221
222  CXXRecordDecl(TagKind TK, DeclContext *DC,
223                SourceLocation L, IdentifierInfo *Id)
224    : RecordDecl(CXXRecord, TK, DC, L, Id), DeclContext(CXXRecord),
225      Bases(0), NumBases(0), Constructors(DC, Id) {}
226
227  ~CXXRecordDecl();
228
229public:
230  /// base_class_iterator - Iterator that traverses the base classes
231  /// of a clas.
232  typedef CXXBaseSpecifier*       base_class_iterator;
233
234  /// base_class_const_iterator - Iterator that traverses the base
235  /// classes of a clas.
236  typedef const CXXBaseSpecifier* base_class_const_iterator;
237
238  static CXXRecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC,
239                               SourceLocation L, IdentifierInfo *Id,
240                               CXXRecordDecl* PrevDecl=0);
241
242  /// setBases - Sets the base classes of this struct or class.
243  void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases);
244
245  /// getNumBases - Retrieves the number of base classes of this
246  /// class.
247  unsigned getNumBases() const { return NumBases; }
248
249  base_class_iterator       bases_begin()       { return Bases; }
250  base_class_const_iterator bases_begin() const { return Bases; }
251  base_class_iterator       bases_end()         { return Bases + NumBases; }
252  base_class_const_iterator bases_end()   const { return Bases + NumBases; }
253
254  const CXXFieldDecl *getMember(unsigned i) const {
255    return cast<const CXXFieldDecl>(RecordDecl::getMember(i));
256  }
257  CXXFieldDecl *getMember(unsigned i) {
258    return cast<CXXFieldDecl>(RecordDecl::getMember(i));
259  }
260
261  /// getMember - If the member doesn't exist, or there are no members, this
262  /// function will return 0;
263  CXXFieldDecl *getMember(IdentifierInfo *name) {
264    return cast_or_null<CXXFieldDecl>(RecordDecl::getMember(name));
265  }
266
267  /// getConstructors - Retrieve the overload set containing all of
268  /// the constructors of this class.
269  OverloadedFunctionDecl       *getConstructors()       { return &Constructors; }
270
271  /// getConstructors - Retrieve the overload set containing all of
272  /// the constructors of this class.
273  const OverloadedFunctionDecl *getConstructors() const { return &Constructors; }
274
275  /// addConstructor - Add another constructor to the list of constructors.
276  void addConstructor(CXXConstructorDecl *ConDecl);
277
278  /// viewInheritance - Renders and displays an inheritance diagram
279  /// for this C++ class and all of its base classes (transitively) using
280  /// GraphViz.
281  void viewInheritance(ASTContext& Context) const;
282
283  static bool classof(const Decl *D) { return D->getKind() == CXXRecord; }
284  static bool classof(const CXXRecordDecl *D) { return true; }
285  static DeclContext *castToDeclContext(const CXXRecordDecl *D) {
286    return static_cast<DeclContext *>(const_cast<CXXRecordDecl*>(D));
287  }
288  static CXXRecordDecl *castFromDeclContext(const DeclContext *DC) {
289    return static_cast<CXXRecordDecl *>(const_cast<DeclContext*>(DC));
290  }
291
292  virtual void Destroy(ASTContext& C);
293
294protected:
295  /// EmitImpl - Serialize this CXXRecordDecl.  Called by Decl::Emit.
296  // FIXME: Implement this.
297  //virtual void EmitImpl(llvm::Serializer& S) const;
298
299  /// CreateImpl - Deserialize a CXXRecordDecl.  Called by Decl::Create.
300  // FIXME: Implement this.
301  static CXXRecordDecl* CreateImpl(Kind DK, llvm::Deserializer& D, ASTContext& C);
302
303  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
304};
305
306/// CXXMethodDecl - Represents a static or instance method of a
307/// struct/union/class.
308class CXXMethodDecl : public FunctionDecl {
309  CXXMethodDecl(CXXRecordDecl *RD, SourceLocation L,
310               IdentifierInfo *Id, QualType T,
311               bool isStatic, bool isInline, ScopedDecl *PrevDecl)
312    : FunctionDecl(CXXMethod, RD, L, Id, T, (isStatic ? Static : None),
313                   isInline, PrevDecl) {}
314
315protected:
316  CXXMethodDecl(Kind DK, CXXRecordDecl *RD, SourceLocation L,
317                IdentifierInfo *Id, QualType T,
318                bool isStatic, bool isInline, ScopedDecl *PrevDecl)
319    : FunctionDecl(DK, RD, L, Id, T, (isStatic ? Static : None),
320                   isInline, PrevDecl) {}
321
322public:
323  static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD,
324                              SourceLocation L, IdentifierInfo *Id,
325                              QualType T, bool isStatic = false,
326                              bool isInline = false,  ScopedDecl *PrevDecl = 0);
327
328  bool isStatic() const { return getStorageClass() == Static; }
329  bool isInstance() const { return !isStatic(); }
330
331  void setAccess(AccessSpecifier AS) { Access = AS; }
332  AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
333
334  /// getThisType - Returns the type of 'this' pointer.
335  /// Should only be called for instance methods.
336  QualType getThisType(ASTContext &C) const;
337
338  unsigned getTypeQualifiers() const {
339    return getType()->getAsFunctionTypeProto()->getTypeQuals();
340  }
341
342  // Implement isa/cast/dyncast/etc.
343  static bool classof(const Decl *D) {
344    return D->getKind() >= CXXMethod && D->getKind() <= CXXConstructor;
345  }
346  static bool classof(const CXXMethodDecl *D) { return true; }
347
348protected:
349  /// EmitImpl - Serialize this CXXMethodDecl.  Called by Decl::Emit.
350  // FIXME: Implement this.
351  //virtual void EmitImpl(llvm::Serializer& S) const;
352
353  /// CreateImpl - Deserialize a CXXMethodDecl.  Called by Decl::Create.
354  // FIXME: Implement this.
355  static CXXMethodDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
356
357  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
358};
359
360/// CXXConstructorDecl - Represents a C++ constructor within a class. For example:
361///
362/// @code
363/// class X {
364/// public:
365///   explicit X(int); // represented by a CXXConstructorDecl.
366/// };
367/// @endcode
368class CXXConstructorDecl : public CXXMethodDecl {
369  /// Explicit - Whether this constructor is explicit.
370  bool Explicit : 1;
371
372  /// ImplicitlyDeclared - Whether this constructor was implicitly
373  /// declared. When false, the constructor was declared by the user.
374  bool ImplicitlyDeclared : 1;
375
376  /// ImplicitlyDefined - Whether this constructor was implicitly
377  /// defined by the compiler. When false, the constructor was defined
378  /// by the user. In C++03, this flag will have the same value as
379  /// ImplicitlyDeclared. In C++0x, however, a constructor that is
380  /// explicitly defaulted (i.e., defined with " = default") will have
381  /// @c !ImplicitlyDeclared && ImplicitlyDefined.
382  bool ImplicitlyDefined : 1;
383
384  /// FIXME: Add support for base and member initializers.
385
386  CXXConstructorDecl(CXXRecordDecl *RD, SourceLocation L,
387                     IdentifierInfo *Id, QualType T,
388                     bool isExplicit, bool isInline, bool isImplicitlyDeclared)
389    : CXXMethodDecl(CXXConstructor, RD, L, Id, T, false, isInline, /*PrevDecl=*/0),
390      Explicit(isExplicit), ImplicitlyDeclared(isImplicitlyDeclared),
391      ImplicitlyDefined(false) { }
392
393public:
394  static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
395                                    SourceLocation L, IdentifierInfo *Id,
396                                    QualType T, bool isExplicit,
397                                    bool isInline, bool isImplicitlyDeclared);
398
399  /// isExplicit - Whether this constructor was marked "explicit" or not.
400  bool isExplicit() const { return Explicit; }
401
402  /// isImplicitlyDeclared - Whether this constructor was implicitly
403  /// declared. If false, then this constructor was explicitly
404  /// declared by the user.
405  bool isImplicitlyDeclared() const { return ImplicitlyDeclared; }
406
407  /// isImplicitlyDefined - Whether this constructor was implicitly
408  /// defined. If false, then this constructor was defined by the
409  /// user. This operation can only be invoked if the constructor has
410  /// already been defined.
411  bool isImplicitlyDefined() const {
412    assert(getBody() != 0 &&
413           "Can only get the implicit-definition flag once the constructor has been defined");
414    return ImplicitlyDefined;
415  }
416
417  /// setImplicitlyDefined
418  void setImplicitlyDefined(bool ID) {
419    assert(getBody() != 0 &&
420           "Can only set the implicit-definition flag once the constructor has been defined");
421    ImplicitlyDefined = ID;
422  }
423
424  // Implement isa/cast/dyncast/etc.
425  static bool classof(const Decl *D) {
426    return D->getKind() == CXXConstructor;
427  }
428  static bool classof(const CXXConstructorDecl *D) { return true; }
429
430  /// EmitImpl - Serialize this CXXConstructorDecl.  Called by Decl::Emit.
431  // FIXME: Implement this.
432  //virtual void EmitImpl(llvm::Serializer& S) const;
433
434  /// CreateImpl - Deserialize a CXXConstructorDecl.  Called by Decl::Create.
435  // FIXME: Implement this.
436  static CXXConstructorDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
437};
438
439/// CXXClassVarDecl - Represents a static data member of a struct/union/class.
440class CXXClassVarDecl : public VarDecl {
441
442  CXXClassVarDecl(CXXRecordDecl *RD, SourceLocation L,
443              IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl)
444    : VarDecl(CXXClassVar, RD, L, Id, T, None, PrevDecl) {}
445public:
446  static CXXClassVarDecl *Create(ASTContext &C, CXXRecordDecl *RD,
447                             SourceLocation L,IdentifierInfo *Id,
448                             QualType T, ScopedDecl *PrevDecl);
449
450  void setAccess(AccessSpecifier AS) { Access = AS; }
451  AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
452
453  // Implement isa/cast/dyncast/etc.
454  static bool classof(const Decl *D) { return D->getKind() == CXXClassVar; }
455  static bool classof(const CXXClassVarDecl *D) { return true; }
456
457protected:
458  /// EmitImpl - Serialize this CXXClassVarDecl. Called by Decl::Emit.
459  // FIXME: Implement this.
460  //virtual void EmitImpl(llvm::Serializer& S) const;
461
462  /// CreateImpl - Deserialize a CXXClassVarDecl.  Called by Decl::Create.
463  // FIXME: Implement this.
464  static CXXClassVarDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
465
466  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
467};
468
469
470/// CXXClassMemberWrapper - A wrapper class for C++ class member decls.
471/// Common functions like set/getAccess are included here to avoid bloating
472/// the interface of non-C++ specific decl classes, like NamedDecl.
473class CXXClassMemberWrapper {
474  Decl *MD;
475
476public:
477  CXXClassMemberWrapper(Decl *D) : MD(D) {
478    assert(isMember(D) && "Not a C++ class member!");
479  }
480
481  AccessSpecifier getAccess() const {
482    return AccessSpecifier(MD->Access);
483  }
484
485  void setAccess(AccessSpecifier AS) {
486    assert(AS != AS_none && "Access must be specified.");
487    MD->Access = AS;
488  }
489
490  CXXRecordDecl *getParent() const {
491    if (ScopedDecl *SD = dyn_cast<ScopedDecl>(MD)) {
492      return cast<CXXRecordDecl>(SD->getDeclContext());
493    }
494    return cast<CXXFieldDecl>(MD)->getParent();
495  }
496
497  static bool isMember(Decl *D) {
498    if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
499      return isa<CXXRecordDecl>(SD->getDeclContext());
500    }
501    return isa<CXXFieldDecl>(D);
502  }
503};
504
505inline void CXXRecordDecl::addConstructor(CXXConstructorDecl *ConDecl) {
506  Constructors.addOverload(ConDecl);
507}
508
509} // end namespace clang
510
511#endif
512