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