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