objc.texi 15.2 KB
Newer Older
1
@c Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
2
@c 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 4 5
@c This is part of the GCC manual.
@c For copying conditions, see the file gcc.texi.

6
@node Objective-C
7 8
@comment  node-name,  next,  previous,  up

9 10 11
@chapter GNU Objective-C runtime features

This document is meant to describe some of the GNU Objective-C runtime
12
features.  It is not intended to teach you Objective-C, there are several
13 14
resources on the Internet that present the language.  Questions and
comments about this document to Ovidiu Predescu
15
@email{ovidiu@@cup.hp.com}.
16

17
@menu
18 19 20 21
* Executing code before main::
* Type encoding::
* Garbage Collection::
* Constant string objects::
22
* compatibility_alias::
23
@end menu
24

25
@node Executing code before main, Type encoding, Objective-C, Objective-C
26 27 28 29
@section @code{+load}: Executing code before main

The GNU Objective-C runtime provides a way that allows you to execute
code before the execution of the program enters the @code{main}
30
function.  The code is executed on a per-class and a per-category basis,
31 32 33 34
through a special class method @code{+load}.

This facility is very useful if you want to initialize global variables
which can be accessed by the program directly, without sending a message
35
to the class first.  The usual way to initialize global variables, in the
36 37 38 39 40 41 42 43
@code{+initialize} method, might not be useful because
@code{+initialize} is only called when the first message is sent to a
class object, which in some cases could be too late.

Suppose for example you have a @code{FileStream} class that declares
@code{Stdin}, @code{Stdout} and @code{Stderr} as global variables, like
below:

44
@smallexample
45 46 47 48 49 50 51 52

FileStream *Stdin = nil;
FileStream *Stdout = nil;
FileStream *Stderr = nil;

@@implementation FileStream

+ (void)initialize
53
@{
54 55
    Stdin = [[FileStream new] initWithFd:0];
    Stdout = [[FileStream new] initWithFd:1];
56 57
    Stderr = [[FileStream new] initWithFd:2];
@}
58

59
/* @r{Other methods here} */
60 61
@@end

62
@end smallexample
63 64

In this example, the initialization of @code{Stdin}, @code{Stdout} and
65
@code{Stderr} in @code{+initialize} occurs too late.  The programmer can
66
send a message to one of these objects before the variables are actually
67
initialized, thus sending messages to the @code{nil} object.  The
68 69
@code{+initialize} method which actually initializes the global
variables is not invoked until the first message is sent to the class
70
object.  The solution would require these variables to be initialized
71 72 73 74 75
just before entering @code{main}.

The correct solution of the above problem is to use the @code{+load}
method instead of @code{+initialize}:

76
@smallexample
77

78 79 80
@@implementation FileStream

+ (void)load
81 82 83 84 85
@{
    Stdin = [[FileStream new] initWithFd:0];
    Stdout = [[FileStream new] initWithFd:1];
    Stderr = [[FileStream new] initWithFd:2];
@}
86

87
/* @r{Other methods here} */
88 89
@@end

90
@end smallexample
91

92
The @code{+load} is a method that is not overridden by categories.  If a
93 94 95
class and a category of it both implement @code{+load}, both methods are
invoked.  This allows some additional initializations to be performed in
a category.
96

97 98 99 100 101
This mechanism is not intended to be a replacement for @code{+initialize}.
You should be aware of its limitations when you decide to use it
instead of @code{+initialize}.

@menu
102
* What you can and what you cannot do in +load::
103 104 105
@end menu


106
@node What you can and what you cannot do in +load,  , Executing code before main, Executing code before main
107 108
@subsection What you can and what you cannot do in @code{+load}

109
The @code{+load} implementation in the GNU runtime guarantees you the following
110 111 112 113 114 115 116 117
things:

@itemize @bullet

@item
you can write whatever C code you like;

@item
118 119
you can send messages to Objective-C constant strings (@code{@@"this is a
constant string"});
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

@item
you can allocate and send messages to objects whose class is implemented
in the same file;

@item
the @code{+load} implementation of all super classes of a class are executed before the @code{+load} of that class is executed;

@item
the @code{+load} implementation of a class is executed before the
@code{+load} implementation of any category.

@end itemize

In particular, the following things, even if they can work in a
particular case, are not guaranteed:

@itemize @bullet

@item
allocation of or sending messages to arbitrary objects;

@item
allocation of or sending messages to objects whose classes have a
category implemented in the same file;

@end itemize

You should make no assumptions about receiving @code{+load} in sibling
149
classes when you write @code{+load} of a class.  The order in which
150
sibling classes receive @code{+load} is not guaranteed.
151

152
The order in which @code{+load} and @code{+initialize} are called could
153
be problematic if this matters.  If you don't allocate objects inside
154
@code{+load}, it is guaranteed that @code{+load} is called before
155
@code{+initialize}.  If you create an object inside @code{+load} the
156
@code{+initialize} method of object's class is invoked even if
157 158
@code{+load} was not invoked.  Note if you explicitly call @code{+load}
on a class, @code{+initialize} will be called first.  To avoid possible
159 160 161
problems try to implement only one of these methods.

The @code{+load} method is also invoked when a bundle is dynamically
162 163
loaded into your running program.  This happens automatically without any
intervening operation from you.  When you write bundles and you need to
164
write @code{+load} you can safely create and send messages to objects whose
165
classes already exist in the running program.  The same restrictions as
166 167 168 169
above apply to classes defined in bundle.



170
@node Type encoding, Garbage Collection, Executing code before main, Objective-C
171 172 173
@section Type encoding

The Objective-C compiler generates type encodings for all the
174
types.  These type encodings are used at runtime to find out information
175 176 177 178 179 180 181
about selectors and methods and about objects and classes.

The types are encoded in the following way:

@c @sp 1

@multitable @columnfractions .25 .75
182 183
@item @code{_Bool}
@tab @code{B}
184
@item @code{char}
185
@tab @code{c}
186
@item @code{unsigned char}
187
@tab @code{C}
188
@item @code{short}
189
@tab @code{s}
190
@item @code{unsigned short}
191
@tab @code{S}
192
@item @code{int}
193
@tab @code{i}
194
@item @code{unsigned int}
195
@tab @code{I}
196
@item @code{long}
197
@tab @code{l}
198
@item @code{unsigned long}
199
@tab @code{L}
200
@item @code{long long}
201
@tab @code{q}
202
@item @code{unsigned long long}
203
@tab @code{Q}
204
@item @code{float}
205
@tab @code{f}
206
@item @code{double}
207
@tab @code{d}
208
@item @code{void}
209
@tab @code{v}
210
@item @code{id}
211
@tab @code{@@}
212
@item @code{Class}
213
@tab @code{#}
214
@item @code{SEL}
215
@tab @code{:}
216
@item @code{char*}
217
@tab @code{*}
218
@item unknown type
219
@tab @code{?}
220
@item Complex types
221
@tab @code{j} followed by the inner type.  For example @code{_Complex double} is encoded as "jd".
222 223
@item bit-fields
@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)
224 225 226 227
@end multitable

@c @sp 1

228
The encoding of bit-fields has changed to allow bit-fields to be properly
229
handled by the runtime functions that compute sizes and alignments of
230 231 232
types that contain bit-fields.  The previous encoding contained only the
size of the bit-field.  Using only this information it is not possible to
reliably compute the size occupied by the bit-field.  This is very
233 234
important in the presence of the Boehm's garbage collector because the
objects are allocated using the typed memory facility available in this
235
collector.  The typed memory allocation requires information about where
236 237
the pointers are located inside the object.

238
The position in the bit-field is the position, counting in bits, of the
239 240 241 242 243 244 245
bit closest to the beginning of the structure.

The non-atomic types are encoded as follows:

@c @sp 1

@multitable @columnfractions .2 .8
246
@item pointers
247
@tab @samp{^} followed by the pointed type.
248
@item arrays
249
@tab @samp{[} followed by the number of elements in the array followed by the type of the elements followed by @samp{]}
250
@item structures
251
@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{@}}
252
@item unions
253
@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{)}
254 255 256
@end multitable

Here are some types and their encodings, as they are generated by the
257
compiler on an i386 machine:
258 259 260 261 262 263 264

@sp 1

@multitable @columnfractions .25 .75
@item Objective-C type
@tab Compiler encoding
@item
265
@smallexample
266
int a[10];
267
@end smallexample
268 269
@tab @code{[10i]}
@item
270
@smallexample
271 272 273 274 275 276 277
struct @{
  int i;
  float f[3];
  int a:3;
  int b:2;
  char c;
@}
278
@end smallexample
279 280 281 282 283 284
@tab @code{@{?=i[3f]b128i3b131i2c@}}
@end multitable

@sp 1

In addition to the types the compiler also encodes the type
285
specifiers.  The table below describes the encoding of the current
286 287 288 289 290 291 292
Objective-C type specifiers:

@sp 1

@multitable @columnfractions .25 .75
@item Specifier
@tab Encoding
293
@item @code{const}
294
@tab @code{r}
295
@item @code{in}
296
@tab @code{n}
297
@item @code{inout}
298
@tab @code{N}
299
@item @code{out}
300
@tab @code{o}
301
@item @code{bycopy}
302
@tab @code{O}
303
@item @code{oneway}
304 305 306 307 308
@tab @code{V}
@end multitable

@sp 1

309
The type specifiers are encoded just before the type.  Unlike types
310 311 312 313
however, the type specifiers are only encoded when they appear in method
argument types.


314
@node Garbage Collection, Constant string objects, Type encoding, Objective-C
315 316 317 318
@section Garbage Collection

Support for a new memory management policy has been added by using a
powerful conservative garbage collector, known as the
319
Boehm-Demers-Weiser conservative garbage collector.  It is available from
320
@w{@uref{http://www.hpl.hp.com/personal/Hans_Boehm/gc/}}.
321 322

To enable the support for it you have to configure the compiler using an
323 324
additional argument, @w{@option{--enable-objc-gc}}.  You need to have
garbage collector installed before building the compiler.  This will
325
build an additional runtime library which has several enhancements to
326
support the garbage collector.  The new library has a new name,
327
@file{libobjc_gc.a} to not conflict with the non-garbage-collected
328 329 330 331
library.

When the garbage collector is used, the objects are allocated using the
so-called typed memory allocation mechanism available in the
332 333
Boehm-Demers-Weiser collector.  This mode requires precise information on
where pointers are located inside objects.  This information is computed
334 335 336
once per class, immediately after the class has been initialized.

There is a new runtime function @code{class_ivar_set_gcinvisible()}
337
which can be used to declare a so-called @dfn{weak pointer}
338
reference.  Such a pointer is basically hidden for the garbage collector;
339 340
this can be useful in certain situations, especially when you want to
keep track of the allocated objects, yet allow them to be
341 342
collected.  This kind of pointers can only be members of objects, you
cannot declare a global pointer as a weak reference.  Every type which is
343 344 345
a pointer type can be declared a weak pointer, including @code{id},
@code{Class} and @code{SEL}.

346
Here is an example of how to use this feature.  Suppose you want to
347 348 349
implement a class whose instances hold a weak pointer reference; the
following class does this:

350
@smallexample
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

@@interface WeakPointer : Object
@{
    const void* weakPointer;
@}

- initWithPointer:(const void*)p;
- (const void*)weakPointer;
@@end


@@implementation WeakPointer

+ (void)initialize
@{
  class_ivar_set_gcinvisible (self, "weakPointer", YES);
@}

- initWithPointer:(const void*)p
@{
  weakPointer = p;
  return self;
@}

- (const void*)weakPointer
@{
  return weakPointer;
@}

@@end

382
@end smallexample
383 384

Weak pointers are supported through a new type character specifier
385
represented by the @samp{!} character.  The
386 387 388 389
@code{class_ivar_set_gcinvisible()} function adds or removes this
specifier to the string type description of the instance variable named
as argument.

390
@c =========================================================================
Mark Mitchell committed
391
@node Constant string objects
392 393 394
@section Constant string objects

GNU Objective-C provides constant string objects that are generated
395
directly by the compiler.  You declare a constant string object by
396
prefixing a C constant string with the character @samp{@@}:
397

398
@smallexample
399
  id myString = @@"this is a constant string object";
400
@end smallexample
401

402
The constant string objects are by default instances of the
403
@code{NXConstantString} class which is provided by the GNU Objective-C
404
runtime.  To get the definition of this class you must include the
405 406 407
@file{objc/NXConstStr.h} header file.

User defined libraries may want to implement their own constant string
408
class.  To be able to support them, the GNU Objective-C compiler provides
409 410
a new command line options @option{-fconstant-string-class=@var{class-name}}.
The provided class should adhere to a strict structure, the same
411 412
as @code{NXConstantString}'s structure:

413
@smallexample
414

415
@@interface MyConstantStringClass
416
@{
417
  Class isa;
418 419 420 421 422
  char *c_string;
  unsigned int len;
@}
@@end

423
@end smallexample
424

425 426 427 428
@code{NXConstantString} inherits from @code{Object}; user class
libraries may choose to inherit the customized constant string class
from a different class than @code{Object}.  There is no requirement in
the methods the constant string class has to implement, but the final
429
ivar layout of the class must be the compatible with the given
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
structure.

When the compiler creates the statically allocated constant string
object, the @code{c_string} field will be filled by the compiler with
the string; the @code{length} field will be filled by the compiler with
the string length; the @code{isa} pointer will be filled with
@code{NULL} by the compiler, and it will later be fixed up automatically
at runtime by the GNU Objective-C runtime library to point to the class
which was set by the @option{-fconstant-string-class} option when the
object file is loaded (if you wonder how it works behind the scenes, the
name of the class to use, and the list of static objects to fixup, are
stored by the compiler in the object file in a place where the GNU
runtime library will find them at runtime).

As a result, when a file is compiled with the
@option{-fconstant-string-class} option, all the constant string objects
will be instances of the class specified as argument to this option.  It
is possible to have multiple compilation units referring to different
constant string classes, neither the compiler nor the linker impose any
restrictions in doing this.
450

451
@c =========================================================================
Mark Mitchell committed
452
@node compatibility_alias
453 454 455 456 457 458
@section compatibility_alias

This is a feature of the Objective-C compiler rather than of the
runtime, anyway since it is documented nowhere and its existence was
forgotten, we are documenting it here.

459 460
The keyword @code{@@compatibility_alias} allows you to define a class name
as equivalent to another class name.  For example:
461

462
@smallexample
463
@@compatibility_alias WOApplication GSWApplication;
464
@end smallexample
465

466 467
tells the compiler that each time it encounters @code{WOApplication} as
a class name, it should replace it with @code{GSWApplication} (that is,
468 469
@code{WOApplication} is just an alias for @code{GSWApplication}).

470
There are some constraints on how this can be used---
471

472
@itemize @bullet
473 474 475 476 477 478

@item @code{WOApplication} (the alias) must not be an existing class;

@item @code{GSWApplication} (the real class) must be an existing class.

@end itemize