DeclCXX.h revision 213d70b58b4f48050c3e545ce1bd4b0ec3af74be
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/ExprCXX.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/TypeLoc.h"
22#include "clang/AST/UnresolvedSet.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/PointerIntPair.h"
25#include "llvm/ADT/SmallPtrSet.h"
26
27namespace clang {
28
29class ClassTemplateDecl;
30class ClassTemplateSpecializationDecl;
31class CXXBasePath;
32class CXXBasePaths;
33class CXXConstructorDecl;
34class CXXConversionDecl;
35class CXXDestructorDecl;
36class CXXMethodDecl;
37class CXXRecordDecl;
38class CXXMemberLookupCriteria;
39class CXXFinalOverriderMap;
40class CXXIndirectPrimaryBaseSet;
41class FriendDecl;
42class LambdaExpr;
43
44/// \brief Represents any kind of function declaration, whether it is a
45/// concrete function or a function template.
46class AnyFunctionDecl {
47  NamedDecl *Function;
48
49  AnyFunctionDecl(NamedDecl *ND) : Function(ND) { }
50
51public:
52  AnyFunctionDecl(FunctionDecl *FD) : Function(FD) { }
53  AnyFunctionDecl(FunctionTemplateDecl *FTD);
54
55  /// \brief Implicily converts any function or function template into a
56  /// named declaration.
57  operator NamedDecl *() const { return Function; }
58
59  /// \brief Retrieve the underlying function or function template.
60  NamedDecl *get() const { return Function; }
61
62  static AnyFunctionDecl getFromNamedDecl(NamedDecl *ND) {
63    return AnyFunctionDecl(ND);
64  }
65};
66
67} // end namespace clang
68
69namespace llvm {
70  /// Implement simplify_type for AnyFunctionDecl, so that we can dyn_cast from
71  /// AnyFunctionDecl to any function or function template declaration.
72  template<> struct simplify_type<const ::clang::AnyFunctionDecl> {
73    typedef ::clang::NamedDecl* SimpleType;
74    static SimpleType getSimplifiedValue(const ::clang::AnyFunctionDecl &Val) {
75      return Val;
76    }
77  };
78  template<> struct simplify_type< ::clang::AnyFunctionDecl>
79  : public simplify_type<const ::clang::AnyFunctionDecl> {};
80
81  // Provide PointerLikeTypeTraits for non-cvr pointers.
82  template<>
83  class PointerLikeTypeTraits< ::clang::AnyFunctionDecl> {
84  public:
85    static inline void *getAsVoidPointer(::clang::AnyFunctionDecl F) {
86      return F.get();
87    }
88    static inline ::clang::AnyFunctionDecl getFromVoidPointer(void *P) {
89      return ::clang::AnyFunctionDecl::getFromNamedDecl(
90                                      static_cast< ::clang::NamedDecl*>(P));
91    }
92
93    enum { NumLowBitsAvailable = 2 };
94  };
95
96} // end namespace llvm
97
98namespace clang {
99
100/// AccessSpecDecl - An access specifier followed by colon ':'.
101///
102/// An objects of this class represents sugar for the syntactic occurrence
103/// of an access specifier followed by a colon in the list of member
104/// specifiers of a C++ class definition.
105///
106/// Note that they do not represent other uses of access specifiers,
107/// such as those occurring in a list of base specifiers.
108/// Also note that this class has nothing to do with so-called
109/// "access declarations" (C++98 11.3 [class.access.dcl]).
110class AccessSpecDecl : public Decl {
111  virtual void anchor();
112  /// ColonLoc - The location of the ':'.
113  SourceLocation ColonLoc;
114
115  AccessSpecDecl(AccessSpecifier AS, DeclContext *DC,
116                 SourceLocation ASLoc, SourceLocation ColonLoc)
117    : Decl(AccessSpec, DC, ASLoc), ColonLoc(ColonLoc) {
118    setAccess(AS);
119  }
120  AccessSpecDecl(EmptyShell Empty)
121    : Decl(AccessSpec, Empty) { }
122public:
123  /// getAccessSpecifierLoc - The location of the access specifier.
124  SourceLocation getAccessSpecifierLoc() const { return getLocation(); }
125  /// setAccessSpecifierLoc - Sets the location of the access specifier.
126  void setAccessSpecifierLoc(SourceLocation ASLoc) { setLocation(ASLoc); }
127
128  /// getColonLoc - The location of the colon following the access specifier.
129  SourceLocation getColonLoc() const { return ColonLoc; }
130  /// setColonLoc - Sets the location of the colon.
131  void setColonLoc(SourceLocation CLoc) { ColonLoc = CLoc; }
132
133  SourceRange getSourceRange() const {
134    return SourceRange(getAccessSpecifierLoc(), getColonLoc());
135  }
136
137  static AccessSpecDecl *Create(ASTContext &C, AccessSpecifier AS,
138                                DeclContext *DC, SourceLocation ASLoc,
139                                SourceLocation ColonLoc) {
140    return new (C) AccessSpecDecl(AS, DC, ASLoc, ColonLoc);
141  }
142  static AccessSpecDecl *CreateDeserialized(ASTContext &C, unsigned ID);
143
144  // Implement isa/cast/dyncast/etc.
145  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
146  static bool classof(const AccessSpecDecl *D) { return true; }
147  static bool classofKind(Kind K) { return K == AccessSpec; }
148};
149
150
151/// CXXBaseSpecifier - A base class of a C++ class.
152///
153/// Each CXXBaseSpecifier represents a single, direct base class (or
154/// struct) of a C++ class (or struct). It specifies the type of that
155/// base class, whether it is a virtual or non-virtual base, and what
156/// level of access (public, protected, private) is used for the
157/// derivation. For example:
158///
159/// @code
160///   class A { };
161///   class B { };
162///   class C : public virtual A, protected B { };
163/// @endcode
164///
165/// In this code, C will have two CXXBaseSpecifiers, one for "public
166/// virtual A" and the other for "protected B".
167class CXXBaseSpecifier {
168  /// Range - The source code range that covers the full base
169  /// specifier, including the "virtual" (if present) and access
170  /// specifier (if present).
171  SourceRange Range;
172
173  /// \brief The source location of the ellipsis, if this is a pack
174  /// expansion.
175  SourceLocation EllipsisLoc;
176
177  /// Virtual - Whether this is a virtual base class or not.
178  bool Virtual : 1;
179
180  /// BaseOfClass - Whether this is the base of a class (true) or of a
181  /// struct (false). This determines the mapping from the access
182  /// specifier as written in the source code to the access specifier
183  /// used for semantic analysis.
184  bool BaseOfClass : 1;
185
186  /// Access - Access specifier as written in the source code (which
187  /// may be AS_none). The actual type of data stored here is an
188  /// AccessSpecifier, but we use "unsigned" here to work around a
189  /// VC++ bug.
190  unsigned Access : 2;
191
192  /// InheritConstructors - Whether the class contains a using declaration
193  /// to inherit the named class's constructors.
194  bool InheritConstructors : 1;
195
196  /// BaseTypeInfo - The type of the base class. This will be a class or struct
197  /// (or a typedef of such). The source code range does not include the
198  /// "virtual" or access specifier.
199  TypeSourceInfo *BaseTypeInfo;
200
201public:
202  CXXBaseSpecifier() { }
203
204  CXXBaseSpecifier(SourceRange R, bool V, bool BC, AccessSpecifier A,
205                   TypeSourceInfo *TInfo, SourceLocation EllipsisLoc)
206    : Range(R), EllipsisLoc(EllipsisLoc), Virtual(V), BaseOfClass(BC),
207      Access(A), InheritConstructors(false), BaseTypeInfo(TInfo) { }
208
209  /// getSourceRange - Retrieves the source range that contains the
210  /// entire base specifier.
211  SourceRange getSourceRange() const { return Range; }
212
213  /// isVirtual - Determines whether the base class is a virtual base
214  /// class (or not).
215  bool isVirtual() const { return Virtual; }
216
217  /// \brief Determine whether this base class is a base of a class declared
218  /// with the 'class' keyword (vs. one declared with the 'struct' keyword).
219  bool isBaseOfClass() const { return BaseOfClass; }
220
221  /// \brief Determine whether this base specifier is a pack expansion.
222  bool isPackExpansion() const { return EllipsisLoc.isValid(); }
223
224  /// \brief Determine whether this base class's constructors get inherited.
225  bool getInheritConstructors() const { return InheritConstructors; }
226
227  /// \brief Set that this base class's constructors should be inherited.
228  void setInheritConstructors(bool Inherit = true) {
229    InheritConstructors = Inherit;
230  }
231
232  /// \brief For a pack expansion, determine the location of the ellipsis.
233  SourceLocation getEllipsisLoc() const {
234    return EllipsisLoc;
235  }
236
237  /// getAccessSpecifier - Returns the access specifier for this base
238  /// specifier. This is the actual base specifier as used for
239  /// semantic analysis, so the result can never be AS_none. To
240  /// retrieve the access specifier as written in the source code, use
241  /// getAccessSpecifierAsWritten().
242  AccessSpecifier getAccessSpecifier() const {
243    if ((AccessSpecifier)Access == AS_none)
244      return BaseOfClass? AS_private : AS_public;
245    else
246      return (AccessSpecifier)Access;
247  }
248
249  /// getAccessSpecifierAsWritten - Retrieves the access specifier as
250  /// written in the source code (which may mean that no access
251  /// specifier was explicitly written). Use getAccessSpecifier() to
252  /// retrieve the access specifier for use in semantic analysis.
253  AccessSpecifier getAccessSpecifierAsWritten() const {
254    return (AccessSpecifier)Access;
255  }
256
257  /// getType - Retrieves the type of the base class. This type will
258  /// always be an unqualified class type.
259  QualType getType() const { return BaseTypeInfo->getType(); }
260
261  /// getTypeLoc - Retrieves the type and source location of the base class.
262  TypeSourceInfo *getTypeSourceInfo() const { return BaseTypeInfo; }
263};
264
265/// CXXRecordDecl - Represents a C++ struct/union/class.
266/// FIXME: This class will disappear once we've properly taught RecordDecl
267/// to deal with C++-specific things.
268class CXXRecordDecl : public RecordDecl {
269
270  friend void TagDecl::startDefinition();
271
272  struct DefinitionData {
273    DefinitionData(CXXRecordDecl *D);
274
275    /// UserDeclaredConstructor - True when this class has a
276    /// user-declared constructor.
277    bool UserDeclaredConstructor : 1;
278
279    /// UserDeclaredCopyConstructor - True when this class has a
280    /// user-declared copy constructor.
281    bool UserDeclaredCopyConstructor : 1;
282
283    /// UserDeclareMoveConstructor - True when this class has a
284    /// user-declared move constructor.
285    bool UserDeclaredMoveConstructor : 1;
286
287    /// UserDeclaredCopyAssignment - True when this class has a
288    /// user-declared copy assignment operator.
289    bool UserDeclaredCopyAssignment : 1;
290
291    /// UserDeclareMoveAssignment - True when this class has a
292    /// user-declared move assignment.
293    bool UserDeclaredMoveAssignment : 1;
294
295    /// UserDeclaredDestructor - True when this class has a
296    /// user-declared destructor.
297    bool UserDeclaredDestructor : 1;
298
299    /// Aggregate - True when this class is an aggregate.
300    bool Aggregate : 1;
301
302    /// PlainOldData - True when this class is a POD-type.
303    bool PlainOldData : 1;
304
305    /// Empty - true when this class is empty for traits purposes,
306    /// i.e. has no data members other than 0-width bit-fields, has no
307    /// virtual function/base, and doesn't inherit from a non-empty
308    /// class. Doesn't take union-ness into account.
309    bool Empty : 1;
310
311    /// Polymorphic - True when this class is polymorphic, i.e. has at
312    /// least one virtual member or derives from a polymorphic class.
313    bool Polymorphic : 1;
314
315    /// Abstract - True when this class is abstract, i.e. has at least
316    /// one pure virtual function, (that can come from a base class).
317    bool Abstract : 1;
318
319    /// IsStandardLayout - True when this class has standard layout.
320    ///
321    /// C++0x [class]p7.  A standard-layout class is a class that:
322    /// * has no non-static data members of type non-standard-layout class (or
323    ///   array of such types) or reference,
324    /// * has no virtual functions (10.3) and no virtual base classes (10.1),
325    /// * has the same access control (Clause 11) for all non-static data
326    ///   members
327    /// * has no non-standard-layout base classes,
328    /// * either has no non-static data members in the most derived class and at
329    ///   most one base class with non-static data members, or has no base
330    ///   classes with non-static data members, and
331    /// * has no base classes of the same type as the first non-static data
332    ///   member.
333    bool IsStandardLayout : 1;
334
335    /// HasNoNonEmptyBases - True when there are no non-empty base classes.
336    ///
337    /// This is a helper bit of state used to implement IsStandardLayout more
338    /// efficiently.
339    bool HasNoNonEmptyBases : 1;
340
341    /// HasPrivateFields - True when there are private non-static data members.
342    bool HasPrivateFields : 1;
343
344    /// HasProtectedFields - True when there are protected non-static data
345    /// members.
346    bool HasProtectedFields : 1;
347
348    /// HasPublicFields - True when there are private non-static data members.
349    bool HasPublicFields : 1;
350
351    /// \brief True if this class (or any subobject) has mutable fields.
352    bool HasMutableFields : 1;
353
354    /// \brief True if there no non-field members declared by the user.
355    bool HasOnlyCMembers : 1;
356
357    /// HasTrivialDefaultConstructor - True when, if this class has a default
358    /// constructor, this default constructor is trivial.
359    ///
360    /// C++0x [class.ctor]p5
361    ///    A default constructor is trivial if it is not user-provided and if
362    ///     -- its class has no virtual functions and no virtual base classes,
363    ///        and
364    ///     -- no non-static data member of its class has a
365    ///        brace-or-equal-initializer, and
366    ///     -- all the direct base classes of its class have trivial
367    ///        default constructors, and
368    ///     -- for all the nonstatic data members of its class that are of class
369    ///        type (or array thereof), each such class has a trivial
370    ///        default constructor.
371    bool HasTrivialDefaultConstructor : 1;
372
373    /// HasConstexprNonCopyMoveConstructor - True when this class has at least
374    /// one user-declared constexpr constructor which is neither the copy nor
375    /// move constructor.
376    bool HasConstexprNonCopyMoveConstructor : 1;
377
378    /// DefaultedDefaultConstructorIsConstexpr - True if a defaulted default
379    /// constructor for this class would be constexpr.
380    bool DefaultedDefaultConstructorIsConstexpr : 1;
381
382    /// DefaultedCopyConstructorIsConstexpr - True if a defaulted copy
383    /// constructor for this class would be constexpr.
384    bool DefaultedCopyConstructorIsConstexpr : 1;
385
386    /// DefaultedMoveConstructorIsConstexpr - True if a defaulted move
387    /// constructor for this class would be constexpr.
388    bool DefaultedMoveConstructorIsConstexpr : 1;
389
390    /// HasConstexprDefaultConstructor - True if this class has a constexpr
391    /// default constructor (either user-declared or implicitly declared).
392    bool HasConstexprDefaultConstructor : 1;
393
394    /// HasConstexprCopyConstructor - True if this class has a constexpr copy
395    /// constructor (either user-declared or implicitly declared).
396    bool HasConstexprCopyConstructor : 1;
397
398    /// HasConstexprMoveConstructor - True if this class has a constexpr move
399    /// constructor (either user-declared or implicitly declared).
400    bool HasConstexprMoveConstructor : 1;
401
402    /// HasTrivialCopyConstructor - True when this class has a trivial copy
403    /// constructor.
404    ///
405    /// C++0x [class.copy]p13:
406    ///   A copy/move constructor for class X is trivial if it is neither
407    ///   user-provided and if
408    ///    -- class X has no virtual functions and no virtual base classes, and
409    ///    -- the constructor selected to copy/move each direct base class
410    ///       subobject is trivial, and
411    ///    -- for each non-static data member of X that is of class type (or an
412    ///       array thereof), the constructor selected to copy/move that member
413    ///       is trivial;
414    ///   otherwise the copy/move constructor is non-trivial.
415    bool HasTrivialCopyConstructor : 1;
416
417    /// HasTrivialMoveConstructor - True when this class has a trivial move
418    /// constructor.
419    ///
420    /// C++0x [class.copy]p13:
421    ///   A copy/move constructor for class X is trivial if it is neither
422    ///   user-provided and if
423    ///    -- class X has no virtual functions and no virtual base classes, and
424    ///    -- the constructor selected to copy/move each direct base class
425    ///       subobject is trivial, and
426    ///    -- for each non-static data member of X that is of class type (or an
427    ///       array thereof), the constructor selected to copy/move that member
428    ///       is trivial;
429    ///   otherwise the copy/move constructor is non-trivial.
430    bool HasTrivialMoveConstructor : 1;
431
432    /// HasTrivialCopyAssignment - True when this class has a trivial copy
433    /// assignment operator.
434    ///
435    /// C++0x [class.copy]p27:
436    ///   A copy/move assignment operator for class X is trivial if it is
437    ///   neither user-provided nor deleted and if
438    ///    -- class X has no virtual functions and no virtual base classes, and
439    ///    -- the assignment operator selected to copy/move each direct base
440    ///       class subobject is trivial, and
441    ///    -- for each non-static data member of X that is of class type (or an
442    ///       array thereof), the assignment operator selected to copy/move
443    ///       that member is trivial;
444    ///   otherwise the copy/move assignment operator is non-trivial.
445    bool HasTrivialCopyAssignment : 1;
446
447    /// HasTrivialMoveAssignment - True when this class has a trivial move
448    /// assignment operator.
449    ///
450    /// C++0x [class.copy]p27:
451    ///   A copy/move assignment operator for class X is trivial if it is
452    ///   neither user-provided nor deleted and if
453    ///    -- class X has no virtual functions and no virtual base classes, and
454    ///    -- the assignment operator selected to copy/move each direct base
455    ///       class subobject is trivial, and
456    ///    -- for each non-static data member of X that is of class type (or an
457    ///       array thereof), the assignment operator selected to copy/move
458    ///       that member is trivial;
459    ///   otherwise the copy/move assignment operator is non-trivial.
460    bool HasTrivialMoveAssignment : 1;
461
462    /// HasTrivialDestructor - True when this class has a trivial destructor.
463    ///
464    /// C++ [class.dtor]p3.  A destructor is trivial if it is an
465    /// implicitly-declared destructor and if:
466    /// * all of the direct base classes of its class have trivial destructors
467    ///   and
468    /// * for all of the non-static data members of its class that are of class
469    ///   type (or array thereof), each such class has a trivial destructor.
470    bool HasTrivialDestructor : 1;
471
472    /// HasNonLiteralTypeFieldsOrBases - True when this class contains at least
473    /// one non-static data member or base class of non-literal or volatile
474    /// type.
475    bool HasNonLiteralTypeFieldsOrBases : 1;
476
477    /// ComputedVisibleConversions - True when visible conversion functions are
478    /// already computed and are available.
479    bool ComputedVisibleConversions : 1;
480
481    /// \brief Whether we have a C++0x user-provided default constructor (not
482    /// explicitly deleted or defaulted).
483    bool UserProvidedDefaultConstructor : 1;
484
485    /// \brief Whether we have already declared the default constructor.
486    bool DeclaredDefaultConstructor : 1;
487
488    /// \brief Whether we have already declared the copy constructor.
489    bool DeclaredCopyConstructor : 1;
490
491    /// \brief Whether we have already declared the move constructor.
492    bool DeclaredMoveConstructor : 1;
493
494    /// \brief Whether we have already declared the copy-assignment operator.
495    bool DeclaredCopyAssignment : 1;
496
497    /// \brief Whether we have already declared the move-assignment operator.
498    bool DeclaredMoveAssignment : 1;
499
500    /// \brief Whether we have already declared a destructor within the class.
501    bool DeclaredDestructor : 1;
502
503    /// \brief Whether an implicit move constructor was attempted to be declared
504    /// but would have been deleted.
505    bool FailedImplicitMoveConstructor : 1;
506
507    /// \brief Whether an implicit move assignment operator was attempted to be
508    /// declared but would have been deleted.
509    bool FailedImplicitMoveAssignment : 1;
510
511    /// \brief Whether this class describes a C++ lambda.
512    bool IsLambda : 1;
513
514    /// NumBases - The number of base class specifiers in Bases.
515    unsigned NumBases;
516
517    /// NumVBases - The number of virtual base class specifiers in VBases.
518    unsigned NumVBases;
519
520    /// Bases - Base classes of this class.
521    /// FIXME: This is wasted space for a union.
522    LazyCXXBaseSpecifiersPtr Bases;
523
524    /// VBases - direct and indirect virtual base classes of this class.
525    LazyCXXBaseSpecifiersPtr VBases;
526
527    /// Conversions - Overload set containing the conversion functions
528    /// of this C++ class (but not its inherited conversion
529    /// functions). Each of the entries in this overload set is a
530    /// CXXConversionDecl.
531    UnresolvedSet<4> Conversions;
532
533    /// VisibleConversions - Overload set containing the conversion
534    /// functions of this C++ class and all those inherited conversion
535    /// functions that are visible in this class. Each of the entries
536    /// in this overload set is a CXXConversionDecl or a
537    /// FunctionTemplateDecl.
538    UnresolvedSet<4> VisibleConversions;
539
540    /// Definition - The declaration which defines this record.
541    CXXRecordDecl *Definition;
542
543    /// FirstFriend - The first friend declaration in this class, or
544    /// null if there aren't any.  This is actually currently stored
545    /// in reverse order.
546    FriendDecl *FirstFriend;
547
548    /// \brief Retrieve the set of direct base classes.
549    CXXBaseSpecifier *getBases() const {
550      return Bases.get(Definition->getASTContext().getExternalSource());
551    }
552
553    /// \brief Retrieve the set of virtual base classes.
554    CXXBaseSpecifier *getVBases() const {
555      return VBases.get(Definition->getASTContext().getExternalSource());
556    }
557  } *DefinitionData;
558
559  /// \brief Describes a C++ closure type (generated by a lambda expression).
560  struct LambdaDefinitionData : public DefinitionData {
561    typedef LambdaExpr::Capture Capture;
562
563    LambdaDefinitionData(CXXRecordDecl *D)
564      : DefinitionData(D), NumCaptures(0), NumExplicitCaptures(0), Captures(0) {
565      IsLambda = true;
566    }
567
568    /// \brief The number of captures in this lambda.
569    unsigned NumCaptures : 16;
570
571    /// \brief The number of explicit captures in this lambda.
572    unsigned NumExplicitCaptures : 15;
573
574    /// \brief The "extra" data associated with the lambda, including
575    /// captures, capture initializers, the body of the lambda, and the
576    /// array-index variables for array captures.
577    Capture *Captures;
578  };
579
580  struct DefinitionData &data() {
581    assert(DefinitionData && "queried property of class with no definition");
582    return *DefinitionData;
583  }
584
585  const struct DefinitionData &data() const {
586    assert(DefinitionData && "queried property of class with no definition");
587    return *DefinitionData;
588  }
589
590  struct LambdaDefinitionData &getLambdaData() const {
591    assert(DefinitionData && "queried property of lambda with no definition");
592    assert(DefinitionData->IsLambda &&
593           "queried lambda property of non-lambda class");
594    return static_cast<LambdaDefinitionData &>(*DefinitionData);
595  }
596
597  /// \brief The template or declaration that this declaration
598  /// describes or was instantiated from, respectively.
599  ///
600  /// For non-templates, this value will be NULL. For record
601  /// declarations that describe a class template, this will be a
602  /// pointer to a ClassTemplateDecl. For member
603  /// classes of class template specializations, this will be the
604  /// MemberSpecializationInfo referring to the member class that was
605  /// instantiated or specialized.
606  llvm::PointerUnion<ClassTemplateDecl*, MemberSpecializationInfo*>
607    TemplateOrInstantiation;
608
609  friend class DeclContext;
610  friend class LambdaExpr;
611
612  /// \brief Notify the class that member has been added.
613  ///
614  /// This routine helps maintain information about the class based on which
615  /// members have been added. It will be invoked by DeclContext::addDecl()
616  /// whenever a member is added to this record.
617  void addedMember(Decl *D);
618
619  void markedVirtualFunctionPure();
620  friend void FunctionDecl::setPure(bool);
621
622  friend class ASTNodeImporter;
623
624protected:
625  CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
626                SourceLocation StartLoc, SourceLocation IdLoc,
627                IdentifierInfo *Id, CXXRecordDecl *PrevDecl);
628
629public:
630  /// base_class_iterator - Iterator that traverses the base classes
631  /// of a class.
632  typedef CXXBaseSpecifier*       base_class_iterator;
633
634  /// base_class_const_iterator - Iterator that traverses the base
635  /// classes of a class.
636  typedef const CXXBaseSpecifier* base_class_const_iterator;
637
638  /// reverse_base_class_iterator = Iterator that traverses the base classes
639  /// of a class in reverse order.
640  typedef std::reverse_iterator<base_class_iterator>
641    reverse_base_class_iterator;
642
643  /// reverse_base_class_iterator = Iterator that traverses the base classes
644  /// of a class in reverse order.
645  typedef std::reverse_iterator<base_class_const_iterator>
646    reverse_base_class_const_iterator;
647
648  virtual CXXRecordDecl *getCanonicalDecl() {
649    return cast<CXXRecordDecl>(RecordDecl::getCanonicalDecl());
650  }
651  virtual const CXXRecordDecl *getCanonicalDecl() const {
652    return cast<CXXRecordDecl>(RecordDecl::getCanonicalDecl());
653  }
654
655  const CXXRecordDecl *getPreviousDecl() const {
656    return cast_or_null<CXXRecordDecl>(RecordDecl::getPreviousDecl());
657  }
658  CXXRecordDecl *getPreviousDecl() {
659    return cast_or_null<CXXRecordDecl>(RecordDecl::getPreviousDecl());
660  }
661
662  const CXXRecordDecl *getMostRecentDecl() const {
663    return cast_or_null<CXXRecordDecl>(RecordDecl::getMostRecentDecl());
664  }
665  CXXRecordDecl *getMostRecentDecl() {
666    return cast_or_null<CXXRecordDecl>(RecordDecl::getMostRecentDecl());
667  }
668
669  CXXRecordDecl *getDefinition() const {
670    if (!DefinitionData) return 0;
671    return data().Definition;
672  }
673
674  bool hasDefinition() const { return DefinitionData != 0; }
675
676  static CXXRecordDecl *Create(const ASTContext &C, TagKind TK, DeclContext *DC,
677                               SourceLocation StartLoc, SourceLocation IdLoc,
678                               IdentifierInfo *Id, CXXRecordDecl* PrevDecl=0,
679                               bool DelayTypeCreation = false);
680  static CXXRecordDecl *CreateLambda(const ASTContext &C, DeclContext *DC,
681                                     SourceLocation Loc);
682  static CXXRecordDecl *CreateDeserialized(const ASTContext &C, unsigned ID);
683
684  bool isDynamicClass() const {
685    return data().Polymorphic || data().NumVBases != 0;
686  }
687
688  /// setBases - Sets the base classes of this struct or class.
689  void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases);
690
691  /// getNumBases - Retrieves the number of base classes of this
692  /// class.
693  unsigned getNumBases() const { return data().NumBases; }
694
695  base_class_iterator bases_begin() { return data().getBases(); }
696  base_class_const_iterator bases_begin() const { return data().getBases(); }
697  base_class_iterator bases_end() { return bases_begin() + data().NumBases; }
698  base_class_const_iterator bases_end() const {
699    return bases_begin() + data().NumBases;
700  }
701  reverse_base_class_iterator       bases_rbegin() {
702    return reverse_base_class_iterator(bases_end());
703  }
704  reverse_base_class_const_iterator bases_rbegin() const {
705    return reverse_base_class_const_iterator(bases_end());
706  }
707  reverse_base_class_iterator bases_rend() {
708    return reverse_base_class_iterator(bases_begin());
709  }
710  reverse_base_class_const_iterator bases_rend() const {
711    return reverse_base_class_const_iterator(bases_begin());
712  }
713
714  /// getNumVBases - Retrieves the number of virtual base classes of this
715  /// class.
716  unsigned getNumVBases() const { return data().NumVBases; }
717
718  base_class_iterator vbases_begin() { return data().getVBases(); }
719  base_class_const_iterator vbases_begin() const { return data().getVBases(); }
720  base_class_iterator vbases_end() { return vbases_begin() + data().NumVBases; }
721  base_class_const_iterator vbases_end() const {
722    return vbases_begin() + data().NumVBases;
723  }
724  reverse_base_class_iterator vbases_rbegin() {
725    return reverse_base_class_iterator(vbases_end());
726  }
727  reverse_base_class_const_iterator vbases_rbegin() const {
728    return reverse_base_class_const_iterator(vbases_end());
729  }
730  reverse_base_class_iterator vbases_rend() {
731    return reverse_base_class_iterator(vbases_begin());
732  }
733  reverse_base_class_const_iterator vbases_rend() const {
734    return reverse_base_class_const_iterator(vbases_begin());
735 }
736
737  /// \brief Determine whether this class has any dependent base classes.
738  bool hasAnyDependentBases() const;
739
740  /// Iterator access to method members.  The method iterator visits
741  /// all method members of the class, including non-instance methods,
742  /// special methods, etc.
743  typedef specific_decl_iterator<CXXMethodDecl> method_iterator;
744
745  /// method_begin - Method begin iterator.  Iterates in the order the methods
746  /// were declared.
747  method_iterator method_begin() const {
748    return method_iterator(decls_begin());
749  }
750  /// method_end - Method end iterator.
751  method_iterator method_end() const {
752    return method_iterator(decls_end());
753  }
754
755  /// Iterator access to constructor members.
756  typedef specific_decl_iterator<CXXConstructorDecl> ctor_iterator;
757
758  ctor_iterator ctor_begin() const {
759    return ctor_iterator(decls_begin());
760  }
761  ctor_iterator ctor_end() const {
762    return ctor_iterator(decls_end());
763  }
764
765  /// An iterator over friend declarations.  All of these are defined
766  /// in DeclFriend.h.
767  class friend_iterator;
768  friend_iterator friend_begin() const;
769  friend_iterator friend_end() const;
770  void pushFriendDecl(FriendDecl *FD);
771
772  /// Determines whether this record has any friends.
773  bool hasFriends() const {
774    return data().FirstFriend != 0;
775  }
776
777  /// \brief Determine if we need to declare a default constructor for
778  /// this class.
779  ///
780  /// This value is used for lazy creation of default constructors.
781  bool needsImplicitDefaultConstructor() const {
782    return !data().UserDeclaredConstructor &&
783           !data().DeclaredDefaultConstructor;
784  }
785
786  /// hasDeclaredDefaultConstructor - Whether this class's default constructor
787  /// has been declared (either explicitly or implicitly).
788  bool hasDeclaredDefaultConstructor() const {
789    return data().DeclaredDefaultConstructor;
790  }
791
792  /// hasConstCopyConstructor - Determines whether this class has a
793  /// copy constructor that accepts a const-qualified argument.
794  bool hasConstCopyConstructor() const;
795
796  /// getCopyConstructor - Returns the copy constructor for this class
797  CXXConstructorDecl *getCopyConstructor(unsigned TypeQuals) const;
798
799  /// getMoveConstructor - Returns the move constructor for this class
800  CXXConstructorDecl *getMoveConstructor() const;
801
802  /// \brief Retrieve the copy-assignment operator for this class, if available.
803  ///
804  /// This routine attempts to find the copy-assignment operator for this
805  /// class, using a simplistic form of overload resolution.
806  ///
807  /// \param ArgIsConst Whether the argument to the copy-assignment operator
808  /// is const-qualified.
809  ///
810  /// \returns The copy-assignment operator that can be invoked, or NULL if
811  /// a unique copy-assignment operator could not be found.
812  CXXMethodDecl *getCopyAssignmentOperator(bool ArgIsConst) const;
813
814  /// getMoveAssignmentOperator - Returns the move assignment operator for this
815  /// class
816  CXXMethodDecl *getMoveAssignmentOperator() const;
817
818  /// hasUserDeclaredConstructor - Whether this class has any
819  /// user-declared constructors. When true, a default constructor
820  /// will not be implicitly declared.
821  bool hasUserDeclaredConstructor() const {
822    return data().UserDeclaredConstructor;
823  }
824
825  /// hasUserProvidedDefaultconstructor - Whether this class has a
826  /// user-provided default constructor per C++0x.
827  bool hasUserProvidedDefaultConstructor() const {
828    return data().UserProvidedDefaultConstructor;
829  }
830
831  /// hasUserDeclaredCopyConstructor - Whether this class has a
832  /// user-declared copy constructor. When false, a copy constructor
833  /// will be implicitly declared.
834  bool hasUserDeclaredCopyConstructor() const {
835    return data().UserDeclaredCopyConstructor;
836  }
837
838  /// \brief Determine whether this class has had its copy constructor
839  /// declared, either via the user or via an implicit declaration.
840  ///
841  /// This value is used for lazy creation of copy constructors.
842  bool hasDeclaredCopyConstructor() const {
843    return data().DeclaredCopyConstructor;
844  }
845
846  /// hasUserDeclaredMoveOperation - Whether this class has a user-
847  /// declared move constructor or assignment operator. When false, a
848  /// move constructor and assignment operator may be implicitly declared.
849  bool hasUserDeclaredMoveOperation() const {
850    return data().UserDeclaredMoveConstructor ||
851           data().UserDeclaredMoveAssignment;
852  }
853
854  /// \brief Determine whether this class has had a move constructor
855  /// declared by the user.
856  bool hasUserDeclaredMoveConstructor() const {
857    return data().UserDeclaredMoveConstructor;
858  }
859
860  /// \brief Determine whether this class has had a move constructor
861  /// declared.
862  bool hasDeclaredMoveConstructor() const {
863    return data().DeclaredMoveConstructor;
864  }
865
866  /// \brief Determine whether implicit move constructor generation for this
867  /// class has failed before.
868  bool hasFailedImplicitMoveConstructor() const {
869    return data().FailedImplicitMoveConstructor;
870  }
871
872  /// \brief Set whether implicit move constructor generation for this class
873  /// has failed before.
874  void setFailedImplicitMoveConstructor(bool Failed = true) {
875    data().FailedImplicitMoveConstructor = Failed;
876  }
877
878  /// \brief Determine whether this class should get an implicit move
879  /// constructor or if any existing special member function inhibits this.
880  ///
881  /// Covers all bullets of C++0x [class.copy]p9 except the last, that the
882  /// constructor wouldn't be deleted, which is only looked up from a cached
883  /// result.
884  bool needsImplicitMoveConstructor() const {
885    return !hasFailedImplicitMoveConstructor() &&
886           !hasDeclaredMoveConstructor() &&
887           !hasUserDeclaredCopyConstructor() &&
888           !hasUserDeclaredCopyAssignment() &&
889           !hasUserDeclaredMoveAssignment() &&
890           !hasUserDeclaredDestructor();
891  }
892
893  /// hasUserDeclaredCopyAssignment - Whether this class has a
894  /// user-declared copy assignment operator. When false, a copy
895  /// assigment operator will be implicitly declared.
896  bool hasUserDeclaredCopyAssignment() const {
897    return data().UserDeclaredCopyAssignment;
898  }
899
900  /// \brief Determine whether this class has had its copy assignment operator
901  /// declared, either via the user or via an implicit declaration.
902  ///
903  /// This value is used for lazy creation of copy assignment operators.
904  bool hasDeclaredCopyAssignment() const {
905    return data().DeclaredCopyAssignment;
906  }
907
908  /// \brief Determine whether this class has had a move assignment
909  /// declared by the user.
910  bool hasUserDeclaredMoveAssignment() const {
911    return data().UserDeclaredMoveAssignment;
912  }
913
914  /// hasDeclaredMoveAssignment - Whether this class has a
915  /// declared move assignment operator.
916  bool hasDeclaredMoveAssignment() const {
917    return data().DeclaredMoveAssignment;
918  }
919
920  /// \brief Determine whether implicit move assignment generation for this
921  /// class has failed before.
922  bool hasFailedImplicitMoveAssignment() const {
923    return data().FailedImplicitMoveAssignment;
924  }
925
926  /// \brief Set whether implicit move assignment generation for this class
927  /// has failed before.
928  void setFailedImplicitMoveAssignment(bool Failed = true) {
929    data().FailedImplicitMoveAssignment = Failed;
930  }
931
932  /// \brief Determine whether this class should get an implicit move
933  /// assignment operator or if any existing special member function inhibits
934  /// this.
935  ///
936  /// Covers all bullets of C++0x [class.copy]p20 except the last, that the
937  /// constructor wouldn't be deleted.
938  bool needsImplicitMoveAssignment() const {
939    return !hasFailedImplicitMoveAssignment() &&
940           !hasDeclaredMoveAssignment() &&
941           !hasUserDeclaredCopyConstructor() &&
942           !hasUserDeclaredCopyAssignment() &&
943           !hasUserDeclaredMoveConstructor() &&
944           !hasUserDeclaredDestructor();
945  }
946
947  /// hasUserDeclaredDestructor - Whether this class has a
948  /// user-declared destructor. When false, a destructor will be
949  /// implicitly declared.
950  bool hasUserDeclaredDestructor() const {
951    return data().UserDeclaredDestructor;
952  }
953
954  /// \brief Determine whether this class has had its destructor declared,
955  /// either via the user or via an implicit declaration.
956  ///
957  /// This value is used for lazy creation of destructors.
958  bool hasDeclaredDestructor() const { return data().DeclaredDestructor; }
959
960  /// \brief Determine whether this class describes a lambda function object.
961  bool isLambda() const { return hasDefinition() && data().IsLambda; }
962
963  /// \brief For a closure type, retrieve the mapping from captured
964  /// variables and this to the non-static data members that store the
965  /// values or references of the captures.
966  ///
967  /// \param Captures Will be populated with the mapping from captured
968  /// variables to the corresponding fields.
969  ///
970  /// \param ThisCapture Will be set to the field declaration for the
971  /// 'this' capture.
972  void getCaptureFields(llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
973                        FieldDecl *&ThisCapture) const;
974
975  /// getConversions - Retrieve the overload set containing all of the
976  /// conversion functions in this class.
977  UnresolvedSetImpl *getConversionFunctions() {
978    return &data().Conversions;
979  }
980  const UnresolvedSetImpl *getConversionFunctions() const {
981    return &data().Conversions;
982  }
983
984  typedef UnresolvedSetImpl::iterator conversion_iterator;
985  conversion_iterator conversion_begin() const {
986    return getConversionFunctions()->begin();
987  }
988  conversion_iterator conversion_end() const {
989    return getConversionFunctions()->end();
990  }
991
992  /// Removes a conversion function from this class.  The conversion
993  /// function must currently be a member of this class.  Furthermore,
994  /// this class must currently be in the process of being defined.
995  void removeConversion(const NamedDecl *Old);
996
997  /// getVisibleConversionFunctions - get all conversion functions visible
998  /// in current class; including conversion function templates.
999  const UnresolvedSetImpl *getVisibleConversionFunctions();
1000
1001  /// isAggregate - Whether this class is an aggregate (C++
1002  /// [dcl.init.aggr]), which is a class with no user-declared
1003  /// constructors, no private or protected non-static data members,
1004  /// no base classes, and no virtual functions (C++ [dcl.init.aggr]p1).
1005  bool isAggregate() const { return data().Aggregate; }
1006
1007  /// isPOD - Whether this class is a POD-type (C++ [class]p4), which is a class
1008  /// that is an aggregate that has no non-static non-POD data members, no
1009  /// reference data members, no user-defined copy assignment operator and no
1010  /// user-defined destructor.
1011  bool isPOD() const { return data().PlainOldData; }
1012
1013  /// \brief True if this class is C-like, without C++-specific features, e.g.
1014  /// it contains only public fields, no bases, tag kind is not 'class', etc.
1015  bool isCLike() const;
1016
1017  /// isEmpty - Whether this class is empty (C++0x [meta.unary.prop]), which
1018  /// means it has a virtual function, virtual base, data member (other than
1019  /// 0-width bit-field) or inherits from a non-empty class. Does NOT include
1020  /// a check for union-ness.
1021  bool isEmpty() const { return data().Empty; }
1022
1023  /// isPolymorphic - Whether this class is polymorphic (C++ [class.virtual]),
1024  /// which means that the class contains or inherits a virtual function.
1025  bool isPolymorphic() const { return data().Polymorphic; }
1026
1027  /// isAbstract - Whether this class is abstract (C++ [class.abstract]),
1028  /// which means that the class contains or inherits a pure virtual function.
1029  bool isAbstract() const { return data().Abstract; }
1030
1031  /// isStandardLayout - Whether this class has standard layout
1032  /// (C++ [class]p7)
1033  bool isStandardLayout() const { return data().IsStandardLayout; }
1034
1035  /// \brief Whether this class, or any of its class subobjects, contains a
1036  /// mutable field.
1037  bool hasMutableFields() const { return data().HasMutableFields; }
1038
1039  /// hasTrivialDefaultConstructor - Whether this class has a trivial default
1040  /// constructor (C++11 [class.ctor]p5).
1041  bool hasTrivialDefaultConstructor() const {
1042    return data().HasTrivialDefaultConstructor &&
1043           (!data().UserDeclaredConstructor ||
1044             data().DeclaredDefaultConstructor);
1045  }
1046
1047  /// hasConstexprNonCopyMoveConstructor - Whether this class has at least one
1048  /// constexpr constructor other than the copy or move constructors.
1049  bool hasConstexprNonCopyMoveConstructor() const {
1050    return data().HasConstexprNonCopyMoveConstructor ||
1051           (!hasUserDeclaredConstructor() &&
1052            defaultedDefaultConstructorIsConstexpr());
1053  }
1054
1055  /// defaultedDefaultConstructorIsConstexpr - Whether a defaulted default
1056  /// constructor for this class would be constexpr.
1057  bool defaultedDefaultConstructorIsConstexpr() const {
1058    return data().DefaultedDefaultConstructorIsConstexpr;
1059  }
1060
1061  /// defaultedCopyConstructorIsConstexpr - Whether a defaulted copy
1062  /// constructor for this class would be constexpr.
1063  bool defaultedCopyConstructorIsConstexpr() const {
1064    return data().DefaultedCopyConstructorIsConstexpr;
1065  }
1066
1067  /// defaultedMoveConstructorIsConstexpr - Whether a defaulted move
1068  /// constructor for this class would be constexpr.
1069  bool defaultedMoveConstructorIsConstexpr() const {
1070    return data().DefaultedMoveConstructorIsConstexpr;
1071  }
1072
1073  /// hasConstexprDefaultConstructor - Whether this class has a constexpr
1074  /// default constructor.
1075  bool hasConstexprDefaultConstructor() const {
1076    return data().HasConstexprDefaultConstructor ||
1077           (!data().UserDeclaredConstructor &&
1078            data().DefaultedDefaultConstructorIsConstexpr && isLiteral());
1079  }
1080
1081  /// hasConstexprCopyConstructor - Whether this class has a constexpr copy
1082  /// constructor.
1083  bool hasConstexprCopyConstructor() const {
1084    return data().HasConstexprCopyConstructor ||
1085           (!data().DeclaredCopyConstructor &&
1086            data().DefaultedCopyConstructorIsConstexpr && isLiteral());
1087  }
1088
1089  /// hasConstexprMoveConstructor - Whether this class has a constexpr move
1090  /// constructor.
1091  bool hasConstexprMoveConstructor() const {
1092    return data().HasConstexprMoveConstructor ||
1093           (needsImplicitMoveConstructor() &&
1094            data().DefaultedMoveConstructorIsConstexpr && isLiteral());
1095  }
1096
1097  // hasTrivialCopyConstructor - Whether this class has a trivial copy
1098  // constructor (C++ [class.copy]p6, C++0x [class.copy]p13)
1099  bool hasTrivialCopyConstructor() const {
1100    return data().HasTrivialCopyConstructor;
1101  }
1102
1103  // hasTrivialMoveConstructor - Whether this class has a trivial move
1104  // constructor (C++0x [class.copy]p13)
1105  bool hasTrivialMoveConstructor() const {
1106    return data().HasTrivialMoveConstructor;
1107  }
1108
1109  // hasTrivialCopyAssignment - Whether this class has a trivial copy
1110  // assignment operator (C++ [class.copy]p11, C++0x [class.copy]p27)
1111  bool hasTrivialCopyAssignment() const {
1112    return data().HasTrivialCopyAssignment;
1113  }
1114
1115  // hasTrivialMoveAssignment - Whether this class has a trivial move
1116  // assignment operator (C++0x [class.copy]p27)
1117  bool hasTrivialMoveAssignment() const {
1118    return data().HasTrivialMoveAssignment;
1119  }
1120
1121  // hasTrivialDestructor - Whether this class has a trivial destructor
1122  // (C++ [class.dtor]p3)
1123  bool hasTrivialDestructor() const { return data().HasTrivialDestructor; }
1124
1125  // hasIrrelevantDestructor - Whether this class has a destructor which has no
1126  // semantic effect. Any such destructor will be trivial, public, defaulted
1127  // and not deleted.
1128  bool hasIrrelevantDestructor() const {
1129    return hasTrivialDestructor() && !hasUserDeclaredDestructor();
1130  }
1131
1132  // hasNonLiteralTypeFieldsOrBases - Whether this class has a non-literal or
1133  // volatile type non-static data member or base class.
1134  bool hasNonLiteralTypeFieldsOrBases() const {
1135    return data().HasNonLiteralTypeFieldsOrBases;
1136  }
1137
1138  // isTriviallyCopyable - Whether this class is considered trivially copyable
1139  // (C++0x [class]p6).
1140  bool isTriviallyCopyable() const;
1141
1142  // isTrivial - Whether this class is considered trivial
1143  //
1144  // C++0x [class]p6
1145  //    A trivial class is a class that has a trivial default constructor and
1146  //    is trivially copiable.
1147  bool isTrivial() const {
1148    return isTriviallyCopyable() && hasTrivialDefaultConstructor();
1149  }
1150
1151  // isLiteral - Whether this class is a literal type.
1152  //
1153  // C++11 [basic.types]p10
1154  //   A class type that has all the following properties:
1155  //     -- it has a trivial destructor
1156  //     -- every constructor call and full-expression in the
1157  //        brace-or-equal-intializers for non-static data members (if any) is
1158  //        a constant expression.
1159  //     -- it is an aggregate type or has at least one constexpr constructor or
1160  //        constructor template that is not a copy or move constructor, and
1161  //     -- all of its non-static data members and base classes are of literal
1162  //        types
1163  //
1164  // We resolve DR1361 by ignoring the second bullet. We resolve DR1452 by
1165  // treating types with trivial default constructors as literal types.
1166  bool isLiteral() const {
1167    return hasTrivialDestructor() &&
1168           (isAggregate() || hasConstexprNonCopyMoveConstructor() ||
1169            hasTrivialDefaultConstructor()) &&
1170           !hasNonLiteralTypeFieldsOrBases();
1171  }
1172
1173  /// \brief If this record is an instantiation of a member class,
1174  /// retrieves the member class from which it was instantiated.
1175  ///
1176  /// This routine will return non-NULL for (non-templated) member
1177  /// classes of class templates. For example, given:
1178  ///
1179  /// \code
1180  /// template<typename T>
1181  /// struct X {
1182  ///   struct A { };
1183  /// };
1184  /// \endcode
1185  ///
1186  /// The declaration for X<int>::A is a (non-templated) CXXRecordDecl
1187  /// whose parent is the class template specialization X<int>. For
1188  /// this declaration, getInstantiatedFromMemberClass() will return
1189  /// the CXXRecordDecl X<T>::A. When a complete definition of
1190  /// X<int>::A is required, it will be instantiated from the
1191  /// declaration returned by getInstantiatedFromMemberClass().
1192  CXXRecordDecl *getInstantiatedFromMemberClass() const;
1193
1194  /// \brief If this class is an instantiation of a member class of a
1195  /// class template specialization, retrieves the member specialization
1196  /// information.
1197  MemberSpecializationInfo *getMemberSpecializationInfo() const;
1198
1199  /// \brief Specify that this record is an instantiation of the
1200  /// member class RD.
1201  void setInstantiationOfMemberClass(CXXRecordDecl *RD,
1202                                     TemplateSpecializationKind TSK);
1203
1204  /// \brief Retrieves the class template that is described by this
1205  /// class declaration.
1206  ///
1207  /// Every class template is represented as a ClassTemplateDecl and a
1208  /// CXXRecordDecl. The former contains template properties (such as
1209  /// the template parameter lists) while the latter contains the
1210  /// actual description of the template's
1211  /// contents. ClassTemplateDecl::getTemplatedDecl() retrieves the
1212  /// CXXRecordDecl that from a ClassTemplateDecl, while
1213  /// getDescribedClassTemplate() retrieves the ClassTemplateDecl from
1214  /// a CXXRecordDecl.
1215  ClassTemplateDecl *getDescribedClassTemplate() const {
1216    return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl*>();
1217  }
1218
1219  void setDescribedClassTemplate(ClassTemplateDecl *Template) {
1220    TemplateOrInstantiation = Template;
1221  }
1222
1223  /// \brief Determine whether this particular class is a specialization or
1224  /// instantiation of a class template or member class of a class template,
1225  /// and how it was instantiated or specialized.
1226  TemplateSpecializationKind getTemplateSpecializationKind() const;
1227
1228  /// \brief Set the kind of specialization or template instantiation this is.
1229  void setTemplateSpecializationKind(TemplateSpecializationKind TSK);
1230
1231  /// getDestructor - Returns the destructor decl for this class.
1232  CXXDestructorDecl *getDestructor() const;
1233
1234  /// isLocalClass - If the class is a local class [class.local], returns
1235  /// the enclosing function declaration.
1236  const FunctionDecl *isLocalClass() const {
1237    if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(getDeclContext()))
1238      return RD->isLocalClass();
1239
1240    return dyn_cast<FunctionDecl>(getDeclContext());
1241  }
1242
1243  /// \brief Determine whether this class is derived from the class \p Base.
1244  ///
1245  /// This routine only determines whether this class is derived from \p Base,
1246  /// but does not account for factors that may make a Derived -> Base class
1247  /// ill-formed, such as private/protected inheritance or multiple, ambiguous
1248  /// base class subobjects.
1249  ///
1250  /// \param Base the base class we are searching for.
1251  ///
1252  /// \returns true if this class is derived from Base, false otherwise.
1253  bool isDerivedFrom(const CXXRecordDecl *Base) const;
1254
1255  /// \brief Determine whether this class is derived from the type \p Base.
1256  ///
1257  /// This routine only determines whether this class is derived from \p Base,
1258  /// but does not account for factors that may make a Derived -> Base class
1259  /// ill-formed, such as private/protected inheritance or multiple, ambiguous
1260  /// base class subobjects.
1261  ///
1262  /// \param Base the base class we are searching for.
1263  ///
1264  /// \param Paths will contain the paths taken from the current class to the
1265  /// given \p Base class.
1266  ///
1267  /// \returns true if this class is derived from Base, false otherwise.
1268  ///
1269  /// \todo add a separate paramaeter to configure IsDerivedFrom, rather than
1270  /// tangling input and output in \p Paths
1271  bool isDerivedFrom(const CXXRecordDecl *Base, CXXBasePaths &Paths) const;
1272
1273  /// \brief Determine whether this class is virtually derived from
1274  /// the class \p Base.
1275  ///
1276  /// This routine only determines whether this class is virtually
1277  /// derived from \p Base, but does not account for factors that may
1278  /// make a Derived -> Base class ill-formed, such as
1279  /// private/protected inheritance or multiple, ambiguous base class
1280  /// subobjects.
1281  ///
1282  /// \param Base the base class we are searching for.
1283  ///
1284  /// \returns true if this class is virtually derived from Base,
1285  /// false otherwise.
1286  bool isVirtuallyDerivedFrom(CXXRecordDecl *Base) const;
1287
1288  /// \brief Determine whether this class is provably not derived from
1289  /// the type \p Base.
1290  bool isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const;
1291
1292  /// \brief Function type used by forallBases() as a callback.
1293  ///
1294  /// \param Base the definition of the base class
1295  ///
1296  /// \returns true if this base matched the search criteria
1297  typedef bool ForallBasesCallback(const CXXRecordDecl *BaseDefinition,
1298                                   void *UserData);
1299
1300  /// \brief Determines if the given callback holds for all the direct
1301  /// or indirect base classes of this type.
1302  ///
1303  /// The class itself does not count as a base class.  This routine
1304  /// returns false if the class has non-computable base classes.
1305  ///
1306  /// \param AllowShortCircuit if false, forces the callback to be called
1307  /// for every base class, even if a dependent or non-matching base was
1308  /// found.
1309  bool forallBases(ForallBasesCallback *BaseMatches, void *UserData,
1310                   bool AllowShortCircuit = true) const;
1311
1312  /// \brief Function type used by lookupInBases() to determine whether a
1313  /// specific base class subobject matches the lookup criteria.
1314  ///
1315  /// \param Specifier the base-class specifier that describes the inheritance
1316  /// from the base class we are trying to match.
1317  ///
1318  /// \param Path the current path, from the most-derived class down to the
1319  /// base named by the \p Specifier.
1320  ///
1321  /// \param UserData a single pointer to user-specified data, provided to
1322  /// lookupInBases().
1323  ///
1324  /// \returns true if this base matched the search criteria, false otherwise.
1325  typedef bool BaseMatchesCallback(const CXXBaseSpecifier *Specifier,
1326                                   CXXBasePath &Path,
1327                                   void *UserData);
1328
1329  /// \brief Look for entities within the base classes of this C++ class,
1330  /// transitively searching all base class subobjects.
1331  ///
1332  /// This routine uses the callback function \p BaseMatches to find base
1333  /// classes meeting some search criteria, walking all base class subobjects
1334  /// and populating the given \p Paths structure with the paths through the
1335  /// inheritance hierarchy that resulted in a match. On a successful search,
1336  /// the \p Paths structure can be queried to retrieve the matching paths and
1337  /// to determine if there were any ambiguities.
1338  ///
1339  /// \param BaseMatches callback function used to determine whether a given
1340  /// base matches the user-defined search criteria.
1341  ///
1342  /// \param UserData user data pointer that will be provided to \p BaseMatches.
1343  ///
1344  /// \param Paths used to record the paths from this class to its base class
1345  /// subobjects that match the search criteria.
1346  ///
1347  /// \returns true if there exists any path from this class to a base class
1348  /// subobject that matches the search criteria.
1349  bool lookupInBases(BaseMatchesCallback *BaseMatches, void *UserData,
1350                     CXXBasePaths &Paths) const;
1351
1352  /// \brief Base-class lookup callback that determines whether the given
1353  /// base class specifier refers to a specific class declaration.
1354  ///
1355  /// This callback can be used with \c lookupInBases() to determine whether
1356  /// a given derived class has is a base class subobject of a particular type.
1357  /// The user data pointer should refer to the canonical CXXRecordDecl of the
1358  /// base class that we are searching for.
1359  static bool FindBaseClass(const CXXBaseSpecifier *Specifier,
1360                            CXXBasePath &Path, void *BaseRecord);
1361
1362  /// \brief Base-class lookup callback that determines whether the
1363  /// given base class specifier refers to a specific class
1364  /// declaration and describes virtual derivation.
1365  ///
1366  /// This callback can be used with \c lookupInBases() to determine
1367  /// whether a given derived class has is a virtual base class
1368  /// subobject of a particular type.  The user data pointer should
1369  /// refer to the canonical CXXRecordDecl of the base class that we
1370  /// are searching for.
1371  static bool FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
1372                                   CXXBasePath &Path, void *BaseRecord);
1373
1374  /// \brief Base-class lookup callback that determines whether there exists
1375  /// a tag with the given name.
1376  ///
1377  /// This callback can be used with \c lookupInBases() to find tag members
1378  /// of the given name within a C++ class hierarchy. The user data pointer
1379  /// is an opaque \c DeclarationName pointer.
1380  static bool FindTagMember(const CXXBaseSpecifier *Specifier,
1381                            CXXBasePath &Path, void *Name);
1382
1383  /// \brief Base-class lookup callback that determines whether there exists
1384  /// a member with the given name.
1385  ///
1386  /// This callback can be used with \c lookupInBases() to find members
1387  /// of the given name within a C++ class hierarchy. The user data pointer
1388  /// is an opaque \c DeclarationName pointer.
1389  static bool FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
1390                                 CXXBasePath &Path, void *Name);
1391
1392  /// \brief Base-class lookup callback that determines whether there exists
1393  /// a member with the given name that can be used in a nested-name-specifier.
1394  ///
1395  /// This callback can be used with \c lookupInBases() to find membes of
1396  /// the given name within a C++ class hierarchy that can occur within
1397  /// nested-name-specifiers.
1398  static bool FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
1399                                            CXXBasePath &Path,
1400                                            void *UserData);
1401
1402  /// \brief Retrieve the final overriders for each virtual member
1403  /// function in the class hierarchy where this class is the
1404  /// most-derived class in the class hierarchy.
1405  void getFinalOverriders(CXXFinalOverriderMap &FinaOverriders) const;
1406
1407  /// \brief Get the indirect primary bases for this class.
1408  void getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const;
1409
1410  /// viewInheritance - Renders and displays an inheritance diagram
1411  /// for this C++ class and all of its base classes (transitively) using
1412  /// GraphViz.
1413  void viewInheritance(ASTContext& Context) const;
1414
1415  /// MergeAccess - Calculates the access of a decl that is reached
1416  /// along a path.
1417  static AccessSpecifier MergeAccess(AccessSpecifier PathAccess,
1418                                     AccessSpecifier DeclAccess) {
1419    assert(DeclAccess != AS_none);
1420    if (DeclAccess == AS_private) return AS_none;
1421    return (PathAccess > DeclAccess ? PathAccess : DeclAccess);
1422  }
1423
1424  /// \brief Indicates that the definition of this class is now complete.
1425  virtual void completeDefinition();
1426
1427  /// \brief Indicates that the definition of this class is now complete,
1428  /// and provides a final overrider map to help determine
1429  ///
1430  /// \param FinalOverriders The final overrider map for this class, which can
1431  /// be provided as an optimization for abstract-class checking. If NULL,
1432  /// final overriders will be computed if they are needed to complete the
1433  /// definition.
1434  void completeDefinition(CXXFinalOverriderMap *FinalOverriders);
1435
1436  /// \brief Determine whether this class may end up being abstract, even though
1437  /// it is not yet known to be abstract.
1438  ///
1439  /// \returns true if this class is not known to be abstract but has any
1440  /// base classes that are abstract. In this case, \c completeDefinition()
1441  /// will need to compute final overriders to determine whether the class is
1442  /// actually abstract.
1443  bool mayBeAbstract() const;
1444
1445  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1446  static bool classofKind(Kind K) {
1447    return K >= firstCXXRecord && K <= lastCXXRecord;
1448  }
1449  static bool classof(const CXXRecordDecl *D) { return true; }
1450  static bool classof(const ClassTemplateSpecializationDecl *D) {
1451    return true;
1452  }
1453
1454  friend class ASTDeclReader;
1455  friend class ASTDeclWriter;
1456  friend class ASTReader;
1457  friend class ASTWriter;
1458};
1459
1460/// CXXMethodDecl - Represents a static or instance method of a
1461/// struct/union/class.
1462class CXXMethodDecl : public FunctionDecl {
1463  virtual void anchor();
1464protected:
1465  CXXMethodDecl(Kind DK, CXXRecordDecl *RD, SourceLocation StartLoc,
1466                const DeclarationNameInfo &NameInfo,
1467                QualType T, TypeSourceInfo *TInfo,
1468                bool isStatic, StorageClass SCAsWritten, bool isInline,
1469                bool isConstexpr, SourceLocation EndLocation)
1470    : FunctionDecl(DK, RD, StartLoc, NameInfo, T, TInfo,
1471                   (isStatic ? SC_Static : SC_None),
1472                   SCAsWritten, isInline, isConstexpr) {
1473    if (EndLocation.isValid())
1474      setRangeEnd(EndLocation);
1475  }
1476
1477public:
1478  static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD,
1479                               SourceLocation StartLoc,
1480                               const DeclarationNameInfo &NameInfo,
1481                               QualType T, TypeSourceInfo *TInfo,
1482                               bool isStatic,
1483                               StorageClass SCAsWritten,
1484                               bool isInline,
1485                               bool isConstexpr,
1486                               SourceLocation EndLocation);
1487
1488  static CXXMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1489
1490  bool isStatic() const { return getStorageClass() == SC_Static; }
1491  bool isInstance() const { return !isStatic(); }
1492
1493  bool isVirtual() const {
1494    CXXMethodDecl *CD =
1495      cast<CXXMethodDecl>(const_cast<CXXMethodDecl*>(this)->getCanonicalDecl());
1496
1497    if (CD->isVirtualAsWritten())
1498      return true;
1499
1500    return (CD->begin_overridden_methods() != CD->end_overridden_methods());
1501  }
1502
1503  /// \brief Determine whether this is a usual deallocation function
1504  /// (C++ [basic.stc.dynamic.deallocation]p2), which is an overloaded
1505  /// delete or delete[] operator with a particular signature.
1506  bool isUsualDeallocationFunction() const;
1507
1508  /// \brief Determine whether this is a copy-assignment operator, regardless
1509  /// of whether it was declared implicitly or explicitly.
1510  bool isCopyAssignmentOperator() const;
1511
1512  /// \brief Determine whether this is a move assignment operator.
1513  bool isMoveAssignmentOperator() const;
1514
1515  const CXXMethodDecl *getCanonicalDecl() const {
1516    return cast<CXXMethodDecl>(FunctionDecl::getCanonicalDecl());
1517  }
1518  CXXMethodDecl *getCanonicalDecl() {
1519    return cast<CXXMethodDecl>(FunctionDecl::getCanonicalDecl());
1520  }
1521
1522  /// isUserProvided - True if it is either an implicit constructor or
1523  /// if it was defaulted or deleted on first declaration.
1524  bool isUserProvided() const {
1525    return !(isDeleted() || getCanonicalDecl()->isDefaulted());
1526  }
1527
1528  ///
1529  void addOverriddenMethod(const CXXMethodDecl *MD);
1530
1531  typedef const CXXMethodDecl ** method_iterator;
1532
1533  method_iterator begin_overridden_methods() const;
1534  method_iterator end_overridden_methods() const;
1535  unsigned size_overridden_methods() const;
1536
1537  /// getParent - Returns the parent of this method declaration, which
1538  /// is the class in which this method is defined.
1539  const CXXRecordDecl *getParent() const {
1540    return cast<CXXRecordDecl>(FunctionDecl::getParent());
1541  }
1542
1543  /// getParent - Returns the parent of this method declaration, which
1544  /// is the class in which this method is defined.
1545  CXXRecordDecl *getParent() {
1546    return const_cast<CXXRecordDecl *>(
1547             cast<CXXRecordDecl>(FunctionDecl::getParent()));
1548  }
1549
1550  /// getThisType - Returns the type of 'this' pointer.
1551  /// Should only be called for instance methods.
1552  QualType getThisType(ASTContext &C) const;
1553
1554  unsigned getTypeQualifiers() const {
1555    return getType()->getAs<FunctionProtoType>()->getTypeQuals();
1556  }
1557
1558  /// \brief Retrieve the ref-qualifier associated with this method.
1559  ///
1560  /// In the following example, \c f() has an lvalue ref-qualifier, \c g()
1561  /// has an rvalue ref-qualifier, and \c h() has no ref-qualifier.
1562  /// \code
1563  /// struct X {
1564  ///   void f() &;
1565  ///   void g() &&;
1566  ///   void h();
1567  /// };
1568  /// \endcode
1569  RefQualifierKind getRefQualifier() const {
1570    return getType()->getAs<FunctionProtoType>()->getRefQualifier();
1571  }
1572
1573  bool hasInlineBody() const;
1574
1575  /// \brief Determine whether this is a lambda closure type's static member
1576  /// function that is used for the result of the lambda's conversion to
1577  /// function pointer (for a lambda with no captures).
1578  ///
1579  /// The function itself, if used, will have a placeholder body that will be
1580  /// supplied by IR generation to either forward to the function call operator
1581  /// or clone the function call operator.
1582  bool isLambdaStaticInvoker() const;
1583
1584  // Implement isa/cast/dyncast/etc.
1585  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1586  static bool classof(const CXXMethodDecl *D) { return true; }
1587  static bool classofKind(Kind K) {
1588    return K >= firstCXXMethod && K <= lastCXXMethod;
1589  }
1590};
1591
1592/// CXXCtorInitializer - Represents a C++ base or member
1593/// initializer, which is part of a constructor initializer that
1594/// initializes one non-static member variable or one base class. For
1595/// example, in the following, both 'A(a)' and 'f(3.14159)' are member
1596/// initializers:
1597///
1598/// @code
1599/// class A { };
1600/// class B : public A {
1601///   float f;
1602/// public:
1603///   B(A& a) : A(a), f(3.14159) { }
1604/// };
1605/// @endcode
1606class CXXCtorInitializer {
1607  /// \brief Either the base class name/delegating constructor type (stored as
1608  /// a TypeSourceInfo*), an normal field (FieldDecl), or an anonymous field
1609  /// (IndirectFieldDecl*) being initialized.
1610  llvm::PointerUnion3<TypeSourceInfo *, FieldDecl *, IndirectFieldDecl *>
1611    Initializee;
1612
1613  /// \brief The source location for the field name or, for a base initializer
1614  /// pack expansion, the location of the ellipsis. In the case of a delegating
1615  /// constructor, it will still include the type's source location as the
1616  /// Initializee points to the CXXConstructorDecl (to allow loop detection).
1617  SourceLocation MemberOrEllipsisLocation;
1618
1619  /// \brief The argument used to initialize the base or member, which may
1620  /// end up constructing an object (when multiple arguments are involved).
1621  /// If 0, this is a field initializer, and the in-class member initializer
1622  /// will be used.
1623  Stmt *Init;
1624
1625  /// LParenLoc - Location of the left paren of the ctor-initializer.
1626  SourceLocation LParenLoc;
1627
1628  /// RParenLoc - Location of the right paren of the ctor-initializer.
1629  SourceLocation RParenLoc;
1630
1631  /// \brief If the initializee is a type, whether that type makes this
1632  /// a delegating initialization.
1633  bool IsDelegating : 1;
1634
1635  /// IsVirtual - If the initializer is a base initializer, this keeps track
1636  /// of whether the base is virtual or not.
1637  bool IsVirtual : 1;
1638
1639  /// IsWritten - Whether or not the initializer is explicitly written
1640  /// in the sources.
1641  bool IsWritten : 1;
1642
1643  /// SourceOrderOrNumArrayIndices - If IsWritten is true, then this
1644  /// number keeps track of the textual order of this initializer in the
1645  /// original sources, counting from 0; otherwise, if IsWritten is false,
1646  /// it stores the number of array index variables stored after this
1647  /// object in memory.
1648  unsigned SourceOrderOrNumArrayIndices : 13;
1649
1650  CXXCtorInitializer(ASTContext &Context, FieldDecl *Member,
1651                     SourceLocation MemberLoc, SourceLocation L, Expr *Init,
1652                     SourceLocation R, VarDecl **Indices, unsigned NumIndices);
1653
1654public:
1655  /// CXXCtorInitializer - Creates a new base-class initializer.
1656  explicit
1657  CXXCtorInitializer(ASTContext &Context, TypeSourceInfo *TInfo, bool IsVirtual,
1658                     SourceLocation L, Expr *Init, SourceLocation R,
1659                     SourceLocation EllipsisLoc);
1660
1661  /// CXXCtorInitializer - Creates a new member initializer.
1662  explicit
1663  CXXCtorInitializer(ASTContext &Context, FieldDecl *Member,
1664                     SourceLocation MemberLoc, SourceLocation L, Expr *Init,
1665                     SourceLocation R);
1666
1667  /// CXXCtorInitializer - Creates a new anonymous field initializer.
1668  explicit
1669  CXXCtorInitializer(ASTContext &Context, IndirectFieldDecl *Member,
1670                     SourceLocation MemberLoc, SourceLocation L, Expr *Init,
1671                     SourceLocation R);
1672
1673  /// CXXCtorInitializer - Creates a new delegating Initializer.
1674  explicit
1675  CXXCtorInitializer(ASTContext &Context, TypeSourceInfo *TInfo,
1676                     SourceLocation L, Expr *Init, SourceLocation R);
1677
1678  /// \brief Creates a new member initializer that optionally contains
1679  /// array indices used to describe an elementwise initialization.
1680  static CXXCtorInitializer *Create(ASTContext &Context, FieldDecl *Member,
1681                                    SourceLocation MemberLoc, SourceLocation L,
1682                                    Expr *Init, SourceLocation R,
1683                                    VarDecl **Indices, unsigned NumIndices);
1684
1685  /// isBaseInitializer - Returns true when this initializer is
1686  /// initializing a base class.
1687  bool isBaseInitializer() const {
1688    return Initializee.is<TypeSourceInfo*>() && !IsDelegating;
1689  }
1690
1691  /// isMemberInitializer - Returns true when this initializer is
1692  /// initializing a non-static data member.
1693  bool isMemberInitializer() const { return Initializee.is<FieldDecl*>(); }
1694
1695  bool isAnyMemberInitializer() const {
1696    return isMemberInitializer() || isIndirectMemberInitializer();
1697  }
1698
1699  bool isIndirectMemberInitializer() const {
1700    return Initializee.is<IndirectFieldDecl*>();
1701  }
1702
1703  /// isInClassMemberInitializer - Returns true when this initializer is an
1704  /// implicit ctor initializer generated for a field with an initializer
1705  /// defined on the member declaration.
1706  bool isInClassMemberInitializer() const {
1707    return !Init;
1708  }
1709
1710  /// isDelegatingInitializer - Returns true when this initializer is creating
1711  /// a delegating constructor.
1712  bool isDelegatingInitializer() const {
1713    return Initializee.is<TypeSourceInfo*>() && IsDelegating;
1714  }
1715
1716  /// \brief Determine whether this initializer is a pack expansion.
1717  bool isPackExpansion() const {
1718    return isBaseInitializer() && MemberOrEllipsisLocation.isValid();
1719  }
1720
1721  // \brief For a pack expansion, returns the location of the ellipsis.
1722  SourceLocation getEllipsisLoc() const {
1723    assert(isPackExpansion() && "Initializer is not a pack expansion");
1724    return MemberOrEllipsisLocation;
1725  }
1726
1727  /// If this is a base class initializer, returns the type of the
1728  /// base class with location information. Otherwise, returns an NULL
1729  /// type location.
1730  TypeLoc getBaseClassLoc() const;
1731
1732  /// If this is a base class initializer, returns the type of the base class.
1733  /// Otherwise, returns NULL.
1734  const Type *getBaseClass() const;
1735
1736  /// Returns whether the base is virtual or not.
1737  bool isBaseVirtual() const {
1738    assert(isBaseInitializer() && "Must call this on base initializer!");
1739
1740    return IsVirtual;
1741  }
1742
1743  /// \brief Returns the declarator information for a base class or delegating
1744  /// initializer.
1745  TypeSourceInfo *getTypeSourceInfo() const {
1746    return Initializee.dyn_cast<TypeSourceInfo *>();
1747  }
1748
1749  /// getMember - If this is a member initializer, returns the
1750  /// declaration of the non-static data member being
1751  /// initialized. Otherwise, returns NULL.
1752  FieldDecl *getMember() const {
1753    if (isMemberInitializer())
1754      return Initializee.get<FieldDecl*>();
1755    return 0;
1756  }
1757  FieldDecl *getAnyMember() const {
1758    if (isMemberInitializer())
1759      return Initializee.get<FieldDecl*>();
1760    if (isIndirectMemberInitializer())
1761      return Initializee.get<IndirectFieldDecl*>()->getAnonField();
1762    return 0;
1763  }
1764
1765  IndirectFieldDecl *getIndirectMember() const {
1766    if (isIndirectMemberInitializer())
1767      return Initializee.get<IndirectFieldDecl*>();
1768    return 0;
1769  }
1770
1771  SourceLocation getMemberLocation() const {
1772    return MemberOrEllipsisLocation;
1773  }
1774
1775  /// \brief Determine the source location of the initializer.
1776  SourceLocation getSourceLocation() const;
1777
1778  /// \brief Determine the source range covering the entire initializer.
1779  SourceRange getSourceRange() const;
1780
1781  /// isWritten - Returns true if this initializer is explicitly written
1782  /// in the source code.
1783  bool isWritten() const { return IsWritten; }
1784
1785  /// \brief Return the source position of the initializer, counting from 0.
1786  /// If the initializer was implicit, -1 is returned.
1787  int getSourceOrder() const {
1788    return IsWritten ? static_cast<int>(SourceOrderOrNumArrayIndices) : -1;
1789  }
1790
1791  /// \brief Set the source order of this initializer. This method can only
1792  /// be called once for each initializer; it cannot be called on an
1793  /// initializer having a positive number of (implicit) array indices.
1794  void setSourceOrder(int pos) {
1795    assert(!IsWritten &&
1796           "calling twice setSourceOrder() on the same initializer");
1797    assert(SourceOrderOrNumArrayIndices == 0 &&
1798           "setSourceOrder() used when there are implicit array indices");
1799    assert(pos >= 0 &&
1800           "setSourceOrder() used to make an initializer implicit");
1801    IsWritten = true;
1802    SourceOrderOrNumArrayIndices = static_cast<unsigned>(pos);
1803  }
1804
1805  SourceLocation getLParenLoc() const { return LParenLoc; }
1806  SourceLocation getRParenLoc() const { return RParenLoc; }
1807
1808  /// \brief Determine the number of implicit array indices used while
1809  /// described an array member initialization.
1810  unsigned getNumArrayIndices() const {
1811    return IsWritten ? 0 : SourceOrderOrNumArrayIndices;
1812  }
1813
1814  /// \brief Retrieve a particular array index variable used to
1815  /// describe an array member initialization.
1816  VarDecl *getArrayIndex(unsigned I) {
1817    assert(I < getNumArrayIndices() && "Out of bounds member array index");
1818    return reinterpret_cast<VarDecl **>(this + 1)[I];
1819  }
1820  const VarDecl *getArrayIndex(unsigned I) const {
1821    assert(I < getNumArrayIndices() && "Out of bounds member array index");
1822    return reinterpret_cast<const VarDecl * const *>(this + 1)[I];
1823  }
1824  void setArrayIndex(unsigned I, VarDecl *Index) {
1825    assert(I < getNumArrayIndices() && "Out of bounds member array index");
1826    reinterpret_cast<VarDecl **>(this + 1)[I] = Index;
1827  }
1828  ArrayRef<VarDecl *> getArrayIndexes() {
1829    assert(getNumArrayIndices() != 0 && "Getting indexes for non-array init");
1830    return ArrayRef<VarDecl *>(reinterpret_cast<VarDecl **>(this + 1),
1831                               getNumArrayIndices());
1832  }
1833
1834  /// \brief Get the initializer. This is 0 if this is an in-class initializer
1835  /// for a non-static data member which has not yet been parsed.
1836  Expr *getInit() const {
1837    if (!Init)
1838      return getAnyMember()->getInClassInitializer();
1839
1840    return static_cast<Expr*>(Init);
1841  }
1842};
1843
1844/// CXXConstructorDecl - Represents a C++ constructor within a
1845/// class. For example:
1846///
1847/// @code
1848/// class X {
1849/// public:
1850///   explicit X(int); // represented by a CXXConstructorDecl.
1851/// };
1852/// @endcode
1853class CXXConstructorDecl : public CXXMethodDecl {
1854  virtual void anchor();
1855  /// IsExplicitSpecified - Whether this constructor declaration has the
1856  /// 'explicit' keyword specified.
1857  bool IsExplicitSpecified : 1;
1858
1859  /// ImplicitlyDefined - Whether this constructor was implicitly
1860  /// defined by the compiler. When false, the constructor was defined
1861  /// by the user. In C++03, this flag will have the same value as
1862  /// Implicit. In C++0x, however, a constructor that is
1863  /// explicitly defaulted (i.e., defined with " = default") will have
1864  /// @c !Implicit && ImplicitlyDefined.
1865  bool ImplicitlyDefined : 1;
1866
1867  /// Support for base and member initializers.
1868  /// CtorInitializers - The arguments used to initialize the base
1869  /// or member.
1870  CXXCtorInitializer **CtorInitializers;
1871  unsigned NumCtorInitializers;
1872
1873  CXXConstructorDecl(CXXRecordDecl *RD, SourceLocation StartLoc,
1874                     const DeclarationNameInfo &NameInfo,
1875                     QualType T, TypeSourceInfo *TInfo,
1876                     bool isExplicitSpecified, bool isInline,
1877                     bool isImplicitlyDeclared, bool isConstexpr)
1878    : CXXMethodDecl(CXXConstructor, RD, StartLoc, NameInfo, T, TInfo, false,
1879                    SC_None, isInline, isConstexpr, SourceLocation()),
1880      IsExplicitSpecified(isExplicitSpecified), ImplicitlyDefined(false),
1881      CtorInitializers(0), NumCtorInitializers(0) {
1882    setImplicit(isImplicitlyDeclared);
1883  }
1884
1885public:
1886  static CXXConstructorDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1887  static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
1888                                    SourceLocation StartLoc,
1889                                    const DeclarationNameInfo &NameInfo,
1890                                    QualType T, TypeSourceInfo *TInfo,
1891                                    bool isExplicit,
1892                                    bool isInline, bool isImplicitlyDeclared,
1893                                    bool isConstexpr);
1894
1895  /// isExplicitSpecified - Whether this constructor declaration has the
1896  /// 'explicit' keyword specified.
1897  bool isExplicitSpecified() const { return IsExplicitSpecified; }
1898
1899  /// isExplicit - Whether this constructor was marked "explicit" or not.
1900  bool isExplicit() const {
1901    return cast<CXXConstructorDecl>(getFirstDeclaration())
1902      ->isExplicitSpecified();
1903  }
1904
1905  /// isImplicitlyDefined - Whether this constructor was implicitly
1906  /// defined. If false, then this constructor was defined by the
1907  /// user. This operation can only be invoked if the constructor has
1908  /// already been defined.
1909  bool isImplicitlyDefined() const {
1910    assert(isThisDeclarationADefinition() &&
1911           "Can only get the implicit-definition flag once the "
1912           "constructor has been defined");
1913    return ImplicitlyDefined;
1914  }
1915
1916  /// setImplicitlyDefined - Set whether this constructor was
1917  /// implicitly defined or not.
1918  void setImplicitlyDefined(bool ID) {
1919    assert(isThisDeclarationADefinition() &&
1920           "Can only set the implicit-definition flag once the constructor "
1921           "has been defined");
1922    ImplicitlyDefined = ID;
1923  }
1924
1925  /// init_iterator - Iterates through the member/base initializer list.
1926  typedef CXXCtorInitializer **init_iterator;
1927
1928  /// init_const_iterator - Iterates through the memberbase initializer list.
1929  typedef CXXCtorInitializer * const * init_const_iterator;
1930
1931  /// init_begin() - Retrieve an iterator to the first initializer.
1932  init_iterator       init_begin()       { return CtorInitializers; }
1933  /// begin() - Retrieve an iterator to the first initializer.
1934  init_const_iterator init_begin() const { return CtorInitializers; }
1935
1936  /// init_end() - Retrieve an iterator past the last initializer.
1937  init_iterator       init_end()       {
1938    return CtorInitializers + NumCtorInitializers;
1939  }
1940  /// end() - Retrieve an iterator past the last initializer.
1941  init_const_iterator init_end() const {
1942    return CtorInitializers + NumCtorInitializers;
1943  }
1944
1945  typedef std::reverse_iterator<init_iterator> init_reverse_iterator;
1946  typedef std::reverse_iterator<init_const_iterator>
1947          init_const_reverse_iterator;
1948
1949  init_reverse_iterator init_rbegin() {
1950    return init_reverse_iterator(init_end());
1951  }
1952  init_const_reverse_iterator init_rbegin() const {
1953    return init_const_reverse_iterator(init_end());
1954  }
1955
1956  init_reverse_iterator init_rend() {
1957    return init_reverse_iterator(init_begin());
1958  }
1959  init_const_reverse_iterator init_rend() const {
1960    return init_const_reverse_iterator(init_begin());
1961  }
1962
1963  /// getNumArgs - Determine the number of arguments used to
1964  /// initialize the member or base.
1965  unsigned getNumCtorInitializers() const {
1966      return NumCtorInitializers;
1967  }
1968
1969  void setNumCtorInitializers(unsigned numCtorInitializers) {
1970    NumCtorInitializers = numCtorInitializers;
1971  }
1972
1973  void setCtorInitializers(CXXCtorInitializer ** initializers) {
1974    CtorInitializers = initializers;
1975  }
1976
1977  /// isDelegatingConstructor - Whether this constructor is a
1978  /// delegating constructor
1979  bool isDelegatingConstructor() const {
1980    return (getNumCtorInitializers() == 1) &&
1981      CtorInitializers[0]->isDelegatingInitializer();
1982  }
1983
1984  /// getTargetConstructor - When this constructor delegates to
1985  /// another, retrieve the target
1986  CXXConstructorDecl *getTargetConstructor() const;
1987
1988  /// isDefaultConstructor - Whether this constructor is a default
1989  /// constructor (C++ [class.ctor]p5), which can be used to
1990  /// default-initialize a class of this type.
1991  bool isDefaultConstructor() const;
1992
1993  /// isCopyConstructor - Whether this constructor is a copy
1994  /// constructor (C++ [class.copy]p2, which can be used to copy the
1995  /// class. @p TypeQuals will be set to the qualifiers on the
1996  /// argument type. For example, @p TypeQuals would be set to @c
1997  /// QualType::Const for the following copy constructor:
1998  ///
1999  /// @code
2000  /// class X {
2001  /// public:
2002  ///   X(const X&);
2003  /// };
2004  /// @endcode
2005  bool isCopyConstructor(unsigned &TypeQuals) const;
2006
2007  /// isCopyConstructor - Whether this constructor is a copy
2008  /// constructor (C++ [class.copy]p2, which can be used to copy the
2009  /// class.
2010  bool isCopyConstructor() const {
2011    unsigned TypeQuals = 0;
2012    return isCopyConstructor(TypeQuals);
2013  }
2014
2015  /// \brief Determine whether this constructor is a move constructor
2016  /// (C++0x [class.copy]p3), which can be used to move values of the class.
2017  ///
2018  /// \param TypeQuals If this constructor is a move constructor, will be set
2019  /// to the type qualifiers on the referent of the first parameter's type.
2020  bool isMoveConstructor(unsigned &TypeQuals) const;
2021
2022  /// \brief Determine whether this constructor is a move constructor
2023  /// (C++0x [class.copy]p3), which can be used to move values of the class.
2024  bool isMoveConstructor() const {
2025    unsigned TypeQuals = 0;
2026    return isMoveConstructor(TypeQuals);
2027  }
2028
2029  /// \brief Determine whether this is a copy or move constructor.
2030  ///
2031  /// \param TypeQuals Will be set to the type qualifiers on the reference
2032  /// parameter, if in fact this is a copy or move constructor.
2033  bool isCopyOrMoveConstructor(unsigned &TypeQuals) const;
2034
2035  /// \brief Determine whether this a copy or move constructor.
2036  bool isCopyOrMoveConstructor() const {
2037    unsigned Quals;
2038    return isCopyOrMoveConstructor(Quals);
2039  }
2040
2041  /// isConvertingConstructor - Whether this constructor is a
2042  /// converting constructor (C++ [class.conv.ctor]), which can be
2043  /// used for user-defined conversions.
2044  bool isConvertingConstructor(bool AllowExplicit) const;
2045
2046  /// \brief Determine whether this is a member template specialization that
2047  /// would copy the object to itself. Such constructors are never used to copy
2048  /// an object.
2049  bool isSpecializationCopyingObject() const;
2050
2051  /// \brief Get the constructor that this inheriting constructor is based on.
2052  const CXXConstructorDecl *getInheritedConstructor() const;
2053
2054  /// \brief Set the constructor that this inheriting constructor is based on.
2055  void setInheritedConstructor(const CXXConstructorDecl *BaseCtor);
2056
2057  const CXXConstructorDecl *getCanonicalDecl() const {
2058    return cast<CXXConstructorDecl>(FunctionDecl::getCanonicalDecl());
2059  }
2060  CXXConstructorDecl *getCanonicalDecl() {
2061    return cast<CXXConstructorDecl>(FunctionDecl::getCanonicalDecl());
2062  }
2063
2064  // Implement isa/cast/dyncast/etc.
2065  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2066  static bool classof(const CXXConstructorDecl *D) { return true; }
2067  static bool classofKind(Kind K) { return K == CXXConstructor; }
2068
2069  friend class ASTDeclReader;
2070  friend class ASTDeclWriter;
2071};
2072
2073/// CXXDestructorDecl - Represents a C++ destructor within a
2074/// class. For example:
2075///
2076/// @code
2077/// class X {
2078/// public:
2079///   ~X(); // represented by a CXXDestructorDecl.
2080/// };
2081/// @endcode
2082class CXXDestructorDecl : public CXXMethodDecl {
2083  virtual void anchor();
2084  /// ImplicitlyDefined - Whether this destructor was implicitly
2085  /// defined by the compiler. When false, the destructor was defined
2086  /// by the user. In C++03, this flag will have the same value as
2087  /// Implicit. In C++0x, however, a destructor that is
2088  /// explicitly defaulted (i.e., defined with " = default") will have
2089  /// @c !Implicit && ImplicitlyDefined.
2090  bool ImplicitlyDefined : 1;
2091
2092  FunctionDecl *OperatorDelete;
2093
2094  CXXDestructorDecl(CXXRecordDecl *RD, SourceLocation StartLoc,
2095                    const DeclarationNameInfo &NameInfo,
2096                    QualType T, TypeSourceInfo *TInfo,
2097                    bool isInline, bool isImplicitlyDeclared)
2098    : CXXMethodDecl(CXXDestructor, RD, StartLoc, NameInfo, T, TInfo, false,
2099                    SC_None, isInline, /*isConstexpr=*/false, SourceLocation()),
2100      ImplicitlyDefined(false), OperatorDelete(0) {
2101    setImplicit(isImplicitlyDeclared);
2102  }
2103
2104public:
2105  static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
2106                                   SourceLocation StartLoc,
2107                                   const DeclarationNameInfo &NameInfo,
2108                                   QualType T, TypeSourceInfo* TInfo,
2109                                   bool isInline,
2110                                   bool isImplicitlyDeclared);
2111  static CXXDestructorDecl *CreateDeserialized(ASTContext & C, unsigned ID);
2112
2113  /// isImplicitlyDefined - Whether this destructor was implicitly
2114  /// defined. If false, then this destructor was defined by the
2115  /// user. This operation can only be invoked if the destructor has
2116  /// already been defined.
2117  bool isImplicitlyDefined() const {
2118    assert(isThisDeclarationADefinition() &&
2119           "Can only get the implicit-definition flag once the destructor has "
2120           "been defined");
2121    return ImplicitlyDefined;
2122  }
2123
2124  /// setImplicitlyDefined - Set whether this destructor was
2125  /// implicitly defined or not.
2126  void setImplicitlyDefined(bool ID) {
2127    assert(isThisDeclarationADefinition() &&
2128           "Can only set the implicit-definition flag once the destructor has "
2129           "been defined");
2130    ImplicitlyDefined = ID;
2131  }
2132
2133  void setOperatorDelete(FunctionDecl *OD) { OperatorDelete = OD; }
2134  const FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
2135
2136  // Implement isa/cast/dyncast/etc.
2137  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2138  static bool classof(const CXXDestructorDecl *D) { return true; }
2139  static bool classofKind(Kind K) { return K == CXXDestructor; }
2140
2141  friend class ASTDeclReader;
2142  friend class ASTDeclWriter;
2143};
2144
2145/// CXXConversionDecl - Represents a C++ conversion function within a
2146/// class. For example:
2147///
2148/// @code
2149/// class X {
2150/// public:
2151///   operator bool();
2152/// };
2153/// @endcode
2154class CXXConversionDecl : public CXXMethodDecl {
2155  virtual void anchor();
2156  /// IsExplicitSpecified - Whether this conversion function declaration is
2157  /// marked "explicit", meaning that it can only be applied when the user
2158  /// explicitly wrote a cast. This is a C++0x feature.
2159  bool IsExplicitSpecified : 1;
2160
2161  CXXConversionDecl(CXXRecordDecl *RD, SourceLocation StartLoc,
2162                    const DeclarationNameInfo &NameInfo,
2163                    QualType T, TypeSourceInfo *TInfo,
2164                    bool isInline, bool isExplicitSpecified,
2165                    bool isConstexpr, SourceLocation EndLocation)
2166    : CXXMethodDecl(CXXConversion, RD, StartLoc, NameInfo, T, TInfo, false,
2167                    SC_None, isInline, isConstexpr, EndLocation),
2168      IsExplicitSpecified(isExplicitSpecified) { }
2169
2170public:
2171  static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD,
2172                                   SourceLocation StartLoc,
2173                                   const DeclarationNameInfo &NameInfo,
2174                                   QualType T, TypeSourceInfo *TInfo,
2175                                   bool isInline, bool isExplicit,
2176                                   bool isConstexpr,
2177                                   SourceLocation EndLocation);
2178  static CXXConversionDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2179
2180  /// IsExplicitSpecified - Whether this conversion function declaration is
2181  /// marked "explicit", meaning that it can only be applied when the user
2182  /// explicitly wrote a cast. This is a C++0x feature.
2183  bool isExplicitSpecified() const { return IsExplicitSpecified; }
2184
2185  /// isExplicit - Whether this is an explicit conversion operator
2186  /// (C++0x only). Explicit conversion operators are only considered
2187  /// when the user has explicitly written a cast.
2188  bool isExplicit() const {
2189    return cast<CXXConversionDecl>(getFirstDeclaration())
2190      ->isExplicitSpecified();
2191  }
2192
2193  /// getConversionType - Returns the type that this conversion
2194  /// function is converting to.
2195  QualType getConversionType() const {
2196    return getType()->getAs<FunctionType>()->getResultType();
2197  }
2198
2199  /// \brief Determine whether this conversion function is a conversion from
2200  /// a lambda closure type to a block pointer.
2201  bool isLambdaToBlockPointerConversion() const;
2202
2203  /// \brief For an implicit conversion function that converts a lambda
2204  /// closure type to a block pointer, retrieve the expression used to
2205  /// copy the closure object into the block.
2206  Expr *getLambdaToBlockPointerCopyInit() const;
2207
2208  /// \brief Set the copy-initialization expression to be used when converting
2209  /// a lambda object to a block pointer.
2210  void setLambdaToBlockPointerCopyInit(Expr *Init);
2211
2212  // Implement isa/cast/dyncast/etc.
2213  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2214  static bool classof(const CXXConversionDecl *D) { return true; }
2215  static bool classofKind(Kind K) { return K == CXXConversion; }
2216
2217  friend class ASTDeclReader;
2218  friend class ASTDeclWriter;
2219};
2220
2221/// LinkageSpecDecl - This represents a linkage specification.  For example:
2222///   extern "C" void foo();
2223///
2224class LinkageSpecDecl : public Decl, public DeclContext {
2225  virtual void anchor();
2226public:
2227  /// LanguageIDs - Used to represent the language in a linkage
2228  /// specification.  The values are part of the serialization abi for
2229  /// ASTs and cannot be changed without altering that abi.  To help
2230  /// ensure a stable abi for this, we choose the DW_LANG_ encodings
2231  /// from the dwarf standard.
2232  enum LanguageIDs {
2233    lang_c = /* DW_LANG_C */ 0x0002,
2234    lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004
2235  };
2236private:
2237  /// Language - The language for this linkage specification.
2238  LanguageIDs Language;
2239  /// ExternLoc - The source location for the extern keyword.
2240  SourceLocation ExternLoc;
2241  /// RBraceLoc - The source location for the right brace (if valid).
2242  SourceLocation RBraceLoc;
2243
2244  LinkageSpecDecl(DeclContext *DC, SourceLocation ExternLoc,
2245                  SourceLocation LangLoc, LanguageIDs lang,
2246                  SourceLocation RBLoc)
2247    : Decl(LinkageSpec, DC, LangLoc), DeclContext(LinkageSpec),
2248      Language(lang), ExternLoc(ExternLoc), RBraceLoc(RBLoc) { }
2249
2250public:
2251  static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC,
2252                                 SourceLocation ExternLoc,
2253                                 SourceLocation LangLoc, LanguageIDs Lang,
2254                                 SourceLocation RBraceLoc = SourceLocation());
2255  static LinkageSpecDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2256
2257  /// \brief Return the language specified by this linkage specification.
2258  LanguageIDs getLanguage() const { return Language; }
2259  /// \brief Set the language specified by this linkage specification.
2260  void setLanguage(LanguageIDs L) { Language = L; }
2261
2262  /// \brief Determines whether this linkage specification had braces in
2263  /// its syntactic form.
2264  bool hasBraces() const { return RBraceLoc.isValid(); }
2265
2266  SourceLocation getExternLoc() const { return ExternLoc; }
2267  SourceLocation getRBraceLoc() const { return RBraceLoc; }
2268  void setExternLoc(SourceLocation L) { ExternLoc = L; }
2269  void setRBraceLoc(SourceLocation L) { RBraceLoc = L; }
2270
2271  SourceLocation getLocEnd() const {
2272    if (hasBraces())
2273      return getRBraceLoc();
2274    // No braces: get the end location of the (only) declaration in context
2275    // (if present).
2276    return decls_empty() ? getLocation() : decls_begin()->getLocEnd();
2277  }
2278
2279  SourceRange getSourceRange() const {
2280    return SourceRange(ExternLoc, getLocEnd());
2281  }
2282
2283  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2284  static bool classof(const LinkageSpecDecl *D) { return true; }
2285  static bool classofKind(Kind K) { return K == LinkageSpec; }
2286  static DeclContext *castToDeclContext(const LinkageSpecDecl *D) {
2287    return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D));
2288  }
2289  static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) {
2290    return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC));
2291  }
2292};
2293
2294/// UsingDirectiveDecl - Represents C++ using-directive. For example:
2295///
2296///    using namespace std;
2297///
2298// NB: UsingDirectiveDecl should be Decl not NamedDecl, but we provide
2299// artificial names for all using-directives in order to store
2300// them in DeclContext effectively.
2301class UsingDirectiveDecl : public NamedDecl {
2302  virtual void anchor();
2303  /// \brief The location of the "using" keyword.
2304  SourceLocation UsingLoc;
2305
2306  /// SourceLocation - Location of 'namespace' token.
2307  SourceLocation NamespaceLoc;
2308
2309  /// \brief The nested-name-specifier that precedes the namespace.
2310  NestedNameSpecifierLoc QualifierLoc;
2311
2312  /// NominatedNamespace - Namespace nominated by using-directive.
2313  NamedDecl *NominatedNamespace;
2314
2315  /// Enclosing context containing both using-directive and nominated
2316  /// namespace.
2317  DeclContext *CommonAncestor;
2318
2319  /// getUsingDirectiveName - Returns special DeclarationName used by
2320  /// using-directives. This is only used by DeclContext for storing
2321  /// UsingDirectiveDecls in its lookup structure.
2322  static DeclarationName getName() {
2323    return DeclarationName::getUsingDirectiveName();
2324  }
2325
2326  UsingDirectiveDecl(DeclContext *DC, SourceLocation UsingLoc,
2327                     SourceLocation NamespcLoc,
2328                     NestedNameSpecifierLoc QualifierLoc,
2329                     SourceLocation IdentLoc,
2330                     NamedDecl *Nominated,
2331                     DeclContext *CommonAncestor)
2332    : NamedDecl(UsingDirective, DC, IdentLoc, getName()), UsingLoc(UsingLoc),
2333      NamespaceLoc(NamespcLoc), QualifierLoc(QualifierLoc),
2334      NominatedNamespace(Nominated), CommonAncestor(CommonAncestor) { }
2335
2336public:
2337  /// \brief Retrieve the nested-name-specifier that qualifies the
2338  /// name of the namespace, with source-location information.
2339  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2340
2341  /// \brief Retrieve the nested-name-specifier that qualifies the
2342  /// name of the namespace.
2343  NestedNameSpecifier *getQualifier() const {
2344    return QualifierLoc.getNestedNameSpecifier();
2345  }
2346
2347  NamedDecl *getNominatedNamespaceAsWritten() { return NominatedNamespace; }
2348  const NamedDecl *getNominatedNamespaceAsWritten() const {
2349    return NominatedNamespace;
2350  }
2351
2352  /// getNominatedNamespace - Returns namespace nominated by using-directive.
2353  NamespaceDecl *getNominatedNamespace();
2354
2355  const NamespaceDecl *getNominatedNamespace() const {
2356    return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace();
2357  }
2358
2359  /// \brief Returns the common ancestor context of this using-directive and
2360  /// its nominated namespace.
2361  DeclContext *getCommonAncestor() { return CommonAncestor; }
2362  const DeclContext *getCommonAncestor() const { return CommonAncestor; }
2363
2364  /// \brief Return the location of the "using" keyword.
2365  SourceLocation getUsingLoc() const { return UsingLoc; }
2366
2367  // FIXME: Could omit 'Key' in name.
2368  /// getNamespaceKeyLocation - Returns location of namespace keyword.
2369  SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; }
2370
2371  /// getIdentLocation - Returns location of identifier.
2372  SourceLocation getIdentLocation() const { return getLocation(); }
2373
2374  static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC,
2375                                    SourceLocation UsingLoc,
2376                                    SourceLocation NamespaceLoc,
2377                                    NestedNameSpecifierLoc QualifierLoc,
2378                                    SourceLocation IdentLoc,
2379                                    NamedDecl *Nominated,
2380                                    DeclContext *CommonAncestor);
2381  static UsingDirectiveDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2382
2383  SourceRange getSourceRange() const {
2384    return SourceRange(UsingLoc, getLocation());
2385  }
2386
2387  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2388  static bool classof(const UsingDirectiveDecl *D) { return true; }
2389  static bool classofKind(Kind K) { return K == UsingDirective; }
2390
2391  // Friend for getUsingDirectiveName.
2392  friend class DeclContext;
2393
2394  friend class ASTDeclReader;
2395};
2396
2397/// NamespaceAliasDecl - Represents a C++ namespace alias. For example:
2398///
2399/// @code
2400/// namespace Foo = Bar;
2401/// @endcode
2402class NamespaceAliasDecl : public NamedDecl {
2403  virtual void anchor();
2404
2405  /// \brief The location of the "namespace" keyword.
2406  SourceLocation NamespaceLoc;
2407
2408  /// IdentLoc - Location of namespace identifier. Accessed by TargetNameLoc.
2409  SourceLocation IdentLoc;
2410
2411  /// \brief The nested-name-specifier that precedes the namespace.
2412  NestedNameSpecifierLoc QualifierLoc;
2413
2414  /// Namespace - The Decl that this alias points to. Can either be a
2415  /// NamespaceDecl or a NamespaceAliasDecl.
2416  NamedDecl *Namespace;
2417
2418  NamespaceAliasDecl(DeclContext *DC, SourceLocation NamespaceLoc,
2419                     SourceLocation AliasLoc, IdentifierInfo *Alias,
2420                     NestedNameSpecifierLoc QualifierLoc,
2421                     SourceLocation IdentLoc, NamedDecl *Namespace)
2422    : NamedDecl(NamespaceAlias, DC, AliasLoc, Alias),
2423      NamespaceLoc(NamespaceLoc), IdentLoc(IdentLoc),
2424      QualifierLoc(QualifierLoc), Namespace(Namespace) { }
2425
2426  friend class ASTDeclReader;
2427
2428public:
2429  /// \brief Retrieve the nested-name-specifier that qualifies the
2430  /// name of the namespace, with source-location information.
2431  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2432
2433  /// \brief Retrieve the nested-name-specifier that qualifies the
2434  /// name of the namespace.
2435  NestedNameSpecifier *getQualifier() const {
2436    return QualifierLoc.getNestedNameSpecifier();
2437  }
2438
2439  /// \brief Retrieve the namespace declaration aliased by this directive.
2440  NamespaceDecl *getNamespace() {
2441    if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(Namespace))
2442      return AD->getNamespace();
2443
2444    return cast<NamespaceDecl>(Namespace);
2445  }
2446
2447  const NamespaceDecl *getNamespace() const {
2448    return const_cast<NamespaceAliasDecl*>(this)->getNamespace();
2449  }
2450
2451  /// Returns the location of the alias name, i.e. 'foo' in
2452  /// "namespace foo = ns::bar;".
2453  SourceLocation getAliasLoc() const { return getLocation(); }
2454
2455  /// Returns the location of the 'namespace' keyword.
2456  SourceLocation getNamespaceLoc() const { return NamespaceLoc; }
2457
2458  /// Returns the location of the identifier in the named namespace.
2459  SourceLocation getTargetNameLoc() const { return IdentLoc; }
2460
2461  /// \brief Retrieve the namespace that this alias refers to, which
2462  /// may either be a NamespaceDecl or a NamespaceAliasDecl.
2463  NamedDecl *getAliasedNamespace() const { return Namespace; }
2464
2465  static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC,
2466                                    SourceLocation NamespaceLoc,
2467                                    SourceLocation AliasLoc,
2468                                    IdentifierInfo *Alias,
2469                                    NestedNameSpecifierLoc QualifierLoc,
2470                                    SourceLocation IdentLoc,
2471                                    NamedDecl *Namespace);
2472
2473  static NamespaceAliasDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2474
2475  virtual SourceRange getSourceRange() const {
2476    return SourceRange(NamespaceLoc, IdentLoc);
2477  }
2478
2479  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2480  static bool classof(const NamespaceAliasDecl *D) { return true; }
2481  static bool classofKind(Kind K) { return K == NamespaceAlias; }
2482};
2483
2484/// UsingShadowDecl - Represents a shadow declaration introduced into
2485/// a scope by a (resolved) using declaration.  For example,
2486///
2487/// namespace A {
2488///   void foo();
2489/// }
2490/// namespace B {
2491///   using A::foo(); // <- a UsingDecl
2492///                   // Also creates a UsingShadowDecl for A::foo in B
2493/// }
2494///
2495class UsingShadowDecl : public NamedDecl {
2496  virtual void anchor();
2497
2498  /// The referenced declaration.
2499  NamedDecl *Underlying;
2500
2501  /// \brief The using declaration which introduced this decl or the next using
2502  /// shadow declaration contained in the aforementioned using declaration.
2503  NamedDecl *UsingOrNextShadow;
2504  friend class UsingDecl;
2505
2506  UsingShadowDecl(DeclContext *DC, SourceLocation Loc, UsingDecl *Using,
2507                  NamedDecl *Target)
2508    : NamedDecl(UsingShadow, DC, Loc, DeclarationName()),
2509      Underlying(Target),
2510      UsingOrNextShadow(reinterpret_cast<NamedDecl *>(Using)) {
2511    if (Target) {
2512      setDeclName(Target->getDeclName());
2513      IdentifierNamespace = Target->getIdentifierNamespace();
2514    }
2515    setImplicit();
2516  }
2517
2518public:
2519  static UsingShadowDecl *Create(ASTContext &C, DeclContext *DC,
2520                                 SourceLocation Loc, UsingDecl *Using,
2521                                 NamedDecl *Target) {
2522    return new (C) UsingShadowDecl(DC, Loc, Using, Target);
2523  }
2524
2525  static UsingShadowDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2526
2527  /// \brief Gets the underlying declaration which has been brought into the
2528  /// local scope.
2529  NamedDecl *getTargetDecl() const { return Underlying; }
2530
2531  /// \brief Sets the underlying declaration which has been brought into the
2532  /// local scope.
2533  void setTargetDecl(NamedDecl* ND) {
2534    assert(ND && "Target decl is null!");
2535    Underlying = ND;
2536    IdentifierNamespace = ND->getIdentifierNamespace();
2537  }
2538
2539  /// \brief Gets the using declaration to which this declaration is tied.
2540  UsingDecl *getUsingDecl() const;
2541
2542  /// \brief The next using shadow declaration contained in the shadow decl
2543  /// chain of the using declaration which introduced this decl.
2544  UsingShadowDecl *getNextUsingShadowDecl() const {
2545    return dyn_cast_or_null<UsingShadowDecl>(UsingOrNextShadow);
2546  }
2547
2548  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2549  static bool classof(const UsingShadowDecl *D) { return true; }
2550  static bool classofKind(Kind K) { return K == Decl::UsingShadow; }
2551
2552  friend class ASTDeclReader;
2553  friend class ASTDeclWriter;
2554};
2555
2556/// UsingDecl - Represents a C++ using-declaration. For example:
2557///    using someNameSpace::someIdentifier;
2558class UsingDecl : public NamedDecl {
2559  virtual void anchor();
2560
2561  /// \brief The source location of the "using" location itself.
2562  SourceLocation UsingLocation;
2563
2564  /// \brief The nested-name-specifier that precedes the name.
2565  NestedNameSpecifierLoc QualifierLoc;
2566
2567  /// DNLoc - Provides source/type location info for the
2568  /// declaration name embedded in the ValueDecl base class.
2569  DeclarationNameLoc DNLoc;
2570
2571  /// \brief The first shadow declaration of the shadow decl chain associated
2572  /// with this using declaration. The bool member of the pair store whether
2573  /// this decl has the 'typename' keyword.
2574  llvm::PointerIntPair<UsingShadowDecl *, 1, bool> FirstUsingShadow;
2575
2576  UsingDecl(DeclContext *DC, SourceLocation UL,
2577            NestedNameSpecifierLoc QualifierLoc,
2578            const DeclarationNameInfo &NameInfo, bool IsTypeNameArg)
2579    : NamedDecl(Using, DC, NameInfo.getLoc(), NameInfo.getName()),
2580      UsingLocation(UL), QualifierLoc(QualifierLoc),
2581      DNLoc(NameInfo.getInfo()), FirstUsingShadow(0, IsTypeNameArg) {
2582  }
2583
2584public:
2585  /// \brief Returns the source location of the "using" keyword.
2586  SourceLocation getUsingLocation() const { return UsingLocation; }
2587
2588  /// \brief Set the source location of the 'using' keyword.
2589  void setUsingLocation(SourceLocation L) { UsingLocation = L; }
2590
2591  /// \brief Retrieve the nested-name-specifier that qualifies the name,
2592  /// with source-location information.
2593  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2594
2595  /// \brief Retrieve the nested-name-specifier that qualifies the name.
2596  NestedNameSpecifier *getQualifier() const {
2597    return QualifierLoc.getNestedNameSpecifier();
2598  }
2599
2600  DeclarationNameInfo getNameInfo() const {
2601    return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc);
2602  }
2603
2604  /// \brief Return true if the using declaration has 'typename'.
2605  bool isTypeName() const { return FirstUsingShadow.getInt(); }
2606
2607  /// \brief Sets whether the using declaration has 'typename'.
2608  void setTypeName(bool TN) { FirstUsingShadow.setInt(TN); }
2609
2610  /// \brief Iterates through the using shadow declarations assosiated with
2611  /// this using declaration.
2612  class shadow_iterator {
2613    /// \brief The current using shadow declaration.
2614    UsingShadowDecl *Current;
2615
2616  public:
2617    typedef UsingShadowDecl*          value_type;
2618    typedef UsingShadowDecl*          reference;
2619    typedef UsingShadowDecl*          pointer;
2620    typedef std::forward_iterator_tag iterator_category;
2621    typedef std::ptrdiff_t            difference_type;
2622
2623    shadow_iterator() : Current(0) { }
2624    explicit shadow_iterator(UsingShadowDecl *C) : Current(C) { }
2625
2626    reference operator*() const { return Current; }
2627    pointer operator->() const { return Current; }
2628
2629    shadow_iterator& operator++() {
2630      Current = Current->getNextUsingShadowDecl();
2631      return *this;
2632    }
2633
2634    shadow_iterator operator++(int) {
2635      shadow_iterator tmp(*this);
2636      ++(*this);
2637      return tmp;
2638    }
2639
2640    friend bool operator==(shadow_iterator x, shadow_iterator y) {
2641      return x.Current == y.Current;
2642    }
2643    friend bool operator!=(shadow_iterator x, shadow_iterator y) {
2644      return x.Current != y.Current;
2645    }
2646  };
2647
2648  shadow_iterator shadow_begin() const {
2649    return shadow_iterator(FirstUsingShadow.getPointer());
2650  }
2651  shadow_iterator shadow_end() const { return shadow_iterator(); }
2652
2653  /// \brief Return the number of shadowed declarations associated with this
2654  /// using declaration.
2655  unsigned shadow_size() const {
2656    return std::distance(shadow_begin(), shadow_end());
2657  }
2658
2659  void addShadowDecl(UsingShadowDecl *S);
2660  void removeShadowDecl(UsingShadowDecl *S);
2661
2662  static UsingDecl *Create(ASTContext &C, DeclContext *DC,
2663                           SourceLocation UsingL,
2664                           NestedNameSpecifierLoc QualifierLoc,
2665                           const DeclarationNameInfo &NameInfo,
2666                           bool IsTypeNameArg);
2667
2668  static UsingDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2669
2670  SourceRange getSourceRange() const {
2671    return SourceRange(UsingLocation, getNameInfo().getEndLoc());
2672  }
2673
2674  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2675  static bool classof(const UsingDecl *D) { return true; }
2676  static bool classofKind(Kind K) { return K == Using; }
2677
2678  friend class ASTDeclReader;
2679  friend class ASTDeclWriter;
2680};
2681
2682/// UnresolvedUsingValueDecl - Represents a dependent using
2683/// declaration which was not marked with 'typename'.  Unlike
2684/// non-dependent using declarations, these *only* bring through
2685/// non-types; otherwise they would break two-phase lookup.
2686///
2687/// template <class T> class A : public Base<T> {
2688///   using Base<T>::foo;
2689/// };
2690class UnresolvedUsingValueDecl : public ValueDecl {
2691  virtual void anchor();
2692
2693  /// \brief The source location of the 'using' keyword
2694  SourceLocation UsingLocation;
2695
2696  /// \brief The nested-name-specifier that precedes the name.
2697  NestedNameSpecifierLoc QualifierLoc;
2698
2699  /// DNLoc - Provides source/type location info for the
2700  /// declaration name embedded in the ValueDecl base class.
2701  DeclarationNameLoc DNLoc;
2702
2703  UnresolvedUsingValueDecl(DeclContext *DC, QualType Ty,
2704                           SourceLocation UsingLoc,
2705                           NestedNameSpecifierLoc QualifierLoc,
2706                           const DeclarationNameInfo &NameInfo)
2707    : ValueDecl(UnresolvedUsingValue, DC,
2708                NameInfo.getLoc(), NameInfo.getName(), Ty),
2709      UsingLocation(UsingLoc), QualifierLoc(QualifierLoc),
2710      DNLoc(NameInfo.getInfo())
2711  { }
2712
2713public:
2714  /// \brief Returns the source location of the 'using' keyword.
2715  SourceLocation getUsingLoc() const { return UsingLocation; }
2716
2717  /// \brief Set the source location of the 'using' keyword.
2718  void setUsingLoc(SourceLocation L) { UsingLocation = L; }
2719
2720  /// \brief Retrieve the nested-name-specifier that qualifies the name,
2721  /// with source-location information.
2722  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2723
2724  /// \brief Retrieve the nested-name-specifier that qualifies the name.
2725  NestedNameSpecifier *getQualifier() const {
2726    return QualifierLoc.getNestedNameSpecifier();
2727  }
2728
2729  DeclarationNameInfo getNameInfo() const {
2730    return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc);
2731  }
2732
2733  static UnresolvedUsingValueDecl *
2734    Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc,
2735           NestedNameSpecifierLoc QualifierLoc,
2736           const DeclarationNameInfo &NameInfo);
2737
2738  static UnresolvedUsingValueDecl *
2739  CreateDeserialized(ASTContext &C, unsigned ID);
2740
2741  SourceRange getSourceRange() const {
2742    return SourceRange(UsingLocation, getNameInfo().getEndLoc());
2743  }
2744
2745  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2746  static bool classof(const UnresolvedUsingValueDecl *D) { return true; }
2747  static bool classofKind(Kind K) { return K == UnresolvedUsingValue; }
2748
2749  friend class ASTDeclReader;
2750  friend class ASTDeclWriter;
2751};
2752
2753/// UnresolvedUsingTypenameDecl - Represents a dependent using
2754/// declaration which was marked with 'typename'.
2755///
2756/// template <class T> class A : public Base<T> {
2757///   using typename Base<T>::foo;
2758/// };
2759///
2760/// The type associated with a unresolved using typename decl is
2761/// currently always a typename type.
2762class UnresolvedUsingTypenameDecl : public TypeDecl {
2763  virtual void anchor();
2764
2765  /// \brief The source location of the 'using' keyword
2766  SourceLocation UsingLocation;
2767
2768  /// \brief The source location of the 'typename' keyword
2769  SourceLocation TypenameLocation;
2770
2771  /// \brief The nested-name-specifier that precedes the name.
2772  NestedNameSpecifierLoc QualifierLoc;
2773
2774  UnresolvedUsingTypenameDecl(DeclContext *DC, SourceLocation UsingLoc,
2775                              SourceLocation TypenameLoc,
2776                              NestedNameSpecifierLoc QualifierLoc,
2777                              SourceLocation TargetNameLoc,
2778                              IdentifierInfo *TargetName)
2779    : TypeDecl(UnresolvedUsingTypename, DC, TargetNameLoc, TargetName,
2780               UsingLoc),
2781      TypenameLocation(TypenameLoc), QualifierLoc(QualifierLoc) { }
2782
2783  friend class ASTDeclReader;
2784
2785public:
2786  /// \brief Returns the source location of the 'using' keyword.
2787  SourceLocation getUsingLoc() const { return getLocStart(); }
2788
2789  /// \brief Returns the source location of the 'typename' keyword.
2790  SourceLocation getTypenameLoc() const { return TypenameLocation; }
2791
2792  /// \brief Retrieve the nested-name-specifier that qualifies the name,
2793  /// with source-location information.
2794  NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2795
2796  /// \brief Retrieve the nested-name-specifier that qualifies the name.
2797  NestedNameSpecifier *getQualifier() const {
2798    return QualifierLoc.getNestedNameSpecifier();
2799  }
2800
2801  static UnresolvedUsingTypenameDecl *
2802    Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc,
2803           SourceLocation TypenameLoc, NestedNameSpecifierLoc QualifierLoc,
2804           SourceLocation TargetNameLoc, DeclarationName TargetName);
2805
2806  static UnresolvedUsingTypenameDecl *
2807  CreateDeserialized(ASTContext &C, unsigned ID);
2808
2809  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2810  static bool classof(const UnresolvedUsingTypenameDecl *D) { return true; }
2811  static bool classofKind(Kind K) { return K == UnresolvedUsingTypename; }
2812};
2813
2814/// StaticAssertDecl - Represents a C++0x static_assert declaration.
2815class StaticAssertDecl : public Decl {
2816  virtual void anchor();
2817  Expr *AssertExpr;
2818  StringLiteral *Message;
2819  SourceLocation RParenLoc;
2820
2821  StaticAssertDecl(DeclContext *DC, SourceLocation StaticAssertLoc,
2822                   Expr *assertexpr, StringLiteral *message,
2823                   SourceLocation RParenLoc)
2824  : Decl(StaticAssert, DC, StaticAssertLoc), AssertExpr(assertexpr),
2825    Message(message), RParenLoc(RParenLoc) { }
2826
2827public:
2828  static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC,
2829                                  SourceLocation StaticAssertLoc,
2830                                  Expr *AssertExpr, StringLiteral *Message,
2831                                  SourceLocation RParenLoc);
2832  static StaticAssertDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2833
2834  Expr *getAssertExpr() { return AssertExpr; }
2835  const Expr *getAssertExpr() const { return AssertExpr; }
2836
2837  StringLiteral *getMessage() { return Message; }
2838  const StringLiteral *getMessage() const { return Message; }
2839
2840  SourceLocation getRParenLoc() const { return RParenLoc; }
2841  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2842
2843  SourceRange getSourceRange() const {
2844    return SourceRange(getLocation(), getRParenLoc());
2845  }
2846
2847  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2848  static bool classof(StaticAssertDecl *D) { return true; }
2849  static bool classofKind(Kind K) { return K == StaticAssert; }
2850
2851  friend class ASTDeclReader;
2852};
2853
2854/// Insertion operator for diagnostics.  This allows sending AccessSpecifier's
2855/// into a diagnostic with <<.
2856const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
2857                                    AccessSpecifier AS);
2858
2859const PartialDiagnostic &operator<<(const PartialDiagnostic &DB,
2860                                    AccessSpecifier AS);
2861
2862} // end namespace clang
2863
2864#endif
2865