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