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