home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / gnu / gcc-2.5.8-bin.lha / lib / gcc-lib / amigados / 2.5.8 / include / objc / objc-api.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  14.7 KB  |  450 lines

  1. /* GNU Objective-C Runtime API.
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  14. License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* As a special exception, if you link this library with files compiled
  21.    with GCC to produce an executable, this does not cause the resulting
  22.    executable to be covered by the GNU General Public License.  This
  23.    exception does not however invalidate any other reasons why the
  24.    executable file might be covered by the GNU General Public License. */
  25.  
  26. #ifndef __objc_api_INCLUDE_GNU
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #define __objc_api_INCLUDE_GNU
  31.  
  32. #include "objc/objc.h"
  33. #include "objc/hash.h"
  34. #include <stdio.h>
  35.  
  36. /* For functions which return Method_t */
  37. #define METHOD_NULL    (Method_t)0
  38.                                                 /* Boolean typedefs */
  39. /*
  40. ** Method descriptor returned by introspective Object methods.
  41. ** This is really just the first part of the more complete objc_method
  42. ** structure defined below and used internally by the runtime.
  43. */
  44. struct objc_method_description
  45. {
  46.     SEL name;            /* this is a selector, not a string */
  47.     char *types;        /* type encoding */
  48. };
  49.  
  50.  
  51.  
  52. /* Filer types used to describe Ivars and Methods.  */
  53. #define _C_ID       '@'
  54. #define _C_CLASS    '#'
  55. #define _C_SEL      ':'
  56. #define _C_CHR      'c'
  57. #define _C_UCHR     'C'
  58. #define _C_SHT      's'
  59. #define _C_USHT     'S'
  60. #define _C_INT      'i'
  61. #define _C_UINT     'I'
  62. #define _C_LNG      'l'
  63. #define _C_ULNG     'L'
  64. #define _C_FLT      'f'
  65. #define _C_DBL      'd'
  66. #define _C_BFLD     'b'
  67. #define _C_VOID     'v'
  68. #define _C_UNDEF    '?'
  69. #define _C_PTR      '^'
  70. #define _C_CHARPTR  '*'
  71. #define _C_ATOM     '%'
  72. #define _C_ARY_B    '['
  73. #define _C_ARY_E    ']'
  74. #define _C_UNION_B  '('
  75. #define _C_UNION_E  ')'
  76. #define _C_STRUCT_B '{'
  77. #define _C_STRUCT_E '}'
  78.  
  79.  
  80.  
  81. /*
  82. ** Set this variable nonzero to print a line describing each
  83. ** message that is sent.  (this is currently disabled)
  84. */
  85. extern BOOL objc_trace;
  86.  
  87.  
  88. /*
  89. ** Whereas a Module (defined further down) is the root (typically) of a file,
  90. ** a Symtab is the root of the class and category definitions within the
  91. ** module.  
  92. ** 
  93. ** A Symtab contains a variable length array of pointers to classes and
  94. ** categories  defined in the module. 
  95. */
  96. typedef struct objc_symtab {
  97.   unsigned long sel_ref_cnt;                     /* Unknown. */
  98.   SEL       *refs;                              /* Unknown. */
  99.   unsigned short cls_def_cnt;                   /* Number of classes compiled
  100.                                                   (defined) in the module. */
  101.   unsigned short cat_def_cnt;                   /* Number of categories 
  102.                                                   compiled (defined) in the 
  103.                                                   module. */
  104.   void      *defs[1];                           /* Variable array of pointers.
  105.                                                   cls_def_cnt of type Class* 
  106.                                                   followed by cat_def_cnt of
  107.                                                   type Category_t. */
  108. } Symtab,   *Symtab_t;
  109.  
  110.  
  111. /*
  112. ** The compiler generates one of these structures for each module that
  113. ** composes the executable (eg main.m).  
  114. ** 
  115. ** This data structure is the root of the definition tree for the module.  
  116. ** 
  117. ** A collect program runs between ld stages and creates a ObjC ctor array. 
  118. ** That array holds a pointer to each module structure of the executable. 
  119. */
  120. typedef struct objc_module {
  121.   unsigned long version;                        /* Compiler revision. */
  122.   unsigned long size;                           /* sizeof(Module). */
  123.   const char* name;                             /* Name of the file where the 
  124.                                                   module was generated.   The 
  125.                                                   name includes the path. */
  126.   Symtab_t    symtab;                           /* Pointer to the Symtab of
  127.                                                   the module.  The Symtab
  128.                                                   holds an array of pointers to 
  129.                                                   the classes and categories 
  130.                                                   defined in the module. */
  131. } Module, *Module_t;
  132.  
  133.  
  134. /*
  135. ** The compiler generates one of these structures for a class that has
  136. ** instance variables defined in its specification. 
  137. */
  138. typedef struct objc_ivar* Ivar_t;
  139. typedef struct objc_ivar_list {
  140.   int   ivar_count;                             /* Number of structures (Ivar) 
  141.                                                   contained in the list.  One
  142.                                                   structure per instance 
  143.                                                   variable defined in the
  144.                                                   class. */
  145.   struct objc_ivar {
  146.     const char* ivar_name;                      /* Name of the instance
  147.                                                   variable as entered in the
  148.                                                   class definition. */
  149.     const char* ivar_type;                      /* Description of the Ivar's
  150.                                                   type.  Useful for 
  151.                                                   debuggers. */
  152.     int         ivar_offset;                    /* Byte offset from the base 
  153.                                                   address of the instance 
  154.                                                   structure to the variable. */
  155.  
  156.   } ivar_list[1];                               /* Variable length 
  157.                                                   structure. */
  158. } IvarList, *IvarList_t;
  159.  
  160.  
  161. /*
  162. ** The compiler generates one (or more) of these structures for a class that
  163. ** has methods defined in its specification. 
  164. ** 
  165. ** The implementation of a class can be broken into separate pieces in a file
  166. ** and categories can break them across modules. To handle this problem is a
  167. ** singly linked list of methods. 
  168. */
  169. typedef struct objc_method Method;
  170. typedef Method* Method_t;
  171. typedef struct objc_method_list {
  172.   struct objc_method_list*  method_next;      /* This variable is used to link 
  173.                                                 a method list to another.  It 
  174.                                                 is a singly linked list. */
  175.   int             method_count;               /* Number of methods defined in 
  176.                                                 this structure. */
  177.   struct objc_method {
  178.     SEL         method_name;                  /* This variable is the method's 
  179.                                                 name.  It is a char*. 
  180.                                                   The unique integer passed to 
  181.                                                 objc_msg_send is a char* too.  
  182.                                                 It is compared against 
  183.                                                 method_name using strcmp. */
  184.     const char* method_types;                 /* Description of the method's
  185.                                                 parameter list.  Useful for
  186.                                                 debuggers. */
  187.     IMP         method_imp;                   /* Address of the method in the 
  188.                                                 executable. */
  189.   } method_list[1];                           /* Variable length 
  190.                                                 structure. */
  191. } MethodList, *MethodList_t;
  192.  
  193. struct objc_protocol_list {
  194.   struct objc_protocol_list *next;
  195.   int count;
  196.   Protocol *list[1];
  197. };
  198.  
  199. /*
  200. ** This is used to assure consistent access to the info field of 
  201. ** classes
  202. */
  203. #ifndef HOST_BITS_PER_LONG
  204. #define HOST_BITS_PER_LONG  (sizeof(long)*8)
  205. #endif 
  206.  
  207. #define __CLS_INFO(cls) ((cls)->info)
  208. #define __CLS_ISINFO(cls, mask) ((__CLS_INFO(cls)&mask)==mask)
  209. #define __CLS_SETINFO(cls, mask) (__CLS_INFO(cls) |= mask)
  210.  
  211. /* The structure is of type MetaClass* */
  212. #define _CLS_META 0x2L
  213. #define CLS_ISMETA(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_META))
  214.  
  215.  
  216. /* The structure is of type Class* */
  217. #define _CLS_CLASS 0x1L
  218. #define CLS_ISCLASS(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_CLASS))
  219.  
  220. /*
  221. ** The class is initialized within the runtime.  This means that 
  222. ** it has had correct super and sublinks assigned
  223. */
  224. #define _CLS_RESOLV 0x8L
  225. #define CLS_ISRESOLV(cls) __CLS_ISINFO(cls, _CLS_RESOLV)
  226. #define CLS_SETRESOLV(cls) __CLS_SETINFO(cls, _CLS_RESOLV)
  227.  
  228. /*
  229. ** The class has been send a +initialize message or a such is not 
  230. ** defined for this class
  231. */
  232. #define _CLS_INITIALIZED 0x04L
  233. #define CLS_ISINITIALIZED(cls) __CLS_ISINFO(cls, _CLS_INITIALIZED)
  234. #define CLS_SETINITIALIZED(cls) __CLS_SETINFO(cls, _CLS_INITIALIZED)
  235.  
  236. /*
  237. ** The class number of this class.  This must be the same for both the 
  238. ** class and it's meta class object
  239. */
  240. #define CLS_GETNUMBER(cls) (__CLS_INFO(cls) >> (HOST_BITS_PER_LONG/2))
  241. #define CLS_SETNUMBER(cls, num) \
  242.   ({ (cls)->info <<= (HOST_BITS_PER_LONG/2); \
  243.      (cls)->info >>= (HOST_BITS_PER_LONG/2); \
  244.      __CLS_SETINFO(cls, (((unsigned long)num) << (HOST_BITS_PER_LONG/2))); })
  245.  
  246. /*
  247. ** The compiler generates one of these structures for each category.  A class
  248. ** may have many categories and contain both instance and factory methods.  
  249. */
  250. typedef struct objc_category {
  251.   const char*   category_name;                /* Name of the category.  Name
  252.                                                 contained in the () of the
  253.                                                 category definition. */
  254.   const char*   class_name;                   /* Name of the class to which
  255.                                                 the category belongs. */
  256.   MethodList_t  instance_methods;             /* Linked list of instance
  257.                                                 methods defined in the 
  258.                                                 category. NULL indicates no
  259.                                                 instance methods defined. */
  260.   MethodList_t  class_methods;                /* Linked list of factory 
  261.                                                 methods defined in the
  262.                                                 category.  NULL indicates no
  263.                                                 class methods defined. */
  264.   struct objc_protocol_list *protocols;          /* List of Protocols 
  265.                              conformed to */
  266. } Category, *Category_t;
  267.  
  268. /*
  269. ** Structure used when a message is send to a class's super class.  The
  270. ** compiler generates one of these structures and passes it to
  271. ** objc_msg_super.
  272. */
  273. typedef struct objc_super {
  274.   id      self;                           /* Id of the object sending
  275.                                                 the message. */
  276.   Class* class;                              /* Object's super class. */
  277. } Super, *Super_t;
  278.  
  279. IMP objc_msg_lookup_super(Super_t super, SEL sel);
  280.  
  281. retval_t objc_msg_sendv(id, SEL, arglist_t);
  282.  
  283.  
  284.  
  285. /*
  286. ** This is a hook which is called by objc_lookup_class and
  287. ** objc_get_class if the runtime is not able to find the class.
  288. ** This may e.g. try to load in the class using dynamic loading.
  289. ** The function is guaranteed to be passed a non-NULL name string.
  290. */
  291. extern Class* (*_objc_lookup_class)(const char *name);
  292.  
  293. extern id (*_objc_object_alloc)(Class* class);
  294.  
  295. extern id (*_objc_object_copy)(id object);
  296.  
  297. extern id (*_objc_object_dispose)(id object);
  298.  
  299. Method_t class_get_class_method(MetaClass* class, SEL aSel);
  300.  
  301. Method_t class_get_instance_method(Class* class, SEL aSel);
  302.  
  303. Class* class_pose_as(Class* impostor, Class* superclass);
  304.  
  305. Class* objc_get_class(const char *name);
  306.  
  307. Class* objc_lookup_class(const char *name);
  308.  
  309. const char *sel_get_name(SEL selector);
  310.  
  311. SEL sel_get_uid(const char *name);
  312.  
  313. SEL sel_register_name(const char *name);
  314.  
  315. BOOL sel_is_mapped (SEL aSel);
  316.  
  317. extern id class_create_instance(Class* class);
  318.  
  319. static inline const char *
  320. class_get_class_name(Class* class)
  321. {
  322.   return CLS_ISCLASS(class)?class->name:((class==Nil)?"Nil":0);
  323. }
  324.  
  325. static inline long
  326. class_get_instance_size(Class* class)
  327. {
  328.   return CLS_ISCLASS(class)?class->instance_size:0;
  329. }
  330.  
  331. static inline MetaClass*
  332. class_get_meta_class(Class* class)
  333. {
  334.   return CLS_ISCLASS(class)?class->class_pointer:Nil;
  335. }
  336.  
  337. static inline Class*
  338. class_get_super_class(Class* class)
  339. {
  340.   return CLS_ISCLASS(class)?class->super_class:Nil;
  341. }
  342.  
  343. static inline int
  344. class_get_version(Class* class)
  345. {
  346.   return CLS_ISCLASS(class)?class->version:-1;
  347. }
  348.  
  349. static inline BOOL
  350. class_is_class(Class* class)
  351. {
  352.   return CLS_ISCLASS(class);
  353. }
  354.  
  355. static inline BOOL
  356. class_is_meta_class(Class* class)
  357. {
  358.   return CLS_ISMETA(class);
  359. }
  360.  
  361.  
  362. static inline void
  363. class_set_version(Class* class, long version)
  364. {
  365.   if (CLS_ISCLASS(class))
  366.     class->version = version;
  367. }
  368.  
  369. static inline IMP
  370. method_get_imp(Method_t method)
  371. {
  372.   return (method!=METHOD_NULL)?method->method_imp:(IMP)0;
  373. }
  374.  
  375. IMP get_imp (Class* class, SEL sel);
  376.  
  377. id object_copy(id object);
  378.  
  379. id object_dispose(id object);
  380.  
  381. static inline Class*
  382. object_get_class(id object)
  383. {
  384.   return ((object!=nil)
  385.       ? (CLS_ISCLASS(object->class_pointer)
  386.          ? object->class_pointer
  387.          : (CLS_ISMETA(object->class_pointer)
  388.         ? (Class*)object
  389.         : Nil))
  390.       : Nil);
  391. }
  392.  
  393. static inline const char *
  394. object_get_class_name(id object)
  395. {
  396.   return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
  397.                          ?object->class_pointer->name
  398.                          :((Class*)object)->name)
  399.                        :"Nil");
  400. }
  401.  
  402. static inline MetaClass*
  403. object_get_meta_class(id object)
  404. {
  405.   return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
  406.                          ?object->class_pointer->class_pointer
  407.                          :(CLS_ISMETA(object->class_pointer)
  408.                            ?object->class_pointer
  409.                            :Nil))
  410.                        :Nil);
  411. }
  412.  
  413. static inline Class*
  414. object_get_super_class
  415. (id object)
  416. {
  417.   return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
  418.                          ?object->class_pointer->super_class
  419.                          :(CLS_ISMETA(object->class_pointer)
  420.                            ?((Class*)object)->super_class
  421.                            :Nil))
  422.                        :Nil);
  423. }
  424.  
  425. static inline BOOL
  426. object_is_class(id object)
  427. {
  428.   return CLS_ISCLASS((Class*)object);
  429. }
  430.  
  431. static inline BOOL
  432. object_is_instance(id object)
  433. {
  434.   return (object!=nil)&&CLS_ISCLASS(object->class_pointer);
  435. }
  436.  
  437. static inline BOOL
  438. object_is_meta_class(id object)
  439. {
  440.   return CLS_ISMETA((Class*)object);
  441. }
  442.  
  443. #ifdef __cplusplus
  444. }
  445. #endif
  446. #endif /* not __objc_api_INCLUDE_GNU */
  447.  
  448.  
  449.  
  450.