blob: d08d7f8a4b3e30f4175f1b35b2b62867f109caa6 [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
Anders Carlsson77bd57c2009-03-27 18:54:29 +000042 /// Class - The record decl of the class that the base is a base of.
Anders Carlsson14734f72009-03-28 04:17:27 +000043 const CXXRecordDecl *Class;
Mike Stump1eb44332009-09-09 15:08:12 +000044
Douglas Gregor94b1dd22008-10-24 04:54:22 +000045 /// SubobjectNumber - Identifies which base class subobject (of type
Mike Stump1eb44332009-09-09 15:08:12 +000046 /// @c Base->getType()) this base path element refers to. This
Douglas Gregor94b1dd22008-10-24 04:54:22 +000047 /// value is only valid if @c !Base->isVirtual(), because there
Mike Stump1eb44332009-09-09 15:08:12 +000048 /// is no base numbering for the zero or one virtual bases of a
Douglas Gregor94b1dd22008-10-24 04:54:22 +000049 /// given type.
50 int SubobjectNumber;
51 };
52
53 /// BasePath - Represents a path from a specific derived class
54 /// (which is not represented as part of the path) to a particular
Douglas Gregor7176fff2009-01-15 00:26:24 +000055 /// (direct or indirect) base class subobject that contains some
56 /// number of declarations with the same name. Individual elements
Douglas Gregor94b1dd22008-10-24 04:54:22 +000057 /// in the path are described by the BasePathElement structure,
58 /// which captures both the link from a derived class to one of its
59 /// direct bases and identification describing which base class
Douglas Gregor7176fff2009-01-15 00:26:24 +000060 /// subobject is being used.
61 struct BasePath : public llvm::SmallVector<BasePathElement, 4> {
62 /// Decls - The set of declarations found inside this base class
63 /// subobject.
64 DeclContext::lookup_result Decls;
65 };
Douglas Gregor94b1dd22008-10-24 04:54:22 +000066
67 /// BasePaths - Represents the set of paths from a derived class to
68 /// one of its (direct or indirect) bases. For example, given the
69 /// following class hierachy:
70 ///
71 /// @code
72 /// class A { };
73 /// class B : public A { };
74 /// class C : public A { };
75 /// class D : public B, public C{ };
76 /// @endcode
77 ///
78 /// There are two potential BasePaths to represent paths from D to a
79 /// base subobject of type A. One path is (D,0) -> (B,0) -> (A,0)
80 /// and another is (D,0)->(C,0)->(A,1). These two paths actually
81 /// refer to two different base class subobjects of the same type,
82 /// so the BasePaths object refers to an ambiguous path. On the
83 /// other hand, consider the following class hierarchy:
84 ///
85 /// @code
86 /// class A { };
87 /// class B : public virtual A { };
88 /// class C : public virtual A { };
89 /// class D : public B, public C{ };
90 /// @endcode
91 ///
92 /// Here, there are two potential BasePaths again, (D, 0) -> (B, 0)
93 /// -> (A,v) and (D, 0) -> (C, 0) -> (A, v), but since both of them
94 /// refer to the same base class subobject of type A (the virtual
95 /// one), there is no ambiguity.
96 class BasePaths {
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +000097 /// Origin - The type from which this search originated.
98 QualType Origin;
99
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000100 /// Paths - The actual set of paths that can be taken from the
101 /// derived class to the same base class.
102 std::list<BasePath> Paths;
103
104 /// ClassSubobjects - Records the class subobjects for each class
105 /// type that we've seen. The first element in the pair says
106 /// whether we found a path to a virtual base for that class type,
107 /// while the element contains the number of non-virtual base
108 /// class subobjects for that class type. The key of the map is
109 /// the cv-unqualified canonical type of the base class subobject.
Mike Stump1eb44332009-09-09 15:08:12 +0000110 std::map<QualType, std::pair<bool, unsigned>, QualTypeOrdering>
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000111 ClassSubobjects;
112
Sebastian Redl07779722008-10-31 14:43:28 +0000113 /// FindAmbiguities - Whether Sema::IsDerivedFrom should try find
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000114 /// ambiguous paths while it is looking for a path from a derived
115 /// type to a base type.
116 bool FindAmbiguities;
117
Sebastian Redl07779722008-10-31 14:43:28 +0000118 /// RecordPaths - Whether Sema::IsDerivedFrom should record paths
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000119 /// while it is determining whether there are paths from a derived
120 /// type to a base type.
121 bool RecordPaths;
122
Sebastian Redl07779722008-10-31 14:43:28 +0000123 /// DetectVirtual - Whether Sema::IsDerivedFrom should abort the search
124 /// if it finds a path that goes across a virtual base. The virtual class
125 /// is also recorded.
126 bool DetectVirtual;
127
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000128 /// ScratchPath - A BasePath that is used by Sema::IsDerivedFrom
129 /// to help build the set of paths.
130 BasePath ScratchPath;
131
Sebastian Redl07779722008-10-31 14:43:28 +0000132 /// DetectedVirtual - The base class that is virtual.
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000133 const RecordType *DetectedVirtual;
Sebastian Redl07779722008-10-31 14:43:28 +0000134
Douglas Gregor31a19b62009-04-01 21:51:26 +0000135 /// \brief Array of the declarations that have been found. This
136 /// array is constructed only if needed, e.g., to iterate over the
137 /// results within LookupResult.
138 NamedDecl **DeclsFound;
139 unsigned NumDeclsFound;
140
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000141 friend class Sema;
142
Douglas Gregor31a19b62009-04-01 21:51:26 +0000143 void ComputeDeclsFound();
144
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000145 public:
146 typedef std::list<BasePath>::const_iterator paths_iterator;
Anders Carlsson6a502a32009-05-13 21:22:06 +0000147 typedef NamedDecl **decl_iterator;
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000149 /// BasePaths - Construct a new BasePaths structure to record the
150 /// paths for a derived-to-base search.
Sebastian Redl07779722008-10-31 14:43:28 +0000151 explicit BasePaths(bool FindAmbiguities = true,
152 bool RecordPaths = true,
153 bool DetectVirtual = true)
154 : FindAmbiguities(FindAmbiguities), RecordPaths(RecordPaths),
Douglas Gregor31a19b62009-04-01 21:51:26 +0000155 DetectVirtual(DetectVirtual), DetectedVirtual(0), DeclsFound(0),
Mike Stump1eb44332009-09-09 15:08:12 +0000156 NumDeclsFound(0) { }
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000157
Douglas Gregor31a19b62009-04-01 21:51:26 +0000158 ~BasePaths() { delete [] DeclsFound; }
159
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000160 paths_iterator begin() const { return Paths.begin(); }
161 paths_iterator end() const { return Paths.end(); }
162
Douglas Gregor7176fff2009-01-15 00:26:24 +0000163 BasePath& front() { return Paths.front(); }
164 const BasePath& front() const { return Paths.front(); }
165
Anders Carlsson6a502a32009-05-13 21:22:06 +0000166 decl_iterator found_decls_begin();
167 decl_iterator found_decls_end();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000168
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000169 bool isAmbiguous(QualType BaseType);
170
171 /// isFindingAmbiguities - Whether we are finding multiple paths
172 /// to detect ambiguities.
173 bool isFindingAmbiguities() const { return FindAmbiguities; }
174
175 /// isRecordingPaths - Whether we are recording paths.
176 bool isRecordingPaths() const { return RecordPaths; }
177
178 /// setRecordingPaths - Specify whether we should be recording
179 /// paths or not.
180 void setRecordingPaths(bool RP) { RecordPaths = RP; }
181
Sebastian Redl07779722008-10-31 14:43:28 +0000182 /// isDetectingVirtual - Whether we are detecting virtual bases.
183 bool isDetectingVirtual() const { return DetectVirtual; }
184
185 /// getDetectedVirtual - The virtual base discovered on the path.
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000186 const RecordType* getDetectedVirtual() const {
Sebastian Redl07779722008-10-31 14:43:28 +0000187 return DetectedVirtual;
188 }
189
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +0000190 /// @brief Retrieve the type from which this base-paths search
191 /// began
192 QualType getOrigin() const { return Origin; }
193 void setOrigin(QualType Type) { Origin = Type; }
194
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000195 void clear();
Douglas Gregor7176fff2009-01-15 00:26:24 +0000196
197 void swap(BasePaths &Other);
198 };
199
200 /// MemberLookupCriteria - Criteria for performing lookup of a
201 /// member of a C++ class. Objects of this type are used to direct
202 /// Sema::LookupCXXClassMember.
203 struct MemberLookupCriteria {
Anders Carlsson9e8a7222009-05-29 23:42:05 +0000204 /// LookupKind - the kind of lookup we're doing.
205 enum LookupKind {
206 LK_Base,
Anders Carlssond12ef8d2009-05-30 00:52:53 +0000207 LK_NamedMember,
208 LK_OverriddenMember
Anders Carlsson9e8a7222009-05-29 23:42:05 +0000209 };
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Douglas Gregor7176fff2009-01-15 00:26:24 +0000211 /// MemberLookupCriteria - Constructs member lookup criteria to
212 /// search for a base class of type Base.
Mike Stump1eb44332009-09-09 15:08:12 +0000213 explicit MemberLookupCriteria(QualType Base)
Anders Carlsson9e8a7222009-05-29 23:42:05 +0000214 : Kind(LK_Base), Base(Base) { }
Douglas Gregor7176fff2009-01-15 00:26:24 +0000215
216 /// MemberLookupCriteria - Constructs member lookup criteria to
217 /// search for a class member with the given Name.
Mike Stump1eb44332009-09-09 15:08:12 +0000218 explicit MemberLookupCriteria(DeclarationName Name,
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000219 Sema::LookupNameKind NameKind,
Mike Stump1eb44332009-09-09 15:08:12 +0000220 unsigned IDNS)
Anders Carlsson9e8a7222009-05-29 23:42:05 +0000221 : Kind(LK_NamedMember), Name(Name), NameKind(NameKind), IDNS(IDNS) { }
Douglas Gregor7176fff2009-01-15 00:26:24 +0000222
Anders Carlssond12ef8d2009-05-30 00:52:53 +0000223 explicit MemberLookupCriteria(CXXMethodDecl *MD)
224 : Kind(LK_OverriddenMember), Method(MD) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Anders Carlsson9e8a7222009-05-29 23:42:05 +0000226 /// Kind - The kind of lookup we're doing.
227 /// LK_Base if we are looking for a base class (whose
228 /// type is Base). LK_NamedMember if we are looking for a named member of
Douglas Gregor7176fff2009-01-15 00:26:24 +0000229 /// the class (with the name Name).
Anders Carlsson9e8a7222009-05-29 23:42:05 +0000230 LookupKind Kind;
Douglas Gregor7176fff2009-01-15 00:26:24 +0000231
232 /// Base - The type of the base class we're searching for, if
233 /// LookupBase is true.
234 QualType Base;
235
236 /// Name - The name of the member we're searching for, if
237 /// LookupBase is false.
238 DeclarationName Name;
239
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000240 Sema::LookupNameKind NameKind;
241 unsigned IDNS;
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Anders Carlssond12ef8d2009-05-30 00:52:53 +0000243 CXXMethodDecl *Method;
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000244 };
245}
246
247#endif