objc.texi 14.9 KB
Newer Older
jsm28's avatar
gcc:  
jsm28 committed
1 2 3 4 5 6
@c Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
@c 1999, 2000, 2001 Free Software Foundation, Inc.
@c This is part of the GCC manual.
@c For copying conditions, see the file gcc.texi.

@node Objective C
law's avatar
 
law committed
7 8
@comment  node-name,  next,  previous,  up

9 10 11 12 13 14
@chapter GNU Objective-C runtime features

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

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

jsm28's avatar
gcc:  
jsm28 committed
25
@node Executing code before main, Type encoding, Objective C, Objective C
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
@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}
function. The code is executed on a per-class and a per-category basis,
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
to the class first. The usual way to initialize global variables, in the
@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:

@example
            
FileStream *Stdin = nil;                                              
FileStream *Stdout = nil;                                          
FileStream *Stderr = nil;                                                
            
@@implementation FileStream                                               
          
+ (void)initialize                                                 
@{
    Stdin = [[FileStream new] initWithFd:0];                           
    Stdout = [[FileStream new] initWithFd:1];                           
    Stderr = [[FileStream new] initWithFd:2];
@}
 
/* Other methods here */
@@end

@end example

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

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

@example

@@implementation FileStream                                             
 
+ (void)load                                 
@{
    Stdin = [[FileStream new] initWithFd:0];
    Stdout = [[FileStream new] initWithFd:1];
    Stderr = [[FileStream new] initWithFd:2];
@}
 
/* Other methods here */                                               
@@end

@end example
 
The @code{+load} is a method that is not overridden by categories. If a
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.
   
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
103
* What you can and what you cannot do in +load::  
104 105 106
@end menu


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

110
The @code{+load} implementation in the GNU runtime guarantees you the following
111 112 113 114 115 116 117 118 119 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
things:

@itemize @bullet

@item
you can write whatever C code you like;

@item
you can send messages to Objective-C constant strings (@@"this is a
constant string");

@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
classes when you write @code{+load} of a class. The order in which
sibling classes receive @code{+load} is not guaranteed.
    
The order in which @code{+load} and @code{+initialize} are called could
be problematic if this matters. If you don't allocate objects inside
@code{+load}, it is guaranteed that @code{+load} is called before
@code{+initialize}. If you create an object inside @code{+load} the
@code{+initialize} method of object's class is invoked even if
@code{+load} was not invoked. Note if you explicitly call @code{+load}
on a class, @code{+initialize} will be called first. To avoid possible
problems try to implement only one of these methods.

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



jsm28's avatar
gcc:  
jsm28 committed
171
@node Type encoding, Garbage Collection, Executing code before main, Objective C
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
@section Type encoding

The Objective-C compiler generates type encodings for all the
types. These type encodings are used at runtime to find out information
about selectors and methods and about objects and classes.

The types are encoded in the following way:

@c @sp 1

@multitable @columnfractions .25 .75
@item @code{char}                      
@tab @code{c}
@item @code{unsigned char}             
@tab @code{C}
@item @code{short}                     
@tab @code{s}
@item @code{unsigned short}            
@tab @code{S}
@item @code{int}                       
@tab @code{i}
@item @code{unsigned int}              
@tab @code{I}
@item @code{long}                      
@tab @code{l}
@item @code{unsigned long}             
@tab @code{L}
@item @code{long long}                 
@tab @code{q}
@item @code{unsigned long long}        
@tab @code{Q}
@item @code{float}                     
@tab @code{f}
@item @code{double}                    
@tab @code{d}
@item @code{void}                      
@tab @code{v}
@item @code{id}                        
@tab @code{@@}
@item @code{Class}                     
@tab @code{#}
@item @code{SEL}                       
@tab @code{:}
@item @code{char*}                     
@tab @code{*}
@item unknown type                     
@tab @code{?}
@item bitfields                 
@tab @code{b} followed by the starting position of the bitfield, the type of the bitfield and the size of the bitfield (the bitfields encoding was changed from the NeXT's compiler encoding, see below)
@end multitable

@c @sp 1

The encoding of bitfields has changed to allow bitfields to be properly
handled by the runtime functions that compute sizes and alignments of
types that contain bitfields. The previous encoding contained only the
size of the bitfield. Using only this information it is not possible to
reliably compute the size occupied by the bitfield. This is very
important in the presence of the Boehm's garbage collector because the
objects are allocated using the typed memory facility available in this
collector. The typed memory allocation requires information about where
the pointers are located inside the object.

The position in the bitfield is the position, counting in bits, of the
bit closest to the beginning of the structure.

The non-atomic types are encoded as follows:

@c @sp 1

@multitable @columnfractions .2 .8
@item pointers          
@tab @code{'^'} followed by the pointed type.
@item arrays
@tab @code{'['} followed by the number of elements in the array followed by the type of the elements followed by @code{']'}
@item structures
@tab @code{'@{'} followed by the name of the structure (or '?' if the structure is unnamed), the '=' sign, the type of the members and by @code{'@}'}
@item unions
@tab @code{'('} followed by the name of the structure (or '?' if the union is unnamed), the '=' sign, the type of the members followed by @code{')'}
@end multitable

Here are some types and their encodings, as they are generated by the
compiler on a i386 machine:

@sp 1

@multitable @columnfractions .25 .75
@item Objective-C type
@tab Compiler encoding
@item
@example
int a[10];
@end example
@tab @code{[10i]}
@item
@example
struct @{
  int i;
  float f[3];
  int a:3;
  int b:2;
  char c;
@}
@end example
@tab @code{@{?=i[3f]b128i3b131i2c@}}
@end multitable

@sp 1

In addition to the types the compiler also encodes the type
specifiers. The table below describes the encoding of the current
Objective-C type specifiers:

@sp 1

@multitable @columnfractions .25 .75
@item Specifier
@tab Encoding
@item @code{const}              
@tab @code{r}
@item @code{in}                 
@tab @code{n}
@item @code{inout}              
@tab @code{N}
@item @code{out}                
@tab @code{o}
@item @code{bycopy}             
@tab @code{O}
@item @code{oneway}             
@tab @code{V}
@end multitable

@sp 1

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


jsm28's avatar
gcc:  
jsm28 committed
311
@node Garbage Collection, Constant string objects, Type encoding, Objective C
312 313 314 315 316
@section Garbage Collection

Support for a new memory management policy has been added by using a
powerful conservative garbage collector, known as the
Boehm-Demers-Weiser conservative garbage collector. It is available from
317
@w{@uref{http://www.hpl.hp.com/personal/Hans_Boehm/gc/}}.
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 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 382 383 384 385 386

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

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

There is a new runtime function @code{class_ivar_set_gcinvisible()}
which can be used to declare a so-called @strong{weak pointer}
reference. Such a pointer is basically hidden for the garbage collector;
this can be useful in certain situations, especially when you want to
keep track of the allocated objects, yet allow them to be
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
a pointer type can be declared a weak pointer, including @code{id},
@code{Class} and @code{SEL}.

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

@example

@@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

@end example

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

387
@c =========================================================================
mmitchel's avatar
mmitchel committed
388
@node Constant string objects
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
@section Constant string objects

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

@example
  id myString = @@"this is a constant string object";
@end example

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

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

@example

@@interface NXConstantString : Object
@{
  char *c_string;
  unsigned int len;
@}
@@end

@end example

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.

When a file is compiled with the @code{-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.
430

431
@c =========================================================================
mmitchel's avatar
mmitchel committed
432
@node compatibility_alias
433 434 435 436 437 438 439 440 441 442 443 444
@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.

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

@example
@@compatibility_alias WOApplication GSWApplication;
@end example
445

446 447 448 449 450 451 452 453 454 455 456 457 458
tells the compiler that each time it encounters @code{WOApplication} as 
a class name, it should replace it with @code{GSWApplication} (that is, 
@code{WOApplication} is just an alias for @code{GSWApplication}).

There are some constraints on how this can be used - 

@itemize @bullet 

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

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

@end itemize