RFC: Enhancing ObjC message lookup in the presence of protocols

Ziemowit Laski zlaski@apple.com
Tue Oct 12 21:46:00 GMT 2004


On 12 Oct 2004, at 9.15, David Ayers wrote:

> @protocol Proto1
> - (const char *)name;
> - (void)set;
> - (int)compare:(id)obj;
> - (int)host
> @end
>
> @protocol Proto2
> - (NSString *)name;
> - (NSSet *)set;
> - (BOOL)compare:(id)obj;
> - (NSString *)host
> @end


If we have

   Class <Proto1, Proto2> cls;
   :
   [cls host];

then, with the patch I just posted to gcc-patches, the compiler
will warn "found `-host' instead of `+host' in protocol(s)", and
then use the `- (int)host' out of Proto1 (i.e., as if we were
messaging 'id <Proto1, Proto2>').

Arguably, both the 'id <Proto1, Proto2>' and 'Class <Proto1, Proto2>'
cases should result in an additional warning about multiple '-host' 
methods being
found.  This can be addressed by a separate patch.  (A more general
problem is when messaging, e.g., 'NSObject <Proto1, Proto2>', where a 
method
declared by NSObject itself clashes with what Proto1 and/or Proto2 
provide.)


> @interface MyRoot1 <Proto1>
> @end
> @interface MyRoot2 <Proto2>
> @end
>
> void
> foo(void)
> {
>   Class <MyProto1> cls1 = bar();
>   Class <MyProto2> cls2 = baz();
>
>   const char *name1 = [cls1 name];
>   NSString *name2 = [cls2 name];
>
>   [cls1 set];
>   NSSet *set = [cls2 set];
>
>   int comp1 = [cls1 compare: cls2];
>   BOOL comp2 = [cls2 compare: cls1];
>
>   int host1 = [cls1 host];
>   NSHost *host2 = [cls2 host];
>
>   ...
> }

I'll double-check, but I believe my current patch will perform correct 
method selections for this test case.
I think I'll collect the code fragments in this e-mail and make a 
class-protocol-2.m out of it. :-)

--Zem
--------------------------------------------------------------
Ziemowit Laski                 1 Infinite Loop, MS 301-2K
Mac OS X Compiler Group        Cupertino, CA USA  95014-2083
Apple Computer, Inc.           +1.408.974.6229  Fax .5477



More information about the Gcc mailing list