DeclCXX.h revision 396b7cd9f6b35d87d17ae03e9448b5c1f2184598
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  /// UserDeclaredConstructor - True when this class has a
211  /// user-declared constructor.
212  bool UserDeclaredConstructor : 1;
213
214  /// UserDeclaredCopyConstructor - True when this class has a
215  /// user-defined copy constructor.
216  bool UserDeclaredCopyConstructor : 1;
217
218  /// Bases - Base classes of this class.
219  /// FIXME: This is wasted space for a union.
220  CXXBaseSpecifier *Bases;
221
222  /// NumBases - The number of base class specifiers in Bases.
223  unsigned NumBases;
224
225  /// Constructors - Overload set containing the constructors of this
226  /// C++ class. Each of the entries in this overload set is a
227  /// CXXConstructorDecl.
228  OverloadedFunctionDecl Constructors;
229
230  CXXRecordDecl(TagKind TK, DeclContext *DC,
231                SourceLocation L, IdentifierInfo *Id)
232    : RecordDecl(CXXRecord, TK, DC, L, Id), DeclContext(CXXRecord),
233      UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
234      Bases(0), NumBases(0), Constructors(DC, Id) { }
235
236  ~CXXRecordDecl();
237
238public:
239  /// base_class_iterator - Iterator that traverses the base classes
240  /// of a clas.
241  typedef CXXBaseSpecifier*       base_class_iterator;
242
243  /// base_class_const_iterator - Iterator that traverses the base
244  /// classes of a clas.
245  typedef const CXXBaseSpecifier* base_class_const_iterator;
246
247  static CXXRecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC,
248                               SourceLocation L, IdentifierInfo *Id,
249                               CXXRecordDecl* PrevDecl=0);
250
251  /// setBases - Sets the base classes of this struct or class.
252  void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases);
253
254  /// getNumBases - Retrieves the number of base classes of this
255  /// class.
256  unsigned getNumBases() const { return NumBases; }
257
258  base_class_iterator       bases_begin()       { return Bases; }
259  base_class_const_iterator bases_begin() const { return Bases; }
260  base_class_iterator       bases_end()         { return Bases + NumBases; }
261  base_class_const_iterator bases_end()   const { return Bases + NumBases; }
262
263  const CXXFieldDecl *getMember(unsigned i) const {
264    return cast<const CXXFieldDecl>(RecordDecl::getMember(i));
265  }
266  CXXFieldDecl *getMember(unsigned i) {
267    return cast<CXXFieldDecl>(RecordDecl::getMember(i));
268  }
269
270  /// getMember - If the member doesn't exist, or there are no members, this
271  /// function will return 0;
272  CXXFieldDecl *getMember(IdentifierInfo *name) {
273    return cast_or_null<CXXFieldDecl>(RecordDecl::getMember(name));
274  }
275
276  /// getConstructors - Retrieve the overload set containing all of
277  /// the constructors of this class.
278  OverloadedFunctionDecl       *getConstructors()       { return &Constructors; }
279
280  /// getConstructors - Retrieve the overload set containing all of
281  /// the constructors of this class.
282  const OverloadedFunctionDecl *getConstructors() const { return &Constructors; }
283
284  /// hasConstCopyConstructor - Determines whether this class has a
285  /// copy constructor that accepts a const-qualified argument.
286  bool hasConstCopyConstructor(ASTContext &Context) const;
287
288  /// addConstructor - Add another constructor to the list of constructors.
289  void addConstructor(ASTContext &Context, CXXConstructorDecl *ConDecl);
290
291  /// hasUserDeclaredConstructor - Whether this class has any
292  /// user-declared constructors. When true, a default constructor
293  /// will not be implicitly declared.
294  bool hasUserDeclaredConstructor() const { return UserDeclaredConstructor; }
295
296  /// hasUserDeclaredCopyConstructor - Whether this class has a
297  /// user-declared copy constructor. When false, a copy constructor
298  /// will be implicitly declared.
299  bool hasUserDeclaredCopyConstructor() const {
300    return UserDeclaredCopyConstructor;
301  }
302
303  /// viewInheritance - Renders and displays an inheritance diagram
304  /// for this C++ class and all of its base classes (transitively) using
305  /// GraphViz.
306  void viewInheritance(ASTContext& Context) const;
307
308  static bool classof(const Decl *D) { return D->getKind() == CXXRecord; }
309  static bool classof(const CXXRecordDecl *D) { return true; }
310  static DeclContext *castToDeclContext(const CXXRecordDecl *D) {
311    return static_cast<DeclContext *>(const_cast<CXXRecordDecl*>(D));
312  }
313  static CXXRecordDecl *castFromDeclContext(const DeclContext *DC) {
314    return static_cast<CXXRecordDecl *>(const_cast<DeclContext*>(DC));
315  }
316
317  virtual void Destroy(ASTContext& C);
318
319protected:
320  /// EmitImpl - Serialize this CXXRecordDecl.  Called by Decl::Emit.
321  // FIXME: Implement this.
322  //virtual void EmitImpl(llvm::Serializer& S) const;
323
324  /// CreateImpl - Deserialize a CXXRecordDecl.  Called by Decl::Create.
325  // FIXME: Implement this.
326  static CXXRecordDecl* CreateImpl(Kind DK, llvm::Deserializer& D, ASTContext& C);
327
328  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
329};
330
331/// CXXMethodDecl - Represents a static or instance method of a
332/// struct/union/class.
333class CXXMethodDecl : public FunctionDecl {
334  CXXMethodDecl(CXXRecordDecl *RD, SourceLocation L,
335               IdentifierInfo *Id, QualType T,
336               bool isStatic, bool isInline, ScopedDecl *PrevDecl)
337    : FunctionDecl(CXXMethod, RD, L, Id, T, (isStatic ? Static : None),
338                   isInline, PrevDecl) {}
339
340protected:
341  CXXMethodDecl(Kind DK, CXXRecordDecl *RD, SourceLocation L,
342                IdentifierInfo *Id, QualType T,
343                bool isStatic, bool isInline, ScopedDecl *PrevDecl)
344    : FunctionDecl(DK, RD, L, Id, T, (isStatic ? Static : None),
345                   isInline, PrevDecl) {}
346
347public:
348  static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD,
349                              SourceLocation L, IdentifierInfo *Id,
350                              QualType T, bool isStatic = false,
351                              bool isInline = false,  ScopedDecl *PrevDecl = 0);
352
353  bool isStatic() const { return getStorageClass() == Static; }
354  bool isInstance() const { return !isStatic(); }
355
356  void setAccess(AccessSpecifier AS) { Access = AS; }
357  AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
358
359  /// getParent - Returns the parent of this method declaration, which
360  /// is the class in which this method is defined.
361  const CXXRecordDecl *getParent() const {
362    return cast<CXXRecordDecl>(FunctionDecl::getParent());
363  }
364
365  /// getParent - Returns the parent of this method declaration, which
366  /// is the class in which this method is defined.
367  CXXRecordDecl *getParent() {
368    return const_cast<CXXRecordDecl *>(
369             cast<CXXRecordDecl>(FunctionDecl::getParent()));
370  }
371
372  /// getThisType - Returns the type of 'this' pointer.
373  /// Should only be called for instance methods.
374  QualType getThisType(ASTContext &C) const;
375
376  unsigned getTypeQualifiers() const {
377    return getType()->getAsFunctionTypeProto()->getTypeQuals();
378  }
379
380  // Implement isa/cast/dyncast/etc.
381  static bool classof(const Decl *D) {
382    return D->getKind() >= CXXMethod && D->getKind() <= CXXConstructor;
383  }
384  static bool classof(const CXXMethodDecl *D) { return true; }
385
386protected:
387  /// EmitImpl - Serialize this CXXMethodDecl.  Called by Decl::Emit.
388  // FIXME: Implement this.
389  //virtual void EmitImpl(llvm::Serializer& S) const;
390
391  /// CreateImpl - Deserialize a CXXMethodDecl.  Called by Decl::Create.
392  // FIXME: Implement this.
393  static CXXMethodDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
394
395  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
396};
397
398/// CXXConstructorDecl - Represents a C++ constructor within a
399/// class. For example:
400///
401/// @code
402/// class X {
403/// public:
404///   explicit X(int); // represented by a CXXConstructorDecl.
405/// };
406/// @endcode
407class CXXConstructorDecl : public CXXMethodDecl {
408  /// Explicit - Whether this constructor is explicit.
409  bool Explicit : 1;
410
411  /// ImplicitlyDeclared - Whether this constructor was implicitly
412  /// declared. When false, the constructor was declared by the user.
413  bool ImplicitlyDeclared : 1;
414
415  /// ImplicitlyDefined - Whether this constructor was implicitly
416  /// defined by the compiler. When false, the constructor was defined
417  /// by the user. In C++03, this flag will have the same value as
418  /// ImplicitlyDeclared. In C++0x, however, a constructor that is
419  /// explicitly defaulted (i.e., defined with " = default") will have
420  /// @c !ImplicitlyDeclared && ImplicitlyDefined.
421  bool ImplicitlyDefined : 1;
422
423  /// FIXME: Add support for base and member initializers.
424
425  CXXConstructorDecl(CXXRecordDecl *RD, SourceLocation L,
426                     IdentifierInfo *Id, QualType T,
427                     bool isExplicit, bool isInline, bool isImplicitlyDeclared)
428    : CXXMethodDecl(CXXConstructor, RD, L, Id, T, false, isInline, /*PrevDecl=*/0),
429      Explicit(isExplicit), ImplicitlyDeclared(isImplicitlyDeclared),
430      ImplicitlyDefined(false) { }
431
432public:
433  static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
434                                    SourceLocation L, IdentifierInfo *Id,
435                                    QualType T, bool isExplicit,
436                                    bool isInline, bool isImplicitlyDeclared);
437
438  /// isExplicit - Whether this constructor was marked "explicit" or not.
439  bool isExplicit() const { return Explicit; }
440
441  /// isImplicitlyDeclared - Whether this constructor was implicitly
442  /// declared. If false, then this constructor was explicitly
443  /// declared by the user.
444  bool isImplicitlyDeclared() const { return ImplicitlyDeclared; }
445
446  /// isImplicitlyDefined - Whether this constructor was implicitly
447  /// defined. If false, then this constructor was defined by the
448  /// user. This operation can only be invoked if the constructor has
449  /// already been defined.
450  bool isImplicitlyDefined() const {
451    assert(getBody() != 0 &&
452           "Can only get the implicit-definition flag once the constructor has been defined");
453    return ImplicitlyDefined;
454  }
455
456  /// setImplicitlyDefined - Set whether this constructor was
457  /// implicitly defined or not.
458  void setImplicitlyDefined(bool ID) {
459    assert(getBody() != 0 &&
460           "Can only set the implicit-definition flag once the constructor has been defined");
461    ImplicitlyDefined = ID;
462  }
463
464  /// isDefaultConstructor - Whether this constructor is a default
465  /// constructor (C++ [class.ctor]p5), which can be used to
466  /// default-initialize a class of this type.
467  bool isDefaultConstructor() const;
468
469  /// isCopyConstructor - Whether this constructor is a copy
470  /// constructor (C++ [class.copy]p2, which can be used to copy the
471  /// class. @p TypeQuals will be set to the qualifiers on the
472  /// argument type. For example, @p TypeQuals would be set to @c
473  /// QualType::Const for the following copy constructor:
474  ///
475  /// @code
476  /// class X {
477  /// public:
478  ///   X(const X&);
479  /// };
480  /// @endcode
481  bool isCopyConstructor(ASTContext &Context, unsigned &TypeQuals) const;
482
483  /// isCopyConstructor - Whether this constructor is a copy
484  /// constructor (C++ [class.copy]p2, which can be used to copy the
485  /// class.
486  bool isCopyConstructor(ASTContext &Context) const {
487    unsigned TypeQuals = 0;
488    return isCopyConstructor(Context, TypeQuals);
489  }
490
491  /// isConvertingConstructor - Whether this constructor is a
492  /// converting constructor (C++ [class.conv.ctor]), which can be
493  /// used for user-defined conversions.
494  bool isConvertingConstructor() const;
495
496  // Implement isa/cast/dyncast/etc.
497  static bool classof(const Decl *D) {
498    return D->getKind() == CXXConstructor;
499  }
500  static bool classof(const CXXConstructorDecl *D) { return true; }
501
502  /// EmitImpl - Serialize this CXXConstructorDecl.  Called by Decl::Emit.
503  // FIXME: Implement this.
504  //virtual void EmitImpl(llvm::Serializer& S) const;
505
506  /// CreateImpl - Deserialize a CXXConstructorDecl.  Called by Decl::Create.
507  // FIXME: Implement this.
508  static CXXConstructorDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
509};
510
511/// CXXClassVarDecl - Represents a static data member of a struct/union/class.
512class CXXClassVarDecl : public VarDecl {
513
514  CXXClassVarDecl(CXXRecordDecl *RD, SourceLocation L,
515              IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl)
516    : VarDecl(CXXClassVar, RD, L, Id, T, None, PrevDecl) {}
517public:
518  static CXXClassVarDecl *Create(ASTContext &C, CXXRecordDecl *RD,
519                             SourceLocation L,IdentifierInfo *Id,
520                             QualType T, ScopedDecl *PrevDecl);
521
522  void setAccess(AccessSpecifier AS) { Access = AS; }
523  AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
524
525  // Implement isa/cast/dyncast/etc.
526  static bool classof(const Decl *D) { return D->getKind() == CXXClassVar; }
527  static bool classof(const CXXClassVarDecl *D) { return true; }
528
529protected:
530  /// EmitImpl - Serialize this CXXClassVarDecl. Called by Decl::Emit.
531  // FIXME: Implement this.
532  //virtual void EmitImpl(llvm::Serializer& S) const;
533
534  /// CreateImpl - Deserialize a CXXClassVarDecl.  Called by Decl::Create.
535  // FIXME: Implement this.
536  static CXXClassVarDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
537
538  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
539};
540
541
542/// CXXClassMemberWrapper - A wrapper class for C++ class member decls.
543/// Common functions like set/getAccess are included here to avoid bloating
544/// the interface of non-C++ specific decl classes, like NamedDecl.
545class CXXClassMemberWrapper {
546  Decl *MD;
547
548public:
549  CXXClassMemberWrapper(Decl *D) : MD(D) {
550    assert(isMember(D) && "Not a C++ class member!");
551  }
552
553  AccessSpecifier getAccess() const {
554    return AccessSpecifier(MD->Access);
555  }
556
557  void setAccess(AccessSpecifier AS) {
558    assert(AS != AS_none && "Access must be specified.");
559    MD->Access = AS;
560  }
561
562  CXXRecordDecl *getParent() const {
563    if (ScopedDecl *SD = dyn_cast<ScopedDecl>(MD)) {
564      return cast<CXXRecordDecl>(SD->getDeclContext());
565    }
566    return cast<CXXFieldDecl>(MD)->getParent();
567  }
568
569  static bool isMember(Decl *D) {
570    if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
571      return isa<CXXRecordDecl>(SD->getDeclContext());
572    }
573    return isa<CXXFieldDecl>(D);
574  }
575};
576
577} // end namespace clang
578
579#endif
580