DeclCXX.h revision 194613
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 {
21
22class ClassTemplateDecl;
23class CXXRecordDecl;
24class CXXConstructorDecl;
25class CXXDestructorDecl;
26class CXXConversionDecl;
27class CXXMethodDecl;
28class ClassTemplateSpecializationDecl;
29
30/// OverloadedFunctionDecl - An instance of this class represents a
31/// set of overloaded functions. All of the functions have the same
32/// name and occur within the same scope.
33///
34/// An OverloadedFunctionDecl has no ownership over the FunctionDecl
35/// nodes it contains. Rather, the FunctionDecls are owned by the
36/// enclosing scope (which also owns the OverloadedFunctionDecl
37/// node). OverloadedFunctionDecl is used primarily to store a set of
38/// overloaded functions for name lookup.
39class OverloadedFunctionDecl : public NamedDecl {
40protected:
41  OverloadedFunctionDecl(DeclContext *DC, DeclarationName N)
42    : NamedDecl(OverloadedFunction, DC, SourceLocation(), N) { }
43
44  /// Functions - the set of overloaded functions contained in this
45  /// overload set.
46  llvm::SmallVector<FunctionDecl *, 4> Functions;
47
48  // FIXME: This should go away when we stop using
49  // OverloadedFunctionDecl to store conversions in CXXRecordDecl.
50  friend class CXXRecordDecl;
51
52public:
53  typedef llvm::SmallVector<FunctionDecl *, 4>::iterator function_iterator;
54  typedef llvm::SmallVector<FunctionDecl *, 4>::const_iterator
55    function_const_iterator;
56
57  static OverloadedFunctionDecl *Create(ASTContext &C, DeclContext *DC,
58                                        DeclarationName N);
59
60  /// addOverload - Add an overloaded function FD to this set of
61  /// overloaded functions.
62  void addOverload(FunctionDecl *FD) {
63    assert((FD->getDeclName() == getDeclName() ||
64            isa<CXXConversionDecl>(FD) || isa<CXXConstructorDecl>(FD)) &&
65           "Overloaded functions must have the same name");
66    Functions.push_back(FD);
67
68    // An overloaded function declaration always has the location of
69    // the most-recently-added function declaration.
70    if (FD->getLocation().isValid())
71      this->setLocation(FD->getLocation());
72  }
73
74  function_iterator function_begin() { return Functions.begin(); }
75  function_iterator function_end() { return Functions.end(); }
76  function_const_iterator function_begin() const { return Functions.begin(); }
77  function_const_iterator function_end() const { return Functions.end(); }
78
79  /// getNumFunctions - the number of overloaded functions stored in
80  /// this set.
81  unsigned getNumFunctions() const { return Functions.size(); }
82
83  /// getFunction - retrieve the ith function in the overload set.
84  const FunctionDecl *getFunction(unsigned i) const {
85    assert(i < getNumFunctions() && "Illegal function #");
86    return Functions[i];
87  }
88  FunctionDecl *getFunction(unsigned i) {
89    assert(i < getNumFunctions() && "Illegal function #");
90    return Functions[i];
91  }
92
93  // getDeclContext - Get the context of these overloaded functions.
94  DeclContext *getDeclContext() {
95    assert(getNumFunctions() > 0 && "Context of an empty overload set");
96    return getFunction(0)->getDeclContext();
97  }
98
99  // Implement isa/cast/dyncast/etc.
100  static bool classof(const Decl *D) {
101    return D->getKind() == OverloadedFunction;
102  }
103  static bool classof(const OverloadedFunctionDecl *D) { return true; }
104};
105
106/// CXXBaseSpecifier - A base class of a C++ class.
107///
108/// Each CXXBaseSpecifier represents a single, direct base class (or
109/// struct) of a C++ class (or struct). It specifies the type of that
110/// base class, whether it is a virtual or non-virtual base, and what
111/// level of access (public, protected, private) is used for the
112/// derivation. For example:
113///
114/// @code
115///   class A { };
116///   class B { };
117///   class C : public virtual A, protected B { };
118/// @endcode
119///
120/// In this code, C will have two CXXBaseSpecifiers, one for "public
121/// virtual A" and the other for "protected B".
122class CXXBaseSpecifier {
123  /// Range - The source code range that covers the full base
124  /// specifier, including the "virtual" (if present) and access
125  /// specifier (if present).
126  SourceRange Range;
127
128  /// Virtual - Whether this is a virtual base class or not.
129  bool Virtual : 1;
130
131  /// BaseOfClass - Whether this is the base of a class (true) or of a
132  /// struct (false). This determines the mapping from the access
133  /// specifier as written in the source code to the access specifier
134  /// used for semantic analysis.
135  bool BaseOfClass : 1;
136
137  /// Access - Access specifier as written in the source code (which
138  /// may be AS_none). The actual type of data stored here is an
139  /// AccessSpecifier, but we use "unsigned" here to work around a
140  /// VC++ bug.
141  unsigned Access : 2;
142
143  /// BaseType - The type of the base class. This will be a class or
144  /// struct (or a typedef of such).
145  QualType BaseType;
146
147public:
148  CXXBaseSpecifier() { }
149
150  CXXBaseSpecifier(SourceRange R, bool V, bool BC, AccessSpecifier A, QualType T)
151    : Range(R), Virtual(V), BaseOfClass(BC), Access(A), BaseType(T) { }
152
153  /// getSourceRange - Retrieves the source range that contains the
154  /// entire base specifier.
155  SourceRange getSourceRange() const { return Range; }
156
157  /// isVirtual - Determines whether the base class is a virtual base
158  /// class (or not).
159  bool isVirtual() const { return Virtual; }
160
161  /// getAccessSpecifier - Returns the access specifier for this base
162  /// specifier. This is the actual base specifier as used for
163  /// semantic analysis, so the result can never be AS_none. To
164  /// retrieve the access specifier as written in the source code, use
165  /// getAccessSpecifierAsWritten().
166  AccessSpecifier getAccessSpecifier() const {
167    if ((AccessSpecifier)Access == AS_none)
168      return BaseOfClass? AS_private : AS_public;
169    else
170      return (AccessSpecifier)Access;
171  }
172
173  /// getAccessSpecifierAsWritten - Retrieves the access specifier as
174  /// written in the source code (which may mean that no access
175  /// specifier was explicitly written). Use getAccessSpecifier() to
176  /// retrieve the access specifier for use in semantic analysis.
177  AccessSpecifier getAccessSpecifierAsWritten() const {
178    return (AccessSpecifier)Access;
179  }
180
181  /// getType - Retrieves the type of the base class. This type will
182  /// always be an unqualified class type.
183  QualType getType() const { return BaseType; }
184};
185
186/// CXXRecordDecl - Represents a C++ struct/union/class.
187/// FIXME: This class will disappear once we've properly taught RecordDecl
188/// to deal with C++-specific things.
189class CXXRecordDecl : public RecordDecl {
190  /// UserDeclaredConstructor - True when this class has a
191  /// user-declared constructor.
192  bool UserDeclaredConstructor : 1;
193
194  /// UserDeclaredCopyConstructor - True when this class has a
195  /// user-declared copy constructor.
196  bool UserDeclaredCopyConstructor : 1;
197
198  /// UserDeclaredCopyAssignment - True when this class has a
199  /// user-declared copy assignment operator.
200  bool UserDeclaredCopyAssignment : 1;
201
202  /// UserDeclaredDestructor - True when this class has a
203  /// user-declared destructor.
204  bool UserDeclaredDestructor : 1;
205
206  /// Aggregate - True when this class is an aggregate.
207  bool Aggregate : 1;
208
209  /// PlainOldData - True when this class is a POD-type.
210  bool PlainOldData : 1;
211
212  /// Polymorphic - True when this class is polymorphic, i.e. has at least one
213  /// virtual member or derives from a polymorphic class.
214  bool Polymorphic : 1;
215
216  /// Abstract - True when this class is abstract, i.e. has at least one
217  /// pure virtual function, (that can come from a base class).
218  bool Abstract : 1;
219
220  /// HasTrivialConstructor - True when this class has a trivial constructor
221  bool HasTrivialConstructor : 1;
222
223  /// HasTrivialDestructor - True when this class has a trivial destructor
224  bool HasTrivialDestructor : 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  /// Conversions - Overload set containing the conversion functions
234  /// of this C++ class (but not its inherited conversion
235  /// functions). Each of the entries in this overload set is a
236  /// CXXConversionDecl.
237  OverloadedFunctionDecl Conversions;
238
239  /// \brief The template or declaration that this declaration
240  /// describes or was instantiated from, respectively.
241  ///
242  /// For non-templates, this value will be NULL. For record
243  /// declarations that describe a class template, this will be a
244  /// pointer to a ClassTemplateDecl. For member
245  /// classes of class template specializations, this will be the
246  /// RecordDecl from which the member class was instantiated.
247  llvm::PointerUnion<ClassTemplateDecl*, CXXRecordDecl*>
248    TemplateOrInstantiation;
249
250protected:
251  CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
252                SourceLocation L, IdentifierInfo *Id);
253
254  ~CXXRecordDecl();
255
256public:
257  /// base_class_iterator - Iterator that traverses the base classes
258  /// of a clas.
259  typedef CXXBaseSpecifier*       base_class_iterator;
260
261  /// base_class_const_iterator - Iterator that traverses the base
262  /// classes of a clas.
263  typedef const CXXBaseSpecifier* base_class_const_iterator;
264
265  static CXXRecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC,
266                               SourceLocation L, IdentifierInfo *Id,
267                               CXXRecordDecl* PrevDecl=0,
268                               bool DelayTypeCreation = false);
269
270  /// setBases - Sets the base classes of this struct or class.
271  void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases);
272
273  /// getNumBases - Retrieves the number of base classes of this
274  /// class.
275  unsigned getNumBases() const { return NumBases; }
276
277  base_class_iterator       bases_begin()       { return Bases; }
278  base_class_const_iterator bases_begin() const { return Bases; }
279  base_class_iterator       bases_end()         { return Bases + NumBases; }
280  base_class_const_iterator bases_end()   const { return Bases + NumBases; }
281
282  /// hasConstCopyConstructor - Determines whether this class has a
283  /// copy constructor that accepts a const-qualified argument.
284  bool hasConstCopyConstructor(ASTContext &Context) const;
285
286  /// hasConstCopyAssignment - Determines whether this class has a
287  /// copy assignment operator that accepts a const-qualified argument.
288  bool hasConstCopyAssignment(ASTContext &Context) const;
289
290  /// addedConstructor - Notify the class that another constructor has
291  /// been added. This routine helps maintain information about the
292  /// class based on which constructors have been added.
293  void addedConstructor(ASTContext &Context, CXXConstructorDecl *ConDecl);
294
295  /// hasUserDeclaredConstructor - Whether this class has any
296  /// user-declared constructors. When true, a default constructor
297  /// will not be implicitly declared.
298  bool hasUserDeclaredConstructor() const { return UserDeclaredConstructor; }
299
300  /// hasUserDeclaredCopyConstructor - Whether this class has a
301  /// user-declared copy constructor. When false, a copy constructor
302  /// will be implicitly declared.
303  bool hasUserDeclaredCopyConstructor() const {
304    return UserDeclaredCopyConstructor;
305  }
306
307  /// addedAssignmentOperator - Notify the class that another assignment
308  /// operator has been added. This routine helps maintain information about the
309   /// class based on which operators have been added.
310  void addedAssignmentOperator(ASTContext &Context, CXXMethodDecl *OpDecl);
311
312  /// hasUserDeclaredCopyAssignment - Whether this class has a
313  /// user-declared copy assignment operator. When false, a copy
314  /// assigment operator will be implicitly declared.
315  bool hasUserDeclaredCopyAssignment() const {
316    return UserDeclaredCopyAssignment;
317  }
318
319  /// hasUserDeclaredDestructor - Whether this class has a
320  /// user-declared destructor. When false, a destructor will be
321  /// implicitly declared.
322  bool hasUserDeclaredDestructor() const { return UserDeclaredDestructor; }
323
324  /// setUserDeclaredDestructor - Set whether this class has a
325  /// user-declared destructor. If not set by the time the class is
326  /// fully defined, a destructor will be implicitly declared.
327  void setUserDeclaredDestructor(bool UCD) {
328    UserDeclaredDestructor = UCD;
329  }
330
331  /// getConversions - Retrieve the overload set containing all of the
332  /// conversion functions in this class.
333  OverloadedFunctionDecl *getConversionFunctions() {
334    return &Conversions;
335  }
336  const OverloadedFunctionDecl *getConversionFunctions() const {
337    return &Conversions;
338  }
339
340  /// addConversionFunction - Add a new conversion function to the
341  /// list of conversion functions.
342  void addConversionFunction(ASTContext &Context, CXXConversionDecl *ConvDecl);
343
344  /// isAggregate - Whether this class is an aggregate (C++
345  /// [dcl.init.aggr]), which is a class with no user-declared
346  /// constructors, no private or protected non-static data members,
347  /// no base classes, and no virtual functions (C++ [dcl.init.aggr]p1).
348  bool isAggregate() const { return Aggregate; }
349
350  /// setAggregate - Set whether this class is an aggregate (C++
351  /// [dcl.init.aggr]).
352  void setAggregate(bool Agg) { Aggregate = Agg; }
353
354  /// isPOD - Whether this class is a POD-type (C++ [class]p4), which is a class
355  /// that is an aggregate that has no non-static non-POD data members, no
356  /// reference data members, no user-defined copy assignment operator and no
357  /// user-defined destructor.
358  bool isPOD() const { return PlainOldData; }
359
360  /// setPOD - Set whether this class is a POD-type (C++ [class]p4).
361  void setPOD(bool POD) { PlainOldData = POD; }
362
363  /// isPolymorphic - Whether this class is polymorphic (C++ [class.virtual]),
364  /// which means that the class contains or inherits a virtual function.
365  bool isPolymorphic() const { return Polymorphic; }
366
367  /// setPolymorphic - Set whether this class is polymorphic (C++
368  /// [class.virtual]).
369  void setPolymorphic(bool Poly) { Polymorphic = Poly; }
370
371  /// isAbstract - Whether this class is abstract (C++ [class.abstract]),
372  /// which means that the class contains or inherits a pure virtual function.
373  bool isAbstract() const { return Abstract; }
374
375  /// setAbstract - Set whether this class is abstract (C++ [class.abstract])
376  void setAbstract(bool Abs) { Abstract = Abs; }
377
378  // hasTrivialConstructor - Whether this class has a trivial constructor
379  // (C++ [class.ctor]p5)
380  bool hasTrivialConstructor() const { return HasTrivialConstructor; }
381
382  // setHasTrivialConstructor - Set whether this class has a trivial constructor
383  // (C++ [class.ctor]p5)
384  void setHasTrivialConstructor(bool TC) { HasTrivialConstructor = TC; }
385
386  // hasTrivialDestructor - Whether this class has a trivial destructor
387  // (C++ [class.dtor]p3)
388  bool hasTrivialDestructor() const { return HasTrivialDestructor; }
389
390  // setHasTrivialDestructor - Set whether this class has a trivial destructor
391  // (C++ [class.dtor]p3)
392  void setHasTrivialDestructor(bool TC) { HasTrivialDestructor = TC; }
393
394  /// \brief If this record is an instantiation of a member class,
395  /// retrieves the member class from which it was instantiated.
396  ///
397  /// This routine will return non-NULL for (non-templated) member
398  /// classes of class templates. For example, given:
399  ///
400  /// \code
401  /// template<typename T>
402  /// struct X {
403  ///   struct A { };
404  /// };
405  /// \endcode
406  ///
407  /// The declaration for X<int>::A is a (non-templated) CXXRecordDecl
408  /// whose parent is the class template specialization X<int>. For
409  /// this declaration, getInstantiatedFromMemberClass() will return
410  /// the CXXRecordDecl X<T>::A. When a complete definition of
411  /// X<int>::A is required, it will be instantiated from the
412  /// declaration returned by getInstantiatedFromMemberClass().
413  CXXRecordDecl *getInstantiatedFromMemberClass() const {
414    return TemplateOrInstantiation.dyn_cast<CXXRecordDecl*>();
415  }
416
417  /// \brief Specify that this record is an instantiation of the
418  /// member class RD.
419  void setInstantiationOfMemberClass(CXXRecordDecl *RD) {
420    TemplateOrInstantiation = RD;
421  }
422
423  /// \brief Retrieves the class template that is described by this
424  /// class declaration.
425  ///
426  /// Every class template is represented as a ClassTemplateDecl and a
427  /// CXXRecordDecl. The former contains template properties (such as
428  /// the template parameter lists) while the latter contains the
429  /// actual description of the template's
430  /// contents. ClassTemplateDecl::getTemplatedDecl() retrieves the
431  /// CXXRecordDecl that from a ClassTemplateDecl, while
432  /// getDescribedClassTemplate() retrieves the ClassTemplateDecl from
433  /// a CXXRecordDecl.
434  ClassTemplateDecl *getDescribedClassTemplate() const {
435    return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl*>();
436  }
437
438  void setDescribedClassTemplate(ClassTemplateDecl *Template) {
439    TemplateOrInstantiation = Template;
440  }
441
442  /// getDefaultConstructor - Returns the default constructor for this class
443  CXXConstructorDecl *getDefaultConstructor(ASTContext &Context);
444
445  /// getDestructor - Returns the destructor decl for this class.
446  const CXXDestructorDecl *getDestructor(ASTContext &Context);
447
448  /// viewInheritance - Renders and displays an inheritance diagram
449  /// for this C++ class and all of its base classes (transitively) using
450  /// GraphViz.
451  void viewInheritance(ASTContext& Context) const;
452
453  static bool classof(const Decl *D) {
454    return D->getKind() == CXXRecord ||
455           D->getKind() == ClassTemplateSpecialization ||
456           D->getKind() == ClassTemplatePartialSpecialization;
457  }
458  static bool classof(const CXXRecordDecl *D) { return true; }
459  static bool classof(const ClassTemplateSpecializationDecl *D) {
460    return true;
461  }
462};
463
464/// CXXMethodDecl - Represents a static or instance method of a
465/// struct/union/class.
466class CXXMethodDecl : public FunctionDecl {
467protected:
468  CXXMethodDecl(Kind DK, CXXRecordDecl *RD, SourceLocation L,
469                DeclarationName N, QualType T,
470                bool isStatic, bool isInline)
471    : FunctionDecl(DK, RD, L, N, T, (isStatic ? Static : None),
472                   isInline) {}
473
474public:
475  static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD,
476                              SourceLocation L, DeclarationName N,
477                              QualType T, bool isStatic = false,
478                              bool isInline = false);
479
480  bool isStatic() const { return getStorageClass() == Static; }
481  bool isInstance() const { return !isStatic(); }
482
483  bool isVirtual() const {
484    return isVirtualAsWritten() ||
485      (begin_overridden_methods() != end_overridden_methods());
486  }
487
488  ///
489  void addOverriddenMethod(const CXXMethodDecl *MD);
490
491  typedef const CXXMethodDecl ** method_iterator;
492
493  method_iterator begin_overridden_methods() const;
494  method_iterator end_overridden_methods() const;
495
496  /// getParent - Returns the parent of this method declaration, which
497  /// is the class in which this method is defined.
498  const CXXRecordDecl *getParent() const {
499    return cast<CXXRecordDecl>(FunctionDecl::getParent());
500  }
501
502  /// getParent - Returns the parent of this method declaration, which
503  /// is the class in which this method is defined.
504  CXXRecordDecl *getParent() {
505    return const_cast<CXXRecordDecl *>(
506             cast<CXXRecordDecl>(FunctionDecl::getParent()));
507  }
508
509  /// getThisType - Returns the type of 'this' pointer.
510  /// Should only be called for instance methods.
511  QualType getThisType(ASTContext &C) const;
512
513  unsigned getTypeQualifiers() const {
514    return getType()->getAsFunctionProtoType()->getTypeQuals();
515  }
516
517  // Implement isa/cast/dyncast/etc.
518  static bool classof(const Decl *D) {
519    return D->getKind() >= CXXMethod && D->getKind() <= CXXConversion;
520  }
521  static bool classof(const CXXMethodDecl *D) { return true; }
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/// };
537/// @endcode
538class CXXBaseOrMemberInitializer {
539  /// BaseOrMember - This points to the entity being initialized,
540  /// which is either a base class (a Type) or a non-static data
541  /// member. When the low bit is 1, it's a base
542  /// class; when the low bit is 0, it's a member.
543  uintptr_t BaseOrMember;
544
545  /// Args - The arguments used to initialize the base or member.
546  Expr **Args;
547  unsigned NumArgs;
548
549public:
550  /// CXXBaseOrMemberInitializer - Creates a new base-class initializer.
551  explicit
552  CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs);
553
554  /// CXXBaseOrMemberInitializer - Creates a new member initializer.
555  explicit
556  CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs);
557
558  /// ~CXXBaseOrMemberInitializer - Destroy the base or member initializer.
559  ~CXXBaseOrMemberInitializer();
560
561  /// arg_iterator - Iterates through the member initialization
562  /// arguments.
563  typedef Expr **arg_iterator;
564
565  /// arg_const_iterator - Iterates through the member initialization
566  /// arguments.
567  typedef Expr * const * arg_const_iterator;
568
569  /// isBaseInitializer - Returns true when this initializer is
570  /// initializing a base class.
571  bool isBaseInitializer() const { return (BaseOrMember & 0x1) != 0; }
572
573  /// isMemberInitializer - Returns true when this initializer is
574  /// initializing a non-static data member.
575  bool isMemberInitializer() const { return (BaseOrMember & 0x1) == 0; }
576
577  /// getBaseClass - If this is a base class initializer, returns the
578  /// type used to specify the initializer. The resulting type will be
579  /// a class type or a typedef of a class type. If this is not a base
580  /// class initializer, returns NULL.
581  Type *getBaseClass() {
582    if (isBaseInitializer())
583      return reinterpret_cast<Type*>(BaseOrMember & ~0x01);
584    else
585      return 0;
586  }
587
588  /// getBaseClass - If this is a base class initializer, returns the
589  /// type used to specify the initializer. The resulting type will be
590  /// a class type or a typedef of a class type. If this is not a base
591  /// class initializer, returns NULL.
592  const Type *getBaseClass() const {
593    if (isBaseInitializer())
594      return reinterpret_cast<const Type*>(BaseOrMember & ~0x01);
595    else
596      return 0;
597  }
598
599  /// getMember - If this is a member initializer, returns the
600  /// declaration of the non-static data member being
601  /// initialized. Otherwise, returns NULL.
602  FieldDecl *getMember() {
603    if (isMemberInitializer())
604      return reinterpret_cast<FieldDecl *>(BaseOrMember);
605    else
606      return 0;
607  }
608
609  /// begin() - Retrieve an iterator to the first initializer argument.
610  arg_iterator       begin()       { return Args; }
611  /// begin() - Retrieve an iterator to the first initializer argument.
612  arg_const_iterator begin() const { return Args; }
613
614  /// end() - Retrieve an iterator past the last initializer argument.
615  arg_iterator       end()       { return Args + NumArgs; }
616  /// end() - Retrieve an iterator past the last initializer argument.
617  arg_const_iterator end() const { return Args + NumArgs; }
618
619  /// getNumArgs - Determine the number of arguments used to
620  /// initialize the member or base.
621  unsigned getNumArgs() const { return NumArgs; }
622};
623
624/// CXXConstructorDecl - Represents a C++ constructor within a
625/// class. For example:
626///
627/// @code
628/// class X {
629/// public:
630///   explicit X(int); // represented by a CXXConstructorDecl.
631/// };
632/// @endcode
633class CXXConstructorDecl : public CXXMethodDecl {
634  /// Explicit - Whether this constructor is explicit.
635  bool Explicit : 1;
636
637  /// ImplicitlyDefined - Whether this constructor was implicitly
638  /// defined by the compiler. When false, the constructor was defined
639  /// by the user. In C++03, this flag will have the same value as
640  /// Implicit. In C++0x, however, a constructor that is
641  /// explicitly defaulted (i.e., defined with " = default") will have
642  /// @c !Implicit && ImplicitlyDefined.
643  bool ImplicitlyDefined : 1;
644
645  /// ImplicitMustBeDefined - Implicit constructor was used to create an
646  /// object of its class type. It must be defined.
647  bool ImplicitMustBeDefined : 1;
648
649  /// FIXME: Add support for base and member initializers.
650
651  CXXConstructorDecl(CXXRecordDecl *RD, SourceLocation L,
652                     DeclarationName N, QualType T,
653                     bool isExplicit, bool isInline, bool isImplicitlyDeclared)
654    : CXXMethodDecl(CXXConstructor, RD, L, N, T, false, isInline),
655      Explicit(isExplicit), ImplicitlyDefined(false),
656      ImplicitMustBeDefined(false) {
657    setImplicit(isImplicitlyDeclared);
658  }
659
660public:
661  static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
662                                    SourceLocation L, DeclarationName N,
663                                    QualType T, bool isExplicit,
664                                    bool isInline, bool isImplicitlyDeclared);
665
666  /// isExplicit - Whether this constructor was marked "explicit" or not.
667  bool isExplicit() const { return Explicit; }
668
669  /// isImplicitlyDefined - Whether this constructor was implicitly
670  /// defined. If false, then this constructor was defined by the
671  /// user. This operation can only be invoked if the constructor has
672  /// already been defined.
673  bool isImplicitlyDefined(ASTContext &C) const {
674    assert(isThisDeclarationADefinition() &&
675           "Can only get the implicit-definition flag once the constructor has been defined");
676    return ImplicitlyDefined;
677  }
678
679  /// setImplicitlyDefined - Set whether this constructor was
680  /// implicitly defined or not.
681  void setImplicitlyDefined(bool ID) {
682    assert(isThisDeclarationADefinition() &&
683           "Can only set the implicit-definition flag once the constructor has been defined");
684    ImplicitlyDefined = ID;
685  }
686
687  /// isImplicitMustBeDefined - Whether a definition must be synthesized for
688  /// the implicit constructor.
689  bool isImplicitMustBeDefined() const {
690    return isImplicit() && ImplicitMustBeDefined;
691  }
692
693  /// setImplicitMustBeDefined - constructor must be implicitly defined.
694  void setImplicitMustBeDefined() {
695    ImplicitMustBeDefined = true;
696  }
697
698  /// isDefaultConstructor - Whether this constructor is a default
699  /// constructor (C++ [class.ctor]p5), which can be used to
700  /// default-initialize a class of this type.
701  bool isDefaultConstructor() const;
702
703  /// isCopyConstructor - Whether this constructor is a copy
704  /// constructor (C++ [class.copy]p2, which can be used to copy the
705  /// class. @p TypeQuals will be set to the qualifiers on the
706  /// argument type. For example, @p TypeQuals would be set to @c
707  /// QualType::Const for the following copy constructor:
708  ///
709  /// @code
710  /// class X {
711  /// public:
712  ///   X(const X&);
713  /// };
714  /// @endcode
715  bool isCopyConstructor(ASTContext &Context, unsigned &TypeQuals) const;
716
717  /// isCopyConstructor - Whether this constructor is a copy
718  /// constructor (C++ [class.copy]p2, which can be used to copy the
719  /// class.
720  bool isCopyConstructor(ASTContext &Context) const {
721    unsigned TypeQuals = 0;
722    return isCopyConstructor(Context, TypeQuals);
723  }
724
725  /// isConvertingConstructor - Whether this constructor is a
726  /// converting constructor (C++ [class.conv.ctor]), which can be
727  /// used for user-defined conversions.
728  bool isConvertingConstructor() const;
729
730  // Implement isa/cast/dyncast/etc.
731  static bool classof(const Decl *D) {
732    return D->getKind() == CXXConstructor;
733  }
734  static bool classof(const CXXConstructorDecl *D) { return true; }
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  /// ImplicitlyDefined - Whether this destructor was implicitly
748  /// defined by the compiler. When false, the destructor was defined
749  /// by the user. In C++03, this flag will have the same value as
750  /// Implicit. In C++0x, however, a destructor that is
751  /// explicitly defaulted (i.e., defined with " = default") will have
752  /// @c !Implicit && ImplicitlyDefined.
753  bool ImplicitlyDefined : 1;
754
755  CXXDestructorDecl(CXXRecordDecl *RD, SourceLocation L,
756                    DeclarationName N, QualType T,
757                    bool isInline, bool isImplicitlyDeclared)
758    : CXXMethodDecl(CXXDestructor, RD, L, N, T, false, isInline),
759      ImplicitlyDefined(false) {
760    setImplicit(isImplicitlyDeclared);
761  }
762
763public:
764  static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
765                                   SourceLocation L, DeclarationName N,
766                                   QualType T, bool isInline,
767                                   bool isImplicitlyDeclared);
768
769  /// isImplicitlyDefined - Whether this destructor was implicitly
770  /// defined. If false, then this destructor was defined by the
771  /// user. This operation can only be invoked if the destructor has
772  /// already been defined.
773  bool isImplicitlyDefined() const {
774    assert(isThisDeclarationADefinition() &&
775           "Can only get the implicit-definition flag once the destructor has been defined");
776    return ImplicitlyDefined;
777  }
778
779  /// setImplicitlyDefined - Set whether this destructor was
780  /// implicitly defined or not.
781  void setImplicitlyDefined(bool ID) {
782    assert(isThisDeclarationADefinition() &&
783           "Can only set the implicit-definition flag once the destructor has been defined");
784    ImplicitlyDefined = ID;
785  }
786
787  // Implement isa/cast/dyncast/etc.
788  static bool classof(const Decl *D) {
789    return D->getKind() == CXXDestructor;
790  }
791  static bool classof(const CXXDestructorDecl *D) { return true; }
792};
793
794/// CXXConversionDecl - Represents a C++ conversion function within a
795/// class. For example:
796///
797/// @code
798/// class X {
799/// public:
800///   operator bool();
801/// };
802/// @endcode
803class CXXConversionDecl : public CXXMethodDecl {
804  /// Explicit - Whether this conversion function is marked
805  /// "explicit", meaning that it can only be applied when the user
806  /// explicitly wrote a cast. This is a C++0x feature.
807  bool Explicit : 1;
808
809  CXXConversionDecl(CXXRecordDecl *RD, SourceLocation L,
810                    DeclarationName N, QualType T,
811                    bool isInline, bool isExplicit)
812    : CXXMethodDecl(CXXConversion, RD, L, N, T, false, isInline),
813      Explicit(isExplicit) { }
814
815public:
816  static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD,
817                                   SourceLocation L, DeclarationName N,
818                                   QualType T, bool isInline,
819                                   bool isExplicit);
820
821  /// isExplicit - Whether this is an explicit conversion operator
822  /// (C++0x only). Explicit conversion operators are only considered
823  /// when the user has explicitly written a cast.
824  bool isExplicit() const { return Explicit; }
825
826  /// getConversionType - Returns the type that this conversion
827  /// function is converting to.
828  QualType getConversionType() const {
829    return getType()->getAsFunctionType()->getResultType();
830  }
831
832  // Implement isa/cast/dyncast/etc.
833  static bool classof(const Decl *D) {
834    return D->getKind() == CXXConversion;
835  }
836  static bool classof(const CXXConversionDecl *D) { return true; }
837};
838
839/// LinkageSpecDecl - This represents a linkage specification.  For example:
840///   extern "C" void foo();
841///
842class LinkageSpecDecl : public Decl, public DeclContext {
843public:
844  /// LanguageIDs - Used to represent the language in a linkage
845  /// specification.  The values are part of the serialization abi for
846  /// ASTs and cannot be changed without altering that abi.  To help
847  /// ensure a stable abi for this, we choose the DW_LANG_ encodings
848  /// from the dwarf standard.
849  enum LanguageIDs { lang_c = /* DW_LANG_C */ 0x0002,
850  lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004 };
851private:
852  /// Language - The language for this linkage specification.
853  LanguageIDs Language;
854
855  /// HadBraces - Whether this linkage specification had curly braces or not.
856  bool HadBraces : 1;
857
858  LinkageSpecDecl(DeclContext *DC, SourceLocation L, LanguageIDs lang,
859                  bool Braces)
860    : Decl(LinkageSpec, DC, L),
861      DeclContext(LinkageSpec), Language(lang), HadBraces(Braces) { }
862
863public:
864  static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC,
865                                 SourceLocation L, LanguageIDs Lang,
866                                 bool Braces);
867
868  LanguageIDs getLanguage() const { return Language; }
869
870  /// hasBraces - Determines whether this linkage specification had
871  /// braces in its syntactic form.
872  bool hasBraces() const { return HadBraces; }
873
874  static bool classof(const Decl *D) {
875    return D->getKind() == LinkageSpec;
876  }
877  static bool classof(const LinkageSpecDecl *D) { return true; }
878  static DeclContext *castToDeclContext(const LinkageSpecDecl *D) {
879    return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D));
880  }
881  static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) {
882    return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC));
883  }
884};
885
886/// UsingDirectiveDecl - Represents C++ using-directive. For example:
887///
888///    using namespace std;
889///
890// NB: UsingDirectiveDecl should be Decl not NamedDecl, but we provide
891// artificial name, for all using-directives in order to store
892// them in DeclContext effectively.
893class UsingDirectiveDecl : public NamedDecl {
894
895  /// SourceLocation - Location of 'namespace' token.
896  SourceLocation NamespaceLoc;
897
898  /// \brief The source range that covers the nested-name-specifier
899  /// preceding the namespace name.
900  SourceRange QualifierRange;
901
902  /// \brief The nested-name-specifier that precedes the namespace
903  /// name, if any.
904  NestedNameSpecifier *Qualifier;
905
906  /// IdentLoc - Location of nominated namespace-name identifier.
907  // FIXME: We don't store location of scope specifier.
908  SourceLocation IdentLoc;
909
910  /// NominatedNamespace - Namespace nominated by using-directive.
911  NamespaceDecl *NominatedNamespace;
912
913  /// Enclosing context containing both using-directive and nomintated
914  /// namespace.
915  DeclContext *CommonAncestor;
916
917  /// getUsingDirectiveName - Returns special DeclarationName used by
918  /// using-directives. This is only used by DeclContext for storing
919  /// UsingDirectiveDecls in its lookup structure.
920  static DeclarationName getName() {
921    return DeclarationName::getUsingDirectiveName();
922  }
923
924  UsingDirectiveDecl(DeclContext *DC, SourceLocation L,
925                     SourceLocation NamespcLoc,
926                     SourceRange QualifierRange,
927                     NestedNameSpecifier *Qualifier,
928                     SourceLocation IdentLoc,
929                     NamespaceDecl *Nominated,
930                     DeclContext *CommonAncestor)
931    : NamedDecl(Decl::UsingDirective, DC, L, getName()),
932      NamespaceLoc(NamespcLoc), QualifierRange(QualifierRange),
933      Qualifier(Qualifier), IdentLoc(IdentLoc),
934      NominatedNamespace(Nominated? Nominated->getOriginalNamespace() : 0),
935      CommonAncestor(CommonAncestor) {
936  }
937
938public:
939  /// \brief Retrieve the source range of the nested-name-specifier
940  /// that qualifiers the namespace name.
941  SourceRange getQualifierRange() const { return QualifierRange; }
942
943  /// \brief Retrieve the nested-name-specifier that qualifies the
944  /// name of the namespace.
945  NestedNameSpecifier *getQualifier() const { return Qualifier; }
946
947  /// getNominatedNamespace - Returns namespace nominated by using-directive.
948  NamespaceDecl *getNominatedNamespace() { return NominatedNamespace; }
949
950  const NamespaceDecl *getNominatedNamespace() const {
951    return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace();
952  }
953
954  /// getCommonAncestor - returns common ancestor context of using-directive,
955  /// and nominated by it namespace.
956  DeclContext *getCommonAncestor() { return CommonAncestor; }
957  const DeclContext *getCommonAncestor() const { return CommonAncestor; }
958
959  /// getNamespaceKeyLocation - Returns location of namespace keyword.
960  SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; }
961
962  /// getIdentLocation - Returns location of identifier.
963  SourceLocation getIdentLocation() const { return IdentLoc; }
964
965  static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC,
966                                    SourceLocation L,
967                                    SourceLocation NamespaceLoc,
968                                    SourceRange QualifierRange,
969                                    NestedNameSpecifier *Qualifier,
970                                    SourceLocation IdentLoc,
971                                    NamespaceDecl *Nominated,
972                                    DeclContext *CommonAncestor);
973
974  static bool classof(const Decl *D) {
975    return D->getKind() == Decl::UsingDirective;
976  }
977  static bool classof(const UsingDirectiveDecl *D) { return true; }
978
979  // Friend for getUsingDirectiveName.
980  friend class DeclContext;
981};
982
983/// NamespaceAliasDecl - Represents a C++ namespace alias. For example:
984///
985/// @code
986/// namespace Foo = Bar;
987/// @endcode
988class NamespaceAliasDecl : public NamedDecl {
989  SourceLocation AliasLoc;
990
991  /// \brief The source range that covers the nested-name-specifier
992  /// preceding the namespace name.
993  SourceRange QualifierRange;
994
995  /// \brief The nested-name-specifier that precedes the namespace
996  /// name, if any.
997  NestedNameSpecifier *Qualifier;
998
999  /// IdentLoc - Location of namespace identifier.
1000  SourceLocation IdentLoc;
1001
1002  /// Namespace - The Decl that this alias points to. Can either be a
1003  /// NamespaceDecl or a NamespaceAliasDecl.
1004  NamedDecl *Namespace;
1005
1006  NamespaceAliasDecl(DeclContext *DC, SourceLocation L,
1007                     SourceLocation AliasLoc, IdentifierInfo *Alias,
1008                     SourceRange QualifierRange,
1009                     NestedNameSpecifier *Qualifier,
1010                     SourceLocation IdentLoc, NamedDecl *Namespace)
1011    : NamedDecl(Decl::NamespaceAlias, DC, L, Alias), AliasLoc(AliasLoc),
1012      QualifierRange(QualifierRange), Qualifier(Qualifier),
1013      IdentLoc(IdentLoc), Namespace(Namespace) { }
1014
1015public:
1016  /// \brief Retrieve the source range of the nested-name-specifier
1017  /// that qualifiers the namespace name.
1018  SourceRange getQualifierRange() const { return QualifierRange; }
1019
1020  /// \brief Retrieve the nested-name-specifier that qualifies the
1021  /// name of the namespace.
1022  NestedNameSpecifier *getQualifier() const { return Qualifier; }
1023
1024  NamespaceDecl *getNamespace() {
1025    if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(Namespace))
1026      return AD->getNamespace();
1027
1028    return cast<NamespaceDecl>(Namespace);
1029  }
1030
1031  const NamespaceDecl *getNamespace() const {
1032    return const_cast<NamespaceAliasDecl*>(this)->getNamespace();
1033  }
1034
1035  /// \brief Retrieve the namespace that this alias refers to, which
1036  /// may either be a NamespaceDecl or a NamespaceAliasDecl.
1037  NamedDecl *getAliasedNamespace() const { return Namespace; }
1038
1039  static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC,
1040                                    SourceLocation L, SourceLocation AliasLoc,
1041                                    IdentifierInfo *Alias,
1042                                    SourceRange QualifierRange,
1043                                    NestedNameSpecifier *Qualifier,
1044                                    SourceLocation IdentLoc,
1045                                    NamedDecl *Namespace);
1046
1047  static bool classof(const Decl *D) {
1048    return D->getKind() == Decl::NamespaceAlias;
1049  }
1050  static bool classof(const NamespaceAliasDecl *D) { return true; }
1051};
1052
1053/// UsingDecl - Represents a C++ using-declaration. For example:
1054///    using someNameSpace::someIdentifier;
1055class UsingDecl : public NamedDecl {
1056
1057  /// \brief The source range that covers the nested-name-specifier
1058  /// preceding the declaration name.
1059  SourceRange NestedNameRange;
1060  /// \brief The source location of the target declaration name.
1061  SourceLocation TargetNameLocation;
1062  /// \brief The source location of the "using" location itself.
1063  SourceLocation UsingLocation;
1064  /// \brief Target declaration.
1065  NamedDecl* TargetDecl;
1066  /// \brief Target declaration.
1067  NestedNameSpecifier* TargetNestedNameDecl;
1068
1069  // Had 'typename' keyword.
1070  bool IsTypeName;
1071
1072  UsingDecl(DeclContext *DC, SourceLocation L, SourceRange NNR,
1073            SourceLocation TargetNL, SourceLocation UL, NamedDecl* Target,
1074            NestedNameSpecifier* TargetNNS, bool IsTypeNameArg)
1075    : NamedDecl(Decl::Using, DC, L, Target->getDeclName()),
1076      NestedNameRange(NNR), TargetNameLocation(TargetNL),
1077      UsingLocation(UL), TargetDecl(Target),
1078      TargetNestedNameDecl(TargetNNS), IsTypeName(IsTypeNameArg) {
1079    this->IdentifierNamespace = TargetDecl->getIdentifierNamespace();
1080  }
1081
1082public:
1083  /// \brief Returns the source range that covers the nested-name-specifier
1084  /// preceding the namespace name.
1085  SourceRange getNestedNameRange() { return(NestedNameRange); }
1086  /// \brief Returns the source location of the target declaration name.
1087  SourceLocation getTargetNameLocation() { return(TargetNameLocation); }
1088  /// \brief Returns the source location of the "using" location itself.
1089  SourceLocation getUsingLocation() { return(UsingLocation); }
1090  /// \brief getTargetDecl - Returns target specified by using-decl.
1091  NamedDecl *getTargetDecl() { return(TargetDecl); }
1092  /// \brief Get target nested name declaration.
1093  NestedNameSpecifier* getTargetNestedNameDecl() { return(TargetNestedNameDecl); }
1094  /// isTypeName - Return true if using decl had 'typename'.
1095  bool isTypeName() const { return(IsTypeName); }
1096
1097  static UsingDecl *Create(ASTContext &C, DeclContext *DC,
1098      SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
1099      SourceLocation UL, NamedDecl* Target,
1100      NestedNameSpecifier* TargetNNS, bool IsTypeNameArg);
1101
1102  static bool classof(const Decl *D) {
1103    return D->getKind() == Decl::Using;
1104  }
1105  static bool classof(const UsingDecl *D) { return true; }
1106};
1107
1108/// StaticAssertDecl - Represents a C++0x static_assert declaration.
1109class StaticAssertDecl : public Decl {
1110  Expr *AssertExpr;
1111  StringLiteral *Message;
1112
1113  StaticAssertDecl(DeclContext *DC, SourceLocation L,
1114                   Expr *assertexpr, StringLiteral *message)
1115  : Decl(StaticAssert, DC, L), AssertExpr(assertexpr), Message(message) { }
1116
1117public:
1118  static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC,
1119                                  SourceLocation L, Expr *AssertExpr,
1120                                  StringLiteral *Message);
1121
1122  Expr *getAssertExpr() { return AssertExpr; }
1123  const Expr *getAssertExpr() const { return AssertExpr; }
1124
1125  StringLiteral *getMessage() { return Message; }
1126  const StringLiteral *getMessage() const { return Message; }
1127
1128  virtual ~StaticAssertDecl();
1129  virtual void Destroy(ASTContext& C);
1130
1131  static bool classof(const Decl *D) {
1132    return D->getKind() == Decl::StaticAssert;
1133  }
1134  static bool classof(StaticAssertDecl *D) { return true; }
1135};
1136
1137/// Insertion operator for diagnostics.  This allows sending AccessSpecifier's
1138/// into a diagnostic with <<.
1139const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1140                                    AccessSpecifier AS);
1141
1142} // end namespace clang
1143
1144#endif
1145