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