DeclCXX.h revision 2e1cd4264d363ca869bf37ef160902f211d21b8c
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;
23class CXXDestructorDecl;
24class CXXConversionDecl;
25
26/// OverloadedFunctionDecl - An instance of this class represents a
27/// set of overloaded functions. All of the functions have the same
28/// name and occur within the same scope.
29///
30/// An OverloadedFunctionDecl has no ownership over the FunctionDecl
31/// nodes it contains. Rather, the FunctionDecls are owned by the
32/// enclosing scope (which also owns the OverloadedFunctionDecl
33/// node). OverloadedFunctionDecl is used primarily to store a set of
34/// overloaded functions for name lookup.
35class OverloadedFunctionDecl : public NamedDecl {
36protected:
37  OverloadedFunctionDecl(DeclContext *DC, DeclarationName N)
38    : NamedDecl(OverloadedFunction, SourceLocation(), N) { }
39
40  /// Functions - the set of overloaded functions contained in this
41  /// overload set.
42  llvm::SmallVector<FunctionDecl *, 4> Functions;
43
44public:
45  typedef llvm::SmallVector<FunctionDecl *, 4>::iterator function_iterator;
46  typedef llvm::SmallVector<FunctionDecl *, 4>::const_iterator
47    function_const_iterator;
48
49  static OverloadedFunctionDecl *Create(ASTContext &C, DeclContext *DC,
50                                        DeclarationName N);
51
52  /// addOverload - Add an overloaded function FD to this set of
53  /// overloaded functions.
54  void addOverload(FunctionDecl *FD) {
55    assert((!getNumFunctions() || (FD->getDeclContext() == getDeclContext())) &&
56           "Overloaded functions must all be in the same context");
57    assert((FD->getDeclName() == getDeclName() ||
58            isa<CXXConversionDecl>(FD) || isa<CXXConstructorDecl>(FD)) &&
59           "Overloaded functions must have the same name");
60    Functions.push_back(FD);
61  }
62
63  function_iterator function_begin() { return Functions.begin(); }
64  function_iterator function_end() { return Functions.end(); }
65  function_const_iterator function_begin() const { return Functions.begin(); }
66  function_const_iterator function_end() const { return Functions.end(); }
67
68  /// getNumFunctions - the number of overloaded functions stored in
69  /// this set.
70  unsigned getNumFunctions() const { return Functions.size(); }
71
72  /// getFunction - retrieve the ith function in the overload set.
73  const FunctionDecl *getFunction(unsigned i) const {
74    assert(i < getNumFunctions() && "Illegal function #");
75    return Functions[i];
76  }
77  FunctionDecl *getFunction(unsigned i) {
78    assert(i < getNumFunctions() && "Illegal function #");
79    return Functions[i];
80  }
81
82  // getDeclContext - Get the context of these overloaded functions.
83  DeclContext *getDeclContext() {
84    assert(getNumFunctions() > 0 && "Context of an empty overload set");
85    return getFunction(0)->getDeclContext();
86  }
87
88  // Implement isa/cast/dyncast/etc.
89  static bool classof(const Decl *D) {
90    return D->getKind() == OverloadedFunction;
91  }
92  static bool classof(const OverloadedFunctionDecl *D) { return true; }
93
94protected:
95  /// EmitImpl - Serialize this FunctionDecl.  Called by Decl::Emit.
96  virtual void EmitImpl(llvm::Serializer& S) const;
97
98  /// CreateImpl - Deserialize an OverloadedFunctionDecl.  Called by
99  /// Decl::Create.
100  static OverloadedFunctionDecl* CreateImpl(llvm::Deserializer& D,
101                                            ASTContext& C);
102
103  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
104  friend class CXXRecordDecl;
105};
106
107/// CXXFieldDecl - Represents an instance field of a C++ struct/union/class.
108class CXXFieldDecl : public FieldDecl {
109  CXXRecordDecl *Parent;
110
111  CXXFieldDecl(CXXRecordDecl *RD, SourceLocation L, IdentifierInfo *Id,
112               QualType T, Expr *BW = NULL)
113    : FieldDecl(CXXField, L, Id, T, BW), Parent(RD) {}
114public:
115  static CXXFieldDecl *Create(ASTContext &C, CXXRecordDecl *RD,SourceLocation L,
116                              IdentifierInfo *Id, QualType T, Expr *BW = NULL);
117
118  void setAccess(AccessSpecifier AS) { Access = AS; }
119  AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
120  CXXRecordDecl *getParent() const { return Parent; }
121
122  // Implement isa/cast/dyncast/etc.
123  static bool classof(const Decl *D) { return D->getKind() == CXXField; }
124  static bool classof(const CXXFieldDecl *D) { return true; }
125};
126
127/// CXXBaseSpecifier - A base class of a C++ class.
128///
129/// Each CXXBaseSpecifier represents a single, direct base class (or
130/// struct) of a C++ class (or struct). It specifies the type of that
131/// base class, whether it is a virtual or non-virtual base, and what
132/// level of access (public, protected, private) is used for the
133/// derivation. For example:
134///
135/// @code
136///   class A { };
137///   class B { };
138///   class C : public virtual A, protected B { };
139/// @endcode
140///
141/// In this code, C will have two CXXBaseSpecifiers, one for "public
142/// virtual A" and the other for "protected B".
143class CXXBaseSpecifier {
144  /// Range - The source code range that covers the full base
145  /// specifier, including the "virtual" (if present) and access
146  /// specifier (if present).
147  SourceRange Range;
148
149  /// Virtual - Whether this is a virtual base class or not.
150  bool Virtual : 1;
151
152  /// BaseOfClass - Whether this is the base of a class (true) or of a
153  /// struct (false). This determines the mapping from the access
154  /// specifier as written in the source code to the access specifier
155  /// used for semantic analysis.
156  bool BaseOfClass : 1;
157
158  /// Access - Access specifier as written in the source code (which
159  /// may be AS_none). The actual type of data stored here is an
160  /// AccessSpecifier, but we use "unsigned" here to work around a
161  /// VC++ bug.
162  unsigned Access : 2;
163
164  /// BaseType - The type of the base class. This will be a class or
165  /// struct (or a typedef of such).
166  QualType BaseType;
167
168public:
169  CXXBaseSpecifier() { }
170
171  CXXBaseSpecifier(SourceRange R, bool V, bool BC, AccessSpecifier A, QualType T)
172    : Range(R), Virtual(V), BaseOfClass(BC), Access(A), BaseType(T) { }
173
174  /// getSourceRange - Retrieves the source range that contains the
175  /// entire base specifier.
176  SourceRange getSourceRange() const { return Range; }
177
178  /// isVirtual - Determines whether the base class is a virtual base
179  /// class (or not).
180  bool isVirtual() const { return Virtual; }
181
182  /// getAccessSpecifier - Returns the access specifier for this base
183  /// specifier. This is the actual base specifier as used for
184  /// semantic analysis, so the result can never be AS_none. To
185  /// retrieve the access specifier as written in the source code, use
186  /// getAccessSpecifierAsWritten().
187  AccessSpecifier getAccessSpecifier() const {
188    if ((AccessSpecifier)Access == AS_none)
189      return BaseOfClass? AS_private : AS_public;
190    else
191      return (AccessSpecifier)Access;
192  }
193
194  /// getAccessSpecifierAsWritten - Retrieves the access specifier as
195  /// written in the source code (which may mean that no access
196  /// specifier was explicitly written). Use getAccessSpecifier() to
197  /// retrieve the access specifier for use in semantic analysis.
198  AccessSpecifier getAccessSpecifierAsWritten() const {
199    return (AccessSpecifier)Access;
200  }
201
202  /// getType - Retrieves the type of the base class. This type will
203  /// always be an unqualified class type.
204  QualType getType() const { return BaseType; }
205};
206
207/// CXXRecordDecl - Represents a C++ struct/union/class.
208/// CXXRecordDecl differs from RecordDecl in several ways. First, it
209/// is a DeclContext, because it can contain other
210/// declarations. Second, it provides additional C++ fields, including
211/// storage for base classes and constructors.
212class CXXRecordDecl : public RecordDecl, public DeclContext {
213  /// UserDeclaredConstructor - True when this class has a
214  /// user-declared constructor.
215  bool UserDeclaredConstructor : 1;
216
217  /// UserDeclaredCopyConstructor - True when this class has a
218  /// user-defined copy constructor.
219  bool UserDeclaredCopyConstructor : 1;
220
221  /// Aggregate - True when this class is an aggregate.
222  bool Aggregate : 1;
223
224  /// Polymorphic - True when this class is polymorphic, i.e. has at least one
225  /// virtual member or derives from a polymorphic class.
226  bool Polymorphic : 1;
227
228  /// Bases - Base classes of this class.
229  /// FIXME: This is wasted space for a union.
230  CXXBaseSpecifier *Bases;
231
232  /// NumBases - The number of base class specifiers in Bases.
233  unsigned NumBases;
234
235  /// Constructors - Overload set containing the constructors of this
236  /// C++ class. Each of the entries in this overload set is a
237  /// CXXConstructorDecl.
238  OverloadedFunctionDecl Constructors;
239
240  // Destructor - The destructor of this C++ class.
241  CXXDestructorDecl *Destructor;
242
243  /// Conversions - Overload set containing the conversion functions
244  /// of this C++ class (but not its inherited conversion
245  /// functions). Each of the entries in this overload set is a
246  /// CXXConversionDecl.
247  OverloadedFunctionDecl Conversions;
248
249  CXXRecordDecl(TagKind TK, DeclContext *DC,
250                SourceLocation L, IdentifierInfo *Id);
251
252  ~CXXRecordDecl();
253
254public:
255  /// base_class_iterator - Iterator that traverses the base classes
256  /// of a clas.
257  typedef CXXBaseSpecifier*       base_class_iterator;
258
259  /// base_class_const_iterator - Iterator that traverses the base
260  /// classes of a clas.
261  typedef const CXXBaseSpecifier* base_class_const_iterator;
262
263  static CXXRecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC,
264                               SourceLocation L, IdentifierInfo *Id,
265                               CXXRecordDecl* PrevDecl=0);
266
267  /// setBases - Sets the base classes of this struct or class.
268  void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases);
269
270  /// getNumBases - Retrieves the number of base classes of this
271  /// class.
272  unsigned getNumBases() const { return NumBases; }
273
274  base_class_iterator       bases_begin()       { return Bases; }
275  base_class_const_iterator bases_begin() const { return Bases; }
276  base_class_iterator       bases_end()         { return Bases + NumBases; }
277  base_class_const_iterator bases_end()   const { return Bases + NumBases; }
278
279  const CXXFieldDecl *getMember(unsigned i) const {
280    return cast<const CXXFieldDecl>(RecordDecl::getMember(i));
281  }
282  CXXFieldDecl *getMember(unsigned i) {
283    return cast<CXXFieldDecl>(RecordDecl::getMember(i));
284  }
285
286  /// getMember - If the member doesn't exist, or there are no members, this
287  /// function will return 0;
288  CXXFieldDecl *getMember(IdentifierInfo *name) {
289    return cast_or_null<CXXFieldDecl>(RecordDecl::getMember(name));
290  }
291
292  /// getConstructors - Retrieve the overload set containing all of
293  /// the constructors of this class.
294  OverloadedFunctionDecl       *getConstructors()       { return &Constructors; }
295
296  /// getConstructors - Retrieve the overload set containing all of
297  /// the constructors of this class.
298  const OverloadedFunctionDecl *getConstructors() const { return &Constructors; }
299
300  /// hasConstCopyConstructor - Determines whether this class has a
301  /// copy constructor that accepts a const-qualified argument.
302  bool hasConstCopyConstructor(ASTContext &Context) const;
303
304  /// addConstructor - Add another constructor to the list of constructors.
305  void addConstructor(ASTContext &Context, CXXConstructorDecl *ConDecl);
306
307  /// hasUserDeclaredConstructor - Whether this class has any
308  /// user-declared constructors. When true, a default constructor
309  /// will not be implicitly declared.
310  bool hasUserDeclaredConstructor() const { return UserDeclaredConstructor; }
311
312  /// hasUserDeclaredCopyConstructor - Whether this class has a
313  /// user-declared copy constructor. When false, a copy constructor
314  /// will be implicitly declared.
315  bool hasUserDeclaredCopyConstructor() const {
316    return UserDeclaredCopyConstructor;
317  }
318
319  /// getDestructor - Retrieve the destructor for this class.
320  CXXDestructorDecl *getDestructor() const { return Destructor; }
321
322  /// setDestructor - Set the destructor for this class.
323  void setDestructor(CXXDestructorDecl *Destructor) {
324    assert(!this->Destructor && "Already have a destructor!");
325    this->Destructor = Destructor;
326  }
327
328  /// getConversions - Retrieve the overload set containing all of the
329  /// conversion functions in this class.
330  OverloadedFunctionDecl *getConversionFunctions() {
331    return &Conversions;
332  }
333  const OverloadedFunctionDecl *getConversionFunctions() const {
334    return &Conversions;
335  }
336
337  /// addConversionFunction - Add a new conversion function to the
338  /// list of conversion functions.
339  void addConversionFunction(ASTContext &Context, CXXConversionDecl *ConvDecl);
340
341  /// isAggregate - Whether this class is an aggregate (C++
342  /// [dcl.init.aggr]), which is a class with no user-declared
343  /// constructors, no private or protected non-static data members,
344  /// no base classes, and no virtual functions (C++ [dcl.init.aggr]p1).
345  bool isAggregate() const { return Aggregate; }
346
347  /// setAggregate - Set whether this class is an aggregate (C++
348  /// [dcl.init.aggr]).
349  void setAggregate(bool Agg) { Aggregate = Agg; }
350
351  /// isPolymorphic - Whether this class is polymorphic (C++ [class.virtual]),
352  /// which means that the class contains or inherits a virtual function.
353  bool isPolymorphic() const { return Polymorphic; }
354
355  /// setPolymorphic - Set whether this class is polymorphic (C++
356  /// [class.virtual]).
357  void setPolymorphic(bool Poly) { Polymorphic = Poly; }
358
359  /// viewInheritance - Renders and displays an inheritance diagram
360  /// for this C++ class and all of its base classes (transitively) using
361  /// GraphViz.
362  void viewInheritance(ASTContext& Context) const;
363
364  static bool classof(const Decl *D) { return D->getKind() == CXXRecord; }
365  static bool classof(const CXXRecordDecl *D) { return true; }
366  static DeclContext *castToDeclContext(const CXXRecordDecl *D) {
367    return static_cast<DeclContext *>(const_cast<CXXRecordDecl*>(D));
368  }
369  static CXXRecordDecl *castFromDeclContext(const DeclContext *DC) {
370    return static_cast<CXXRecordDecl *>(const_cast<DeclContext*>(DC));
371  }
372
373  virtual void Destroy(ASTContext& C);
374
375protected:
376  /// EmitImpl - Serialize this CXXRecordDecl.  Called by Decl::Emit.
377  // FIXME: Implement this.
378  //virtual void EmitImpl(llvm::Serializer& S) const;
379
380  /// CreateImpl - Deserialize a CXXRecordDecl.  Called by Decl::Create.
381  // FIXME: Implement this.
382  static CXXRecordDecl* CreateImpl(Kind DK, llvm::Deserializer& D, ASTContext& C);
383
384  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
385};
386
387/// CXXMethodDecl - Represents a static or instance method of a
388/// struct/union/class.
389class CXXMethodDecl : public FunctionDecl {
390protected:
391  CXXMethodDecl(Kind DK, CXXRecordDecl *RD, SourceLocation L,
392                DeclarationName N, QualType T,
393                bool isStatic, bool isInline, ScopedDecl *PrevDecl)
394    : FunctionDecl(DK, RD, L, N, T, (isStatic ? Static : None),
395                   isInline, PrevDecl) {}
396
397public:
398  static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD,
399                              SourceLocation L, IdentifierInfo *Id,
400                              QualType T, bool isStatic = false,
401                              bool isInline = false,  ScopedDecl *PrevDecl = 0);
402
403  bool isStatic() const { return getStorageClass() == Static; }
404  bool isInstance() const { return !isStatic(); }
405
406  bool isOutOfLineDefinition() const {
407    return getLexicalDeclContext() != getDeclContext();
408  }
409
410  void setAccess(AccessSpecifier AS) { Access = AS; }
411  AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
412
413  /// getParent - Returns the parent of this method declaration, which
414  /// is the class in which this method is defined.
415  const CXXRecordDecl *getParent() const {
416    return cast<CXXRecordDecl>(FunctionDecl::getParent());
417  }
418
419  /// getParent - Returns the parent of this method declaration, which
420  /// is the class in which this method is defined.
421  CXXRecordDecl *getParent() {
422    return const_cast<CXXRecordDecl *>(
423             cast<CXXRecordDecl>(FunctionDecl::getParent()));
424  }
425
426  /// getThisType - Returns the type of 'this' pointer.
427  /// Should only be called for instance methods.
428  QualType getThisType(ASTContext &C) const;
429
430  unsigned getTypeQualifiers() const {
431    return getType()->getAsFunctionTypeProto()->getTypeQuals();
432  }
433
434  // Implement isa/cast/dyncast/etc.
435  static bool classof(const Decl *D) {
436    return D->getKind() >= CXXMethod && D->getKind() <= CXXConversion;
437  }
438  static bool classof(const CXXMethodDecl *D) { return true; }
439
440protected:
441  /// EmitImpl - Serialize this CXXMethodDecl.  Called by Decl::Emit.
442  // FIXME: Implement this.
443  //virtual void EmitImpl(llvm::Serializer& S) const;
444
445  /// CreateImpl - Deserialize a CXXMethodDecl.  Called by Decl::Create.
446  // FIXME: Implement this.
447  static CXXMethodDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
448
449  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
450};
451
452/// CXXBaseOrMemberInitializer - Represents a C++ base or member
453/// initializer, which is part of a constructor initializer that
454/// initializes one non-static member variable or one base class. For
455/// example, in the following, both 'A(a)' and 'f(3.14159)' are member
456/// initializers:
457///
458/// @code
459/// class A { };
460/// class B : public A {
461///   float f;
462/// public:
463///   B(A& a) : A(a), f(3.14159) { }
464/// };
465class CXXBaseOrMemberInitializer {
466  /// BaseOrMember - This points to the entity being initialized,
467  /// which is either a base class (a Type) or a non-static data
468  /// member (a CXXFieldDecl). When the low bit is 1, it's a base
469  /// class; when the low bit is 0, it's a member.
470  uintptr_t BaseOrMember;
471
472  /// Args - The arguments used to initialize the base or member.
473  Expr **Args;
474  unsigned NumArgs;
475
476public:
477  /// CXXBaseOrMemberInitializer - Creates a new base-class initializer.
478  explicit
479  CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs);
480
481  /// CXXBaseOrMemberInitializer - Creates a new member initializer.
482  explicit
483  CXXBaseOrMemberInitializer(CXXFieldDecl *Member, Expr **Args, unsigned NumArgs);
484
485  /// ~CXXBaseOrMemberInitializer - Destroy the base or member initializer.
486  ~CXXBaseOrMemberInitializer();
487
488  /// arg_iterator - Iterates through the member initialization
489  /// arguments.
490  typedef Expr **arg_iterator;
491
492  /// arg_const_iterator - Iterates through the member initialization
493  /// arguments.
494  typedef Expr * const * arg_const_iterator;
495
496  /// isBaseInitializer - Returns true when this initializer is
497  /// initializing a base class.
498  bool isBaseInitializer() const { return (BaseOrMember & 0x1) != 0; }
499
500  /// isMemberInitializer - Returns true when this initializer is
501  /// initializing a non-static data member.
502  bool isMemberInitializer() const { return (BaseOrMember & 0x1) == 0; }
503
504  /// getBaseClass - If this is a base class initializer, returns the
505  /// type used to specify the initializer. The resulting type will be
506  /// a class type or a typedef of a class type. If this is not a base
507  /// class initializer, returns NULL.
508  Type *getBaseClass() {
509    if (isBaseInitializer())
510      return reinterpret_cast<Type*>(BaseOrMember & ~0x01);
511    else
512      return 0;
513  }
514
515  /// getBaseClass - If this is a base class initializer, returns the
516  /// type used to specify the initializer. The resulting type will be
517  /// a class type or a typedef of a class type. If this is not a base
518  /// class initializer, returns NULL.
519  const Type *getBaseClass() const {
520    if (isBaseInitializer())
521      return reinterpret_cast<const Type*>(BaseOrMember & ~0x01);
522    else
523      return 0;
524  }
525
526  /// getMember - If this is a member initializer, returns the
527  /// declaration of the non-static data member being
528  /// initialized. Otherwise, returns NULL.
529  CXXFieldDecl *getMember() {
530    if (isMemberInitializer())
531      return reinterpret_cast<CXXFieldDecl *>(BaseOrMember);
532    else
533      return 0;
534  }
535
536  /// begin() - Retrieve an iterator to the first initializer argument.
537  arg_iterator       begin()       { return Args; }
538  /// begin() - Retrieve an iterator to the first initializer argument.
539  arg_const_iterator begin() const { return Args; }
540
541  /// end() - Retrieve an iterator past the last initializer argument.
542  arg_iterator       end()       { return Args + NumArgs; }
543  /// end() - Retrieve an iterator past the last initializer argument.
544  arg_const_iterator end() const { return Args + NumArgs; }
545
546  /// getNumArgs - Determine the number of arguments used to
547  /// initialize the member or base.
548  unsigned getNumArgs() const { return NumArgs; }
549};
550
551/// CXXConstructorDecl - Represents a C++ constructor within a
552/// class. For example:
553///
554/// @code
555/// class X {
556/// public:
557///   explicit X(int); // represented by a CXXConstructorDecl.
558/// };
559/// @endcode
560class CXXConstructorDecl : public CXXMethodDecl {
561  /// Explicit - Whether this constructor is explicit.
562  bool Explicit : 1;
563
564  /// ImplicitlyDeclared - Whether this constructor was implicitly
565  /// declared. When false, the constructor was declared by the user.
566  bool ImplicitlyDeclared : 1;
567
568  /// ImplicitlyDefined - Whether this constructor was implicitly
569  /// defined by the compiler. When false, the constructor was defined
570  /// by the user. In C++03, this flag will have the same value as
571  /// ImplicitlyDeclared. In C++0x, however, a constructor that is
572  /// explicitly defaulted (i.e., defined with " = default") will have
573  /// @c !ImplicitlyDeclared && ImplicitlyDefined.
574  bool ImplicitlyDefined : 1;
575
576  /// FIXME: Add support for base and member initializers.
577
578  CXXConstructorDecl(CXXRecordDecl *RD, SourceLocation L,
579                     DeclarationName N, QualType T,
580                     bool isExplicit, bool isInline, bool isImplicitlyDeclared)
581    : CXXMethodDecl(CXXConstructor, RD, L, N, T, false, isInline,
582                    /*PrevDecl=*/0),
583      Explicit(isExplicit), ImplicitlyDeclared(isImplicitlyDeclared),
584      ImplicitlyDefined(false) { }
585
586public:
587  static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
588                                    SourceLocation L, DeclarationName N,
589                                    QualType T, bool isExplicit,
590                                    bool isInline, bool isImplicitlyDeclared);
591
592  /// isExplicit - Whether this constructor was marked "explicit" or not.
593  bool isExplicit() const { return Explicit; }
594
595  /// isImplicitlyDeclared - Whether this constructor was implicitly
596  /// declared. If false, then this constructor was explicitly
597  /// declared by the user.
598  bool isImplicitlyDeclared() const { return ImplicitlyDeclared; }
599
600  /// isImplicitlyDefined - Whether this constructor was implicitly
601  /// defined. If false, then this constructor was defined by the
602  /// user. This operation can only be invoked if the constructor has
603  /// already been defined.
604  bool isImplicitlyDefined() const {
605    assert(getBody() != 0 &&
606           "Can only get the implicit-definition flag once the constructor has been defined");
607    return ImplicitlyDefined;
608  }
609
610  /// setImplicitlyDefined - Set whether this constructor was
611  /// implicitly defined or not.
612  void setImplicitlyDefined(bool ID) {
613    assert(getBody() != 0 &&
614           "Can only set the implicit-definition flag once the constructor has been defined");
615    ImplicitlyDefined = ID;
616  }
617
618  /// isDefaultConstructor - Whether this constructor is a default
619  /// constructor (C++ [class.ctor]p5), which can be used to
620  /// default-initialize a class of this type.
621  bool isDefaultConstructor() const;
622
623  /// isCopyConstructor - Whether this constructor is a copy
624  /// constructor (C++ [class.copy]p2, which can be used to copy the
625  /// class. @p TypeQuals will be set to the qualifiers on the
626  /// argument type. For example, @p TypeQuals would be set to @c
627  /// QualType::Const for the following copy constructor:
628  ///
629  /// @code
630  /// class X {
631  /// public:
632  ///   X(const X&);
633  /// };
634  /// @endcode
635  bool isCopyConstructor(ASTContext &Context, unsigned &TypeQuals) const;
636
637  /// isCopyConstructor - Whether this constructor is a copy
638  /// constructor (C++ [class.copy]p2, which can be used to copy the
639  /// class.
640  bool isCopyConstructor(ASTContext &Context) const {
641    unsigned TypeQuals = 0;
642    return isCopyConstructor(Context, TypeQuals);
643  }
644
645  /// isConvertingConstructor - Whether this constructor is a
646  /// converting constructor (C++ [class.conv.ctor]), which can be
647  /// used for user-defined conversions.
648  bool isConvertingConstructor() const;
649
650  // Implement isa/cast/dyncast/etc.
651  static bool classof(const Decl *D) {
652    return D->getKind() == CXXConstructor;
653  }
654  static bool classof(const CXXConstructorDecl *D) { return true; }
655
656  /// EmitImpl - Serialize this CXXConstructorDecl.  Called by Decl::Emit.
657  // FIXME: Implement this.
658  //virtual void EmitImpl(llvm::Serializer& S) const;
659
660  /// CreateImpl - Deserialize a CXXConstructorDecl.  Called by Decl::Create.
661  // FIXME: Implement this.
662  static CXXConstructorDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
663};
664
665/// CXXDestructorDecl - Represents a C++ destructor within a
666/// class. For example:
667///
668/// @code
669/// class X {
670/// public:
671///   ~X(); // represented by a CXXDestructorDecl.
672/// };
673/// @endcode
674class CXXDestructorDecl : public CXXMethodDecl {
675  /// ImplicitlyDeclared - Whether this destructor was implicitly
676  /// declared. When false, the destructor was declared by the user.
677  bool ImplicitlyDeclared : 1;
678
679  /// ImplicitlyDefined - Whether this destructor was implicitly
680  /// defined by the compiler. When false, the destructor was defined
681  /// by the user. In C++03, this flag will have the same value as
682  /// ImplicitlyDeclared. In C++0x, however, a destructor that is
683  /// explicitly defaulted (i.e., defined with " = default") will have
684  /// @c !ImplicitlyDeclared && ImplicitlyDefined.
685  bool ImplicitlyDefined : 1;
686
687  CXXDestructorDecl(CXXRecordDecl *RD, SourceLocation L,
688                    DeclarationName N, QualType T,
689                    bool isInline, bool isImplicitlyDeclared)
690    : CXXMethodDecl(CXXDestructor, RD, L, N, T, false, isInline,
691                    /*PrevDecl=*/0),
692      ImplicitlyDeclared(isImplicitlyDeclared),
693      ImplicitlyDefined(false) { }
694
695public:
696  static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
697                                   SourceLocation L, DeclarationName N,
698                                   QualType T, bool isInline,
699                                   bool isImplicitlyDeclared);
700
701  /// isImplicitlyDeclared - Whether this destructor was implicitly
702  /// declared. If false, then this destructor was explicitly
703  /// declared by the user.
704  bool isImplicitlyDeclared() const { return ImplicitlyDeclared; }
705
706  /// isImplicitlyDefined - Whether this destructor was implicitly
707  /// defined. If false, then this destructor was defined by the
708  /// user. This operation can only be invoked if the destructor has
709  /// already been defined.
710  bool isImplicitlyDefined() const {
711    assert(getBody() != 0 &&
712           "Can only get the implicit-definition flag once the destructor has been defined");
713    return ImplicitlyDefined;
714  }
715
716  /// setImplicitlyDefined - Set whether this destructor was
717  /// implicitly defined or not.
718  void setImplicitlyDefined(bool ID) {
719    assert(getBody() != 0 &&
720           "Can only set the implicit-definition flag once the destructor has been defined");
721    ImplicitlyDefined = ID;
722  }
723
724  // Implement isa/cast/dyncast/etc.
725  static bool classof(const Decl *D) {
726    return D->getKind() == CXXDestructor;
727  }
728  static bool classof(const CXXDestructorDecl *D) { return true; }
729
730  /// EmitImpl - Serialize this CXXDestructorDecl.  Called by Decl::Emit.
731  // FIXME: Implement this.
732  //virtual void EmitImpl(llvm::Serializer& S) const;
733
734  /// CreateImpl - Deserialize a CXXDestructorDecl.  Called by Decl::Create.
735  // FIXME: Implement this.
736  static CXXDestructorDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
737};
738
739/// CXXConversionDecl - Represents a C++ conversion function within a
740/// class. For example:
741///
742/// @code
743/// class X {
744/// public:
745///   operator bool();
746/// };
747/// @endcode
748class CXXConversionDecl : public CXXMethodDecl {
749  /// Explicit - Whether this conversion function is marked
750  /// "explicit", meaning that it can only be applied when the user
751  /// explicitly wrote a cast. This is a C++0x feature.
752  bool Explicit : 1;
753
754  CXXConversionDecl(CXXRecordDecl *RD, SourceLocation L,
755                    DeclarationName N, QualType T,
756                    bool isInline, bool isExplicit)
757    : CXXMethodDecl(CXXConversion, RD, L, N, T, false, isInline,
758                    /*PrevDecl=*/0),
759      Explicit(isExplicit) { }
760
761public:
762  static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD,
763                                   SourceLocation L, DeclarationName N,
764                                   QualType T, bool isInline,
765                                   bool isExplicit);
766
767  /// isExplicit - Whether this is an explicit conversion operator
768  /// (C++0x only). Explicit conversion operators are only considered
769  /// when the user has explicitly written a cast.
770  bool isExplicit() const { return Explicit; }
771
772  /// getConversionType - Returns the type that this conversion
773  /// function is converting to.
774  QualType getConversionType() const {
775    return getType()->getAsFunctionType()->getResultType();
776  }
777
778  // Implement isa/cast/dyncast/etc.
779  static bool classof(const Decl *D) {
780    return D->getKind() == CXXConversion;
781  }
782  static bool classof(const CXXConversionDecl *D) { return true; }
783
784  /// EmitImpl - Serialize this CXXConversionDecl.  Called by Decl::Emit.
785  // FIXME: Implement this.
786  //virtual void EmitImpl(llvm::Serializer& S) const;
787
788  /// CreateImpl - Deserialize a CXXConversionDecl.  Called by Decl::Create.
789  // FIXME: Implement this.
790  static CXXConversionDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
791};
792
793/// CXXClassVarDecl - Represents a static data member of a struct/union/class.
794class CXXClassVarDecl : public VarDecl {
795
796  CXXClassVarDecl(CXXRecordDecl *RD, SourceLocation L,
797              IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl)
798    : VarDecl(CXXClassVar, RD, L, Id, T, None, PrevDecl) {}
799public:
800  static CXXClassVarDecl *Create(ASTContext &C, CXXRecordDecl *RD,
801                             SourceLocation L,IdentifierInfo *Id,
802                             QualType T, ScopedDecl *PrevDecl);
803
804  void setAccess(AccessSpecifier AS) { Access = AS; }
805  AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
806
807  // Implement isa/cast/dyncast/etc.
808  static bool classof(const Decl *D) { return D->getKind() == CXXClassVar; }
809  static bool classof(const CXXClassVarDecl *D) { return true; }
810
811protected:
812  /// EmitImpl - Serialize this CXXClassVarDecl. Called by Decl::Emit.
813  // FIXME: Implement this.
814  //virtual void EmitImpl(llvm::Serializer& S) const;
815
816  /// CreateImpl - Deserialize a CXXClassVarDecl.  Called by Decl::Create.
817  // FIXME: Implement this.
818  static CXXClassVarDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
819
820  friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
821};
822
823
824/// CXXClassMemberWrapper - A wrapper class for C++ class member decls.
825/// Common functions like set/getAccess are included here to avoid bloating
826/// the interface of non-C++ specific decl classes, like NamedDecl.
827class CXXClassMemberWrapper {
828  Decl *MD;
829
830public:
831  CXXClassMemberWrapper(Decl *D) : MD(D) {
832    assert(isMember(D) && "Not a C++ class member!");
833  }
834
835  AccessSpecifier getAccess() const {
836    return AccessSpecifier(MD->Access);
837  }
838
839  void setAccess(AccessSpecifier AS) {
840    assert(AS != AS_none && "Access must be specified.");
841    MD->Access = AS;
842  }
843
844  CXXRecordDecl *getParent() const {
845    if (ScopedDecl *SD = dyn_cast<ScopedDecl>(MD)) {
846      return cast<CXXRecordDecl>(SD->getDeclContext());
847    }
848    return cast<CXXFieldDecl>(MD)->getParent();
849  }
850
851  static bool isMember(Decl *D) {
852    if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
853      return isa<CXXRecordDecl>(SD->getDeclContext());
854    }
855    return isa<CXXFieldDecl>(D);
856  }
857};
858
859/// LinkageSpecDecl - This represents a linkage specification.  For example:
860///   extern "C" void foo();
861///
862class LinkageSpecDecl : public Decl {
863public:
864  /// LanguageIDs - Used to represent the language in a linkage
865  /// specification.  The values are part of the serialization abi for
866  /// ASTs and cannot be changed without altering that abi.  To help
867  /// ensure a stable abi for this, we choose the DW_LANG_ encodings
868  /// from the dwarf standard.
869  enum LanguageIDs { lang_c = /* DW_LANG_C */ 0x0002,
870  lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004 };
871private:
872  /// Language - The language for this linkage specification.
873  LanguageIDs Language;
874  /// D - This is the Decl of the linkage specification.
875  Decl *D;
876
877  LinkageSpecDecl(SourceLocation L, LanguageIDs lang, Decl *d)
878  : Decl(LinkageSpec, L), Language(lang), D(d) {}
879public:
880  static LinkageSpecDecl *Create(ASTContext &C, SourceLocation L,
881                                 LanguageIDs Lang, Decl *D);
882
883  LanguageIDs getLanguage() const { return Language; }
884  const Decl *getDecl() const { return D; }
885  Decl *getDecl() { return D; }
886
887  static bool classof(const Decl *D) {
888    return D->getKind() == LinkageSpec;
889  }
890  static bool classof(const LinkageSpecDecl *D) { return true; }
891
892protected:
893  void EmitInRec(llvm::Serializer& S) const;
894  void ReadInRec(llvm::Deserializer& D, ASTContext& C);
895};
896
897
898
899} // end namespace clang
900
901#endif
902