[PATCH] D32891: [Sema][ObjC++] Objective-C++ support for __is_base_of(B, D)

Erik Pilkington via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu May 4 18:51:59 PDT 2017


erik.pilkington created this revision.

This patch adds Objective-C interfaces support for the type trait `is_base_of`, so that `__is_base_of(NSString, NSMutableString)` is true.

rdar://24308607

Thanks for taking a look!
Erik


https://reviews.llvm.org/D32891

Files:
  lib/Sema/SemaExprCXX.cpp
  test/SemaObjCXX/is-base-of.mm


Index: test/SemaObjCXX/is-base-of.mm
===================================================================
--- test/SemaObjCXX/is-base-of.mm
+++ test/SemaObjCXX/is-base-of.mm
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
+
+ at interface NSObj
+ at end
+
+ at interface NSChild : NSObj
+ at end
+
+static_assert(__is_base_of(NSObj, NSChild), "");
+static_assert(!__is_base_of(NSChild, NSObj), "");
+
+static_assert(__is_base_of(NSObj, NSObj), "");
+
+static_assert(!__is_base_of(NSObj *, NSChild *), "");
+static_assert(!__is_base_of(NSChild *, NSObj *), "");
+
+static_assert(__is_base_of(const volatile NSObj, NSChild), "");
+static_assert(__is_base_of(NSObj, const volatile NSChild), "");
+
+ at class NSForward; // expected-note{{forward declaration of class}}
+
+static_assert(!__is_base_of(NSForward, NSObj), "");
+static_assert(!__is_base_of(NSObj, NSForward), ""); // expected-error{{incomplete type 'NSForward'}}
Index: lib/Sema/SemaExprCXX.cpp
===================================================================
--- lib/Sema/SemaExprCXX.cpp
+++ lib/Sema/SemaExprCXX.cpp
@@ -4720,10 +4720,24 @@
     // regard to cv-qualifiers.
 
     const RecordType *lhsRecord = LhsT->getAs<RecordType>();
-    if (!lhsRecord) return false;
-
     const RecordType *rhsRecord = RhsT->getAs<RecordType>();
-    if (!rhsRecord) return false;
+    if (!rhsRecord || !lhsRecord) {
+      const ObjCObjectType *lhsObjTy = LhsT->getAs<ObjCObjectType>();
+      const ObjCObjectType *rhsObjTy = RhsT->getAs<ObjCObjectType>();
+      if (!lhsObjTy || !rhsObjTy)
+        return false;
+
+      ObjCInterfaceDecl *BaseInterface = lhsObjTy->getInterface();
+      ObjCInterfaceDecl *DerivedInterface = rhsObjTy->getInterface();
+      if (!BaseInterface || !DerivedInterface)
+        return false;
+
+      if (Self.RequireCompleteType(
+              KeyLoc, RhsT, diag::err_incomplete_type_used_in_type_trait_expr))
+        return false;
+
+      return BaseInterface->isSuperClassOf(DerivedInterface);
+    }
 
     assert(Self.Context.hasSameUnqualifiedType(LhsT, RhsT)
              == (lhsRecord == rhsRecord));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32891.97905.patch
Type: text/x-patch
Size: 2115 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170505/2c231729/attachment.bin>


More information about the cfe-commits mailing list