CXXInheritance.h revision 198092
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/Type.h" 20#include "clang/AST/TypeOrdering.h" 21#include "llvm/ADT/SmallVector.h" 22#include <list> 23#include <map> 24#include <cassert> 25 26namespace clang { 27 28class CXXBaseSpecifier; 29class CXXMethodDecl; 30class CXXRecordDecl; 31class NamedDecl; 32 33/// \brief Represents an element in a path from a derived class to a 34/// base class. 35/// 36/// Each step in the path references the link from a 37/// derived class to one of its direct base classes, along with a 38/// base "number" that identifies which base subobject of the 39/// original derived class we are referencing. 40struct CXXBasePathElement { 41 /// \brief The base specifier that states the link from a derived 42 /// class to a base class, which will be followed by this base 43 /// path element. 44 const CXXBaseSpecifier *Base; 45 46 /// \brief The record decl of the class that the base is a base of. 47 const CXXRecordDecl *Class; 48 49 /// \brief Identifies which base class subobject (of type 50 /// \c Base->getType()) this base path element refers to. 51 /// 52 /// This value is only valid if \c !Base->isVirtual(), because there 53 /// is no base numbering for the zero or one virtual bases of a 54 /// given type. 55 int SubobjectNumber; 56}; 57 58/// \brief Represents a path from a specific derived class 59/// (which is not represented as part of the path) to a particular 60/// (direct or indirect) base class subobject. 61/// 62/// Individual elements in the path are described by the \c CXXBasePathElement 63/// structure, which captures both the link from a derived class to one of its 64/// direct bases and identification describing which base class 65/// subobject is being used. 66struct CXXBasePath : public llvm::SmallVector<CXXBasePathElement, 4> { 67 /// \brief The set of declarations found inside this base class 68 /// subobject. 69 DeclContext::lookup_result Decls; 70}; 71 72/// BasePaths - Represents the set of paths from a derived class to 73/// one of its (direct or indirect) bases. For example, given the 74/// following class hierachy: 75/// 76/// @code 77/// class A { }; 78/// class B : public A { }; 79/// class C : public A { }; 80/// class D : public B, public C{ }; 81/// @endcode 82/// 83/// There are two potential BasePaths to represent paths from D to a 84/// base subobject of type A. One path is (D,0) -> (B,0) -> (A,0) 85/// and another is (D,0)->(C,0)->(A,1). These two paths actually 86/// refer to two different base class subobjects of the same type, 87/// so the BasePaths object refers to an ambiguous path. On the 88/// other hand, consider the following class hierarchy: 89/// 90/// @code 91/// class A { }; 92/// class B : public virtual A { }; 93/// class C : public virtual A { }; 94/// class D : public B, public C{ }; 95/// @endcode 96/// 97/// Here, there are two potential BasePaths again, (D, 0) -> (B, 0) 98/// -> (A,v) and (D, 0) -> (C, 0) -> (A, v), but since both of them 99/// refer to the same base class subobject of type A (the virtual 100/// one), there is no ambiguity. 101class CXXBasePaths { 102 /// \brief The type from which this search originated. 103 CXXRecordDecl *Origin; 104 105 /// Paths - The actual set of paths that can be taken from the 106 /// derived class to the same base class. 107 std::list<CXXBasePath> Paths; 108 109 /// ClassSubobjects - Records the class subobjects for each class 110 /// type that we've seen. The first element in the pair says 111 /// whether we found a path to a virtual base for that class type, 112 /// while the element contains the number of non-virtual base 113 /// class subobjects for that class type. The key of the map is 114 /// the cv-unqualified canonical type of the base class subobject. 115 std::map<QualType, std::pair<bool, unsigned>, QualTypeOrdering> 116 ClassSubobjects; 117 118 /// FindAmbiguities - Whether Sema::IsDerivedFrom should try find 119 /// ambiguous paths while it is looking for a path from a derived 120 /// type to a base type. 121 bool FindAmbiguities; 122 123 /// RecordPaths - Whether Sema::IsDerivedFrom should record paths 124 /// while it is determining whether there are paths from a derived 125 /// type to a base type. 126 bool RecordPaths; 127 128 /// DetectVirtual - Whether Sema::IsDerivedFrom should abort the search 129 /// if it finds a path that goes across a virtual base. The virtual class 130 /// is also recorded. 131 bool DetectVirtual; 132 133 /// ScratchPath - A BasePath that is used by Sema::IsDerivedFrom 134 /// to help build the set of paths. 135 CXXBasePath ScratchPath; 136 137 /// DetectedVirtual - The base class that is virtual. 138 const RecordType *DetectedVirtual; 139 140 /// \brief Array of the declarations that have been found. This 141 /// array is constructed only if needed, e.g., to iterate over the 142 /// results within LookupResult. 143 NamedDecl **DeclsFound; 144 unsigned NumDeclsFound; 145 146 friend class CXXRecordDecl; 147 148 void ComputeDeclsFound(); 149 150public: 151 typedef std::list<CXXBasePath>::const_iterator paths_iterator; 152 typedef NamedDecl **decl_iterator; 153 154 /// BasePaths - Construct a new BasePaths structure to record the 155 /// paths for a derived-to-base search. 156 explicit CXXBasePaths(bool FindAmbiguities = true, 157 bool RecordPaths = true, 158 bool DetectVirtual = true) 159 : FindAmbiguities(FindAmbiguities), RecordPaths(RecordPaths), 160 DetectVirtual(DetectVirtual), DetectedVirtual(0), DeclsFound(0), 161 NumDeclsFound(0) { } 162 163 ~CXXBasePaths() { delete [] DeclsFound; } 164 165 paths_iterator begin() const { return Paths.begin(); } 166 paths_iterator end() const { return Paths.end(); } 167 168 CXXBasePath& front() { return Paths.front(); } 169 const CXXBasePath& front() const { return Paths.front(); } 170 171 decl_iterator found_decls_begin(); 172 decl_iterator found_decls_end(); 173 174 /// \brief Determine whether the path from the most-derived type to the 175 /// given base type is ambiguous (i.e., it refers to multiple subobjects of 176 /// the same base type). 177 bool isAmbiguous(QualType BaseType); 178 179 /// \brief Whether we are finding multiple paths to detect ambiguities. 180 bool isFindingAmbiguities() const { return FindAmbiguities; } 181 182 /// \brief Whether we are recording paths. 183 bool isRecordingPaths() const { return RecordPaths; } 184 185 /// \brief Specify whether we should be recording paths or not. 186 void setRecordingPaths(bool RP) { RecordPaths = RP; } 187 188 /// \brief Whether we are detecting virtual bases. 189 bool isDetectingVirtual() const { return DetectVirtual; } 190 191 /// \brief The virtual base discovered on the path (if we are merely 192 /// detecting virtuals). 193 const RecordType* getDetectedVirtual() const { 194 return DetectedVirtual; 195 } 196 197 /// \brief Retrieve the type from which this base-paths search 198 /// began 199 CXXRecordDecl *getOrigin() const { return Origin; } 200 void setOrigin(CXXRecordDecl *Rec) { Origin = Rec; } 201 202 /// \brief Clear the base-paths results. 203 void clear(); 204 205 /// \brief Swap this data structure's contents with another CXXBasePaths 206 /// object. 207 void swap(CXXBasePaths &Other); 208}; 209 210} // end namespace clang 211 212#endif 213