clang API Documentation
00001 //===------ CXXInheritance.h - C++ Inheritance ------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file provides routines that help analyzing C++ inheritance hierarchies. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CLANG_AST_CXXINHERITANCE_H 00015 #define LLVM_CLANG_AST_CXXINHERITANCE_H 00016 00017 #include "clang/AST/DeclBase.h" 00018 #include "clang/AST/DeclCXX.h" 00019 #include "clang/AST/DeclarationName.h" 00020 #include "clang/AST/Type.h" 00021 #include "clang/AST/TypeOrdering.h" 00022 #include "llvm/ADT/MapVector.h" 00023 #include "llvm/ADT/SmallSet.h" 00024 #include "llvm/ADT/SmallVector.h" 00025 #include <cassert> 00026 #include <list> 00027 #include <map> 00028 00029 namespace clang { 00030 00031 class CXXBaseSpecifier; 00032 class CXXMethodDecl; 00033 class CXXRecordDecl; 00034 class NamedDecl; 00035 00036 /// \brief Represents an element in a path from a derived class to a 00037 /// base class. 00038 /// 00039 /// Each step in the path references the link from a 00040 /// derived class to one of its direct base classes, along with a 00041 /// base "number" that identifies which base subobject of the 00042 /// original derived class we are referencing. 00043 struct CXXBasePathElement { 00044 /// \brief The base specifier that states the link from a derived 00045 /// class to a base class, which will be followed by this base 00046 /// path element. 00047 const CXXBaseSpecifier *Base; 00048 00049 /// \brief The record decl of the class that the base is a base of. 00050 const CXXRecordDecl *Class; 00051 00052 /// \brief Identifies which base class subobject (of type 00053 /// \c Base->getType()) this base path element refers to. 00054 /// 00055 /// This value is only valid if \c !Base->isVirtual(), because there 00056 /// is no base numbering for the zero or one virtual bases of a 00057 /// given type. 00058 int SubobjectNumber; 00059 }; 00060 00061 /// \brief Represents a path from a specific derived class 00062 /// (which is not represented as part of the path) to a particular 00063 /// (direct or indirect) base class subobject. 00064 /// 00065 /// Individual elements in the path are described by the \c CXXBasePathElement 00066 /// structure, which captures both the link from a derived class to one of its 00067 /// direct bases and identification describing which base class 00068 /// subobject is being used. 00069 class CXXBasePath : public SmallVector<CXXBasePathElement, 4> { 00070 public: 00071 CXXBasePath() : Access(AS_public) {} 00072 00073 /// \brief The access along this inheritance path. This is only 00074 /// calculated when recording paths. AS_none is a special value 00075 /// used to indicate a path which permits no legal access. 00076 AccessSpecifier Access; 00077 00078 /// \brief The set of declarations found inside this base class 00079 /// subobject. 00080 DeclContext::lookup_result Decls; 00081 00082 void clear() { 00083 SmallVectorImpl<CXXBasePathElement>::clear(); 00084 Access = AS_public; 00085 } 00086 }; 00087 00088 /// BasePaths - Represents the set of paths from a derived class to 00089 /// one of its (direct or indirect) bases. For example, given the 00090 /// following class hierarchy: 00091 /// 00092 /// @code 00093 /// class A { }; 00094 /// class B : public A { }; 00095 /// class C : public A { }; 00096 /// class D : public B, public C{ }; 00097 /// @endcode 00098 /// 00099 /// There are two potential BasePaths to represent paths from D to a 00100 /// base subobject of type A. One path is (D,0) -> (B,0) -> (A,0) 00101 /// and another is (D,0)->(C,0)->(A,1). These two paths actually 00102 /// refer to two different base class subobjects of the same type, 00103 /// so the BasePaths object refers to an ambiguous path. On the 00104 /// other hand, consider the following class hierarchy: 00105 /// 00106 /// @code 00107 /// class A { }; 00108 /// class B : public virtual A { }; 00109 /// class C : public virtual A { }; 00110 /// class D : public B, public C{ }; 00111 /// @endcode 00112 /// 00113 /// Here, there are two potential BasePaths again, (D, 0) -> (B, 0) 00114 /// -> (A,v) and (D, 0) -> (C, 0) -> (A, v), but since both of them 00115 /// refer to the same base class subobject of type A (the virtual 00116 /// one), there is no ambiguity. 00117 class CXXBasePaths { 00118 /// \brief The type from which this search originated. 00119 CXXRecordDecl *Origin; 00120 00121 /// Paths - The actual set of paths that can be taken from the 00122 /// derived class to the same base class. 00123 std::list<CXXBasePath> Paths; 00124 00125 /// ClassSubobjects - Records the class subobjects for each class 00126 /// type that we've seen. The first element in the pair says 00127 /// whether we found a path to a virtual base for that class type, 00128 /// while the element contains the number of non-virtual base 00129 /// class subobjects for that class type. The key of the map is 00130 /// the cv-unqualified canonical type of the base class subobject. 00131 llvm::SmallDenseMap<QualType, std::pair<bool, unsigned>, 8> ClassSubobjects; 00132 00133 /// FindAmbiguities - Whether Sema::IsDerivedFrom should try find 00134 /// ambiguous paths while it is looking for a path from a derived 00135 /// type to a base type. 00136 bool FindAmbiguities; 00137 00138 /// RecordPaths - Whether Sema::IsDerivedFrom should record paths 00139 /// while it is determining whether there are paths from a derived 00140 /// type to a base type. 00141 bool RecordPaths; 00142 00143 /// DetectVirtual - Whether Sema::IsDerivedFrom should abort the search 00144 /// if it finds a path that goes across a virtual base. The virtual class 00145 /// is also recorded. 00146 bool DetectVirtual; 00147 00148 /// ScratchPath - A BasePath that is used by Sema::lookupInBases 00149 /// to help build the set of paths. 00150 CXXBasePath ScratchPath; 00151 00152 /// DetectedVirtual - The base class that is virtual. 00153 const RecordType *DetectedVirtual; 00154 00155 /// \brief Array of the declarations that have been found. This 00156 /// array is constructed only if needed, e.g., to iterate over the 00157 /// results within LookupResult. 00158 NamedDecl **DeclsFound; 00159 unsigned NumDeclsFound; 00160 00161 friend class CXXRecordDecl; 00162 00163 void ComputeDeclsFound(); 00164 00165 bool lookupInBases(ASTContext &Context, 00166 const CXXRecordDecl *Record, 00167 CXXRecordDecl::BaseMatchesCallback *BaseMatches, 00168 void *UserData); 00169 public: 00170 typedef std::list<CXXBasePath>::iterator paths_iterator; 00171 typedef std::list<CXXBasePath>::const_iterator const_paths_iterator; 00172 typedef NamedDecl **decl_iterator; 00173 00174 /// BasePaths - Construct a new BasePaths structure to record the 00175 /// paths for a derived-to-base search. 00176 explicit CXXBasePaths(bool FindAmbiguities = true, 00177 bool RecordPaths = true, 00178 bool DetectVirtual = true) 00179 : FindAmbiguities(FindAmbiguities), RecordPaths(RecordPaths), 00180 DetectVirtual(DetectVirtual), DetectedVirtual(nullptr), 00181 DeclsFound(nullptr), NumDeclsFound(0) { } 00182 00183 ~CXXBasePaths() { delete [] DeclsFound; } 00184 00185 paths_iterator begin() { return Paths.begin(); } 00186 paths_iterator end() { return Paths.end(); } 00187 const_paths_iterator begin() const { return Paths.begin(); } 00188 const_paths_iterator end() const { return Paths.end(); } 00189 00190 CXXBasePath& front() { return Paths.front(); } 00191 const CXXBasePath& front() const { return Paths.front(); } 00192 00193 typedef llvm::iterator_range<decl_iterator> decl_range; 00194 decl_range found_decls(); 00195 00196 /// \brief Determine whether the path from the most-derived type to the 00197 /// given base type is ambiguous (i.e., it refers to multiple subobjects of 00198 /// the same base type). 00199 bool isAmbiguous(CanQualType BaseType); 00200 00201 /// \brief Whether we are finding multiple paths to detect ambiguities. 00202 bool isFindingAmbiguities() const { return FindAmbiguities; } 00203 00204 /// \brief Whether we are recording paths. 00205 bool isRecordingPaths() const { return RecordPaths; } 00206 00207 /// \brief Specify whether we should be recording paths or not. 00208 void setRecordingPaths(bool RP) { RecordPaths = RP; } 00209 00210 /// \brief Whether we are detecting virtual bases. 00211 bool isDetectingVirtual() const { return DetectVirtual; } 00212 00213 /// \brief The virtual base discovered on the path (if we are merely 00214 /// detecting virtuals). 00215 const RecordType* getDetectedVirtual() const { 00216 return DetectedVirtual; 00217 } 00218 00219 /// \brief Retrieve the type from which this base-paths search 00220 /// began 00221 CXXRecordDecl *getOrigin() const { return Origin; } 00222 void setOrigin(CXXRecordDecl *Rec) { Origin = Rec; } 00223 00224 /// \brief Clear the base-paths results. 00225 void clear(); 00226 00227 /// \brief Swap this data structure's contents with another CXXBasePaths 00228 /// object. 00229 void swap(CXXBasePaths &Other); 00230 }; 00231 00232 /// \brief Uniquely identifies a virtual method within a class 00233 /// hierarchy by the method itself and a class subobject number. 00234 struct UniqueVirtualMethod { 00235 UniqueVirtualMethod() 00236 : Method(nullptr), Subobject(0), InVirtualSubobject(nullptr) { } 00237 00238 UniqueVirtualMethod(CXXMethodDecl *Method, unsigned Subobject, 00239 const CXXRecordDecl *InVirtualSubobject) 00240 : Method(Method), Subobject(Subobject), 00241 InVirtualSubobject(InVirtualSubobject) { } 00242 00243 /// \brief The overriding virtual method. 00244 CXXMethodDecl *Method; 00245 00246 /// \brief The subobject in which the overriding virtual method 00247 /// resides. 00248 unsigned Subobject; 00249 00250 /// \brief The virtual base class subobject of which this overridden 00251 /// virtual method is a part. Note that this records the closest 00252 /// derived virtual base class subobject. 00253 const CXXRecordDecl *InVirtualSubobject; 00254 00255 friend bool operator==(const UniqueVirtualMethod &X, 00256 const UniqueVirtualMethod &Y) { 00257 return X.Method == Y.Method && X.Subobject == Y.Subobject && 00258 X.InVirtualSubobject == Y.InVirtualSubobject; 00259 } 00260 00261 friend bool operator!=(const UniqueVirtualMethod &X, 00262 const UniqueVirtualMethod &Y) { 00263 return !(X == Y); 00264 } 00265 }; 00266 00267 /// \brief The set of methods that override a given virtual method in 00268 /// each subobject where it occurs. 00269 /// 00270 /// The first part of the pair is the subobject in which the 00271 /// overridden virtual function occurs, while the second part of the 00272 /// pair is the virtual method that overrides it (including the 00273 /// subobject in which that virtual function occurs). 00274 class OverridingMethods { 00275 typedef SmallVector<UniqueVirtualMethod, 4> ValuesT; 00276 typedef llvm::MapVector<unsigned, ValuesT> MapType; 00277 MapType Overrides; 00278 00279 public: 00280 // Iterate over the set of subobjects that have overriding methods. 00281 typedef MapType::iterator iterator; 00282 typedef MapType::const_iterator const_iterator; 00283 iterator begin() { return Overrides.begin(); } 00284 const_iterator begin() const { return Overrides.begin(); } 00285 iterator end() { return Overrides.end(); } 00286 const_iterator end() const { return Overrides.end(); } 00287 unsigned size() const { return Overrides.size(); } 00288 00289 // Iterate over the set of overriding virtual methods in a given 00290 // subobject. 00291 typedef SmallVectorImpl<UniqueVirtualMethod>::iterator 00292 overriding_iterator; 00293 typedef SmallVectorImpl<UniqueVirtualMethod>::const_iterator 00294 overriding_const_iterator; 00295 00296 // Add a new overriding method for a particular subobject. 00297 void add(unsigned OverriddenSubobject, UniqueVirtualMethod Overriding); 00298 00299 // Add all of the overriding methods from "other" into overrides for 00300 // this method. Used when merging the overrides from multiple base 00301 // class subobjects. 00302 void add(const OverridingMethods &Other); 00303 00304 // Replace all overriding virtual methods in all subobjects with the 00305 // given virtual method. 00306 void replaceAll(UniqueVirtualMethod Overriding); 00307 }; 00308 00309 /// \brief A mapping from each virtual member function to its set of 00310 /// final overriders. 00311 /// 00312 /// Within a class hierarchy for a given derived class, each virtual 00313 /// member function in that hierarchy has one or more "final 00314 /// overriders" (C++ [class.virtual]p2). A final overrider for a 00315 /// virtual function "f" is the virtual function that will actually be 00316 /// invoked when dispatching a call to "f" through the 00317 /// vtable. Well-formed classes have a single final overrider for each 00318 /// virtual function; in abstract classes, the final overrider for at 00319 /// least one virtual function is a pure virtual function. Due to 00320 /// multiple, virtual inheritance, it is possible for a class to have 00321 /// more than one final overrider. Athough this is an error (per C++ 00322 /// [class.virtual]p2), it is not considered an error here: the final 00323 /// overrider map can represent multiple final overriders for a 00324 /// method, and it is up to the client to determine whether they are 00325 /// problem. For example, the following class \c D has two final 00326 /// overriders for the virtual function \c A::f(), one in \c C and one 00327 /// in \c D: 00328 /// 00329 /// \code 00330 /// struct A { virtual void f(); }; 00331 /// struct B : virtual A { virtual void f(); }; 00332 /// struct C : virtual A { virtual void f(); }; 00333 /// struct D : B, C { }; 00334 /// \endcode 00335 /// 00336 /// This data structure contaings a mapping from every virtual 00337 /// function *that does not override an existing virtual function* and 00338 /// in every subobject where that virtual function occurs to the set 00339 /// of virtual functions that override it. Thus, the same virtual 00340 /// function \c A::f can actually occur in multiple subobjects of type 00341 /// \c A due to multiple inheritance, and may be overriden by 00342 /// different virtual functions in each, as in the following example: 00343 /// 00344 /// \code 00345 /// struct A { virtual void f(); }; 00346 /// struct B : A { virtual void f(); }; 00347 /// struct C : A { virtual void f(); }; 00348 /// struct D : B, C { }; 00349 /// \endcode 00350 /// 00351 /// Unlike in the previous example, where the virtual functions \c 00352 /// B::f and \c C::f both overrode \c A::f in the same subobject of 00353 /// type \c A, in this example the two virtual functions both override 00354 /// \c A::f but in *different* subobjects of type A. This is 00355 /// represented by numbering the subobjects in which the overridden 00356 /// and the overriding virtual member functions are located. Subobject 00357 /// 0 represents the virtua base class subobject of that type, while 00358 /// subobject numbers greater than 0 refer to non-virtual base class 00359 /// subobjects of that type. 00360 class CXXFinalOverriderMap 00361 : public llvm::MapVector<const CXXMethodDecl *, OverridingMethods> { }; 00362 00363 /// \brief A set of all the primary bases for a class. 00364 class CXXIndirectPrimaryBaseSet 00365 : public llvm::SmallSet<const CXXRecordDecl*, 32> { }; 00366 00367 } // end namespace clang 00368 00369 #endif