blob: eb169a2982bb366c87aeb9c756f306a096447993 [file] [log] [blame]
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001//===------ SemaInherit.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 Sema data structures that help analyse C++
11// inheritance semantics, including searching the inheritance
12// hierarchy.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CLANG_SEMA_INHERIT_H
17#define LLVM_CLANG_SEMA_INHERIT_H
18
Douglas Gregor7176fff2009-01-15 00:26:24 +000019#include "clang/AST/DeclarationName.h"
20#include "clang/AST/DeclBase.h"
Douglas Gregor94b1dd22008-10-24 04:54:22 +000021#include "clang/AST/Type.h"
22#include "clang/AST/TypeOrdering.h"
23#include "llvm/ADT/SmallVector.h"
24#include <list>
25#include <map>
26
27namespace clang {
28 class Sema;
29 class CXXBaseSpecifier;
Sebastian Redl07779722008-10-31 14:43:28 +000030 class CXXRecordType;
Douglas Gregor94b1dd22008-10-24 04:54:22 +000031
32 /// BasePathElement - An element in a path from a derived class to a
33 /// base class. Each step in the path references the link from a
34 /// derived class to one of its direct base classes, along with a
35 /// base "number" that identifies which base subobject of the
36 /// original derived class we are referencing.
37 struct BasePathElement {
38 /// Base - The base specifier that states the link from a derived
39 /// class to a base class, which will be followed by this base
40 /// path element.
41 const CXXBaseSpecifier *Base;
42
43 /// SubobjectNumber - Identifies which base class subobject (of type
44 /// @c Base->getType()) this base path element refers to. This
45 /// value is only valid if @c !Base->isVirtual(), because there
46 /// is no base numbering for the zero or one virtual bases of a
47 /// given type.
48 int SubobjectNumber;
49 };
50
51 /// BasePath - Represents a path from a specific derived class
52 /// (which is not represented as part of the path) to a particular
Douglas Gregor7176fff2009-01-15 00:26:24 +000053 /// (direct or indirect) base class subobject that contains some
54 /// number of declarations with the same name. Individual elements
Douglas Gregor94b1dd22008-10-24 04:54:22 +000055 /// in the path are described by the BasePathElement structure,
56 /// which captures both the link from a derived class to one of its
57 /// direct bases and identification describing which base class
Douglas Gregor7176fff2009-01-15 00:26:24 +000058 /// subobject is being used.
59 struct BasePath : public llvm::SmallVector<BasePathElement, 4> {
60 /// Decls - The set of declarations found inside this base class
61 /// subobject.
62 DeclContext::lookup_result Decls;
63 };
Douglas Gregor94b1dd22008-10-24 04:54:22 +000064
65 /// BasePaths - Represents the set of paths from a derived class to
66 /// one of its (direct or indirect) bases. For example, given the
67 /// following class hierachy:
68 ///
69 /// @code
70 /// class A { };
71 /// class B : public A { };
72 /// class C : public A { };
73 /// class D : public B, public C{ };
74 /// @endcode
75 ///
76 /// There are two potential BasePaths to represent paths from D to a
77 /// base subobject of type A. One path is (D,0) -> (B,0) -> (A,0)
78 /// and another is (D,0)->(C,0)->(A,1). These two paths actually
79 /// refer to two different base class subobjects of the same type,
80 /// so the BasePaths object refers to an ambiguous path. On the
81 /// other hand, consider the following class hierarchy:
82 ///
83 /// @code
84 /// class A { };
85 /// class B : public virtual A { };
86 /// class C : public virtual A { };
87 /// class D : public B, public C{ };
88 /// @endcode
89 ///
90 /// Here, there are two potential BasePaths again, (D, 0) -> (B, 0)
91 /// -> (A,v) and (D, 0) -> (C, 0) -> (A, v), but since both of them
92 /// refer to the same base class subobject of type A (the virtual
93 /// one), there is no ambiguity.
94 class BasePaths {
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +000095 /// Origin - The type from which this search originated.
96 QualType Origin;
97
Douglas Gregor94b1dd22008-10-24 04:54:22 +000098 /// Paths - The actual set of paths that can be taken from the
99 /// derived class to the same base class.
100 std::list<BasePath> Paths;
101
102 /// ClassSubobjects - Records the class subobjects for each class
103 /// type that we've seen. The first element in the pair says
104 /// whether we found a path to a virtual base for that class type,
105 /// while the element contains the number of non-virtual base
106 /// class subobjects for that class type. The key of the map is
107 /// the cv-unqualified canonical type of the base class subobject.
108 std::map<QualType, std::pair<bool, unsigned>, QualTypeOrdering>
109 ClassSubobjects;
110
Sebastian Redl07779722008-10-31 14:43:28 +0000111 /// FindAmbiguities - Whether Sema::IsDerivedFrom should try find
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000112 /// ambiguous paths while it is looking for a path from a derived
113 /// type to a base type.
114 bool FindAmbiguities;
115
Sebastian Redl07779722008-10-31 14:43:28 +0000116 /// RecordPaths - Whether Sema::IsDerivedFrom should record paths
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000117 /// while it is determining whether there are paths from a derived
118 /// type to a base type.
119 bool RecordPaths;
120
Sebastian Redl07779722008-10-31 14:43:28 +0000121 /// DetectVirtual - Whether Sema::IsDerivedFrom should abort the search
122 /// if it finds a path that goes across a virtual base. The virtual class
123 /// is also recorded.
124 bool DetectVirtual;
125
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000126 /// ScratchPath - A BasePath that is used by Sema::IsDerivedFrom
127 /// to help build the set of paths.
128 BasePath ScratchPath;
129
Sebastian Redl07779722008-10-31 14:43:28 +0000130 /// DetectedVirtual - The base class that is virtual.
131 const CXXRecordType *DetectedVirtual;
132
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000133 friend class Sema;
134
135 public:
136 typedef std::list<BasePath>::const_iterator paths_iterator;
137
138 /// BasePaths - Construct a new BasePaths structure to record the
139 /// paths for a derived-to-base search.
Sebastian Redl07779722008-10-31 14:43:28 +0000140 explicit BasePaths(bool FindAmbiguities = true,
141 bool RecordPaths = true,
142 bool DetectVirtual = true)
143 : FindAmbiguities(FindAmbiguities), RecordPaths(RecordPaths),
144 DetectVirtual(DetectVirtual), DetectedVirtual(0)
145 {}
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000146
147 paths_iterator begin() const { return Paths.begin(); }
148 paths_iterator end() const { return Paths.end(); }
149
Douglas Gregor7176fff2009-01-15 00:26:24 +0000150 BasePath& front() { return Paths.front(); }
151 const BasePath& front() const { return Paths.front(); }
152
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000153 bool isAmbiguous(QualType BaseType);
154
155 /// isFindingAmbiguities - Whether we are finding multiple paths
156 /// to detect ambiguities.
157 bool isFindingAmbiguities() const { return FindAmbiguities; }
158
159 /// isRecordingPaths - Whether we are recording paths.
160 bool isRecordingPaths() const { return RecordPaths; }
161
162 /// setRecordingPaths - Specify whether we should be recording
163 /// paths or not.
164 void setRecordingPaths(bool RP) { RecordPaths = RP; }
165
Sebastian Redl07779722008-10-31 14:43:28 +0000166 /// isDetectingVirtual - Whether we are detecting virtual bases.
167 bool isDetectingVirtual() const { return DetectVirtual; }
168
169 /// getDetectedVirtual - The virtual base discovered on the path.
170 const CXXRecordType* getDetectedVirtual() const {
171 return DetectedVirtual;
172 }
173
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +0000174 /// @brief Retrieve the type from which this base-paths search
175 /// began
176 QualType getOrigin() const { return Origin; }
177 void setOrigin(QualType Type) { Origin = Type; }
178
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000179 void clear();
Douglas Gregor7176fff2009-01-15 00:26:24 +0000180
181 void swap(BasePaths &Other);
182 };
183
184 /// MemberLookupCriteria - Criteria for performing lookup of a
185 /// member of a C++ class. Objects of this type are used to direct
186 /// Sema::LookupCXXClassMember.
187 struct MemberLookupCriteria {
188 /// MemberLookupCriteria - Constructs member lookup criteria to
189 /// search for a base class of type Base.
190 explicit MemberLookupCriteria(QualType Base)
191 : LookupBase(true), Base(Base) { }
192
193 /// MemberLookupCriteria - Constructs member lookup criteria to
194 /// search for a class member with the given Name.
195 explicit MemberLookupCriteria(DeclarationName Name,
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000196 Sema::LookupNameKind NameKind,
197 unsigned IDNS)
198 : LookupBase(false), Name(Name), NameKind(NameKind), IDNS(IDNS) { }
Douglas Gregor7176fff2009-01-15 00:26:24 +0000199
200 /// LookupBase - True if we are looking for a base class (whose
201 /// type is Base). If false, we are looking for a named member of
202 /// the class (with the name Name).
203 bool LookupBase;
204
205 /// Base - The type of the base class we're searching for, if
206 /// LookupBase is true.
207 QualType Base;
208
209 /// Name - The name of the member we're searching for, if
210 /// LookupBase is false.
211 DeclarationName Name;
212
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000213 Sema::LookupNameKind NameKind;
214 unsigned IDNS;
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000215 };
216}
217
218#endif