Home | History | Annotate | Download | only in AST
      1 //===------ CXXInheritance.h - C++ Inheritance ------------------*- 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 provides routines that help analyzing C++ inheritance hierarchies.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_CLANG_AST_CXXINHERITANCE_H
     15 #define LLVM_CLANG_AST_CXXINHERITANCE_H
     16 
     17 #include "clang/AST/DeclarationName.h"
     18 #include "clang/AST/DeclBase.h"
     19 #include "clang/AST/DeclCXX.h"
     20 #include "clang/AST/Type.h"
     21 #include "clang/AST/TypeOrdering.h"
     22 #include "llvm/ADT/DenseMap.h"
     23 #include "llvm/ADT/SmallSet.h"
     24 #include "llvm/ADT/SmallVector.h"
     25 #include <list>
     26 #include <map>
     27 #include <cassert>
     28 
     29 namespace clang {
     30 
     31 class CXXBaseSpecifier;
     32 class CXXMethodDecl;
     33 class CXXRecordDecl;
     34 class NamedDecl;
     35 
     36 /// \brief Represents an element in a path from a derived class to a
     37 /// base class.
     38 ///
     39 /// Each step in the path references the link from a
     40 /// derived class to one of its direct base classes, along with a
     41 /// base "number" that identifies which base subobject of the
     42 /// original derived class we are referencing.
     43 struct CXXBasePathElement {
     44   /// \brief The base specifier that states the link from a derived
     45   /// class to a base class, which will be followed by this base
     46   /// path element.
     47   const CXXBaseSpecifier *Base;
     48 
     49   /// \brief The record decl of the class that the base is a base of.
     50   const CXXRecordDecl *Class;
     51 
     52   /// \brief Identifies which base class subobject (of type
     53   /// \c Base->getType()) this base path element refers to.
     54   ///
     55   /// This value is only valid if \c !Base->isVirtual(), because there
     56   /// is no base numbering for the zero or one virtual bases of a
     57   /// given type.
     58   int SubobjectNumber;
     59 };
     60 
     61 /// \brief Represents a path from a specific derived class
     62 /// (which is not represented as part of the path) to a particular
     63 /// (direct or indirect) base class subobject.
     64 ///
     65 /// Individual elements in the path are described by the \c CXXBasePathElement
     66 /// structure, which captures both the link from a derived class to one of its
     67 /// direct bases and identification describing which base class
     68 /// subobject is being used.
     69 class CXXBasePath : public llvm::SmallVector<CXXBasePathElement, 4> {
     70 public:
     71   CXXBasePath() : Access(AS_public) {}
     72 
     73   /// \brief The access along this inheritance path.  This is only
     74   /// calculated when recording paths.  AS_none is a special value
     75   /// used to indicate a path which permits no legal access.
     76   AccessSpecifier Access;
     77 
     78   /// \brief The set of declarations found inside this base class
     79   /// subobject.
     80   DeclContext::lookup_result Decls;
     81 
     82   void clear() {
     83     llvm::SmallVectorImpl<CXXBasePathElement>::clear();
     84     Access = AS_public;
     85   }
     86 };
     87 
     88 /// BasePaths - Represents the set of paths from a derived class to
     89 /// one of its (direct or indirect) bases. For example, given the
     90 /// following class hierarchy:
     91 ///
     92 /// @code
     93 /// class A { };
     94 /// class B : public A { };
     95 /// class C : public A { };
     96 /// class D : public B, public C{ };
     97 /// @endcode
     98 ///
     99 /// There are two potential BasePaths to represent paths from D to a
    100 /// base subobject of type A. One path is (D,0) -> (B,0) -> (A,0)
    101 /// and another is (D,0)->(C,0)->(A,1). These two paths actually
    102 /// refer to two different base class subobjects of the same type,
    103 /// so the BasePaths object refers to an ambiguous path. On the
    104 /// other hand, consider the following class hierarchy:
    105 ///
    106 /// @code
    107 /// class A { };
    108 /// class B : public virtual A { };
    109 /// class C : public virtual A { };
    110 /// class D : public B, public C{ };
    111 /// @endcode
    112 ///
    113 /// Here, there are two potential BasePaths again, (D, 0) -> (B, 0)
    114 /// -> (A,v) and (D, 0) -> (C, 0) -> (A, v), but since both of them
    115 /// refer to the same base class subobject of type A (the virtual
    116 /// one), there is no ambiguity.
    117 class CXXBasePaths {
    118   /// \brief The type from which this search originated.
    119   CXXRecordDecl *Origin;
    120 
    121   /// Paths - The actual set of paths that can be taken from the
    122   /// derived class to the same base class.
    123   std::list<CXXBasePath> Paths;
    124 
    125   /// ClassSubobjects - Records the class subobjects for each class
    126   /// type that we've seen. The first element in the pair says
    127   /// whether we found a path to a virtual base for that class type,
    128   /// while the element contains the number of non-virtual base
    129   /// class subobjects for that class type. The key of the map is
    130   /// the cv-unqualified canonical type of the base class subobject.
    131   std::map<QualType, std::pair<bool, unsigned>, QualTypeOrdering>
    132     ClassSubobjects;
    133 
    134   /// FindAmbiguities - Whether Sema::IsDerivedFrom should try find
    135   /// ambiguous paths while it is looking for a path from a derived
    136   /// type to a base type.
    137   bool FindAmbiguities;
    138 
    139   /// RecordPaths - Whether Sema::IsDerivedFrom should record paths
    140   /// while it is determining whether there are paths from a derived
    141   /// type to a base type.
    142   bool RecordPaths;
    143 
    144   /// DetectVirtual - Whether Sema::IsDerivedFrom should abort the search
    145   /// if it finds a path that goes across a virtual base. The virtual class
    146   /// is also recorded.
    147   bool DetectVirtual;
    148 
    149   /// ScratchPath - A BasePath that is used by Sema::lookupInBases
    150   /// to help build the set of paths.
    151   CXXBasePath ScratchPath;
    152 
    153   /// DetectedVirtual - The base class that is virtual.
    154   const RecordType *DetectedVirtual;
    155 
    156   /// \brief Array of the declarations that have been found. This
    157   /// array is constructed only if needed, e.g., to iterate over the
    158   /// results within LookupResult.
    159   NamedDecl **DeclsFound;
    160   unsigned NumDeclsFound;
    161 
    162   friend class CXXRecordDecl;
    163 
    164   void ComputeDeclsFound();
    165 
    166   bool lookupInBases(ASTContext &Context,
    167                      const CXXRecordDecl *Record,
    168                      CXXRecordDecl::BaseMatchesCallback *BaseMatches,
    169                      void *UserData);
    170 public:
    171   typedef std::list<CXXBasePath>::iterator paths_iterator;
    172   typedef std::list<CXXBasePath>::const_iterator const_paths_iterator;
    173   typedef NamedDecl **decl_iterator;
    174 
    175   /// BasePaths - Construct a new BasePaths structure to record the
    176   /// paths for a derived-to-base search.
    177   explicit CXXBasePaths(bool FindAmbiguities = true,
    178                         bool RecordPaths = true,
    179                         bool DetectVirtual = true)
    180     : FindAmbiguities(FindAmbiguities), RecordPaths(RecordPaths),
    181       DetectVirtual(DetectVirtual), DetectedVirtual(0), DeclsFound(0),
    182       NumDeclsFound(0) { }
    183 
    184   ~CXXBasePaths() { delete [] DeclsFound; }
    185 
    186   paths_iterator begin() { return Paths.begin(); }
    187   paths_iterator end()   { return Paths.end(); }
    188   const_paths_iterator begin() const { return Paths.begin(); }
    189   const_paths_iterator end()   const { return Paths.end(); }
    190 
    191   CXXBasePath&       front()       { return Paths.front(); }
    192   const CXXBasePath& front() const { return Paths.front(); }
    193 
    194   decl_iterator found_decls_begin();
    195   decl_iterator found_decls_end();
    196 
    197   /// \brief Determine whether the path from the most-derived type to the
    198   /// given base type is ambiguous (i.e., it refers to multiple subobjects of
    199   /// the same base type).
    200   bool isAmbiguous(CanQualType BaseType);
    201 
    202   /// \brief Whether we are finding multiple paths to detect ambiguities.
    203   bool isFindingAmbiguities() const { return FindAmbiguities; }
    204 
    205   /// \brief Whether we are recording paths.
    206   bool isRecordingPaths() const { return RecordPaths; }
    207 
    208   /// \brief Specify whether we should be recording paths or not.
    209   void setRecordingPaths(bool RP) { RecordPaths = RP; }
    210 
    211   /// \brief Whether we are detecting virtual bases.
    212   bool isDetectingVirtual() const { return DetectVirtual; }
    213 
    214   /// \brief The virtual base discovered on the path (if we are merely
    215   /// detecting virtuals).
    216   const RecordType* getDetectedVirtual() const {
    217     return DetectedVirtual;
    218   }
    219 
    220   /// \brief Retrieve the type from which this base-paths search
    221   /// began
    222   CXXRecordDecl *getOrigin() const { return Origin; }
    223   void setOrigin(CXXRecordDecl *Rec) { Origin = Rec; }
    224 
    225   /// \brief Clear the base-paths results.
    226   void clear();
    227 
    228   /// \brief Swap this data structure's contents with another CXXBasePaths
    229   /// object.
    230   void swap(CXXBasePaths &Other);
    231 };
    232 
    233 /// \brief Uniquely identifies a virtual method within a class
    234 /// hierarchy by the method itself and a class subobject number.
    235 struct UniqueVirtualMethod {
    236   UniqueVirtualMethod() : Method(0), Subobject(0), InVirtualSubobject(0) { }
    237 
    238   UniqueVirtualMethod(CXXMethodDecl *Method, unsigned Subobject,
    239                       const CXXRecordDecl *InVirtualSubobject)
    240     : Method(Method), Subobject(Subobject),
    241       InVirtualSubobject(InVirtualSubobject) { }
    242 
    243   /// \brief The overriding virtual method.
    244   CXXMethodDecl *Method;
    245 
    246   /// \brief The subobject in which the overriding virtual method
    247   /// resides.
    248   unsigned Subobject;
    249 
    250   /// \brief The virtual base class subobject of which this overridden
    251   /// virtual method is a part. Note that this records the closest
    252   /// derived virtual base class subobject.
    253   const CXXRecordDecl *InVirtualSubobject;
    254 
    255   friend bool operator==(const UniqueVirtualMethod &X,
    256                          const UniqueVirtualMethod &Y) {
    257     return X.Method == Y.Method && X.Subobject == Y.Subobject &&
    258       X.InVirtualSubobject == Y.InVirtualSubobject;
    259   }
    260 
    261   friend bool operator!=(const UniqueVirtualMethod &X,
    262                          const UniqueVirtualMethod &Y) {
    263     return !(X == Y);
    264   }
    265 };
    266 
    267 /// \brief The set of methods that override a given virtual method in
    268 /// each subobject where it occurs.
    269 ///
    270 /// The first part of the pair is the subobject in which the
    271 /// overridden virtual function occurs, while the second part of the
    272 /// pair is the virtual method that overrides it (including the
    273 /// subobject in which that virtual function occurs).
    274 class OverridingMethods {
    275   llvm::DenseMap<unsigned, llvm::SmallVector<UniqueVirtualMethod, 4> >
    276     Overrides;
    277 
    278 public:
    279   // Iterate over the set of subobjects that have overriding methods.
    280   typedef llvm::DenseMap<unsigned, llvm::SmallVector<UniqueVirtualMethod, 4> >
    281             ::iterator iterator;
    282   typedef llvm::DenseMap<unsigned, llvm::SmallVector<UniqueVirtualMethod, 4> >
    283             ::const_iterator const_iterator;
    284   iterator begin() { return Overrides.begin(); }
    285   const_iterator begin() const { return Overrides.begin(); }
    286   iterator end() { return Overrides.end(); }
    287   const_iterator end() const { return Overrides.end(); }
    288   unsigned size() const { return Overrides.size(); }
    289 
    290   // Iterate over the set of overriding virtual methods in a given
    291   // subobject.
    292   typedef llvm::SmallVector<UniqueVirtualMethod, 4>::iterator
    293     overriding_iterator;
    294   typedef llvm::SmallVector<UniqueVirtualMethod, 4>::const_iterator
    295     overriding_const_iterator;
    296 
    297   // Add a new overriding method for a particular subobject.
    298   void add(unsigned OverriddenSubobject, UniqueVirtualMethod Overriding);
    299 
    300   // Add all of the overriding methods from "other" into overrides for
    301   // this method. Used when merging the overrides from multiple base
    302   // class subobjects.
    303   void add(const OverridingMethods &Other);
    304 
    305   // Replace all overriding virtual methods in all subobjects with the
    306   // given virtual method.
    307   void replaceAll(UniqueVirtualMethod Overriding);
    308 };
    309 
    310 /// \brief A mapping from each virtual member function to its set of
    311 /// final overriders.
    312 ///
    313 /// Within a class hierarchy for a given derived class, each virtual
    314 /// member function in that hierarchy has one or more "final
    315 /// overriders" (C++ [class.virtual]p2). A final overrider for a
    316 /// virtual function "f" is the virtual function that will actually be
    317 /// invoked when dispatching a call to "f" through the
    318 /// vtable. Well-formed classes have a single final overrider for each
    319 /// virtual function; in abstract classes, the final overrider for at
    320 /// least one virtual function is a pure virtual function. Due to
    321 /// multiple, virtual inheritance, it is possible for a class to have
    322 /// more than one final overrider. Athough this is an error (per C++
    323 /// [class.virtual]p2), it is not considered an error here: the final
    324 /// overrider map can represent multiple final overriders for a
    325 /// method, and it is up to the client to determine whether they are
    326 /// problem. For example, the following class \c D has two final
    327 /// overriders for the virtual function \c A::f(), one in \c C and one
    328 /// in \c D:
    329 ///
    330 /// \code
    331 ///   struct A { virtual void f(); };
    332 ///   struct B : virtual A { virtual void f(); };
    333 ///   struct C : virtual A { virtual void f(); };
    334 ///   struct D : B, C { };
    335 /// \endcode
    336 ///
    337 /// This data structure contaings a mapping from every virtual
    338 /// function *that does not override an existing virtual function* and
    339 /// in every subobject where that virtual function occurs to the set
    340 /// of virtual functions that override it. Thus, the same virtual
    341 /// function \c A::f can actually occur in multiple subobjects of type
    342 /// \c A due to multiple inheritance, and may be overriden by
    343 /// different virtual functions in each, as in the following example:
    344 ///
    345 /// \code
    346 ///   struct A { virtual void f(); };
    347 ///   struct B : A { virtual void f(); };
    348 ///   struct C : A { virtual void f(); };
    349 ///   struct D : B, C { };
    350 /// \endcode
    351 ///
    352 /// Unlike in the previous example, where the virtual functions \c
    353 /// B::f and \c C::f both overrode \c A::f in the same subobject of
    354 /// type \c A, in this example the two virtual functions both override
    355 /// \c A::f but in *different* subobjects of type A. This is
    356 /// represented by numbering the subobjects in which the overridden
    357 /// and the overriding virtual member functions are located. Subobject
    358 /// 0 represents the virtua base class subobject of that type, while
    359 /// subobject numbers greater than 0 refer to non-virtual base class
    360 /// subobjects of that type.
    361 class CXXFinalOverriderMap
    362   : public llvm::DenseMap<const CXXMethodDecl *, OverridingMethods> { };
    363 
    364 /// \brief A set of all the primary bases for a class.
    365 class CXXIndirectPrimaryBaseSet
    366   : public llvm::SmallSet<const CXXRecordDecl*, 32> { };
    367 
    368 } // end namespace clang
    369 
    370 #endif
    371