Each type in C has a different size. (Even the same type can have different sizes on different systems, but we'll ignore that for now.) int
can have 32 or 64 bits, depending on the system. double
takes 64 bits. char
represents 8 bits (but might be passed as a regular int
depending on the system's passing convention). And finally and most importantly, struct
types have various sizes, depending on how many elements are in it and each of their sizes; there is no bound to how big it can be. So it is impossible to pass arguments the same way regardless of type. Therefore, how the calling function arranges the arguments, and how the called function interprets its arguments, must be dependent on the function's signature. (You cannot have a type-agnostic "argument array"; what would be the size of the array elements?) When normal function calls are compiled, the compiler knows the signature at compile time, and can arrange it correctly according to calling convention. But NSInvocation
is for managing an invocation at runtime. Therefore, it needs a representation of the method signature to work.
There are several things that the NSInvocation
can do. Each of those things requires knowledge of the number and types (at least the sizes of the types) of the parameters:
- When a message is sent to an object that does not have a method for it, the runtime constructs an
NSInvocation
object and passes it to-forwardInvocation:
. TheNSInvocation
object contains a copy of all the arguments that are passed, since it can be stored and invoked later. Therefore, the runtime needs to know, at the very least, how big the parameters in total are, in order to copy the right amount of data from registers and/or stack (depending on how arguments are arranged in the calling convention) into theNSInvocation
object. - When you have an
NSInvocation
object, you can query for the value of the i'th argument, using-getArgument:atIndex:
. You can also set/change the value for the i'th argument, using-setArgument:atIndex:
. This requires it to know 1) Where in its data buffer the i'th parameter starts; this requires knowing how big the previous parameters are, and 2) how big the i'th parameter is, so that it can copy the right amount of data (if it copies too little, it will have a corrupt value; if it copies too much, say, when you dogetArgument
, it can overwrite the buffer you gave it; or when you dosetArgument
, overwrite other arguments). - You can have it do
-retainArguments
, which causes it to retain all arguments of object pointer type. This requires it to distinguish between object pointer types and other types, so the type information must include not only the size. - You can invoke the
NSInvocation
, which causes it to construct and execute the call to the method. This requires it to know, at the very least, how much data to copy from its buffer into the registers/stack to place all the data where the function will expect it. This requires knowing at least the combined size of all parameters, and probably also needs to know the sizes of individual parameters, so that the divide between parameters on registers and parameters on stack can be figured out correctly. You can get the return value of the call using
-getReturnValue:
; this has similar issues to the getting of arguments above.- A thing not mentioned above is that the return type may also have a great effect on the calling mechanism. On x86 and ARM, the common architectures for Objective-C, when the return type is a
struct
type, the calling convention is very different -- effectively an additional (first) parameter is added before all the regular parameters, which is a pointer to the space that the struct result should be written. This is instead of the regular calling convention where the result is returned in a register. (In PowerPC I believe thatdouble
return type is also treated specially.) So knowing the return type is essentially for constructing and invoking theNSInvocation
.
- A thing not mentioned above is that the return type may also have a great effect on the calling mechanism. On x86 and ARM, the common architectures for Objective-C, when the return type is a