Mercurial > hg > CbC > CbC_gcc
comparison gcc/doc/objc.texi @ 111:04ced10e8804
gcc 7
author | kono |
---|---|
date | Fri, 27 Oct 2017 22:46:09 +0900 |
parents | f6334be47118 |
children | 84e7813d76e9 |
comparison
equal
deleted
inserted
replaced
68:561a7518be6b | 111:04ced10e8804 |
---|---|
1 @c Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, | 1 @c Copyright (C) 1988-2017 Free Software Foundation, Inc. |
2 @c 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2010 | |
3 @c Free Software Foundation, Inc. | |
4 @c This is part of the GCC manual. | 2 @c This is part of the GCC manual. |
5 @c For copying conditions, see the file gcc.texi. | 3 @c For copying conditions, see the file gcc.texi. |
6 | 4 |
7 @node Objective-C | 5 @node Objective-C |
8 @comment node-name, next, previous, up | 6 @comment node-name, next, previous, up |
9 | 7 |
10 @chapter GNU Objective-C features | 8 @chapter GNU Objective-C Features |
11 | 9 |
12 This document is meant to describe some of the GNU Objective-C | 10 This document is meant to describe some of the GNU Objective-C |
13 features. It is not intended to teach you Objective-C. There are | 11 features. It is not intended to teach you Objective-C. There are |
14 several resources on the Internet that present the language. | 12 several resources on the Internet that present the language. |
15 | 13 |
26 * Messaging with the GNU Objective-C runtime:: | 24 * Messaging with the GNU Objective-C runtime:: |
27 @end menu | 25 @end menu |
28 | 26 |
29 @c ========================================================================= | 27 @c ========================================================================= |
30 @node GNU Objective-C runtime API | 28 @node GNU Objective-C runtime API |
31 @section GNU Objective-C runtime API | 29 @section GNU Objective-C Runtime API |
32 | 30 |
33 This section is specific for the GNU Objective-C runtime. If you are | 31 This section is specific for the GNU Objective-C runtime. If you are |
34 using a different runtime, you can skip it. | 32 using a different runtime, you can skip it. |
35 | 33 |
36 The GNU Objective-C runtime provides an API that allows you to | 34 The GNU Objective-C runtime provides an API that allows you to |
50 * Traditional GNU Objective-C runtime API:: | 48 * Traditional GNU Objective-C runtime API:: |
51 @end menu | 49 @end menu |
52 | 50 |
53 @c ========================================================================= | 51 @c ========================================================================= |
54 @node Modern GNU Objective-C runtime API | 52 @node Modern GNU Objective-C runtime API |
55 @subsection Modern GNU Objective-C runtime API | 53 @subsection Modern GNU Objective-C Runtime API |
56 | 54 |
57 The GNU Objective-C runtime provides an API which is similar to the | 55 The GNU Objective-C runtime provides an API which is similar to the |
58 one provided by the ``Objective-C 2.0'' Apple/NeXT Objective-C | 56 one provided by the ``Objective-C 2.0'' Apple/NeXT Objective-C |
59 runtime. The API is documented in the public header files of the GNU | 57 runtime. The API is documented in the public header files of the GNU |
60 Objective-C runtime: | 58 Objective-C runtime: |
109 The header files contain detailed documentation for each function in | 107 The header files contain detailed documentation for each function in |
110 the GNU Objective-C runtime API. | 108 the GNU Objective-C runtime API. |
111 | 109 |
112 @c ========================================================================= | 110 @c ========================================================================= |
113 @node Traditional GNU Objective-C runtime API | 111 @node Traditional GNU Objective-C runtime API |
114 @subsection Traditional GNU Objective-C runtime API | 112 @subsection Traditional GNU Objective-C Runtime API |
115 | 113 |
116 The GNU Objective-C runtime used to provide a different API, which we | 114 The GNU Objective-C runtime used to provide a different API, which we |
117 call the ``traditional'' GNU Objective-C runtime API. Functions | 115 call the ``traditional'' GNU Objective-C runtime API. Functions |
118 belonging to this API are easy to recognize because they use a | 116 belonging to this API are easy to recognize because they use a |
119 different naming convention, such as @code{class_get_super_class()} | 117 different naming convention, such as @code{class_get_super_class()} |
120 (traditional API) instead of @code{class_getSuperclass()} (modern | 118 (traditional API) instead of @code{class_getSuperclass()} (modern |
121 API). Software using this API includes the file | 119 API). Software using this API includes the file |
122 @file{objc/objc-api.h} where it is declared. | 120 @file{objc/objc-api.h} where it is declared. |
123 | 121 |
124 The traditional API is deprecated but it is still supported in this | 122 Starting with GCC 4.7.0, the traditional GNU runtime API is no longer |
125 release of the runtime; you can access it as usual by including | 123 available. |
126 @file{objc/objc-api.h}. | |
127 | |
128 If you are using the traditional API you are urged to upgrade your | |
129 software to use the modern API because the traditional API requires | |
130 access to private runtime internals to do anything serious with it; | |
131 for this reason, there is no guarantee that future releases of the GNU | |
132 Objective-C runtime library will be able to provide a fully compatible | |
133 @file{objc/objc-api.h} as the private runtime internals change. It is | |
134 expected that the next release will hide a number of runtime internals | |
135 making the traditional API nominally supported but fairly useless | |
136 beyond very simple use cases. | |
137 | |
138 Finally, you can not include both @file{objc/objc-api.h} and | |
139 @file{objc/runtime.h} at the same time. The traditional and modern | |
140 APIs unfortunately have some conflicting declarations (such as the one | |
141 for @code{Method}) and can not be used at the same time. | |
142 | 124 |
143 @c ========================================================================= | 125 @c ========================================================================= |
144 @node Executing code before main | 126 @node Executing code before main |
145 @section @code{+load}: Executing code before main | 127 @section @code{+load}: Executing Code before @code{main} |
146 | 128 |
147 This section is specific for the GNU Objective-C runtime. If you are | 129 This section is specific for the GNU Objective-C runtime. If you are |
148 using a different runtime, you can skip it. | 130 using a different runtime, you can skip it. |
149 | 131 |
150 The GNU Objective-C runtime provides a way that allows you to execute | 132 The GNU Objective-C runtime provides a way that allows you to execute |
224 * What you can and what you cannot do in +load:: | 206 * What you can and what you cannot do in +load:: |
225 @end menu | 207 @end menu |
226 | 208 |
227 | 209 |
228 @node What you can and what you cannot do in +load | 210 @node What you can and what you cannot do in +load |
229 @subsection What you can and what you cannot do in @code{+load} | 211 @subsection What You Can and Cannot Do in @code{+load} |
230 | 212 |
231 @code{+load} is to be used only as a last resort. Because it is | 213 @code{+load} is to be used only as a last resort. Because it is |
232 executed very early, most of the Objective-C runtime machinery will | 214 executed very early, most of the Objective-C runtime machinery will |
233 not be ready when @code{+load} is executed; hence @code{+load} works | 215 not be ready when @code{+load} is executed; hence @code{+load} works |
234 best for executing C code that is independent on the Objective-C | 216 best for executing C code that is independent on the Objective-C |
295 above apply to classes defined in bundle. | 277 above apply to classes defined in bundle. |
296 | 278 |
297 | 279 |
298 | 280 |
299 @node Type encoding | 281 @node Type encoding |
300 @section Type encoding | 282 @section Type Encoding |
301 | 283 |
302 This is an advanced section. Type encodings are used extensively by | 284 This is an advanced section. Type encodings are used extensively by |
303 the compiler and by the runtime, but you generally do not need to know | 285 the compiler and by the runtime, but you generally do not need to know |
304 about them to use Objective-C. | 286 about them to use Objective-C. |
305 | 287 |
397 Here are some types and their encodings, as they are generated by the | 379 Here are some types and their encodings, as they are generated by the |
398 compiler on an i386 machine: | 380 compiler on an i386 machine: |
399 | 381 |
400 @sp 1 | 382 @sp 1 |
401 | 383 |
402 @multitable @columnfractions .25 .75 | 384 @multitable @columnfractions .60 .40 |
403 @item Objective-C type | 385 @item Objective-C type |
404 @tab Compiler encoding | 386 @tab Compiler encoding |
405 @item | 387 @item |
406 @smallexample | 388 @smallexample |
407 int a[10]; | 389 int a[10]; |
420 @tab @code{@{?=i[3f]b128i3b131i2c@}} | 402 @tab @code{@{?=i[3f]b128i3b131i2c@}} |
421 @item | 403 @item |
422 @smallexample | 404 @smallexample |
423 int a __attribute__ ((vector_size (16))); | 405 int a __attribute__ ((vector_size (16))); |
424 @end smallexample | 406 @end smallexample |
425 @tab @code{![16,16i]} (alignment would depend on the machine) | 407 @tab @code{![16,16i]} (alignment depends on the machine) |
426 @end multitable | 408 @end multitable |
427 | 409 |
428 @sp 1 | 410 @sp 1 |
429 | 411 |
430 In addition to the types the compiler also encodes the type | 412 In addition to the types the compiler also encodes the type |
503 * @@encode:: | 485 * @@encode:: |
504 * Method signatures:: | 486 * Method signatures:: |
505 @end menu | 487 @end menu |
506 | 488 |
507 @node Legacy type encoding | 489 @node Legacy type encoding |
508 @subsection Legacy type encoding | 490 @subsection Legacy Type Encoding |
509 | 491 |
510 Unfortunately, historically GCC used to have a number of bugs in its | 492 Unfortunately, historically GCC used to have a number of bugs in its |
511 encoding code. The NeXT runtime expects GCC to emit type encodings in | 493 encoding code. The NeXT runtime expects GCC to emit type encodings in |
512 this historical format (compatible with GCC-3.3), so when using the | 494 this historical format (compatible with GCC-3.3), so when using the |
513 NeXT runtime, GCC will introduce on purpose a number of incorrect | 495 NeXT runtime, GCC will introduce on purpose a number of incorrect |
534 In addition to that, the NeXT runtime uses a different encoding for | 516 In addition to that, the NeXT runtime uses a different encoding for |
535 bitfields. It encodes them as @code{b} followed by the size, without | 517 bitfields. It encodes them as @code{b} followed by the size, without |
536 a bit offset or the underlying field type. | 518 a bit offset or the underlying field type. |
537 | 519 |
538 @node @@encode | 520 @node @@encode |
539 @subsection @@encode | 521 @subsection @code{@@encode} |
540 | 522 |
541 GNU Objective-C supports the @code{@@encode} syntax that allows you to | 523 GNU Objective-C supports the @code{@@encode} syntax that allows you to |
542 create a type encoding from a C/Objective-C type. For example, | 524 create a type encoding from a C/Objective-C type. For example, |
543 @code{@@encode(int)} is compiled by the compiler into @code{"i"}. | 525 @code{@@encode(int)} is compiled by the compiler into @code{"i"}. |
544 | 526 |
546 @code{const}. For example, @code{@@encode(const char*)} is valid and | 528 @code{const}. For example, @code{@@encode(const char*)} is valid and |
547 is compiled into @code{"r*"}, while @code{@@encode(bycopy char *)} is | 529 is compiled into @code{"r*"}, while @code{@@encode(bycopy char *)} is |
548 invalid and will cause a compilation error. | 530 invalid and will cause a compilation error. |
549 | 531 |
550 @node Method signatures | 532 @node Method signatures |
551 @subsection Method signatures | 533 @subsection Method Signatures |
552 | 534 |
553 This section documents the encoding of method types, which is rarely | 535 This section documents the encoding of method types, which is rarely |
554 needed to use Objective-C. You should skip it at a first reading; the | 536 needed to use Objective-C. You should skip it at a first reading; the |
555 runtime provides functions that will work on methods and can walk | 537 runtime provides functions that will work on methods and can walk |
556 through the list of parameters and interpret them for you. These | 538 through the list of parameters and interpret them for you. These |
649 | 631 |
650 @@implementation WeakPointer | 632 @@implementation WeakPointer |
651 | 633 |
652 + (void)initialize | 634 + (void)initialize |
653 @{ | 635 @{ |
654 class_ivar_set_gcinvisible (self, "weakPointer", YES); | 636 if (self == objc_lookUpClass ("WeakPointer")) |
637 class_ivar_set_gcinvisible (self, "weakPointer", YES); | |
655 @} | 638 @} |
656 | 639 |
657 - initWithPointer:(const void*)p | 640 - initWithPointer:(const void*)p |
658 @{ | 641 @{ |
659 weakPointer = p; | 642 weakPointer = p; |
675 specifier to the string type description of the instance variable named | 658 specifier to the string type description of the instance variable named |
676 as argument. | 659 as argument. |
677 | 660 |
678 @c ========================================================================= | 661 @c ========================================================================= |
679 @node Constant string objects | 662 @node Constant string objects |
680 @section Constant string objects | 663 @section Constant String Objects |
681 | 664 |
682 GNU Objective-C provides constant string objects that are generated | 665 GNU Objective-C provides constant string objects that are generated |
683 directly by the compiler. You declare a constant string object by | 666 directly by the compiler. You declare a constant string object by |
684 prefixing a C constant string with the character @samp{@@}: | 667 prefixing a C constant string with the character @samp{@@}: |
685 | 668 |
736 constant string classes, neither the compiler nor the linker impose any | 719 constant string classes, neither the compiler nor the linker impose any |
737 restrictions in doing this. | 720 restrictions in doing this. |
738 | 721 |
739 @c ========================================================================= | 722 @c ========================================================================= |
740 @node compatibility_alias | 723 @node compatibility_alias |
741 @section compatibility_alias | 724 @section @code{compatibility_alias} |
742 | 725 |
743 The keyword @code{@@compatibility_alias} allows you to define a class name | 726 The keyword @code{@@compatibility_alias} allows you to define a class name |
744 as equivalent to another class name. For example: | 727 as equivalent to another class name. For example: |
745 | 728 |
746 @smallexample | 729 @smallexample |
873 @option{-fobjc-exceptions}. | 856 @option{-fobjc-exceptions}. |
874 | 857 |
875 | 858 |
876 @c ========================================================================= | 859 @c ========================================================================= |
877 @node Fast enumeration | 860 @node Fast enumeration |
878 @section Fast enumeration | 861 @section Fast Enumeration |
879 | 862 |
880 @menu | 863 @menu |
881 * Using fast enumeration:: | 864 * Using fast enumeration:: |
882 * c99-like fast enumeration syntax:: | 865 * c99-like fast enumeration syntax:: |
883 * Fast enumeration details:: | 866 * Fast enumeration details:: |
884 * Fast enumeration protocol:: | 867 * Fast enumeration protocol:: |
885 @end menu | 868 @end menu |
886 | 869 |
887 @c ================================ | 870 @c ================================ |
888 @node Using fast enumeration | 871 @node Using fast enumeration |
889 @subsection Using fast enumeration | 872 @subsection Using Fast Enumeration |
890 | 873 |
891 GNU Objective-C provides support for the fast enumeration syntax: | 874 GNU Objective-C provides support for the fast enumeration syntax: |
892 | 875 |
893 @smallexample | 876 @smallexample |
894 id array = @dots{}; | 877 id array = @dots{}; |
924 @end smallexample | 907 @end smallexample |
925 | 908 |
926 | 909 |
927 @c ================================ | 910 @c ================================ |
928 @node c99-like fast enumeration syntax | 911 @node c99-like fast enumeration syntax |
929 @subsection c99-like fast enumeration syntax | 912 @subsection C99-Like Fast Enumeration Syntax |
930 | 913 |
931 A c99-like declaration syntax is also allowed: | 914 A c99-like declaration syntax is also allowed: |
932 | 915 |
933 @smallexample | 916 @smallexample |
934 id array = @dots{}; | 917 id array = @dots{}; |
958 Note that the option @option{-std=c99} is not required to allow this | 941 Note that the option @option{-std=c99} is not required to allow this |
959 syntax in Objective-C. | 942 syntax in Objective-C. |
960 | 943 |
961 @c ================================ | 944 @c ================================ |
962 @node Fast enumeration details | 945 @node Fast enumeration details |
963 @subsection Fast enumeration details | 946 @subsection Fast Enumeration Details |
964 | 947 |
965 Here is a more technical description with the gory details. Consider the code | 948 Here is a more technical description with the gory details. Consider the code |
966 | 949 |
967 @smallexample | 950 @smallexample |
968 for (@var{object expression} in @var{collection expression}) | 951 for (@var{object expression} in @var{collection expression}) |
1021 | 1004 |
1022 @end itemize | 1005 @end itemize |
1023 | 1006 |
1024 @c ================================ | 1007 @c ================================ |
1025 @node Fast enumeration protocol | 1008 @node Fast enumeration protocol |
1026 @subsection Fast enumeration protocol | 1009 @subsection Fast Enumeration Protocol |
1027 | 1010 |
1028 If you want your own collection object to be usable with fast | 1011 If you want your own collection object to be usable with fast |
1029 enumeration, you need to have it implement the method | 1012 enumeration, you need to have it implement the method |
1030 | 1013 |
1031 @smallexample | 1014 @smallexample |
1032 - (unsigned long) countByEnumeratingWithState: (NSFastEnumerationState *)state | 1015 - (unsigned long) countByEnumeratingWithState: (NSFastEnumerationState *)state |
1033 objects: (id *)objects | 1016 objects: (id *)objects |
1034 count: (unsigned long)len; | 1017 count: (unsigned long)len; |
1035 @end smallexample | 1018 @end smallexample |
1036 | 1019 |
1037 where @code{NSFastEnumerationState} must be defined in your code as follows: | 1020 where @code{NSFastEnumerationState} must be defined in your code as follows: |
1091 value to be of type @code{unsigned long}. They could also be declared | 1074 value to be of type @code{unsigned long}. They could also be declared |
1092 to be of type @code{unsigned int} and everything would still work. | 1075 to be of type @code{unsigned int} and everything would still work. |
1093 | 1076 |
1094 @c ========================================================================= | 1077 @c ========================================================================= |
1095 @node Messaging with the GNU Objective-C runtime | 1078 @node Messaging with the GNU Objective-C runtime |
1096 @section Messaging with the GNU Objective-C runtime | 1079 @section Messaging with the GNU Objective-C Runtime |
1097 | 1080 |
1098 This section is specific for the GNU Objective-C runtime. If you are | 1081 This section is specific for the GNU Objective-C runtime. If you are |
1099 using a different runtime, you can skip it. | 1082 using a different runtime, you can skip it. |
1100 | 1083 |
1101 The implementation of messaging in the GNU Objective-C runtime is | 1084 The implementation of messaging in the GNU Objective-C runtime is |
1122 * Forwarding hook:: | 1105 * Forwarding hook:: |
1123 @end menu | 1106 @end menu |
1124 | 1107 |
1125 @c ========================================================================= | 1108 @c ========================================================================= |
1126 @node Dynamically registering methods | 1109 @node Dynamically registering methods |
1127 @subsection Dynamically registering methods | 1110 @subsection Dynamically Registering Methods |
1128 | 1111 |
1129 If @code{objc_msg_lookup()} does not find a suitable method | 1112 If @code{objc_msg_lookup()} does not find a suitable method |
1130 implementation, because the receiver does not implement the required | 1113 implementation, because the receiver does not implement the required |
1131 method, it tries to see if the class can dynamically register the | 1114 method, it tries to see if the class can dynamically register the |
1132 method. | 1115 method. |
1136 | 1119 |
1137 @smallexample | 1120 @smallexample |
1138 + (BOOL) resolveInstanceMethod: (SEL)selector; | 1121 + (BOOL) resolveInstanceMethod: (SEL)selector; |
1139 @end smallexample | 1122 @end smallexample |
1140 | 1123 |
1141 in the case of an instance method, or | 1124 in the case of an instance method, or |
1142 | 1125 |
1143 @smallexample | 1126 @smallexample |
1144 + (BOOL) resolveClassMethod: (SEL)selector; | 1127 + (BOOL) resolveClassMethod: (SEL)selector; |
1145 @end smallexample | 1128 @end smallexample |
1146 | 1129 |
1168 @code{resolveClassMethod:} was added to the GNU Objective-C runtime in | 1151 @code{resolveClassMethod:} was added to the GNU Objective-C runtime in |
1169 GCC version 4.6. | 1152 GCC version 4.6. |
1170 | 1153 |
1171 @c ========================================================================= | 1154 @c ========================================================================= |
1172 @node Forwarding hook | 1155 @node Forwarding hook |
1173 @subsection Forwarding hook | 1156 @subsection Forwarding Hook |
1174 | 1157 |
1175 The GNU Objective-C runtime provides a hook, called | 1158 The GNU Objective-C runtime provides a hook, called |
1176 @code{__objc_msg_forward2}, which is called by | 1159 @code{__objc_msg_forward2}, which is called by |
1177 @code{objc_msg_lookup()} when it can't find a method implementation in | 1160 @code{objc_msg_lookup()} when it cannot find a method implementation in |
1178 the runtime tables and after calling @code{+resolveInstanceMethod:} | 1161 the runtime tables and after calling @code{+resolveInstanceMethod:} |
1179 and @code{+resolveClassMethod:} has been attempted and did not succeed | 1162 and @code{+resolveClassMethod:} has been attempted and did not succeed |
1180 in dynamically registering the method. | 1163 in dynamically registering the method. |
1181 | 1164 |
1182 To configure the hook, you set the global variable | 1165 To configure the hook, you set the global variable |
1183 @code{__objc_msg_foward2} to a function with the same argument and | 1166 @code{__objc_msg_forward2} to a function with the same argument and |
1184 return types of @code{objc_msg_lookup()}. When | 1167 return types of @code{objc_msg_lookup()}. When |
1185 @code{objc_msg_lookup()} can not find a method implementation, it | 1168 @code{objc_msg_lookup()} can not find a method implementation, it |
1186 invokes the hook function you provided to get a method implementation | 1169 invokes the hook function you provided to get a method implementation |
1187 to return. So, in practice @code{__objc_msg_forward2} allows you to | 1170 to return. So, in practice @code{__objc_msg_forward2} allows you to |
1188 extend @code{objc_msg_lookup()} by adding some custom code that is | 1171 extend @code{objc_msg_lookup()} by adding some custom code that is |