clang 20.0.0git
CXXInheritance.h
Go to the documentation of this file.
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"
19#include "clang/AST/Type.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
31namespace clang {
32
33class ASTContext;
34class 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.
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.
48
49 /// The record decl of the class that the base is a base of.
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.
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.
69class CXXBasePath : public SmallVector<CXXBasePathElement, 4> {
70public:
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.
75
76 CXXBasePath() = default;
77
78 /// The declarations found inside this base class subobject.
80
81 void clear() {
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.
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,
168 bool LookupInDependent = false);
169
170public:
171 using paths_iterator = std::list<CXXBasePath>::iterator;
172 using const_paths_iterator = std::list<CXXBasePath>::const_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, 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).
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.
231 /// The overriding virtual method.
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.
242
244
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).
271 using MapType = llvm::MapVector<unsigned, ValuesT>;
272
273 MapType Overrides;
274
275public:
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.
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.
358 : public llvm::MapVector<const CXXMethodDecl *, OverridingMethods> {};
359
360/// A set of all the primary bases for a class.
362 : public llvm::SmallSet<const CXXRecordDecl*, 32> {};
363
364inline bool
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.
371inline bool inheritanceModelHasNVOffsetField(bool IsMemberFunction,
372 MSInheritanceModel Inheritance) {
373 return IsMemberFunction && Inheritance >= MSInheritanceModel::Multiple;
374}
375
376inline bool
378 return Inheritance >= MSInheritanceModel::Virtual;
379}
380
381inline 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
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
#define X(type, name)
Definition: Value.h:144
llvm::MachO::Record Record
Definition: MachO.h:31
Defines various enumerations that describe declaration and type specifiers.
Allows QualTypes to be sorted and hence used in maps and sets.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
Represents a path from a specific derived class (which is not represented as part of the path) to a p...
DeclContext::lookup_iterator Decls
The declarations found inside this base class subobject.
AccessSpecifier Access
The access along this inheritance path.
CXXBasePath()=default
BasePaths - Represents the set of paths from a derived class to one of its (direct or indirect) bases...
const RecordType * getDetectedVirtual() const
The virtual base discovered on the path (if we are merely detecting virtuals).
const CXXRecordDecl * getOrigin() const
Retrieve the type from which this base-paths search began.
CXXBasePaths(bool FindAmbiguities=true, bool RecordPaths=true, bool DetectVirtual=true)
BasePaths - Construct a new BasePaths structure to record the paths for a derived-to-base search.
CXXBasePath & front()
bool isRecordingPaths() const
Whether we are recording paths.
paths_iterator begin()
paths_iterator end()
void setRecordingPaths(bool RP)
Specify whether we should be recording paths or not.
void setOrigin(const CXXRecordDecl *Rec)
llvm::iterator_range< decl_iterator > decl_range
bool isDetectingVirtual() const
Whether we are detecting virtual bases.
bool isAmbiguous(CanQualType BaseType)
Determine whether the path from the most-derived type to the given base type is ambiguous (i....
void clear()
Clear the base-paths results.
std::list< CXXBasePath >::iterator paths_iterator
const_paths_iterator end() const
const CXXBasePath & front() const
std::list< CXXBasePath >::const_iterator const_paths_iterator
bool isFindingAmbiguities() const
Whether we are finding multiple paths to detect ambiguities.
void swap(CXXBasePaths &Other)
Swap this data structure's contents with another CXXBasePaths object.
const_paths_iterator begin() const
Represents a base class of a C++ class.
Definition: DeclCXX.h:146
A mapping from each virtual member function to its set of final overriders.
A set of all the primary bases for a class.
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2078
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
llvm::function_ref< bool(const CXXBaseSpecifier *Specifier, CXXBasePath &Path)> BaseMatchesCallback
Function type used by lookupInBases() to determine whether a specific base class subobject matches th...
Definition: DeclCXX.h:1658
This represents a decl that may have a name.
Definition: Decl.h:253
The set of methods that override a given virtual method in each subobject where it occurs.
SmallVectorImpl< UniqueVirtualMethod >::iterator overriding_iterator
void replaceAll(UniqueVirtualMethod Overriding)
const_iterator begin() const
MapType::iterator iterator
SmallVectorImpl< UniqueVirtualMethod >::const_iterator overriding_const_iterator
MapType::const_iterator const_iterator
void add(unsigned OverriddenSubobject, UniqueVirtualMethod Overriding)
const_iterator end() const
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:6072
The JSON file list parser is used to communicate input to InstallAPI.
bool inheritanceModelHasNVOffsetField(bool IsMemberFunction, MSInheritanceModel Inheritance)
bool inheritanceModelHasOnlyOneField(bool IsMemberFunction, MSInheritanceModel Inheritance)
bool inheritanceModelHasVBPtrOffsetField(MSInheritanceModel Inheritance)
bool inheritanceModelHasVBTableOffsetField(MSInheritanceModel Inheritance)
MSInheritanceModel
Assigned inheritance model for a class in the MS C++ ABI.
Definition: Specifiers.h:398
@ Other
Other implicit parameter.
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
Definition: Specifiers.h:123
@ AS_public
Definition: Specifiers.h:124
Represents an element in a path from a derived class to a base class.
int SubobjectNumber
Identifies which base class subobject (of type Base->getType()) this base path element refers to.
const CXXRecordDecl * Class
The record decl of the class that the base is a base of.
const CXXBaseSpecifier * Base
The base specifier that states the link from a derived class to a base class, which will be followed ...
Uniquely identifies a virtual method within a class hierarchy by the method itself and a class subobj...
friend bool operator==(const UniqueVirtualMethod &X, const UniqueVirtualMethod &Y)
UniqueVirtualMethod(CXXMethodDecl *Method, unsigned Subobject, const CXXRecordDecl *InVirtualSubobject)
CXXMethodDecl * Method
The overriding virtual method.
unsigned Subobject
The subobject in which the overriding virtual method resides.
friend bool operator!=(const UniqueVirtualMethod &X, const UniqueVirtualMethod &Y)
const CXXRecordDecl * InVirtualSubobject
The virtual base class subobject of which this overridden virtual method is a part.