2008-10-23 08:40:37 +08:00
|
|
|
//===---- SemaInherit.cpp - C++ Inheritance ---------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides Sema routines for C++ inheritance semantics,
|
2009-03-27 12:43:36 +08:00
|
|
|
// including searching the inheritance hierarchy.
|
2008-10-23 08:40:37 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-10-24 12:54:22 +08:00
|
|
|
#include "SemaInherit.h"
|
2009-02-08 04:10:22 +08:00
|
|
|
#include "Sema.h"
|
2008-10-23 08:40:37 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
2008-10-24 12:54:22 +08:00
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "clang/AST/TypeOrdering.h"
|
2009-01-15 08:26:24 +08:00
|
|
|
#include <algorithm>
|
2008-10-24 12:54:22 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
2008-10-23 08:40:37 +08:00
|
|
|
|
2008-10-25 00:17:19 +08:00
|
|
|
using namespace clang;
|
2008-10-23 08:40:37 +08:00
|
|
|
|
2009-04-02 05:51:26 +08:00
|
|
|
/// \brief Computes the set of declarations referenced by these base
|
|
|
|
/// paths.
|
|
|
|
void BasePaths::ComputeDeclsFound() {
|
|
|
|
assert(NumDeclsFound == 0 && !DeclsFound &&
|
|
|
|
"Already computed the set of declarations");
|
|
|
|
|
|
|
|
std::set<NamedDecl *> Decls;
|
|
|
|
for (BasePaths::paths_iterator Path = begin(), PathEnd = end();
|
|
|
|
Path != PathEnd; ++Path)
|
|
|
|
Decls.insert(*Path->Decls.first);
|
|
|
|
|
|
|
|
NumDeclsFound = Decls.size();
|
|
|
|
DeclsFound = new NamedDecl * [NumDeclsFound];
|
|
|
|
std::copy(Decls.begin(), Decls.end(), DeclsFound);
|
|
|
|
}
|
|
|
|
|
|
|
|
NamedDecl **BasePaths::found_decls_begin() {
|
|
|
|
if (NumDeclsFound == 0)
|
|
|
|
ComputeDeclsFound();
|
|
|
|
return DeclsFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
NamedDecl **BasePaths::found_decls_end() {
|
|
|
|
if (NumDeclsFound == 0)
|
|
|
|
ComputeDeclsFound();
|
|
|
|
return DeclsFound + NumDeclsFound;
|
|
|
|
}
|
|
|
|
|
2008-10-24 12:54:22 +08:00
|
|
|
/// isAmbiguous - Determines whether the set of paths provided is
|
|
|
|
/// ambiguous, i.e., there are two or more paths that refer to
|
|
|
|
/// different base class subobjects of the same type. BaseType must be
|
|
|
|
/// an unqualified, canonical class type.
|
|
|
|
bool BasePaths::isAmbiguous(QualType BaseType) {
|
2008-10-25 00:17:19 +08:00
|
|
|
assert(BaseType->isCanonical() && "Base type must be the canonical type");
|
|
|
|
assert(BaseType.getCVRQualifiers() == 0 && "Base type must be unqualified");
|
2008-10-24 12:54:22 +08:00
|
|
|
std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
|
|
|
|
return Subobjects.second + (Subobjects.first? 1 : 0) > 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// clear - Clear out all prior path information.
|
|
|
|
void BasePaths::clear() {
|
|
|
|
Paths.clear();
|
|
|
|
ClassSubobjects.clear();
|
|
|
|
ScratchPath.clear();
|
2008-10-31 22:43:28 +08:00
|
|
|
DetectedVirtual = 0;
|
2008-10-24 12:54:22 +08:00
|
|
|
}
|
|
|
|
|
2009-01-15 08:26:24 +08:00
|
|
|
/// @brief Swaps the contents of this BasePaths structure with the
|
|
|
|
/// contents of Other.
|
|
|
|
void BasePaths::swap(BasePaths &Other) {
|
2009-01-16 08:38:09 +08:00
|
|
|
std::swap(Origin, Other.Origin);
|
2009-01-15 08:26:24 +08:00
|
|
|
Paths.swap(Other.Paths);
|
|
|
|
ClassSubobjects.swap(Other.ClassSubobjects);
|
|
|
|
std::swap(FindAmbiguities, Other.FindAmbiguities);
|
|
|
|
std::swap(RecordPaths, Other.RecordPaths);
|
|
|
|
std::swap(DetectVirtual, Other.DetectVirtual);
|
|
|
|
std::swap(DetectedVirtual, Other.DetectedVirtual);
|
|
|
|
}
|
|
|
|
|
2008-10-29 08:13:59 +08:00
|
|
|
/// IsDerivedFrom - Determine whether the type Derived is derived from
|
|
|
|
/// the type Base, ignoring qualifiers on Base and Derived. This
|
|
|
|
/// routine does not assess whether an actual conversion from a
|
|
|
|
/// Derived* to a Base* is legal, because it does not account for
|
|
|
|
/// ambiguous conversions or conversions to private/protected bases.
|
2008-10-24 12:54:22 +08:00
|
|
|
bool Sema::IsDerivedFrom(QualType Derived, QualType Base) {
|
2008-10-31 22:43:28 +08:00
|
|
|
BasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
|
|
|
|
/*DetectVirtual=*/false);
|
2008-10-24 12:54:22 +08:00
|
|
|
return IsDerivedFrom(Derived, Base, Paths);
|
|
|
|
}
|
|
|
|
|
2008-10-29 08:13:59 +08:00
|
|
|
/// IsDerivedFrom - Determine whether the type Derived is derived from
|
|
|
|
/// the type Base, ignoring qualifiers on Base and Derived. This
|
|
|
|
/// routine does not assess whether an actual conversion from a
|
|
|
|
/// Derived* to a Base* is legal, because it does not account for
|
|
|
|
/// ambiguous conversions or conversions to private/protected
|
2008-10-24 12:54:22 +08:00
|
|
|
/// bases. This routine will use Paths to determine if there are
|
|
|
|
/// ambiguous paths (if @c Paths.isFindingAmbiguities()) and record
|
2008-10-29 08:13:59 +08:00
|
|
|
/// information about all of the paths (if @c Paths.isRecordingPaths()).
|
2008-10-24 12:54:22 +08:00
|
|
|
bool Sema::IsDerivedFrom(QualType Derived, QualType Base, BasePaths &Paths) {
|
2008-10-23 08:40:37 +08:00
|
|
|
Derived = Context.getCanonicalType(Derived).getUnqualifiedType();
|
|
|
|
Base = Context.getCanonicalType(Base).getUnqualifiedType();
|
2008-10-31 22:43:28 +08:00
|
|
|
|
2008-10-29 08:13:59 +08:00
|
|
|
if (!Derived->isRecordType() || !Base->isRecordType())
|
|
|
|
return false;
|
2008-10-23 08:40:37 +08:00
|
|
|
|
|
|
|
if (Derived == Base)
|
|
|
|
return false;
|
|
|
|
|
2009-01-16 08:38:09 +08:00
|
|
|
Paths.setOrigin(Derived);
|
2009-02-28 09:32:25 +08:00
|
|
|
return LookupInBases(cast<CXXRecordDecl>(Derived->getAsRecordType()->getDecl()),
|
2009-01-15 08:26:24 +08:00
|
|
|
MemberLookupCriteria(Base), Paths);
|
|
|
|
}
|
2008-10-24 12:54:22 +08:00
|
|
|
|
2009-01-15 08:26:24 +08:00
|
|
|
/// LookupInBases - Look for something that meets the specified
|
|
|
|
/// Criteria within the base classes of Class (or any of its base
|
|
|
|
/// classes, transitively). This routine populates BasePaths with the
|
|
|
|
/// list of paths that one can take to find the entity that meets the
|
|
|
|
/// search criteria, and returns true if any such entity is found. The
|
|
|
|
/// various options passed to the BasePath constructor will affect the
|
|
|
|
/// behavior of this lookup, e.g., whether it finds ambiguities,
|
|
|
|
/// records paths, or attempts to detect the use of virtual base
|
|
|
|
/// classes.
|
|
|
|
bool Sema::LookupInBases(CXXRecordDecl *Class,
|
|
|
|
const MemberLookupCriteria& Criteria,
|
|
|
|
BasePaths &Paths) {
|
|
|
|
bool FoundPath = false;
|
|
|
|
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator BaseSpec = Class->bases_begin(),
|
|
|
|
BaseSpecEnd = Class->bases_end();
|
|
|
|
BaseSpec != BaseSpecEnd; ++BaseSpec) {
|
|
|
|
// Find the record of the base class subobjects for this type.
|
|
|
|
QualType BaseType = Context.getCanonicalType(BaseSpec->getType());
|
|
|
|
BaseType = BaseType.getUnqualifiedType();
|
|
|
|
|
|
|
|
// Determine whether we need to visit this base class at all,
|
|
|
|
// updating the count of subobjects appropriately.
|
|
|
|
std::pair<bool, unsigned>& Subobjects = Paths.ClassSubobjects[BaseType];
|
|
|
|
bool VisitBase = true;
|
|
|
|
bool SetVirtual = false;
|
|
|
|
if (BaseSpec->isVirtual()) {
|
|
|
|
VisitBase = !Subobjects.first;
|
|
|
|
Subobjects.first = true;
|
|
|
|
if (Paths.isDetectingVirtual() && Paths.DetectedVirtual == 0) {
|
|
|
|
// If this is the first virtual we find, remember it. If it turns out
|
|
|
|
// there is no base path here, we'll reset it later.
|
2009-02-28 09:32:25 +08:00
|
|
|
Paths.DetectedVirtual = BaseType->getAsRecordType();
|
2009-01-15 08:26:24 +08:00
|
|
|
SetVirtual = true;
|
2008-10-24 12:54:22 +08:00
|
|
|
}
|
2009-01-15 08:26:24 +08:00
|
|
|
} else
|
|
|
|
++Subobjects.second;
|
2008-10-24 12:54:22 +08:00
|
|
|
|
2009-01-15 08:26:24 +08:00
|
|
|
if (Paths.isRecordingPaths()) {
|
|
|
|
// Add this base specifier to the current path.
|
|
|
|
BasePathElement Element;
|
|
|
|
Element.Base = &*BaseSpec;
|
2009-03-28 02:54:29 +08:00
|
|
|
Element.Class = Class;
|
2009-01-15 08:26:24 +08:00
|
|
|
if (BaseSpec->isVirtual())
|
|
|
|
Element.SubobjectNumber = 0;
|
|
|
|
else
|
|
|
|
Element.SubobjectNumber = Subobjects.second;
|
|
|
|
Paths.ScratchPath.push_back(Element);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXXRecordDecl *BaseRecord
|
|
|
|
= cast<CXXRecordDecl>(BaseSpec->getType()->getAsRecordType()->getDecl());
|
|
|
|
|
|
|
|
// Either look at the base class type or look into the base class
|
|
|
|
// type to see if we've found a member that meets the search
|
|
|
|
// criteria.
|
|
|
|
bool FoundPathToThisBase = false;
|
|
|
|
if (Criteria.LookupBase) {
|
|
|
|
FoundPathToThisBase
|
|
|
|
= (Context.getCanonicalType(BaseSpec->getType()) == Criteria.Base);
|
|
|
|
} else {
|
2009-04-10 05:40:53 +08:00
|
|
|
Paths.ScratchPath.Decls = BaseRecord->lookup(Context, Criteria.Name);
|
2009-01-15 08:26:24 +08:00
|
|
|
while (Paths.ScratchPath.Decls.first != Paths.ScratchPath.Decls.second) {
|
Eliminated LookupCriteria, whose creation was causing a bottleneck for
LookupName et al. Instead, use an enum and a bool to describe its
contents.
Optimized the C/Objective-C path through LookupName, eliminating any
unnecessarily C++isms. Simplify IdentifierResolver::iterator, removing
some code and arguments that are no longer used.
Eliminated LookupDeclInScope/LookupDeclInContext, moving all callers
over to LookupName, LookupQualifiedName, or LookupParsedName, as
appropriate.
All together, I'm seeing a 0.2% speedup on Cocoa.h with PTH and
-disable-free. Plus, we're down to three name-lookup routines.
llvm-svn: 63354
2009-01-30 09:04:22 +08:00
|
|
|
if (isAcceptableLookupResult(*Paths.ScratchPath.Decls.first,
|
|
|
|
Criteria.NameKind, Criteria.IDNS)) {
|
2009-01-15 08:26:24 +08:00
|
|
|
FoundPathToThisBase = true;
|
|
|
|
break;
|
2008-10-24 12:54:22 +08:00
|
|
|
}
|
2009-01-15 08:26:24 +08:00
|
|
|
++Paths.ScratchPath.Decls.first;
|
2008-10-24 12:54:22 +08:00
|
|
|
}
|
2009-01-15 08:26:24 +08:00
|
|
|
}
|
2008-10-24 12:54:22 +08:00
|
|
|
|
2009-01-15 08:26:24 +08:00
|
|
|
if (FoundPathToThisBase) {
|
|
|
|
// We've found a path that terminates that this base.
|
|
|
|
FoundPath = true;
|
|
|
|
if (Paths.isRecordingPaths()) {
|
|
|
|
// We have a path. Make a copy of it before moving on.
|
|
|
|
Paths.Paths.push_back(Paths.ScratchPath);
|
|
|
|
} else if (!Paths.isFindingAmbiguities()) {
|
|
|
|
// We found a path and we don't care about ambiguities;
|
|
|
|
// return immediately.
|
|
|
|
return FoundPath;
|
2008-10-31 22:43:28 +08:00
|
|
|
}
|
2009-01-15 08:26:24 +08:00
|
|
|
}
|
|
|
|
// C++ [class.member.lookup]p2:
|
|
|
|
// A member name f in one sub-object B hides a member name f in
|
|
|
|
// a sub-object A if A is a base class sub-object of B. Any
|
|
|
|
// declarations that are so hidden are eliminated from
|
|
|
|
// consideration.
|
|
|
|
else if (VisitBase && LookupInBases(BaseRecord, Criteria, Paths)) {
|
|
|
|
// There is a path to a base class that meets the criteria. If we're not
|
|
|
|
// collecting paths or finding ambiguities, we're done.
|
|
|
|
FoundPath = true;
|
|
|
|
if (!Paths.isFindingAmbiguities())
|
|
|
|
return FoundPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pop this base specifier off the current path (if we're
|
|
|
|
// collecting paths).
|
|
|
|
if (Paths.isRecordingPaths())
|
|
|
|
Paths.ScratchPath.pop_back();
|
|
|
|
// If we set a virtual earlier, and this isn't a path, forget it again.
|
|
|
|
if (SetVirtual && !FoundPath) {
|
|
|
|
Paths.DetectedVirtual = 0;
|
2008-10-23 08:40:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-24 12:54:22 +08:00
|
|
|
return FoundPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckDerivedToBaseConversion - Check whether the Derived-to-Base
|
|
|
|
/// conversion (where Derived and Base are class types) is
|
|
|
|
/// well-formed, meaning that the conversion is unambiguous (and
|
2009-03-27 13:05:05 +08:00
|
|
|
/// that all of the base classes are accessible). Returns true
|
2008-10-24 12:54:22 +08:00
|
|
|
/// and emits a diagnostic if the code is ill-formed, returns false
|
|
|
|
/// otherwise. Loc is the location where this routine should point to
|
|
|
|
/// if there is an error, and Range is the source range to highlight
|
|
|
|
/// if there is an error.
|
|
|
|
bool
|
2008-10-25 00:17:19 +08:00
|
|
|
Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base,
|
|
|
|
SourceLocation Loc, SourceRange Range) {
|
2008-10-24 12:54:22 +08:00
|
|
|
// First, determine whether the path from Derived to Base is
|
|
|
|
// ambiguous. This is slightly more expensive than checking whether
|
|
|
|
// the Derived to Base conversion exists, because here we need to
|
|
|
|
// explore multiple paths to determine if there is an ambiguity.
|
2009-03-27 13:05:05 +08:00
|
|
|
BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
|
2008-10-31 22:43:28 +08:00
|
|
|
/*DetectVirtual=*/false);
|
2008-10-24 12:54:22 +08:00
|
|
|
bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths);
|
2009-01-29 02:33:18 +08:00
|
|
|
assert(DerivationOkay &&
|
|
|
|
"Can only be used with a derived-to-base conversion");
|
|
|
|
(void)DerivationOkay;
|
2008-10-24 12:54:22 +08:00
|
|
|
|
2009-03-27 13:05:05 +08:00
|
|
|
if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) {
|
|
|
|
// Check that the base class can be accessed.
|
|
|
|
return CheckBaseClassAccess(Derived, Base, Paths, Loc);
|
|
|
|
}
|
2008-10-24 12:54:22 +08:00
|
|
|
|
2008-10-25 00:17:19 +08:00
|
|
|
// We know that the derived-to-base conversion is ambiguous, and
|
|
|
|
// we're going to produce a diagnostic. Perform the derived-to-base
|
|
|
|
// search just one more time to compute all of the possible paths so
|
|
|
|
// that we can print them out. This is more expensive than any of
|
|
|
|
// the previous derived-to-base checks we've done, but at this point
|
|
|
|
// performance isn't as much of an issue.
|
|
|
|
Paths.clear();
|
|
|
|
Paths.setRecordingPaths(true);
|
|
|
|
bool StillOkay = IsDerivedFrom(Derived, Base, Paths);
|
|
|
|
assert(StillOkay && "Can only be used with a derived-to-base conversion");
|
2009-01-29 02:33:18 +08:00
|
|
|
(void)StillOkay;
|
2008-10-25 00:17:19 +08:00
|
|
|
|
|
|
|
// Build up a textual representation of the ambiguous paths, e.g.,
|
|
|
|
// D -> B -> A, that will be used to illustrate the ambiguous
|
|
|
|
// conversions in the diagnostic. We only print one of the paths
|
|
|
|
// to each base class subobject.
|
2009-01-16 08:38:09 +08:00
|
|
|
std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths);
|
|
|
|
|
|
|
|
Diag(Loc, diag::err_ambiguous_derived_to_base_conv)
|
|
|
|
<< Derived << Base << PathDisplayStr << Range;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Builds a string representing ambiguous paths from a
|
|
|
|
/// specific derived class to different subobjects of the same base
|
|
|
|
/// class.
|
|
|
|
///
|
|
|
|
/// This function builds a string that can be used in error messages
|
|
|
|
/// to show the different paths that one can take through the
|
|
|
|
/// inheritance hierarchy to go from the derived class to different
|
|
|
|
/// subobjects of a base class. The result looks something like this:
|
|
|
|
/// @code
|
|
|
|
/// struct D -> struct B -> struct A
|
|
|
|
/// struct D -> struct C -> struct A
|
|
|
|
/// @endcode
|
|
|
|
std::string Sema::getAmbiguousPathsDisplayString(BasePaths &Paths) {
|
2008-10-25 00:17:19 +08:00
|
|
|
std::string PathDisplayStr;
|
|
|
|
std::set<unsigned> DisplayedPaths;
|
|
|
|
for (BasePaths::paths_iterator Path = Paths.begin();
|
|
|
|
Path != Paths.end(); ++Path) {
|
|
|
|
if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) {
|
|
|
|
// We haven't displayed a path to this particular base
|
|
|
|
// class subobject yet.
|
|
|
|
PathDisplayStr += "\n ";
|
2009-01-16 08:38:09 +08:00
|
|
|
PathDisplayStr += Paths.getOrigin().getAsString();
|
2008-10-25 00:17:19 +08:00
|
|
|
for (BasePath::const_iterator Element = Path->begin();
|
|
|
|
Element != Path->end(); ++Element)
|
|
|
|
PathDisplayStr += " -> " + Element->Base->getType().getAsString();
|
|
|
|
}
|
2008-10-24 12:54:22 +08:00
|
|
|
}
|
2009-01-16 08:38:09 +08:00
|
|
|
|
|
|
|
return PathDisplayStr;
|
2008-10-23 08:40:37 +08:00
|
|
|
}
|