blob: a676b64ff8685ced6083256764452e55208af53b [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();
45}
46
47/// IsDerivedFrom - Determine whether the class type Derived is
48/// derived from the class type Base, ignoring qualifiers on Base and
49/// Derived. This routine does not assess whether an actual conversion
50/// from a Derived* to a Base* is legal, because it does not account
51/// for ambiguous conversions or conversions to private/protected bases.
52bool Sema::IsDerivedFrom(QualType Derived, QualType Base) {
53 BasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false);
54 return IsDerivedFrom(Derived, Base, Paths);
55}
56
Douglas Gregorbc0805a2008-10-23 00:40:37 +000057/// IsDerivedFrom - Determine whether the class type Derived is
58/// derived from the class type Base, ignoring qualifiers on Base and
59/// Derived. This routine does not assess whether an actual conversion
60/// from a Derived* to a Base* is legal, because it does not account
61/// for ambiguous conversions or conversions to private/protected
Douglas Gregor94b1dd22008-10-24 04:54:22 +000062/// bases. This routine will use Paths to determine if there are
63/// ambiguous paths (if @c Paths.isFindingAmbiguities()) and record
64/// information about all of the paths (if
65/// @c Paths.isRecordingPaths()).
66bool Sema::IsDerivedFrom(QualType Derived, QualType Base, BasePaths &Paths) {
67 bool FoundPath = false;
68
Douglas Gregorbc0805a2008-10-23 00:40:37 +000069 Derived = Context.getCanonicalType(Derived).getUnqualifiedType();
70 Base = Context.getCanonicalType(Base).getUnqualifiedType();
71
72 assert(Derived->isRecordType() && "IsDerivedFrom requires a class type");
73 assert(Base->isRecordType() && "IsDerivedFrom requires a class type");
74
75 if (Derived == Base)
76 return false;
77
78 if (const RecordType *DerivedType = Derived->getAsRecordType()) {
79 const CXXRecordDecl *Decl
80 = static_cast<const CXXRecordDecl *>(DerivedType->getDecl());
Douglas Gregor57c856b2008-10-23 18:13:27 +000081 for (CXXRecordDecl::base_class_const_iterator BaseSpec = Decl->bases_begin();
82 BaseSpec != Decl->bases_end(); ++BaseSpec) {
Douglas Gregor94b1dd22008-10-24 04:54:22 +000083 // Find the record of the base class subobjects for this type.
84 QualType BaseType = Context.getCanonicalType(BaseSpec->getType());
85 BaseType = BaseType.getUnqualifiedType();
86
87 // Determine whether we need to visit this base class at all,
88 // updating the count of subobjects appropriately.
89 std::pair<bool, unsigned>& Subobjects = Paths.ClassSubobjects[BaseType];
90 bool VisitBase = true;
91 if (BaseSpec->isVirtual()) {
92 VisitBase = !Subobjects.first;
93 Subobjects.first = true;
94 } else
95 ++Subobjects.second;
96
97 if (Paths.isRecordingPaths()) {
98 // Add this base specifier to the current path.
99 BasePathElement Element;
100 Element.Base = &*BaseSpec;
101 if (BaseSpec->isVirtual())
102 Element.SubobjectNumber = 0;
103 else
104 Element.SubobjectNumber = Subobjects.second;
105 Paths.ScratchPath.push_back(Element);
106 }
107
108 if (Context.getCanonicalType(BaseSpec->getType()) == Base) {
109 // We've found the base we're looking for.
110 FoundPath = true;
111 if (Paths.isRecordingPaths()) {
112 // We have a path. Make a copy of it before moving on.
113 Paths.Paths.push_back(Paths.ScratchPath);
114 } else if (!Paths.isFindingAmbiguities()) {
115 // We found a path and we don't care about ambiguities;
116 // return immediately.
117 return FoundPath;
118 }
119 } else if (VisitBase && IsDerivedFrom(BaseSpec->getType(), Base, Paths)) {
120 // There is a path to the base we want. If we're not
121 // collecting paths or finding ambiguities, we're done.
122 FoundPath = true;
123 if (!Paths.isFindingAmbiguities())
124 return FoundPath;
125 }
126
127 // Pop this base specifier off the current path (if we're
128 // collecting paths).
129 if (Paths.isRecordingPaths())
130 Paths.ScratchPath.pop_back();
Douglas Gregorbc0805a2008-10-23 00:40:37 +0000131 }
132 }
133
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000134 return FoundPath;
135}
136
137/// CheckDerivedToBaseConversion - Check whether the Derived-to-Base
138/// conversion (where Derived and Base are class types) is
139/// well-formed, meaning that the conversion is unambiguous (and
140/// FIXME: that all of the base classes are accessible). Returns true
141/// and emits a diagnostic if the code is ill-formed, returns false
142/// otherwise. Loc is the location where this routine should point to
143/// if there is an error, and Range is the source range to highlight
144/// if there is an error.
145bool
Douglas Gregor0575d4a2008-10-24 16:17:19 +0000146Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base,
147 SourceLocation Loc, SourceRange Range) {
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000148 // First, determine whether the path from Derived to Base is
149 // ambiguous. This is slightly more expensive than checking whether
150 // the Derived to Base conversion exists, because here we need to
151 // explore multiple paths to determine if there is an ambiguity.
152 BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false);
153 bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths);
154 assert(DerivationOkay && "Can only be used with a derived-to-base conversion");
155 if (!DerivationOkay)
156 return true;
157
Douglas Gregor0575d4a2008-10-24 16:17:19 +0000158 if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType()))
159 return false;
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000160
Douglas Gregor0575d4a2008-10-24 16:17:19 +0000161 // We know that the derived-to-base conversion is ambiguous, and
162 // we're going to produce a diagnostic. Perform the derived-to-base
163 // search just one more time to compute all of the possible paths so
164 // that we can print them out. This is more expensive than any of
165 // the previous derived-to-base checks we've done, but at this point
166 // performance isn't as much of an issue.
167 Paths.clear();
168 Paths.setRecordingPaths(true);
169 bool StillOkay = IsDerivedFrom(Derived, Base, Paths);
170 assert(StillOkay && "Can only be used with a derived-to-base conversion");
171 if (!StillOkay)
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000172 return true;
Douglas Gregor0575d4a2008-10-24 16:17:19 +0000173
174 // Build up a textual representation of the ambiguous paths, e.g.,
175 // D -> B -> A, that will be used to illustrate the ambiguous
176 // conversions in the diagnostic. We only print one of the paths
177 // to each base class subobject.
178 std::string PathDisplayStr;
179 std::set<unsigned> DisplayedPaths;
180 for (BasePaths::paths_iterator Path = Paths.begin();
181 Path != Paths.end(); ++Path) {
182 if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) {
183 // We haven't displayed a path to this particular base
184 // class subobject yet.
185 PathDisplayStr += "\n ";
186 PathDisplayStr += Derived.getAsString();
187 for (BasePath::const_iterator Element = Path->begin();
188 Element != Path->end(); ++Element)
189 PathDisplayStr += " -> " + Element->Base->getType().getAsString();
190 }
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000191 }
Douglas Gregor0575d4a2008-10-24 16:17:19 +0000192
193 Diag(Loc, diag::err_ambiguous_derived_to_base_conv,
194 Derived.getAsString(), Base.getAsString(), PathDisplayStr, Range);
195 return true;
Douglas Gregorbc0805a2008-10-23 00:40:37 +0000196}
197