1*e4b17023SJohn Marino@c Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2*e4b17023SJohn Marino@c 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2010 3*e4b17023SJohn Marino@c Free Software Foundation, Inc. 4*e4b17023SJohn Marino@c This is part of the GCC manual. 5*e4b17023SJohn Marino@c For copying conditions, see the file gcc.texi. 6*e4b17023SJohn Marino 7*e4b17023SJohn Marino@node Objective-C 8*e4b17023SJohn Marino@comment node-name, next, previous, up 9*e4b17023SJohn Marino 10*e4b17023SJohn Marino@chapter GNU Objective-C features 11*e4b17023SJohn Marino 12*e4b17023SJohn MarinoThis document is meant to describe some of the GNU Objective-C 13*e4b17023SJohn Marinofeatures. It is not intended to teach you Objective-C. There are 14*e4b17023SJohn Marinoseveral resources on the Internet that present the language. 15*e4b17023SJohn Marino 16*e4b17023SJohn Marino@menu 17*e4b17023SJohn Marino* GNU Objective-C runtime API:: 18*e4b17023SJohn Marino* Executing code before main:: 19*e4b17023SJohn Marino* Type encoding:: 20*e4b17023SJohn Marino* Garbage Collection:: 21*e4b17023SJohn Marino* Constant string objects:: 22*e4b17023SJohn Marino* compatibility_alias:: 23*e4b17023SJohn Marino* Exceptions:: 24*e4b17023SJohn Marino* Synchronization:: 25*e4b17023SJohn Marino* Fast enumeration:: 26*e4b17023SJohn Marino* Messaging with the GNU Objective-C runtime:: 27*e4b17023SJohn Marino@end menu 28*e4b17023SJohn Marino 29*e4b17023SJohn Marino@c ========================================================================= 30*e4b17023SJohn Marino@node GNU Objective-C runtime API 31*e4b17023SJohn Marino@section GNU Objective-C runtime API 32*e4b17023SJohn Marino 33*e4b17023SJohn MarinoThis section is specific for the GNU Objective-C runtime. If you are 34*e4b17023SJohn Marinousing a different runtime, you can skip it. 35*e4b17023SJohn Marino 36*e4b17023SJohn MarinoThe GNU Objective-C runtime provides an API that allows you to 37*e4b17023SJohn Marinointeract with the Objective-C runtime system, querying the live 38*e4b17023SJohn Marinoruntime structures and even manipulating them. This allows you for 39*e4b17023SJohn Marinoexample to inspect and navigate classes, methods and protocols; to 40*e4b17023SJohn Marinodefine new classes or new methods, and even to modify existing classes 41*e4b17023SJohn Marinoor protocols. 42*e4b17023SJohn Marino 43*e4b17023SJohn MarinoIf you are using a ``Foundation'' library such as GNUstep-Base, this 44*e4b17023SJohn Marinolibrary will provide you with a rich set of functionality to do most 45*e4b17023SJohn Marinoof the inspection tasks, and you probably will only need direct access 46*e4b17023SJohn Marinoto the GNU Objective-C runtime API to define new classes or methods. 47*e4b17023SJohn Marino 48*e4b17023SJohn Marino@menu 49*e4b17023SJohn Marino* Modern GNU Objective-C runtime API:: 50*e4b17023SJohn Marino* Traditional GNU Objective-C runtime API:: 51*e4b17023SJohn Marino@end menu 52*e4b17023SJohn Marino 53*e4b17023SJohn Marino@c ========================================================================= 54*e4b17023SJohn Marino@node Modern GNU Objective-C runtime API 55*e4b17023SJohn Marino@subsection Modern GNU Objective-C runtime API 56*e4b17023SJohn Marino 57*e4b17023SJohn MarinoThe GNU Objective-C runtime provides an API which is similar to the 58*e4b17023SJohn Marinoone provided by the ``Objective-C 2.0'' Apple/NeXT Objective-C 59*e4b17023SJohn Marinoruntime. The API is documented in the public header files of the GNU 60*e4b17023SJohn MarinoObjective-C runtime: 61*e4b17023SJohn Marino 62*e4b17023SJohn Marino@itemize @bullet 63*e4b17023SJohn Marino 64*e4b17023SJohn Marino@item 65*e4b17023SJohn Marino@file{objc/objc.h}: this is the basic Objective-C header file, 66*e4b17023SJohn Marinodefining the basic Objective-C types such as @code{id}, @code{Class} 67*e4b17023SJohn Marinoand @code{BOOL}. You have to include this header to do almost 68*e4b17023SJohn Marinoanything with Objective-C. 69*e4b17023SJohn Marino 70*e4b17023SJohn Marino@item 71*e4b17023SJohn Marino@file{objc/runtime.h}: this header declares most of the public runtime 72*e4b17023SJohn MarinoAPI functions allowing you to inspect and manipulate the Objective-C 73*e4b17023SJohn Marinoruntime data structures. These functions are fairly standardized 74*e4b17023SJohn Marinoacross Objective-C runtimes and are almost identical to the Apple/NeXT 75*e4b17023SJohn MarinoObjective-C runtime ones. It does not declare functions in some 76*e4b17023SJohn Marinospecialized areas (constructing and forwarding message invocations, 77*e4b17023SJohn Marinothreading) which are in the other headers below. You have to include 78*e4b17023SJohn Marino@file{objc/objc.h} and @file{objc/runtime.h} to use any of the 79*e4b17023SJohn Marinofunctions, such as @code{class_getName()}, declared in 80*e4b17023SJohn Marino@file{objc/runtime.h}. 81*e4b17023SJohn Marino 82*e4b17023SJohn Marino@item 83*e4b17023SJohn Marino@file{objc/message.h}: this header declares public functions used to 84*e4b17023SJohn Marinoconstruct, deconstruct and forward message invocations. Because 85*e4b17023SJohn Marinomessaging is done in quite a different way on different runtimes, 86*e4b17023SJohn Marinofunctions in this header are specific to the GNU Objective-C runtime 87*e4b17023SJohn Marinoimplementation. 88*e4b17023SJohn Marino 89*e4b17023SJohn Marino@item 90*e4b17023SJohn Marino@file{objc/objc-exception.h}: this header declares some public 91*e4b17023SJohn Marinofunctions related to Objective-C exceptions. For example functions in 92*e4b17023SJohn Marinothis header allow you to throw an Objective-C exception from plain 93*e4b17023SJohn MarinoC/C++ code. 94*e4b17023SJohn Marino 95*e4b17023SJohn Marino@item 96*e4b17023SJohn Marino@file{objc/objc-sync.h}: this header declares some public functions 97*e4b17023SJohn Marinorelated to the Objective-C @code{@@synchronized()} syntax, allowing 98*e4b17023SJohn Marinoyou to emulate an Objective-C @code{@@synchronized()} block in plain 99*e4b17023SJohn MarinoC/C++ code. 100*e4b17023SJohn Marino 101*e4b17023SJohn Marino@item 102*e4b17023SJohn Marino@file{objc/thr.h}: this header declares a public runtime API threading 103*e4b17023SJohn Marinolayer that is only provided by the GNU Objective-C runtime. It 104*e4b17023SJohn Marinodeclares functions such as @code{objc_mutex_lock()}, which provide a 105*e4b17023SJohn Marinoplatform-independent set of threading functions. 106*e4b17023SJohn Marino 107*e4b17023SJohn Marino@end itemize 108*e4b17023SJohn Marino 109*e4b17023SJohn MarinoThe header files contain detailed documentation for each function in 110*e4b17023SJohn Marinothe GNU Objective-C runtime API. 111*e4b17023SJohn Marino 112*e4b17023SJohn Marino@c ========================================================================= 113*e4b17023SJohn Marino@node Traditional GNU Objective-C runtime API 114*e4b17023SJohn Marino@subsection Traditional GNU Objective-C runtime API 115*e4b17023SJohn Marino 116*e4b17023SJohn MarinoThe GNU Objective-C runtime used to provide a different API, which we 117*e4b17023SJohn Marinocall the ``traditional'' GNU Objective-C runtime API. Functions 118*e4b17023SJohn Marinobelonging to this API are easy to recognize because they use a 119*e4b17023SJohn Marinodifferent naming convention, such as @code{class_get_super_class()} 120*e4b17023SJohn Marino(traditional API) instead of @code{class_getSuperclass()} (modern 121*e4b17023SJohn MarinoAPI). Software using this API includes the file 122*e4b17023SJohn Marino@file{objc/objc-api.h} where it is declared. 123*e4b17023SJohn Marino 124*e4b17023SJohn MarinoStarting with GCC 4.7.0, the traditional GNU runtime API is no longer 125*e4b17023SJohn Marinoavailable. 126*e4b17023SJohn Marino 127*e4b17023SJohn Marino@c ========================================================================= 128*e4b17023SJohn Marino@node Executing code before main 129*e4b17023SJohn Marino@section @code{+load}: Executing code before main 130*e4b17023SJohn Marino 131*e4b17023SJohn MarinoThis section is specific for the GNU Objective-C runtime. If you are 132*e4b17023SJohn Marinousing a different runtime, you can skip it. 133*e4b17023SJohn Marino 134*e4b17023SJohn MarinoThe GNU Objective-C runtime provides a way that allows you to execute 135*e4b17023SJohn Marinocode before the execution of the program enters the @code{main} 136*e4b17023SJohn Marinofunction. The code is executed on a per-class and a per-category basis, 137*e4b17023SJohn Marinothrough a special class method @code{+load}. 138*e4b17023SJohn Marino 139*e4b17023SJohn MarinoThis facility is very useful if you want to initialize global variables 140*e4b17023SJohn Marinowhich can be accessed by the program directly, without sending a message 141*e4b17023SJohn Marinoto the class first. The usual way to initialize global variables, in the 142*e4b17023SJohn Marino@code{+initialize} method, might not be useful because 143*e4b17023SJohn Marino@code{+initialize} is only called when the first message is sent to a 144*e4b17023SJohn Marinoclass object, which in some cases could be too late. 145*e4b17023SJohn Marino 146*e4b17023SJohn MarinoSuppose for example you have a @code{FileStream} class that declares 147*e4b17023SJohn Marino@code{Stdin}, @code{Stdout} and @code{Stderr} as global variables, like 148*e4b17023SJohn Marinobelow: 149*e4b17023SJohn Marino 150*e4b17023SJohn Marino@smallexample 151*e4b17023SJohn Marino 152*e4b17023SJohn MarinoFileStream *Stdin = nil; 153*e4b17023SJohn MarinoFileStream *Stdout = nil; 154*e4b17023SJohn MarinoFileStream *Stderr = nil; 155*e4b17023SJohn Marino 156*e4b17023SJohn Marino@@implementation FileStream 157*e4b17023SJohn Marino 158*e4b17023SJohn Marino+ (void)initialize 159*e4b17023SJohn Marino@{ 160*e4b17023SJohn Marino Stdin = [[FileStream new] initWithFd:0]; 161*e4b17023SJohn Marino Stdout = [[FileStream new] initWithFd:1]; 162*e4b17023SJohn Marino Stderr = [[FileStream new] initWithFd:2]; 163*e4b17023SJohn Marino@} 164*e4b17023SJohn Marino 165*e4b17023SJohn Marino/* @r{Other methods here} */ 166*e4b17023SJohn Marino@@end 167*e4b17023SJohn Marino 168*e4b17023SJohn Marino@end smallexample 169*e4b17023SJohn Marino 170*e4b17023SJohn MarinoIn this example, the initialization of @code{Stdin}, @code{Stdout} and 171*e4b17023SJohn Marino@code{Stderr} in @code{+initialize} occurs too late. The programmer can 172*e4b17023SJohn Marinosend a message to one of these objects before the variables are actually 173*e4b17023SJohn Marinoinitialized, thus sending messages to the @code{nil} object. The 174*e4b17023SJohn Marino@code{+initialize} method which actually initializes the global 175*e4b17023SJohn Marinovariables is not invoked until the first message is sent to the class 176*e4b17023SJohn Marinoobject. The solution would require these variables to be initialized 177*e4b17023SJohn Marinojust before entering @code{main}. 178*e4b17023SJohn Marino 179*e4b17023SJohn MarinoThe correct solution of the above problem is to use the @code{+load} 180*e4b17023SJohn Marinomethod instead of @code{+initialize}: 181*e4b17023SJohn Marino 182*e4b17023SJohn Marino@smallexample 183*e4b17023SJohn Marino 184*e4b17023SJohn Marino@@implementation FileStream 185*e4b17023SJohn Marino 186*e4b17023SJohn Marino+ (void)load 187*e4b17023SJohn Marino@{ 188*e4b17023SJohn Marino Stdin = [[FileStream new] initWithFd:0]; 189*e4b17023SJohn Marino Stdout = [[FileStream new] initWithFd:1]; 190*e4b17023SJohn Marino Stderr = [[FileStream new] initWithFd:2]; 191*e4b17023SJohn Marino@} 192*e4b17023SJohn Marino 193*e4b17023SJohn Marino/* @r{Other methods here} */ 194*e4b17023SJohn Marino@@end 195*e4b17023SJohn Marino 196*e4b17023SJohn Marino@end smallexample 197*e4b17023SJohn Marino 198*e4b17023SJohn MarinoThe @code{+load} is a method that is not overridden by categories. If a 199*e4b17023SJohn Marinoclass and a category of it both implement @code{+load}, both methods are 200*e4b17023SJohn Marinoinvoked. This allows some additional initializations to be performed in 201*e4b17023SJohn Marinoa category. 202*e4b17023SJohn Marino 203*e4b17023SJohn MarinoThis mechanism is not intended to be a replacement for @code{+initialize}. 204*e4b17023SJohn MarinoYou should be aware of its limitations when you decide to use it 205*e4b17023SJohn Marinoinstead of @code{+initialize}. 206*e4b17023SJohn Marino 207*e4b17023SJohn Marino@menu 208*e4b17023SJohn Marino* What you can and what you cannot do in +load:: 209*e4b17023SJohn Marino@end menu 210*e4b17023SJohn Marino 211*e4b17023SJohn Marino 212*e4b17023SJohn Marino@node What you can and what you cannot do in +load 213*e4b17023SJohn Marino@subsection What you can and what you cannot do in @code{+load} 214*e4b17023SJohn Marino 215*e4b17023SJohn Marino@code{+load} is to be used only as a last resort. Because it is 216*e4b17023SJohn Marinoexecuted very early, most of the Objective-C runtime machinery will 217*e4b17023SJohn Marinonot be ready when @code{+load} is executed; hence @code{+load} works 218*e4b17023SJohn Marinobest for executing C code that is independent on the Objective-C 219*e4b17023SJohn Marinoruntime. 220*e4b17023SJohn Marino 221*e4b17023SJohn MarinoThe @code{+load} implementation in the GNU runtime guarantees you the 222*e4b17023SJohn Marinofollowing things: 223*e4b17023SJohn Marino 224*e4b17023SJohn Marino@itemize @bullet 225*e4b17023SJohn Marino 226*e4b17023SJohn Marino@item 227*e4b17023SJohn Marinoyou can write whatever C code you like; 228*e4b17023SJohn Marino 229*e4b17023SJohn Marino@item 230*e4b17023SJohn Marinoyou can allocate and send messages to objects whose class is implemented 231*e4b17023SJohn Marinoin the same file; 232*e4b17023SJohn Marino 233*e4b17023SJohn Marino@item 234*e4b17023SJohn Marinothe @code{+load} implementation of all super classes of a class are 235*e4b17023SJohn Marinoexecuted before the @code{+load} of that class is executed; 236*e4b17023SJohn Marino 237*e4b17023SJohn Marino@item 238*e4b17023SJohn Marinothe @code{+load} implementation of a class is executed before the 239*e4b17023SJohn Marino@code{+load} implementation of any category. 240*e4b17023SJohn Marino 241*e4b17023SJohn Marino@end itemize 242*e4b17023SJohn Marino 243*e4b17023SJohn MarinoIn particular, the following things, even if they can work in a 244*e4b17023SJohn Marinoparticular case, are not guaranteed: 245*e4b17023SJohn Marino 246*e4b17023SJohn Marino@itemize @bullet 247*e4b17023SJohn Marino 248*e4b17023SJohn Marino@item 249*e4b17023SJohn Marinoallocation of or sending messages to arbitrary objects; 250*e4b17023SJohn Marino 251*e4b17023SJohn Marino@item 252*e4b17023SJohn Marinoallocation of or sending messages to objects whose classes have a 253*e4b17023SJohn Marinocategory implemented in the same file; 254*e4b17023SJohn Marino 255*e4b17023SJohn Marino@item 256*e4b17023SJohn Marinosending messages to Objective-C constant strings (@code{@@"this is a 257*e4b17023SJohn Marinoconstant string"}); 258*e4b17023SJohn Marino 259*e4b17023SJohn Marino@end itemize 260*e4b17023SJohn Marino 261*e4b17023SJohn MarinoYou should make no assumptions about receiving @code{+load} in sibling 262*e4b17023SJohn Marinoclasses when you write @code{+load} of a class. The order in which 263*e4b17023SJohn Marinosibling classes receive @code{+load} is not guaranteed. 264*e4b17023SJohn Marino 265*e4b17023SJohn MarinoThe order in which @code{+load} and @code{+initialize} are called could 266*e4b17023SJohn Marinobe problematic if this matters. If you don't allocate objects inside 267*e4b17023SJohn Marino@code{+load}, it is guaranteed that @code{+load} is called before 268*e4b17023SJohn Marino@code{+initialize}. If you create an object inside @code{+load} the 269*e4b17023SJohn Marino@code{+initialize} method of object's class is invoked even if 270*e4b17023SJohn Marino@code{+load} was not invoked. Note if you explicitly call @code{+load} 271*e4b17023SJohn Marinoon a class, @code{+initialize} will be called first. To avoid possible 272*e4b17023SJohn Marinoproblems try to implement only one of these methods. 273*e4b17023SJohn Marino 274*e4b17023SJohn MarinoThe @code{+load} method is also invoked when a bundle is dynamically 275*e4b17023SJohn Marinoloaded into your running program. This happens automatically without any 276*e4b17023SJohn Marinointervening operation from you. When you write bundles and you need to 277*e4b17023SJohn Marinowrite @code{+load} you can safely create and send messages to objects whose 278*e4b17023SJohn Marinoclasses already exist in the running program. The same restrictions as 279*e4b17023SJohn Marinoabove apply to classes defined in bundle. 280*e4b17023SJohn Marino 281*e4b17023SJohn Marino 282*e4b17023SJohn Marino 283*e4b17023SJohn Marino@node Type encoding 284*e4b17023SJohn Marino@section Type encoding 285*e4b17023SJohn Marino 286*e4b17023SJohn MarinoThis is an advanced section. Type encodings are used extensively by 287*e4b17023SJohn Marinothe compiler and by the runtime, but you generally do not need to know 288*e4b17023SJohn Marinoabout them to use Objective-C. 289*e4b17023SJohn Marino 290*e4b17023SJohn MarinoThe Objective-C compiler generates type encodings for all the types. 291*e4b17023SJohn MarinoThese type encodings are used at runtime to find out information about 292*e4b17023SJohn Marinoselectors and methods and about objects and classes. 293*e4b17023SJohn Marino 294*e4b17023SJohn MarinoThe types are encoded in the following way: 295*e4b17023SJohn Marino 296*e4b17023SJohn Marino@c @sp 1 297*e4b17023SJohn Marino 298*e4b17023SJohn Marino@multitable @columnfractions .25 .75 299*e4b17023SJohn Marino@item @code{_Bool} 300*e4b17023SJohn Marino@tab @code{B} 301*e4b17023SJohn Marino@item @code{char} 302*e4b17023SJohn Marino@tab @code{c} 303*e4b17023SJohn Marino@item @code{unsigned char} 304*e4b17023SJohn Marino@tab @code{C} 305*e4b17023SJohn Marino@item @code{short} 306*e4b17023SJohn Marino@tab @code{s} 307*e4b17023SJohn Marino@item @code{unsigned short} 308*e4b17023SJohn Marino@tab @code{S} 309*e4b17023SJohn Marino@item @code{int} 310*e4b17023SJohn Marino@tab @code{i} 311*e4b17023SJohn Marino@item @code{unsigned int} 312*e4b17023SJohn Marino@tab @code{I} 313*e4b17023SJohn Marino@item @code{long} 314*e4b17023SJohn Marino@tab @code{l} 315*e4b17023SJohn Marino@item @code{unsigned long} 316*e4b17023SJohn Marino@tab @code{L} 317*e4b17023SJohn Marino@item @code{long long} 318*e4b17023SJohn Marino@tab @code{q} 319*e4b17023SJohn Marino@item @code{unsigned long long} 320*e4b17023SJohn Marino@tab @code{Q} 321*e4b17023SJohn Marino@item @code{float} 322*e4b17023SJohn Marino@tab @code{f} 323*e4b17023SJohn Marino@item @code{double} 324*e4b17023SJohn Marino@tab @code{d} 325*e4b17023SJohn Marino@item @code{long double} 326*e4b17023SJohn Marino@tab @code{D} 327*e4b17023SJohn Marino@item @code{void} 328*e4b17023SJohn Marino@tab @code{v} 329*e4b17023SJohn Marino@item @code{id} 330*e4b17023SJohn Marino@tab @code{@@} 331*e4b17023SJohn Marino@item @code{Class} 332*e4b17023SJohn Marino@tab @code{#} 333*e4b17023SJohn Marino@item @code{SEL} 334*e4b17023SJohn Marino@tab @code{:} 335*e4b17023SJohn Marino@item @code{char*} 336*e4b17023SJohn Marino@tab @code{*} 337*e4b17023SJohn Marino@item @code{enum} 338*e4b17023SJohn Marino@tab an @code{enum} is encoded exactly as the integer type that the compiler uses for it, which depends on the enumeration 339*e4b17023SJohn Marinovalues. Often the compiler users @code{unsigned int}, which is then encoded as @code{I}. 340*e4b17023SJohn Marino@item unknown type 341*e4b17023SJohn Marino@tab @code{?} 342*e4b17023SJohn Marino@item Complex types 343*e4b17023SJohn Marino@tab @code{j} followed by the inner type. For example @code{_Complex double} is encoded as "jd". 344*e4b17023SJohn Marino@item bit-fields 345*e4b17023SJohn Marino@tab @code{b} followed by the starting position of the bit-field, the type of the bit-field and the size of the bit-field (the bit-fields encoding was changed from the NeXT's compiler encoding, see below) 346*e4b17023SJohn Marino@end multitable 347*e4b17023SJohn Marino 348*e4b17023SJohn Marino@c @sp 1 349*e4b17023SJohn Marino 350*e4b17023SJohn MarinoThe encoding of bit-fields has changed to allow bit-fields to be 351*e4b17023SJohn Marinoproperly handled by the runtime functions that compute sizes and 352*e4b17023SJohn Marinoalignments of types that contain bit-fields. The previous encoding 353*e4b17023SJohn Marinocontained only the size of the bit-field. Using only this information 354*e4b17023SJohn Marinoit is not possible to reliably compute the size occupied by the 355*e4b17023SJohn Marinobit-field. This is very important in the presence of the Boehm's 356*e4b17023SJohn Marinogarbage collector because the objects are allocated using the typed 357*e4b17023SJohn Marinomemory facility available in this collector. The typed memory 358*e4b17023SJohn Marinoallocation requires information about where the pointers are located 359*e4b17023SJohn Marinoinside the object. 360*e4b17023SJohn Marino 361*e4b17023SJohn MarinoThe position in the bit-field is the position, counting in bits, of the 362*e4b17023SJohn Marinobit closest to the beginning of the structure. 363*e4b17023SJohn Marino 364*e4b17023SJohn MarinoThe non-atomic types are encoded as follows: 365*e4b17023SJohn Marino 366*e4b17023SJohn Marino@c @sp 1 367*e4b17023SJohn Marino 368*e4b17023SJohn Marino@multitable @columnfractions .2 .8 369*e4b17023SJohn Marino@item pointers 370*e4b17023SJohn Marino@tab @samp{^} followed by the pointed type. 371*e4b17023SJohn Marino@item arrays 372*e4b17023SJohn Marino@tab @samp{[} followed by the number of elements in the array followed by the type of the elements followed by @samp{]} 373*e4b17023SJohn Marino@item structures 374*e4b17023SJohn Marino@tab @samp{@{} followed by the name of the structure (or @samp{?} if the structure is unnamed), the @samp{=} sign, the type of the members and by @samp{@}} 375*e4b17023SJohn Marino@item unions 376*e4b17023SJohn Marino@tab @samp{(} followed by the name of the structure (or @samp{?} if the union is unnamed), the @samp{=} sign, the type of the members followed by @samp{)} 377*e4b17023SJohn Marino@item vectors 378*e4b17023SJohn Marino@tab @samp{![} followed by the vector_size (the number of bytes composing the vector) followed by a comma, followed by the alignment (in bytes) of the vector, followed by the type of the elements followed by @samp{]} 379*e4b17023SJohn Marino@end multitable 380*e4b17023SJohn Marino 381*e4b17023SJohn MarinoHere are some types and their encodings, as they are generated by the 382*e4b17023SJohn Marinocompiler on an i386 machine: 383*e4b17023SJohn Marino 384*e4b17023SJohn Marino@sp 1 385*e4b17023SJohn Marino 386*e4b17023SJohn Marino@multitable @columnfractions .25 .75 387*e4b17023SJohn Marino@item Objective-C type 388*e4b17023SJohn Marino@tab Compiler encoding 389*e4b17023SJohn Marino@item 390*e4b17023SJohn Marino@smallexample 391*e4b17023SJohn Marinoint a[10]; 392*e4b17023SJohn Marino@end smallexample 393*e4b17023SJohn Marino@tab @code{[10i]} 394*e4b17023SJohn Marino@item 395*e4b17023SJohn Marino@smallexample 396*e4b17023SJohn Marinostruct @{ 397*e4b17023SJohn Marino int i; 398*e4b17023SJohn Marino float f[3]; 399*e4b17023SJohn Marino int a:3; 400*e4b17023SJohn Marino int b:2; 401*e4b17023SJohn Marino char c; 402*e4b17023SJohn Marino@} 403*e4b17023SJohn Marino@end smallexample 404*e4b17023SJohn Marino@tab @code{@{?=i[3f]b128i3b131i2c@}} 405*e4b17023SJohn Marino@item 406*e4b17023SJohn Marino@smallexample 407*e4b17023SJohn Marinoint a __attribute__ ((vector_size (16))); 408*e4b17023SJohn Marino@end smallexample 409*e4b17023SJohn Marino@tab @code{![16,16i]} (alignment would depend on the machine) 410*e4b17023SJohn Marino@end multitable 411*e4b17023SJohn Marino 412*e4b17023SJohn Marino@sp 1 413*e4b17023SJohn Marino 414*e4b17023SJohn MarinoIn addition to the types the compiler also encodes the type 415*e4b17023SJohn Marinospecifiers. The table below describes the encoding of the current 416*e4b17023SJohn MarinoObjective-C type specifiers: 417*e4b17023SJohn Marino 418*e4b17023SJohn Marino@sp 1 419*e4b17023SJohn Marino 420*e4b17023SJohn Marino@multitable @columnfractions .25 .75 421*e4b17023SJohn Marino@item Specifier 422*e4b17023SJohn Marino@tab Encoding 423*e4b17023SJohn Marino@item @code{const} 424*e4b17023SJohn Marino@tab @code{r} 425*e4b17023SJohn Marino@item @code{in} 426*e4b17023SJohn Marino@tab @code{n} 427*e4b17023SJohn Marino@item @code{inout} 428*e4b17023SJohn Marino@tab @code{N} 429*e4b17023SJohn Marino@item @code{out} 430*e4b17023SJohn Marino@tab @code{o} 431*e4b17023SJohn Marino@item @code{bycopy} 432*e4b17023SJohn Marino@tab @code{O} 433*e4b17023SJohn Marino@item @code{byref} 434*e4b17023SJohn Marino@tab @code{R} 435*e4b17023SJohn Marino@item @code{oneway} 436*e4b17023SJohn Marino@tab @code{V} 437*e4b17023SJohn Marino@end multitable 438*e4b17023SJohn Marino 439*e4b17023SJohn Marino@sp 1 440*e4b17023SJohn Marino 441*e4b17023SJohn MarinoThe type specifiers are encoded just before the type. Unlike types 442*e4b17023SJohn Marinohowever, the type specifiers are only encoded when they appear in method 443*e4b17023SJohn Marinoargument types. 444*e4b17023SJohn Marino 445*e4b17023SJohn MarinoNote how @code{const} interacts with pointers: 446*e4b17023SJohn Marino 447*e4b17023SJohn Marino@sp 1 448*e4b17023SJohn Marino 449*e4b17023SJohn Marino@multitable @columnfractions .25 .75 450*e4b17023SJohn Marino@item Objective-C type 451*e4b17023SJohn Marino@tab Compiler encoding 452*e4b17023SJohn Marino@item 453*e4b17023SJohn Marino@smallexample 454*e4b17023SJohn Marinoconst int 455*e4b17023SJohn Marino@end smallexample 456*e4b17023SJohn Marino@tab @code{ri} 457*e4b17023SJohn Marino@item 458*e4b17023SJohn Marino@smallexample 459*e4b17023SJohn Marinoconst int* 460*e4b17023SJohn Marino@end smallexample 461*e4b17023SJohn Marino@tab @code{^ri} 462*e4b17023SJohn Marino@item 463*e4b17023SJohn Marino@smallexample 464*e4b17023SJohn Marinoint *const 465*e4b17023SJohn Marino@end smallexample 466*e4b17023SJohn Marino@tab @code{r^i} 467*e4b17023SJohn Marino@end multitable 468*e4b17023SJohn Marino 469*e4b17023SJohn Marino@sp 1 470*e4b17023SJohn Marino 471*e4b17023SJohn Marino@code{const int*} is a pointer to a @code{const int}, and so is 472*e4b17023SJohn Marinoencoded as @code{^ri}. @code{int* const}, instead, is a @code{const} 473*e4b17023SJohn Marinopointer to an @code{int}, and so is encoded as @code{r^i}. 474*e4b17023SJohn Marino 475*e4b17023SJohn MarinoFinally, there is a complication when encoding @code{const char *} 476*e4b17023SJohn Marinoversus @code{char * const}. Because @code{char *} is encoded as 477*e4b17023SJohn Marino@code{*} and not as @code{^c}, there is no way to express the fact 478*e4b17023SJohn Marinothat @code{r} applies to the pointer or to the pointee. 479*e4b17023SJohn Marino 480*e4b17023SJohn MarinoHence, it is assumed as a convention that @code{r*} means @code{const 481*e4b17023SJohn Marinochar *} (since it is what is most often meant), and there is no way to 482*e4b17023SJohn Marinoencode @code{char *const}. @code{char *const} would simply be encoded 483*e4b17023SJohn Marinoas @code{*}, and the @code{const} is lost. 484*e4b17023SJohn Marino 485*e4b17023SJohn Marino@menu 486*e4b17023SJohn Marino* Legacy type encoding:: 487*e4b17023SJohn Marino* @@encode:: 488*e4b17023SJohn Marino* Method signatures:: 489*e4b17023SJohn Marino@end menu 490*e4b17023SJohn Marino 491*e4b17023SJohn Marino@node Legacy type encoding 492*e4b17023SJohn Marino@subsection Legacy type encoding 493*e4b17023SJohn Marino 494*e4b17023SJohn MarinoUnfortunately, historically GCC used to have a number of bugs in its 495*e4b17023SJohn Marinoencoding code. The NeXT runtime expects GCC to emit type encodings in 496*e4b17023SJohn Marinothis historical format (compatible with GCC-3.3), so when using the 497*e4b17023SJohn MarinoNeXT runtime, GCC will introduce on purpose a number of incorrect 498*e4b17023SJohn Marinoencodings: 499*e4b17023SJohn Marino 500*e4b17023SJohn Marino@itemize @bullet 501*e4b17023SJohn Marino 502*e4b17023SJohn Marino@item 503*e4b17023SJohn Marinothe read-only qualifier of the pointee gets emitted before the '^'. 504*e4b17023SJohn MarinoThe read-only qualifier of the pointer itself gets ignored, unless it 505*e4b17023SJohn Marinois a typedef. Also, the 'r' is only emitted for the outermost type. 506*e4b17023SJohn Marino 507*e4b17023SJohn Marino@item 508*e4b17023SJohn Marino32-bit longs are encoded as 'l' or 'L', but not always. For typedefs, 509*e4b17023SJohn Marinothe compiler uses 'i' or 'I' instead if encoding a struct field or a 510*e4b17023SJohn Marinopointer. 511*e4b17023SJohn Marino 512*e4b17023SJohn Marino@item 513*e4b17023SJohn Marino@code{enum}s are always encoded as 'i' (int) even if they are actually 514*e4b17023SJohn Marinounsigned or long. 515*e4b17023SJohn Marino 516*e4b17023SJohn Marino@end itemize 517*e4b17023SJohn Marino 518*e4b17023SJohn MarinoIn addition to that, the NeXT runtime uses a different encoding for 519*e4b17023SJohn Marinobitfields. It encodes them as @code{b} followed by the size, without 520*e4b17023SJohn Marinoa bit offset or the underlying field type. 521*e4b17023SJohn Marino 522*e4b17023SJohn Marino@node @@encode 523*e4b17023SJohn Marino@subsection @@encode 524*e4b17023SJohn Marino 525*e4b17023SJohn MarinoGNU Objective-C supports the @code{@@encode} syntax that allows you to 526*e4b17023SJohn Marinocreate a type encoding from a C/Objective-C type. For example, 527*e4b17023SJohn Marino@code{@@encode(int)} is compiled by the compiler into @code{"i"}. 528*e4b17023SJohn Marino 529*e4b17023SJohn Marino@code{@@encode} does not support type qualifiers other than 530*e4b17023SJohn Marino@code{const}. For example, @code{@@encode(const char*)} is valid and 531*e4b17023SJohn Marinois compiled into @code{"r*"}, while @code{@@encode(bycopy char *)} is 532*e4b17023SJohn Marinoinvalid and will cause a compilation error. 533*e4b17023SJohn Marino 534*e4b17023SJohn Marino@node Method signatures 535*e4b17023SJohn Marino@subsection Method signatures 536*e4b17023SJohn Marino 537*e4b17023SJohn MarinoThis section documents the encoding of method types, which is rarely 538*e4b17023SJohn Marinoneeded to use Objective-C. You should skip it at a first reading; the 539*e4b17023SJohn Marinoruntime provides functions that will work on methods and can walk 540*e4b17023SJohn Marinothrough the list of parameters and interpret them for you. These 541*e4b17023SJohn Marinofunctions are part of the public ``API'' and are the preferred way to 542*e4b17023SJohn Marinointeract with method signatures from user code. 543*e4b17023SJohn Marino 544*e4b17023SJohn MarinoBut if you need to debug a problem with method signatures and need to 545*e4b17023SJohn Marinoknow how they are implemented (i.e., the ``ABI''), read on. 546*e4b17023SJohn Marino 547*e4b17023SJohn MarinoMethods have their ``signature'' encoded and made available to the 548*e4b17023SJohn Marinoruntime. The ``signature'' encodes all the information required to 549*e4b17023SJohn Marinodynamically build invocations of the method at runtime: return type 550*e4b17023SJohn Marinoand arguments. 551*e4b17023SJohn Marino 552*e4b17023SJohn MarinoThe ``signature'' is a null-terminated string, composed of the following: 553*e4b17023SJohn Marino 554*e4b17023SJohn Marino@itemize @bullet 555*e4b17023SJohn Marino 556*e4b17023SJohn Marino@item 557*e4b17023SJohn MarinoThe return type, including type qualifiers. For example, a method 558*e4b17023SJohn Marinoreturning @code{int} would have @code{i} here. 559*e4b17023SJohn Marino 560*e4b17023SJohn Marino@item 561*e4b17023SJohn MarinoThe total size (in bytes) required to pass all the parameters. This 562*e4b17023SJohn Marinoincludes the two hidden parameters (the object @code{self} and the 563*e4b17023SJohn Marinomethod selector @code{_cmd}). 564*e4b17023SJohn Marino 565*e4b17023SJohn Marino@item 566*e4b17023SJohn MarinoEach argument, with the type encoding, followed by the offset (in 567*e4b17023SJohn Marinobytes) of the argument in the list of parameters. 568*e4b17023SJohn Marino 569*e4b17023SJohn Marino@end itemize 570*e4b17023SJohn Marino 571*e4b17023SJohn MarinoFor example, a method with no arguments and returning @code{int} would 572*e4b17023SJohn Marinohave the signature @code{i8@@0:4} if the size of a pointer is 4. The 573*e4b17023SJohn Marinosignature is interpreted as follows: the @code{i} is the return type 574*e4b17023SJohn Marino(an @code{int}), the @code{8} is the total size of the parameters in 575*e4b17023SJohn Marinobytes (two pointers each of size 4), the @code{@@0} is the first 576*e4b17023SJohn Marinoparameter (an object at byte offset @code{0}) and @code{:4} is the 577*e4b17023SJohn Marinosecond parameter (a @code{SEL} at byte offset @code{4}). 578*e4b17023SJohn Marino 579*e4b17023SJohn MarinoYou can easily find more examples by running the ``strings'' program 580*e4b17023SJohn Marinoon an Objective-C object file compiled by GCC. You'll see a lot of 581*e4b17023SJohn Marinostrings that look very much like @code{i8@@0:4}. They are signatures 582*e4b17023SJohn Marinoof Objective-C methods. 583*e4b17023SJohn Marino 584*e4b17023SJohn Marino 585*e4b17023SJohn Marino@node Garbage Collection 586*e4b17023SJohn Marino@section Garbage Collection 587*e4b17023SJohn Marino 588*e4b17023SJohn MarinoThis section is specific for the GNU Objective-C runtime. If you are 589*e4b17023SJohn Marinousing a different runtime, you can skip it. 590*e4b17023SJohn Marino 591*e4b17023SJohn MarinoSupport for garbage collection with the GNU runtime has been added by 592*e4b17023SJohn Marinousing a powerful conservative garbage collector, known as the 593*e4b17023SJohn MarinoBoehm-Demers-Weiser conservative garbage collector. 594*e4b17023SJohn Marino 595*e4b17023SJohn MarinoTo enable the support for it you have to configure the compiler using 596*e4b17023SJohn Marinoan additional argument, @w{@option{--enable-objc-gc}}. This will 597*e4b17023SJohn Marinobuild the boehm-gc library, and build an additional runtime library 598*e4b17023SJohn Marinowhich has several enhancements to support the garbage collector. The 599*e4b17023SJohn Marinonew library has a new name, @file{libobjc_gc.a} to not conflict with 600*e4b17023SJohn Marinothe non-garbage-collected library. 601*e4b17023SJohn Marino 602*e4b17023SJohn MarinoWhen the garbage collector is used, the objects are allocated using the 603*e4b17023SJohn Marinoso-called typed memory allocation mechanism available in the 604*e4b17023SJohn MarinoBoehm-Demers-Weiser collector. This mode requires precise information on 605*e4b17023SJohn Marinowhere pointers are located inside objects. This information is computed 606*e4b17023SJohn Marinoonce per class, immediately after the class has been initialized. 607*e4b17023SJohn Marino 608*e4b17023SJohn MarinoThere is a new runtime function @code{class_ivar_set_gcinvisible()} 609*e4b17023SJohn Marinowhich can be used to declare a so-called @dfn{weak pointer} 610*e4b17023SJohn Marinoreference. Such a pointer is basically hidden for the garbage collector; 611*e4b17023SJohn Marinothis can be useful in certain situations, especially when you want to 612*e4b17023SJohn Marinokeep track of the allocated objects, yet allow them to be 613*e4b17023SJohn Marinocollected. This kind of pointers can only be members of objects, you 614*e4b17023SJohn Marinocannot declare a global pointer as a weak reference. Every type which is 615*e4b17023SJohn Marinoa pointer type can be declared a weak pointer, including @code{id}, 616*e4b17023SJohn Marino@code{Class} and @code{SEL}. 617*e4b17023SJohn Marino 618*e4b17023SJohn MarinoHere is an example of how to use this feature. Suppose you want to 619*e4b17023SJohn Marinoimplement a class whose instances hold a weak pointer reference; the 620*e4b17023SJohn Marinofollowing class does this: 621*e4b17023SJohn Marino 622*e4b17023SJohn Marino@smallexample 623*e4b17023SJohn Marino 624*e4b17023SJohn Marino@@interface WeakPointer : Object 625*e4b17023SJohn Marino@{ 626*e4b17023SJohn Marino const void* weakPointer; 627*e4b17023SJohn Marino@} 628*e4b17023SJohn Marino 629*e4b17023SJohn Marino- initWithPointer:(const void*)p; 630*e4b17023SJohn Marino- (const void*)weakPointer; 631*e4b17023SJohn Marino@@end 632*e4b17023SJohn Marino 633*e4b17023SJohn Marino 634*e4b17023SJohn Marino@@implementation WeakPointer 635*e4b17023SJohn Marino 636*e4b17023SJohn Marino+ (void)initialize 637*e4b17023SJohn Marino@{ 638*e4b17023SJohn Marino if (self == objc_lookUpClass ("WeakPointer")) 639*e4b17023SJohn Marino class_ivar_set_gcinvisible (self, "weakPointer", YES); 640*e4b17023SJohn Marino@} 641*e4b17023SJohn Marino 642*e4b17023SJohn Marino- initWithPointer:(const void*)p 643*e4b17023SJohn Marino@{ 644*e4b17023SJohn Marino weakPointer = p; 645*e4b17023SJohn Marino return self; 646*e4b17023SJohn Marino@} 647*e4b17023SJohn Marino 648*e4b17023SJohn Marino- (const void*)weakPointer 649*e4b17023SJohn Marino@{ 650*e4b17023SJohn Marino return weakPointer; 651*e4b17023SJohn Marino@} 652*e4b17023SJohn Marino 653*e4b17023SJohn Marino@@end 654*e4b17023SJohn Marino 655*e4b17023SJohn Marino@end smallexample 656*e4b17023SJohn Marino 657*e4b17023SJohn MarinoWeak pointers are supported through a new type character specifier 658*e4b17023SJohn Marinorepresented by the @samp{!} character. The 659*e4b17023SJohn Marino@code{class_ivar_set_gcinvisible()} function adds or removes this 660*e4b17023SJohn Marinospecifier to the string type description of the instance variable named 661*e4b17023SJohn Marinoas argument. 662*e4b17023SJohn Marino 663*e4b17023SJohn Marino@c ========================================================================= 664*e4b17023SJohn Marino@node Constant string objects 665*e4b17023SJohn Marino@section Constant string objects 666*e4b17023SJohn Marino 667*e4b17023SJohn MarinoGNU Objective-C provides constant string objects that are generated 668*e4b17023SJohn Marinodirectly by the compiler. You declare a constant string object by 669*e4b17023SJohn Marinoprefixing a C constant string with the character @samp{@@}: 670*e4b17023SJohn Marino 671*e4b17023SJohn Marino@smallexample 672*e4b17023SJohn Marino id myString = @@"this is a constant string object"; 673*e4b17023SJohn Marino@end smallexample 674*e4b17023SJohn Marino 675*e4b17023SJohn MarinoThe constant string objects are by default instances of the 676*e4b17023SJohn Marino@code{NXConstantString} class which is provided by the GNU Objective-C 677*e4b17023SJohn Marinoruntime. To get the definition of this class you must include the 678*e4b17023SJohn Marino@file{objc/NXConstStr.h} header file. 679*e4b17023SJohn Marino 680*e4b17023SJohn MarinoUser defined libraries may want to implement their own constant string 681*e4b17023SJohn Marinoclass. To be able to support them, the GNU Objective-C compiler provides 682*e4b17023SJohn Marinoa new command line options @option{-fconstant-string-class=@var{class-name}}. 683*e4b17023SJohn MarinoThe provided class should adhere to a strict structure, the same 684*e4b17023SJohn Marinoas @code{NXConstantString}'s structure: 685*e4b17023SJohn Marino 686*e4b17023SJohn Marino@smallexample 687*e4b17023SJohn Marino 688*e4b17023SJohn Marino@@interface MyConstantStringClass 689*e4b17023SJohn Marino@{ 690*e4b17023SJohn Marino Class isa; 691*e4b17023SJohn Marino char *c_string; 692*e4b17023SJohn Marino unsigned int len; 693*e4b17023SJohn Marino@} 694*e4b17023SJohn Marino@@end 695*e4b17023SJohn Marino 696*e4b17023SJohn Marino@end smallexample 697*e4b17023SJohn Marino 698*e4b17023SJohn Marino@code{NXConstantString} inherits from @code{Object}; user class 699*e4b17023SJohn Marinolibraries may choose to inherit the customized constant string class 700*e4b17023SJohn Marinofrom a different class than @code{Object}. There is no requirement in 701*e4b17023SJohn Marinothe methods the constant string class has to implement, but the final 702*e4b17023SJohn Marinoivar layout of the class must be the compatible with the given 703*e4b17023SJohn Marinostructure. 704*e4b17023SJohn Marino 705*e4b17023SJohn MarinoWhen the compiler creates the statically allocated constant string 706*e4b17023SJohn Marinoobject, the @code{c_string} field will be filled by the compiler with 707*e4b17023SJohn Marinothe string; the @code{length} field will be filled by the compiler with 708*e4b17023SJohn Marinothe string length; the @code{isa} pointer will be filled with 709*e4b17023SJohn Marino@code{NULL} by the compiler, and it will later be fixed up automatically 710*e4b17023SJohn Marinoat runtime by the GNU Objective-C runtime library to point to the class 711*e4b17023SJohn Marinowhich was set by the @option{-fconstant-string-class} option when the 712*e4b17023SJohn Marinoobject file is loaded (if you wonder how it works behind the scenes, the 713*e4b17023SJohn Marinoname of the class to use, and the list of static objects to fixup, are 714*e4b17023SJohn Marinostored by the compiler in the object file in a place where the GNU 715*e4b17023SJohn Marinoruntime library will find them at runtime). 716*e4b17023SJohn Marino 717*e4b17023SJohn MarinoAs a result, when a file is compiled with the 718*e4b17023SJohn Marino@option{-fconstant-string-class} option, all the constant string objects 719*e4b17023SJohn Marinowill be instances of the class specified as argument to this option. It 720*e4b17023SJohn Marinois possible to have multiple compilation units referring to different 721*e4b17023SJohn Marinoconstant string classes, neither the compiler nor the linker impose any 722*e4b17023SJohn Marinorestrictions in doing this. 723*e4b17023SJohn Marino 724*e4b17023SJohn Marino@c ========================================================================= 725*e4b17023SJohn Marino@node compatibility_alias 726*e4b17023SJohn Marino@section compatibility_alias 727*e4b17023SJohn Marino 728*e4b17023SJohn MarinoThe keyword @code{@@compatibility_alias} allows you to define a class name 729*e4b17023SJohn Marinoas equivalent to another class name. For example: 730*e4b17023SJohn Marino 731*e4b17023SJohn Marino@smallexample 732*e4b17023SJohn Marino@@compatibility_alias WOApplication GSWApplication; 733*e4b17023SJohn Marino@end smallexample 734*e4b17023SJohn Marino 735*e4b17023SJohn Marinotells the compiler that each time it encounters @code{WOApplication} as 736*e4b17023SJohn Marinoa class name, it should replace it with @code{GSWApplication} (that is, 737*e4b17023SJohn Marino@code{WOApplication} is just an alias for @code{GSWApplication}). 738*e4b17023SJohn Marino 739*e4b17023SJohn MarinoThere are some constraints on how this can be used--- 740*e4b17023SJohn Marino 741*e4b17023SJohn Marino@itemize @bullet 742*e4b17023SJohn Marino 743*e4b17023SJohn Marino@item @code{WOApplication} (the alias) must not be an existing class; 744*e4b17023SJohn Marino 745*e4b17023SJohn Marino@item @code{GSWApplication} (the real class) must be an existing class. 746*e4b17023SJohn Marino 747*e4b17023SJohn Marino@end itemize 748*e4b17023SJohn Marino 749*e4b17023SJohn Marino@c ========================================================================= 750*e4b17023SJohn Marino@node Exceptions 751*e4b17023SJohn Marino@section Exceptions 752*e4b17023SJohn Marino 753*e4b17023SJohn MarinoGNU Objective-C provides exception support built into the language, as 754*e4b17023SJohn Marinoin the following example: 755*e4b17023SJohn Marino 756*e4b17023SJohn Marino@smallexample 757*e4b17023SJohn Marino @@try @{ 758*e4b17023SJohn Marino @dots{} 759*e4b17023SJohn Marino @@throw expr; 760*e4b17023SJohn Marino @dots{} 761*e4b17023SJohn Marino @} 762*e4b17023SJohn Marino @@catch (AnObjCClass *exc) @{ 763*e4b17023SJohn Marino @dots{} 764*e4b17023SJohn Marino @@throw expr; 765*e4b17023SJohn Marino @dots{} 766*e4b17023SJohn Marino @@throw; 767*e4b17023SJohn Marino @dots{} 768*e4b17023SJohn Marino @} 769*e4b17023SJohn Marino @@catch (AnotherClass *exc) @{ 770*e4b17023SJohn Marino @dots{} 771*e4b17023SJohn Marino @} 772*e4b17023SJohn Marino @@catch (id allOthers) @{ 773*e4b17023SJohn Marino @dots{} 774*e4b17023SJohn Marino @} 775*e4b17023SJohn Marino @@finally @{ 776*e4b17023SJohn Marino @dots{} 777*e4b17023SJohn Marino @@throw expr; 778*e4b17023SJohn Marino @dots{} 779*e4b17023SJohn Marino @} 780*e4b17023SJohn Marino@end smallexample 781*e4b17023SJohn Marino 782*e4b17023SJohn MarinoThe @code{@@throw} statement may appear anywhere in an Objective-C or 783*e4b17023SJohn MarinoObjective-C++ program; when used inside of a @code{@@catch} block, the 784*e4b17023SJohn Marino@code{@@throw} may appear without an argument (as shown above), in 785*e4b17023SJohn Marinowhich case the object caught by the @code{@@catch} will be rethrown. 786*e4b17023SJohn Marino 787*e4b17023SJohn MarinoNote that only (pointers to) Objective-C objects may be thrown and 788*e4b17023SJohn Marinocaught using this scheme. When an object is thrown, it will be caught 789*e4b17023SJohn Marinoby the nearest @code{@@catch} clause capable of handling objects of 790*e4b17023SJohn Marinothat type, analogously to how @code{catch} blocks work in C++ and 791*e4b17023SJohn MarinoJava. A @code{@@catch(id @dots{})} clause (as shown above) may also 792*e4b17023SJohn Marinobe provided to catch any and all Objective-C exceptions not caught by 793*e4b17023SJohn Marinoprevious @code{@@catch} clauses (if any). 794*e4b17023SJohn Marino 795*e4b17023SJohn MarinoThe @code{@@finally} clause, if present, will be executed upon exit 796*e4b17023SJohn Marinofrom the immediately preceding @code{@@try @dots{} @@catch} section. 797*e4b17023SJohn MarinoThis will happen regardless of whether any exceptions are thrown, 798*e4b17023SJohn Marinocaught or rethrown inside the @code{@@try @dots{} @@catch} section, 799*e4b17023SJohn Marinoanalogously to the behavior of the @code{finally} clause in Java. 800*e4b17023SJohn Marino 801*e4b17023SJohn MarinoThere are several caveats to using the new exception mechanism: 802*e4b17023SJohn Marino 803*e4b17023SJohn Marino@itemize @bullet 804*e4b17023SJohn Marino@item 805*e4b17023SJohn MarinoThe @option{-fobjc-exceptions} command line option must be used when 806*e4b17023SJohn Marinocompiling Objective-C files that use exceptions. 807*e4b17023SJohn Marino 808*e4b17023SJohn Marino@item 809*e4b17023SJohn MarinoWith the GNU runtime, exceptions are always implemented as ``native'' 810*e4b17023SJohn Marinoexceptions and it is recommended that the @option{-fexceptions} and 811*e4b17023SJohn Marino@option{-shared-libgcc} options are used when linking. 812*e4b17023SJohn Marino 813*e4b17023SJohn Marino@item 814*e4b17023SJohn MarinoWith the NeXT runtime, although currently designed to be binary 815*e4b17023SJohn Marinocompatible with @code{NS_HANDLER}-style idioms provided by the 816*e4b17023SJohn Marino@code{NSException} class, the new exceptions can only be used on Mac 817*e4b17023SJohn MarinoOS X 10.3 (Panther) and later systems, due to additional functionality 818*e4b17023SJohn Marinoneeded in the NeXT Objective-C runtime. 819*e4b17023SJohn Marino 820*e4b17023SJohn Marino@item 821*e4b17023SJohn MarinoAs mentioned above, the new exceptions do not support handling 822*e4b17023SJohn Marinotypes other than Objective-C objects. Furthermore, when used from 823*e4b17023SJohn MarinoObjective-C++, the Objective-C exception model does not interoperate with C++ 824*e4b17023SJohn Marinoexceptions at this time. This means you cannot @code{@@throw} an exception 825*e4b17023SJohn Marinofrom Objective-C and @code{catch} it in C++, or vice versa 826*e4b17023SJohn Marino(i.e., @code{throw @dots{} @@catch}). 827*e4b17023SJohn Marino@end itemize 828*e4b17023SJohn Marino 829*e4b17023SJohn Marino@c ========================================================================= 830*e4b17023SJohn Marino@node Synchronization 831*e4b17023SJohn Marino@section Synchronization 832*e4b17023SJohn Marino 833*e4b17023SJohn MarinoGNU Objective-C provides support for synchronized blocks: 834*e4b17023SJohn Marino 835*e4b17023SJohn Marino@smallexample 836*e4b17023SJohn Marino @@synchronized (ObjCClass *guard) @{ 837*e4b17023SJohn Marino @dots{} 838*e4b17023SJohn Marino @} 839*e4b17023SJohn Marino@end smallexample 840*e4b17023SJohn Marino 841*e4b17023SJohn MarinoUpon entering the @code{@@synchronized} block, a thread of execution 842*e4b17023SJohn Marinoshall first check whether a lock has been placed on the corresponding 843*e4b17023SJohn Marino@code{guard} object by another thread. If it has, the current thread 844*e4b17023SJohn Marinoshall wait until the other thread relinquishes its lock. Once 845*e4b17023SJohn Marino@code{guard} becomes available, the current thread will place its own 846*e4b17023SJohn Marinolock on it, execute the code contained in the @code{@@synchronized} 847*e4b17023SJohn Marinoblock, and finally relinquish the lock (thereby making @code{guard} 848*e4b17023SJohn Marinoavailable to other threads). 849*e4b17023SJohn Marino 850*e4b17023SJohn MarinoUnlike Java, Objective-C does not allow for entire methods to be 851*e4b17023SJohn Marinomarked @code{@@synchronized}. Note that throwing exceptions out of 852*e4b17023SJohn Marino@code{@@synchronized} blocks is allowed, and will cause the guarding 853*e4b17023SJohn Marinoobject to be unlocked properly. 854*e4b17023SJohn Marino 855*e4b17023SJohn MarinoBecause of the interactions between synchronization and exception 856*e4b17023SJohn Marinohandling, you can only use @code{@@synchronized} when compiling with 857*e4b17023SJohn Marinoexceptions enabled, that is with the command line option 858*e4b17023SJohn Marino@option{-fobjc-exceptions}. 859*e4b17023SJohn Marino 860*e4b17023SJohn Marino 861*e4b17023SJohn Marino@c ========================================================================= 862*e4b17023SJohn Marino@node Fast enumeration 863*e4b17023SJohn Marino@section Fast enumeration 864*e4b17023SJohn Marino 865*e4b17023SJohn Marino@menu 866*e4b17023SJohn Marino* Using fast enumeration:: 867*e4b17023SJohn Marino* c99-like fast enumeration syntax:: 868*e4b17023SJohn Marino* Fast enumeration details:: 869*e4b17023SJohn Marino* Fast enumeration protocol:: 870*e4b17023SJohn Marino@end menu 871*e4b17023SJohn Marino 872*e4b17023SJohn Marino@c ================================ 873*e4b17023SJohn Marino@node Using fast enumeration 874*e4b17023SJohn Marino@subsection Using fast enumeration 875*e4b17023SJohn Marino 876*e4b17023SJohn MarinoGNU Objective-C provides support for the fast enumeration syntax: 877*e4b17023SJohn Marino 878*e4b17023SJohn Marino@smallexample 879*e4b17023SJohn Marino id array = @dots{}; 880*e4b17023SJohn Marino id object; 881*e4b17023SJohn Marino 882*e4b17023SJohn Marino for (object in array) 883*e4b17023SJohn Marino @{ 884*e4b17023SJohn Marino /* Do something with 'object' */ 885*e4b17023SJohn Marino @} 886*e4b17023SJohn Marino@end smallexample 887*e4b17023SJohn Marino 888*e4b17023SJohn Marino@code{array} needs to be an Objective-C object (usually a collection 889*e4b17023SJohn Marinoobject, for example an array, a dictionary or a set) which implements 890*e4b17023SJohn Marinothe ``Fast Enumeration Protocol'' (see below). If you are using a 891*e4b17023SJohn MarinoFoundation library such as GNUstep Base or Apple Cocoa Foundation, all 892*e4b17023SJohn Marinocollection objects in the library implement this protocol and can be 893*e4b17023SJohn Marinoused in this way. 894*e4b17023SJohn Marino 895*e4b17023SJohn MarinoThe code above would iterate over all objects in @code{array}. For 896*e4b17023SJohn Marinoeach of them, it assigns it to @code{object}, then executes the 897*e4b17023SJohn Marino@code{Do something with 'object'} statements. 898*e4b17023SJohn Marino 899*e4b17023SJohn MarinoHere is a fully worked-out example using a Foundation library (which 900*e4b17023SJohn Marinoprovides the implementation of @code{NSArray}, @code{NSString} and 901*e4b17023SJohn Marino@code{NSLog}): 902*e4b17023SJohn Marino 903*e4b17023SJohn Marino@smallexample 904*e4b17023SJohn Marino NSArray *array = [NSArray arrayWithObjects: @@"1", @@"2", @@"3", nil]; 905*e4b17023SJohn Marino NSString *object; 906*e4b17023SJohn Marino 907*e4b17023SJohn Marino for (object in array) 908*e4b17023SJohn Marino NSLog (@@"Iterating over %@@", object); 909*e4b17023SJohn Marino@end smallexample 910*e4b17023SJohn Marino 911*e4b17023SJohn Marino 912*e4b17023SJohn Marino@c ================================ 913*e4b17023SJohn Marino@node c99-like fast enumeration syntax 914*e4b17023SJohn Marino@subsection c99-like fast enumeration syntax 915*e4b17023SJohn Marino 916*e4b17023SJohn MarinoA c99-like declaration syntax is also allowed: 917*e4b17023SJohn Marino 918*e4b17023SJohn Marino@smallexample 919*e4b17023SJohn Marino id array = @dots{}; 920*e4b17023SJohn Marino 921*e4b17023SJohn Marino for (id object in array) 922*e4b17023SJohn Marino @{ 923*e4b17023SJohn Marino /* Do something with 'object' */ 924*e4b17023SJohn Marino @} 925*e4b17023SJohn Marino@end smallexample 926*e4b17023SJohn Marino 927*e4b17023SJohn Marinothis is completely equivalent to: 928*e4b17023SJohn Marino 929*e4b17023SJohn Marino@smallexample 930*e4b17023SJohn Marino id array = @dots{}; 931*e4b17023SJohn Marino 932*e4b17023SJohn Marino @{ 933*e4b17023SJohn Marino id object; 934*e4b17023SJohn Marino for (object in array) 935*e4b17023SJohn Marino @{ 936*e4b17023SJohn Marino /* Do something with 'object' */ 937*e4b17023SJohn Marino @} 938*e4b17023SJohn Marino @} 939*e4b17023SJohn Marino@end smallexample 940*e4b17023SJohn Marino 941*e4b17023SJohn Marinobut can save some typing. 942*e4b17023SJohn Marino 943*e4b17023SJohn MarinoNote that the option @option{-std=c99} is not required to allow this 944*e4b17023SJohn Marinosyntax in Objective-C. 945*e4b17023SJohn Marino 946*e4b17023SJohn Marino@c ================================ 947*e4b17023SJohn Marino@node Fast enumeration details 948*e4b17023SJohn Marino@subsection Fast enumeration details 949*e4b17023SJohn Marino 950*e4b17023SJohn MarinoHere is a more technical description with the gory details. Consider the code 951*e4b17023SJohn Marino 952*e4b17023SJohn Marino@smallexample 953*e4b17023SJohn Marino for (@var{object expression} in @var{collection expression}) 954*e4b17023SJohn Marino @{ 955*e4b17023SJohn Marino @var{statements} 956*e4b17023SJohn Marino @} 957*e4b17023SJohn Marino@end smallexample 958*e4b17023SJohn Marino 959*e4b17023SJohn Marinohere is what happens when you run it: 960*e4b17023SJohn Marino 961*e4b17023SJohn Marino@itemize @bullet 962*e4b17023SJohn Marino@item 963*e4b17023SJohn Marino@code{@var{collection expression}} is evaluated exactly once and the 964*e4b17023SJohn Marinoresult is used as the collection object to iterate over. This means 965*e4b17023SJohn Marinoit is safe to write code such as @code{for (object in [NSDictionary 966*e4b17023SJohn MarinokeyEnumerator]) @dots{}}. 967*e4b17023SJohn Marino 968*e4b17023SJohn Marino@item 969*e4b17023SJohn Marinothe iteration is implemented by the compiler by repeatedly getting 970*e4b17023SJohn Marinobatches of objects from the collection object using the fast 971*e4b17023SJohn Marinoenumeration protocol (see below), then iterating over all objects in 972*e4b17023SJohn Marinothe batch. This is faster than a normal enumeration where objects are 973*e4b17023SJohn Marinoretrieved one by one (hence the name ``fast enumeration''). 974*e4b17023SJohn Marino 975*e4b17023SJohn Marino@item 976*e4b17023SJohn Marinoif there are no objects in the collection, then 977*e4b17023SJohn Marino@code{@var{object expression}} is set to @code{nil} and the loop 978*e4b17023SJohn Marinoimmediately terminates. 979*e4b17023SJohn Marino 980*e4b17023SJohn Marino@item 981*e4b17023SJohn Marinoif there are objects in the collection, then for each object in the 982*e4b17023SJohn Marinocollection (in the order they are returned) @code{@var{object expression}} 983*e4b17023SJohn Marinois set to the object, then @code{@var{statements}} are executed. 984*e4b17023SJohn Marino 985*e4b17023SJohn Marino@item 986*e4b17023SJohn Marino@code{@var{statements}} can contain @code{break} and @code{continue} 987*e4b17023SJohn Marinocommands, which will abort the iteration or skip to the next loop 988*e4b17023SJohn Marinoiteration as expected. 989*e4b17023SJohn Marino 990*e4b17023SJohn Marino@item 991*e4b17023SJohn Marinowhen the iteration ends because there are no more objects to iterate 992*e4b17023SJohn Marinoover, @code{@var{object expression}} is set to @code{nil}. This allows 993*e4b17023SJohn Marinoyou to determine whether the iteration finished because a @code{break} 994*e4b17023SJohn Marinocommand was used (in which case @code{@var{object expression}} will remain 995*e4b17023SJohn Marinoset to the last object that was iterated over) or because it iterated 996*e4b17023SJohn Marinoover all the objects (in which case @code{@var{object expression}} will be 997*e4b17023SJohn Marinoset to @code{nil}). 998*e4b17023SJohn Marino 999*e4b17023SJohn Marino@item 1000*e4b17023SJohn Marino@code{@var{statements}} must not make any changes to the collection 1001*e4b17023SJohn Marinoobject; if they do, it is a hard error and the fast enumeration 1002*e4b17023SJohn Marinoterminates by invoking @code{objc_enumerationMutation}, a runtime 1003*e4b17023SJohn Marinofunction that normally aborts the program but which can be customized 1004*e4b17023SJohn Marinoby Foundation libraries via @code{objc_set_mutation_handler} to do 1005*e4b17023SJohn Marinosomething different, such as raising an exception. 1006*e4b17023SJohn Marino 1007*e4b17023SJohn Marino@end itemize 1008*e4b17023SJohn Marino 1009*e4b17023SJohn Marino@c ================================ 1010*e4b17023SJohn Marino@node Fast enumeration protocol 1011*e4b17023SJohn Marino@subsection Fast enumeration protocol 1012*e4b17023SJohn Marino 1013*e4b17023SJohn MarinoIf you want your own collection object to be usable with fast 1014*e4b17023SJohn Marinoenumeration, you need to have it implement the method 1015*e4b17023SJohn Marino 1016*e4b17023SJohn Marino@smallexample 1017*e4b17023SJohn Marino- (unsigned long) countByEnumeratingWithState: (NSFastEnumerationState *)state 1018*e4b17023SJohn Marino objects: (id *)objects 1019*e4b17023SJohn Marino count: (unsigned long)len; 1020*e4b17023SJohn Marino@end smallexample 1021*e4b17023SJohn Marino 1022*e4b17023SJohn Marinowhere @code{NSFastEnumerationState} must be defined in your code as follows: 1023*e4b17023SJohn Marino 1024*e4b17023SJohn Marino@smallexample 1025*e4b17023SJohn Marinotypedef struct 1026*e4b17023SJohn Marino@{ 1027*e4b17023SJohn Marino unsigned long state; 1028*e4b17023SJohn Marino id *itemsPtr; 1029*e4b17023SJohn Marino unsigned long *mutationsPtr; 1030*e4b17023SJohn Marino unsigned long extra[5]; 1031*e4b17023SJohn Marino@} NSFastEnumerationState; 1032*e4b17023SJohn Marino@end smallexample 1033*e4b17023SJohn Marino 1034*e4b17023SJohn MarinoIf no @code{NSFastEnumerationState} is defined in your code, the 1035*e4b17023SJohn Marinocompiler will automatically replace @code{NSFastEnumerationState *} 1036*e4b17023SJohn Marinowith @code{struct __objcFastEnumerationState *}, where that type is 1037*e4b17023SJohn Marinosilently defined by the compiler in an identical way. This can be 1038*e4b17023SJohn Marinoconfusing and we recommend that you define 1039*e4b17023SJohn Marino@code{NSFastEnumerationState} (as shown above) instead. 1040*e4b17023SJohn Marino 1041*e4b17023SJohn MarinoThe method is called repeatedly during a fast enumeration to retrieve 1042*e4b17023SJohn Marinobatches of objects. Each invocation of the method should retrieve the 1043*e4b17023SJohn Marinonext batch of objects. 1044*e4b17023SJohn Marino 1045*e4b17023SJohn MarinoThe return value of the method is the number of objects in the current 1046*e4b17023SJohn Marinobatch; this should not exceed @code{len}, which is the maximum size of 1047*e4b17023SJohn Marinoa batch as requested by the caller. The batch itself is returned in 1048*e4b17023SJohn Marinothe @code{itemsPtr} field of the @code{NSFastEnumerationState} struct. 1049*e4b17023SJohn Marino 1050*e4b17023SJohn MarinoTo help with returning the objects, the @code{objects} array is a C 1051*e4b17023SJohn Marinoarray preallocated by the caller (on the stack) of size @code{len}. 1052*e4b17023SJohn MarinoIn many cases you can put the objects you want to return in that 1053*e4b17023SJohn Marino@code{objects} array, then do @code{itemsPtr = objects}. But you 1054*e4b17023SJohn Marinodon't have to; if your collection already has the objects to return in 1055*e4b17023SJohn Marinosome form of C array, it could return them from there instead. 1056*e4b17023SJohn Marino 1057*e4b17023SJohn MarinoThe @code{state} and @code{extra} fields of the 1058*e4b17023SJohn Marino@code{NSFastEnumerationState} structure allows your collection object 1059*e4b17023SJohn Marinoto keep track of the state of the enumeration. In a simple array 1060*e4b17023SJohn Marinoimplementation, @code{state} may keep track of the index of the last 1061*e4b17023SJohn Marinoobject that was returned, and @code{extra} may be unused. 1062*e4b17023SJohn Marino 1063*e4b17023SJohn MarinoThe @code{mutationsPtr} field of the @code{NSFastEnumerationState} is 1064*e4b17023SJohn Marinoused to keep track of mutations. It should point to a number; before 1065*e4b17023SJohn Marinoworking on each object, the fast enumeration loop will check that this 1066*e4b17023SJohn Marinonumber has not changed. If it has, a mutation has happened and the 1067*e4b17023SJohn Marinofast enumeration will abort. So, @code{mutationsPtr} could be set to 1068*e4b17023SJohn Marinopoint to some sort of version number of your collection, which is 1069*e4b17023SJohn Marinoincreased by one every time there is a change (for example when an 1070*e4b17023SJohn Marinoobject is added or removed). Or, if you are content with less strict 1071*e4b17023SJohn Marinomutation checks, it could point to the number of objects in your 1072*e4b17023SJohn Marinocollection or some other value that can be checked to perform an 1073*e4b17023SJohn Marinoapproximate check that the collection has not been mutated. 1074*e4b17023SJohn Marino 1075*e4b17023SJohn MarinoFinally, note how we declared the @code{len} argument and the return 1076*e4b17023SJohn Marinovalue to be of type @code{unsigned long}. They could also be declared 1077*e4b17023SJohn Marinoto be of type @code{unsigned int} and everything would still work. 1078*e4b17023SJohn Marino 1079*e4b17023SJohn Marino@c ========================================================================= 1080*e4b17023SJohn Marino@node Messaging with the GNU Objective-C runtime 1081*e4b17023SJohn Marino@section Messaging with the GNU Objective-C runtime 1082*e4b17023SJohn Marino 1083*e4b17023SJohn MarinoThis section is specific for the GNU Objective-C runtime. If you are 1084*e4b17023SJohn Marinousing a different runtime, you can skip it. 1085*e4b17023SJohn Marino 1086*e4b17023SJohn MarinoThe implementation of messaging in the GNU Objective-C runtime is 1087*e4b17023SJohn Marinodesigned to be portable, and so is based on standard C. 1088*e4b17023SJohn Marino 1089*e4b17023SJohn MarinoSending a message in the GNU Objective-C runtime is composed of two 1090*e4b17023SJohn Marinoseparate steps. First, there is a call to the lookup function, 1091*e4b17023SJohn Marino@code{objc_msg_lookup ()} (or, in the case of messages to super, 1092*e4b17023SJohn Marino@code{objc_msg_lookup_super ()}). This runtime function takes as 1093*e4b17023SJohn Marinoargument the receiver and the selector of the method to be called; it 1094*e4b17023SJohn Marinoreturns the @code{IMP}, that is a pointer to the function implementing 1095*e4b17023SJohn Marinothe method. The second step of method invocation consists of casting 1096*e4b17023SJohn Marinothis pointer function to the appropriate function pointer type, and 1097*e4b17023SJohn Marinocalling the function pointed to it with the right arguments. 1098*e4b17023SJohn Marino 1099*e4b17023SJohn MarinoFor example, when the compiler encounters a method invocation such as 1100*e4b17023SJohn Marino@code{[object init]}, it compiles it into a call to 1101*e4b17023SJohn Marino@code{objc_msg_lookup (object, @@selector(init))} followed by a cast 1102*e4b17023SJohn Marinoof the returned value to the appropriate function pointer type, and 1103*e4b17023SJohn Marinothen it calls it. 1104*e4b17023SJohn Marino 1105*e4b17023SJohn Marino@menu 1106*e4b17023SJohn Marino* Dynamically registering methods:: 1107*e4b17023SJohn Marino* Forwarding hook:: 1108*e4b17023SJohn Marino@end menu 1109*e4b17023SJohn Marino 1110*e4b17023SJohn Marino@c ========================================================================= 1111*e4b17023SJohn Marino@node Dynamically registering methods 1112*e4b17023SJohn Marino@subsection Dynamically registering methods 1113*e4b17023SJohn Marino 1114*e4b17023SJohn MarinoIf @code{objc_msg_lookup()} does not find a suitable method 1115*e4b17023SJohn Marinoimplementation, because the receiver does not implement the required 1116*e4b17023SJohn Marinomethod, it tries to see if the class can dynamically register the 1117*e4b17023SJohn Marinomethod. 1118*e4b17023SJohn Marino 1119*e4b17023SJohn MarinoTo do so, the runtime checks if the class of the receiver implements 1120*e4b17023SJohn Marinothe method 1121*e4b17023SJohn Marino 1122*e4b17023SJohn Marino@smallexample 1123*e4b17023SJohn Marino+ (BOOL) resolveInstanceMethod: (SEL)selector; 1124*e4b17023SJohn Marino@end smallexample 1125*e4b17023SJohn Marino 1126*e4b17023SJohn Marinoin the case of an instance method, or 1127*e4b17023SJohn Marino 1128*e4b17023SJohn Marino@smallexample 1129*e4b17023SJohn Marino+ (BOOL) resolveClassMethod: (SEL)selector; 1130*e4b17023SJohn Marino@end smallexample 1131*e4b17023SJohn Marino 1132*e4b17023SJohn Marinoin the case of a class method. If the class implements it, the 1133*e4b17023SJohn Marinoruntime invokes it, passing as argument the selector of the original 1134*e4b17023SJohn Marinomethod, and if it returns @code{YES}, the runtime tries the lookup 1135*e4b17023SJohn Marinoagain, which could now succeed if a matching method was added 1136*e4b17023SJohn Marinodynamically by @code{+resolveInstanceMethod:} or 1137*e4b17023SJohn Marino@code{+resolveClassMethod:}. 1138*e4b17023SJohn Marino 1139*e4b17023SJohn MarinoThis allows classes to dynamically register methods (by adding them to 1140*e4b17023SJohn Marinothe class using @code{class_addMethod}) when they are first called. 1141*e4b17023SJohn MarinoTo do so, a class should implement @code{+resolveInstanceMethod:} (or, 1142*e4b17023SJohn Marinodepending on the case, @code{+resolveClassMethod:}) and have it 1143*e4b17023SJohn Marinorecognize the selectors of methods that can be registered dynamically 1144*e4b17023SJohn Marinoat runtime, register them, and return @code{YES}. It should return 1145*e4b17023SJohn Marino@code{NO} for methods that it does not dynamically registered at 1146*e4b17023SJohn Marinoruntime. 1147*e4b17023SJohn Marino 1148*e4b17023SJohn MarinoIf @code{+resolveInstanceMethod:} (or @code{+resolveClassMethod:}) is 1149*e4b17023SJohn Marinonot implemented or returns @code{NO}, the runtime then tries the 1150*e4b17023SJohn Marinoforwarding hook. 1151*e4b17023SJohn Marino 1152*e4b17023SJohn MarinoSupport for @code{+resolveInstanceMethod:} and 1153*e4b17023SJohn Marino@code{resolveClassMethod:} was added to the GNU Objective-C runtime in 1154*e4b17023SJohn MarinoGCC version 4.6. 1155*e4b17023SJohn Marino 1156*e4b17023SJohn Marino@c ========================================================================= 1157*e4b17023SJohn Marino@node Forwarding hook 1158*e4b17023SJohn Marino@subsection Forwarding hook 1159*e4b17023SJohn Marino 1160*e4b17023SJohn MarinoThe GNU Objective-C runtime provides a hook, called 1161*e4b17023SJohn Marino@code{__objc_msg_forward2}, which is called by 1162*e4b17023SJohn Marino@code{objc_msg_lookup()} when it can't find a method implementation in 1163*e4b17023SJohn Marinothe runtime tables and after calling @code{+resolveInstanceMethod:} 1164*e4b17023SJohn Marinoand @code{+resolveClassMethod:} has been attempted and did not succeed 1165*e4b17023SJohn Marinoin dynamically registering the method. 1166*e4b17023SJohn Marino 1167*e4b17023SJohn MarinoTo configure the hook, you set the global variable 1168*e4b17023SJohn Marino@code{__objc_msg_foward2} to a function with the same argument and 1169*e4b17023SJohn Marinoreturn types of @code{objc_msg_lookup()}. When 1170*e4b17023SJohn Marino@code{objc_msg_lookup()} can not find a method implementation, it 1171*e4b17023SJohn Marinoinvokes the hook function you provided to get a method implementation 1172*e4b17023SJohn Marinoto return. So, in practice @code{__objc_msg_forward2} allows you to 1173*e4b17023SJohn Marinoextend @code{objc_msg_lookup()} by adding some custom code that is 1174*e4b17023SJohn Marinocalled to do a further lookup when no standard method implementation 1175*e4b17023SJohn Marinocan be found using the normal lookup. 1176*e4b17023SJohn Marino 1177*e4b17023SJohn MarinoThis hook is generally reserved for ``Foundation'' libraries such as 1178*e4b17023SJohn MarinoGNUstep Base, which use it to implement their high-level method 1179*e4b17023SJohn Marinoforwarding API, typically based around the @code{forwardInvocation:} 1180*e4b17023SJohn Marinomethod. So, unless you are implementing your own ``Foundation'' 1181*e4b17023SJohn Marinolibrary, you should not set this hook. 1182*e4b17023SJohn Marino 1183*e4b17023SJohn MarinoIn a typical forwarding implementation, the @code{__objc_msg_forward2} 1184*e4b17023SJohn Marinohook function determines the argument and return type of the method 1185*e4b17023SJohn Marinothat is being looked up, and then creates a function that takes these 1186*e4b17023SJohn Marinoarguments and has that return type, and returns it to the caller. 1187*e4b17023SJohn MarinoCreating this function is non-trivial and is typically performed using 1188*e4b17023SJohn Marinoa dedicated library such as @code{libffi}. 1189*e4b17023SJohn Marino 1190*e4b17023SJohn MarinoThe forwarding method implementation thus created is returned by 1191*e4b17023SJohn Marino@code{objc_msg_lookup()} and is executed as if it was a normal method 1192*e4b17023SJohn Marinoimplementation. When the forwarding method implementation is called, 1193*e4b17023SJohn Marinoit is usually expected to pack all arguments into some sort of object 1194*e4b17023SJohn Marino(typically, an @code{NSInvocation} in a ``Foundation'' library), and 1195*e4b17023SJohn Marinohand it over to the programmer (@code{forwardInvocation:}) who is then 1196*e4b17023SJohn Marinoallowed to manipulate the method invocation using a high-level API 1197*e4b17023SJohn Marinoprovided by the ``Foundation'' library. For example, the programmer 1198*e4b17023SJohn Marinomay want to examine the method invocation arguments and name and 1199*e4b17023SJohn Marinopotentially change them before forwarding the method invocation to one 1200*e4b17023SJohn Marinoor more local objects (@code{performInvocation:}) or even to remote 1201*e4b17023SJohn Marinoobjects (by using Distributed Objects or some other mechanism). When 1202*e4b17023SJohn Marinoall this completes, the return value is passed back and must be 1203*e4b17023SJohn Marinoreturned correctly to the original caller. 1204*e4b17023SJohn Marino 1205*e4b17023SJohn MarinoNote that the GNU Objective-C runtime currently provides no support 1206*e4b17023SJohn Marinofor method forwarding or method invocations other than the 1207*e4b17023SJohn Marino@code{__objc_msg_forward2} hook. 1208*e4b17023SJohn Marino 1209*e4b17023SJohn MarinoIf the forwarding hook does not exist or returns @code{NULL}, the 1210*e4b17023SJohn Marinoruntime currently attempts forwarding using an older, deprecated API, 1211*e4b17023SJohn Marinoand if that fails, it aborts the program. In future versions of the 1212*e4b17023SJohn MarinoGNU Objective-C runtime, the runtime will immediately abort. 1213