What is the Objective-C 2.0 class interface and implementation converted to GCC or Clang

Since Objective-C is a superset of C, all Objective-C specific statements are converted to C statements at the time the .m file is compiled (by my assumption, a preprocessor). So, for example, a message expression like [receiver method] is converted to a call to the messaging function objc_msgSend(receiver, selector) .

My question is: if I have a class definition as follows:

 @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 did he convert to compiler (in version 2.0 of Objective-C)? Is it converted to calls to functions like objc_allocateClassPair() , class_addIvar() , class_addMethod() and objc_registerClassPair() to create a class, add its instance variables, add its methods and register the class accordingly (so that the class struct is actually defined at runtime, but not loaded as a structure from an executable file)?

+6
source share
1 answer

Since Objective-C is a superset of C, all Objective-C specific statements are converted to C statements during the compilation of the .m file (by the preprocessor, I think).

This was true in 1988. Although Objective-C could still be compiled this way, it was not for long.

The compiler parses Objective-C along with C and sometimes C ++ and emits an abstract syntax tree [AST], which is the result after processing. That AST includes various definitions of Objective-C is completely direct.

Note that the details of GCC compilation and LLVM compilation differ.

If you look at the compiled output, you will see that the mach-o file - the executable product - contains various sections in the file that contains Objective-C metadata, including class definitions, selector tables, ivar locations, etc. The compiler generates this metadata in a .o file, and then the linker combines everything together, eliminates duplicate information (this is not a duplicate of a character) and writes mach-o.

As mentioned in another question, you can use rewriting Objective-C to rewrite Objective-C to direct C, but the resulting code is inefficient and slightly different from the regulation compilation pipeline.

+14
source

Source: https://habr.com/ru/post/922287/


All Articles