Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1 | //===---- 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 Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 17 | #include "SemaInherit.h" |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 20 | #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 Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 26 | |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 27 | using namespace clang; |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 28 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 29 | /// 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. |
| 33 | bool BasePaths::isAmbiguous(QualType BaseType) { |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 34 | assert(BaseType->isCanonical() && "Base type must be the canonical type"); |
| 35 | assert(BaseType.getCVRQualifiers() == 0 && "Base type must be unqualified"); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 36 | 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. |
| 41 | void BasePaths::clear() { |
| 42 | Paths.clear(); |
| 43 | ClassSubobjects.clear(); |
| 44 | ScratchPath.clear(); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 45 | DetectedVirtual = 0; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 48 | /// 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 Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 53 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 54 | BasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false, |
| 55 | /*DetectVirtual=*/false); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 56 | return IsDerivedFrom(Derived, Base, Paths); |
| 57 | } |
| 58 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 59 | /// 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 Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 64 | /// bases. This routine will use Paths to determine if there are |
| 65 | /// ambiguous paths (if @c Paths.isFindingAmbiguities()) and record |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 66 | /// information about all of the paths (if @c Paths.isRecordingPaths()). |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 67 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, BasePaths &Paths) { |
| 68 | bool FoundPath = false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 69 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 70 | Derived = Context.getCanonicalType(Derived).getUnqualifiedType(); |
| 71 | Base = Context.getCanonicalType(Base).getUnqualifiedType(); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 72 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 73 | if (!Derived->isRecordType() || !Base->isRecordType()) |
| 74 | return false; |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 75 | |
| 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 Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 82 | for (CXXRecordDecl::base_class_const_iterator BaseSpec = Decl->bases_begin(); |
| 83 | BaseSpec != Decl->bases_end(); ++BaseSpec) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 84 | // 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 Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 92 | bool SetVirtual = false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 93 | if (BaseSpec->isVirtual()) { |
| 94 | VisitBase = !Subobjects.first; |
| 95 | Subobjects.first = true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 96 | 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 Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 103 | } 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 Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 140 | // 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 Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 147 | 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. |
| 158 | bool |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 159 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
| 160 | SourceLocation Loc, SourceRange Range) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 161 | // 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 Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 165 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 166 | /*DetectVirtual=*/false); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 167 | 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 Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 172 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) |
| 173 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 174 | |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 175 | // 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 Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 186 | return true; |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 187 | |
| 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 Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 205 | } |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 206 | |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame^] | 207 | Diag(Loc, diag::err_ambiguous_derived_to_base_conv) |
| 208 | << Derived.getAsString() << Base.getAsString() << PathDisplayStr << Range; |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 209 | return true; |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 210 | } |
| 211 | |