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