DeclCXX.h revision 1827403a7138946305c0058f262e02b595cf882f
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
1118  /// getParent - Returns the parent of this method declaration, which
1119  /// is the class in which this method is defined.
1120  const CXXRecordDecl *getParent() const {
1121    return cast<CXXRecordDecl>(FunctionDecl::getParent());
1122  }
1123
1124  /// getParent - Returns the parent of this method declaration, which
1125  /// is the class in which this method is defined.
1126  CXXRecordDecl *getParent() {
1127    return const_cast<CXXRecordDecl *>(
1128             cast<CXXRecordDecl>(FunctionDecl::getParent()));
1129  }
1130
1131  /// getThisType - Returns the type of 'this' pointer.
1132  /// Should only be called for instance methods.
1133  QualType getThisType(ASTContext &C) const;
1134
1135  unsigned getTypeQualifiers() const {
1136    return getType()->getAs<FunctionProtoType>()->getTypeQuals();
1137  }
1138
1139  bool hasInlineBody() const;
1140
1141  // Implement isa/cast/dyncast/etc.
1142  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1143  static bool classof(const CXXMethodDecl *D) { return true; }
1144  static bool classofKind(Kind K) {
1145    return K >= firstCXXMethod && K <= lastCXXMethod;
1146  }
1147};
1148
1149/// CXXBaseOrMemberInitializer - Represents a C++ base or member
1150/// initializer, which is part of a constructor initializer that
1151/// initializes one non-static member variable or one base class. For
1152/// example, in the following, both 'A(a)' and 'f(3.14159)' are member
1153/// initializers:
1154///
1155/// @code
1156/// class A { };
1157/// class B : public A {
1158///   float f;
1159/// public:
1160///   B(A& a) : A(a), f(3.14159) { }
1161/// };
1162/// @endcode
1163class CXXBaseOrMemberInitializer {
1164  /// \brief Either the base class name (stored as a TypeSourceInfo*) or the
1165  /// field being initialized.
1166  llvm::PointerUnion<TypeSourceInfo *, FieldDecl *> BaseOrMember;
1167
1168  /// \brief The source location for the field name.
1169  SourceLocation MemberLocation;
1170
1171  /// \brief The argument used to initialize the base or member, which may
1172  /// end up constructing an object (when multiple arguments are involved).
1173  Stmt *Init;
1174
1175  /// \brief Stores either the constructor to call to initialize this base or
1176  /// member (a CXXConstructorDecl pointer), or stores the anonymous union of
1177  /// which the initialized value is a member.
1178  ///
1179  /// When the value is a FieldDecl pointer, 'BaseOrMember' is class's
1180  /// anonymous union data member, this field holds the FieldDecl for the
1181  /// member of the anonymous union being initialized.
1182  /// @code
1183  /// struct X {
1184  ///   X() : au_i1(123) {}
1185  ///   union {
1186  ///     int au_i1;
1187  ///     float au_f1;
1188  ///   };
1189  /// };
1190  /// @endcode
1191  /// In above example, BaseOrMember holds the field decl. for anonymous union
1192  /// and AnonUnionMember holds field decl for au_i1.
1193  FieldDecl *AnonUnionMember;
1194
1195  /// LParenLoc - Location of the left paren of the ctor-initializer.
1196  SourceLocation LParenLoc;
1197
1198  /// RParenLoc - Location of the right paren of the ctor-initializer.
1199  SourceLocation RParenLoc;
1200
1201  /// IsVirtual - If the initializer is a base initializer, this keeps track
1202  /// of whether the base is virtual or not.
1203  bool IsVirtual : 1;
1204
1205  /// IsWritten - Whether or not the initializer is explicitly written
1206  /// in the sources.
1207  bool IsWritten : 1;
1208  /// SourceOrderOrNumArrayIndices - If IsWritten is true, then this
1209  /// number keeps track of the textual order of this initializer in the
1210  /// original sources, counting from 0; otherwise, if IsWritten is false,
1211  /// it stores the number of array index variables stored after this
1212  /// object in memory.
1213  unsigned SourceOrderOrNumArrayIndices : 14;
1214
1215  CXXBaseOrMemberInitializer(ASTContext &Context,
1216                             FieldDecl *Member, SourceLocation MemberLoc,
1217                             SourceLocation L,
1218                             Expr *Init,
1219                             SourceLocation R,
1220                             VarDecl **Indices,
1221                             unsigned NumIndices);
1222
1223public:
1224  /// CXXBaseOrMemberInitializer - Creates a new base-class initializer.
1225  explicit
1226  CXXBaseOrMemberInitializer(ASTContext &Context,
1227                             TypeSourceInfo *TInfo, bool IsVirtual,
1228                             SourceLocation L,
1229                             Expr *Init,
1230                             SourceLocation R);
1231
1232  /// CXXBaseOrMemberInitializer - Creates a new member initializer.
1233  explicit
1234  CXXBaseOrMemberInitializer(ASTContext &Context,
1235                             FieldDecl *Member, SourceLocation MemberLoc,
1236                             SourceLocation L,
1237                             Expr *Init,
1238                             SourceLocation R);
1239
1240  /// \brief Creates a new member initializer that optionally contains
1241  /// array indices used to describe an elementwise initialization.
1242  static CXXBaseOrMemberInitializer *Create(ASTContext &Context,
1243                                            FieldDecl *Member,
1244                                            SourceLocation MemberLoc,
1245                                            SourceLocation L,
1246                                            Expr *Init,
1247                                            SourceLocation R,
1248                                            VarDecl **Indices,
1249                                            unsigned NumIndices);
1250
1251  /// \brief Destroy the base or member initializer.
1252  void Destroy(ASTContext &Context);
1253
1254  /// isBaseInitializer - Returns true when this initializer is
1255  /// initializing a base class.
1256  bool isBaseInitializer() const { return BaseOrMember.is<TypeSourceInfo*>(); }
1257
1258  /// isMemberInitializer - Returns true when this initializer is
1259  /// initializing a non-static data member.
1260  bool isMemberInitializer() const { return BaseOrMember.is<FieldDecl*>(); }
1261
1262  /// If this is a base class initializer, returns the type of the
1263  /// base class with location information. Otherwise, returns an NULL
1264  /// type location.
1265  TypeLoc getBaseClassLoc() const;
1266
1267  /// If this is a base class initializer, returns the type of the base class.
1268  /// Otherwise, returns NULL.
1269  const Type *getBaseClass() const;
1270  Type *getBaseClass();
1271
1272  /// Returns whether the base is virtual or not.
1273  bool isBaseVirtual() const {
1274    assert(isBaseInitializer() && "Must call this on base initializer!");
1275
1276    return IsVirtual;
1277  }
1278
1279  /// \brief Returns the declarator information for a base class initializer.
1280  TypeSourceInfo *getBaseClassInfo() const {
1281    return BaseOrMember.dyn_cast<TypeSourceInfo *>();
1282  }
1283
1284  /// getMember - If this is a member initializer, returns the
1285  /// declaration of the non-static data member being
1286  /// initialized. Otherwise, returns NULL.
1287  FieldDecl *getMember() {
1288    if (isMemberInitializer())
1289      return BaseOrMember.get<FieldDecl*>();
1290    else
1291      return 0;
1292  }
1293
1294  SourceLocation getMemberLocation() const {
1295    return MemberLocation;
1296  }
1297
1298  void setMember(FieldDecl *Member) {
1299    assert(isMemberInitializer());
1300    BaseOrMember = Member;
1301  }
1302
1303  /// \brief Determine the source location of the initializer.
1304  SourceLocation getSourceLocation() const;
1305
1306  /// \brief Determine the source range covering the entire initializer.
1307  SourceRange getSourceRange() const;
1308
1309  /// isWritten - Returns true if this initializer is explicitly written
1310  /// in the source code.
1311  bool isWritten() const { return IsWritten; }
1312
1313  /// \brief Return the source position of the initializer, counting from 0.
1314  /// If the initializer was implicit, -1 is returned.
1315  int getSourceOrder() const {
1316    return IsWritten ? static_cast<int>(SourceOrderOrNumArrayIndices) : -1;
1317  }
1318
1319  /// \brief Set the source order of this initializer. This method can only
1320  /// be called once for each initializer; it cannot be called on an
1321  /// initializer having a positive number of (implicit) array indices.
1322  void setSourceOrder(int pos) {
1323    assert(!IsWritten &&
1324           "calling twice setSourceOrder() on the same initializer");
1325    assert(SourceOrderOrNumArrayIndices == 0 &&
1326           "setSourceOrder() used when there are implicit array indices");
1327    assert(pos >= 0 &&
1328           "setSourceOrder() used to make an initializer implicit");
1329    IsWritten = true;
1330    SourceOrderOrNumArrayIndices = static_cast<unsigned>(pos);
1331  }
1332
1333  FieldDecl *getAnonUnionMember() const {
1334    return AnonUnionMember;
1335  }
1336  void setAnonUnionMember(FieldDecl *anonMember) {
1337    AnonUnionMember = anonMember;
1338  }
1339
1340
1341  SourceLocation getLParenLoc() const { return LParenLoc; }
1342  SourceLocation getRParenLoc() const { return RParenLoc; }
1343
1344  /// \brief Determine the number of implicit array indices used while
1345  /// described an array member initialization.
1346  unsigned getNumArrayIndices() const {
1347    return IsWritten ? 0 : SourceOrderOrNumArrayIndices;
1348  }
1349
1350  /// \brief Retrieve a particular array index variable used to
1351  /// describe an array member initialization.
1352  VarDecl *getArrayIndex(unsigned I) {
1353    assert(I < getNumArrayIndices() && "Out of bounds member array index");
1354    return reinterpret_cast<VarDecl **>(this + 1)[I];
1355  }
1356  const VarDecl *getArrayIndex(unsigned I) const {
1357    assert(I < getNumArrayIndices() && "Out of bounds member array index");
1358    return reinterpret_cast<const VarDecl * const *>(this + 1)[I];
1359  }
1360  void setArrayIndex(unsigned I, VarDecl *Index) {
1361    assert(I < getNumArrayIndices() && "Out of bounds member array index");
1362    reinterpret_cast<VarDecl **>(this + 1)[I] = Index;
1363  }
1364
1365  Expr *getInit() { return static_cast<Expr *>(Init); }
1366};
1367
1368/// CXXConstructorDecl - Represents a C++ constructor within a
1369/// class. For example:
1370///
1371/// @code
1372/// class X {
1373/// public:
1374///   explicit X(int); // represented by a CXXConstructorDecl.
1375/// };
1376/// @endcode
1377class CXXConstructorDecl : public CXXMethodDecl {
1378  /// IsExplicitSpecified - Whether this constructor declaration has the
1379  /// 'explicit' keyword specified.
1380  bool IsExplicitSpecified : 1;
1381
1382  /// ImplicitlyDefined - Whether this constructor was implicitly
1383  /// defined by the compiler. When false, the constructor was defined
1384  /// by the user. In C++03, this flag will have the same value as
1385  /// Implicit. In C++0x, however, a constructor that is
1386  /// explicitly defaulted (i.e., defined with " = default") will have
1387  /// @c !Implicit && ImplicitlyDefined.
1388  bool ImplicitlyDefined : 1;
1389
1390  /// Support for base and member initializers.
1391  /// BaseOrMemberInitializers - The arguments used to initialize the base
1392  /// or member.
1393  CXXBaseOrMemberInitializer **BaseOrMemberInitializers;
1394  unsigned NumBaseOrMemberInitializers;
1395
1396  CXXConstructorDecl(CXXRecordDecl *RD, SourceLocation L,
1397                     DeclarationName N, QualType T, TypeSourceInfo *TInfo,
1398                     bool isExplicitSpecified, bool isInline,
1399                     bool isImplicitlyDeclared)
1400    : CXXMethodDecl(CXXConstructor, RD, L, N, T, TInfo, false,
1401                    FunctionDecl::None, isInline),
1402      IsExplicitSpecified(isExplicitSpecified), ImplicitlyDefined(false),
1403      BaseOrMemberInitializers(0), NumBaseOrMemberInitializers(0) {
1404    setImplicit(isImplicitlyDeclared);
1405  }
1406  virtual void Destroy(ASTContext& C);
1407
1408public:
1409  static CXXConstructorDecl *Create(ASTContext &C, EmptyShell Empty);
1410  static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
1411                                    SourceLocation L, DeclarationName N,
1412                                    QualType T, TypeSourceInfo *TInfo,
1413                                    bool isExplicit,
1414                                    bool isInline, bool isImplicitlyDeclared);
1415
1416  /// isExplicitSpecified - Whether this constructor declaration has the
1417  /// 'explicit' keyword specified.
1418  bool isExplicitSpecified() const { return IsExplicitSpecified; }
1419
1420  /// isExplicit - Whether this constructor was marked "explicit" or not.
1421  bool isExplicit() const {
1422    return cast<CXXConstructorDecl>(getFirstDeclaration())
1423      ->isExplicitSpecified();
1424  }
1425
1426  /// isImplicitlyDefined - Whether this constructor was implicitly
1427  /// defined. If false, then this constructor was defined by the
1428  /// user. This operation can only be invoked if the constructor has
1429  /// already been defined.
1430  bool isImplicitlyDefined() const {
1431    assert(isThisDeclarationADefinition() &&
1432           "Can only get the implicit-definition flag once the "
1433           "constructor has been defined");
1434    return ImplicitlyDefined;
1435  }
1436
1437  /// setImplicitlyDefined - Set whether this constructor was
1438  /// implicitly defined or not.
1439  void setImplicitlyDefined(bool ID) {
1440    assert(isThisDeclarationADefinition() &&
1441           "Can only set the implicit-definition flag once the constructor "
1442           "has been defined");
1443    ImplicitlyDefined = ID;
1444  }
1445
1446  /// init_iterator - Iterates through the member/base initializer list.
1447  typedef CXXBaseOrMemberInitializer **init_iterator;
1448
1449  /// init_const_iterator - Iterates through the memberbase initializer list.
1450  typedef CXXBaseOrMemberInitializer * const * init_const_iterator;
1451
1452  /// init_begin() - Retrieve an iterator to the first initializer.
1453  init_iterator       init_begin()       { return BaseOrMemberInitializers; }
1454  /// begin() - Retrieve an iterator to the first initializer.
1455  init_const_iterator init_begin() const { return BaseOrMemberInitializers; }
1456
1457  /// init_end() - Retrieve an iterator past the last initializer.
1458  init_iterator       init_end()       {
1459    return BaseOrMemberInitializers + NumBaseOrMemberInitializers;
1460  }
1461  /// end() - Retrieve an iterator past the last initializer.
1462  init_const_iterator init_end() const {
1463    return BaseOrMemberInitializers + NumBaseOrMemberInitializers;
1464  }
1465
1466  /// getNumArgs - Determine the number of arguments used to
1467  /// initialize the member or base.
1468  unsigned getNumBaseOrMemberInitializers() const {
1469      return NumBaseOrMemberInitializers;
1470  }
1471
1472  void setNumBaseOrMemberInitializers(unsigned numBaseOrMemberInitializers) {
1473    NumBaseOrMemberInitializers = numBaseOrMemberInitializers;
1474  }
1475
1476  void setBaseOrMemberInitializers(CXXBaseOrMemberInitializer ** initializers) {
1477    BaseOrMemberInitializers = initializers;
1478  }
1479  /// isDefaultConstructor - Whether this constructor is a default
1480  /// constructor (C++ [class.ctor]p5), which can be used to
1481  /// default-initialize a class of this type.
1482  bool isDefaultConstructor() const;
1483
1484  /// isCopyConstructor - Whether this constructor is a copy
1485  /// constructor (C++ [class.copy]p2, which can be used to copy the
1486  /// class. @p TypeQuals will be set to the qualifiers on the
1487  /// argument type. For example, @p TypeQuals would be set to @c
1488  /// QualType::Const for the following copy constructor:
1489  ///
1490  /// @code
1491  /// class X {
1492  /// public:
1493  ///   X(const X&);
1494  /// };
1495  /// @endcode
1496  bool isCopyConstructor(unsigned &TypeQuals) const;
1497
1498  /// isCopyConstructor - Whether this constructor is a copy
1499  /// constructor (C++ [class.copy]p2, which can be used to copy the
1500  /// class.
1501  bool isCopyConstructor() const {
1502    unsigned TypeQuals = 0;
1503    return isCopyConstructor(TypeQuals);
1504  }
1505
1506  /// isConvertingConstructor - Whether this constructor is a
1507  /// converting constructor (C++ [class.conv.ctor]), which can be
1508  /// used for user-defined conversions.
1509  bool isConvertingConstructor(bool AllowExplicit) const;
1510
1511  /// \brief Determine whether this is a member template specialization that
1512  /// looks like a copy constructor. Such constructors are never used to copy
1513  /// an object.
1514  bool isCopyConstructorLikeSpecialization() const;
1515
1516  // Implement isa/cast/dyncast/etc.
1517  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1518  static bool classof(const CXXConstructorDecl *D) { return true; }
1519  static bool classofKind(Kind K) { return K == CXXConstructor; }
1520
1521  friend class PCHDeclReader;
1522  friend class PCHDeclWriter;
1523};
1524
1525/// CXXDestructorDecl - Represents a C++ destructor within a
1526/// class. For example:
1527///
1528/// @code
1529/// class X {
1530/// public:
1531///   ~X(); // represented by a CXXDestructorDecl.
1532/// };
1533/// @endcode
1534class CXXDestructorDecl : public CXXMethodDecl {
1535  /// ImplicitlyDefined - Whether this destructor was implicitly
1536  /// defined by the compiler. When false, the destructor was defined
1537  /// by the user. In C++03, this flag will have the same value as
1538  /// Implicit. In C++0x, however, a destructor that is
1539  /// explicitly defaulted (i.e., defined with " = default") will have
1540  /// @c !Implicit && ImplicitlyDefined.
1541  bool ImplicitlyDefined : 1;
1542
1543  FunctionDecl *OperatorDelete;
1544
1545  CXXDestructorDecl(CXXRecordDecl *RD, SourceLocation L,
1546                    DeclarationName N, QualType T,
1547                    bool isInline, bool isImplicitlyDeclared)
1548    : CXXMethodDecl(CXXDestructor, RD, L, N, T, /*TInfo=*/0, false,
1549                    FunctionDecl::None, isInline),
1550      ImplicitlyDefined(false), OperatorDelete(0) {
1551    setImplicit(isImplicitlyDeclared);
1552  }
1553
1554public:
1555  static CXXDestructorDecl *Create(ASTContext& C, EmptyShell Empty);
1556  static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
1557                                   SourceLocation L, DeclarationName N,
1558                                   QualType T, bool isInline,
1559                                   bool isImplicitlyDeclared);
1560
1561  /// isImplicitlyDefined - Whether this destructor was implicitly
1562  /// defined. If false, then this destructor was defined by the
1563  /// user. This operation can only be invoked if the destructor has
1564  /// already been defined.
1565  bool isImplicitlyDefined() const {
1566    assert(isThisDeclarationADefinition() &&
1567           "Can only get the implicit-definition flag once the destructor has been defined");
1568    return ImplicitlyDefined;
1569  }
1570
1571  /// setImplicitlyDefined - Set whether this destructor was
1572  /// implicitly defined or not.
1573  void setImplicitlyDefined(bool ID) {
1574    assert(isThisDeclarationADefinition() &&
1575           "Can only set the implicit-definition flag once the destructor has been defined");
1576    ImplicitlyDefined = ID;
1577  }
1578
1579  void setOperatorDelete(FunctionDecl *OD) { OperatorDelete = OD; }
1580  const FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
1581
1582  // Implement isa/cast/dyncast/etc.
1583  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1584  static bool classof(const CXXDestructorDecl *D) { return true; }
1585  static bool classofKind(Kind K) { return K == CXXDestructor; }
1586
1587  friend class PCHDeclReader;
1588  friend class PCHDeclWriter;
1589};
1590
1591/// CXXConversionDecl - Represents a C++ conversion function within a
1592/// class. For example:
1593///
1594/// @code
1595/// class X {
1596/// public:
1597///   operator bool();
1598/// };
1599/// @endcode
1600class CXXConversionDecl : public CXXMethodDecl {
1601  /// IsExplicitSpecified - Whether this conversion function declaration is
1602  /// marked "explicit", meaning that it can only be applied when the user
1603  /// explicitly wrote a cast. This is a C++0x feature.
1604  bool IsExplicitSpecified : 1;
1605
1606  CXXConversionDecl(CXXRecordDecl *RD, SourceLocation L,
1607                    DeclarationName N, QualType T, TypeSourceInfo *TInfo,
1608                    bool isInline, bool isExplicitSpecified)
1609    : CXXMethodDecl(CXXConversion, RD, L, N, T, TInfo, false,
1610                    FunctionDecl::None, isInline),
1611      IsExplicitSpecified(isExplicitSpecified) { }
1612
1613public:
1614  static CXXConversionDecl *Create(ASTContext &C, EmptyShell Empty);
1615  static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD,
1616                                   SourceLocation L, DeclarationName N,
1617                                   QualType T, TypeSourceInfo *TInfo,
1618                                   bool isInline, bool isExplicit);
1619
1620  /// IsExplicitSpecified - Whether this conversion function declaration is
1621  /// marked "explicit", meaning that it can only be applied when the user
1622  /// explicitly wrote a cast. This is a C++0x feature.
1623  bool isExplicitSpecified() const { return IsExplicitSpecified; }
1624
1625  /// isExplicit - Whether this is an explicit conversion operator
1626  /// (C++0x only). Explicit conversion operators are only considered
1627  /// when the user has explicitly written a cast.
1628  bool isExplicit() const {
1629    return cast<CXXConversionDecl>(getFirstDeclaration())
1630      ->isExplicitSpecified();
1631  }
1632
1633  /// getConversionType - Returns the type that this conversion
1634  /// function is converting to.
1635  QualType getConversionType() const {
1636    return getType()->getAs<FunctionType>()->getResultType();
1637  }
1638
1639  // Implement isa/cast/dyncast/etc.
1640  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1641  static bool classof(const CXXConversionDecl *D) { return true; }
1642  static bool classofKind(Kind K) { return K == CXXConversion; }
1643
1644  friend class PCHDeclReader;
1645  friend class PCHDeclWriter;
1646};
1647
1648/// LinkageSpecDecl - This represents a linkage specification.  For example:
1649///   extern "C" void foo();
1650///
1651class LinkageSpecDecl : public Decl, public DeclContext {
1652public:
1653  /// LanguageIDs - Used to represent the language in a linkage
1654  /// specification.  The values are part of the serialization abi for
1655  /// ASTs and cannot be changed without altering that abi.  To help
1656  /// ensure a stable abi for this, we choose the DW_LANG_ encodings
1657  /// from the dwarf standard.
1658  enum LanguageIDs {
1659    lang_c = /* DW_LANG_C */ 0x0002,
1660    lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004
1661  };
1662private:
1663  /// Language - The language for this linkage specification.
1664  LanguageIDs Language;
1665
1666  /// HadBraces - Whether this linkage specification had curly braces or not.
1667  bool HadBraces : 1;
1668
1669  LinkageSpecDecl(DeclContext *DC, SourceLocation L, LanguageIDs lang,
1670                  bool Braces)
1671    : Decl(LinkageSpec, DC, L),
1672      DeclContext(LinkageSpec), Language(lang), HadBraces(Braces) { }
1673
1674public:
1675  static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC,
1676                                 SourceLocation L, LanguageIDs Lang,
1677                                 bool Braces);
1678
1679  /// \brief Return the language specified by this linkage specification.
1680  LanguageIDs getLanguage() const { return Language; }
1681
1682  /// \brief Set the language specified by this linkage specification.
1683  void setLanguage(LanguageIDs L) { Language = L; }
1684
1685  /// \brief Determines whether this linkage specification had braces in
1686  /// its syntactic form.
1687  bool hasBraces() const { return HadBraces; }
1688
1689  /// \brief Set whether this linkage specification has braces in its
1690  /// syntactic form.
1691  void setHasBraces(bool B) { HadBraces = B; }
1692
1693  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1694  static bool classof(const LinkageSpecDecl *D) { return true; }
1695  static bool classofKind(Kind K) { return K == LinkageSpec; }
1696  static DeclContext *castToDeclContext(const LinkageSpecDecl *D) {
1697    return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D));
1698  }
1699  static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) {
1700    return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC));
1701  }
1702};
1703
1704/// UsingDirectiveDecl - Represents C++ using-directive. For example:
1705///
1706///    using namespace std;
1707///
1708// NB: UsingDirectiveDecl should be Decl not NamedDecl, but we provide
1709// artificial name, for all using-directives in order to store
1710// them in DeclContext effectively.
1711class UsingDirectiveDecl : public NamedDecl {
1712
1713  /// SourceLocation - Location of 'namespace' token.
1714  SourceLocation NamespaceLoc;
1715
1716  /// \brief The source range that covers the nested-name-specifier
1717  /// preceding the namespace name.
1718  SourceRange QualifierRange;
1719
1720  /// \brief The nested-name-specifier that precedes the namespace
1721  /// name, if any.
1722  NestedNameSpecifier *Qualifier;
1723
1724  /// IdentLoc - Location of nominated namespace-name identifier.
1725  // FIXME: We don't store location of scope specifier.
1726  SourceLocation IdentLoc;
1727
1728  /// NominatedNamespace - Namespace nominated by using-directive.
1729  NamedDecl *NominatedNamespace;
1730
1731  /// Enclosing context containing both using-directive and nominated
1732  /// namespace.
1733  DeclContext *CommonAncestor;
1734
1735  /// getUsingDirectiveName - Returns special DeclarationName used by
1736  /// using-directives. This is only used by DeclContext for storing
1737  /// UsingDirectiveDecls in its lookup structure.
1738  static DeclarationName getName() {
1739    return DeclarationName::getUsingDirectiveName();
1740  }
1741
1742  UsingDirectiveDecl(DeclContext *DC, SourceLocation L,
1743                     SourceLocation NamespcLoc,
1744                     SourceRange QualifierRange,
1745                     NestedNameSpecifier *Qualifier,
1746                     SourceLocation IdentLoc,
1747                     NamedDecl *Nominated,
1748                     DeclContext *CommonAncestor)
1749    : NamedDecl(UsingDirective, DC, L, getName()),
1750      NamespaceLoc(NamespcLoc), QualifierRange(QualifierRange),
1751      Qualifier(Qualifier), IdentLoc(IdentLoc),
1752      NominatedNamespace(Nominated),
1753      CommonAncestor(CommonAncestor) {
1754  }
1755
1756public:
1757  /// \brief Retrieve the source range of the nested-name-specifier
1758  /// that qualifies the namespace name.
1759  SourceRange getQualifierRange() const { return QualifierRange; }
1760
1761  /// \brief Set the source range of the nested-name-specifier that
1762  /// qualifies the namespace name.
1763  void setQualifierRange(SourceRange R) { QualifierRange = R; }
1764
1765  /// \brief Retrieve the nested-name-specifier that qualifies the
1766  /// name of the namespace.
1767  NestedNameSpecifier *getQualifier() const { return Qualifier; }
1768
1769  /// \brief Set the nested-name-specifier that qualifes the name of the
1770  /// namespace.
1771  void setQualifier(NestedNameSpecifier *NNS) { Qualifier = NNS; }
1772
1773  NamedDecl *getNominatedNamespaceAsWritten() { return NominatedNamespace; }
1774  const NamedDecl *getNominatedNamespaceAsWritten() const {
1775    return NominatedNamespace;
1776  }
1777
1778  /// getNominatedNamespace - Returns namespace nominated by using-directive.
1779  NamespaceDecl *getNominatedNamespace();
1780
1781  const NamespaceDecl *getNominatedNamespace() const {
1782    return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace();
1783  }
1784
1785  /// setNominatedNamespace - Set the namespace nominataed by the
1786  /// using-directive.
1787  void setNominatedNamespace(NamedDecl* NS);
1788
1789  /// \brief Returns the common ancestor context of this using-directive and
1790  /// its nominated namespace.
1791  DeclContext *getCommonAncestor() { return CommonAncestor; }
1792  const DeclContext *getCommonAncestor() const { return CommonAncestor; }
1793
1794  /// \brief Set the common ancestor context of this using-directive and its
1795  /// nominated namespace.
1796  void setCommonAncestor(DeclContext* Cxt) { CommonAncestor = Cxt; }
1797
1798  // FIXME: Could omit 'Key' in name.
1799  /// getNamespaceKeyLocation - Returns location of namespace keyword.
1800  SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; }
1801
1802  /// setNamespaceKeyLocation - Set the the location of the namespacekeyword.
1803  void setNamespaceKeyLocation(SourceLocation L) { NamespaceLoc = L; }
1804
1805  /// getIdentLocation - Returns location of identifier.
1806  SourceLocation getIdentLocation() const { return IdentLoc; }
1807
1808  /// setIdentLocation - set the location of the identifier.
1809  void setIdentLocation(SourceLocation L) { IdentLoc = L; }
1810
1811  static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC,
1812                                    SourceLocation L,
1813                                    SourceLocation NamespaceLoc,
1814                                    SourceRange QualifierRange,
1815                                    NestedNameSpecifier *Qualifier,
1816                                    SourceLocation IdentLoc,
1817                                    NamedDecl *Nominated,
1818                                    DeclContext *CommonAncestor);
1819
1820  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1821  static bool classof(const UsingDirectiveDecl *D) { return true; }
1822  static bool classofKind(Kind K) { return K == UsingDirective; }
1823
1824  // Friend for getUsingDirectiveName.
1825  friend class DeclContext;
1826};
1827
1828/// NamespaceAliasDecl - Represents a C++ namespace alias. For example:
1829///
1830/// @code
1831/// namespace Foo = Bar;
1832/// @endcode
1833class NamespaceAliasDecl : public NamedDecl {
1834  SourceLocation AliasLoc;
1835
1836  /// \brief The source range that covers the nested-name-specifier
1837  /// preceding the namespace name.
1838  SourceRange QualifierRange;
1839
1840  /// \brief The nested-name-specifier that precedes the namespace
1841  /// name, if any.
1842  NestedNameSpecifier *Qualifier;
1843
1844  /// IdentLoc - Location of namespace identifier. Accessed by TargetNameLoc.
1845  SourceLocation IdentLoc;
1846
1847  /// Namespace - The Decl that this alias points to. Can either be a
1848  /// NamespaceDecl or a NamespaceAliasDecl.
1849  NamedDecl *Namespace;
1850
1851  NamespaceAliasDecl(DeclContext *DC, SourceLocation L,
1852                     SourceLocation AliasLoc, IdentifierInfo *Alias,
1853                     SourceRange QualifierRange,
1854                     NestedNameSpecifier *Qualifier,
1855                     SourceLocation IdentLoc, NamedDecl *Namespace)
1856    : NamedDecl(NamespaceAlias, DC, L, Alias), AliasLoc(AliasLoc),
1857      QualifierRange(QualifierRange), Qualifier(Qualifier),
1858      IdentLoc(IdentLoc), Namespace(Namespace) { }
1859
1860public:
1861  /// \brief Retrieve the source range of the nested-name-specifier
1862  /// that qualifiers the namespace name.
1863  SourceRange getQualifierRange() const { return QualifierRange; }
1864
1865  /// \brief Set the source range of the nested-name-specifier that qualifies
1866  /// the namespace name.
1867  void setQualifierRange(SourceRange R) { QualifierRange = R; }
1868
1869  /// \brief Retrieve the nested-name-specifier that qualifies the
1870  /// name of the namespace.
1871  NestedNameSpecifier *getQualifier() const { return Qualifier; }
1872
1873  /// \brief Set the nested-name-specifier that qualifies the name of the
1874  /// namespace.
1875  void setQualifier(NestedNameSpecifier *NNS) { Qualifier = NNS; }
1876
1877  /// \brief Retrieve the namespace declaration aliased by this directive.
1878  NamespaceDecl *getNamespace() {
1879    if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(Namespace))
1880      return AD->getNamespace();
1881
1882    return cast<NamespaceDecl>(Namespace);
1883  }
1884
1885  const NamespaceDecl *getNamespace() const {
1886    return const_cast<NamespaceAliasDecl*>(this)->getNamespace();
1887  }
1888
1889  /// Returns the location of the alias name, i.e. 'foo' in
1890  /// "namespace foo = ns::bar;".
1891  SourceLocation getAliasLoc() const { return AliasLoc; }
1892
1893  /// Set the location o;f the alias name, e.e., 'foo' in
1894  /// "namespace foo = ns::bar;".
1895  void setAliasLoc(SourceLocation L) { AliasLoc = L; }
1896
1897  /// Returns the location of the 'namespace' keyword.
1898  SourceLocation getNamespaceLoc() const { return getLocation(); }
1899
1900  /// Returns the location of the identifier in the named namespace.
1901  SourceLocation getTargetNameLoc() const { return IdentLoc; }
1902
1903  /// Set the location of the identifier in the named namespace.
1904  void setTargetNameLoc(SourceLocation L) { IdentLoc = L; }
1905
1906  /// \brief Retrieve the namespace that this alias refers to, which
1907  /// may either be a NamespaceDecl or a NamespaceAliasDecl.
1908  NamedDecl *getAliasedNamespace() const { return Namespace; }
1909
1910  /// \brief Set the namespace or namespace alias pointed to by this
1911  /// alias decl.
1912  void setAliasedNamespace(NamedDecl *ND) {
1913    assert((isa<NamespaceAliasDecl>(ND) || isa<NamespaceDecl>(ND)) &&
1914      "expecting namespace or namespace alias decl");
1915      Namespace = ND;
1916  }
1917
1918  static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC,
1919                                    SourceLocation L, SourceLocation AliasLoc,
1920                                    IdentifierInfo *Alias,
1921                                    SourceRange QualifierRange,
1922                                    NestedNameSpecifier *Qualifier,
1923                                    SourceLocation IdentLoc,
1924                                    NamedDecl *Namespace);
1925
1926  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1927  static bool classof(const NamespaceAliasDecl *D) { return true; }
1928  static bool classofKind(Kind K) { return K == NamespaceAlias; }
1929};
1930
1931/// UsingShadowDecl - Represents a shadow declaration introduced into
1932/// a scope by a (resolved) using declaration.  For example,
1933///
1934/// namespace A {
1935///   void foo();
1936/// }
1937/// namespace B {
1938///   using A::foo(); // <- a UsingDecl
1939///                   // Also creates a UsingShadowDecl for A::foo in B
1940/// }
1941///
1942class UsingShadowDecl : public NamedDecl {
1943  /// The referenced declaration.
1944  NamedDecl *Underlying;
1945
1946  /// The using declaration which introduced this decl.
1947  UsingDecl *Using;
1948
1949  UsingShadowDecl(DeclContext *DC, SourceLocation Loc, UsingDecl *Using,
1950                  NamedDecl *Target)
1951    : NamedDecl(UsingShadow, DC, Loc, DeclarationName()),
1952      Underlying(Target), Using(Using) {
1953    if (Target) {
1954      setDeclName(Target->getDeclName());
1955      IdentifierNamespace = Target->getIdentifierNamespace();
1956    }
1957    setImplicit();
1958  }
1959
1960public:
1961  static UsingShadowDecl *Create(ASTContext &C, DeclContext *DC,
1962                                 SourceLocation Loc, UsingDecl *Using,
1963                                 NamedDecl *Target) {
1964    return new (C) UsingShadowDecl(DC, Loc, Using, Target);
1965  }
1966
1967  /// \brief Gets the underlying declaration which has been brought into the
1968  /// local scope.
1969  NamedDecl *getTargetDecl() const { return Underlying; }
1970
1971  /// \brief Sets the underlying declaration which has been brought into the
1972  /// local scope.
1973  void setTargetDecl(NamedDecl* ND) {
1974    assert(ND && "Target decl is null!");
1975    Underlying = ND;
1976    IdentifierNamespace = ND->getIdentifierNamespace();
1977  }
1978
1979  /// \brief Gets the using declaration to which this declaration is tied.
1980  UsingDecl *getUsingDecl() const { return Using; }
1981
1982  /// \brief Sets the using declaration that introduces this target
1983  /// declaration.
1984  void setUsingDecl(UsingDecl* UD) { Using = UD; }
1985
1986  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1987  static bool classof(const UsingShadowDecl *D) { return true; }
1988  static bool classofKind(Kind K) { return K == Decl::UsingShadow; }
1989};
1990
1991/// UsingDecl - Represents a C++ using-declaration. For example:
1992///    using someNameSpace::someIdentifier;
1993class UsingDecl : public NamedDecl {
1994  /// \brief The source range that covers the nested-name-specifier
1995  /// preceding the declaration name.
1996  SourceRange NestedNameRange;
1997
1998  /// \brief The source location of the "using" location itself.
1999  SourceLocation UsingLocation;
2000
2001  /// \brief Target nested name specifier.
2002  NestedNameSpecifier* TargetNestedName;
2003
2004  /// \brief The collection of shadow declarations associated with
2005  /// this using declaration.  This set can change as a class is
2006  /// processed.
2007  llvm::SmallPtrSet<UsingShadowDecl*, 8> Shadows;
2008
2009  // \brief Has 'typename' keyword.
2010  bool IsTypeName;
2011
2012  UsingDecl(DeclContext *DC, SourceLocation L, SourceRange NNR,
2013            SourceLocation UL, NestedNameSpecifier* TargetNNS,
2014            DeclarationName Name, bool IsTypeNameArg)
2015    : NamedDecl(Using, DC, L, Name),
2016      NestedNameRange(NNR), UsingLocation(UL), TargetNestedName(TargetNNS),
2017      IsTypeName(IsTypeNameArg) {
2018  }
2019
2020public:
2021  // FIXME: Should be const?
2022  /// \brief Returns the source range that covers the nested-name-specifier
2023  /// preceding the namespace name.
2024  SourceRange getNestedNameRange() { return NestedNameRange; }
2025
2026  /// \brief Set the source range of the nested-name-specifier.
2027  void setNestedNameRange(SourceRange R) { NestedNameRange = R; }
2028
2029  // FIXME; Should be const?
2030  // FIXME: Naming is inconsistent with other get*Loc functions.
2031  /// \brief Returns the source location of the "using" keyword.
2032  SourceLocation getUsingLocation() { return UsingLocation; }
2033
2034  /// \brief Set the source location of the 'using' keyword.
2035  void setUsingLocation(SourceLocation L) { UsingLocation = L; }
2036
2037
2038  /// \brief Get the target nested name declaration.
2039  NestedNameSpecifier* getTargetNestedNameDecl() {
2040    return TargetNestedName;
2041  }
2042
2043  /// \brief Set the target nested name declaration.
2044  void setTargetNestedNameDecl(NestedNameSpecifier *NNS) {
2045    TargetNestedName = NNS;
2046  }
2047
2048  /// \brief Return true if the using declaration has 'typename'.
2049  bool isTypeName() const { return IsTypeName; }
2050
2051  /// \brief Sets whether the using declaration has 'typename'.
2052  void setTypeName(bool TN) { IsTypeName = TN; }
2053
2054  typedef llvm::SmallPtrSet<UsingShadowDecl*,8>::const_iterator shadow_iterator;
2055  shadow_iterator shadow_begin() const { return Shadows.begin(); }
2056  shadow_iterator shadow_end() const { return Shadows.end(); }
2057
2058  void addShadowDecl(UsingShadowDecl *S) {
2059    assert(S->getUsingDecl() == this);
2060    if (!Shadows.insert(S)) {
2061      assert(false && "declaration already in set");
2062    }
2063  }
2064  void removeShadowDecl(UsingShadowDecl *S) {
2065    assert(S->getUsingDecl() == this);
2066    if (!Shadows.erase(S)) {
2067      assert(false && "declaration not in set");
2068    }
2069  }
2070
2071  /// \brief Return the number of shadowed declarations associated with this
2072  /// using declaration.
2073  unsigned getNumShadowDecls() const {
2074    return Shadows.size();
2075  }
2076
2077  static UsingDecl *Create(ASTContext &C, DeclContext *DC,
2078      SourceLocation IdentL, SourceRange NNR, SourceLocation UsingL,
2079      NestedNameSpecifier* TargetNNS, DeclarationName Name, bool IsTypeNameArg);
2080
2081  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2082  static bool classof(const UsingDecl *D) { return true; }
2083  static bool classofKind(Kind K) { return K == Using; }
2084};
2085
2086/// UnresolvedUsingValueDecl - Represents a dependent using
2087/// declaration which was not marked with 'typename'.  Unlike
2088/// non-dependent using declarations, these *only* bring through
2089/// non-types; otherwise they would break two-phase lookup.
2090///
2091/// template <class T> class A : public Base<T> {
2092///   using Base<T>::foo;
2093/// };
2094class UnresolvedUsingValueDecl : public ValueDecl {
2095  /// \brief The source range that covers the nested-name-specifier
2096  /// preceding the declaration name.
2097  SourceRange TargetNestedNameRange;
2098
2099  /// \brief The source location of the 'using' keyword
2100  SourceLocation UsingLocation;
2101
2102  NestedNameSpecifier *TargetNestedNameSpecifier;
2103
2104  UnresolvedUsingValueDecl(DeclContext *DC, QualType Ty,
2105                           SourceLocation UsingLoc, SourceRange TargetNNR,
2106                           NestedNameSpecifier *TargetNNS,
2107                           SourceLocation TargetNameLoc,
2108                           DeclarationName TargetName)
2109    : ValueDecl(UnresolvedUsingValue, DC, TargetNameLoc, TargetName, Ty),
2110    TargetNestedNameRange(TargetNNR), UsingLocation(UsingLoc),
2111    TargetNestedNameSpecifier(TargetNNS)
2112  { }
2113
2114public:
2115  /// \brief Returns the source range that covers the nested-name-specifier
2116  /// preceding the namespace name.
2117  SourceRange getTargetNestedNameRange() const { return TargetNestedNameRange; }
2118
2119  /// \brief Set the source range coverting the nested-name-specifier preceding
2120  /// the namespace name.
2121  void setTargetNestedNameRange(SourceRange R) { TargetNestedNameRange = R; }
2122
2123  /// \brief Get target nested name declaration.
2124  NestedNameSpecifier* getTargetNestedNameSpecifier() {
2125    return TargetNestedNameSpecifier;
2126  }
2127
2128  /// \brief Set the nested name declaration.
2129  void setTargetNestedNameSpecifier(NestedNameSpecifier* NNS) {
2130    TargetNestedNameSpecifier = NNS;
2131  }
2132
2133  /// \brief Returns the source location of the 'using' keyword.
2134  SourceLocation getUsingLoc() const { return UsingLocation; }
2135
2136  /// \brief Set the source location of the 'using' keyword.
2137  void setUsingLoc(SourceLocation L) { UsingLocation = L; }
2138
2139  static UnresolvedUsingValueDecl *
2140    Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc,
2141           SourceRange TargetNNR, NestedNameSpecifier *TargetNNS,
2142           SourceLocation TargetNameLoc, DeclarationName TargetName);
2143
2144  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2145  static bool classof(const UnresolvedUsingValueDecl *D) { return true; }
2146  static bool classofKind(Kind K) { return K == UnresolvedUsingValue; }
2147};
2148
2149/// UnresolvedUsingTypenameDecl - Represents a dependent using
2150/// declaration which was marked with 'typename'.
2151///
2152/// template <class T> class A : public Base<T> {
2153///   using typename Base<T>::foo;
2154/// };
2155///
2156/// The type associated with a unresolved using typename decl is
2157/// currently always a typename type.
2158class UnresolvedUsingTypenameDecl : public TypeDecl {
2159  /// \brief The source range that covers the nested-name-specifier
2160  /// preceding the declaration name.
2161  SourceRange TargetNestedNameRange;
2162
2163  /// \brief The source location of the 'using' keyword
2164  SourceLocation UsingLocation;
2165
2166  /// \brief The source location of the 'typename' keyword
2167  SourceLocation TypenameLocation;
2168
2169  NestedNameSpecifier *TargetNestedNameSpecifier;
2170
2171  UnresolvedUsingTypenameDecl(DeclContext *DC, SourceLocation UsingLoc,
2172                    SourceLocation TypenameLoc,
2173                    SourceRange TargetNNR, NestedNameSpecifier *TargetNNS,
2174                    SourceLocation TargetNameLoc, IdentifierInfo *TargetName)
2175  : TypeDecl(UnresolvedUsingTypename, DC, TargetNameLoc, TargetName),
2176    TargetNestedNameRange(TargetNNR), UsingLocation(UsingLoc),
2177    TypenameLocation(TypenameLoc), TargetNestedNameSpecifier(TargetNNS)
2178  { }
2179
2180public:
2181  /// \brief Returns the source range that covers the nested-name-specifier
2182  /// preceding the namespace name.
2183  SourceRange getTargetNestedNameRange() const { return TargetNestedNameRange; }
2184
2185  /// \brief Set the source range coverting the nested-name-specifier preceding
2186  /// the namespace name.
2187  void setTargetNestedNameRange(SourceRange R) { TargetNestedNameRange = R; }
2188
2189  /// \brief Get target nested name declaration.
2190  NestedNameSpecifier* getTargetNestedNameSpecifier() {
2191    return TargetNestedNameSpecifier;
2192  }
2193
2194  /// \brief Set the nested name declaration.
2195  void setTargetNestedNameSpecifier(NestedNameSpecifier* NNS) {
2196    TargetNestedNameSpecifier = NNS;
2197  }
2198
2199  /// \brief Returns the source location of the 'using' keyword.
2200  SourceLocation getUsingLoc() const { return UsingLocation; }
2201
2202  /// \brief Set the source location of the 'using' keyword.
2203  void setUsingLoc(SourceLocation L) { UsingLocation = L; }
2204
2205  /// \brief Returns the source location of the 'typename' keyword.
2206  SourceLocation getTypenameLoc() const { return TypenameLocation; }
2207
2208  /// \brief Set the source location of the 'typename' keyword.
2209  void setTypenameLoc(SourceLocation L) { TypenameLocation = L; }
2210
2211  static UnresolvedUsingTypenameDecl *
2212    Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc,
2213           SourceLocation TypenameLoc,
2214           SourceRange TargetNNR, NestedNameSpecifier *TargetNNS,
2215           SourceLocation TargetNameLoc, DeclarationName TargetName);
2216
2217  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2218  static bool classof(const UnresolvedUsingTypenameDecl *D) { return true; }
2219  static bool classofKind(Kind K) { return K == UnresolvedUsingTypename; }
2220};
2221
2222/// StaticAssertDecl - Represents a C++0x static_assert declaration.
2223class StaticAssertDecl : public Decl {
2224  Expr *AssertExpr;
2225  StringLiteral *Message;
2226
2227  StaticAssertDecl(DeclContext *DC, SourceLocation L,
2228                   Expr *assertexpr, StringLiteral *message)
2229  : Decl(StaticAssert, DC, L), AssertExpr(assertexpr), Message(message) { }
2230
2231public:
2232  static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC,
2233                                  SourceLocation L, Expr *AssertExpr,
2234                                  StringLiteral *Message);
2235
2236  Expr *getAssertExpr() { return AssertExpr; }
2237  const Expr *getAssertExpr() const { return AssertExpr; }
2238
2239  StringLiteral *getMessage() { return Message; }
2240  const StringLiteral *getMessage() const { return Message; }
2241
2242  virtual ~StaticAssertDecl();
2243  virtual void Destroy(ASTContext& C);
2244
2245  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2246  static bool classof(StaticAssertDecl *D) { return true; }
2247  static bool classofKind(Kind K) { return K == StaticAssert; }
2248};
2249
2250/// Insertion operator for diagnostics.  This allows sending AccessSpecifier's
2251/// into a diagnostic with <<.
2252const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
2253                                    AccessSpecifier AS);
2254
2255} // end namespace clang
2256
2257#endif
2258