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