SemaInherit.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #include "Sema.h"
  16. #include "clang/AST/ASTContext.h"
  17. #include "clang/AST/DeclCXX.h"
  18. namespace clang {
  19. /// IsDerivedFrom - Determine whether the class type Derived is
  20. /// derived from the class type Base, ignoring qualifiers on Base and
  21. /// Derived. This routine does not assess whether an actual conversion
  22. /// from a Derived* to a Base* is legal, because it does not account
  23. /// for ambiguous conversions or conversions to private/protected
  24. /// bases.
  25. bool Sema::IsDerivedFrom(QualType Derived, QualType Base)
  26. {
  27. Derived = Context.getCanonicalType(Derived).getUnqualifiedType();
  28. Base = Context.getCanonicalType(Base).getUnqualifiedType();
  29. assert(Derived->isRecordType() && "IsDerivedFrom requires a class type");
  30. assert(Base->isRecordType() && "IsDerivedFrom requires a class type");
  31. if (Derived == Base)
  32. return false;
  33. if (const RecordType *DerivedType = Derived->getAsRecordType()) {
  34. const CXXRecordDecl *Decl
  35. = static_cast<const CXXRecordDecl *>(DerivedType->getDecl());
  36. for (CXXRecordDecl::base_class_const_iterator BaseSpec = Decl->bases_begin();
  37. BaseSpec != Decl->bases_end(); ++BaseSpec) {
  38. if (Context.getCanonicalType(BaseSpec->getType()) == Base
  39. || IsDerivedFrom(BaseSpec->getType(), Base))
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45. } // end namespace clang