[cfe-dev] Objective-C tidy up

David Chisnall csdavec at swansea.ac.uk
Thu Jun 5 08:26:17 PDT 2008

On 4 Jun 2008, at 18:11, Chris Lattner wrote:

>> Any function in Objective-C can use self and _cmd as identifier
>> names.  Any occurrence of the 'this' token in a C++ program refers
>> to an instance of an object with a type defined by the context.
> Does 'super' in ObjC work the same way?

Yes.  Objective-C is a pure superset of C, so anything that is valid C  
is valid Objective-C.  Which means my current handling of super is  

I'm not sure the best way to handle this, since super has very  
specific semantics in Objective-C (it is only valid as the receiver  
for messages) and so can't just have a phantom decl added.   
Suggestions welcome...

>> So, the question is not what should occurrences of the self and _cmd
>> tokens be turned into (although that might be a separate question),
>> it's what the entries in the decl map should be populated with.  As
>> far as my code goes, this can be pretty much any sort of decl.  It
>> needs to have a type associated with it for Sema to be able to be
>> able to do type checking (for self, this is a pointer to an instance
>> of the current class, e.g. NSObject*, for _cmd it is SEL).  For
>> CodeGen, it needs to have a name.
> Gotcha.  What do you think of a new subclass of ValueDecl?

Do we need a new subclass?  I'm not actually sure what this needs that  
a ValueDecl doesn't provide, so we could possibly just use a ValueDecl.

> It would be really nice to lazily allocate these objects when they are
> first used.  This would allow us to avoid allocating the _cmd decl for
> almost every ObjC method, and avoid 'self' on many of them.

That would be ideal.  I'm not sure where the best place to put it  
would be.  Ideally I would want failed lookups in the decl map to  
insert it, but I think a few places access the decl map.  One other  
option would be to allocate the self decl on a per-class basis (since  
it's the same for every method in a class - a pointer to an instance  
of that class) and the _cmd once per module.  It would probably easier  
to allocate them both once per class.

It is very rare to have a class which does not contain at least one  
reference to self.  Most classes will override -init, and the common  
pattern for this is to start the method with:

if (nil == (self = [self init])) return nil;

References to _cmd are less common - they are generally only used for  
message forwarding (which tends to only be done in a root class) or  
for debugging.  I'm not sure if we would get any noticeable speedup  
from allocating this on use over allocating it in the  
ObjCImplementationDecl and ObjCCategoryImplDecl[1].


[1] These should probably have a common superclass, since they are  
almost identical - in fact an ObjCImplementationDecl could inherit  
from ObjCCategoryImplDecl, since a class is effectively a category  
which is allowed to declare instance variables.

More information about the cfe-dev mailing list