]> granicus.if.org Git - clang/commitdiff
Patch to implement Protocols on class extensions.
authorFariborz Jahanian <fjahanian@apple.com>
Mon, 5 Oct 2009 20:41:32 +0000 (20:41 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Mon, 5 Oct 2009 20:41:32 +0000 (20:41 +0000)
<rdar://problem/7269631> Protocols on class extensions don't work

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@83322 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/DeclObjC.h
lib/AST/DeclObjC.cpp
lib/Sema/SemaDeclObjC.cpp
test/CodeGenObjC/protocol-in-extended-class.m [new file with mode: 0644]

index fec2607fac78ef6fa65466a3b65b7802a3b06ba5..2b12bb5c1b6d22cd3a29a781c2831fc54010f9c3 100644 (file)
@@ -469,6 +469,11 @@ public:
     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);
   }
index 4185ac1682891cae70dd3ad81a21d0d69969a4d7..e978a5b60fabadfc876d1bb748298eeed80828ed 100644 (file)
@@ -118,6 +118,47 @@ ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
   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;
index c11b4e06667b822fca0a18bb015d890fb67b628d..51651ab4bba4c439b05c8d6170906975f7aebcd8 100644 (file)
@@ -588,8 +588,15 @@ ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
     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);
diff --git a/test/CodeGenObjC/protocol-in-extended-class.m b/test/CodeGenObjC/protocol-in-extended-class.m
new file mode 100644 (file)
index 0000000..87bda46
--- /dev/null
@@ -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
+
+@protocol MyProtocol
+@end
+
+@protocol ExtendedProtocol
+@end
+
+@interface ItDoesntWork<MyProtocol> {
+}
+-(void) Meth;
+@end
+
+@interface ItDoesntWork() <MyProtocol, ExtendedProtocol>
+@end
+
+@implementation ItDoesntWork
+-(void) Meth {
+    ItDoesntWork <MyProtocol, ExtendedProtocol> *p = 0;
+ }
+@end
+
+// CHECK-LP64: l_OBJC_PROTOCOL_$_ExtendedProtocol:
+
+// CHECK-LP32: L_OBJC_PROTOCOL_ExtendedProtocol: