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