148fb7bfaSmrg /* GNU Objective C Runtime messaging declarations 2*b1e83836Smrg Copyright (C) 1993-2022 Free Software Foundation, Inc. 348fb7bfaSmrg 448fb7bfaSmrg This file is part of GCC. 548fb7bfaSmrg 648fb7bfaSmrg GCC is free software; you can redistribute it and/or modify 748fb7bfaSmrg it under the terms of the GNU General Public License as published by 848fb7bfaSmrg the Free Software Foundation; either version 3, or (at your option) 948fb7bfaSmrg any later version. 1048fb7bfaSmrg 1148fb7bfaSmrg GCC is distributed in the hope that it will be useful, 1248fb7bfaSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of 1348fb7bfaSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1448fb7bfaSmrg GNU General Public License for more details. 1548fb7bfaSmrg 1648fb7bfaSmrg Under Section 7 of GPL version 3, you are granted additional 1748fb7bfaSmrg permissions described in the GCC Runtime Library Exception, version 1848fb7bfaSmrg 3.1, as published by the Free Software Foundation. 1948fb7bfaSmrg 2048fb7bfaSmrg You should have received a copy of the GNU General Public License and 2148fb7bfaSmrg a copy of the GCC Runtime Library Exception along with this program; 2248fb7bfaSmrg see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 2348fb7bfaSmrg <http://www.gnu.org/licenses/>. */ 2448fb7bfaSmrg 2548fb7bfaSmrg #ifndef __objc_message_INCLUDE_GNU 2648fb7bfaSmrg #define __objc_message_INCLUDE_GNU 2748fb7bfaSmrg 2848fb7bfaSmrg #include "objc.h" 2948fb7bfaSmrg #include "objc-decls.h" 3048fb7bfaSmrg 3148fb7bfaSmrg #ifdef __cplusplus 3248fb7bfaSmrg extern "C" { 3348fb7bfaSmrg #endif 3448fb7bfaSmrg 3548fb7bfaSmrg /* This file includes declarations of the messaging functions and 3648fb7bfaSmrg types. */ 3748fb7bfaSmrg 3848fb7bfaSmrg /* Compatibility note: the messaging function is one area where the 3948fb7bfaSmrg GNU runtime and the Apple/NeXT runtime differ significantly. If 4048fb7bfaSmrg you can, it is recommended that you use higher-level facilities 4148fb7bfaSmrg (provided by a Foundation library such as GNUstep Base) to perform 4248fb7bfaSmrg forwarding or other advanced messaging tricks. */ 4348fb7bfaSmrg 4448fb7bfaSmrg /* This function returns the IMP (C function implementing a method) to 4548fb7bfaSmrg use to invoke the method with selector 'op' of receiver 'receiver'. 4648fb7bfaSmrg 4748fb7bfaSmrg This is the function used by the compiler when compiling method 4848fb7bfaSmrg invocations with the GNU runtime. For example, the method call 4948fb7bfaSmrg 5048fb7bfaSmrg result = [receiver method]; 5148fb7bfaSmrg 5248fb7bfaSmrg is compiled by the compiler (with the GNU runtime) into the 5348fb7bfaSmrg equivalent of: 5448fb7bfaSmrg 5548fb7bfaSmrg { 5648fb7bfaSmrg IMP function = objc_msg_lookup (receiver, @selector (method)); 5748fb7bfaSmrg result = function (receiver, @selector (method)); 5848fb7bfaSmrg } 5948fb7bfaSmrg 6048fb7bfaSmrg so, a call to objc_msg_lookup() determines the IMP (the C function 6148fb7bfaSmrg implementing the method) to call. Then, the function is called. 6248fb7bfaSmrg If the method takes or returns different arguments, the compiler 6348fb7bfaSmrg will cast 'function' to the right type before invoking it, making 6448fb7bfaSmrg sure arguments and return value are handled correctly. 6548fb7bfaSmrg 6648fb7bfaSmrg objc_msg_lookup() must always return a valid function that can be 6748fb7bfaSmrg called with the required method signature (otherwise the 6848fb7bfaSmrg compiler-generated code shown above could segfault). If 'receiver' 6948fb7bfaSmrg is NULL, objc_msg_lookup() returns a C function that does nothing, 7048fb7bfaSmrg ignores all its arguments, and returns NULL (see nil_method.c). If 7148fb7bfaSmrg 'receiver' does not respond to the selector 'op', objc_msg_lookup() 7248fb7bfaSmrg will try to call +resolveClassMethod: or resolveInstanceMethod: as 7348fb7bfaSmrg appropriate, and if they return YES, it will try the lookup again 7448fb7bfaSmrg (+resolveClassMethod: and +resolveInstanceMethod: can thus install 7548fb7bfaSmrg dynamically methods as they are requested). If 7648fb7bfaSmrg +resolveClassMethod: or +resolveInstanceMethod: are either not 7748fb7bfaSmrg available, or return NO, or return YES but 'receiver' still doesn't 7848fb7bfaSmrg implement the 'selector' after calling them, the runtime returns a 7948fb7bfaSmrg generic "forwarding" function that can be called with the required 8048fb7bfaSmrg method signature and which can process the method invocation 8148fb7bfaSmrg according to the forwarding API. There are two runtime hooks that 8248fb7bfaSmrg allow Foundation libraries (such as GNUstep-Base) to return their 8348fb7bfaSmrg own forwarding function in preference to the runtime ones. When 8448fb7bfaSmrg that happens, the Foundation library effectively takes complete 8548fb7bfaSmrg control of the forwarding process; any method invocation where the 8648fb7bfaSmrg selector is not implemented by the receiver will end up calling a 8748fb7bfaSmrg forwarding function chosen by the Foundation library. */ 8848fb7bfaSmrg objc_EXPORT IMP objc_msg_lookup (id receiver, SEL op); 8948fb7bfaSmrg 9048fb7bfaSmrg /* Structure used when a message is send to a class's super class. 9148fb7bfaSmrg The compiler generates one of these structures and passes it to 9248fb7bfaSmrg objc_msg_lookup_super() when a [super method] call is compiled. */ 9348fb7bfaSmrg 9448fb7bfaSmrg /* Modern API. */ 9548fb7bfaSmrg struct objc_super 9648fb7bfaSmrg { 9748fb7bfaSmrg id self; /* The receiver of the message. */ 9848fb7bfaSmrg Class super_class; /* The superclass of the receiver. */ 9948fb7bfaSmrg }; 10048fb7bfaSmrg 10148fb7bfaSmrg /* This is used by the compiler instead of objc_msg_lookup () when 10248fb7bfaSmrg compiling a call to 'super', such as [super method]. This requires 10348fb7bfaSmrg sending a message to super->self, but looking up the method as if 10448fb7bfaSmrg super->self was in class super->super_class. */ 10548fb7bfaSmrg objc_EXPORT IMP objc_msg_lookup_super (struct objc_super *super, SEL sel); 10648fb7bfaSmrg 10748fb7bfaSmrg /* Hooks for method forwarding. They make it easy to substitute the 10848fb7bfaSmrg built-in forwarding with one based on a library, such as ffi, that 10948fb7bfaSmrg implement closures, thereby avoiding gcc's __builtin_apply 11048fb7bfaSmrg problems. __objc_msg_forward2's result will be preferred over that 11148fb7bfaSmrg of __objc_msg_forward if both are set and return non-NULL. */ 11248fb7bfaSmrg objc_EXPORT IMP (*__objc_msg_forward)(SEL); 11348fb7bfaSmrg objc_EXPORT IMP (*__objc_msg_forward2)(id, SEL); 11448fb7bfaSmrg 11548fb7bfaSmrg #ifdef __cplusplus 11648fb7bfaSmrg } 11748fb7bfaSmrg #endif 11848fb7bfaSmrg 11948fb7bfaSmrg #endif /* not __objc_message_INCLUDE_GNU */ 120