]>
Commit | Line | Data |
---|---|---|
fc304191 | 1 | /* Implement classes and message passing for Objective C. |
d353bf18 | 2 | Copyright (C) 1992-2015 Free Software Foundation, Inc. |
51eb6608 | 3 | Contributed by Steve Naroff. |
fc304191 | 4 | |
e3708be9 | 5 | This file is part of GCC. |
fc304191 | 6 | |
e3708be9 | 7 | GCC is free software; you can redistribute it and/or modify |
fc304191 | 8 | it under the terms of the GNU General Public License as published by |
c3adda75 | 9 | the Free Software Foundation; either version 3, or (at your option) |
fc304191 | 10 | any later version. |
11 | ||
e3708be9 | 12 | GCC is distributed in the hope that it will be useful, |
fc304191 | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
c3adda75 | 18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
20 | ||
fc304191 | 21 | #include "config.h" |
47003e2a | 22 | #include "system.h" |
805e22b2 | 23 | #include "coretypes.h" |
24 | #include "tm.h" | |
b20a8bb4 | 25 | #include "alias.h" |
fc304191 | 26 | #include "tree.h" |
9ef16211 | 27 | #include "options.h" |
b20a8bb4 | 28 | #include "fold-const.h" |
9ed99284 | 29 | #include "stringpool.h" |
30 | #include "stor-layout.h" | |
31 | #include "attribs.h" | |
13dcc150 | 32 | |
33 | #ifdef OBJCPLUS | |
e53d55e7 | 34 | #include "cp/cp-tree.h" |
13dcc150 | 35 | #else |
e53d55e7 | 36 | #include "c/c-tree.h" |
37 | #include "c/c-lang.h" | |
13dcc150 | 38 | #endif |
39 | ||
7bedc3a0 | 40 | #include "c-family/c-common.h" |
6c536c4f | 41 | #include "c-family/c-objc.h" |
7bedc3a0 | 42 | #include "c-family/c-pragma.h" |
1f6616ee | 43 | #include "c-family/c-format.h" |
fc304191 | 44 | #include "flags.h" |
d1b92730 | 45 | #include "langhooks.h" |
1a62b6bf | 46 | #include "objc-act.h" |
f41791cf | 47 | #include "objc-map.h" |
a3020f2f | 48 | #include "hard-reg-set.h" |
c450c529 | 49 | #include "function.h" |
cd03a192 | 50 | #include "toplev.h" |
965ae951 | 51 | #include "debug.h" |
c94b1d0e | 52 | #include "c-family/c-target.h" |
852f689e | 53 | #include "diagnostic-core.h" |
a608187f | 54 | #include "intl.h" |
376c21d1 | 55 | #include "cgraph.h" |
e24e8df7 | 56 | #include "tree-iterator.h" |
d7c168e8 | 57 | #include "langhooks-def.h" |
267785bc | 58 | /* Different initialization, code gen and meta data generation for each |
59 | runtime. */ | |
60 | #include "objc-runtime-hooks.h" | |
61 | /* Routines used mainly by the runtimes. */ | |
62 | #include "objc-runtime-shared-support.h" | |
40c8d1dd | 63 | /* For default_tree_printer (). */ |
64 | #include "tree-pretty-print.h" | |
65 | ||
c4b9c21a | 66 | /* For enum gimplify_status */ |
bc61cadb | 67 | #include "gimple-expr.h" |
a8783bee | 68 | #include "gimplify.h" |
c4b9c21a | 69 | |
f572c7ba | 70 | /* For encode_method_prototype(). */ |
71 | #include "objc-encoding.h" | |
72 | ||
0ced0389 | 73 | static unsigned int should_call_super_dealloc = 0; |
74 | ||
13dcc150 | 75 | /* When building Objective-C++, we are not linking against the C front-end |
76 | and so need to replicate the C tree-construction functions in some way. */ | |
77 | #ifdef OBJCPLUS | |
78 | #define OBJCP_REMAP_FUNCTIONS | |
79 | #include "objcp-decl.h" | |
80 | #endif /* OBJCPLUS */ | |
d74ae00c | 81 | |
0a1740b4 | 82 | /* This is the default way of generating a method name. */ |
e23bf1fb | 83 | /* This has the problem that "test_method:argument:" and |
84 | "test:method_argument:" will generate the same name | |
85 | ("_i_Test__test_method_argument_" for an instance method of the | |
86 | class "Test"), so you can't have them both in the same class! | |
87 | Moreover, the demangling (going from | |
88 | "_i_Test__test_method_argument" back to the original name) is | |
89 | undefined because there are two correct ways of demangling the | |
90 | name. */ | |
0a1740b4 | 91 | #ifndef OBJC_GEN_METHOD_LABEL |
ff059d05 | 92 | #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \ |
93 | do { \ | |
94 | char *temp; \ | |
95 | sprintf ((BUF), "_%s_%s_%s_%s", \ | |
96 | ((IS_INST) ? "i" : "c"), \ | |
97 | (CLASS_NAME), \ | |
98 | ((CAT_NAME)? (CAT_NAME) : ""), \ | |
99 | (SEL_NAME)); \ | |
100 | for (temp = (BUF); *temp; temp++) \ | |
101 | if (*temp == ':') *temp = '_'; \ | |
0a1740b4 | 102 | } while (0) |
0ad17d1b | 103 | #endif |
c450c529 | 104 | |
105 | /* These need specifying. */ | |
106 | #ifndef OBJC_FORWARDING_STACK_OFFSET | |
107 | #define OBJC_FORWARDING_STACK_OFFSET 0 | |
108 | #endif | |
109 | ||
110 | #ifndef OBJC_FORWARDING_MIN_OFFSET | |
111 | #define OBJC_FORWARDING_MIN_OFFSET 0 | |
112 | #endif | |
267785bc | 113 | |
fc304191 | 114 | /*** Private Interface (procedures) ***/ |
115 | ||
267785bc | 116 | /* Init stuff. */ |
117 | static void synth_module_prologue (void); | |
c450c529 | 118 | |
a92771b8 | 119 | /* Code generation. */ |
fc304191 | 120 | |
c1cfb0f5 | 121 | static tree start_class (enum tree_code, tree, tree, tree, tree); |
13dcc150 | 122 | static tree continue_class (tree); |
123 | static void finish_class (tree); | |
4232a958 | 124 | static void start_method_def (tree, tree); |
267785bc | 125 | |
c213e196 | 126 | static tree start_protocol (enum tree_code, tree, tree, tree); |
450302fe | 127 | static tree build_method_decl (enum tree_code, tree, tree, tree, bool); |
069761fb | 128 | static tree objc_add_method (tree, tree, int, bool); |
4a8875ed | 129 | static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree); |
13dcc150 | 130 | static tree build_ivar_reference (tree); |
131 | static tree is_ivar (tree, tree); | |
13dcc150 | 132 | |
499ea517 | 133 | /* We only need the following for ObjC; ObjC++ will use C++'s definition |
134 | of DERIVED_FROM_P. */ | |
135 | #ifndef OBJCPLUS | |
136 | static bool objc_derived_from_p (tree, tree); | |
137 | #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD) | |
138 | #endif | |
86c110ac | 139 | |
140 | /* Property. */ | |
86c110ac | 141 | static void objc_gen_property_data (tree, tree); |
142 | static void objc_synthesize_getter (tree, tree, tree); | |
86c110ac | 143 | static void objc_synthesize_setter (tree, tree, tree); |
86c110ac | 144 | static tree lookup_property (tree, tree); |
145 | static tree lookup_property_in_list (tree, tree); | |
146 | static tree lookup_property_in_protocol_list (tree, tree); | |
267785bc | 147 | static void build_common_objc_property_accessor_helpers (void); |
86c110ac | 148 | |
499ea517 | 149 | static void objc_xref_basetypes (tree, tree); |
150 | ||
499ea517 | 151 | static tree get_class_ivars (tree, bool); |
39b8ddc6 | 152 | |
0a65c3bb | 153 | static void build_fast_enumeration_state_template (void); |
154 | ||
0ced0389 | 155 | #ifdef OBJCPLUS |
156 | static void objc_generate_cxx_cdtors (void); | |
157 | #endif | |
39b8ddc6 | 158 | |
40c8d1dd | 159 | /* objc attribute */ |
6bbb4715 | 160 | static void objc_decl_method_attributes (tree*, tree, int); |
40c8d1dd | 161 | static tree build_keyword_selector (tree); |
ebcbb79c | 162 | |
267785bc | 163 | static void hash_init (void); |
fc304191 | 164 | |
f41791cf | 165 | /* Hash tables to manage the global pool of method prototypes. Each |
166 | of these maps map a method name (selector) identifier to either a | |
167 | single tree (for methods with a single method prototype) or a | |
168 | TREE_VEC (for methods with multiple method prototypes). */ | |
169 | static GTY(()) objc_map_t instance_method_map = 0; | |
170 | static GTY(()) objc_map_t class_method_map = 0; | |
c450c529 | 171 | |
e9e7c336 | 172 | /* Hash tables to manage the global pool of class names. */ |
173 | ||
f41791cf | 174 | static GTY(()) objc_map_t class_name_map = 0; |
175 | static GTY(()) objc_map_t alias_name_map = 0; | |
e9e7c336 | 176 | |
39b8ddc6 | 177 | static tree lookup_method (tree, tree); |
c17b85ea | 178 | static tree lookup_method_static (tree, tree, int); |
c450c529 | 179 | |
f41791cf | 180 | static void interface_hash_init (void); |
181 | static tree add_interface (tree, tree); | |
86c110ac | 182 | static void add_category (tree, tree); |
183 | static inline tree lookup_category (tree, tree); | |
184 | ||
c213e196 | 185 | /* Protocols. */ |
c450c529 | 186 | |
89e83a91 | 187 | static tree lookup_protocol (tree, bool, bool); |
188 | static tree lookup_and_install_protocols (tree, bool); | |
fc304191 | 189 | |
ac206aff | 190 | #ifdef OBJCPLUS |
39b8ddc6 | 191 | static void really_start_method (tree, tree); |
ac206aff | 192 | #else |
193 | static void really_start_method (tree, struct c_arg_info *); | |
194 | #endif | |
0ced0389 | 195 | static int comp_proto_with_proto (tree, tree, int); |
ea446b0b | 196 | static tree objc_decay_parm_type (tree); |
fc304191 | 197 | |
a92771b8 | 198 | /* Utilities for debugging and error diagnostics. */ |
fc304191 | 199 | |
13dcc150 | 200 | static char *gen_type_name (tree); |
201 | static char *gen_type_name_0 (tree); | |
202 | static char *gen_method_decl (tree); | |
203 | static char *gen_declaration (tree); | |
c450c529 | 204 | |
a92771b8 | 205 | /* Everything else. */ |
c450c529 | 206 | |
267785bc | 207 | static void generate_struct_by_value_array (void) ATTRIBUTE_NORETURN; |
208 | ||
39b8ddc6 | 209 | static void mark_referenced_methods (void); |
f26877d5 | 210 | static bool objc_type_valid_for_messaging (tree type, bool allow_classes); |
f41791cf | 211 | static tree check_duplicates (tree, int, int); |
fc304191 | 212 | |
213 | /*** Private Interface (data) ***/ | |
0ced0389 | 214 | /* Flags for lookup_method_static(). */ |
736dea43 | 215 | |
216 | /* Look for class methods. */ | |
217 | #define OBJC_LOOKUP_CLASS 1 | |
218 | /* Do not examine superclasses. */ | |
219 | #define OBJC_LOOKUP_NO_SUPER 2 | |
220 | /* Disable returning an instance method of a root class when a class | |
221 | method can't be found. */ | |
6bbb4715 | 222 | #define OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS 4 |
0ced0389 | 223 | |
3e1e20c1 | 224 | /* The OCTI_... enumeration itself is in objc/objc-act.h. */ |
99ff4160 | 225 | tree objc_global_trees[OCTI_MAX]; |
fc304191 | 226 | |
99ff4160 | 227 | struct imp_entry *imp_list = 0; |
228 | int imp_count = 0; /* `@implementation' */ | |
229 | int cat_count = 0; /* `@category' */ | |
230 | ||
06511efd | 231 | objc_ivar_visibility_kind objc_ivar_visibility, objc_default_ivar_visibility; |
13dcc150 | 232 | |
c17b85ea | 233 | /* Use to generate method labels. */ |
234 | static int method_slot = 0; | |
fc304191 | 235 | |
069761fb | 236 | /* Flag to say whether methods in a protocol are optional or |
237 | required. */ | |
238 | static bool objc_method_optional_flag = false; | |
239 | ||
e93ea189 | 240 | static int objc_collecting_ivars = 0; |
241 | ||
e16610d0 | 242 | /* Flag that is set to 'true' while we are processing a class |
243 | extension. Since a class extension just "reopens" the main | |
244 | @interface, this can be used to determine if we are in the main | |
245 | @interface, or in a class extension. */ | |
246 | static bool objc_in_class_extension = false; | |
247 | ||
e0ece38e | 248 | static char *errbuf; /* Buffer for error diagnostics */ |
fc304191 | 249 | |
d7489d8d | 250 | /* An array of all the local variables in the current function that |
251 | need to be marked as volatile. */ | |
f1f41a6c | 252 | vec<tree, va_gc> *local_variables_to_volatilize = NULL; |
d7489d8d | 253 | |
13dcc150 | 254 | /* Store all constructed constant strings in a hash table so that |
255 | they get uniqued properly. */ | |
256 | ||
2ef51f0e | 257 | struct GTY((for_user)) string_descriptor { |
13dcc150 | 258 | /* The literal argument . */ |
259 | tree literal; | |
260 | ||
261 | /* The resulting constant string. */ | |
262 | tree constructor; | |
263 | }; | |
264 | ||
b594087e | 265 | struct objc_string_hasher : ggc_ptr_hash<string_descriptor> |
2ef51f0e | 266 | { |
267 | static hashval_t hash (string_descriptor *); | |
268 | static bool equal (string_descriptor *, string_descriptor *); | |
269 | }; | |
270 | ||
271 | static GTY(()) hash_table<objc_string_hasher> *string_htab; | |
13dcc150 | 272 | |
fc304191 | 273 | FILE *gen_declaration_file; |
274 | ||
267785bc | 275 | /* Hooks for stuff that differs between runtimes. */ |
276 | objc_runtime_hooks runtime; | |
6ab3a6e6 | 277 | |
e4a7640a | 278 | /* Create a temporary variable of type 'type'. If 'name' is set, uses |
279 | the specified name, else use no name. Returns the declaration of | |
280 | the type. The 'name' is mostly useful for debugging. | |
281 | */ | |
267785bc | 282 | tree |
e4a7640a | 283 | objc_create_temporary_var (tree type, const char *name) |
284 | { | |
285 | tree decl; | |
286 | ||
287 | if (name != NULL) | |
288 | { | |
289 | decl = build_decl (input_location, | |
290 | VAR_DECL, get_identifier (name), type); | |
291 | } | |
292 | else | |
293 | { | |
294 | decl = build_decl (input_location, | |
295 | VAR_DECL, NULL_TREE, type); | |
296 | } | |
297 | TREE_USED (decl) = 1; | |
298 | DECL_ARTIFICIAL (decl) = 1; | |
299 | DECL_IGNORED_P (decl) = 1; | |
300 | DECL_CONTEXT (decl) = current_function_decl; | |
301 | ||
302 | return decl; | |
303 | } | |
304 | ||
3e1e20c1 | 305 | /* Some platforms pass small structures through registers versus |
306 | through an invisible pointer. Determine at what size structure is | |
307 | the transition point between the two possibilities. */ | |
ebcbb79c | 308 | |
647c0320 | 309 | static void |
39b8ddc6 | 310 | generate_struct_by_value_array (void) |
ebcbb79c | 311 | { |
312 | tree type; | |
6ab3a6e6 | 313 | tree decls; |
ebcbb79c | 314 | int i, j; |
315 | int aggregate_in_mem[32]; | |
316 | int found = 0; | |
317 | ||
3c2c63b4 | 318 | /* Presumably no platform passes 32 byte structures in a register. */ |
319 | /* ??? As an example, m64/ppc/Darwin can pass up to 8*long+13*double | |
320 | in registers. */ | |
ebcbb79c | 321 | for (i = 1; i < 32; i++) |
322 | { | |
323 | char buffer[5]; | |
6ab3a6e6 | 324 | tree *chain = NULL; |
ebcbb79c | 325 | |
326 | /* Create an unnamed struct that has `i' character components */ | |
0b09525f | 327 | type = objc_start_struct (NULL_TREE); |
ebcbb79c | 328 | |
329 | strcpy (buffer, "c1"); | |
6ab3a6e6 | 330 | decls = add_field_decl (char_type_node, buffer, &chain); |
ebcbb79c | 331 | |
332 | for (j = 1; j < i; j++) | |
333 | { | |
334 | sprintf (buffer, "c%d", j + 1); | |
6ab3a6e6 | 335 | add_field_decl (char_type_node, buffer, &chain); |
ebcbb79c | 336 | } |
6ab3a6e6 | 337 | objc_finish_struct (type, decls); |
39b8ddc6 | 338 | |
45550790 | 339 | aggregate_in_mem[i] = aggregate_value_p (type, 0); |
ebcbb79c | 340 | if (!aggregate_in_mem[i]) |
341 | found = 1; | |
342 | } | |
39b8ddc6 | 343 | |
ebcbb79c | 344 | /* We found some structures that are returned in registers instead of memory |
3e1e20c1 | 345 | so output the necessary data. */ |
ebcbb79c | 346 | if (found) |
347 | { | |
348 | for (i = 31; i >= 0; i--) | |
349 | if (!aggregate_in_mem[i]) | |
350 | break; | |
86d3c909 | 351 | printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n", i); |
ebcbb79c | 352 | } |
39b8ddc6 | 353 | |
ebcbb79c | 354 | exit (0); |
355 | } | |
356 | ||
03bde601 | 357 | bool |
39b8ddc6 | 358 | objc_init (void) |
fc304191 | 359 | { |
267785bc | 360 | bool ok; |
13dcc150 | 361 | #ifdef OBJCPLUS |
362 | if (cxx_init () == false) | |
363 | #else | |
03bde601 | 364 | if (c_objc_common_init () == false) |
13dcc150 | 365 | #endif |
03bde601 | 366 | return false; |
9ceb1c29 | 367 | |
3c2c63b4 | 368 | /* print_struct_values is triggered by -print-runtime-info (used |
369 | when building libobjc, with an empty file as input). It does not | |
370 | require any ObjC setup, and it never returns. | |
371 | ||
372 | -fcompare-debug is used to check the compiler output; we are | |
373 | executed twice, once with flag_compare_debug set, and once with | |
374 | it not set. If the flag is used together with | |
375 | -print-runtime-info, we want to print the runtime info only once, | |
376 | else it would be output in duplicate. So we check | |
377 | flag_compare_debug to output it in only one of the invocations. | |
378 | ||
379 | As a side effect, this also that means -fcompare-debug | |
380 | -print-runtime-info will run the compiler twice, and compare the | |
381 | generated assembler file; the first time the compiler exits | |
382 | immediately (producing no file), and the second time it compiles | |
383 | an empty file. This checks, as a side effect, that compiling an | |
384 | empty file produces no assembler output. */ | |
267785bc | 385 | if (print_struct_values && !flag_compare_debug) |
267785bc | 386 | generate_struct_by_value_array (); |
fc304191 | 387 | |
267785bc | 388 | /* Set up stuff used by FE parser and all runtimes. */ |
389 | errbuf = XNEWVEC (char, 1024 * 10); | |
f41791cf | 390 | interface_hash_init (); |
267785bc | 391 | hash_init (); |
32ed3798 | 392 | objc_encoding_init (); |
267785bc | 393 | /* ... and then check flags and set-up for the selected runtime ... */ |
394 | if (flag_next_runtime && flag_objc_abi >= 2) | |
395 | ok = objc_next_runtime_abi_02_init (&runtime); | |
396 | else if (flag_next_runtime) | |
397 | ok = objc_next_runtime_abi_01_init (&runtime); | |
398 | else | |
399 | ok = objc_gnu_runtime_abi_01_init (&runtime); | |
ebcbb79c | 400 | |
267785bc | 401 | /* If that part of the setup failed - bail out immediately. */ |
402 | if (!ok) | |
403 | return false; | |
3c2f1b06 | 404 | |
06511efd | 405 | /* Determine the default visibility for instance variables. */ |
406 | switch (default_ivar_visibility) | |
407 | { | |
408 | case IVAR_VISIBILITY_PRIVATE: | |
409 | objc_default_ivar_visibility = OBJC_IVAR_VIS_PRIVATE; | |
410 | break; | |
411 | case IVAR_VISIBILITY_PUBLIC: | |
412 | objc_default_ivar_visibility = OBJC_IVAR_VIS_PUBLIC; | |
413 | break; | |
414 | case IVAR_VISIBILITY_PACKAGE: | |
415 | objc_default_ivar_visibility = OBJC_IVAR_VIS_PACKAGE; | |
416 | break; | |
417 | default: | |
418 | objc_default_ivar_visibility = OBJC_IVAR_VIS_PROTECTED; | |
419 | } | |
420 | ||
267785bc | 421 | /* Generate general types and push runtime-specific decls to file scope. */ |
422 | synth_module_prologue (); | |
33b3681f | 423 | |
03bde601 | 424 | return true; |
bf1846b6 | 425 | } |
426 | ||
3a1c9df2 | 427 | /* This is called at the end of parsing by the C/C++ parsers. */ |
fc304191 | 428 | void |
64cd9619 | 429 | objc_write_global_declarations (void) |
fc304191 | 430 | { |
376c21d1 | 431 | mark_referenced_methods (); |
bf1846b6 | 432 | |
267785bc | 433 | /* A missing @end might not be detected by the parser. */ |
434 | if (objc_implementation_context) | |
435 | { | |
436 | warning (0, "%<@end%> missing in implementation context"); | |
437 | finish_class (objc_implementation_context); | |
438 | objc_ivar_chain = NULL_TREE; | |
439 | objc_implementation_context = NULL_TREE; | |
440 | } | |
441 | ||
442 | if (warn_selector) | |
443 | { | |
f41791cf | 444 | objc_map_iterator_t i; |
267785bc | 445 | |
f41791cf | 446 | objc_map_iterator_initialize (class_method_map, &i); |
447 | while (objc_map_iterator_move_to_next (class_method_map, &i)) | |
448 | check_duplicates (objc_map_iterator_current_value (class_method_map, i), 0, 1); | |
267785bc | 449 | |
f41791cf | 450 | objc_map_iterator_initialize (instance_method_map, &i); |
451 | while (objc_map_iterator_move_to_next (instance_method_map, &i)) | |
452 | check_duplicates (objc_map_iterator_current_value (instance_method_map, i), 0, 0); | |
267785bc | 453 | } |
454 | ||
455 | /* TODO: consider an early exit here if either errorcount or sorrycount | |
456 | is non-zero. Not only is it wasting time to generate the metadata, | |
6bbb4715 | 457 | it needlessly imposes need to re-check for things that are already |
267785bc | 458 | determined to be errors. */ |
fc304191 | 459 | |
267785bc | 460 | /* Finalize Objective-C runtime data. No need to generate tables |
461 | and code if only checking syntax, or if generating a PCH file. */ | |
462 | if (!flag_syntax_only && !pch_file) | |
463 | { | |
f6593d66 | 464 | location_t saved_location; |
465 | ||
267785bc | 466 | /* If gen_declaration desired, open the output file. */ |
467 | if (flag_gen_declaration) | |
468 | { | |
469 | char * const dumpname = concat (dump_base_name, ".decl", NULL); | |
470 | gen_declaration_file = fopen (dumpname, "w"); | |
471 | if (gen_declaration_file == 0) | |
c05be867 | 472 | fatal_error (input_location, "can%'t open %s: %m", dumpname); |
267785bc | 473 | free (dumpname); |
474 | } | |
f6593d66 | 475 | |
476 | /* Set the input location to BUILTINS_LOCATION. This is good | |
477 | for error messages, in case any is generated while producing | |
478 | the metadata, but it also silences warnings that would be | |
479 | produced when compiling with -Wpadded in case when padding is | |
480 | automatically added to the built-in runtime data structure | |
481 | declarations. We know about this padding, and it is fine; we | |
482 | don't want users to see any warnings about it if they use | |
483 | -Wpadded. */ | |
484 | saved_location = input_location; | |
485 | input_location = BUILTINS_LOCATION; | |
486 | ||
267785bc | 487 | /* Compute and emit the meta-data tables for this runtime. */ |
488 | (*runtime.generate_metadata) (); | |
f6593d66 | 489 | |
490 | /* Restore the original location, just in case it mattered. */ | |
491 | input_location = saved_location; | |
492 | ||
267785bc | 493 | /* ... and then close any declaration file we opened. */ |
494 | if (gen_declaration_file) | |
495 | fclose (gen_declaration_file); | |
496 | } | |
fc304191 | 497 | } |
267785bc | 498 | |
e5876878 | 499 | /* Return the first occurrence of a method declaration corresponding |
500 | to sel_name in rproto_list. Search rproto_list recursively. | |
501 | If is_class is 0, search for instance methods, otherwise for class | |
502 | methods. */ | |
c450c529 | 503 | static tree |
39b8ddc6 | 504 | lookup_method_in_protocol_list (tree rproto_list, tree sel_name, |
e5876878 | 505 | int is_class) |
c450c529 | 506 | { |
53cd8676 | 507 | tree rproto, p, m; |
c450c529 | 508 | |
509 | for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto)) | |
510 | { | |
53cd8676 | 511 | p = TREE_VALUE (rproto); |
512 | m = NULL_TREE; | |
c450c529 | 513 | |
514 | if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE) | |
515 | { | |
53cd8676 | 516 | /* First, search the @required protocol methods. */ |
517 | if (is_class) | |
518 | m = lookup_method (PROTOCOL_CLS_METHODS (p), sel_name); | |
519 | else | |
520 | m = lookup_method (PROTOCOL_NST_METHODS (p), sel_name); | |
521 | ||
522 | if (m) | |
523 | return m; | |
524 | ||
525 | /* If still not found, search the @optional protocol methods. */ | |
526 | if (is_class) | |
527 | m = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (p), sel_name); | |
528 | else | |
529 | m = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (p), sel_name); | |
530 | ||
531 | if (m) | |
532 | return m; | |
533 | ||
534 | /* If still not found, search the attached protocols. */ | |
535 | if (PROTOCOL_LIST (p)) | |
536 | m = lookup_method_in_protocol_list (PROTOCOL_LIST (p), | |
537 | sel_name, is_class); | |
538 | if (m) | |
539 | return m; | |
c450c529 | 540 | } |
541 | else | |
7d27e4c9 | 542 | { |
543 | ; /* An identifier...if we could not find a protocol. */ | |
544 | } | |
c450c529 | 545 | } |
e0ece38e | 546 | |
c450c529 | 547 | return 0; |
548 | } | |
549 | ||
550 | static tree | |
39b8ddc6 | 551 | lookup_protocol_in_reflist (tree rproto_list, tree lproto) |
c450c529 | 552 | { |
3e1e20c1 | 553 | tree rproto, p; |
c450c529 | 554 | |
3e1e20c1 | 555 | /* Make sure the protocol is supported by the object on the rhs. */ |
556 | if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE) | |
557 | { | |
558 | tree fnd = 0; | |
559 | for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto)) | |
560 | { | |
561 | p = TREE_VALUE (rproto); | |
c450c529 | 562 | |
3e1e20c1 | 563 | if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE) |
564 | { | |
565 | if (lproto == p) | |
566 | fnd = lproto; | |
c450c529 | 567 | |
3e1e20c1 | 568 | else if (PROTOCOL_LIST (p)) |
569 | fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto); | |
570 | } | |
c450c529 | 571 | |
3e1e20c1 | 572 | if (fnd) |
573 | return fnd; | |
574 | } | |
575 | } | |
576 | else | |
577 | { | |
578 | ; /* An identifier...if we could not find a protocol. */ | |
579 | } | |
c450c529 | 580 | |
3e1e20c1 | 581 | return 0; |
fc304191 | 582 | } |
583 | ||
13dcc150 | 584 | void |
a336eb4b | 585 | objc_start_class_interface (tree klass, tree super_class, |
586 | tree protos, tree attributes) | |
13dcc150 | 587 | { |
c1cfb0f5 | 588 | if (flag_objc1_only && attributes) |
6bbb4715 | 589 | error_at (input_location, "class attributes are not available in Objective-C 1.0"); |
c1cfb0f5 | 590 | |
13dcc150 | 591 | objc_interface_context |
592 | = objc_ivar_context | |
c1cfb0f5 | 593 | = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos, attributes); |
06511efd | 594 | objc_ivar_visibility = objc_default_ivar_visibility; |
13dcc150 | 595 | } |
596 | ||
597 | void | |
a336eb4b | 598 | objc_start_category_interface (tree klass, tree categ, |
599 | tree protos, tree attributes) | |
13dcc150 | 600 | { |
a336eb4b | 601 | if (attributes) |
1ef143b6 | 602 | { |
603 | if (flag_objc1_only) | |
604 | error_at (input_location, "category attributes are not available in Objective-C 1.0"); | |
605 | else | |
6bbb4715 | 606 | warning_at (input_location, OPT_Wattributes, |
1ef143b6 | 607 | "category attributes are not available in this version" |
608 | " of the compiler, (ignored)"); | |
609 | } | |
e16610d0 | 610 | if (categ == NULL_TREE) |
611 | { | |
612 | if (flag_objc1_only) | |
613 | error_at (input_location, "class extensions are not available in Objective-C 1.0"); | |
598d5210 | 614 | else |
615 | { | |
616 | /* Iterate over all the classes and categories implemented | |
617 | up to now in this compilation unit. */ | |
618 | struct imp_entry *t; | |
619 | ||
620 | for (t = imp_list; t; t = t->next) | |
621 | { | |
622 | /* If we find a class @implementation with the same name | |
623 | as the one we are extending, produce an error. */ | |
624 | if (TREE_CODE (t->imp_context) == CLASS_IMPLEMENTATION_TYPE | |
625 | && IDENTIFIER_POINTER (CLASS_NAME (t->imp_context)) == IDENTIFIER_POINTER (klass)) | |
6bbb4715 | 626 | error_at (input_location, |
598d5210 | 627 | "class extension for class %qE declared after its %<@implementation%>", |
628 | klass); | |
629 | } | |
630 | } | |
e16610d0 | 631 | } |
13dcc150 | 632 | objc_interface_context |
c1cfb0f5 | 633 | = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos, NULL_TREE); |
13dcc150 | 634 | objc_ivar_chain |
635 | = continue_class (objc_interface_context); | |
636 | } | |
637 | ||
638 | void | |
a336eb4b | 639 | objc_start_protocol (tree name, tree protos, tree attributes) |
13dcc150 | 640 | { |
c213e196 | 641 | if (flag_objc1_only && attributes) |
6bbb4715 | 642 | error_at (input_location, "protocol attributes are not available in Objective-C 1.0"); |
c213e196 | 643 | |
13dcc150 | 644 | objc_interface_context |
c213e196 | 645 | = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos, attributes); |
069761fb | 646 | objc_method_optional_flag = false; |
13dcc150 | 647 | } |
648 | ||
649 | void | |
650 | objc_continue_interface (void) | |
651 | { | |
652 | objc_ivar_chain | |
653 | = continue_class (objc_interface_context); | |
654 | } | |
655 | ||
656 | void | |
657 | objc_finish_interface (void) | |
658 | { | |
659 | finish_class (objc_interface_context); | |
660 | objc_interface_context = NULL_TREE; | |
069761fb | 661 | objc_method_optional_flag = false; |
e16610d0 | 662 | objc_in_class_extension = false; |
13dcc150 | 663 | } |
664 | ||
665 | void | |
ceb0f408 | 666 | objc_start_class_implementation (tree klass, tree super_class) |
13dcc150 | 667 | { |
668 | objc_implementation_context | |
669 | = objc_ivar_context | |
c1cfb0f5 | 670 | = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE, |
671 | NULL_TREE); | |
06511efd | 672 | objc_ivar_visibility = objc_default_ivar_visibility; |
13dcc150 | 673 | } |
674 | ||
675 | void | |
ceb0f408 | 676 | objc_start_category_implementation (tree klass, tree categ) |
13dcc150 | 677 | { |
678 | objc_implementation_context | |
c1cfb0f5 | 679 | = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE, |
680 | NULL_TREE); | |
13dcc150 | 681 | objc_ivar_chain |
682 | = continue_class (objc_implementation_context); | |
683 | } | |
684 | ||
685 | void | |
686 | objc_continue_implementation (void) | |
687 | { | |
688 | objc_ivar_chain | |
689 | = continue_class (objc_implementation_context); | |
690 | } | |
691 | ||
692 | void | |
693 | objc_finish_implementation (void) | |
694 | { | |
0ced0389 | 695 | #ifdef OBJCPLUS |
696 | if (flag_objc_call_cxx_cdtors) | |
697 | objc_generate_cxx_cdtors (); | |
698 | #endif | |
699 | ||
13dcc150 | 700 | if (objc_implementation_context) |
701 | { | |
702 | finish_class (objc_implementation_context); | |
703 | objc_ivar_chain = NULL_TREE; | |
704 | objc_implementation_context = NULL_TREE; | |
705 | } | |
706 | else | |
c3ceba8e | 707 | warning (0, "%<@end%> must appear in an @implementation context"); |
13dcc150 | 708 | } |
709 | ||
710 | void | |
4a8875ed | 711 | objc_set_visibility (objc_ivar_visibility_kind visibility) |
13dcc150 | 712 | { |
4a8875ed | 713 | if (visibility == OBJC_IVAR_VIS_PACKAGE) |
1ef143b6 | 714 | { |
715 | if (flag_objc1_only) | |
716 | error ("%<@package%> is not available in Objective-C 1.0"); | |
717 | else | |
718 | warning (0, "%<@package%> presently has the same effect as %<@public%>"); | |
719 | } | |
4a8875ed | 720 | objc_ivar_visibility = visibility; |
13dcc150 | 721 | } |
722 | ||
069761fb | 723 | void |
724 | objc_set_method_opt (bool optional) | |
725 | { | |
1ef143b6 | 726 | if (flag_objc1_only) |
d2389fe7 | 727 | { |
728 | if (optional) | |
6bbb4715 | 729 | error_at (input_location, "%<@optional%> is not available in Objective-C 1.0"); |
d2389fe7 | 730 | else |
6bbb4715 | 731 | error_at (input_location, "%<@required%> is not available in Objective-C 1.0"); |
d2389fe7 | 732 | } |
1ef143b6 | 733 | |
069761fb | 734 | objc_method_optional_flag = optional; |
6bbb4715 | 735 | if (!objc_interface_context |
069761fb | 736 | || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE) |
737 | { | |
d2389fe7 | 738 | if (optional) |
739 | error ("%<@optional%> is allowed in @protocol context only"); | |
740 | else | |
741 | error ("%<@required%> is allowed in @protocol context only"); | |
069761fb | 742 | objc_method_optional_flag = false; |
743 | } | |
744 | } | |
745 | ||
7f5203a8 | 746 | /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or |
747 | PROTOCOL. */ | |
748 | static tree | |
749 | lookup_property_in_list (tree chain, tree property) | |
750 | { | |
751 | tree x; | |
752 | for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x)) | |
753 | if (PROPERTY_NAME (x) == property) | |
754 | return x; | |
755 | return NULL_TREE; | |
756 | } | |
757 | ||
758 | /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */ | |
759 | static tree lookup_property_in_protocol_list (tree rproto_list, tree property) | |
760 | { | |
761 | tree rproto, x; | |
762 | for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto)) | |
763 | { | |
764 | tree p = TREE_VALUE (rproto); | |
765 | if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE) | |
766 | { | |
767 | if ((x = lookup_property_in_list (p, property))) | |
768 | return x; | |
769 | if (PROTOCOL_LIST (p)) | |
770 | return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property); | |
771 | } | |
772 | else | |
773 | { | |
774 | ; /* An identifier...if we could not find a protocol. */ | |
775 | } | |
776 | } | |
777 | return NULL_TREE; | |
778 | } | |
779 | ||
780 | /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the | |
781 | chain of interface hierarchy. */ | |
782 | static tree | |
783 | lookup_property (tree interface_type, tree property) | |
784 | { | |
785 | tree inter = interface_type; | |
786 | while (inter) | |
787 | { | |
788 | tree x, category; | |
789 | if ((x = lookup_property_in_list (inter, property))) | |
790 | return x; | |
791 | /* Failing that, look for the property in each category of the class. */ | |
792 | category = inter; | |
793 | while ((category = CLASS_CATEGORY_LIST (category))) | |
794 | { | |
795 | if ((x = lookup_property_in_list (category, property))) | |
796 | return x; | |
797 | ||
798 | /* When checking a category, also check the protocols | |
799 | attached with the category itself. */ | |
800 | if (CLASS_PROTOCOL_LIST (category) | |
801 | && (x = lookup_property_in_protocol_list | |
802 | (CLASS_PROTOCOL_LIST (category), property))) | |
803 | return x; | |
804 | } | |
805 | ||
806 | /* Failing to find in categories, look for property in protocol list. */ | |
6bbb4715 | 807 | if (CLASS_PROTOCOL_LIST (inter) |
7f5203a8 | 808 | && (x = lookup_property_in_protocol_list |
809 | (CLASS_PROTOCOL_LIST (inter), property))) | |
810 | return x; | |
6bbb4715 | 811 | |
7f5203a8 | 812 | /* Failing that, climb up the inheritance hierarchy. */ |
813 | inter = lookup_interface (CLASS_SUPER_NAME (inter)); | |
814 | } | |
815 | return inter; | |
816 | } | |
817 | ||
7590f0e5 | 818 | /* This routine is called by the parser when a |
819 | @property... declaration is found. 'decl' is the declaration of | |
820 | the property (type/identifier), and the other arguments represent | |
821 | property attributes that may have been specified in the Objective-C | |
822 | declaration. 'parsed_property_readonly' is 'true' if the attribute | |
823 | 'readonly' was specified, and 'false' if not; similarly for the | |
824 | other bool parameters. 'parsed_property_getter_ident' is NULL_TREE | |
825 | if the attribute 'getter' was not specified, and is the identifier | |
826 | corresponding to the specified getter if it was; similarly for | |
827 | 'parsed_property_setter_ident'. */ | |
86c110ac | 828 | void |
7590f0e5 | 829 | objc_add_property_declaration (location_t location, tree decl, |
830 | bool parsed_property_readonly, bool parsed_property_readwrite, | |
831 | bool parsed_property_assign, bool parsed_property_retain, | |
832 | bool parsed_property_copy, bool parsed_property_nonatomic, | |
9d9f5bb3 | 833 | tree parsed_property_getter_ident, tree parsed_property_setter_ident) |
86c110ac | 834 | { |
7590f0e5 | 835 | tree property_decl; |
836 | tree x; | |
9d9f5bb3 | 837 | /* 'property_readonly' and 'property_assign_semantics' are the final |
838 | attributes of the property after all parsed attributes have been | |
839 | considered (eg, if we parsed no 'readonly' and no 'readwrite', ie | |
840 | parsed_property_readonly = false and parsed_property_readwrite = | |
841 | false, then property_readonly will be false because the default | |
842 | is readwrite). */ | |
7590f0e5 | 843 | bool property_readonly = false; |
9d9f5bb3 | 844 | objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN; |
e16610d0 | 845 | bool property_extension_in_class_extension = false; |
7590f0e5 | 846 | |
1ef143b6 | 847 | if (flag_objc1_only) |
848 | error_at (input_location, "%<@property%> is not available in Objective-C 1.0"); | |
849 | ||
7590f0e5 | 850 | if (parsed_property_readonly && parsed_property_readwrite) |
86c110ac | 851 | { |
7590f0e5 | 852 | error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute"); |
853 | /* In case of conflicting attributes (here and below), after | |
854 | producing an error, we pick one of the attributes and keep | |
855 | going. */ | |
856 | property_readonly = false; | |
857 | } | |
858 | else | |
859 | { | |
860 | if (parsed_property_readonly) | |
86c110ac | 861 | property_readonly = true; |
6bbb4715 | 862 | |
7590f0e5 | 863 | if (parsed_property_readwrite) |
864 | property_readonly = false; | |
86c110ac | 865 | } |
86c110ac | 866 | |
7590f0e5 | 867 | if (parsed_property_readonly && parsed_property_setter_ident) |
868 | { | |
25bf0383 | 869 | error_at (location, "%<readonly%> attribute conflicts with %<setter%> attribute"); |
9d9f5bb3 | 870 | property_readonly = false; |
7590f0e5 | 871 | } |
86c110ac | 872 | |
7590f0e5 | 873 | if (parsed_property_assign && parsed_property_retain) |
874 | { | |
875 | error_at (location, "%<assign%> attribute conflicts with %<retain%> attribute"); | |
876 | property_assign_semantics = OBJC_PROPERTY_RETAIN; | |
877 | } | |
878 | else if (parsed_property_assign && parsed_property_copy) | |
879 | { | |
880 | error_at (location, "%<assign%> attribute conflicts with %<copy%> attribute"); | |
881 | property_assign_semantics = OBJC_PROPERTY_COPY; | |
882 | } | |
883 | else if (parsed_property_retain && parsed_property_copy) | |
884 | { | |
885 | error_at (location, "%<retain%> attribute conflicts with %<copy%> attribute"); | |
886 | property_assign_semantics = OBJC_PROPERTY_COPY; | |
887 | } | |
888 | else | |
889 | { | |
890 | if (parsed_property_assign) | |
891 | property_assign_semantics = OBJC_PROPERTY_ASSIGN; | |
86c110ac | 892 | |
7590f0e5 | 893 | if (parsed_property_retain) |
894 | property_assign_semantics = OBJC_PROPERTY_RETAIN; | |
895 | ||
896 | if (parsed_property_copy) | |
897 | property_assign_semantics = OBJC_PROPERTY_COPY; | |
898 | } | |
899 | ||
9d9f5bb3 | 900 | if (!objc_interface_context) |
86c110ac | 901 | { |
9d9f5bb3 | 902 | error_at (location, "property declaration not in @interface or @protocol context"); |
86c110ac | 903 | return; |
904 | } | |
905 | ||
9d9f5bb3 | 906 | /* At this point we know that we are either in an interface, a |
907 | category, or a protocol. */ | |
908 | ||
25bf0383 | 909 | /* We expect a FIELD_DECL from the parser. Make sure we didn't get |
910 | something else, as that would confuse the checks below. */ | |
911 | if (TREE_CODE (decl) != FIELD_DECL) | |
912 | { | |
913 | error_at (location, "invalid property declaration"); | |
6bbb4715 | 914 | return; |
25bf0383 | 915 | } |
916 | ||
917 | /* Do some spot-checks for the most obvious invalid types. */ | |
918 | ||
919 | if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) | |
9d9f5bb3 | 920 | { |
25bf0383 | 921 | error_at (location, "property can not be an array"); |
9d9f5bb3 | 922 | return; |
923 | } | |
86c110ac | 924 | |
25bf0383 | 925 | /* The C++/ObjC++ parser seems to reject the ':' for a bitfield when |
926 | parsing, while the C/ObjC parser accepts it and gives us a | |
927 | FIELD_DECL with a DECL_INITIAL set. So we use the DECL_INITIAL | |
928 | to check for a bitfield when doing ObjC. */ | |
929 | #ifndef OBJCPLUS | |
930 | if (DECL_INITIAL (decl)) | |
931 | { | |
932 | /* A @property is not an actual variable, but it is a way to | |
933 | describe a pair of accessor methods, so its type (which is | |
934 | the type of the return value of the getter and the first | |
935 | argument of the setter) can't be a bitfield (as return values | |
936 | and arguments of functions can not be bitfields). The | |
937 | underlying instance variable could be a bitfield, but that is | |
938 | a different matter. */ | |
939 | error_at (location, "property can not be a bit-field"); | |
6bbb4715 | 940 | return; |
25bf0383 | 941 | } |
942 | #endif | |
943 | ||
944 | /* TODO: Check that the property type is an Objective-C object or a | |
945 | "POD". */ | |
9d9f5bb3 | 946 | |
f26877d5 | 947 | /* Implement -Wproperty-assign-default (which is enabled by default). */ |
948 | if (warn_property_assign_default | |
9d9f5bb3 | 949 | /* If garbage collection is not being used, then 'assign' is |
950 | valid for objects (and typically used for delegates) but it | |
951 | is wrong in most cases (since most objects need to be | |
952 | retained or copied in setters). Warn users when 'assign' is | |
953 | used implicitly. */ | |
f26877d5 | 954 | && property_assign_semantics == OBJC_PROPERTY_ASSIGN |
955 | /* Read-only properties are never assigned, so the assignment | |
956 | semantics do not matter in that case. */ | |
957 | && !property_readonly | |
958 | && !flag_objc_gc) | |
959 | { | |
9d9f5bb3 | 960 | /* Please note that it would make sense to default to 'assign' |
961 | for non-{Objective-C objects}, and to 'retain' for | |
962 | Objective-C objects. But that would break compatibility with | |
963 | other compilers. */ | |
f26877d5 | 964 | if (!parsed_property_assign && !parsed_property_retain && !parsed_property_copy) |
86c110ac | 965 | { |
f26877d5 | 966 | /* Use 'false' so we do not warn for Class objects. */ |
967 | if (objc_type_valid_for_messaging (TREE_TYPE (decl), false)) | |
86c110ac | 968 | { |
6bbb4715 | 969 | warning_at (location, |
f26877d5 | 970 | 0, |
6bbb4715 | 971 | "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>", |
f26877d5 | 972 | decl); |
6bbb4715 | 973 | inform (location, |
f26877d5 | 974 | "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it"); |
86c110ac | 975 | } |
976 | } | |
86c110ac | 977 | } |
6bbb4715 | 978 | |
9d9f5bb3 | 979 | if (property_assign_semantics == OBJC_PROPERTY_RETAIN |
f26877d5 | 980 | && !objc_type_valid_for_messaging (TREE_TYPE (decl), true)) |
9d9f5bb3 | 981 | error_at (location, "%<retain%> attribute is only valid for Objective-C objects"); |
6bbb4715 | 982 | |
9d9f5bb3 | 983 | if (property_assign_semantics == OBJC_PROPERTY_COPY |
f26877d5 | 984 | && !objc_type_valid_for_messaging (TREE_TYPE (decl), true)) |
9d9f5bb3 | 985 | error_at (location, "%<copy%> attribute is only valid for Objective-C objects"); |
7590f0e5 | 986 | |
7f5203a8 | 987 | /* Now determine the final property getter and setter names. They |
988 | will be stored in the PROPERTY_DECL, from which they'll always be | |
989 | extracted and used. */ | |
990 | ||
991 | /* Adjust, or fill in, setter and getter names. We overwrite the | |
992 | parsed_property_setter_ident and parsed_property_getter_ident | |
993 | with the final setter and getter identifiers that will be | |
994 | used. */ | |
995 | if (parsed_property_setter_ident) | |
996 | { | |
997 | /* The setter should be terminated by ':', but the parser only | |
998 | gives us an identifier without ':'. So, we need to add ':' | |
999 | at the end. */ | |
1000 | const char *parsed_setter = IDENTIFIER_POINTER (parsed_property_setter_ident); | |
1001 | size_t length = strlen (parsed_setter); | |
1002 | char *final_setter = (char *)alloca (length + 2); | |
1003 | ||
1004 | sprintf (final_setter, "%s:", parsed_setter); | |
1005 | parsed_property_setter_ident = get_identifier (final_setter); | |
1006 | } | |
1007 | else | |
1008 | { | |
1009 | if (!property_readonly) | |
6bbb4715 | 1010 | parsed_property_setter_ident = get_identifier (objc_build_property_setter_name |
7f5203a8 | 1011 | (DECL_NAME (decl))); |
1012 | } | |
1013 | ||
1014 | if (!parsed_property_getter_ident) | |
1015 | parsed_property_getter_ident = DECL_NAME (decl); | |
1016 | ||
9d9f5bb3 | 1017 | /* Check for duplicate property declarations. We first check the |
7f5203a8 | 1018 | immediate context for a property with the same name. Any such |
e16610d0 | 1019 | declarations are an error, unless this is a class extension and |
1020 | we are extending a property from readonly to readwrite. */ | |
9d9f5bb3 | 1021 | for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x)) |
1022 | { | |
1023 | if (PROPERTY_NAME (x) == DECL_NAME (decl)) | |
86c110ac | 1024 | { |
e16610d0 | 1025 | if (objc_in_class_extension |
1026 | && property_readonly == 0 | |
1027 | && PROPERTY_READONLY (x) == 1) | |
1028 | { | |
1029 | /* This is a class extension, and we are extending an | |
1030 | existing readonly property to a readwrite one. | |
1031 | That's fine. :-) */ | |
1032 | property_extension_in_class_extension = true; | |
1033 | break; | |
1034 | } | |
1035 | else | |
1036 | { | |
1037 | location_t original_location = DECL_SOURCE_LOCATION (x); | |
6bbb4715 | 1038 | |
e16610d0 | 1039 | error_at (location, "redeclaration of property %qD", decl); |
6bbb4715 | 1040 | |
e16610d0 | 1041 | if (original_location != UNKNOWN_LOCATION) |
1042 | inform (original_location, "originally specified here"); | |
1043 | return; | |
1044 | } | |
1045 | } | |
86c110ac | 1046 | } |
9d9f5bb3 | 1047 | |
e16610d0 | 1048 | /* If x is not NULL_TREE, we must be in a class extension and we're |
1049 | extending a readonly property. In that case, no point in | |
1050 | searching for another declaration. */ | |
1051 | if (x == NULL_TREE) | |
1052 | { | |
1053 | /* We now need to check for existing property declarations (in | |
1054 | the superclass, other categories or protocols) and check that | |
1055 | the new declaration is not in conflict with existing | |
1056 | ones. */ | |
7f5203a8 | 1057 | |
e16610d0 | 1058 | /* Search for a previous, existing declaration of a property |
1059 | with the same name in superclasses, protocols etc. If one is | |
1060 | found, it will be in the 'x' variable. */ | |
7f5203a8 | 1061 | |
e16610d0 | 1062 | /* Note that, for simplicity, the following may search again the |
1063 | local context. That's Ok as nothing will be found (else we'd | |
1064 | have thrown an error above); it's only a little inefficient, | |
1065 | but the code is simpler. */ | |
1066 | switch (TREE_CODE (objc_interface_context)) | |
7f5203a8 | 1067 | { |
e16610d0 | 1068 | case CLASS_INTERFACE_TYPE: |
1069 | /* Look up the property in the current @interface (which | |
1070 | will find nothing), then its protocols and categories and | |
1071 | superclasses. */ | |
1072 | x = lookup_property (objc_interface_context, DECL_NAME (decl)); | |
1073 | break; | |
1074 | case CATEGORY_INTERFACE_TYPE: | |
1075 | /* Look up the property in the main @interface, then | |
1076 | protocols and categories (one of them is ours, and will | |
1077 | find nothing) and superclasses. */ | |
1078 | x = lookup_property (lookup_interface (CLASS_NAME (objc_interface_context)), | |
1079 | DECL_NAME (decl)); | |
1080 | break; | |
1081 | case PROTOCOL_INTERFACE_TYPE: | |
1082 | /* Looks up the property in any protocols attached to the | |
1083 | current protocol. */ | |
1084 | if (PROTOCOL_LIST (objc_interface_context)) | |
1085 | { | |
1086 | x = lookup_property_in_protocol_list (PROTOCOL_LIST (objc_interface_context), | |
1087 | DECL_NAME (decl)); | |
1088 | } | |
1089 | break; | |
1090 | default: | |
1091 | gcc_unreachable (); | |
7f5203a8 | 1092 | } |
7f5203a8 | 1093 | } |
1094 | ||
1095 | if (x != NULL_TREE) | |
1096 | { | |
1097 | /* An existing property was found; check that it has the same | |
1098 | types, or it is compatible. */ | |
1099 | location_t original_location = DECL_SOURCE_LOCATION (x); | |
db496216 | 1100 | |
7f5203a8 | 1101 | if (PROPERTY_NONATOMIC (x) != parsed_property_nonatomic) |
1102 | { | |
25bf0383 | 1103 | warning_at (location, 0, |
1104 | "'nonatomic' attribute of property %qD conflicts with previous declaration", decl); | |
6bbb4715 | 1105 | |
7f5203a8 | 1106 | if (original_location != UNKNOWN_LOCATION) |
1107 | inform (original_location, "originally specified here"); | |
1108 | return; | |
1109 | } | |
1110 | ||
1111 | if (PROPERTY_GETTER_NAME (x) != parsed_property_getter_ident) | |
1112 | { | |
25bf0383 | 1113 | warning_at (location, 0, |
1114 | "'getter' attribute of property %qD conflicts with previous declaration", decl); | |
6bbb4715 | 1115 | |
7f5203a8 | 1116 | if (original_location != UNKNOWN_LOCATION) |
1117 | inform (original_location, "originally specified here"); | |
1118 | return; | |
1119 | } | |
1120 | ||
1121 | /* We can only compare the setter names if both the old and new property have a setter. */ | |
1122 | if (!property_readonly && !PROPERTY_READONLY(x)) | |
1123 | { | |
1124 | if (PROPERTY_SETTER_NAME (x) != parsed_property_setter_ident) | |
1125 | { | |
25bf0383 | 1126 | warning_at (location, 0, |
1127 | "'setter' attribute of property %qD conflicts with previous declaration", decl); | |
6bbb4715 | 1128 | |
7f5203a8 | 1129 | if (original_location != UNKNOWN_LOCATION) |
1130 | inform (original_location, "originally specified here"); | |
1131 | return; | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | if (PROPERTY_ASSIGN_SEMANTICS (x) != property_assign_semantics) | |
1136 | { | |
25bf0383 | 1137 | warning_at (location, 0, |
1138 | "assign semantics attributes of property %qD conflict with previous declaration", decl); | |
6bbb4715 | 1139 | |
7f5203a8 | 1140 | if (original_location != UNKNOWN_LOCATION) |
1141 | inform (original_location, "originally specified here"); | |
1142 | return; | |
1143 | } | |
1144 | ||
1145 | /* It's ok to have a readonly property that becomes a readwrite, but not vice versa. */ | |
1146 | if (PROPERTY_READONLY (x) == 0 && property_readonly == 1) | |
1147 | { | |
25bf0383 | 1148 | warning_at (location, 0, |
1149 | "'readonly' attribute of property %qD conflicts with previous declaration", decl); | |
6bbb4715 | 1150 | |
7f5203a8 | 1151 | if (original_location != UNKNOWN_LOCATION) |
1152 | inform (original_location, "originally specified here"); | |
1153 | return; | |
1154 | } | |
1155 | ||
ad312852 | 1156 | /* We now check that the new and old property declarations have |
1157 | the same types (or compatible one). In the Objective-C | |
1158 | tradition of loose type checking, we do type-checking but | |
1159 | only generate warnings (not errors) if they do not match. | |
1160 | For non-readonly properties, the types must match exactly; | |
1161 | for readonly properties, it is allowed to use a "more | |
1162 | specialized" type in the new property declaration. Eg, the | |
1163 | superclass has a getter returning (NSArray *) and the | |
1164 | subclass a getter returning (NSMutableArray *). The object's | |
1165 | getter returns an (NSMutableArray *); but if you cast the | |
1166 | object to the superclass, which is allowed, you'd still | |
1167 | expect the getter to return an (NSArray *), which works since | |
1168 | an (NSMutableArray *) is an (NSArray *) too. So, the set of | |
1169 | objects belonging to the type of the new @property should be | |
1170 | a subset of the set of objects belonging to the type of the | |
1171 | old @property. This is what "specialization" means. And the | |
1172 | reason it only applies to readonly properties is that for a | |
1173 | readwrite property the setter would have the opposite | |
1174 | requirement - ie that the superclass type is more specialized | |
1175 | then the subclass one; hence the only way to satisfy both | |
1176 | constraints is that the types match. */ | |
1177 | ||
1178 | /* If the types are not the same in the C sense, we warn ... */ | |
1179 | if (!comptypes (TREE_TYPE (x), TREE_TYPE (decl)) | |
1180 | /* ... unless the property is readonly, in which case we | |
1181 | allow a new, more specialized, declaration. */ | |
6bbb4715 | 1182 | && (!property_readonly |
ad312852 | 1183 | || !objc_compare_types (TREE_TYPE (x), |
1184 | TREE_TYPE (decl), -5, NULL_TREE))) | |
7f5203a8 | 1185 | { |
ad312852 | 1186 | warning_at (location, 0, |
1187 | "type of property %qD conflicts with previous declaration", decl); | |
1188 | if (original_location != UNKNOWN_LOCATION) | |
1189 | inform (original_location, "originally specified here"); | |
1190 | return; | |
7f5203a8 | 1191 | } |
e16610d0 | 1192 | |
1193 | /* If we are in a class extension and we're extending a readonly | |
1194 | property in the main @interface, we'll just update the | |
1195 | existing property with the readwrite flag and potentially the | |
1196 | new setter name. */ | |
1197 | if (property_extension_in_class_extension) | |
1198 | { | |
1199 | PROPERTY_READONLY (x) = 0; | |
1200 | PROPERTY_SETTER_NAME (x) = parsed_property_setter_ident; | |
1201 | return; | |
1202 | } | |
7f5203a8 | 1203 | } |
9d9f5bb3 | 1204 | |
1205 | /* Create a PROPERTY_DECL node. */ | |
1206 | property_decl = make_node (PROPERTY_DECL); | |
1207 | ||
1208 | /* Copy the basic information from the original decl. */ | |
1209 | TREE_TYPE (property_decl) = TREE_TYPE (decl); | |
1210 | DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl); | |
1211 | TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl); | |
6bbb4715 | 1212 | |
9d9f5bb3 | 1213 | /* Add property-specific information. */ |
1214 | PROPERTY_NAME (property_decl) = DECL_NAME (decl); | |
1215 | PROPERTY_GETTER_NAME (property_decl) = parsed_property_getter_ident; | |
1216 | PROPERTY_SETTER_NAME (property_decl) = parsed_property_setter_ident; | |
1217 | PROPERTY_READONLY (property_decl) = property_readonly; | |
1218 | PROPERTY_NONATOMIC (property_decl) = parsed_property_nonatomic; | |
1219 | PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics; | |
1220 | PROPERTY_IVAR_NAME (property_decl) = NULL_TREE; | |
1221 | PROPERTY_DYNAMIC (property_decl) = 0; | |
1222 | ||
db496216 | 1223 | /* Remember the fact that the property was found in the @optional |
1224 | section in a @protocol, or not. */ | |
1225 | if (objc_method_optional_flag) | |
1226 | PROPERTY_OPTIONAL (property_decl) = 1; | |
1227 | else | |
1228 | PROPERTY_OPTIONAL (property_decl) = 0; | |
1229 | ||
7f5203a8 | 1230 | /* Note that PROPERTY_GETTER_NAME is always set for all |
1231 | PROPERTY_DECLs, and PROPERTY_SETTER_NAME is always set for all | |
1232 | PROPERTY_DECLs where PROPERTY_READONLY == 0. Any time we deal | |
1233 | with a getter or setter, we should get the PROPERTY_DECL and use | |
1234 | PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know the correct | |
1235 | names. */ | |
1236 | ||
9d9f5bb3 | 1237 | /* Add the PROPERTY_DECL to the list of properties for the class. */ |
1238 | TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context); | |
1239 | CLASS_PROPERTY_DECL (objc_interface_context) = property_decl; | |
86c110ac | 1240 | } |
1241 | ||
890881e7 | 1242 | /* This is a subroutine of objc_maybe_build_component_ref. Search the |
736dea43 | 1243 | list of methods in the interface (and, failing that, the local list |
1244 | in the implementation, and failing that, the protocol list) | |
890881e7 | 1245 | provided for a 'setter' or 'getter' for 'component' with default |
1246 | names (ie, if 'component' is "name", then search for "name" and | |
db496216 | 1247 | "setName:"). It is also possible to specify a different |
1248 | 'getter_name' (this is used for @optional readonly properties). If | |
1249 | any is found, then create an artificial property that uses them. | |
1250 | Return NULL_TREE if 'getter' or 'setter' could not be found. */ | |
890881e7 | 1251 | static tree |
6bbb4715 | 1252 | maybe_make_artificial_property_decl (tree interface, tree implementation, |
db496216 | 1253 | tree protocol_list, tree component, bool is_class, |
1254 | tree getter_name) | |
890881e7 | 1255 | { |
890881e7 | 1256 | tree setter_name = get_identifier (objc_build_property_setter_name (component)); |
1257 | tree getter = NULL_TREE; | |
1258 | tree setter = NULL_TREE; | |
1259 | ||
db496216 | 1260 | if (getter_name == NULL_TREE) |
1261 | getter_name = component; | |
1262 | ||
736dea43 | 1263 | /* First, check the @interface and all superclasses. */ |
890881e7 | 1264 | if (interface) |
1265 | { | |
1266 | int flags = 0; | |
1267 | ||
736dea43 | 1268 | /* Using instance methods of the root class as accessors is most |
1269 | likely unwanted and can be extremely confusing (and, most | |
1270 | importantly, other Objective-C 2.0 compilers do not do it). | |
1271 | Turn it off. */ | |
890881e7 | 1272 | if (is_class) |
736dea43 | 1273 | flags = OBJC_LOOKUP_CLASS | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS; |
6bbb4715 | 1274 | |
890881e7 | 1275 | getter = lookup_method_static (interface, getter_name, flags); |
1276 | setter = lookup_method_static (interface, setter_name, flags); | |
1277 | } | |
1278 | ||
736dea43 | 1279 | /* Second, check the local @implementation context. */ |
1280 | if (!getter && !setter) | |
1281 | { | |
1282 | if (implementation) | |
1283 | { | |
1284 | if (is_class) | |
1285 | { | |
1286 | getter = lookup_method (CLASS_CLS_METHODS (implementation), getter_name); | |
1287 | setter = lookup_method (CLASS_CLS_METHODS (implementation), setter_name); | |
1288 | } | |
1289 | else | |
1290 | { | |
1291 | getter = lookup_method (CLASS_NST_METHODS (implementation), getter_name); | |
6bbb4715 | 1292 | setter = lookup_method (CLASS_NST_METHODS (implementation), setter_name); |
736dea43 | 1293 | } |
1294 | } | |
1295 | } | |
1296 | ||
1297 | /* Try the protocol_list if we didn't find anything in the | |
1298 | @interface and in the @implementation. */ | |
890881e7 | 1299 | if (!getter && !setter) |
1300 | { | |
1301 | getter = lookup_method_in_protocol_list (protocol_list, getter_name, is_class); | |
1302 | setter = lookup_method_in_protocol_list (protocol_list, setter_name, is_class); | |
1303 | } | |
1304 | ||
1305 | /* There needs to be at least a getter or setter for this to be a | |
1306 | valid 'object.component' syntax. */ | |
1307 | if (getter || setter) | |
1308 | { | |
1309 | /* Yes ... determine the type of the expression. */ | |
1310 | tree property_decl; | |
1311 | tree type; | |
6bbb4715 | 1312 | |
890881e7 | 1313 | if (getter) |
1314 | type = TREE_VALUE (TREE_TYPE (getter)); | |
1315 | else | |
1316 | type = TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter))); | |
6bbb4715 | 1317 | |
890881e7 | 1318 | /* Create an artificial property declaration with the |
1319 | information we collected on the type and getter/setter | |
1320 | names. */ | |
1321 | property_decl = make_node (PROPERTY_DECL); | |
6bbb4715 | 1322 | |
890881e7 | 1323 | TREE_TYPE (property_decl) = type; |
1324 | DECL_SOURCE_LOCATION (property_decl) = input_location; | |
1325 | TREE_DEPRECATED (property_decl) = 0; | |
1326 | DECL_ARTIFICIAL (property_decl) = 1; | |
d67e8485 | 1327 | |
890881e7 | 1328 | /* Add property-specific information. Note that one of |
1329 | PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a | |
1330 | non-existing method; this will generate an error when the | |
1331 | expression is later compiled. At this stage we don't know if | |
1332 | the getter or setter will be used, so we can't generate an | |
1333 | error. */ | |
1334 | PROPERTY_NAME (property_decl) = component; | |
1335 | PROPERTY_GETTER_NAME (property_decl) = getter_name; | |
1336 | PROPERTY_SETTER_NAME (property_decl) = setter_name; | |
1337 | PROPERTY_READONLY (property_decl) = 0; | |
1338 | PROPERTY_NONATOMIC (property_decl) = 0; | |
1339 | PROPERTY_ASSIGN_SEMANTICS (property_decl) = 0; | |
1340 | PROPERTY_IVAR_NAME (property_decl) = NULL_TREE; | |
1341 | PROPERTY_DYNAMIC (property_decl) = 0; | |
db496216 | 1342 | PROPERTY_OPTIONAL (property_decl) = 0; |
890881e7 | 1343 | |
1344 | if (!getter) | |
1345 | PROPERTY_HAS_NO_GETTER (property_decl) = 1; | |
1346 | ||
1347 | /* The following is currently unused, but it's nice to have | |
1348 | there. We may use it if we need in the future. */ | |
1349 | if (!setter) | |
1350 | PROPERTY_HAS_NO_SETTER (property_decl) = 1; | |
1351 | ||
1352 | return property_decl; | |
1353 | } | |
1354 | ||
1355 | return NULL_TREE; | |
1356 | } | |
86c110ac | 1357 | |
9d9f5bb3 | 1358 | /* This hook routine is invoked by the parser when an expression such |
1359 | as 'xxx.yyy' is parsed. We get a chance to process these | |
1360 | expressions in a way that is specified to Objective-C (to implement | |
890881e7 | 1361 | the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars). |
1362 | If the expression is not an Objective-C specified expression, we | |
1363 | should return NULL_TREE; else we return the expression. | |
1364 | ||
1365 | At the moment this only implements dot-syntax and properties (not | |
1366 | non-fragile ivars yet), ie 'object.property' or 'object.component' | |
1367 | where 'component' is not a declared property, but a valid getter or | |
1368 | setter for it could be found. */ | |
86c110ac | 1369 | tree |
9d9f5bb3 | 1370 | objc_maybe_build_component_ref (tree object, tree property_ident) |
86c110ac | 1371 | { |
1372 | tree x = NULL_TREE; | |
1373 | tree rtype; | |
1374 | ||
736dea43 | 1375 | /* If we are in Objective-C 1.0 mode, dot-syntax and properties are |
1376 | not available. */ | |
1ef143b6 | 1377 | if (flag_objc1_only) |
1378 | return NULL_TREE; | |
1379 | ||
736dea43 | 1380 | /* Try to determine if 'object' is an Objective-C object or not. If |
1381 | not, return. */ | |
6bbb4715 | 1382 | if (object == NULL_TREE || object == error_mark_node |
9d9f5bb3 | 1383 | || (rtype = TREE_TYPE (object)) == NULL_TREE) |
86c110ac | 1384 | return NULL_TREE; |
6bbb4715 | 1385 | |
9d9f5bb3 | 1386 | if (property_ident == NULL_TREE || property_ident == error_mark_node |
1387 | || TREE_CODE (property_ident) != IDENTIFIER_NODE) | |
86c110ac | 1388 | return NULL_TREE; |
1389 | ||
736dea43 | 1390 | /* The following analysis of 'object' is similar to the one used for |
1391 | the 'receiver' of a method invocation. We need to determine what | |
1392 | 'object' is and find the appropriate property (either declared, | |
1393 | or artificial) for it (in the same way as we need to find the | |
1394 | appropriate method prototype for a method invocation). There are | |
1395 | some simplifications here though: "object.property" is invalid if | |
1396 | "object" has a type of "id" or "Class"; it must at least have a | |
1397 | protocol attached to it, and "object" is never a class name as | |
1398 | that is done by objc_build_class_component_ref. Finally, we | |
1399 | don't know if this really is a dot-syntax expression, so we want | |
1400 | to make a quick exit if it is not; for this reason, we try to | |
1401 | postpone checks after determining that 'object' looks like an | |
1402 | Objective-C object. */ | |
1403 | ||
86c110ac | 1404 | if (objc_is_id (rtype)) |
1405 | { | |
736dea43 | 1406 | /* This is the case that the 'object' is of type 'id' or |
1407 | 'Class'. */ | |
890881e7 | 1408 | |
736dea43 | 1409 | /* Check if at least it is of type 'id <Protocol>' or 'Class |
1410 | <Protocol>'; if so, look the property up in the | |
1411 | protocols. */ | |
1412 | if (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))) | |
890881e7 | 1413 | { |
736dea43 | 1414 | tree rprotos = TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype)); |
6bbb4715 | 1415 | |
736dea43 | 1416 | if (rprotos) |
1417 | { | |
1418 | /* No point looking up declared @properties if we are | |
1419 | dealing with a class. Classes have no declared | |
1420 | properties. */ | |
1421 | if (!IS_CLASS (rtype)) | |
1422 | x = lookup_property_in_protocol_list (rprotos, property_ident); | |
db496216 | 1423 | |
736dea43 | 1424 | if (x == NULL_TREE) |
1425 | { | |
1426 | /* Ok, no property. Maybe it was an | |
1427 | object.component dot-syntax without a declared | |
1428 | property (this is valid for classes too). Look | |
1429 | for getter/setter methods and internally declare | |
86d30f03 | 1430 | an artificial property based on them if found. */ |
736dea43 | 1431 | x = maybe_make_artificial_property_decl (NULL_TREE, |
1432 | NULL_TREE, | |
6bbb4715 | 1433 | rprotos, |
736dea43 | 1434 | property_ident, |
db496216 | 1435 | IS_CLASS (rtype), |
1436 | NULL_TREE); | |
1437 | } | |
1438 | else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x)) | |
1439 | { | |
1440 | /* This is a special, complicated case. If the | |
1441 | property is optional, and is read-only, then the | |
1442 | property is always used for reading, but an | |
1443 | eventual existing non-property setter can be used | |
1444 | for writing. We create an artificial property | |
1445 | decl copying the getter from the optional | |
1446 | property, and looking up the setter in the | |
1447 | interface. */ | |
1448 | x = maybe_make_artificial_property_decl (NULL_TREE, | |
1449 | NULL_TREE, | |
1450 | rprotos, | |
1451 | property_ident, | |
1452 | false, | |
6bbb4715 | 1453 | PROPERTY_GETTER_NAME (x)); |
736dea43 | 1454 | } |
1455 | } | |
1456 | } | |
1457 | else if (objc_method_context) | |
1458 | { | |
1459 | /* Else, if we are inside a method it could be the case of | |
1460 | 'super' or 'self'. */ | |
1461 | tree interface_type = NULL_TREE; | |
1462 | tree t = object; | |
1463 | while (TREE_CODE (t) == COMPOUND_EXPR | |
1464 | || TREE_CODE (t) == MODIFY_EXPR | |
1465 | || CONVERT_EXPR_P (t) | |
1466 | || TREE_CODE (t) == COMPONENT_REF) | |
1467 | t = TREE_OPERAND (t, 0); | |
6bbb4715 | 1468 | |
1469 | if (t == UOBJC_SUPER_decl) | |
1a19222c | 1470 | interface_type = lookup_interface (CLASS_SUPER_NAME (implementation_template)); |
736dea43 | 1471 | else if (t == self_decl) |
1472 | interface_type = lookup_interface (CLASS_NAME (implementation_template)); | |
1473 | ||
736dea43 | 1474 | if (interface_type) |
1475 | { | |
1476 | if (TREE_CODE (objc_method_context) != CLASS_METHOD_DECL) | |
ad312852 | 1477 | x = lookup_property (interface_type, property_ident); |
6bbb4715 | 1478 | |
736dea43 | 1479 | if (x == NULL_TREE) |
1480 | { | |
1481 | /* Try the dot-syntax without a declared property. | |
1482 | If this is an access to 'self', it is possible | |
1483 | that they may refer to a setter/getter that is | |
1484 | not declared in the interface, but exists locally | |
1485 | in the implementation. In that case, get the | |
1486 | implementation context and use it. */ | |
1487 | tree implementation = NULL_TREE; | |
1488 | ||
1489 | if (t == self_decl) | |
1490 | implementation = objc_implementation_context; | |
6bbb4715 | 1491 | |
1492 | x = maybe_make_artificial_property_decl | |
736dea43 | 1493 | (interface_type, implementation, NULL_TREE, |
1494 | property_ident, | |
db496216 | 1495 | (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL), |
1496 | NULL_TREE); | |
1497 | } | |
1498 | else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x)) | |
1499 | { | |
1500 | tree implementation = NULL_TREE; | |
6bbb4715 | 1501 | |
db496216 | 1502 | if (t == self_decl) |
1503 | implementation = objc_implementation_context; | |
6bbb4715 | 1504 | |
db496216 | 1505 | x = maybe_make_artificial_property_decl (interface_type, |
1506 | implementation, | |
1507 | NULL_TREE, | |
1508 | property_ident, | |
1509 | false, | |
6bbb4715 | 1510 | PROPERTY_GETTER_NAME (x)); |
736dea43 | 1511 | } |
1512 | } | |
890881e7 | 1513 | } |
86c110ac | 1514 | } |
1515 | else | |
1516 | { | |
736dea43 | 1517 | /* This is the case where we have more information on 'rtype'. */ |
86c110ac | 1518 | tree basetype = TYPE_MAIN_VARIANT (rtype); |
1519 | ||
736dea43 | 1520 | /* Skip the pointer - if none, it's not an Objective-C object or |
1521 | class. */ | |
86c110ac | 1522 | if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE) |
1523 | basetype = TREE_TYPE (basetype); | |
1524 | else | |
1525 | return NULL_TREE; | |
1526 | ||
736dea43 | 1527 | /* Traverse typedefs. */ |
86c110ac | 1528 | while (basetype != NULL_TREE |
6bbb4715 | 1529 | && TREE_CODE (basetype) == RECORD_TYPE |
86c110ac | 1530 | && OBJC_TYPE_NAME (basetype) |
1531 | && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL | |
1532 | && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype))) | |
1533 | basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)); | |
1534 | ||
1535 | if (basetype != NULL_TREE && TYPED_OBJECT (basetype)) | |
1536 | { | |
1537 | tree interface_type = TYPE_OBJC_INTERFACE (basetype); | |
9d9f5bb3 | 1538 | tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype); |
890881e7 | 1539 | |
6bbb4715 | 1540 | if (interface_type |
736dea43 | 1541 | && (TREE_CODE (interface_type) == CLASS_INTERFACE_TYPE |
1542 | || TREE_CODE (interface_type) == CATEGORY_INTERFACE_TYPE | |
1543 | || TREE_CODE (interface_type) == PROTOCOL_INTERFACE_TYPE)) | |
890881e7 | 1544 | { |
736dea43 | 1545 | /* Not sure 'rtype' could ever be a class here! Just |
1546 | for safety we keep the checks. */ | |
1547 | if (!IS_CLASS (rtype)) | |
1548 | { | |
1549 | x = lookup_property (interface_type, property_ident); | |
6bbb4715 | 1550 | |
736dea43 | 1551 | if (x == NULL_TREE) |
6bbb4715 | 1552 | x = lookup_property_in_protocol_list (protocol_list, |
736dea43 | 1553 | property_ident); |
1554 | } | |
6bbb4715 | 1555 | |
736dea43 | 1556 | if (x == NULL_TREE) |
1557 | { | |
1558 | /* Try the dot-syntax without a declared property. | |
1559 | If we are inside a method implementation, it is | |
1560 | possible that they may refer to a setter/getter | |
1561 | that is not declared in the interface, but exists | |
1562 | locally in the implementation. In that case, get | |
1563 | the implementation context and use it. */ | |
1564 | tree implementation = NULL_TREE; | |
1565 | ||
1566 | if (objc_implementation_context | |
6bbb4715 | 1567 | && CLASS_NAME (objc_implementation_context) |
736dea43 | 1568 | == OBJC_TYPE_NAME (interface_type)) |
1569 | implementation = objc_implementation_context; | |
6bbb4715 | 1570 | |
736dea43 | 1571 | x = maybe_make_artificial_property_decl (interface_type, |
1572 | implementation, | |
6bbb4715 | 1573 | protocol_list, |
736dea43 | 1574 | property_ident, |
db496216 | 1575 | IS_CLASS (rtype), |
1576 | NULL_TREE); | |
736dea43 | 1577 | } |
db496216 | 1578 | else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x)) |
1579 | { | |
1580 | tree implementation = NULL_TREE; | |
1581 | ||
1582 | if (objc_implementation_context | |
6bbb4715 | 1583 | && CLASS_NAME (objc_implementation_context) |
db496216 | 1584 | == OBJC_TYPE_NAME (interface_type)) |
1585 | implementation = objc_implementation_context; | |
6bbb4715 | 1586 | |
db496216 | 1587 | x = maybe_make_artificial_property_decl (interface_type, |
1588 | implementation, | |
1589 | protocol_list, | |
1590 | property_ident, | |
1591 | false, | |
6bbb4715 | 1592 | PROPERTY_GETTER_NAME (x)); |
1593 | } | |
890881e7 | 1594 | } |
86c110ac | 1595 | } |
1596 | } | |
1597 | ||
1598 | if (x) | |
1599 | { | |
9d9f5bb3 | 1600 | tree expression; |
736dea43 | 1601 | tree getter_call; |
d67e8485 | 1602 | tree deprecated_method_prototype = NULL_TREE; |
9d9f5bb3 | 1603 | |
1604 | /* We have an additional nasty problem here; if this | |
1605 | PROPERTY_REF needs to become a 'getter', then the conversion | |
1606 | from PROPERTY_REF into a getter call happens in gimplify, | |
736dea43 | 1607 | after the selector table has already been generated and when |
1608 | it is too late to add another selector to it. To work around | |
1609 | the problem, we always create the getter call at this stage, | |
1610 | which puts the selector in the table. Note that if the | |
1611 | PROPERTY_REF becomes a 'setter' instead of a 'getter', then | |
1612 | we have added a selector too many to the selector table. | |
1613 | This is a little inefficient. | |
1614 | ||
1615 | Also note that method calls to 'self' and 'super' require the | |
1616 | context (self_decl, UOBJS_SUPER_decl, | |
1617 | objc_implementation_context etc) to be built correctly; this | |
1618 | is yet another reason why building the call at the gimplify | |
1619 | stage (when this context has been lost) is not very | |
1620 | practical. If we build it at this stage, we know it will | |
1621 | always be built correctly. | |
890881e7 | 1622 | |
1623 | If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial | |
1624 | property decl created to deal with a dotsyntax not really | |
1625 | referring to an existing property) then do not try to build a | |
1626 | call to the getter as there is no getter. */ | |
736dea43 | 1627 | if (PROPERTY_HAS_NO_GETTER (x)) |
1628 | getter_call = NULL_TREE; | |
1629 | else | |
d67e8485 | 1630 | getter_call = objc_finish_message_expr |
1631 | (object, PROPERTY_GETTER_NAME (x), NULL_TREE, | |
1632 | /* Disable the immediate deprecation warning if the getter | |
1633 | is deprecated, but record the fact that the getter is | |
1634 | deprecated by setting PROPERTY_REF_DEPRECATED_GETTER to | |
1635 | the method prototype. */ | |
1636 | &deprecated_method_prototype); | |
1637 | ||
1638 | expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call, | |
1639 | deprecated_method_prototype); | |
736dea43 | 1640 | SET_EXPR_LOCATION (expression, input_location); |
1641 | TREE_SIDE_EFFECTS (expression) = 1; | |
6bbb4715 | 1642 | |
9d9f5bb3 | 1643 | return expression; |
86c110ac | 1644 | } |
86c110ac | 1645 | |
9d9f5bb3 | 1646 | return NULL_TREE; |
86c110ac | 1647 | } |
1648 | ||
b0d0931f | 1649 | /* This hook routine is invoked by the parser when an expression such |
1650 | as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the | |
1651 | Objective-C 2.0 dot-syntax applied to classes, so we need to | |
1652 | convert it into a setter/getter call on the class. */ | |
1653 | tree | |
1654 | objc_build_class_component_ref (tree class_name, tree property_ident) | |
1655 | { | |
1656 | tree x = NULL_TREE; | |
1657 | tree object, rtype; | |
6bbb4715 | 1658 | |
b0d0931f | 1659 | if (flag_objc1_only) |
1660 | error_at (input_location, "the dot syntax is not available in Objective-C 1.0"); | |
6bbb4715 | 1661 | |
b0d0931f | 1662 | if (class_name == NULL_TREE || class_name == error_mark_node |
1663 | || TREE_CODE (class_name) != IDENTIFIER_NODE) | |
1664 | return error_mark_node; | |
6bbb4715 | 1665 | |
b0d0931f | 1666 | if (property_ident == NULL_TREE || property_ident == error_mark_node |
1667 | || TREE_CODE (property_ident) != IDENTIFIER_NODE) | |
1668 | return NULL_TREE; | |
6bbb4715 | 1669 | |
b0d0931f | 1670 | object = objc_get_class_reference (class_name); |
1671 | if (!object) | |
1672 | { | |
1673 | /* We know that 'class_name' is an Objective-C class name as the | |
1674 | parser won't call this function if it is not. This is only a | |
1675 | double-check for safety. */ | |
6bbb4715 | 1676 | error_at (input_location, "could not find class %qE", class_name); |
b0d0931f | 1677 | return error_mark_node; |
1678 | } | |
1679 | ||
1680 | rtype = lookup_interface (class_name); | |
1681 | if (!rtype) | |
1682 | { | |
1683 | /* Again, this should never happen, but we do check. */ | |
6bbb4715 | 1684 | error_at (input_location, "could not find interface for class %qE", class_name); |
b0d0931f | 1685 | return error_mark_node; |
1686 | } | |
c1cfb0f5 | 1687 | else |
1688 | { | |
1689 | if (TREE_DEPRECATED (rtype)) | |
6bbb4715 | 1690 | warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", class_name); |
c1cfb0f5 | 1691 | } |
b0d0931f | 1692 | |
736dea43 | 1693 | x = maybe_make_artificial_property_decl (rtype, NULL_TREE, NULL_TREE, |
b0d0931f | 1694 | property_ident, |
db496216 | 1695 | true, NULL_TREE); |
6bbb4715 | 1696 | |
b0d0931f | 1697 | if (x) |
1698 | { | |
1699 | tree expression; | |
736dea43 | 1700 | tree getter_call; |
d67e8485 | 1701 | tree deprecated_method_prototype = NULL_TREE; |
b0d0931f | 1702 | |
736dea43 | 1703 | if (PROPERTY_HAS_NO_GETTER (x)) |
1704 | getter_call = NULL_TREE; | |
1705 | else | |
d67e8485 | 1706 | getter_call = objc_finish_message_expr |
1707 | (object, PROPERTY_GETTER_NAME (x), NULL_TREE, | |
1708 | &deprecated_method_prototype); | |
b0d0931f | 1709 | |
d67e8485 | 1710 | expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call, |
1711 | deprecated_method_prototype); | |
b0d0931f | 1712 | SET_EXPR_LOCATION (expression, input_location); |
1713 | TREE_SIDE_EFFECTS (expression) = 1; | |
736dea43 | 1714 | |
b0d0931f | 1715 | return expression; |
1716 | } | |
1717 | else | |
1718 | { | |
6bbb4715 | 1719 | error_at (input_location, "could not find setter/getter for %qE in class %qE", |
1720 | property_ident, class_name); | |
b0d0931f | 1721 | return error_mark_node; |
1722 | } | |
1723 | ||
1724 | return NULL_TREE; | |
1725 | } | |
1726 | ||
1727 | ||
1728 | ||
9d9f5bb3 | 1729 | /* This is used because we don't want to expose PROPERTY_REF to the |
1730 | C/C++ frontends. Maybe we should! */ | |
1731 | bool | |
1732 | objc_is_property_ref (tree node) | |
86c110ac | 1733 | { |
9d9f5bb3 | 1734 | if (node && TREE_CODE (node) == PROPERTY_REF) |
1735 | return true; | |
86c110ac | 1736 | else |
9d9f5bb3 | 1737 | return false; |
86c110ac | 1738 | } |
1739 | ||
6d922cb7 | 1740 | /* This function builds a setter call for a PROPERTY_REF (real, for a |
1741 | declared property, or artificial, for a dot-syntax accessor which | |
1742 | is not corresponding to a property). 'lhs' must be a PROPERTY_REF | |
1743 | (the caller must check this beforehand). 'rhs' is the value to | |
1744 | assign to the property. A plain setter call is returned, or | |
1745 | error_mark_node if the property is readonly. */ | |
1746 | ||
1747 | static tree | |
1748 | objc_build_setter_call (tree lhs, tree rhs) | |
1749 | { | |
1750 | tree object_expr = PROPERTY_REF_OBJECT (lhs); | |
1751 | tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs); | |
6bbb4715 | 1752 | |
6d922cb7 | 1753 | if (PROPERTY_READONLY (property_decl)) |
1754 | { | |
6bbb4715 | 1755 | error ("readonly property can not be set"); |
6d922cb7 | 1756 | return error_mark_node; |
1757 | } | |
1758 | else | |
1759 | { | |
1760 | tree setter_argument = build_tree_list (NULL_TREE, rhs); | |
1761 | tree setter; | |
6bbb4715 | 1762 | |
6d922cb7 | 1763 | /* TODO: Check that the setter return type is 'void'. */ |
1764 | ||
1765 | /* TODO: Decay arguments in C. */ | |
6bbb4715 | 1766 | setter = objc_finish_message_expr (object_expr, |
6d922cb7 | 1767 | PROPERTY_SETTER_NAME (property_decl), |
d67e8485 | 1768 | setter_argument, NULL); |
6d922cb7 | 1769 | return setter; |
1770 | } | |
1771 | ||
1772 | /* Unreachable, but the compiler may not realize. */ | |
1773 | return error_mark_node; | |
1774 | } | |
1775 | ||
9d9f5bb3 | 1776 | /* This hook routine is called when a MODIFY_EXPR is being built. We |
1777 | check what is being modified; if it is a PROPERTY_REF, we need to | |
1778 | generate a 'setter' function call for the property. If this is not | |
1779 | a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go | |
1780 | on creating their MODIFY_EXPR. | |
86c110ac | 1781 | |
9d9f5bb3 | 1782 | This is used for example if you write |
86c110ac | 1783 | |
9d9f5bb3 | 1784 | object.count = 1; |
86c110ac | 1785 | |
9d9f5bb3 | 1786 | where 'count' is a property. The left-hand side creates a |
1787 | PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR | |
1788 | to assign something to it. We intercept that here, and generate a | |
1789 | call to the 'setter' method instead. */ | |
1790 | tree | |
1791 | objc_maybe_build_modify_expr (tree lhs, tree rhs) | |
86c110ac | 1792 | { |
9d9f5bb3 | 1793 | if (lhs && TREE_CODE (lhs) == PROPERTY_REF) |
86c110ac | 1794 | { |
6d922cb7 | 1795 | /* Building a simple call to the setter method would work for cases such as |
86c110ac | 1796 | |
6d922cb7 | 1797 | object.count = 1; |
890881e7 | 1798 | |
6d922cb7 | 1799 | but wouldn't work for cases such as |
890881e7 | 1800 | |
6d922cb7 | 1801 | count = object2.count = 1; |
1802 | ||
1803 | to get these to work with very little effort, we build a | |
1804 | compound statement which does the setter call (to set the | |
1805 | property to 'rhs'), but which can also be evaluated returning | |
d098f394 | 1806 | the 'rhs'. If the 'rhs' has no side effects, we can simply |
1807 | evaluate it twice, building | |
1808 | ||
1809 | ([object setProperty: rhs]; rhs) | |
1810 | ||
1811 | If it has side effects, we put it in a temporary variable first, | |
1812 | so we create the following: | |
6d922cb7 | 1813 | |
1814 | (temp = rhs; [object setProperty: temp]; temp) | |
d098f394 | 1815 | |
1816 | setter_argument is rhs in the first case, and temp in the second | |
1817 | case. | |
6d922cb7 | 1818 | */ |
d098f394 | 1819 | tree setter_argument; |
1820 | ||
6d922cb7 | 1821 | /* s1, s2 and s3 are the tree statements that we need in the |
1822 | compound expression. */ | |
1823 | tree s1, s2, s3, compound_expr; | |
d098f394 | 1824 | |
1825 | if (TREE_SIDE_EFFECTS (rhs)) | |
1826 | { | |
1827 | tree bind; | |
6bbb4715 | 1828 | |
d098f394 | 1829 | /* Declare __objc_property_temp in a local bind. */ |
1830 | setter_argument = objc_create_temporary_var (TREE_TYPE (rhs), "__objc_property_temp"); | |
1831 | DECL_SOURCE_LOCATION (setter_argument) = input_location; | |
1832 | bind = build3 (BIND_EXPR, void_type_node, setter_argument, NULL, NULL); | |
1833 | SET_EXPR_LOCATION (bind, input_location); | |
1834 | TREE_SIDE_EFFECTS (bind) = 1; | |
1835 | add_stmt (bind); | |
1836 | ||
1837 | /* s1: x = rhs */ | |
1838 | s1 = build_modify_expr (input_location, setter_argument, NULL_TREE, | |
1839 | NOP_EXPR, | |
1840 | input_location, rhs, NULL_TREE); | |
1841 | SET_EXPR_LOCATION (s1, input_location); | |
1842 | } | |
1843 | else | |
1844 | { | |
1845 | /* No s1. */ | |
1846 | setter_argument = rhs; | |
1847 | s1 = NULL_TREE; | |
1848 | } | |
6bbb4715 | 1849 | |
6d922cb7 | 1850 | /* Now build the compound statement. */ |
6bbb4715 | 1851 | |
d098f394 | 1852 | /* s2: [object setProperty: x] */ |
1853 | s2 = objc_build_setter_call (lhs, setter_argument); | |
6bbb4715 | 1854 | |
d098f394 | 1855 | /* This happens if building the setter failed because the |
1856 | property is readonly. */ | |
6d922cb7 | 1857 | if (s2 == error_mark_node) |
1858 | return error_mark_node; | |
1859 | ||
1860 | SET_EXPR_LOCATION (s2, input_location); | |
6bbb4715 | 1861 | |
d098f394 | 1862 | /* s3: x */ |
1863 | s3 = convert (TREE_TYPE (lhs), setter_argument); | |
6d922cb7 | 1864 | |
d098f394 | 1865 | /* Now build the compound statement (s1, s2, s3) or (s2, s3) as |
1866 | appropriate. */ | |
1867 | if (s1) | |
1868 | compound_expr = build_compound_expr (input_location, build_compound_expr (input_location, s1, s2), s3); | |
1869 | else | |
6bbb4715 | 1870 | compound_expr = build_compound_expr (input_location, s2, s3); |
6d922cb7 | 1871 | |
1872 | /* Without this, with -Wall you get a 'valued computed is not | |
1873 | used' every time there is a "object.property = x" where the | |
1874 | value of the resulting MODIFY_EXPR is not used. That is | |
1875 | correct (maybe a more sophisticated implementation could | |
1876 | avoid generating the compound expression if not needed), but | |
1877 | we need to turn it off. */ | |
1878 | TREE_NO_WARNING (compound_expr) = 1; | |
1879 | return compound_expr; | |
9d9f5bb3 | 1880 | } |
1881 | else | |
1882 | return NULL_TREE; | |
86c110ac | 1883 | } |
1884 | ||
e4a7640a | 1885 | /* This hook is called by the frontend when one of the four unary |
1886 | expressions PREINCREMENT_EXPR, POSTINCREMENT_EXPR, | |
1887 | PREDECREMENT_EXPR and POSTDECREMENT_EXPR is being built with an | |
1888 | argument which is a PROPERTY_REF. For example, this happens if you have | |
1889 | ||
1890 | object.count++; | |
1891 | ||
1892 | where 'count' is a property. We need to use the 'getter' and | |
1893 | 'setter' for the property in an appropriate way to build the | |
1894 | appropriate expression. 'code' is the code for the expression (one | |
1895 | of the four mentioned above); 'argument' is the PROPERTY_REF, and | |
6bbb4715 | 1896 | 'increment' is how much we need to add or subtract. */ |
e4a7640a | 1897 | tree |
1898 | objc_build_incr_expr_for_property_ref (location_t location, | |
6bbb4715 | 1899 | enum tree_code code, |
e4a7640a | 1900 | tree argument, tree increment) |
1901 | { | |
1902 | /* Here are the expressions that we want to build: | |
1903 | ||
1904 | For PREINCREMENT_EXPR / PREDECREMENT_EXPR: | |
1905 | (temp = [object property] +/- increment, [object setProperty: temp], temp) | |
6bbb4715 | 1906 | |
e4a7640a | 1907 | For POSTINCREMENT_EXPR / POSTECREMENT_EXPR: |
1908 | (temp = [object property], [object setProperty: temp +/- increment], temp) */ | |
6bbb4715 | 1909 | |
e4a7640a | 1910 | tree temp_variable_decl, bind; |
1911 | /* s1, s2 and s3 are the tree statements that we need in the | |
1912 | compound expression. */ | |
6d922cb7 | 1913 | tree s1, s2, s3, compound_expr; |
6bbb4715 | 1914 | |
e4a7640a | 1915 | /* Safety check. */ |
1916 | if (!argument || TREE_CODE (argument) != PROPERTY_REF) | |
1917 | return error_mark_node; | |
1918 | ||
1919 | /* Declare __objc_property_temp in a local bind. */ | |
1920 | temp_variable_decl = objc_create_temporary_var (TREE_TYPE (argument), "__objc_property_temp"); | |
1921 | DECL_SOURCE_LOCATION (temp_variable_decl) = location; | |
1922 | bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL); | |
1923 | SET_EXPR_LOCATION (bind, location); | |
1924 | TREE_SIDE_EFFECTS (bind) = 1; | |
1925 | add_stmt (bind); | |
6bbb4715 | 1926 | |
e4a7640a | 1927 | /* Now build the compound statement. */ |
6bbb4715 | 1928 | |
e4a7640a | 1929 | /* Note that the 'getter' is generated at gimplify time; at this |
1930 | time, we can simply put the property_ref (ie, argument) wherever | |
1931 | we want the getter ultimately to be. */ | |
6bbb4715 | 1932 | |
e4a7640a | 1933 | /* s1: __objc_property_temp = [object property] <+/- increment> */ |
1934 | switch (code) | |
1935 | { | |
6bbb4715 | 1936 | case PREINCREMENT_EXPR: |
e4a7640a | 1937 | /* __objc_property_temp = [object property] + increment */ |
6d922cb7 | 1938 | s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE, |
1939 | NOP_EXPR, | |
6bbb4715 | 1940 | location, build2 (PLUS_EXPR, TREE_TYPE (argument), |
6d922cb7 | 1941 | argument, increment), NULL_TREE); |
e4a7640a | 1942 | break; |
1943 | case PREDECREMENT_EXPR: | |
1944 | /* __objc_property_temp = [object property] - increment */ | |
6d922cb7 | 1945 | s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE, |
1946 | NOP_EXPR, | |
6bbb4715 | 1947 | location, build2 (MINUS_EXPR, TREE_TYPE (argument), |
6d922cb7 | 1948 | argument, increment), NULL_TREE); |
e4a7640a | 1949 | break; |
1950 | case POSTINCREMENT_EXPR: | |
1951 | case POSTDECREMENT_EXPR: | |
1952 | /* __objc_property_temp = [object property] */ | |
6d922cb7 | 1953 | s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE, |
1954 | NOP_EXPR, | |
1955 | location, argument, NULL_TREE); | |
e4a7640a | 1956 | break; |
1957 | default: | |
1958 | gcc_unreachable (); | |
1959 | } | |
6bbb4715 | 1960 | |
e4a7640a | 1961 | /* s2: [object setProperty: __objc_property_temp <+/- increment>] */ |
1962 | switch (code) | |
1963 | { | |
6bbb4715 | 1964 | case PREINCREMENT_EXPR: |
e4a7640a | 1965 | case PREDECREMENT_EXPR: |
1966 | /* [object setProperty: __objc_property_temp] */ | |
6d922cb7 | 1967 | s2 = objc_build_setter_call (argument, temp_variable_decl); |
e4a7640a | 1968 | break; |
1969 | case POSTINCREMENT_EXPR: | |
1970 | /* [object setProperty: __objc_property_temp + increment] */ | |
6d922cb7 | 1971 | s2 = objc_build_setter_call (argument, |
6bbb4715 | 1972 | build2 (PLUS_EXPR, TREE_TYPE (argument), |
6d922cb7 | 1973 | temp_variable_decl, increment)); |
e4a7640a | 1974 | break; |
1975 | case POSTDECREMENT_EXPR: | |
1976 | /* [object setProperty: __objc_property_temp - increment] */ | |
6d922cb7 | 1977 | s2 = objc_build_setter_call (argument, |
6bbb4715 | 1978 | build2 (MINUS_EXPR, TREE_TYPE (argument), |
6d922cb7 | 1979 | temp_variable_decl, increment)); |
e4a7640a | 1980 | break; |
1981 | default: | |
1982 | gcc_unreachable (); | |
1983 | } | |
1984 | ||
1985 | /* This happens if building the setter failed because the property | |
1986 | is readonly. */ | |
1987 | if (s2 == error_mark_node) | |
1988 | return error_mark_node; | |
1989 | ||
6bbb4715 | 1990 | SET_EXPR_LOCATION (s2, location); |
1991 | ||
e4a7640a | 1992 | /* s3: __objc_property_temp */ |
6d922cb7 | 1993 | s3 = convert (TREE_TYPE (argument), temp_variable_decl); |
6bbb4715 | 1994 | |
e4a7640a | 1995 | /* Now build the compound statement (s1, s2, s3) */ |
6d922cb7 | 1996 | compound_expr = build_compound_expr (location, build_compound_expr (location, s1, s2), s3); |
1997 | ||
1998 | /* Prevent C++ from warning with -Wall that "right operand of comma | |
1999 | operator has no effect". */ | |
2000 | TREE_NO_WARNING (compound_expr) = 1; | |
2001 | return compound_expr; | |
e4a7640a | 2002 | } |
2003 | ||
13dcc150 | 2004 | tree |
45b2b110 | 2005 | objc_build_method_signature (bool is_class_method, tree rettype, tree selector, |
450302fe | 2006 | tree optparms, bool ellipsis) |
13dcc150 | 2007 | { |
45b2b110 | 2008 | if (is_class_method) |
2009 | return build_method_decl (CLASS_METHOD_DECL, rettype, selector, | |
2010 | optparms, ellipsis); | |
2011 | else | |
2012 | return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector, | |
2013 | optparms, ellipsis); | |
13dcc150 | 2014 | } |
2015 | ||
2016 | void | |
45b2b110 | 2017 | objc_add_method_declaration (bool is_class_method, tree decl, tree attributes) |
13dcc150 | 2018 | { |
2019 | if (!objc_interface_context) | |
d4a8fb25 | 2020 | { |
2021 | /* PS: At the moment, due to how the parser works, it should be | |
2022 | impossible to get here. But it's good to have the check in | |
2023 | case the parser changes. | |
2024 | */ | |
c05be867 | 2025 | fatal_error (input_location, |
2026 | "method declaration not in @interface context"); | |
d4a8fb25 | 2027 | } |
13dcc150 | 2028 | |
1ef143b6 | 2029 | if (flag_objc1_only && attributes) |
2030 | error_at (input_location, "method attributes are not available in Objective-C 1.0"); | |
2031 | ||
40c8d1dd | 2032 | objc_decl_method_attributes (&decl, attributes, 0); |
13dcc150 | 2033 | objc_add_method (objc_interface_context, |
2034 | decl, | |
45b2b110 | 2035 | is_class_method, |
069761fb | 2036 | objc_method_optional_flag); |
13dcc150 | 2037 | } |
2038 | ||
d4a8fb25 | 2039 | /* Return 'true' if the method definition could be started, and |
2040 | 'false' if not (because we are outside an @implementation context). | |
4232a958 | 2041 | EXPR is NULL or an expression that needs to be evaluated for the |
2042 | side effects of array size expressions in the parameters. | |
d4a8fb25 | 2043 | */ |
2044 | bool | |
4232a958 | 2045 | objc_start_method_definition (bool is_class_method, tree decl, tree attributes, |
2046 | tree expr) | |
13dcc150 | 2047 | { |
2048 | if (!objc_implementation_context) | |
d4a8fb25 | 2049 | { |
2050 | error ("method definition not in @implementation context"); | |
2051 | return false; | |
2052 | } | |
13dcc150 | 2053 | |
06517bd4 | 2054 | if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node) |
2055 | return false; | |
2056 | ||
b24ba8fa | 2057 | #ifndef OBJCPLUS |
2058 | /* Indicate no valid break/continue context by setting these variables | |
2059 | to some non-null, non-label value. We'll notice and emit the proper | |
2060 | error message in c_finish_bc_stmt. */ | |
2061 | c_break_label = c_cont_label = size_zero_node; | |
2062 | #endif | |
2063 | ||
8c582e4f | 2064 | if (attributes) |
2065 | warning_at (input_location, 0, "method attributes can not be specified in @implementation context"); | |
2066 | else | |
2067 | objc_decl_method_attributes (&decl, attributes, 0); | |
2068 | ||
13dcc150 | 2069 | objc_add_method (objc_implementation_context, |
2070 | decl, | |
45b2b110 | 2071 | is_class_method, |
069761fb | 2072 | /* is optional */ false); |
4232a958 | 2073 | start_method_def (decl, expr); |
d4a8fb25 | 2074 | return true; |
13dcc150 | 2075 | } |
2076 | ||
2077 | void | |
2078 | objc_add_instance_variable (tree decl) | |
2079 | { | |
2080 | (void) add_instance_variable (objc_ivar_context, | |
4a8875ed | 2081 | objc_ivar_visibility, |
13dcc150 | 2082 | decl); |
2083 | } | |
2084 | ||
ceb0f408 | 2085 | /* Construct a C struct with same name as KLASS, a base struct with tag |
499ea517 | 2086 | SUPER_NAME (if any), and FIELDS indicated. */ |
d7c168e8 | 2087 | |
499ea517 | 2088 | static tree |
ceb0f408 | 2089 | objc_build_struct (tree klass, tree fields, tree super_name) |
499ea517 | 2090 | { |
ceb0f408 | 2091 | tree name = CLASS_NAME (klass); |
0b09525f | 2092 | tree s = objc_start_struct (name); |
499ea517 | 2093 | tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE); |
6ab3a6e6 | 2094 | tree t; |
1e094109 | 2095 | vec<tree> objc_info = vNULL; |
6ab3a6e6 | 2096 | int i; |
499ea517 | 2097 | |
2098 | if (super) | |
2099 | { | |
2100 | /* Prepend a packed variant of the base class into the layout. This | |
2101 | is necessary to preserve ObjC ABI compatibility. */ | |
e60a6f7b | 2102 | tree base = build_decl (input_location, |
2103 | FIELD_DECL, NULL_TREE, super); | |
499ea517 | 2104 | tree field = TYPE_FIELDS (super); |
2105 | ||
1767a056 | 2106 | while (field && DECL_CHAIN (field) |
2107 | && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL) | |
2108 | field = DECL_CHAIN (field); | |
499ea517 | 2109 | |
08cc44e7 | 2110 | /* For ObjC ABI purposes, the "packed" size of a base class is |
0616a948 | 2111 | the sum of the offset and the size (in bits) of the last field |
2112 | in the class. */ | |
499ea517 | 2113 | DECL_SIZE (base) |
2114 | = (field && TREE_CODE (field) == FIELD_DECL | |
6db57367 | 2115 | ? size_binop (PLUS_EXPR, |
499ea517 | 2116 | size_binop (PLUS_EXPR, |
2117 | size_binop | |
2118 | (MULT_EXPR, | |
2119 | convert (bitsizetype, | |
2120 | DECL_FIELD_OFFSET (field)), | |
2121 | bitsize_int (BITS_PER_UNIT)), | |
2122 | DECL_FIELD_BIT_OFFSET (field)), | |
2123 | DECL_SIZE (field)) | |
2124 | : bitsize_zero_node); | |
2125 | DECL_SIZE_UNIT (base) | |
2126 | = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)), | |
2127 | size_int (BITS_PER_UNIT)); | |
2128 | DECL_ARTIFICIAL (base) = 1; | |
2129 | DECL_ALIGN (base) = 1; | |
2130 | DECL_FIELD_CONTEXT (base) = s; | |
2131 | #ifdef OBJCPLUS | |
2132 | DECL_FIELD_IS_BASE (base) = 1; | |
2133 | ||
2134 | if (fields) | |
2135 | TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */ | |
2136 | #endif /* are following the ObjC ABI here. */ | |
1767a056 | 2137 | DECL_CHAIN (base) = fields; |
499ea517 | 2138 | fields = base; |
2139 | } | |
2140 | ||
2f78a7ce | 2141 | /* NB: Calling finish_struct() may cause type TYPE_OBJC_INFO |
2142 | information in all variants of this RECORD_TYPE to be destroyed | |
2143 | (this is because the C frontend manipulates TYPE_LANG_SPECIFIC | |
2144 | for something else and then will change all variants to use the | |
2145 | same resulting TYPE_LANG_SPECIFIC, ignoring the fact that we use | |
2146 | it for ObjC protocols and that such propagation will make all | |
2147 | variants use the same objc_info), but it is therein that we store | |
2148 | protocol conformance info (e.g., 'NSObject <MyProtocol>'). | |
2149 | Hence, we must save the ObjC-specific information before calling | |
0bee387e | 2150 | finish_struct(), and then reinstate it afterwards. */ |
2151 | ||
2f78a7ce | 2152 | for (t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t)) |
2b03c8ee | 2153 | { |
2f78a7ce | 2154 | INIT_TYPE_OBJC_INFO (t); |
f1f41a6c | 2155 | objc_info.safe_push (TYPE_OBJC_INFO (t)); |
2b03c8ee | 2156 | } |
0bee387e | 2157 | |
0b09525f | 2158 | s = objc_finish_struct (s, fields); |
499ea517 | 2159 | |
2f78a7ce | 2160 | for (i = 0, t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++) |
8e1be677 | 2161 | { |
2f78a7ce | 2162 | /* We now want to restore the different TYPE_OBJC_INFO, but we |
2163 | have the additional problem that the C frontend doesn't just | |
2164 | copy TYPE_LANG_SPECIFIC from one variant to the other; it | |
2165 | actually makes all of them the *same* TYPE_LANG_SPECIFIC. As | |
2166 | we need a different TYPE_OBJC_INFO for each (and | |
2167 | TYPE_OBJC_INFO is a field in TYPE_LANG_SPECIFIC), we need to | |
2168 | make a copy of each TYPE_LANG_SPECIFIC before we modify | |
2169 | TYPE_OBJC_INFO. */ | |
2170 | if (TYPE_LANG_SPECIFIC (t)) | |
2171 | { | |
2172 | /* Create a copy of TYPE_LANG_SPECIFIC. */ | |
2173 | struct lang_type *old_lang_type = TYPE_LANG_SPECIFIC (t); | |
2174 | ALLOC_OBJC_TYPE_LANG_SPECIFIC (t); | |
2175 | memcpy (TYPE_LANG_SPECIFIC (t), old_lang_type, | |
2176 | SIZEOF_OBJC_TYPE_LANG_SPECIFIC); | |
2177 | } | |
2178 | else | |
2179 | { | |
2180 | /* Just create a new one. */ | |
2181 | ALLOC_OBJC_TYPE_LANG_SPECIFIC (t); | |
2182 | } | |
2183 | /* Replace TYPE_OBJC_INFO with the saved one. This restores any | |
2184 | protocol information that may have been associated with the | |
2185 | type. */ | |
f1f41a6c | 2186 | TYPE_OBJC_INFO (t) = objc_info[i]; |
2f78a7ce | 2187 | /* Replace the IDENTIFIER_NODE with an actual @interface now |
2188 | that we have it. */ | |
ceb0f408 | 2189 | TYPE_OBJC_INTERFACE (t) = klass; |
8e1be677 | 2190 | } |
f1f41a6c | 2191 | objc_info.release (); |
0bee387e | 2192 | |
499ea517 | 2193 | /* Use TYPE_BINFO structures to point at the super class, if any. */ |
2194 | objc_xref_basetypes (s, super); | |
2195 | ||
8e1be677 | 2196 | /* Mark this struct as a class template. */ |
ceb0f408 | 2197 | CLASS_STATIC_TEMPLATE (klass) = s; |
8e1be677 | 2198 | |
499ea517 | 2199 | return s; |
2200 | } | |
2201 | ||
2202 | /* Mark DECL as being 'volatile' for purposes of Darwin | |
2203 | _setjmp()/_longjmp() exception handling. Called from | |
2204 | objc_mark_locals_volatile(). */ | |
2205 | void | |
2206 | objc_volatilize_decl (tree decl) | |
2207 | { | |
2208 | /* Do not mess with variables that are 'static' or (already) | |
2209 | 'volatile'. */ | |
2210 | if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl) | |
2211 | && (TREE_CODE (decl) == VAR_DECL | |
2212 | || TREE_CODE (decl) == PARM_DECL)) | |
2213 | { | |
d7489d8d | 2214 | if (local_variables_to_volatilize == NULL) |
f1f41a6c | 2215 | vec_alloc (local_variables_to_volatilize, 8); |
499ea517 | 2216 | |
f1f41a6c | 2217 | vec_safe_push (local_variables_to_volatilize, decl); |
d7489d8d | 2218 | } |
2219 | } | |
499ea517 | 2220 | |
d7489d8d | 2221 | /* Called when parsing of a function completes; if any local variables |
2222 | in the function were marked as variables to volatilize, change them | |
2223 | to volatile. We do this at the end of the function when the | |
2224 | warnings about discarding 'volatile' have already been produced. | |
2225 | We are making the variables as volatile just to force the compiler | |
2226 | to preserve them between setjmp/longjmp, but we don't want warnings | |
2227 | for them as they aren't really volatile. */ | |
2228 | void | |
2229 | objc_finish_function (void) | |
2230 | { | |
2231 | /* If there are any local variables to volatilize, volatilize them. */ | |
2232 | if (local_variables_to_volatilize) | |
2233 | { | |
2234 | int i; | |
2235 | tree decl; | |
f1f41a6c | 2236 | FOR_EACH_VEC_ELT (*local_variables_to_volatilize, i, decl) |
d7489d8d | 2237 | { |
2238 | tree t = TREE_TYPE (decl); | |
2239 | ||
2240 | t = build_qualified_type (t, TYPE_QUALS (t) | TYPE_QUAL_VOLATILE); | |
2241 | TREE_TYPE (decl) = t; | |
2242 | TREE_THIS_VOLATILE (decl) = 1; | |
2243 | TREE_SIDE_EFFECTS (decl) = 1; | |
2244 | DECL_REGISTER (decl) = 0; | |
499ea517 | 2245 | #ifndef OBJCPLUS |
d7489d8d | 2246 | C_DECL_REGISTER (decl) = 0; |
499ea517 | 2247 | #endif |
d7489d8d | 2248 | } |
2249 | ||
2250 | /* Now we delete the vector. This sets it to NULL as well. */ | |
f1f41a6c | 2251 | vec_free (local_variables_to_volatilize); |
499ea517 | 2252 | } |
2253 | } | |
2254 | ||
2255 | /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS | |
97419919 | 2256 | (including its categories and superclasses) or by object type TYP. |
499ea517 | 2257 | Issue a warning if PROTO is not adopted anywhere and WARN is set. */ |
2258 | ||
2259 | static bool | |
2260 | objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn) | |
d7c168e8 | 2261 | { |
499ea517 | 2262 | bool class_type = (cls != NULL_TREE); |
d7c168e8 | 2263 | |
499ea517 | 2264 | while (cls) |
d7c168e8 | 2265 | { |
499ea517 | 2266 | tree c; |
2267 | ||
2268 | /* Check protocols adopted by the class and its categories. */ | |
2269 | for (c = cls; c; c = CLASS_CATEGORY_LIST (c)) | |
2270 | { | |
2271 | if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto)) | |
2272 | return true; | |
2273 | } | |
2274 | ||
2275 | /* Repeat for superclasses. */ | |
2276 | cls = lookup_interface (CLASS_SUPER_NAME (cls)); | |
d7c168e8 | 2277 | } |
499ea517 | 2278 | |
2279 | /* Check for any protocols attached directly to the object type. */ | |
2280 | if (TYPE_HAS_OBJC_INFO (typ)) | |
d7c168e8 | 2281 | { |
499ea517 | 2282 | if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto)) |
2283 | return true; | |
2284 | } | |
2285 | ||
2286 | if (warn) | |
2287 | { | |
a608187f | 2288 | *errbuf = 0; |
499ea517 | 2289 | gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ)); |
499ea517 | 2290 | /* NB: Types 'id' and 'Class' cannot reasonably be described as |
2291 | "implementing" a given protocol, since they do not have an | |
2292 | implementation. */ | |
a608187f | 2293 | if (class_type) |
2294 | warning (0, "class %qs does not implement the %qE protocol", | |
2295 | identifier_to_locale (errbuf), PROTOCOL_NAME (proto)); | |
2296 | else | |
2297 | warning (0, "type %qs does not conform to the %qE protocol", | |
2298 | identifier_to_locale (errbuf), PROTOCOL_NAME (proto)); | |
d7c168e8 | 2299 | } |
499ea517 | 2300 | |
2301 | return false; | |
d7c168e8 | 2302 | } |
2303 | ||
499ea517 | 2304 | /* Check if class RCLS and instance struct type RTYP conform to at least the |
2305 | same protocols that LCLS and LTYP conform to. */ | |
2306 | ||
2307 | static bool | |
2308 | objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn) | |
2309 | { | |
2310 | tree p; | |
2311 | bool have_lproto = false; | |
2312 | ||
2313 | while (lcls) | |
2314 | { | |
2315 | /* NB: We do _not_ look at categories defined for LCLS; these may or | |
2316 | may not get loaded in, and therefore it is unreasonable to require | |
2317 | that RCLS/RTYP must implement any of their protocols. */ | |
2318 | for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p)) | |
2319 | { | |
2320 | have_lproto = true; | |
d7c168e8 | 2321 | |
499ea517 | 2322 | if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn)) |
2323 | return warn; | |
2324 | } | |
02c2efaa | 2325 | |
499ea517 | 2326 | /* Repeat for superclasses. */ |
2327 | lcls = lookup_interface (CLASS_SUPER_NAME (lcls)); | |
2328 | } | |
02c2efaa | 2329 | |
499ea517 | 2330 | /* Check for any protocols attached directly to the object type. */ |
2331 | if (TYPE_HAS_OBJC_INFO (ltyp)) | |
2332 | { | |
2333 | for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p)) | |
2334 | { | |
2335 | have_lproto = true; | |
fadbd30d | 2336 | |
499ea517 | 2337 | if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn)) |
2338 | return warn; | |
2339 | } | |
2340 | } | |
2341 | ||
2342 | /* NB: If LTYP and LCLS have no protocols to search for, return 'true' | |
2343 | vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get | |
2344 | away with simply checking for 'id' or 'Class' (!RCLS), since this | |
2345 | routine will not get called in other cases. */ | |
2346 | return have_lproto || (rcls != NULL_TREE); | |
2347 | } | |
2348 | ||
4abfc532 | 2349 | /* Given two types TYPE1 and TYPE2, return their least common ancestor. |
2350 | Both TYPE1 and TYPE2 must be pointers, and already determined to be | |
2351 | compatible by objc_compare_types() below. */ | |
2352 | ||
2353 | tree | |
2354 | objc_common_type (tree type1, tree type2) | |
2355 | { | |
2356 | tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2); | |
2357 | ||
2358 | while (POINTER_TYPE_P (inner1)) | |
2359 | { | |
2360 | inner1 = TREE_TYPE (inner1); | |
2361 | inner2 = TREE_TYPE (inner2); | |
2362 | } | |
2363 | ||
2364 | /* If one type is derived from another, return the base type. */ | |
2365 | if (DERIVED_FROM_P (inner1, inner2)) | |
2366 | return type1; | |
2367 | else if (DERIVED_FROM_P (inner2, inner1)) | |
2368 | return type2; | |
2369 | ||
2370 | /* If both types are 'Class', return 'Class'. */ | |
2371 | if (objc_is_class_id (inner1) && objc_is_class_id (inner2)) | |
2372 | return objc_class_type; | |
2373 | ||
2374 | /* Otherwise, return 'id'. */ | |
2375 | return objc_object_type; | |
2376 | } | |
2377 | ||
499ea517 | 2378 | /* Determine if it is permissible to assign (if ARGNO is greater than -3) |
2379 | an instance of RTYP to an instance of LTYP or to compare the two | |
2380 | (if ARGNO is equal to -3), per ObjC type system rules. Before | |
2381 | returning 'true', this routine may issue warnings related to, e.g., | |
2382 | protocol conformance. When returning 'false', the routine must | |
2383 | produce absolutely no warnings; the C or C++ front-end will do so | |
ad312852 | 2384 | instead, if needed. If either LTYP or RTYP is not an Objective-C |
2385 | type, the routine must return 'false'. | |
499ea517 | 2386 | |
2387 | The ARGNO parameter is encoded as follows: | |
2388 | >= 1 Parameter number (CALLEE contains function being called); | |
2389 | 0 Return value; | |
2390 | -1 Assignment; | |
2391 | -2 Initialization; | |
a198e083 | 2392 | -3 Comparison (LTYP and RTYP may match in either direction); |
ad312852 | 2393 | -4 Silent comparison (for C++ overload resolution); |
6bbb4715 | 2394 | -5 Silent "specialization" comparison for RTYP to be a "specialization" |
2395 | of LTYP (a specialization means that RTYP is LTYP plus some constraints, | |
ad312852 | 2396 | so that each object of type RTYP is also of type LTYP). This is used |
2397 | when comparing property types. */ | |
499ea517 | 2398 | |
2399 | bool | |
2400 | objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee) | |
fc304191 | 2401 | { |
499ea517 | 2402 | tree lcls, rcls, lproto, rproto; |
2403 | bool pointers_compatible; | |
2404 | ||
2405 | /* We must be dealing with pointer types */ | |
2406 | if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp)) | |
2407 | return false; | |
c450c529 | 2408 | |
499ea517 | 2409 | do |
c450c529 | 2410 | { |
499ea517 | 2411 | ltyp = TREE_TYPE (ltyp); /* Remove indirections. */ |
2412 | rtyp = TREE_TYPE (rtyp); | |
2413 | } | |
2414 | while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp)); | |
c450c529 | 2415 | |
b24ba8fa | 2416 | /* We must also handle function pointers, since ObjC is a bit more |
2417 | lenient than C or C++ on this. */ | |
2418 | if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE) | |
2419 | { | |
dffb24a6 | 2420 | function_args_iterator liter, riter; |
2421 | ||
b24ba8fa | 2422 | /* Return types must be covariant. */ |
2423 | if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp)) | |
2424 | && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp), | |
2425 | argno, callee)) | |
2426 | return false; | |
2427 | ||
2428 | /* Argument types must be contravariant. */ | |
dffb24a6 | 2429 | function_args_iter_init (&liter, ltyp); |
2430 | function_args_iter_init (&riter, rtyp); | |
2431 | ||
2432 | while (1) | |
b24ba8fa | 2433 | { |
dffb24a6 | 2434 | ltyp = function_args_iter_cond (&liter); |
2435 | rtyp = function_args_iter_cond (&riter); | |
2436 | ||
2437 | /* If we've exhaused both lists simulateously, we're done. */ | |
2438 | if (ltyp == NULL_TREE && rtyp == NULL_TREE) | |
2439 | break; | |
2440 | ||
2441 | /* If one list is shorter than the other, they fail to match. */ | |
2442 | if (ltyp == NULL_TREE || rtyp == NULL_TREE) | |
b24ba8fa | 2443 | return false; |
b24ba8fa | 2444 | |
dffb24a6 | 2445 | if (!comptypes (rtyp, ltyp) |
2446 | && !objc_compare_types (rtyp, ltyp, argno, callee)) | |
2447 | return false; | |
2448 | ||
2449 | function_args_iter_next (&liter); | |
2450 | function_args_iter_next (&riter); | |
2451 | } | |
2452 | ||
2453 | return true; | |
b24ba8fa | 2454 | } |
2455 | ||
499ea517 | 2456 | /* Past this point, we are only interested in ObjC class instances, |
2457 | or 'id' or 'Class'. */ | |
2458 | if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE) | |
2459 | return false; | |
c450c529 | 2460 | |
499ea517 | 2461 | if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp) |
2462 | && !TYPE_HAS_OBJC_INFO (ltyp)) | |
2463 | return false; | |
9e8a7e85 | 2464 | |
499ea517 | 2465 | if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp) |
2466 | && !TYPE_HAS_OBJC_INFO (rtyp)) | |
2467 | return false; | |
39b8ddc6 | 2468 | |
a198e083 | 2469 | /* Past this point, we are committed to returning 'true' to the caller |
2470 | (unless performing a silent comparison; see below). However, we can | |
2471 | still warn about type and/or protocol mismatches. */ | |
c450c529 | 2472 | |
499ea517 | 2473 | if (TYPE_HAS_OBJC_INFO (ltyp)) |
2474 | { | |
2475 | lcls = TYPE_OBJC_INTERFACE (ltyp); | |
2476 | lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp); | |
2477 | } | |
2478 | else | |
2479 | lcls = lproto = NULL_TREE; | |
c450c529 | 2480 | |
499ea517 | 2481 | if (TYPE_HAS_OBJC_INFO (rtyp)) |
2482 | { | |
2483 | rcls = TYPE_OBJC_INTERFACE (rtyp); | |
2484 | rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp); | |
2485 | } | |
2486 | else | |
2487 | rcls = rproto = NULL_TREE; | |
9e8a7e85 | 2488 | |
8e1be677 | 2489 | /* If we could not find an @interface declaration, we must have |
2490 | only seen a @class declaration; for purposes of type comparison, | |
2491 | treat it as a stand-alone (root) class. */ | |
2492 | ||
2493 | if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE) | |
2494 | lcls = NULL_TREE; | |
2495 | ||
2496 | if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE) | |
2497 | rcls = NULL_TREE; | |
2498 | ||
ad312852 | 2499 | /* If either type is an unqualified 'id', we're done. This is because |
2500 | an 'id' can be assigned to or from any type with no warnings. */ | |
2501 | if (argno != -5) | |
2502 | { | |
2503 | if ((!lproto && objc_is_object_id (ltyp)) | |
2504 | || (!rproto && objc_is_object_id (rtyp))) | |
2505 | return true; | |
2506 | } | |
2507 | else | |
2508 | { | |
2509 | /* For property checks, though, an 'id' is considered the most | |
2510 | general type of object, hence if you try to specialize an | |
2511 | 'NSArray *' (ltyp) property with an 'id' (rtyp) one, we need | |
2512 | to warn. */ | |
2513 | if (!lproto && objc_is_object_id (ltyp)) | |
2514 | return true; | |
2515 | } | |
6bbb4715 | 2516 | |
499ea517 | 2517 | pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp)); |
e0ece38e | 2518 | |
499ea517 | 2519 | /* If the underlying types are the same, and at most one of them has |
2520 | a protocol list, we do not need to issue any diagnostics. */ | |
2521 | if (pointers_compatible && (!lproto || !rproto)) | |
2522 | return true; | |
9e8a7e85 | 2523 | |
499ea517 | 2524 | /* If exactly one of the types is 'Class', issue a diagnostic; any |
2525 | exceptions of this rule have already been handled. */ | |
2526 | if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp)) | |
2527 | pointers_compatible = false; | |
2528 | /* Otherwise, check for inheritance relations. */ | |
2529 | else | |
2530 | { | |
2531 | if (!pointers_compatible) | |
ad312852 | 2532 | { |
2533 | /* Again, if any of the two is an 'id', we're satisfied, | |
2534 | unless we're comparing properties, in which case only an | |
2535 | 'id' on the left-hand side (old property) is good | |
2536 | enough. */ | |
2537 | if (argno != -5) | |
2538 | pointers_compatible | |
2539 | = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp)); | |
2540 | else | |
6bbb4715 | 2541 | pointers_compatible = objc_is_object_id (ltyp); |
ad312852 | 2542 | } |
499ea517 | 2543 | |
2544 | if (!pointers_compatible) | |
2545 | pointers_compatible = DERIVED_FROM_P (ltyp, rtyp); | |
2546 | ||
ad312852 | 2547 | if (!pointers_compatible && (argno == -3 || argno == -4)) |
499ea517 | 2548 | pointers_compatible = DERIVED_FROM_P (rtyp, ltyp); |
2549 | } | |
2550 | ||
2551 | /* If the pointers match modulo protocols, check for protocol conformance | |
2552 | mismatches. */ | |
2553 | if (pointers_compatible) | |
2554 | { | |
2555 | pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp, | |
2556 | argno != -3); | |
2557 | ||
2558 | if (!pointers_compatible && argno == -3) | |
2559 | pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp, | |
2560 | argno != -3); | |
2561 | } | |
2562 | ||
2563 | if (!pointers_compatible) | |
2564 | { | |
a198e083 | 2565 | /* The two pointers are not exactly compatible. Issue a warning, unless |
2566 | we are performing a silent comparison, in which case return 'false' | |
2567 | instead. */ | |
499ea517 | 2568 | /* NB: For the time being, we shall make our warnings look like their |
2569 | C counterparts. In the future, we may wish to make them more | |
2570 | ObjC-specific. */ | |
2571 | switch (argno) | |
02c2efaa | 2572 | { |
ad312852 | 2573 | case -5: |
a198e083 | 2574 | case -4: |
2575 | return false; | |
2576 | ||
499ea517 | 2577 | case -3: |
2578 | warning (0, "comparison of distinct Objective-C types lacks a cast"); | |
2579 | break; | |
2580 | ||
2581 | case -2: | |
2582 | warning (0, "initialization from distinct Objective-C type"); | |
2583 | break; | |
2584 | ||
2585 | case -1: | |
2586 | warning (0, "assignment from distinct Objective-C type"); | |
2587 | break; | |
2588 | ||
2589 | case 0: | |
2590 | warning (0, "distinct Objective-C type in return"); | |
2591 | break; | |
2592 | ||
2593 | default: | |
2594 | warning (0, "passing argument %d of %qE from distinct " | |
2595 | "Objective-C type", argno, callee); | |
2596 | break; | |
02c2efaa | 2597 | } |
c450c529 | 2598 | } |
e0ece38e | 2599 | |
499ea517 | 2600 | return true; |
2601 | } | |
2602 | ||
4abfc532 | 2603 | /* This routine is similar to objc_compare_types except that function-pointers are |
2604 | excluded. This is because, caller assumes that common types are of (id, Object*) | |
2605 | variety and calls objc_common_type to obtain a common type. There is no commonolty | |
2606 | between two function-pointers in this regard. */ | |
2607 | ||
6bbb4715 | 2608 | bool |
4abfc532 | 2609 | objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee) |
2610 | { | |
2611 | if (objc_compare_types (ltyp, rtyp, argno, callee)) | |
2612 | { | |
2613 | /* exclude function-pointer types. */ | |
2614 | do | |
2615 | { | |
2616 | ltyp = TREE_TYPE (ltyp); /* Remove indirections. */ | |
2617 | rtyp = TREE_TYPE (rtyp); | |
2618 | } | |
2619 | while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp)); | |
2620 | return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE); | |
2621 | } | |
2622 | return false; | |
2623 | } | |
2624 | ||
499ea517 | 2625 | #ifndef OBJCPLUS |
2626 | /* Determine if CHILD is derived from PARENT. The routine assumes that | |
2627 | both parameters are RECORD_TYPEs, and is non-reflexive. */ | |
2628 | ||
2629 | static bool | |
2630 | objc_derived_from_p (tree parent, tree child) | |
2631 | { | |
2632 | parent = TYPE_MAIN_VARIANT (parent); | |
2633 | ||
2634 | for (child = TYPE_MAIN_VARIANT (child); | |
2635 | TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));) | |
02c2efaa | 2636 | { |
499ea517 | 2637 | child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO |
2638 | (TYPE_BINFO (child), | |
2639 | 0))); | |
2640 | ||
2641 | if (child == parent) | |
2642 | return true; | |
02c2efaa | 2643 | } |
fc304191 | 2644 | |
499ea517 | 2645 | return false; |
2646 | } | |
2647 | #endif | |
2648 | ||
267785bc | 2649 | tree |
499ea517 | 2650 | objc_build_component_ref (tree datum, tree component) |
2651 | { | |
2652 | /* If COMPONENT is NULL, the caller is referring to the anonymous | |
2653 | base class field. */ | |
2654 | if (!component) | |
2655 | { | |
2656 | tree base = TYPE_FIELDS (TREE_TYPE (datum)); | |
fc304191 | 2657 | |
499ea517 | 2658 | return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE); |
2659 | } | |
fc304191 | 2660 | |
499ea517 | 2661 | /* The 'build_component_ref' routine has been removed from the C++ |
2662 | front-end, but 'finish_class_member_access_expr' seems to be | |
2663 | a worthy substitute. */ | |
2664 | #ifdef OBJCPLUS | |
717ecce9 | 2665 | return finish_class_member_access_expr (datum, component, false, |
2666 | tf_warning_or_error); | |
499ea517 | 2667 | #else |
e60a6f7b | 2668 | return build_component_ref (input_location, datum, component); |
499ea517 | 2669 | #endif |
2670 | } | |
fc304191 | 2671 | |
499ea517 | 2672 | /* Recursively copy inheritance information rooted at BINFO. To do this, |
2673 | we emulate the song and dance performed by cp/tree.c:copy_binfo(). */ | |
beed1ffc | 2674 | |
499ea517 | 2675 | static tree |
2676 | objc_copy_binfo (tree binfo) | |
2677 | { | |
2678 | tree btype = BINFO_TYPE (binfo); | |
2679 | tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo)); | |
2680 | tree base_binfo; | |
2681 | int ix; | |
fc304191 | 2682 | |
499ea517 | 2683 | BINFO_TYPE (binfo2) = btype; |
2684 | BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo); | |
2685 | BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo); | |
2686 | ||
2687 | /* Recursively copy base binfos of BINFO. */ | |
2688 | for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++) | |
fc304191 | 2689 | { |
499ea517 | 2690 | tree base_binfo2 = objc_copy_binfo (base_binfo); |
2691 | ||
2692 | BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2; | |
2693 | BINFO_BASE_APPEND (binfo2, base_binfo2); | |
2694 | } | |
2695 | ||
2696 | return binfo2; | |
2697 | } | |
2698 | ||
2699 | /* Record superclass information provided in BASETYPE for ObjC class REF. | |
2700 | This is loosely based on cp/decl.c:xref_basetypes(). */ | |
fc304191 | 2701 | |
499ea517 | 2702 | static void |
2703 | objc_xref_basetypes (tree ref, tree basetype) | |
2704 | { | |
d8d5c9af | 2705 | tree variant; |
499ea517 | 2706 | tree binfo = make_tree_binfo (basetype ? 1 : 0); |
499ea517 | 2707 | TYPE_BINFO (ref) = binfo; |
2708 | BINFO_OFFSET (binfo) = size_zero_node; | |
2709 | BINFO_TYPE (binfo) = ref; | |
c450c529 | 2710 | |
d8d5c9af | 2711 | gcc_assert (TYPE_MAIN_VARIANT (ref) == ref); |
2712 | for (variant = ref; variant; variant = TYPE_NEXT_VARIANT (variant)) | |
2713 | TYPE_BINFO (variant) = binfo; | |
2714 | ||
499ea517 | 2715 | if (basetype) |
2716 | { | |
2717 | tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype)); | |
c450c529 | 2718 | |
499ea517 | 2719 | BINFO_INHERITANCE_CHAIN (base_binfo) = binfo; |
f1f41a6c | 2720 | vec_alloc (BINFO_BASE_ACCESSES (binfo), 1); |
499ea517 | 2721 | BINFO_BASE_APPEND (binfo, base_binfo); |
2722 | BINFO_BASE_ACCESS_APPEND (binfo, access_public_node); | |
fc304191 | 2723 | } |
499ea517 | 2724 | } |
2725 | ||
c17b85ea | 2726 | /* Called from finish_decl. */ |
fc304191 | 2727 | |
c450c529 | 2728 | void |
39b8ddc6 | 2729 | objc_check_decl (tree decl) |
c450c529 | 2730 | { |
2731 | tree type = TREE_TYPE (decl); | |
2732 | ||
c17b85ea | 2733 | if (TREE_CODE (type) != RECORD_TYPE) |
2734 | return; | |
66eb9e7e | 2735 | if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type)))) |
a608187f | 2736 | error ("statically allocated instance of Objective-C class %qE", |
2737 | type); | |
c450c529 | 2738 | } |
2739 | ||
e147d6aa | 2740 | void |
2741 | objc_check_global_decl (tree decl) | |
2742 | { | |
2743 | tree id = DECL_NAME (decl); | |
2744 | if (objc_is_class_name (id) && global_bindings_p()) | |
2745 | error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id)); | |
2746 | } | |
2747 | ||
2f78a7ce | 2748 | /* Construct a PROTOCOLS-qualified variant of INTERFACE, where |
2749 | INTERFACE may either name an Objective-C class, or refer to the | |
2750 | special 'id' or 'Class' types. If INTERFACE is not a valid ObjC | |
2751 | type, just return it unchanged. This function is often called when | |
2752 | PROTOCOLS is NULL_TREE, in which case we simply look up the | |
2753 | appropriate INTERFACE. */ | |
fc304191 | 2754 | |
2755 | tree | |
13dcc150 | 2756 | objc_get_protocol_qualified_type (tree interface, tree protocols) |
c450c529 | 2757 | { |
6972a3bc | 2758 | /* If INTERFACE is not provided, default to 'id'. */ |
2759 | tree type = (interface ? objc_is_id (interface) : objc_object_type); | |
2760 | bool is_ptr = (type != NULL_TREE); | |
c450c529 | 2761 | |
6972a3bc | 2762 | if (!is_ptr) |
c450c529 | 2763 | { |
13dcc150 | 2764 | type = objc_is_class_name (interface); |
c450c529 | 2765 | |
13dcc150 | 2766 | if (type) |
b24ba8fa | 2767 | { |
2768 | /* If looking at a typedef, retrieve the precise type it | |
2769 | describes. */ | |
2770 | if (TREE_CODE (interface) == IDENTIFIER_NODE) | |
2771 | interface = identifier_global_value (interface); | |
2772 | ||
2773 | type = ((interface && TREE_CODE (interface) == TYPE_DECL | |
2774 | && DECL_ORIGINAL_TYPE (interface)) | |
2775 | ? DECL_ORIGINAL_TYPE (interface) | |
2776 | : xref_tag (RECORD_TYPE, type)); | |
2777 | } | |
13dcc150 | 2778 | else |
e5479368 | 2779 | { |
2780 | /* This case happens when we are given an 'interface' which | |
2781 | is not a valid class name. For example if a typedef was | |
2782 | used, and 'interface' really is the identifier of the | |
2783 | typedef, but when you resolve it you don't get an | |
2784 | Objective-C class, but something else, such as 'int'. | |
2785 | This is an error; protocols make no sense unless you use | |
2786 | them with Objective-C objects. */ | |
2787 | error_at (input_location, "only Objective-C object types can be qualified with a protocol"); | |
2788 | ||
2789 | /* Try to recover. Ignore the invalid class name, and treat | |
2790 | the object as an 'id' to silence further warnings about | |
2791 | the class. */ | |
2792 | type = objc_object_type; | |
2793 | is_ptr = true; | |
2794 | } | |
13dcc150 | 2795 | } |
e0ece38e | 2796 | |
c450c529 | 2797 | if (protocols) |
2798 | { | |
1561d3cd | 2799 | type = build_variant_type_copy (type); |
13dcc150 | 2800 | |
6972a3bc | 2801 | /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s) |
2802 | to the pointee. */ | |
2803 | if (is_ptr) | |
2804 | { | |
6753bca0 | 2805 | tree orig_pointee_type = TREE_TYPE (type); |
2806 | TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type); | |
2807 | ||
2808 | /* Set up the canonical type information. */ | |
6bbb4715 | 2809 | TYPE_CANONICAL (type) |
6753bca0 | 2810 | = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type)); |
2811 | ||
6972a3bc | 2812 | TYPE_POINTER_TO (TREE_TYPE (type)) = type; |
2813 | type = TREE_TYPE (type); | |
2814 | } | |
2815 | ||
2816 | /* Look up protocols and install in lang specific list. */ | |
2817 | DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type)); | |
89e83a91 | 2818 | TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols |
2819 | (protocols, /* definition_required */ false); | |
6972a3bc | 2820 | |
2821 | /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class', | |
2822 | return the pointer to the new pointee variant. */ | |
2823 | if (is_ptr) | |
2824 | type = TYPE_POINTER_TO (type); | |
2825 | else | |
2826 | TYPE_OBJC_INTERFACE (type) | |
2827 | = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type)); | |
c450c529 | 2828 | } |
13dcc150 | 2829 | |
c450c529 | 2830 | return type; |
2831 | } | |
2832 | ||
b11c0115 | 2833 | /* Check for circular dependencies in protocols. The arguments are |
2834 | PROTO, the protocol to check, and LIST, a list of protocol it | |
2835 | conforms to. */ | |
2836 | ||
39b8ddc6 | 2837 | static void |
2838 | check_protocol_recursively (tree proto, tree list) | |
b11c0115 | 2839 | { |
2840 | tree p; | |
2841 | ||
2842 | for (p = list; p; p = TREE_CHAIN (p)) | |
2843 | { | |
2844 | tree pp = TREE_VALUE (p); | |
2845 | ||
2846 | if (TREE_CODE (pp) == IDENTIFIER_NODE) | |
89e83a91 | 2847 | pp = lookup_protocol (pp, /* warn if deprecated */ false, |
2848 | /* definition_required */ false); | |
b11c0115 | 2849 | |
2850 | if (pp == proto) | |
c05be867 | 2851 | fatal_error (input_location, "protocol %qE has circular dependency", |
a608187f | 2852 | PROTOCOL_NAME (pp)); |
b11c0115 | 2853 | if (pp) |
2854 | check_protocol_recursively (proto, PROTOCOL_LIST (pp)); | |
2855 | } | |
2856 | } | |
2857 | ||
c213e196 | 2858 | /* Look up PROTOCOLS, and return a list of those that are found. If |
2859 | none are found, return NULL. Note that this function will emit a | |
89e83a91 | 2860 | warning if a protocol is found and is deprecated. If |
2861 | 'definition_required', then warn if the protocol is found but is | |
2862 | not defined (ie, if we only saw a forward-declaration of the | |
2863 | protocol (as in "@protocol NSObject;") not a real definition with | |
2864 | the list of methods). */ | |
c450c529 | 2865 | static tree |
89e83a91 | 2866 | lookup_and_install_protocols (tree protocols, bool definition_required) |
fc304191 | 2867 | { |
c450c529 | 2868 | tree proto; |
c17b85ea | 2869 | tree return_value = NULL_TREE; |
c450c529 | 2870 | |
e1f293c0 | 2871 | if (protocols == error_mark_node) |
2872 | return NULL; | |
2873 | ||
c450c529 | 2874 | for (proto = protocols; proto; proto = TREE_CHAIN (proto)) |
2875 | { | |
2876 | tree ident = TREE_VALUE (proto); | |
89e83a91 | 2877 | tree p = lookup_protocol (ident, /* warn_if_deprecated */ true, |
2878 | definition_required); | |
c450c529 | 2879 | |
77874a3c | 2880 | if (p) |
c17b85ea | 2881 | return_value = chainon (return_value, |
2882 | build_tree_list (NULL_TREE, p)); | |
77874a3c | 2883 | else if (ident != error_mark_node) |
a608187f | 2884 | error ("cannot find protocol declaration for %qE", |
2885 | ident); | |
c450c529 | 2886 | } |
e0ece38e | 2887 | |
c450c529 | 2888 | return return_value; |
fc304191 | 2889 | } |
2890 | ||
4d4b71d3 | 2891 | static void |
267785bc | 2892 | build_common_objc_exception_stuff (void) |
13dcc150 | 2893 | { |
267785bc | 2894 | tree noreturn_list, nothrow_list, temp_type; |
13dcc150 | 2895 | |
267785bc | 2896 | noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL); |
2897 | nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL); | |
13dcc150 | 2898 | |
267785bc | 2899 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
2900 | /* void objc_sync_enter(id); */ | |
2901 | /* void objc_sync_exit(id); */ | |
2902 | temp_type = build_function_type_list (void_type_node, | |
2903 | objc_object_type, | |
2904 | NULL_TREE); | |
2905 | objc_exception_throw_decl | |
2906 | = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL, | |
2907 | noreturn_list); | |
2908 | /* Make sure that objc_exception_throw (id) claims that it may throw an | |
2909 | exception. */ | |
2910 | TREE_NOTHROW (objc_exception_throw_decl) = 0; | |
13dcc150 | 2911 | |
267785bc | 2912 | objc_sync_enter_decl |
2913 | = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN, | |
2914 | NULL, nothrow_list); | |
13dcc150 | 2915 | |
267785bc | 2916 | objc_sync_exit_decl |
2917 | = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN, | |
2918 | NULL, nothrow_list); | |
4d4b71d3 | 2919 | } |
2920 | ||
e0ece38e | 2921 | /* Purpose: "play" parser, creating/installing representations |
c450c529 | 2922 | of the declarations that are required by Objective-C. |
2923 | ||
e0ece38e | 2924 | Model: |
c450c529 | 2925 | |
39b8ddc6 | 2926 | type_spec--------->sc_spec |
2927 | (tree_list) (tree_list) | |
2928 | | | | |
2929 | | | | |
2930 | identifier_node identifier_node */ | |
c450c529 | 2931 | |
fc304191 | 2932 | static void |
39b8ddc6 | 2933 | synth_module_prologue (void) |
fc304191 | 2934 | { |
13dcc150 | 2935 | tree type; |
2936 | enum debug_info_type save_write_symbols = write_symbols; | |
2937 | const struct gcc_debug_hooks *const save_hooks = debug_hooks; | |
2938 | ||
2939 | /* Suppress outputting debug symbols, because | |
08cc44e7 | 2940 | dbxout_init hasn't been called yet. */ |
13dcc150 | 2941 | write_symbols = NO_DEBUG; |
2942 | debug_hooks = &do_nothing_debug_hooks; | |
2943 | ||
2944 | #ifdef OBJCPLUS | |
2945 | push_lang_context (lang_name_c); /* extern "C" */ | |
2946 | #endif | |
2947 | ||
2948 | /* The following are also defined in <objc/objc.h> and friends. */ | |
fc304191 | 2949 | |
fc304191 | 2950 | objc_object_id = get_identifier (TAG_OBJECT); |
13dcc150 | 2951 | objc_class_id = get_identifier (TAG_CLASS); |
fc304191 | 2952 | |
2953 | objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id); | |
13dcc150 | 2954 | objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id); |
6db57367 | 2955 | |
13dcc150 | 2956 | objc_object_type = build_pointer_type (objc_object_reference); |
2957 | objc_class_type = build_pointer_type (objc_class_reference); | |
fc304191 | 2958 | |
13dcc150 | 2959 | objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME); |
2960 | objc_class_name = get_identifier (CLASS_TYPEDEF_NAME); | |
fc304191 | 2961 | |
13dcc150 | 2962 | /* Declare the 'id' and 'Class' typedefs. */ |
e60a6f7b | 2963 | type = lang_hooks.decls.pushdecl (build_decl (input_location, |
2964 | TYPE_DECL, | |
13dcc150 | 2965 | objc_object_name, |
2966 | objc_object_type)); | |
7cb6f926 | 2967 | TREE_NO_WARNING (type) = 1; |
267785bc | 2968 | |
e60a6f7b | 2969 | type = lang_hooks.decls.pushdecl (build_decl (input_location, |
2970 | TYPE_DECL, | |
13dcc150 | 2971 | objc_class_name, |
2972 | objc_class_type)); | |
7cb6f926 | 2973 | TREE_NO_WARNING (type) = 1; |
13dcc150 | 2974 | |
2975 | /* Forward-declare '@interface Protocol'. */ | |
13dcc150 | 2976 | type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME); |
29d7200d | 2977 | objc_declare_class (type); |
267785bc | 2978 | objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE, type)); |
fc304191 | 2979 | |
01a43715 | 2980 | /* Declare receiver type used for dispatching messages to 'super'. */ |
c17b85ea | 2981 | /* `struct objc_super *' */ |
e5876878 | 2982 | objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE, |
2983 | get_identifier (TAG_SUPER))); | |
c450c529 | 2984 | |
0ced0389 | 2985 | /* Declare pointers to method and ivar lists. */ |
2986 | objc_method_list_ptr = build_pointer_type | |
2987 | (xref_tag (RECORD_TYPE, | |
2988 | get_identifier (UTAG_METHOD_LIST))); | |
2989 | objc_method_proto_list_ptr | |
2990 | = build_pointer_type (xref_tag (RECORD_TYPE, | |
2991 | get_identifier (UTAG_METHOD_PROTOTYPE_LIST))); | |
2992 | objc_ivar_list_ptr = build_pointer_type | |
2993 | (xref_tag (RECORD_TYPE, | |
2994 | get_identifier (UTAG_IVAR_LIST))); | |
2995 | ||
267785bc | 2996 | build_common_objc_exception_stuff (); |
fc304191 | 2997 | |
267785bc | 2998 | /* Set-up runtime-specific templates, message and exception stuff. */ |
2999 | (*runtime.initialize) (); | |
13dcc150 | 3000 | |
7fd68cee | 3001 | /* Declare objc_getProperty, object_setProperty and other property |
3002 | accessor helpers. */ | |
267785bc | 3003 | build_common_objc_property_accessor_helpers (); |
c450c529 | 3004 | |
c450c529 | 3005 | /* Forward declare constant_string_id and constant_string_type. */ |
4411d0ab | 3006 | if (!constant_string_class_name) |
267785bc | 3007 | constant_string_class_name = runtime.default_constant_string_class_name; |
4411d0ab | 3008 | constant_string_id = get_identifier (constant_string_class_name); |
29d7200d | 3009 | objc_declare_class (constant_string_id); |
c17b85ea | 3010 | |
3011 | /* Pre-build the following entities - for speed/convenience. */ | |
3012 | self_id = get_identifier ("self"); | |
3013 | ucmd_id = get_identifier ("_cmd"); | |
13dcc150 | 3014 | |
0a65c3bb | 3015 | /* Declare struct _objc_fast_enumeration_state { ... }; */ |
3016 | build_fast_enumeration_state_template (); | |
6bbb4715 | 3017 | |
0a65c3bb | 3018 | /* void objc_enumeration_mutation (id) */ |
820ecd67 | 3019 | type = build_function_type_list (void_type_node, |
3020 | objc_object_type, NULL_TREE); | |
6bbb4715 | 3021 | objc_enumeration_mutation_decl |
3022 | = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN, | |
0a65c3bb | 3023 | NULL, NULL_TREE); |
3024 | TREE_NOTHROW (objc_enumeration_mutation_decl) = 0; | |
3025 | ||
13dcc150 | 3026 | #ifdef OBJCPLUS |
3027 | pop_lang_context (); | |
3028 | #endif | |
3029 | ||
3030 | write_symbols = save_write_symbols; | |
3031 | debug_hooks = save_hooks; | |
fc304191 | 3032 | } |
3033 | ||
267785bc | 3034 | /* --- const strings --- */ |
3035 | ||
c17b85ea | 3036 | /* Ensure that the ivar list for NSConstantString/NXConstantString |
3037 | (or whatever was specified via `-fconstant-string-class') | |
3038 | contains fields at least as large as the following three, so that | |
3039 | the runtime can stomp on them with confidence: | |
fd46afbf | 3040 | |
39b8ddc6 | 3041 | struct STRING_OBJECT_CLASS_NAME |
fd46afbf | 3042 | { |
3043 | Object isa; | |
3044 | char *cString; | |
3045 | unsigned int length; | |
3046 | }; */ | |
3047 | ||
c17b85ea | 3048 | static int |
3049 | check_string_class_template (void) | |
fd46afbf | 3050 | { |
0616a948 | 3051 | tree field_decl = objc_get_class_ivars (constant_string_id); |
fd46afbf | 3052 | |
c17b85ea | 3053 | #define AT_LEAST_AS_LARGE_AS(F, T) \ |
3054 | (F && TREE_CODE (F) == FIELD_DECL \ | |
f9ae6f95 | 3055 | && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \ |
3056 | >= TREE_INT_CST_LOW (TYPE_SIZE (T)))) | |
fd46afbf | 3057 | |
c17b85ea | 3058 | if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node)) |
3059 | return 0; | |
fd46afbf | 3060 | |
1767a056 | 3061 | field_decl = DECL_CHAIN (field_decl); |
c17b85ea | 3062 | if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node)) |
3063 | return 0; | |
3064 | ||
1767a056 | 3065 | field_decl = DECL_CHAIN (field_decl); |
c17b85ea | 3066 | return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node); |
fd46afbf | 3067 | |
c17b85ea | 3068 | #undef AT_LEAST_AS_LARGE_AS |
fd46afbf | 3069 | } |
3070 | ||
c17b85ea | 3071 | /* Avoid calling `check_string_class_template ()' more than once. */ |
3072 | static GTY(()) int string_layout_checked; | |
3073 | ||
0616a948 | 3074 | /* Construct an internal string layout to be used as a template for |
3075 | creating NSConstantString/NXConstantString instances. */ | |
3076 | ||
3077 | static tree | |
3078 | objc_build_internal_const_str_type (void) | |
3079 | { | |
3080 | tree type = (*lang_hooks.types.make_type) (RECORD_TYPE); | |
e60a6f7b | 3081 | tree fields = build_decl (input_location, |
3082 | FIELD_DECL, NULL_TREE, ptr_type_node); | |
3083 | tree field = build_decl (input_location, | |
3084 | FIELD_DECL, NULL_TREE, ptr_type_node); | |
0616a948 | 3085 | |
1767a056 | 3086 | DECL_CHAIN (field) = fields; fields = field; |
e60a6f7b | 3087 | field = build_decl (input_location, |
3088 | FIELD_DECL, NULL_TREE, unsigned_type_node); | |
1767a056 | 3089 | DECL_CHAIN (field) = fields; fields = field; |
0616a948 | 3090 | /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in |
3091 | reverse order! */ | |
3092 | finish_builtin_struct (type, "__builtin_ObjCString", | |
3093 | fields, NULL_TREE); | |
3094 | ||
3095 | return type; | |
3096 | } | |
3097 | ||
c450c529 | 3098 | /* Custom build_string which sets TREE_TYPE! */ |
3099 | ||
267785bc | 3100 | tree |
39b8ddc6 | 3101 | my_build_string (int len, const char *str) |
fc304191 | 3102 | { |
070236f0 | 3103 | return fix_string_type (build_string (len, str)); |
c450c529 | 3104 | } |
3105 | ||
f14c8207 | 3106 | /* Build a string with contents STR and length LEN and convert it to a |
3107 | pointer. */ | |
3108 | ||
267785bc | 3109 | tree |
f14c8207 | 3110 | my_build_string_pointer (int len, const char *str) |
3111 | { | |
3112 | tree string = my_build_string (len, str); | |
3113 | tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string))); | |
3114 | return build1 (ADDR_EXPR, ptrtype, string); | |
3115 | } | |
13dcc150 | 3116 | |
2ef51f0e | 3117 | hashval_t |
3118 | objc_string_hasher::hash (string_descriptor *ptr) | |
13dcc150 | 3119 | { |
2ef51f0e | 3120 | const_tree const str = ptr->literal; |
13dcc150 | 3121 | const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str); |
3122 | int i, len = TREE_STRING_LENGTH (str); | |
3123 | hashval_t h = len; | |
3124 | ||
3125 | for (i = 0; i < len; i++) | |
3126 | h = ((h * 613) + p[i]); | |
3127 | ||
3128 | return h; | |
3129 | } | |
3130 | ||
2ef51f0e | 3131 | bool |
3132 | objc_string_hasher::equal (string_descriptor *ptr1, string_descriptor *ptr2) | |
13dcc150 | 3133 | { |
2ef51f0e | 3134 | const_tree const str1 = ptr1->literal; |
3135 | const_tree const str2 = ptr2->literal; | |
13dcc150 | 3136 | int len1 = TREE_STRING_LENGTH (str1); |
3137 | ||
3138 | return (len1 == TREE_STRING_LENGTH (str2) | |
3139 | && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2), | |
3140 | len1)); | |
3141 | } | |
3142 | ||
c17b85ea | 3143 | /* Given a chain of STRING_CST's, build a static instance of |
3144 | NXConstantString which points at the concatenation of those | |
3145 | strings. We place the string object in the __string_objects | |
3146 | section of the __OBJC segment. The Objective-C runtime will | |
3147 | initialize the isa pointers of the string objects to point at the | |
3148 | NXConstantString class object. */ | |
c450c529 | 3149 | |
3150 | tree | |
66eb9e7e | 3151 | objc_build_string_object (tree string) |
c450c529 | 3152 | { |
d4238e8b | 3153 | tree constant_string_class; |
c450c529 | 3154 | int length; |
267785bc | 3155 | tree addr; |
13dcc150 | 3156 | struct string_descriptor *desc, key; |
c450c529 | 3157 | |
8a8f7a54 | 3158 | /* We should be passed a STRING_CST. */ |
3159 | gcc_checking_assert (TREE_CODE (string) == STRING_CST); | |
c450c529 | 3160 | length = TREE_STRING_LENGTH (string) - 1; |
3161 | ||
6bbb4715 | 3162 | /* The target may have different ideas on how to construct an ObjC string |
3163 | literal. On Darwin (Mac OS X), for example, we may wish to obtain a | |
d4238e8b | 3164 | constant CFString reference instead. |
3165 | At present, this is only supported for the NeXT runtime. */ | |
6bbb4715 | 3166 | if (flag_next_runtime |
267785bc | 3167 | && targetcm.objc_construct_string_object) |
d4238e8b | 3168 | { |
1f6616ee | 3169 | tree constructor = (*targetcm.objc_construct_string_object) (string); |
d4238e8b | 3170 | if (constructor) |
3171 | return build1 (NOP_EXPR, objc_object_type, constructor); | |
3172 | } | |
3173 | ||
13dcc150 | 3174 | /* Check whether the string class being used actually exists and has the |
3175 | correct ivar layout. */ | |
c17b85ea | 3176 | if (!string_layout_checked) |
3177 | { | |
13dcc150 | 3178 | string_layout_checked = -1; |
3179 | constant_string_class = lookup_interface (constant_string_id); | |
0616a948 | 3180 | internal_const_str_type = objc_build_internal_const_str_type (); |
13dcc150 | 3181 | |
3182 | if (!constant_string_class | |
3183 | || !(constant_string_type | |
3184 | = CLASS_STATIC_TEMPLATE (constant_string_class))) | |
a608187f | 3185 | error ("cannot find interface declaration for %qE", |
3186 | constant_string_id); | |
13dcc150 | 3187 | /* The NSConstantString/NXConstantString ivar layout is now known. */ |
3188 | else if (!check_string_class_template ()) | |
a608187f | 3189 | error ("interface %qE does not have valid constant string layout", |
3190 | constant_string_id); | |
267785bc | 3191 | /* If the runtime can generate a literal reference to the string class, |
3192 | don't need to run a constructor. */ | |
3193 | else if (!(*runtime.setup_const_string_class_decl)()) | |
3194 | error ("cannot find reference tag for class %qE", constant_string_id); | |
13dcc150 | 3195 | else |
4d4b71d3 | 3196 | { |
13dcc150 | 3197 | string_layout_checked = 1; /* Success! */ |
3198 | add_class_reference (constant_string_id); | |
4d4b71d3 | 3199 | } |
4d4b71d3 | 3200 | } |
3201 | ||
13dcc150 | 3202 | if (string_layout_checked == -1) |
3203 | return error_mark_node; | |
6a9006c3 | 3204 | |
13dcc150 | 3205 | /* Perhaps we already constructed a constant string just like this one? */ |
3206 | key.literal = string; | |
2ef51f0e | 3207 | string_descriptor **loc = string_htab->find_slot (&key, INSERT); |
3208 | desc = *loc; | |
13dcc150 | 3209 | |
3210 | if (!desc) | |
e0ece38e | 3211 | { |
25a27413 | 3212 | *loc = desc = ggc_alloc<string_descriptor> (); |
13dcc150 | 3213 | desc->literal = string; |
6bbb4715 | 3214 | desc->constructor = |
267785bc | 3215 | (*runtime.build_const_string_constructor) (input_location, string, length); |
e0ece38e | 3216 | } |
3217 | ||
0616a948 | 3218 | addr = convert (build_pointer_type (constant_string_type), |
b6889cb0 | 3219 | build_unary_op (input_location, |
3220 | ADDR_EXPR, desc->constructor, 1)); | |
13dcc150 | 3221 | |
3222 | return addr; | |
51eb6608 | 3223 | } |
3224 | ||
6a9006c3 | 3225 | /* Build a static constant CONSTRUCTOR |
3226 | with type TYPE and elements ELTS. */ | |
3227 | ||
267785bc | 3228 | tree |
f1f41a6c | 3229 | objc_build_constructor (tree type, vec<constructor_elt, va_gc> *elts) |
6a9006c3 | 3230 | { |
4c6b0360 | 3231 | tree constructor = build_constructor (type, elts); |
1cd45b1e | 3232 | |
c450c529 | 3233 | TREE_CONSTANT (constructor) = 1; |
3234 | TREE_STATIC (constructor) = 1; | |
3235 | TREE_READONLY (constructor) = 1; | |
3236 | ||
c17b85ea | 3237 | #ifdef OBJCPLUS |
13dcc150 | 3238 | /* Adjust for impedance mismatch. We should figure out how to build |
3239 | CONSTRUCTORs that consistently please both the C and C++ gods. */ | |
f1f41a6c | 3240 | if (!(*elts)[0].index) |
b4d6f57b | 3241 | TREE_TYPE (constructor) = init_list_type_node; |
c17b85ea | 3242 | #endif |
13dcc150 | 3243 | |
6a9006c3 | 3244 | return constructor; |
fc304191 | 3245 | } |
848fa8f1 | 3246 | |
267785bc | 3247 | /* Return the DECL of the string IDENT in the SECTION. */ |
fc304191 | 3248 | |
267785bc | 3249 | tree |
3250 | get_objc_string_decl (tree ident, enum string_section section) | |
fc304191 | 3251 | { |
267785bc | 3252 | tree chain; |
fc304191 | 3253 | |
267785bc | 3254 | switch (section) |
c17b85ea | 3255 | { |
267785bc | 3256 | case class_names: |
3257 | chain = class_names_chain; | |
3258 | break; | |
3259 | case meth_var_names: | |
3260 | chain = meth_var_names_chain; | |
3261 | break; | |
3262 | case meth_var_types: | |
3263 | chain = meth_var_types_chain; | |
3264 | break; | |
3265 | case prop_names_attr: | |
3266 | chain = prop_names_attr_chain; | |
3267 | break; | |
3268 | default: | |
3269 | gcc_unreachable (); | |
c17b85ea | 3270 | } |
fc304191 | 3271 | |
267785bc | 3272 | for (; chain != 0; chain = TREE_CHAIN (chain)) |
3273 | if (TREE_VALUE (chain) == ident) | |
3274 | return (TREE_PURPOSE (chain)); | |
3275 | ||
3276 | /* We didn't find the entry. */ | |
3277 | return NULL_TREE; | |
fc304191 | 3278 | } |
3279 | ||
267785bc | 3280 | /* Create a class reference, but don't create a variable to reference |
3281 | it. */ | |
848fa8f1 | 3282 | |
267785bc | 3283 | void |
3284 | add_class_reference (tree ident) | |
fc304191 | 3285 | { |
267785bc | 3286 | tree chain; |
fc304191 | 3287 | |
267785bc | 3288 | if ((chain = cls_ref_chain)) |
d3b19b01 | 3289 | { |
267785bc | 3290 | tree tail; |
3291 | do | |
3292 | { | |
3293 | if (ident == TREE_VALUE (chain)) | |
3294 | return; | |
d3b19b01 | 3295 | |
267785bc | 3296 | tail = chain; |
3297 | chain = TREE_CHAIN (chain); | |
3298 | } | |
3299 | while (chain); | |
d3b19b01 | 3300 | |
267785bc | 3301 | /* Append to the end of the list */ |
3302 | TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE); | |
3303 | } | |
3304 | else | |
3305 | cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE); | |
fc304191 | 3306 | } |
3307 | ||
267785bc | 3308 | /* Get a class reference, creating it if necessary. Also create the |
3309 | reference variable. */ | |
3310 | tree | |
3311 | objc_get_class_reference (tree ident) | |
fc304191 | 3312 | { |
267785bc | 3313 | tree orig_ident = (DECL_P (ident) |
3314 | ? DECL_NAME (ident) | |
3315 | : TYPE_P (ident) | |
3316 | ? OBJC_TYPE_NAME (ident) | |
3317 | : ident); | |
3318 | bool local_scope = false; | |
fc304191 | 3319 | |
267785bc | 3320 | #ifdef OBJCPLUS |
3321 | if (processing_template_decl) | |
3322 | /* Must wait until template instantiation time. */ | |
009ec0e5 | 3323 | return build_min_nt_loc (UNKNOWN_LOCATION, CLASS_REFERENCE_EXPR, ident); |
267785bc | 3324 | #endif |
fc304191 | 3325 | |
267785bc | 3326 | if (TREE_CODE (ident) == TYPE_DECL) |
3327 | ident = (DECL_ORIGINAL_TYPE (ident) | |
3328 | ? DECL_ORIGINAL_TYPE (ident) | |
3329 | : TREE_TYPE (ident)); | |
fc304191 | 3330 | |
267785bc | 3331 | #ifdef OBJCPLUS |
3332 | if (TYPE_P (ident) | |
3333 | && CP_TYPE_CONTEXT (ident) != global_namespace) | |
3334 | local_scope = true; | |
3335 | #endif | |
fc304191 | 3336 | |
267785bc | 3337 | if (local_scope || !(ident = objc_is_class_name (ident))) |
4c6b0360 | 3338 | { |
267785bc | 3339 | error ("%qE is not an Objective-C class name or alias", |
3340 | orig_ident); | |
3341 | return error_mark_node; | |
4c6b0360 | 3342 | } |
fc304191 | 3343 | |
267785bc | 3344 | return (*runtime.get_class_reference) (ident); |
3345 | } | |
fc304191 | 3346 | |
267785bc | 3347 | void |
3348 | objc_declare_alias (tree alias_ident, tree class_ident) | |
3349 | { | |
3350 | tree underlying_class; | |
fc304191 | 3351 | |
267785bc | 3352 | #ifdef OBJCPLUS |
3353 | if (current_namespace != global_namespace) { | |
3354 | error ("Objective-C declarations may only appear in global scope"); | |
3355 | } | |
3356 | #endif /* OBJCPLUS */ | |
fc304191 | 3357 | |
267785bc | 3358 | if (!(underlying_class = objc_is_class_name (class_ident))) |
3359 | warning (0, "cannot find class %qE", class_ident); | |
3360 | else if (objc_is_class_name (alias_ident)) | |
3361 | warning (0, "class %qE already exists", alias_ident); | |
3362 | else | |
6a9006c3 | 3363 | { |
267785bc | 3364 | /* Implement @compatibility_alias as a typedef. */ |
3365 | #ifdef OBJCPLUS | |
3366 | push_lang_context (lang_name_c); /* extern "C" */ | |
3367 | #endif | |
3368 | lang_hooks.decls.pushdecl (build_decl | |
3369 | (input_location, | |
3370 | TYPE_DECL, | |
3371 | alias_ident, | |
3372 | xref_tag (RECORD_TYPE, underlying_class))); | |
3373 | #ifdef OBJCPLUS | |
3374 | pop_lang_context (); | |
3375 | #endif | |
f41791cf | 3376 | objc_map_put (alias_name_map, alias_ident, underlying_class); |
6a9006c3 | 3377 | } |
fc304191 | 3378 | } |
3379 | ||
267785bc | 3380 | void |
29d7200d | 3381 | objc_declare_class (tree identifier) |
c17b85ea | 3382 | { |
267785bc | 3383 | #ifdef OBJCPLUS |
3384 | if (current_namespace != global_namespace) { | |
3385 | error ("Objective-C declarations may only appear in global scope"); | |
3386 | } | |
3387 | #endif /* OBJCPLUS */ | |
13dcc150 | 3388 | |
29d7200d | 3389 | if (! objc_is_class_name (identifier)) |
267785bc | 3390 | { |
29d7200d | 3391 | tree record = lookup_name (identifier), type = record; |
6bbb4715 | 3392 | |
29d7200d | 3393 | if (record) |
267785bc | 3394 | { |
29d7200d | 3395 | if (TREE_CODE (record) == TYPE_DECL) |
3396 | type = DECL_ORIGINAL_TYPE (record) | |
3397 | ? DECL_ORIGINAL_TYPE (record) | |
3398 | : TREE_TYPE (record); | |
6bbb4715 | 3399 | |
29d7200d | 3400 | if (!TYPE_HAS_OBJC_INFO (type) |
3401 | || !TYPE_OBJC_INTERFACE (type)) | |
267785bc | 3402 | { |
29d7200d | 3403 | error ("%qE redeclared as different kind of symbol", |
3404 | identifier); | |
3405 | error ("previous declaration of %q+D", | |
3406 | record); | |
267785bc | 3407 | } |
fc304191 | 3408 | } |
6bbb4715 | 3409 | |
29d7200d | 3410 | record = xref_tag (RECORD_TYPE, identifier); |
3411 | INIT_TYPE_OBJC_INFO (record); | |
3412 | /* In the case of a @class declaration, we store the ident in | |
3413 | the TYPE_OBJC_INTERFACE. If later an @interface is found, | |
3414 | we'll replace the ident with the interface. */ | |
3415 | TYPE_OBJC_INTERFACE (record) = identifier; | |
f41791cf | 3416 | objc_map_put (class_name_map, identifier, NULL_TREE); |
fc304191 | 3417 | } |
fc304191 | 3418 | } |
3419 | ||
267785bc | 3420 | tree |
3421 | objc_is_class_name (tree ident) | |
fc304191 | 3422 | { |
05ca272f | 3423 | if (ident && TREE_CODE (ident) == IDENTIFIER_NODE) |
3424 | { | |
3425 | tree t = identifier_global_value (ident); | |
3426 | if (t) | |
3427 | ident = t; | |
3428 | } | |
3429 | ||
267785bc | 3430 | while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident)) |
3431 | ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident)); | |
13dcc150 | 3432 | |
267785bc | 3433 | if (ident && TREE_CODE (ident) == RECORD_TYPE) |
3434 | ident = OBJC_TYPE_NAME (ident); | |
13dcc150 | 3435 | #ifdef OBJCPLUS |
267785bc | 3436 | if (ident && TREE_CODE (ident) == TYPE_DECL) |
3437 | { | |
3438 | tree type = TREE_TYPE (ident); | |
3439 | if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM) | |
3440 | return NULL_TREE; | |
3441 | ident = DECL_NAME (ident); | |
3442 | } | |
13dcc150 | 3443 | #endif |
267785bc | 3444 | if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE) |
3445 | return NULL_TREE; | |
fc304191 | 3446 | |
267785bc | 3447 | if (lookup_interface (ident)) |
3448 | return ident; | |
fc304191 | 3449 | |
f41791cf | 3450 | { |
3451 | tree target; | |
fc304191 | 3452 | |
f41791cf | 3453 | target = objc_map_get (class_name_map, ident); |
3454 | if (target != OBJC_MAP_NOT_FOUND) | |
3455 | return ident; | |
3456 | ||
3457 | target = objc_map_get (alias_name_map, ident); | |
3458 | if (target != OBJC_MAP_NOT_FOUND) | |
3459 | return target; | |
3460 | } | |
13dcc150 | 3461 | |
267785bc | 3462 | return 0; |
13dcc150 | 3463 | } |
3464 | ||
267785bc | 3465 | /* Check whether TYPE is either 'id' or 'Class'. */ |
fc304191 | 3466 | |
267785bc | 3467 | tree |
3468 | objc_is_id (tree type) | |
13dcc150 | 3469 | { |
05ca272f | 3470 | if (type && TREE_CODE (type) == IDENTIFIER_NODE) |
3471 | { | |
3472 | tree t = identifier_global_value (type); | |
3473 | if (t) | |
3474 | type = t; | |
3475 | } | |
fc304191 | 3476 | |
267785bc | 3477 | if (type && TREE_CODE (type) == TYPE_DECL) |
3478 | type = TREE_TYPE (type); | |
fc304191 | 3479 | |
267785bc | 3480 | /* NB: This function may be called before the ObjC front-end has |
3481 | been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */ | |
3482 | return (objc_object_type && type | |
3483 | && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type)) | |
3484 | ? type | |
3485 | : NULL_TREE); | |
13dcc150 | 3486 | } |
c450c529 | 3487 | |
267785bc | 3488 | /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC |
3489 | class instance. This is needed by other parts of the compiler to | |
3490 | handle ObjC types gracefully. */ | |
13dcc150 | 3491 | |
267785bc | 3492 | tree |
3493 | objc_is_object_ptr (tree type) | |
13dcc150 | 3494 | { |
267785bc | 3495 | tree ret; |
13dcc150 | 3496 | |
267785bc | 3497 | type = TYPE_MAIN_VARIANT (type); |
3498 | if (!POINTER_TYPE_P (type)) | |
3499 | return 0; | |
13dcc150 | 3500 | |
267785bc | 3501 | ret = objc_is_id (type); |
3502 | if (!ret) | |
3503 | ret = objc_is_class_name (TREE_TYPE (type)); | |
13dcc150 | 3504 | |
267785bc | 3505 | return ret; |
fc304191 | 3506 | } |
e0ece38e | 3507 | |
267785bc | 3508 | static int |
3509 | objc_is_gcable_type (tree type, int or_strong_p) | |
51eb6608 | 3510 | { |
267785bc | 3511 | tree name; |
51eb6608 | 3512 | |
267785bc | 3513 | if (!TYPE_P (type)) |
3514 | return 0; | |
3515 | if (objc_is_id (TYPE_MAIN_VARIANT (type))) | |
3516 | return 1; | |
3517 | if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type))) | |
3518 | return 1; | |
3519 | if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF) | |
3520 | return 0; | |
3521 | type = TREE_TYPE (type); | |
3522 | if (TREE_CODE (type) != RECORD_TYPE) | |
3523 | return 0; | |
3524 | name = TYPE_NAME (type); | |
3525 | return (objc_is_class_name (name) != NULL_TREE); | |
51eb6608 | 3526 | } |
3527 | ||
267785bc | 3528 | static tree |
3529 | objc_substitute_decl (tree expr, tree oldexpr, tree newexpr) | |
51eb6608 | 3530 | { |
267785bc | 3531 | if (expr == oldexpr) |
3532 | return newexpr; | |
51eb6608 | 3533 | |
267785bc | 3534 | switch (TREE_CODE (expr)) |
51eb6608 | 3535 | { |
267785bc | 3536 | case COMPONENT_REF: |
3537 | return objc_build_component_ref | |
3538 | (objc_substitute_decl (TREE_OPERAND (expr, 0), | |
3539 | oldexpr, | |
3540 | newexpr), | |
3541 | DECL_NAME (TREE_OPERAND (expr, 1))); | |
3542 | case ARRAY_REF: | |
3543 | return build_array_ref (input_location, | |
3544 | objc_substitute_decl (TREE_OPERAND (expr, 0), | |
3545 | oldexpr, | |
3546 | newexpr), | |
3547 | TREE_OPERAND (expr, 1)); | |
3548 | case INDIRECT_REF: | |
3549 | return build_indirect_ref (input_location, | |
3550 | objc_substitute_decl (TREE_OPERAND (expr, 0), | |
3551 | oldexpr, | |
3552 | newexpr), RO_ARROW); | |
3553 | default: | |
3554 | return expr; | |
51eb6608 | 3555 | } |
51eb6608 | 3556 | } |
3557 | ||
fc304191 | 3558 | static tree |
267785bc | 3559 | objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs) |
fc304191 | 3560 | { |
267785bc | 3561 | tree func_params; |
3562 | /* The LHS parameter contains the expression 'outervar->memberspec'; | |
3563 | we need to transform it into '&((typeof(outervar) *) 0)->memberspec', | |
3564 | where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]'). | |
3565 | */ | |
3566 | tree offs | |
3567 | = objc_substitute_decl | |
3568 | (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node)); | |
3569 | tree func | |
3570 | = (flag_objc_direct_dispatch | |
3571 | ? objc_assign_ivar_fast_decl | |
3572 | : objc_assign_ivar_decl); | |
fc304191 | 3573 | |
267785bc | 3574 | offs = convert (integer_type_node, build_unary_op (input_location, |
3575 | ADDR_EXPR, offs, 0)); | |
3576 | offs = fold (offs); | |
3577 | func_params = tree_cons (NULL_TREE, | |
3578 | convert (objc_object_type, rhs), | |
3579 | tree_cons (NULL_TREE, convert (objc_object_type, outervar), | |
3580 | tree_cons (NULL_TREE, offs, | |
3581 | NULL_TREE))); | |
fc304191 | 3582 | |
267785bc | 3583 | return build_function_call (input_location, func, func_params); |
13dcc150 | 3584 | } |
fc304191 | 3585 | |
267785bc | 3586 | static tree |
3587 | objc_build_global_assignment (tree lhs, tree rhs) | |
13dcc150 | 3588 | { |
267785bc | 3589 | tree func_params = tree_cons (NULL_TREE, |
3590 | convert (objc_object_type, rhs), | |
3591 | tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type), | |
3592 | build_unary_op (input_location, ADDR_EXPR, lhs, 0)), | |
3593 | NULL_TREE)); | |
c450c529 | 3594 | |
6bbb4715 | 3595 | return build_function_call (input_location, |
267785bc | 3596 | objc_assign_global_decl, func_params); |
fc304191 | 3597 | } |
c450c529 | 3598 | |
c450c529 | 3599 | static tree |
267785bc | 3600 | objc_build_strong_cast_assignment (tree lhs, tree rhs) |
c450c529 | 3601 | { |
267785bc | 3602 | tree func_params = tree_cons (NULL_TREE, |
3603 | convert (objc_object_type, rhs), | |
3604 | tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type), | |
3605 | build_unary_op (input_location, ADDR_EXPR, lhs, 0)), | |
3606 | NULL_TREE)); | |
3607 | ||
267785bc | 3608 | return build_function_call (input_location, |
3609 | objc_assign_strong_cast_decl, func_params); | |
c450c529 | 3610 | } |
3611 | ||
267785bc | 3612 | static int |
3613 | objc_is_gcable_p (tree expr) | |
fc304191 | 3614 | { |
267785bc | 3615 | return (TREE_CODE (expr) == COMPONENT_REF |
3616 | ? objc_is_gcable_p (TREE_OPERAND (expr, 1)) | |
3617 | : TREE_CODE (expr) == ARRAY_REF | |
3618 | ? (objc_is_gcable_p (TREE_TYPE (expr)) | |
3619 | || objc_is_gcable_p (TREE_OPERAND (expr, 0))) | |
3620 | : TREE_CODE (expr) == ARRAY_TYPE | |
3621 | ? objc_is_gcable_p (TREE_TYPE (expr)) | |
3622 | : TYPE_P (expr) | |
3623 | ? objc_is_gcable_type (expr, 1) | |
3624 | : (objc_is_gcable_p (TREE_TYPE (expr)) | |
3625 | || (DECL_P (expr) | |
3626 | && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr))))); | |
c4796e8b | 3627 | } |
3628 | ||
267785bc | 3629 | static int |
3630 | objc_is_ivar_reference_p (tree expr) | |
c4796e8b | 3631 | { |
267785bc | 3632 | return (TREE_CODE (expr) == ARRAY_REF |
3633 | ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0)) | |
3634 | : TREE_CODE (expr) == COMPONENT_REF | |
3635 | ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL | |
3636 | : 0); | |
c4796e8b | 3637 | } |
3638 | ||
267785bc | 3639 | static int |
3640 | objc_is_global_reference_p (tree expr) | |
c4796e8b | 3641 | { |
267785bc | 3642 | return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR |
3643 | ? objc_is_global_reference_p (TREE_OPERAND (expr, 0)) | |
3644 | : DECL_P (expr) | |
3645 | ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr)) | |
3646 | : 0); | |
3647 | } | |
c4796e8b | 3648 | |
267785bc | 3649 | tree |
3650 | objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs) | |
3651 | { | |
3652 | tree result = NULL_TREE, outer; | |
3653 | int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0; | |
c4796e8b | 3654 | |
267785bc | 3655 | /* This function is currently only used with the next runtime with |
3656 | garbage collection enabled (-fobjc-gc). */ | |
3657 | gcc_assert (flag_next_runtime); | |
c4796e8b | 3658 | |
267785bc | 3659 | /* See if we have any lhs casts, and strip them out. NB: The lvalue casts |
3660 | will have been transformed to the form '*(type *)&expr'. */ | |
3661 | if (TREE_CODE (lhs) == INDIRECT_REF) | |
3662 | { | |
3663 | outer = TREE_OPERAND (lhs, 0); | |
c4796e8b | 3664 | |
267785bc | 3665 | while (!strong_cast_p |
3666 | && (CONVERT_EXPR_P (outer) | |
3667 | || TREE_CODE (outer) == NON_LVALUE_EXPR)) | |
146a801c | 3668 | { |
267785bc | 3669 | tree lhstype = TREE_TYPE (outer); |
39b8ddc6 | 3670 | |
267785bc | 3671 | /* Descend down the cast chain, and record the first objc_gc |
3672 | attribute found. */ | |
3673 | if (POINTER_TYPE_P (lhstype)) | |
3674 | { | |
3675 | tree attr | |
3676 | = lookup_attribute ("objc_gc", | |
3677 | TYPE_ATTRIBUTES (TREE_TYPE (lhstype))); | |
fc304191 | 3678 | |
267785bc | 3679 | if (attr) |
3680 | strong_cast_p = 1; | |
3681 | } | |
4c6b0360 | 3682 | |
267785bc | 3683 | outer = TREE_OPERAND (outer, 0); |
4c6b0360 | 3684 | } |
c450c529 | 3685 | } |
c450c529 | 3686 | |
267785bc | 3687 | /* If we have a __strong cast, it trumps all else. */ |
3688 | if (strong_cast_p) | |
146a801c | 3689 | { |
267785bc | 3690 | if (modifycode != NOP_EXPR) |
3691 | goto invalid_pointer_arithmetic; | |
c450c529 | 3692 | |
267785bc | 3693 | if (warn_assign_intercept) |
3694 | warning (0, "strong-cast assignment has been intercepted"); | |
146a801c | 3695 | |
267785bc | 3696 | result = objc_build_strong_cast_assignment (lhs, rhs); |
e0ece38e | 3697 | |
267785bc | 3698 | goto exit_point; |
146a801c | 3699 | } |
3700 | ||
267785bc | 3701 | /* the lhs must be of a suitable type, regardless of its underlying |
3702 | structure. */ | |
3703 | if (!objc_is_gcable_p (lhs)) | |
3704 | goto exit_point; | |
146a801c | 3705 | |
267785bc | 3706 | outer = lhs; |
146a801c | 3707 | |
267785bc | 3708 | while (outer |
3709 | && (TREE_CODE (outer) == COMPONENT_REF | |
3710 | || TREE_CODE (outer) == ARRAY_REF)) | |
3711 | outer = TREE_OPERAND (outer, 0); | |
c450c529 | 3712 | |
267785bc | 3713 | if (TREE_CODE (outer) == INDIRECT_REF) |
c450c529 | 3714 | { |
267785bc | 3715 | outer = TREE_OPERAND (outer, 0); |
3716 | indirect_p = 1; | |
c450c529 | 3717 | } |
3718 | ||
267785bc | 3719 | outer_gc_p = objc_is_gcable_p (outer); |
c450c529 | 3720 | |
267785bc | 3721 | /* Handle ivar assignments. */ |
3722 | if (objc_is_ivar_reference_p (lhs)) | |
3723 | { | |
3724 | /* if the struct to the left of the ivar is not an Objective-C object (__strong | |
3725 | doesn't cut it here), the best we can do here is suggest a cast. */ | |
3726 | if (!objc_is_gcable_type (TREE_TYPE (outer), 0)) | |
3727 | { | |
3728 | /* We may still be able to use the global write barrier... */ | |
3729 | if (!indirect_p && objc_is_global_reference_p (outer)) | |
3730 | goto global_reference; | |
c17b85ea | 3731 | |
267785bc | 3732 | suggest_cast: |
3733 | if (modifycode == NOP_EXPR) | |
3734 | { | |
3735 | if (warn_assign_intercept) | |
3736 | warning (0, "strong-cast may possibly be needed"); | |
3737 | } | |
c450c529 | 3738 | |
267785bc | 3739 | goto exit_point; |
3740 | } | |
c450c529 | 3741 | |
267785bc | 3742 | if (modifycode != NOP_EXPR) |
3743 | goto invalid_pointer_arithmetic; | |
fc304191 | 3744 | |
267785bc | 3745 | if (warn_assign_intercept) |
3746 | warning (0, "instance variable assignment has been intercepted"); | |
c450c529 | 3747 | |
267785bc | 3748 | result = objc_build_ivar_assignment (outer, lhs, rhs); |
fc304191 | 3749 | |
267785bc | 3750 | goto exit_point; |
3751 | } | |
3752 | ||
3753 | /* Likewise, intercept assignment to global/static variables if their type is | |
3754 | GC-marked. */ | |
3755 | if (objc_is_global_reference_p (outer)) | |
fc304191 | 3756 | { |
267785bc | 3757 | if (indirect_p) |
3758 | goto suggest_cast; | |
fc304191 | 3759 | |
267785bc | 3760 | global_reference: |
3761 | if (modifycode != NOP_EXPR) | |
3762 | { | |
3763 | invalid_pointer_arithmetic: | |
3764 | if (outer_gc_p) | |
3765 | warning (0, "pointer arithmetic for garbage-collected objects not allowed"); | |
fc304191 | 3766 | |
267785bc | 3767 | goto exit_point; |
3768 | } | |
3769 | ||
3770 | if (warn_assign_intercept) | |
3771 | warning (0, "global/static variable assignment has been intercepted"); | |
3772 | ||
3773 | result = objc_build_global_assignment (lhs, rhs); | |
fc304191 | 3774 | } |
267785bc | 3775 | |
3776 | /* In all other cases, fall back to the normal mechanism. */ | |
3777 | exit_point: | |
3778 | return result; | |
fc304191 | 3779 | } |
3780 | ||
f41791cf | 3781 | /* Implementation of the table mapping a class name (as an identifier) |
3782 | to a class node. The two public functions for it are | |
3783 | lookup_interface() and add_interface(). add_interface() is only | |
3784 | used in this file, so we can make it static. */ | |
c17b85ea | 3785 | |
f41791cf | 3786 | static GTY(()) objc_map_t interface_map; |
499ea517 | 3787 | |
f41791cf | 3788 | static void |
3789 | interface_hash_init (void) | |
267785bc | 3790 | { |
f41791cf | 3791 | interface_map = objc_map_alloc_ggc (200); |
267785bc | 3792 | } |
3793 | ||
f41791cf | 3794 | static tree |
3795 | add_interface (tree class_name, tree name) | |
267785bc | 3796 | { |
f41791cf | 3797 | /* Put interfaces on list in reverse order. */ |
3798 | TREE_CHAIN (class_name) = interface_chain; | |
3799 | interface_chain = class_name; | |
3800 | ||
3801 | /* Add it to the map. */ | |
3802 | objc_map_put (interface_map, name, class_name); | |
3803 | ||
3804 | return interface_chain; | |
267785bc | 3805 | } |
0ced0389 | 3806 | |
267785bc | 3807 | tree |
3808 | lookup_interface (tree ident) | |
3809 | { | |
499ea517 | 3810 | #ifdef OBJCPLUS |
267785bc | 3811 | if (ident && TREE_CODE (ident) == TYPE_DECL) |
3812 | ident = DECL_NAME (ident); | |
c17b85ea | 3813 | #endif |
c17b85ea | 3814 | |
267785bc | 3815 | if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE) |
3816 | return NULL_TREE; | |
c17b85ea | 3817 | |
267785bc | 3818 | { |
f41791cf | 3819 | tree interface = objc_map_get (interface_map, ident); |
fc304191 | 3820 | |
f41791cf | 3821 | if (interface == OBJC_MAP_NOT_FOUND) |
3822 | return NULL_TREE; | |
3823 | else | |
3824 | return interface; | |
267785bc | 3825 | } |
3826 | } | |
e0ece38e | 3827 | |
394dd737 | 3828 | |
3829 | ||
267785bc | 3830 | /* Implement @defs (<classname>) within struct bodies. */ |
fc304191 | 3831 | |
267785bc | 3832 | tree |
3833 | objc_get_class_ivars (tree class_name) | |
3834 | { | |
3835 | tree interface = lookup_interface (class_name); | |
fc304191 | 3836 | |
267785bc | 3837 | if (interface) |
3838 | return get_class_ivars (interface, true); | |
6b094e50 | 3839 | |
267785bc | 3840 | error ("cannot find interface declaration for %qE", |
3841 | class_name); | |
fc304191 | 3842 | |
267785bc | 3843 | return error_mark_node; |
c450c529 | 3844 | } |
3845 | ||
394dd737 | 3846 | |
3847 | /* Functions used by the hashtable for field duplicates in | |
3848 | objc_detect_field_duplicates(). Ideally, we'd use a standard | |
3849 | key-value dictionary hashtable , and store as keys the field names, | |
3850 | and as values the actual declarations (used to print nice error | |
3851 | messages with the locations). But, the hashtable we are using only | |
3852 | allows us to store keys in the hashtable, without values (it looks | |
3853 | more like a set). So, we store the DECLs, but define equality as | |
3854 | DECLs having the same name, and hash as the hash of the name. */ | |
d1455aa3 | 3855 | |
770ff93b | 3856 | struct decl_name_hash : nofree_ptr_hash <tree_node> |
d1455aa3 | 3857 | { |
9969c043 | 3858 | static inline hashval_t hash (const tree_node *); |
3859 | static inline bool equal (const tree_node *, const tree_node *); | |
d1455aa3 | 3860 | }; |
3861 | ||
3862 | inline hashval_t | |
9969c043 | 3863 | decl_name_hash::hash (const tree_node *q) |
394dd737 | 3864 | { |
394dd737 | 3865 | return (hashval_t) ((intptr_t)(DECL_NAME (q)) >> 3); |
3866 | } | |
3867 | ||
d1455aa3 | 3868 | inline bool |
9969c043 | 3869 | decl_name_hash::equal (const tree_node *a, const tree_node *b) |
394dd737 | 3870 | { |
394dd737 | 3871 | return DECL_NAME (a) == DECL_NAME (b); |
3872 | } | |
3873 | ||
267785bc | 3874 | /* Called when checking the variables in a struct. If we are not |
394dd737 | 3875 | doing the ivars list inside an @interface context, then return |
3876 | false. Else, perform the check for duplicate ivars, then return | |
3877 | true. The check for duplicates checks if an instance variable with | |
3878 | the same name exists in the class or in a superclass. If | |
3879 | 'check_superclasses_only' is set to true, then it is assumed that | |
3880 | checks for instance variables in the same class has already been | |
3881 | performed (this is the case for ObjC++) and only the instance | |
3882 | variables of superclasses are checked. */ | |
3883 | bool | |
3884 | objc_detect_field_duplicates (bool check_superclasses_only) | |
267785bc | 3885 | { |
6bbb4715 | 3886 | if (!objc_collecting_ivars || !objc_interface_context |
394dd737 | 3887 | || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE) |
3888 | return false; | |
3889 | ||
3890 | /* We have two ways of doing this check: | |
6bbb4715 | 3891 | |
394dd737 | 3892 | "direct comparison": we iterate over the instance variables and |
3893 | compare them directly. This works great for small numbers of | |
3894 | instance variables (such as 10 or 20), which are extremely common. | |
3895 | But it will potentially take forever for the pathological case with | |
3896 | a huge number (eg, 10k) of instance variables. | |
6bbb4715 | 3897 | |
394dd737 | 3898 | "hashtable": we use a hashtable, which requires a single sweep |
3899 | through the list of instances variables. This is much slower for a | |
3900 | small number of variables, and we only use it for large numbers. | |
3901 | ||
3902 | To decide which one to use, we need to get an idea of how many | |
3903 | instance variables we have to compare. */ | |
3904 | { | |
3905 | unsigned int number_of_ivars_to_check = 0; | |
3906 | { | |
3907 | tree ivar; | |
3908 | for (ivar = CLASS_RAW_IVARS (objc_interface_context); | |
3909 | ivar; ivar = DECL_CHAIN (ivar)) | |
3910 | { | |
3911 | /* Ignore anonymous ivars. */ | |
3912 | if (DECL_NAME (ivar)) | |
3913 | number_of_ivars_to_check++; | |
3914 | } | |
3915 | } | |
267785bc | 3916 | |
394dd737 | 3917 | /* Exit if there is nothing to do. */ |
3918 | if (number_of_ivars_to_check == 0) | |
3919 | return true; | |
6bbb4715 | 3920 | |
394dd737 | 3921 | /* In case that there are only 1 or 2 instance variables to check, |
3922 | we always use direct comparison. If there are more, it is | |
3923 | worth iterating over the instance variables in the superclass | |
3924 | to count how many there are (note that this has the same cost | |
3925 | as checking 1 instance variable by direct comparison, which is | |
3926 | why we skip this check in the case of 1 or 2 ivars and just do | |
3927 | the direct comparison) and then decide if it worth using a | |
3928 | hashtable. */ | |
3929 | if (number_of_ivars_to_check > 2) | |
3930 | { | |
3931 | unsigned int number_of_superclass_ivars = 0; | |
3932 | { | |
3933 | tree interface; | |
3934 | for (interface = lookup_interface (CLASS_SUPER_NAME (objc_interface_context)); | |
3935 | interface; interface = lookup_interface (CLASS_SUPER_NAME (interface))) | |
3936 | { | |
3937 | tree ivar; | |
3938 | for (ivar = CLASS_RAW_IVARS (interface); | |
3939 | ivar; ivar = DECL_CHAIN (ivar)) | |
3940 | number_of_superclass_ivars++; | |
3941 | } | |
3942 | } | |
3943 | ||
3944 | /* We use a hashtable if we have over 10k comparisons. */ | |
6bbb4715 | 3945 | if (number_of_ivars_to_check * (number_of_superclass_ivars |
394dd737 | 3946 | + (number_of_ivars_to_check / 2)) |
3947 | > 10000) | |
3948 | { | |
3949 | /* First, build the hashtable by putting all the instance | |
3950 | variables of superclasses in it. */ | |
c1f445d2 | 3951 | hash_table<decl_name_hash> htab (37); |
394dd737 | 3952 | tree interface; |
3953 | for (interface = lookup_interface (CLASS_SUPER_NAME | |
3954 | (objc_interface_context)); | |
3955 | interface; interface = lookup_interface | |
3956 | (CLASS_SUPER_NAME (interface))) | |
3957 | { | |
3958 | tree ivar; | |
3959 | for (ivar = CLASS_RAW_IVARS (interface); ivar; | |
3960 | ivar = DECL_CHAIN (ivar)) | |
3961 | { | |
3962 | if (DECL_NAME (ivar) != NULL_TREE) | |
3963 | { | |
d1455aa3 | 3964 | tree_node **slot = htab.find_slot (ivar, INSERT); |
394dd737 | 3965 | /* Do not check for duplicate instance |
3966 | variables in superclasses. Errors have | |
3967 | already been generated. */ | |
3968 | *slot = ivar; | |
3969 | } | |
3970 | } | |
3971 | } | |
6bbb4715 | 3972 | |
394dd737 | 3973 | /* Now, we go through all the instance variables in the |
3974 | class, and check that they are not in the | |
3975 | hashtable. */ | |
3976 | if (check_superclasses_only) | |
3977 | { | |
3978 | tree ivar; | |
3979 | for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar; | |
3980 | ivar = DECL_CHAIN (ivar)) | |
3981 | { | |
3982 | if (DECL_NAME (ivar) != NULL_TREE) | |
3983 | { | |
d1455aa3 | 3984 | tree duplicate_ivar = htab.find (ivar); |
394dd737 | 3985 | if (duplicate_ivar != HTAB_EMPTY_ENTRY) |
3986 | { | |
3987 | error_at (DECL_SOURCE_LOCATION (ivar), | |
3988 | "duplicate instance variable %q+D", | |
3989 | ivar); | |
3990 | inform (DECL_SOURCE_LOCATION (duplicate_ivar), | |
3991 | "previous declaration of %q+D", | |
3992 | duplicate_ivar); | |
3993 | /* FIXME: Do we need the following ? */ | |
3994 | /* DECL_NAME (ivar) = NULL_TREE; */ | |
3995 | } | |
3996 | } | |
3997 | } | |
3998 | } | |
3999 | else | |
4000 | { | |
4001 | /* If we're checking for duplicates in the class as | |
4002 | well, we insert variables in the hashtable as we | |
4003 | check them, so if a duplicate follows, it will be | |
4004 | caught. */ | |
4005 | tree ivar; | |
4006 | for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar; | |
4007 | ivar = DECL_CHAIN (ivar)) | |
4008 | { | |
4009 | if (DECL_NAME (ivar) != NULL_TREE) | |
4010 | { | |
d1455aa3 | 4011 | tree_node **slot = htab.find_slot (ivar, INSERT); |
394dd737 | 4012 | if (*slot) |
4013 | { | |
4014 | tree duplicate_ivar = (tree)(*slot); | |
4015 | error_at (DECL_SOURCE_LOCATION (ivar), | |
4016 | "duplicate instance variable %q+D", | |
4017 | ivar); | |
4018 | inform (DECL_SOURCE_LOCATION (duplicate_ivar), | |
4019 | "previous declaration of %q+D", | |
4020 | duplicate_ivar); | |
4021 | /* FIXME: Do we need the following ? */ | |
4022 | /* DECL_NAME (ivar) = NULL_TREE; */ | |
4023 | } | |
4024 | *slot = ivar; | |
4025 | } | |
4026 | } | |
4027 | } | |
394dd737 | 4028 | return true; |
4029 | } | |
4030 | } | |
4031 | } | |
6bbb4715 | 4032 | |
394dd737 | 4033 | /* This is the "direct comparison" approach, which is used in most |
4034 | non-pathological cases. */ | |
4035 | { | |
4036 | /* Walk up to class hierarchy, starting with this class (this is | |
4037 | the external loop, because lookup_interface() is expensive, and | |
4038 | we want to do it few times). */ | |
4039 | tree interface = objc_interface_context; | |
4040 | ||
4041 | if (check_superclasses_only) | |
4042 | interface = lookup_interface (CLASS_SUPER_NAME (interface)); | |
6bbb4715 | 4043 | |
394dd737 | 4044 | for ( ; interface; interface = lookup_interface |
4045 | (CLASS_SUPER_NAME (interface))) | |
4046 | { | |
4047 | tree ivar_being_checked; | |
4048 | ||
4049 | for (ivar_being_checked = CLASS_RAW_IVARS (objc_interface_context); | |
4050 | ivar_being_checked; | |
4051 | ivar_being_checked = DECL_CHAIN (ivar_being_checked)) | |
4052 | { | |
4053 | tree decl; | |
6bbb4715 | 4054 | |
394dd737 | 4055 | /* Ignore anonymous ivars. */ |
4056 | if (DECL_NAME (ivar_being_checked) == NULL_TREE) | |
4057 | continue; | |
4058 | ||
4059 | /* Note how we stop when we find the ivar we are checking | |
4060 | (this can only happen in the main class, not | |
4061 | superclasses), to avoid comparing things twice | |
4062 | (otherwise, for each ivar, you'd compare A to B then B | |
4063 | to A, and get duplicated error messages). */ | |
4064 | for (decl = CLASS_RAW_IVARS (interface); | |
4065 | decl && decl != ivar_being_checked; | |
4066 | decl = DECL_CHAIN (decl)) | |
4067 | { | |
4068 | if (DECL_NAME (ivar_being_checked) == DECL_NAME (decl)) | |
4069 | { | |
4070 | error_at (DECL_SOURCE_LOCATION (ivar_being_checked), | |
4071 | "duplicate instance variable %q+D", | |
4072 | ivar_being_checked); | |
4073 | inform (DECL_SOURCE_LOCATION (decl), | |
4074 | "previous declaration of %q+D", | |
4075 | decl); | |
4076 | /* FIXME: Do we need the following ? */ | |
4077 | /* DECL_NAME (ivar_being_checked) = NULL_TREE; */ | |
4078 | } | |
4079 | } | |
4080 | } | |
4081 | } | |
4082 | } | |
4083 | return true; | |
267785bc | 4084 | } |
c450c529 | 4085 | |
267785bc | 4086 | /* Used by: build_private_template, continue_class, |
4087 | and for @defs constructs. */ | |
c4796e8b | 4088 | |
c450c529 | 4089 | static tree |
267785bc | 4090 | get_class_ivars (tree interface, bool inherited) |
c450c529 | 4091 | { |
267785bc | 4092 | tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface)); |
4093 | ||
4094 | /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared | |
4095 | by the current class (i.e., they do not include super-class ivars). | |
4096 | However, the CLASS_IVARS list will be side-effected by a call to | |
4097 | finish_struct(), which will fill in field offsets. */ | |
4098 | if (!CLASS_IVARS (interface)) | |
4099 | CLASS_IVARS (interface) = ivar_chain; | |
4100 | ||
4101 | if (!inherited) | |
4102 | return ivar_chain; | |
4103 | ||
4104 | while (CLASS_SUPER_NAME (interface)) | |
c4796e8b | 4105 | { |
267785bc | 4106 | /* Prepend super-class ivars. */ |
4107 | interface = lookup_interface (CLASS_SUPER_NAME (interface)); | |
4108 | ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)), | |
4109 | ivar_chain); | |
c4796e8b | 4110 | } |
c450c529 | 4111 | |
267785bc | 4112 | return ivar_chain; |
4113 | } | |
4114 | ||
4115 | void | |
4116 | objc_maybe_warn_exceptions (location_t loc) | |
4117 | { | |
4118 | /* -fobjc-exceptions is required to enable Objective-C exceptions. | |
4119 | For example, on Darwin, ObjC exceptions require a sufficiently | |
4120 | recent version of the runtime, so the user must ask for them | |
4121 | explicitly. On other platforms, at the moment -fobjc-exceptions | |
4122 | triggers -fexceptions which again is required for exceptions to | |
4123 | work. */ | |
4124 | if (!flag_objc_exceptions) | |
c450c529 | 4125 | { |
267785bc | 4126 | /* Warn only once per compilation unit. */ |
4127 | static bool warned = false; | |
c450c529 | 4128 | |
267785bc | 4129 | if (!warned) |
4130 | { | |
4131 | error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax"); | |
4132 | warned = true; | |
4133 | } | |
fc304191 | 4134 | } |
267785bc | 4135 | } |
4136 | ||
4137 | static struct objc_try_context *cur_try_context; | |
c450c529 | 4138 | |
267785bc | 4139 | /* Called just after parsing the @try and its associated BODY. We now |
4140 | must prepare for the tricky bits -- handling the catches and finally. */ | |
c450c529 | 4141 | |
267785bc | 4142 | void |
4143 | objc_begin_try_stmt (location_t try_locus, tree body) | |
4144 | { | |
4145 | struct objc_try_context *c = XCNEW (struct objc_try_context); | |
4146 | c->outer = cur_try_context; | |
4147 | c->try_body = body; | |
4148 | c->try_locus = try_locus; | |
4149 | c->end_try_locus = input_location; | |
4150 | cur_try_context = c; | |
c450c529 | 4151 | |
267785bc | 4152 | /* Collect the list of local variables. We'll mark them as volatile |
4153 | at the end of compilation of this function to prevent them being | |
4154 | clobbered by setjmp/longjmp. */ | |
4155 | if (flag_objc_sjlj_exceptions) | |
4156 | objc_mark_locals_volatile (NULL); | |
c450c529 | 4157 | } |
4158 | ||
267785bc | 4159 | /* Called just after parsing "@catch (parm)". Open a binding level, |
4160 | enter DECL into the binding level, and initialize it. Leave the | |
4161 | binding level open while the body of the compound statement is | |
4162 | parsed. If DECL is NULL_TREE, then we are compiling "@catch(...)" | |
4163 | which we compile as "@catch(id tmp_variable)". */ | |
4164 | ||
c450c529 | 4165 | void |
267785bc | 4166 | objc_begin_catch_clause (tree decl) |
c450c529 | 4167 | { |
267785bc | 4168 | tree compound, type, t; |
4169 | bool ellipsis = false; | |
c17b85ea | 4170 | |
267785bc | 4171 | /* Begin a new scope that the entire catch clause will live in. */ |
4172 | compound = c_begin_compound_stmt (true); | |
c17b85ea | 4173 | |
267785bc | 4174 | /* Create the appropriate declaration for the argument. */ |
4175 | if (decl == error_mark_node) | |
4176 | type = error_mark_node; | |
4177 | else | |
4178 | { | |
4179 | if (decl == NULL_TREE) | |
4180 | { | |
4181 | /* If @catch(...) was specified, create a temporary variable of | |
4182 | type 'id' and use it. */ | |
4183 | decl = objc_create_temporary_var (objc_object_type, "__objc_generic_catch_var"); | |
4184 | DECL_SOURCE_LOCATION (decl) = input_location; | |
4185 | /* ... but allow the runtime to differentiate between ellipsis and the | |
4186 | case of @catch (id xyz). */ | |
4187 | ellipsis = true; | |
4188 | } | |
4189 | else | |
4190 | { | |
4191 | /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */ | |
4192 | decl = build_decl (input_location, | |
4193 | VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl)); | |
4194 | } | |
4195 | lang_hooks.decls.pushdecl (decl); | |
c450c529 | 4196 | |
267785bc | 4197 | /* Mark the declaration as used so you never any warnings whether |
4198 | you use the exception argument or not. TODO: Implement a | |
4199 | -Wunused-exception-parameter flag, which would cause warnings | |
4200 | if exception parameter is not used. */ | |
4201 | TREE_USED (decl) = 1; | |
4202 | DECL_READ_P (decl) = 1; | |
c450c529 | 4203 | |
267785bc | 4204 | type = TREE_TYPE (decl); |
4205 | } | |
c450c529 | 4206 | |
267785bc | 4207 | /* Verify that the type of the catch is valid. It must be a pointer |
4208 | to an Objective-C class, or "id" (which is catch-all). */ | |
4209 | if (type == error_mark_node) | |
4210 | { | |
4211 | ;/* Just keep going. */ | |
4212 | } | |
4213 | else if (!objc_type_valid_for_messaging (type, false)) | |
4214 | { | |
4215 | error ("@catch parameter is not a known Objective-C class type"); | |
4216 | type = error_mark_node; | |
4217 | } | |
4218 | else if (TYPE_HAS_OBJC_INFO (TREE_TYPE (type)) | |
4219 | && TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (type))) | |
4220 | { | |
4221 | error ("@catch parameter can not be protocol-qualified"); | |
6bbb4715 | 4222 | type = error_mark_node; |
267785bc | 4223 | } |
4224 | else if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type))) | |
4225 | /* @catch (id xyz) or @catch (...) but we note this for runtimes that | |
4226 | identify 'id'. */ | |
4227 | ; | |
4228 | else | |
4229 | { | |
4230 | /* If 'type' was built using typedefs, we need to get rid of | |
4231 | them and get a simple pointer to the class. */ | |
4232 | bool is_typedef = false; | |
4233 | tree x = TYPE_MAIN_VARIANT (type); | |
6bbb4715 | 4234 | |
267785bc | 4235 | /* Skip from the pointer to the pointee. */ |
4236 | if (TREE_CODE (x) == POINTER_TYPE) | |
4237 | x = TREE_TYPE (x); | |
6bbb4715 | 4238 | |
267785bc | 4239 | /* Traverse typedef aliases */ |
4240 | while (TREE_CODE (x) == RECORD_TYPE && OBJC_TYPE_NAME (x) | |
4241 | && TREE_CODE (OBJC_TYPE_NAME (x)) == TYPE_DECL | |
4242 | && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x))) | |
c450c529 | 4243 | { |
267785bc | 4244 | is_typedef = true; |
4245 | x = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x)); | |
4246 | } | |
6972a3bc | 4247 | |
267785bc | 4248 | /* If it was a typedef, build a pointer to the final, original |
4249 | class. */ | |
4250 | if (is_typedef) | |
4251 | type = build_pointer_type (x); | |
6972a3bc | 4252 | |
267785bc | 4253 | if (cur_try_context->catch_list) |
4254 | { | |
4255 | /* Examine previous @catch clauses and see if we've already | |
4256 | caught the type in question. */ | |
4257 | tree_stmt_iterator i = tsi_start (cur_try_context->catch_list); | |
4258 | for (; !tsi_end_p (i); tsi_next (&i)) | |
4259 | { | |
4260 | tree stmt = tsi_stmt (i); | |
4261 | t = CATCH_TYPES (stmt); | |
4262 | if (t == error_mark_node) | |
4263 | continue; | |
4264 | if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type))) | |
6972a3bc | 4265 | { |
267785bc | 4266 | warning (0, "exception of type %<%T%> will be caught", |
4267 | TREE_TYPE (type)); | |
4268 | warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>", | |
4269 | TREE_TYPE (t ? t : objc_object_type)); | |
4270 | break; | |
6972a3bc | 4271 | } |
c17b85ea | 4272 | } |
c450c529 | 4273 | } |
4274 | } | |
fc304191 | 4275 | |
267785bc | 4276 | t = (*runtime.begin_catch) (&cur_try_context, type, decl, compound, ellipsis); |
4277 | add_stmt (t); | |
4278 | } | |
fc304191 | 4279 | |
267785bc | 4280 | /* Called just after parsing the closing brace of a @catch clause. Close |
4281 | the open binding level, and record a CATCH_EXPR for it. */ | |
c17b85ea | 4282 | |
267785bc | 4283 | void |
4284 | objc_finish_catch_clause (void) | |
4285 | { | |
4286 | tree c = cur_try_context->current_catch; | |
4287 | cur_try_context->current_catch = NULL; | |
4288 | cur_try_context->end_catch_locus = input_location; | |
c17b85ea | 4289 | |
267785bc | 4290 | CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1); |
fc304191 | 4291 | |
267785bc | 4292 | (*runtime.finish_catch) (&cur_try_context, c); |
4293 | } | |
fc304191 | 4294 | |
267785bc | 4295 | /* Called after parsing a @finally clause and its associated BODY. |
4296 | Record the body for later placement. */ | |
fc304191 | 4297 | |
267785bc | 4298 | void |
4299 | objc_build_finally_clause (location_t finally_locus, tree body) | |
4300 | { | |
4301 | cur_try_context->finally_body = body; | |
4302 | cur_try_context->finally_locus = finally_locus; | |
4303 | cur_try_context->end_finally_locus = input_location; | |
260fbc4e | 4304 | } |
4305 | ||
267785bc | 4306 | /* Called to finalize a @try construct. */ |
13dcc150 | 4307 | |
4308 | tree | |
267785bc | 4309 | objc_finish_try_stmt (void) |
13dcc150 | 4310 | { |
267785bc | 4311 | struct objc_try_context *c = cur_try_context; |
4312 | tree stmt; | |
13dcc150 | 4313 | |
267785bc | 4314 | if (c->catch_list == NULL && c->finally_body == NULL) |
4315 | error ("%<@try%> without %<@catch%> or %<@finally%>"); | |
13dcc150 | 4316 | |
267785bc | 4317 | stmt = (*runtime.finish_try_stmt) (&cur_try_context); |
4318 | add_stmt (stmt); | |
13dcc150 | 4319 | |
267785bc | 4320 | cur_try_context = c->outer; |
4321 | free (c); | |
4322 | return stmt; | |
4323 | } | |
c17b85ea | 4324 | |
260fbc4e | 4325 | tree |
267785bc | 4326 | objc_build_throw_stmt (location_t loc, tree throw_expr) |
260fbc4e | 4327 | { |
267785bc | 4328 | bool rethrown = false; |
13dcc150 | 4329 | |
267785bc | 4330 | objc_maybe_warn_exceptions (loc); |
13dcc150 | 4331 | |
267785bc | 4332 | /* Don't waste time trying to build something if we're already dead. */ |
4333 | if (throw_expr == error_mark_node) | |
4334 | return error_mark_node; | |
13dcc150 | 4335 | |
267785bc | 4336 | if (throw_expr == NULL) |
4337 | { | |
4338 | /* If we're not inside a @catch block, there is no "current | |
4339 | exception" to be rethrown. */ | |
4340 | if (cur_try_context == NULL | |
4341 | || cur_try_context->current_catch == NULL) | |
4342 | { | |
4343 | error_at (loc, "%<@throw%> (rethrow) used outside of a @catch block"); | |
4344 | return error_mark_node; | |
4345 | } | |
4346 | ||
4347 | /* Otherwise the object is still sitting in the EXC_PTR_EXPR | |
4348 | value that we get from the runtime. */ | |
4349 | throw_expr = (*runtime.build_exc_ptr) (&cur_try_context); | |
4350 | rethrown = true; | |
4351 | } | |
4352 | else | |
4353 | { | |
4354 | if (!objc_type_valid_for_messaging (TREE_TYPE (throw_expr), true)) | |
4355 | { | |
4356 | error_at (loc, "%<@throw%> argument is not an object"); | |
4357 | return error_mark_node; | |
4358 | } | |
4359 | } | |
4360 | ||
4361 | return (*runtime.build_throw_stmt) (loc, throw_expr, rethrown); | |
fc304191 | 4362 | } |
4363 | ||
267785bc | 4364 | tree |
4365 | objc_build_synchronized (location_t start_locus, tree object_expr, tree body) | |
0ced0389 | 4366 | { |
267785bc | 4367 | /* object_expr should never be NULL; but in case it is, convert it to |
4368 | error_mark_node. */ | |
4369 | if (object_expr == NULL) | |
4370 | object_expr = error_mark_node; | |
0ced0389 | 4371 | |
267785bc | 4372 | /* Validate object_expr. If not valid, set it to error_mark_node. */ |
4373 | if (object_expr != error_mark_node) | |
0ced0389 | 4374 | { |
267785bc | 4375 | if (!objc_type_valid_for_messaging (TREE_TYPE (object_expr), true)) |
4376 | { | |
4377 | error_at (start_locus, "%<@synchronized%> argument is not an object"); | |
4378 | object_expr = error_mark_node; | |
4379 | } | |
0ced0389 | 4380 | } |
6bbb4715 | 4381 | |
267785bc | 4382 | if (object_expr == error_mark_node) |
4383 | { | |
4384 | /* If we found an error, we simply ignore the '@synchronized'. | |
4385 | Compile the body so we can keep going with minimal | |
4386 | casualties. */ | |
4387 | return add_stmt (body); | |
4388 | } | |
4389 | else | |
4390 | { | |
4391 | tree call; | |
4392 | tree args; | |
0ced0389 | 4393 | |
6bbb4715 | 4394 | /* objc_sync_enter (object_expr); */ |
267785bc | 4395 | object_expr = save_expr (object_expr); |
4396 | args = tree_cons (NULL, object_expr, NULL); | |
4397 | call = build_function_call (input_location, | |
4398 | objc_sync_enter_decl, args); | |
4399 | SET_EXPR_LOCATION (call, start_locus); | |
4400 | add_stmt (call); | |
0ced0389 | 4401 | |
267785bc | 4402 | /* Build "objc_sync_exit (object_expr);" but do not add it yet; |
4403 | it goes inside the @finalize() clause. */ | |
4404 | args = tree_cons (NULL, object_expr, NULL); | |
4405 | call = build_function_call (input_location, | |
4406 | objc_sync_exit_decl, args); | |
4407 | SET_EXPR_LOCATION (call, input_location); | |
0ced0389 | 4408 | |
267785bc | 4409 | /* @try { body; } */ |
4410 | objc_begin_try_stmt (start_locus, body); | |
6bbb4715 | 4411 | |
267785bc | 4412 | /* @finally { objc_sync_exit (object_expr); } */ |
4413 | objc_build_finally_clause (input_location, call); | |
6bbb4715 | 4414 | |
267785bc | 4415 | /* End of try statement. */ |
4416 | return objc_finish_try_stmt (); | |
4417 | } | |
0ced0389 | 4418 | } |
4419 | ||
267785bc | 4420 | /* Construct a C struct corresponding to ObjC class CLASS, with the same |
4421 | name as the class: | |
0ced0389 | 4422 | |
267785bc | 4423 | struct <classname> { |
4424 | struct _objc_class *isa; | |
4425 | ... | |
4426 | }; */ | |
0ced0389 | 4427 | |
267785bc | 4428 | static void |
4429 | build_private_template (tree klass) | |
0ced0389 | 4430 | { |
267785bc | 4431 | if (!CLASS_STATIC_TEMPLATE (klass)) |
4432 | { | |
4433 | tree record = objc_build_struct (klass, | |
4434 | get_class_ivars (klass, false), | |
4435 | CLASS_SUPER_NAME (klass)); | |
0ced0389 | 4436 | |
267785bc | 4437 | /* Set the TREE_USED bit for this struct, so that stab generator |
4438 | can emit stabs for this struct type. */ | |
4439 | if (flag_debug_only_used_symbols && TYPE_STUB_DECL (record)) | |
4440 | TREE_USED (TYPE_STUB_DECL (record)) = 1; | |
0ced0389 | 4441 | |
267785bc | 4442 | /* Copy the attributes from the class to the type. */ |
4443 | if (TREE_DEPRECATED (klass)) | |
4444 | TREE_DEPRECATED (record) = 1; | |
267785bc | 4445 | } |
0ced0389 | 4446 | } |
4447 | ||
267785bc | 4448 | /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the |
4449 | current class. */ | |
4450 | #ifdef OBJCPLUS | |
4451 | static void | |
4452 | objc_generate_cxx_ctor_or_dtor (bool dtor) | |
4453 | { | |
4454 | tree fn, body, compound_stmt, ivar; | |
0ced0389 | 4455 | |
267785bc | 4456 | /* - (id) .cxx_construct { ... return self; } */ |
4457 | /* - (void) .cxx_construct { ... } */ | |
0ced0389 | 4458 | |
267785bc | 4459 | objc_start_method_definition |
4460 | (false /* is_class_method */, | |
4461 | objc_build_method_signature (false /* is_class_method */, | |
4462 | build_tree_list (NULL_TREE, | |
4463 | dtor | |
4464 | ? void_type_node | |
4465 | : objc_object_type), | |
4466 | get_identifier (dtor | |
4467 | ? TAG_CXX_DESTRUCT | |
4468 | : TAG_CXX_CONSTRUCT), | |
4469 | make_node (TREE_LIST), | |
4232a958 | 4470 | false), NULL, NULL_TREE); |
267785bc | 4471 | body = begin_function_body (); |
4472 | compound_stmt = begin_compound_stmt (0); | |
0ced0389 | 4473 | |
267785bc | 4474 | ivar = CLASS_IVARS (implementation_template); |
4475 | /* Destroy ivars in reverse order. */ | |
4476 | if (dtor) | |
4477 | ivar = nreverse (copy_list (ivar)); | |
6db57367 | 4478 | |
267785bc | 4479 | for (; ivar; ivar = TREE_CHAIN (ivar)) |
0ced0389 | 4480 | { |
267785bc | 4481 | if (TREE_CODE (ivar) == FIELD_DECL) |
0ced0389 | 4482 | { |
267785bc | 4483 | tree type = TREE_TYPE (ivar); |
0ced0389 | 4484 | |
267785bc | 4485 | /* Call the ivar's default constructor or destructor. Do not |
4486 | call the destructor unless a corresponding constructor call | |
4487 | has also been made (or is not needed). */ | |
4488 | if (MAYBE_CLASS_TYPE_P (type) | |
4489 | && (dtor | |
4490 | ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) | |
4491 | && (!TYPE_NEEDS_CONSTRUCTING (type) | |
4492 | || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))) | |
4493 | : (TYPE_NEEDS_CONSTRUCTING (type) | |
4494 | && TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))) | |
4495 | finish_expr_stmt | |
4496 | (build_special_member_call | |
4497 | (build_ivar_reference (DECL_NAME (ivar)), | |
4498 | dtor ? complete_dtor_identifier : complete_ctor_identifier, | |
4499 | NULL, type, LOOKUP_NORMAL, tf_warning_or_error)); | |
0ced0389 | 4500 | } |
267785bc | 4501 | } |
0ced0389 | 4502 | |
267785bc | 4503 | /* The constructor returns 'self'. */ |
4504 | if (!dtor) | |
4505 | finish_return_stmt (self_decl); | |
0ced0389 | 4506 | |
267785bc | 4507 | finish_compound_stmt (compound_stmt); |
4508 | finish_function_body (body); | |
4509 | fn = current_function_decl; | |
4510 | finish_function (); | |
4511 | objc_finish_method_definition (fn); | |
4512 | } | |
0ced0389 | 4513 | |
267785bc | 4514 | /* The following routine will examine the current @interface for any |
4515 | non-POD C++ ivars requiring non-trivial construction and/or | |
4516 | destruction, and then synthesize special '- .cxx_construct' and/or | |
4517 | '- .cxx_destruct' methods which will run the appropriate | |
4518 | construction or destruction code. Note that ivars inherited from | |
4519 | super-classes are _not_ considered. */ | |
4520 | static void | |
4521 | objc_generate_cxx_cdtors (void) | |
4522 | { | |
4523 | bool need_ctor = false, need_dtor = false; | |
4524 | tree ivar; | |
0ced0389 | 4525 | |
267785bc | 4526 | /* Error case, due to possibly an extra @end. */ |
4527 | if (!objc_implementation_context) | |
4528 | return; | |
0ced0389 | 4529 | |
267785bc | 4530 | /* We do not want to do this for categories, since they do not have |
4531 | their own ivars. */ | |
0ced0389 | 4532 | |
267785bc | 4533 | if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE) |
4534 | return; | |
0ced0389 | 4535 | |
267785bc | 4536 | /* First, determine if we even need a constructor and/or destructor. */ |
0ced0389 | 4537 | |
267785bc | 4538 | for (ivar = CLASS_IVARS (implementation_template); ivar; |
4539 | ivar = TREE_CHAIN (ivar)) | |
4540 | { | |
4541 | if (TREE_CODE (ivar) == FIELD_DECL) | |
4542 | { | |
4543 | tree type = TREE_TYPE (ivar); | |
0ced0389 | 4544 | |
267785bc | 4545 | if (MAYBE_CLASS_TYPE_P (type)) |
4546 | { | |
4547 | if (TYPE_NEEDS_CONSTRUCTING (type) | |
4548 | && TYPE_HAS_DEFAULT_CONSTRUCTOR (type)) | |
4549 | /* NB: If a default constructor is not available, we will not | |
4550 | be able to initialize this ivar; the add_instance_variable() | |
4551 | routine will already have warned about this. */ | |
4552 | need_ctor = true; | |
4553 | ||
4554 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) | |
4555 | && (!TYPE_NEEDS_CONSTRUCTING (type) | |
4556 | || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))) | |
4557 | /* NB: If a default constructor is not available, we will not | |
4558 | call the destructor either, for symmetry. */ | |
4559 | need_dtor = true; | |
4560 | } | |
4561 | } | |
0ced0389 | 4562 | } |
4563 | ||
267785bc | 4564 | /* Generate '- .cxx_construct' if needed. */ |
0ced0389 | 4565 | |
267785bc | 4566 | if (need_ctor) |
4567 | objc_generate_cxx_ctor_or_dtor (false); | |
0ced0389 | 4568 | |
267785bc | 4569 | /* Generate '- .cxx_destruct' if needed. */ |
3c905014 | 4570 | |
267785bc | 4571 | if (need_dtor) |
4572 | objc_generate_cxx_ctor_or_dtor (true); | |
4573 | ||
4574 | /* The 'imp_list' variable points at an imp_entry record for the current | |
4575 | @implementation. Record the existence of '- .cxx_construct' and/or | |
4576 | '- .cxx_destruct' methods therein; it will be included in the | |
4577 | metadata for the class if the runtime needs it. */ | |
4578 | imp_list->has_cxx_cdtors = (need_ctor || need_dtor); | |
0ced0389 | 4579 | } |
267785bc | 4580 | #endif |
0ced0389 | 4581 | |
267785bc | 4582 | static void |
4583 | error_with_ivar (const char *message, tree decl) | |
0ced0389 | 4584 | { |
267785bc | 4585 | error_at (DECL_SOURCE_LOCATION (decl), "%s %qs", |
4586 | message, identifier_to_locale (gen_declaration (decl))); | |
4587 | ||
0ced0389 | 4588 | } |
4589 | ||
267785bc | 4590 | static void |
4591 | check_ivars (tree inter, tree imp) | |
0ced0389 | 4592 | { |
267785bc | 4593 | tree intdecls = CLASS_RAW_IVARS (inter); |
4594 | tree impdecls = CLASS_RAW_IVARS (imp); | |
4595 | ||
4596 | while (1) | |
4597 | { | |
4598 | tree t1, t2; | |
4599 | ||
c17b85ea | 4600 | #ifdef OBJCPLUS |
267785bc | 4601 | if (intdecls && TREE_CODE (intdecls) == TYPE_DECL) |
4602 | intdecls = TREE_CHAIN (intdecls); | |
c17b85ea | 4603 | #endif |
267785bc | 4604 | if (intdecls == 0 && impdecls == 0) |
4605 | break; | |
4606 | if (intdecls == 0 || impdecls == 0) | |
4607 | { | |
4608 | error ("inconsistent instance variable specification"); | |
4609 | break; | |
4610 | } | |
0ced0389 | 4611 | |
267785bc | 4612 | t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls); |
0ced0389 | 4613 | |
267785bc | 4614 | if (!comptypes (t1, t2) |
4615 | || !tree_int_cst_equal (DECL_INITIAL (intdecls), | |
4616 | DECL_INITIAL (impdecls))) | |
4617 | { | |
4618 | if (DECL_NAME (intdecls) == DECL_NAME (impdecls)) | |
4619 | { | |
4620 | error_with_ivar ("conflicting instance variable type", | |
4621 | impdecls); | |
4622 | error_with_ivar ("previous declaration of", | |
4623 | intdecls); | |
4624 | } | |
4625 | else /* both the type and the name don't match */ | |
4626 | { | |
4627 | error ("inconsistent instance variable specification"); | |
4628 | break; | |
4629 | } | |
4630 | } | |
c17b85ea | 4631 | |
267785bc | 4632 | else if (DECL_NAME (intdecls) != DECL_NAME (impdecls)) |
4633 | { | |
4634 | error_with_ivar ("conflicting instance variable name", | |
4635 | impdecls); | |
4636 | error_with_ivar ("previous declaration of", | |
4637 | intdecls); | |
4638 | } | |
c17b85ea | 4639 | |
267785bc | 4640 | intdecls = DECL_CHAIN (intdecls); |
4641 | impdecls = DECL_CHAIN (impdecls); | |
4642 | } | |
c17b85ea | 4643 | } |
4644 | ||
267785bc | 4645 | |
4646 | static void | |
4647 | mark_referenced_methods (void) | |
e93ea189 | 4648 | { |
267785bc | 4649 | struct imp_entry *impent; |
4650 | tree chain; | |
e93ea189 | 4651 | |
267785bc | 4652 | for (impent = imp_list; impent; impent = impent->next) |
4653 | { | |
4654 | chain = CLASS_CLS_METHODS (impent->imp_context); | |
4655 | while (chain) | |
4656 | { | |
415d1b9a | 4657 | cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output (); |
267785bc | 4658 | chain = DECL_CHAIN (chain); |
4659 | } | |
e93ea189 | 4660 | |
267785bc | 4661 | chain = CLASS_NST_METHODS (impent->imp_context); |
4662 | while (chain) | |
4663 | { | |
415d1b9a | 4664 | cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output (); |
267785bc | 4665 | chain = DECL_CHAIN (chain); |
4666 | } | |
4667 | } | |
4668 | } | |
c450c529 | 4669 | |
267785bc | 4670 | /* If type is empty or only type qualifiers are present, add default |
4671 | type of id (otherwise grokdeclarator will default to int). */ | |
4672 | static inline tree | |
4673 | adjust_type_for_id_default (tree type) | |
c450c529 | 4674 | { |
267785bc | 4675 | if (!type) |
4676 | type = make_node (TREE_LIST); | |
499ea517 | 4677 | |
267785bc | 4678 | if (!TREE_VALUE (type)) |
4679 | TREE_VALUE (type) = objc_object_type; | |
4680 | else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE | |
4681 | && TYPED_OBJECT (TREE_VALUE (type))) | |
4682 | error ("can not use an object as parameter to a method"); | |
c17b85ea | 4683 | |
267785bc | 4684 | return type; |
c17b85ea | 4685 | } |
4686 | ||
267785bc | 4687 | /* Return a KEYWORD_DECL built using the specified key_name, arg_type, |
4688 | arg_name and attributes. (TODO: Rename KEYWORD_DECL to | |
4689 | OBJC_METHOD_PARM_DECL ?) | |
c17b85ea | 4690 | |
267785bc | 4691 | A KEYWORD_DECL is a tree representing the declaration of a |
4692 | parameter of an Objective-C method. It is produced when parsing a | |
4693 | fragment of Objective-C method declaration of the form | |
c17b85ea | 4694 | |
267785bc | 4695 | keyworddecl: |
4696 | selector ':' '(' typename ')' identifier | |
c17b85ea | 4697 | |
267785bc | 4698 | For example, take the Objective-C method |
c17b85ea | 4699 | |
6bbb4715 | 4700 | -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type; |
c17b85ea | 4701 | |
267785bc | 4702 | the two fragments "pathForResource:(NSString *)resource" and |
4703 | "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The | |
4704 | KEYWORD_DECL stores the 'key_name' (eg, identifier for | |
4705 | "pathForResource"), the 'arg_type' (eg, tree representing a | |
4706 | NSString *), the 'arg_name' (eg identifier for "resource") and | |
4707 | potentially some attributes (for example, a tree representing | |
4708 | __attribute__ ((unused)) if such an attribute was attached to a | |
4709 | certain parameter). You can access this information using the | |
4710 | TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name), | |
4711 | KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes). | |
c17b85ea | 4712 | |
267785bc | 4713 | 'key_name' is an identifier node (and is optional as you can omit |
4714 | it in Objective-C methods). | |
4715 | 'arg_type' is a tree list (and is optional too if no parameter type | |
4716 | was specified). | |
4717 | 'arg_name' is an identifier node and is required. | |
4718 | 'attributes' is an optional tree containing parameter attributes. */ | |
4719 | tree | |
6bbb4715 | 4720 | objc_build_keyword_decl (tree key_name, tree arg_type, |
267785bc | 4721 | tree arg_name, tree attributes) |
4722 | { | |
4723 | tree keyword_decl; | |
c17b85ea | 4724 | |
267785bc | 4725 | if (flag_objc1_only && attributes) |
4726 | error_at (input_location, "method argument attributes are not available in Objective-C 1.0"); | |
c17b85ea | 4727 | |
267785bc | 4728 | /* If no type is specified, default to "id". */ |
4729 | arg_type = adjust_type_for_id_default (arg_type); | |
2363ef00 | 4730 | |
267785bc | 4731 | keyword_decl = make_node (KEYWORD_DECL); |
58d82cd0 | 4732 | |
267785bc | 4733 | TREE_TYPE (keyword_decl) = arg_type; |
4734 | KEYWORD_ARG_NAME (keyword_decl) = arg_name; | |
4735 | KEYWORD_KEY_NAME (keyword_decl) = key_name; | |
4736 | DECL_ATTRIBUTES (keyword_decl) = attributes; | |
c17b85ea | 4737 | |
267785bc | 4738 | return keyword_decl; |
c17b85ea | 4739 | } |
e24e8df7 | 4740 | |
267785bc | 4741 | /* Given a chain of keyword_decl's, synthesize the full keyword selector. */ |
4742 | static tree | |
4743 | build_keyword_selector (tree selector) | |
c17b85ea | 4744 | { |
267785bc | 4745 | int len = 0; |
4746 | tree key_chain, key_name; | |
4747 | char *buf; | |
c17b85ea | 4748 | |
267785bc | 4749 | /* Scan the selector to see how much space we'll need. */ |
4750 | for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain)) | |
a654f093 | 4751 | { |
267785bc | 4752 | switch (TREE_CODE (selector)) |
33b3681f | 4753 | { |
267785bc | 4754 | case KEYWORD_DECL: |
4755 | key_name = KEYWORD_KEY_NAME (key_chain); | |
4756 | break; | |
4757 | case TREE_LIST: | |
4758 | key_name = TREE_PURPOSE (key_chain); | |
4759 | break; | |
4760 | default: | |
4761 | gcc_unreachable (); | |
33b3681f | 4762 | } |
267785bc | 4763 | |
4764 | if (key_name) | |
4765 | len += IDENTIFIER_LENGTH (key_name) + 1; | |
4766 | else | |
4767 | /* Just a ':' arg. */ | |
4768 | len++; | |
33b3681f | 4769 | } |
a654f093 | 4770 | |
267785bc | 4771 | buf = (char *) alloca (len + 1); |
4772 | /* Start the buffer out as an empty string. */ | |
4773 | buf[0] = '\0'; | |
e24e8df7 | 4774 | |
267785bc | 4775 | for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain)) |
e24e8df7 | 4776 | { |
267785bc | 4777 | switch (TREE_CODE (selector)) |
e24e8df7 | 4778 | { |
267785bc | 4779 | case KEYWORD_DECL: |
4780 | key_name = KEYWORD_KEY_NAME (key_chain); | |
4781 | break; | |
4782 | case TREE_LIST: | |
4783 | key_name = TREE_PURPOSE (key_chain); | |
4784 | /* The keyword decl chain will later be used as a function | |
4785 | argument chain. Unhook the selector itself so as to not | |
4786 | confuse other parts of the compiler. */ | |
4787 | TREE_PURPOSE (key_chain) = NULL_TREE; | |
4788 | break; | |
4789 | default: | |
4790 | gcc_unreachable (); | |
e24e8df7 | 4791 | } |
c17b85ea | 4792 | |
267785bc | 4793 | if (key_name) |
4794 | strcat (buf, IDENTIFIER_POINTER (key_name)); | |
4795 | strcat (buf, ":"); | |
4796 | } | |
c17b85ea | 4797 | |
d546b341 | 4798 | return get_identifier_with_length (buf, len); |
c17b85ea | 4799 | } |
4800 | ||
267785bc | 4801 | /* Used for declarations and definitions. */ |
e24e8df7 | 4802 | |
4803 | static tree | |
267785bc | 4804 | build_method_decl (enum tree_code code, tree ret_type, tree selector, |
4805 | tree add_args, bool ellipsis) | |
c17b85ea | 4806 | { |
267785bc | 4807 | tree method_decl; |
c17b85ea | 4808 | |
267785bc | 4809 | /* If no type is specified, default to "id". */ |
4810 | ret_type = adjust_type_for_id_default (ret_type); | |
c17b85ea | 4811 | |
267785bc | 4812 | /* Note how a method_decl has a TREE_TYPE which is not the function |
4813 | type of the function implementing the method, but only the return | |
4814 | type of the method. We may want to change this, and store the | |
4815 | entire function type in there (eg, it may be used to simplify | |
4816 | dealing with attributes below). */ | |
4817 | method_decl = make_node (code); | |
4818 | TREE_TYPE (method_decl) = ret_type; | |
c17b85ea | 4819 | |
267785bc | 4820 | /* If we have a keyword selector, create an identifier_node that |
4821 | represents the full selector name (`:' included)... */ | |
4822 | if (TREE_CODE (selector) == KEYWORD_DECL) | |
4823 | { | |
4824 | METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector); | |
4825 | METHOD_SEL_ARGS (method_decl) = selector; | |
4826 | METHOD_ADD_ARGS (method_decl) = add_args; | |
4827 | METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis; | |
4828 | } | |
4829 | else | |
4830 | { | |
4831 | METHOD_SEL_NAME (method_decl) = selector; | |
4832 | METHOD_SEL_ARGS (method_decl) = NULL_TREE; | |
4833 | METHOD_ADD_ARGS (method_decl) = NULL_TREE; | |
4834 | } | |
c17b85ea | 4835 | |
267785bc | 4836 | return method_decl; |
c17b85ea | 4837 | } |
4838 | ||
267785bc | 4839 | /* This routine processes objective-c method attributes. */ |
6db57367 | 4840 | |
267785bc | 4841 | static void |
4842 | objc_decl_method_attributes (tree *node, tree attributes, int flags) | |
c17b85ea | 4843 | { |
267785bc | 4844 | /* TODO: Replace the hackery below. An idea would be to store the |
4845 | full function type in the method declaration (for example in | |
4846 | TREE_TYPE) and then expose ObjC method declarations to c-family | |
4847 | and they could deal with them by simply treating them as | |
4848 | functions. */ | |
c17b85ea | 4849 | |
267785bc | 4850 | /* Because of the dangers in the hackery below, we filter out any |
4851 | attribute that we do not know about. For the ones we know about, | |
4852 | we know that they work with the hackery. For the other ones, | |
4853 | there is no guarantee, so we have to filter them out. */ | |
4854 | tree filtered_attributes = NULL_TREE; | |
c17b85ea | 4855 | |
267785bc | 4856 | if (attributes) |
c17b85ea | 4857 | { |
267785bc | 4858 | tree attribute; |
4859 | for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute)) | |
e24e8df7 | 4860 | { |
267785bc | 4861 | tree name = TREE_PURPOSE (attribute); |
6bbb4715 | 4862 | |
267785bc | 4863 | if (is_attribute_p ("deprecated", name) |
4864 | || is_attribute_p ("sentinel", name) | |
4865 | || is_attribute_p ("noreturn", name)) | |
e24e8df7 | 4866 | { |
267785bc | 4867 | /* An attribute that we support; add it to the filtered |
4868 | attributes. */ | |
6bbb4715 | 4869 | filtered_attributes = chainon (filtered_attributes, |
267785bc | 4870 | copy_node (attribute)); |
e24e8df7 | 4871 | } |
267785bc | 4872 | else if (is_attribute_p ("format", name)) |
4873 | { | |
4874 | /* "format" is special because before adding it to the | |
4875 | filtered attributes we need to adjust the specified | |
4876 | format by adding the hidden function parameters for | |
4877 | an Objective-C method (self, _cmd). */ | |
4878 | tree new_attribute = copy_node (attribute); | |
4879 | ||
4880 | /* Check the arguments specified with the attribute, and | |
4881 | modify them adding 2 for the two hidden arguments. | |
4882 | Note how this differs from C++; according to the | |
4883 | specs, C++ does not do it so you have to add the +1 | |
4884 | yourself. For Objective-C, instead, the compiler | |
4885 | adds the +2 for you. */ | |
4886 | ||
4887 | /* The attribute arguments have not been checked yet, so | |
4888 | we need to be careful as they could be missing or | |
4889 | invalid. If anything looks wrong, we skip the | |
4890 | process and the compiler will complain about it later | |
4891 | when it validates the attribute. */ | |
4892 | /* Check that we have at least three arguments. */ | |
4893 | if (TREE_VALUE (new_attribute) | |
4894 | && TREE_CHAIN (TREE_VALUE (new_attribute)) | |
4895 | && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (new_attribute)))) | |
4896 | { | |
4897 | tree second_argument = TREE_CHAIN (TREE_VALUE (new_attribute)); | |
4898 | tree third_argument = TREE_CHAIN (second_argument); | |
4899 | tree number; | |
c17b85ea | 4900 | |
267785bc | 4901 | /* This is the second argument, the "string-index", |
4902 | which specifies the index of the format string | |
4903 | argument. Add 2. */ | |
4904 | number = TREE_VALUE (second_argument); | |
936a705d | 4905 | if (number |
4906 | && TREE_CODE (number) == INTEGER_CST | |
4907 | && !wi::eq_p (number, 0)) | |
e913b5cd | 4908 | TREE_VALUE (second_argument) |
705abd1a | 4909 | = wide_int_to_tree (TREE_TYPE (number), |
4910 | wi::add (number, 2)); | |
6bbb4715 | 4911 | |
267785bc | 4912 | /* This is the third argument, the "first-to-check", |
4913 | which specifies the index of the first argument to | |
4914 | check. This could be 0, meaning it is not available, | |
4915 | in which case we don't need to add 2. Add 2 if not | |
4916 | 0. */ | |
4917 | number = TREE_VALUE (third_argument); | |
936a705d | 4918 | if (number |
4919 | && TREE_CODE (number) == INTEGER_CST | |
4920 | && !wi::eq_p (number, 0)) | |
e913b5cd | 4921 | TREE_VALUE (third_argument) |
705abd1a | 4922 | = wide_int_to_tree (TREE_TYPE (number), |
4923 | wi::add (number, 2)); | |
267785bc | 4924 | } |
4925 | filtered_attributes = chainon (filtered_attributes, | |
4926 | new_attribute); | |
4927 | } | |
5aaf62c7 | 4928 | else if (is_attribute_p ("nonnull", name)) |
4929 | { | |
4930 | /* We need to fixup all the argument indexes by adding 2 | |
4931 | for the two hidden arguments of an Objective-C method | |
4932 | invocation, similat to what we do above for the | |
4933 | "format" attribute. */ | |
4934 | /* FIXME: This works great in terms of implementing the | |
4935 | functionality, but the warnings that are produced by | |
4936 | nonnull do mention the argument index (while the | |
4937 | format ones don't). For example, you could get | |
4938 | "warning: null argument where non-null required | |
4939 | (argument 3)". Now in that message, "argument 3" | |
4940 | includes the 2 hidden arguments; it would be much | |
4941 | more friendly to call it "argument 1", as that would | |
4942 | be consistent with __attribute__ ((nonnnull (1))). | |
4943 | To do this, we'd need to have the C family code that | |
4944 | checks the arguments know about adding/removing 2 to | |
4945 | the argument index ... or alternatively we could | |
4946 | maybe store the "printable" argument index in | |
4947 | addition to the actual argument index ? Some | |
4948 | refactoring is needed to do this elegantly. */ | |
4949 | tree new_attribute = copy_node (attribute); | |
4950 | tree argument = TREE_VALUE (attribute); | |
4951 | while (argument != NULL_TREE) | |
4952 | { | |
4953 | /* Get the value of the argument and add 2. */ | |
4954 | tree number = TREE_VALUE (argument); | |
2c5faab9 | 4955 | if (number && TREE_CODE (number) == INTEGER_CST |
796b6678 | 4956 | && !wi::eq_p (number, 0)) |
e913b5cd | 4957 | TREE_VALUE (argument) |
705abd1a | 4958 | = wide_int_to_tree (TREE_TYPE (number), |
4959 | wi::add (number, 2)); | |
5aaf62c7 | 4960 | argument = TREE_CHAIN (argument); |
4961 | } | |
4962 | ||
4963 | filtered_attributes = chainon (filtered_attributes, | |
4964 | new_attribute); | |
4965 | } | |
267785bc | 4966 | else |
4967 | warning (OPT_Wattributes, "%qE attribute directive ignored", name); | |
e24e8df7 | 4968 | } |
4969 | } | |
c17b85ea | 4970 | |
267785bc | 4971 | if (filtered_attributes) |
c17b85ea | 4972 | { |
267785bc | 4973 | /* This hackery changes the TREE_TYPE of the ObjC method |
4974 | declaration to be a function type, so that decl_attributes | |
4975 | will treat the ObjC method as if it was a function. Some | |
4976 | attributes (sentinel, format) will be applied to the function | |
4977 | type, changing it in place; so after calling decl_attributes, | |
4978 | we extract the function type attributes and store them in | |
4979 | METHOD_TYPE_ATTRIBUTES. Some other attributes (noreturn, | |
4980 | deprecated) are applied directly to the method declaration | |
4981 | (by setting TREE_DEPRECATED and TREE_THIS_VOLATILE) so there | |
4982 | is nothing to do. */ | |
4983 | tree saved_type = TREE_TYPE (*node); | |
5318d5a9 | 4984 | TREE_TYPE (*node) |
4985 | = build_function_type_for_method (TREE_VALUE (saved_type), *node, | |
4986 | METHOD_REF, 0); | |
267785bc | 4987 | decl_attributes (node, filtered_attributes, flags); |
4988 | METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node)); | |
4989 | TREE_TYPE (*node) = saved_type; | |
c17b85ea | 4990 | } |
c17b85ea | 4991 | } |
4992 | ||
6bbb4715 | 4993 | bool |
267785bc | 4994 | objc_method_decl (enum tree_code opcode) |
4995 | { | |
4996 | return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL; | |
4997 | } | |
c17b85ea | 4998 | |
5318d5a9 | 4999 | /* Return a function type for METHOD with RETURN_TYPE. CONTEXT is |
5000 | either METHOD_DEF or METHOD_REF, indicating whether we are defining a | |
5001 | method or calling one. SUPER_FLAG indicates whether this is a send | |
5002 | to super; this makes a difference for the NeXT calling sequence in | |
5003 | which the lookup and the method call are done together. If METHOD is | |
5004 | NULL, user-defined arguments (i.e., beyond self and _cmd) shall be | |
5005 | represented as varargs. */ | |
c17b85ea | 5006 | |
267785bc | 5007 | tree |
5318d5a9 | 5008 | build_function_type_for_method (tree return_type, tree method, |
5009 | int context, bool super_flag) | |
e24e8df7 | 5010 | { |
f1f41a6c | 5011 | vec<tree, va_gc> *argtypes = make_tree_vector (); |
5318d5a9 | 5012 | tree t, ftype; |
5013 | bool is_varargs = false; | |
e24e8df7 | 5014 | |
5318d5a9 | 5015 | (*runtime.get_arg_type_list_base) (&argtypes, method, context, super_flag); |
e24e8df7 | 5016 | |
5318d5a9 | 5017 | /* No actual method prototype given; remaining args passed as varargs. */ |
5018 | if (method == NULL_TREE) | |
5019 | { | |
5020 | is_varargs = true; | |
5021 | goto build_ftype; | |
5022 | } | |
e24e8df7 | 5023 | |
5318d5a9 | 5024 | for (t = METHOD_SEL_ARGS (method); t; t = DECL_CHAIN (t)) |
267785bc | 5025 | { |
5318d5a9 | 5026 | tree arg_type = TREE_VALUE (TREE_TYPE (t)); |
e24e8df7 | 5027 | |
5318d5a9 | 5028 | /* Decay argument types for the underlying C function as |
5029 | appropriate. */ | |
267785bc | 5030 | arg_type = objc_decay_parm_type (arg_type); |
e24e8df7 | 5031 | |
f1f41a6c | 5032 | vec_safe_push (argtypes, arg_type); |
267785bc | 5033 | } |
e24e8df7 | 5034 | |
5318d5a9 | 5035 | if (METHOD_ADD_ARGS (method)) |
c17b85ea | 5036 | { |
5318d5a9 | 5037 | for (t = TREE_CHAIN (METHOD_ADD_ARGS (method)); |
5038 | t; t = TREE_CHAIN (t)) | |
267785bc | 5039 | { |
5318d5a9 | 5040 | tree arg_type = TREE_TYPE (TREE_VALUE (t)); |
267785bc | 5041 | |
5042 | arg_type = objc_decay_parm_type (arg_type); | |
e24e8df7 | 5043 | |
f1f41a6c | 5044 | vec_safe_push (argtypes, arg_type); |
267785bc | 5045 | } |
e24e8df7 | 5046 | |
5318d5a9 | 5047 | if (METHOD_ADD_ARGS_ELLIPSIS_P (method)) |
5048 | is_varargs = true; | |
c17b85ea | 5049 | } |
5318d5a9 | 5050 | |
5051 | build_ftype: | |
5052 | if (is_varargs) | |
5053 | ftype = build_varargs_function_type_vec (return_type, argtypes); | |
e24e8df7 | 5054 | else |
5318d5a9 | 5055 | ftype = build_function_type_vec (return_type, argtypes); |
c17b85ea | 5056 | |
5318d5a9 | 5057 | release_tree_vector (argtypes); |
5058 | return ftype; | |
267785bc | 5059 | } |
c17b85ea | 5060 | |
f41791cf | 5061 | /* The 'method' argument is a tree; this tree could either be a single |
5062 | method, which is returned, or could be a TREE_VEC containing a list | |
5063 | of methods. In that case, the first one is returned, and warnings | |
5064 | are issued as appropriate. */ | |
267785bc | 5065 | static tree |
f41791cf | 5066 | check_duplicates (tree method, int methods, int is_class) |
e24e8df7 | 5067 | { |
f41791cf | 5068 | tree first_method; |
5069 | size_t i; | |
c17b85ea | 5070 | |
f41791cf | 5071 | if (method == NULL_TREE) |
5072 | return NULL_TREE; | |
267785bc | 5073 | |
f41791cf | 5074 | if (TREE_CODE (method) != TREE_VEC) |
5075 | return method; | |
5076 | ||
5077 | /* We have two or more methods with the same name but different | |
5078 | types. */ | |
5079 | first_method = TREE_VEC_ELT (method, 0); | |
5080 | ||
5081 | /* But just how different are those types? If | |
5082 | -Wno-strict-selector-match is specified, we shall not complain if | |
5083 | the differences are solely among types with identical size and | |
5084 | alignment. */ | |
5085 | if (!warn_strict_selector_match) | |
5086 | { | |
d69c64b7 | 5087 | for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++) |
f41791cf | 5088 | if (!comp_proto_with_proto (first_method, TREE_VEC_ELT (method, i), 0)) |
5089 | goto issue_warning; | |
5090 | ||
5091 | return first_method; | |
e24e8df7 | 5092 | } |
f41791cf | 5093 | |
5094 | issue_warning: | |
5095 | if (methods) | |
5096 | { | |
5097 | bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL; | |
5098 | ||
5099 | warning_at (input_location, 0, | |
5100 | "multiple methods named %<%c%E%> found", | |
5101 | (is_class ? '+' : '-'), | |
5102 | METHOD_SEL_NAME (first_method)); | |
5103 | inform (DECL_SOURCE_LOCATION (first_method), "using %<%c%s%>", | |
5104 | (type ? '-' : '+'), | |
5105 | identifier_to_locale (gen_method_decl (first_method))); | |
5106 | } | |
5107 | else | |
5108 | { | |
5109 | bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL; | |
5110 | ||
5111 | warning_at (input_location, 0, | |
5112 | "multiple selectors named %<%c%E%> found", | |
5113 | (is_class ? '+' : '-'), | |
5114 | METHOD_SEL_NAME (first_method)); | |
5115 | inform (DECL_SOURCE_LOCATION (first_method), "found %<%c%s%>", | |
5116 | (type ? '-' : '+'), | |
5117 | identifier_to_locale (gen_method_decl (first_method))); | |
5118 | } | |
5119 | ||
d69c64b7 | 5120 | for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++) |
f41791cf | 5121 | { |
5122 | bool type = TREE_CODE (TREE_VEC_ELT (method, i)) == INSTANCE_METHOD_DECL; | |
5123 | ||
5124 | inform (DECL_SOURCE_LOCATION (TREE_VEC_ELT (method, i)), "also found %<%c%s%>", | |
5125 | (type ? '-' : '+'), | |
5126 | identifier_to_locale (gen_method_decl (TREE_VEC_ELT (method, i)))); | |
5127 | } | |
5128 | ||
5129 | return first_method; | |
e24e8df7 | 5130 | } |
c17b85ea | 5131 | |
267785bc | 5132 | /* If RECEIVER is a class reference, return the identifier node for |
5133 | the referenced class. RECEIVER is created by objc_get_class_reference, | |
5134 | so we check the exact form created depending on which runtimes are | |
5135 | used. */ | |
c17b85ea | 5136 | |
267785bc | 5137 | static tree |
5138 | receiver_is_class_object (tree receiver, int self, int super) | |
e24e8df7 | 5139 | { |
267785bc | 5140 | tree exp, arg; |
c17b85ea | 5141 | |
267785bc | 5142 | /* The receiver is 'self' or 'super' in the context of a class method. */ |
5143 | if (objc_method_context | |
5144 | && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL | |
5145 | && (self || super)) | |
5146 | return (super | |
5147 | ? CLASS_SUPER_NAME (implementation_template) | |
5148 | : CLASS_NAME (implementation_template)); | |
5149 | ||
5150 | /* The runtime might encapsulate things its own way. */ | |
5151 | exp = (*runtime.receiver_is_class_object) (receiver); | |
5152 | if (exp) | |
5153 | return exp; | |
5154 | ||
5155 | /* The receiver is a function call that returns an id. Check if | |
7e23366d | 5156 | it is a call to objc_getClass, if so, pick up the class name. |
5157 | ||
5158 | This is required by the GNU runtime, which compiles | |
5159 | ||
5160 | [NSObject alloc] | |
5161 | ||
5162 | into | |
5163 | ||
5164 | [objc_get_class ("NSObject") alloc]; | |
5165 | ||
5166 | and then, to check that the receiver responds to the +alloc | |
5167 | method, needs to be able to determine that the objc_get_class() | |
5168 | call returns the NSObject class and not just a generic Class | |
5169 | pointer. | |
5170 | ||
5171 | But, traditionally this is enabled for all runtimes, not just the | |
5172 | GNU one, which means that the compiler is smarter than you'd | |
5173 | expect when dealing with objc_getClass(). For example, with the | |
5174 | Apple runtime, in the code | |
5175 | ||
5176 | [objc_getClass ("NSObject") alloc]; | |
5177 | ||
5178 | the compiler will recognize the objc_getClass() call as special | |
5179 | (due to the code below) and so will know that +alloc is called on | |
5180 | the 'NSObject' class, and can perform the corresponding checks. | |
5181 | ||
5182 | Programmers can disable this behaviour by casting the results of | |
5183 | objc_getClass() to 'Class' (this may seem weird because | |
5184 | objc_getClass() is already declared to return 'Class', but the | |
5185 | compiler treats it as a special function). This may be useful if | |
5186 | the class is never declared, and the compiler would complain | |
5187 | about a missing @interface for it. Then, you can do | |
5188 | ||
5189 | [(Class)objc_getClass ("MyClassNeverDeclared") alloc]; | |
5190 | ||
5191 | to silence the warnings. */ | |
267785bc | 5192 | if (TREE_CODE (receiver) == CALL_EXPR |
5193 | && (exp = CALL_EXPR_FN (receiver)) | |
5194 | && TREE_CODE (exp) == ADDR_EXPR | |
5195 | && (exp = TREE_OPERAND (exp, 0)) | |
5196 | && TREE_CODE (exp) == FUNCTION_DECL | |
5197 | /* For some reason, we sometimes wind up with multiple FUNCTION_DECL | |
5198 | prototypes for objc_get_class(). Thankfully, they seem to share the | |
5199 | same function type. */ | |
5200 | && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl) | |
5201 | && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), runtime.tag_getclass) | |
5202 | /* We have a call to objc_get_class/objc_getClass! */ | |
5203 | && (arg = CALL_EXPR_ARG (receiver, 0))) | |
5204 | { | |
5205 | STRIP_NOPS (arg); | |
5206 | if (TREE_CODE (arg) == ADDR_EXPR | |
5207 | && (arg = TREE_OPERAND (arg, 0)) | |
5208 | && TREE_CODE (arg) == STRING_CST) | |
5209 | /* Finally, we have the class name. */ | |
5210 | return get_identifier (TREE_STRING_POINTER (arg)); | |
5211 | } | |
5212 | return 0; | |
c17b85ea | 5213 | } |
5214 | ||
267785bc | 5215 | /* If we are currently building a message expr, this holds |
5216 | the identifier of the selector of the message. This is | |
5217 | used when printing warnings about argument mismatches. */ | |
c17b85ea | 5218 | |
267785bc | 5219 | static tree current_objc_message_selector = 0; |
5220 | ||
5221 | tree | |
5222 | objc_message_selector (void) | |
e24e8df7 | 5223 | { |
267785bc | 5224 | return current_objc_message_selector; |
e24e8df7 | 5225 | } |
c17b85ea | 5226 | |
267785bc | 5227 | /* Construct an expression for sending a message. |
5228 | MESS has the object to send to in TREE_PURPOSE | |
5229 | and the argument list (including selector) in TREE_VALUE. | |
5230 | ||
5231 | (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...); | |
5232 | (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */ | |
c17b85ea | 5233 | |
0ced0389 | 5234 | tree |
4185cf58 | 5235 | objc_build_message_expr (tree receiver, tree message_args) |
e24e8df7 | 5236 | { |
267785bc | 5237 | tree sel_name; |
5238 | #ifdef OBJCPLUS | |
4185cf58 | 5239 | tree args = TREE_PURPOSE (message_args); |
267785bc | 5240 | #else |
4185cf58 | 5241 | tree args = message_args; |
267785bc | 5242 | #endif |
5243 | tree method_params = NULL_TREE; | |
e24e8df7 | 5244 | |
267785bc | 5245 | if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK) |
5246 | return error_mark_node; | |
c17b85ea | 5247 | |
267785bc | 5248 | /* Obtain the full selector name. */ |
5249 | switch (TREE_CODE (args)) | |
e24e8df7 | 5250 | { |
267785bc | 5251 | case IDENTIFIER_NODE: |
5252 | /* A unary selector. */ | |
5253 | sel_name = args; | |
5254 | break; | |
5255 | case TREE_LIST: | |
5256 | sel_name = build_keyword_selector (args); | |
5257 | break; | |
5258 | default: | |
5259 | gcc_unreachable (); | |
e24e8df7 | 5260 | } |
267785bc | 5261 | |
5262 | /* Build the parameter list to give to the method. */ | |
5263 | if (TREE_CODE (args) == TREE_LIST) | |
5264 | #ifdef OBJCPLUS | |
4185cf58 | 5265 | method_params = chainon (args, TREE_VALUE (message_args)); |
267785bc | 5266 | #else |
e24e8df7 | 5267 | { |
267785bc | 5268 | tree chain = args, prev = NULL_TREE; |
5269 | ||
5270 | /* We have a keyword selector--check for comma expressions. */ | |
5271 | while (chain) | |
e24e8df7 | 5272 | { |
267785bc | 5273 | tree element = TREE_VALUE (chain); |
5274 | ||
5275 | /* We have a comma expression, must collapse... */ | |
5276 | if (TREE_CODE (element) == TREE_LIST) | |
5277 | { | |
5278 | if (prev) | |
5279 | TREE_CHAIN (prev) = element; | |
5280 | else | |
5281 | args = element; | |
5282 | } | |
5283 | prev = chain; | |
5284 | chain = TREE_CHAIN (chain); | |
5285 | } | |
5286 | method_params = args; | |
e24e8df7 | 5287 | } |
267785bc | 5288 | #endif |
e24e8df7 | 5289 | |
267785bc | 5290 | #ifdef OBJCPLUS |
5291 | if (processing_template_decl) | |
5292 | /* Must wait until template instantiation time. */ | |
009ec0e5 | 5293 | return build_min_nt_loc (UNKNOWN_LOCATION, MESSAGE_SEND_EXPR, receiver, |
5294 | sel_name, method_params); | |
267785bc | 5295 | #endif |
5296 | ||
5297 | return objc_finish_message_expr (receiver, sel_name, method_params, NULL); | |
c17b85ea | 5298 | } |
5299 | ||
267785bc | 5300 | /* Look up method SEL_NAME that would be suitable for receiver |
5301 | of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is | |
5302 | nonzero), and report on any duplicates. */ | |
0375a275 | 5303 | |
267785bc | 5304 | static tree |
5305 | lookup_method_in_hash_lists (tree sel_name, int is_class) | |
5306 | { | |
f41791cf | 5307 | tree method_prototype = OBJC_MAP_NOT_FOUND; |
c17b85ea | 5308 | |
267785bc | 5309 | if (!is_class) |
f41791cf | 5310 | method_prototype = objc_map_get (instance_method_map, sel_name); |
5311 | ||
5312 | if (method_prototype == OBJC_MAP_NOT_FOUND) | |
40ec327c | 5313 | { |
f41791cf | 5314 | method_prototype = objc_map_get (class_method_map, sel_name); |
267785bc | 5315 | is_class = 1; |
f41791cf | 5316 | |
5317 | if (method_prototype == OBJC_MAP_NOT_FOUND) | |
5318 | return NULL_TREE; | |
40ec327c | 5319 | } |
e24e8df7 | 5320 | |
267785bc | 5321 | return check_duplicates (method_prototype, 1, is_class); |
c17b85ea | 5322 | } |
5323 | ||
267785bc | 5324 | /* The 'objc_finish_message_expr' routine is called from within |
5325 | 'objc_build_message_expr' for non-template functions. In the case of | |
5326 | C++ template functions, it is called from 'build_expr_from_tree' | |
5327 | (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded. | |
5328 | ||
5329 | If the DEPRECATED_METHOD_PROTOTYPE argument is NULL, then we warn | |
5330 | if the method being used is deprecated. If it is not NULL, instead | |
5331 | of deprecating, we set *DEPRECATED_METHOD_PROTOTYPE to the method | |
5332 | prototype that was used and is deprecated. This is useful for | |
5333 | getter calls that are always generated when compiling dot-syntax | |
5334 | expressions, even if they may not be used. In that case, we don't | |
5335 | want the warning immediately; we produce it (if needed) at gimplify | |
5336 | stage when we are sure that the deprecated getter is being | |
5337 | used. */ | |
0ced0389 | 5338 | tree |
267785bc | 5339 | objc_finish_message_expr (tree receiver, tree sel_name, tree method_params, |
5340 | tree *deprecated_method_prototype) | |
5cada957 | 5341 | { |
267785bc | 5342 | tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype; |
5343 | tree retval, class_tree; | |
5344 | int self, super, have_cast; | |
5cada957 | 5345 | |
267785bc | 5346 | /* We have used the receiver, so mark it as read. */ |
5347 | mark_exp_read (receiver); | |
5348 | ||
5349 | /* Extract the receiver of the message, as well as its type | |
5350 | (where the latter may take the form of a cast or be inferred | |
5351 | from the implementation context). */ | |
5352 | rtype = receiver; | |
5353 | while (TREE_CODE (rtype) == COMPOUND_EXPR | |
a53aa046 | 5354 | || TREE_CODE (rtype) == MODIFY_EXPR |
5355 | || CONVERT_EXPR_P (rtype) | |
5356 | || TREE_CODE (rtype) == COMPONENT_REF) | |
267785bc | 5357 | rtype = TREE_OPERAND (rtype, 0); |
5358 | ||
a53aa046 | 5359 | /* self is 1 if this is a message to self, 0 otherwise */ |
267785bc | 5360 | self = (rtype == self_decl); |
a53aa046 | 5361 | |
5362 | /* super is 1 if this is a message to super, 0 otherwise. */ | |
267785bc | 5363 | super = (rtype == UOBJC_SUPER_decl); |
a53aa046 | 5364 | |
5365 | /* rtype is the type of the receiver. */ | |
267785bc | 5366 | rtype = TREE_TYPE (receiver); |
5367 | ||
a53aa046 | 5368 | /* have_cast is 1 if the receiver is casted. */ |
267785bc | 5369 | have_cast = (TREE_CODE (receiver) == NOP_EXPR |
5370 | || (TREE_CODE (receiver) == COMPOUND_EXPR | |
5371 | && !IS_SUPER (rtype))); | |
5372 | ||
5373 | /* If we are calling [super dealloc], reset our warning flag. */ | |
5374 | if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name))) | |
5375 | should_call_super_dealloc = 0; | |
5376 | ||
5377 | /* If the receiver is a class object, retrieve the corresponding | |
a53aa046 | 5378 | @interface, if one exists. class_tree is the class name |
5379 | identifier, or NULL_TREE if this is not a class method or the | |
5380 | class name could not be determined (as in the case "Class c; [c | |
5381 | method];"). */ | |
267785bc | 5382 | class_tree = receiver_is_class_object (receiver, self, super); |
5383 | ||
5384 | /* Now determine the receiver type (if an explicit cast has not been | |
5385 | provided). */ | |
5386 | if (!have_cast) | |
5cada957 | 5387 | { |
267785bc | 5388 | if (class_tree) |
a53aa046 | 5389 | { |
5390 | /* We are here when we have no cast, and we have a class | |
5391 | name. So, this is a plain method to a class object, as | |
5392 | in [NSObject alloc]. Find the interface corresponding to | |
5393 | the class name. */ | |
5394 | rtype = lookup_interface (class_tree); | |
5395 | ||
5396 | if (rtype == NULL_TREE) | |
5397 | { | |
5398 | /* If 'rtype' is NULL_TREE at this point it means that | |
5399 | we have seen no @interface corresponding to that | |
7e23366d | 5400 | class name, only a @class declaration (alternatively, |
5401 | this was a call such as [objc_getClass("SomeClass") | |
5402 | alloc], where we've never seen the @interface of | |
5403 | SomeClass). So, we have a class name (class_tree) | |
5404 | but no actual details of the class methods. We won't | |
5405 | be able to check that the class responds to the | |
5406 | method, and we will have to guess the method | |
5407 | prototype. Emit a warning, then keep going (this | |
5408 | will use any method with a matching name, as if the | |
5409 | receiver was of type 'Class'). */ | |
a53aa046 | 5410 | warning (0, "@interface of class %qE not found", class_tree); |
5411 | } | |
5412 | } | |
267785bc | 5413 | /* Handle `self' and `super'. */ |
5414 | else if (super) | |
5cada957 | 5415 | { |
267785bc | 5416 | if (!CLASS_SUPER_NAME (implementation_template)) |
5417 | { | |
5418 | error ("no super class declared in @interface for %qE", | |
5419 | CLASS_NAME (implementation_template)); | |
5420 | return error_mark_node; | |
5421 | } | |
5422 | rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template)); | |
5cada957 | 5423 | } |
267785bc | 5424 | else if (self) |
5425 | rtype = lookup_interface (CLASS_NAME (implementation_template)); | |
5cada957 | 5426 | } |
267785bc | 5427 | |
267785bc | 5428 | if (objc_is_id (rtype)) |
5cada957 | 5429 | { |
a53aa046 | 5430 | /* The receiver is of type 'id' or 'Class' (with or without some |
5431 | protocols attached to it). */ | |
5432 | ||
5433 | /* We set class_tree to the identifier for 'Class' if this is a | |
5434 | class method, and to NULL_TREE if not. */ | |
267785bc | 5435 | class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE); |
a53aa046 | 5436 | |
5437 | /* 'rprotos' is the list of protocols that the receiver | |
5438 | supports. */ | |
267785bc | 5439 | rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype)) |
5440 | ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype)) | |
5441 | : NULL_TREE); | |
a53aa046 | 5442 | |
5443 | /* We have no information on the type, and we set it to | |
5444 | NULL_TREE. */ | |
267785bc | 5445 | rtype = NULL_TREE; |
5446 | ||
a53aa046 | 5447 | /* If there are any protocols, check that the method we are |
5448 | calling appears in the protocol list. If there are no | |
5449 | protocols, this is a message to 'id' or 'Class' and we accept | |
5450 | any method that exists. */ | |
267785bc | 5451 | if (rprotos) |
5452 | { | |
a53aa046 | 5453 | /* If messaging 'id <Protos>' or 'Class <Proto>', first |
5454 | search in protocols themselves for the method | |
5455 | prototype. */ | |
267785bc | 5456 | method_prototype |
5457 | = lookup_method_in_protocol_list (rprotos, sel_name, | |
5458 | class_tree != NULL_TREE); | |
5459 | ||
a53aa046 | 5460 | /* If messaging 'Class <Proto>' but did not find a class |
5461 | method prototype, search for an instance method instead, | |
5462 | and warn about having done so. */ | |
267785bc | 5463 | if (!method_prototype && !rtype && class_tree != NULL_TREE) |
5464 | { | |
5465 | method_prototype | |
5466 | = lookup_method_in_protocol_list (rprotos, sel_name, 0); | |
5467 | ||
5468 | if (method_prototype) | |
5469 | warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)", | |
5470 | sel_name, sel_name); | |
5471 | } | |
5472 | } | |
5cada957 | 5473 | } |
267785bc | 5474 | else if (rtype) |
5cada957 | 5475 | { |
a53aa046 | 5476 | /* We have a receiver type which is more specific than 'id' or |
5477 | 'Class'. */ | |
267785bc | 5478 | tree orig_rtype = rtype; |
5cada957 | 5479 | |
267785bc | 5480 | if (TREE_CODE (rtype) == POINTER_TYPE) |
5481 | rtype = TREE_TYPE (rtype); | |
5482 | /* Traverse typedef aliases */ | |
5483 | while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype) | |
5484 | && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL | |
5485 | && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype))) | |
5486 | rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)); | |
5487 | if (TYPED_OBJECT (rtype)) | |
5488 | { | |
5489 | rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype); | |
5490 | rtype = TYPE_OBJC_INTERFACE (rtype); | |
5491 | } | |
267785bc | 5492 | if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE) |
5493 | { | |
a53aa046 | 5494 | /* If we could not find an @interface declaration, we must |
5495 | have only seen a @class declaration; so, we cannot say | |
5496 | anything more intelligent about which methods the | |
5497 | receiver will understand. Note that this only happens | |
5498 | for instance methods; for class methods to a class where | |
5499 | we have only seen a @class declaration, | |
5500 | lookup_interface() above would have set rtype to | |
5501 | NULL_TREE. */ | |
5502 | if (rprotos) | |
5503 | { | |
5504 | /* We could not find an @interface declaration, yet, if | |
5505 | there are protocols attached to the type, we can | |
5506 | still look up the method in the protocols. Ie, we | |
5507 | are in the following case: | |
5508 | ||
5509 | @class MyClass; | |
5510 | MyClass<MyProtocol> *x; | |
5511 | [x method]; | |
5512 | ||
5513 | If 'MyProtocol' has the method 'method', we can check | |
5514 | and retrieve the method prototype. */ | |
5515 | method_prototype | |
5516 | = lookup_method_in_protocol_list (rprotos, sel_name, 0); | |
5517 | ||
5518 | /* At this point, if we have found the method_prototype, | |
5519 | we are quite happy. The details of the class are | |
5520 | irrelevant. If we haven't found it, a warning will | |
5521 | have been produced that the method could not be found | |
5522 | in the protocol, and we won't produce further | |
5523 | warnings (please note that this means that "@class | |
5524 | MyClass; MyClass <MyProtocol> *x;" is exactly | |
5525 | equivalent to "id <MyProtocol> x", which isn't too | |
5526 | satisfactory but it's not easy to see how to do | |
5527 | better). */ | |
5528 | } | |
5529 | else | |
5530 | { | |
5531 | if (rtype) | |
5532 | { | |
5533 | /* We could not find an @interface declaration, and | |
5534 | there are no protocols attached to the receiver, | |
5535 | so we can't complete the check that the receiver | |
5536 | responds to the method, and we can't retrieve the | |
5537 | method prototype. But, because the receiver has | |
5538 | a well-specified class, the programmer did want | |
5539 | this check to be performed. Emit a warning, then | |
5540 | keep going as if it was an 'id'. To remove the | |
5541 | warning, either include an @interface for the | |
5542 | class, or cast the receiver to 'id'. Note that | |
5543 | rtype is an IDENTIFIER_NODE at this point. */ | |
5544 | warning (0, "@interface of class %qE not found", rtype); | |
5545 | } | |
5546 | } | |
5547 | ||
267785bc | 5548 | rtype = NULL_TREE; |
267785bc | 5549 | } |
5550 | else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE | |
5551 | || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE) | |
5552 | { | |
a53aa046 | 5553 | /* We have a valid ObjC class name with an associated |
5554 | @interface. Look up the method name in the published | |
5555 | @interface for the class (and its superclasses). */ | |
267785bc | 5556 | method_prototype |
5557 | = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE); | |
5cada957 | 5558 | |
267785bc | 5559 | /* If the method was not found in the @interface, it may still |
5560 | exist locally as part of the @implementation. */ | |
5561 | if (!method_prototype && objc_implementation_context | |
5562 | && CLASS_NAME (objc_implementation_context) | |
5563 | == OBJC_TYPE_NAME (rtype)) | |
5564 | method_prototype | |
5565 | = lookup_method | |
5566 | ((class_tree | |
5567 | ? CLASS_CLS_METHODS (objc_implementation_context) | |
5568 | : CLASS_NST_METHODS (objc_implementation_context)), | |
5569 | sel_name); | |
5cada957 | 5570 | |
267785bc | 5571 | /* If we haven't found a candidate method by now, try looking for |
5572 | it in the protocol list. */ | |
5573 | if (!method_prototype && rprotos) | |
5574 | method_prototype | |
5575 | = lookup_method_in_protocol_list (rprotos, sel_name, | |
5576 | class_tree != NULL_TREE); | |
5577 | } | |
5578 | else | |
5579 | { | |
a53aa046 | 5580 | /* We have a type, but it's not an Objective-C type (!). */ |
267785bc | 5581 | warning (0, "invalid receiver type %qs", |
5582 | identifier_to_locale (gen_type_name (orig_rtype))); | |
5583 | /* After issuing the "invalid receiver" warning, perform method | |
5584 | lookup as if we were messaging 'id'. */ | |
5585 | rtype = rprotos = NULL_TREE; | |
5586 | } | |
5cada957 | 5587 | } |
a53aa046 | 5588 | /* Note that rtype could also be NULL_TREE. This happens if we are |
5589 | messaging a class by name, but the class was only | |
5590 | forward-declared using @class. */ | |
c17b85ea | 5591 | |
a53aa046 | 5592 | /* For 'id' or 'Class' receivers, search in the global hash table as |
5593 | a last resort. For all receivers, warn if protocol searches have | |
5594 | failed. */ | |
267785bc | 5595 | if (!method_prototype) |
5596 | { | |
5597 | if (rprotos) | |
5598 | warning (0, "%<%c%E%> not found in protocol(s)", | |
5599 | (class_tree ? '+' : '-'), | |
5600 | sel_name); | |
c17b85ea | 5601 | |
267785bc | 5602 | if (!rtype) |
5603 | method_prototype | |
5604 | = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE); | |
5605 | } | |
c17b85ea | 5606 | |
6bbb4715 | 5607 | if (!method_prototype) |
267785bc | 5608 | { |
5609 | static bool warn_missing_methods = false; | |
c17b85ea | 5610 | |
267785bc | 5611 | if (rtype) |
5612 | warning (0, "%qE may not respond to %<%c%E%>", | |
5613 | OBJC_TYPE_NAME (rtype), | |
5614 | (class_tree ? '+' : '-'), | |
5615 | sel_name); | |
5616 | /* If we are messaging an 'id' or 'Class' object and made it here, | |
5617 | then we have failed to find _any_ instance or class method, | |
5618 | respectively. */ | |
5619 | else | |
5620 | warning (0, "no %<%c%E%> method found", | |
5621 | (class_tree ? '+' : '-'), | |
5622 | sel_name); | |
0ced0389 | 5623 | |
267785bc | 5624 | if (!warn_missing_methods) |
5625 | { | |
6bbb4715 | 5626 | warning_at (input_location, |
267785bc | 5627 | 0, "(Messages without a matching method signature"); |
6bbb4715 | 5628 | warning_at (input_location, |
267785bc | 5629 | 0, "will be assumed to return %<id%> and accept"); |
6bbb4715 | 5630 | warning_at (input_location, |
267785bc | 5631 | 0, "%<...%> as arguments.)"); |
5632 | warn_missing_methods = true; | |
5633 | } | |
5634 | } | |
5635 | else | |
5636 | { | |
5637 | /* Warn if the method is deprecated, but not if the receiver is | |
5638 | a generic 'id'. 'id' is used to cast an object to a generic | |
5639 | object of an unspecified class; in that case, we'll use | |
5640 | whatever method prototype we can find to get the method | |
5641 | argument and return types, but it is not appropriate to | |
5642 | produce deprecation warnings since we don't know the class | |
5643 | that the object will be of at runtime. The @interface(s) for | |
5644 | that class may not even be available to the compiler right | |
5645 | now, and it is perfectly possible that the method is marked | |
5646 | as non-deprecated in such @interface(s). | |
c450c529 | 5647 | |
267785bc | 5648 | In practice this makes sense since casting an object to 'id' |
5649 | is often used precisely to turn off warnings associated with | |
5650 | the object being of a particular class. */ | |
5651 | if (TREE_DEPRECATED (method_prototype) && rtype != NULL_TREE) | |
5652 | { | |
5653 | if (deprecated_method_prototype) | |
5654 | *deprecated_method_prototype = method_prototype; | |
5655 | else | |
5656 | warn_deprecated_use (method_prototype, NULL_TREE); | |
5657 | } | |
5658 | } | |
e24e8df7 | 5659 | |
267785bc | 5660 | /* Save the selector name for printing error messages. */ |
5661 | current_objc_message_selector = sel_name; | |
e24e8df7 | 5662 | |
267785bc | 5663 | /* Build the method call. |
6bbb4715 | 5664 | TODO: Get the location from somewhere that will work for delayed |
267785bc | 5665 | expansion. */ |
6bbb4715 | 5666 | |
267785bc | 5667 | retval = (*runtime.build_objc_method_call) (input_location, method_prototype, |
5668 | receiver, rtype, sel_name, | |
5669 | method_params, super); | |
e24e8df7 | 5670 | |
267785bc | 5671 | current_objc_message_selector = 0; |
e24e8df7 | 5672 | |
267785bc | 5673 | return retval; |
5674 | } | |
5675 | \f | |
c450c529 | 5676 | |
6bbb4715 | 5677 | /* This routine creates a static variable used to implement @protocol(MyProtocol) |
267785bc | 5678 | expression. This variable will be initialized to global protocol_t meta-data |
5679 | pointer. */ | |
c450c529 | 5680 | |
267785bc | 5681 | /* This function is called by the parser when (and only when) a |
5682 | @protocol() expression is found, in order to compile it. */ | |
5683 | tree | |
5684 | objc_build_protocol_expr (tree protoname) | |
5685 | { | |
5686 | tree p = lookup_protocol (protoname, /* warn if deprecated */ true, | |
5687 | /* definition_required */ false); | |
c1cfb0f5 | 5688 | |
267785bc | 5689 | if (!p) |
5690 | { | |
5691 | error ("cannot find protocol declaration for %qE", protoname); | |
5692 | return error_mark_node; | |
c450c529 | 5693 | } |
c450c529 | 5694 | |
267785bc | 5695 | return (*runtime.get_protocol_reference) (input_location, p); |
5696 | } | |
c450c529 | 5697 | |
267785bc | 5698 | /* This function is called by the parser when a @selector() expression |
5699 | is found, in order to compile it. It is only called by the parser | |
5700 | and only to compile a @selector(). LOC is the location of the | |
5701 | @selector. */ | |
5702 | tree | |
5703 | objc_build_selector_expr (location_t loc, tree selnamelist) | |
c450c529 | 5704 | { |
267785bc | 5705 | tree selname; |
c450c529 | 5706 | |
267785bc | 5707 | /* Obtain the full selector name. */ |
5708 | switch (TREE_CODE (selnamelist)) | |
5709 | { | |
5710 | case IDENTIFIER_NODE: | |
5711 | /* A unary selector. */ | |
5712 | selname = selnamelist; | |
5713 | break; | |
5714 | case TREE_LIST: | |
5715 | selname = build_keyword_selector (selnamelist); | |
5716 | break; | |
5717 | default: | |
5718 | gcc_unreachable (); | |
5719 | } | |
c450c529 | 5720 | |
267785bc | 5721 | /* If we are required to check @selector() expressions as they |
5722 | are found, check that the selector has been declared. */ | |
5723 | if (warn_undeclared_selector) | |
5724 | { | |
5725 | /* Look the selector up in the list of all known class and | |
5726 | instance methods (up to this line) to check that the selector | |
5727 | exists. */ | |
f41791cf | 5728 | tree method; |
c450c529 | 5729 | |
267785bc | 5730 | /* First try with instance methods. */ |
f41791cf | 5731 | method = objc_map_get (instance_method_map, selname); |
c450c529 | 5732 | |
267785bc | 5733 | /* If not found, try with class methods. */ |
f41791cf | 5734 | if (method == OBJC_MAP_NOT_FOUND) |
267785bc | 5735 | { |
f41791cf | 5736 | method = objc_map_get (class_method_map, selname); |
c450c529 | 5737 | |
f41791cf | 5738 | /* If still not found, print out a warning. */ |
5739 | if (method == OBJC_MAP_NOT_FOUND) | |
5740 | warning (0, "undeclared selector %qE", selname); | |
267785bc | 5741 | } |
5742 | } | |
c450c529 | 5743 | |
267785bc | 5744 | /* The runtimes do this differently, most particularly, GNU has typed |
5745 | selectors, whilst NeXT does not. */ | |
5746 | return (*runtime.build_selector_reference) (loc, selname, NULL_TREE); | |
c450c529 | 5747 | } |
5748 | ||
267785bc | 5749 | static tree |
5750 | build_ivar_reference (tree id) | |
5751 | { | |
5752 | tree base; | |
5753 | if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL) | |
5754 | { | |
5755 | /* Historically, a class method that produced objects (factory | |
5756 | method) would assign `self' to the instance that it | |
5757 | allocated. This would effectively turn the class method into | |
5758 | an instance method. Following this assignment, the instance | |
5759 | variables could be accessed. That practice, while safe, | |
5760 | violates the simple rule that a class method should not refer | |
5761 | to an instance variable. It's better to catch the cases | |
5762 | where this is done unknowingly than to support the above | |
5763 | paradigm. */ | |
5764 | warning (0, "instance variable %qE accessed in class method", | |
5765 | id); | |
5766 | self_decl = convert (objc_instance_type, self_decl); /* cast */ | |
c450c529 | 5767 | } |
c450c529 | 5768 | |
267785bc | 5769 | base = build_indirect_ref (input_location, self_decl, RO_ARROW); |
5770 | return (*runtime.build_ivar_reference) (input_location, base, id); | |
c450c529 | 5771 | } |
5772 | ||
267785bc | 5773 | static void |
5774 | hash_init (void) | |
5775 | { | |
f41791cf | 5776 | instance_method_map = objc_map_alloc_ggc (1000); |
5777 | class_method_map = objc_map_alloc_ggc (1000); | |
c450c529 | 5778 | |
f41791cf | 5779 | class_name_map = objc_map_alloc_ggc (200); |
5780 | alias_name_map = objc_map_alloc_ggc (200); | |
c450c529 | 5781 | |
267785bc | 5782 | /* Initialize the hash table used to hold the constant string objects. */ |
2ef51f0e | 5783 | string_htab = hash_table<objc_string_hasher>::create_ggc (31); |
c450c529 | 5784 | } |
5785 | ||
f41791cf | 5786 | /* Use the following to add a method to class_method_map or |
5787 | instance_method_map. It will add the method, keyed by the | |
5788 | METHOD_SEL_NAME. If the method already exists, but with one or | |
5789 | more different prototypes, it will store a TREE_VEC in the map, | |
5790 | with the method prototypes in the vector. */ | |
267785bc | 5791 | static void |
f41791cf | 5792 | insert_method_into_method_map (bool class_method, tree method) |
267785bc | 5793 | { |
f41791cf | 5794 | tree method_name = METHOD_SEL_NAME (method); |
5795 | tree existing_entry; | |
5796 | objc_map_t map; | |
c450c529 | 5797 | |
f41791cf | 5798 | if (class_method) |
5799 | map = class_method_map; | |
267785bc | 5800 | else |
f41791cf | 5801 | map = instance_method_map; |
c450c529 | 5802 | |
f41791cf | 5803 | /* Check if the method already exists in the map. */ |
5804 | existing_entry = objc_map_get (map, method_name); | |
267785bc | 5805 | |
f41791cf | 5806 | /* If not, we simply add it to the map. */ |
5807 | if (existing_entry == OBJC_MAP_NOT_FOUND) | |
5808 | objc_map_put (map, method_name, method); | |
5809 | else | |
267785bc | 5810 | { |
f41791cf | 5811 | tree new_entry; |
5812 | ||
5813 | /* If an entry already exists, it's more complicated. We'll | |
5814 | have to check whether the method prototype is the same or | |
5815 | not. */ | |
5816 | if (TREE_CODE (existing_entry) != TREE_VEC) | |
5817 | { | |
5818 | /* If the method prototypes are the same, there is nothing | |
5819 | to do. */ | |
5820 | if (comp_proto_with_proto (method, existing_entry, 1)) | |
5821 | return; | |
267785bc | 5822 | |
f41791cf | 5823 | /* If not, create a vector to store both the method already |
5824 | in the map, and the new one that we are adding. */ | |
5825 | new_entry = make_tree_vec (2); | |
5826 | ||
5827 | TREE_VEC_ELT (new_entry, 0) = existing_entry; | |
5828 | TREE_VEC_ELT (new_entry, 1) = method; | |
5829 | } | |
5830 | else | |
5831 | { | |
5832 | /* An entry already exists, and it's already a vector. This | |
5833 | means that at least 2 different method prototypes were | |
5834 | already found, and we're considering registering yet | |
5835 | another one. */ | |
5836 | size_t i; | |
5837 | ||
5838 | /* Check all the existing prototypes. If any matches the | |
5839 | one we need to add, there is nothing to do because it's | |
5840 | already there. */ | |
d69c64b7 | 5841 | for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++) |
f41791cf | 5842 | if (comp_proto_with_proto (method, TREE_VEC_ELT (existing_entry, i), 1)) |
5843 | return; | |
267785bc | 5844 | |
f41791cf | 5845 | /* Else, create a new, bigger vector and add the new method |
5846 | at the end of it. This is inefficient but extremely | |
5847 | rare; in any sane program most methods have a single | |
5848 | prototype, and very few, if any, will have more than | |
5849 | 2! */ | |
5850 | new_entry = make_tree_vec (TREE_VEC_LENGTH (existing_entry) + 1); | |
5851 | ||
5852 | /* Copy the methods from the existing vector. */ | |
d69c64b7 | 5853 | for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++) |
f41791cf | 5854 | TREE_VEC_ELT (new_entry, i) = TREE_VEC_ELT (existing_entry, i); |
5855 | ||
5856 | /* Add the new method at the end. */ | |
5857 | TREE_VEC_ELT (new_entry, i) = method; | |
5858 | } | |
c450c529 | 5859 | |
f41791cf | 5860 | /* Store the new vector in the map. */ |
5861 | objc_map_put (map, method_name, new_entry); | |
c450c529 | 5862 | } |
267785bc | 5863 | } |
c450c529 | 5864 | |
267785bc | 5865 | \f |
5866 | static tree | |
5867 | lookup_method (tree mchain, tree method) | |
5868 | { | |
5869 | tree key; | |
c450c529 | 5870 | |
267785bc | 5871 | if (TREE_CODE (method) == IDENTIFIER_NODE) |
5872 | key = method; | |
5873 | else | |
5874 | key = METHOD_SEL_NAME (method); | |
c450c529 | 5875 | |
267785bc | 5876 | while (mchain) |
5877 | { | |
5878 | if (METHOD_SEL_NAME (mchain) == key) | |
5879 | return mchain; | |
39b8ddc6 | 5880 | |
267785bc | 5881 | mchain = DECL_CHAIN (mchain); |
c450c529 | 5882 | } |
267785bc | 5883 | return NULL_TREE; |
c450c529 | 5884 | } |
5885 | ||
267785bc | 5886 | /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance |
5887 | method in INTERFACE, along with any categories and protocols | |
5888 | attached thereto. If method is not found, and the | |
5889 | OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS, recursively examine the | |
5890 | INTERFACE's superclass. If OBJC_LOOKUP_CLASS is set, | |
5891 | OBJC_LOOKUP_NO_SUPER is clear, and no suitable class method could | |
5892 | be found in INTERFACE or any of its superclasses, look for an | |
5893 | _instance_ method of the same name in the root class as a last | |
5894 | resort. This behaviour can be turned off by using | |
5895 | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS. | |
c450c529 | 5896 | |
267785bc | 5897 | If a suitable method cannot be found, return NULL_TREE. */ |
c450c529 | 5898 | |
267785bc | 5899 | static tree |
5900 | lookup_method_static (tree interface, tree ident, int flags) | |
5901 | { | |
5902 | tree meth = NULL_TREE, root_inter = NULL_TREE; | |
5903 | tree inter = interface; | |
5904 | int is_class = (flags & OBJC_LOOKUP_CLASS); | |
5905 | int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER); | |
5906 | int no_instance_methods_of_root_class = (flags & OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS); | |
c450c529 | 5907 | |
267785bc | 5908 | while (inter) |
5909 | { | |
5910 | tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter); | |
5911 | tree category = inter; | |
c450c529 | 5912 | |
267785bc | 5913 | /* First, look up the method in the class itself. */ |
5914 | if ((meth = lookup_method (chain, ident))) | |
5915 | return meth; | |
c450c529 | 5916 | |
267785bc | 5917 | /* Failing that, look for the method in each category of the class. */ |
5918 | while ((category = CLASS_CATEGORY_LIST (category))) | |
5919 | { | |
5920 | chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category); | |
c450c529 | 5921 | |
267785bc | 5922 | /* Check directly in each category. */ |
5923 | if ((meth = lookup_method (chain, ident))) | |
5924 | return meth; | |
c450c529 | 5925 | |
267785bc | 5926 | /* Failing that, check in each category's protocols. */ |
5927 | if (CLASS_PROTOCOL_LIST (category)) | |
5928 | { | |
5929 | if ((meth = (lookup_method_in_protocol_list | |
5930 | (CLASS_PROTOCOL_LIST (category), ident, is_class)))) | |
5931 | return meth; | |
5932 | } | |
5933 | } | |
c450c529 | 5934 | |
267785bc | 5935 | /* If not found in categories, check in protocols of the main class. */ |
5936 | if (CLASS_PROTOCOL_LIST (inter)) | |
5937 | { | |
5938 | if ((meth = (lookup_method_in_protocol_list | |
5939 | (CLASS_PROTOCOL_LIST (inter), ident, is_class)))) | |
5940 | return meth; | |
5941 | } | |
c450c529 | 5942 | |
267785bc | 5943 | /* If we were instructed not to look in superclasses, don't. */ |
5944 | if (no_superclasses) | |
5945 | return NULL_TREE; | |
6a9006c3 | 5946 | |
267785bc | 5947 | /* Failing that, climb up the inheritance hierarchy. */ |
5948 | root_inter = inter; | |
5949 | inter = lookup_interface (CLASS_SUPER_NAME (inter)); | |
c450c529 | 5950 | } |
267785bc | 5951 | while (inter); |
c450c529 | 5952 | |
267785bc | 5953 | if (is_class && !no_instance_methods_of_root_class) |
c450c529 | 5954 | { |
267785bc | 5955 | /* If no class (factory) method was found, check if an _instance_ |
5956 | method of the same name exists in the root class. This is what | |
5957 | the Objective-C runtime will do. */ | |
5958 | return lookup_method_static (root_inter, ident, 0); | |
c450c529 | 5959 | } |
5960 | else | |
267785bc | 5961 | { |
6bbb4715 | 5962 | /* If an instance method was not found, return 0. */ |
267785bc | 5963 | return NULL_TREE; |
5964 | } | |
c450c529 | 5965 | } |
5966 | ||
267785bc | 5967 | static tree |
5968 | objc_add_method (tree klass, tree method, int is_class, bool is_optional) | |
0ced0389 | 5969 | { |
267785bc | 5970 | tree existing_method = NULL_TREE; |
0ced0389 | 5971 | |
267785bc | 5972 | /* The first thing we do is look up the method in the list of |
5973 | methods already defined in the interface (or implementation). */ | |
5974 | if (is_class) | |
5975 | existing_method = lookup_method (CLASS_CLS_METHODS (klass), method); | |
5976 | else | |
5977 | existing_method = lookup_method (CLASS_NST_METHODS (klass), method); | |
0ced0389 | 5978 | |
267785bc | 5979 | /* In the case of protocols, we have a second list of methods to |
5980 | consider, the list of optional ones. */ | |
5981 | if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE) | |
0ced0389 | 5982 | { |
267785bc | 5983 | /* @required methods are added to the protocol's normal list. |
5984 | @optional methods are added to the protocol's OPTIONAL lists. | |
5985 | Note that adding the methods to the optional lists disables | |
5986 | checking that the methods are implemented by classes | |
5987 | implementing the protocol, since these checks only use the | |
5988 | CLASS_CLS_METHODS and CLASS_NST_METHODS. */ | |
0ced0389 | 5989 | |
267785bc | 5990 | /* First of all, if the method to add is @optional, and we found |
5991 | it already existing as @required, emit an error. */ | |
5992 | if (is_optional && existing_method) | |
5993 | { | |
5994 | error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time", | |
5995 | (is_class ? '+' : '-'), | |
5996 | METHOD_SEL_NAME (existing_method)); | |
5997 | inform (DECL_SOURCE_LOCATION (existing_method), | |
5998 | "previous declaration of %<%c%E%> as %<@required%>", | |
5999 | (is_class ? '+' : '-'), | |
6000 | METHOD_SEL_NAME (existing_method)); | |
0ced0389 | 6001 | } |
0ced0389 | 6002 | |
267785bc | 6003 | /* Now check the list of @optional methods if we didn't find the |
6004 | method in the @required list. */ | |
6005 | if (!existing_method) | |
0ced0389 | 6006 | { |
267785bc | 6007 | if (is_class) |
6008 | existing_method = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (klass), method); | |
6009 | else | |
6010 | existing_method = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (klass), method); | |
6bbb4715 | 6011 | |
267785bc | 6012 | if (!is_optional && existing_method) |
0ced0389 | 6013 | { |
267785bc | 6014 | error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time", |
6015 | (is_class ? '+' : '-'), | |
6016 | METHOD_SEL_NAME (existing_method)); | |
6017 | inform (DECL_SOURCE_LOCATION (existing_method), | |
6018 | "previous declaration of %<%c%E%> as %<@optional%>", | |
6019 | (is_class ? '+' : '-'), | |
6020 | METHOD_SEL_NAME (existing_method)); | |
0ced0389 | 6021 | } |
6022 | } | |
6023 | } | |
6024 | ||
267785bc | 6025 | /* If the method didn't exist already, add it. */ |
6026 | if (!existing_method) | |
fc304191 | 6027 | { |
267785bc | 6028 | if (is_optional) |
c450c529 | 6029 | { |
267785bc | 6030 | if (is_class) |
6031 | { | |
6032 | /* Put the method on the list in reverse order. */ | |
6033 | TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass); | |
6034 | PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method; | |
6035 | } | |
6036 | else | |
146a801c | 6037 | { |
267785bc | 6038 | TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass); |
6039 | PROTOCOL_OPTIONAL_NST_METHODS (klass) = method; | |
146a801c | 6040 | } |
c450c529 | 6041 | } |
267785bc | 6042 | else |
c450c529 | 6043 | { |
267785bc | 6044 | if (is_class) |
6045 | { | |
6046 | DECL_CHAIN (method) = CLASS_CLS_METHODS (klass); | |
6047 | CLASS_CLS_METHODS (klass) = method; | |
6048 | } | |
6049 | else | |
146a801c | 6050 | { |
267785bc | 6051 | DECL_CHAIN (method) = CLASS_NST_METHODS (klass); |
6052 | CLASS_NST_METHODS (klass) = method; | |
146a801c | 6053 | } |
c450c529 | 6054 | } |
fc304191 | 6055 | } |
c450c529 | 6056 | else |
fc304191 | 6057 | { |
267785bc | 6058 | /* The method was already defined. Check that the types match |
6059 | for an @interface for a class or category, or for a | |
6060 | @protocol. Give hard errors on methods with identical | |
6061 | selectors but differing argument and/or return types. We do | |
6062 | not do this for @implementations, because C/C++ will do it | |
6063 | for us (i.e., there will be duplicate function definition | |
6064 | errors). */ | |
6065 | if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE | |
6066 | || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE | |
6067 | /* Starting with GCC 4.6, we emit the same error for | |
6068 | protocols too. The situation is identical to | |
6069 | @interfaces as there is no possible meaningful reason | |
6070 | for defining the same method with different signatures | |
6071 | in the very same @protocol. If that was allowed, | |
6072 | whenever the protocol is used (both at compile and run | |
6073 | time) there wouldn't be any meaningful way to decide | |
6074 | which of the two method signatures should be used. */ | |
6075 | || TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE) | |
6076 | && !comp_proto_with_proto (method, existing_method, 1)) | |
6077 | { | |
6078 | error ("duplicate declaration of method %<%c%E%> with conflicting types", | |
6079 | (is_class ? '+' : '-'), | |
6080 | METHOD_SEL_NAME (existing_method)); | |
6081 | inform (DECL_SOURCE_LOCATION (existing_method), | |
6082 | "previous declaration of %<%c%E%>", | |
6083 | (is_class ? '+' : '-'), | |
6084 | METHOD_SEL_NAME (existing_method)); | |
6085 | } | |
fc304191 | 6086 | } |
e0ece38e | 6087 | |
267785bc | 6088 | if (is_class) |
f41791cf | 6089 | insert_method_into_method_map (true, method); |
fc304191 | 6090 | else |
6091 | { | |
f41791cf | 6092 | insert_method_into_method_map (false, method); |
267785bc | 6093 | |
6094 | /* Instance methods in root classes (and categories thereof) | |
6095 | may act as class methods as a last resort. We also add | |
6096 | instance methods listed in @protocol declarations to | |
6097 | the class hash table, on the assumption that @protocols | |
6098 | may be adopted by root classes or categories. */ | |
6099 | if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE | |
6100 | || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE) | |
6101 | klass = lookup_interface (CLASS_NAME (klass)); | |
6102 | ||
6103 | if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE | |
6104 | || !CLASS_SUPER_NAME (klass)) | |
f41791cf | 6105 | insert_method_into_method_map (true, method); |
fc304191 | 6106 | } |
e0ece38e | 6107 | |
267785bc | 6108 | return method; |
fc304191 | 6109 | } |
6110 | ||
146a801c | 6111 | static void |
267785bc | 6112 | add_category (tree klass, tree category) |
146a801c | 6113 | { |
267785bc | 6114 | /* Put categories on list in reverse order. */ |
6115 | tree cat = lookup_category (klass, CLASS_SUPER_NAME (category)); | |
fc304191 | 6116 | |
267785bc | 6117 | if (cat) |
c450c529 | 6118 | { |
267785bc | 6119 | warning (0, "duplicate interface declaration for category %<%E(%E)%>", |
6120 | CLASS_NAME (klass), | |
6121 | CLASS_SUPER_NAME (category)); | |
c450c529 | 6122 | } |
6123 | else | |
6124 | { | |
267785bc | 6125 | CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass); |
6126 | CLASS_CATEGORY_LIST (klass) = category; | |
c17b85ea | 6127 | } |
fc304191 | 6128 | } |
6129 | ||
7840e94f | 6130 | #ifndef OBJCPLUS |
6131 | /* A flexible array member is a C99 extension where you can use | |
6132 | "type[]" at the end of a struct to mean a variable-length array. | |
6133 | ||
6134 | In Objective-C, instance variables are fundamentally members of a | |
6135 | struct, but the struct can always be extended by subclassing; hence | |
6136 | we need to detect and forbid all instance variables declared using | |
6137 | flexible array members. | |
6138 | ||
6139 | No check for this is needed in Objective-C++, since C++ does not | |
6140 | have flexible array members. */ | |
6141 | ||
6142 | /* Determine whether TYPE is a structure with a flexible array member, | |
6143 | a union containing such a structure (possibly recursively) or an | |
6144 | array of such structures or unions. These are all invalid as | |
6145 | instance variable. */ | |
6146 | static bool | |
6147 | flexible_array_type_p (tree type) | |
6148 | { | |
6149 | tree x; | |
6150 | switch (TREE_CODE (type)) | |
6151 | { | |
6152 | case RECORD_TYPE: | |
6153 | x = TYPE_FIELDS (type); | |
6154 | if (x == NULL_TREE) | |
6155 | return false; | |
6156 | while (DECL_CHAIN (x) != NULL_TREE) | |
6157 | x = DECL_CHAIN (x); | |
6158 | if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE | |
6159 | && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE | |
6160 | && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE | |
6161 | && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) | |
6162 | return true; | |
6163 | return false; | |
6164 | case UNION_TYPE: | |
6165 | for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x)) | |
6166 | { | |
6167 | if (flexible_array_type_p (TREE_TYPE (x))) | |
6168 | return true; | |
6169 | } | |
6170 | return false; | |
6171 | /* Note that we also check for arrays of something that uses a flexible array member. */ | |
6172 | case ARRAY_TYPE: | |
6173 | if (flexible_array_type_p (TREE_TYPE (type))) | |
6174 | return true; | |
6175 | return false; | |
6176 | default: | |
6177 | return false; | |
6178 | } | |
6179 | } | |
6180 | #endif | |
6181 | ||
cc79dae3 | 6182 | /* Produce a printable version of an ivar name. This is only used |
6183 | inside add_instance_variable. */ | |
6184 | static const char * | |
6185 | printable_ivar_name (tree field_decl) | |
6186 | { | |
6187 | if (DECL_NAME (field_decl)) | |
6188 | return identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl))); | |
6189 | else | |
6190 | return _("<unnamed>"); | |
6191 | } | |
6192 | ||
267785bc | 6193 | /* Called after parsing each instance variable declaration. Necessary to |
6194 | preserve typedefs and implement public/private... | |
fc304191 | 6195 | |
267785bc | 6196 | VISIBILITY is 1 for public, 0 for protected, and 2 for private. */ |
fc304191 | 6197 | |
267785bc | 6198 | static tree |
6bbb4715 | 6199 | add_instance_variable (tree klass, objc_ivar_visibility_kind visibility, |
267785bc | 6200 | tree field_decl) |
fc304191 | 6201 | { |
267785bc | 6202 | tree field_type = TREE_TYPE (field_decl); |
fc304191 | 6203 | |
267785bc | 6204 | #ifdef OBJCPLUS |
6205 | if (TREE_CODE (field_type) == REFERENCE_TYPE) | |
6206 | { | |
6207 | error ("illegal reference type specified for instance variable %qs", | |
cc79dae3 | 6208 | printable_ivar_name (field_decl)); |
267785bc | 6209 | /* Return class as is without adding this ivar. */ |
6210 | return klass; | |
6211 | } | |
6212 | #endif | |
fc304191 | 6213 | |
267785bc | 6214 | if (field_type == error_mark_node || !TYPE_SIZE (field_type) |
6215 | || TYPE_SIZE (field_type) == error_mark_node) | |
6216 | /* 'type[0]' is allowed, but 'type[]' is not! */ | |
fc304191 | 6217 | { |
cc79dae3 | 6218 | error ("instance variable %qs has unknown size", |
6219 | printable_ivar_name (field_decl)); | |
267785bc | 6220 | /* Return class as is without adding this ivar. */ |
6221 | return klass; | |
6222 | } | |
fc304191 | 6223 | |
7840e94f | 6224 | #ifndef OBJCPLUS |
6225 | /* Also, in C reject a struct with a flexible array member. Ie, | |
6226 | ||
6227 | struct A { int x; int[] y; }; | |
6228 | ||
6229 | @interface X | |
6230 | { | |
6231 | struct A instance_variable; | |
6232 | } | |
6233 | @end | |
6234 | ||
6235 | is not valid because if the class is subclassed, we wouldn't be able | |
6236 | to calculate the offset of the next instance variable. */ | |
6237 | if (flexible_array_type_p (field_type)) | |
6238 | { | |
cc79dae3 | 6239 | error ("instance variable %qs uses flexible array member", |
6240 | printable_ivar_name (field_decl)); | |
7840e94f | 6241 | /* Return class as is without adding this ivar. */ |
6bbb4715 | 6242 | return klass; |
7840e94f | 6243 | } |
6244 | #endif | |
6245 | ||
c17b85ea | 6246 | #ifdef OBJCPLUS |
267785bc | 6247 | /* Check if the ivar being added has a non-POD C++ type. If so, we will |
6248 | need to either (1) warn the user about it or (2) generate suitable | |
6249 | constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct' | |
6250 | methods (if '-fobjc-call-cxx-cdtors' was specified). */ | |
6251 | if (MAYBE_CLASS_TYPE_P (field_type) | |
6252 | && (TYPE_NEEDS_CONSTRUCTING (field_type) | |
6253 | || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type) | |
6254 | || TYPE_POLYMORPHIC_P (field_type))) | |
6255 | { | |
6256 | tree type_name = OBJC_TYPE_NAME (field_type); | |
e0ece38e | 6257 | |
267785bc | 6258 | if (flag_objc_call_cxx_cdtors) |
6259 | { | |
6260 | /* Since the ObjC runtime will be calling the constructors and | |
6261 | destructors for us, the only thing we can't handle is the lack | |
6262 | of a default constructor. */ | |
6263 | if (TYPE_NEEDS_CONSTRUCTING (field_type) | |
6264 | && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type)) | |
6265 | { | |
6266 | warning (0, "type %qE has no default constructor to call", | |
6267 | type_name); | |
fc304191 | 6268 | |
267785bc | 6269 | /* If we cannot call a constructor, we should also avoid |
6270 | calling the destructor, for symmetry. */ | |
6271 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)) | |
6272 | warning (0, "destructor for %qE shall not be run either", | |
6273 | type_name); | |
6274 | } | |
6275 | } | |
6276 | else | |
fc304191 | 6277 | { |
267785bc | 6278 | static bool warn_cxx_ivars = false; |
6279 | ||
6280 | if (TYPE_POLYMORPHIC_P (field_type)) | |
fc304191 | 6281 | { |
267785bc | 6282 | /* Vtable pointers are Real Bad(tm), since Obj-C cannot |
6283 | initialize them. */ | |
6284 | error ("type %qE has virtual member functions", type_name); | |
6285 | error ("illegal aggregate type %qE specified " | |
6286 | "for instance variable %qs", | |
cc79dae3 | 6287 | type_name, printable_ivar_name (field_decl)); |
267785bc | 6288 | /* Return class as is without adding this ivar. */ |
6289 | return klass; | |
fc304191 | 6290 | } |
267785bc | 6291 | |
6292 | /* User-defined constructors and destructors are not known to Obj-C | |
6293 | and hence will not be called. This may or may not be a problem. */ | |
6294 | if (TYPE_NEEDS_CONSTRUCTING (field_type)) | |
6295 | warning (0, "type %qE has a user-defined constructor", type_name); | |
6296 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)) | |
6297 | warning (0, "type %qE has a user-defined destructor", type_name); | |
6298 | ||
6299 | if (!warn_cxx_ivars) | |
fc304191 | 6300 | { |
267785bc | 6301 | warning (0, "C++ constructors and destructors will not " |
6302 | "be invoked for Objective-C fields"); | |
6303 | warn_cxx_ivars = true; | |
fc304191 | 6304 | } |
6305 | } | |
fc304191 | 6306 | } |
267785bc | 6307 | #endif |
fc304191 | 6308 | |
267785bc | 6309 | /* Overload the public attribute, it is not used for FIELD_DECLs. */ |
6310 | switch (visibility) | |
6311 | { | |
6312 | case OBJC_IVAR_VIS_PROTECTED: | |
6313 | TREE_PUBLIC (field_decl) = 0; | |
6314 | TREE_PRIVATE (field_decl) = 0; | |
6315 | TREE_PROTECTED (field_decl) = 1; | |
6316 | break; | |
fc304191 | 6317 | |
267785bc | 6318 | case OBJC_IVAR_VIS_PACKAGE: |
6319 | /* TODO: Implement the package variant. */ | |
6320 | case OBJC_IVAR_VIS_PUBLIC: | |
6321 | TREE_PUBLIC (field_decl) = 1; | |
6322 | TREE_PRIVATE (field_decl) = 0; | |
6323 | TREE_PROTECTED (field_decl) = 0; | |
6324 | break; | |
fc304191 | 6325 | |
267785bc | 6326 | case OBJC_IVAR_VIS_PRIVATE: |
6327 | TREE_PUBLIC (field_decl) = 0; | |
6328 | TREE_PRIVATE (field_decl) = 1; | |
6329 | TREE_PROTECTED (field_decl) = 0; | |
6330 | break; | |
fc304191 | 6331 | |
267785bc | 6332 | } |
fc304191 | 6333 | |
267785bc | 6334 | CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl); |
fc304191 | 6335 | |
267785bc | 6336 | return klass; |
fc304191 | 6337 | } |
6338 | ||
267785bc | 6339 | /* True if the ivar is private and we are not in its implementation. */ |
c450c529 | 6340 | |
267785bc | 6341 | static int |
6342 | is_private (tree decl) | |
fc304191 | 6343 | { |
267785bc | 6344 | return (TREE_PRIVATE (decl) |
6345 | && ! is_ivar (CLASS_IVARS (implementation_template), | |
6346 | DECL_NAME (decl))); | |
fc304191 | 6347 | } |
6348 | ||
cfc77d33 | 6349 | /* Searches all the instance variables of 'klass' and of its |
6350 | superclasses for an instance variable whose name (identifier) is | |
6351 | 'ivar_name_ident'. Return the declaration (DECL) of the instance | |
6352 | variable, if found, or NULL_TREE, if not found. */ | |
6353 | static inline tree | |
6354 | ivar_of_class (tree klass, tree ivar_name_ident) | |
6355 | { | |
6356 | /* First, look up the ivar in CLASS_RAW_IVARS. */ | |
6357 | tree decl_chain = CLASS_RAW_IVARS (klass); | |
6358 | ||
6359 | for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain)) | |
6360 | if (DECL_NAME (decl_chain) == ivar_name_ident) | |
6361 | return decl_chain; | |
6362 | ||
6363 | /* If not found, search up the class hierarchy. */ | |
6364 | while (CLASS_SUPER_NAME (klass)) | |
6365 | { | |
6366 | klass = lookup_interface (CLASS_SUPER_NAME (klass)); | |
6bbb4715 | 6367 | |
cfc77d33 | 6368 | decl_chain = CLASS_RAW_IVARS (klass); |
6bbb4715 | 6369 | |
cfc77d33 | 6370 | for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain)) |
6371 | if (DECL_NAME (decl_chain) == ivar_name_ident) | |
6372 | return decl_chain; | |
6373 | } | |
6374 | ||
6375 | return NULL_TREE; | |
6376 | } | |
6377 | ||
267785bc | 6378 | /* We have an instance variable reference;, check to see if it is public. */ |
c450c529 | 6379 | |
267785bc | 6380 | int |
6381 | objc_is_public (tree expr, tree identifier) | |
fc304191 | 6382 | { |
267785bc | 6383 | tree basetype, decl; |
fc304191 | 6384 | |
267785bc | 6385 | #ifdef OBJCPLUS |
6386 | if (processing_template_decl) | |
6387 | return 1; | |
6388 | #endif | |
fc304191 | 6389 | |
267785bc | 6390 | if (TREE_TYPE (expr) == error_mark_node) |
6391 | return 1; | |
fc304191 | 6392 | |
267785bc | 6393 | basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr)); |
fc304191 | 6394 | |
267785bc | 6395 | if (basetype && TREE_CODE (basetype) == RECORD_TYPE) |
6396 | { | |
6397 | if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype)) | |
6398 | { | |
6399 | tree klass = lookup_interface (OBJC_TYPE_NAME (basetype)); | |
fc304191 | 6400 | |
267785bc | 6401 | if (!klass) |
6402 | { | |
6403 | error ("cannot find interface declaration for %qE", | |
6404 | OBJC_TYPE_NAME (basetype)); | |
6405 | return 0; | |
6406 | } | |
fc304191 | 6407 | |
cfc77d33 | 6408 | if ((decl = ivar_of_class (klass, identifier))) |
267785bc | 6409 | { |
6410 | if (TREE_PUBLIC (decl)) | |
6411 | return 1; | |
fc304191 | 6412 | |
267785bc | 6413 | /* Important difference between the Stepstone translator: |
6414 | all instance variables should be public within the context | |
6415 | of the implementation. */ | |
6416 | if (objc_implementation_context | |
6417 | && ((TREE_CODE (objc_implementation_context) | |
6418 | == CLASS_IMPLEMENTATION_TYPE) | |
6419 | || (TREE_CODE (objc_implementation_context) | |
6420 | == CATEGORY_IMPLEMENTATION_TYPE))) | |
6421 | { | |
6422 | tree curtype = TYPE_MAIN_VARIANT | |
6423 | (CLASS_STATIC_TEMPLATE | |
6424 | (implementation_template)); | |
fc304191 | 6425 | |
267785bc | 6426 | if (basetype == curtype |
6427 | || DERIVED_FROM_P (basetype, curtype)) | |
6428 | { | |
6429 | int priv = is_private (decl); | |
fc304191 | 6430 | |
267785bc | 6431 | if (priv) |
6432 | error ("instance variable %qE is declared private", | |
6433 | DECL_NAME (decl)); | |
fc304191 | 6434 | |
267785bc | 6435 | return !priv; |
6436 | } | |
6437 | } | |
fc304191 | 6438 | |
267785bc | 6439 | /* The 2.95.2 compiler sometimes allowed C functions to access |
6440 | non-@public ivars. We will let this slide for now... */ | |
6441 | if (!objc_method_context) | |
6442 | { | |
6443 | warning (0, "instance variable %qE is %s; " | |
6444 | "this will be a hard error in the future", | |
6445 | identifier, | |
6446 | TREE_PRIVATE (decl) ? "@private" : "@protected"); | |
6447 | return 1; | |
6448 | } | |
fc304191 | 6449 | |
267785bc | 6450 | error ("instance variable %qE is declared %s", |
6451 | identifier, | |
6452 | TREE_PRIVATE (decl) ? "private" : "protected"); | |
6453 | return 0; | |
6454 | } | |
6455 | } | |
6456 | } | |
fc304191 | 6457 | |
267785bc | 6458 | return 1; |
fc304191 | 6459 | } |
267785bc | 6460 | \f |
6461 | /* Make sure all methods in CHAIN (a list of method declarations from | |
6462 | an @interface or a @protocol) are in IMPLEMENTATION (the | |
6463 | implementation context). This is used to check for example that | |
6464 | all methods declared in an @interface were implemented in an | |
6465 | @implementation. | |
fc304191 | 6466 | |
267785bc | 6467 | Some special methods (property setters/getters) are special and if |
6468 | they are not found in IMPLEMENTATION, we look them up in its | |
6469 | superclasses. */ | |
df6caf6c | 6470 | |
c17b85ea | 6471 | static int |
267785bc | 6472 | check_methods (tree chain, tree implementation, int mtype) |
fc304191 | 6473 | { |
267785bc | 6474 | int first = 1; |
6475 | tree list; | |
fc304191 | 6476 | |
267785bc | 6477 | if (mtype == (int)'+') |
6478 | list = CLASS_CLS_METHODS (implementation); | |
fc304191 | 6479 | else |
267785bc | 6480 | list = CLASS_NST_METHODS (implementation); |
fc304191 | 6481 | |
267785bc | 6482 | while (chain) |
fc304191 | 6483 | { |
267785bc | 6484 | /* If the method is associated with a dynamic property, then it |
6485 | is Ok not to have the method implementation, as it will be | |
6486 | generated dynamically at runtime. To decide if the method is | |
6487 | associated with a @dynamic property, we search the list of | |
6488 | @synthesize and @dynamic for this implementation, and look | |
6489 | for any @dynamic property with the same setter or getter name | |
6490 | as this method. */ | |
6491 | tree x; | |
6492 | for (x = IMPL_PROPERTY_DECL (implementation); x; x = TREE_CHAIN (x)) | |
6493 | if (PROPERTY_DYNAMIC (x) | |
6494 | && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain) | |
6495 | || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain))) | |
6496 | break; | |
6bbb4715 | 6497 | |
267785bc | 6498 | if (x != NULL_TREE) |
6499 | { | |
6500 | chain = TREE_CHAIN (chain); /* next method... */ | |
6501 | continue; | |
6502 | } | |
fc304191 | 6503 | |
267785bc | 6504 | if (!lookup_method (list, chain)) |
6505 | { | |
6506 | /* If the method is a property setter/getter, we'll still | |
6507 | allow it to be missing if it is implemented by | |
6508 | 'interface' or any of its superclasses. */ | |
6509 | tree property = METHOD_PROPERTY_CONTEXT (chain); | |
6510 | if (property) | |
6511 | { | |
6512 | /* Note that since this is a property getter/setter, it | |
6513 | is obviously an instance method. */ | |
6514 | tree interface = NULL_TREE; | |
fc304191 | 6515 | |
267785bc | 6516 | /* For a category, first check the main class |
6517 | @interface. */ | |
6518 | if (TREE_CODE (implementation) == CATEGORY_IMPLEMENTATION_TYPE) | |
6519 | { | |
6520 | interface = lookup_interface (CLASS_NAME (implementation)); | |
ad8b9c20 | 6521 | |
267785bc | 6522 | /* If the method is found in the main class, it's Ok. */ |
6523 | if (lookup_method (CLASS_NST_METHODS (interface), chain)) | |
6524 | { | |
6525 | chain = DECL_CHAIN (chain); | |
6bbb4715 | 6526 | continue; |
267785bc | 6527 | } |
fc304191 | 6528 | |
267785bc | 6529 | /* Else, get the superclass. */ |
6530 | if (CLASS_SUPER_NAME (interface)) | |
6531 | interface = lookup_interface (CLASS_SUPER_NAME (interface)); | |
6532 | else | |
6533 | interface = NULL_TREE; | |
6534 | } | |
33ad0ed5 | 6535 | |
267785bc | 6536 | /* Get the superclass for classes. */ |
6537 | if (TREE_CODE (implementation) == CLASS_IMPLEMENTATION_TYPE) | |
6538 | { | |
6539 | if (CLASS_SUPER_NAME (implementation)) | |
6540 | interface = lookup_interface (CLASS_SUPER_NAME (implementation)); | |
6541 | else | |
6542 | interface = NULL_TREE; | |
6543 | } | |
fc304191 | 6544 | |
267785bc | 6545 | /* Now, interface is the superclass, if any; go check it. */ |
6546 | if (interface) | |
6547 | { | |
6548 | if (lookup_method_static (interface, chain, 0)) | |
6549 | { | |
6550 | chain = DECL_CHAIN (chain); | |
6551 | continue; | |
6552 | } | |
6553 | } | |
6554 | /* Else, fall through - warn. */ | |
6555 | } | |
6556 | if (first) | |
6557 | { | |
6558 | switch (TREE_CODE (implementation)) | |
6559 | { | |
6560 | case CLASS_IMPLEMENTATION_TYPE: | |
6561 | warning (0, "incomplete implementation of class %qE", | |
6562 | CLASS_NAME (implementation)); | |
6563 | break; | |
6564 | case CATEGORY_IMPLEMENTATION_TYPE: | |
6565 | warning (0, "incomplete implementation of category %qE", | |
6566 | CLASS_SUPER_NAME (implementation)); | |
6567 | break; | |
6568 | default: | |
6569 | gcc_unreachable (); | |
6570 | } | |
6571 | first = 0; | |
6572 | } | |
ad8b9c20 | 6573 | |
267785bc | 6574 | warning (0, "method definition for %<%c%E%> not found", |
6575 | mtype, METHOD_SEL_NAME (chain)); | |
6576 | } | |
fc304191 | 6577 | |
267785bc | 6578 | chain = DECL_CHAIN (chain); |
fc304191 | 6579 | } |
fc304191 | 6580 | |
267785bc | 6581 | return first; |
fc304191 | 6582 | } |
6583 | ||
267785bc | 6584 | /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */ |
c450c529 | 6585 | |
267785bc | 6586 | static int |
6587 | conforms_to_protocol (tree klass, tree protocol) | |
fc304191 | 6588 | { |
267785bc | 6589 | if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE) |
6590 | { | |
6591 | tree p = CLASS_PROTOCOL_LIST (klass); | |
6592 | while (p && TREE_VALUE (p) != protocol) | |
6593 | p = TREE_CHAIN (p); | |
fc304191 | 6594 | |
267785bc | 6595 | if (!p) |
6596 | { | |
6597 | tree super = (CLASS_SUPER_NAME (klass) | |
6598 | ? lookup_interface (CLASS_SUPER_NAME (klass)) | |
6599 | : NULL_TREE); | |
6600 | int tmp = super ? conforms_to_protocol (super, protocol) : 0; | |
6601 | if (!tmp) | |
6602 | return 0; | |
6603 | } | |
6604 | } | |
fc304191 | 6605 | |
267785bc | 6606 | return 1; |
fc304191 | 6607 | } |
6608 | ||
267785bc | 6609 | /* Make sure all methods in CHAIN are accessible as MTYPE methods in |
6610 | CONTEXT. This is one of two mechanisms to check protocol integrity. */ | |
fc304191 | 6611 | |
267785bc | 6612 | static int |
6613 | check_methods_accessible (tree chain, tree context, int mtype) | |
376c21d1 | 6614 | { |
267785bc | 6615 | int first = 1; |
6616 | tree list; | |
6617 | tree base_context = context; | |
376c21d1 | 6618 | |
267785bc | 6619 | while (chain) |
376c21d1 | 6620 | { |
267785bc | 6621 | /* If the method is associated with a dynamic property, then it |
6622 | is Ok not to have the method implementation, as it will be | |
6623 | generated dynamically at runtime. Search for any @dynamic | |
6624 | property with the same setter or getter name as this | |
6625 | method. TODO: Use a hashtable lookup. */ | |
6626 | tree x; | |
6627 | for (x = IMPL_PROPERTY_DECL (base_context); x; x = TREE_CHAIN (x)) | |
6628 | if (PROPERTY_DYNAMIC (x) | |
6629 | && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain) | |
6630 | || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain))) | |
6631 | break; | |
6bbb4715 | 6632 | |
267785bc | 6633 | if (x != NULL_TREE) |
376c21d1 | 6634 | { |
267785bc | 6635 | chain = TREE_CHAIN (chain); /* next method... */ |
6636 | continue; | |
6bbb4715 | 6637 | } |
2c0b522d | 6638 | |
267785bc | 6639 | context = base_context; |
6640 | while (context) | |
376c21d1 | 6641 | { |
267785bc | 6642 | if (mtype == '+') |
6643 | list = CLASS_CLS_METHODS (context); | |
6644 | else | |
6645 | list = CLASS_NST_METHODS (context); | |
c450c529 | 6646 | |
267785bc | 6647 | if (lookup_method (list, chain)) |
6648 | break; | |
fc304191 | 6649 | |
267785bc | 6650 | switch (TREE_CODE (context)) |
6651 | { | |
6652 | case CLASS_IMPLEMENTATION_TYPE: | |
6653 | case CLASS_INTERFACE_TYPE: | |
6654 | context = (CLASS_SUPER_NAME (context) | |
6655 | ? lookup_interface (CLASS_SUPER_NAME (context)) | |
6656 | : NULL_TREE); | |
6657 | break; | |
6658 | case CATEGORY_IMPLEMENTATION_TYPE: | |
6659 | case CATEGORY_INTERFACE_TYPE: | |
6660 | context = (CLASS_NAME (context) | |
6661 | ? lookup_interface (CLASS_NAME (context)) | |
6662 | : NULL_TREE); | |
6663 | break; | |
6664 | default: | |
6665 | gcc_unreachable (); | |
6666 | } | |
6667 | } | |
ad8b9c20 | 6668 | |
267785bc | 6669 | if (context == NULL_TREE) |
6670 | { | |
6671 | if (first) | |
6672 | { | |
6673 | switch (TREE_CODE (objc_implementation_context)) | |
6674 | { | |
6675 | case CLASS_IMPLEMENTATION_TYPE: | |
6676 | warning (0, "incomplete implementation of class %qE", | |
6677 | CLASS_NAME (objc_implementation_context)); | |
6678 | break; | |
6679 | case CATEGORY_IMPLEMENTATION_TYPE: | |
6680 | warning (0, "incomplete implementation of category %qE", | |
6681 | CLASS_SUPER_NAME (objc_implementation_context)); | |
6682 | break; | |
6683 | default: | |
6684 | gcc_unreachable (); | |
6685 | } | |
6686 | first = 0; | |
6687 | } | |
6688 | warning (0, "method definition for %<%c%E%> not found", | |
6689 | mtype, METHOD_SEL_NAME (chain)); | |
6690 | } | |
fc304191 | 6691 | |
267785bc | 6692 | chain = TREE_CHAIN (chain); /* next method... */ |
fc304191 | 6693 | } |
267785bc | 6694 | return first; |
c450c529 | 6695 | } |
6696 | ||
267785bc | 6697 | /* Check whether the current interface (accessible via |
6698 | 'objc_implementation_context') actually implements protocol P, along | |
6699 | with any protocols that P inherits. */ | |
c450c529 | 6700 | |
267785bc | 6701 | static void |
6702 | check_protocol (tree p, const char *type, tree name) | |
6703 | { | |
6704 | if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE) | |
29d8eac9 | 6705 | { |
267785bc | 6706 | int f1, f2; |
c450c529 | 6707 | |
267785bc | 6708 | /* Ensure that all protocols have bodies! */ |
6709 | if (warn_protocol) | |
6710 | { | |
6711 | f1 = check_methods (PROTOCOL_CLS_METHODS (p), | |
6712 | objc_implementation_context, | |
6713 | '+'); | |
6714 | f2 = check_methods (PROTOCOL_NST_METHODS (p), | |
6715 | objc_implementation_context, | |
6716 | '-'); | |
6717 | } | |
6718 | else | |
6719 | { | |
6720 | f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p), | |
6721 | objc_implementation_context, | |
6722 | '+'); | |
6723 | f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p), | |
6724 | objc_implementation_context, | |
6725 | '-'); | |
6726 | } | |
c450c529 | 6727 | |
267785bc | 6728 | if (!f1 || !f2) |
6729 | warning (0, "%s %qE does not fully implement the %qE protocol", | |
6730 | type, name, PROTOCOL_NAME (p)); | |
6731 | } | |
c450c529 | 6732 | |
267785bc | 6733 | /* Check protocols recursively. */ |
6734 | if (PROTOCOL_LIST (p)) | |
c450c529 | 6735 | { |
267785bc | 6736 | tree subs = PROTOCOL_LIST (p); |
6737 | tree super_class = | |
6738 | lookup_interface (CLASS_SUPER_NAME (implementation_template)); | |
c450c529 | 6739 | |
267785bc | 6740 | while (subs) |
c450c529 | 6741 | { |
267785bc | 6742 | tree sub = TREE_VALUE (subs); |
6743 | ||
6744 | /* If the superclass does not conform to the protocols | |
6745 | inherited by P, then we must! */ | |
6746 | if (!super_class || !conforms_to_protocol (super_class, sub)) | |
6747 | check_protocol (sub, type, name); | |
6748 | subs = TREE_CHAIN (subs); | |
c450c529 | 6749 | } |
6750 | } | |
267785bc | 6751 | } |
c450c529 | 6752 | |
267785bc | 6753 | /* Check whether the current interface (accessible via |
6754 | 'objc_implementation_context') actually implements the protocols listed | |
6755 | in PROTO_LIST. */ | |
c450c529 | 6756 | |
267785bc | 6757 | static void |
6758 | check_protocols (tree proto_list, const char *type, tree name) | |
6759 | { | |
6760 | for ( ; proto_list; proto_list = TREE_CHAIN (proto_list)) | |
29d8eac9 | 6761 | { |
267785bc | 6762 | tree p = TREE_VALUE (proto_list); |
c450c529 | 6763 | |
267785bc | 6764 | check_protocol (p, type, name); |
6765 | } | |
fc304191 | 6766 | } |
267785bc | 6767 | \f |
6768 | /* Make sure that the class CLASS_NAME is defined CODE says which kind | |
6769 | of thing CLASS_NAME ought to be. It can be CLASS_INTERFACE_TYPE, | |
6770 | CLASS_IMPLEMENTATION_TYPE, CATEGORY_INTERFACE_TYPE, or | |
6771 | CATEGORY_IMPLEMENTATION_TYPE. For a CATEGORY_INTERFACE_TYPE, | |
6772 | SUPER_NAME is the name of the category. For a class extension, | |
6773 | CODE is CATEGORY_INTERFACE_TYPE and SUPER_NAME is NULL_TREE. */ | |
fc304191 | 6774 | static tree |
267785bc | 6775 | start_class (enum tree_code code, tree class_name, tree super_name, |
6776 | tree protocol_list, tree attributes) | |
fc304191 | 6777 | { |
267785bc | 6778 | tree klass = NULL_TREE; |
6779 | tree decl; | |
fc304191 | 6780 | |
267785bc | 6781 | #ifdef OBJCPLUS |
6782 | if (current_namespace != global_namespace) | |
fc304191 | 6783 | { |
267785bc | 6784 | error ("Objective-C declarations may only appear in global scope"); |
fc304191 | 6785 | } |
267785bc | 6786 | #endif /* OBJCPLUS */ |
6787 | ||
6788 | if (objc_implementation_context) | |
fc304191 | 6789 | { |
267785bc | 6790 | warning (0, "%<@end%> missing in implementation context"); |
6791 | finish_class (objc_implementation_context); | |
6792 | objc_ivar_chain = NULL_TREE; | |
6793 | objc_implementation_context = NULL_TREE; | |
fc304191 | 6794 | } |
c450c529 | 6795 | |
267785bc | 6796 | /* If this is a class extension, we'll be "reopening" the existing |
6797 | CLASS_INTERFACE_TYPE, so in that case there is no need to create | |
6798 | a new node. */ | |
6799 | if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE) | |
13dcc150 | 6800 | { |
267785bc | 6801 | klass = make_node (code); |
6802 | TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS); | |
6803 | } | |
fc304191 | 6804 | |
267785bc | 6805 | /* Check for existence of the super class, if one was specified. Note |
6806 | that we must have seen an @interface, not just a @class. If we | |
6807 | are looking at a @compatibility_alias, traverse it first. */ | |
6808 | if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE) | |
6809 | && super_name) | |
fc304191 | 6810 | { |
267785bc | 6811 | tree super = objc_is_class_name (super_name); |
6812 | tree super_interface = NULL_TREE; | |
6813 | ||
6814 | if (super) | |
6815 | super_interface = lookup_interface (super); | |
6bbb4715 | 6816 | |
267785bc | 6817 | if (!super_interface) |
6818 | { | |
6819 | error ("cannot find interface declaration for %qE, superclass of %qE", | |
6820 | super ? super : super_name, | |
6821 | class_name); | |
6822 | super_name = NULL_TREE; | |
6823 | } | |
6824 | else | |
6825 | { | |
6826 | if (TREE_DEPRECATED (super_interface)) | |
6bbb4715 | 6827 | warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", |
267785bc | 6828 | super); |
6829 | super_name = super; | |
6830 | } | |
fc304191 | 6831 | } |
6832 | ||
267785bc | 6833 | if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE) |
fc304191 | 6834 | { |
267785bc | 6835 | CLASS_NAME (klass) = class_name; |
6836 | CLASS_SUPER_NAME (klass) = super_name; | |
6837 | CLASS_CLS_METHODS (klass) = NULL_TREE; | |
fc304191 | 6838 | } |
6839 | ||
267785bc | 6840 | if (! objc_is_class_name (class_name) |
6841 | && (decl = lookup_name (class_name))) | |
c450c529 | 6842 | { |
267785bc | 6843 | error ("%qE redeclared as different kind of symbol", |
6844 | class_name); | |
6845 | error ("previous declaration of %q+D", | |
6846 | decl); | |
c450c529 | 6847 | } |
6848 | ||
267785bc | 6849 | switch (code) |
13dcc150 | 6850 | { |
267785bc | 6851 | case CLASS_IMPLEMENTATION_TYPE: |
6852 | { | |
6853 | tree chain; | |
6bbb4715 | 6854 | |
267785bc | 6855 | for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain)) |
6856 | if (TREE_VALUE (chain) == class_name) | |
6857 | { | |
6858 | error ("reimplementation of class %qE", | |
6859 | class_name); | |
6860 | /* TODO: error message saying where it was previously | |
6861 | implemented. */ | |
6862 | break; | |
6863 | } | |
6864 | if (chain == NULL_TREE) | |
6865 | implemented_classes = tree_cons (NULL_TREE, class_name, | |
6866 | implemented_classes); | |
6867 | } | |
fc304191 | 6868 | |
267785bc | 6869 | /* Reset for multiple classes per file. */ |
6870 | method_slot = 0; | |
c17b85ea | 6871 | |
267785bc | 6872 | objc_implementation_context = klass; |
5c872430 | 6873 | |
267785bc | 6874 | /* Lookup the interface for this implementation. */ |
fc304191 | 6875 | |
267785bc | 6876 | if (!(implementation_template = lookup_interface (class_name))) |
6877 | { | |
6878 | warning (0, "cannot find interface declaration for %qE", | |
6879 | class_name); | |
f41791cf | 6880 | add_interface (implementation_template = objc_implementation_context, |
6881 | class_name); | |
267785bc | 6882 | } |
c450c529 | 6883 | |
267785bc | 6884 | /* If a super class has been specified in the implementation, |
6885 | insure it conforms to the one specified in the interface. */ | |
fc304191 | 6886 | |
267785bc | 6887 | if (super_name |
6888 | && (super_name != CLASS_SUPER_NAME (implementation_template))) | |
fc304191 | 6889 | { |
267785bc | 6890 | tree previous_name = CLASS_SUPER_NAME (implementation_template); |
6891 | error ("conflicting super class name %qE", | |
6892 | super_name); | |
6893 | if (previous_name) | |
6894 | error ("previous declaration of %qE", previous_name); | |
fc304191 | 6895 | else |
267785bc | 6896 | error ("previous declaration"); |
fc304191 | 6897 | } |
fc304191 | 6898 | |
267785bc | 6899 | else if (! super_name) |
6900 | { | |
6901 | CLASS_SUPER_NAME (objc_implementation_context) | |
6902 | = CLASS_SUPER_NAME (implementation_template); | |
6903 | } | |
6904 | break; | |
fc304191 | 6905 | |
267785bc | 6906 | case CLASS_INTERFACE_TYPE: |
6907 | if (lookup_interface (class_name)) | |
6908 | #ifdef OBJCPLUS | |
6909 | error ("duplicate interface declaration for class %qE", class_name); | |
6910 | #else | |
6911 | warning (0, "duplicate interface declaration for class %qE", class_name); | |
6912 | #endif | |
6913 | else | |
f41791cf | 6914 | add_interface (klass, class_name); |
6bbb4715 | 6915 | |
267785bc | 6916 | if (protocol_list) |
6917 | CLASS_PROTOCOL_LIST (klass) | |
6918 | = lookup_and_install_protocols (protocol_list, /* definition_required */ true); | |
c450c529 | 6919 | |
267785bc | 6920 | if (attributes) |
6921 | { | |
6922 | tree attribute; | |
6923 | for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute)) | |
6924 | { | |
6925 | tree name = TREE_PURPOSE (attribute); | |
6bbb4715 | 6926 | |
a0aa15e7 | 6927 | /* TODO: Document what the objc_exception attribute is/does. */ |
267785bc | 6928 | /* We handle the 'deprecated' and (undocumented) 'objc_exception' |
6929 | attributes. */ | |
6930 | if (is_attribute_p ("deprecated", name)) | |
6931 | TREE_DEPRECATED (klass) = 1; | |
6932 | else if (is_attribute_p ("objc_exception", name)) | |
6933 | CLASS_HAS_EXCEPTION_ATTR (klass) = 1; | |
6934 | else | |
6935 | /* Warn about and ignore all others for now, but store them. */ | |
6936 | warning (OPT_Wattributes, "%qE attribute directive ignored", name); | |
6937 | } | |
6938 | TYPE_ATTRIBUTES (klass) = attributes; | |
6939 | } | |
6bbb4715 | 6940 | break; |
fc304191 | 6941 | |
267785bc | 6942 | case CATEGORY_INTERFACE_TYPE: |
6943 | { | |
6944 | tree class_category_is_assoc_with; | |
6bbb4715 | 6945 | |
267785bc | 6946 | /* For a category, class_name is really the name of the class that |
6947 | the following set of methods will be associated with. We must | |
6948 | find the interface so that can derive the objects template. */ | |
6949 | if (!(class_category_is_assoc_with = lookup_interface (class_name))) | |
6950 | { | |
6951 | error ("cannot find interface declaration for %qE", | |
6952 | class_name); | |
6953 | exit (FATAL_EXIT_CODE); | |
6954 | } | |
6955 | else | |
6956 | { | |
6957 | if (TREE_DEPRECATED (class_category_is_assoc_with)) | |
6bbb4715 | 6958 | warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", |
267785bc | 6959 | class_name); |
fc304191 | 6960 | |
267785bc | 6961 | if (super_name == NULL_TREE) |
6962 | { | |
6963 | /* This is a class extension. Get the original | |
6964 | interface, and continue working on it. */ | |
6965 | objc_in_class_extension = true; | |
6966 | klass = class_category_is_assoc_with; | |
fc304191 | 6967 | |
267785bc | 6968 | if (protocol_list) |
6969 | { | |
6970 | /* Append protocols to the original protocol | |
6971 | list. */ | |
6972 | CLASS_PROTOCOL_LIST (klass) | |
6973 | = chainon (CLASS_PROTOCOL_LIST (klass), | |
6974 | lookup_and_install_protocols | |
6975 | (protocol_list, | |
6976 | /* definition_required */ true)); | |
6977 | } | |
6978 | } | |
6979 | else | |
6980 | { | |
6981 | add_category (class_category_is_assoc_with, klass); | |
6bbb4715 | 6982 | |
267785bc | 6983 | if (protocol_list) |
6984 | CLASS_PROTOCOL_LIST (klass) | |
6985 | = lookup_and_install_protocols | |
6986 | (protocol_list, /* definition_required */ true); | |
6987 | } | |
6988 | } | |
6989 | } | |
6990 | break; | |
6bbb4715 | 6991 | |
267785bc | 6992 | case CATEGORY_IMPLEMENTATION_TYPE: |
6993 | /* Reset for multiple classes per file. */ | |
6994 | method_slot = 0; | |
fc304191 | 6995 | |
267785bc | 6996 | objc_implementation_context = klass; |
fc304191 | 6997 | |
267785bc | 6998 | /* For a category, class_name is really the name of the class that |
6999 | the following set of methods will be associated with. We must | |
7000 | find the interface so that can derive the objects template. */ | |
fc304191 | 7001 | |
267785bc | 7002 | if (!(implementation_template = lookup_interface (class_name))) |
7003 | { | |
7004 | error ("cannot find interface declaration for %qE", | |
7005 | class_name); | |
7006 | exit (FATAL_EXIT_CODE); | |
7007 | } | |
7008 | break; | |
7009 | default: | |
7010 | gcc_unreachable (); | |
7011 | } | |
7012 | return klass; | |
fc304191 | 7013 | } |
7014 | ||
267785bc | 7015 | static tree |
7016 | continue_class (tree klass) | |
fc304191 | 7017 | { |
267785bc | 7018 | switch (TREE_CODE (klass)) |
e17b52f6 | 7019 | { |
29d8eac9 | 7020 | case CLASS_IMPLEMENTATION_TYPE: |
29d8eac9 | 7021 | case CATEGORY_IMPLEMENTATION_TYPE: |
29d8eac9 | 7022 | { |
267785bc | 7023 | struct imp_entry *imp_entry; |
7024 | ||
7025 | /* Check consistency of the instance variables. */ | |
7026 | ||
7027 | if (CLASS_RAW_IVARS (klass)) | |
7028 | check_ivars (implementation_template, klass); | |
6bbb4715 | 7029 | |
267785bc | 7030 | /* code generation */ |
7031 | #ifdef OBJCPLUS | |
7032 | push_lang_context (lang_name_c); | |
7033 | #endif | |
7034 | build_private_template (implementation_template); | |
7035 | uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template); | |
7036 | objc_instance_type = build_pointer_type (uprivate_record); | |
7037 | ||
25a27413 | 7038 | imp_entry = ggc_alloc<struct imp_entry> (); |
267785bc | 7039 | |
7040 | imp_entry->next = imp_list; | |
7041 | imp_entry->imp_context = klass; | |
7042 | imp_entry->imp_template = implementation_template; | |
7043 | ucls_super_ref = uucls_super_ref = NULL; | |
7044 | if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE) | |
7045 | { | |
7046 | imp_entry->class_decl = (*runtime.class_decl) (klass); | |
7047 | imp_entry->meta_decl = (*runtime.metaclass_decl) (klass); | |
7048 | } | |
7049 | else | |
7050 | { | |
7051 | imp_entry->class_decl = (*runtime.category_decl) (klass); | |
7052 | imp_entry->meta_decl = NULL; | |
7053 | } | |
7054 | imp_entry->has_cxx_cdtors = 0; | |
7055 | ||
7056 | /* Append to front and increment count. */ | |
7057 | imp_list = imp_entry; | |
7058 | if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE) | |
7059 | imp_count++; | |
7060 | else | |
7061 | cat_count++; | |
7062 | #ifdef OBJCPLUS | |
7063 | pop_lang_context (); | |
7064 | #endif /* OBJCPLUS */ | |
6bbb4715 | 7065 | |
267785bc | 7066 | return get_class_ivars (implementation_template, true); |
29d8eac9 | 7067 | break; |
267785bc | 7068 | } |
7069 | case CLASS_INTERFACE_TYPE: | |
29d8eac9 | 7070 | { |
267785bc | 7071 | if (objc_in_class_extension) |
7072 | return NULL_TREE; | |
7073 | #ifdef OBJCPLUS | |
7074 | push_lang_context (lang_name_c); | |
7075 | #endif /* OBJCPLUS */ | |
7076 | objc_collecting_ivars = 1; | |
7077 | build_private_template (klass); | |
7078 | objc_collecting_ivars = 0; | |
7079 | #ifdef OBJCPLUS | |
7080 | pop_lang_context (); | |
7081 | #endif /* OBJCPLUS */ | |
7082 | return NULL_TREE; | |
29d8eac9 | 7083 | break; |
7084 | } | |
7085 | default: | |
267785bc | 7086 | return error_mark_node; |
e17b52f6 | 7087 | } |
c450c529 | 7088 | } |
7089 | ||
267785bc | 7090 | /* This routine builds name of the setter synthesized function. */ |
f572c7ba | 7091 | char * |
267785bc | 7092 | objc_build_property_setter_name (tree ident) |
c450c529 | 7093 | { |
267785bc | 7094 | /* TODO: Use alloca to allocate buffer of appropriate size. */ |
7095 | static char string[BUFSIZE]; | |
7096 | sprintf (string, "set%s:", IDENTIFIER_POINTER (ident)); | |
7097 | string[3] = TOUPPER (string[3]); | |
7098 | return string; | |
c450c529 | 7099 | } |
7100 | ||
267785bc | 7101 | /* This routine prepares the declarations of the property accessor |
7102 | helper functions (objc_getProperty(), etc) that are used when | |
6bbb4715 | 7103 | @synthesize is used. |
7104 | ||
7105 | runtime-specific routines are built in the respective runtime | |
267785bc | 7106 | initialize functions. */ |
6bbb4715 | 7107 | static void |
267785bc | 7108 | build_common_objc_property_accessor_helpers (void) |
7109 | { | |
7110 | tree type; | |
39b8ddc6 | 7111 | |
267785bc | 7112 | /* Declare the following function: |
7113 | id | |
6bbb4715 | 7114 | objc_getProperty (id self, SEL _cmd, |
267785bc | 7115 | ptrdiff_t offset, BOOL is_atomic); */ |
7116 | type = build_function_type_list (objc_object_type, | |
7117 | objc_object_type, | |
7118 | objc_selector_type, | |
7119 | ptrdiff_type_node, | |
7120 | boolean_type_node, | |
7121 | NULL_TREE); | |
7122 | objc_getProperty_decl = add_builtin_function ("objc_getProperty", | |
7123 | type, 0, NOT_BUILT_IN, | |
7124 | NULL, NULL_TREE); | |
7125 | TREE_NOTHROW (objc_getProperty_decl) = 0; | |
6bbb4715 | 7126 | |
267785bc | 7127 | /* Declare the following function: |
7128 | void | |
6bbb4715 | 7129 | objc_setProperty (id self, SEL _cmd, |
7130 | ptrdiff_t offset, id new_value, | |
267785bc | 7131 | BOOL is_atomic, BOOL should_copy); */ |
7132 | type = build_function_type_list (void_type_node, | |
7133 | objc_object_type, | |
7134 | objc_selector_type, | |
7135 | ptrdiff_type_node, | |
7136 | objc_object_type, | |
7137 | boolean_type_node, | |
7138 | boolean_type_node, | |
7139 | NULL_TREE); | |
7140 | objc_setProperty_decl = add_builtin_function ("objc_setProperty", | |
7141 | type, 0, NOT_BUILT_IN, | |
7142 | NULL, NULL_TREE); | |
7143 | TREE_NOTHROW (objc_setProperty_decl) = 0; | |
fc304191 | 7144 | } |
7145 | ||
267785bc | 7146 | /* This looks up an ivar in a class (including superclasses). */ |
fc304191 | 7147 | static tree |
267785bc | 7148 | lookup_ivar (tree interface, tree instance_variable_name) |
fc304191 | 7149 | { |
267785bc | 7150 | while (interface) |
fc304191 | 7151 | { |
267785bc | 7152 | tree decl_chain; |
6bbb4715 | 7153 | |
267785bc | 7154 | for (decl_chain = CLASS_IVARS (interface); decl_chain; decl_chain = DECL_CHAIN (decl_chain)) |
7155 | if (DECL_NAME (decl_chain) == instance_variable_name) | |
7156 | return decl_chain; | |
6bbb4715 | 7157 | |
267785bc | 7158 | /* Not found. Search superclass if any. */ |
7159 | if (CLASS_SUPER_NAME (interface)) | |
7160 | interface = lookup_interface (CLASS_SUPER_NAME (interface)); | |
fc304191 | 7161 | } |
6bbb4715 | 7162 | |
267785bc | 7163 | return NULL_TREE; |
fc304191 | 7164 | } |
7165 | ||
267785bc | 7166 | /* This routine synthesizes a 'getter' method. This is only called |
7167 | for @synthesize properties. */ | |
7168 | static void | |
7169 | objc_synthesize_getter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property) | |
fc304191 | 7170 | { |
267785bc | 7171 | location_t location = DECL_SOURCE_LOCATION (property); |
7172 | tree fn, decl; | |
7173 | tree body; | |
7174 | tree ret_val; | |
fc304191 | 7175 | |
267785bc | 7176 | /* If user has implemented a getter with same name then do nothing. */ |
7177 | if (lookup_method (CLASS_NST_METHODS (objc_implementation_context), | |
7178 | PROPERTY_GETTER_NAME (property))) | |
7179 | return; | |
fc304191 | 7180 | |
267785bc | 7181 | /* Find declaration of the property getter in the interface (or |
7182 | superclass, or protocol). There must be one. */ | |
7183 | decl = lookup_method_static (klass, PROPERTY_GETTER_NAME (property), 0); | |
fc304191 | 7184 | |
267785bc | 7185 | /* If one not declared in the interface, this condition has already |
7186 | been reported as user error (because property was not declared in | |
7187 | the interface). */ | |
7188 | if (!decl) | |
7189 | return; | |
fc304191 | 7190 | |
267785bc | 7191 | /* Adapt the 'decl'. Use the source location of the @synthesize |
7192 | statement for error messages. */ | |
7193 | decl = copy_node (decl); | |
7194 | DECL_SOURCE_LOCATION (decl) = location; | |
fc304191 | 7195 | |
4232a958 | 7196 | objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE, |
7197 | NULL_TREE); | |
267785bc | 7198 | body = c_begin_compound_stmt (true); |
c450c529 | 7199 | |
267785bc | 7200 | /* Now we need to decide how we build the getter. There are three |
7201 | cases: | |
40c8d1dd | 7202 | |
267785bc | 7203 | for 'copy' or 'retain' properties we need to use the |
7204 | objc_getProperty() accessor helper which knows about retain and | |
7205 | copy. It supports both 'nonatomic' and 'atomic' access. | |
8c582e4f | 7206 | |
267785bc | 7207 | for 'nonatomic, assign' properties we can access the instance |
7208 | variable directly. 'nonatomic' means we don't have to use locks, | |
7209 | and 'assign' means we don't have to worry about retain or copy. | |
7210 | If you combine the two, it means we can just access the instance | |
7211 | variable directly. | |
8c582e4f | 7212 | |
267785bc | 7213 | for 'atomic, assign' properties we use objc_copyStruct() (for the |
7214 | next runtime) or objc_getPropertyStruct() (for the GNU runtime). */ | |
7215 | switch (PROPERTY_ASSIGN_SEMANTICS (property)) | |
40c8d1dd | 7216 | { |
267785bc | 7217 | case OBJC_PROPERTY_RETAIN: |
7218 | case OBJC_PROPERTY_COPY: | |
7219 | { | |
7220 | /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */ | |
7221 | tree cmd, ivar, offset, is_atomic; | |
7222 | cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)); | |
8c582e4f | 7223 | |
267785bc | 7224 | /* Find the ivar to compute the offset. */ |
7225 | ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property)); | |
7226 | if (!ivar || is_private (ivar)) | |
7227 | { | |
7228 | /* This should never happen. */ | |
7229 | error_at (location, | |
7230 | "can not find instance variable associated with property"); | |
7231 | ret_val = error_mark_node; | |
7232 | break; | |
7233 | } | |
7234 | offset = byte_position (ivar); | |
8c582e4f | 7235 | |
267785bc | 7236 | if (PROPERTY_NONATOMIC (property)) |
7237 | is_atomic = boolean_false_node; | |
7238 | else | |
7239 | is_atomic = boolean_true_node; | |
7240 | ||
7241 | ret_val = build_function_call | |
7242 | (location, | |
7243 | /* Function prototype. */ | |
7244 | objc_getProperty_decl, | |
7245 | /* Parameters. */ | |
7246 | tree_cons /* self */ | |
7247 | (NULL_TREE, self_decl, | |
7248 | tree_cons /* _cmd */ | |
7249 | (NULL_TREE, cmd, | |
7250 | tree_cons /* offset */ | |
7251 | (NULL_TREE, offset, | |
7252 | tree_cons /* is_atomic */ | |
7253 | (NULL_TREE, is_atomic, NULL_TREE))))); | |
7254 | } | |
7255 | break; | |
6bbb4715 | 7256 | case OBJC_PROPERTY_ASSIGN: |
267785bc | 7257 | if (PROPERTY_NONATOMIC (property)) |
7258 | { | |
7259 | /* We build "return self->PROPERTY_IVAR_NAME;" */ | |
7260 | ret_val = objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)); | |
7261 | break; | |
7262 | } | |
7263 | else | |
7264 | { | |
7265 | /* We build | |
7266 | <property type> __objc_property_temp; | |
7267 | objc_getPropertyStruct (&__objc_property_temp, | |
7268 | &(self->PROPERTY_IVAR_NAME), | |
7269 | sizeof (type of self->PROPERTY_IVAR_NAME), | |
7270 | is_atomic, | |
7271 | false) | |
7272 | return __objc_property_temp; | |
7273 | ||
7274 | For the NeXT runtime, we need to use objc_copyStruct | |
7275 | instead of objc_getPropertyStruct. */ | |
7276 | tree objc_property_temp_decl, function_decl, function_call; | |
7277 | tree size_of, is_atomic; | |
7278 | ||
7279 | objc_property_temp_decl = objc_create_temporary_var (TREE_TYPE (property), "__objc_property_temp"); | |
7280 | DECL_SOURCE_LOCATION (objc_property_temp_decl) = location; | |
7281 | objc_property_temp_decl = lang_hooks.decls.pushdecl (objc_property_temp_decl); | |
7282 | ||
7283 | /* sizeof (ivar type). Since the ivar and the property have | |
7284 | the same type, there is no need to lookup the ivar. */ | |
7285 | size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property), | |
7286 | true /* is_sizeof */, | |
a179a7dc | 7287 | false /* min_alignof */, |
267785bc | 7288 | false /* complain */); |
6bbb4715 | 7289 | |
267785bc | 7290 | if (PROPERTY_NONATOMIC (property)) |
7291 | is_atomic = boolean_false_node; | |
8c582e4f | 7292 | else |
267785bc | 7293 | is_atomic = boolean_true_node; |
6bbb4715 | 7294 | |
267785bc | 7295 | if (objc_copyStruct_decl) |
7296 | function_decl = objc_copyStruct_decl; | |
7297 | else | |
7298 | function_decl = objc_getPropertyStruct_decl; | |
7299 | ||
7300 | function_call = build_function_call | |
7301 | (location, | |
7302 | /* Function prototype. */ | |
7303 | function_decl, | |
7304 | /* Parameters. */ | |
7305 | tree_cons /* &__objc_property_temp_decl */ | |
7306 | /* Warning: note that using build_fold_addr_expr_loc() | |
7307 | here causes invalid code to be generated. */ | |
7308 | (NULL_TREE, build_unary_op (location, ADDR_EXPR, objc_property_temp_decl, 0), | |
7309 | tree_cons /* &(self->PROPERTY_IVAR_NAME); */ | |
6bbb4715 | 7310 | (NULL_TREE, build_fold_addr_expr_loc (location, |
7311 | objc_lookup_ivar | |
267785bc | 7312 | (NULL_TREE, PROPERTY_IVAR_NAME (property))), |
7313 | tree_cons /* sizeof (PROPERTY_IVAR) */ | |
7314 | (NULL_TREE, size_of, | |
7315 | tree_cons /* is_atomic */ | |
7316 | (NULL_TREE, is_atomic, | |
7317 | /* TODO: This is currently ignored by the GNU | |
7318 | runtime, but what about the next one ? */ | |
7319 | tree_cons /* has_strong */ | |
7320 | (NULL_TREE, boolean_true_node, NULL_TREE)))))); | |
7321 | ||
7322 | add_stmt (function_call); | |
7323 | ||
7324 | ret_val = objc_property_temp_decl; | |
8c582e4f | 7325 | } |
267785bc | 7326 | break; |
7327 | default: | |
7328 | gcc_unreachable (); | |
8c582e4f | 7329 | } |
7330 | ||
267785bc | 7331 | gcc_assert (ret_val); |
40c8d1dd | 7332 | |
267785bc | 7333 | #ifdef OBJCPLUS |
7334 | finish_return_stmt (ret_val); | |
7335 | #else | |
7336 | c_finish_return (location, ret_val, NULL_TREE); | |
7337 | #endif | |
7338 | ||
7339 | add_stmt (c_end_compound_stmt (location, body, true)); | |
7340 | fn = current_function_decl; | |
7341 | #ifdef OBJCPLUS | |
7342 | finish_function (); | |
7343 | #endif | |
7344 | objc_finish_method_definition (fn); | |
40c8d1dd | 7345 | } |
7346 | ||
267785bc | 7347 | /* This routine synthesizes a 'setter' method. */ |
848fa8f1 | 7348 | |
267785bc | 7349 | static void |
7350 | objc_synthesize_setter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property) | |
fc304191 | 7351 | { |
267785bc | 7352 | location_t location = DECL_SOURCE_LOCATION (property); |
7353 | tree fn, decl; | |
7354 | tree body; | |
7355 | tree new_value, statement; | |
fc304191 | 7356 | |
267785bc | 7357 | /* If user has implemented a setter with same name then do nothing. */ |
7358 | if (lookup_method (CLASS_NST_METHODS (objc_implementation_context), | |
7359 | PROPERTY_SETTER_NAME (property))) | |
7360 | return; | |
fc304191 | 7361 | |
267785bc | 7362 | /* Find declaration of the property setter in the interface (or |
7363 | superclass, or protocol). There must be one. */ | |
7364 | decl = lookup_method_static (klass, PROPERTY_SETTER_NAME (property), 0); | |
fc304191 | 7365 | |
267785bc | 7366 | /* If one not declared in the interface, this condition has already |
7367 | been reported as user error (because property was not declared in | |
7368 | the interface). */ | |
7369 | if (!decl) | |
7370 | return; | |
c17b85ea | 7371 | |
267785bc | 7372 | /* Adapt the 'decl'. Use the source location of the @synthesize |
7373 | statement for error messages. */ | |
7374 | decl = copy_node (decl); | |
7375 | DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (property); | |
13dcc150 | 7376 | |
4232a958 | 7377 | objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE, |
7378 | NULL_TREE); | |
b5266a0c | 7379 | |
267785bc | 7380 | body = c_begin_compound_stmt (true); |
fc304191 | 7381 | |
267785bc | 7382 | /* The 'new_value' is the only argument to the method, which is the |
7383 | 3rd argument of the function, after self and _cmd. We use twice | |
7384 | TREE_CHAIN to move forward two arguments. */ | |
7385 | new_value = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl))); | |
7386 | ||
7387 | /* This would presumably happen if the user has specified a | |
7388 | prototype for the setter that does not have an argument! */ | |
7389 | if (new_value == NULL_TREE) | |
fc304191 | 7390 | { |
267785bc | 7391 | /* TODO: This should be caught much earlier than this. */ |
7392 | error_at (DECL_SOURCE_LOCATION (decl), "invalid setter, it must have one argument"); | |
7393 | /* Try to recover somehow. */ | |
7394 | new_value = error_mark_node; | |
7395 | } | |
13dcc150 | 7396 | |
267785bc | 7397 | /* Now we need to decide how we build the setter. There are three |
7398 | cases: | |
ea446b0b | 7399 | |
267785bc | 7400 | for 'copy' or 'retain' properties we need to use the |
7401 | objc_setProperty() accessor helper which knows about retain and | |
7402 | copy. It supports both 'nonatomic' and 'atomic' access. | |
13dcc150 | 7403 | |
267785bc | 7404 | for 'nonatomic, assign' properties we can access the instance |
7405 | variable directly. 'nonatomic' means we don't have to use locks, | |
7406 | and 'assign' means we don't have to worry about retain or copy. | |
7407 | If you combine the two, it means we can just access the instance | |
7408 | variable directly. | |
fc304191 | 7409 | |
267785bc | 7410 | for 'atomic, assign' properties we use objc_copyStruct() (for the |
7411 | next runtime) or objc_setPropertyStruct() (for the GNU runtime). */ | |
7412 | switch (PROPERTY_ASSIGN_SEMANTICS (property)) | |
fc304191 | 7413 | { |
267785bc | 7414 | case OBJC_PROPERTY_RETAIN: |
7415 | case OBJC_PROPERTY_COPY: | |
7416 | { | |
7417 | /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */ | |
7418 | tree cmd, ivar, offset, is_atomic, should_copy; | |
7419 | cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)); | |
fc304191 | 7420 | |
267785bc | 7421 | /* Find the ivar to compute the offset. */ |
7422 | ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property)); | |
7423 | if (!ivar || is_private (ivar)) | |
7424 | { | |
7425 | error_at (location, | |
7426 | "can not find instance variable associated with property"); | |
7427 | statement = error_mark_node; | |
7428 | break; | |
7429 | } | |
7430 | offset = byte_position (ivar); | |
fc304191 | 7431 | |
267785bc | 7432 | if (PROPERTY_NONATOMIC (property)) |
7433 | is_atomic = boolean_false_node; | |
7434 | else | |
7435 | is_atomic = boolean_true_node; | |
6bbb4715 | 7436 | |
267785bc | 7437 | if (PROPERTY_ASSIGN_SEMANTICS (property) == OBJC_PROPERTY_COPY) |
7438 | should_copy = boolean_true_node; | |
7439 | else | |
7440 | should_copy = boolean_false_node; | |
0ced0389 | 7441 | |
267785bc | 7442 | statement = build_function_call |
7443 | (location, | |
7444 | /* Function prototype. */ | |
7445 | objc_setProperty_decl, | |
7446 | /* Parameters. */ | |
7447 | tree_cons /* self */ | |
7448 | (NULL_TREE, self_decl, | |
7449 | tree_cons /* _cmd */ | |
7450 | (NULL_TREE, cmd, | |
7451 | tree_cons /* offset */ | |
7452 | (NULL_TREE, offset, | |
7453 | tree_cons /* new_value */ | |
7454 | (NULL_TREE, new_value, | |
7455 | tree_cons /* is_atomic */ | |
6bbb4715 | 7456 | (NULL_TREE, is_atomic, |
267785bc | 7457 | tree_cons /* should_copy */ |
7458 | (NULL_TREE, should_copy, NULL_TREE))))))); | |
7459 | } | |
7460 | break; | |
6bbb4715 | 7461 | case OBJC_PROPERTY_ASSIGN: |
267785bc | 7462 | if (PROPERTY_NONATOMIC (property)) |
7463 | { | |
7464 | /* We build "self->PROPERTY_IVAR_NAME = new_value;" */ | |
7465 | statement = build_modify_expr | |
7466 | (location, | |
7467 | objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)), | |
6bbb4715 | 7468 | NULL_TREE, NOP_EXPR, |
267785bc | 7469 | location, new_value, NULL_TREE); |
7470 | break; | |
7471 | } | |
7472 | else | |
7473 | { | |
7474 | /* We build | |
7475 | objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME), | |
7476 | &new_value, | |
7477 | sizeof (type of self->PROPERTY_IVAR_NAME), | |
7478 | is_atomic, | |
7479 | false) | |
0ced0389 | 7480 | |
267785bc | 7481 | For the NeXT runtime, we need to use objc_copyStruct |
7482 | instead of objc_getPropertyStruct. */ | |
7483 | tree function_decl, size_of, is_atomic; | |
2744a89f | 7484 | |
267785bc | 7485 | /* sizeof (ivar type). Since the ivar and the property have |
7486 | the same type, there is no need to lookup the ivar. */ | |
7487 | size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property), | |
7488 | true /* is_sizeof */, | |
a179a7dc | 7489 | false /* min_alignof */, |
267785bc | 7490 | false /* complain */); |
6bbb4715 | 7491 | |
267785bc | 7492 | if (PROPERTY_NONATOMIC (property)) |
7493 | is_atomic = boolean_false_node; | |
2744a89f | 7494 | else |
267785bc | 7495 | is_atomic = boolean_true_node; |
6bbb4715 | 7496 | |
267785bc | 7497 | if (objc_copyStruct_decl) |
7498 | function_decl = objc_copyStruct_decl; | |
7499 | else | |
7500 | function_decl = objc_setPropertyStruct_decl; | |
2744a89f | 7501 | |
6bbb4715 | 7502 | statement = build_function_call |
267785bc | 7503 | (location, |
7504 | /* Function prototype. */ | |
7505 | function_decl, | |
7506 | /* Parameters. */ | |
7507 | tree_cons /* &(self->PROPERTY_IVAR_NAME); */ | |
6bbb4715 | 7508 | (NULL_TREE, build_fold_addr_expr_loc (location, |
7509 | objc_lookup_ivar | |
267785bc | 7510 | (NULL_TREE, PROPERTY_IVAR_NAME (property))), |
7511 | tree_cons /* &new_value */ | |
7512 | (NULL_TREE, build_fold_addr_expr_loc (location, new_value), | |
7513 | tree_cons /* sizeof (PROPERTY_IVAR) */ | |
7514 | (NULL_TREE, size_of, | |
7515 | tree_cons /* is_atomic */ | |
7516 | (NULL_TREE, is_atomic, | |
7517 | /* TODO: This is currently ignored by the GNU | |
7518 | runtime, but what about the next one ? */ | |
7519 | tree_cons /* has_strong */ | |
7520 | (NULL_TREE, boolean_true_node, NULL_TREE)))))); | |
7521 | } | |
7522 | break; | |
7523 | default: | |
7524 | gcc_unreachable (); | |
fc304191 | 7525 | } |
267785bc | 7526 | gcc_assert (statement); |
fc304191 | 7527 | |
6bbb4715 | 7528 | add_stmt (statement); |
267785bc | 7529 | add_stmt (c_end_compound_stmt (location, body, true)); |
7530 | fn = current_function_decl; | |
7531 | #ifdef OBJCPLUS | |
7532 | finish_function (); | |
7533 | #endif | |
7534 | objc_finish_method_definition (fn); | |
7535 | } | |
9be46be2 | 7536 | |
267785bc | 7537 | /* This function is a sub-routine of objc_add_synthesize_declaration. |
7538 | It is called for each property to synthesize once we have | |
7539 | determined that the context is Ok. */ | |
7540 | static void | |
7541 | objc_add_synthesize_declaration_for_property (location_t location, tree interface, | |
7542 | tree property_name, tree ivar_name) | |
fc304191 | 7543 | { |
267785bc | 7544 | /* Find the @property declaration. */ |
7545 | tree property; | |
7546 | tree x; | |
b11c0115 | 7547 | |
267785bc | 7548 | /* Check that synthesize or dynamic has not already been used for |
7549 | the same property. */ | |
7550 | for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property)) | |
7551 | if (PROPERTY_NAME (property) == property_name) | |
7552 | { | |
7553 | location_t original_location = DECL_SOURCE_LOCATION (property); | |
6bbb4715 | 7554 | |
267785bc | 7555 | if (PROPERTY_DYNAMIC (property)) |
6bbb4715 | 7556 | error_at (location, "property %qs already specified in %<@dynamic%>", |
267785bc | 7557 | IDENTIFIER_POINTER (property_name)); |
7558 | else | |
6bbb4715 | 7559 | error_at (location, "property %qs already specified in %<@synthesize%>", |
267785bc | 7560 | IDENTIFIER_POINTER (property_name)); |
6bbb4715 | 7561 | |
267785bc | 7562 | if (original_location != UNKNOWN_LOCATION) |
7563 | inform (original_location, "originally specified here"); | |
7564 | return; | |
7565 | } | |
39b8ddc6 | 7566 | |
267785bc | 7567 | /* Check that the property is declared in the interface. It could |
7568 | also be declared in a superclass or protocol. */ | |
7569 | property = lookup_property (interface, property_name); | |
c17b85ea | 7570 | |
267785bc | 7571 | if (!property) |
c17b85ea | 7572 | { |
6bbb4715 | 7573 | error_at (location, "no declaration of property %qs found in the interface", |
267785bc | 7574 | IDENTIFIER_POINTER (property_name)); |
7575 | return; | |
7576 | } | |
7577 | else | |
7578 | { | |
7579 | /* We have to copy the property, because we want to chain it to | |
7580 | the implementation context, and we want to store the source | |
7581 | location of the @synthesize, not of the original | |
7582 | @property. */ | |
7583 | property = copy_node (property); | |
7584 | DECL_SOURCE_LOCATION (property) = location; | |
fc304191 | 7585 | } |
fc304191 | 7586 | |
267785bc | 7587 | /* Determine PROPERTY_IVAR_NAME. */ |
7588 | if (ivar_name == NULL_TREE) | |
7589 | ivar_name = property_name; | |
fc304191 | 7590 | |
267785bc | 7591 | /* Check that the instance variable exists. You can only use an |
7592 | instance variable from the same class, not one from the | |
7593 | superclass (this makes sense as it allows us to check that an | |
7594 | instance variable is only used in one synthesized property). */ | |
7595 | { | |
7596 | tree ivar = is_ivar (CLASS_IVARS (interface), ivar_name); | |
7597 | tree type_of_ivar; | |
7598 | if (!ivar) | |
7599 | { | |
6bbb4715 | 7600 | error_at (location, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar", |
267785bc | 7601 | IDENTIFIER_POINTER (property_name)); |
7602 | return; | |
7603 | } | |
fc304191 | 7604 | |
267785bc | 7605 | if (DECL_BIT_FIELD_TYPE (ivar)) |
7606 | type_of_ivar = DECL_BIT_FIELD_TYPE (ivar); | |
7607 | else | |
7608 | type_of_ivar = TREE_TYPE (ivar); | |
6bbb4715 | 7609 | |
267785bc | 7610 | /* If the instance variable has a different C type, we throw an error ... */ |
7611 | if (!comptypes (TREE_TYPE (property), type_of_ivar) | |
7612 | /* ... unless the property is readonly, in which case we allow | |
7613 | the instance variable to be more specialized (this means we | |
7614 | can generate the getter all right and it works). */ | |
7615 | && (!PROPERTY_READONLY (property) | |
7616 | || !objc_compare_types (TREE_TYPE (property), | |
7617 | type_of_ivar, -5, NULL_TREE))) | |
7618 | { | |
7619 | location_t original_location = DECL_SOURCE_LOCATION (ivar); | |
6bbb4715 | 7620 | |
267785bc | 7621 | error_at (location, "property %qs is using instance variable %qs of incompatible type", |
7622 | IDENTIFIER_POINTER (property_name), | |
7623 | IDENTIFIER_POINTER (ivar_name)); | |
6bbb4715 | 7624 | |
267785bc | 7625 | if (original_location != UNKNOWN_LOCATION) |
7626 | inform (original_location, "originally specified here"); | |
7627 | } | |
c450c529 | 7628 | |
267785bc | 7629 | /* If the instance variable is a bitfield, the property must be |
7630 | 'assign', 'nonatomic' because the runtime getter/setter helper | |
7631 | do not work with bitfield instance variables. */ | |
7632 | if (DECL_BIT_FIELD_TYPE (ivar)) | |
7633 | { | |
7634 | /* If there is an error, we return and not generate any | |
7635 | getter/setter because trying to set up the runtime | |
7636 | getter/setter helper calls with bitfields is at high risk | |
7637 | of ICE. */ | |
fc304191 | 7638 | |
267785bc | 7639 | if (PROPERTY_ASSIGN_SEMANTICS (property) != OBJC_PROPERTY_ASSIGN) |
7640 | { | |
7641 | location_t original_location = DECL_SOURCE_LOCATION (ivar); | |
6bbb4715 | 7642 | |
267785bc | 7643 | error_at (location, "'assign' property %qs is using bit-field instance variable %qs", |
7644 | IDENTIFIER_POINTER (property_name), | |
7645 | IDENTIFIER_POINTER (ivar_name)); | |
6bbb4715 | 7646 | |
267785bc | 7647 | if (original_location != UNKNOWN_LOCATION) |
7648 | inform (original_location, "originally specified here"); | |
7649 | return; | |
7650 | } | |
fc304191 | 7651 | |
267785bc | 7652 | if (!PROPERTY_NONATOMIC (property)) |
7653 | { | |
7654 | location_t original_location = DECL_SOURCE_LOCATION (ivar); | |
6bbb4715 | 7655 | |
267785bc | 7656 | error_at (location, "'atomic' property %qs is using bit-field instance variable %qs", |
7657 | IDENTIFIER_POINTER (property_name), | |
7658 | IDENTIFIER_POINTER (ivar_name)); | |
6bbb4715 | 7659 | |
267785bc | 7660 | if (original_location != UNKNOWN_LOCATION) |
7661 | inform (original_location, "originally specified here"); | |
7662 | return; | |
7663 | } | |
7664 | } | |
7665 | } | |
fc304191 | 7666 | |
267785bc | 7667 | /* Check that no other property is using the same instance |
7668 | variable. */ | |
7669 | for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x)) | |
7670 | if (PROPERTY_IVAR_NAME (x) == ivar_name) | |
7671 | { | |
7672 | location_t original_location = DECL_SOURCE_LOCATION (x); | |
6bbb4715 | 7673 | |
267785bc | 7674 | error_at (location, "property %qs is using the same instance variable as property %qs", |
7675 | IDENTIFIER_POINTER (property_name), | |
7676 | IDENTIFIER_POINTER (PROPERTY_NAME (x))); | |
6bbb4715 | 7677 | |
267785bc | 7678 | if (original_location != UNKNOWN_LOCATION) |
7679 | inform (original_location, "originally specified here"); | |
6bbb4715 | 7680 | |
267785bc | 7681 | /* We keep going on. This won't cause the compiler to fail; |
7682 | the failure would most likely be at runtime. */ | |
7683 | } | |
fc304191 | 7684 | |
267785bc | 7685 | /* Note that a @synthesize (and only a @synthesize) always sets |
7686 | PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a | |
7687 | @synthesize by that. */ | |
7688 | PROPERTY_IVAR_NAME (property) = ivar_name; | |
6bbb4715 | 7689 | |
267785bc | 7690 | /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the |
7691 | original declaration; they are always set (with the exception of | |
7692 | PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */ | |
fc304191 | 7693 | |
267785bc | 7694 | /* Add the property to the list of properties for current implementation. */ |
7695 | TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context); | |
7696 | IMPL_PROPERTY_DECL (objc_implementation_context) = property; | |
fc304191 | 7697 | |
267785bc | 7698 | /* Note how we don't actually synthesize the getter/setter here; it |
7699 | would be very natural, but we may miss the fact that the user has | |
7700 | implemented his own getter/setter later on in the @implementation | |
7701 | (in which case we shouldn't generate getter/setter). We wait | |
7702 | until we have parsed it all before generating the code. */ | |
7703 | } | |
fc304191 | 7704 | |
267785bc | 7705 | /* This function is called by the parser after a @synthesize |
7706 | expression is parsed. 'location' is the location of the | |
7707 | @synthesize expression, and 'property_and_ivar_list' is a chained | |
7708 | list of the property and ivar names. */ | |
7709 | void | |
7710 | objc_add_synthesize_declaration (location_t location, tree property_and_ivar_list) | |
7711 | { | |
7712 | tree interface, chain; | |
c17b85ea | 7713 | |
267785bc | 7714 | if (flag_objc1_only) |
7715 | error_at (input_location, "%<@synthesize%> is not available in Objective-C 1.0"); | |
99ff4160 | 7716 | |
267785bc | 7717 | if (property_and_ivar_list == error_mark_node) |
7718 | return; | |
945e49ce | 7719 | |
267785bc | 7720 | if (!objc_implementation_context) |
7721 | { | |
7722 | /* We can get here only in Objective-C; the Objective-C++ parser | |
7723 | detects the problem while parsing, outputs the error | |
7724 | "misplaced '@synthesize' Objective-C++ construct" and skips | |
7725 | the declaration. */ | |
7726 | error_at (location, "%<@synthesize%> not in @implementation context"); | |
7727 | return; | |
7728 | } | |
945e49ce | 7729 | |
267785bc | 7730 | if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE) |
7731 | { | |
7732 | error_at (location, "%<@synthesize%> can not be used in categories"); | |
7733 | return; | |
7734 | } | |
6db57367 | 7735 | |
267785bc | 7736 | interface = lookup_interface (CLASS_NAME (objc_implementation_context)); |
7737 | if (!interface) | |
945e49ce | 7738 | { |
267785bc | 7739 | /* I can't see how this could happen, but it is good as a safety check. */ |
6bbb4715 | 7740 | error_at (location, |
267785bc | 7741 | "%<@synthesize%> requires the @interface of the class to be available"); |
7742 | return; | |
945e49ce | 7743 | } |
c17b85ea | 7744 | |
267785bc | 7745 | /* Now, iterate over the properties and do each of them. */ |
7746 | for (chain = property_and_ivar_list; chain; chain = TREE_CHAIN (chain)) | |
7747 | { | |
6bbb4715 | 7748 | objc_add_synthesize_declaration_for_property (location, interface, TREE_VALUE (chain), |
267785bc | 7749 | TREE_PURPOSE (chain)); |
7750 | } | |
c17b85ea | 7751 | } |
7752 | ||
267785bc | 7753 | /* This function is a sub-routine of objc_add_dynamic_declaration. It |
7754 | is called for each property to mark as dynamic once we have | |
7755 | determined that the context is Ok. */ | |
7756 | static void | |
7757 | objc_add_dynamic_declaration_for_property (location_t location, tree interface, | |
7758 | tree property_name) | |
39b8ddc6 | 7759 | { |
267785bc | 7760 | /* Find the @property declaration. */ |
7761 | tree property; | |
86c110ac | 7762 | |
267785bc | 7763 | /* Check that synthesize or dynamic has not already been used for |
7764 | the same property. */ | |
7765 | for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property)) | |
7766 | if (PROPERTY_NAME (property) == property_name) | |
7767 | { | |
7768 | location_t original_location = DECL_SOURCE_LOCATION (property); | |
6bbb4715 | 7769 | |
267785bc | 7770 | if (PROPERTY_DYNAMIC (property)) |
6bbb4715 | 7771 | error_at (location, "property %qs already specified in %<@dynamic%>", |
267785bc | 7772 | IDENTIFIER_POINTER (property_name)); |
7773 | else | |
7774 | error_at (location, "property %qs already specified in %<@synthesize%>", | |
7775 | IDENTIFIER_POINTER (property_name)); | |
c17b85ea | 7776 | |
267785bc | 7777 | if (original_location != UNKNOWN_LOCATION) |
7778 | inform (original_location, "originally specified here"); | |
7779 | return; | |
7780 | } | |
0ced0389 | 7781 | |
267785bc | 7782 | /* Check that the property is declared in the interface. It could |
7783 | also be declared in a superclass or protocol. */ | |
7784 | property = lookup_property (interface, property_name); | |
c17b85ea | 7785 | |
267785bc | 7786 | if (!property) |
c17b85ea | 7787 | { |
267785bc | 7788 | error_at (location, "no declaration of property %qs found in the interface", |
7789 | IDENTIFIER_POINTER (property_name)); | |
7790 | return; | |
99ff4160 | 7791 | } |
267785bc | 7792 | else |
fc304191 | 7793 | { |
267785bc | 7794 | /* We have to copy the property, because we want to chain it to |
7795 | the implementation context, and we want to store the source | |
7796 | location of the @synthesize, not of the original | |
7797 | @property. */ | |
7798 | property = copy_node (property); | |
7799 | DECL_SOURCE_LOCATION (property) = location; | |
7800 | } | |
c450c529 | 7801 | |
267785bc | 7802 | /* Note that a @dynamic (and only a @dynamic) always sets |
7803 | PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that. | |
7804 | (actually, as explained above, PROPERTY_DECL generated by | |
7805 | @property and associated with a @dynamic property are also marked | |
7806 | as PROPERTY_DYNAMIC). */ | |
7807 | PROPERTY_DYNAMIC (property) = 1; | |
9e8a7e85 | 7808 | |
267785bc | 7809 | /* Add the property to the list of properties for current implementation. */ |
7810 | TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context); | |
7811 | IMPL_PROPERTY_DECL (objc_implementation_context) = property; | |
7812 | } | |
9e8a7e85 | 7813 | |
267785bc | 7814 | /* This function is called by the parser after a @dynamic expression |
7815 | is parsed. 'location' is the location of the @dynamic expression, | |
7816 | and 'property_list' is a chained list of all the property | |
7817 | names. */ | |
7818 | void | |
7819 | objc_add_dynamic_declaration (location_t location, tree property_list) | |
7820 | { | |
7821 | tree interface, chain; | |
c17b85ea | 7822 | |
267785bc | 7823 | if (flag_objc1_only) |
7824 | error_at (input_location, "%<@dynamic%> is not available in Objective-C 1.0"); | |
c450c529 | 7825 | |
267785bc | 7826 | if (property_list == error_mark_node) |
7827 | return; | |
c17b85ea | 7828 | |
267785bc | 7829 | if (!objc_implementation_context) |
7830 | { | |
7831 | /* We can get here only in Objective-C; the Objective-C++ parser | |
7832 | detects the problem while parsing, outputs the error | |
7833 | "misplaced '@dynamic' Objective-C++ construct" and skips the | |
7834 | declaration. */ | |
7835 | error_at (location, "%<@dynamic%> not in @implementation context"); | |
7836 | return; | |
6db57367 | 7837 | } |
c450c529 | 7838 | |
267785bc | 7839 | /* @dynamic is allowed in categories. */ |
7840 | switch (TREE_CODE (objc_implementation_context)) | |
9e8a7e85 | 7841 | { |
267785bc | 7842 | case CLASS_IMPLEMENTATION_TYPE: |
7843 | interface = lookup_interface (CLASS_NAME (objc_implementation_context)); | |
7844 | break; | |
7845 | case CATEGORY_IMPLEMENTATION_TYPE: | |
6bbb4715 | 7846 | interface = lookup_category (implementation_template, |
267785bc | 7847 | CLASS_SUPER_NAME (objc_implementation_context)); |
7848 | break; | |
7849 | default: | |
7850 | gcc_unreachable (); | |
7851 | } | |
9e8a7e85 | 7852 | |
267785bc | 7853 | if (!interface) |
7854 | { | |
7855 | /* I can't see how this could happen, but it is good as a safety check. */ | |
7856 | error_at (location, | |
7857 | "%<@dynamic%> requires the @interface of the class to be available"); | |
7858 | return; | |
9e8a7e85 | 7859 | } |
7860 | ||
267785bc | 7861 | /* Now, iterate over the properties and do each of them. */ |
7862 | for (chain = property_list; chain; chain = TREE_CHAIN (chain)) | |
fc304191 | 7863 | { |
267785bc | 7864 | objc_add_dynamic_declaration_for_property (location, interface, TREE_VALUE (chain)); |
7865 | } | |
7866 | } | |
39b8ddc6 | 7867 | |
6bbb4715 | 7868 | /* Main routine to generate code/data for all the property information for |
267785bc | 7869 | current implementation (class or category). CLASS is the interface where |
7870 | ivars are declared. CLASS_METHODS is where methods are found which | |
7871 | could be a class or a category depending on whether we are implementing | |
7872 | property of a class or a category. */ | |
9e8a7e85 | 7873 | |
267785bc | 7874 | static void |
7875 | objc_gen_property_data (tree klass, tree class_methods) | |
7876 | { | |
7877 | tree x; | |
8c582e4f | 7878 | |
267785bc | 7879 | for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x)) |
7880 | { | |
7881 | /* @dynamic property - nothing to check or synthesize. */ | |
7882 | if (PROPERTY_DYNAMIC (x)) | |
7883 | continue; | |
6bbb4715 | 7884 | |
267785bc | 7885 | /* @synthesize property - need to synthesize the accessors. */ |
7886 | if (PROPERTY_IVAR_NAME (x)) | |
d67e8485 | 7887 | { |
267785bc | 7888 | objc_synthesize_getter (klass, class_methods, x); |
6bbb4715 | 7889 | |
267785bc | 7890 | if (PROPERTY_READONLY (x) == 0) |
7891 | objc_synthesize_setter (klass, class_methods, x); | |
7892 | ||
7893 | continue; | |
d67e8485 | 7894 | } |
8c582e4f | 7895 | |
267785bc | 7896 | gcc_unreachable (); |
7897 | } | |
7898 | } | |
fc304191 | 7899 | |
267785bc | 7900 | /* This is called once we see the "@end" in an interface/implementation. */ |
fc304191 | 7901 | |
267785bc | 7902 | static void |
7903 | finish_class (tree klass) | |
7904 | { | |
7905 | switch (TREE_CODE (klass)) | |
7906 | { | |
7907 | case CLASS_IMPLEMENTATION_TYPE: | |
7908 | { | |
7909 | /* All metadata generation is done in runtime.generate_metadata(). */ | |
6bbb4715 | 7910 | |
267785bc | 7911 | /* Generate what needed for property; setters, getters, etc. */ |
7912 | objc_gen_property_data (implementation_template, implementation_template); | |
146a801c | 7913 | |
267785bc | 7914 | if (implementation_template != objc_implementation_context) |
7915 | { | |
7916 | /* Ensure that all method listed in the interface contain bodies. */ | |
7917 | check_methods (CLASS_CLS_METHODS (implementation_template), | |
7918 | objc_implementation_context, '+'); | |
7919 | check_methods (CLASS_NST_METHODS (implementation_template), | |
7920 | objc_implementation_context, '-'); | |
146a801c | 7921 | |
267785bc | 7922 | if (CLASS_PROTOCOL_LIST (implementation_template)) |
7923 | check_protocols (CLASS_PROTOCOL_LIST (implementation_template), | |
7924 | "class", | |
7925 | CLASS_NAME (objc_implementation_context)); | |
7926 | } | |
7927 | break; | |
7928 | } | |
7929 | case CATEGORY_IMPLEMENTATION_TYPE: | |
7930 | { | |
7931 | tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass)); | |
6bbb4715 | 7932 | |
267785bc | 7933 | if (category) |
7934 | { | |
7935 | /* Generate what needed for property; setters, getters, etc. */ | |
7936 | objc_gen_property_data (implementation_template, category); | |
fc304191 | 7937 | |
267785bc | 7938 | /* Ensure all method listed in the interface contain bodies. */ |
7939 | check_methods (CLASS_CLS_METHODS (category), | |
7940 | objc_implementation_context, '+'); | |
7941 | check_methods (CLASS_NST_METHODS (category), | |
7942 | objc_implementation_context, '-'); | |
6bbb4715 | 7943 | |
267785bc | 7944 | if (CLASS_PROTOCOL_LIST (category)) |
7945 | check_protocols (CLASS_PROTOCOL_LIST (category), | |
7946 | "category", | |
7947 | CLASS_SUPER_NAME (objc_implementation_context)); | |
7948 | } | |
7949 | break; | |
7950 | } | |
7951 | case CLASS_INTERFACE_TYPE: | |
7952 | case CATEGORY_INTERFACE_TYPE: | |
7953 | case PROTOCOL_INTERFACE_TYPE: | |
7954 | { | |
7955 | /* Process properties of the class. */ | |
7956 | tree x; | |
7957 | for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x)) | |
7958 | { | |
7959 | /* Now we check that the appropriate getter is declared, | |
7960 | and if not, we declare one ourselves. */ | |
7961 | tree getter_decl = lookup_method (CLASS_NST_METHODS (klass), | |
7962 | PROPERTY_GETTER_NAME (x)); | |
6bbb4715 | 7963 | |
267785bc | 7964 | if (getter_decl) |
7965 | { | |
7966 | /* TODO: Check that the declaration is consistent with the property. */ | |
7967 | ; | |
7968 | } | |
7969 | else | |
7970 | { | |
7971 | /* Generate an instance method declaration for the | |
7972 | getter; for example "- (id) name;". In general it | |
7973 | will be of the form | |
7974 | -(type)property_getter_name; */ | |
7975 | tree rettype = build_tree_list (NULL_TREE, TREE_TYPE (x)); | |
6bbb4715 | 7976 | getter_decl = build_method_decl (INSTANCE_METHOD_DECL, |
7977 | rettype, PROPERTY_GETTER_NAME (x), | |
267785bc | 7978 | NULL_TREE, false); |
7979 | if (PROPERTY_OPTIONAL (x)) | |
7980 | objc_add_method (objc_interface_context, getter_decl, false, true); | |
7981 | else | |
7982 | objc_add_method (objc_interface_context, getter_decl, false, false); | |
7983 | TREE_DEPRECATED (getter_decl) = TREE_DEPRECATED (x); | |
7984 | METHOD_PROPERTY_CONTEXT (getter_decl) = x; | |
7985 | } | |
fc304191 | 7986 | |
267785bc | 7987 | if (PROPERTY_READONLY (x) == 0) |
7988 | { | |
7989 | /* Now we check that the appropriate setter is declared, | |
7990 | and if not, we declare on ourselves. */ | |
6bbb4715 | 7991 | tree setter_decl = lookup_method (CLASS_NST_METHODS (klass), |
267785bc | 7992 | PROPERTY_SETTER_NAME (x)); |
6bbb4715 | 7993 | |
267785bc | 7994 | if (setter_decl) |
7995 | { | |
7996 | /* TODO: Check that the declaration is consistent with the property. */ | |
7997 | ; | |
7998 | } | |
7999 | else | |
8000 | { | |
8001 | /* The setter name is something like 'setName:'. | |
8002 | We need the substring 'setName' to build the | |
8003 | method declaration due to how the declaration | |
8004 | works. TODO: build_method_decl() will then | |
8005 | generate back 'setName:' from 'setName'; it | |
8006 | would be more efficient to hook into there. */ | |
8007 | const char *full_setter_name = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x)); | |
8008 | size_t length = strlen (full_setter_name); | |
8009 | char *setter_name = (char *) alloca (length); | |
8010 | tree ret_type, selector, arg_type, arg_name; | |
6bbb4715 | 8011 | |
267785bc | 8012 | strcpy (setter_name, full_setter_name); |
8013 | setter_name[length - 1] = '\0'; | |
8014 | ret_type = build_tree_list (NULL_TREE, void_type_node); | |
8015 | arg_type = build_tree_list (NULL_TREE, TREE_TYPE (x)); | |
8016 | arg_name = get_identifier ("_value"); | |
8017 | selector = objc_build_keyword_decl (get_identifier (setter_name), | |
8018 | arg_type, arg_name, NULL); | |
6bbb4715 | 8019 | setter_decl = build_method_decl (INSTANCE_METHOD_DECL, |
267785bc | 8020 | ret_type, selector, |
8021 | build_tree_list (NULL_TREE, NULL_TREE), | |
8022 | false); | |
8023 | if (PROPERTY_OPTIONAL (x)) | |
8024 | objc_add_method (objc_interface_context, setter_decl, false, true); | |
8025 | else | |
8026 | objc_add_method (objc_interface_context, setter_decl, false, false); | |
8027 | TREE_DEPRECATED (setter_decl) = TREE_DEPRECATED (x); | |
8028 | METHOD_PROPERTY_CONTEXT (setter_decl) = x; | |
6bbb4715 | 8029 | } |
267785bc | 8030 | } |
8031 | } | |
8032 | break; | |
8033 | } | |
8034 | default: | |
8035 | gcc_unreachable (); | |
8036 | break; | |
8037 | } | |
fc304191 | 8038 | } |
fc304191 | 8039 | |
8040 | static tree | |
267785bc | 8041 | add_protocol (tree protocol) |
8042 | { | |
8043 | /* Put protocol on list in reverse order. */ | |
8044 | TREE_CHAIN (protocol) = protocol_chain; | |
8045 | protocol_chain = protocol; | |
8046 | return protocol_chain; | |
8047 | } | |
40d49b18 | 8048 | |
267785bc | 8049 | /* Check that a protocol is defined, and, recursively, that all |
8050 | protocols that this protocol conforms to are defined too. */ | |
8051 | static void | |
8052 | check_that_protocol_is_defined (tree protocol) | |
8053 | { | |
8054 | if (!PROTOCOL_DEFINED (protocol)) | |
8055 | warning (0, "definition of protocol %qE not found", | |
8056 | PROTOCOL_NAME (protocol)); | |
8057 | ||
8058 | /* If the protocol itself conforms to other protocols, check them | |
8059 | too, recursively. */ | |
8060 | if (PROTOCOL_LIST (protocol)) | |
fc304191 | 8061 | { |
267785bc | 8062 | tree p; |
40d49b18 | 8063 | |
267785bc | 8064 | for (p = PROTOCOL_LIST (protocol); p; p = TREE_CHAIN (p)) |
8065 | check_that_protocol_is_defined (TREE_VALUE (p)); | |
40d49b18 | 8066 | } |
c450c529 | 8067 | } |
40d49b18 | 8068 | |
267785bc | 8069 | /* Looks up a protocol. If 'warn_if_deprecated' is true, a warning is |
8070 | emitted if the protocol is deprecated. If 'definition_required' is | |
8071 | true, a warning is emitted if a full @protocol definition has not | |
8072 | been seen. */ | |
8073 | static tree | |
8074 | lookup_protocol (tree ident, bool warn_if_deprecated, bool definition_required) | |
c450c529 | 8075 | { |
267785bc | 8076 | tree chain; |
c450c529 | 8077 | |
267785bc | 8078 | for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain)) |
8079 | if (ident == PROTOCOL_NAME (chain)) | |
8080 | { | |
8081 | if (warn_if_deprecated && TREE_DEPRECATED (chain)) | |
8082 | { | |
8083 | /* It would be nice to use warn_deprecated_use() here, but | |
8084 | we are using TREE_CHAIN (which is supposed to be the | |
8085 | TYPE_STUB_DECL for a TYPE) for something different. */ | |
6bbb4715 | 8086 | warning (OPT_Wdeprecated_declarations, "protocol %qE is deprecated", |
267785bc | 8087 | PROTOCOL_NAME (chain)); |
8088 | } | |
c450c529 | 8089 | |
267785bc | 8090 | if (definition_required) |
8091 | check_that_protocol_is_defined (chain); | |
c450c529 | 8092 | |
267785bc | 8093 | return chain; |
8094 | } | |
c450c529 | 8095 | |
267785bc | 8096 | return NULL_TREE; |
8097 | } | |
8098 | ||
8099 | /* This function forward declares the protocols named by NAMES. If | |
8100 | they are already declared or defined, the function has no effect. */ | |
8101 | ||
8102 | void | |
a758bf7d | 8103 | objc_declare_protocol (tree name, tree attributes) |
c450c529 | 8104 | { |
267785bc | 8105 | bool deprecated = false; |
c450c529 | 8106 | |
267785bc | 8107 | #ifdef OBJCPLUS |
8108 | if (current_namespace != global_namespace) { | |
8109 | error ("Objective-C declarations may only appear in global scope"); | |
8110 | } | |
8111 | #endif /* OBJCPLUS */ | |
8112 | ||
8113 | /* Determine if 'deprecated', the only attribute we recognize for | |
8114 | protocols, was used. Ignore all other attributes. */ | |
8115 | if (attributes) | |
c450c529 | 8116 | { |
267785bc | 8117 | tree attribute; |
8118 | for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute)) | |
8119 | { | |
8120 | tree name = TREE_PURPOSE (attribute); | |
6bbb4715 | 8121 | |
267785bc | 8122 | if (is_attribute_p ("deprecated", name)) |
8123 | deprecated = true; | |
8124 | else | |
8125 | warning (OPT_Wattributes, "%qE attribute directive ignored", name); | |
8126 | } | |
fc304191 | 8127 | } |
c450c529 | 8128 | |
a758bf7d | 8129 | if (lookup_protocol (name, /* warn if deprecated */ false, |
8130 | /* definition_required */ false) == NULL_TREE) | |
267785bc | 8131 | { |
a758bf7d | 8132 | tree protocol = make_node (PROTOCOL_INTERFACE_TYPE); |
6bbb4715 | 8133 | |
a758bf7d | 8134 | TYPE_LANG_SLOT_1 (protocol) |
8135 | = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS); | |
8136 | PROTOCOL_NAME (protocol) = name; | |
8137 | PROTOCOL_LIST (protocol) = NULL_TREE; | |
8138 | add_protocol (protocol); | |
8139 | PROTOCOL_DEFINED (protocol) = 0; | |
8140 | PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE; | |
6bbb4715 | 8141 | |
a758bf7d | 8142 | if (attributes) |
3e7fb307 | 8143 | { |
a758bf7d | 8144 | /* TODO: Do we need to store the attributes here ? */ |
8145 | TYPE_ATTRIBUTES (protocol) = attributes; | |
8146 | if (deprecated) | |
8147 | TREE_DEPRECATED (protocol) = 1; | |
267785bc | 8148 | } |
3e7fb307 | 8149 | } |
fc304191 | 8150 | } |
c450c529 | 8151 | |
267785bc | 8152 | static tree |
8153 | start_protocol (enum tree_code code, tree name, tree list, tree attributes) | |
fc304191 | 8154 | { |
267785bc | 8155 | tree protocol; |
8156 | bool deprecated = false; | |
fc304191 | 8157 | |
267785bc | 8158 | #ifdef OBJCPLUS |
8159 | if (current_namespace != global_namespace) { | |
8160 | error ("Objective-C declarations may only appear in global scope"); | |
8161 | } | |
8162 | #endif /* OBJCPLUS */ | |
fc304191 | 8163 | |
267785bc | 8164 | /* Determine if 'deprecated', the only attribute we recognize for |
8165 | protocols, was used. Ignore all other attributes. */ | |
8166 | if (attributes) | |
a70e45e3 | 8167 | { |
267785bc | 8168 | tree attribute; |
8169 | for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute)) | |
a70e45e3 | 8170 | { |
267785bc | 8171 | tree name = TREE_PURPOSE (attribute); |
6bbb4715 | 8172 | |
267785bc | 8173 | if (is_attribute_p ("deprecated", name)) |
8174 | deprecated = true; | |
8175 | else | |
8176 | warning (OPT_Wattributes, "%qE attribute directive ignored", name); | |
a70e45e3 | 8177 | } |
8178 | } | |
39b8ddc6 | 8179 | |
267785bc | 8180 | protocol = lookup_protocol (name, /* warn_if_deprecated */ false, |
8181 | /* definition_required */ false); | |
a70e45e3 | 8182 | |
267785bc | 8183 | if (!protocol) |
8184 | { | |
8185 | protocol = make_node (code); | |
8186 | TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS); | |
fc304191 | 8187 | |
267785bc | 8188 | PROTOCOL_NAME (protocol) = name; |
8189 | PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false); | |
8190 | add_protocol (protocol); | |
8191 | PROTOCOL_DEFINED (protocol) = 1; | |
8192 | PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE; | |
8dff6440 | 8193 | |
267785bc | 8194 | check_protocol_recursively (protocol, list); |
8195 | } | |
8196 | else if (! PROTOCOL_DEFINED (protocol)) | |
8197 | { | |
8198 | PROTOCOL_DEFINED (protocol) = 1; | |
8199 | PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false); | |
fc304191 | 8200 | |
267785bc | 8201 | check_protocol_recursively (protocol, list); |
8202 | } | |
8203 | else | |
8204 | { | |
8205 | warning (0, "duplicate declaration for protocol %qE", | |
8206 | name); | |
8207 | } | |
fc304191 | 8208 | |
267785bc | 8209 | if (attributes) |
e5299e71 | 8210 | { |
267785bc | 8211 | TYPE_ATTRIBUTES (protocol) = attributes; |
8212 | if (deprecated) | |
8213 | TREE_DEPRECATED (protocol) = 1; | |
e5299e71 | 8214 | } |
fc304191 | 8215 | |
267785bc | 8216 | return protocol; |
fc304191 | 8217 | } |
39b8ddc6 | 8218 | |
267785bc | 8219 | /* Decay array and function parameters into pointers. */ |
39b8ddc6 | 8220 | |
267785bc | 8221 | static tree |
8222 | objc_decay_parm_type (tree type) | |
fc304191 | 8223 | { |
267785bc | 8224 | if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE) |
8225 | type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE | |
8226 | ? TREE_TYPE (type) | |
8227 | : type); | |
e9e7c336 | 8228 | |
267785bc | 8229 | return type; |
fc304191 | 8230 | } |
8231 | ||
267785bc | 8232 | static GTY(()) tree objc_parmlist = NULL_TREE; |
e9e7c336 | 8233 | |
267785bc | 8234 | /* Append PARM to a list of formal parameters of a method, making a necessary |
8235 | array-to-pointer adjustment along the way. */ | |
8236 | ||
8237 | void | |
8238 | objc_push_parm (tree parm) | |
e9e7c336 | 8239 | { |
267785bc | 8240 | tree type; |
e9e7c336 | 8241 | |
267785bc | 8242 | if (TREE_TYPE (parm) == error_mark_node) |
e9e7c336 | 8243 | { |
267785bc | 8244 | objc_parmlist = chainon (objc_parmlist, parm); |
8245 | return; | |
e9e7c336 | 8246 | } |
e9e7c336 | 8247 | |
267785bc | 8248 | /* Decay arrays and functions into pointers. */ |
8249 | type = objc_decay_parm_type (TREE_TYPE (parm)); | |
e9e7c336 | 8250 | |
267785bc | 8251 | /* If the parameter type has been decayed, a new PARM_DECL needs to be |
8252 | built as well. */ | |
8253 | if (type != TREE_TYPE (parm)) | |
8254 | parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type); | |
e9e7c336 | 8255 | |
267785bc | 8256 | DECL_ARG_TYPE (parm) |
8257 | = lang_hooks.types.type_promotes_to (TREE_TYPE (parm)); | |
e9e7c336 | 8258 | |
267785bc | 8259 | /* Record constancy and volatility. */ |
8260 | c_apply_type_quals_to_decl | |
8261 | ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0) | |
8262 | | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0) | |
b560fabd | 8263 | | (TYPE_ATOMIC (TREE_TYPE (parm)) ? TYPE_QUAL_ATOMIC : 0) |
267785bc | 8264 | | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm); |
e9e7c336 | 8265 | |
267785bc | 8266 | objc_parmlist = chainon (objc_parmlist, parm); |
e9e7c336 | 8267 | } |
8268 | ||
267785bc | 8269 | /* Retrieve the formal parameter list constructed via preceding calls to |
8270 | objc_push_parm(). */ | |
4b89df4c | 8271 | |
267785bc | 8272 | #ifdef OBJCPLUS |
8273 | tree | |
4232a958 | 8274 | objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED, |
8275 | tree expr ATTRIBUTE_UNUSED) | |
fc304191 | 8276 | { |
267785bc | 8277 | tree parm_info = objc_parmlist; |
8278 | objc_parmlist = NULL_TREE; | |
fc304191 | 8279 | |
267785bc | 8280 | return parm_info; |
fc304191 | 8281 | } |
267785bc | 8282 | #else |
8283 | struct c_arg_info * | |
4232a958 | 8284 | objc_get_parm_info (int have_ellipsis, tree expr) |
fc304191 | 8285 | { |
267785bc | 8286 | tree parm_info = objc_parmlist; |
8287 | struct c_arg_info *arg_info; | |
8288 | /* The C front-end requires an elaborate song and dance at | |
8289 | this point. */ | |
8290 | push_scope (); | |
8291 | declare_parm_level (); | |
8292 | while (parm_info) | |
fc304191 | 8293 | { |
267785bc | 8294 | tree next = DECL_CHAIN (parm_info); |
fc304191 | 8295 | |
267785bc | 8296 | DECL_CHAIN (parm_info) = NULL_TREE; |
8297 | parm_info = pushdecl (parm_info); | |
8298 | finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE); | |
8299 | parm_info = next; | |
fc304191 | 8300 | } |
4232a958 | 8301 | arg_info = get_parm_info (have_ellipsis, expr); |
267785bc | 8302 | pop_scope (); |
8303 | objc_parmlist = NULL_TREE; | |
8304 | return arg_info; | |
fc304191 | 8305 | } |
267785bc | 8306 | #endif |
fc304191 | 8307 | |
267785bc | 8308 | /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC |
8309 | method definitions. In the case of instance methods, we can be more | |
8310 | specific as to the type of 'self'. */ | |
fc304191 | 8311 | |
267785bc | 8312 | static void |
8313 | synth_self_and_ucmd_args (void) | |
fc304191 | 8314 | { |
267785bc | 8315 | tree self_type; |
fc304191 | 8316 | |
267785bc | 8317 | if (objc_method_context |
8318 | && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL) | |
8319 | self_type = objc_instance_type; | |
fc304191 | 8320 | else |
267785bc | 8321 | /* Really a `struct objc_class *'. However, we allow people to |
8322 | assign to self, which changes its type midstream. */ | |
8323 | self_type = objc_object_type; | |
fc304191 | 8324 | |
267785bc | 8325 | /* id self; */ |
8326 | objc_push_parm (build_decl (input_location, | |
8327 | PARM_DECL, self_id, self_type)); | |
a70e45e3 | 8328 | |
267785bc | 8329 | /* SEL _cmd; */ |
8330 | objc_push_parm (build_decl (input_location, | |
8331 | PARM_DECL, ucmd_id, objc_selector_type)); | |
fc304191 | 8332 | } |
8333 | ||
267785bc | 8334 | /* Transform an Objective-C method definition into a static C function |
8335 | definition, synthesizing the first two arguments, "self" and "_cmd", | |
4232a958 | 8336 | in the process. EXPR is NULL or an expression that needs to be |
8337 | evaluated for the side effects of array size expressions in the | |
8338 | parameters. */ | |
6db57367 | 8339 | |
267785bc | 8340 | static void |
4232a958 | 8341 | start_method_def (tree method, tree expr) |
fc304191 | 8342 | { |
267785bc | 8343 | tree parmlist; |
8344 | #ifdef OBJCPLUS | |
8345 | tree parm_info; | |
8346 | #else | |
8347 | struct c_arg_info *parm_info; | |
8348 | #endif | |
8349 | int have_ellipsis = 0; | |
fc304191 | 8350 | |
267785bc | 8351 | /* If we are defining a "dealloc" method in a non-root class, we |
8352 | will need to check if a [super dealloc] is missing, and warn if | |
8353 | it is. */ | |
8354 | if(CLASS_SUPER_NAME (objc_implementation_context) | |
8355 | && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method)))) | |
8356 | should_call_super_dealloc = 1; | |
8357 | else | |
8358 | should_call_super_dealloc = 0; | |
c17b85ea | 8359 | |
267785bc | 8360 | /* Required to implement _msgSuper. */ |
8361 | objc_method_context = method; | |
8362 | UOBJC_SUPER_decl = NULL_TREE; | |
fc304191 | 8363 | |
267785bc | 8364 | /* Generate prototype declarations for arguments..."new-style". */ |
8365 | synth_self_and_ucmd_args (); | |
c450c529 | 8366 | |
267785bc | 8367 | /* Generate argument declarations if a keyword_decl. */ |
8368 | parmlist = METHOD_SEL_ARGS (method); | |
8369 | while (parmlist) | |
8370 | { | |
8371 | /* parmlist is a KEYWORD_DECL. */ | |
8372 | tree type = TREE_VALUE (TREE_TYPE (parmlist)); | |
8373 | tree parm; | |
c17b85ea | 8374 | |
267785bc | 8375 | parm = build_decl (input_location, |
8376 | PARM_DECL, KEYWORD_ARG_NAME (parmlist), type); | |
8377 | decl_attributes (&parm, DECL_ATTRIBUTES (parmlist), 0); | |
8378 | objc_push_parm (parm); | |
8379 | parmlist = DECL_CHAIN (parmlist); | |
8380 | } | |
fc304191 | 8381 | |
267785bc | 8382 | if (METHOD_ADD_ARGS (method)) |
8383 | { | |
8384 | tree akey; | |
8385 | ||
8386 | for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method)); | |
8387 | akey; akey = TREE_CHAIN (akey)) | |
c450c529 | 8388 | { |
267785bc | 8389 | objc_push_parm (TREE_VALUE (akey)); |
c450c529 | 8390 | } |
8391 | ||
267785bc | 8392 | if (METHOD_ADD_ARGS_ELLIPSIS_P (method)) |
8393 | have_ellipsis = 1; | |
fc304191 | 8394 | } |
fc304191 | 8395 | |
4232a958 | 8396 | parm_info = objc_get_parm_info (have_ellipsis, expr); |
fc304191 | 8397 | |
267785bc | 8398 | really_start_method (objc_method_context, parm_info); |
8399 | } | |
df6caf6c | 8400 | |
267785bc | 8401 | /* Return 1 if TYPE1 is equivalent to TYPE2 for purposes of method |
8402 | overloading. */ | |
8403 | static int | |
8404 | objc_types_are_equivalent (tree type1, tree type2) | |
945e49ce | 8405 | { |
267785bc | 8406 | if (type1 == type2) |
8407 | return 1; | |
945e49ce | 8408 | |
267785bc | 8409 | /* Strip away indirections. */ |
8410 | while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE) | |
8411 | && (TREE_CODE (type1) == TREE_CODE (type2))) | |
8412 | type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2); | |
8413 | if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2)) | |
8414 | return 0; | |
945e49ce | 8415 | |
267785bc | 8416 | /* Compare the protocol lists. */ |
8417 | type1 = (TYPE_HAS_OBJC_INFO (type1) | |
8418 | ? TYPE_OBJC_PROTOCOL_LIST (type1) | |
8419 | : NULL_TREE); | |
8420 | type2 = (TYPE_HAS_OBJC_INFO (type2) | |
8421 | ? TYPE_OBJC_PROTOCOL_LIST (type2) | |
8422 | : NULL_TREE); | |
9436777e | 8423 | |
267785bc | 8424 | /* If there are no protocols (most common case), the types are |
8425 | identical. */ | |
8426 | if (type1 == NULL_TREE && type2 == NULL_TREE) | |
8427 | return 1; | |
6bbb4715 | 8428 | |
267785bc | 8429 | /* If one has protocols, and the other one hasn't, they are not |
8430 | identical. */ | |
8431 | if ((type1 == NULL_TREE && type2 != NULL_TREE) | |
8432 | || (type1 != NULL_TREE && type2 == NULL_TREE)) | |
8433 | return 0; | |
9436777e | 8434 | else |
9436777e | 8435 | { |
267785bc | 8436 | /* Else, both have protocols, and we need to do the full |
8437 | comparison. It is possible that either type1 or type2 | |
8438 | contain some duplicate protocols in the list, so we can't | |
8439 | even just compare list_length as a first check. */ | |
8440 | tree t; | |
9436777e | 8441 | |
267785bc | 8442 | for (t = type2; t; t = TREE_CHAIN (t)) |
8443 | if (!lookup_protocol_in_reflist (type1, TREE_VALUE (t))) | |
8444 | return 0; | |
6bbb4715 | 8445 | |
267785bc | 8446 | for (t = type1; t; t = TREE_CHAIN (t)) |
8447 | if (!lookup_protocol_in_reflist (type2, TREE_VALUE (t))) | |
8448 | return 0; | |
6bbb4715 | 8449 | |
267785bc | 8450 | return 1; |
9436777e | 8451 | } |
267785bc | 8452 | } |
9436777e | 8453 | |
267785bc | 8454 | /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */ |
fc304191 | 8455 | |
267785bc | 8456 | static int |
8457 | objc_types_share_size_and_alignment (tree type1, tree type2) | |
8458 | { | |
8459 | return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)) | |
8460 | && TYPE_ALIGN (type1) == TYPE_ALIGN (type2)); | |
8461 | } | |
945e49ce | 8462 | |
267785bc | 8463 | /* Return 1 if PROTO1 is equivalent to PROTO2 |
8464 | for purposes of method overloading. Ordinarily, the type signatures | |
8465 | should match up exactly, unless STRICT is zero, in which case we | |
8466 | shall allow differences in which the size and alignment of a type | |
8467 | is the same. */ | |
945e49ce | 8468 | |
267785bc | 8469 | static int |
8470 | comp_proto_with_proto (tree proto1, tree proto2, int strict) | |
8471 | { | |
bfb15295 | 8472 | tree type1, type2; |
8473 | ||
267785bc | 8474 | /* The following test is needed in case there are hashing |
8475 | collisions. */ | |
8476 | if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2)) | |
8477 | return 0; | |
945e49ce | 8478 | |
267785bc | 8479 | /* Compare return types. */ |
8480 | type1 = TREE_VALUE (TREE_TYPE (proto1)); | |
8481 | type2 = TREE_VALUE (TREE_TYPE (proto2)); | |
0ced0389 | 8482 | |
267785bc | 8483 | if (!objc_types_are_equivalent (type1, type2) |
8484 | && (strict || !objc_types_share_size_and_alignment (type1, type2))) | |
8485 | return 0; | |
8486 | ||
8487 | /* Compare argument types. */ | |
0ced0389 | 8488 | |
bfb15295 | 8489 | /* The first argument (objc_object_type) is always the same, no need |
8490 | to compare. */ | |
8491 | ||
8492 | /* The second argument (objc_selector_type) is always the same, no | |
8493 | need to compare. */ | |
8494 | ||
8495 | /* Compare the other arguments. */ | |
8496 | { | |
8497 | tree arg1, arg2; | |
8498 | ||
8499 | /* Compare METHOD_SEL_ARGS. */ | |
8500 | for (arg1 = METHOD_SEL_ARGS (proto1), arg2 = METHOD_SEL_ARGS (proto2); | |
8501 | arg1 && arg2; | |
8502 | arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2)) | |
8503 | { | |
8504 | type1 = TREE_VALUE (TREE_TYPE (arg1)); | |
8505 | type2 = TREE_VALUE (TREE_TYPE (arg2)); | |
6bbb4715 | 8506 | |
bfb15295 | 8507 | /* FIXME: Do we need to decay argument types to compare them ? */ |
8508 | type1 = objc_decay_parm_type (type1); | |
8509 | type2 = objc_decay_parm_type (type2); | |
6bbb4715 | 8510 | |
bfb15295 | 8511 | if (!objc_types_are_equivalent (type1, type2) |
8512 | && (strict || !objc_types_share_size_and_alignment (type1, type2))) | |
8513 | return 0; | |
8514 | } | |
6bbb4715 | 8515 | |
bfb15295 | 8516 | /* The loop ends when arg1 or arg2 are NULL. Make sure they are |
8517 | both NULL. */ | |
8518 | if (arg1 != arg2) | |
8519 | return 0; | |
8520 | ||
8521 | /* Compare METHOD_ADD_ARGS. */ | |
8522 | if ((METHOD_ADD_ARGS (proto1) && !METHOD_ADD_ARGS (proto2)) | |
8523 | || (METHOD_ADD_ARGS (proto2) && !METHOD_ADD_ARGS (proto1))) | |
8524 | return 0; | |
8525 | ||
8526 | if (METHOD_ADD_ARGS (proto1)) | |
8527 | { | |
8528 | for (arg1 = TREE_CHAIN (METHOD_ADD_ARGS (proto1)), arg2 = TREE_CHAIN (METHOD_ADD_ARGS (proto2)); | |
8529 | arg1 && arg2; | |
8530 | arg1 = TREE_CHAIN (arg1), arg2 = TREE_CHAIN (arg2)) | |
8531 | { | |
8532 | type1 = TREE_TYPE (TREE_VALUE (arg1)); | |
8533 | type2 = TREE_TYPE (TREE_VALUE (arg2)); | |
6bbb4715 | 8534 | |
bfb15295 | 8535 | /* FIXME: Do we need to decay argument types to compare them ? */ |
8536 | type1 = objc_decay_parm_type (type1); | |
8537 | type2 = objc_decay_parm_type (type2); | |
6bbb4715 | 8538 | |
bfb15295 | 8539 | if (!objc_types_are_equivalent (type1, type2) |
8540 | && (strict || !objc_types_share_size_and_alignment (type1, type2))) | |
8541 | return 0; | |
8542 | } | |
8543 | } | |
6bbb4715 | 8544 | |
bfb15295 | 8545 | /* The loop ends when arg1 or arg2 are NULL. Make sure they are |
8546 | both NULL. */ | |
8547 | if (arg1 != arg2) | |
8548 | return 0; | |
8549 | ||
8550 | /* Compare METHOD_ADD_ARGS_ELLIPSIS_P. */ | |
8551 | if (METHOD_ADD_ARGS_ELLIPSIS_P (proto1) != METHOD_ADD_ARGS_ELLIPSIS_P (proto2)) | |
8552 | return 0; | |
8553 | } | |
8554 | ||
8555 | /* Success. */ | |
8556 | return 1; | |
fc304191 | 8557 | } |
8558 | ||
267785bc | 8559 | /* This routine returns true if TYPE is a valid objc object type, |
8560 | suitable for messaging; false otherwise. If 'accept_class' is | |
8561 | 'true', then a Class object is considered valid for messaging and | |
8562 | 'true' is returned if 'type' refers to a Class. If 'accept_class' | |
8563 | is 'false', then a Class object is not considered valid for | |
8564 | messaging and 'false' is returned in that case. */ | |
8565 | ||
8566 | static bool | |
8567 | objc_type_valid_for_messaging (tree type, bool accept_classes) | |
fc304191 | 8568 | { |
267785bc | 8569 | if (!POINTER_TYPE_P (type)) |
8570 | return false; | |
e0ece38e | 8571 | |
267785bc | 8572 | /* Remove the pointer indirection; don't remove more than one |
8573 | otherwise we'd consider "NSObject **" a valid type for messaging, | |
8574 | which it isn't. */ | |
8575 | type = TREE_TYPE (type); | |
fc304191 | 8576 | |
267785bc | 8577 | if (TREE_CODE (type) != RECORD_TYPE) |
8578 | return false; | |
c450c529 | 8579 | |
267785bc | 8580 | if (objc_is_object_id (type)) |
8581 | return true; | |
c450c529 | 8582 | |
267785bc | 8583 | if (objc_is_class_id (type)) |
8584 | return accept_classes; | |
fc304191 | 8585 | |
267785bc | 8586 | if (TYPE_HAS_OBJC_INFO (type)) |
8587 | return true; | |
fc304191 | 8588 | |
267785bc | 8589 | return false; |
8590 | } | |
c17b85ea | 8591 | |
267785bc | 8592 | void |
8593 | objc_start_function (tree name, tree type, tree attrs, | |
8594 | #ifdef OBJCPLUS | |
8595 | tree params | |
8596 | #else | |
8597 | struct c_arg_info *params | |
8598 | #endif | |
8599 | ) | |
8600 | { | |
8601 | tree fndecl = build_decl (input_location, | |
8602 | FUNCTION_DECL, name, type); | |
fc304191 | 8603 | |
267785bc | 8604 | #ifdef OBJCPLUS |
8605 | DECL_ARGUMENTS (fndecl) = params; | |
8606 | DECL_INITIAL (fndecl) = error_mark_node; | |
8607 | DECL_EXTERNAL (fndecl) = 0; | |
8608 | TREE_STATIC (fndecl) = 1; | |
8609 | retrofit_lang_decl (fndecl); | |
8610 | cplus_decl_attributes (&fndecl, attrs, 0); | |
8611 | start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT); | |
8612 | #else | |
8613 | current_function_returns_value = 0; /* Assume, until we see it does. */ | |
8614 | current_function_returns_null = 0; | |
8615 | decl_attributes (&fndecl, attrs, 0); | |
8616 | announce_function (fndecl); | |
8617 | DECL_INITIAL (fndecl) = error_mark_node; | |
8618 | DECL_EXTERNAL (fndecl) = 0; | |
8619 | TREE_STATIC (fndecl) = 1; | |
8620 | current_function_decl = pushdecl (fndecl); | |
8621 | push_scope (); | |
8622 | declare_parm_level (); | |
8623 | DECL_RESULT (current_function_decl) | |
8624 | = build_decl (input_location, | |
8625 | RESULT_DECL, NULL_TREE, | |
8626 | TREE_TYPE (TREE_TYPE (current_function_decl))); | |
8627 | DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1; | |
8628 | DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1; | |
8629 | start_fname_decls (); | |
8630 | store_parm_decls_from (params); | |
8631 | #endif | |
13dcc150 | 8632 | |
267785bc | 8633 | TREE_USED (current_function_decl) = 1; |
fc304191 | 8634 | } |
7fd68cee | 8635 | |
267785bc | 8636 | /* - Generate an identifier for the function. the format is "_n_cls", |
8637 | where 1 <= n <= nMethods, and cls is the name the implementation we | |
8638 | are processing. | |
8639 | - Install the return type from the method declaration. | |
8640 | - If we have a prototype, check for type consistency. */ | |
8641 | ||
8642 | static void | |
8643 | really_start_method (tree method, | |
8644 | #ifdef OBJCPLUS | |
8645 | tree parmlist | |
8646 | #else | |
8647 | struct c_arg_info *parmlist | |
8648 | #endif | |
8649 | ) | |
fc304191 | 8650 | { |
267785bc | 8651 | tree ret_type, meth_type; |
8652 | tree method_id; | |
8653 | const char *sel_name, *class_name, *cat_name; | |
8654 | char *buf; | |
fc304191 | 8655 | |
267785bc | 8656 | /* Synth the storage class & assemble the return type. */ |
8657 | ret_type = TREE_VALUE (TREE_TYPE (method)); | |
c450c529 | 8658 | |
267785bc | 8659 | sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method)); |
8660 | class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context)); | |
8661 | cat_name = ((TREE_CODE (objc_implementation_context) | |
8662 | == CLASS_IMPLEMENTATION_TYPE) | |
8663 | ? NULL | |
8664 | : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context))); | |
8665 | method_slot++; | |
c450c529 | 8666 | |
267785bc | 8667 | /* Make sure this is big enough for any plausible method label. */ |
8668 | buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name) | |
8669 | + (cat_name ? strlen (cat_name) : 0)); | |
fc304191 | 8670 | |
267785bc | 8671 | OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL, |
8672 | class_name, cat_name, sel_name, method_slot); | |
8673 | ||
8674 | method_id = get_identifier (buf); | |
499ea517 | 8675 | |
8676 | #ifdef OBJCPLUS | |
267785bc | 8677 | /* Objective-C methods cannot be overloaded, so we don't need |
8678 | the type encoding appended. It looks bad anyway... */ | |
8679 | push_lang_context (lang_name_c); | |
499ea517 | 8680 | #endif |
8681 | ||
5318d5a9 | 8682 | meth_type = build_function_type_for_method (ret_type, method, METHOD_DEF, 0); |
267785bc | 8683 | objc_start_function (method_id, meth_type, NULL_TREE, parmlist); |
ed672485 | 8684 | |
267785bc | 8685 | /* Set self_decl from the first argument. */ |
8686 | self_decl = DECL_ARGUMENTS (current_function_decl); | |
fc304191 | 8687 | |
267785bc | 8688 | /* Suppress unused warnings. */ |
8689 | TREE_USED (self_decl) = 1; | |
8690 | DECL_READ_P (self_decl) = 1; | |
8691 | TREE_USED (DECL_CHAIN (self_decl)) = 1; | |
8692 | DECL_READ_P (DECL_CHAIN (self_decl)) = 1; | |
8693 | #ifdef OBJCPLUS | |
8694 | pop_lang_context (); | |
8695 | #endif | |
0ced0389 | 8696 | |
267785bc | 8697 | METHOD_DEFINITION (method) = current_function_decl; |
fc304191 | 8698 | |
267785bc | 8699 | /* Check consistency...start_function, pushdecl, duplicate_decls. */ |
fc304191 | 8700 | |
267785bc | 8701 | if (implementation_template != objc_implementation_context) |
8702 | { | |
8703 | tree proto | |
8704 | = lookup_method_static (implementation_template, | |
8705 | METHOD_SEL_NAME (method), | |
8706 | ((TREE_CODE (method) == CLASS_METHOD_DECL) | |
8707 | | OBJC_LOOKUP_NO_SUPER)); | |
2bd21a29 | 8708 | |
267785bc | 8709 | if (proto) |
8710 | { | |
8711 | if (!comp_proto_with_proto (method, proto, 1)) | |
8712 | { | |
8713 | bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL; | |
499ea517 | 8714 | |
267785bc | 8715 | warning_at (DECL_SOURCE_LOCATION (method), 0, |
8716 | "conflicting types for %<%c%s%>", | |
8717 | (type ? '-' : '+'), | |
8718 | identifier_to_locale (gen_method_decl (method))); | |
8719 | inform (DECL_SOURCE_LOCATION (proto), | |
8720 | "previous declaration of %<%c%s%>", | |
8721 | (type ? '-' : '+'), | |
8722 | identifier_to_locale (gen_method_decl (proto))); | |
8723 | } | |
8724 | else | |
8725 | { | |
8726 | /* If the method in the @interface was deprecated, mark | |
8727 | the implemented method as deprecated too. It should | |
8728 | never be used for messaging (when the deprecation | |
8729 | warnings are produced), but just in case. */ | |
8730 | if (TREE_DEPRECATED (proto)) | |
8731 | TREE_DEPRECATED (method) = 1; | |
499ea517 | 8732 | |
267785bc | 8733 | /* If the method in the @interface was marked as |
8734 | 'noreturn', mark the function implementing the method | |
8735 | as 'noreturn' too. */ | |
8736 | TREE_THIS_VOLATILE (current_function_decl) = TREE_THIS_VOLATILE (proto); | |
8737 | } | |
8738 | } | |
8739 | else | |
8740 | { | |
8741 | /* We have a method @implementation even though we did not | |
8742 | see a corresponding @interface declaration (which is allowed | |
8743 | by Objective-C rules). Go ahead and place the method in | |
8744 | the @interface anyway, so that message dispatch lookups | |
8745 | will see it. */ | |
8746 | tree interface = implementation_template; | |
c450c529 | 8747 | |
267785bc | 8748 | if (TREE_CODE (objc_implementation_context) |
8749 | == CATEGORY_IMPLEMENTATION_TYPE) | |
8750 | interface = lookup_category | |
8751 | (interface, | |
8752 | CLASS_SUPER_NAME (objc_implementation_context)); | |
6db57367 | 8753 | |
267785bc | 8754 | if (interface) |
8755 | objc_add_method (interface, copy_node (method), | |
6bbb4715 | 8756 | TREE_CODE (method) == CLASS_METHOD_DECL, |
267785bc | 8757 | /* is_optional= */ false); |
fc304191 | 8758 | } |
fc304191 | 8759 | } |
fc304191 | 8760 | } |
7f5203a8 | 8761 | |
267785bc | 8762 | static void *UOBJC_SUPER_scope = 0; |
fc304191 | 8763 | |
267785bc | 8764 | /* _n_Method (id self, SEL sel, ...) |
8765 | { | |
8766 | struct objc_super _S; | |
8767 | _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...); | |
8768 | } */ | |
8769 | ||
8770 | static tree | |
8771 | get_super_receiver (void) | |
fc304191 | 8772 | { |
267785bc | 8773 | if (objc_method_context) |
8774 | { | |
8775 | tree super_expr, super_expr_list, class_expr; | |
8776 | bool inst_meth; | |
8777 | if (!UOBJC_SUPER_decl) | |
8778 | { | |
8779 | UOBJC_SUPER_decl = build_decl (input_location, | |
8780 | VAR_DECL, get_identifier (TAG_SUPER), | |
8781 | objc_super_template); | |
8782 | /* This prevents `unused variable' warnings when compiling with -Wall. */ | |
8783 | TREE_USED (UOBJC_SUPER_decl) = 1; | |
8784 | DECL_READ_P (UOBJC_SUPER_decl) = 1; | |
8785 | lang_hooks.decls.pushdecl (UOBJC_SUPER_decl); | |
8786 | finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE, | |
6bbb4715 | 8787 | NULL_TREE); |
267785bc | 8788 | UOBJC_SUPER_scope = objc_get_current_scope (); |
8789 | } | |
7f5203a8 | 8790 | |
267785bc | 8791 | /* Set receiver to self. */ |
8792 | super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id); | |
8793 | super_expr = build_modify_expr (input_location, super_expr, NULL_TREE, | |
8794 | NOP_EXPR, input_location, self_decl, | |
8795 | NULL_TREE); | |
8796 | super_expr_list = super_expr; | |
fc304191 | 8797 | |
267785bc | 8798 | /* Set class to begin searching. */ |
8799 | /* Get the ident for the superclass class field & build a ref to it. | |
8800 | ??? maybe we should just name the field the same for all runtimes. */ | |
8801 | super_expr = (*runtime.super_superclassfield_ident) (); | |
8802 | super_expr = objc_build_component_ref (UOBJC_SUPER_decl, super_expr); | |
9d9f5bb3 | 8803 | |
267785bc | 8804 | gcc_assert (imp_list->imp_context == objc_implementation_context |
8805 | && imp_list->imp_template == implementation_template); | |
8806 | inst_meth = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL); | |
8807 | ||
8808 | if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE) | |
6bbb4715 | 8809 | class_expr = (*runtime.get_class_super_ref) (input_location, |
267785bc | 8810 | imp_list, inst_meth); |
8811 | else | |
8812 | /* We have a category. */ | |
fc304191 | 8813 | { |
267785bc | 8814 | tree super_name = CLASS_SUPER_NAME (imp_list->imp_template); |
8815 | tree super_class; | |
8816 | ||
8817 | /* Barf if super used in a category of a root object. */ | |
8818 | if (!super_name) | |
7f5203a8 | 8819 | { |
267785bc | 8820 | error ("no super class declared in interface for %qE", |
8821 | CLASS_NAME (imp_list->imp_template)); | |
8822 | return error_mark_node; | |
8823 | } | |
7f5203a8 | 8824 | |
6bbb4715 | 8825 | super_class = (*runtime.get_category_super_ref) (input_location, |
267785bc | 8826 | imp_list, inst_meth); |
6bbb4715 | 8827 | class_expr = build_c_cast (input_location, |
267785bc | 8828 | TREE_TYPE (super_expr), super_class); |
8829 | } | |
7f5203a8 | 8830 | |
6bbb4715 | 8831 | super_expr = build_modify_expr (input_location, super_expr, NULL_TREE, |
267785bc | 8832 | NOP_EXPR, |
8833 | input_location, class_expr, NULL_TREE); | |
7f5203a8 | 8834 | |
6bbb4715 | 8835 | super_expr_list = build_compound_expr (input_location, |
267785bc | 8836 | super_expr_list, super_expr); |
7f5203a8 | 8837 | |
6bbb4715 | 8838 | super_expr = build_unary_op (input_location, |
267785bc | 8839 | ADDR_EXPR, UOBJC_SUPER_decl, 0); |
8840 | super_expr_list = build_compound_expr (input_location, | |
8841 | super_expr_list, super_expr); | |
7f5203a8 | 8842 | |
267785bc | 8843 | return super_expr_list; |
8844 | } | |
8845 | else | |
8846 | { | |
8847 | error ("[super ...] must appear in a method context"); | |
8848 | return error_mark_node; | |
fc304191 | 8849 | } |
c450c529 | 8850 | } |
8851 | ||
267785bc | 8852 | /* When exiting a scope, sever links to a 'super' declaration (if any) |
8853 | therein contained. */ | |
b11c0115 | 8854 | |
267785bc | 8855 | void |
8856 | objc_clear_super_receiver (void) | |
c450c529 | 8857 | { |
267785bc | 8858 | if (objc_method_context |
a0aa15e7 | 8859 | && UOBJC_SUPER_scope == objc_get_current_scope ()) |
8860 | { | |
8861 | UOBJC_SUPER_decl = 0; | |
8862 | UOBJC_SUPER_scope = 0; | |
8863 | } | |
267785bc | 8864 | } |
e0ece38e | 8865 | |
267785bc | 8866 | void |
8867 | objc_finish_method_definition (tree fndecl) | |
8868 | { | |
8869 | /* We cannot validly inline ObjC methods, at least not without a language | |
8870 | extension to declare that a method need not be dynamically | |
8871 | dispatched, so suppress all thoughts of doing so. */ | |
8872 | DECL_UNINLINABLE (fndecl) = 1; | |
e0ece38e | 8873 | |
267785bc | 8874 | #ifndef OBJCPLUS |
8875 | /* The C++ front-end will have called finish_function() for us. */ | |
8876 | finish_function (); | |
8877 | #endif | |
8878 | ||
8879 | METHOD_ENCODING (objc_method_context) | |
8880 | = encode_method_prototype (objc_method_context); | |
8881 | ||
8882 | /* Required to implement _msgSuper. This must be done AFTER finish_function, | |
8883 | since the optimizer may find "may be used before set" errors. */ | |
8884 | objc_method_context = NULL_TREE; | |
8885 | ||
8886 | if (should_call_super_dealloc) | |
8887 | warning (0, "method possibly missing a [super dealloc] call"); | |
c450c529 | 8888 | } |
8889 | ||
267785bc | 8890 | /* Given a tree DECL node, produce a printable description of it in the given |
8891 | buffer, overwriting the buffer. */ | |
a8102dff | 8892 | |
267785bc | 8893 | static char * |
8894 | gen_declaration (tree decl) | |
a8102dff | 8895 | { |
267785bc | 8896 | errbuf[0] = '\0'; |
a8102dff | 8897 | |
267785bc | 8898 | if (DECL_P (decl)) |
a8102dff | 8899 | { |
267785bc | 8900 | gen_type_name_0 (TREE_TYPE (decl)); |
9d9f5bb3 | 8901 | |
267785bc | 8902 | if (DECL_NAME (decl)) |
a8102dff | 8903 | { |
267785bc | 8904 | if (!POINTER_TYPE_P (TREE_TYPE (decl))) |
8905 | strcat (errbuf, " "); | |
a8102dff | 8906 | |
267785bc | 8907 | strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl))); |
a8102dff | 8908 | } |
8909 | ||
267785bc | 8910 | if (DECL_INITIAL (decl) |
8911 | && TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST) | |
8912 | sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC, | |
f9ae6f95 | 8913 | TREE_INT_CST_LOW (DECL_INITIAL (decl))); |
a8102dff | 8914 | } |
267785bc | 8915 | |
8916 | return errbuf; | |
a8102dff | 8917 | } |
8918 | ||
267785bc | 8919 | /* Given a tree TYPE node, produce a printable description of it in the given |
8920 | buffer, overwriting the buffer. */ | |
39b8ddc6 | 8921 | |
267785bc | 8922 | static char * |
8923 | gen_type_name_0 (tree type) | |
c450c529 | 8924 | { |
267785bc | 8925 | tree orig = type, proto; |
8926 | ||
8927 | if (TYPE_P (type) && TYPE_NAME (type)) | |
8928 | type = TYPE_NAME (type); | |
8929 | else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE) | |
c450c529 | 8930 | { |
267785bc | 8931 | tree inner = TREE_TYPE (type); |
c450c529 | 8932 | |
267785bc | 8933 | while (TREE_CODE (inner) == ARRAY_TYPE) |
8934 | inner = TREE_TYPE (inner); | |
c450c529 | 8935 | |
267785bc | 8936 | gen_type_name_0 (inner); |
39b8ddc6 | 8937 | |
267785bc | 8938 | if (!POINTER_TYPE_P (inner)) |
8939 | strcat (errbuf, " "); | |
3e1e20c1 | 8940 | |
267785bc | 8941 | if (POINTER_TYPE_P (type)) |
8942 | strcat (errbuf, "*"); | |
8943 | else | |
8944 | while (type != inner) | |
8945 | { | |
8946 | strcat (errbuf, "["); | |
b11c0115 | 8947 | |
267785bc | 8948 | if (TYPE_DOMAIN (type)) |
8949 | { | |
8950 | char sz[20]; | |
39b8ddc6 | 8951 | |
267785bc | 8952 | sprintf (sz, HOST_WIDE_INT_PRINT_DEC, |
f9ae6f95 | 8953 | (TREE_INT_CST_LOW |
267785bc | 8954 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1)); |
8955 | strcat (errbuf, sz); | |
8956 | } | |
39b8ddc6 | 8957 | |
267785bc | 8958 | strcat (errbuf, "]"); |
8959 | type = TREE_TYPE (type); | |
8960 | } | |
b11c0115 | 8961 | |
267785bc | 8962 | goto exit_function; |
b11c0115 | 8963 | } |
fc304191 | 8964 | |
267785bc | 8965 | if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type)) |
8966 | type = DECL_NAME (type); | |
c17b85ea | 8967 | |
267785bc | 8968 | strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE |
6bbb4715 | 8969 | ? IDENTIFIER_POINTER (type) |
267785bc | 8970 | : ""); |
8971 | ||
8972 | /* For 'id' and 'Class', adopted protocols are stored in the pointee. */ | |
8973 | if (objc_is_id (orig)) | |
8974 | orig = TREE_TYPE (orig); | |
8975 | ||
8976 | proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE; | |
8977 | ||
8978 | if (proto) | |
e4a9e43e | 8979 | { |
267785bc | 8980 | strcat (errbuf, " <"); |
8981 | ||
8982 | while (proto) { | |
8983 | strcat (errbuf, | |
8984 | IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto)))); | |
8985 | proto = TREE_CHAIN (proto); | |
8986 | strcat (errbuf, proto ? ", " : ">"); | |
8987 | } | |
e4a9e43e | 8988 | } |
8989 | ||
267785bc | 8990 | exit_function: |
8991 | return errbuf; | |
8992 | } | |
8993 | ||
8994 | static char * | |
8995 | gen_type_name (tree type) | |
8996 | { | |
8997 | errbuf[0] = '\0'; | |
8998 | ||
8999 | return gen_type_name_0 (type); | |
9000 | } | |
9001 | ||
9002 | /* Given a method tree, put a printable description into the given | |
9003 | buffer (overwriting) and return a pointer to the buffer. */ | |
9004 | ||
9005 | static char * | |
9006 | gen_method_decl (tree method) | |
9007 | { | |
9008 | tree chain; | |
9009 | ||
9010 | strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */ | |
9011 | gen_type_name_0 (TREE_VALUE (TREE_TYPE (method))); | |
9012 | strcat (errbuf, ")"); | |
9013 | chain = METHOD_SEL_ARGS (method); | |
9014 | ||
9015 | if (chain) | |
e16610d0 | 9016 | { |
267785bc | 9017 | /* We have a chain of keyword_decls. */ |
9018 | do | |
9019 | { | |
9020 | if (KEYWORD_KEY_NAME (chain)) | |
9021 | strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain))); | |
9022 | ||
9023 | strcat (errbuf, ":("); | |
9024 | gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain))); | |
9025 | strcat (errbuf, ")"); | |
9026 | ||
9027 | strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain))); | |
9028 | if ((chain = DECL_CHAIN (chain))) | |
9029 | strcat (errbuf, " "); | |
9030 | } | |
9031 | while (chain); | |
9032 | ||
9033 | if (METHOD_ADD_ARGS (method)) | |
9034 | { | |
9035 | chain = TREE_CHAIN (METHOD_ADD_ARGS (method)); | |
9036 | ||
9037 | /* Know we have a chain of parm_decls. */ | |
9038 | while (chain) | |
9039 | { | |
9040 | strcat (errbuf, ", "); | |
9041 | gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain))); | |
9042 | chain = TREE_CHAIN (chain); | |
9043 | } | |
9044 | ||
9045 | if (METHOD_ADD_ARGS_ELLIPSIS_P (method)) | |
9046 | strcat (errbuf, ", ..."); | |
9047 | } | |
e16610d0 | 9048 | } |
fc304191 | 9049 | |
267785bc | 9050 | else |
9051 | /* We have a unary selector. */ | |
9052 | strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method))); | |
9053 | ||
9054 | return errbuf; | |
9055 | } | |
9056 | \f | |
9057 | /* Debug info. */ | |
9058 | ||
9059 | ||
9060 | /* Dump an @interface declaration of the supplied class CHAIN to the | |
9061 | supplied file FP. Used to implement the -gen-decls option (which | |
9062 | prints out an @interface declaration of all classes compiled in | |
9063 | this run); potentially useful for debugging the compiler too. */ | |
9064 | void | |
9065 | dump_interface (FILE *fp, tree chain) | |
9066 | { | |
9067 | /* FIXME: A heap overflow here whenever a method (or ivar) | |
9068 | declaration is so long that it doesn't fit in the buffer. The | |
9069 | code and all the related functions should be rewritten to avoid | |
9070 | using fixed size buffers. */ | |
9071 | const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain)); | |
9072 | tree ivar_decls = CLASS_RAW_IVARS (chain); | |
9073 | tree nst_methods = CLASS_NST_METHODS (chain); | |
9074 | tree cls_methods = CLASS_CLS_METHODS (chain); | |
9075 | ||
9076 | fprintf (fp, "\n@interface %s", my_name); | |
9077 | ||
9078 | /* CLASS_SUPER_NAME is used to store the superclass name for | |
9079 | classes, and the category name for categories. */ | |
9080 | if (CLASS_SUPER_NAME (chain)) | |
13dcc150 | 9081 | { |
267785bc | 9082 | const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain)); |
0ced0389 | 9083 | |
267785bc | 9084 | switch (TREE_CODE (chain)) |
0ced0389 | 9085 | { |
267785bc | 9086 | case CATEGORY_IMPLEMENTATION_TYPE: |
9087 | case CATEGORY_INTERFACE_TYPE: | |
9088 | fprintf (fp, " (%s)\n", name); | |
9089 | break; | |
9090 | default: | |
9091 | fprintf (fp, " : %s\n", name); | |
9092 | break; | |
0ced0389 | 9093 | } |
267785bc | 9094 | } |
9095 | else | |
9096 | fprintf (fp, "\n"); | |
9097 | ||
9098 | /* FIXME - the following doesn't seem to work at the moment. */ | |
9099 | if (ivar_decls) | |
9100 | { | |
9101 | fprintf (fp, "{\n"); | |
9102 | do | |
c1cfb0f5 | 9103 | { |
267785bc | 9104 | fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls)); |
9105 | ivar_decls = TREE_CHAIN (ivar_decls); | |
c1cfb0f5 | 9106 | } |
267785bc | 9107 | while (ivar_decls); |
9108 | fprintf (fp, "}\n"); | |
13dcc150 | 9109 | } |
9110 | ||
267785bc | 9111 | while (nst_methods) |
e16610d0 | 9112 | { |
267785bc | 9113 | fprintf (fp, "- %s;\n", gen_method_decl (nst_methods)); |
9114 | nst_methods = TREE_CHAIN (nst_methods); | |
e16610d0 | 9115 | } |
fc304191 | 9116 | |
267785bc | 9117 | while (cls_methods) |
c450c529 | 9118 | { |
267785bc | 9119 | fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods)); |
9120 | cls_methods = TREE_CHAIN (cls_methods); | |
c450c529 | 9121 | } |
9122 | ||
267785bc | 9123 | fprintf (fp, "@end\n"); |
9124 | } | |
c450c529 | 9125 | |
267785bc | 9126 | #if 0 |
9127 | /* Produce the pretty printing for an Objective-C method. This is | |
9128 | currently unused, but could be handy while reorganizing the pretty | |
9129 | printing to be more robust. */ | |
9130 | static const char * | |
9131 | objc_pretty_print_method (bool is_class_method, | |
9132 | const char *class_name, | |
9133 | const char *category_name, | |
9134 | const char *selector) | |
9135 | { | |
9136 | if (category_name) | |
9137 | { | |
6bbb4715 | 9138 | char *result = XNEWVEC (char, strlen (class_name) + strlen (category_name) |
267785bc | 9139 | + strlen (selector) + 7); |
fc304191 | 9140 | |
267785bc | 9141 | if (is_class_method) |
9142 | sprintf (result, "+[%s(%s) %s]", class_name, category_name, selector); | |
9143 | else | |
9144 | sprintf (result, "-[%s(%s) %s]", class_name, category_name, selector); | |
fc304191 | 9145 | |
267785bc | 9146 | return result; |
9147 | } | |
9148 | else | |
9149 | { | |
9150 | char *result = XNEWVEC (char, strlen (class_name) | |
9151 | + strlen (selector) + 5); | |
fc304191 | 9152 | |
267785bc | 9153 | if (is_class_method) |
9154 | sprintf (result, "+[%s %s]", class_name, selector); | |
9155 | else | |
9156 | sprintf (result, "-[%s %s]", class_name, selector); | |
fc304191 | 9157 | |
6bbb4715 | 9158 | return result; |
267785bc | 9159 | } |
9160 | } | |
9161 | #endif | |
fc304191 | 9162 | |
267785bc | 9163 | /* Demangle function for Objective-C. Attempt to demangle the |
9164 | function name associated with a method (eg, going from | |
9165 | "_i_NSObject__class" to "-[NSObject class]"); usually for the | |
9166 | purpose of pretty printing or error messages. Return the demangled | |
9167 | name, or NULL if the string is not an Objective-C mangled method | |
9168 | name. | |
9169 | ||
9170 | Because of how the mangling is done, any method that has a '_' in | |
9171 | its original name is at risk of being demangled incorrectly. In | |
9172 | some cases there are multiple valid ways to demangle a method name | |
9173 | and there is no way we can decide. | |
9174 | ||
9175 | TODO: objc_demangle() can't always get it right; the right way to | |
9176 | get this correct for all method names would be to store the | |
9177 | Objective-C method name somewhere in the function decl. Then, | |
9178 | there is no demangling to do; we'd just pull the method name out of | |
9179 | the decl. As an additional bonus, when printing error messages we | |
9180 | could check for such a method name, and if we find it, we know the | |
9181 | function is actually an Objective-C method and we could print error | |
9182 | messages saying "In method '+[NSObject class]" instead of "In | |
9183 | function '+[NSObject class]" as we do now. */ | |
9184 | static const char * | |
9185 | objc_demangle (const char *mangled) | |
9186 | { | |
9187 | char *demangled, *cp; | |
9188 | ||
9189 | /* First of all, if the name is too short it can't be an Objective-C | |
9190 | mangled method name. */ | |
9191 | if (mangled[0] == '\0' || mangled[1] == '\0' || mangled[2] == '\0') | |
9192 | return NULL; | |
9193 | ||
9194 | /* If the name looks like an already demangled one, return it | |
9195 | unchanged. This should only happen on Darwin, where method names | |
9196 | are mangled differently into a pretty-print form (such as | |
9197 | '+[NSObject class]', see darwin.h). In that case, demangling is | |
9198 | a no-op, but we need to return the demangled name if it was an | |
9199 | ObjC one, and return NULL if not. We should be safe as no C/C++ | |
9200 | function can start with "-[" or "+[". */ | |
9201 | if ((mangled[0] == '-' || mangled[0] == '+') | |
9202 | && (mangled[1] == '[')) | |
9203 | return mangled; | |
9204 | ||
9205 | if (mangled[0] == '_' && | |
9206 | (mangled[1] == 'i' || mangled[1] == 'c') && | |
9207 | mangled[2] == '_') | |
9208 | { | |
9209 | cp = demangled = XNEWVEC (char, strlen(mangled) + 2); | |
9210 | if (mangled[1] == 'i') | |
9211 | *cp++ = '-'; /* for instance method */ | |
9212 | else | |
9213 | *cp++ = '+'; /* for class method */ | |
9214 | *cp++ = '['; /* opening left brace */ | |
9215 | strcpy(cp, mangled+3); /* tack on the rest of the mangled name */ | |
9216 | while (*cp && *cp == '_') | |
9217 | cp++; /* skip any initial underbars in class name */ | |
9218 | cp = strchr(cp, '_'); /* find first non-initial underbar */ | |
9219 | if (cp == NULL) | |
29d8eac9 | 9220 | { |
267785bc | 9221 | free(demangled); /* not mangled name */ |
9222 | return NULL; | |
29d8eac9 | 9223 | } |
267785bc | 9224 | if (cp[1] == '_') /* easy case: no category name */ |
a8102dff | 9225 | { |
267785bc | 9226 | *cp++ = ' '; /* replace two '_' with one ' ' */ |
9227 | strcpy(cp, mangled + (cp - demangled) + 2); | |
a8102dff | 9228 | } |
fc304191 | 9229 | else |
c1cfb0f5 | 9230 | { |
267785bc | 9231 | *cp++ = '('; /* less easy case: category name */ |
9232 | cp = strchr(cp, '_'); | |
9233 | if (cp == 0) | |
c1cfb0f5 | 9234 | { |
267785bc | 9235 | free(demangled); /* not mangled name */ |
9236 | return NULL; | |
c1cfb0f5 | 9237 | } |
267785bc | 9238 | *cp++ = ')'; |
9239 | *cp++ = ' '; /* overwriting 1st char of method name... */ | |
9240 | strcpy(cp, mangled + (cp - demangled)); /* get it back */ | |
c1cfb0f5 | 9241 | } |
267785bc | 9242 | /* Now we have the method name. We need to generally replace |
9243 | '_' with ':' but trying to preserve '_' if it could only have | |
9244 | been in the mangled string because it was already in the | |
9245 | original name. In cases where it's ambiguous, we assume that | |
9246 | any '_' originated from a ':'. */ | |
e0ece38e | 9247 | |
267785bc | 9248 | /* Initial '_'s in method name can't have been generating by |
9249 | converting ':'s. Skip them. */ | |
9250 | while (*cp && *cp == '_') | |
9251 | cp++; | |
c1cfb0f5 | 9252 | |
267785bc | 9253 | /* If the method name does not end with '_', then it has no |
9254 | arguments and there was no replacement of ':'s with '_'s | |
9255 | during mangling. Check for that case, and skip any | |
9256 | replacement if so. This at least guarantees that methods | |
9257 | with no arguments are always demangled correctly (unless the | |
9258 | original name ends with '_'). */ | |
9259 | if (*(mangled + strlen (mangled) - 1) != '_') | |
9260 | { | |
9261 | /* Skip to the end. */ | |
9262 | for (; *cp; cp++) | |
9263 | ; | |
9264 | } | |
9265 | else | |
9266 | { | |
9267 | /* Replace remaining '_' with ':'. This may get it wrong if | |
9268 | there were '_'s in the original name. In most cases it | |
9269 | is impossible to disambiguate. */ | |
9270 | for (; *cp; cp++) | |
9271 | if (*cp == '_') | |
6bbb4715 | 9272 | *cp = ':'; |
267785bc | 9273 | } |
9274 | *cp++ = ']'; /* closing right brace */ | |
9275 | *cp++ = 0; /* string terminator */ | |
9276 | return demangled; | |
9277 | } | |
9278 | else | |
9279 | return NULL; /* not an objc mangled name */ | |
9280 | } | |
e16610d0 | 9281 | |
267785bc | 9282 | /* Try to pretty-print a decl. If the 'decl' is an Objective-C |
9283 | specific decl, return the printable name for it. If not, return | |
9284 | NULL. */ | |
9285 | const char * | |
9286 | objc_maybe_printable_name (tree decl, int v ATTRIBUTE_UNUSED) | |
9287 | { | |
9288 | switch (TREE_CODE (decl)) | |
9289 | { | |
9290 | case FUNCTION_DECL: | |
9291 | return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl))); | |
29d8eac9 | 9292 | break; |
fc304191 | 9293 | |
267785bc | 9294 | /* The following happens when we are printing a deprecation |
9295 | warning for a method. The warn_deprecation() will end up | |
9296 | trying to print the decl for INSTANCE_METHOD_DECL or | |
9297 | CLASS_METHOD_DECL. It would be nice to be able to print | |
9298 | "-[NSObject autorelease] is deprecated", but to do that, we'd | |
9299 | need to store the class and method name in the method decl, | |
9300 | which we currently don't do. For now, just return the name | |
9301 | of the method. We don't return NULL, because that may | |
9302 | trigger further attempts to pretty-print the decl in C/C++, | |
9303 | but they wouldn't know how to pretty-print it. */ | |
9304 | case INSTANCE_METHOD_DECL: | |
9305 | case CLASS_METHOD_DECL: | |
9306 | return IDENTIFIER_POINTER (DECL_NAME (decl)); | |
9307 | break; | |
9308 | /* This happens when printing a deprecation warning for a | |
9309 | property. We may want to consider some sort of pretty | |
9310 | printing (eg, include the class name where it was declared | |
9311 | ?). */ | |
9312 | case PROPERTY_DECL: | |
9313 | return IDENTIFIER_POINTER (PROPERTY_NAME (decl)); | |
29d8eac9 | 9314 | break; |
9315 | default: | |
267785bc | 9316 | return NULL; |
9317 | break; | |
fc304191 | 9318 | } |
fc304191 | 9319 | } |
9320 | ||
267785bc | 9321 | /* Return a printable name for 'decl'. This first tries |
9322 | objc_maybe_printable_name(), and if that fails, it returns the name | |
9323 | in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for | |
9324 | Objective-C; in Objective-C++, setting the hook is not enough | |
9325 | because lots of C++ Front-End code calls cxx_printable_name, | |
9326 | dump_decl and other C++ functions directly. So instead we have | |
9327 | modified dump_decl to call objc_maybe_printable_name directly. */ | |
9328 | const char * | |
9329 | objc_printable_name (tree decl, int v) | |
9330 | { | |
9331 | const char *demangled_name = objc_maybe_printable_name (decl, v); | |
fc304191 | 9332 | |
267785bc | 9333 | if (demangled_name != NULL) |
9334 | return demangled_name; | |
9335 | else | |
9336 | return IDENTIFIER_POINTER (DECL_NAME (decl)); | |
9337 | } | |
fc304191 | 9338 | |
6bbb4715 | 9339 | /* Routine is called to issue diagnostic when reference to a private |
9340 | ivar is made and no other variable with same name is found in | |
267785bc | 9341 | current scope. */ |
9342 | bool | |
9343 | objc_diagnose_private_ivar (tree id) | |
9344 | { | |
9345 | tree ivar; | |
9346 | if (!objc_method_context) | |
9347 | return false; | |
9348 | ivar = is_ivar (objc_ivar_chain, id); | |
9349 | if (ivar && is_private (ivar)) | |
9350 | { | |
6bbb4715 | 9351 | error ("instance variable %qs is declared private", |
267785bc | 9352 | IDENTIFIER_POINTER (id)); |
9353 | return true; | |
9354 | } | |
9355 | return false; | |
9356 | } | |
fc304191 | 9357 | |
267785bc | 9358 | /* Look up ID as an instance variable. OTHER contains the result of |
9359 | the C or C++ lookup, which we may want to use instead. */ | |
9360 | /* To use properties inside an instance method, use self.property. */ | |
9361 | tree | |
9362 | objc_lookup_ivar (tree other, tree id) | |
9363 | { | |
9364 | tree ivar; | |
fc304191 | 9365 | |
267785bc | 9366 | /* If we are not inside of an ObjC method, ivar lookup makes no sense. */ |
9367 | if (!objc_method_context) | |
9368 | return other; | |
fc304191 | 9369 | |
267785bc | 9370 | if (!strcmp (IDENTIFIER_POINTER (id), "super")) |
9371 | /* We have a message to super. */ | |
9372 | return get_super_receiver (); | |
fc304191 | 9373 | |
267785bc | 9374 | /* In a class method, look up an instance variable only as a last |
9375 | resort. */ | |
9376 | if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL | |
9377 | && other && other != error_mark_node) | |
9378 | return other; | |
9379 | ||
06511efd | 9380 | /* Don't look up the ivar if the user has explicitly advised against |
9381 | it with -fno-local-ivars. */ | |
9382 | ||
9383 | if (!flag_local_ivars) | |
9384 | return other; | |
9385 | ||
267785bc | 9386 | /* Look up the ivar, but do not use it if it is not accessible. */ |
9387 | ivar = is_ivar (objc_ivar_chain, id); | |
6bbb4715 | 9388 | |
267785bc | 9389 | if (!ivar || is_private (ivar)) |
9390 | return other; | |
9391 | ||
9392 | /* In an instance method, a local variable (or parameter) may hide the | |
9393 | instance variable. */ | |
9394 | if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL | |
9395 | && other && other != error_mark_node | |
13dcc150 | 9396 | #ifdef OBJCPLUS |
267785bc | 9397 | && CP_DECL_CONTEXT (other) != global_namespace) |
9398 | #else | |
9399 | && !DECL_FILE_SCOPE_P (other)) | |
9400 | #endif | |
9401 | { | |
06511efd | 9402 | if (warn_shadow_ivar == 1 || (warn_shadow && warn_shadow_ivar != 0)) { |
9403 | warning (warn_shadow_ivar ? OPT_Wshadow_ivar : OPT_Wshadow, | |
9404 | "local declaration of %qE hides instance variable", id); | |
9405 | } | |
9406 | ||
267785bc | 9407 | return other; |
fc304191 | 9408 | } |
fc304191 | 9409 | |
267785bc | 9410 | /* At this point, we are either in an instance method with no obscuring |
9411 | local definitions, or in a class method with no alternate definitions | |
9412 | at all. */ | |
9413 | return build_ivar_reference (id); | |
86c110ac | 9414 | } |
9415 | ||
267785bc | 9416 | /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This |
9417 | needs to be done if we are calling a function through a cast. */ | |
9418 | ||
9419 | tree | |
9420 | objc_rewrite_function_call (tree function, tree first_param) | |
7fd68cee | 9421 | { |
267785bc | 9422 | if (TREE_CODE (function) == NOP_EXPR |
9423 | && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR | |
9424 | && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0)) | |
9425 | == FUNCTION_DECL) | |
9426 | { | |
9427 | function = build3 (OBJ_TYPE_REF, TREE_TYPE (function), | |
9428 | TREE_OPERAND (function, 0), | |
9429 | first_param, size_zero_node); | |
9430 | } | |
7fd68cee | 9431 | |
267785bc | 9432 | return function; |
9433 | } | |
7fd68cee | 9434 | |
267785bc | 9435 | /* This is called to "gimplify" a PROPERTY_REF node. It builds the |
9436 | corresponding 'getter' function call. Note that we assume the | |
9437 | PROPERTY_REF to be valid since we generated it while parsing. */ | |
9438 | static void | |
9439 | objc_gimplify_property_ref (tree *expr_p) | |
9440 | { | |
9441 | tree getter = PROPERTY_REF_GETTER_CALL (*expr_p); | |
9442 | tree call_exp; | |
7fd68cee | 9443 | |
267785bc | 9444 | if (getter == NULL_TREE) |
7fd68cee | 9445 | { |
267785bc | 9446 | tree property_decl = PROPERTY_REF_PROPERTY_DECL (*expr_p); |
9447 | /* This can happen if DECL_ARTIFICIAL (*expr_p), but | |
9448 | should be impossible for real properties, which always | |
9449 | have a getter. */ | |
9450 | error_at (EXPR_LOCATION (*expr_p), "no %qs getter found", | |
9451 | IDENTIFIER_POINTER (PROPERTY_NAME (property_decl))); | |
9452 | /* Try to recover from the error to prevent an ICE. We take | |
9453 | zero and cast it to the type of the property. */ | |
9454 | *expr_p = convert (TREE_TYPE (property_decl), | |
9455 | integer_zero_node); | |
9456 | return; | |
7fd68cee | 9457 | } |
7fd68cee | 9458 | |
267785bc | 9459 | if (PROPERTY_REF_DEPRECATED_GETTER (*expr_p)) |
9460 | { | |
9461 | /* PROPERTY_REF_DEPRECATED_GETTER contains the method prototype | |
9462 | that is deprecated. */ | |
9463 | warn_deprecated_use (PROPERTY_REF_DEPRECATED_GETTER (*expr_p), | |
9464 | NULL_TREE); | |
7fd68cee | 9465 | } |
7fd68cee | 9466 | |
267785bc | 9467 | call_exp = getter; |
9468 | #ifdef OBJCPLUS | |
9469 | /* In C++, a getter which returns an aggregate value results in a | |
9470 | target_expr which initializes a temporary to the call | |
9471 | expression. */ | |
9472 | if (TREE_CODE (getter) == TARGET_EXPR) | |
7fd68cee | 9473 | { |
267785bc | 9474 | gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter))); |
9475 | gcc_assert (TREE_CODE (TREE_OPERAND (getter, 0)) == VAR_DECL); | |
9476 | call_exp = TREE_OPERAND (getter, 1); | |
7fd68cee | 9477 | } |
267785bc | 9478 | #endif |
9479 | gcc_assert (TREE_CODE (call_exp) == CALL_EXPR); | |
6bbb4715 | 9480 | |
267785bc | 9481 | *expr_p = call_exp; |
7fd68cee | 9482 | } |
9483 | ||
267785bc | 9484 | /* This is called when "gimplifying" the trees. We need to gimplify |
9485 | the Objective-C/Objective-C++ specific trees, then hand over the | |
9486 | process to C/C++. */ | |
9487 | int | |
9488 | objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p) | |
86c110ac | 9489 | { |
267785bc | 9490 | enum tree_code code = TREE_CODE (*expr_p); |
9491 | switch (code) | |
7fd68cee | 9492 | { |
267785bc | 9493 | /* Look for the special case of OBJC_TYPE_REF with the address |
9494 | of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend | |
9495 | or one of its cousins). */ | |
9496 | case OBJ_TYPE_REF: | |
9497 | if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR | |
9498 | && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0)) | |
9499 | == FUNCTION_DECL) | |
7fd68cee | 9500 | { |
267785bc | 9501 | enum gimplify_status r0, r1; |
7fd68cee | 9502 | |
267785bc | 9503 | /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the |
9504 | value of the OBJ_TYPE_REF, so force them to be emitted | |
9505 | during subexpression evaluation rather than after the | |
9506 | OBJ_TYPE_REF. This permits objc_msgSend calls in | |
9507 | Objective C to use direct rather than indirect calls when | |
9508 | the object expression has a postincrement. */ | |
9509 | r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL, | |
9510 | is_gimple_val, fb_rvalue); | |
9511 | r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p, | |
9512 | is_gimple_val, fb_rvalue); | |
6bbb4715 | 9513 | |
267785bc | 9514 | return MIN (r0, r1); |
7fd68cee | 9515 | } |
9516 | break; | |
267785bc | 9517 | case PROPERTY_REF: |
9518 | objc_gimplify_property_ref (expr_p); | |
9519 | /* Do not return yet; let C/C++ gimplify the resulting expression. */ | |
9520 | break; | |
7fd68cee | 9521 | default: |
267785bc | 9522 | break; |
7fd68cee | 9523 | } |
9d9f5bb3 | 9524 | |
86c110ac | 9525 | #ifdef OBJCPLUS |
267785bc | 9526 | return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p); |
86c110ac | 9527 | #else |
267785bc | 9528 | return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p); |
86c110ac | 9529 | #endif |
86c110ac | 9530 | } |
9531 | ||
267785bc | 9532 | /* --- FAST ENUMERATION --- */ |
9533 | /* Begin code generation for fast enumeration (foreach) ... */ | |
86c110ac | 9534 | |
267785bc | 9535 | /* Defines |
86c110ac | 9536 | |
267785bc | 9537 | struct __objcFastEnumerationState |
9538 | { | |
9539 | unsigned long state; | |
9540 | id *itemsPtr; | |
9541 | unsigned long *mutationsPtr; | |
9542 | unsigned long extra[5]; | |
9543 | }; | |
86c110ac | 9544 | |
267785bc | 9545 | Confusingly enough, NSFastEnumeration is then defined by libraries |
6bbb4715 | 9546 | to be the same structure. |
267785bc | 9547 | */ |
86c110ac | 9548 | |
267785bc | 9549 | static void |
9550 | build_fast_enumeration_state_template (void) | |
9551 | { | |
9552 | tree decls, *chain = NULL; | |
9d9f5bb3 | 9553 | |
267785bc | 9554 | /* { */ |
6bbb4715 | 9555 | objc_fast_enumeration_state_template = objc_start_struct (get_identifier |
267785bc | 9556 | (TAG_FAST_ENUMERATION_STATE)); |
86c110ac | 9557 | |
267785bc | 9558 | /* unsigned long state; */ |
9559 | decls = add_field_decl (long_unsigned_type_node, "state", &chain); | |
9d9f5bb3 | 9560 | |
267785bc | 9561 | /* id *itemsPtr; */ |
6bbb4715 | 9562 | add_field_decl (build_pointer_type (objc_object_type), |
267785bc | 9563 | "itemsPtr", &chain); |
9d9f5bb3 | 9564 | |
267785bc | 9565 | /* unsigned long *mutationsPtr; */ |
6bbb4715 | 9566 | add_field_decl (build_pointer_type (long_unsigned_type_node), |
267785bc | 9567 | "mutationsPtr", &chain); |
7fd68cee | 9568 | |
267785bc | 9569 | /* unsigned long extra[5]; */ |
6bbb4715 | 9570 | add_field_decl (build_sized_array_type (long_unsigned_type_node, 5), |
267785bc | 9571 | "extra", &chain); |
7fd68cee | 9572 | |
267785bc | 9573 | /* } */ |
9574 | objc_finish_struct (objc_fast_enumeration_state_template, decls); | |
9575 | } | |
7fd68cee | 9576 | |
267785bc | 9577 | /* |
9578 | 'objc_finish_foreach_loop()' generates the code for an Objective-C | |
9579 | foreach loop. The 'location' argument is the location of the 'for' | |
9580 | that starts the loop. The 'object_expression' is the expression of | |
9581 | the 'object' that iterates; the 'collection_expression' is the | |
9582 | expression of the collection that we iterate over (we need to make | |
9583 | sure we evaluate this only once); the 'for_body' is the set of | |
9584 | statements to be executed in each iteration; 'break_label' and | |
9585 | 'continue_label' are the break and continue labels which we need to | |
9586 | emit since the <statements> may be jumping to 'break_label' (if they | |
9587 | contain 'break') or to 'continue_label' (if they contain | |
9588 | 'continue'). | |
7fd68cee | 9589 | |
267785bc | 9590 | The syntax is |
6bbb4715 | 9591 | |
267785bc | 9592 | for (<object expression> in <collection expression>) |
9593 | <statements> | |
7fd68cee | 9594 | |
267785bc | 9595 | which is compiled into the following blurb: |
9596 | ||
9597 | { | |
9598 | id __objc_foreach_collection; | |
9599 | __objc_fast_enumeration_state __objc_foreach_enum_state; | |
9600 | unsigned long __objc_foreach_batchsize; | |
9601 | id __objc_foreach_items[16]; | |
9602 | __objc_foreach_collection = <collection expression>; | |
9603 | __objc_foreach_enum_state = { 0 }; | |
9604 | __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; | |
6bbb4715 | 9605 | |
267785bc | 9606 | if (__objc_foreach_batchsize == 0) |
9607 | <object expression> = nil; | |
9608 | else | |
9609 | { | |
9610 | unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; | |
9611 | next_batch: | |
7fd68cee | 9612 | { |
267785bc | 9613 | unsigned long __objc_foreach_index; |
9614 | __objc_foreach_index = 0; | |
7fd68cee | 9615 | |
267785bc | 9616 | next_object: |
9617 | if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); | |
9618 | <object expression> = enumState.itemsPtr[__objc_foreach_index]; | |
9619 | <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] | |
7fd68cee | 9620 | |
267785bc | 9621 | continue_label: |
9622 | __objc_foreach_index++; | |
9623 | if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; | |
9624 | __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; | |
9625 | } | |
9626 | if (__objc_foreach_batchsize != 0) goto next_batch; | |
9627 | <object expression> = nil; | |
9628 | break_label: | |
7fd68cee | 9629 | } |
267785bc | 9630 | } |
7fd68cee | 9631 | |
267785bc | 9632 | 'statements' may contain a 'continue' or 'break' instruction, which |
9633 | the user expects to 'continue' or 'break' the entire foreach loop. | |
9634 | We are provided the labels that 'break' and 'continue' jump to, so | |
9635 | we place them where we want them to jump to when they pick them. | |
6bbb4715 | 9636 | |
267785bc | 9637 | Optimization TODO: we could cache the IMP of |
9638 | countByEnumeratingWithState:objects:count:. | |
9639 | */ | |
7fd68cee | 9640 | |
267785bc | 9641 | /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */ |
9642 | /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */ | |
7fd68cee | 9643 | |
267785bc | 9644 | #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP |
9645 | #include "tree-pretty-print.h" | |
86c110ac | 9646 | #endif |
86c110ac | 9647 | |
267785bc | 9648 | void |
6bbb4715 | 9649 | objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body, |
267785bc | 9650 | tree break_label, tree continue_label) |
9d9f5bb3 | 9651 | { |
267785bc | 9652 | /* A tree representing the __objcFastEnumerationState struct type, |
9653 | or NSFastEnumerationState struct, whatever we are using. */ | |
9654 | tree objc_fast_enumeration_state_type; | |
9d9f5bb3 | 9655 | |
267785bc | 9656 | /* The trees representing the declarations of each of the local variables. */ |
9657 | tree objc_foreach_collection_decl; | |
9658 | tree objc_foreach_enum_state_decl; | |
9659 | tree objc_foreach_items_decl; | |
9660 | tree objc_foreach_batchsize_decl; | |
9661 | tree objc_foreach_mutations_pointer_decl; | |
9662 | tree objc_foreach_index_decl; | |
9d9f5bb3 | 9663 | |
267785bc | 9664 | /* A tree representing the selector countByEnumeratingWithState:objects:count:. */ |
9665 | tree selector_name; | |
9d9f5bb3 | 9666 | |
267785bc | 9667 | /* A tree representing the local bind. */ |
9668 | tree bind; | |
9d9f5bb3 | 9669 | |
267785bc | 9670 | /* A tree representing the external 'if (__objc_foreach_batchsize)' */ |
9671 | tree first_if; | |
9d9f5bb3 | 9672 | |
267785bc | 9673 | /* A tree representing the 'else' part of 'first_if' */ |
9674 | tree first_else; | |
9d9f5bb3 | 9675 | |
267785bc | 9676 | /* A tree representing the 'next_batch' label. */ |
9677 | tree next_batch_label_decl; | |
9d9f5bb3 | 9678 | |
267785bc | 9679 | /* A tree representing the binding after the 'next_batch' label. */ |
9680 | tree next_batch_bind; | |
9d9f5bb3 | 9681 | |
267785bc | 9682 | /* A tree representing the 'next_object' label. */ |
9683 | tree next_object_label_decl; | |
9d9f5bb3 | 9684 | |
267785bc | 9685 | /* Temporary variables. */ |
9686 | tree t; | |
9687 | int i; | |
9d9f5bb3 | 9688 | |
1ef143b6 | 9689 | if (flag_objc1_only) |
267785bc | 9690 | error_at (location, "fast enumeration is not available in Objective-C 1.0"); |
1ef143b6 | 9691 | |
267785bc | 9692 | if (object_expression == error_mark_node) |
e1f293c0 | 9693 | return; |
9694 | ||
267785bc | 9695 | if (collection_expression == error_mark_node) |
9696 | return; | |
9d9f5bb3 | 9697 | |
267785bc | 9698 | if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression), true)) |
9d9f5bb3 | 9699 | { |
267785bc | 9700 | error_at (location, "iterating variable in fast enumeration is not an object"); |
9d9f5bb3 | 9701 | return; |
9702 | } | |
9703 | ||
267785bc | 9704 | if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression), true)) |
9d9f5bb3 | 9705 | { |
267785bc | 9706 | error_at (location, "collection in fast enumeration is not an object"); |
9d9f5bb3 | 9707 | return; |
9708 | } | |
9709 | ||
267785bc | 9710 | /* TODO: Check that object_expression is either a variable |
9711 | declaration, or an lvalue. */ | |
9d9f5bb3 | 9712 | |
267785bc | 9713 | /* This kludge is an idea from apple. We use the |
9714 | __objcFastEnumerationState struct implicitly defined by the | |
9715 | compiler, unless a NSFastEnumerationState struct has been defined | |
9716 | (by a Foundation library such as GNUstep Base) in which case, we | |
9717 | use that one. | |
9718 | */ | |
9719 | objc_fast_enumeration_state_type = objc_fast_enumeration_state_template; | |
9720 | { | |
9721 | tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState")); | |
9d9f5bb3 | 9722 | |
267785bc | 9723 | if (objc_NSFastEnumeration_type) |
9d9f5bb3 | 9724 | { |
267785bc | 9725 | /* TODO: We really need to check that |
9726 | objc_NSFastEnumeration_type is the same as ours! */ | |
9727 | if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL) | |
9728 | { | |
9729 | /* If it's a typedef, use the original type. */ | |
9730 | if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type)) | |
9731 | objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type); | |
9732 | else | |
6bbb4715 | 9733 | objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type); |
267785bc | 9734 | } |
9d9f5bb3 | 9735 | } |
267785bc | 9736 | } |
9d9f5bb3 | 9737 | |
267785bc | 9738 | /* { */ |
9739 | /* Done by c-parser.c. */ | |
9d9f5bb3 | 9740 | |
267785bc | 9741 | /* type object; */ |
9742 | /* Done by c-parser.c. */ | |
e1f293c0 | 9743 | |
267785bc | 9744 | /* Disable warnings that 'object' is unused. For example the code |
9d9f5bb3 | 9745 | |
267785bc | 9746 | for (id object in collection) |
9747 | i++; | |
e1f293c0 | 9748 | |
267785bc | 9749 | which can be used to count how many objects there are in the |
9750 | collection is fine and should generate no warnings even if | |
9751 | 'object' is technically unused. */ | |
9752 | TREE_USED (object_expression) = 1; | |
9753 | if (DECL_P (object_expression)) | |
9754 | DECL_READ_P (object_expression) = 1; | |
9d9f5bb3 | 9755 | |
267785bc | 9756 | /* id __objc_foreach_collection */ |
9757 | objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection"); | |
1ef143b6 | 9758 | |
267785bc | 9759 | /* __objcFastEnumerationState __objc_foreach_enum_state; */ |
9760 | objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state"); | |
9761 | TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl; | |
e1f293c0 | 9762 | |
267785bc | 9763 | /* id __objc_foreach_items[16]; */ |
9764 | objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items"); | |
9765 | TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl; | |
e1f293c0 | 9766 | |
267785bc | 9767 | /* unsigned long __objc_foreach_batchsize; */ |
9768 | objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize"); | |
9769 | TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl; | |
f26877d5 | 9770 | |
267785bc | 9771 | /* Generate the local variable binding. */ |
9772 | bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL); | |
9773 | SET_EXPR_LOCATION (bind, location); | |
9774 | TREE_SIDE_EFFECTS (bind) = 1; | |
6bbb4715 | 9775 | |
267785bc | 9776 | /* __objc_foreach_collection = <collection expression>; */ |
9777 | t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression); | |
9778 | SET_EXPR_LOCATION (t, location); | |
9779 | append_to_statement_list (t, &BIND_EXPR_BODY (bind)); | |
9780 | /* We have used 'collection_expression'. */ | |
9781 | mark_exp_read (collection_expression); | |
9d9f5bb3 | 9782 | |
267785bc | 9783 | /* __objc_foreach_enum_state.state = 0; */ |
6bbb4715 | 9784 | t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl, |
267785bc | 9785 | get_identifier ("state")), |
9786 | build_int_cst (long_unsigned_type_node, 0)); | |
9787 | SET_EXPR_LOCATION (t, location); | |
9788 | append_to_statement_list (t, &BIND_EXPR_BODY (bind)); | |
9789 | ||
9790 | /* __objc_foreach_enum_state.itemsPtr = NULL; */ | |
6bbb4715 | 9791 | t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl, |
267785bc | 9792 | get_identifier ("itemsPtr")), |
9793 | null_pointer_node); | |
9794 | SET_EXPR_LOCATION (t, location); | |
9795 | append_to_statement_list (t, &BIND_EXPR_BODY (bind)); | |
9796 | ||
9797 | /* __objc_foreach_enum_state.mutationsPtr = NULL; */ | |
6bbb4715 | 9798 | t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl, |
267785bc | 9799 | get_identifier ("mutationsPtr")), |
9800 | null_pointer_node); | |
9801 | SET_EXPR_LOCATION (t, location); | |
9802 | append_to_statement_list (t, &BIND_EXPR_BODY (bind)); | |
9803 | ||
9804 | /* __objc_foreach_enum_state.extra[0] = 0; */ | |
9805 | /* __objc_foreach_enum_state.extra[1] = 0; */ | |
9806 | /* __objc_foreach_enum_state.extra[2] = 0; */ | |
9807 | /* __objc_foreach_enum_state.extra[3] = 0; */ | |
9808 | /* __objc_foreach_enum_state.extra[4] = 0; */ | |
9809 | for (i = 0; i < 5 ; i++) | |
9d9f5bb3 | 9810 | { |
267785bc | 9811 | t = build2 (MODIFY_EXPR, void_type_node, |
6bbb4715 | 9812 | build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl, |
267785bc | 9813 | get_identifier ("extra")), |
9814 | build_int_cst (NULL_TREE, i)), | |
9815 | build_int_cst (long_unsigned_type_node, 0)); | |
9816 | SET_EXPR_LOCATION (t, location); | |
9817 | append_to_statement_list (t, &BIND_EXPR_BODY (bind)); | |
9d9f5bb3 | 9818 | } |
6bbb4715 | 9819 | |
267785bc | 9820 | /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */ |
9821 | selector_name = get_identifier ("countByEnumeratingWithState:objects:count:"); | |
9822 | #ifdef OBJCPLUS | |
9823 | t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name, | |
9824 | /* Parameters. */ | |
9825 | tree_cons /* &__objc_foreach_enum_state */ | |
9826 | (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl), | |
9827 | tree_cons /* __objc_foreach_items */ | |
9828 | (NULL_TREE, objc_foreach_items_decl, | |
9829 | tree_cons /* 16 */ | |
9830 | (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL); | |
9831 | #else | |
9832 | /* In C, we need to decay the __objc_foreach_items array that we are passing. */ | |
9833 | { | |
9834 | struct c_expr array; | |
9835 | array.value = objc_foreach_items_decl; | |
9836 | t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name, | |
9837 | /* Parameters. */ | |
9838 | tree_cons /* &__objc_foreach_enum_state */ | |
9839 | (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl), | |
9840 | tree_cons /* __objc_foreach_items */ | |
9841 | (NULL_TREE, default_function_array_conversion (location, array).value, | |
9842 | tree_cons /* 16 */ | |
9843 | (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL); | |
9844 | } | |
9845 | #endif | |
9846 | t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl, | |
9847 | convert (long_unsigned_type_node, t)); | |
9848 | SET_EXPR_LOCATION (t, location); | |
9849 | append_to_statement_list (t, &BIND_EXPR_BODY (bind)); | |
e1f293c0 | 9850 | |
267785bc | 9851 | /* if (__objc_foreach_batchsize == 0) */ |
6bbb4715 | 9852 | first_if = build3 (COND_EXPR, void_type_node, |
267785bc | 9853 | /* Condition. */ |
6bbb4715 | 9854 | c_fully_fold |
9855 | (c_common_truthvalue_conversion | |
9856 | (location, | |
267785bc | 9857 | build_binary_op (location, |
6bbb4715 | 9858 | EQ_EXPR, |
267785bc | 9859 | objc_foreach_batchsize_decl, |
9860 | build_int_cst (long_unsigned_type_node, 0), 1)), | |
9861 | false, NULL), | |
9862 | /* Then block (we fill it in later). */ | |
9863 | NULL_TREE, | |
9864 | /* Else block (we fill it in later). */ | |
9865 | NULL_TREE); | |
9866 | SET_EXPR_LOCATION (first_if, location); | |
9867 | append_to_statement_list (first_if, &BIND_EXPR_BODY (bind)); | |
86c110ac | 9868 | |
267785bc | 9869 | /* then <object expression> = nil; */ |
9870 | t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node)); | |
9871 | SET_EXPR_LOCATION (t, location); | |
9872 | COND_EXPR_THEN (first_if) = t; | |
86c110ac | 9873 | |
267785bc | 9874 | /* Now we build the 'else' part of the if; once we finish building |
9875 | it, we attach it to first_if as the 'else' part. */ | |
9d9f5bb3 | 9876 | |
267785bc | 9877 | /* else */ |
9878 | /* { */ | |
9d9f5bb3 | 9879 | |
267785bc | 9880 | /* unsigned long __objc_foreach_mutations_pointer; */ |
9881 | objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer"); | |
86c110ac | 9882 | |
267785bc | 9883 | /* Generate the local variable binding. */ |
9884 | first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL); | |
9885 | SET_EXPR_LOCATION (first_else, location); | |
9886 | TREE_SIDE_EFFECTS (first_else) = 1; | |
c450c529 | 9887 | |
267785bc | 9888 | /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */ |
6bbb4715 | 9889 | t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, |
9890 | build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl, | |
267785bc | 9891 | get_identifier ("mutationsPtr")), |
9892 | RO_UNARY_STAR)); | |
9893 | SET_EXPR_LOCATION (t, location); | |
9894 | append_to_statement_list (t, &BIND_EXPR_BODY (first_else)); | |
86c110ac | 9895 | |
267785bc | 9896 | /* next_batch: */ |
9897 | next_batch_label_decl = create_artificial_label (location); | |
6bbb4715 | 9898 | t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl); |
267785bc | 9899 | SET_EXPR_LOCATION (t, location); |
9900 | append_to_statement_list (t, &BIND_EXPR_BODY (first_else)); | |
6bbb4715 | 9901 | |
267785bc | 9902 | /* { */ |
29d8eac9 | 9903 | |
267785bc | 9904 | /* unsigned long __objc_foreach_index; */ |
9905 | objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index"); | |
7f5203a8 | 9906 | |
267785bc | 9907 | /* Generate the local variable binding. */ |
9908 | next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL); | |
9909 | SET_EXPR_LOCATION (next_batch_bind, location); | |
9910 | TREE_SIDE_EFFECTS (next_batch_bind) = 1; | |
9911 | append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else)); | |
0a65c3bb | 9912 | |
267785bc | 9913 | /* __objc_foreach_index = 0; */ |
9914 | t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl, | |
9915 | build_int_cst (long_unsigned_type_node, 0)); | |
9916 | SET_EXPR_LOCATION (t, location); | |
9917 | append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind)); | |
0a65c3bb | 9918 | |
267785bc | 9919 | /* next_object: */ |
9920 | next_object_label_decl = create_artificial_label (location); | |
9921 | t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl); | |
9922 | SET_EXPR_LOCATION (t, location); | |
9923 | append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind)); | |
0a65c3bb | 9924 | |
267785bc | 9925 | /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */ |
6bbb4715 | 9926 | t = build3 (COND_EXPR, void_type_node, |
267785bc | 9927 | /* Condition. */ |
6bbb4715 | 9928 | c_fully_fold |
9929 | (c_common_truthvalue_conversion | |
9930 | (location, | |
9931 | build_binary_op | |
267785bc | 9932 | (location, |
6bbb4715 | 9933 | NE_EXPR, |
267785bc | 9934 | objc_foreach_mutations_pointer_decl, |
6bbb4715 | 9935 | build_indirect_ref (location, |
9936 | objc_build_component_ref (objc_foreach_enum_state_decl, | |
267785bc | 9937 | get_identifier ("mutationsPtr")), |
9938 | RO_UNARY_STAR), 1)), | |
9939 | false, NULL), | |
9940 | /* Then block. */ | |
9941 | build_function_call (input_location, | |
9942 | objc_enumeration_mutation_decl, | |
9943 | tree_cons (NULL, collection_expression, NULL)), | |
9944 | /* Else block. */ | |
9945 | NULL_TREE); | |
9946 | SET_EXPR_LOCATION (t, location); | |
9947 | append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind)); | |
0a65c3bb | 9948 | |
267785bc | 9949 | /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */ |
6bbb4715 | 9950 | t = build2 (MODIFY_EXPR, void_type_node, object_expression, |
9951 | build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl, | |
267785bc | 9952 | get_identifier ("itemsPtr")), |
9953 | objc_foreach_index_decl)); | |
9954 | SET_EXPR_LOCATION (t, location); | |
9955 | append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind)); | |
0a65c3bb | 9956 | |
267785bc | 9957 | /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */ |
9958 | append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind)); | |
0a65c3bb | 9959 | |
267785bc | 9960 | /* continue_label: */ |
9961 | if (continue_label) | |
9962 | { | |
9963 | t = build1 (LABEL_EXPR, void_type_node, continue_label); | |
9964 | SET_EXPR_LOCATION (t, location); | |
9965 | append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind)); | |
9966 | } | |
0a65c3bb | 9967 | |
267785bc | 9968 | /* __objc_foreach_index++; */ |
6bbb4715 | 9969 | t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl, |
267785bc | 9970 | build_binary_op (location, |
9971 | PLUS_EXPR, | |
9972 | objc_foreach_index_decl, | |
9973 | build_int_cst (long_unsigned_type_node, 1), 1)); | |
9974 | SET_EXPR_LOCATION (t, location); | |
9975 | append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind)); | |
0a65c3bb | 9976 | |
267785bc | 9977 | /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */ |
6bbb4715 | 9978 | t = build3 (COND_EXPR, void_type_node, |
267785bc | 9979 | /* Condition. */ |
6bbb4715 | 9980 | c_fully_fold |
9981 | (c_common_truthvalue_conversion | |
9982 | (location, | |
267785bc | 9983 | build_binary_op (location, |
6bbb4715 | 9984 | LT_EXPR, |
267785bc | 9985 | objc_foreach_index_decl, |
9986 | objc_foreach_batchsize_decl, 1)), | |
9987 | false, NULL), | |
9988 | /* Then block. */ | |
9989 | build1 (GOTO_EXPR, void_type_node, next_object_label_decl), | |
9990 | /* Else block. */ | |
9991 | NULL_TREE); | |
9992 | SET_EXPR_LOCATION (t, location); | |
9993 | append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind)); | |
6bbb4715 | 9994 | |
267785bc | 9995 | /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */ |
9996 | #ifdef OBJCPLUS | |
9997 | t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name, | |
9998 | /* Parameters. */ | |
9999 | tree_cons /* &__objc_foreach_enum_state */ | |
10000 | (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl), | |
10001 | tree_cons /* __objc_foreach_items */ | |
10002 | (NULL_TREE, objc_foreach_items_decl, | |
10003 | tree_cons /* 16 */ | |
10004 | (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL); | |
10005 | #else | |
10006 | /* In C, we need to decay the __objc_foreach_items array that we are passing. */ | |
0a65c3bb | 10007 | { |
267785bc | 10008 | struct c_expr array; |
10009 | array.value = objc_foreach_items_decl; | |
10010 | t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name, | |
10011 | /* Parameters. */ | |
10012 | tree_cons /* &__objc_foreach_enum_state */ | |
10013 | (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl), | |
10014 | tree_cons /* __objc_foreach_items */ | |
10015 | (NULL_TREE, default_function_array_conversion (location, array).value, | |
10016 | tree_cons /* 16 */ | |
10017 | (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL); | |
10018 | } | |
10019 | #endif | |
6bbb4715 | 10020 | t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl, |
267785bc | 10021 | convert (long_unsigned_type_node, t)); |
10022 | SET_EXPR_LOCATION (t, location); | |
10023 | append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind)); | |
0a65c3bb | 10024 | |
267785bc | 10025 | /* } */ |
0a65c3bb | 10026 | |
267785bc | 10027 | /* if (__objc_foreach_batchsize != 0) goto next_batch; */ |
6bbb4715 | 10028 | t = build3 (COND_EXPR, void_type_node, |
267785bc | 10029 | /* Condition. */ |
6bbb4715 | 10030 | c_fully_fold |
10031 | (c_common_truthvalue_conversion | |
10032 | (location, | |
267785bc | 10033 | build_binary_op (location, |
6bbb4715 | 10034 | NE_EXPR, |
267785bc | 10035 | objc_foreach_batchsize_decl, |
10036 | build_int_cst (long_unsigned_type_node, 0), 1)), | |
10037 | false, NULL), | |
10038 | /* Then block. */ | |
10039 | build1 (GOTO_EXPR, void_type_node, next_batch_label_decl), | |
10040 | /* Else block. */ | |
10041 | NULL_TREE); | |
10042 | SET_EXPR_LOCATION (t, location); | |
10043 | append_to_statement_list (t, &BIND_EXPR_BODY (first_else)); | |
0a65c3bb | 10044 | |
267785bc | 10045 | /* <object expression> = nil; */ |
10046 | t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node)); | |
10047 | SET_EXPR_LOCATION (t, location); | |
10048 | append_to_statement_list (t, &BIND_EXPR_BODY (first_else)); | |
10049 | ||
10050 | /* break_label: */ | |
10051 | if (break_label) | |
10052 | { | |
10053 | t = build1 (LABEL_EXPR, void_type_node, break_label); | |
10054 | SET_EXPR_LOCATION (t, location); | |
10055 | append_to_statement_list (t, &BIND_EXPR_BODY (first_else)); | |
10056 | } | |
0a65c3bb | 10057 | |
267785bc | 10058 | /* } */ |
10059 | COND_EXPR_ELSE (first_if) = first_else; | |
10060 | ||
10061 | /* Do the whole thing. */ | |
10062 | add_stmt (bind); | |
0a65c3bb | 10063 | |
10064 | #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP | |
267785bc | 10065 | /* This will print to stderr the whole blurb generated by the |
10066 | compiler while compiling (assuming the compiler doesn't crash | |
10067 | before getting here). | |
10068 | */ | |
10069 | debug_generic_stmt (bind); | |
0a65c3bb | 10070 | #endif |
10071 | ||
267785bc | 10072 | /* } */ |
10073 | /* Done by c-parser.c */ | |
10074 | } | |
10075 | ||
10076 | /* --- SUPPORT FOR FORMAT ARG CHECKING --- */ | |
10077 | /* Return true if we have an NxString object pointer. */ | |
10078 | ||
10079 | bool | |
10080 | objc_string_ref_type_p (tree strp) | |
10081 | { | |
10082 | tree tmv; | |
10083 | if (!strp || TREE_CODE (strp) != POINTER_TYPE) | |
10084 | return false; | |
10085 | ||
10086 | tmv = TYPE_MAIN_VARIANT (TREE_TYPE (strp)); | |
10087 | tmv = OBJC_TYPE_NAME (tmv); | |
10088 | return (tmv | |
6bbb4715 | 10089 | && TREE_CODE (tmv) == IDENTIFIER_NODE |
10090 | && IDENTIFIER_POINTER (tmv) | |
267785bc | 10091 | && !strncmp (IDENTIFIER_POINTER (tmv), "NSString", 8)); |
10092 | } | |
10093 | ||
10094 | /* At present the behavior of this is undefined and it does nothing. */ | |
0a65c3bb | 10095 | void |
6bbb4715 | 10096 | objc_check_format_arg (tree ARG_UNUSED (format_arg), |
267785bc | 10097 | tree ARG_UNUSED (args_list)) |
0a65c3bb | 10098 | { |
267785bc | 10099 | } |
0a65c3bb | 10100 | |
9b88d08d | 10101 | void |
10102 | objc_common_init_ts (void) | |
10103 | { | |
10104 | c_common_init_ts (); | |
10105 | ||
10106 | MARK_TS_DECL_NON_COMMON (CLASS_METHOD_DECL); | |
10107 | MARK_TS_DECL_NON_COMMON (INSTANCE_METHOD_DECL); | |
10108 | MARK_TS_DECL_NON_COMMON (KEYWORD_DECL); | |
10109 | MARK_TS_DECL_NON_COMMON (PROPERTY_DECL); | |
10110 | ||
10111 | MARK_TS_COMMON (CLASS_INTERFACE_TYPE); | |
10112 | MARK_TS_COMMON (PROTOCOL_INTERFACE_TYPE); | |
10113 | MARK_TS_COMMON (CLASS_IMPLEMENTATION_TYPE); | |
10114 | ||
10115 | MARK_TS_TYPED (MESSAGE_SEND_EXPR); | |
10116 | MARK_TS_TYPED (PROPERTY_REF); | |
10117 | } | |
10118 | ||
83c7dcd8 | 10119 | size_t |
10120 | objc_common_tree_size (enum tree_code code) | |
10121 | { | |
10122 | switch (code) | |
10123 | { | |
10124 | case CLASS_METHOD_DECL: | |
10125 | case INSTANCE_METHOD_DECL: | |
10126 | case KEYWORD_DECL: | |
10127 | case PROPERTY_DECL: | |
10128 | return sizeof (struct tree_decl_non_common); | |
10129 | default: | |
10130 | gcc_unreachable (); | |
10131 | ||
10132 | } | |
10133 | } | |
10134 | ||
10135 | ||
6090deb8 | 10136 | #include "gt-objc-objc-act.h" |