views:

176

answers:

2

Can anyone explain how @encode works to extract the datatype elements present in a given object, struct, or datatype into a type definition to be used as a class descriptor for instantiation?

Or maybe a pointer to some resources for learning about the implementation of new preprocessor directives?

+3  A: 

The @encode directive parses the provided type and generates a constant string based on that type. The encoding of all C primitive types (including signed and unsigned versions) and the Objective-C id and SEL types all have single-character encodings, these can be found in <objc/runtime.h>. More complicated types such as structs and arrays have larger encodings.

More information is available in the Objective-C Runtime Programming Guide [PDF].

dreamlax
Thanks for your help but I know WHAT it does. I'm interested in HOW it does it.
Anderson
Once the source has been parsed into an ASTs, the `@encode` method probably traverses the tree and inspects the individual types and generates the string from that.
dreamlax
The way I understand it is that at compile-time, Type Descriptors are generated using the @encode directive with the contents of each class's @interface header. These type descriptors are stored inside the binary and the .ctors section instantiates each class object at run-time using the __attribute__((constructors)) function.
Anderson
So how can you introspect an opaque struct object if it is not a full-fledged object yet?
Anderson
@Anderson: you can't. The encoding of an incomplete `struct` is also incomplete.
dreamlax
Also, what has `@encode` got to do with interface headers? Or constructors?
dreamlax
My overall purpose of this question and my previous one: http://stackoverflow.com/questions/2219501/how-does-the-objective-c-runtime-instantiate-the-root-metaclass-and-other-class-d is to gain knowledge in how Objective-C uses the interface header to create the metaclass and class objects which must be available at runtime in order to instantiate objects.
Anderson
I'm following http://www.planetpdf.com/codecuts/pdfs/ooc.pdf as a guide to implementing a basic ooc implementation. Rather than hard code every class description manually, I'm interested in how Objective-C does it (I've been told it uses @encode to build Type Descriptors at compile-time) to get an idea of how I could do it myself.
Anderson
Objects are not required... This works... how does it do it?('struct test{ int ti ; char tc ;} ;char * encodedstruct = @encode(struct test) ;printf("%s", encodedstruct ) ;')
Anderson
It's done at compile time *by the compiler*. The compiler is fully aware of each type, its size, and its signedness. You cannot implement something like this outside the compiler unless you create a static analyser to perform the same task.
dreamlax
+1  A: 

The phrasing of the original question may have been unclear and I think that my mentioning of a possible implementation involving the preprocessor caused the conversation to turn toward the subtleties of how compilers work rather than the intended question.

Please reference this question, which I believe is much more clear as to what I'm trying to learn: http://stackoverflow.com/questions/2255637/how-would-i-implement-something-similar-to-the-objective-c-encode-compiler-dir

Anderson
It can't be done in the preprocessor because the preprocessor is completely unaware of any type information.
dreamlax