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