1 //===------ CXXInheritance.h - C++ Inheritance ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides routines that help analyzing C++ inheritance hierarchies.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_CXXINHERITANCE_H
15 #define LLVM_CLANG_AST_CXXINHERITANCE_H
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/SmallVector.h"
30 class CXXBaseSpecifier;
35 /// \brief Represents an element in a path from a derived class to a
38 /// Each step in the path references the link from a
39 /// derived class to one of its direct base classes, along with a
40 /// base "number" that identifies which base subobject of the
41 /// original derived class we are referencing.
42 struct CXXBasePathElement {
43 /// \brief The base specifier that states the link from a derived
44 /// class to a base class, which will be followed by this base
46 const CXXBaseSpecifier *Base;
48 /// \brief The record decl of the class that the base is a base of.
49 const CXXRecordDecl *Class;
51 /// \brief Identifies which base class subobject (of type
52 /// \c Base->getType()) this base path element refers to.
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
60 /// \brief Represents a path from a specific derived class
61 /// (which is not represented as part of the path) to a particular
62 /// (direct or indirect) base class subobject.
64 /// Individual elements in the path are described by the \c CXXBasePathElement
65 /// structure, which captures both the link from a derived class to one of its
66 /// direct bases and identification describing which base class
67 /// subobject is being used.
68 class CXXBasePath : public llvm::SmallVector<CXXBasePathElement, 4> {
70 CXXBasePath() : Access(AS_public) {}
72 /// \brief The access along this inheritance path. This is only
73 /// calculated when recording paths. AS_none is a special value
74 /// used to indicate a path which permits no legal access.
75 AccessSpecifier Access;
77 /// \brief The set of declarations found inside this base class
79 DeclContext::lookup_result Decls;
82 llvm::SmallVectorImpl<CXXBasePathElement>::clear();
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 hierachy:
93 /// class B : public A { };
94 /// class C : public A { };
95 /// class D : public B, public C{ };
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:
107 /// class B : public virtual A { };
108 /// class C : public virtual A { };
109 /// class D : public B, public C{ };
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 /// \brief The type from which this search originated.
118 CXXRecordDecl *Origin;
120 /// Paths - The actual set of paths that can be taken from the
121 /// derived class to the same base class.
122 std::list<CXXBasePath> Paths;
124 /// ClassSubobjects - Records the class subobjects for each class
125 /// type that we've seen. The first element in the pair says
126 /// whether we found a path to a virtual base for that class type,
127 /// while the element contains the number of non-virtual base
128 /// class subobjects for that class type. The key of the map is
129 /// the cv-unqualified canonical type of the base class subobject.
130 std::map<QualType, std::pair<bool, unsigned>, QualTypeOrdering>
133 /// FindAmbiguities - Whether Sema::IsDerivedFrom should try find
134 /// ambiguous paths while it is looking for a path from a derived
135 /// type to a base type.
136 bool FindAmbiguities;
138 /// RecordPaths - Whether Sema::IsDerivedFrom should record paths
139 /// while it is determining whether there are paths from a derived
140 /// type to a base type.
143 /// DetectVirtual - Whether Sema::IsDerivedFrom should abort the search
144 /// if it finds a path that goes across a virtual base. The virtual class
145 /// is also recorded.
148 /// ScratchPath - A BasePath that is used by Sema::lookupInBases
149 /// to help build the set of paths.
150 CXXBasePath ScratchPath;
152 /// DetectedVirtual - The base class that is virtual.
153 const RecordType *DetectedVirtual;
155 /// \brief Array of the declarations that have been found. This
156 /// array is constructed only if needed, e.g., to iterate over the
157 /// results within LookupResult.
158 NamedDecl **DeclsFound;
159 unsigned NumDeclsFound;
161 friend class CXXRecordDecl;
163 void ComputeDeclsFound();
165 bool lookupInBases(ASTContext &Context,
166 const CXXRecordDecl *Record,
167 CXXRecordDecl::BaseMatchesCallback *BaseMatches,
170 typedef std::list<CXXBasePath>::iterator paths_iterator;
171 typedef std::list<CXXBasePath>::const_iterator const_paths_iterator;
172 typedef NamedDecl **decl_iterator;
174 /// BasePaths - Construct a new BasePaths structure to record the
175 /// paths for a derived-to-base search.
176 explicit CXXBasePaths(bool FindAmbiguities = true,
177 bool RecordPaths = true,
178 bool DetectVirtual = true)
179 : FindAmbiguities(FindAmbiguities), RecordPaths(RecordPaths),
180 DetectVirtual(DetectVirtual), DetectedVirtual(0), DeclsFound(0),
183 ~CXXBasePaths() { delete [] DeclsFound; }
185 paths_iterator begin() { return Paths.begin(); }
186 paths_iterator end() { return Paths.end(); }
187 const_paths_iterator begin() const { return Paths.begin(); }
188 const_paths_iterator end() const { return Paths.end(); }
190 CXXBasePath& front() { return Paths.front(); }
191 const CXXBasePath& front() const { return Paths.front(); }
193 decl_iterator found_decls_begin();
194 decl_iterator found_decls_end();
196 /// \brief Determine whether the path from the most-derived type to the
197 /// given base type is ambiguous (i.e., it refers to multiple subobjects of
198 /// the same base type).
199 bool isAmbiguous(CanQualType BaseType);
201 /// \brief Whether we are finding multiple paths to detect ambiguities.
202 bool isFindingAmbiguities() const { return FindAmbiguities; }
204 /// \brief Whether we are recording paths.
205 bool isRecordingPaths() const { return RecordPaths; }
207 /// \brief Specify whether we should be recording paths or not.
208 void setRecordingPaths(bool RP) { RecordPaths = RP; }
210 /// \brief Whether we are detecting virtual bases.
211 bool isDetectingVirtual() const { return DetectVirtual; }
213 /// \brief The virtual base discovered on the path (if we are merely
214 /// detecting virtuals).
215 const RecordType* getDetectedVirtual() const {
216 return DetectedVirtual;
219 /// \brief Retrieve the type from which this base-paths search
221 CXXRecordDecl *getOrigin() const { return Origin; }
222 void setOrigin(CXXRecordDecl *Rec) { Origin = Rec; }
224 /// \brief Clear the base-paths results.
227 /// \brief Swap this data structure's contents with another CXXBasePaths
229 void swap(CXXBasePaths &Other);
232 /// \brief Uniquely identifies a virtual method within a class
233 /// hierarchy by the method itself and a class subobject number.
234 struct UniqueVirtualMethod {
235 UniqueVirtualMethod() : Method(0), Subobject(0), InVirtualSubobject(0) { }
237 UniqueVirtualMethod(CXXMethodDecl *Method, unsigned Subobject,
238 const CXXRecordDecl *InVirtualSubobject)
239 : Method(Method), Subobject(Subobject),
240 InVirtualSubobject(InVirtualSubobject) { }
242 /// \brief The overriding virtual method.
243 CXXMethodDecl *Method;
245 /// \brief The subobject in which the overriding virtual method
249 /// \brief The virtual base class subobject of which this overridden
250 /// virtual method is a part. Note that this records the closest
251 /// derived virtual base class subobject.
252 const CXXRecordDecl *InVirtualSubobject;
254 friend bool operator==(const UniqueVirtualMethod &X,
255 const UniqueVirtualMethod &Y) {
256 return X.Method == Y.Method && X.Subobject == Y.Subobject &&
257 X.InVirtualSubobject == Y.InVirtualSubobject;
260 friend bool operator!=(const UniqueVirtualMethod &X,
261 const UniqueVirtualMethod &Y) {
266 /// \brief The set of methods that override a given virtual method in
267 /// each subobject where it occurs.
269 /// The first part of the pair is the subobject in which the
270 /// overridden virtual function occurs, while the second part of the
271 /// pair is the virtual method that overrides it (including the
272 /// subobject in which that virtual function occurs).
273 class OverridingMethods {
274 llvm::DenseMap<unsigned, llvm::SmallVector<UniqueVirtualMethod, 4> >
278 // Iterate over the set of subobjects that have overriding methods.
279 typedef llvm::DenseMap<unsigned, llvm::SmallVector<UniqueVirtualMethod, 4> >
281 typedef llvm::DenseMap<unsigned, llvm::SmallVector<UniqueVirtualMethod, 4> >
282 ::const_iterator const_iterator;
283 iterator begin() { return Overrides.begin(); }
284 const_iterator begin() const { return Overrides.begin(); }
285 iterator end() { return Overrides.end(); }
286 const_iterator end() const { return Overrides.end(); }
287 unsigned size() const { return Overrides.size(); }
289 // Iterate over the set of overriding virtual methods in a given
291 typedef llvm::SmallVector<UniqueVirtualMethod, 4>::iterator
293 typedef llvm::SmallVector<UniqueVirtualMethod, 4>::const_iterator
294 overriding_const_iterator;
296 // Add a new overriding method for a particular subobject.
297 void add(unsigned OverriddenSubobject, UniqueVirtualMethod Overriding);
299 // Add all of the overriding methods from "other" into overrides for
300 // this method. Used when merging the overrides from multiple base
302 void add(const OverridingMethods &Other);
304 // Replace all overriding virtual methods in all subobjects with the
305 // given virtual method.
306 void replaceAll(UniqueVirtualMethod Overriding);
309 /// \brief A mapping from each virtual member function to its set of
310 /// final overriders.
312 /// Within a class hierarchy for a given derived class, each virtual
313 /// member function in that hierarchy has one or more "final
314 /// overriders" (C++ [class.virtual]p2). A final overrider for a
315 /// virtual function "f" is the virtual function that will actually be
316 /// invoked when dispatching a call to "f" through the
317 /// vtable. Well-formed classes have a single final overrider for each
318 /// virtual function; in abstract classes, the final overrider for at
319 /// least one virtual function is a pure virtual function. Due to
320 /// multiple, virtual inheritance, it is possible for a class to have
321 /// more than one final overrider. Athough this is an error (per C++
322 /// [class.virtual]p2), it is not considered an error here: the final
323 /// overrider map can represent multiple final overriders for a
324 /// method, and it is up to the client to determine whether they are
325 /// problem. For example, the following class \c D has two final
326 /// overriders for the virtual function \c A::f(), one in \c C and one
330 /// struct A { virtual void f(); };
331 /// struct B : virtual A { virtual void f(); };
332 /// struct C : virtual A { virtual void f(); };
333 /// struct D : B, C { };
336 /// This data structure contaings a mapping from every virtual
337 /// function *that does not override an existing virtual function* and
338 /// in every subobject where that virtual function occurs to the set
339 /// of virtual functions that override it. Thus, the same virtual
340 /// function \c A::f can actually occur in multiple subobjects of type
341 /// \c A due to multiple inheritance, and may be overriden by
342 /// different virtual functions in each, as in the following example:
345 /// struct A { virtual void f(); };
346 /// struct B : A { virtual void f(); };
347 /// struct C : A { virtual void f(); };
348 /// struct D : B, C { };
351 /// Unlike in the previous example, where the virtual functions \c
352 /// B::f and \c C::f both overrode \c A::f in the same subobject of
353 /// type \c A, in this example the two virtual functions both override
354 /// \c A::f but in *different* subobjects of type A. This is
355 /// represented by numbering the subobjects in which the overridden
356 /// and the overriding virtual member functions are located. Subobject
357 /// 0 represents the virtua base class subobject of that type, while
358 /// subobject numbers greater than 0 refer to non-virtual base class
359 /// subobjects of that type.
360 class CXXFinalOverriderMap
361 : public llvm::DenseMap<const CXXMethodDecl *, OverridingMethods> { };
363 } // end namespace clang