blob: 6138685b2644fae276deb1da6d570924cc776650 [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.
43 const CXXRecordDecl *Class;
44
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 Gregor94b1dd22008-10-24 04:54:22 +0000135 friend class Sema;
136
137 public:
138 typedef std::list<BasePath>::const_iterator paths_iterator;
139
140 /// BasePaths - Construct a new BasePaths structure to record the
141 /// paths for a derived-to-base search.
Sebastian Redl07779722008-10-31 14:43:28 +0000142 explicit BasePaths(bool FindAmbiguities = true,
143 bool RecordPaths = true,
144 bool DetectVirtual = true)
145 : FindAmbiguities(FindAmbiguities), RecordPaths(RecordPaths),
146 DetectVirtual(DetectVirtual), DetectedVirtual(0)
147 {}
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000148
149 paths_iterator begin() const { return Paths.begin(); }
150 paths_iterator end() const { return Paths.end(); }
151
Douglas Gregor7176fff2009-01-15 00:26:24 +0000152 BasePath& front() { return Paths.front(); }
153 const BasePath& front() const { return Paths.front(); }
154
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000155 bool isAmbiguous(QualType BaseType);
156
157 /// isFindingAmbiguities - Whether we are finding multiple paths
158 /// to detect ambiguities.
159 bool isFindingAmbiguities() const { return FindAmbiguities; }
160
161 /// isRecordingPaths - Whether we are recording paths.
162 bool isRecordingPaths() const { return RecordPaths; }
163
164 /// setRecordingPaths - Specify whether we should be recording
165 /// paths or not.
166 void setRecordingPaths(bool RP) { RecordPaths = RP; }
167
Sebastian Redl07779722008-10-31 14:43:28 +0000168 /// isDetectingVirtual - Whether we are detecting virtual bases.
169 bool isDetectingVirtual() const { return DetectVirtual; }
170
171 /// getDetectedVirtual - The virtual base discovered on the path.
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000172 const RecordType* getDetectedVirtual() const {
Sebastian Redl07779722008-10-31 14:43:28 +0000173 return DetectedVirtual;
174 }
175
Douglas Gregor4dc6b1c2009-01-16 00:38:09 +0000176 /// @brief Retrieve the type from which this base-paths search
177 /// began
178 QualType getOrigin() const { return Origin; }
179 void setOrigin(QualType Type) { Origin = Type; }
180
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000181 void clear();
Douglas Gregor7176fff2009-01-15 00:26:24 +0000182
183 void swap(BasePaths &Other);
184 };
185
186 /// MemberLookupCriteria - Criteria for performing lookup of a
187 /// member of a C++ class. Objects of this type are used to direct
188 /// Sema::LookupCXXClassMember.
189 struct MemberLookupCriteria {
190 /// MemberLookupCriteria - Constructs member lookup criteria to
191 /// search for a base class of type Base.
192 explicit MemberLookupCriteria(QualType Base)
193 : LookupBase(true), Base(Base) { }
194
195 /// MemberLookupCriteria - Constructs member lookup criteria to
196 /// search for a class member with the given Name.
197 explicit MemberLookupCriteria(DeclarationName Name,
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000198 Sema::LookupNameKind NameKind,
199 unsigned IDNS)
200 : LookupBase(false), Name(Name), NameKind(NameKind), IDNS(IDNS) { }
Douglas Gregor7176fff2009-01-15 00:26:24 +0000201
202 /// LookupBase - True if we are looking for a base class (whose
203 /// type is Base). If false, we are looking for a named member of
204 /// the class (with the name Name).
205 bool LookupBase;
206
207 /// Base - The type of the base class we're searching for, if
208 /// LookupBase is true.
209 QualType Base;
210
211 /// Name - The name of the member we're searching for, if
212 /// LookupBase is false.
213 DeclarationName Name;
214
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000215 Sema::LookupNameKind NameKind;
216 unsigned IDNS;
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000217 };
218}
219
220#endif