blob: b1e791a17ba34d1ffa51dcd37b289ed664936c14 [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;
Anders Carlsson77bd57c2009-03-27 18:54:29 +000044
Douglas Gregor94b1dd22008-10-24 04:54:22 +000045 /// SubobjectNumber - Identifies which base class subobject (of type
46 /// @c Base->getType()) this base path element refers to. This
47 /// value is only valid if @c !Base->isVirtual(), because there
48 /// is no base numbering for the zero or one virtual bases of a
49 /// 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.
110 std::map<QualType, std::pair<bool, unsigned>, QualTypeOrdering>
111 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;
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000148
149 /// 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),
156 NumDeclsFound(0)
Sebastian Redl07779722008-10-31 14:43:28 +0000157 {}
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000158
Douglas Gregor31a19b62009-04-01 21:51:26 +0000159 ~BasePaths() { delete [] DeclsFound; }
160
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000161 paths_iterator begin() const { return Paths.begin(); }
162 paths_iterator end() const { return Paths.end(); }
163
Douglas Gregor7176fff2009-01-15 00:26:24 +0000164 BasePath& front() { return Paths.front(); }
165 const BasePath& front() const { return Paths.front(); }
166
Anders Carlsson6a502a32009-05-13 21:22:06 +0000167 decl_iterator found_decls_begin();
168 decl_iterator found_decls_end();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000169
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000170 bool isAmbiguous(QualType BaseType);
171
172 /// isFindingAmbiguities - Whether we are finding multiple paths
173 /// to detect ambiguities.
174 bool isFindingAmbiguities() const { return FindAmbiguities; }
175
176 /// isRecordingPaths - Whether we are recording paths.
177 bool isRecordingPaths() const { return RecordPaths; }
178
179 /// setRecordingPaths - Specify whether we should be recording
180 /// paths or not.
181 void setRecordingPaths(bool RP) { RecordPaths = RP; }
182
Sebastian Redl07779722008-10-31 14:43:28 +0000183 /// isDetectingVirtual - Whether we are detecting virtual bases.
184 bool isDetectingVirtual() const { return DetectVirtual; }
185
186 /// getDetectedVirtual - The virtual base discovered on the path.
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000187 const RecordType* getDetectedVirtual() const {
Sebastian Redl07779722008-10-31 14:43:28 +0000188 return DetectedVirtual;
189 }
190
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +0000191 /// @brief Retrieve the type from which this base-paths search
192 /// began
193 QualType getOrigin() const { return Origin; }
194 void setOrigin(QualType Type) { Origin = Type; }
195
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000196 void clear();
Douglas Gregor7176fff2009-01-15 00:26:24 +0000197
198 void swap(BasePaths &Other);
199 };
200
201 /// MemberLookupCriteria - Criteria for performing lookup of a
202 /// member of a C++ class. Objects of this type are used to direct
203 /// Sema::LookupCXXClassMember.
204 struct MemberLookupCriteria {
Anders Carlsson9e8a7222009-05-29 23:42:05 +0000205 /// LookupKind - the kind of lookup we're doing.
206 enum LookupKind {
207 LK_Base,
Anders Carlssond12ef8d2009-05-30 00:52:53 +0000208 LK_NamedMember,
209 LK_OverriddenMember
Anders Carlsson9e8a7222009-05-29 23:42:05 +0000210 };
211
Douglas Gregor7176fff2009-01-15 00:26:24 +0000212 /// MemberLookupCriteria - Constructs member lookup criteria to
213 /// search for a base class of type Base.
214 explicit MemberLookupCriteria(QualType Base)
Anders Carlsson9e8a7222009-05-29 23:42:05 +0000215 : Kind(LK_Base), Base(Base) { }
Douglas Gregor7176fff2009-01-15 00:26:24 +0000216
217 /// MemberLookupCriteria - Constructs member lookup criteria to
218 /// search for a class member with the given Name.
219 explicit MemberLookupCriteria(DeclarationName Name,
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000220 Sema::LookupNameKind NameKind,
221 unsigned IDNS)
Anders Carlsson9e8a7222009-05-29 23:42:05 +0000222 : Kind(LK_NamedMember), Name(Name), NameKind(NameKind), IDNS(IDNS) { }
Douglas Gregor7176fff2009-01-15 00:26:24 +0000223
Anders Carlssond12ef8d2009-05-30 00:52:53 +0000224 explicit MemberLookupCriteria(CXXMethodDecl *MD)
225 : Kind(LK_OverriddenMember), Method(MD) { }
226
Anders Carlsson9e8a7222009-05-29 23:42:05 +0000227 /// Kind - The kind of lookup we're doing.
228 /// LK_Base if we are looking for a base class (whose
229 /// type is Base). LK_NamedMember if we are looking for a named member of
Douglas Gregor7176fff2009-01-15 00:26:24 +0000230 /// the class (with the name Name).
Anders Carlsson9e8a7222009-05-29 23:42:05 +0000231 LookupKind Kind;
Douglas Gregor7176fff2009-01-15 00:26:24 +0000232
233 /// Base - The type of the base class we're searching for, if
234 /// LookupBase is true.
235 QualType Base;
236
237 /// Name - The name of the member we're searching for, if
238 /// LookupBase is false.
239 DeclarationName Name;
240
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000241 Sema::LookupNameKind NameKind;
242 unsigned IDNS;
Anders Carlssond12ef8d2009-05-30 00:52:53 +0000243
244 CXXMethodDecl *Method;
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000245 };
246}
247
248#endif