[clang] [clang] Implement `__is_virtual_base_of()` intrinsic (PR #100393)

Vlad Serebrennikov via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 24 11:51:02 PDT 2024


================
@@ -6027,6 +6027,33 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, const TypeSourceI
     return cast<CXXRecordDecl>(rhsRecord->getDecl())
       ->isDerivedFrom(cast<CXXRecordDecl>(lhsRecord->getDecl()));
   }
+  case BTT_IsVirtualBaseOf: {
+    const RecordType *BaseRecord = LhsT->getAs<RecordType>();
+    const RecordType *DerivedRecord = RhsT->getAs<RecordType>();
+
+    if (!BaseRecord || !DerivedRecord) {
+      DiagnoseVLAInCXXTypeTrait(Self, Lhs, tok::kw___is_virtual_base_of);
+      DiagnoseVLAInCXXTypeTrait(Self, Rhs, tok::kw___is_virtual_base_of);
+      return false;
+    }
+
+    if (BaseRecord->isUnionType() || DerivedRecord->isUnionType())
+      return false;
+
+    if (!BaseRecord->isStructureOrClassType() ||
+        !DerivedRecord->isStructureOrClassType())
+      return false;
+
+    if (Self.RequireCompleteType(Rhs->getTypeLoc().getBeginLoc(), RhsT,
+                                 diag::err_incomplete_type))
+      return false;
+
+    if (Self.Context.hasSameUnqualifiedType(LhsT, RhsT))
+      return false;
----------------
Endilll wrote:

> but I don’t think it’s possible for a type to be its own base class—virtual or not—

Well, `std::is_base_of_v<T, T>` is ought to be true per http://eel.is/c++draft/meta.rel#lib:is_base_of, even though I don't see this in core language wording (which might be the reason this is specified in the library in the first place).

> Is the check for virtual bases expensive?

I haven't measured, but why do more work when we can help it?

https://github.com/llvm/llvm-project/pull/100393


More information about the cfe-commits mailing list