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