 |
|
 |
|
| Files: |
1 |
|
Branches Taken: |
100.0% |
2 / 2 |
| Generated: |
2010-02-10 01:31 |
|
Branches Executed: |
100.0% |
2 / 2 |
| |
|
Line Coverage: |
100.0% |
21 / 21 |
| |
 |
|
 |
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 :
26 : namespace clang {
27 :
28 : class CXXBaseSpecifier;
29 : class CXXMethodDecl;
30 : class CXXRecordDecl;
31 : class 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.
40 10203: struct 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.
66 24010: class CXXBasePath : public llvm::SmallVector<CXXBasePathElement, 4> {
67 : public:
68 20570: CXXBasePath() : Access(AS_public) {}
69 :
70 : /// \brief The access along this inheritance path. This is only
71 : /// calculated when recording paths. AS_none is a special value
72 : /// used to indicate a path which permits no legal access.
73 : AccessSpecifier Access;
74 :
75 : /// \brief The set of declarations found inside this base class
76 : /// subobject.
77 : DeclContext::lookup_result Decls;
78 :
79 35: void clear() {
80 35: llvm::SmallVectorImpl<CXXBasePathElement>::clear();
81 35: Access = AS_public;
82 35: }
83 : };
84 :
85 : /// BasePaths - Represents the set of paths from a derived class to
86 : /// one of its (direct or indirect) bases. For example, given the
87 : /// following class hierachy:
88 : ///
89 : /// @code
90 : /// class A { };
91 : /// class B : public A { };
92 : /// class C : public A { };
93 : /// class D : public B, public C{ };
94 : /// @endcode
95 : ///
96 : /// There are two potential BasePaths to represent paths from D to a
97 : /// base subobject of type A. One path is (D,0) -> (B,0) -> (A,0)
98 : /// and another is (D,0)->(C,0)->(A,1). These two paths actually
99 : /// refer to two different base class subobjects of the same type,
100 : /// so the BasePaths object refers to an ambiguous path. On the
101 : /// other hand, consider the following class hierarchy:
102 : ///
103 : /// @code
104 : /// class A { };
105 : /// class B : public virtual A { };
106 : /// class C : public virtual A { };
107 : /// class D : public B, public C{ };
108 : /// @endcode
109 : ///
110 : /// Here, there are two potential BasePaths again, (D, 0) -> (B, 0)
111 : /// -> (A,v) and (D, 0) -> (C, 0) -> (A, v), but since both of them
112 : /// refer to the same base class subobject of type A (the virtual
113 : /// one), there is no ambiguity.
114 : class CXXBasePaths {
115 : /// \brief The type from which this search originated.
116 : CXXRecordDecl *Origin;
117 :
118 : /// Paths - The actual set of paths that can be taken from the
119 : /// derived class to the same base class.
120 : std::list<CXXBasePath> Paths;
121 :
122 : /// ClassSubobjects - Records the class subobjects for each class
123 : /// type that we've seen. The first element in the pair says
124 : /// whether we found a path to a virtual base for that class type,
125 : /// while the element contains the number of non-virtual base
126 : /// class subobjects for that class type. The key of the map is
127 : /// the cv-unqualified canonical type of the base class subobject.
128 : std::map<QualType, std::pair<bool, unsigned>, QualTypeOrdering>
129 : 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 : NamedDecl **DeclsFound;
157 : unsigned NumDeclsFound;
158 :
159 : friend class CXXRecordDecl;
160 :
161 : void ComputeDeclsFound();
162 :
163 : public:
164 : typedef std::list<CXXBasePath>::const_iterator paths_iterator;
165 : typedef NamedDecl **decl_iterator;
166 :
167 : /// BasePaths - Construct a new BasePaths structure to record the
168 : /// paths for a derived-to-base search.
169 : explicit CXXBasePaths(bool FindAmbiguities = true,
170 : bool RecordPaths = true,
171 20570: bool DetectVirtual = true)
172 : : FindAmbiguities(FindAmbiguities), RecordPaths(RecordPaths),
173 : DetectVirtual(DetectVirtual), DetectedVirtual(0), DeclsFound(0),
174 20570: NumDeclsFound(0) { }
175 :
157: branch 0 taken
20413: branch 1 taken
176 20570: ~CXXBasePaths() { delete [] DeclsFound; }
177 :
178 796: paths_iterator begin() const { return Paths.begin(); }
179 896: paths_iterator end() const { return Paths.end(); }
180 :
181 1134: CXXBasePath& front() { return Paths.front(); }
182 : const CXXBasePath& front() const { return Paths.front(); }
183 :
184 : decl_iterator found_decls_begin();
185 : decl_iterator found_decls_end();
186 :
187 : /// \brief Determine whether the path from the most-derived type to the
188 : /// given base type is ambiguous (i.e., it refers to multiple subobjects of
189 : /// the same base type).
190 : bool isAmbiguous(QualType BaseType);
191 :
192 : /// \brief Whether we are finding multiple paths to detect ambiguities.
193 1827: bool isFindingAmbiguities() const { return FindAmbiguities; }
194 :
195 : /// \brief Whether we are recording paths.
196 20138: bool isRecordingPaths() const { return RecordPaths; }
197 :
198 : /// \brief Specify whether we should be recording paths or not.
199 25: void setRecordingPaths(bool RP) { RecordPaths = RP; }
200 :
201 : /// \brief Whether we are detecting virtual bases.
202 1848: bool isDetectingVirtual() const { return DetectVirtual; }
203 :
204 : /// \brief The virtual base discovered on the path (if we are merely
205 : /// detecting virtuals).
206 284: const RecordType* getDetectedVirtual() const {
207 284: return DetectedVirtual;
208 : }
209 :
210 : /// \brief Retrieve the type from which this base-paths search
211 : /// began
212 54: CXXRecordDecl *getOrigin() const { return Origin; }
213 9434: void setOrigin(CXXRecordDecl *Rec) { Origin = Rec; }
214 :
215 : /// \brief Clear the base-paths results.
216 : void clear();
217 :
218 : /// \brief Swap this data structure's contents with another CXXBasePaths
219 : /// object.
220 : void swap(CXXBasePaths &Other);
221 : };
222 :
223 : } // end namespace clang
224 :
225 : #endif
Generated: 2010-02-10 01:31 by zcov