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