blob: db7bfb8e652e6a51bbf5b63efe470121f6bd4f8f [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 {
95 /// Paths - The actual set of paths that can be taken from the
96 /// derived class to the same base class.
97 std::list<BasePath> Paths;
98
99 /// ClassSubobjects - Records the class subobjects for each class
100 /// type that we've seen. The first element in the pair says
101 /// whether we found a path to a virtual base for that class type,
102 /// while the element contains the number of non-virtual base
103 /// class subobjects for that class type. The key of the map is
104 /// the cv-unqualified canonical type of the base class subobject.
105 std::map<QualType, std::pair<bool, unsigned>, QualTypeOrdering>
106 ClassSubobjects;
107
Sebastian Redl07779722008-10-31 14:43:28 +0000108 /// FindAmbiguities - Whether Sema::IsDerivedFrom should try find
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000109 /// ambiguous paths while it is looking for a path from a derived
110 /// type to a base type.
111 bool FindAmbiguities;
112
Sebastian Redl07779722008-10-31 14:43:28 +0000113 /// RecordPaths - Whether Sema::IsDerivedFrom should record paths
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000114 /// while it is determining whether there are paths from a derived
115 /// type to a base type.
116 bool RecordPaths;
117
Sebastian Redl07779722008-10-31 14:43:28 +0000118 /// DetectVirtual - Whether Sema::IsDerivedFrom should abort the search
119 /// if it finds a path that goes across a virtual base. The virtual class
120 /// is also recorded.
121 bool DetectVirtual;
122
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000123 /// ScratchPath - A BasePath that is used by Sema::IsDerivedFrom
124 /// to help build the set of paths.
125 BasePath ScratchPath;
126
Sebastian Redl07779722008-10-31 14:43:28 +0000127 /// DetectedVirtual - The base class that is virtual.
128 const CXXRecordType *DetectedVirtual;
129
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000130 friend class Sema;
131
132 public:
133 typedef std::list<BasePath>::const_iterator paths_iterator;
134
135 /// BasePaths - Construct a new BasePaths structure to record the
136 /// paths for a derived-to-base search.
Sebastian Redl07779722008-10-31 14:43:28 +0000137 explicit BasePaths(bool FindAmbiguities = true,
138 bool RecordPaths = true,
139 bool DetectVirtual = true)
140 : FindAmbiguities(FindAmbiguities), RecordPaths(RecordPaths),
141 DetectVirtual(DetectVirtual), DetectedVirtual(0)
142 {}
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000143
144 paths_iterator begin() const { return Paths.begin(); }
145 paths_iterator end() const { return Paths.end(); }
146
Douglas Gregor7176fff2009-01-15 00:26:24 +0000147 BasePath& front() { return Paths.front(); }
148 const BasePath& front() const { return Paths.front(); }
149
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000150 bool isAmbiguous(QualType BaseType);
151
152 /// isFindingAmbiguities - Whether we are finding multiple paths
153 /// to detect ambiguities.
154 bool isFindingAmbiguities() const { return FindAmbiguities; }
155
156 /// isRecordingPaths - Whether we are recording paths.
157 bool isRecordingPaths() const { return RecordPaths; }
158
159 /// setRecordingPaths - Specify whether we should be recording
160 /// paths or not.
161 void setRecordingPaths(bool RP) { RecordPaths = RP; }
162
Sebastian Redl07779722008-10-31 14:43:28 +0000163 /// isDetectingVirtual - Whether we are detecting virtual bases.
164 bool isDetectingVirtual() const { return DetectVirtual; }
165
166 /// getDetectedVirtual - The virtual base discovered on the path.
167 const CXXRecordType* getDetectedVirtual() const {
168 return DetectedVirtual;
169 }
170
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000171 void clear();
Douglas Gregor7176fff2009-01-15 00:26:24 +0000172
173 void swap(BasePaths &Other);
174 };
175
176 /// MemberLookupCriteria - Criteria for performing lookup of a
177 /// member of a C++ class. Objects of this type are used to direct
178 /// Sema::LookupCXXClassMember.
179 struct MemberLookupCriteria {
180 /// MemberLookupCriteria - Constructs member lookup criteria to
181 /// search for a base class of type Base.
182 explicit MemberLookupCriteria(QualType Base)
183 : LookupBase(true), Base(Base) { }
184
185 /// MemberLookupCriteria - Constructs member lookup criteria to
186 /// search for a class member with the given Name.
187 explicit MemberLookupCriteria(DeclarationName Name,
188 Sema::LookupCriteria Criteria)
189 : LookupBase(false), Name(Name), Criteria(Criteria) { }
190
191 /// LookupBase - True if we are looking for a base class (whose
192 /// type is Base). If false, we are looking for a named member of
193 /// the class (with the name Name).
194 bool LookupBase;
195
196 /// Base - The type of the base class we're searching for, if
197 /// LookupBase is true.
198 QualType Base;
199
200 /// Name - The name of the member we're searching for, if
201 /// LookupBase is false.
202 DeclarationName Name;
203
204 /// Criteria - The criteria by which we evaluate a named member,
205 /// if LookupBase is false.
206 Sema::LookupCriteria Criteria;
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000207 };
208}
209
210#endif