Since Objective-C is a C superset, all Objective-C specific statements are converted into C statements during the compiling of a .m file (by the preprocessor, I guess). So, for example, a message expression like [receiver method]
is converted into a call on the messaging function objc_msgSend(receiver, selector)
.
My question is: if I have a class definition like this:
@interface ClassA {
int var1;
float var2;
id var3;
}
-(void) method1;
-(int) method2: (int) num1;
@end
@implementation ClassA
-(void) method1 {
// Implementation of the method
}
-(int) method2: (int) num1 {
// Implementation of the method
}
@end
what is it converted into by the compiler (in 2.0 version of Objective-C)? Is it converted into calls on functions like objc_allocateClassPair()
, class_addIvar()
, class_addMethod()
and objc_registerClassPair()
, in order to create the class, add its instance variables, add its methods and register the class, respectively (So that the class struct is actually defined in runtime instead of being loaded as a struct from the executable file)?
These days, Xcode ships with clang as the compiler. Wherever we write compiler, you can read it as clang. clang is the tool that takes Objective-C code, analyzes it, and transforms it into a more low-level representation that resembles assembly code: LLVM Intermediate Representation.
Compiling Objective-C into C doesn't make sense, because then it would need to parse the C code and compile it. Objective-C compiles into machine code. Remember that the language (Objective-C, C, C++) only defines the rules to correctly write code.
Since Objective-C is a C superset, all Objective-C specific statements are converted into C statements during the compiling of a .m file (by the preprocessor, I guess).
That was true in 1988. While Objective-C could still be compiled in that fashion, it hasn't been in a long time.
The compiler parses Objective-C, along with C and-- sometimes-- C++, and emits an abstract syntax tree [AST] that represents the post-preprocessed output. That AST includes the various Objective-C definitions quite directly.
Note that the details of GCC's compilation and LLVM's compilation are diverging.
If you look at the compiled output, you'll see that the mach-o file -- the executable product -- has various sections in the file that contain the Objective-C metadata including class definitions, selector tables, ivar layout, etc... The compiler generates this metadata into the .o file and then the linker mashes it all together, eliminates duplicate information (that isn't a duplicate symbol), and writes the mach-o.
As someone mentioned in a different question, you can use the Objective-C rewriter to rewrite Objective-C to straight C, but the resulting code is inefficient and quite a bit different than the regulation compilation pipeline.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With