blob: eccd31f6a63a5e03b20bae4303955b8bdddb72e4 [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
19#include "clang/AST/Type.h"
20#include "clang/AST/TypeOrdering.h"
21#include "llvm/ADT/SmallVector.h"
22#include <list>
23#include <map>
24
25namespace clang {
26 class Sema;
27 class CXXBaseSpecifier;
Sebastian Redl07779722008-10-31 14:43:28 +000028 class CXXRecordType;
Douglas Gregor94b1dd22008-10-24 04:54:22 +000029
30 /// BasePathElement - An element in a path from a derived class to a
31 /// base class. Each step in the path references the link from a
32 /// derived class to one of its direct base classes, along with a
33 /// base "number" that identifies which base subobject of the
34 /// original derived class we are referencing.
35 struct BasePathElement {
36 /// Base - The base specifier that states the link from a derived
37 /// class to a base class, which will be followed by this base
38 /// path element.
39 const CXXBaseSpecifier *Base;
40
41 /// SubobjectNumber - Identifies which base class subobject (of type
42 /// @c Base->getType()) this base path element refers to. This
43 /// value is only valid if @c !Base->isVirtual(), because there
44 /// is no base numbering for the zero or one virtual bases of a
45 /// given type.
46 int SubobjectNumber;
47 };
48
49 /// BasePath - Represents a path from a specific derived class
50 /// (which is not represented as part of the path) to a particular
51 /// (direct or indirect) base class subobject. Individual elements
52 /// in the path are described by the BasePathElement structure,
53 /// which captures both the link from a derived class to one of its
54 /// direct bases and identification describing which base class
55 /// subobject is being used.
56 typedef llvm::SmallVector<BasePathElement, 4> BasePath;
57
58 /// BasePaths - Represents the set of paths from a derived class to
59 /// one of its (direct or indirect) bases. For example, given the
60 /// following class hierachy:
61 ///
62 /// @code
63 /// class A { };
64 /// class B : public A { };
65 /// class C : public A { };
66 /// class D : public B, public C{ };
67 /// @endcode
68 ///
69 /// There are two potential BasePaths to represent paths from D to a
70 /// base subobject of type A. One path is (D,0) -> (B,0) -> (A,0)
71 /// and another is (D,0)->(C,0)->(A,1). These two paths actually
72 /// refer to two different base class subobjects of the same type,
73 /// so the BasePaths object refers to an ambiguous path. On the
74 /// other hand, consider the following class hierarchy:
75 ///
76 /// @code
77 /// class A { };
78 /// class B : public virtual A { };
79 /// class C : public virtual A { };
80 /// class D : public B, public C{ };
81 /// @endcode
82 ///
83 /// Here, there are two potential BasePaths again, (D, 0) -> (B, 0)
84 /// -> (A,v) and (D, 0) -> (C, 0) -> (A, v), but since both of them
85 /// refer to the same base class subobject of type A (the virtual
86 /// one), there is no ambiguity.
87 class BasePaths {
88 /// Paths - The actual set of paths that can be taken from the
89 /// derived class to the same base class.
90 std::list<BasePath> Paths;
91
92 /// ClassSubobjects - Records the class subobjects for each class
93 /// type that we've seen. The first element in the pair says
94 /// whether we found a path to a virtual base for that class type,
95 /// while the element contains the number of non-virtual base
96 /// class subobjects for that class type. The key of the map is
97 /// the cv-unqualified canonical type of the base class subobject.
98 std::map<QualType, std::pair<bool, unsigned>, QualTypeOrdering>
99 ClassSubobjects;
100
Sebastian Redl07779722008-10-31 14:43:28 +0000101 /// FindAmbiguities - Whether Sema::IsDerivedFrom should try find
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000102 /// ambiguous paths while it is looking for a path from a derived
103 /// type to a base type.
104 bool FindAmbiguities;
105
Sebastian Redl07779722008-10-31 14:43:28 +0000106 /// RecordPaths - Whether Sema::IsDerivedFrom should record paths
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000107 /// while it is determining whether there are paths from a derived
108 /// type to a base type.
109 bool RecordPaths;
110
Sebastian Redl07779722008-10-31 14:43:28 +0000111 /// DetectVirtual - Whether Sema::IsDerivedFrom should abort the search
112 /// if it finds a path that goes across a virtual base. The virtual class
113 /// is also recorded.
114 bool DetectVirtual;
115
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000116 /// ScratchPath - A BasePath that is used by Sema::IsDerivedFrom
117 /// to help build the set of paths.
118 BasePath ScratchPath;
119
Sebastian Redl07779722008-10-31 14:43:28 +0000120 /// DetectedVirtual - The base class that is virtual.
121 const CXXRecordType *DetectedVirtual;
122
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000123 friend class Sema;
124
125 public:
126 typedef std::list<BasePath>::const_iterator paths_iterator;
127
128 /// BasePaths - Construct a new BasePaths structure to record the
129 /// paths for a derived-to-base search.
Sebastian Redl07779722008-10-31 14:43:28 +0000130 explicit BasePaths(bool FindAmbiguities = true,
131 bool RecordPaths = true,
132 bool DetectVirtual = true)
133 : FindAmbiguities(FindAmbiguities), RecordPaths(RecordPaths),
134 DetectVirtual(DetectVirtual), DetectedVirtual(0)
135 {}
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000136
137 paths_iterator begin() const { return Paths.begin(); }
138 paths_iterator end() const { return Paths.end(); }
139
140 bool isAmbiguous(QualType BaseType);
141
142 /// isFindingAmbiguities - Whether we are finding multiple paths
143 /// to detect ambiguities.
144 bool isFindingAmbiguities() const { return FindAmbiguities; }
145
146 /// isRecordingPaths - Whether we are recording paths.
147 bool isRecordingPaths() const { return RecordPaths; }
148
149 /// setRecordingPaths - Specify whether we should be recording
150 /// paths or not.
151 void setRecordingPaths(bool RP) { RecordPaths = RP; }
152
Sebastian Redl07779722008-10-31 14:43:28 +0000153 /// isDetectingVirtual - Whether we are detecting virtual bases.
154 bool isDetectingVirtual() const { return DetectVirtual; }
155
156 /// getDetectedVirtual - The virtual base discovered on the path.
157 const CXXRecordType* getDetectedVirtual() const {
158 return DetectedVirtual;
159 }
160
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000161 void clear();
162 };
163}
164
165#endif