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" |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 24 | #include <memory> |
| 25 | #include <set> |
| 26 | #include <string> |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 27 | |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 28 | using namespace clang; |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 29 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 30 | /// isAmbiguous - Determines whether the set of paths provided is |
| 31 | /// ambiguous, i.e., there are two or more paths that refer to |
| 32 | /// different base class subobjects of the same type. BaseType must be |
| 33 | /// an unqualified, canonical class type. |
| 34 | bool BasePaths::isAmbiguous(QualType BaseType) { |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 35 | assert(BaseType->isCanonical() && "Base type must be the canonical type"); |
| 36 | assert(BaseType.getCVRQualifiers() == 0 && "Base type must be unqualified"); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 37 | std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType]; |
| 38 | return Subobjects.second + (Subobjects.first? 1 : 0) > 1; |
| 39 | } |
| 40 | |
| 41 | /// clear - Clear out all prior path information. |
| 42 | void BasePaths::clear() { |
| 43 | Paths.clear(); |
| 44 | ClassSubobjects.clear(); |
| 45 | ScratchPath.clear(); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 46 | DetectedVirtual = 0; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 49 | /// @brief Swaps the contents of this BasePaths structure with the |
| 50 | /// contents of Other. |
| 51 | void BasePaths::swap(BasePaths &Other) { |
| 52 | Paths.swap(Other.Paths); |
| 53 | ClassSubobjects.swap(Other.ClassSubobjects); |
| 54 | std::swap(FindAmbiguities, Other.FindAmbiguities); |
| 55 | std::swap(RecordPaths, Other.RecordPaths); |
| 56 | std::swap(DetectVirtual, Other.DetectVirtual); |
| 57 | std::swap(DetectedVirtual, Other.DetectedVirtual); |
| 58 | } |
| 59 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 60 | /// IsDerivedFrom - Determine whether the type Derived is derived from |
| 61 | /// the type Base, ignoring qualifiers on Base and Derived. This |
| 62 | /// routine does not assess whether an actual conversion from a |
| 63 | /// Derived* to a Base* is legal, because it does not account for |
| 64 | /// ambiguous conversions or conversions to private/protected bases. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 65 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 66 | BasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false, |
| 67 | /*DetectVirtual=*/false); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 68 | return IsDerivedFrom(Derived, Base, Paths); |
| 69 | } |
| 70 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 71 | /// IsDerivedFrom - Determine whether the type Derived is derived from |
| 72 | /// the type Base, ignoring qualifiers on Base and Derived. This |
| 73 | /// routine does not assess whether an actual conversion from a |
| 74 | /// Derived* to a Base* is legal, because it does not account for |
| 75 | /// ambiguous conversions or conversions to private/protected |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 76 | /// bases. This routine will use Paths to determine if there are |
| 77 | /// ambiguous paths (if @c Paths.isFindingAmbiguities()) and record |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 78 | /// information about all of the paths (if @c Paths.isRecordingPaths()). |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 79 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, BasePaths &Paths) { |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 80 | Derived = Context.getCanonicalType(Derived).getUnqualifiedType(); |
| 81 | Base = Context.getCanonicalType(Base).getUnqualifiedType(); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 82 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 83 | if (!Derived->isRecordType() || !Base->isRecordType()) |
| 84 | return false; |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 85 | |
| 86 | if (Derived == Base) |
| 87 | return false; |
| 88 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 89 | return LookupInBases(cast<CXXRecordType>(Derived->getAsRecordType())->getDecl(), |
| 90 | MemberLookupCriteria(Base), Paths); |
| 91 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 92 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 93 | /// LookupInBases - Look for something that meets the specified |
| 94 | /// Criteria within the base classes of Class (or any of its base |
| 95 | /// classes, transitively). This routine populates BasePaths with the |
| 96 | /// list of paths that one can take to find the entity that meets the |
| 97 | /// search criteria, and returns true if any such entity is found. The |
| 98 | /// various options passed to the BasePath constructor will affect the |
| 99 | /// behavior of this lookup, e.g., whether it finds ambiguities, |
| 100 | /// records paths, or attempts to detect the use of virtual base |
| 101 | /// classes. |
| 102 | bool Sema::LookupInBases(CXXRecordDecl *Class, |
| 103 | const MemberLookupCriteria& Criteria, |
| 104 | BasePaths &Paths) { |
| 105 | bool FoundPath = false; |
| 106 | |
| 107 | for (CXXRecordDecl::base_class_const_iterator BaseSpec = Class->bases_begin(), |
| 108 | BaseSpecEnd = Class->bases_end(); |
| 109 | BaseSpec != BaseSpecEnd; ++BaseSpec) { |
| 110 | // Find the record of the base class subobjects for this type. |
| 111 | QualType BaseType = Context.getCanonicalType(BaseSpec->getType()); |
| 112 | BaseType = BaseType.getUnqualifiedType(); |
| 113 | |
| 114 | // Determine whether we need to visit this base class at all, |
| 115 | // updating the count of subobjects appropriately. |
| 116 | std::pair<bool, unsigned>& Subobjects = Paths.ClassSubobjects[BaseType]; |
| 117 | bool VisitBase = true; |
| 118 | bool SetVirtual = false; |
| 119 | if (BaseSpec->isVirtual()) { |
| 120 | VisitBase = !Subobjects.first; |
| 121 | Subobjects.first = true; |
| 122 | if (Paths.isDetectingVirtual() && Paths.DetectedVirtual == 0) { |
| 123 | // If this is the first virtual we find, remember it. If it turns out |
| 124 | // there is no base path here, we'll reset it later. |
| 125 | Paths.DetectedVirtual = cast<CXXRecordType>(BaseType->getAsRecordType()); |
| 126 | SetVirtual = true; |
| 127 | } |
| 128 | } else |
| 129 | ++Subobjects.second; |
| 130 | |
| 131 | if (Paths.isRecordingPaths()) { |
| 132 | // Add this base specifier to the current path. |
| 133 | BasePathElement Element; |
| 134 | Element.Base = &*BaseSpec; |
| 135 | if (BaseSpec->isVirtual()) |
| 136 | Element.SubobjectNumber = 0; |
| 137 | else |
| 138 | Element.SubobjectNumber = Subobjects.second; |
| 139 | Paths.ScratchPath.push_back(Element); |
| 140 | } |
| 141 | |
| 142 | CXXRecordDecl *BaseRecord |
| 143 | = cast<CXXRecordDecl>(BaseSpec->getType()->getAsRecordType()->getDecl()); |
| 144 | |
| 145 | // Either look at the base class type or look into the base class |
| 146 | // type to see if we've found a member that meets the search |
| 147 | // criteria. |
| 148 | bool FoundPathToThisBase = false; |
| 149 | if (Criteria.LookupBase) { |
| 150 | FoundPathToThisBase |
| 151 | = (Context.getCanonicalType(BaseSpec->getType()) == Criteria.Base); |
| 152 | } else { |
| 153 | Paths.ScratchPath.Decls = BaseRecord->lookup(Criteria.Name); |
| 154 | while (Paths.ScratchPath.Decls.first != Paths.ScratchPath.Decls.second) { |
| 155 | if (Criteria.Criteria.isLookupResult(*Paths.ScratchPath.Decls.first)) { |
| 156 | FoundPathToThisBase = true; |
| 157 | break; |
| 158 | } |
| 159 | ++Paths.ScratchPath.Decls.first; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if (FoundPathToThisBase) { |
| 164 | // We've found a path that terminates that this base. |
| 165 | FoundPath = true; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 166 | if (Paths.isRecordingPaths()) { |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 167 | // We have a path. Make a copy of it before moving on. |
| 168 | Paths.Paths.push_back(Paths.ScratchPath); |
| 169 | } else if (!Paths.isFindingAmbiguities()) { |
| 170 | // We found a path and we don't care about ambiguities; |
| 171 | // return immediately. |
| 172 | return FoundPath; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 173 | } |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 174 | } |
| 175 | // C++ [class.member.lookup]p2: |
| 176 | // A member name f in one sub-object B hides a member name f in |
| 177 | // a sub-object A if A is a base class sub-object of B. Any |
| 178 | // declarations that are so hidden are eliminated from |
| 179 | // consideration. |
| 180 | else if (VisitBase && LookupInBases(BaseRecord, Criteria, Paths)) { |
| 181 | // There is a path to a base class that meets the criteria. If we're not |
| 182 | // collecting paths or finding ambiguities, we're done. |
| 183 | FoundPath = true; |
| 184 | if (!Paths.isFindingAmbiguities()) |
| 185 | return FoundPath; |
| 186 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 187 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 188 | // Pop this base specifier off the current path (if we're |
| 189 | // collecting paths). |
| 190 | if (Paths.isRecordingPaths()) |
| 191 | Paths.ScratchPath.pop_back(); |
| 192 | // If we set a virtual earlier, and this isn't a path, forget it again. |
| 193 | if (SetVirtual && !FoundPath) { |
| 194 | Paths.DetectedVirtual = 0; |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 198 | return FoundPath; |
| 199 | } |
| 200 | |
| 201 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 202 | /// conversion (where Derived and Base are class types) is |
| 203 | /// well-formed, meaning that the conversion is unambiguous (and |
| 204 | /// FIXME: that all of the base classes are accessible). Returns true |
| 205 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 206 | /// otherwise. Loc is the location where this routine should point to |
| 207 | /// if there is an error, and Range is the source range to highlight |
| 208 | /// if there is an error. |
| 209 | bool |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 210 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
| 211 | SourceLocation Loc, SourceRange Range) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 212 | // First, determine whether the path from Derived to Base is |
| 213 | // ambiguous. This is slightly more expensive than checking whether |
| 214 | // the Derived to Base conversion exists, because here we need to |
| 215 | // explore multiple paths to determine if there is an ambiguity. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 216 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 217 | /*DetectVirtual=*/false); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 218 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 219 | assert(DerivationOkay && "Can only be used with a derived-to-base conversion"); |
| 220 | if (!DerivationOkay) |
| 221 | return true; |
| 222 | |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 223 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) |
| 224 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 225 | |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 226 | // We know that the derived-to-base conversion is ambiguous, and |
| 227 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 228 | // search just one more time to compute all of the possible paths so |
| 229 | // that we can print them out. This is more expensive than any of |
| 230 | // the previous derived-to-base checks we've done, but at this point |
| 231 | // performance isn't as much of an issue. |
| 232 | Paths.clear(); |
| 233 | Paths.setRecordingPaths(true); |
| 234 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 235 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 236 | if (!StillOkay) |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 237 | return true; |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 238 | |
| 239 | // Build up a textual representation of the ambiguous paths, e.g., |
| 240 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 241 | // conversions in the diagnostic. We only print one of the paths |
| 242 | // to each base class subobject. |
| 243 | std::string PathDisplayStr; |
| 244 | std::set<unsigned> DisplayedPaths; |
| 245 | for (BasePaths::paths_iterator Path = Paths.begin(); |
| 246 | Path != Paths.end(); ++Path) { |
| 247 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 248 | // We haven't displayed a path to this particular base |
| 249 | // class subobject yet. |
| 250 | PathDisplayStr += "\n "; |
| 251 | PathDisplayStr += Derived.getAsString(); |
| 252 | for (BasePath::const_iterator Element = Path->begin(); |
| 253 | Element != Path->end(); ++Element) |
| 254 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 255 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 256 | } |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 257 | |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 258 | Diag(Loc, diag::err_ambiguous_derived_to_base_conv) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 259 | << Derived << Base << PathDisplayStr << Range; |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 260 | return true; |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 261 | } |