blob: 8d008a2ce61f2324e9ed90269fac82817d9df4dd [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
Sebastian Redl22460502009-02-07 00:15:38 +000019#include "Sema.h"
Douglas Gregor7176fff2009-01-15 00:26:24 +000020#include "clang/AST/DeclarationName.h"
21#include "clang/AST/DeclBase.h"
Douglas Gregor94b1dd22008-10-24 04:54:22 +000022#include "clang/AST/Type.h"
23#include "clang/AST/TypeOrdering.h"
24#include "llvm/ADT/SmallVector.h"
25#include <list>
26#include <map>
27
28namespace clang {
Douglas Gregor94b1dd22008-10-24 04:54:22 +000029 class CXXBaseSpecifier;
30
31 /// BasePathElement - An element in a path from a derived class to a
32 /// base class. Each step in the path references the link from a
33 /// derived class to one of its direct base classes, along with a
34 /// base "number" that identifies which base subobject of the
35 /// original derived class we are referencing.
36 struct BasePathElement {
37 /// Base - The base specifier that states the link from a derived
38 /// class to a base class, which will be followed by this base
39 /// path element.
40 const CXXBaseSpecifier *Base;
41
42 /// SubobjectNumber - Identifies which base class subobject (of type
43 /// @c Base->getType()) this base path element refers to. This
44 /// value is only valid if @c !Base->isVirtual(), because there
45 /// is no base numbering for the zero or one virtual bases of a
46 /// given type.
47 int SubobjectNumber;
48 };
49
50 /// BasePath - Represents a path from a specific derived class
51 /// (which is not represented as part of the path) to a particular
Douglas Gregor7176fff2009-01-15 00:26:24 +000052 /// (direct or indirect) base class subobject that contains some
53 /// number of declarations with the same name. Individual elements
Douglas Gregor94b1dd22008-10-24 04:54:22 +000054 /// in the path are described by the BasePathElement structure,
55 /// which captures both the link from a derived class to one of its
56 /// direct bases and identification describing which base class
Douglas Gregor7176fff2009-01-15 00:26:24 +000057 /// subobject is being used.
58 struct BasePath : public llvm::SmallVector<BasePathElement, 4> {
59 /// Decls - The set of declarations found inside this base class
60 /// subobject.
61 DeclContext::lookup_result Decls;
62 };
Douglas Gregor94b1dd22008-10-24 04:54:22 +000063
64 /// BasePaths - Represents the set of paths from a derived class to
65 /// one of its (direct or indirect) bases. For example, given the
66 /// following class hierachy:
67 ///
68 /// @code
69 /// class A { };
70 /// class B : public A { };
71 /// class C : public A { };
72 /// class D : public B, public C{ };
73 /// @endcode
74 ///
75 /// There are two potential BasePaths to represent paths from D to a
76 /// base subobject of type A. One path is (D,0) -> (B,0) -> (A,0)
77 /// and another is (D,0)->(C,0)->(A,1). These two paths actually
78 /// refer to two different base class subobjects of the same type,
79 /// so the BasePaths object refers to an ambiguous path. On the
80 /// other hand, consider the following class hierarchy:
81 ///
82 /// @code
83 /// class A { };
84 /// class B : public virtual A { };
85 /// class C : public virtual A { };
86 /// class D : public B, public C{ };
87 /// @endcode
88 ///
89 /// Here, there are two potential BasePaths again, (D, 0) -> (B, 0)
90 /// -> (A,v) and (D, 0) -> (C, 0) -> (A, v), but since both of them
91 /// refer to the same base class subobject of type A (the virtual
92 /// one), there is no ambiguity.
93 class BasePaths {
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +000094 /// Origin - The type from which this search originated.
95 QualType Origin;
96
Douglas Gregor94b1dd22008-10-24 04:54:22 +000097 /// Paths - The actual set of paths that can be taken from the
98 /// derived class to the same base class.
99 std::list<BasePath> Paths;
100
101 /// ClassSubobjects - Records the class subobjects for each class
102 /// type that we've seen. The first element in the pair says
103 /// whether we found a path to a virtual base for that class type,
104 /// while the element contains the number of non-virtual base
105 /// class subobjects for that class type. The key of the map is
106 /// the cv-unqualified canonical type of the base class subobject.
107 std::map<QualType, std::pair<bool, unsigned>, QualTypeOrdering>
108 ClassSubobjects;
109
Sebastian Redl07779722008-10-31 14:43:28 +0000110 /// FindAmbiguities - Whether Sema::IsDerivedFrom should try find
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000111 /// ambiguous paths while it is looking for a path from a derived
112 /// type to a base type.
113 bool FindAmbiguities;
114
Sebastian Redl07779722008-10-31 14:43:28 +0000115 /// RecordPaths - Whether Sema::IsDerivedFrom should record paths
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000116 /// while it is determining whether there are paths from a derived
117 /// type to a base type.
118 bool RecordPaths;
119
Sebastian Redl07779722008-10-31 14:43:28 +0000120 /// DetectVirtual - Whether Sema::IsDerivedFrom should abort the search
121 /// if it finds a path that goes across a virtual base. The virtual class
122 /// is also recorded.
123 bool DetectVirtual;
124
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000125 /// ScratchPath - A BasePath that is used by Sema::IsDerivedFrom
126 /// to help build the set of paths.
127 BasePath ScratchPath;
128
Sebastian Redl07779722008-10-31 14:43:28 +0000129 /// DetectedVirtual - The base class that is virtual.
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000130 const RecordType *DetectedVirtual;
Sebastian Redl07779722008-10-31 14:43:28 +0000131
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000132 friend class Sema;
133
134 public:
135 typedef std::list<BasePath>::const_iterator paths_iterator;
136
137 /// BasePaths - Construct a new BasePaths structure to record the
138 /// paths for a derived-to-base search.
Sebastian Redl07779722008-10-31 14:43:28 +0000139 explicit BasePaths(bool FindAmbiguities = true,
140 bool RecordPaths = true,
141 bool DetectVirtual = true)
142 : FindAmbiguities(FindAmbiguities), RecordPaths(RecordPaths),
143 DetectVirtual(DetectVirtual), DetectedVirtual(0)
144 {}
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000145
146 paths_iterator begin() const { return Paths.begin(); }
147 paths_iterator end() const { return Paths.end(); }
148
Douglas Gregor7176fff2009-01-15 00:26:24 +0000149 BasePath& front() { return Paths.front(); }
150 const BasePath& front() const { return Paths.front(); }
151
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000152 bool isAmbiguous(QualType BaseType);
153
154 /// isFindingAmbiguities - Whether we are finding multiple paths
155 /// to detect ambiguities.
156 bool isFindingAmbiguities() const { return FindAmbiguities; }
157
158 /// isRecordingPaths - Whether we are recording paths.
159 bool isRecordingPaths() const { return RecordPaths; }
160
161 /// setRecordingPaths - Specify whether we should be recording
162 /// paths or not.
163 void setRecordingPaths(bool RP) { RecordPaths = RP; }
164
Sebastian Redl07779722008-10-31 14:43:28 +0000165 /// isDetectingVirtual - Whether we are detecting virtual bases.
166 bool isDetectingVirtual() const { return DetectVirtual; }
167
168 /// getDetectedVirtual - The virtual base discovered on the path.
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000169 const RecordType* getDetectedVirtual() const {
Sebastian Redl07779722008-10-31 14:43:28 +0000170 return DetectedVirtual;
171 }
172
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +0000173 /// @brief Retrieve the type from which this base-paths search
174 /// began
175 QualType getOrigin() const { return Origin; }
176 void setOrigin(QualType Type) { Origin = Type; }
177
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000178 void clear();
Douglas Gregor7176fff2009-01-15 00:26:24 +0000179
180 void swap(BasePaths &Other);
181 };
182
183 /// MemberLookupCriteria - Criteria for performing lookup of a
184 /// member of a C++ class. Objects of this type are used to direct
185 /// Sema::LookupCXXClassMember.
186 struct MemberLookupCriteria {
187 /// MemberLookupCriteria - Constructs member lookup criteria to
188 /// search for a base class of type Base.
189 explicit MemberLookupCriteria(QualType Base)
190 : LookupBase(true), Base(Base) { }
191
192 /// MemberLookupCriteria - Constructs member lookup criteria to
193 /// search for a class member with the given Name.
194 explicit MemberLookupCriteria(DeclarationName Name,
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000195 Sema::LookupNameKind NameKind,
196 unsigned IDNS)
197 : LookupBase(false), Name(Name), NameKind(NameKind), IDNS(IDNS) { }
Douglas Gregor7176fff2009-01-15 00:26:24 +0000198
199 /// LookupBase - True if we are looking for a base class (whose
200 /// type is Base). If false, we are looking for a named member of
201 /// the class (with the name Name).
202 bool LookupBase;
203
204 /// Base - The type of the base class we're searching for, if
205 /// LookupBase is true.
206 QualType Base;
207
208 /// Name - The name of the member we're searching for, if
209 /// LookupBase is false.
210 DeclarationName Name;
211
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000212 Sema::LookupNameKind NameKind;
213 unsigned IDNS;
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000214 };
215}
216
217#endif