blob: 398f388f88ac6f5ae5e6d9e4cbf3570365be205b [file] [log] [blame]
Douglas Gregorbc0805a2008-10-23 00:40:37 +00001//===---- SemaInherit.cpp - 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 routines for C++ inheritance semantics,
11// including searching the inheritance hierarchy and (eventually)
12// access checking.
13//
14//===----------------------------------------------------------------------===//
15
16#include "Sema.h"
Douglas Gregor94b1dd22008-10-24 04:54:22 +000017#include "SemaInherit.h"
Douglas Gregorbc0805a2008-10-23 00:40:37 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclCXX.h"
Douglas Gregor94b1dd22008-10-24 04:54:22 +000020#include "clang/AST/Type.h"
21#include "clang/AST/TypeOrdering.h"
22#include "clang/Basic/Diagnostic.h"
23#include <memory>
24#include <set>
25#include <string>
Douglas Gregorbc0805a2008-10-23 00:40:37 +000026
Douglas Gregor0575d4a2008-10-24 16:17:19 +000027using namespace clang;
Douglas Gregorbc0805a2008-10-23 00:40:37 +000028
Douglas Gregor94b1dd22008-10-24 04:54:22 +000029/// isAmbiguous - Determines whether the set of paths provided is
30/// ambiguous, i.e., there are two or more paths that refer to
31/// different base class subobjects of the same type. BaseType must be
32/// an unqualified, canonical class type.
33bool BasePaths::isAmbiguous(QualType BaseType) {
Douglas Gregor0575d4a2008-10-24 16:17:19 +000034 assert(BaseType->isCanonical() && "Base type must be the canonical type");
35 assert(BaseType.getCVRQualifiers() == 0 && "Base type must be unqualified");
Douglas Gregor94b1dd22008-10-24 04:54:22 +000036 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
37 return Subobjects.second + (Subobjects.first? 1 : 0) > 1;
38}
39
40/// clear - Clear out all prior path information.
41void BasePaths::clear() {
42 Paths.clear();
43 ClassSubobjects.clear();
44 ScratchPath.clear();
Sebastian Redl07779722008-10-31 14:43:28 +000045 DetectedVirtual = 0;
Douglas Gregor94b1dd22008-10-24 04:54:22 +000046}
47
Douglas Gregor27c8dc02008-10-29 00:13:59 +000048/// IsDerivedFrom - Determine whether the type Derived is derived from
49/// the type Base, ignoring qualifiers on Base and Derived. This
50/// routine does not assess whether an actual conversion from a
51/// Derived* to a Base* is legal, because it does not account for
52/// ambiguous conversions or conversions to private/protected bases.
Douglas Gregor94b1dd22008-10-24 04:54:22 +000053bool Sema::IsDerivedFrom(QualType Derived, QualType Base) {
Sebastian Redl07779722008-10-31 14:43:28 +000054 BasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
55 /*DetectVirtual=*/false);
Douglas Gregor94b1dd22008-10-24 04:54:22 +000056 return IsDerivedFrom(Derived, Base, Paths);
57}
58
Douglas Gregor27c8dc02008-10-29 00:13:59 +000059/// IsDerivedFrom - Determine whether the type Derived is derived from
60/// the type Base, ignoring qualifiers on Base and Derived. This
61/// routine does not assess whether an actual conversion from a
62/// Derived* to a Base* is legal, because it does not account for
63/// ambiguous conversions or conversions to private/protected
Douglas Gregor94b1dd22008-10-24 04:54:22 +000064/// bases. This routine will use Paths to determine if there are
65/// ambiguous paths (if @c Paths.isFindingAmbiguities()) and record
Douglas Gregor27c8dc02008-10-29 00:13:59 +000066/// information about all of the paths (if @c Paths.isRecordingPaths()).
Douglas Gregor94b1dd22008-10-24 04:54:22 +000067bool Sema::IsDerivedFrom(QualType Derived, QualType Base, BasePaths &Paths) {
68 bool FoundPath = false;
Sebastian Redl07779722008-10-31 14:43:28 +000069
Douglas Gregorbc0805a2008-10-23 00:40:37 +000070 Derived = Context.getCanonicalType(Derived).getUnqualifiedType();
71 Base = Context.getCanonicalType(Base).getUnqualifiedType();
Sebastian Redl07779722008-10-31 14:43:28 +000072
Douglas Gregor27c8dc02008-10-29 00:13:59 +000073 if (!Derived->isRecordType() || !Base->isRecordType())
74 return false;
Douglas Gregorbc0805a2008-10-23 00:40:37 +000075
76 if (Derived == Base)
77 return false;
78
79 if (const RecordType *DerivedType = Derived->getAsRecordType()) {
80 const CXXRecordDecl *Decl
81 = static_cast<const CXXRecordDecl *>(DerivedType->getDecl());
Douglas Gregor57c856b2008-10-23 18:13:27 +000082 for (CXXRecordDecl::base_class_const_iterator BaseSpec = Decl->bases_begin();
83 BaseSpec != Decl->bases_end(); ++BaseSpec) {
Douglas Gregor94b1dd22008-10-24 04:54:22 +000084 // Find the record of the base class subobjects for this type.
85 QualType BaseType = Context.getCanonicalType(BaseSpec->getType());
86 BaseType = BaseType.getUnqualifiedType();
87
88 // Determine whether we need to visit this base class at all,
89 // updating the count of subobjects appropriately.
90 std::pair<bool, unsigned>& Subobjects = Paths.ClassSubobjects[BaseType];
91 bool VisitBase = true;
Sebastian Redl07779722008-10-31 14:43:28 +000092 bool SetVirtual = false;
Douglas Gregor94b1dd22008-10-24 04:54:22 +000093 if (BaseSpec->isVirtual()) {
94 VisitBase = !Subobjects.first;
95 Subobjects.first = true;
Sebastian Redl07779722008-10-31 14:43:28 +000096 if (Paths.isDetectingVirtual() && Paths.DetectedVirtual == 0) {
97 // If this is the first virtual we find, remember it. If it turns out
98 // there is no base path here, we'll reset it later.
99 Paths.DetectedVirtual = static_cast<const CXXRecordType*>(
100 BaseType->getAsRecordType());
101 SetVirtual = true;
102 }
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000103 } else
104 ++Subobjects.second;
105
106 if (Paths.isRecordingPaths()) {
107 // Add this base specifier to the current path.
108 BasePathElement Element;
109 Element.Base = &*BaseSpec;
110 if (BaseSpec->isVirtual())
111 Element.SubobjectNumber = 0;
112 else
113 Element.SubobjectNumber = Subobjects.second;
114 Paths.ScratchPath.push_back(Element);
115 }
116
117 if (Context.getCanonicalType(BaseSpec->getType()) == Base) {
118 // We've found the base we're looking for.
119 FoundPath = true;
120 if (Paths.isRecordingPaths()) {
121 // We have a path. Make a copy of it before moving on.
122 Paths.Paths.push_back(Paths.ScratchPath);
123 } else if (!Paths.isFindingAmbiguities()) {
124 // We found a path and we don't care about ambiguities;
125 // return immediately.
126 return FoundPath;
127 }
128 } else if (VisitBase && IsDerivedFrom(BaseSpec->getType(), Base, Paths)) {
129 // There is a path to the base we want. If we're not
130 // collecting paths or finding ambiguities, we're done.
131 FoundPath = true;
132 if (!Paths.isFindingAmbiguities())
133 return FoundPath;
134 }
135
136 // Pop this base specifier off the current path (if we're
137 // collecting paths).
138 if (Paths.isRecordingPaths())
139 Paths.ScratchPath.pop_back();
Sebastian Redl07779722008-10-31 14:43:28 +0000140 // If we set a virtual earlier, and this isn't a path, forget it again.
141 if (SetVirtual && !FoundPath) {
142 Paths.DetectedVirtual = 0;
143 }
Douglas Gregorbc0805a2008-10-23 00:40:37 +0000144 }
145 }
146
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000147 return FoundPath;
148}
149
150/// CheckDerivedToBaseConversion - Check whether the Derived-to-Base
151/// conversion (where Derived and Base are class types) is
152/// well-formed, meaning that the conversion is unambiguous (and
153/// FIXME: that all of the base classes are accessible). Returns true
154/// and emits a diagnostic if the code is ill-formed, returns false
155/// otherwise. Loc is the location where this routine should point to
156/// if there is an error, and Range is the source range to highlight
157/// if there is an error.
158bool
Douglas Gregor0575d4a2008-10-24 16:17:19 +0000159Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base,
160 SourceLocation Loc, SourceRange Range) {
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000161 // First, determine whether the path from Derived to Base is
162 // ambiguous. This is slightly more expensive than checking whether
163 // the Derived to Base conversion exists, because here we need to
164 // explore multiple paths to determine if there is an ambiguity.
Sebastian Redl07779722008-10-31 14:43:28 +0000165 BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false,
166 /*DetectVirtual=*/false);
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000167 bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths);
168 assert(DerivationOkay && "Can only be used with a derived-to-base conversion");
169 if (!DerivationOkay)
170 return true;
171
Douglas Gregor0575d4a2008-10-24 16:17:19 +0000172 if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType()))
173 return false;
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000174
Douglas Gregor0575d4a2008-10-24 16:17:19 +0000175 // We know that the derived-to-base conversion is ambiguous, and
176 // we're going to produce a diagnostic. Perform the derived-to-base
177 // search just one more time to compute all of the possible paths so
178 // that we can print them out. This is more expensive than any of
179 // the previous derived-to-base checks we've done, but at this point
180 // performance isn't as much of an issue.
181 Paths.clear();
182 Paths.setRecordingPaths(true);
183 bool StillOkay = IsDerivedFrom(Derived, Base, Paths);
184 assert(StillOkay && "Can only be used with a derived-to-base conversion");
185 if (!StillOkay)
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000186 return true;
Douglas Gregor0575d4a2008-10-24 16:17:19 +0000187
188 // Build up a textual representation of the ambiguous paths, e.g.,
189 // D -> B -> A, that will be used to illustrate the ambiguous
190 // conversions in the diagnostic. We only print one of the paths
191 // to each base class subobject.
192 std::string PathDisplayStr;
193 std::set<unsigned> DisplayedPaths;
194 for (BasePaths::paths_iterator Path = Paths.begin();
195 Path != Paths.end(); ++Path) {
196 if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) {
197 // We haven't displayed a path to this particular base
198 // class subobject yet.
199 PathDisplayStr += "\n ";
200 PathDisplayStr += Derived.getAsString();
201 for (BasePath::const_iterator Element = Path->begin();
202 Element != Path->end(); ++Element)
203 PathDisplayStr += " -> " + Element->Base->getType().getAsString();
204 }
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000205 }
Douglas Gregor0575d4a2008-10-24 16:17:19 +0000206
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000207 Diag(Loc, diag::err_ambiguous_derived_to_base_conv)
208 << Derived.getAsString() << Base.getAsString() << PathDisplayStr << Range;
Douglas Gregor0575d4a2008-10-24 16:17:19 +0000209 return true;
Douglas Gregorbc0805a2008-10-23 00:40:37 +0000210}
211