1/* GNU Objective-C Runtime API. 2 Copyright (C) 1993, 1995, 1996, 1997 Free Software Foundation, Inc. 3 4This file is part of GNU CC. 5 6GNU CC is free software; you can redistribute it and/or modify it 7under the terms of the GNU General Public License as published by the 8Free Software Foundation; either version 2, or (at your option) any 9later version. 10 11GNU CC is distributed in the hope that it will be useful, but WITHOUT 12ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 14License for more details. 15 16You should have received a copy of the GNU General Public License 17along with GNU CC; see the file COPYING. If not, write to 18the Free Software Foundation, 59 Temple Place - Suite 330, 19Boston, MA 02111-1307, USA. */ 20 21/* As a special exception, if you link this library with files compiled 22 with GCC to produce an executable, this does not cause the resulting 23 executable to be covered by the GNU General Public License. This 24 exception does not however invalidate any other reasons why the 25 executable file might be covered by the GNU General Public License. */ 26 27#ifndef __objc_api_INCLUDE_GNU 28#define __objc_api_INCLUDE_GNU 29 30#include "objc/objc.h" 31#include "objc/hash.h" 32#include "objc/thr.h" 33#include <stdio.h> 34#include <stdarg.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*/ 44struct objc_method_description 45{ 46 SEL name; /* this is a selector, not a string */ 47 char *types; /* type encoding */ 48}; 49 50/* Filer types used to describe Ivars and Methods. */ 51#define _C_ID '@' 52#define _C_CLASS '#' 53#define _C_SEL ':' 54#define _C_CHR 'c' 55#define _C_UCHR 'C' 56#define _C_SHT 's' 57#define _C_USHT 'S' 58#define _C_INT 'i' 59#define _C_UINT 'I' 60#define _C_LNG 'l' 61#define _C_ULNG 'L' 62#define _C_LNG_LNG 'q' 63#define _C_ULNG_LNG 'Q' 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** Error handling 82** 83** Call objc_error() or objc_verror() to record an error; this error 84** routine will generally exit the program but not necessarily if the 85** user has installed his own error handler. 86** 87** Call objc_set_error_handler to assign your own function for 88** handling errors. The function should return YES if it is ok 89** to continue execution, or return NO or just abort if the 90** program should be stopped. The default error handler is just to 91** print a message on stderr. 92** 93** The error handler function should be of type objc_error_handler 94** The first parameter is an object instance of relevance. 95** The second parameter is an error code. 96** The third parameter is a format string in the printf style. 97** The fourth parameter is a variable list of arguments. 98*/ 99extern void objc_error(id object, int code, const char* fmt, ...); 100extern void objc_verror(id object, int code, const char* fmt, va_list ap); 101typedef BOOL (*objc_error_handler)(id, int code, const char *fmt, va_list ap); 102objc_error_handler objc_set_error_handler(objc_error_handler func); 103 104/* 105** Error codes 106** These are used by the runtime library, and your 107** error handling may use them to determine if the error is 108** hard or soft thus whether execution can continue or abort. 109*/ 110#define OBJC_ERR_UNKNOWN 0 /* Generic error */ 111 112#define OBJC_ERR_OBJC_VERSION 1 /* Incorrect runtime version */ 113#define OBJC_ERR_GCC_VERSION 2 /* Incorrect compiler version */ 114#define OBJC_ERR_MODULE_SIZE 3 /* Bad module size */ 115#define OBJC_ERR_PROTOCOL_VERSION 4 /* Incorrect protocol version */ 116 117#define OBJC_ERR_MEMORY 10 /* Out of memory */ 118 119#define OBJC_ERR_RECURSE_ROOT 20 /* Attempt to archive the root 120 object more than once. */ 121#define OBJC_ERR_BAD_DATA 21 /* Didn't read expected data */ 122#define OBJC_ERR_BAD_KEY 22 /* Bad key for object */ 123#define OBJC_ERR_BAD_CLASS 23 /* Unknown class */ 124#define OBJC_ERR_BAD_TYPE 24 /* Bad type specification */ 125#define OBJC_ERR_NO_READ 25 /* Cannot read stream */ 126#define OBJC_ERR_NO_WRITE 26 /* Cannot write stream */ 127#define OBJC_ERR_STREAM_VERSION 27 /* Incorrect stream version */ 128#define OBJC_ERR_BAD_OPCODE 28 /* Bad opcode */ 129 130#define OBJC_ERR_UNIMPLEMENTED 30 /* Method is not implemented */ 131 132#define OBJC_ERR_BAD_STATE 40 /* Bad thread state */ 133 134/* 135** Set this variable nonzero to print a line describing each 136** message that is sent. (this is currently disabled) 137*/ 138extern BOOL objc_trace; 139 140 141/* For every class which happens to have statically allocated instances in 142 this module, one OBJC_STATIC_INSTANCES is allocated by the compiler. 143 INSTANCES is NULL terminated and points to all statically allocated 144 instances of this class. */ 145struct objc_static_instances 146{ 147 char *class_name; 148 id instances[0]; 149}; 150 151/* 152** Whereas a Module (defined further down) is the root (typically) of a file, 153** a Symtab is the root of the class and category definitions within the 154** module. 155** 156** A Symtab contains a variable length array of pointers to classes and 157** categories defined in the module. 158*/ 159typedef struct objc_symtab { 160 unsigned long sel_ref_cnt; /* Unknown. */ 161 SEL refs; /* Unknown. */ 162 unsigned short cls_def_cnt; /* Number of classes compiled 163 (defined) in the module. */ 164 unsigned short cat_def_cnt; /* Number of categories 165 compiled (defined) in the 166 module. */ 167 168 void *defs[1]; /* Variable array of pointers. 169 cls_def_cnt of type Class 170 followed by cat_def_cnt of 171 type Category_t, followed 172 by a NULL terminated array 173 of objc_static_instances. */ 174} Symtab, *Symtab_t; 175 176 177/* 178** The compiler generates one of these structures for each module that 179** composes the executable (eg main.m). 180** 181** This data structure is the root of the definition tree for the module. 182** 183** A collect program runs between ld stages and creates a ObjC ctor array. 184** That array holds a pointer to each module structure of the executable. 185*/ 186typedef struct objc_module { 187 unsigned long version; /* Compiler revision. */ 188 unsigned long size; /* sizeof(Module). */ 189 const char* name; /* Name of the file where the 190 module was generated. The 191 name includes the path. */ 192 193 Symtab_t symtab; /* Pointer to the Symtab of 194 the module. The Symtab 195 holds an array of 196 pointers to 197 the classes and categories 198 defined in the module. */ 199} Module, *Module_t; 200 201 202/* 203** The compiler generates one of these structures for a class that has 204** instance variables defined in its specification. 205*/ 206typedef struct objc_ivar* Ivar_t; 207typedef struct objc_ivar_list { 208 int ivar_count; /* Number of structures (Ivar) 209 contained in the list. One 210 structure per instance 211 variable defined in the 212 class. */ 213 struct objc_ivar { 214 const char* ivar_name; /* Name of the instance 215 variable as entered in the 216 class definition. */ 217 const char* ivar_type; /* Description of the Ivar's 218 type. Useful for 219 debuggers. */ 220 int ivar_offset; /* Byte offset from the base 221 address of the instance 222 structure to the variable. */ 223 224 } ivar_list[1]; /* Variable length 225 structure. */ 226} IvarList, *IvarList_t; 227 228 229/* 230** The compiler generates one (or more) of these structures for a class that 231** has methods defined in its specification. 232** 233** The implementation of a class can be broken into separate pieces in a file 234** and categories can break them across modules. To handle this problem is a 235** singly linked list of methods. 236*/ 237typedef struct objc_method Method; 238typedef Method* Method_t; 239typedef struct objc_method_list { 240 struct objc_method_list* method_next; /* This variable is used to link 241 a method list to another. It 242 is a singly linked list. */ 243 int method_count; /* Number of methods defined in 244 this structure. */ 245 struct objc_method { 246 SEL method_name; /* This variable is the method's 247 name. It is a char*. 248 The unique integer passed to 249 objc_msg_send is a char* too. 250 It is compared against 251 method_name using strcmp. */ 252 const char* method_types; /* Description of the method's 253 parameter list. Useful for 254 debuggers. */ 255 IMP method_imp; /* Address of the method in the 256 executable. */ 257 } method_list[1]; /* Variable length 258 structure. */ 259} MethodList, *MethodList_t; 260 261struct objc_protocol_list { 262 struct objc_protocol_list *next; 263 int count; 264 Protocol *list[1]; 265}; 266 267/* 268** This is used to assure consistent access to the info field of 269** classes 270*/ 271#ifndef HOST_BITS_PER_LONG 272#define HOST_BITS_PER_LONG (sizeof(long)*8) 273#endif 274 275#define __CLS_INFO(cls) ((cls)->info) 276#define __CLS_ISINFO(cls, mask) ((__CLS_INFO(cls)&mask)==mask) 277#define __CLS_SETINFO(cls, mask) (__CLS_INFO(cls) |= mask) 278 279/* The structure is of type MetaClass */ 280#define _CLS_META 0x2L 281#define CLS_ISMETA(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_META)) 282 283 284/* The structure is of type Class */ 285#define _CLS_CLASS 0x1L 286#define CLS_ISCLASS(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_CLASS)) 287 288/* 289** The class is initialized within the runtime. This means that 290** it has had correct super and sublinks assigned 291*/ 292#define _CLS_RESOLV 0x8L 293#define CLS_ISRESOLV(cls) __CLS_ISINFO(cls, _CLS_RESOLV) 294#define CLS_SETRESOLV(cls) __CLS_SETINFO(cls, _CLS_RESOLV) 295 296/* 297** The class has been send a +initialize message or a such is not 298** defined for this class 299*/ 300#define _CLS_INITIALIZED 0x04L 301#define CLS_ISINITIALIZED(cls) __CLS_ISINFO(cls, _CLS_INITIALIZED) 302#define CLS_SETINITIALIZED(cls) __CLS_SETINFO(cls, _CLS_INITIALIZED) 303 304/* 305** The class number of this class. This must be the same for both the 306** class and its meta class object 307*/ 308#define CLS_GETNUMBER(cls) (__CLS_INFO(cls) >> (HOST_BITS_PER_LONG/2)) 309#define CLS_SETNUMBER(cls, num) \ 310 ({ (cls)->info <<= (HOST_BITS_PER_LONG/2); \ 311 (cls)->info >>= (HOST_BITS_PER_LONG/2); \ 312 __CLS_SETINFO(cls, (((unsigned long)num) << (HOST_BITS_PER_LONG/2))); }) 313 314/* 315** The compiler generates one of these structures for each category. A class 316** may have many categories and contain both instance and factory methods. 317*/ 318typedef struct objc_category { 319 const char* category_name; /* Name of the category. Name 320 contained in the () of the 321 category definition. */ 322 const char* class_name; /* Name of the class to which 323 the category belongs. */ 324 MethodList_t instance_methods; /* Linked list of instance 325 methods defined in the 326 category. NULL indicates no 327 instance methods defined. */ 328 MethodList_t class_methods; /* Linked list of factory 329 methods defined in the 330 category. NULL indicates no 331 class methods defined. */ 332 struct objc_protocol_list *protocols; /* List of Protocols 333 conformed to */ 334} Category, *Category_t; 335 336/* 337** Structure used when a message is send to a class's super class. The 338** compiler generates one of these structures and passes it to 339** objc_msg_super. 340*/ 341typedef struct objc_super { 342 id self; /* Id of the object sending 343 the message. */ 344 Class class; /* Object's super class. */ 345} Super, *Super_t; 346 347IMP objc_msg_lookup_super(Super_t super, SEL sel); 348 349retval_t objc_msg_sendv(id, SEL, arglist_t); 350 351 352 353/* 354** This is a hook which is called by objc_lookup_class and 355** objc_get_class if the runtime is not able to find the class. 356** This may e.g. try to load in the class using dynamic loading. 357** The function is guaranteed to be passed a non-NULL name string. 358*/ 359extern Class (*_objc_lookup_class)(const char *name); 360 361/* 362** This is a hook which is called by __objc_exec_class every time a class 363** or a category is loaded into the runtime. This may e.g. help a 364** dynamic loader determine the classes that have been loaded when 365** an object file is dynamically linked in. 366*/ 367extern void (*_objc_load_callback)(Class class, Category* category); 368 369/* 370** Hook functions for allocating, copying and disposing of instances 371*/ 372extern id (*_objc_object_alloc)(Class class); 373extern id (*_objc_object_copy)(id object); 374extern id (*_objc_object_dispose)(id object); 375 376/* 377** Standard functions for memory allocation and disposal. 378** Users should use these functions in their ObjC programs so 379** that they work properly with garbage collectors as well as 380** can take advantage of the exception/error handling available. 381*/ 382void * 383objc_malloc(size_t size); 384 385void * 386objc_atomic_malloc(size_t size); 387 388void * 389objc_valloc(size_t size); 390 391void * 392objc_realloc(void *mem, size_t size); 393 394void * 395objc_calloc(size_t nelem, size_t size); 396 397void 398objc_free(void *mem); 399 400/* 401** Hook functions for memory allocation and disposal. 402** This makes it easy to substitute garbage collection systems 403** such as Boehm's GC by assigning these function pointers 404** to the GC's allocation routines. By default these point 405** to the ANSI standard malloc, realloc, free, etc. 406** 407** Users should call the normal objc routines above for 408** memory allocation and disposal within their programs. 409*/ 410extern void *(*_objc_malloc)(size_t); 411extern void *(*_objc_atomic_malloc)(size_t); 412extern void *(*_objc_valloc)(size_t); 413extern void *(*_objc_realloc)(void *, size_t); 414extern void *(*_objc_calloc)(size_t, size_t); 415extern void (*_objc_free)(void *); 416 417Method_t class_get_class_method(MetaClass class, SEL aSel); 418 419Method_t class_get_instance_method(Class class, SEL aSel); 420 421Class class_pose_as(Class impostor, Class superclass); 422 423Class objc_get_class(const char *name); 424 425Class objc_lookup_class(const char *name); 426 427Class objc_next_class(void **enum_state); 428 429const char *sel_get_name(SEL selector); 430 431const char *sel_get_type(SEL selector); 432 433SEL sel_get_uid(const char *name); 434 435SEL sel_get_any_uid(const char *name); 436 437SEL sel_get_any_typed_uid(const char *name); 438 439SEL sel_get_typed_uid(const char *name, const char*); 440 441SEL sel_register_name(const char *name); 442 443SEL sel_register_typed_name(const char *name, const char*type); 444 445 446BOOL sel_is_mapped (SEL aSel); 447 448extern id class_create_instance(Class class); 449 450static inline const char * 451class_get_class_name(Class class) 452{ 453 return CLS_ISCLASS(class)?class->name:((class==Nil)?"Nil":0); 454} 455 456static inline long 457class_get_instance_size(Class class) 458{ 459 return CLS_ISCLASS(class)?class->instance_size:0; 460} 461 462static inline MetaClass 463class_get_meta_class(Class class) 464{ 465 return CLS_ISCLASS(class)?class->class_pointer:Nil; 466} 467 468static inline Class 469class_get_super_class(Class class) 470{ 471 return CLS_ISCLASS(class)?class->super_class:Nil; 472} 473 474static inline int 475class_get_version(Class class) 476{ 477 return CLS_ISCLASS(class)?class->version:-1; 478} 479 480static inline BOOL 481class_is_class(Class class) 482{ 483 return CLS_ISCLASS(class); 484} 485 486static inline BOOL 487class_is_meta_class(Class class) 488{ 489 return CLS_ISMETA(class); 490} 491 492 493static inline void 494class_set_version(Class class, long version) 495{ 496 if (CLS_ISCLASS(class)) 497 class->version = version; 498} 499 500static inline void * 501class_get_gc_object_type (Class class) 502{ 503 return CLS_ISCLASS(class) ? class->gc_object_type : NULL; 504} 505 506/* Mark the instance variable as innaccessible to the garbage collector */ 507extern void class_ivar_set_gcinvisible (Class class, 508 const char* ivarname, 509 BOOL gcInvisible); 510 511static inline IMP 512method_get_imp(Method_t method) 513{ 514 return (method!=METHOD_NULL)?method->method_imp:(IMP)0; 515} 516 517IMP get_imp (Class class, SEL sel); 518 519/* Redefine on NeXTSTEP so as not to conflict with system function */ 520#ifdef __NeXT__ 521#define object_copy gnu_object_copy 522#define object_dispose gnu_object_dispose 523#endif 524 525id object_copy(id object); 526 527id object_dispose(id object); 528 529static inline Class 530object_get_class(id object) 531{ 532 return ((object!=nil) 533 ? (CLS_ISCLASS(object->class_pointer) 534 ? object->class_pointer 535 : (CLS_ISMETA(object->class_pointer) 536 ? (Class)object 537 : Nil)) 538 : Nil); 539} 540 541static inline const char * 542object_get_class_name(id object) 543{ 544 return ((object!=nil)?(CLS_ISCLASS(object->class_pointer) 545 ?object->class_pointer->name 546 :((Class)object)->name) 547 :"Nil"); 548} 549 550static inline MetaClass 551object_get_meta_class(id object) 552{ 553 return ((object!=nil)?(CLS_ISCLASS(object->class_pointer) 554 ?object->class_pointer->class_pointer 555 :(CLS_ISMETA(object->class_pointer) 556 ?object->class_pointer 557 :Nil)) 558 :Nil); 559} 560 561static inline Class 562object_get_super_class 563(id object) 564{ 565 return ((object!=nil)?(CLS_ISCLASS(object->class_pointer) 566 ?object->class_pointer->super_class 567 :(CLS_ISMETA(object->class_pointer) 568 ?((Class)object)->super_class 569 :Nil)) 570 :Nil); 571} 572 573static inline BOOL 574object_is_class(id object) 575{ 576 return CLS_ISCLASS((Class)object); 577} 578 579static inline BOOL 580object_is_instance(id object) 581{ 582 return (object!=nil)&&CLS_ISCLASS(object->class_pointer); 583} 584 585static inline BOOL 586object_is_meta_class(id object) 587{ 588 return CLS_ISMETA((Class)object); 589} 590 591struct sarray* 592objc_get_uninstalled_dtable(void); 593 594#endif /* not __objc_api_INCLUDE_GNU */ 595 596 597 598