tusharfloyd
tusharfloyd

Reputation: 1972

Is Objective-C converted to C code before compilation?

I know objective C is strict superset of C and we are writing the same thing. But when i write

@interface Myintf {}
@end

Does it get converted to a C struct or is it that the memory layout for the data structure Myintf prepared by Objective c compiler is same as that of a C struct defined in runtime.h?

and same question about objc_msgsend

Apple document says

In Objective-C, messages aren’t bound to method implementations until runtime. The compiler converts a message expression, into a call on a messaging function, objc_msgSend. This function takes the receiver and the name of the method mentioned in the message—that is, the method selector—as its two principal parameters:

Upvotes: 1

Views: 154

Answers (2)

JeremyP
JeremyP

Reputation: 86651

Does it get converted to a C struct

In the old days it used to, but with the modern runtime (OS X 10.5+ 64 bit and iOS), I think it's a bit more complicated. It can't be using a traditional struct because the Fragile Instance Variable problem is solved.

and same question about objc_msgsend

Yes. All method invocations - or more correctly, all message sends - are converted into calls to obj_msgsend() (except for when super is used as the receiver when a different C function is used).

Note that early implementations of Objective-C were implemented as a preprocessor and produced C source code as an intermediate step. The modern compiler does not bother with this and goes straight from Objective-C source code to an object code format.

Upvotes: 3

justin
justin

Reputation: 104698

No and no. Both cases ultimately rely on the runtime. In a way, it is converted to use C interfaces, but there is a level of abstraction introduced -- it's not entirely static.

It will help to look at the assembly generated by the compiler to see how this works in more detail.

Given the declaration:

id objc_msgSend(id theReceiver, SEL theSelector, ...);

The compiler inserts a call to objc_msgSend when your implementation calls a method. It is not reduced to a static C function call, but dynamic dispatch -- another level of indirection, if you like to think of it that way.

Upvotes: 2

Related Questions