OSDN Git Service

* doc/include/gcc-common.texi (version-GCC): Increase to 3.3.
[pf3gnuchains/gcc-fork.git] / gcc / doc / objc.texi
1 @c Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
2 @c 1999, 2000, 2001 Free Software Foundation, Inc.
3 @c This is part of the GCC manual.
4 @c For copying conditions, see the file gcc.texi.
5
6 @node Objective-C
7 @comment  node-name,  next,  previous,  up
8
9 @chapter GNU Objective-C runtime features
10
11 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 resources on the Internet that present the language.  Questions and
14 comments about this document to Ovidiu Predescu
15 @email{ovidiu@@cup.hp.com}.
16
17 @menu
18 * Executing code before main::
19 * Type encoding::
20 * Garbage Collection::
21 * Constant string objects::
22 * compatibility_alias::
23 @end menu
24
25 @node Executing code before main, Type encoding, Objective-C, Objective-C
26 @section @code{+load}: Executing code before main
27
28
29 The GNU Objective-C runtime provides a way that allows you to execute
30 code before the execution of the program enters the @code{main}
31 function.  The code is executed on a per-class and a per-category basis,
32 through a special class method @code{+load}.
33
34 This facility is very useful if you want to initialize global variables
35 which can be accessed by the program directly, without sending a message
36 to the class first.  The usual way to initialize global variables, in the
37 @code{+initialize} method, might not be useful because
38 @code{+initialize} is only called when the first message is sent to a
39 class object, which in some cases could be too late.
40
41 Suppose for example you have a @code{FileStream} class that declares
42 @code{Stdin}, @code{Stdout} and @code{Stderr} as global variables, like
43 below:
44
45 @example
46
47 FileStream *Stdin = nil;
48 FileStream *Stdout = nil;
49 FileStream *Stderr = nil;
50
51 @@implementation FileStream
52
53 + (void)initialize
54 @{
55     Stdin = [[FileStream new] initWithFd:0];
56     Stdout = [[FileStream new] initWithFd:1];
57     Stderr = [[FileStream new] initWithFd:2];
58 @}
59
60 /* Other methods here */
61 @@end
62
63 @end example
64
65 In this example, the initialization of @code{Stdin}, @code{Stdout} and
66 @code{Stderr} in @code{+initialize} occurs too late.  The programmer can
67 send a message to one of these objects before the variables are actually
68 initialized, thus sending messages to the @code{nil} object.  The
69 @code{+initialize} method which actually initializes the global
70 variables is not invoked until the first message is sent to the class
71 object.  The solution would require these variables to be initialized
72 just before entering @code{main}.
73
74 The correct solution of the above problem is to use the @code{+load}
75 method instead of @code{+initialize}:
76
77 @example
78
79 @@implementation FileStream
80
81 + (void)load
82 @{
83     Stdin = [[FileStream new] initWithFd:0];
84     Stdout = [[FileStream new] initWithFd:1];
85     Stderr = [[FileStream new] initWithFd:2];
86 @}
87
88 /* Other methods here */
89 @@end
90
91 @end example
92
93 The @code{+load} is a method that is not overridden by categories.  If a
94 class and a category of it both implement @code{+load}, both methods are
95 invoked.  This allows some additional initializations to be performed in
96 a category.
97
98 This mechanism is not intended to be a replacement for @code{+initialize}.
99 You should be aware of its limitations when you decide to use it
100 instead of @code{+initialize}.
101
102 @menu
103 * What you can and what you cannot do in +load::
104 @end menu
105
106
107 @node What you can and what you cannot do in +load,  , Executing code before main, Executing code before main
108 @subsection What you can and what you cannot do in @code{+load}
109
110 The @code{+load} implementation in the GNU runtime guarantees you the following
111 things:
112
113 @itemize @bullet
114
115 @item
116 you can write whatever C code you like;
117
118 @item
119 you can send messages to Objective-C constant strings (@code{@@"this is a
120 constant string"});
121
122 @item
123 you can allocate and send messages to objects whose class is implemented
124 in the same file;
125
126 @item
127 the @code{+load} implementation of all super classes of a class are executed before the @code{+load} of that class is executed;
128
129 @item
130 the @code{+load} implementation of a class is executed before the
131 @code{+load} implementation of any category.
132
133 @end itemize
134
135 In particular, the following things, even if they can work in a
136 particular case, are not guaranteed:
137
138 @itemize @bullet
139
140 @item
141 allocation of or sending messages to arbitrary objects;
142
143 @item
144 allocation of or sending messages to objects whose classes have a
145 category implemented in the same file;
146
147 @end itemize
148
149 You should make no assumptions about receiving @code{+load} in sibling
150 classes when you write @code{+load} of a class.  The order in which
151 sibling classes receive @code{+load} is not guaranteed.
152
153 The order in which @code{+load} and @code{+initialize} are called could
154 be problematic if this matters.  If you don't allocate objects inside
155 @code{+load}, it is guaranteed that @code{+load} is called before
156 @code{+initialize}.  If you create an object inside @code{+load} the
157 @code{+initialize} method of object's class is invoked even if
158 @code{+load} was not invoked.  Note if you explicitly call @code{+load}
159 on a class, @code{+initialize} will be called first.  To avoid possible
160 problems try to implement only one of these methods.
161
162 The @code{+load} method is also invoked when a bundle is dynamically
163 loaded into your running program.  This happens automatically without any
164 intervening operation from you.  When you write bundles and you need to
165 write @code{+load} you can safely create and send messages to objects whose
166 classes already exist in the running program.  The same restrictions as
167 above apply to classes defined in bundle.
168
169
170
171 @node Type encoding, Garbage Collection, Executing code before main, Objective-C
172 @section Type encoding
173
174 The Objective-C compiler generates type encodings for all the
175 types.  These type encodings are used at runtime to find out information
176 about selectors and methods and about objects and classes.
177
178 The types are encoded in the following way:
179
180 @c @sp 1
181
182 @multitable @columnfractions .25 .75
183 @item @code{char}
184 @tab @code{c}
185 @item @code{unsigned char}
186 @tab @code{C}
187 @item @code{short}
188 @tab @code{s}
189 @item @code{unsigned short}
190 @tab @code{S}
191 @item @code{int}
192 @tab @code{i}
193 @item @code{unsigned int}
194 @tab @code{I}
195 @item @code{long}
196 @tab @code{l}
197 @item @code{unsigned long}
198 @tab @code{L}
199 @item @code{long long}
200 @tab @code{q}
201 @item @code{unsigned long long}
202 @tab @code{Q}
203 @item @code{float}
204 @tab @code{f}
205 @item @code{double}
206 @tab @code{d}
207 @item @code{void}
208 @tab @code{v}
209 @item @code{id}
210 @tab @code{@@}
211 @item @code{Class}
212 @tab @code{#}
213 @item @code{SEL}
214 @tab @code{:}
215 @item @code{char*}
216 @tab @code{*}
217 @item unknown type
218 @tab @code{?}
219 @item bit-fields
220 @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)
221 @end multitable
222
223 @c @sp 1
224
225 The encoding of bit-fields has changed to allow bit-fields to be properly
226 handled by the runtime functions that compute sizes and alignments of
227 types that contain bit-fields.  The previous encoding contained only the
228 size of the bit-field.  Using only this information it is not possible to
229 reliably compute the size occupied by the bit-field.  This is very
230 important in the presence of the Boehm's garbage collector because the
231 objects are allocated using the typed memory facility available in this
232 collector.  The typed memory allocation requires information about where
233 the pointers are located inside the object.
234
235 The position in the bit-field is the position, counting in bits, of the
236 bit closest to the beginning of the structure.
237
238 The non-atomic types are encoded as follows:
239
240 @c @sp 1
241
242 @multitable @columnfractions .2 .8
243 @item pointers
244 @tab @samp{^} followed by the pointed type.
245 @item arrays
246 @tab @samp{[} followed by the number of elements in the array followed by the type of the elements followed by @samp{]}
247 @item structures
248 @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{@}}
249 @item unions
250 @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{)}
251 @end multitable
252
253 Here are some types and their encodings, as they are generated by the
254 compiler on an i386 machine:
255
256 @sp 1
257
258 @multitable @columnfractions .25 .75
259 @item Objective-C type
260 @tab Compiler encoding
261 @item
262 @example
263 int a[10];
264 @end example
265 @tab @code{[10i]}
266 @item
267 @example
268 struct @{
269   int i;
270   float f[3];
271   int a:3;
272   int b:2;
273   char c;
274 @}
275 @end example
276 @tab @code{@{?=i[3f]b128i3b131i2c@}}
277 @end multitable
278
279 @sp 1
280
281 In addition to the types the compiler also encodes the type
282 specifiers.  The table below describes the encoding of the current
283 Objective-C type specifiers:
284
285 @sp 1
286
287 @multitable @columnfractions .25 .75
288 @item Specifier
289 @tab Encoding
290 @item @code{const}
291 @tab @code{r}
292 @item @code{in}
293 @tab @code{n}
294 @item @code{inout}
295 @tab @code{N}
296 @item @code{out}
297 @tab @code{o}
298 @item @code{bycopy}
299 @tab @code{O}
300 @item @code{oneway}
301 @tab @code{V}
302 @end multitable
303
304 @sp 1
305
306 The type specifiers are encoded just before the type.  Unlike types
307 however, the type specifiers are only encoded when they appear in method
308 argument types.
309
310
311 @node Garbage Collection, Constant string objects, Type encoding, Objective-C
312 @section Garbage Collection
313
314 Support for a new memory management policy has been added by using a
315 powerful conservative garbage collector, known as the
316 Boehm-Demers-Weiser conservative garbage collector.  It is available from
317 @w{@uref{http://www.hpl.hp.com/personal/Hans_Boehm/gc/}}.
318
319 To enable the support for it you have to configure the compiler using an
320 additional argument, @w{@option{--enable-objc-gc}}.  You need to have
321 garbage collector installed before building the compiler.  This will
322 build an additional runtime library which has several enhancements to
323 support the garbage collector.  The new library has a new name,
324 @file{libobjc_gc.a} to not conflict with the non-garbage-collected
325 library.
326
327 When the garbage collector is used, the objects are allocated using the
328 so-called typed memory allocation mechanism available in the
329 Boehm-Demers-Weiser collector.  This mode requires precise information on
330 where pointers are located inside objects.  This information is computed
331 once per class, immediately after the class has been initialized.
332
333 There is a new runtime function @code{class_ivar_set_gcinvisible()}
334 which can be used to declare a so-called @dfn{weak pointer}
335 reference.  Such a pointer is basically hidden for the garbage collector;
336 this can be useful in certain situations, especially when you want to
337 keep track of the allocated objects, yet allow them to be
338 collected.  This kind of pointers can only be members of objects, you
339 cannot declare a global pointer as a weak reference.  Every type which is
340 a pointer type can be declared a weak pointer, including @code{id},
341 @code{Class} and @code{SEL}.
342
343 Here is an example of how to use this feature.  Suppose you want to
344 implement a class whose instances hold a weak pointer reference; the
345 following class does this:
346
347 @example
348
349 @@interface WeakPointer : Object
350 @{
351     const void* weakPointer;
352 @}
353
354 - initWithPointer:(const void*)p;
355 - (const void*)weakPointer;
356 @@end
357
358
359 @@implementation WeakPointer
360
361 + (void)initialize
362 @{
363   class_ivar_set_gcinvisible (self, "weakPointer", YES);
364 @}
365
366 - initWithPointer:(const void*)p
367 @{
368   weakPointer = p;
369   return self;
370 @}
371
372 - (const void*)weakPointer
373 @{
374   return weakPointer;
375 @}
376
377 @@end
378
379 @end example
380
381 Weak pointers are supported through a new type character specifier
382 represented by the @samp{!} character.  The
383 @code{class_ivar_set_gcinvisible()} function adds or removes this
384 specifier to the string type description of the instance variable named
385 as argument.
386
387 @c =========================================================================
388 @node Constant string objects
389 @section Constant string objects
390
391 GNU Objective-C provides constant string objects that are generated
392 directly by the compiler.  You declare a constant string object by
393 prefixing a C constant string with the character @samp{@@}:
394
395 @example
396   id myString = @@"this is a constant string object";
397 @end example
398
399 The constant string objects are usually instances of the
400 @code{NXConstantString} class which is provided by the GNU Objective-C
401 runtime.  To get the definition of this class you must include the
402 @file{objc/NXConstStr.h} header file.
403
404 User defined libraries may want to implement their own constant string
405 class.  To be able to support them, the GNU Objective-C compiler provides
406 a new command line options @option{-fconstant-string-class=@var{class-name}}.
407 The provided class should adhere to a strict structure, the same
408 as @code{NXConstantString}'s structure:
409
410 @example
411
412 @@interface NXConstantString : Object
413 @{
414   char *c_string;
415   unsigned int len;
416 @}
417 @@end
418
419 @end example
420
421 User class libraries may choose to inherit the customized constant
422 string class from a different class than @code{Object}.  There is no
423 requirement in the methods the constant string class has to implement.
424
425 When a file is compiled with the @option{-fconstant-string-class} option,
426 all the constant string objects will be instances of the class specified
427 as argument to this option.  It is possible to have multiple compilation
428 units referring to different constant string classes, neither the
429 compiler nor the linker impose any restrictions in doing this.
430
431 @c =========================================================================
432 @node compatibility_alias
433 @section compatibility_alias
434
435 This is a feature of the Objective-C compiler rather than of the
436 runtime, anyway since it is documented nowhere and its existence was
437 forgotten, we are documenting it here.
438
439 The keyword @code{@@compatibility_alias} allows you to define a class name
440 as equivalent to another class name.  For example:
441
442 @example
443 @@compatibility_alias WOApplication GSWApplication;
444 @end example
445
446 tells the compiler that each time it encounters @code{WOApplication} as
447 a class name, it should replace it with @code{GSWApplication} (that is,
448 @code{WOApplication} is just an alias for @code{GSWApplication}).
449
450 There are some constraints on how this can be used---
451
452 @itemize @bullet
453
454 @item @code{WOApplication} (the alias) must not be an existing class;
455
456 @item @code{GSWApplication} (the real class) must be an existing class.
457
458 @end itemize