[cfe-dev] Proposal to simplify ObjC Type AST's

steve naroff snaroff at apple.com
Wed May 20 08:45:45 PDT 2009


On May 19, 2009, at 8:05 PM, David Chisnall wrote:
>
> On 20 May 2009, at 00:50, Fariborz Jahanian wrote:
>
>> I am not sure if any want want to do this. But following code is
>> legal:
>> #include <objc/objc.h>
>> id pi;
>> Class min()
>> {
>>  return pi->isa;
>> }
>
> This is a much more common idiom.  Ideally, however, we would be
> treating this as an ivar access with a known offset of 0, rather than
> as a structure field access.  This probably ought to generate the same
> code irrespective of whether pi is an id, an NSObject* or an Object*
> (although it should give a warning / error on NSObject where isa is
> declared @protected).  Perhaps we should be declaring an implicit
> Class isa as an ivar on any ObjCIdType, so ObjCIdType defines a
> pointer to an object with a single ivar which accepts all messages.
>

This sounds good to me.

Consider the following pseudo code (for discussion...not meant to be  
complete):

@class Class, Protocol;

@interface id // ObjCIdType: The implicit root of all ObjC objects.
{
   Class isa;
}
// All ObjC roots will inherit the following class methods.
// This will greatly simplify defining an ObjC root class.

+ (Class)class;

+ (Class)superclass;
+ (BOOL)instancesRespondToSelector:(SEL)aSelector;
+ (BOOL)conformsToProtocol:(Protocol *)protocol;
+ (IMP)instanceMethodForSelector:(SEL)aSelector;

- (BOOL)isKindOfClass:(Class)aClass;
- (BOOL)isMemberOfClass:(Class)aClass;
- (BOOL)conformsToProtocol:(Protocol *)aProtocol;
- (BOOL)respondsToSelector:(SEL)aSelector;
@end

@interface Class // ObjCClassType: Will implicitly inherit from 'id'

// No explicit ivar declarations, the structure of 'Class' is  
intentionally opaque (since the details are runtime specific).

- (Class)class;
- (Class)superclass;
- (BOOL)instancesRespondToSelector:(SEL)aSelector;
- (BOOL)conformsToProtocol:(Protocol *)protocol;
- (IMP)instanceMethodForSelector:(SEL)aSelector;

@end

Conceptually, I like this...it makes some of the current legacy C  
'magic' much more explicit.

It does, however, require another bit of magic. Since the current  
definition of 'id' and 'Class' include a pointer, we will have to  
treat this specially (and not require an explicit '*' as we do for  
user-defined classes). Shouldn't be a big deal, however wanted to  
mention it. One day, maybe we can make the '*' implicit for user- 
defined classes:-)

The other issue is source compatibility with existing root classes  
(that explicitly declare an 'isa', for example). This could be handled  
in many ways...not worth talking about until we agree on the concept/ 
direction.

What do you think?

snaroff


> David
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20090520/3d28eee8/attachment.html>


More information about the cfe-dev mailing list