message.h 5.1 KB

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