DeclCXX.h revision 46bb4f1a3ac9517406887939612eb8df4b7be006
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, other than those for
11//  templates (in DeclTemplate.h) and friends (in DeclFriend.h).
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_AST_DECLCXX_H
16#define LLVM_CLANG_AST_DECLCXX_H
17
18#include "clang/AST/Expr.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/TypeLoc.h"
21#include "clang/AST/UnresolvedSet.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/SmallPtrSet.h"
24
25namespace clang {
26
27class ClassTemplateDecl;
28class ClassTemplateSpecializationDecl;
29class CXXBasePath;
30class CXXBasePaths;
31class CXXConstructorDecl;
32class CXXConversionDecl;
33class CXXDestructorDecl;
34class CXXMethodDecl;
35class CXXRecordDecl;
36class CXXMemberLookupCriteria;
37class CXXFinalOverriderMap;
38class CXXIndirectPrimaryBaseSet;
39class FriendDecl;
40
41/// \brief Represents any kind of function declaration, whether it is a
42/// concrete function or a function template.
43class AnyFunctionDecl {
44  NamedDecl *Function;
45
46  AnyFunctionDecl(NamedDecl *ND) : Function(ND) { }
47
48public:
49  AnyFunctionDecl(FunctionDecl *FD) : Function(FD) { }
50  AnyFunctionDecl(FunctionTemplateDecl *FTD);
51
52  /// \brief Implicily converts any function or function template into a
53  /// named declaration.
54  operator NamedDecl *() const { return Function; }
55
56  /// \brief Retrieve the underlying function or function template.
57  NamedDecl *get() const { return Function; }
58
59  static AnyFunctionDecl getFromNamedDecl(NamedDecl *ND) {
60    return AnyFunctionDecl(ND);
61  }
62};
63
64} // end namespace clang
65
66namespace llvm {
67  /// Implement simplify_type for AnyFunctionDecl, so that we can dyn_cast from
68  /// AnyFunctionDecl to any function or function template declaration.
69  template<> struct simplify_type<const ::clang::AnyFunctionDecl> {
70    typedef ::clang::NamedDecl* SimpleType;
71    static SimpleType getSimplifiedValue(const ::clang::AnyFunctionDecl &Val) {
72      return Val;
73    }
74  };
75  template<> struct simplify_type< ::clang::AnyFunctionDecl>
76  : public simplify_type<const ::clang::AnyFunctionDecl> {};
77
78  // Provide PointerLikeTypeTraits for non-cvr pointers.
79  template<>
80  class PointerLikeTypeTraits< ::clang::AnyFunctionDecl> {
81  public:
82    static inline void *getAsVoidPointer(::clang::AnyFunctionDecl F) {
83      return F.get();
84    }
85    static inline ::clang::AnyFunctionDecl getFromVoidPointer(void *P) {
86      return ::clang::AnyFunctionDecl::getFromNamedDecl(
87                                      static_cast< ::clang::NamedDecl*>(P));
88    }
89
90    enum { NumLowBitsAvailable = 2 };
91  };
92
93} // end namespace llvm
94
95namespace clang {
96
97/// AccessSpecDecl - An access specifier followed by colon ':'.
98///
99/// An objects of this class represents sugar for the syntactic occurrence
100/// of an access specifier followed by a colon in the list of member
101/// specifiers of a C++ class definition.
102///
103/// Note that they do not represent other uses of access specifiers,
104/// such as those occurring in a list of base specifiers.
105/// Also note that this class has nothing to do with so-called
106/// "access declarations" (C++98 11.3 [class.access.dcl]).
107class AccessSpecDecl : public Decl {
108  /// ColonLoc - The location of the ':'.
109  SourceLocation ColonLoc;
110
111  AccessSpecDecl(AccessSpecifier AS, DeclContext *DC,
112                 SourceLocation ASLoc, SourceLocation ColonLoc)
113    : Decl(AccessSpec, DC, ASLoc), ColonLoc(ColonLoc) {
114    setAccess(AS);
115  }
116  AccessSpecDecl(EmptyShell Empty)
117    : Decl(AccessSpec, Empty) { }
118public:
119  /// getAccessSpecifierLoc - The location of the access specifier.
120  SourceLocation getAccessSpecifierLoc() const { return getLocation(); }
121  /// setAccessSpecifierLoc - Sets the location of the access specifier.
122  void setAccessSpecifierLoc(SourceLocation ASLoc) { setLocation(ASLoc); }
123
124  /// getColonLoc - The location of the colon following the access specifier.
125  SourceLocation getColonLoc() const { return ColonLoc; }
126  /// setColonLoc - Sets the location of the colon.
127  void setColonLoc(SourceLocation CLoc) { ColonLoc = CLoc; }
128
129  SourceRange getSourceRange() const {
130    return SourceRange(getAccessSpecifierLoc(), getColonLoc());
131  }
132
133  static AccessSpecDecl *Create(ASTContext &C, AccessSpecifier AS,
134                                DeclContext *DC, SourceLocation ASLoc,
135                                SourceLocation ColonLoc) {
136    return new (C) AccessSpecDecl(AS, DC, ASLoc, ColonLoc);
137  }
138  static AccessSpecDecl *Create(ASTContext &C, EmptyShell Empty) {
139    return new (C) AccessSpecDecl(Empty);
140  }
141
142  // Implement isa/cast/dyncast/etc.
143  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
144  static bool classof(const AccessSpecDecl *D) { return true; }
145  static bool classofKind(Kind K) { return K == AccessSpec; }
146};
147
148
149/// CXXBaseSpecifier - A base class of a C++ class.
150///
151/// Each CXXBaseSpecifier represents a single, direct base class (or
152/// struct) of a C++ class (or struct). It specifies the type of that
153/// base class, whether it is a virtual or non-virtual base, and what
154/// level of access (public, protected, private) is used for the
155/// derivation. For example:
156///
157/// @code
158///   class A { };
159///   class B { };
160///   class C : public virtual A, protected B { };
161/// @endcode
162///
163/// In this code, C will have two CXXBaseSpecifiers, one for "public
164/// virtual A" and the other for "protected B".
165class CXXBaseSpecifier {
166  /// Range - The source code range that covers the full base
167  /// specifier, including the "virtual" (if present) and access
168  /// specifier (if present).
169  SourceRange Range;
170
171  /// \brief The source location of the ellipsis, if this is a pack
172  /// expansion.
173  SourceLocation EllipsisLoc;
174
175  /// Virtual - Whether this is a virtual base class or not.
176  bool Virtual : 1;
177
178  /// BaseOfClass - Whether this is the base of a class (true) or of a
179  /// struct (false). This determines the mapping from the access
180  /// specifier as written in the source code to the access specifier
181  /// used for semantic analysis.
182  bool BaseOfClass : 1;
183
184  /// Access - Access specifier as written in the source code (which
185  /// may be AS_none). The actual type of data stored here is an
186  /// AccessSpecifier, but we use "unsigned" here to work around a
187  /// VC++ bug.
188  unsigned Access : 2;
189
190  /// InheritConstructors - Whether the class contains a using declaration
191  /// to inherit the named class's constructors.
192  bool InheritConstructors : 1;
193
194  /// BaseTypeInfo - The type of the base class. This will be a class or struct
195  /// (or a typedef of such). The source code range does not include the
196  /// "virtual" or access specifier.
197  TypeSourceInfo *BaseTypeInfo;
198
199public:
200  CXXBaseSpecifier() { }
201
202  CXXBaseSpecifier(SourceRange R, bool V, bool BC, AccessSpecifier A,
203                   TypeSourceInfo *TInfo, SourceLocation EllipsisLoc)
204    : Range(R), EllipsisLoc(EllipsisLoc), Virtual(V), BaseOfClass(BC),
205      Access(A), InheritConstructors(false), BaseTypeInfo(TInfo) { }
206
207  /// getSourceRange - Retrieves the source range that contains the
208  /// entire base specifier.
209  SourceRange getSourceRange() const { return Range; }
210
211  /// isVirtual - Determines whether the base class is a virtual base
212  /// class (or not).
213  bool isVirtual() const { return Virtual; }
214
215  /// \brief Determine whether this base class is a base of a class declared
216  /// with the 'class' keyword (vs. one declared with the 'struct' keyword).
217  bool isBaseOfClass() const { return BaseOfClass; }
218
219  /// \brief Determine whether this base specifier is a pack expansion.
220  bool isPackExpansion() const { return EllipsisLoc.isValid(); }
221
222  /// \brief Determine whether this base class's constructors get inherited.
223  bool getInheritConstructors() const { return InheritConstructors; }
224
225  /// \brief Set that this base class's constructors should be inherited.
226  void setInheritConstructors(bool Inherit = true) {
227    InheritConstructors = Inherit;
228  }
229
230  /// \brief For a pack expansion, determine the location of the ellipsis.
231  SourceLocation getEllipsisLoc() const {
232    return EllipsisLoc;
233  }
234
235  /// getAccessSpecifier - Returns the access specifier for this base
236  /// specifier. This is the actual base specifier as used for
237  /// semantic analysis, so the result can never be AS_none. To
238  /// retrieve the access specifier as written in the source code, use
239  /// getAccessSpecifierAsWritten().
240  AccessSpecifier getAccessSpecifier() const {
241    if ((AccessSpecifier)Access == AS_none)
242      return BaseOfClass? AS_private : AS_public;
243    else
244      return (AccessSpecifier)Access;
245  }
246
247  /// getAccessSpecifierAsWritten - Retrieves the access specifier as
248  /// written in the source code (which may mean that no access
249  /// specifier was explicitly written). Use getAccessSpecifier() to
250  /// retrieve the access specifier for use in semantic analysis.
251  AccessSpecifier getAccessSpecifierAsWritten() const {
252    return (AccessSpecifier)Access;
253  }
254
255  /// getType - Retrieves the type of the base class. This type will
256  /// always be an unqualified class type.
257  QualType getType() const { return BaseTypeInfo->getType(); }
258
259  /// getTypeLoc - Retrieves the type and source location of the base class.
260  TypeSourceInfo *getTypeSourceInfo() const { return BaseTypeInfo; }
261};
262
263/// CXXRecordDecl - Represents a C++ struct/union/class.
264/// FIXME: This class will disappear once we've properly taught RecordDecl
265/// to deal with C++-specific things.
266class CXXRecordDecl : public RecordDecl {
267
268  friend void TagDecl::startDefinition();
269
270  struct DefinitionData {
271    DefinitionData(CXXRecordDecl *D);
272
273    /// UserDeclaredConstructor - True when this class has a
274    /// user-declared constructor.
275    bool UserDeclaredConstructor : 1;
276
277    /// UserDeclaredCopyConstructor - True when this class has a
278    /// user-declared copy constructor.
279    bool UserDeclaredCopyConstructor : 1;
280
281    /// UserDeclaredCopyAssignment - True when this class has a
282    /// user-declared copy assignment operator.
283    bool UserDeclaredCopyAssignment : 1;
284
285    /// UserDeclaredDestructor - True when this class has a
286    /// user-declared destructor.
287    bool UserDeclaredDestructor : 1;
288
289    /// Aggregate - True when this class is an aggregate.
290    bool Aggregate : 1;
291
292    /// PlainOldData - True when this class is a POD-type.
293    bool PlainOldData : 1;
294
295    /// Empty - true when this class is empty for traits purposes,
296    /// i.e. has no data members other than 0-width bit-fields, has no
297    /// virtual function/base, and doesn't inherit from a non-empty
298    /// class. Doesn't take union-ness into account.
299    bool Empty : 1;
300
301    /// Polymorphic - True when this class is polymorphic, i.e. has at
302    /// least one virtual member or derives from a polymorphic class.
303    bool Polymorphic : 1;
304
305    /// Abstract - True when this class is abstract, i.e. has at least
306    /// one pure virtual function, (that can come from a base class).
307    bool Abstract : 1;
308
309    /// HasTrivialConstructor - True when this class has a trivial constructor.
310    ///
311    /// C++ [class.ctor]p5.  A constructor is trivial if it is an
312    /// implicitly-declared default constructor and if:
313    /// * its class has no virtual functions and no virtual base classes, and
314    /// * all the direct base classes of its class have trivial constructors, and
315    /// * for all the nonstatic data members of its class that are of class type
316    ///   (or array thereof), each such class has a trivial constructor.
317    bool HasTrivialConstructor : 1;
318
319    /// HasTrivialCopyConstructor - True when this class has a trivial copy
320    /// constructor.
321    ///
322    /// C++ [class.copy]p6.  A copy constructor for class X is trivial
323    /// if it is implicitly declared and if
324    /// * class X has no virtual functions and no virtual base classes, and
325    /// * each direct base class of X has a trivial copy constructor, and
326    /// * for all the nonstatic data members of X that are of class type (or
327    ///   array thereof), each such class type has a trivial copy constructor;
328    /// otherwise the copy constructor is non-trivial.
329    bool HasTrivialCopyConstructor : 1;
330
331    /// HasTrivialCopyAssignment - True when this class has a trivial copy
332    /// assignment operator.
333    ///
334    /// C++ [class.copy]p11.  A copy assignment operator for class X is
335    /// trivial if it is implicitly declared and if
336    /// * class X has no virtual functions and no virtual base classes, and
337    /// * each direct base class of X has a trivial copy assignment operator, and
338    /// * for all the nonstatic data members of X that are of class type (or
339    ///   array thereof), each such class type has a trivial copy assignment
340    ///   operator;
341    /// otherwise the copy assignment operator is non-trivial.
342    bool HasTrivialCopyAssignment : 1;
343
344    /// HasTrivialDestructor - True when this class has a trivial destructor.
345    ///
346    /// C++ [class.dtor]p3.  A destructor is trivial if it is an
347    /// implicitly-declared destructor and if:
348    /// * all of the direct base classes of its class have trivial destructors
349    ///   and
350    /// * for all of the non-static data members of its class that are of class
351    ///   type (or array thereof), each such class has a trivial destructor.
352    bool HasTrivialDestructor : 1;
353
354    /// ComputedVisibleConversions - True when visible conversion functions are
355    /// already computed and are available.
356    bool ComputedVisibleConversions : 1;
357
358    /// \brief Whether we have already declared the default constructor or
359    /// do not need to have one declared.
360    bool DeclaredDefaultConstructor : 1;
361
362    /// \brief Whether we have already declared the copy constructor.
363    bool DeclaredCopyConstructor : 1;
364
365    /// \brief Whether we have already declared the copy-assignment operator.
366    bool DeclaredCopyAssignment : 1;
367
368    /// \brief Whether we have already declared a destructor within the class.
369    bool DeclaredDestructor : 1;
370
371    /// NumBases - The number of base class specifiers in Bases.
372    unsigned NumBases;
373
374    /// NumVBases - The number of virtual base class specifiers in VBases.
375    unsigned NumVBases;
376
377    /// Bases - Base classes of this class.
378    /// FIXME: This is wasted space for a union.
379    LazyCXXBaseSpecifiersPtr Bases;
380
381    /// VBases - direct and indirect virtual base classes of this class.
382    LazyCXXBaseSpecifiersPtr VBases;
383
384    /// Conversions - Overload set containing the conversion functions
385    /// of this C++ class (but not its inherited conversion
386    /// functions). Each of the entries in this overload set is a
387    /// CXXConversionDecl.
388    UnresolvedSet<4> Conversions;
389
390    /// VisibleConversions - Overload set containing the conversion
391    /// functions of this C++ class and all those inherited conversion
392    /// functions that are visible in this class. Each of the entries
393    /// in this overload set is a CXXConversionDecl or a
394    /// FunctionTemplateDecl.
395    UnresolvedSet<4> VisibleConversions;
396
397    /// Definition - The declaration which defines this record.
398    CXXRecordDecl *Definition;
399
400    /// FirstFriend - The first friend declaration in this class, or
401    /// null if there aren't any.  This is actually currently stored
402    /// in reverse order.
403    FriendDecl *FirstFriend;
404
405    /// \brief Retrieve the set of direct base classes.
406    CXXBaseSpecifier *getBases() const {
407      return Bases.get(Definition->getASTContext().getExternalSource());
408    }
409
410    /// \brief Retrieve the set of virtual base classes.
411    CXXBaseSpecifier *getVBases() const {
412      return VBases.get(Definition->getASTContext().getExternalSource());
413    }
414  } *DefinitionData;
415
416  struct DefinitionData &data() {
417    assert(DefinitionData && "queried property of class with no definition");
418    return *DefinitionData;
419  }
420
421  const struct DefinitionData &data() const {
422    assert(DefinitionData && "queried property of class with no definition");
423    return *DefinitionData;
424  }
425
426  /// \brief The template or declaration that this declaration
427  /// describes or was instantiated from, respectively.
428  ///
429  /// For non-templates, this value will be NULL. For record
430  /// declarations that describe a class template, this will be a
431  /// pointer to a ClassTemplateDecl. For member
432  /// classes of class template specializations, this will be the
433  /// MemberSpecializationInfo referring to the member class that was
434  /// instantiated or specialized.
435  llvm::PointerUnion<ClassTemplateDecl*, MemberSpecializationInfo*>
436    TemplateOrInstantiation;
437
438  friend class DeclContext;
439
440  /// \brief Notify the class that member has been added.
441  ///
442  /// This routine helps maintain information about the class based on which
443  /// members have been added. It will be invoked by DeclContext::addDecl()
444  /// whenever a member is added to this record.
445  void addedMember(Decl *D);
446
447  void markedVirtualFunctionPure();
448  friend void FunctionDecl::setPure(bool);
449
450protected:
451  CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
452                SourceLocation L, IdentifierInfo *Id,
453                CXXRecordDecl *PrevDecl,
454                SourceLocation TKL = SourceLocation());
455
456public:
457  /// base_class_iterator - Iterator that traverses the base classes
458  /// of a class.
459  typedef CXXBaseSpecifier*       base_class_iterator;
460
461  /// base_class_const_iterator - Iterator that traverses the base
462  /// classes of a class.
463  typedef const CXXBaseSpecifier* base_class_const_iterator;
464
465  /// reverse_base_class_iterator = Iterator that traverses the base classes
466  /// of a class in reverse order.
467  typedef std::reverse_iterator<base_class_iterator>
468    reverse_base_class_iterator;
469
470  /// reverse_base_class_iterator = Iterator that traverses the base classes
471  /// of a class in reverse order.
472  typedef std::reverse_iterator<base_class_const_iterator>
473    reverse_base_class_const_iterator;
474
475  virtual CXXRecordDecl *getCanonicalDecl() {
476    return cast<CXXRecordDecl>(RecordDecl::getCanonicalDecl());
477  }
478  virtual const CXXRecordDecl *getCanonicalDecl() const {
479    return cast<CXXRecordDecl>(RecordDecl::getCanonicalDecl());
480  }
481
482  const CXXRecordDecl *getPreviousDeclaration() const {
483    return cast_or_null<CXXRecordDecl>(RecordDecl::getPreviousDeclaration());
484  }
485  CXXRecordDecl *getPreviousDeclaration() {
486    return cast_or_null<CXXRecordDecl>(RecordDecl::getPreviousDeclaration());
487  }
488
489  CXXRecordDecl *getDefinition() const {
490    if (!DefinitionData) return 0;
491    return data().Definition;
492  }
493
494  bool hasDefinition() const { return DefinitionData != 0; }
495
496  static CXXRecordDecl *Create(const ASTContext &C, TagKind TK, DeclContext *DC,
497                               SourceLocation L, IdentifierInfo *Id,
498                               SourceLocation TKL = SourceLocation(),
499                               CXXRecordDecl* PrevDecl=0,
500                               bool DelayTypeCreation = false);
501  static CXXRecordDecl *Create(const ASTContext &C, EmptyShell Empty);
502
503  bool isDynamicClass() const {
504    return data().Polymorphic || data().NumVBases != 0;
505  }
506
507  /// setBases - Sets the base classes of this struct or class.
508  void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases);
509
510  /// getNumBases - Retrieves the number of base classes of this
511  /// class.
512  unsigned getNumBases() const { return data().NumBases; }
513
514  base_class_iterator bases_begin() { return data().getBases(); }
515  base_class_const_iterator bases_begin() const { return data().getBases(); }
516  base_class_iterator bases_end() { return bases_begin() + data().NumBases; }
517  base_class_const_iterator bases_end() const {
518    return bases_begin() + data().NumBases;
519  }
520  reverse_base_class_iterator       bases_rbegin() {
521    return reverse_base_class_iterator(bases_end());
522  }
523  reverse_base_class_const_iterator bases_rbegin() const {
524    return reverse_base_class_const_iterator(bases_end());
525  }
526  reverse_base_class_iterator bases_rend() {
527    return reverse_base_class_iterator(bases_begin());
528  }
529  reverse_base_class_const_iterator bases_rend() const {
530    return reverse_base_class_const_iterator(bases_begin());
531  }
532
533  /// getNumVBases - Retrieves the number of virtual base classes of this
534  /// class.
535  unsigned getNumVBases() const { return data().NumVBases; }
536
537  base_class_iterator vbases_begin() { return data().getVBases(); }
538  base_class_const_iterator vbases_begin() const { return data().getVBases(); }
539  base_class_iterator vbases_end() { return vbases_begin() + data().NumVBases; }
540  base_class_const_iterator vbases_end() const {
541    return vbases_begin() + data().NumVBases;
542  }
543  reverse_base_class_iterator vbases_rbegin() {
544    return reverse_base_class_iterator(vbases_end());
545  }
546  reverse_base_class_const_iterator vbases_rbegin() const {
547    return reverse_base_class_const_iterator(vbases_end());
548  }
549  reverse_base_class_iterator vbases_rend() {
550    return reverse_base_class_iterator(vbases_begin());
551  }
552  reverse_base_class_const_iterator vbases_rend() const {
553    return reverse_base_class_const_iterator(vbases_begin());
554 }
555
556  /// \brief Determine whether this class has any dependent base classes.
557  bool hasAnyDependentBases() const;
558
559  /// Iterator access to method members.  The method iterator visits
560  /// all method members of the class, including non-instance methods,
561  /// special methods, etc.
562  typedef specific_decl_iterator<CXXMethodDecl> method_iterator;
563
564  /// method_begin - Method begin iterator.  Iterates in the order the methods
565  /// were declared.
566  method_iterator method_begin() const {
567    return method_iterator(decls_begin());
568  }
569  /// method_end - Method end iterator.
570  method_iterator method_end() const {
571    return method_iterator(decls_end());
572  }
573
574  /// Iterator access to constructor members.
575  typedef specific_decl_iterator<CXXConstructorDecl> ctor_iterator;
576
577  ctor_iterator ctor_begin() const {
578    return ctor_iterator(decls_begin());
579  }
580  ctor_iterator ctor_end() const {
581    return ctor_iterator(decls_end());
582  }
583
584  /// An iterator over friend declarations.  All of these are defined
585  /// in DeclFriend.h.
586  class friend_iterator;
587  friend_iterator friend_begin() const;
588  friend_iterator friend_end() const;
589  void pushFriendDecl(FriendDecl *FD);
590
591  /// Determines whether this record has any friends.
592  bool hasFriends() const {
593    return data().FirstFriend != 0;
594  }
595
596  /// \brief Determine whether this class has had its default constructor
597  /// declared implicitly or does not need one declared implicitly.
598  ///
599  /// This value is used for lazy creation of default constructors.
600  bool hasDeclaredDefaultConstructor() const {
601    return data().DeclaredDefaultConstructor;
602  }
603
604  /// hasConstCopyConstructor - Determines whether this class has a
605  /// copy constructor that accepts a const-qualified argument.
606  bool hasConstCopyConstructor(const ASTContext &Context) const;
607
608  /// getCopyConstructor - Returns the copy constructor for this class
609  CXXConstructorDecl *getCopyConstructor(const ASTContext &Context,
610                                         unsigned TypeQuals) const;
611
612  /// \brief Retrieve the copy-assignment operator for this class, if available.
613  ///
614  /// This routine attempts to find the copy-assignment operator for this
615  /// class, using a simplistic form of overload resolution.
616  ///
617  /// \param ArgIsConst Whether the argument to the copy-assignment operator
618  /// is const-qualified.
619  ///
620  /// \returns The copy-assignment operator that can be invoked, or NULL if
621  /// a unique copy-assignment operator could not be found.
622  CXXMethodDecl *getCopyAssignmentOperator(bool ArgIsConst) const;
623
624  /// hasUserDeclaredConstructor - Whether this class has any
625  /// user-declared constructors. When true, a default constructor
626  /// will not be implicitly declared.
627  bool hasUserDeclaredConstructor() const {
628    return data().UserDeclaredConstructor;
629  }
630
631  /// hasUserDeclaredCopyConstructor - Whether this class has a
632  /// user-declared copy constructor. When false, a copy constructor
633  /// will be implicitly declared.
634  bool hasUserDeclaredCopyConstructor() const {
635    return data().UserDeclaredCopyConstructor;
636  }
637
638  /// \brief Determine whether this class has had its copy constructor
639  /// declared, either via the user or via an implicit declaration.
640  ///
641  /// This value is used for lazy creation of copy constructors.
642  bool hasDeclaredCopyConstructor() const {
643    return data().DeclaredCopyConstructor;
644  }
645
646  /// hasUserDeclaredCopyAssignment - Whether this class has a
647  /// user-declared copy assignment operator. When false, a copy
648  /// assigment operator will be implicitly declared.
649  bool hasUserDeclaredCopyAssignment() const {
650    return data().UserDeclaredCopyAssignment;
651  }
652
653  /// \brief Determine whether this class has had its copy assignment operator
654  /// declared, either via the user or via an implicit declaration.
655  ///
656  /// This value is used for lazy creation of copy assignment operators.
657  bool hasDeclaredCopyAssignment() const {
658    return data().DeclaredCopyAssignment;
659  }
660
661  /// hasUserDeclaredDestructor - Whether this class has a
662  /// user-declared destructor. When false, a destructor will be
663  /// implicitly declared.
664  bool hasUserDeclaredDestructor() const {
665    return data().UserDeclaredDestructor;
666  }
667
668  /// \brief Determine whether this class has had its destructor declared,
669  /// either via the user or via an implicit declaration.
670  ///
671  /// This value is used for lazy creation of destructors.
672  bool hasDeclaredDestructor() const { return data().DeclaredDestructor; }
673
674  /// getConversions - Retrieve the overload set containing all of the
675  /// conversion functions in this class.
676  UnresolvedSetImpl *getConversionFunctions() {
677    return &data().Conversions;
678  }
679  const UnresolvedSetImpl *getConversionFunctions() const {
680    return &data().Conversions;
681  }
682
683  typedef UnresolvedSetImpl::iterator conversion_iterator;
684  conversion_iterator conversion_begin() const {
685    return getConversionFunctions()->begin();
686  }
687  conversion_iterator conversion_end() const {
688    return getConversionFunctions()->end();
689  }
690
691  /// Removes a conversion function from this class.  The conversion
692  /// function must currently be a member of this class.  Furthermore,
693  /// this class must currently be in the process of being defined.
694  void removeConversion(const NamedDecl *Old);
695
696  /// getVisibleConversionFunctions - get all conversion functions visible
697  /// in current class; including conversion function templates.
698  const UnresolvedSetImpl *getVisibleConversionFunctions();
699
700  /// isAggregate - Whether this class is an aggregate (C++
701  /// [dcl.init.aggr]), which is a class with no user-declared
702  /// constructors, no private or protected non-static data members,
703  /// no base classes, and no virtual functions (C++ [dcl.init.aggr]p1).
704  bool isAggregate() const { return data().Aggregate; }
705
706  /// isPOD - Whether this class is a POD-type (C++ [class]p4), which is a class
707  /// that is an aggregate that has no non-static non-POD data members, no
708  /// reference data members, no user-defined copy assignment operator and no
709  /// user-defined destructor.
710  bool isPOD() const { return data().PlainOldData; }
711
712  /// isEmpty - Whether this class is empty (C++0x [meta.unary.prop]), which
713  /// means it has a virtual function, virtual base, data member (other than
714  /// 0-width bit-field) or inherits from a non-empty class. Does NOT include
715  /// a check for union-ness.
716  bool isEmpty() const { return data().Empty; }
717
718  /// isPolymorphic - Whether this class is polymorphic (C++ [class.virtual]),
719  /// which means that the class contains or inherits a virtual function.
720  bool isPolymorphic() const { return data().Polymorphic; }
721
722  /// isAbstract - Whether this class is abstract (C++ [class.abstract]),
723  /// which means that the class contains or inherits a pure virtual function.
724  bool isAbstract() const { return data().Abstract; }
725
726  // hasTrivialConstructor - Whether this class has a trivial constructor
727  // (C++ [class.ctor]p5)
728  bool hasTrivialConstructor() const { return data().HasTrivialConstructor; }
729
730  // hasTrivialCopyConstructor - Whether this class has a trivial copy
731  // constructor (C++ [class.copy]p6)
732  bool hasTrivialCopyConstructor() const {
733    return data().HasTrivialCopyConstructor;
734  }
735
736  // hasTrivialCopyAssignment - Whether this class has a trivial copy
737  // assignment operator (C++ [class.copy]p11)
738  bool hasTrivialCopyAssignment() const {
739    return data().HasTrivialCopyAssignment;
740  }
741
742  // hasTrivialDestructor - Whether this class has a trivial destructor
743  // (C++ [class.dtor]p3)
744  bool hasTrivialDestructor() const { return data().HasTrivialDestructor; }
745
746  /// \brief If this record is an instantiation of a member class,
747  /// retrieves the member class from which it was instantiated.
748  ///
749  /// This routine will return non-NULL for (non-templated) member
750  /// classes of class templates. For example, given:
751  ///
752  /// \code
753  /// template<typename T>
754  /// struct X {
755  ///   struct A { };
756  /// };
757  /// \endcode
758  ///
759  /// The declaration for X<int>::A is a (non-templated) CXXRecordDecl
760  /// whose parent is the class template specialization X<int>. For
761  /// this declaration, getInstantiatedFromMemberClass() will return
762  /// the CXXRecordDecl X<T>::A. When a complete definition of
763  /// X<int>::A is required, it will be instantiated from the
764  /// declaration returned by getInstantiatedFromMemberClass().
765  CXXRecordDecl *getInstantiatedFromMemberClass() const;
766
767  /// \brief If this class is an instantiation of a member class of a
768  /// class template specialization, retrieves the member specialization
769  /// information.
770  MemberSpecializationInfo *getMemberSpecializationInfo() const;
771
772  /// \brief Specify that this record is an instantiation of the
773  /// member class RD.
774  void setInstantiationOfMemberClass(CXXRecordDecl *RD,
775                                     TemplateSpecializationKind TSK);
776
777  /// \brief Retrieves the class template that is described by this
778  /// class declaration.
779  ///
780  /// Every class template is represented as a ClassTemplateDecl and a
781  /// CXXRecordDecl. The former contains template properties (such as
782  /// the template parameter lists) while the latter contains the
783  /// actual description of the template's
784  /// contents. ClassTemplateDecl::getTemplatedDecl() retrieves the
785  /// CXXRecordDecl that from a ClassTemplateDecl, while
786  /// getDescribedClassTemplate() retrieves the ClassTemplateDecl from
787  /// a CXXRecordDecl.
788  ClassTemplateDecl *getDescribedClassTemplate() const {
789    return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl*>();
790  }
791
792  void setDescribedClassTemplate(ClassTemplateDecl *Template) {
793    TemplateOrInstantiation = Template;
794  }
795
796  /// \brief Determine whether this particular class is a specialization or
797  /// instantiation of a class template or member class of a class template,
798  /// and how it was instantiated or specialized.
799  TemplateSpecializationKind getTemplateSpecializationKind() const;
800
801  /// \brief Set the kind of specialization or template instantiation this is.
802  void setTemplateSpecializationKind(TemplateSpecializationKind TSK);
803
804  /// getDestructor - Returns the destructor decl for this class.
805  CXXDestructorDecl *getDestructor() const;
806
807  /// isLocalClass - If the class is a local class [class.local], returns
808  /// the enclosing function declaration.
809  const FunctionDecl *isLocalClass() const {
810    if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(getDeclContext()))
811      return RD->isLocalClass();
812
813    return dyn_cast<FunctionDecl>(getDeclContext());
814  }
815
816  /// \brief Determine whether this class is derived from the class \p Base.
817  ///
818  /// This routine only determines whether this class is derived from \p Base,
819  /// but does not account for factors that may make a Derived -> Base class
820  /// ill-formed, such as private/protected inheritance or multiple, ambiguous
821  /// base class subobjects.
822  ///
823  /// \param Base the base class we are searching for.
824  ///
825  /// \returns true if this class is derived from Base, false otherwise.
826  bool isDerivedFrom(const CXXRecordDecl *Base) const;
827
828  /// \brief Determine whether this class is derived from the type \p Base.
829  ///
830  /// This routine only determines whether this class is derived from \p Base,
831  /// but does not account for factors that may make a Derived -> Base class
832  /// ill-formed, such as private/protected inheritance or multiple, ambiguous
833  /// base class subobjects.
834  ///
835  /// \param Base the base class we are searching for.
836  ///
837  /// \param Paths will contain the paths taken from the current class to the
838  /// given \p Base class.
839  ///
840  /// \returns true if this class is derived from Base, false otherwise.
841  ///
842  /// \todo add a separate paramaeter to configure IsDerivedFrom, rather than
843  /// tangling input and output in \p Paths
844  bool isDerivedFrom(const CXXRecordDecl *Base, CXXBasePaths &Paths) const;
845
846  /// \brief Determine whether this class is virtually derived from
847  /// the class \p Base.
848  ///
849  /// This routine only determines whether this class is virtually
850  /// derived from \p Base, but does not account for factors that may
851  /// make a Derived -> Base class ill-formed, such as
852  /// private/protected inheritance or multiple, ambiguous base class
853  /// subobjects.
854  ///
855  /// \param Base the base class we are searching for.
856  ///
857  /// \returns true if this class is virtually derived from Base,
858  /// false otherwise.
859  bool isVirtuallyDerivedFrom(CXXRecordDecl *Base) const;
860
861  /// \brief Determine whether this class is provably not derived from
862  /// the type \p Base.
863  bool isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const;
864
865  /// \brief Function type used by forallBases() as a callback.
866  ///
867  /// \param Base the definition of the base class
868  ///
869  /// \returns true if this base matched the search criteria
870  typedef bool ForallBasesCallback(const CXXRecordDecl *BaseDefinition,
871                                   void *UserData);
872
873  /// \brief Determines if the given callback holds for all the direct
874  /// or indirect base classes of this type.
875  ///
876  /// The class itself does not count as a base class.  This routine
877  /// returns false if the class has non-computable base classes.
878  ///
879  /// \param AllowShortCircuit if false, forces the callback to be called
880  /// for every base class, even if a dependent or non-matching base was
881  /// found.
882  bool forallBases(ForallBasesCallback *BaseMatches, void *UserData,
883                   bool AllowShortCircuit = true) const;
884
885  /// \brief Function type used by lookupInBases() to determine whether a
886  /// specific base class subobject matches the lookup criteria.
887  ///
888  /// \param Specifier the base-class specifier that describes the inheritance
889  /// from the base class we are trying to match.
890  ///
891  /// \param Path the current path, from the most-derived class down to the
892  /// base named by the \p Specifier.
893  ///
894  /// \param UserData a single pointer to user-specified data, provided to
895  /// lookupInBases().
896  ///
897  /// \returns true if this base matched the search criteria, false otherwise.
898  typedef bool BaseMatchesCallback(const CXXBaseSpecifier *Specifier,
899                                   CXXBasePath &Path,
900                                   void *UserData);
901
902  /// \brief Look for entities within the base classes of this C++ class,
903  /// transitively searching all base class subobjects.
904  ///
905  /// This routine uses the callback function \p BaseMatches to find base
906  /// classes meeting some search criteria, walking all base class subobjects
907  /// and populating the given \p Paths structure with the paths through the
908  /// inheritance hierarchy that resulted in a match. On a successful search,
909  /// the \p Paths structure can be queried to retrieve the matching paths and
910  /// to determine if there were any ambiguities.
911  ///
912  /// \param BaseMatches callback function used to determine whether a given
913  /// base matches the user-defined search criteria.
914  ///
915  /// \param UserData user data pointer that will be provided to \p BaseMatches.
916  ///
917  /// \param Paths used to record the paths from this class to its base class
918  /// subobjects that match the search criteria.
919  ///
920  /// \returns true if there exists any path from this class to a base class
921  /// subobject that matches the search criteria.
922  bool lookupInBases(BaseMatchesCallback *BaseMatches, void *UserData,
923                     CXXBasePaths &Paths) const;
924
925  /// \brief Base-class lookup callback that determines whether the given
926  /// base class specifier refers to a specific class declaration.
927  ///
928  /// This callback can be used with \c lookupInBases() to determine whether
929  /// a given derived class has is a base class subobject of a particular type.
930  /// The user data pointer should refer to the canonical CXXRecordDecl of the
931  /// base class that we are searching for.
932  static bool FindBaseClass(const CXXBaseSpecifier *Specifier,
933                            CXXBasePath &Path, void *BaseRecord);
934
935  /// \brief Base-class lookup callback that determines whether the
936  /// given base class specifier refers to a specific class
937  /// declaration and describes virtual derivation.
938  ///
939  /// This callback can be used with \c lookupInBases() to determine
940  /// whether a given derived class has is a virtual base class
941  /// subobject of a particular type.  The user data pointer should
942  /// refer to the canonical CXXRecordDecl of the base class that we
943  /// are searching for.
944  static bool FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
945                                   CXXBasePath &Path, void *BaseRecord);
946
947  /// \brief Base-class lookup callback that determines whether there exists
948  /// a tag with the given name.
949  ///
950  /// This callback can be used with \c lookupInBases() to find tag members
951  /// of the given name within a C++ class hierarchy. The user data pointer
952  /// is an opaque \c DeclarationName pointer.
953  static bool FindTagMember(const CXXBaseSpecifier *Specifier,
954                            CXXBasePath &Path, void *Name);
955
956  /// \brief Base-class lookup callback that determines whether there exists
957  /// a member with the given name.
958  ///
959  /// This callback can be used with \c lookupInBases() to find members
960  /// of the given name within a C++ class hierarchy. The user data pointer
961  /// is an opaque \c DeclarationName pointer.
962  static bool FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
963                                 CXXBasePath &Path, void *Name);
964
965  /// \brief Base-class lookup callback that determines whether there exists
966  /// a member with the given name that can be used in a nested-name-specifier.
967  ///
968  /// This callback can be used with \c lookupInBases() to find membes of
969  /// the given name within a C++ class hierarchy that can occur within
970  /// nested-name-specifiers.
971  static bool FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
972                                            CXXBasePath &Path,
973                                            void *UserData);
974
975  /// \brief Retrieve the final overriders for each virtual member
976  /// function in the class hierarchy where this class is the
977  /// most-derived class in the class hierarchy.
978  void getFinalOverriders(CXXFinalOverriderMap &FinaOverriders) const;
979
980  /// \brief Get the indirect primary bases for this class.
981  void getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const;
982
983  /// viewInheritance - Renders and displays an inheritance diagram
984  /// for this C++ class and all of its base classes (transitively) using
985  /// GraphViz.
986  void viewInheritance(ASTContext& Context) const;
987
988  /// MergeAccess - Calculates the access of a decl that is reached
989  /// along a path.
990  static AccessSpecifier MergeAccess(AccessSpecifier PathAccess,
991                                     AccessSpecifier DeclAccess) {
992    assert(DeclAccess != AS_none);
993    if (DeclAccess == AS_private) return AS_none;
994    return (PathAccess > DeclAccess ? PathAccess : DeclAccess);
995  }
996
997  /// \brief Indicates that the definition of this class is now complete.
998  virtual void completeDefinition();
999
1000  /// \brief Indicates that the definition of this class is now complete,
1001  /// and provides a final overrider map to help determine
1002  ///
1003  /// \param FinalOverriders The final overrider map for this class, which can
1004  /// be provided as an optimization for abstract-class checking. If NULL,
1005  /// final overriders will be computed if they are needed to complete the
1006  /// definition.
1007  void completeDefinition(CXXFinalOverriderMap *FinalOverriders);
1008
1009  /// \brief Determine whether this class may end up being abstract, even though
1010  /// it is not yet known to be abstract.
1011  ///
1012  /// \returns true if this class is not known to be abstract but has any
1013  /// base classes that are abstract. In this case, \c completeDefinition()
1014  /// will need to compute final overriders to determine whether the class is
1015  /// actually abstract.
1016  bool mayBeAbstract() const;
1017
1018  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1019  static bool classofKind(Kind K) {
1020    return K >= firstCXXRecord && K <= lastCXXRecord;
1021  }
1022  static bool classof(const CXXRecordDecl *D) { return true; }
1023  static bool classof(const ClassTemplateSpecializationDecl *D) {
1024    return true;
1025  }
1026
1027  friend class ASTDeclReader;
1028  friend class ASTDeclWriter;
1029  friend class ASTReader;
1030  friend class ASTWriter;
1031};
1032
1033/// CXXMethodDecl - Represents a static or instance method of a
1034/// struct/union/class.
1035class CXXMethodDecl : public FunctionDecl {
1036protected:
1037  CXXMethodDecl(Kind DK, CXXRecordDecl *RD,
1038                const DeclarationNameInfo &NameInfo,
1039                QualType T, TypeSourceInfo *TInfo,
1040                bool isStatic, StorageClass SCAsWritten, bool isInline)
1041    : FunctionDecl(DK, RD, NameInfo, T, TInfo, (isStatic ? SC_Static : SC_None),
1042                   SCAsWritten, isInline) {}
1043
1044public:
1045  static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD,
1046                               const DeclarationNameInfo &NameInfo,
1047                               QualType T, TypeSourceInfo *TInfo,
1048                               bool isStatic = false,
1049                               StorageClass SCAsWritten = SC_None,
1050                               bool isInline = false);
1051
1052  bool isStatic() const { return getStorageClass() == SC_Static; }
1053  bool isInstance() const { return !isStatic(); }
1054
1055  bool isVirtual() const {
1056    CXXMethodDecl *CD =
1057      cast<CXXMethodDecl>(const_cast<CXXMethodDecl*>(this)->getCanonicalDecl());
1058
1059    if (CD->isVirtualAsWritten())
1060      return true;
1061
1062    return (CD->begin_overridden_methods() != CD->end_overridden_methods());
1063  }
1064
1065  /// \brief Determine whether this is a usual deallocation function
1066  /// (C++ [basic.stc.dynamic.deallocation]p2), which is an overloaded
1067  /// delete or delete[] operator with a particular signature.
1068  bool isUsualDeallocationFunction() const;
1069
1070  /// \brief Determine whether this is a copy-assignment operator, regardless
1071  /// of whether it was declared implicitly or explicitly.
1072  bool isCopyAssignmentOperator() const;
1073
1074  const CXXMethodDecl *getCanonicalDecl() const {
1075    return cast<CXXMethodDecl>(FunctionDecl::getCanonicalDecl());
1076  }
1077  CXXMethodDecl *getCanonicalDecl() {
1078    return cast<CXXMethodDecl>(FunctionDecl::getCanonicalDecl());
1079  }
1080
1081  ///
1082  void addOverriddenMethod(const CXXMethodDecl *MD);
1083
1084  typedef const CXXMethodDecl ** method_iterator;
1085
1086  method_iterator begin_overridden_methods() const;
1087  method_iterator end_overridden_methods() const;
1088  unsigned size_overridden_methods() const;
1089
1090  /// getParent - Returns the parent of this method declaration, which
1091  /// is the class in which this method is defined.
1092  const CXXRecordDecl *getParent() const {
1093    return cast<CXXRecordDecl>(FunctionDecl::getParent());
1094  }
1095
1096  /// getParent - Returns the parent of this method declaration, which
1097  /// is the class in which this method is defined.
1098  CXXRecordDecl *getParent() {
1099    return const_cast<CXXRecordDecl *>(
1100             cast<CXXRecordDecl>(FunctionDecl::getParent()));
1101  }
1102
1103  /// getThisType - Returns the type of 'this' pointer.
1104  /// Should only be called for instance methods.
1105  QualType getThisType(ASTContext &C) const;
1106
1107  unsigned getTypeQualifiers() const {
1108    return getType()->getAs<FunctionProtoType>()->getTypeQuals();
1109  }
1110
1111  /// \brief Retrieve the ref-qualifier associated with this method.
1112  ///
1113  /// In the following example, \c f() has an lvalue ref-qualifier, \c g()
1114  /// has an rvalue ref-qualifier, and \c h() has no ref-qualifier.
1115  /// \code
1116  /// struct X {
1117  ///   void f() &;
1118  ///   void g() &&;
1119  ///   void h();
1120  /// };
1121  RefQualifierKind getRefQualifier() const {
1122    return getType()->getAs<FunctionProtoType>()->getRefQualifier();
1123  }
1124
1125  bool hasInlineBody() const;
1126
1127  // Implement isa/cast/dyncast/etc.
1128  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1129  static bool classof(const CXXMethodDecl *D) { return true; }
1130  static bool classofKind(Kind K) {
1131    return K >= firstCXXMethod && K <= lastCXXMethod;
1132  }
1133};
1134
1135/// CXXCtorInitializer - Represents a C++ base or member
1136/// initializer, which is part of a constructor initializer that
1137/// initializes one non-static member variable or one base class. For
1138/// example, in the following, both 'A(a)' and 'f(3.14159)' are member
1139/// initializers:
1140///
1141/// @code
1142/// class A { };
1143/// class B : public A {
1144///   float f;
1145/// public:
1146///   B(A& a) : A(a), f(3.14159) { }
1147/// };
1148/// @endcode
1149class CXXCtorInitializer {
1150  /// \brief Either the base class name (stored as a TypeSourceInfo*), an normal
1151  /// field (FieldDecl), anonymous field (IndirectFieldDecl*), or target
1152  /// constructor (CXXConstructorDecl*) being initialized.
1153  llvm::PointerUnion4<TypeSourceInfo *, FieldDecl *, IndirectFieldDecl *,
1154                      CXXConstructorDecl *>
1155    Initializee;
1156
1157  /// \brief The source location for the field name or, for a base initializer
1158  /// pack expansion, the location of the ellipsis. In the case of a delegating
1159  /// constructor, it will still include the type's source location as the
1160  /// Initializee points to the CXXConstructorDecl (to allow loop detection).
1161  SourceLocation MemberOrEllipsisLocation;
1162
1163  /// \brief The argument used to initialize the base or member, which may
1164  /// end up constructing an object (when multiple arguments are involved).
1165  Stmt *Init;
1166
1167  /// LParenLoc - Location of the left paren of the ctor-initializer.
1168  SourceLocation LParenLoc;
1169
1170  /// RParenLoc - Location of the right paren of the ctor-initializer.
1171  SourceLocation RParenLoc;
1172
1173  /// IsVirtual - If the initializer is a base initializer, this keeps track
1174  /// of whether the base is virtual or not.
1175  bool IsVirtual : 1;
1176
1177  /// IsWritten - Whether or not the initializer is explicitly written
1178  /// in the sources.
1179  bool IsWritten : 1;
1180
1181  /// SourceOrderOrNumArrayIndices - If IsWritten is true, then this
1182  /// number keeps track of the textual order of this initializer in the
1183  /// original sources, counting from 0; otherwise, if IsWritten is false,
1184  /// it stores the number of array index variables stored after this
1185  /// object in memory.
1186  unsigned SourceOrderOrNumArrayIndices : 14;
1187
1188  CXXCtorInitializer(ASTContext &Context, FieldDecl *Member,
1189                     SourceLocation MemberLoc, SourceLocation L, Expr *Init,
1190                     SourceLocation R, VarDecl **Indices, unsigned NumIndices);
1191
1192public:
1193  /// CXXCtorInitializer - Creates a new base-class initializer.
1194  explicit
1195  CXXCtorInitializer(ASTContext &Context, TypeSourceInfo *TInfo, bool IsVirtual,
1196                     SourceLocation L, Expr *Init, SourceLocation R,
1197                     SourceLocation EllipsisLoc);
1198
1199  /// CXXCtorInitializer - Creates a new member initializer.
1200  explicit
1201  CXXCtorInitializer(ASTContext &Context, FieldDecl *Member,
1202                     SourceLocation MemberLoc, SourceLocation L, Expr *Init,
1203                     SourceLocation R);
1204
1205  /// CXXCtorInitializer - Creates a new anonymous field initializer.
1206  explicit
1207  CXXCtorInitializer(ASTContext &Context, IndirectFieldDecl *Member,
1208                     SourceLocation MemberLoc, SourceLocation L, Expr *Init,
1209                     SourceLocation R);
1210
1211  /// CXXCtorInitializer - Creates a new delegating Initializer.
1212  explicit
1213  CXXCtorInitializer(ASTContext &Context, SourceLocation D, SourceLocation L,
1214                     CXXConstructorDecl *Target, Expr *Init, SourceLocation R);
1215
1216  /// \brief Creates a new member initializer that optionally contains
1217  /// array indices used to describe an elementwise initialization.
1218  static CXXCtorInitializer *Create(ASTContext &Context, FieldDecl *Member,
1219                                    SourceLocation MemberLoc, SourceLocation L,
1220                                    Expr *Init, SourceLocation R,
1221                                    VarDecl **Indices, unsigned NumIndices);
1222
1223  /// isBaseInitializer - Returns true when this initializer is
1224  /// initializing a base class.
1225  bool isBaseInitializer() const { return Initializee.is<TypeSourceInfo*>(); }
1226
1227  /// isMemberInitializer - Returns true when this initializer is
1228  /// initializing a non-static data member.
1229  bool isMemberInitializer() const { return Initializee.is<FieldDecl*>(); }
1230
1231  bool isAnyMemberInitializer() const {
1232    return isMemberInitializer() || isIndirectMemberInitializer();
1233  }
1234
1235  bool isIndirectMemberInitializer() const {
1236    return Initializee.is<IndirectFieldDecl*>();
1237  }
1238
1239  /// isDelegatingInitializer - Returns true when this initializer is creating
1240  /// a delegating constructor.
1241  bool isDelegatingInitializer() const {
1242    return Initializee.is<CXXConstructorDecl *>();
1243  }
1244
1245  /// \brief Determine whether this initializer is a pack expansion.
1246  bool isPackExpansion() const {
1247    return isBaseInitializer() && MemberOrEllipsisLocation.isValid();
1248  }
1249
1250  // \brief For a pack expansion, returns the location of the ellipsis.
1251  SourceLocation getEllipsisLoc() const {
1252    assert(isPackExpansion() && "Initializer is not a pack expansion");
1253    return MemberOrEllipsisLocation;
1254  }
1255
1256  /// If this is a base class initializer, returns the type of the
1257  /// base class with location information. Otherwise, returns an NULL
1258  /// type location.
1259  TypeLoc getBaseClassLoc() const;
1260
1261  /// If this is a base class initializer, returns the type of the base class.
1262  /// Otherwise, returns NULL.
1263  const Type *getBaseClass() const;
1264
1265  /// Returns whether the base is virtual or not.
1266  bool isBaseVirtual() const {
1267    assert(isBaseInitializer() && "Must call this on base initializer!");
1268
1269    return IsVirtual;
1270  }
1271
1272  /// \brief Returns the declarator information for a base class initializer.
1273  TypeSourceInfo *getBaseClassInfo() const {
1274    return Initializee.dyn_cast<TypeSourceInfo *>();
1275  }
1276
1277  /// getMember - If this is a member initializer, returns the
1278  /// declaration of the non-static data member being
1279  /// initialized. Otherwise, returns NULL.
1280  FieldDecl *getMember() const {
1281    if (isMemberInitializer())
1282      return Initializee.get<FieldDecl*>();
1283    else
1284      return 0;
1285  }
1286  FieldDecl *getAnyMember() const {
1287    if (isMemberInitializer())
1288      return Initializee.get<FieldDecl*>();
1289    else if (isIndirectMemberInitializer())
1290      return Initializee.get<IndirectFieldDecl*>()->getAnonField();
1291    else
1292      return 0;
1293  }
1294
1295  IndirectFieldDecl *getIndirectMember() const {
1296    if (isIndirectMemberInitializer())
1297      return Initializee.get<IndirectFieldDecl*>();
1298    else
1299      return 0;
1300  }
1301
1302  CXXConstructorDecl *getTargetConstructor() const {
1303    if (isDelegatingInitializer())
1304      return Initializee.get<CXXConstructorDecl*>();
1305    else
1306      return 0;
1307  }
1308
1309  SourceLocation getMemberLocation() const {
1310    return MemberOrEllipsisLocation;
1311  }
1312
1313  /// \brief Determine the source location of the initializer.
1314  SourceLocation getSourceLocation() const;
1315
1316  /// \brief Determine the source range covering the entire initializer.
1317  SourceRange getSourceRange() const;
1318
1319  /// isWritten - Returns true if this initializer is explicitly written
1320  /// in the source code.
1321  bool isWritten() const { return IsWritten; }
1322
1323  /// \brief Return the source position of the initializer, counting from 0.
1324  /// If the initializer was implicit, -1 is returned.
1325  int getSourceOrder() const {
1326    return IsWritten ? static_cast<int>(SourceOrderOrNumArrayIndices) : -1;
1327  }
1328
1329  /// \brief Set the source order of this initializer. This method can only
1330  /// be called once for each initializer; it cannot be called on an
1331  /// initializer having a positive number of (implicit) array indices.
1332  void setSourceOrder(int pos) {
1333    assert(!IsWritten &&
1334           "calling twice setSourceOrder() on the same initializer");
1335    assert(SourceOrderOrNumArrayIndices == 0 &&
1336           "setSourceOrder() used when there are implicit array indices");
1337    assert(pos >= 0 &&
1338           "setSourceOrder() used to make an initializer implicit");
1339    IsWritten = true;
1340    SourceOrderOrNumArrayIndices = static_cast<unsigned>(pos);
1341  }
1342
1343  SourceLocation getLParenLoc() const { return LParenLoc; }
1344  SourceLocation getRParenLoc() const { return RParenLoc; }
1345
1346  /// \brief Determine the number of implicit array indices used while
1347  /// described an array member initialization.
1348  unsigned getNumArrayIndices() const {
1349    return IsWritten ? 0 : SourceOrderOrNumArrayIndices;
1350  }
1351
1352  /// \brief Retrieve a particular array index variable used to
1353  /// describe an array member initialization.
1354  VarDecl *getArrayIndex(unsigned I) {
1355    assert(I < getNumArrayIndices() && "Out of bounds member array index");
1356    return reinterpret_cast<VarDecl **>(this + 1)[I];
1357  }
1358  const VarDecl *getArrayIndex(unsigned I) const {
1359    assert(I < getNumArrayIndices() && "Out of bounds member array index");
1360    return reinterpret_cast<const VarDecl * const *>(this + 1)[I];
1361  }
1362  void setArrayIndex(unsigned I, VarDecl *Index) {
1363    assert(I < getNumArrayIndices() && "Out of bounds member array index");
1364    reinterpret_cast<VarDecl **>(this + 1)[I] = Index;
1365  }
1366
1367  Expr *getInit() const { return static_cast<Expr *>(Init); }
1368};
1369
1370/// CXXConstructorDecl - Represents a C++ constructor within a
1371/// class. For example:
1372///
1373/// @code
1374/// class X {
1375/// public:
1376///   explicit X(int); // represented by a CXXConstructorDecl.
1377/// };
1378/// @endcode
1379class CXXConstructorDecl : public CXXMethodDecl {
1380  /// IsExplicitSpecified - Whether this constructor declaration has the
1381  /// 'explicit' keyword specified.
1382  bool IsExplicitSpecified : 1;
1383
1384  /// ImplicitlyDefined - Whether this constructor was implicitly
1385  /// defined by the compiler. When false, the constructor was defined
1386  /// by the user. In C++03, this flag will have the same value as
1387  /// Implicit. In C++0x, however, a constructor that is
1388  /// explicitly defaulted (i.e., defined with " = default") will have
1389  /// @c !Implicit && ImplicitlyDefined.
1390  bool ImplicitlyDefined : 1;
1391
1392  /// Support for base and member initializers.
1393  /// CtorInitializers - The arguments used to initialize the base
1394  /// or member.
1395  CXXCtorInitializer **CtorInitializers;
1396  unsigned NumCtorInitializers;
1397
1398  CXXConstructorDecl(CXXRecordDecl *RD, const DeclarationNameInfo &NameInfo,
1399                     QualType T, TypeSourceInfo *TInfo,
1400                     bool isExplicitSpecified, bool isInline,
1401                     bool isImplicitlyDeclared)
1402    : CXXMethodDecl(CXXConstructor, RD, NameInfo, T, TInfo, false,
1403                    SC_None, isInline),
1404      IsExplicitSpecified(isExplicitSpecified), ImplicitlyDefined(false),
1405      CtorInitializers(0), NumCtorInitializers(0) {
1406    setImplicit(isImplicitlyDeclared);
1407  }
1408
1409public:
1410  static CXXConstructorDecl *Create(ASTContext &C, EmptyShell Empty);
1411  static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
1412                                    const DeclarationNameInfo &NameInfo,
1413                                    QualType T, TypeSourceInfo *TInfo,
1414                                    bool isExplicit,
1415                                    bool isInline, bool isImplicitlyDeclared);
1416
1417  /// isExplicitSpecified - Whether this constructor declaration has the
1418  /// 'explicit' keyword specified.
1419  bool isExplicitSpecified() const { return IsExplicitSpecified; }
1420
1421  /// isExplicit - Whether this constructor was marked "explicit" or not.
1422  bool isExplicit() const {
1423    return cast<CXXConstructorDecl>(getFirstDeclaration())
1424      ->isExplicitSpecified();
1425  }
1426
1427  /// isImplicitlyDefined - Whether this constructor was implicitly
1428  /// defined. If false, then this constructor was defined by the
1429  /// user. This operation can only be invoked if the constructor has
1430  /// already been defined.
1431  bool isImplicitlyDefined() const {
1432    assert(isThisDeclarationADefinition() &&
1433           "Can only get the implicit-definition flag once the "
1434           "constructor has been defined");
1435    return ImplicitlyDefined;
1436  }
1437
1438  /// setImplicitlyDefined - Set whether this constructor was
1439  /// implicitly defined or not.
1440  void setImplicitlyDefined(bool ID) {
1441    assert(isThisDeclarationADefinition() &&
1442           "Can only set the implicit-definition flag once the constructor "
1443           "has been defined");
1444    ImplicitlyDefined = ID;
1445  }
1446
1447  /// init_iterator - Iterates through the member/base initializer list.
1448  typedef CXXCtorInitializer **init_iterator;
1449
1450  /// init_const_iterator - Iterates through the memberbase initializer list.
1451  typedef CXXCtorInitializer * const * init_const_iterator;
1452
1453  /// init_begin() - Retrieve an iterator to the first initializer.
1454  init_iterator       init_begin()       { return CtorInitializers; }
1455  /// begin() - Retrieve an iterator to the first initializer.
1456  init_const_iterator init_begin() const { return CtorInitializers; }
1457
1458  /// init_end() - Retrieve an iterator past the last initializer.
1459  init_iterator       init_end()       {
1460    return CtorInitializers + NumCtorInitializers;
1461  }
1462  /// end() - Retrieve an iterator past the last initializer.
1463  init_const_iterator init_end() const {
1464    return CtorInitializers + NumCtorInitializers;
1465  }
1466
1467  typedef std::reverse_iterator<init_iterator> init_reverse_iterator;
1468  typedef std::reverse_iterator<init_const_iterator> init_const_reverse_iterator;
1469
1470  init_reverse_iterator init_rbegin() {
1471    return init_reverse_iterator(init_end());
1472  }
1473  init_const_reverse_iterator init_rbegin() const {
1474    return init_const_reverse_iterator(init_end());
1475  }
1476
1477  init_reverse_iterator init_rend() {
1478    return init_reverse_iterator(init_begin());
1479  }
1480  init_const_reverse_iterator init_rend() const {
1481    return init_const_reverse_iterator(init_begin());
1482  }
1483
1484  /// getNumArgs - Determine the number of arguments used to
1485  /// initialize the member or base.
1486  unsigned getNumCtorInitializers() const {
1487      return NumCtorInitializers;
1488  }
1489
1490  void setNumCtorInitializers(unsigned numCtorInitializers) {
1491    NumCtorInitializers = numCtorInitializers;
1492  }
1493
1494  void setCtorInitializers(CXXCtorInitializer ** initializers) {
1495    CtorInitializers = initializers;
1496  }
1497  /// isDefaultConstructor - Whether this constructor is a default
1498  /// constructor (C++ [class.ctor]p5), which can be used to
1499  /// default-initialize a class of this type.
1500  bool isDefaultConstructor() const;
1501
1502  /// isCopyConstructor - Whether this constructor is a copy
1503  /// constructor (C++ [class.copy]p2, which can be used to copy the
1504  /// class. @p TypeQuals will be set to the qualifiers on the
1505  /// argument type. For example, @p TypeQuals would be set to @c
1506  /// QualType::Const for the following copy constructor:
1507  ///
1508  /// @code
1509  /// class X {
1510  /// public:
1511  ///   X(const X&);
1512  /// };
1513  /// @endcode
1514  bool isCopyConstructor(unsigned &TypeQuals) const;
1515
1516  /// isCopyConstructor - Whether this constructor is a copy
1517  /// constructor (C++ [class.copy]p2, which can be used to copy the
1518  /// class.
1519  bool isCopyConstructor() const {
1520    unsigned TypeQuals = 0;
1521    return isCopyConstructor(TypeQuals);
1522  }
1523
1524  /// \brief Determine whether this constructor is a move constructor
1525  /// (C++0x [class.copy]p3), which can be used to move values of the class.
1526  ///
1527  /// \param TypeQuals If this constructor is a move constructor, will be set
1528  /// to the type qualifiers on the referent of the first parameter's type.
1529  bool isMoveConstructor(unsigned &TypeQuals) const;
1530
1531  /// \brief Determine whether this constructor is a move constructor
1532  /// (C++0x [class.copy]p3), which can be used to move values of the class.
1533  bool isMoveConstructor() const;
1534
1535  /// \brief Determine whether this is a copy or move constructor.
1536  ///
1537  /// \param TypeQuals Will be set to the type qualifiers on the reference
1538  /// parameter, if in fact this is a copy or move constructor.
1539  bool isCopyOrMoveConstructor(unsigned &TypeQuals) const;
1540
1541  /// \brief Determine whether this a copy or move constructor.
1542  bool isCopyOrMoveConstructor() const {
1543    unsigned Quals;
1544    return isCopyOrMoveConstructor(Quals);
1545  }
1546
1547  /// isConvertingConstructor - Whether this constructor is a
1548  /// converting constructor (C++ [class.conv.ctor]), which can be
1549  /// used for user-defined conversions.
1550  bool isConvertingConstructor(bool AllowExplicit) const;
1551
1552  /// \brief Determine whether this is a member template specialization that
1553  /// would copy the object to itself. Such constructors are never used to copy
1554  /// an object.
1555  bool isSpecializationCopyingObject() const;
1556
1557  /// \brief Get the constructor that this inheriting constructor is based on.
1558  const CXXConstructorDecl *getInheritedConstructor() const;
1559
1560  /// \brief Set the constructor that this inheriting constructor is based on.
1561  void setInheritedConstructor(const CXXConstructorDecl *BaseCtor);
1562
1563  // Implement isa/cast/dyncast/etc.
1564  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1565  static bool classof(const CXXConstructorDecl *D) { return true; }
1566  static bool classofKind(Kind K) { return K == CXXConstructor; }
1567
1568  friend class ASTDeclReader;
1569  friend class ASTDeclWriter;
1570};
1571
1572/// CXXDestructorDecl - Represents a C++ destructor within a
1573/// class. For example:
1574///
1575/// @code
1576/// class X {
1577/// public:
1578///   ~X(); // represented by a CXXDestructorDecl.
1579/// };
1580/// @endcode
1581class CXXDestructorDecl : public CXXMethodDecl {
1582  /// ImplicitlyDefined - Whether this destructor was implicitly
1583  /// defined by the compiler. When false, the destructor was defined
1584  /// by the user. In C++03, this flag will have the same value as
1585  /// Implicit. In C++0x, however, a destructor that is
1586  /// explicitly defaulted (i.e., defined with " = default") will have
1587  /// @c !Implicit && ImplicitlyDefined.
1588  bool ImplicitlyDefined : 1;
1589
1590  FunctionDecl *OperatorDelete;
1591
1592  CXXDestructorDecl(CXXRecordDecl *RD, const DeclarationNameInfo &NameInfo,
1593                    QualType T, TypeSourceInfo *TInfo,
1594                    bool isInline, bool isImplicitlyDeclared)
1595    : CXXMethodDecl(CXXDestructor, RD, NameInfo, T, TInfo, false,
1596                    SC_None, isInline),
1597      ImplicitlyDefined(false), OperatorDelete(0) {
1598    setImplicit(isImplicitlyDeclared);
1599  }
1600
1601public:
1602  static CXXDestructorDecl *Create(ASTContext& C, EmptyShell Empty);
1603  static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
1604                                   const DeclarationNameInfo &NameInfo,
1605                                   QualType T, TypeSourceInfo* TInfo,
1606                                   bool isInline,
1607                                   bool isImplicitlyDeclared);
1608
1609  /// isImplicitlyDefined - Whether this destructor was implicitly
1610  /// defined. If false, then this destructor was defined by the
1611  /// user. This operation can only be invoked if the destructor has
1612  /// already been defined.
1613  bool isImplicitlyDefined() const {
1614    assert(isThisDeclarationADefinition() &&
1615           "Can only get the implicit-definition flag once the destructor has been defined");
1616    return ImplicitlyDefined;
1617  }
1618
1619  /// setImplicitlyDefined - Set whether this destructor was
1620  /// implicitly defined or not.
1621  void setImplicitlyDefined(bool ID) {
1622    assert(isThisDeclarationADefinition() &&
1623           "Can only set the implicit-definition flag once the destructor has been defined");
1624    ImplicitlyDefined = ID;
1625  }
1626
1627  void setOperatorDelete(FunctionDecl *OD) { OperatorDelete = OD; }
1628  const FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
1629
1630  // Implement isa/cast/dyncast/etc.
1631  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1632  static bool classof(const CXXDestructorDecl *D) { return true; }
1633  static bool classofKind(Kind K) { return K == CXXDestructor; }
1634
1635  friend class ASTDeclReader;
1636  friend class ASTDeclWriter;
1637};
1638
1639/// CXXConversionDecl - Represents a C++ conversion function within a
1640/// class. For example:
1641///
1642/// @code
1643/// class X {
1644/// public:
1645///   operator bool();
1646/// };
1647/// @endcode
1648class CXXConversionDecl : public CXXMethodDecl {
1649  /// IsExplicitSpecified - Whether this conversion function declaration is
1650  /// marked "explicit", meaning that it can only be applied when the user
1651  /// explicitly wrote a cast. This is a C++0x feature.
1652  bool IsExplicitSpecified : 1;
1653
1654  CXXConversionDecl(CXXRecordDecl *RD, const DeclarationNameInfo &NameInfo,
1655                    QualType T, TypeSourceInfo *TInfo,
1656                    bool isInline, bool isExplicitSpecified)
1657    : CXXMethodDecl(CXXConversion, RD, NameInfo, T, TInfo, false,
1658                    SC_None, isInline),
1659      IsExplicitSpecified(isExplicitSpecified) { }
1660
1661public:
1662  static CXXConversionDecl *Create(ASTContext &C, EmptyShell Empty);
1663  static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD,
1664                                   const DeclarationNameInfo &NameInfo,
1665                                   QualType T, TypeSourceInfo *TInfo,
1666                                   bool isInline, bool isExplicit);
1667
1668  /// IsExplicitSpecified - Whether this conversion function declaration is
1669  /// marked "explicit", meaning that it can only be applied when the user
1670  /// explicitly wrote a cast. This is a C++0x feature.
1671  bool isExplicitSpecified() const { return IsExplicitSpecified; }
1672
1673  /// isExplicit - Whether this is an explicit conversion operator
1674  /// (C++0x only). Explicit conversion operators are only considered
1675  /// when the user has explicitly written a cast.
1676  bool isExplicit() const {
1677    return cast<CXXConversionDecl>(getFirstDeclaration())
1678      ->isExplicitSpecified();
1679  }
1680
1681  /// getConversionType - Returns the type that this conversion
1682  /// function is converting to.
1683  QualType getConversionType() const {
1684    return getType()->getAs<FunctionType>()->getResultType();
1685  }
1686
1687  // Implement isa/cast/dyncast/etc.
1688  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1689  static bool classof(const CXXConversionDecl *D) { return true; }
1690  static bool classofKind(Kind K) { return K == CXXConversion; }
1691
1692  friend class ASTDeclReader;
1693  friend class ASTDeclWriter;
1694};
1695
1696/// LinkageSpecDecl - This represents a linkage specification.  For example:
1697///   extern "C" void foo();
1698///
1699class LinkageSpecDecl : public Decl, public DeclContext {
1700public:
1701  /// LanguageIDs - Used to represent the language in a linkage
1702  /// specification.  The values are part of the serialization abi for
1703  /// ASTs and cannot be changed without altering that abi.  To help
1704  /// ensure a stable abi for this, we choose the DW_LANG_ encodings
1705  /// from the dwarf standard.
1706  enum LanguageIDs {
1707    lang_c = /* DW_LANG_C */ 0x0002,
1708    lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004
1709  };
1710private:
1711  /// Language - The language for this linkage specification.
1712  LanguageIDs Language;
1713
1714  /// HadBraces - Whether this linkage specification had curly braces or not.
1715  bool HadBraces : 1;
1716
1717  LinkageSpecDecl(DeclContext *DC, SourceLocation L, LanguageIDs lang,
1718                  bool Braces)
1719    : Decl(LinkageSpec, DC, L),
1720      DeclContext(LinkageSpec), Language(lang), HadBraces(Braces) { }
1721
1722public:
1723  static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC,
1724                                 SourceLocation L, LanguageIDs Lang,
1725                                 bool Braces);
1726
1727  /// \brief Return the language specified by this linkage specification.
1728  LanguageIDs getLanguage() const { return Language; }
1729
1730  /// \brief Set the language specified by this linkage specification.
1731  void setLanguage(LanguageIDs L) { Language = L; }
1732
1733  /// \brief Determines whether this linkage specification had braces in
1734  /// its syntactic form.
1735  bool hasBraces() const { return HadBraces; }
1736
1737  /// \brief Set whether this linkage specification has braces in its
1738  /// syntactic form.
1739  void setHasBraces(bool B) { HadBraces = B; }
1740
1741  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1742  static bool classof(const LinkageSpecDecl *D) { return true; }
1743  static bool classofKind(Kind K) { return K == LinkageSpec; }
1744  static DeclContext *castToDeclContext(const LinkageSpecDecl *D) {
1745    return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D));
1746  }
1747  static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) {
1748    return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC));
1749  }
1750};
1751
1752/// UsingDirectiveDecl - Represents C++ using-directive. For example:
1753///
1754///    using namespace std;
1755///
1756// NB: UsingDirectiveDecl should be Decl not NamedDecl, but we provide
1757// artificial name, for all using-directives in order to store
1758// them in DeclContext effectively.
1759class UsingDirectiveDecl : public NamedDecl {
1760  /// \brief The location of the "using" keyword.
1761  SourceLocation UsingLoc;
1762
1763  /// SourceLocation - Location of 'namespace' token.
1764  SourceLocation NamespaceLoc;
1765
1766  /// \brief The nested-name-specifier that precedes the namespace.
1767  NestedNameSpecifierLoc QualifierLoc;
1768
1769  /// NominatedNamespace - Namespace nominated by using-directive.
1770  NamedDecl *NominatedNamespace;
1771
1772  /// Enclosing context containing both using-directive and nominated
1773  /// namespace.
1774  DeclContext *CommonAncestor;
1775
1776  /// getUsingDirectiveName - Returns special DeclarationName used by
1777  /// using-directives. This is only used by DeclContext for storing
1778  /// UsingDirectiveDecls in its lookup structure.
1779  static DeclarationName getName() {
1780    return DeclarationName::getUsingDirectiveName();
1781  }
1782
1783  UsingDirectiveDecl(DeclContext *DC, SourceLocation UsingLoc,
1784                     SourceLocation NamespcLoc,
1785                     NestedNameSpecifierLoc QualifierLoc,
1786                     SourceLocation IdentLoc,
1787                     NamedDecl *Nominated,
1788                     DeclContext *CommonAncestor)
1789    : NamedDecl(UsingDirective, DC, IdentLoc, getName()), UsingLoc(UsingLoc),
1790      NamespaceLoc(NamespcLoc), QualifierLoc(QualifierLoc),
1791      NominatedNamespace(Nominated), CommonAncestor(CommonAncestor) { }
1792
1793public:
1794  /// \brief Retrieve the nested-name-specifier that qualifies the
1795  /// name of the namespace, with source-location information.
1796  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
1797
1798  /// \brief Retrieve the nested-name-specifier that qualifies the
1799  /// name of the namespace.
1800  NestedNameSpecifier *getQualifier() const {
1801    return QualifierLoc.getNestedNameSpecifier();
1802  }
1803
1804  NamedDecl *getNominatedNamespaceAsWritten() { return NominatedNamespace; }
1805  const NamedDecl *getNominatedNamespaceAsWritten() const {
1806    return NominatedNamespace;
1807  }
1808
1809  /// getNominatedNamespace - Returns namespace nominated by using-directive.
1810  NamespaceDecl *getNominatedNamespace();
1811
1812  const NamespaceDecl *getNominatedNamespace() const {
1813    return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace();
1814  }
1815
1816  /// \brief Returns the common ancestor context of this using-directive and
1817  /// its nominated namespace.
1818  DeclContext *getCommonAncestor() { return CommonAncestor; }
1819  const DeclContext *getCommonAncestor() const { return CommonAncestor; }
1820
1821  /// \brief Return the location of the "using" keyword.
1822  SourceLocation getUsingLoc() const { return UsingLoc; }
1823
1824  // FIXME: Could omit 'Key' in name.
1825  /// getNamespaceKeyLocation - Returns location of namespace keyword.
1826  SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; }
1827
1828  /// getIdentLocation - Returns location of identifier.
1829  SourceLocation getIdentLocation() const { return getLocation(); }
1830
1831  static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC,
1832                                    SourceLocation UsingLoc,
1833                                    SourceLocation NamespaceLoc,
1834                                    NestedNameSpecifierLoc QualifierLoc,
1835                                    SourceLocation IdentLoc,
1836                                    NamedDecl *Nominated,
1837                                    DeclContext *CommonAncestor);
1838
1839  SourceRange getSourceRange() const {
1840    return SourceRange(UsingLoc, getLocation());
1841  }
1842
1843  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1844  static bool classof(const UsingDirectiveDecl *D) { return true; }
1845  static bool classofKind(Kind K) { return K == UsingDirective; }
1846
1847  // Friend for getUsingDirectiveName.
1848  friend class DeclContext;
1849
1850  friend class ASTDeclReader;
1851};
1852
1853/// NamespaceAliasDecl - Represents a C++ namespace alias. For example:
1854///
1855/// @code
1856/// namespace Foo = Bar;
1857/// @endcode
1858class NamespaceAliasDecl : public NamedDecl {
1859  /// \brief The location of the "namespace" keyword.
1860  SourceLocation NamespaceLoc;
1861
1862  /// IdentLoc - Location of namespace identifier. Accessed by TargetNameLoc.
1863  SourceLocation IdentLoc;
1864
1865  /// \brief The nested-name-specifier that precedes the namespace.
1866  NestedNameSpecifierLoc QualifierLoc;
1867
1868  /// Namespace - The Decl that this alias points to. Can either be a
1869  /// NamespaceDecl or a NamespaceAliasDecl.
1870  NamedDecl *Namespace;
1871
1872  NamespaceAliasDecl(DeclContext *DC, SourceLocation NamespaceLoc,
1873                     SourceLocation AliasLoc, IdentifierInfo *Alias,
1874                     NestedNameSpecifierLoc QualifierLoc,
1875                     SourceLocation IdentLoc, NamedDecl *Namespace)
1876    : NamedDecl(NamespaceAlias, DC, AliasLoc, Alias),
1877      NamespaceLoc(NamespaceLoc), IdentLoc(IdentLoc),
1878      QualifierLoc(QualifierLoc), Namespace(Namespace) { }
1879
1880  friend class ASTDeclReader;
1881
1882public:
1883  /// \brief Retrieve the nested-name-specifier that qualifies the
1884  /// name of the namespace, with source-location information.
1885  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
1886
1887  /// \brief Retrieve the nested-name-specifier that qualifies the
1888  /// name of the namespace.
1889  NestedNameSpecifier *getQualifier() const {
1890    return QualifierLoc.getNestedNameSpecifier();
1891  }
1892
1893  /// \brief Retrieve the namespace declaration aliased by this directive.
1894  NamespaceDecl *getNamespace() {
1895    if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(Namespace))
1896      return AD->getNamespace();
1897
1898    return cast<NamespaceDecl>(Namespace);
1899  }
1900
1901  const NamespaceDecl *getNamespace() const {
1902    return const_cast<NamespaceAliasDecl*>(this)->getNamespace();
1903  }
1904
1905  /// Returns the location of the alias name, i.e. 'foo' in
1906  /// "namespace foo = ns::bar;".
1907  SourceLocation getAliasLoc() const { return getLocation(); }
1908
1909  /// Returns the location of the 'namespace' keyword.
1910  SourceLocation getNamespaceLoc() const { return NamespaceLoc; }
1911
1912  /// Returns the location of the identifier in the named namespace.
1913  SourceLocation getTargetNameLoc() const { return IdentLoc; }
1914
1915  /// \brief Retrieve the namespace that this alias refers to, which
1916  /// may either be a NamespaceDecl or a NamespaceAliasDecl.
1917  NamedDecl *getAliasedNamespace() const { return Namespace; }
1918
1919  static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC,
1920                                    SourceLocation NamespaceLoc,
1921                                    SourceLocation AliasLoc,
1922                                    IdentifierInfo *Alias,
1923                                    NestedNameSpecifierLoc QualifierLoc,
1924                                    SourceLocation IdentLoc,
1925                                    NamedDecl *Namespace);
1926
1927  virtual SourceRange getSourceRange() const {
1928    return SourceRange(NamespaceLoc, IdentLoc);
1929  }
1930
1931  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1932  static bool classof(const NamespaceAliasDecl *D) { return true; }
1933  static bool classofKind(Kind K) { return K == NamespaceAlias; }
1934};
1935
1936/// UsingShadowDecl - Represents a shadow declaration introduced into
1937/// a scope by a (resolved) using declaration.  For example,
1938///
1939/// namespace A {
1940///   void foo();
1941/// }
1942/// namespace B {
1943///   using A::foo(); // <- a UsingDecl
1944///                   // Also creates a UsingShadowDecl for A::foo in B
1945/// }
1946///
1947class UsingShadowDecl : public NamedDecl {
1948  /// The referenced declaration.
1949  NamedDecl *Underlying;
1950
1951  /// \brief The using declaration which introduced this decl or the next using
1952  /// shadow declaration contained in the aforementioned using declaration.
1953  NamedDecl *UsingOrNextShadow;
1954  friend class UsingDecl;
1955
1956  UsingShadowDecl(DeclContext *DC, SourceLocation Loc, UsingDecl *Using,
1957                  NamedDecl *Target)
1958    : NamedDecl(UsingShadow, DC, Loc, DeclarationName()),
1959      Underlying(Target),
1960      UsingOrNextShadow(reinterpret_cast<NamedDecl *>(Using)) {
1961    if (Target) {
1962      setDeclName(Target->getDeclName());
1963      IdentifierNamespace = Target->getIdentifierNamespace();
1964    }
1965    setImplicit();
1966  }
1967
1968public:
1969  static UsingShadowDecl *Create(ASTContext &C, DeclContext *DC,
1970                                 SourceLocation Loc, UsingDecl *Using,
1971                                 NamedDecl *Target) {
1972    return new (C) UsingShadowDecl(DC, Loc, Using, Target);
1973  }
1974
1975  /// \brief Gets the underlying declaration which has been brought into the
1976  /// local scope.
1977  NamedDecl *getTargetDecl() const { return Underlying; }
1978
1979  /// \brief Sets the underlying declaration which has been brought into the
1980  /// local scope.
1981  void setTargetDecl(NamedDecl* ND) {
1982    assert(ND && "Target decl is null!");
1983    Underlying = ND;
1984    IdentifierNamespace = ND->getIdentifierNamespace();
1985  }
1986
1987  /// \brief Gets the using declaration to which this declaration is tied.
1988  UsingDecl *getUsingDecl() const;
1989
1990  /// \brief The next using shadow declaration contained in the shadow decl
1991  /// chain of the using declaration which introduced this decl.
1992  UsingShadowDecl *getNextUsingShadowDecl() const {
1993    return dyn_cast_or_null<UsingShadowDecl>(UsingOrNextShadow);
1994  }
1995
1996  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1997  static bool classof(const UsingShadowDecl *D) { return true; }
1998  static bool classofKind(Kind K) { return K == Decl::UsingShadow; }
1999
2000  friend class ASTDeclReader;
2001  friend class ASTDeclWriter;
2002};
2003
2004/// UsingDecl - Represents a C++ using-declaration. For example:
2005///    using someNameSpace::someIdentifier;
2006class UsingDecl : public NamedDecl {
2007  /// \brief The source location of the "using" location itself.
2008  SourceLocation UsingLocation;
2009
2010  /// \brief The nested-name-specifier that precedes the name.
2011  NestedNameSpecifierLoc QualifierLoc;
2012
2013  /// DNLoc - Provides source/type location info for the
2014  /// declaration name embedded in the ValueDecl base class.
2015  DeclarationNameLoc DNLoc;
2016
2017  /// \brief The first shadow declaration of the shadow decl chain associated
2018  /// with this using declaration.
2019  UsingShadowDecl *FirstUsingShadow;
2020
2021  // \brief Has 'typename' keyword.
2022  bool IsTypeName;
2023
2024  UsingDecl(DeclContext *DC, SourceLocation UL,
2025            NestedNameSpecifierLoc QualifierLoc,
2026            const DeclarationNameInfo &NameInfo, bool IsTypeNameArg)
2027    : NamedDecl(Using, DC, NameInfo.getLoc(), NameInfo.getName()),
2028      UsingLocation(UL), QualifierLoc(QualifierLoc),
2029      DNLoc(NameInfo.getInfo()), FirstUsingShadow(0),IsTypeName(IsTypeNameArg) {
2030  }
2031
2032public:
2033  /// \brief Returns the source location of the "using" keyword.
2034  SourceLocation getUsingLocation() const { return UsingLocation; }
2035
2036  /// \brief Set the source location of the 'using' keyword.
2037  void setUsingLocation(SourceLocation L) { UsingLocation = L; }
2038
2039  /// \brief Retrieve the nested-name-specifier that qualifies the name,
2040  /// with source-location information.
2041  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2042
2043  /// \brief Retrieve the nested-name-specifier that qualifies the name.
2044  NestedNameSpecifier *getQualifier() const {
2045    return QualifierLoc.getNestedNameSpecifier();
2046  }
2047
2048  DeclarationNameInfo getNameInfo() const {
2049    return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc);
2050  }
2051
2052  /// \brief Return true if the using declaration has 'typename'.
2053  bool isTypeName() const { return IsTypeName; }
2054
2055  /// \brief Sets whether the using declaration has 'typename'.
2056  void setTypeName(bool TN) { IsTypeName = TN; }
2057
2058  /// \brief Iterates through the using shadow declarations assosiated with
2059  /// this using declaration.
2060  class shadow_iterator {
2061    /// \brief The current using shadow declaration.
2062    UsingShadowDecl *Current;
2063
2064  public:
2065    typedef UsingShadowDecl*          value_type;
2066    typedef UsingShadowDecl*          reference;
2067    typedef UsingShadowDecl*          pointer;
2068    typedef std::forward_iterator_tag iterator_category;
2069    typedef std::ptrdiff_t            difference_type;
2070
2071    shadow_iterator() : Current(0) { }
2072    explicit shadow_iterator(UsingShadowDecl *C) : Current(C) { }
2073
2074    reference operator*() const { return Current; }
2075    pointer operator->() const { return Current; }
2076
2077    shadow_iterator& operator++() {
2078      Current = Current->getNextUsingShadowDecl();
2079      return *this;
2080    }
2081
2082    shadow_iterator operator++(int) {
2083      shadow_iterator tmp(*this);
2084      ++(*this);
2085      return tmp;
2086    }
2087
2088    friend bool operator==(shadow_iterator x, shadow_iterator y) {
2089      return x.Current == y.Current;
2090    }
2091    friend bool operator!=(shadow_iterator x, shadow_iterator y) {
2092      return x.Current != y.Current;
2093    }
2094  };
2095
2096  shadow_iterator shadow_begin() const {
2097    return shadow_iterator(FirstUsingShadow);
2098  }
2099  shadow_iterator shadow_end() const { return shadow_iterator(); }
2100
2101  /// \brief Return the number of shadowed declarations associated with this
2102  /// using declaration.
2103  unsigned shadow_size() const {
2104    return std::distance(shadow_begin(), shadow_end());
2105  }
2106
2107  void addShadowDecl(UsingShadowDecl *S);
2108  void removeShadowDecl(UsingShadowDecl *S);
2109
2110  static UsingDecl *Create(ASTContext &C, DeclContext *DC,
2111                           SourceLocation UsingL,
2112                           NestedNameSpecifierLoc QualifierLoc,
2113                           const DeclarationNameInfo &NameInfo,
2114                           bool IsTypeNameArg);
2115
2116  SourceRange getSourceRange() const {
2117    return SourceRange(UsingLocation, getNameInfo().getEndLoc());
2118  }
2119
2120  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2121  static bool classof(const UsingDecl *D) { return true; }
2122  static bool classofKind(Kind K) { return K == Using; }
2123
2124  friend class ASTDeclReader;
2125  friend class ASTDeclWriter;
2126};
2127
2128/// UnresolvedUsingValueDecl - Represents a dependent using
2129/// declaration which was not marked with 'typename'.  Unlike
2130/// non-dependent using declarations, these *only* bring through
2131/// non-types; otherwise they would break two-phase lookup.
2132///
2133/// template <class T> class A : public Base<T> {
2134///   using Base<T>::foo;
2135/// };
2136class UnresolvedUsingValueDecl : public ValueDecl {
2137  /// \brief The source location of the 'using' keyword
2138  SourceLocation UsingLocation;
2139
2140  /// \brief The nested-name-specifier that precedes the name.
2141  NestedNameSpecifierLoc QualifierLoc;
2142
2143  /// DNLoc - Provides source/type location info for the
2144  /// declaration name embedded in the ValueDecl base class.
2145  DeclarationNameLoc DNLoc;
2146
2147  UnresolvedUsingValueDecl(DeclContext *DC, QualType Ty,
2148                           SourceLocation UsingLoc,
2149                           NestedNameSpecifierLoc QualifierLoc,
2150                           const DeclarationNameInfo &NameInfo)
2151    : ValueDecl(UnresolvedUsingValue, DC,
2152                NameInfo.getLoc(), NameInfo.getName(), Ty),
2153      UsingLocation(UsingLoc), QualifierLoc(QualifierLoc),
2154      DNLoc(NameInfo.getInfo())
2155  { }
2156
2157public:
2158  /// \brief Returns the source location of the 'using' keyword.
2159  SourceLocation getUsingLoc() const { return UsingLocation; }
2160
2161  /// \brief Set the source location of the 'using' keyword.
2162  void setUsingLoc(SourceLocation L) { UsingLocation = L; }
2163
2164  /// \brief Retrieve the nested-name-specifier that qualifies the name,
2165  /// with source-location information.
2166  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2167
2168  /// \brief Retrieve the nested-name-specifier that qualifies the name.
2169  NestedNameSpecifier *getQualifier() const {
2170    return QualifierLoc.getNestedNameSpecifier();
2171  }
2172
2173  DeclarationNameInfo getNameInfo() const {
2174    return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc);
2175  }
2176
2177  static UnresolvedUsingValueDecl *
2178    Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc,
2179           NestedNameSpecifierLoc QualifierLoc,
2180           const DeclarationNameInfo &NameInfo);
2181
2182  SourceRange getSourceRange() const {
2183    return SourceRange(UsingLocation, getNameInfo().getEndLoc());
2184  }
2185
2186  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2187  static bool classof(const UnresolvedUsingValueDecl *D) { return true; }
2188  static bool classofKind(Kind K) { return K == UnresolvedUsingValue; }
2189
2190  friend class ASTDeclReader;
2191  friend class ASTDeclWriter;
2192};
2193
2194/// UnresolvedUsingTypenameDecl - Represents a dependent using
2195/// declaration which was marked with 'typename'.
2196///
2197/// template <class T> class A : public Base<T> {
2198///   using typename Base<T>::foo;
2199/// };
2200///
2201/// The type associated with a unresolved using typename decl is
2202/// currently always a typename type.
2203class UnresolvedUsingTypenameDecl : public TypeDecl {
2204  /// \brief The source location of the 'using' keyword
2205  SourceLocation UsingLocation;
2206
2207  /// \brief The source location of the 'typename' keyword
2208  SourceLocation TypenameLocation;
2209
2210  /// \brief The nested-name-specifier that precedes the name.
2211  NestedNameSpecifierLoc QualifierLoc;
2212
2213  UnresolvedUsingTypenameDecl(DeclContext *DC, SourceLocation UsingLoc,
2214                              SourceLocation TypenameLoc,
2215                              NestedNameSpecifierLoc QualifierLoc,
2216                              SourceLocation TargetNameLoc,
2217                              IdentifierInfo *TargetName)
2218  : TypeDecl(UnresolvedUsingTypename, DC, TargetNameLoc, TargetName),
2219    UsingLocation(UsingLoc), TypenameLocation(TypenameLoc),
2220    QualifierLoc(QualifierLoc) { }
2221
2222  friend class ASTDeclReader;
2223
2224public:
2225  /// \brief Returns the source location of the 'using' keyword.
2226  SourceLocation getUsingLoc() const { return UsingLocation; }
2227
2228  /// \brief Returns the source location of the 'typename' keyword.
2229  SourceLocation getTypenameLoc() const { return TypenameLocation; }
2230
2231  /// \brief Retrieve the nested-name-specifier that qualifies the name,
2232  /// with source-location information.
2233  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2234
2235  /// \brief Retrieve the nested-name-specifier that qualifies the name.
2236  NestedNameSpecifier *getQualifier() const {
2237    return QualifierLoc.getNestedNameSpecifier();
2238  }
2239
2240  static UnresolvedUsingTypenameDecl *
2241    Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc,
2242           SourceLocation TypenameLoc, NestedNameSpecifierLoc QualifierLoc,
2243           SourceLocation TargetNameLoc, DeclarationName TargetName);
2244
2245  SourceRange getSourceRange() const {
2246    return SourceRange(UsingLocation, getLocation());
2247  }
2248
2249  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2250  static bool classof(const UnresolvedUsingTypenameDecl *D) { return true; }
2251  static bool classofKind(Kind K) { return K == UnresolvedUsingTypename; }
2252};
2253
2254/// StaticAssertDecl - Represents a C++0x static_assert declaration.
2255class StaticAssertDecl : public Decl {
2256  Expr *AssertExpr;
2257  StringLiteral *Message;
2258
2259  StaticAssertDecl(DeclContext *DC, SourceLocation L,
2260                   Expr *assertexpr, StringLiteral *message)
2261  : Decl(StaticAssert, DC, L), AssertExpr(assertexpr), Message(message) { }
2262
2263public:
2264  static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC,
2265                                  SourceLocation L, Expr *AssertExpr,
2266                                  StringLiteral *Message);
2267
2268  Expr *getAssertExpr() { return AssertExpr; }
2269  const Expr *getAssertExpr() const { return AssertExpr; }
2270
2271  StringLiteral *getMessage() { return Message; }
2272  const StringLiteral *getMessage() const { return Message; }
2273
2274  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2275  static bool classof(StaticAssertDecl *D) { return true; }
2276  static bool classofKind(Kind K) { return K == StaticAssert; }
2277
2278  friend class ASTDeclReader;
2279};
2280
2281/// Insertion operator for diagnostics.  This allows sending AccessSpecifier's
2282/// into a diagnostic with <<.
2283const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
2284                                    AccessSpecifier AS);
2285
2286} // end namespace clang
2287
2288#endif
2289