[cfe-commits] r42975 - in /cfe/trunk: AST/Type.cpp include/clang/AST/Type.h

Steve Naroff snaroff at apple.com
Sun Oct 14 20:14:17 PDT 2007


Author: snaroff
Date: Sun Oct 14 22:14:16 2007
New Revision: 42975

URL: http://llvm.org/viewvc/llvm-project?rev=42975&view=rev
Log:

Teach the type checker about "id". This removes the following bogus warning...

[dylan:~/llvm/tools/clang] admin% ../../Debug/bin/clang t.m
t.m:29:18: warning: incompatible pointer types assigning 'id' to 'NSString *'
    resultString = [[NSString alloc] initWithFormat:0 arguments:0];
    ~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Modified:
    cfe/trunk/AST/Type.cpp
    cfe/trunk/include/clang/AST/Type.h

Modified: cfe/trunk/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/Type.cpp?rev=42975&r1=42974&r2=42975&view=diff

==============================================================================
--- cfe/trunk/AST/Type.cpp (original)
+++ cfe/trunk/AST/Type.cpp Sun Oct 14 22:14:16 2007
@@ -267,6 +267,21 @@
   return lBuiltin->getKind() == rBuiltin->getKind();
 }
 
+// FIXME: Devise a way to do this without using strcmp.
+bool Type::isObjcIdType() const {
+  if (const RecordType *RT = getAsStructureType())
+    return !strcmp(RT->getDecl()->getName(), "objc_object");
+  return false;
+}
+
+bool Type::objcTypesAreCompatible(QualType lhs, QualType rhs) {
+  if (lhs->isObjcInterfaceType() && rhs->isObjcIdType())
+    return true;
+  else if (lhs->isObjcIdType() && rhs->isObjcInterfaceType())
+    return true;
+  return false;
+}
+
 bool Type::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
   return true; // FIXME: IMPLEMENT.
 }
@@ -384,9 +399,14 @@
     return true;
   
   // If the canonical type classes don't match, they can't be compatible
-  if (lcanon->getTypeClass() != rcanon->getTypeClass())
+  if (lcanon->getTypeClass() != rcanon->getTypeClass()) {
+    // For Objective-C, it is possible for two types to be compatible
+    // when their classes don't match (when dealing with "id"). If either type
+    // is an interface, we defer to objcTypesAreCompatible(). 
+    if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType())
+      return objcTypesAreCompatible(lcanon, rcanon);
     return false;
-
+  }
   switch (lcanon->getTypeClass()) {
     case Type::Pointer:
       return pointerTypesAreCompatible(lcanon, rcanon);

Modified: cfe/trunk/include/clang/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Type.h?rev=42975&r1=42974&r2=42975&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/trunk/include/clang/AST/Type.h Sun Oct 14 22:14:16 2007
@@ -276,6 +276,7 @@
   bool isVectorType() const; // GCC vector type.
   bool isOCUVectorType() const; // OCU vector type.
   bool isObjcInterfaceType() const; // includes conforming protocol type
+  bool isObjcIdType() const;
   
   // Type Checking Functions: Check to see if this type is structurally the
   // specified type, ignoring typedefs, and return a pointer to the best type
@@ -325,6 +326,7 @@
   static bool arrayTypesAreCompatible(QualType, QualType); // C99 6.7.5.2p6
   static bool builtinTypesAreCompatible(QualType, QualType);
   static bool interfaceTypesAreCompatible(QualType, QualType);
+  static bool objcTypesAreCompatible(QualType, QualType);
 private:  
   QualType getCanonicalTypeInternal() const { return CanonicalType; }
   friend class QualType;





More information about the cfe-commits mailing list