CXXInheritance.h 15 KB

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