[cfe-commits] r83322 - in /cfe/trunk: include/clang/AST/DeclObjC.h lib/AST/DeclObjC.cpp lib/Sema/SemaDeclObjC.cpp test/CodeGenObjC/protocol-in-extended-class.m

Fariborz Jahanian fjahanian at apple.com
Mon Oct 5 13:41:32 PDT 2009


Author: fjahanian
Date: Mon Oct  5 15:41:32 2009
New Revision: 83322

URL: http://llvm.org/viewvc/llvm-project?rev=83322&view=rev
Log:
Patch to implement Protocols on class extensions.
<rdar://problem/7269631> Protocols on class extensions don't work


Added:
    cfe/trunk/test/CodeGenObjC/protocol-in-extended-class.m
Modified:
    cfe/trunk/include/clang/AST/DeclObjC.h
    cfe/trunk/lib/AST/DeclObjC.cpp
    cfe/trunk/lib/Sema/SemaDeclObjC.cpp

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

==============================================================================
--- cfe/trunk/include/clang/AST/DeclObjC.h (original)
+++ cfe/trunk/include/clang/AST/DeclObjC.h Mon Oct  5 15:41:32 2009
@@ -469,6 +469,11 @@
     ReferencedProtocols.set(List, Num, C);
   }
 
+  /// mergeClassExtensionProtocolList - Merge class extension's protocol list
+  /// into the protocol list for this class.
+  void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List, unsigned Num,
+                       ASTContext &C);
+
   void setIVarList(ObjCIvarDecl * const *List, unsigned Num, ASTContext &C) {
     IVars.set(List, Num, C);
   }

Modified: cfe/trunk/lib/AST/DeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclObjC.cpp?rev=83322&r1=83321&r2=83322&view=diff

==============================================================================
--- cfe/trunk/lib/AST/DeclObjC.cpp (original)
+++ cfe/trunk/lib/AST/DeclObjC.cpp Mon Oct  5 15:41:32 2009
@@ -118,6 +118,47 @@
   return 0;
 }
 
+void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
+                              ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
+                              ASTContext &C)
+{
+  if (ReferencedProtocols.empty()) {
+    ReferencedProtocols.set(ExtList, ExtNum, C);
+    return;
+  }
+  // Check for duplicate protocol in class's protocol list.
+  // This is (O)2. But it is extremely rare and number of protocols in
+  // class or its extension are very few.
+  llvm::SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
+  for (unsigned i = 0; i < ExtNum; i++) {
+    bool protocolExists = false;
+    ObjCProtocolDecl *ProtoInExtension = ExtList[i];
+    for (protocol_iterator p = protocol_begin(), e = protocol_end();
+         p != e; p++) {
+      ObjCProtocolDecl *Proto = (*p);
+      if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
+        protocolExists = true;
+        break;
+      }      
+    }
+    // Do we want to warn on a protocol in extension class which
+    // already exist in the class? Probably not.
+    if (!protocolExists)
+      ProtocolRefs.push_back(ProtoInExtension);
+  }
+  if (ProtocolRefs.empty())
+    return;
+  
+  for (protocol_iterator p = protocol_begin(), e = protocol_end();
+       p != e; p++)
+    ProtocolRefs.push_back(*p);
+  ReferencedProtocols.Destroy(C);
+  unsigned NumProtoRefs = ProtocolRefs.size();
+  setProtocolList((ObjCProtocolDecl**)&ProtocolRefs[0], NumProtoRefs, C);
+  // Merge ProtocolRefs into class's protocol list;
+  
+}
+
 ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
                                               ObjCInterfaceDecl *&clsDeclared) {
   ObjCInterfaceDecl* ClassDecl = this;

Modified: cfe/trunk/lib/Sema/SemaDeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclObjC.cpp?rev=83322&r1=83321&r2=83322&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclObjC.cpp Mon Oct  5 15:41:32 2009
@@ -588,8 +588,15 @@
     CDecl->insertNextClassCategory();
 
   if (NumProtoRefs) {
-    CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context);
-    CDecl->setLocEnd(EndProtoLoc);
+    // Protocols in the class extension belong to the class.
+    if (!CDecl->getIdentifier())
+     IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs, 
+                                            NumProtoRefs,Context); 
+    else {
+      CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
+                             Context);
+      CDecl->setLocEnd(EndProtoLoc);
+    }
   }
 
   CheckObjCDeclScope(CDecl);

Added: cfe/trunk/test/CodeGenObjC/protocol-in-extended-class.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/protocol-in-extended-class.m?rev=83322&view=auto

==============================================================================
--- cfe/trunk/test/CodeGenObjC/protocol-in-extended-class.m (added)
+++ cfe/trunk/test/CodeGenObjC/protocol-in-extended-class.m Mon Oct  5 15:41:32 2009
@@ -0,0 +1,29 @@
+// RUN: clang-cc -triple x86_64-apple-darwin10 -S %s -o %t-64.s &&
+// RUN: FileCheck -check-prefix LP64 --input-file=%t-64.s %s &&
+// RUN: clang-cc -triple i386-apple-darwin -S %s -o %t-32.s &&
+// RUN: FileCheck -check-prefix LP32 --input-file=%t-32.s %s &&
+// RUN: true
+
+ at protocol MyProtocol
+ at end
+
+ at protocol ExtendedProtocol
+ at end
+
+ at interface ItDoesntWork<MyProtocol> {
+}
+-(void) Meth;
+ at end
+
+ at interface ItDoesntWork() <MyProtocol, ExtendedProtocol>
+ at end
+
+ at implementation ItDoesntWork
+-(void) Meth {
+    ItDoesntWork <MyProtocol, ExtendedProtocol> *p = 0;
+ }
+ at end
+
+// CHECK-LP64: l_OBJC_PROTOCOL_$_ExtendedProtocol:
+
+// CHECK-LP32: L_OBJC_PROTOCOL_ExtendedProtocol:





More information about the cfe-commits mailing list