2001-05-09 10:16:47 -04:00
@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.
2001-06-21 11:25:56 -04:00
@node Objective-C
2000-08-11 17:25:29 -04:00
@comment node-name, next, previous, up
1998-09-17 04:04:30 -04:00
@chapter GNU Objective-C runtime features
This document is meant to describe some of the GNU Objective-C runtime
2001-06-26 18:47:11 -04:00
features. It is not intended to teach you Objective-C, there are several
1998-09-17 04:04:30 -04:00
resources on the Internet that present the language. Questions and
comments about this document to Ovidiu Predescu
2001-01-12 12:06:15 -05:00
@email{ovidiu@@cup.hp.com}.
1998-09-17 04:04:30 -04:00
2000-08-14 10:06:53 -04:00
@menu
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
* Executing code before main::
* Type encoding::
* Garbage Collection::
* Constant string objects::
2001-02-28 12:16:17 -05:00
* compatibility_alias::
2000-08-14 10:06:53 -04:00
@end menu
1998-09-17 04:04:30 -04:00
2001-06-21 11:25:56 -04:00
@node Executing code before main, Type encoding, Objective-C, Objective-C
1998-09-17 04:04:30 -04:00
@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}
2001-06-26 18:47:11 -04:00
function. The code is executed on a per-class and a per-category basis,
1998-09-17 04:04:30 -04:00
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
2001-06-26 18:47:11 -04:00
to the class first. The usual way to initialize global variables, in the
1998-09-17 04:04:30 -04:00
@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
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
FileStream *Stdin = nil;
FileStream *Stdout = nil;
FileStream *Stderr = nil;
@@implementation FileStream
+ (void)initialize
1998-09-17 04:04:30 -04:00
@{
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
Stdin = [[FileStream new] initWithFd:0];
Stdout = [[FileStream new] initWithFd:1];
1998-09-17 04:04:30 -04:00
Stderr = [[FileStream new] initWithFd:2];
@}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
1998-09-17 04:04:30 -04:00
/* Other methods here */
@@end
@end example
In this example, the initialization of @code{Stdin}, @code{Stdout} and
2001-06-26 18:47:11 -04:00
@code{Stderr} in @code{+initialize} occurs too late. The programmer can
1998-09-17 04:04:30 -04:00
send a message to one of these objects before the variables are actually
2001-06-26 18:47:11 -04:00
initialized, thus sending messages to the @code{nil} object. The
1998-09-17 04:04:30 -04:00
@code{+initialize} method which actually initializes the global
variables is not invoked until the first message is sent to the class
2001-06-26 18:47:11 -04:00
object. The solution would require these variables to be initialized
1998-09-17 04:04:30 -04:00
just before entering @code{main}.
The correct solution of the above problem is to use the @code{+load}
method instead of @code{+initialize}:
@example
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@@implementation FileStream
+ (void)load
1998-09-17 04:04:30 -04:00
@{
Stdin = [[FileStream new] initWithFd:0];
Stdout = [[FileStream new] initWithFd:1];
Stderr = [[FileStream new] initWithFd:2];
@}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
/* Other methods here */
1998-09-17 04:04:30 -04:00
@@end
@end example
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
2001-06-26 18:47:11 -04:00
The @code{+load} is a method that is not overridden by categories. If a
1998-09-17 04:04:30 -04:00
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.
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
1998-09-17 04:04:30 -04:00
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
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
* What you can and what you cannot do in +load::
1998-09-17 04:04:30 -04:00
@end menu
2000-08-07 02:27:47 -04:00
@node What you can and what you cannot do in +load, , Executing code before main, Executing code before main
1998-09-17 04:04:30 -04:00
@subsection What you can and what you cannot do in @code{+load}
2000-08-07 02:27:47 -04:00
The @code{+load} implementation in the GNU runtime guarantees you the following
1998-09-17 04:04:30 -04:00
things:
@itemize @bullet
@item
you can write whatever C code you like;
@item
2001-06-11 08:05:10 -04:00
you can send messages to Objective-C constant strings (@code{@@"this is a
constant string"});
1998-09-17 04:04:30 -04:00
@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
2001-06-26 18:47:11 -04:00
classes when you write @code{+load} of a class. The order in which
1998-09-17 04:04:30 -04:00
sibling classes receive @code{+load} is not guaranteed.
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
1998-09-17 04:04:30 -04:00
The order in which @code{+load} and @code{+initialize} are called could
2001-06-26 18:47:11 -04:00
be problematic if this matters. If you don't allocate objects inside
1998-09-17 04:04:30 -04:00
@code{+load}, it is guaranteed that @code{+load} is called before
2001-06-26 18:47:11 -04:00
@code{+initialize}. If you create an object inside @code{+load} the
1998-09-17 04:04:30 -04:00
@code{+initialize} method of object's class is invoked even if
2001-06-26 18:47:11 -04:00
@code{+load} was not invoked. Note if you explicitly call @code{+load}
on a class, @code{+initialize} will be called first. To avoid possible
1998-09-17 04:04:30 -04:00
problems try to implement only one of these methods.
The @code{+load} method is also invoked when a bundle is dynamically
2001-06-26 18:47:11 -04:00
loaded into your running program. This happens automatically without any
intervening operation from you. When you write bundles and you need to
1998-09-17 04:04:30 -04:00
write @code{+load} you can safely create and send messages to objects whose
2001-06-26 18:47:11 -04:00
classes already exist in the running program. The same restrictions as
1998-09-17 04:04:30 -04:00
above apply to classes defined in bundle.
2001-06-21 11:25:56 -04:00
@node Type encoding, Garbage Collection, Executing code before main, Objective-C
1998-09-17 04:04:30 -04:00
@section Type encoding
The Objective-C compiler generates type encodings for all the
2001-06-26 18:47:11 -04:00
types. These type encodings are used at runtime to find out information
1998-09-17 04:04:30 -04:00
about selectors and methods and about objects and classes.
The types are encoded in the following way:
@c @sp 1
@multitable @columnfractions .25 .75
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{char}
1998-09-17 04:04:30 -04:00
@tab @code{c}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{unsigned char}
1998-09-17 04:04:30 -04:00
@tab @code{C}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{short}
1998-09-17 04:04:30 -04:00
@tab @code{s}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{unsigned short}
1998-09-17 04:04:30 -04:00
@tab @code{S}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{int}
1998-09-17 04:04:30 -04:00
@tab @code{i}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{unsigned int}
1998-09-17 04:04:30 -04:00
@tab @code{I}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{long}
1998-09-17 04:04:30 -04:00
@tab @code{l}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{unsigned long}
1998-09-17 04:04:30 -04:00
@tab @code{L}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{long long}
1998-09-17 04:04:30 -04:00
@tab @code{q}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{unsigned long long}
1998-09-17 04:04:30 -04:00
@tab @code{Q}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{float}
1998-09-17 04:04:30 -04:00
@tab @code{f}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{double}
1998-09-17 04:04:30 -04:00
@tab @code{d}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{void}
1998-09-17 04:04:30 -04:00
@tab @code{v}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{id}
1998-09-17 04:04:30 -04:00
@tab @code{@@}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{Class}
1998-09-17 04:04:30 -04:00
@tab @code{#}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{SEL}
1998-09-17 04:04:30 -04:00
@tab @code{:}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{char*}
1998-09-17 04:04:30 -04:00
@tab @code{*}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item unknown type
1998-09-17 04:04:30 -04:00
@tab @code{?}
c-tree.texi, [...]: Fix spelling and typos.
* doc/c-tree.texi, doc/contrib.texi, doc/extend.texi,
doc/gcc.texi, doc/install.texi, doc/invoke.texi, doc/md.texi,
doc/objc.texi, doc/rtl.texi, doc/tm.texi: Fix spelling and typos.
Consistently use "built-in" and "bit-field". Minor logical markup
improvements.
* doc/gcc.1: Regenerate.
From-SVN: r43383
2001-06-14 18:51:18 -04:00
@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)
1998-09-17 04:04:30 -04:00
@end multitable
@c @sp 1
c-tree.texi, [...]: Fix spelling and typos.
* doc/c-tree.texi, doc/contrib.texi, doc/extend.texi,
doc/gcc.texi, doc/install.texi, doc/invoke.texi, doc/md.texi,
doc/objc.texi, doc/rtl.texi, doc/tm.texi: Fix spelling and typos.
Consistently use "built-in" and "bit-field". Minor logical markup
improvements.
* doc/gcc.1: Regenerate.
From-SVN: r43383
2001-06-14 18:51:18 -04:00
The encoding of bit-fields has changed to allow bit-fields to be properly
1998-09-17 04:04:30 -04:00
handled by the runtime functions that compute sizes and alignments of
2001-06-26 18:47:11 -04:00
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
1998-09-17 04:04:30 -04:00
important in the presence of the Boehm's garbage collector because the
objects are allocated using the typed memory facility available in this
2001-06-26 18:47:11 -04:00
collector. The typed memory allocation requires information about where
1998-09-17 04:04:30 -04:00
the pointers are located inside the object.
c-tree.texi, [...]: Fix spelling and typos.
* doc/c-tree.texi, doc/contrib.texi, doc/extend.texi,
doc/gcc.texi, doc/install.texi, doc/invoke.texi, doc/md.texi,
doc/objc.texi, doc/rtl.texi, doc/tm.texi: Fix spelling and typos.
Consistently use "built-in" and "bit-field". Minor logical markup
improvements.
* doc/gcc.1: Regenerate.
From-SVN: r43383
2001-06-14 18:51:18 -04:00
The position in the bit-field is the position, counting in bits, of the
1998-09-17 04:04:30 -04:00
bit closest to the beginning of the structure.
The non-atomic types are encoded as follows:
@c @sp 1
@multitable @columnfractions .2 .8
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item pointers
2001-06-11 08:05:10 -04:00
@tab @samp{^} followed by the pointed type.
1998-09-17 04:04:30 -04:00
@item arrays
2001-06-11 08:05:10 -04:00
@tab @samp{[} followed by the number of elements in the array followed by the type of the elements followed by @samp{]}
1998-09-17 04:04:30 -04:00
@item structures
2001-06-11 08:05:10 -04:00
@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{@}}
1998-09-17 04:04:30 -04:00
@item unions
2001-06-11 08:05:10 -04:00
@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{)}
1998-09-17 04:04:30 -04:00
@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
2001-06-26 18:47:11 -04:00
specifiers. The table below describes the encoding of the current
1998-09-17 04:04:30 -04:00
Objective-C type specifiers:
@sp 1
@multitable @columnfractions .25 .75
@item Specifier
@tab Encoding
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{const}
1998-09-17 04:04:30 -04:00
@tab @code{r}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{in}
1998-09-17 04:04:30 -04:00
@tab @code{n}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{inout}
1998-09-17 04:04:30 -04:00
@tab @code{N}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{out}
1998-09-17 04:04:30 -04:00
@tab @code{o}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{bycopy}
1998-09-17 04:04:30 -04:00
@tab @code{O}
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@item @code{oneway}
1998-09-17 04:04:30 -04:00
@tab @code{V}
@end multitable
@sp 1
2001-06-26 18:47:11 -04:00
The type specifiers are encoded just before the type. Unlike types
1998-09-17 04:04:30 -04:00
however, the type specifiers are only encoded when they appear in method
argument types.
2001-06-21 11:25:56 -04:00
@node Garbage Collection, Constant string objects, Type encoding, Objective-C
1998-09-17 04:04:30 -04:00
@section Garbage Collection
Support for a new memory management policy has been added by using a
powerful conservative garbage collector, known as the
2001-06-26 18:47:11 -04:00
Boehm-Demers-Weiser conservative garbage collector. It is available from
2000-08-07 02:27:47 -04:00
@w{@uref{http://www.hpl.hp.com/personal/Hans_Boehm/gc/}}.
1998-09-17 04:04:30 -04:00
To enable the support for it you have to configure the compiler using an
2001-06-26 18:47:11 -04:00
additional argument, @w{@option{--enable-objc-gc}}. You need to have
garbage collector installed before building the compiler. This will
1998-09-17 04:04:30 -04:00
build an additional runtime library which has several enhancements to
2001-06-26 18:47:11 -04:00
support the garbage collector. The new library has a new name,
2001-06-11 08:05:10 -04:00
@file{libobjc_gc.a} to not conflict with the non-garbage-collected
1998-09-17 04:04:30 -04:00
library.
When the garbage collector is used, the objects are allocated using the
so-called typed memory allocation mechanism available in the
2001-06-26 18:47:11 -04:00
Boehm-Demers-Weiser collector. This mode requires precise information on
where pointers are located inside objects. This information is computed
1998-09-17 04:04:30 -04:00
once per class, immediately after the class has been initialized.
There is a new runtime function @code{class_ivar_set_gcinvisible()}
2001-06-11 08:05:10 -04:00
which can be used to declare a so-called @dfn{weak pointer}
2001-06-26 18:47:11 -04:00
reference. Such a pointer is basically hidden for the garbage collector;
1998-09-17 04:04:30 -04:00
this can be useful in certain situations, especially when you want to
keep track of the allocated objects, yet allow them to be
2001-06-26 18:47:11 -04:00
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
1998-09-17 04:04:30 -04:00
a pointer type can be declared a weak pointer, including @code{id},
@code{Class} and @code{SEL}.
2001-06-26 18:47:11 -04:00
Here is an example of how to use this feature. Suppose you want to
1998-09-17 04:04:30 -04:00
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
2001-06-26 18:47:11 -04:00
represented by the @samp{!} character. The
1998-09-17 04:04:30 -04:00
@code{class_ivar_set_gcinvisible()} function adds or removes this
specifier to the string type description of the instance variable named
as argument.
2000-08-07 02:27:47 -04:00
@c =========================================================================
2001-05-01 20:39:01 -04:00
@node Constant string objects
2000-08-07 02:27:47 -04:00
@section Constant string objects
GNU Objective-C provides constant string objects that are generated
2001-06-26 18:47:11 -04:00
directly by the compiler. You declare a constant string object by
2001-06-11 08:05:10 -04:00
prefixing a C constant string with the character @samp{@@}:
2000-08-07 02:27:47 -04:00
@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
2001-06-26 18:47:11 -04:00
runtime. To get the definition of this class you must include the
2000-08-07 02:27:47 -04:00
@file{objc/NXConstStr.h} header file.
User defined libraries may want to implement their own constant string
2001-06-26 18:47:11 -04:00
class. To be able to support them, the GNU Objective-C compiler provides
2001-06-11 08:05:10 -04:00
a new command line options @option{-fconstant-string-class=@var{class-name}}.
The provided class should adhere to a strict structure, the same
2000-08-07 02:27:47 -04:00
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
2001-06-26 18:47:11 -04:00
string class from a different class than @code{Object}. There is no
2000-08-07 02:27:47 -04:00
requirement in the methods the constant string class has to implement.
2001-06-11 08:05:10 -04:00
When a file is compiled with the @option{-fconstant-string-class} option,
2000-08-07 02:27:47 -04:00
all the constant string objects will be instances of the class specified
2001-06-26 18:47:11 -04:00
as argument to this option. It is possible to have multiple compilation
2000-08-07 02:27:47 -04:00
units referring to different constant string classes, neither the
compiler nor the linker impose any restrictions in doing this.
1998-09-17 04:04:30 -04:00
2001-02-28 12:16:17 -05:00
@c =========================================================================
2001-05-01 20:39:01 -04:00
@node compatibility_alias
2001-02-28 12:16:17 -05:00
@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.
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
The keyword @code{@@compatibility_alias} allows you to define a class name
as equivalent to another class name. For example:
2001-02-28 12:16:17 -05:00
@example
@@compatibility_alias WOApplication GSWApplication;
@end example
1998-09-17 04:04:30 -04:00
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
tells the compiler that each time it encounters @code{WOApplication} as
a class name, it should replace it with @code{GSWApplication} (that is,
2001-02-28 12:16:17 -05:00
@code{WOApplication} is just an alias for @code{GSWApplication}).
2001-06-11 08:05:10 -04:00
There are some constraints on how this can be used---
2001-02-28 12:16:17 -05:00
c-tree.texi, [...]: Remove trailing whitespace.
* c-tree.texi, contrib.texi, cpp.texi, extend.texi, gcc.texi,
gcov.texi, install.texi, md.texi, objc.texi, rtl.texi, tm.texi:
Remove trailing whitespace.
From-SVN: r42678
2001-05-28 16:57:50 -04:00
@itemize @bullet
2001-02-28 12:16:17 -05:00
@item @code{WOApplication} (the alias) must not be an existing class;
@item @code{GSWApplication} (the real class) must be an existing class.
@end itemize