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