CXXInheritance.h revision 204643
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/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/SmallVector.h" 23#include <list> 24#include <map> 25#include <cassert> 26 27namespace clang { 28 29class CXXBaseSpecifier; 30class CXXMethodDecl; 31class CXXRecordDecl; 32class 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. 41struct 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. 67class CXXBasePath : public llvm::SmallVector<CXXBasePathElement, 4> { 68public: 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 llvm::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 hierachy: 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. 115class 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 std::map<QualType, std::pair<bool, unsigned>, QualTypeOrdering> 130 ClassSubobjects; 131 132 /// FindAmbiguities - Whether Sema::IsDerivedFrom should try find 133 /// ambiguous paths while it is looking for a path from a derived 134 /// type to a base type. 135 bool FindAmbiguities; 136 137 /// RecordPaths - Whether Sema::IsDerivedFrom should record paths 138 /// while it is determining whether there are paths from a derived 139 /// type to a base type. 140 bool RecordPaths; 141 142 /// DetectVirtual - Whether Sema::IsDerivedFrom should abort the search 143 /// if it finds a path that goes across a virtual base. The virtual class 144 /// is also recorded. 145 bool DetectVirtual; 146 147 /// ScratchPath - A BasePath that is used by Sema::lookupInBases 148 /// to help build the set of paths. 149 CXXBasePath ScratchPath; 150 151 /// DetectedVirtual - The base class that is virtual. 152 const RecordType *DetectedVirtual; 153 154 /// \brief Array of the declarations that have been found. This 155 /// array is constructed only if needed, e.g., to iterate over the 156 /// results within LookupResult. 157 NamedDecl **DeclsFound; 158 unsigned NumDeclsFound; 159 160 friend class CXXRecordDecl; 161 162 void ComputeDeclsFound(); 163 164 bool lookupInBases(ASTContext &Context, 165 const CXXRecordDecl *Record, 166 CXXRecordDecl::BaseMatchesCallback *BaseMatches, 167 void *UserData); 168public: 169 typedef std::list<CXXBasePath>::iterator paths_iterator; 170 typedef std::list<CXXBasePath>::const_iterator const_paths_iterator; 171 typedef NamedDecl **decl_iterator; 172 173 /// BasePaths - Construct a new BasePaths structure to record the 174 /// paths for a derived-to-base search. 175 explicit CXXBasePaths(bool FindAmbiguities = true, 176 bool RecordPaths = true, 177 bool DetectVirtual = true) 178 : FindAmbiguities(FindAmbiguities), RecordPaths(RecordPaths), 179 DetectVirtual(DetectVirtual), DetectedVirtual(0), DeclsFound(0), 180 NumDeclsFound(0) { } 181 182 ~CXXBasePaths() { delete [] DeclsFound; } 183 184 paths_iterator begin() { return Paths.begin(); } 185 paths_iterator end() { return Paths.end(); } 186 const_paths_iterator begin() const { return Paths.begin(); } 187 const_paths_iterator end() const { return Paths.end(); } 188 189 CXXBasePath& front() { return Paths.front(); } 190 const CXXBasePath& front() const { return Paths.front(); } 191 192 decl_iterator found_decls_begin(); 193 decl_iterator found_decls_end(); 194 195 /// \brief Determine whether the path from the most-derived type to the 196 /// given base type is ambiguous (i.e., it refers to multiple subobjects of 197 /// the same base type). 198 bool isAmbiguous(QualType BaseType); 199 200 /// \brief Whether we are finding multiple paths to detect ambiguities. 201 bool isFindingAmbiguities() const { return FindAmbiguities; } 202 203 /// \brief Whether we are recording paths. 204 bool isRecordingPaths() const { return RecordPaths; } 205 206 /// \brief Specify whether we should be recording paths or not. 207 void setRecordingPaths(bool RP) { RecordPaths = RP; } 208 209 /// \brief Whether we are detecting virtual bases. 210 bool isDetectingVirtual() const { return DetectVirtual; } 211 212 /// \brief The virtual base discovered on the path (if we are merely 213 /// detecting virtuals). 214 const RecordType* getDetectedVirtual() const { 215 return DetectedVirtual; 216 } 217 218 /// \brief Retrieve the type from which this base-paths search 219 /// began 220 CXXRecordDecl *getOrigin() const { return Origin; } 221 void setOrigin(CXXRecordDecl *Rec) { Origin = Rec; } 222 223 /// \brief Clear the base-paths results. 224 void clear(); 225 226 /// \brief Swap this data structure's contents with another CXXBasePaths 227 /// object. 228 void swap(CXXBasePaths &Other); 229}; 230 231} // end namespace clang 232 233#endif 234