| 1 |
/* GNU Objective C Runtime messaging declarations |
| 2 |
Copyright (C) 1993-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 3, or (at your option) |
| 9 |
any later version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
Under Section 7 of GPL version 3, you are granted additional |
| 17 |
permissions described in the GCC Runtime Library Exception, version |
| 18 |
3.1, as published by the Free Software Foundation. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License and |
| 21 |
a copy of the GCC Runtime Library Exception along with this program; |
| 22 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 |
<http://www.gnu.org/licenses/>. */ |
| 24 |
|
| 25 |
#ifndef __objc_message_INCLUDE_GNU |
| 26 |
#define __objc_message_INCLUDE_GNU |
| 27 |
|
| 28 |
#include "objc.h" |
| 29 |
#include "objc-decls.h" |
| 30 |
|
| 31 |
#ifdef __cplusplus |
| 32 |
extern "C" { |
| 33 |
#endif |
| 34 |
|
| 35 |
/* This file includes declarations of the messaging functions and |
| 36 |
types. */ |
| 37 |
|
| 38 |
/* Compatibility note: the messaging function is one area where the |
| 39 |
GNU runtime and the Apple/NeXT runtime differ significantly. If |
| 40 |
you can, it is recommended that you use higher-level facilities |
| 41 |
(provided by a Foundation library such as GNUstep Base) to perform |
| 42 |
forwarding or other advanced messaging tricks. */ |
| 43 |
|
| 44 |
/* This function returns the IMP (C function implementing a method) to |
| 45 |
use to invoke the method with selector 'op' of receiver 'receiver'. |
| 46 |
|
| 47 |
This is the function used by the compiler when compiling method |
| 48 |
invocations with the GNU runtime. For example, the method call |
| 49 |
|
| 50 |
result = [receiver method]; |
| 51 |
|
| 52 |
is compiled by the compiler (with the GNU runtime) into the |
| 53 |
equivalent of: |
| 54 |
|
| 55 |
{ |
| 56 |
IMP function = objc_msg_lookup (receiver, @selector (method)); |
| 57 |
result = function (receiver, @selector (method)); |
| 58 |
} |
| 59 |
|
| 60 |
so, a call to objc_msg_lookup() determines the IMP (the C function |
| 61 |
implementing the method) to call. Then, the function is called. |
| 62 |
If the method takes or returns different arguments, the compiler |
| 63 |
will cast 'function' to the right type before invoking it, making |
| 64 |
sure arguments and return value are handled correctly. |
| 65 |
|
| 66 |
objc_msg_lookup() must always return a valid function that can be |
| 67 |
called with the required method signature (otherwise the |
| 68 |
compiler-generated code shown above could segfault). If 'receiver' |
| 69 |
is NULL, objc_msg_lookup() returns a C function that does nothing, |
| 70 |
ignores all its arguments, and returns NULL (see nil_method.c). If |
| 71 |
'receiver' does not respond to the selector 'op', objc_msg_lookup() |
| 72 |
will try to call +resolveClassMethod: or resolveInstanceMethod: as |
| 73 |
appropriate, and if they return YES, it will try the lookup again |
| 74 |
(+resolveClassMethod: and +resolveInstanceMethod: can thus install |
| 75 |
dynamically methods as they are requested). If |
| 76 |
+resolveClassMethod: or +resolveInstanceMethod: are either not |
| 77 |
available, or return NO, or return YES but 'receiver' still doesn't |
| 78 |
implement the 'selector' after calling them, the runtime returns a |
| 79 |
generic "forwarding" function that can be called with the required |
| 80 |
method signature and which can process the method invocation |
| 81 |
according to the forwarding API. There are two runtime hooks that |
| 82 |
allow Foundation libraries (such as GNUstep-Base) to return their |
| 83 |
own forwarding function in preference to the runtime ones. When |
| 84 |
that happens, the Foundation library effectively takes complete |
| 85 |
control of the forwarding process; any method invocation where the |
| 86 |
selector is not implemented by the receiver will end up calling a |
| 87 |
forwarding function chosen by the Foundation library. */ |
| 88 |
objc_EXPORT IMP objc_msg_lookup (id receiver, SEL op); |
| 89 |
|
| 90 |
/* Structure used when a message is send to a class's super class. |
| 91 |
The compiler generates one of these structures and passes it to |
| 92 |
objc_msg_lookup_super() when a [super method] call is compiled. */ |
| 93 |
|
| 94 |
/* Modern API. */ |
| 95 |
struct objc_super |
| 96 |
{ |
| 97 |
id self; /* The receiver of the message. */ |
| 98 |
Class super_class; /* The superclass of the receiver. */ |
| 99 |
}; |
| 100 |
|
| 101 |
/* This is used by the compiler instead of objc_msg_lookup () when |
| 102 |
compiling a call to 'super', such as [super method]. This requires |
| 103 |
sending a message to super->self, but looking up the method as if |
| 104 |
super->self was in class super->super_class. */ |
| 105 |
objc_EXPORT IMP objc_msg_lookup_super (struct objc_super *super, SEL sel); |
| 106 |
|
| 107 |
/* Hooks for method forwarding. They make it easy to substitute the |
| 108 |
built-in forwarding with one based on a library, such as ffi, that |
| 109 |
implement closures, thereby avoiding gcc's __builtin_apply |
| 110 |
problems. __objc_msg_forward2's result will be preferred over that |
| 111 |
of __objc_msg_forward if both are set and return non-NULL. */ |
| 112 |
objc_EXPORT IMP (*__objc_msg_forward)(SEL); |
| 113 |
objc_EXPORT IMP (*__objc_msg_forward2)(id, SEL); |
| 114 |
|
| 115 |
#ifdef __cplusplus |
| 116 |
} |
| 117 |
#endif |
| 118 |
|
| 119 |
#endif /* not __objc_message_INCLUDE_GNU */ |