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 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 16 | #include "SemaInherit.h" |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 17 | #include "Sema.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" |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 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 | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 48 | /// @brief Swaps the contents of this BasePaths structure with the |
| 49 | /// contents of Other. |
| 50 | void BasePaths::swap(BasePaths &Other) { |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 51 | std::swap(Origin, Other.Origin); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 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 | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 89 | Paths.setOrigin(Derived); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 90 | return LookupInBases(cast<CXXRecordType>(Derived->getAsRecordType())->getDecl(), |
| 91 | MemberLookupCriteria(Base), Paths); |
| 92 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 93 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 94 | /// LookupInBases - Look for something that meets the specified |
| 95 | /// Criteria within the base classes of Class (or any of its base |
| 96 | /// classes, transitively). This routine populates BasePaths with the |
| 97 | /// list of paths that one can take to find the entity that meets the |
| 98 | /// search criteria, and returns true if any such entity is found. The |
| 99 | /// various options passed to the BasePath constructor will affect the |
| 100 | /// behavior of this lookup, e.g., whether it finds ambiguities, |
| 101 | /// records paths, or attempts to detect the use of virtual base |
| 102 | /// classes. |
| 103 | bool Sema::LookupInBases(CXXRecordDecl *Class, |
| 104 | const MemberLookupCriteria& Criteria, |
| 105 | BasePaths &Paths) { |
| 106 | bool FoundPath = false; |
| 107 | |
| 108 | for (CXXRecordDecl::base_class_const_iterator BaseSpec = Class->bases_begin(), |
| 109 | BaseSpecEnd = Class->bases_end(); |
| 110 | BaseSpec != BaseSpecEnd; ++BaseSpec) { |
| 111 | // Find the record of the base class subobjects for this type. |
| 112 | QualType BaseType = Context.getCanonicalType(BaseSpec->getType()); |
| 113 | BaseType = BaseType.getUnqualifiedType(); |
| 114 | |
| 115 | // Determine whether we need to visit this base class at all, |
| 116 | // updating the count of subobjects appropriately. |
| 117 | std::pair<bool, unsigned>& Subobjects = Paths.ClassSubobjects[BaseType]; |
| 118 | bool VisitBase = true; |
| 119 | bool SetVirtual = false; |
| 120 | if (BaseSpec->isVirtual()) { |
| 121 | VisitBase = !Subobjects.first; |
| 122 | Subobjects.first = true; |
| 123 | if (Paths.isDetectingVirtual() && Paths.DetectedVirtual == 0) { |
| 124 | // If this is the first virtual we find, remember it. If it turns out |
| 125 | // there is no base path here, we'll reset it later. |
| 126 | Paths.DetectedVirtual = cast<CXXRecordType>(BaseType->getAsRecordType()); |
| 127 | SetVirtual = true; |
| 128 | } |
| 129 | } else |
| 130 | ++Subobjects.second; |
| 131 | |
| 132 | if (Paths.isRecordingPaths()) { |
| 133 | // Add this base specifier to the current path. |
| 134 | BasePathElement Element; |
| 135 | Element.Base = &*BaseSpec; |
| 136 | if (BaseSpec->isVirtual()) |
| 137 | Element.SubobjectNumber = 0; |
| 138 | else |
| 139 | Element.SubobjectNumber = Subobjects.second; |
| 140 | Paths.ScratchPath.push_back(Element); |
| 141 | } |
| 142 | |
| 143 | CXXRecordDecl *BaseRecord |
| 144 | = cast<CXXRecordDecl>(BaseSpec->getType()->getAsRecordType()->getDecl()); |
| 145 | |
| 146 | // Either look at the base class type or look into the base class |
| 147 | // type to see if we've found a member that meets the search |
| 148 | // criteria. |
| 149 | bool FoundPathToThisBase = false; |
| 150 | if (Criteria.LookupBase) { |
| 151 | FoundPathToThisBase |
| 152 | = (Context.getCanonicalType(BaseSpec->getType()) == Criteria.Base); |
| 153 | } else { |
| 154 | Paths.ScratchPath.Decls = BaseRecord->lookup(Criteria.Name); |
| 155 | while (Paths.ScratchPath.Decls.first != Paths.ScratchPath.Decls.second) { |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 156 | if (isAcceptableLookupResult(*Paths.ScratchPath.Decls.first, |
| 157 | Criteria.NameKind, Criteria.IDNS)) { |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 158 | FoundPathToThisBase = true; |
| 159 | break; |
| 160 | } |
| 161 | ++Paths.ScratchPath.Decls.first; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (FoundPathToThisBase) { |
| 166 | // We've found a path that terminates that this base. |
| 167 | FoundPath = true; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 168 | if (Paths.isRecordingPaths()) { |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 169 | // We have a path. Make a copy of it before moving on. |
| 170 | Paths.Paths.push_back(Paths.ScratchPath); |
| 171 | } else if (!Paths.isFindingAmbiguities()) { |
| 172 | // We found a path and we don't care about ambiguities; |
| 173 | // return immediately. |
| 174 | return FoundPath; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 175 | } |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 176 | } |
| 177 | // C++ [class.member.lookup]p2: |
| 178 | // A member name f in one sub-object B hides a member name f in |
| 179 | // a sub-object A if A is a base class sub-object of B. Any |
| 180 | // declarations that are so hidden are eliminated from |
| 181 | // consideration. |
| 182 | else if (VisitBase && LookupInBases(BaseRecord, Criteria, Paths)) { |
| 183 | // There is a path to a base class that meets the criteria. If we're not |
| 184 | // collecting paths or finding ambiguities, we're done. |
| 185 | FoundPath = true; |
| 186 | if (!Paths.isFindingAmbiguities()) |
| 187 | return FoundPath; |
| 188 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 189 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 190 | // Pop this base specifier off the current path (if we're |
| 191 | // collecting paths). |
| 192 | if (Paths.isRecordingPaths()) |
| 193 | Paths.ScratchPath.pop_back(); |
| 194 | // If we set a virtual earlier, and this isn't a path, forget it again. |
| 195 | if (SetVirtual && !FoundPath) { |
| 196 | Paths.DetectedVirtual = 0; |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 200 | return FoundPath; |
| 201 | } |
| 202 | |
| 203 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 204 | /// conversion (where Derived and Base are class types) is |
| 205 | /// well-formed, meaning that the conversion is unambiguous (and |
| 206 | /// FIXME: that all of the base classes are accessible). Returns true |
| 207 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 208 | /// otherwise. Loc is the location where this routine should point to |
| 209 | /// if there is an error, and Range is the source range to highlight |
| 210 | /// if there is an error. |
| 211 | bool |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 212 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
| 213 | SourceLocation Loc, SourceRange Range) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 214 | // First, determine whether the path from Derived to Base is |
| 215 | // ambiguous. This is slightly more expensive than checking whether |
| 216 | // the Derived to Base conversion exists, because here we need to |
| 217 | // explore multiple paths to determine if there is an ambiguity. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 218 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 219 | /*DetectVirtual=*/false); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 220 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 221 | assert(DerivationOkay && |
| 222 | "Can only be used with a derived-to-base conversion"); |
| 223 | (void)DerivationOkay; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 224 | |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 225 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) |
| 226 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 227 | |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 228 | // We know that the derived-to-base conversion is ambiguous, and |
| 229 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 230 | // search just one more time to compute all of the possible paths so |
| 231 | // that we can print them out. This is more expensive than any of |
| 232 | // the previous derived-to-base checks we've done, but at this point |
| 233 | // performance isn't as much of an issue. |
| 234 | Paths.clear(); |
| 235 | Paths.setRecordingPaths(true); |
| 236 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 237 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 238 | (void)StillOkay; |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 239 | |
| 240 | // Build up a textual representation of the ambiguous paths, e.g., |
| 241 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 242 | // conversions in the diagnostic. We only print one of the paths |
| 243 | // to each base class subobject. |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 244 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 245 | |
| 246 | Diag(Loc, diag::err_ambiguous_derived_to_base_conv) |
| 247 | << Derived << Base << PathDisplayStr << Range; |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | /// @brief Builds a string representing ambiguous paths from a |
| 252 | /// specific derived class to different subobjects of the same base |
| 253 | /// class. |
| 254 | /// |
| 255 | /// This function builds a string that can be used in error messages |
| 256 | /// to show the different paths that one can take through the |
| 257 | /// inheritance hierarchy to go from the derived class to different |
| 258 | /// subobjects of a base class. The result looks something like this: |
| 259 | /// @code |
| 260 | /// struct D -> struct B -> struct A |
| 261 | /// struct D -> struct C -> struct A |
| 262 | /// @endcode |
| 263 | std::string Sema::getAmbiguousPathsDisplayString(BasePaths &Paths) { |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 264 | std::string PathDisplayStr; |
| 265 | std::set<unsigned> DisplayedPaths; |
| 266 | for (BasePaths::paths_iterator Path = Paths.begin(); |
| 267 | Path != Paths.end(); ++Path) { |
| 268 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 269 | // We haven't displayed a path to this particular base |
| 270 | // class subobject yet. |
| 271 | PathDisplayStr += "\n "; |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 272 | PathDisplayStr += Paths.getOrigin().getAsString(); |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 273 | for (BasePath::const_iterator Element = Path->begin(); |
| 274 | Element != Path->end(); ++Element) |
| 275 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 276 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 277 | } |
Douglas Gregor | 4dc6b1c | 2009-01-16 00:38:09 +0000 | [diff] [blame] | 278 | |
| 279 | return PathDisplayStr; |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 280 | } |