]>
Commit | Line | Data |
---|---|---|
6c65299b | 1 | /* Implement classes and message passing for Objective C. |
23a5b65a | 2 | Copyright (C) 1992-2014 Free Software Foundation, Inc. |
4cb8c14b | 3 | Contributed by Steve Naroff. |
6c65299b | 4 | |
b9593599 | 5 | This file is part of GCC. |
6c65299b | 6 | |
b9593599 | 7 | GCC is free software; you can redistribute it and/or modify |
6c65299b | 8 | it under the terms of the GNU General Public License as published by |
2b7d992a | 9 | the Free Software Foundation; either version 3, or (at your option) |
6c65299b RS |
10 | any later version. |
11 | ||
b9593599 | 12 | GCC is distributed in the hope that it will be useful, |
6c65299b RS |
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 | |
2b7d992a NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
20 | ||
6c65299b | 21 | #include "config.h" |
90fbb8c9 | 22 | #include "system.h" |
4977bab6 ZW |
23 | #include "coretypes.h" |
24 | #include "tm.h" | |
6c65299b | 25 | #include "tree.h" |
d8a2d370 DN |
26 | #include "stringpool.h" |
27 | #include "stor-layout.h" | |
28 | #include "attribs.h" | |
6408ef33 ZL |
29 | |
30 | #ifdef OBJCPLUS | |
d4a10d0a | 31 | #include "cp/cp-tree.h" |
6408ef33 | 32 | #else |
d4a10d0a SB |
33 | #include "c/c-tree.h" |
34 | #include "c/c-lang.h" | |
6408ef33 ZL |
35 | #endif |
36 | ||
39dabefd | 37 | #include "c-family/c-common.h" |
61d3ce20 | 38 | #include "c-family/c-objc.h" |
39dabefd | 39 | #include "c-family/c-pragma.h" |
91ebb981 | 40 | #include "c-family/c-format.h" |
6c65299b | 41 | #include "flags.h" |
e57e265b | 42 | #include "langhooks.h" |
e58232cd | 43 | #include "objc-act.h" |
3cc2dd4b | 44 | #include "objc-map.h" |
6c65299b | 45 | #include "input.h" |
e31c7eec | 46 | #include "function.h" |
d6f4ec51 | 47 | #include "toplev.h" |
9099cc4f | 48 | #include "debug.h" |
acce4e77 | 49 | #include "c-family/c-target.h" |
1da2ed5f | 50 | #include "diagnostic-core.h" |
f41c4af3 | 51 | #include "intl.h" |
0d446150 | 52 | #include "cgraph.h" |
093c7153 | 53 | #include "tree-iterator.h" |
703c8606 | 54 | #include "hash-table.h" |
807e902e | 55 | #include "wide-int.h" |
3f16185f | 56 | #include "langhooks-def.h" |
d764a8e6 IS |
57 | /* Different initialization, code gen and meta data generation for each |
58 | runtime. */ | |
59 | #include "objc-runtime-hooks.h" | |
60 | /* Routines used mainly by the runtimes. */ | |
61 | #include "objc-runtime-shared-support.h" | |
a1178b30 IS |
62 | /* For default_tree_printer (). */ |
63 | #include "tree-pretty-print.h" | |
64 | ||
245763e3 | 65 | /* For enum gimplify_status */ |
2fb9a547 | 66 | #include "gimple-expr.h" |
45b0be94 | 67 | #include "gimplify.h" |
245763e3 | 68 | |
f027ee7c NP |
69 | /* For encode_method_prototype(). */ |
70 | #include "objc-encoding.h" | |
71 | ||
38b9c8c3 ZL |
72 | static unsigned int should_call_super_dealloc = 0; |
73 | ||
6408ef33 ZL |
74 | /* When building Objective-C++, we are not linking against the C front-end |
75 | and so need to replicate the C tree-construction functions in some way. */ | |
76 | #ifdef OBJCPLUS | |
77 | #define OBJCP_REMAP_FUNCTIONS | |
78 | #include "objcp-decl.h" | |
79 | #endif /* OBJCPLUS */ | |
cff559bc | 80 | |
187e4b0f | 81 | /* This is the default way of generating a method name. */ |
b8a18805 NP |
82 | /* This has the problem that "test_method:argument:" and |
83 | "test:method_argument:" will generate the same name | |
84 | ("_i_Test__test_method_argument_" for an instance method of the | |
85 | class "Test"), so you can't have them both in the same class! | |
86 | Moreover, the demangling (going from | |
87 | "_i_Test__test_method_argument" back to the original name) is | |
88 | undefined because there are two correct ways of demangling the | |
89 | name. */ | |
187e4b0f | 90 | #ifndef OBJC_GEN_METHOD_LABEL |
b54b3fb0 RS |
91 | #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \ |
92 | do { \ | |
93 | char *temp; \ | |
94 | sprintf ((BUF), "_%s_%s_%s_%s", \ | |
95 | ((IS_INST) ? "i" : "c"), \ | |
96 | (CLASS_NAME), \ | |
97 | ((CAT_NAME)? (CAT_NAME) : ""), \ | |
98 | (SEL_NAME)); \ | |
99 | for (temp = (BUF); *temp; temp++) \ | |
100 | if (*temp == ':') *temp = '_'; \ | |
187e4b0f | 101 | } while (0) |
152551c6 | 102 | #endif |
e31c7eec TW |
103 | |
104 | /* These need specifying. */ | |
105 | #ifndef OBJC_FORWARDING_STACK_OFFSET | |
106 | #define OBJC_FORWARDING_STACK_OFFSET 0 | |
107 | #endif | |
108 | ||
109 | #ifndef OBJC_FORWARDING_MIN_OFFSET | |
110 | #define OBJC_FORWARDING_MIN_OFFSET 0 | |
111 | #endif | |
d764a8e6 | 112 | |
6c65299b RS |
113 | /*** Private Interface (procedures) ***/ |
114 | ||
d764a8e6 IS |
115 | /* Init stuff. */ |
116 | static void synth_module_prologue (void); | |
e31c7eec | 117 | |
0f41302f | 118 | /* Code generation. */ |
6c65299b | 119 | |
d4e72c58 | 120 | static tree start_class (enum tree_code, tree, tree, tree, tree); |
6408ef33 ZL |
121 | static tree continue_class (tree); |
122 | static void finish_class (tree); | |
a04a722b | 123 | static void start_method_def (tree, tree); |
d764a8e6 | 124 | |
6b192a09 | 125 | static tree start_protocol (enum tree_code, tree, tree, tree); |
dbb74365 | 126 | static tree build_method_decl (enum tree_code, tree, tree, tree, bool); |
92902b1b | 127 | static tree objc_add_method (tree, tree, int, bool); |
c37d8c30 | 128 | static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree); |
6408ef33 ZL |
129 | static tree build_ivar_reference (tree); |
130 | static tree is_ivar (tree, tree); | |
6408ef33 | 131 | |
d11dd684 ZL |
132 | /* We only need the following for ObjC; ObjC++ will use C++'s definition |
133 | of DERIVED_FROM_P. */ | |
134 | #ifndef OBJCPLUS | |
135 | static bool objc_derived_from_p (tree, tree); | |
136 | #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD) | |
137 | #endif | |
668ea4b1 IS |
138 | |
139 | /* Property. */ | |
668ea4b1 IS |
140 | static void objc_gen_property_data (tree, tree); |
141 | static void objc_synthesize_getter (tree, tree, tree); | |
668ea4b1 | 142 | static void objc_synthesize_setter (tree, tree, tree); |
668ea4b1 IS |
143 | static tree lookup_property (tree, tree); |
144 | static tree lookup_property_in_list (tree, tree); | |
145 | static tree lookup_property_in_protocol_list (tree, tree); | |
d764a8e6 | 146 | static void build_common_objc_property_accessor_helpers (void); |
668ea4b1 | 147 | |
d11dd684 ZL |
148 | static void objc_xref_basetypes (tree, tree); |
149 | ||
d11dd684 | 150 | static tree get_class_ivars (tree, bool); |
011d50d9 | 151 | |
f05b9d93 NP |
152 | static void build_fast_enumeration_state_template (void); |
153 | ||
38b9c8c3 ZL |
154 | #ifdef OBJCPLUS |
155 | static void objc_generate_cxx_cdtors (void); | |
156 | #endif | |
011d50d9 | 157 | |
a1178b30 | 158 | /* objc attribute */ |
944fb799 | 159 | static void objc_decl_method_attributes (tree*, tree, int); |
a1178b30 | 160 | static tree build_keyword_selector (tree); |
7cc273ad | 161 | |
d764a8e6 | 162 | static void hash_init (void); |
6c65299b | 163 | |
3cc2dd4b NP |
164 | /* Hash tables to manage the global pool of method prototypes. Each |
165 | of these maps map a method name (selector) identifier to either a | |
166 | single tree (for methods with a single method prototype) or a | |
167 | TREE_VEC (for methods with multiple method prototypes). */ | |
168 | static GTY(()) objc_map_t instance_method_map = 0; | |
169 | static GTY(()) objc_map_t class_method_map = 0; | |
e31c7eec | 170 | |
660820d1 NP |
171 | /* Hash tables to manage the global pool of class names. */ |
172 | ||
3cc2dd4b NP |
173 | static GTY(()) objc_map_t class_name_map = 0; |
174 | static GTY(()) objc_map_t alias_name_map = 0; | |
660820d1 | 175 | |
011d50d9 | 176 | static tree lookup_method (tree, tree); |
264fa2db | 177 | static tree lookup_method_static (tree, tree, int); |
e31c7eec | 178 | |
3cc2dd4b NP |
179 | static void interface_hash_init (void); |
180 | static tree add_interface (tree, tree); | |
668ea4b1 IS |
181 | static void add_category (tree, tree); |
182 | static inline tree lookup_category (tree, tree); | |
183 | ||
6b192a09 | 184 | /* Protocols. */ |
e31c7eec | 185 | |
db0581ae NP |
186 | static tree lookup_protocol (tree, bool, bool); |
187 | static tree lookup_and_install_protocols (tree, bool); | |
6c65299b | 188 | |
f8893e47 | 189 | #ifdef OBJCPLUS |
011d50d9 | 190 | static void really_start_method (tree, tree); |
f8893e47 JM |
191 | #else |
192 | static void really_start_method (tree, struct c_arg_info *); | |
193 | #endif | |
38b9c8c3 | 194 | static int comp_proto_with_proto (tree, tree, int); |
26877873 | 195 | static tree objc_decay_parm_type (tree); |
6c65299b | 196 | |
0f41302f | 197 | /* Utilities for debugging and error diagnostics. */ |
6c65299b | 198 | |
6408ef33 ZL |
199 | static char *gen_type_name (tree); |
200 | static char *gen_type_name_0 (tree); | |
201 | static char *gen_method_decl (tree); | |
202 | static char *gen_declaration (tree); | |
e31c7eec | 203 | |
0f41302f | 204 | /* Everything else. */ |
e31c7eec | 205 | |
d764a8e6 IS |
206 | static void generate_struct_by_value_array (void) ATTRIBUTE_NORETURN; |
207 | ||
011d50d9 | 208 | static void mark_referenced_methods (void); |
9a179d01 | 209 | static bool objc_type_valid_for_messaging (tree type, bool allow_classes); |
3cc2dd4b | 210 | static tree check_duplicates (tree, int, int); |
6c65299b RS |
211 | |
212 | /*** Private Interface (data) ***/ | |
38b9c8c3 | 213 | /* Flags for lookup_method_static(). */ |
a9625a91 NP |
214 | |
215 | /* Look for class methods. */ | |
216 | #define OBJC_LOOKUP_CLASS 1 | |
217 | /* Do not examine superclasses. */ | |
218 | #define OBJC_LOOKUP_NO_SUPER 2 | |
219 | /* Disable returning an instance method of a root class when a class | |
220 | method can't be found. */ | |
944fb799 | 221 | #define OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS 4 |
38b9c8c3 | 222 | |
ad074d60 | 223 | /* The OCTI_... enumeration itself is in objc/objc-act.h. */ |
8607f1bc | 224 | tree objc_global_trees[OCTI_MAX]; |
6c65299b | 225 | |
8607f1bc ZL |
226 | struct imp_entry *imp_list = 0; |
227 | int imp_count = 0; /* `@implementation' */ | |
228 | int cat_count = 0; /* `@category' */ | |
229 | ||
dcaaa5a0 | 230 | objc_ivar_visibility_kind objc_ivar_visibility, objc_default_ivar_visibility; |
6408ef33 | 231 | |
264fa2db ZL |
232 | /* Use to generate method labels. */ |
233 | static int method_slot = 0; | |
6c65299b | 234 | |
92902b1b IS |
235 | /* Flag to say whether methods in a protocol are optional or |
236 | required. */ | |
237 | static bool objc_method_optional_flag = false; | |
238 | ||
c0c24aa4 NP |
239 | static int objc_collecting_ivars = 0; |
240 | ||
ec3e9f82 NP |
241 | /* Flag that is set to 'true' while we are processing a class |
242 | extension. Since a class extension just "reopens" the main | |
243 | @interface, this can be used to determine if we are in the main | |
244 | @interface, or in a class extension. */ | |
245 | static bool objc_in_class_extension = false; | |
246 | ||
51900510 | 247 | static char *errbuf; /* Buffer for error diagnostics */ |
6c65299b | 248 | |
6c39e757 NP |
249 | /* An array of all the local variables in the current function that |
250 | need to be marked as volatile. */ | |
9771b263 | 251 | vec<tree, va_gc> *local_variables_to_volatilize = NULL; |
6c39e757 | 252 | |
6408ef33 ZL |
253 | /* Store all constructed constant strings in a hash table so that |
254 | they get uniqued properly. */ | |
255 | ||
d1b38208 | 256 | struct GTY(()) string_descriptor { |
6408ef33 ZL |
257 | /* The literal argument . */ |
258 | tree literal; | |
259 | ||
260 | /* The resulting constant string. */ | |
261 | tree constructor; | |
262 | }; | |
263 | ||
264 | static GTY((param_is (struct string_descriptor))) htab_t string_htab; | |
265 | ||
6c65299b RS |
266 | FILE *gen_declaration_file; |
267 | ||
d764a8e6 IS |
268 | /* Hooks for stuff that differs between runtimes. */ |
269 | objc_runtime_hooks runtime; | |
3e78185c | 270 | |
925e8657 NP |
271 | /* Create a temporary variable of type 'type'. If 'name' is set, uses |
272 | the specified name, else use no name. Returns the declaration of | |
273 | the type. The 'name' is mostly useful for debugging. | |
274 | */ | |
d764a8e6 | 275 | tree |
925e8657 NP |
276 | objc_create_temporary_var (tree type, const char *name) |
277 | { | |
278 | tree decl; | |
279 | ||
280 | if (name != NULL) | |
281 | { | |
282 | decl = build_decl (input_location, | |
283 | VAR_DECL, get_identifier (name), type); | |
284 | } | |
285 | else | |
286 | { | |
287 | decl = build_decl (input_location, | |
288 | VAR_DECL, NULL_TREE, type); | |
289 | } | |
290 | TREE_USED (decl) = 1; | |
291 | DECL_ARTIFICIAL (decl) = 1; | |
292 | DECL_IGNORED_P (decl) = 1; | |
293 | DECL_CONTEXT (decl) = current_function_decl; | |
294 | ||
295 | return decl; | |
296 | } | |
297 | ||
ad074d60 SS |
298 | /* Some platforms pass small structures through registers versus |
299 | through an invisible pointer. Determine at what size structure is | |
300 | the transition point between the two possibilities. */ | |
7cc273ad | 301 | |
e36bf33a | 302 | static void |
011d50d9 | 303 | generate_struct_by_value_array (void) |
7cc273ad L |
304 | { |
305 | tree type; | |
3e78185c | 306 | tree decls; |
7cc273ad L |
307 | int i, j; |
308 | int aggregate_in_mem[32]; | |
309 | int found = 0; | |
310 | ||
6da97b7b NP |
311 | /* Presumably no platform passes 32 byte structures in a register. */ |
312 | /* ??? As an example, m64/ppc/Darwin can pass up to 8*long+13*double | |
313 | in registers. */ | |
7cc273ad L |
314 | for (i = 1; i < 32; i++) |
315 | { | |
316 | char buffer[5]; | |
3e78185c | 317 | tree *chain = NULL; |
7cc273ad L |
318 | |
319 | /* Create an unnamed struct that has `i' character components */ | |
24b97832 | 320 | type = objc_start_struct (NULL_TREE); |
7cc273ad L |
321 | |
322 | strcpy (buffer, "c1"); | |
3e78185c | 323 | decls = add_field_decl (char_type_node, buffer, &chain); |
7cc273ad L |
324 | |
325 | for (j = 1; j < i; j++) | |
326 | { | |
327 | sprintf (buffer, "c%d", j + 1); | |
3e78185c | 328 | add_field_decl (char_type_node, buffer, &chain); |
7cc273ad | 329 | } |
3e78185c | 330 | objc_finish_struct (type, decls); |
011d50d9 | 331 | |
61f71b34 | 332 | aggregate_in_mem[i] = aggregate_value_p (type, 0); |
7cc273ad L |
333 | if (!aggregate_in_mem[i]) |
334 | found = 1; | |
335 | } | |
011d50d9 | 336 | |
7cc273ad | 337 | /* We found some structures that are returned in registers instead of memory |
ad074d60 | 338 | so output the necessary data. */ |
7cc273ad L |
339 | if (found) |
340 | { | |
341 | for (i = 31; i >= 0; i--) | |
342 | if (!aggregate_in_mem[i]) | |
343 | break; | |
2d66a8d3 | 344 | printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n", i); |
7cc273ad | 345 | } |
011d50d9 | 346 | |
7cc273ad L |
347 | exit (0); |
348 | } | |
349 | ||
4bfec483 | 350 | bool |
011d50d9 | 351 | objc_init (void) |
6c65299b | 352 | { |
d764a8e6 | 353 | bool ok; |
6408ef33 ZL |
354 | #ifdef OBJCPLUS |
355 | if (cxx_init () == false) | |
356 | #else | |
4bfec483 | 357 | if (c_objc_common_init () == false) |
6408ef33 | 358 | #endif |
4bfec483 | 359 | return false; |
f5e99456 | 360 | |
6da97b7b NP |
361 | /* print_struct_values is triggered by -print-runtime-info (used |
362 | when building libobjc, with an empty file as input). It does not | |
363 | require any ObjC setup, and it never returns. | |
364 | ||
365 | -fcompare-debug is used to check the compiler output; we are | |
366 | executed twice, once with flag_compare_debug set, and once with | |
367 | it not set. If the flag is used together with | |
368 | -print-runtime-info, we want to print the runtime info only once, | |
369 | else it would be output in duplicate. So we check | |
370 | flag_compare_debug to output it in only one of the invocations. | |
371 | ||
372 | As a side effect, this also that means -fcompare-debug | |
373 | -print-runtime-info will run the compiler twice, and compare the | |
374 | generated assembler file; the first time the compiler exits | |
375 | immediately (producing no file), and the second time it compiles | |
376 | an empty file. This checks, as a side effect, that compiling an | |
377 | empty file produces no assembler output. */ | |
d764a8e6 | 378 | if (print_struct_values && !flag_compare_debug) |
d764a8e6 | 379 | generate_struct_by_value_array (); |
6c65299b | 380 | |
d764a8e6 IS |
381 | /* Set up stuff used by FE parser and all runtimes. */ |
382 | errbuf = XNEWVEC (char, 1024 * 10); | |
3cc2dd4b | 383 | interface_hash_init (); |
d764a8e6 | 384 | hash_init (); |
a8f18c40 | 385 | objc_encoding_init (); |
d764a8e6 IS |
386 | /* ... and then check flags and set-up for the selected runtime ... */ |
387 | if (flag_next_runtime && flag_objc_abi >= 2) | |
388 | ok = objc_next_runtime_abi_02_init (&runtime); | |
389 | else if (flag_next_runtime) | |
390 | ok = objc_next_runtime_abi_01_init (&runtime); | |
391 | else | |
392 | ok = objc_gnu_runtime_abi_01_init (&runtime); | |
7cc273ad | 393 | |
d764a8e6 IS |
394 | /* If that part of the setup failed - bail out immediately. */ |
395 | if (!ok) | |
396 | return false; | |
bcdb1106 | 397 | |
dcaaa5a0 DP |
398 | /* Determine the default visibility for instance variables. */ |
399 | switch (default_ivar_visibility) | |
400 | { | |
401 | case IVAR_VISIBILITY_PRIVATE: | |
402 | objc_default_ivar_visibility = OBJC_IVAR_VIS_PRIVATE; | |
403 | break; | |
404 | case IVAR_VISIBILITY_PUBLIC: | |
405 | objc_default_ivar_visibility = OBJC_IVAR_VIS_PUBLIC; | |
406 | break; | |
407 | case IVAR_VISIBILITY_PACKAGE: | |
408 | objc_default_ivar_visibility = OBJC_IVAR_VIS_PACKAGE; | |
409 | break; | |
410 | default: | |
411 | objc_default_ivar_visibility = OBJC_IVAR_VIS_PROTECTED; | |
412 | } | |
413 | ||
d764a8e6 IS |
414 | /* Generate general types and push runtime-specific decls to file scope. */ |
415 | synth_module_prologue (); | |
46270f14 | 416 | |
4bfec483 | 417 | return true; |
8b0e9a72 AO |
418 | } |
419 | ||
977e30bc NP |
420 | /* This is called automatically (at the very end of compilation) by |
421 | c_write_global_declarations and cp_write_global_declarations. */ | |
6c65299b | 422 | void |
977e30bc | 423 | objc_write_global_declarations (void) |
6c65299b | 424 | { |
0d446150 | 425 | mark_referenced_methods (); |
8b0e9a72 | 426 | |
d764a8e6 IS |
427 | /* A missing @end might not be detected by the parser. */ |
428 | if (objc_implementation_context) | |
429 | { | |
430 | warning (0, "%<@end%> missing in implementation context"); | |
431 | finish_class (objc_implementation_context); | |
432 | objc_ivar_chain = NULL_TREE; | |
433 | objc_implementation_context = NULL_TREE; | |
434 | } | |
435 | ||
436 | if (warn_selector) | |
437 | { | |
3cc2dd4b | 438 | objc_map_iterator_t i; |
d764a8e6 | 439 | |
3cc2dd4b NP |
440 | objc_map_iterator_initialize (class_method_map, &i); |
441 | while (objc_map_iterator_move_to_next (class_method_map, &i)) | |
442 | check_duplicates (objc_map_iterator_current_value (class_method_map, i), 0, 1); | |
d764a8e6 | 443 | |
3cc2dd4b NP |
444 | objc_map_iterator_initialize (instance_method_map, &i); |
445 | while (objc_map_iterator_move_to_next (instance_method_map, &i)) | |
446 | check_duplicates (objc_map_iterator_current_value (instance_method_map, i), 0, 0); | |
d764a8e6 IS |
447 | } |
448 | ||
449 | /* TODO: consider an early exit here if either errorcount or sorrycount | |
450 | is non-zero. Not only is it wasting time to generate the metadata, | |
944fb799 | 451 | it needlessly imposes need to re-check for things that are already |
d764a8e6 | 452 | determined to be errors. */ |
6c65299b | 453 | |
d764a8e6 IS |
454 | /* Finalize Objective-C runtime data. No need to generate tables |
455 | and code if only checking syntax, or if generating a PCH file. */ | |
456 | if (!flag_syntax_only && !pch_file) | |
457 | { | |
6d549148 NP |
458 | location_t saved_location; |
459 | ||
d764a8e6 IS |
460 | /* If gen_declaration desired, open the output file. */ |
461 | if (flag_gen_declaration) | |
462 | { | |
463 | char * const dumpname = concat (dump_base_name, ".decl", NULL); | |
464 | gen_declaration_file = fopen (dumpname, "w"); | |
465 | if (gen_declaration_file == 0) | |
466 | fatal_error ("can%'t open %s: %m", dumpname); | |
467 | free (dumpname); | |
468 | } | |
6d549148 NP |
469 | |
470 | /* Set the input location to BUILTINS_LOCATION. This is good | |
471 | for error messages, in case any is generated while producing | |
472 | the metadata, but it also silences warnings that would be | |
473 | produced when compiling with -Wpadded in case when padding is | |
474 | automatically added to the built-in runtime data structure | |
475 | declarations. We know about this padding, and it is fine; we | |
476 | don't want users to see any warnings about it if they use | |
477 | -Wpadded. */ | |
478 | saved_location = input_location; | |
479 | input_location = BUILTINS_LOCATION; | |
480 | ||
d764a8e6 IS |
481 | /* Compute and emit the meta-data tables for this runtime. */ |
482 | (*runtime.generate_metadata) (); | |
6d549148 NP |
483 | |
484 | /* Restore the original location, just in case it mattered. */ | |
485 | input_location = saved_location; | |
486 | ||
d764a8e6 IS |
487 | /* ... and then close any declaration file we opened. */ |
488 | if (gen_declaration_file) | |
489 | fclose (gen_declaration_file); | |
490 | } | |
6c65299b | 491 | } |
d764a8e6 | 492 | |
5c234cd7 DA |
493 | /* Return the first occurrence of a method declaration corresponding |
494 | to sel_name in rproto_list. Search rproto_list recursively. | |
495 | If is_class is 0, search for instance methods, otherwise for class | |
496 | methods. */ | |
e31c7eec | 497 | static tree |
011d50d9 | 498 | lookup_method_in_protocol_list (tree rproto_list, tree sel_name, |
5c234cd7 | 499 | int is_class) |
e31c7eec | 500 | { |
354d8ce1 | 501 | tree rproto, p, m; |
e31c7eec TW |
502 | |
503 | for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto)) | |
504 | { | |
354d8ce1 NP |
505 | p = TREE_VALUE (rproto); |
506 | m = NULL_TREE; | |
e31c7eec TW |
507 | |
508 | if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE) | |
509 | { | |
354d8ce1 NP |
510 | /* First, search the @required protocol methods. */ |
511 | if (is_class) | |
512 | m = lookup_method (PROTOCOL_CLS_METHODS (p), sel_name); | |
513 | else | |
514 | m = lookup_method (PROTOCOL_NST_METHODS (p), sel_name); | |
515 | ||
516 | if (m) | |
517 | return m; | |
518 | ||
519 | /* If still not found, search the @optional protocol methods. */ | |
520 | if (is_class) | |
521 | m = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (p), sel_name); | |
522 | else | |
523 | m = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (p), sel_name); | |
524 | ||
525 | if (m) | |
526 | return m; | |
527 | ||
528 | /* If still not found, search the attached protocols. */ | |
529 | if (PROTOCOL_LIST (p)) | |
530 | m = lookup_method_in_protocol_list (PROTOCOL_LIST (p), | |
531 | sel_name, is_class); | |
532 | if (m) | |
533 | return m; | |
e31c7eec TW |
534 | } |
535 | else | |
51723711 KG |
536 | { |
537 | ; /* An identifier...if we could not find a protocol. */ | |
538 | } | |
e31c7eec | 539 | } |
51900510 | 540 | |
e31c7eec TW |
541 | return 0; |
542 | } | |
543 | ||
544 | static tree | |
011d50d9 | 545 | lookup_protocol_in_reflist (tree rproto_list, tree lproto) |
e31c7eec | 546 | { |
ad074d60 | 547 | tree rproto, p; |
e31c7eec | 548 | |
ad074d60 SS |
549 | /* Make sure the protocol is supported by the object on the rhs. */ |
550 | if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE) | |
551 | { | |
552 | tree fnd = 0; | |
553 | for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto)) | |
554 | { | |
555 | p = TREE_VALUE (rproto); | |
e31c7eec | 556 | |
ad074d60 SS |
557 | if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE) |
558 | { | |
559 | if (lproto == p) | |
560 | fnd = lproto; | |
e31c7eec | 561 | |
ad074d60 SS |
562 | else if (PROTOCOL_LIST (p)) |
563 | fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto); | |
564 | } | |
e31c7eec | 565 | |
ad074d60 SS |
566 | if (fnd) |
567 | return fnd; | |
568 | } | |
569 | } | |
570 | else | |
571 | { | |
572 | ; /* An identifier...if we could not find a protocol. */ | |
573 | } | |
e31c7eec | 574 | |
ad074d60 | 575 | return 0; |
6c65299b RS |
576 | } |
577 | ||
6408ef33 | 578 | void |
c165dca7 IS |
579 | objc_start_class_interface (tree klass, tree super_class, |
580 | tree protos, tree attributes) | |
6408ef33 | 581 | { |
d4e72c58 | 582 | if (flag_objc1_only && attributes) |
944fb799 | 583 | error_at (input_location, "class attributes are not available in Objective-C 1.0"); |
d4e72c58 | 584 | |
6408ef33 ZL |
585 | objc_interface_context |
586 | = objc_ivar_context | |
d4e72c58 | 587 | = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos, attributes); |
dcaaa5a0 | 588 | objc_ivar_visibility = objc_default_ivar_visibility; |
6408ef33 ZL |
589 | } |
590 | ||
591 | void | |
c165dca7 IS |
592 | objc_start_category_interface (tree klass, tree categ, |
593 | tree protos, tree attributes) | |
6408ef33 | 594 | { |
c165dca7 | 595 | if (attributes) |
22d8d616 NP |
596 | { |
597 | if (flag_objc1_only) | |
598 | error_at (input_location, "category attributes are not available in Objective-C 1.0"); | |
599 | else | |
944fb799 | 600 | warning_at (input_location, OPT_Wattributes, |
22d8d616 NP |
601 | "category attributes are not available in this version" |
602 | " of the compiler, (ignored)"); | |
603 | } | |
ec3e9f82 NP |
604 | if (categ == NULL_TREE) |
605 | { | |
606 | if (flag_objc1_only) | |
607 | error_at (input_location, "class extensions are not available in Objective-C 1.0"); | |
35bce82a NP |
608 | else |
609 | { | |
610 | /* Iterate over all the classes and categories implemented | |
611 | up to now in this compilation unit. */ | |
612 | struct imp_entry *t; | |
613 | ||
614 | for (t = imp_list; t; t = t->next) | |
615 | { | |
616 | /* If we find a class @implementation with the same name | |
617 | as the one we are extending, produce an error. */ | |
618 | if (TREE_CODE (t->imp_context) == CLASS_IMPLEMENTATION_TYPE | |
619 | && IDENTIFIER_POINTER (CLASS_NAME (t->imp_context)) == IDENTIFIER_POINTER (klass)) | |
944fb799 | 620 | error_at (input_location, |
35bce82a NP |
621 | "class extension for class %qE declared after its %<@implementation%>", |
622 | klass); | |
623 | } | |
624 | } | |
ec3e9f82 | 625 | } |
6408ef33 | 626 | objc_interface_context |
d4e72c58 | 627 | = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos, NULL_TREE); |
6408ef33 ZL |
628 | objc_ivar_chain |
629 | = continue_class (objc_interface_context); | |
630 | } | |
631 | ||
632 | void | |
c165dca7 | 633 | objc_start_protocol (tree name, tree protos, tree attributes) |
6408ef33 | 634 | { |
6b192a09 | 635 | if (flag_objc1_only && attributes) |
944fb799 | 636 | error_at (input_location, "protocol attributes are not available in Objective-C 1.0"); |
6b192a09 | 637 | |
6408ef33 | 638 | objc_interface_context |
6b192a09 | 639 | = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos, attributes); |
92902b1b | 640 | objc_method_optional_flag = false; |
6408ef33 ZL |
641 | } |
642 | ||
643 | void | |
644 | objc_continue_interface (void) | |
645 | { | |
646 | objc_ivar_chain | |
647 | = continue_class (objc_interface_context); | |
648 | } | |
649 | ||
650 | void | |
651 | objc_finish_interface (void) | |
652 | { | |
653 | finish_class (objc_interface_context); | |
654 | objc_interface_context = NULL_TREE; | |
92902b1b | 655 | objc_method_optional_flag = false; |
ec3e9f82 | 656 | objc_in_class_extension = false; |
6408ef33 ZL |
657 | } |
658 | ||
659 | void | |
5025f379 | 660 | objc_start_class_implementation (tree klass, tree super_class) |
6408ef33 ZL |
661 | { |
662 | objc_implementation_context | |
663 | = objc_ivar_context | |
d4e72c58 NP |
664 | = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE, |
665 | NULL_TREE); | |
dcaaa5a0 | 666 | objc_ivar_visibility = objc_default_ivar_visibility; |
6408ef33 ZL |
667 | } |
668 | ||
669 | void | |
5025f379 | 670 | objc_start_category_implementation (tree klass, tree categ) |
6408ef33 ZL |
671 | { |
672 | objc_implementation_context | |
d4e72c58 NP |
673 | = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE, |
674 | NULL_TREE); | |
6408ef33 ZL |
675 | objc_ivar_chain |
676 | = continue_class (objc_implementation_context); | |
677 | } | |
678 | ||
679 | void | |
680 | objc_continue_implementation (void) | |
681 | { | |
682 | objc_ivar_chain | |
683 | = continue_class (objc_implementation_context); | |
684 | } | |
685 | ||
686 | void | |
687 | objc_finish_implementation (void) | |
688 | { | |
38b9c8c3 ZL |
689 | #ifdef OBJCPLUS |
690 | if (flag_objc_call_cxx_cdtors) | |
691 | objc_generate_cxx_cdtors (); | |
692 | #endif | |
693 | ||
6408ef33 ZL |
694 | if (objc_implementation_context) |
695 | { | |
696 | finish_class (objc_implementation_context); | |
697 | objc_ivar_chain = NULL_TREE; | |
698 | objc_implementation_context = NULL_TREE; | |
699 | } | |
700 | else | |
d4ee4d25 | 701 | warning (0, "%<@end%> must appear in an @implementation context"); |
6408ef33 ZL |
702 | } |
703 | ||
704 | void | |
c37d8c30 | 705 | objc_set_visibility (objc_ivar_visibility_kind visibility) |
6408ef33 | 706 | { |
c37d8c30 | 707 | if (visibility == OBJC_IVAR_VIS_PACKAGE) |
22d8d616 NP |
708 | { |
709 | if (flag_objc1_only) | |
710 | error ("%<@package%> is not available in Objective-C 1.0"); | |
711 | else | |
712 | warning (0, "%<@package%> presently has the same effect as %<@public%>"); | |
713 | } | |
c37d8c30 | 714 | objc_ivar_visibility = visibility; |
6408ef33 ZL |
715 | } |
716 | ||
92902b1b IS |
717 | void |
718 | objc_set_method_opt (bool optional) | |
719 | { | |
22d8d616 | 720 | if (flag_objc1_only) |
445eb4c6 NP |
721 | { |
722 | if (optional) | |
944fb799 | 723 | error_at (input_location, "%<@optional%> is not available in Objective-C 1.0"); |
445eb4c6 | 724 | else |
944fb799 | 725 | error_at (input_location, "%<@required%> is not available in Objective-C 1.0"); |
445eb4c6 | 726 | } |
22d8d616 | 727 | |
92902b1b | 728 | objc_method_optional_flag = optional; |
944fb799 | 729 | if (!objc_interface_context |
92902b1b IS |
730 | || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE) |
731 | { | |
445eb4c6 NP |
732 | if (optional) |
733 | error ("%<@optional%> is allowed in @protocol context only"); | |
734 | else | |
735 | error ("%<@required%> is allowed in @protocol context only"); | |
92902b1b IS |
736 | objc_method_optional_flag = false; |
737 | } | |
738 | } | |
739 | ||
4ca5d2a7 NP |
740 | /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or |
741 | PROTOCOL. */ | |
742 | static tree | |
743 | lookup_property_in_list (tree chain, tree property) | |
744 | { | |
745 | tree x; | |
746 | for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x)) | |
747 | if (PROPERTY_NAME (x) == property) | |
748 | return x; | |
749 | return NULL_TREE; | |
750 | } | |
751 | ||
752 | /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */ | |
753 | static tree lookup_property_in_protocol_list (tree rproto_list, tree property) | |
754 | { | |
755 | tree rproto, x; | |
756 | for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto)) | |
757 | { | |
758 | tree p = TREE_VALUE (rproto); | |
759 | if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE) | |
760 | { | |
761 | if ((x = lookup_property_in_list (p, property))) | |
762 | return x; | |
763 | if (PROTOCOL_LIST (p)) | |
764 | return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property); | |
765 | } | |
766 | else | |
767 | { | |
768 | ; /* An identifier...if we could not find a protocol. */ | |
769 | } | |
770 | } | |
771 | return NULL_TREE; | |
772 | } | |
773 | ||
774 | /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the | |
775 | chain of interface hierarchy. */ | |
776 | static tree | |
777 | lookup_property (tree interface_type, tree property) | |
778 | { | |
779 | tree inter = interface_type; | |
780 | while (inter) | |
781 | { | |
782 | tree x, category; | |
783 | if ((x = lookup_property_in_list (inter, property))) | |
784 | return x; | |
785 | /* Failing that, look for the property in each category of the class. */ | |
786 | category = inter; | |
787 | while ((category = CLASS_CATEGORY_LIST (category))) | |
788 | { | |
789 | if ((x = lookup_property_in_list (category, property))) | |
790 | return x; | |
791 | ||
792 | /* When checking a category, also check the protocols | |
793 | attached with the category itself. */ | |
794 | if (CLASS_PROTOCOL_LIST (category) | |
795 | && (x = lookup_property_in_protocol_list | |
796 | (CLASS_PROTOCOL_LIST (category), property))) | |
797 | return x; | |
798 | } | |
799 | ||
800 | /* Failing to find in categories, look for property in protocol list. */ | |
944fb799 | 801 | if (CLASS_PROTOCOL_LIST (inter) |
4ca5d2a7 NP |
802 | && (x = lookup_property_in_protocol_list |
803 | (CLASS_PROTOCOL_LIST (inter), property))) | |
804 | return x; | |
944fb799 | 805 | |
4ca5d2a7 NP |
806 | /* Failing that, climb up the inheritance hierarchy. */ |
807 | inter = lookup_interface (CLASS_SUPER_NAME (inter)); | |
808 | } | |
809 | return inter; | |
810 | } | |
811 | ||
200290f2 NP |
812 | /* This routine is called by the parser when a |
813 | @property... declaration is found. 'decl' is the declaration of | |
814 | the property (type/identifier), and the other arguments represent | |
815 | property attributes that may have been specified in the Objective-C | |
816 | declaration. 'parsed_property_readonly' is 'true' if the attribute | |
817 | 'readonly' was specified, and 'false' if not; similarly for the | |
818 | other bool parameters. 'parsed_property_getter_ident' is NULL_TREE | |
819 | if the attribute 'getter' was not specified, and is the identifier | |
820 | corresponding to the specified getter if it was; similarly for | |
821 | 'parsed_property_setter_ident'. */ | |
668ea4b1 | 822 | void |
200290f2 NP |
823 | objc_add_property_declaration (location_t location, tree decl, |
824 | bool parsed_property_readonly, bool parsed_property_readwrite, | |
825 | bool parsed_property_assign, bool parsed_property_retain, | |
826 | bool parsed_property_copy, bool parsed_property_nonatomic, | |
46a88c12 | 827 | tree parsed_property_getter_ident, tree parsed_property_setter_ident) |
668ea4b1 | 828 | { |
200290f2 NP |
829 | tree property_decl; |
830 | tree x; | |
46a88c12 NP |
831 | /* 'property_readonly' and 'property_assign_semantics' are the final |
832 | attributes of the property after all parsed attributes have been | |
833 | considered (eg, if we parsed no 'readonly' and no 'readwrite', ie | |
834 | parsed_property_readonly = false and parsed_property_readwrite = | |
835 | false, then property_readonly will be false because the default | |
836 | is readwrite). */ | |
200290f2 | 837 | bool property_readonly = false; |
46a88c12 | 838 | objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN; |
ec3e9f82 | 839 | bool property_extension_in_class_extension = false; |
200290f2 | 840 | |
22d8d616 NP |
841 | if (flag_objc1_only) |
842 | error_at (input_location, "%<@property%> is not available in Objective-C 1.0"); | |
843 | ||
200290f2 | 844 | if (parsed_property_readonly && parsed_property_readwrite) |
668ea4b1 | 845 | { |
200290f2 NP |
846 | error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute"); |
847 | /* In case of conflicting attributes (here and below), after | |
848 | producing an error, we pick one of the attributes and keep | |
849 | going. */ | |
850 | property_readonly = false; | |
851 | } | |
852 | else | |
853 | { | |
854 | if (parsed_property_readonly) | |
668ea4b1 | 855 | property_readonly = true; |
944fb799 | 856 | |
200290f2 NP |
857 | if (parsed_property_readwrite) |
858 | property_readonly = false; | |
668ea4b1 | 859 | } |
668ea4b1 | 860 | |
200290f2 NP |
861 | if (parsed_property_readonly && parsed_property_setter_ident) |
862 | { | |
8926bd5d | 863 | error_at (location, "%<readonly%> attribute conflicts with %<setter%> attribute"); |
46a88c12 | 864 | property_readonly = false; |
200290f2 | 865 | } |
668ea4b1 | 866 | |
200290f2 NP |
867 | if (parsed_property_assign && parsed_property_retain) |
868 | { | |
869 | error_at (location, "%<assign%> attribute conflicts with %<retain%> attribute"); | |
870 | property_assign_semantics = OBJC_PROPERTY_RETAIN; | |
871 | } | |
872 | else if (parsed_property_assign && parsed_property_copy) | |
873 | { | |
874 | error_at (location, "%<assign%> attribute conflicts with %<copy%> attribute"); | |
875 | property_assign_semantics = OBJC_PROPERTY_COPY; | |
876 | } | |
877 | else if (parsed_property_retain && parsed_property_copy) | |
878 | { | |
879 | error_at (location, "%<retain%> attribute conflicts with %<copy%> attribute"); | |
880 | property_assign_semantics = OBJC_PROPERTY_COPY; | |
881 | } | |
882 | else | |
883 | { | |
884 | if (parsed_property_assign) | |
885 | property_assign_semantics = OBJC_PROPERTY_ASSIGN; | |
668ea4b1 | 886 | |
200290f2 NP |
887 | if (parsed_property_retain) |
888 | property_assign_semantics = OBJC_PROPERTY_RETAIN; | |
889 | ||
890 | if (parsed_property_copy) | |
891 | property_assign_semantics = OBJC_PROPERTY_COPY; | |
892 | } | |
893 | ||
46a88c12 | 894 | if (!objc_interface_context) |
668ea4b1 | 895 | { |
46a88c12 | 896 | error_at (location, "property declaration not in @interface or @protocol context"); |
668ea4b1 IS |
897 | return; |
898 | } | |
899 | ||
46a88c12 NP |
900 | /* At this point we know that we are either in an interface, a |
901 | category, or a protocol. */ | |
902 | ||
8926bd5d NP |
903 | /* We expect a FIELD_DECL from the parser. Make sure we didn't get |
904 | something else, as that would confuse the checks below. */ | |
905 | if (TREE_CODE (decl) != FIELD_DECL) | |
906 | { | |
907 | error_at (location, "invalid property declaration"); | |
944fb799 | 908 | return; |
8926bd5d NP |
909 | } |
910 | ||
911 | /* Do some spot-checks for the most obvious invalid types. */ | |
912 | ||
913 | if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) | |
46a88c12 | 914 | { |
8926bd5d | 915 | error_at (location, "property can not be an array"); |
46a88c12 NP |
916 | return; |
917 | } | |
668ea4b1 | 918 | |
8926bd5d NP |
919 | /* The C++/ObjC++ parser seems to reject the ':' for a bitfield when |
920 | parsing, while the C/ObjC parser accepts it and gives us a | |
921 | FIELD_DECL with a DECL_INITIAL set. So we use the DECL_INITIAL | |
922 | to check for a bitfield when doing ObjC. */ | |
923 | #ifndef OBJCPLUS | |
924 | if (DECL_INITIAL (decl)) | |
925 | { | |
926 | /* A @property is not an actual variable, but it is a way to | |
927 | describe a pair of accessor methods, so its type (which is | |
928 | the type of the return value of the getter and the first | |
929 | argument of the setter) can't be a bitfield (as return values | |
930 | and arguments of functions can not be bitfields). The | |
931 | underlying instance variable could be a bitfield, but that is | |
932 | a different matter. */ | |
933 | error_at (location, "property can not be a bit-field"); | |
944fb799 | 934 | return; |
8926bd5d NP |
935 | } |
936 | #endif | |
937 | ||
938 | /* TODO: Check that the property type is an Objective-C object or a | |
939 | "POD". */ | |
46a88c12 | 940 | |
9a179d01 NP |
941 | /* Implement -Wproperty-assign-default (which is enabled by default). */ |
942 | if (warn_property_assign_default | |
46a88c12 NP |
943 | /* If garbage collection is not being used, then 'assign' is |
944 | valid for objects (and typically used for delegates) but it | |
945 | is wrong in most cases (since most objects need to be | |
946 | retained or copied in setters). Warn users when 'assign' is | |
947 | used implicitly. */ | |
9a179d01 NP |
948 | && property_assign_semantics == OBJC_PROPERTY_ASSIGN |
949 | /* Read-only properties are never assigned, so the assignment | |
950 | semantics do not matter in that case. */ | |
951 | && !property_readonly | |
952 | && !flag_objc_gc) | |
953 | { | |
46a88c12 NP |
954 | /* Please note that it would make sense to default to 'assign' |
955 | for non-{Objective-C objects}, and to 'retain' for | |
956 | Objective-C objects. But that would break compatibility with | |
957 | other compilers. */ | |
9a179d01 | 958 | if (!parsed_property_assign && !parsed_property_retain && !parsed_property_copy) |
668ea4b1 | 959 | { |
9a179d01 NP |
960 | /* Use 'false' so we do not warn for Class objects. */ |
961 | if (objc_type_valid_for_messaging (TREE_TYPE (decl), false)) | |
668ea4b1 | 962 | { |
944fb799 | 963 | warning_at (location, |
9a179d01 | 964 | 0, |
944fb799 | 965 | "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>", |
9a179d01 | 966 | decl); |
944fb799 | 967 | inform (location, |
9a179d01 | 968 | "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it"); |
668ea4b1 IS |
969 | } |
970 | } | |
668ea4b1 | 971 | } |
944fb799 | 972 | |
46a88c12 | 973 | if (property_assign_semantics == OBJC_PROPERTY_RETAIN |
9a179d01 | 974 | && !objc_type_valid_for_messaging (TREE_TYPE (decl), true)) |
46a88c12 | 975 | error_at (location, "%<retain%> attribute is only valid for Objective-C objects"); |
944fb799 | 976 | |
46a88c12 | 977 | if (property_assign_semantics == OBJC_PROPERTY_COPY |
9a179d01 | 978 | && !objc_type_valid_for_messaging (TREE_TYPE (decl), true)) |
46a88c12 | 979 | error_at (location, "%<copy%> attribute is only valid for Objective-C objects"); |
200290f2 | 980 | |
4ca5d2a7 NP |
981 | /* Now determine the final property getter and setter names. They |
982 | will be stored in the PROPERTY_DECL, from which they'll always be | |
983 | extracted and used. */ | |
984 | ||
985 | /* Adjust, or fill in, setter and getter names. We overwrite the | |
986 | parsed_property_setter_ident and parsed_property_getter_ident | |
987 | with the final setter and getter identifiers that will be | |
988 | used. */ | |
989 | if (parsed_property_setter_ident) | |
990 | { | |
991 | /* The setter should be terminated by ':', but the parser only | |
992 | gives us an identifier without ':'. So, we need to add ':' | |
993 | at the end. */ | |
994 | const char *parsed_setter = IDENTIFIER_POINTER (parsed_property_setter_ident); | |
995 | size_t length = strlen (parsed_setter); | |
996 | char *final_setter = (char *)alloca (length + 2); | |
997 | ||
998 | sprintf (final_setter, "%s:", parsed_setter); | |
999 | parsed_property_setter_ident = get_identifier (final_setter); | |
1000 | } | |
1001 | else | |
1002 | { | |
1003 | if (!property_readonly) | |
944fb799 | 1004 | parsed_property_setter_ident = get_identifier (objc_build_property_setter_name |
4ca5d2a7 NP |
1005 | (DECL_NAME (decl))); |
1006 | } | |
1007 | ||
1008 | if (!parsed_property_getter_ident) | |
1009 | parsed_property_getter_ident = DECL_NAME (decl); | |
1010 | ||
46a88c12 | 1011 | /* Check for duplicate property declarations. We first check the |
4ca5d2a7 | 1012 | immediate context for a property with the same name. Any such |
ec3e9f82 NP |
1013 | declarations are an error, unless this is a class extension and |
1014 | we are extending a property from readonly to readwrite. */ | |
46a88c12 NP |
1015 | for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x)) |
1016 | { | |
1017 | if (PROPERTY_NAME (x) == DECL_NAME (decl)) | |
668ea4b1 | 1018 | { |
ec3e9f82 NP |
1019 | if (objc_in_class_extension |
1020 | && property_readonly == 0 | |
1021 | && PROPERTY_READONLY (x) == 1) | |
1022 | { | |
1023 | /* This is a class extension, and we are extending an | |
1024 | existing readonly property to a readwrite one. | |
1025 | That's fine. :-) */ | |
1026 | property_extension_in_class_extension = true; | |
1027 | break; | |
1028 | } | |
1029 | else | |
1030 | { | |
1031 | location_t original_location = DECL_SOURCE_LOCATION (x); | |
944fb799 | 1032 | |
ec3e9f82 | 1033 | error_at (location, "redeclaration of property %qD", decl); |
944fb799 | 1034 | |
ec3e9f82 NP |
1035 | if (original_location != UNKNOWN_LOCATION) |
1036 | inform (original_location, "originally specified here"); | |
1037 | return; | |
1038 | } | |
1039 | } | |
668ea4b1 | 1040 | } |
46a88c12 | 1041 | |
ec3e9f82 NP |
1042 | /* If x is not NULL_TREE, we must be in a class extension and we're |
1043 | extending a readonly property. In that case, no point in | |
1044 | searching for another declaration. */ | |
1045 | if (x == NULL_TREE) | |
1046 | { | |
1047 | /* We now need to check for existing property declarations (in | |
1048 | the superclass, other categories or protocols) and check that | |
1049 | the new declaration is not in conflict with existing | |
1050 | ones. */ | |
4ca5d2a7 | 1051 | |
ec3e9f82 NP |
1052 | /* Search for a previous, existing declaration of a property |
1053 | with the same name in superclasses, protocols etc. If one is | |
1054 | found, it will be in the 'x' variable. */ | |
4ca5d2a7 | 1055 | |
ec3e9f82 NP |
1056 | /* Note that, for simplicity, the following may search again the |
1057 | local context. That's Ok as nothing will be found (else we'd | |
1058 | have thrown an error above); it's only a little inefficient, | |
1059 | but the code is simpler. */ | |
1060 | switch (TREE_CODE (objc_interface_context)) | |
4ca5d2a7 | 1061 | { |
ec3e9f82 NP |
1062 | case CLASS_INTERFACE_TYPE: |
1063 | /* Look up the property in the current @interface (which | |
1064 | will find nothing), then its protocols and categories and | |
1065 | superclasses. */ | |
1066 | x = lookup_property (objc_interface_context, DECL_NAME (decl)); | |
1067 | break; | |
1068 | case CATEGORY_INTERFACE_TYPE: | |
1069 | /* Look up the property in the main @interface, then | |
1070 | protocols and categories (one of them is ours, and will | |
1071 | find nothing) and superclasses. */ | |
1072 | x = lookup_property (lookup_interface (CLASS_NAME (objc_interface_context)), | |
1073 | DECL_NAME (decl)); | |
1074 | break; | |
1075 | case PROTOCOL_INTERFACE_TYPE: | |
1076 | /* Looks up the property in any protocols attached to the | |
1077 | current protocol. */ | |
1078 | if (PROTOCOL_LIST (objc_interface_context)) | |
1079 | { | |
1080 | x = lookup_property_in_protocol_list (PROTOCOL_LIST (objc_interface_context), | |
1081 | DECL_NAME (decl)); | |
1082 | } | |
1083 | break; | |
1084 | default: | |
1085 | gcc_unreachable (); | |
4ca5d2a7 | 1086 | } |
4ca5d2a7 NP |
1087 | } |
1088 | ||
1089 | if (x != NULL_TREE) | |
1090 | { | |
1091 | /* An existing property was found; check that it has the same | |
1092 | types, or it is compatible. */ | |
1093 | location_t original_location = DECL_SOURCE_LOCATION (x); | |
2dd24dbd | 1094 | |
4ca5d2a7 NP |
1095 | if (PROPERTY_NONATOMIC (x) != parsed_property_nonatomic) |
1096 | { | |
8926bd5d NP |
1097 | warning_at (location, 0, |
1098 | "'nonatomic' attribute of property %qD conflicts with previous declaration", decl); | |
944fb799 | 1099 | |
4ca5d2a7 NP |
1100 | if (original_location != UNKNOWN_LOCATION) |
1101 | inform (original_location, "originally specified here"); | |
1102 | return; | |
1103 | } | |
1104 | ||
1105 | if (PROPERTY_GETTER_NAME (x) != parsed_property_getter_ident) | |
1106 | { | |
8926bd5d NP |
1107 | warning_at (location, 0, |
1108 | "'getter' attribute of property %qD conflicts with previous declaration", decl); | |
944fb799 | 1109 | |
4ca5d2a7 NP |
1110 | if (original_location != UNKNOWN_LOCATION) |
1111 | inform (original_location, "originally specified here"); | |
1112 | return; | |
1113 | } | |
1114 | ||
1115 | /* We can only compare the setter names if both the old and new property have a setter. */ | |
1116 | if (!property_readonly && !PROPERTY_READONLY(x)) | |
1117 | { | |
1118 | if (PROPERTY_SETTER_NAME (x) != parsed_property_setter_ident) | |
1119 | { | |
8926bd5d NP |
1120 | warning_at (location, 0, |
1121 | "'setter' attribute of property %qD conflicts with previous declaration", decl); | |
944fb799 | 1122 | |
4ca5d2a7 NP |
1123 | if (original_location != UNKNOWN_LOCATION) |
1124 | inform (original_location, "originally specified here"); | |
1125 | return; | |
1126 | } | |
1127 | } | |
1128 | ||
1129 | if (PROPERTY_ASSIGN_SEMANTICS (x) != property_assign_semantics) | |
1130 | { | |
8926bd5d NP |
1131 | warning_at (location, 0, |
1132 | "assign semantics attributes of property %qD conflict with previous declaration", decl); | |
944fb799 | 1133 | |
4ca5d2a7 NP |
1134 | if (original_location != UNKNOWN_LOCATION) |
1135 | inform (original_location, "originally specified here"); | |
1136 | return; | |
1137 | } | |
1138 | ||
1139 | /* It's ok to have a readonly property that becomes a readwrite, but not vice versa. */ | |
1140 | if (PROPERTY_READONLY (x) == 0 && property_readonly == 1) | |
1141 | { | |
8926bd5d NP |
1142 | warning_at (location, 0, |
1143 | "'readonly' attribute of property %qD conflicts with previous declaration", decl); | |
944fb799 | 1144 | |
4ca5d2a7 NP |
1145 | if (original_location != UNKNOWN_LOCATION) |
1146 | inform (original_location, "originally specified here"); | |
1147 | return; | |
1148 | } | |
1149 | ||
10e34e6e NP |
1150 | /* We now check that the new and old property declarations have |
1151 | the same types (or compatible one). In the Objective-C | |
1152 | tradition of loose type checking, we do type-checking but | |
1153 | only generate warnings (not errors) if they do not match. | |
1154 | For non-readonly properties, the types must match exactly; | |
1155 | for readonly properties, it is allowed to use a "more | |
1156 | specialized" type in the new property declaration. Eg, the | |
1157 | superclass has a getter returning (NSArray *) and the | |
1158 | subclass a getter returning (NSMutableArray *). The object's | |
1159 | getter returns an (NSMutableArray *); but if you cast the | |
1160 | object to the superclass, which is allowed, you'd still | |
1161 | expect the getter to return an (NSArray *), which works since | |
1162 | an (NSMutableArray *) is an (NSArray *) too. So, the set of | |
1163 | objects belonging to the type of the new @property should be | |
1164 | a subset of the set of objects belonging to the type of the | |
1165 | old @property. This is what "specialization" means. And the | |
1166 | reason it only applies to readonly properties is that for a | |
1167 | readwrite property the setter would have the opposite | |
1168 | requirement - ie that the superclass type is more specialized | |
1169 | then the subclass one; hence the only way to satisfy both | |
1170 | constraints is that the types match. */ | |
1171 | ||
1172 | /* If the types are not the same in the C sense, we warn ... */ | |
1173 | if (!comptypes (TREE_TYPE (x), TREE_TYPE (decl)) | |
1174 | /* ... unless the property is readonly, in which case we | |
1175 | allow a new, more specialized, declaration. */ | |
944fb799 | 1176 | && (!property_readonly |
10e34e6e NP |
1177 | || !objc_compare_types (TREE_TYPE (x), |
1178 | TREE_TYPE (decl), -5, NULL_TREE))) | |
4ca5d2a7 | 1179 | { |
10e34e6e NP |
1180 | warning_at (location, 0, |
1181 | "type of property %qD conflicts with previous declaration", decl); | |
1182 | if (original_location != UNKNOWN_LOCATION) | |
1183 | inform (original_location, "originally specified here"); | |
1184 | return; | |
4ca5d2a7 | 1185 | } |
ec3e9f82 NP |
1186 | |
1187 | /* If we are in a class extension and we're extending a readonly | |
1188 | property in the main @interface, we'll just update the | |
1189 | existing property with the readwrite flag and potentially the | |
1190 | new setter name. */ | |
1191 | if (property_extension_in_class_extension) | |
1192 | { | |
1193 | PROPERTY_READONLY (x) = 0; | |
1194 | PROPERTY_SETTER_NAME (x) = parsed_property_setter_ident; | |
1195 | return; | |
1196 | } | |
4ca5d2a7 | 1197 | } |
46a88c12 NP |
1198 | |
1199 | /* Create a PROPERTY_DECL node. */ | |
1200 | property_decl = make_node (PROPERTY_DECL); | |
1201 | ||
1202 | /* Copy the basic information from the original decl. */ | |
1203 | TREE_TYPE (property_decl) = TREE_TYPE (decl); | |
1204 | DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl); | |
1205 | TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl); | |
944fb799 | 1206 | |
46a88c12 NP |
1207 | /* Add property-specific information. */ |
1208 | PROPERTY_NAME (property_decl) = DECL_NAME (decl); | |
1209 | PROPERTY_GETTER_NAME (property_decl) = parsed_property_getter_ident; | |
1210 | PROPERTY_SETTER_NAME (property_decl) = parsed_property_setter_ident; | |
1211 | PROPERTY_READONLY (property_decl) = property_readonly; | |
1212 | PROPERTY_NONATOMIC (property_decl) = parsed_property_nonatomic; | |
1213 | PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics; | |
1214 | PROPERTY_IVAR_NAME (property_decl) = NULL_TREE; | |
1215 | PROPERTY_DYNAMIC (property_decl) = 0; | |
1216 | ||
2dd24dbd NP |
1217 | /* Remember the fact that the property was found in the @optional |
1218 | section in a @protocol, or not. */ | |
1219 | if (objc_method_optional_flag) | |
1220 | PROPERTY_OPTIONAL (property_decl) = 1; | |
1221 | else | |
1222 | PROPERTY_OPTIONAL (property_decl) = 0; | |
1223 | ||
4ca5d2a7 NP |
1224 | /* Note that PROPERTY_GETTER_NAME is always set for all |
1225 | PROPERTY_DECLs, and PROPERTY_SETTER_NAME is always set for all | |
1226 | PROPERTY_DECLs where PROPERTY_READONLY == 0. Any time we deal | |
1227 | with a getter or setter, we should get the PROPERTY_DECL and use | |
1228 | PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know the correct | |
1229 | names. */ | |
1230 | ||
46a88c12 NP |
1231 | /* Add the PROPERTY_DECL to the list of properties for the class. */ |
1232 | TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context); | |
1233 | CLASS_PROPERTY_DECL (objc_interface_context) = property_decl; | |
668ea4b1 IS |
1234 | } |
1235 | ||
43236c26 | 1236 | /* This is a subroutine of objc_maybe_build_component_ref. Search the |
a9625a91 NP |
1237 | list of methods in the interface (and, failing that, the local list |
1238 | in the implementation, and failing that, the protocol list) | |
43236c26 NP |
1239 | provided for a 'setter' or 'getter' for 'component' with default |
1240 | names (ie, if 'component' is "name", then search for "name" and | |
2dd24dbd NP |
1241 | "setName:"). It is also possible to specify a different |
1242 | 'getter_name' (this is used for @optional readonly properties). If | |
1243 | any is found, then create an artificial property that uses them. | |
1244 | Return NULL_TREE if 'getter' or 'setter' could not be found. */ | |
43236c26 | 1245 | static tree |
944fb799 | 1246 | maybe_make_artificial_property_decl (tree interface, tree implementation, |
2dd24dbd NP |
1247 | tree protocol_list, tree component, bool is_class, |
1248 | tree getter_name) | |
43236c26 | 1249 | { |
43236c26 NP |
1250 | tree setter_name = get_identifier (objc_build_property_setter_name (component)); |
1251 | tree getter = NULL_TREE; | |
1252 | tree setter = NULL_TREE; | |
1253 | ||
2dd24dbd NP |
1254 | if (getter_name == NULL_TREE) |
1255 | getter_name = component; | |
1256 | ||
a9625a91 | 1257 | /* First, check the @interface and all superclasses. */ |
43236c26 NP |
1258 | if (interface) |
1259 | { | |
1260 | int flags = 0; | |
1261 | ||
a9625a91 NP |
1262 | /* Using instance methods of the root class as accessors is most |
1263 | likely unwanted and can be extremely confusing (and, most | |
1264 | importantly, other Objective-C 2.0 compilers do not do it). | |
1265 | Turn it off. */ | |
43236c26 | 1266 | if (is_class) |
a9625a91 | 1267 | flags = OBJC_LOOKUP_CLASS | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS; |
944fb799 | 1268 | |
43236c26 NP |
1269 | getter = lookup_method_static (interface, getter_name, flags); |
1270 | setter = lookup_method_static (interface, setter_name, flags); | |
1271 | } | |
1272 | ||
a9625a91 NP |
1273 | /* Second, check the local @implementation context. */ |
1274 | if (!getter && !setter) | |
1275 | { | |
1276 | if (implementation) | |
1277 | { | |
1278 | if (is_class) | |
1279 | { | |
1280 | getter = lookup_method (CLASS_CLS_METHODS (implementation), getter_name); | |
1281 | setter = lookup_method (CLASS_CLS_METHODS (implementation), setter_name); | |
1282 | } | |
1283 | else | |
1284 | { | |
1285 | getter = lookup_method (CLASS_NST_METHODS (implementation), getter_name); | |
944fb799 | 1286 | setter = lookup_method (CLASS_NST_METHODS (implementation), setter_name); |
a9625a91 NP |
1287 | } |
1288 | } | |
1289 | } | |
1290 | ||
1291 | /* Try the protocol_list if we didn't find anything in the | |
1292 | @interface and in the @implementation. */ | |
43236c26 NP |
1293 | if (!getter && !setter) |
1294 | { | |
1295 | getter = lookup_method_in_protocol_list (protocol_list, getter_name, is_class); | |
1296 | setter = lookup_method_in_protocol_list (protocol_list, setter_name, is_class); | |
1297 | } | |
1298 | ||
1299 | /* There needs to be at least a getter or setter for this to be a | |
1300 | valid 'object.component' syntax. */ | |
1301 | if (getter || setter) | |
1302 | { | |
1303 | /* Yes ... determine the type of the expression. */ | |
1304 | tree property_decl; | |
1305 | tree type; | |
944fb799 | 1306 | |
43236c26 NP |
1307 | if (getter) |
1308 | type = TREE_VALUE (TREE_TYPE (getter)); | |
1309 | else | |
1310 | type = TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter))); | |
944fb799 | 1311 | |
43236c26 NP |
1312 | /* Create an artificial property declaration with the |
1313 | information we collected on the type and getter/setter | |
1314 | names. */ | |
1315 | property_decl = make_node (PROPERTY_DECL); | |
944fb799 | 1316 | |
43236c26 NP |
1317 | TREE_TYPE (property_decl) = type; |
1318 | DECL_SOURCE_LOCATION (property_decl) = input_location; | |
1319 | TREE_DEPRECATED (property_decl) = 0; | |
1320 | DECL_ARTIFICIAL (property_decl) = 1; | |
b4f588c4 | 1321 | |
43236c26 NP |
1322 | /* Add property-specific information. Note that one of |
1323 | PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a | |
1324 | non-existing method; this will generate an error when the | |
1325 | expression is later compiled. At this stage we don't know if | |
1326 | the getter or setter will be used, so we can't generate an | |
1327 | error. */ | |
1328 | PROPERTY_NAME (property_decl) = component; | |
1329 | PROPERTY_GETTER_NAME (property_decl) = getter_name; | |
1330 | PROPERTY_SETTER_NAME (property_decl) = setter_name; | |
1331 | PROPERTY_READONLY (property_decl) = 0; | |
1332 | PROPERTY_NONATOMIC (property_decl) = 0; | |
1333 | PROPERTY_ASSIGN_SEMANTICS (property_decl) = 0; | |
1334 | PROPERTY_IVAR_NAME (property_decl) = NULL_TREE; | |
1335 | PROPERTY_DYNAMIC (property_decl) = 0; | |
2dd24dbd | 1336 | PROPERTY_OPTIONAL (property_decl) = 0; |
43236c26 NP |
1337 | |
1338 | if (!getter) | |
1339 | PROPERTY_HAS_NO_GETTER (property_decl) = 1; | |
1340 | ||
1341 | /* The following is currently unused, but it's nice to have | |
1342 | there. We may use it if we need in the future. */ | |
1343 | if (!setter) | |
1344 | PROPERTY_HAS_NO_SETTER (property_decl) = 1; | |
1345 | ||
1346 | return property_decl; | |
1347 | } | |
1348 | ||
1349 | return NULL_TREE; | |
1350 | } | |
668ea4b1 | 1351 | |
46a88c12 NP |
1352 | /* This hook routine is invoked by the parser when an expression such |
1353 | as 'xxx.yyy' is parsed. We get a chance to process these | |
1354 | expressions in a way that is specified to Objective-C (to implement | |
43236c26 NP |
1355 | the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars). |
1356 | If the expression is not an Objective-C specified expression, we | |
1357 | should return NULL_TREE; else we return the expression. | |
1358 | ||
1359 | At the moment this only implements dot-syntax and properties (not | |
1360 | non-fragile ivars yet), ie 'object.property' or 'object.component' | |
1361 | where 'component' is not a declared property, but a valid getter or | |
1362 | setter for it could be found. */ | |
668ea4b1 | 1363 | tree |
46a88c12 | 1364 | objc_maybe_build_component_ref (tree object, tree property_ident) |
668ea4b1 IS |
1365 | { |
1366 | tree x = NULL_TREE; | |
1367 | tree rtype; | |
1368 | ||
a9625a91 NP |
1369 | /* If we are in Objective-C 1.0 mode, dot-syntax and properties are |
1370 | not available. */ | |
22d8d616 NP |
1371 | if (flag_objc1_only) |
1372 | return NULL_TREE; | |
1373 | ||
a9625a91 NP |
1374 | /* Try to determine if 'object' is an Objective-C object or not. If |
1375 | not, return. */ | |
944fb799 | 1376 | if (object == NULL_TREE || object == error_mark_node |
46a88c12 | 1377 | || (rtype = TREE_TYPE (object)) == NULL_TREE) |
668ea4b1 | 1378 | return NULL_TREE; |
944fb799 | 1379 | |
46a88c12 NP |
1380 | if (property_ident == NULL_TREE || property_ident == error_mark_node |
1381 | || TREE_CODE (property_ident) != IDENTIFIER_NODE) | |
668ea4b1 IS |
1382 | return NULL_TREE; |
1383 | ||
a9625a91 NP |
1384 | /* The following analysis of 'object' is similar to the one used for |
1385 | the 'receiver' of a method invocation. We need to determine what | |
1386 | 'object' is and find the appropriate property (either declared, | |
1387 | or artificial) for it (in the same way as we need to find the | |
1388 | appropriate method prototype for a method invocation). There are | |
1389 | some simplifications here though: "object.property" is invalid if | |
1390 | "object" has a type of "id" or "Class"; it must at least have a | |
1391 | protocol attached to it, and "object" is never a class name as | |
1392 | that is done by objc_build_class_component_ref. Finally, we | |
1393 | don't know if this really is a dot-syntax expression, so we want | |
1394 | to make a quick exit if it is not; for this reason, we try to | |
1395 | postpone checks after determining that 'object' looks like an | |
1396 | Objective-C object. */ | |
1397 | ||
668ea4b1 IS |
1398 | if (objc_is_id (rtype)) |
1399 | { | |
a9625a91 NP |
1400 | /* This is the case that the 'object' is of type 'id' or |
1401 | 'Class'. */ | |
43236c26 | 1402 | |
a9625a91 NP |
1403 | /* Check if at least it is of type 'id <Protocol>' or 'Class |
1404 | <Protocol>'; if so, look the property up in the | |
1405 | protocols. */ | |
1406 | if (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))) | |
43236c26 | 1407 | { |
a9625a91 | 1408 | tree rprotos = TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype)); |
944fb799 | 1409 | |
a9625a91 NP |
1410 | if (rprotos) |
1411 | { | |
1412 | /* No point looking up declared @properties if we are | |
1413 | dealing with a class. Classes have no declared | |
1414 | properties. */ | |
1415 | if (!IS_CLASS (rtype)) | |
1416 | x = lookup_property_in_protocol_list (rprotos, property_ident); | |
2dd24dbd | 1417 | |
a9625a91 NP |
1418 | if (x == NULL_TREE) |
1419 | { | |
1420 | /* Ok, no property. Maybe it was an | |
1421 | object.component dot-syntax without a declared | |
1422 | property (this is valid for classes too). Look | |
1423 | for getter/setter methods and internally declare | |
587ae1e3 | 1424 | an artificial property based on them if found. */ |
a9625a91 NP |
1425 | x = maybe_make_artificial_property_decl (NULL_TREE, |
1426 | NULL_TREE, | |
944fb799 | 1427 | rprotos, |
a9625a91 | 1428 | property_ident, |
2dd24dbd NP |
1429 | IS_CLASS (rtype), |
1430 | NULL_TREE); | |
1431 | } | |
1432 | else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x)) | |
1433 | { | |
1434 | /* This is a special, complicated case. If the | |
1435 | property is optional, and is read-only, then the | |
1436 | property is always used for reading, but an | |
1437 | eventual existing non-property setter can be used | |
1438 | for writing. We create an artificial property | |
1439 | decl copying the getter from the optional | |
1440 | property, and looking up the setter in the | |
1441 | interface. */ | |
1442 | x = maybe_make_artificial_property_decl (NULL_TREE, | |
1443 | NULL_TREE, | |
1444 | rprotos, | |
1445 | property_ident, | |
1446 | false, | |
944fb799 | 1447 | PROPERTY_GETTER_NAME (x)); |
a9625a91 NP |
1448 | } |
1449 | } | |
1450 | } | |
1451 | else if (objc_method_context) | |
1452 | { | |
1453 | /* Else, if we are inside a method it could be the case of | |
1454 | 'super' or 'self'. */ | |
1455 | tree interface_type = NULL_TREE; | |
1456 | tree t = object; | |
1457 | while (TREE_CODE (t) == COMPOUND_EXPR | |
1458 | || TREE_CODE (t) == MODIFY_EXPR | |
1459 | || CONVERT_EXPR_P (t) | |
1460 | || TREE_CODE (t) == COMPONENT_REF) | |
1461 | t = TREE_OPERAND (t, 0); | |
944fb799 MS |
1462 | |
1463 | if (t == UOBJC_SUPER_decl) | |
d402edc1 | 1464 | interface_type = lookup_interface (CLASS_SUPER_NAME (implementation_template)); |
a9625a91 NP |
1465 | else if (t == self_decl) |
1466 | interface_type = lookup_interface (CLASS_NAME (implementation_template)); | |
1467 | ||
a9625a91 NP |
1468 | if (interface_type) |
1469 | { | |
1470 | if (TREE_CODE (objc_method_context) != CLASS_METHOD_DECL) | |
10e34e6e | 1471 | x = lookup_property (interface_type, property_ident); |
944fb799 | 1472 | |
a9625a91 NP |
1473 | if (x == NULL_TREE) |
1474 | { | |
1475 | /* Try the dot-syntax without a declared property. | |
1476 | If this is an access to 'self', it is possible | |
1477 | that they may refer to a setter/getter that is | |
1478 | not declared in the interface, but exists locally | |
1479 | in the implementation. In that case, get the | |
1480 | implementation context and use it. */ | |
1481 | tree implementation = NULL_TREE; | |
1482 | ||
1483 | if (t == self_decl) | |
1484 | implementation = objc_implementation_context; | |
944fb799 MS |
1485 | |
1486 | x = maybe_make_artificial_property_decl | |
a9625a91 NP |
1487 | (interface_type, implementation, NULL_TREE, |
1488 | property_ident, | |
2dd24dbd NP |
1489 | (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL), |
1490 | NULL_TREE); | |
1491 | } | |
1492 | else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x)) | |
1493 | { | |
1494 | tree implementation = NULL_TREE; | |
944fb799 | 1495 | |
2dd24dbd NP |
1496 | if (t == self_decl) |
1497 | implementation = objc_implementation_context; | |
944fb799 | 1498 | |
2dd24dbd NP |
1499 | x = maybe_make_artificial_property_decl (interface_type, |
1500 | implementation, | |
1501 | NULL_TREE, | |
1502 | property_ident, | |
1503 | false, | |
944fb799 | 1504 | PROPERTY_GETTER_NAME (x)); |
a9625a91 NP |
1505 | } |
1506 | } | |
43236c26 | 1507 | } |
668ea4b1 IS |
1508 | } |
1509 | else | |
1510 | { | |
a9625a91 | 1511 | /* This is the case where we have more information on 'rtype'. */ |
668ea4b1 IS |
1512 | tree basetype = TYPE_MAIN_VARIANT (rtype); |
1513 | ||
a9625a91 NP |
1514 | /* Skip the pointer - if none, it's not an Objective-C object or |
1515 | class. */ | |
668ea4b1 IS |
1516 | if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE) |
1517 | basetype = TREE_TYPE (basetype); | |
1518 | else | |
1519 | return NULL_TREE; | |
1520 | ||
a9625a91 | 1521 | /* Traverse typedefs. */ |
668ea4b1 | 1522 | while (basetype != NULL_TREE |
944fb799 | 1523 | && TREE_CODE (basetype) == RECORD_TYPE |
668ea4b1 IS |
1524 | && OBJC_TYPE_NAME (basetype) |
1525 | && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL | |
1526 | && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype))) | |
1527 | basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)); | |
1528 | ||
1529 | if (basetype != NULL_TREE && TYPED_OBJECT (basetype)) | |
1530 | { | |
1531 | tree interface_type = TYPE_OBJC_INTERFACE (basetype); | |
46a88c12 | 1532 | tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype); |
43236c26 | 1533 | |
944fb799 | 1534 | if (interface_type |
a9625a91 NP |
1535 | && (TREE_CODE (interface_type) == CLASS_INTERFACE_TYPE |
1536 | || TREE_CODE (interface_type) == CATEGORY_INTERFACE_TYPE | |
1537 | || TREE_CODE (interface_type) == PROTOCOL_INTERFACE_TYPE)) | |
43236c26 | 1538 | { |
a9625a91 NP |
1539 | /* Not sure 'rtype' could ever be a class here! Just |
1540 | for safety we keep the checks. */ | |
1541 | if (!IS_CLASS (rtype)) | |
1542 | { | |
1543 | x = lookup_property (interface_type, property_ident); | |
944fb799 | 1544 | |
a9625a91 | 1545 | if (x == NULL_TREE) |
944fb799 | 1546 | x = lookup_property_in_protocol_list (protocol_list, |
a9625a91 NP |
1547 | property_ident); |
1548 | } | |
944fb799 | 1549 | |
a9625a91 NP |
1550 | if (x == NULL_TREE) |
1551 | { | |
1552 | /* Try the dot-syntax without a declared property. | |
1553 | If we are inside a method implementation, it is | |
1554 | possible that they may refer to a setter/getter | |
1555 | that is not declared in the interface, but exists | |
1556 | locally in the implementation. In that case, get | |
1557 | the implementation context and use it. */ | |
1558 | tree implementation = NULL_TREE; | |
1559 | ||
1560 | if (objc_implementation_context | |
944fb799 | 1561 | && CLASS_NAME (objc_implementation_context) |
a9625a91 NP |
1562 | == OBJC_TYPE_NAME (interface_type)) |
1563 | implementation = objc_implementation_context; | |
944fb799 | 1564 | |
a9625a91 NP |
1565 | x = maybe_make_artificial_property_decl (interface_type, |
1566 | implementation, | |
944fb799 | 1567 | protocol_list, |
a9625a91 | 1568 | property_ident, |
2dd24dbd NP |
1569 | IS_CLASS (rtype), |
1570 | NULL_TREE); | |
a9625a91 | 1571 | } |
2dd24dbd NP |
1572 | else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x)) |
1573 | { | |
1574 | tree implementation = NULL_TREE; | |
1575 | ||
1576 | if (objc_implementation_context | |
944fb799 | 1577 | && CLASS_NAME (objc_implementation_context) |
2dd24dbd NP |
1578 | == OBJC_TYPE_NAME (interface_type)) |
1579 | implementation = objc_implementation_context; | |
944fb799 | 1580 | |
2dd24dbd NP |
1581 | x = maybe_make_artificial_property_decl (interface_type, |
1582 | implementation, | |
1583 | protocol_list, | |
1584 | property_ident, | |
1585 | false, | |
944fb799 MS |
1586 | PROPERTY_GETTER_NAME (x)); |
1587 | } | |
43236c26 | 1588 | } |
668ea4b1 IS |
1589 | } |
1590 | } | |
1591 | ||
1592 | if (x) | |
1593 | { | |
46a88c12 | 1594 | tree expression; |
a9625a91 | 1595 | tree getter_call; |
b4f588c4 | 1596 | tree deprecated_method_prototype = NULL_TREE; |
46a88c12 NP |
1597 | |
1598 | /* We have an additional nasty problem here; if this | |
1599 | PROPERTY_REF needs to become a 'getter', then the conversion | |
1600 | from PROPERTY_REF into a getter call happens in gimplify, | |
a9625a91 NP |
1601 | after the selector table has already been generated and when |
1602 | it is too late to add another selector to it. To work around | |
1603 | the problem, we always create the getter call at this stage, | |
1604 | which puts the selector in the table. Note that if the | |
1605 | PROPERTY_REF becomes a 'setter' instead of a 'getter', then | |
1606 | we have added a selector too many to the selector table. | |
1607 | This is a little inefficient. | |
1608 | ||
1609 | Also note that method calls to 'self' and 'super' require the | |
1610 | context (self_decl, UOBJS_SUPER_decl, | |
1611 | objc_implementation_context etc) to be built correctly; this | |
1612 | is yet another reason why building the call at the gimplify | |
1613 | stage (when this context has been lost) is not very | |
1614 | practical. If we build it at this stage, we know it will | |
1615 | always be built correctly. | |
43236c26 NP |
1616 | |
1617 | If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial | |
1618 | property decl created to deal with a dotsyntax not really | |
1619 | referring to an existing property) then do not try to build a | |
1620 | call to the getter as there is no getter. */ | |
a9625a91 NP |
1621 | if (PROPERTY_HAS_NO_GETTER (x)) |
1622 | getter_call = NULL_TREE; | |
1623 | else | |
b4f588c4 NP |
1624 | getter_call = objc_finish_message_expr |
1625 | (object, PROPERTY_GETTER_NAME (x), NULL_TREE, | |
1626 | /* Disable the immediate deprecation warning if the getter | |
1627 | is deprecated, but record the fact that the getter is | |
1628 | deprecated by setting PROPERTY_REF_DEPRECATED_GETTER to | |
1629 | the method prototype. */ | |
1630 | &deprecated_method_prototype); | |
1631 | ||
1632 | expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call, | |
1633 | deprecated_method_prototype); | |
a9625a91 NP |
1634 | SET_EXPR_LOCATION (expression, input_location); |
1635 | TREE_SIDE_EFFECTS (expression) = 1; | |
944fb799 | 1636 | |
46a88c12 | 1637 | return expression; |
668ea4b1 | 1638 | } |
668ea4b1 | 1639 | |
46a88c12 | 1640 | return NULL_TREE; |
668ea4b1 IS |
1641 | } |
1642 | ||
bede2adc NP |
1643 | /* This hook routine is invoked by the parser when an expression such |
1644 | as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the | |
1645 | Objective-C 2.0 dot-syntax applied to classes, so we need to | |
1646 | convert it into a setter/getter call on the class. */ | |
1647 | tree | |
1648 | objc_build_class_component_ref (tree class_name, tree property_ident) | |
1649 | { | |
1650 | tree x = NULL_TREE; | |
1651 | tree object, rtype; | |
944fb799 | 1652 | |
bede2adc NP |
1653 | if (flag_objc1_only) |
1654 | error_at (input_location, "the dot syntax is not available in Objective-C 1.0"); | |
944fb799 | 1655 | |
bede2adc NP |
1656 | if (class_name == NULL_TREE || class_name == error_mark_node |
1657 | || TREE_CODE (class_name) != IDENTIFIER_NODE) | |
1658 | return error_mark_node; | |
944fb799 | 1659 | |
bede2adc NP |
1660 | if (property_ident == NULL_TREE || property_ident == error_mark_node |
1661 | || TREE_CODE (property_ident) != IDENTIFIER_NODE) | |
1662 | return NULL_TREE; | |
944fb799 | 1663 | |
bede2adc NP |
1664 | object = objc_get_class_reference (class_name); |
1665 | if (!object) | |
1666 | { | |
1667 | /* We know that 'class_name' is an Objective-C class name as the | |
1668 | parser won't call this function if it is not. This is only a | |
1669 | double-check for safety. */ | |
944fb799 | 1670 | error_at (input_location, "could not find class %qE", class_name); |
bede2adc NP |
1671 | return error_mark_node; |
1672 | } | |
1673 | ||
1674 | rtype = lookup_interface (class_name); | |
1675 | if (!rtype) | |
1676 | { | |
1677 | /* Again, this should never happen, but we do check. */ | |
944fb799 | 1678 | error_at (input_location, "could not find interface for class %qE", class_name); |
bede2adc NP |
1679 | return error_mark_node; |
1680 | } | |
d4e72c58 NP |
1681 | else |
1682 | { | |
1683 | if (TREE_DEPRECATED (rtype)) | |
944fb799 | 1684 | warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", class_name); |
d4e72c58 | 1685 | } |
bede2adc | 1686 | |
a9625a91 | 1687 | x = maybe_make_artificial_property_decl (rtype, NULL_TREE, NULL_TREE, |
bede2adc | 1688 | property_ident, |
2dd24dbd | 1689 | true, NULL_TREE); |
944fb799 | 1690 | |
bede2adc NP |
1691 | if (x) |
1692 | { | |
1693 | tree expression; | |
a9625a91 | 1694 | tree getter_call; |
b4f588c4 | 1695 | tree deprecated_method_prototype = NULL_TREE; |
bede2adc | 1696 | |
a9625a91 NP |
1697 | if (PROPERTY_HAS_NO_GETTER (x)) |
1698 | getter_call = NULL_TREE; | |
1699 | else | |
b4f588c4 NP |
1700 | getter_call = objc_finish_message_expr |
1701 | (object, PROPERTY_GETTER_NAME (x), NULL_TREE, | |
1702 | &deprecated_method_prototype); | |
bede2adc | 1703 | |
b4f588c4 NP |
1704 | expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call, |
1705 | deprecated_method_prototype); | |
bede2adc NP |
1706 | SET_EXPR_LOCATION (expression, input_location); |
1707 | TREE_SIDE_EFFECTS (expression) = 1; | |
a9625a91 | 1708 | |
bede2adc NP |
1709 | return expression; |
1710 | } | |
1711 | else | |
1712 | { | |
944fb799 MS |
1713 | error_at (input_location, "could not find setter/getter for %qE in class %qE", |
1714 | property_ident, class_name); | |
bede2adc NP |
1715 | return error_mark_node; |
1716 | } | |
1717 | ||
1718 | return NULL_TREE; | |
1719 | } | |
1720 | ||
1721 | ||
1722 | ||
46a88c12 NP |
1723 | /* This is used because we don't want to expose PROPERTY_REF to the |
1724 | C/C++ frontends. Maybe we should! */ | |
1725 | bool | |
1726 | objc_is_property_ref (tree node) | |
668ea4b1 | 1727 | { |
46a88c12 NP |
1728 | if (node && TREE_CODE (node) == PROPERTY_REF) |
1729 | return true; | |
668ea4b1 | 1730 | else |
46a88c12 | 1731 | return false; |
668ea4b1 IS |
1732 | } |
1733 | ||
b6cfe8ac NP |
1734 | /* This function builds a setter call for a PROPERTY_REF (real, for a |
1735 | declared property, or artificial, for a dot-syntax accessor which | |
1736 | is not corresponding to a property). 'lhs' must be a PROPERTY_REF | |
1737 | (the caller must check this beforehand). 'rhs' is the value to | |
1738 | assign to the property. A plain setter call is returned, or | |
1739 | error_mark_node if the property is readonly. */ | |
1740 | ||
1741 | static tree | |
1742 | objc_build_setter_call (tree lhs, tree rhs) | |
1743 | { | |
1744 | tree object_expr = PROPERTY_REF_OBJECT (lhs); | |
1745 | tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs); | |
944fb799 | 1746 | |
b6cfe8ac NP |
1747 | if (PROPERTY_READONLY (property_decl)) |
1748 | { | |
944fb799 | 1749 | error ("readonly property can not be set"); |
b6cfe8ac NP |
1750 | return error_mark_node; |
1751 | } | |
1752 | else | |
1753 | { | |
1754 | tree setter_argument = build_tree_list (NULL_TREE, rhs); | |
1755 | tree setter; | |
944fb799 | 1756 | |
b6cfe8ac NP |
1757 | /* TODO: Check that the setter return type is 'void'. */ |
1758 | ||
1759 | /* TODO: Decay arguments in C. */ | |
944fb799 | 1760 | setter = objc_finish_message_expr (object_expr, |
b6cfe8ac | 1761 | PROPERTY_SETTER_NAME (property_decl), |
b4f588c4 | 1762 | setter_argument, NULL); |
b6cfe8ac NP |
1763 | return setter; |
1764 | } | |
1765 | ||
1766 | /* Unreachable, but the compiler may not realize. */ | |
1767 | return error_mark_node; | |
1768 | } | |
1769 | ||
46a88c12 NP |
1770 | /* This hook routine is called when a MODIFY_EXPR is being built. We |
1771 | check what is being modified; if it is a PROPERTY_REF, we need to | |
1772 | generate a 'setter' function call for the property. If this is not | |
1773 | a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go | |
1774 | on creating their MODIFY_EXPR. | |
668ea4b1 | 1775 | |
46a88c12 | 1776 | This is used for example if you write |
668ea4b1 | 1777 | |
46a88c12 | 1778 | object.count = 1; |
668ea4b1 | 1779 | |
46a88c12 NP |
1780 | where 'count' is a property. The left-hand side creates a |
1781 | PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR | |
1782 | to assign something to it. We intercept that here, and generate a | |
1783 | call to the 'setter' method instead. */ | |
1784 | tree | |
1785 | objc_maybe_build_modify_expr (tree lhs, tree rhs) | |
668ea4b1 | 1786 | { |
46a88c12 | 1787 | if (lhs && TREE_CODE (lhs) == PROPERTY_REF) |
668ea4b1 | 1788 | { |
b6cfe8ac | 1789 | /* Building a simple call to the setter method would work for cases such as |
668ea4b1 | 1790 | |
b6cfe8ac | 1791 | object.count = 1; |
43236c26 | 1792 | |
b6cfe8ac | 1793 | but wouldn't work for cases such as |
43236c26 | 1794 | |
b6cfe8ac NP |
1795 | count = object2.count = 1; |
1796 | ||
1797 | to get these to work with very little effort, we build a | |
1798 | compound statement which does the setter call (to set the | |
1799 | property to 'rhs'), but which can also be evaluated returning | |
3890ce93 NP |
1800 | the 'rhs'. If the 'rhs' has no side effects, we can simply |
1801 | evaluate it twice, building | |
1802 | ||
1803 | ([object setProperty: rhs]; rhs) | |
1804 | ||
1805 | If it has side effects, we put it in a temporary variable first, | |
1806 | so we create the following: | |
b6cfe8ac NP |
1807 | |
1808 | (temp = rhs; [object setProperty: temp]; temp) | |
3890ce93 NP |
1809 | |
1810 | setter_argument is rhs in the first case, and temp in the second | |
1811 | case. | |
b6cfe8ac | 1812 | */ |
3890ce93 NP |
1813 | tree setter_argument; |
1814 | ||
b6cfe8ac NP |
1815 | /* s1, s2 and s3 are the tree statements that we need in the |
1816 | compound expression. */ | |
1817 | tree s1, s2, s3, compound_expr; | |
3890ce93 NP |
1818 | |
1819 | if (TREE_SIDE_EFFECTS (rhs)) | |
1820 | { | |
1821 | tree bind; | |
944fb799 | 1822 | |
3890ce93 NP |
1823 | /* Declare __objc_property_temp in a local bind. */ |
1824 | setter_argument = objc_create_temporary_var (TREE_TYPE (rhs), "__objc_property_temp"); | |
1825 | DECL_SOURCE_LOCATION (setter_argument) = input_location; | |
1826 | bind = build3 (BIND_EXPR, void_type_node, setter_argument, NULL, NULL); | |
1827 | SET_EXPR_LOCATION (bind, input_location); | |
1828 | TREE_SIDE_EFFECTS (bind) = 1; | |
1829 | add_stmt (bind); | |
1830 | ||
1831 | /* s1: x = rhs */ | |
1832 | s1 = build_modify_expr (input_location, setter_argument, NULL_TREE, | |
1833 | NOP_EXPR, | |
1834 | input_location, rhs, NULL_TREE); | |
1835 | SET_EXPR_LOCATION (s1, input_location); | |
1836 | } | |
1837 | else | |
1838 | { | |
1839 | /* No s1. */ | |
1840 | setter_argument = rhs; | |
1841 | s1 = NULL_TREE; | |
1842 | } | |
944fb799 | 1843 | |
b6cfe8ac | 1844 | /* Now build the compound statement. */ |
944fb799 | 1845 | |
3890ce93 NP |
1846 | /* s2: [object setProperty: x] */ |
1847 | s2 = objc_build_setter_call (lhs, setter_argument); | |
944fb799 | 1848 | |
3890ce93 NP |
1849 | /* This happens if building the setter failed because the |
1850 | property is readonly. */ | |
b6cfe8ac NP |
1851 | if (s2 == error_mark_node) |
1852 | return error_mark_node; | |
1853 | ||
1854 | SET_EXPR_LOCATION (s2, input_location); | |
944fb799 | 1855 | |
3890ce93 NP |
1856 | /* s3: x */ |
1857 | s3 = convert (TREE_TYPE (lhs), setter_argument); | |
b6cfe8ac | 1858 | |
3890ce93 NP |
1859 | /* Now build the compound statement (s1, s2, s3) or (s2, s3) as |
1860 | appropriate. */ | |
1861 | if (s1) | |
1862 | compound_expr = build_compound_expr (input_location, build_compound_expr (input_location, s1, s2), s3); | |
1863 | else | |
944fb799 | 1864 | compound_expr = build_compound_expr (input_location, s2, s3); |
b6cfe8ac NP |
1865 | |
1866 | /* Without this, with -Wall you get a 'valued computed is not | |
1867 | used' every time there is a "object.property = x" where the | |
1868 | value of the resulting MODIFY_EXPR is not used. That is | |
1869 | correct (maybe a more sophisticated implementation could | |
1870 | avoid generating the compound expression if not needed), but | |
1871 | we need to turn it off. */ | |
1872 | TREE_NO_WARNING (compound_expr) = 1; | |
1873 | return compound_expr; | |
46a88c12 NP |
1874 | } |
1875 | else | |
1876 | return NULL_TREE; | |
668ea4b1 IS |
1877 | } |
1878 | ||
925e8657 NP |
1879 | /* This hook is called by the frontend when one of the four unary |
1880 | expressions PREINCREMENT_EXPR, POSTINCREMENT_EXPR, | |
1881 | PREDECREMENT_EXPR and POSTDECREMENT_EXPR is being built with an | |
1882 | argument which is a PROPERTY_REF. For example, this happens if you have | |
1883 | ||
1884 | object.count++; | |
1885 | ||
1886 | where 'count' is a property. We need to use the 'getter' and | |
1887 | 'setter' for the property in an appropriate way to build the | |
1888 | appropriate expression. 'code' is the code for the expression (one | |
1889 | of the four mentioned above); 'argument' is the PROPERTY_REF, and | |
944fb799 | 1890 | 'increment' is how much we need to add or subtract. */ |
925e8657 NP |
1891 | tree |
1892 | objc_build_incr_expr_for_property_ref (location_t location, | |
944fb799 | 1893 | enum tree_code code, |
925e8657 NP |
1894 | tree argument, tree increment) |
1895 | { | |
1896 | /* Here are the expressions that we want to build: | |
1897 | ||
1898 | For PREINCREMENT_EXPR / PREDECREMENT_EXPR: | |
1899 | (temp = [object property] +/- increment, [object setProperty: temp], temp) | |
944fb799 | 1900 | |
925e8657 NP |
1901 | For POSTINCREMENT_EXPR / POSTECREMENT_EXPR: |
1902 | (temp = [object property], [object setProperty: temp +/- increment], temp) */ | |
944fb799 | 1903 | |
925e8657 NP |
1904 | tree temp_variable_decl, bind; |
1905 | /* s1, s2 and s3 are the tree statements that we need in the | |
1906 | compound expression. */ | |
b6cfe8ac | 1907 | tree s1, s2, s3, compound_expr; |
944fb799 | 1908 | |
925e8657 NP |
1909 | /* Safety check. */ |
1910 | if (!argument || TREE_CODE (argument) != PROPERTY_REF) | |
1911 | return error_mark_node; | |
1912 | ||
1913 | /* Declare __objc_property_temp in a local bind. */ | |
1914 | temp_variable_decl = objc_create_temporary_var (TREE_TYPE (argument), "__objc_property_temp"); | |
1915 | DECL_SOURCE_LOCATION (temp_variable_decl) = location; | |
1916 | bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL); | |
1917 | SET_EXPR_LOCATION (bind, location); | |
1918 | TREE_SIDE_EFFECTS (bind) = 1; | |
1919 | add_stmt (bind); | |
944fb799 | 1920 | |
925e8657 | 1921 | /* Now build the compound statement. */ |
944fb799 | 1922 | |
925e8657 NP |
1923 | /* Note that the 'getter' is generated at gimplify time; at this |
1924 | time, we can simply put the property_ref (ie, argument) wherever | |
1925 | we want the getter ultimately to be. */ | |
944fb799 | 1926 | |
925e8657 NP |
1927 | /* s1: __objc_property_temp = [object property] <+/- increment> */ |
1928 | switch (code) | |
1929 | { | |
944fb799 | 1930 | case PREINCREMENT_EXPR: |
925e8657 | 1931 | /* __objc_property_temp = [object property] + increment */ |
b6cfe8ac NP |
1932 | s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE, |
1933 | NOP_EXPR, | |
944fb799 | 1934 | location, build2 (PLUS_EXPR, TREE_TYPE (argument), |
b6cfe8ac | 1935 | argument, increment), NULL_TREE); |
925e8657 NP |
1936 | break; |
1937 | case PREDECREMENT_EXPR: | |
1938 | /* __objc_property_temp = [object property] - increment */ | |
b6cfe8ac NP |
1939 | s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE, |
1940 | NOP_EXPR, | |
944fb799 | 1941 | location, build2 (MINUS_EXPR, TREE_TYPE (argument), |
b6cfe8ac | 1942 | argument, increment), NULL_TREE); |
925e8657 NP |
1943 | break; |
1944 | case POSTINCREMENT_EXPR: | |
1945 | case POSTDECREMENT_EXPR: | |
1946 | /* __objc_property_temp = [object property] */ | |
b6cfe8ac NP |
1947 | s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE, |
1948 | NOP_EXPR, | |
1949 | location, argument, NULL_TREE); | |
925e8657 NP |
1950 | break; |
1951 | default: | |
1952 | gcc_unreachable (); | |
1953 | } | |
944fb799 | 1954 | |
925e8657 NP |
1955 | /* s2: [object setProperty: __objc_property_temp <+/- increment>] */ |
1956 | switch (code) | |
1957 | { | |
944fb799 | 1958 | case PREINCREMENT_EXPR: |
925e8657 NP |
1959 | case PREDECREMENT_EXPR: |
1960 | /* [object setProperty: __objc_property_temp] */ | |
b6cfe8ac | 1961 | s2 = objc_build_setter_call (argument, temp_variable_decl); |
925e8657 NP |
1962 | break; |
1963 | case POSTINCREMENT_EXPR: | |
1964 | /* [object setProperty: __objc_property_temp + increment] */ | |
b6cfe8ac | 1965 | s2 = objc_build_setter_call (argument, |
944fb799 | 1966 | build2 (PLUS_EXPR, TREE_TYPE (argument), |
b6cfe8ac | 1967 | temp_variable_decl, increment)); |
925e8657 NP |
1968 | break; |
1969 | case POSTDECREMENT_EXPR: | |
1970 | /* [object setProperty: __objc_property_temp - increment] */ | |
b6cfe8ac | 1971 | s2 = objc_build_setter_call (argument, |
944fb799 | 1972 | build2 (MINUS_EXPR, TREE_TYPE (argument), |
b6cfe8ac | 1973 | temp_variable_decl, increment)); |
925e8657 NP |
1974 | break; |
1975 | default: | |
1976 | gcc_unreachable (); | |
1977 | } | |
1978 | ||
1979 | /* This happens if building the setter failed because the property | |
1980 | is readonly. */ | |
1981 | if (s2 == error_mark_node) | |
1982 | return error_mark_node; | |
1983 | ||
944fb799 MS |
1984 | SET_EXPR_LOCATION (s2, location); |
1985 | ||
925e8657 | 1986 | /* s3: __objc_property_temp */ |
b6cfe8ac | 1987 | s3 = convert (TREE_TYPE (argument), temp_variable_decl); |
944fb799 | 1988 | |
925e8657 | 1989 | /* Now build the compound statement (s1, s2, s3) */ |
b6cfe8ac NP |
1990 | compound_expr = build_compound_expr (location, build_compound_expr (location, s1, s2), s3); |
1991 | ||
1992 | /* Prevent C++ from warning with -Wall that "right operand of comma | |
1993 | operator has no effect". */ | |
1994 | TREE_NO_WARNING (compound_expr) = 1; | |
1995 | return compound_expr; | |
925e8657 NP |
1996 | } |
1997 | ||
6408ef33 | 1998 | tree |
249a82c4 | 1999 | objc_build_method_signature (bool is_class_method, tree rettype, tree selector, |
dbb74365 | 2000 | tree optparms, bool ellipsis) |
6408ef33 | 2001 | { |
249a82c4 NP |
2002 | if (is_class_method) |
2003 | return build_method_decl (CLASS_METHOD_DECL, rettype, selector, | |
2004 | optparms, ellipsis); | |
2005 | else | |
2006 | return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector, | |
2007 | optparms, ellipsis); | |
6408ef33 ZL |
2008 | } |
2009 | ||
2010 | void | |
249a82c4 | 2011 | objc_add_method_declaration (bool is_class_method, tree decl, tree attributes) |
6408ef33 ZL |
2012 | { |
2013 | if (!objc_interface_context) | |
45547c7f NP |
2014 | { |
2015 | /* PS: At the moment, due to how the parser works, it should be | |
2016 | impossible to get here. But it's good to have the check in | |
2017 | case the parser changes. | |
2018 | */ | |
2019 | fatal_error ("method declaration not in @interface context"); | |
2020 | } | |
6408ef33 | 2021 | |
22d8d616 NP |
2022 | if (flag_objc1_only && attributes) |
2023 | error_at (input_location, "method attributes are not available in Objective-C 1.0"); | |
2024 | ||
a1178b30 | 2025 | objc_decl_method_attributes (&decl, attributes, 0); |
6408ef33 ZL |
2026 | objc_add_method (objc_interface_context, |
2027 | decl, | |
249a82c4 | 2028 | is_class_method, |
92902b1b | 2029 | objc_method_optional_flag); |
6408ef33 ZL |
2030 | } |
2031 | ||
45547c7f NP |
2032 | /* Return 'true' if the method definition could be started, and |
2033 | 'false' if not (because we are outside an @implementation context). | |
a04a722b JM |
2034 | EXPR is NULL or an expression that needs to be evaluated for the |
2035 | side effects of array size expressions in the parameters. | |
45547c7f NP |
2036 | */ |
2037 | bool | |
a04a722b JM |
2038 | objc_start_method_definition (bool is_class_method, tree decl, tree attributes, |
2039 | tree expr) | |
6408ef33 ZL |
2040 | { |
2041 | if (!objc_implementation_context) | |
45547c7f NP |
2042 | { |
2043 | error ("method definition not in @implementation context"); | |
2044 | return false; | |
2045 | } | |
6408ef33 | 2046 | |
a26d8862 NP |
2047 | if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node) |
2048 | return false; | |
2049 | ||
0f185d6e NP |
2050 | #ifndef OBJCPLUS |
2051 | /* Indicate no valid break/continue context by setting these variables | |
2052 | to some non-null, non-label value. We'll notice and emit the proper | |
2053 | error message in c_finish_bc_stmt. */ | |
2054 | c_break_label = c_cont_label = size_zero_node; | |
2055 | #endif | |
2056 | ||
2debdb4f NP |
2057 | if (attributes) |
2058 | warning_at (input_location, 0, "method attributes can not be specified in @implementation context"); | |
2059 | else | |
2060 | objc_decl_method_attributes (&decl, attributes, 0); | |
2061 | ||
6408ef33 ZL |
2062 | objc_add_method (objc_implementation_context, |
2063 | decl, | |
249a82c4 | 2064 | is_class_method, |
92902b1b | 2065 | /* is optional */ false); |
a04a722b | 2066 | start_method_def (decl, expr); |
45547c7f | 2067 | return true; |
6408ef33 ZL |
2068 | } |
2069 | ||
2070 | void | |
2071 | objc_add_instance_variable (tree decl) | |
2072 | { | |
2073 | (void) add_instance_variable (objc_ivar_context, | |
c37d8c30 | 2074 | objc_ivar_visibility, |
6408ef33 ZL |
2075 | decl); |
2076 | } | |
2077 | ||
5025f379 | 2078 | /* Construct a C struct with same name as KLASS, a base struct with tag |
d11dd684 | 2079 | SUPER_NAME (if any), and FIELDS indicated. */ |
3f16185f | 2080 | |
d11dd684 | 2081 | static tree |
5025f379 | 2082 | objc_build_struct (tree klass, tree fields, tree super_name) |
d11dd684 | 2083 | { |
5025f379 | 2084 | tree name = CLASS_NAME (klass); |
24b97832 | 2085 | tree s = objc_start_struct (name); |
d11dd684 | 2086 | tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE); |
3e78185c | 2087 | tree t; |
6e1aa848 | 2088 | vec<tree> objc_info = vNULL; |
3e78185c | 2089 | int i; |
d11dd684 ZL |
2090 | |
2091 | if (super) | |
2092 | { | |
2093 | /* Prepend a packed variant of the base class into the layout. This | |
2094 | is necessary to preserve ObjC ABI compatibility. */ | |
c2255bc4 AH |
2095 | tree base = build_decl (input_location, |
2096 | FIELD_DECL, NULL_TREE, super); | |
d11dd684 ZL |
2097 | tree field = TYPE_FIELDS (super); |
2098 | ||
910ad8de NF |
2099 | while (field && DECL_CHAIN (field) |
2100 | && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL) | |
2101 | field = DECL_CHAIN (field); | |
d11dd684 | 2102 | |
39a13be5 | 2103 | /* For ObjC ABI purposes, the "packed" size of a base class is |
c64de75f ZL |
2104 | the sum of the offset and the size (in bits) of the last field |
2105 | in the class. */ | |
d11dd684 ZL |
2106 | DECL_SIZE (base) |
2107 | = (field && TREE_CODE (field) == FIELD_DECL | |
64ee9490 | 2108 | ? size_binop (PLUS_EXPR, |
d11dd684 ZL |
2109 | size_binop (PLUS_EXPR, |
2110 | size_binop | |
2111 | (MULT_EXPR, | |
2112 | convert (bitsizetype, | |
2113 | DECL_FIELD_OFFSET (field)), | |
2114 | bitsize_int (BITS_PER_UNIT)), | |
2115 | DECL_FIELD_BIT_OFFSET (field)), | |
2116 | DECL_SIZE (field)) | |
2117 | : bitsize_zero_node); | |
2118 | DECL_SIZE_UNIT (base) | |
2119 | = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)), | |
2120 | size_int (BITS_PER_UNIT)); | |
2121 | DECL_ARTIFICIAL (base) = 1; | |
2122 | DECL_ALIGN (base) = 1; | |
2123 | DECL_FIELD_CONTEXT (base) = s; | |
2124 | #ifdef OBJCPLUS | |
2125 | DECL_FIELD_IS_BASE (base) = 1; | |
2126 | ||
2127 | if (fields) | |
2128 | TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */ | |
2129 | #endif /* are following the ObjC ABI here. */ | |
910ad8de | 2130 | DECL_CHAIN (base) = fields; |
d11dd684 ZL |
2131 | fields = base; |
2132 | } | |
2133 | ||
a693d3a8 NP |
2134 | /* NB: Calling finish_struct() may cause type TYPE_OBJC_INFO |
2135 | information in all variants of this RECORD_TYPE to be destroyed | |
2136 | (this is because the C frontend manipulates TYPE_LANG_SPECIFIC | |
2137 | for something else and then will change all variants to use the | |
2138 | same resulting TYPE_LANG_SPECIFIC, ignoring the fact that we use | |
2139 | it for ObjC protocols and that such propagation will make all | |
2140 | variants use the same objc_info), but it is therein that we store | |
2141 | protocol conformance info (e.g., 'NSObject <MyProtocol>'). | |
2142 | Hence, we must save the ObjC-specific information before calling | |
2aba33dd ZL |
2143 | finish_struct(), and then reinstate it afterwards. */ |
2144 | ||
a693d3a8 | 2145 | for (t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t)) |
76dcaf33 | 2146 | { |
a693d3a8 | 2147 | INIT_TYPE_OBJC_INFO (t); |
9771b263 | 2148 | objc_info.safe_push (TYPE_OBJC_INFO (t)); |
76dcaf33 | 2149 | } |
2aba33dd | 2150 | |
24b97832 | 2151 | s = objc_finish_struct (s, fields); |
d11dd684 | 2152 | |
a693d3a8 | 2153 | for (i = 0, t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++) |
a0e71127 | 2154 | { |
a693d3a8 NP |
2155 | /* We now want to restore the different TYPE_OBJC_INFO, but we |
2156 | have the additional problem that the C frontend doesn't just | |
2157 | copy TYPE_LANG_SPECIFIC from one variant to the other; it | |
2158 | actually makes all of them the *same* TYPE_LANG_SPECIFIC. As | |
2159 | we need a different TYPE_OBJC_INFO for each (and | |
2160 | TYPE_OBJC_INFO is a field in TYPE_LANG_SPECIFIC), we need to | |
2161 | make a copy of each TYPE_LANG_SPECIFIC before we modify | |
2162 | TYPE_OBJC_INFO. */ | |
2163 | if (TYPE_LANG_SPECIFIC (t)) | |
2164 | { | |
2165 | /* Create a copy of TYPE_LANG_SPECIFIC. */ | |
2166 | struct lang_type *old_lang_type = TYPE_LANG_SPECIFIC (t); | |
2167 | ALLOC_OBJC_TYPE_LANG_SPECIFIC (t); | |
2168 | memcpy (TYPE_LANG_SPECIFIC (t), old_lang_type, | |
2169 | SIZEOF_OBJC_TYPE_LANG_SPECIFIC); | |
2170 | } | |
2171 | else | |
2172 | { | |
2173 | /* Just create a new one. */ | |
2174 | ALLOC_OBJC_TYPE_LANG_SPECIFIC (t); | |
2175 | } | |
2176 | /* Replace TYPE_OBJC_INFO with the saved one. This restores any | |
2177 | protocol information that may have been associated with the | |
2178 | type. */ | |
9771b263 | 2179 | TYPE_OBJC_INFO (t) = objc_info[i]; |
a693d3a8 NP |
2180 | /* Replace the IDENTIFIER_NODE with an actual @interface now |
2181 | that we have it. */ | |
5025f379 | 2182 | TYPE_OBJC_INTERFACE (t) = klass; |
a0e71127 | 2183 | } |
9771b263 | 2184 | objc_info.release (); |
2aba33dd | 2185 | |
d11dd684 ZL |
2186 | /* Use TYPE_BINFO structures to point at the super class, if any. */ |
2187 | objc_xref_basetypes (s, super); | |
2188 | ||
a0e71127 | 2189 | /* Mark this struct as a class template. */ |
5025f379 | 2190 | CLASS_STATIC_TEMPLATE (klass) = s; |
a0e71127 | 2191 | |
d11dd684 ZL |
2192 | return s; |
2193 | } | |
2194 | ||
2195 | /* Mark DECL as being 'volatile' for purposes of Darwin | |
2196 | _setjmp()/_longjmp() exception handling. Called from | |
2197 | objc_mark_locals_volatile(). */ | |
2198 | void | |
2199 | objc_volatilize_decl (tree decl) | |
2200 | { | |
2201 | /* Do not mess with variables that are 'static' or (already) | |
2202 | 'volatile'. */ | |
2203 | if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl) | |
2204 | && (TREE_CODE (decl) == VAR_DECL | |
2205 | || TREE_CODE (decl) == PARM_DECL)) | |
2206 | { | |
6c39e757 | 2207 | if (local_variables_to_volatilize == NULL) |
9771b263 | 2208 | vec_alloc (local_variables_to_volatilize, 8); |
d11dd684 | 2209 | |
9771b263 | 2210 | vec_safe_push (local_variables_to_volatilize, decl); |
6c39e757 NP |
2211 | } |
2212 | } | |
d11dd684 | 2213 | |
6c39e757 NP |
2214 | /* Called when parsing of a function completes; if any local variables |
2215 | in the function were marked as variables to volatilize, change them | |
2216 | to volatile. We do this at the end of the function when the | |
2217 | warnings about discarding 'volatile' have already been produced. | |
2218 | We are making the variables as volatile just to force the compiler | |
2219 | to preserve them between setjmp/longjmp, but we don't want warnings | |
2220 | for them as they aren't really volatile. */ | |
2221 | void | |
2222 | objc_finish_function (void) | |
2223 | { | |
2224 | /* If there are any local variables to volatilize, volatilize them. */ | |
2225 | if (local_variables_to_volatilize) | |
2226 | { | |
2227 | int i; | |
2228 | tree decl; | |
9771b263 | 2229 | FOR_EACH_VEC_ELT (*local_variables_to_volatilize, i, decl) |
6c39e757 NP |
2230 | { |
2231 | tree t = TREE_TYPE (decl); | |
2232 | ||
2233 | t = build_qualified_type (t, TYPE_QUALS (t) | TYPE_QUAL_VOLATILE); | |
2234 | TREE_TYPE (decl) = t; | |
2235 | TREE_THIS_VOLATILE (decl) = 1; | |
2236 | TREE_SIDE_EFFECTS (decl) = 1; | |
2237 | DECL_REGISTER (decl) = 0; | |
d11dd684 | 2238 | #ifndef OBJCPLUS |
6c39e757 | 2239 | C_DECL_REGISTER (decl) = 0; |
d11dd684 | 2240 | #endif |
6c39e757 NP |
2241 | } |
2242 | ||
2243 | /* Now we delete the vector. This sets it to NULL as well. */ | |
9771b263 | 2244 | vec_free (local_variables_to_volatilize); |
d11dd684 ZL |
2245 | } |
2246 | } | |
2247 | ||
2248 | /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS | |
c9819cb2 | 2249 | (including its categories and superclasses) or by object type TYP. |
d11dd684 ZL |
2250 | Issue a warning if PROTO is not adopted anywhere and WARN is set. */ |
2251 | ||
2252 | static bool | |
2253 | objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn) | |
3f16185f | 2254 | { |
d11dd684 | 2255 | bool class_type = (cls != NULL_TREE); |
3f16185f | 2256 | |
d11dd684 | 2257 | while (cls) |
3f16185f | 2258 | { |
d11dd684 ZL |
2259 | tree c; |
2260 | ||
2261 | /* Check protocols adopted by the class and its categories. */ | |
2262 | for (c = cls; c; c = CLASS_CATEGORY_LIST (c)) | |
2263 | { | |
2264 | if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto)) | |
2265 | return true; | |
2266 | } | |
2267 | ||
2268 | /* Repeat for superclasses. */ | |
2269 | cls = lookup_interface (CLASS_SUPER_NAME (cls)); | |
3f16185f | 2270 | } |
d11dd684 ZL |
2271 | |
2272 | /* Check for any protocols attached directly to the object type. */ | |
2273 | if (TYPE_HAS_OBJC_INFO (typ)) | |
3f16185f | 2274 | { |
d11dd684 ZL |
2275 | if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto)) |
2276 | return true; | |
2277 | } | |
2278 | ||
2279 | if (warn) | |
2280 | { | |
f41c4af3 | 2281 | *errbuf = 0; |
d11dd684 | 2282 | gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ)); |
d11dd684 ZL |
2283 | /* NB: Types 'id' and 'Class' cannot reasonably be described as |
2284 | "implementing" a given protocol, since they do not have an | |
2285 | implementation. */ | |
f41c4af3 JM |
2286 | if (class_type) |
2287 | warning (0, "class %qs does not implement the %qE protocol", | |
2288 | identifier_to_locale (errbuf), PROTOCOL_NAME (proto)); | |
2289 | else | |
2290 | warning (0, "type %qs does not conform to the %qE protocol", | |
2291 | identifier_to_locale (errbuf), PROTOCOL_NAME (proto)); | |
3f16185f | 2292 | } |
d11dd684 ZL |
2293 | |
2294 | return false; | |
3f16185f AM |
2295 | } |
2296 | ||
d11dd684 ZL |
2297 | /* Check if class RCLS and instance struct type RTYP conform to at least the |
2298 | same protocols that LCLS and LTYP conform to. */ | |
2299 | ||
2300 | static bool | |
2301 | objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn) | |
2302 | { | |
2303 | tree p; | |
2304 | bool have_lproto = false; | |
2305 | ||
2306 | while (lcls) | |
2307 | { | |
2308 | /* NB: We do _not_ look at categories defined for LCLS; these may or | |
2309 | may not get loaded in, and therefore it is unreasonable to require | |
2310 | that RCLS/RTYP must implement any of their protocols. */ | |
2311 | for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p)) | |
2312 | { | |
2313 | have_lproto = true; | |
3f16185f | 2314 | |
d11dd684 ZL |
2315 | if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn)) |
2316 | return warn; | |
2317 | } | |
1074d9d4 | 2318 | |
d11dd684 ZL |
2319 | /* Repeat for superclasses. */ |
2320 | lcls = lookup_interface (CLASS_SUPER_NAME (lcls)); | |
2321 | } | |
1074d9d4 | 2322 | |
d11dd684 ZL |
2323 | /* Check for any protocols attached directly to the object type. */ |
2324 | if (TYPE_HAS_OBJC_INFO (ltyp)) | |
2325 | { | |
2326 | for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p)) | |
2327 | { | |
2328 | have_lproto = true; | |
392202b0 | 2329 | |
d11dd684 ZL |
2330 | if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn)) |
2331 | return warn; | |
2332 | } | |
2333 | } | |
2334 | ||
2335 | /* NB: If LTYP and LCLS have no protocols to search for, return 'true' | |
2336 | vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get | |
2337 | away with simply checking for 'id' or 'Class' (!RCLS), since this | |
2338 | routine will not get called in other cases. */ | |
2339 | return have_lproto || (rcls != NULL_TREE); | |
2340 | } | |
2341 | ||
b581b85b NP |
2342 | /* Given two types TYPE1 and TYPE2, return their least common ancestor. |
2343 | Both TYPE1 and TYPE2 must be pointers, and already determined to be | |
2344 | compatible by objc_compare_types() below. */ | |
2345 | ||
2346 | tree | |
2347 | objc_common_type (tree type1, tree type2) | |
2348 | { | |
2349 | tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2); | |
2350 | ||
2351 | while (POINTER_TYPE_P (inner1)) | |
2352 | { | |
2353 | inner1 = TREE_TYPE (inner1); | |
2354 | inner2 = TREE_TYPE (inner2); | |
2355 | } | |
2356 | ||
2357 | /* If one type is derived from another, return the base type. */ | |
2358 | if (DERIVED_FROM_P (inner1, inner2)) | |
2359 | return type1; | |
2360 | else if (DERIVED_FROM_P (inner2, inner1)) | |
2361 | return type2; | |
2362 | ||
2363 | /* If both types are 'Class', return 'Class'. */ | |
2364 | if (objc_is_class_id (inner1) && objc_is_class_id (inner2)) | |
2365 | return objc_class_type; | |
2366 | ||
2367 | /* Otherwise, return 'id'. */ | |
2368 | return objc_object_type; | |
2369 | } | |
2370 | ||
d11dd684 ZL |
2371 | /* Determine if it is permissible to assign (if ARGNO is greater than -3) |
2372 | an instance of RTYP to an instance of LTYP or to compare the two | |
2373 | (if ARGNO is equal to -3), per ObjC type system rules. Before | |
2374 | returning 'true', this routine may issue warnings related to, e.g., | |
2375 | protocol conformance. When returning 'false', the routine must | |
2376 | produce absolutely no warnings; the C or C++ front-end will do so | |
10e34e6e NP |
2377 | instead, if needed. If either LTYP or RTYP is not an Objective-C |
2378 | type, the routine must return 'false'. | |
d11dd684 ZL |
2379 | |
2380 | The ARGNO parameter is encoded as follows: | |
2381 | >= 1 Parameter number (CALLEE contains function being called); | |
2382 | 0 Return value; | |
2383 | -1 Assignment; | |
2384 | -2 Initialization; | |
4c116505 | 2385 | -3 Comparison (LTYP and RTYP may match in either direction); |
10e34e6e | 2386 | -4 Silent comparison (for C++ overload resolution); |
944fb799 MS |
2387 | -5 Silent "specialization" comparison for RTYP to be a "specialization" |
2388 | of LTYP (a specialization means that RTYP is LTYP plus some constraints, | |
10e34e6e NP |
2389 | so that each object of type RTYP is also of type LTYP). This is used |
2390 | when comparing property types. */ | |
d11dd684 ZL |
2391 | |
2392 | bool | |
2393 | objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee) | |
6c65299b | 2394 | { |
d11dd684 ZL |
2395 | tree lcls, rcls, lproto, rproto; |
2396 | bool pointers_compatible; | |
2397 | ||
2398 | /* We must be dealing with pointer types */ | |
2399 | if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp)) | |
2400 | return false; | |
e31c7eec | 2401 | |
d11dd684 | 2402 | do |
e31c7eec | 2403 | { |
d11dd684 ZL |
2404 | ltyp = TREE_TYPE (ltyp); /* Remove indirections. */ |
2405 | rtyp = TREE_TYPE (rtyp); | |
2406 | } | |
2407 | while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp)); | |
e31c7eec | 2408 | |
0f185d6e NP |
2409 | /* We must also handle function pointers, since ObjC is a bit more |
2410 | lenient than C or C++ on this. */ | |
2411 | if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE) | |
2412 | { | |
769b55b2 NF |
2413 | function_args_iterator liter, riter; |
2414 | ||
0f185d6e NP |
2415 | /* Return types must be covariant. */ |
2416 | if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp)) | |
2417 | && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp), | |
2418 | argno, callee)) | |
2419 | return false; | |
2420 | ||
2421 | /* Argument types must be contravariant. */ | |
769b55b2 NF |
2422 | function_args_iter_init (&liter, ltyp); |
2423 | function_args_iter_init (&riter, rtyp); | |
2424 | ||
2425 | while (1) | |
0f185d6e | 2426 | { |
769b55b2 NF |
2427 | ltyp = function_args_iter_cond (&liter); |
2428 | rtyp = function_args_iter_cond (&riter); | |
2429 | ||
2430 | /* If we've exhaused both lists simulateously, we're done. */ | |
2431 | if (ltyp == NULL_TREE && rtyp == NULL_TREE) | |
2432 | break; | |
2433 | ||
2434 | /* If one list is shorter than the other, they fail to match. */ | |
2435 | if (ltyp == NULL_TREE || rtyp == NULL_TREE) | |
0f185d6e | 2436 | return false; |
0f185d6e | 2437 | |
769b55b2 NF |
2438 | if (!comptypes (rtyp, ltyp) |
2439 | && !objc_compare_types (rtyp, ltyp, argno, callee)) | |
2440 | return false; | |
2441 | ||
2442 | function_args_iter_next (&liter); | |
2443 | function_args_iter_next (&riter); | |
2444 | } | |
2445 | ||
2446 | return true; | |
0f185d6e NP |
2447 | } |
2448 | ||
d11dd684 ZL |
2449 | /* Past this point, we are only interested in ObjC class instances, |
2450 | or 'id' or 'Class'. */ | |
2451 | if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE) | |
2452 | return false; | |
e31c7eec | 2453 | |
d11dd684 ZL |
2454 | if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp) |
2455 | && !TYPE_HAS_OBJC_INFO (ltyp)) | |
2456 | return false; | |
3a3589b4 | 2457 | |
d11dd684 ZL |
2458 | if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp) |
2459 | && !TYPE_HAS_OBJC_INFO (rtyp)) | |
2460 | return false; | |
011d50d9 | 2461 | |
4c116505 NP |
2462 | /* Past this point, we are committed to returning 'true' to the caller |
2463 | (unless performing a silent comparison; see below). However, we can | |
2464 | still warn about type and/or protocol mismatches. */ | |
e31c7eec | 2465 | |
d11dd684 ZL |
2466 | if (TYPE_HAS_OBJC_INFO (ltyp)) |
2467 | { | |
2468 | lcls = TYPE_OBJC_INTERFACE (ltyp); | |
2469 | lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp); | |
2470 | } | |
2471 | else | |
2472 | lcls = lproto = NULL_TREE; | |
e31c7eec | 2473 | |
d11dd684 ZL |
2474 | if (TYPE_HAS_OBJC_INFO (rtyp)) |
2475 | { | |
2476 | rcls = TYPE_OBJC_INTERFACE (rtyp); | |
2477 | rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp); | |
2478 | } | |
2479 | else | |
2480 | rcls = rproto = NULL_TREE; | |
3a3589b4 | 2481 | |
a0e71127 ZL |
2482 | /* If we could not find an @interface declaration, we must have |
2483 | only seen a @class declaration; for purposes of type comparison, | |
2484 | treat it as a stand-alone (root) class. */ | |
2485 | ||
2486 | if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE) | |
2487 | lcls = NULL_TREE; | |
2488 | ||
2489 | if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE) | |
2490 | rcls = NULL_TREE; | |
2491 | ||
10e34e6e NP |
2492 | /* If either type is an unqualified 'id', we're done. This is because |
2493 | an 'id' can be assigned to or from any type with no warnings. */ | |
2494 | if (argno != -5) | |
2495 | { | |
2496 | if ((!lproto && objc_is_object_id (ltyp)) | |
2497 | || (!rproto && objc_is_object_id (rtyp))) | |
2498 | return true; | |
2499 | } | |
2500 | else | |
2501 | { | |
2502 | /* For property checks, though, an 'id' is considered the most | |
2503 | general type of object, hence if you try to specialize an | |
2504 | 'NSArray *' (ltyp) property with an 'id' (rtyp) one, we need | |
2505 | to warn. */ | |
2506 | if (!lproto && objc_is_object_id (ltyp)) | |
2507 | return true; | |
2508 | } | |
944fb799 | 2509 | |
d11dd684 | 2510 | pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp)); |
51900510 | 2511 | |
d11dd684 ZL |
2512 | /* If the underlying types are the same, and at most one of them has |
2513 | a protocol list, we do not need to issue any diagnostics. */ | |
2514 | if (pointers_compatible && (!lproto || !rproto)) | |
2515 | return true; | |
3a3589b4 | 2516 | |
d11dd684 ZL |
2517 | /* If exactly one of the types is 'Class', issue a diagnostic; any |
2518 | exceptions of this rule have already been handled. */ | |
2519 | if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp)) | |
2520 | pointers_compatible = false; | |
2521 | /* Otherwise, check for inheritance relations. */ | |
2522 | else | |
2523 | { | |
2524 | if (!pointers_compatible) | |
10e34e6e NP |
2525 | { |
2526 | /* Again, if any of the two is an 'id', we're satisfied, | |
2527 | unless we're comparing properties, in which case only an | |
2528 | 'id' on the left-hand side (old property) is good | |
2529 | enough. */ | |
2530 | if (argno != -5) | |
2531 | pointers_compatible | |
2532 | = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp)); | |
2533 | else | |
944fb799 | 2534 | pointers_compatible = objc_is_object_id (ltyp); |
10e34e6e | 2535 | } |
d11dd684 ZL |
2536 | |
2537 | if (!pointers_compatible) | |
2538 | pointers_compatible = DERIVED_FROM_P (ltyp, rtyp); | |
2539 | ||
10e34e6e | 2540 | if (!pointers_compatible && (argno == -3 || argno == -4)) |
d11dd684 ZL |
2541 | pointers_compatible = DERIVED_FROM_P (rtyp, ltyp); |
2542 | } | |
2543 | ||
2544 | /* If the pointers match modulo protocols, check for protocol conformance | |
2545 | mismatches. */ | |
2546 | if (pointers_compatible) | |
2547 | { | |
2548 | pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp, | |
2549 | argno != -3); | |
2550 | ||
2551 | if (!pointers_compatible && argno == -3) | |
2552 | pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp, | |
2553 | argno != -3); | |
2554 | } | |
2555 | ||
2556 | if (!pointers_compatible) | |
2557 | { | |
4c116505 NP |
2558 | /* The two pointers are not exactly compatible. Issue a warning, unless |
2559 | we are performing a silent comparison, in which case return 'false' | |
2560 | instead. */ | |
d11dd684 ZL |
2561 | /* NB: For the time being, we shall make our warnings look like their |
2562 | C counterparts. In the future, we may wish to make them more | |
2563 | ObjC-specific. */ | |
2564 | switch (argno) | |
1074d9d4 | 2565 | { |
10e34e6e | 2566 | case -5: |
4c116505 NP |
2567 | case -4: |
2568 | return false; | |
2569 | ||
d11dd684 ZL |
2570 | case -3: |
2571 | warning (0, "comparison of distinct Objective-C types lacks a cast"); | |
2572 | break; | |
2573 | ||
2574 | case -2: | |
2575 | warning (0, "initialization from distinct Objective-C type"); | |
2576 | break; | |
2577 | ||
2578 | case -1: | |
2579 | warning (0, "assignment from distinct Objective-C type"); | |
2580 | break; | |
2581 | ||
2582 | case 0: | |
2583 | warning (0, "distinct Objective-C type in return"); | |
2584 | break; | |
2585 | ||
2586 | default: | |
2587 | warning (0, "passing argument %d of %qE from distinct " | |
2588 | "Objective-C type", argno, callee); | |
2589 | break; | |
1074d9d4 | 2590 | } |
e31c7eec | 2591 | } |
51900510 | 2592 | |
d11dd684 ZL |
2593 | return true; |
2594 | } | |
2595 | ||
b581b85b NP |
2596 | /* This routine is similar to objc_compare_types except that function-pointers are |
2597 | excluded. This is because, caller assumes that common types are of (id, Object*) | |
2598 | variety and calls objc_common_type to obtain a common type. There is no commonolty | |
2599 | between two function-pointers in this regard. */ | |
2600 | ||
944fb799 | 2601 | bool |
b581b85b NP |
2602 | objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee) |
2603 | { | |
2604 | if (objc_compare_types (ltyp, rtyp, argno, callee)) | |
2605 | { | |
2606 | /* exclude function-pointer types. */ | |
2607 | do | |
2608 | { | |
2609 | ltyp = TREE_TYPE (ltyp); /* Remove indirections. */ | |
2610 | rtyp = TREE_TYPE (rtyp); | |
2611 | } | |
2612 | while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp)); | |
2613 | return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE); | |
2614 | } | |
2615 | return false; | |
2616 | } | |
2617 | ||
d11dd684 ZL |
2618 | #ifndef OBJCPLUS |
2619 | /* Determine if CHILD is derived from PARENT. The routine assumes that | |
2620 | both parameters are RECORD_TYPEs, and is non-reflexive. */ | |
2621 | ||
2622 | static bool | |
2623 | objc_derived_from_p (tree parent, tree child) | |
2624 | { | |
2625 | parent = TYPE_MAIN_VARIANT (parent); | |
2626 | ||
2627 | for (child = TYPE_MAIN_VARIANT (child); | |
2628 | TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));) | |
1074d9d4 | 2629 | { |
d11dd684 ZL |
2630 | child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO |
2631 | (TYPE_BINFO (child), | |
2632 | 0))); | |
2633 | ||
2634 | if (child == parent) | |
2635 | return true; | |
1074d9d4 | 2636 | } |
6c65299b | 2637 | |
d11dd684 ZL |
2638 | return false; |
2639 | } | |
2640 | #endif | |
2641 | ||
d764a8e6 | 2642 | tree |
d11dd684 ZL |
2643 | objc_build_component_ref (tree datum, tree component) |
2644 | { | |
2645 | /* If COMPONENT is NULL, the caller is referring to the anonymous | |
2646 | base class field. */ | |
2647 | if (!component) | |
2648 | { | |
2649 | tree base = TYPE_FIELDS (TREE_TYPE (datum)); | |
6c65299b | 2650 | |
d11dd684 ZL |
2651 | return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE); |
2652 | } | |
6c65299b | 2653 | |
d11dd684 ZL |
2654 | /* The 'build_component_ref' routine has been removed from the C++ |
2655 | front-end, but 'finish_class_member_access_expr' seems to be | |
2656 | a worthy substitute. */ | |
2657 | #ifdef OBJCPLUS | |
525521b6 DG |
2658 | return finish_class_member_access_expr (datum, component, false, |
2659 | tf_warning_or_error); | |
d11dd684 | 2660 | #else |
c2255bc4 | 2661 | return build_component_ref (input_location, datum, component); |
d11dd684 ZL |
2662 | #endif |
2663 | } | |
6c65299b | 2664 | |
d11dd684 ZL |
2665 | /* Recursively copy inheritance information rooted at BINFO. To do this, |
2666 | we emulate the song and dance performed by cp/tree.c:copy_binfo(). */ | |
f75963f5 | 2667 | |
d11dd684 ZL |
2668 | static tree |
2669 | objc_copy_binfo (tree binfo) | |
2670 | { | |
2671 | tree btype = BINFO_TYPE (binfo); | |
2672 | tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo)); | |
2673 | tree base_binfo; | |
2674 | int ix; | |
6c65299b | 2675 | |
d11dd684 ZL |
2676 | BINFO_TYPE (binfo2) = btype; |
2677 | BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo); | |
2678 | BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo); | |
2679 | ||
2680 | /* Recursively copy base binfos of BINFO. */ | |
2681 | for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++) | |
6c65299b | 2682 | { |
d11dd684 ZL |
2683 | tree base_binfo2 = objc_copy_binfo (base_binfo); |
2684 | ||
2685 | BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2; | |
2686 | BINFO_BASE_APPEND (binfo2, base_binfo2); | |
2687 | } | |
2688 | ||
2689 | return binfo2; | |
2690 | } | |
2691 | ||
2692 | /* Record superclass information provided in BASETYPE for ObjC class REF. | |
2693 | This is loosely based on cp/decl.c:xref_basetypes(). */ | |
6c65299b | 2694 | |
d11dd684 ZL |
2695 | static void |
2696 | objc_xref_basetypes (tree ref, tree basetype) | |
2697 | { | |
2698 | tree binfo = make_tree_binfo (basetype ? 1 : 0); | |
6c65299b | 2699 | |
d11dd684 ZL |
2700 | TYPE_BINFO (ref) = binfo; |
2701 | BINFO_OFFSET (binfo) = size_zero_node; | |
2702 | BINFO_TYPE (binfo) = ref; | |
e31c7eec | 2703 | |
d11dd684 ZL |
2704 | if (basetype) |
2705 | { | |
2706 | tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype)); | |
e31c7eec | 2707 | |
d11dd684 | 2708 | BINFO_INHERITANCE_CHAIN (base_binfo) = binfo; |
9771b263 | 2709 | vec_alloc (BINFO_BASE_ACCESSES (binfo), 1); |
d11dd684 ZL |
2710 | BINFO_BASE_APPEND (binfo, base_binfo); |
2711 | BINFO_BASE_ACCESS_APPEND (binfo, access_public_node); | |
6c65299b | 2712 | } |
d11dd684 ZL |
2713 | } |
2714 | ||
264fa2db | 2715 | /* Called from finish_decl. */ |
6c65299b | 2716 | |
e31c7eec | 2717 | void |
011d50d9 | 2718 | objc_check_decl (tree decl) |
e31c7eec TW |
2719 | { |
2720 | tree type = TREE_TYPE (decl); | |
2721 | ||
264fa2db ZL |
2722 | if (TREE_CODE (type) != RECORD_TYPE) |
2723 | return; | |
57a6af27 | 2724 | if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type)))) |
f41c4af3 JM |
2725 | error ("statically allocated instance of Objective-C class %qE", |
2726 | type); | |
e31c7eec TW |
2727 | } |
2728 | ||
a6341d57 NP |
2729 | void |
2730 | objc_check_global_decl (tree decl) | |
2731 | { | |
2732 | tree id = DECL_NAME (decl); | |
2733 | if (objc_is_class_name (id) && global_bindings_p()) | |
2734 | error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id)); | |
2735 | } | |
2736 | ||
a693d3a8 NP |
2737 | /* Construct a PROTOCOLS-qualified variant of INTERFACE, where |
2738 | INTERFACE may either name an Objective-C class, or refer to the | |
2739 | special 'id' or 'Class' types. If INTERFACE is not a valid ObjC | |
2740 | type, just return it unchanged. This function is often called when | |
2741 | PROTOCOLS is NULL_TREE, in which case we simply look up the | |
2742 | appropriate INTERFACE. */ | |
6c65299b RS |
2743 | |
2744 | tree | |
6408ef33 | 2745 | objc_get_protocol_qualified_type (tree interface, tree protocols) |
e31c7eec | 2746 | { |
035e8f01 ZL |
2747 | /* If INTERFACE is not provided, default to 'id'. */ |
2748 | tree type = (interface ? objc_is_id (interface) : objc_object_type); | |
2749 | bool is_ptr = (type != NULL_TREE); | |
e31c7eec | 2750 | |
035e8f01 | 2751 | if (!is_ptr) |
e31c7eec | 2752 | { |
6408ef33 | 2753 | type = objc_is_class_name (interface); |
e31c7eec | 2754 | |
6408ef33 | 2755 | if (type) |
0f185d6e NP |
2756 | { |
2757 | /* If looking at a typedef, retrieve the precise type it | |
2758 | describes. */ | |
2759 | if (TREE_CODE (interface) == IDENTIFIER_NODE) | |
2760 | interface = identifier_global_value (interface); | |
2761 | ||
2762 | type = ((interface && TREE_CODE (interface) == TYPE_DECL | |
2763 | && DECL_ORIGINAL_TYPE (interface)) | |
2764 | ? DECL_ORIGINAL_TYPE (interface) | |
2765 | : xref_tag (RECORD_TYPE, type)); | |
2766 | } | |
6408ef33 | 2767 | else |
5a2a6eb0 NP |
2768 | { |
2769 | /* This case happens when we are given an 'interface' which | |
2770 | is not a valid class name. For example if a typedef was | |
2771 | used, and 'interface' really is the identifier of the | |
2772 | typedef, but when you resolve it you don't get an | |
2773 | Objective-C class, but something else, such as 'int'. | |
2774 | This is an error; protocols make no sense unless you use | |
2775 | them with Objective-C objects. */ | |
2776 | error_at (input_location, "only Objective-C object types can be qualified with a protocol"); | |
2777 | ||
2778 | /* Try to recover. Ignore the invalid class name, and treat | |
2779 | the object as an 'id' to silence further warnings about | |
2780 | the class. */ | |
2781 | type = objc_object_type; | |
2782 | is_ptr = true; | |
2783 | } | |
6408ef33 | 2784 | } |
51900510 | 2785 | |
e31c7eec TW |
2786 | if (protocols) |
2787 | { | |
8c1d6d62 | 2788 | type = build_variant_type_copy (type); |
6408ef33 | 2789 | |
035e8f01 ZL |
2790 | /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s) |
2791 | to the pointee. */ | |
2792 | if (is_ptr) | |
2793 | { | |
06d40de8 DG |
2794 | tree orig_pointee_type = TREE_TYPE (type); |
2795 | TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type); | |
2796 | ||
2797 | /* Set up the canonical type information. */ | |
944fb799 | 2798 | TYPE_CANONICAL (type) |
06d40de8 DG |
2799 | = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type)); |
2800 | ||
035e8f01 ZL |
2801 | TYPE_POINTER_TO (TREE_TYPE (type)) = type; |
2802 | type = TREE_TYPE (type); | |
2803 | } | |
2804 | ||
2805 | /* Look up protocols and install in lang specific list. */ | |
2806 | DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type)); | |
db0581ae NP |
2807 | TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols |
2808 | (protocols, /* definition_required */ false); | |
035e8f01 ZL |
2809 | |
2810 | /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class', | |
2811 | return the pointer to the new pointee variant. */ | |
2812 | if (is_ptr) | |
2813 | type = TYPE_POINTER_TO (type); | |
2814 | else | |
2815 | TYPE_OBJC_INTERFACE (type) | |
2816 | = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type)); | |
e31c7eec | 2817 | } |
6408ef33 | 2818 | |
e31c7eec TW |
2819 | return type; |
2820 | } | |
2821 | ||
f2e6e530 ZL |
2822 | /* Check for circular dependencies in protocols. The arguments are |
2823 | PROTO, the protocol to check, and LIST, a list of protocol it | |
2824 | conforms to. */ | |
2825 | ||
011d50d9 AJ |
2826 | static void |
2827 | check_protocol_recursively (tree proto, tree list) | |
f2e6e530 ZL |
2828 | { |
2829 | tree p; | |
2830 | ||
2831 | for (p = list; p; p = TREE_CHAIN (p)) | |
2832 | { | |
2833 | tree pp = TREE_VALUE (p); | |
2834 | ||
2835 | if (TREE_CODE (pp) == IDENTIFIER_NODE) | |
db0581ae NP |
2836 | pp = lookup_protocol (pp, /* warn if deprecated */ false, |
2837 | /* definition_required */ false); | |
f2e6e530 ZL |
2838 | |
2839 | if (pp == proto) | |
f41c4af3 JM |
2840 | fatal_error ("protocol %qE has circular dependency", |
2841 | PROTOCOL_NAME (pp)); | |
f2e6e530 ZL |
2842 | if (pp) |
2843 | check_protocol_recursively (proto, PROTOCOL_LIST (pp)); | |
2844 | } | |
2845 | } | |
2846 | ||
6b192a09 NP |
2847 | /* Look up PROTOCOLS, and return a list of those that are found. If |
2848 | none are found, return NULL. Note that this function will emit a | |
db0581ae NP |
2849 | warning if a protocol is found and is deprecated. If |
2850 | 'definition_required', then warn if the protocol is found but is | |
2851 | not defined (ie, if we only saw a forward-declaration of the | |
2852 | protocol (as in "@protocol NSObject;") not a real definition with | |
2853 | the list of methods). */ | |
e31c7eec | 2854 | static tree |
db0581ae | 2855 | lookup_and_install_protocols (tree protocols, bool definition_required) |
6c65299b | 2856 | { |
e31c7eec | 2857 | tree proto; |
264fa2db | 2858 | tree return_value = NULL_TREE; |
e31c7eec | 2859 | |
da57d1b9 NP |
2860 | if (protocols == error_mark_node) |
2861 | return NULL; | |
2862 | ||
e31c7eec TW |
2863 | for (proto = protocols; proto; proto = TREE_CHAIN (proto)) |
2864 | { | |
2865 | tree ident = TREE_VALUE (proto); | |
db0581ae NP |
2866 | tree p = lookup_protocol (ident, /* warn_if_deprecated */ true, |
2867 | definition_required); | |
e31c7eec | 2868 | |
7eb314dc | 2869 | if (p) |
264fa2db ZL |
2870 | return_value = chainon (return_value, |
2871 | build_tree_list (NULL_TREE, p)); | |
7eb314dc | 2872 | else if (ident != error_mark_node) |
f41c4af3 JM |
2873 | error ("cannot find protocol declaration for %qE", |
2874 | ident); | |
e31c7eec | 2875 | } |
51900510 | 2876 | |
e31c7eec | 2877 | return return_value; |
6c65299b RS |
2878 | } |
2879 | ||
fa340504 | 2880 | static void |
d764a8e6 | 2881 | build_common_objc_exception_stuff (void) |
6408ef33 | 2882 | { |
d764a8e6 | 2883 | tree noreturn_list, nothrow_list, temp_type; |
6408ef33 | 2884 | |
d764a8e6 IS |
2885 | noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL); |
2886 | nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL); | |
6408ef33 | 2887 | |
d764a8e6 IS |
2888 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
2889 | /* void objc_sync_enter(id); */ | |
2890 | /* void objc_sync_exit(id); */ | |
2891 | temp_type = build_function_type_list (void_type_node, | |
2892 | objc_object_type, | |
2893 | NULL_TREE); | |
2894 | objc_exception_throw_decl | |
2895 | = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL, | |
2896 | noreturn_list); | |
2897 | /* Make sure that objc_exception_throw (id) claims that it may throw an | |
2898 | exception. */ | |
2899 | TREE_NOTHROW (objc_exception_throw_decl) = 0; | |
6408ef33 | 2900 | |
d764a8e6 IS |
2901 | objc_sync_enter_decl |
2902 | = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN, | |
2903 | NULL, nothrow_list); | |
6408ef33 | 2904 | |
d764a8e6 IS |
2905 | objc_sync_exit_decl |
2906 | = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN, | |
2907 | NULL, nothrow_list); | |
fa340504 SS |
2908 | } |
2909 | ||
51900510 | 2910 | /* Purpose: "play" parser, creating/installing representations |
e31c7eec TW |
2911 | of the declarations that are required by Objective-C. |
2912 | ||
51900510 | 2913 | Model: |
e31c7eec | 2914 | |
011d50d9 AJ |
2915 | type_spec--------->sc_spec |
2916 | (tree_list) (tree_list) | |
2917 | | | | |
2918 | | | | |
2919 | identifier_node identifier_node */ | |
e31c7eec | 2920 | |
6c65299b | 2921 | static void |
011d50d9 | 2922 | synth_module_prologue (void) |
6c65299b | 2923 | { |
6408ef33 ZL |
2924 | tree type; |
2925 | enum debug_info_type save_write_symbols = write_symbols; | |
2926 | const struct gcc_debug_hooks *const save_hooks = debug_hooks; | |
2927 | ||
2928 | /* Suppress outputting debug symbols, because | |
39a13be5 | 2929 | dbxout_init hasn't been called yet. */ |
6408ef33 ZL |
2930 | write_symbols = NO_DEBUG; |
2931 | debug_hooks = &do_nothing_debug_hooks; | |
2932 | ||
2933 | #ifdef OBJCPLUS | |
2934 | push_lang_context (lang_name_c); /* extern "C" */ | |
2935 | #endif | |
2936 | ||
2937 | /* The following are also defined in <objc/objc.h> and friends. */ | |
6c65299b | 2938 | |
6c65299b | 2939 | objc_object_id = get_identifier (TAG_OBJECT); |
6408ef33 | 2940 | objc_class_id = get_identifier (TAG_CLASS); |
6c65299b RS |
2941 | |
2942 | objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id); | |
6408ef33 | 2943 | objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id); |
64ee9490 | 2944 | |
6408ef33 ZL |
2945 | objc_object_type = build_pointer_type (objc_object_reference); |
2946 | objc_class_type = build_pointer_type (objc_class_reference); | |
6c65299b | 2947 | |
6408ef33 ZL |
2948 | objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME); |
2949 | objc_class_name = get_identifier (CLASS_TYPEDEF_NAME); | |
6c65299b | 2950 | |
6408ef33 | 2951 | /* Declare the 'id' and 'Class' typedefs. */ |
c2255bc4 AH |
2952 | type = lang_hooks.decls.pushdecl (build_decl (input_location, |
2953 | TYPE_DECL, | |
6408ef33 ZL |
2954 | objc_object_name, |
2955 | objc_object_type)); | |
df0ed6c5 | 2956 | TREE_NO_WARNING (type) = 1; |
d764a8e6 | 2957 | |
c2255bc4 AH |
2958 | type = lang_hooks.decls.pushdecl (build_decl (input_location, |
2959 | TYPE_DECL, | |
6408ef33 ZL |
2960 | objc_class_name, |
2961 | objc_class_type)); | |
df0ed6c5 | 2962 | TREE_NO_WARNING (type) = 1; |
6408ef33 ZL |
2963 | |
2964 | /* Forward-declare '@interface Protocol'. */ | |
6408ef33 | 2965 | type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME); |
32dabdaf | 2966 | objc_declare_class (type); |
d764a8e6 | 2967 | objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE, type)); |
6c65299b | 2968 | |
1a4a7065 | 2969 | /* Declare receiver type used for dispatching messages to 'super'. */ |
264fa2db | 2970 | /* `struct objc_super *' */ |
5c234cd7 DA |
2971 | objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE, |
2972 | get_identifier (TAG_SUPER))); | |
e31c7eec | 2973 | |
38b9c8c3 ZL |
2974 | /* Declare pointers to method and ivar lists. */ |
2975 | objc_method_list_ptr = build_pointer_type | |
2976 | (xref_tag (RECORD_TYPE, | |
2977 | get_identifier (UTAG_METHOD_LIST))); | |
2978 | objc_method_proto_list_ptr | |
2979 | = build_pointer_type (xref_tag (RECORD_TYPE, | |
2980 | get_identifier (UTAG_METHOD_PROTOTYPE_LIST))); | |
2981 | objc_ivar_list_ptr = build_pointer_type | |
2982 | (xref_tag (RECORD_TYPE, | |
2983 | get_identifier (UTAG_IVAR_LIST))); | |
2984 | ||
d764a8e6 | 2985 | build_common_objc_exception_stuff (); |
6c65299b | 2986 | |
d764a8e6 IS |
2987 | /* Set-up runtime-specific templates, message and exception stuff. */ |
2988 | (*runtime.initialize) (); | |
6408ef33 | 2989 | |
8f07a2aa NP |
2990 | /* Declare objc_getProperty, object_setProperty and other property |
2991 | accessor helpers. */ | |
d764a8e6 | 2992 | build_common_objc_property_accessor_helpers (); |
e31c7eec | 2993 | |
e31c7eec | 2994 | /* Forward declare constant_string_id and constant_string_type. */ |
fda47030 | 2995 | if (!constant_string_class_name) |
d764a8e6 | 2996 | constant_string_class_name = runtime.default_constant_string_class_name; |
fda47030 | 2997 | constant_string_id = get_identifier (constant_string_class_name); |
32dabdaf | 2998 | objc_declare_class (constant_string_id); |
264fa2db ZL |
2999 | |
3000 | /* Pre-build the following entities - for speed/convenience. */ | |
3001 | self_id = get_identifier ("self"); | |
3002 | ucmd_id = get_identifier ("_cmd"); | |
6408ef33 | 3003 | |
f05b9d93 NP |
3004 | /* Declare struct _objc_fast_enumeration_state { ... }; */ |
3005 | build_fast_enumeration_state_template (); | |
944fb799 | 3006 | |
f05b9d93 | 3007 | /* void objc_enumeration_mutation (id) */ |
491add72 NF |
3008 | type = build_function_type_list (void_type_node, |
3009 | objc_object_type, NULL_TREE); | |
944fb799 MS |
3010 | objc_enumeration_mutation_decl |
3011 | = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN, | |
f05b9d93 NP |
3012 | NULL, NULL_TREE); |
3013 | TREE_NOTHROW (objc_enumeration_mutation_decl) = 0; | |
3014 | ||
6408ef33 ZL |
3015 | #ifdef OBJCPLUS |
3016 | pop_lang_context (); | |
3017 | #endif | |
3018 | ||
3019 | write_symbols = save_write_symbols; | |
3020 | debug_hooks = save_hooks; | |
6c65299b RS |
3021 | } |
3022 | ||
d764a8e6 IS |
3023 | /* --- const strings --- */ |
3024 | ||
264fa2db ZL |
3025 | /* Ensure that the ivar list for NSConstantString/NXConstantString |
3026 | (or whatever was specified via `-fconstant-string-class') | |
3027 | contains fields at least as large as the following three, so that | |
3028 | the runtime can stomp on them with confidence: | |
c66c59d1 | 3029 | |
011d50d9 | 3030 | struct STRING_OBJECT_CLASS_NAME |
c66c59d1 RH |
3031 | { |
3032 | Object isa; | |
3033 | char *cString; | |
3034 | unsigned int length; | |
3035 | }; */ | |
3036 | ||
264fa2db ZL |
3037 | static int |
3038 | check_string_class_template (void) | |
c66c59d1 | 3039 | { |
c64de75f | 3040 | tree field_decl = objc_get_class_ivars (constant_string_id); |
c66c59d1 | 3041 | |
264fa2db ZL |
3042 | #define AT_LEAST_AS_LARGE_AS(F, T) \ |
3043 | (F && TREE_CODE (F) == FIELD_DECL \ | |
c64de75f | 3044 | && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \ |
264fa2db | 3045 | >= TREE_INT_CST_LOW (TYPE_SIZE (T)))) |
c66c59d1 | 3046 | |
264fa2db ZL |
3047 | if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node)) |
3048 | return 0; | |
c66c59d1 | 3049 | |
910ad8de | 3050 | field_decl = DECL_CHAIN (field_decl); |
264fa2db ZL |
3051 | if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node)) |
3052 | return 0; | |
3053 | ||
910ad8de | 3054 | field_decl = DECL_CHAIN (field_decl); |
264fa2db | 3055 | return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node); |
c66c59d1 | 3056 | |
264fa2db | 3057 | #undef AT_LEAST_AS_LARGE_AS |
c66c59d1 RH |
3058 | } |
3059 | ||
264fa2db ZL |
3060 | /* Avoid calling `check_string_class_template ()' more than once. */ |
3061 | static GTY(()) int string_layout_checked; | |
3062 | ||
c64de75f ZL |
3063 | /* Construct an internal string layout to be used as a template for |
3064 | creating NSConstantString/NXConstantString instances. */ | |
3065 | ||
3066 | static tree | |
3067 | objc_build_internal_const_str_type (void) | |
3068 | { | |
3069 | tree type = (*lang_hooks.types.make_type) (RECORD_TYPE); | |
c2255bc4 AH |
3070 | tree fields = build_decl (input_location, |
3071 | FIELD_DECL, NULL_TREE, ptr_type_node); | |
3072 | tree field = build_decl (input_location, | |
3073 | FIELD_DECL, NULL_TREE, ptr_type_node); | |
c64de75f | 3074 | |
910ad8de | 3075 | DECL_CHAIN (field) = fields; fields = field; |
c2255bc4 AH |
3076 | field = build_decl (input_location, |
3077 | FIELD_DECL, NULL_TREE, unsigned_type_node); | |
910ad8de | 3078 | DECL_CHAIN (field) = fields; fields = field; |
c64de75f ZL |
3079 | /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in |
3080 | reverse order! */ | |
3081 | finish_builtin_struct (type, "__builtin_ObjCString", | |
3082 | fields, NULL_TREE); | |
3083 | ||
3084 | return type; | |
3085 | } | |
3086 | ||
e31c7eec TW |
3087 | /* Custom build_string which sets TREE_TYPE! */ |
3088 | ||
d764a8e6 | 3089 | tree |
011d50d9 | 3090 | my_build_string (int len, const char *str) |
6c65299b | 3091 | { |
b84a3874 | 3092 | return fix_string_type (build_string (len, str)); |
e31c7eec TW |
3093 | } |
3094 | ||
46bdb9cf JM |
3095 | /* Build a string with contents STR and length LEN and convert it to a |
3096 | pointer. */ | |
3097 | ||
d764a8e6 | 3098 | tree |
46bdb9cf JM |
3099 | my_build_string_pointer (int len, const char *str) |
3100 | { | |
3101 | tree string = my_build_string (len, str); | |
3102 | tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string))); | |
3103 | return build1 (ADDR_EXPR, ptrtype, string); | |
3104 | } | |
6408ef33 ZL |
3105 | |
3106 | static hashval_t | |
3107 | string_hash (const void *ptr) | |
3108 | { | |
741ac903 | 3109 | const_tree const str = ((const struct string_descriptor *)ptr)->literal; |
6408ef33 ZL |
3110 | const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str); |
3111 | int i, len = TREE_STRING_LENGTH (str); | |
3112 | hashval_t h = len; | |
3113 | ||
3114 | for (i = 0; i < len; i++) | |
3115 | h = ((h * 613) + p[i]); | |
3116 | ||
3117 | return h; | |
3118 | } | |
3119 | ||
3120 | static int | |
3121 | string_eq (const void *ptr1, const void *ptr2) | |
3122 | { | |
741ac903 KG |
3123 | const_tree const str1 = ((const struct string_descriptor *)ptr1)->literal; |
3124 | const_tree const str2 = ((const struct string_descriptor *)ptr2)->literal; | |
6408ef33 ZL |
3125 | int len1 = TREE_STRING_LENGTH (str1); |
3126 | ||
3127 | return (len1 == TREE_STRING_LENGTH (str2) | |
3128 | && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2), | |
3129 | len1)); | |
3130 | } | |
3131 | ||
264fa2db ZL |
3132 | /* Given a chain of STRING_CST's, build a static instance of |
3133 | NXConstantString which points at the concatenation of those | |
3134 | strings. We place the string object in the __string_objects | |
3135 | section of the __OBJC segment. The Objective-C runtime will | |
3136 | initialize the isa pointers of the string objects to point at the | |
3137 | NXConstantString class object. */ | |
e31c7eec TW |
3138 | |
3139 | tree | |
57a6af27 | 3140 | objc_build_string_object (tree string) |
e31c7eec | 3141 | { |
26705988 | 3142 | tree constant_string_class; |
e31c7eec | 3143 | int length; |
d764a8e6 | 3144 | tree addr; |
6408ef33 ZL |
3145 | struct string_descriptor *desc, key; |
3146 | void **loc; | |
e31c7eec | 3147 | |
f439d6ba IS |
3148 | /* We should be passed a STRING_CST. */ |
3149 | gcc_checking_assert (TREE_CODE (string) == STRING_CST); | |
e31c7eec TW |
3150 | length = TREE_STRING_LENGTH (string) - 1; |
3151 | ||
944fb799 MS |
3152 | /* The target may have different ideas on how to construct an ObjC string |
3153 | literal. On Darwin (Mac OS X), for example, we may wish to obtain a | |
26705988 IS |
3154 | constant CFString reference instead. |
3155 | At present, this is only supported for the NeXT runtime. */ | |
944fb799 | 3156 | if (flag_next_runtime |
d764a8e6 | 3157 | && targetcm.objc_construct_string_object) |
26705988 | 3158 | { |
91ebb981 | 3159 | tree constructor = (*targetcm.objc_construct_string_object) (string); |
26705988 IS |
3160 | if (constructor) |
3161 | return build1 (NOP_EXPR, objc_object_type, constructor); | |
3162 | } | |
3163 | ||
6408ef33 ZL |
3164 | /* Check whether the string class being used actually exists and has the |
3165 | correct ivar layout. */ | |
264fa2db ZL |
3166 | if (!string_layout_checked) |
3167 | { | |
6408ef33 ZL |
3168 | string_layout_checked = -1; |
3169 | constant_string_class = lookup_interface (constant_string_id); | |
c64de75f | 3170 | internal_const_str_type = objc_build_internal_const_str_type (); |
6408ef33 ZL |
3171 | |
3172 | if (!constant_string_class | |
3173 | || !(constant_string_type | |
3174 | = CLASS_STATIC_TEMPLATE (constant_string_class))) | |
f41c4af3 JM |
3175 | error ("cannot find interface declaration for %qE", |
3176 | constant_string_id); | |
6408ef33 ZL |
3177 | /* The NSConstantString/NXConstantString ivar layout is now known. */ |
3178 | else if (!check_string_class_template ()) | |
f41c4af3 JM |
3179 | error ("interface %qE does not have valid constant string layout", |
3180 | constant_string_id); | |
d764a8e6 IS |
3181 | /* If the runtime can generate a literal reference to the string class, |
3182 | don't need to run a constructor. */ | |
3183 | else if (!(*runtime.setup_const_string_class_decl)()) | |
3184 | error ("cannot find reference tag for class %qE", constant_string_id); | |
6408ef33 | 3185 | else |
fa340504 | 3186 | { |
6408ef33 ZL |
3187 | string_layout_checked = 1; /* Success! */ |
3188 | add_class_reference (constant_string_id); | |
fa340504 | 3189 | } |
fa340504 SS |
3190 | } |
3191 | ||
6408ef33 ZL |
3192 | if (string_layout_checked == -1) |
3193 | return error_mark_node; | |
89ef1046 | 3194 | |
6408ef33 ZL |
3195 | /* Perhaps we already constructed a constant string just like this one? */ |
3196 | key.literal = string; | |
3197 | loc = htab_find_slot (string_htab, &key, INSERT); | |
e1e4cdc4 | 3198 | desc = (struct string_descriptor *) *loc; |
6408ef33 ZL |
3199 | |
3200 | if (!desc) | |
51900510 | 3201 | { |
766090c2 | 3202 | *loc = desc = ggc_alloc<string_descriptor> (); |
6408ef33 | 3203 | desc->literal = string; |
944fb799 | 3204 | desc->constructor = |
d764a8e6 | 3205 | (*runtime.build_const_string_constructor) (input_location, string, length); |
51900510 RK |
3206 | } |
3207 | ||
c64de75f | 3208 | addr = convert (build_pointer_type (constant_string_type), |
c9f9eb5d AH |
3209 | build_unary_op (input_location, |
3210 | ADDR_EXPR, desc->constructor, 1)); | |
6408ef33 ZL |
3211 | |
3212 | return addr; | |
4cb8c14b RK |
3213 | } |
3214 | ||
89ef1046 RS |
3215 | /* Build a static constant CONSTRUCTOR |
3216 | with type TYPE and elements ELTS. */ | |
3217 | ||
d764a8e6 | 3218 | tree |
9771b263 | 3219 | objc_build_constructor (tree type, vec<constructor_elt, va_gc> *elts) |
89ef1046 | 3220 | { |
076a7055 | 3221 | tree constructor = build_constructor (type, elts); |
ff1c8a1a | 3222 | |
e31c7eec TW |
3223 | TREE_CONSTANT (constructor) = 1; |
3224 | TREE_STATIC (constructor) = 1; | |
3225 | TREE_READONLY (constructor) = 1; | |
3226 | ||
264fa2db | 3227 | #ifdef OBJCPLUS |
6408ef33 ZL |
3228 | /* Adjust for impedance mismatch. We should figure out how to build |
3229 | CONSTRUCTORs that consistently please both the C and C++ gods. */ | |
9771b263 | 3230 | if (!(*elts)[0].index) |
7ce841d2 | 3231 | TREE_TYPE (constructor) = init_list_type_node; |
264fa2db | 3232 | #endif |
6408ef33 | 3233 | |
89ef1046 | 3234 | return constructor; |
6c65299b | 3235 | } |
af35aeb2 | 3236 | |
d764a8e6 | 3237 | /* Return the DECL of the string IDENT in the SECTION. */ |
6c65299b | 3238 | |
d764a8e6 IS |
3239 | tree |
3240 | get_objc_string_decl (tree ident, enum string_section section) | |
6c65299b | 3241 | { |
d764a8e6 | 3242 | tree chain; |
6c65299b | 3243 | |
d764a8e6 | 3244 | switch (section) |
264fa2db | 3245 | { |
d764a8e6 IS |
3246 | case class_names: |
3247 | chain = class_names_chain; | |
3248 | break; | |
3249 | case meth_var_names: | |
3250 | chain = meth_var_names_chain; | |
3251 | break; | |
3252 | case meth_var_types: | |
3253 | chain = meth_var_types_chain; | |
3254 | break; | |
3255 | case prop_names_attr: | |
3256 | chain = prop_names_attr_chain; | |
3257 | break; | |
3258 | default: | |
3259 | gcc_unreachable (); | |
264fa2db | 3260 | } |
6c65299b | 3261 | |
d764a8e6 IS |
3262 | for (; chain != 0; chain = TREE_CHAIN (chain)) |
3263 | if (TREE_VALUE (chain) == ident) | |
3264 | return (TREE_PURPOSE (chain)); | |
3265 | ||
3266 | /* We didn't find the entry. */ | |
3267 | return NULL_TREE; | |
6c65299b RS |
3268 | } |
3269 | ||
d764a8e6 IS |
3270 | /* Create a class reference, but don't create a variable to reference |
3271 | it. */ | |
af35aeb2 | 3272 | |
d764a8e6 IS |
3273 | void |
3274 | add_class_reference (tree ident) | |
6c65299b | 3275 | { |
d764a8e6 | 3276 | tree chain; |
6c65299b | 3277 | |
d764a8e6 | 3278 | if ((chain = cls_ref_chain)) |
e335b3ee | 3279 | { |
d764a8e6 IS |
3280 | tree tail; |
3281 | do | |
3282 | { | |
3283 | if (ident == TREE_VALUE (chain)) | |
3284 | return; | |
e335b3ee | 3285 | |
d764a8e6 IS |
3286 | tail = chain; |
3287 | chain = TREE_CHAIN (chain); | |
3288 | } | |
3289 | while (chain); | |
e335b3ee | 3290 | |
d764a8e6 IS |
3291 | /* Append to the end of the list */ |
3292 | TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE); | |
3293 | } | |
3294 | else | |
3295 | cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE); | |
6c65299b RS |
3296 | } |
3297 | ||
d764a8e6 IS |
3298 | /* Get a class reference, creating it if necessary. Also create the |
3299 | reference variable. */ | |
3300 | tree | |
3301 | objc_get_class_reference (tree ident) | |
6c65299b | 3302 | { |
d764a8e6 IS |
3303 | tree orig_ident = (DECL_P (ident) |
3304 | ? DECL_NAME (ident) | |
3305 | : TYPE_P (ident) | |
3306 | ? OBJC_TYPE_NAME (ident) | |
3307 | : ident); | |
3308 | bool local_scope = false; | |
6c65299b | 3309 | |
d764a8e6 IS |
3310 | #ifdef OBJCPLUS |
3311 | if (processing_template_decl) | |
3312 | /* Must wait until template instantiation time. */ | |
d7faa110 | 3313 | return build_min_nt_loc (UNKNOWN_LOCATION, CLASS_REFERENCE_EXPR, ident); |
d764a8e6 | 3314 | #endif |
6c65299b | 3315 | |
d764a8e6 IS |
3316 | if (TREE_CODE (ident) == TYPE_DECL) |
3317 | ident = (DECL_ORIGINAL_TYPE (ident) | |
3318 | ? DECL_ORIGINAL_TYPE (ident) | |
3319 | : TREE_TYPE (ident)); | |
6c65299b | 3320 | |
d764a8e6 IS |
3321 | #ifdef OBJCPLUS |
3322 | if (TYPE_P (ident) | |
3323 | && CP_TYPE_CONTEXT (ident) != global_namespace) | |
3324 | local_scope = true; | |
3325 | #endif | |
6c65299b | 3326 | |
d764a8e6 | 3327 | if (local_scope || !(ident = objc_is_class_name (ident))) |
076a7055 | 3328 | { |
d764a8e6 IS |
3329 | error ("%qE is not an Objective-C class name or alias", |
3330 | orig_ident); | |
3331 | return error_mark_node; | |
076a7055 | 3332 | } |
6c65299b | 3333 | |
d764a8e6 IS |
3334 | return (*runtime.get_class_reference) (ident); |
3335 | } | |
6c65299b | 3336 | |
d764a8e6 IS |
3337 | void |
3338 | objc_declare_alias (tree alias_ident, tree class_ident) | |
3339 | { | |
3340 | tree underlying_class; | |
6c65299b | 3341 | |
d764a8e6 IS |
3342 | #ifdef OBJCPLUS |
3343 | if (current_namespace != global_namespace) { | |
3344 | error ("Objective-C declarations may only appear in global scope"); | |
3345 | } | |
3346 | #endif /* OBJCPLUS */ | |
6c65299b | 3347 | |
d764a8e6 IS |
3348 | if (!(underlying_class = objc_is_class_name (class_ident))) |
3349 | warning (0, "cannot find class %qE", class_ident); | |
3350 | else if (objc_is_class_name (alias_ident)) | |
3351 | warning (0, "class %qE already exists", alias_ident); | |
3352 | else | |
89ef1046 | 3353 | { |
d764a8e6 IS |
3354 | /* Implement @compatibility_alias as a typedef. */ |
3355 | #ifdef OBJCPLUS | |
3356 | push_lang_context (lang_name_c); /* extern "C" */ | |
3357 | #endif | |
3358 | lang_hooks.decls.pushdecl (build_decl | |
3359 | (input_location, | |
3360 | TYPE_DECL, | |
3361 | alias_ident, | |
3362 | xref_tag (RECORD_TYPE, underlying_class))); | |
3363 | #ifdef OBJCPLUS | |
3364 | pop_lang_context (); | |
3365 | #endif | |
3cc2dd4b | 3366 | objc_map_put (alias_name_map, alias_ident, underlying_class); |
89ef1046 | 3367 | } |
6c65299b RS |
3368 | } |
3369 | ||
d764a8e6 | 3370 | void |
32dabdaf | 3371 | objc_declare_class (tree identifier) |
264fa2db | 3372 | { |
d764a8e6 IS |
3373 | #ifdef OBJCPLUS |
3374 | if (current_namespace != global_namespace) { | |
3375 | error ("Objective-C declarations may only appear in global scope"); | |
3376 | } | |
3377 | #endif /* OBJCPLUS */ | |
6408ef33 | 3378 | |
32dabdaf | 3379 | if (! objc_is_class_name (identifier)) |
d764a8e6 | 3380 | { |
32dabdaf | 3381 | tree record = lookup_name (identifier), type = record; |
944fb799 | 3382 | |
32dabdaf | 3383 | if (record) |
d764a8e6 | 3384 | { |
32dabdaf NP |
3385 | if (TREE_CODE (record) == TYPE_DECL) |
3386 | type = DECL_ORIGINAL_TYPE (record) | |
3387 | ? DECL_ORIGINAL_TYPE (record) | |
3388 | : TREE_TYPE (record); | |
944fb799 | 3389 | |
32dabdaf NP |
3390 | if (!TYPE_HAS_OBJC_INFO (type) |
3391 | || !TYPE_OBJC_INTERFACE (type)) | |
d764a8e6 | 3392 | { |
32dabdaf NP |
3393 | error ("%qE redeclared as different kind of symbol", |
3394 | identifier); | |
3395 | error ("previous declaration of %q+D", | |
3396 | record); | |
d764a8e6 | 3397 | } |
6c65299b | 3398 | } |
944fb799 | 3399 | |
32dabdaf NP |
3400 | record = xref_tag (RECORD_TYPE, identifier); |
3401 | INIT_TYPE_OBJC_INFO (record); | |
3402 | /* In the case of a @class declaration, we store the ident in | |
3403 | the TYPE_OBJC_INTERFACE. If later an @interface is found, | |
3404 | we'll replace the ident with the interface. */ | |
3405 | TYPE_OBJC_INTERFACE (record) = identifier; | |
3cc2dd4b | 3406 | objc_map_put (class_name_map, identifier, NULL_TREE); |
6c65299b | 3407 | } |
6c65299b RS |
3408 | } |
3409 | ||
d764a8e6 IS |
3410 | tree |
3411 | objc_is_class_name (tree ident) | |
6c65299b | 3412 | { |
0d8a2528 NP |
3413 | if (ident && TREE_CODE (ident) == IDENTIFIER_NODE) |
3414 | { | |
3415 | tree t = identifier_global_value (ident); | |
3416 | if (t) | |
3417 | ident = t; | |
3418 | } | |
3419 | ||
d764a8e6 IS |
3420 | while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident)) |
3421 | ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident)); | |
6408ef33 | 3422 | |
d764a8e6 IS |
3423 | if (ident && TREE_CODE (ident) == RECORD_TYPE) |
3424 | ident = OBJC_TYPE_NAME (ident); | |
6408ef33 | 3425 | #ifdef OBJCPLUS |
d764a8e6 IS |
3426 | if (ident && TREE_CODE (ident) == TYPE_DECL) |
3427 | { | |
3428 | tree type = TREE_TYPE (ident); | |
3429 | if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM) | |
3430 | return NULL_TREE; | |
3431 | ident = DECL_NAME (ident); | |
3432 | } | |
6408ef33 | 3433 | #endif |
d764a8e6 IS |
3434 | if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE) |
3435 | return NULL_TREE; | |
6c65299b | 3436 | |
d764a8e6 IS |
3437 | if (lookup_interface (ident)) |
3438 | return ident; | |
6c65299b | 3439 | |
3cc2dd4b NP |
3440 | { |
3441 | tree target; | |
6c65299b | 3442 | |
3cc2dd4b NP |
3443 | target = objc_map_get (class_name_map, ident); |
3444 | if (target != OBJC_MAP_NOT_FOUND) | |
3445 | return ident; | |
3446 | ||
3447 | target = objc_map_get (alias_name_map, ident); | |
3448 | if (target != OBJC_MAP_NOT_FOUND) | |
3449 | return target; | |
3450 | } | |
6408ef33 | 3451 | |
d764a8e6 | 3452 | return 0; |
6408ef33 ZL |
3453 | } |
3454 | ||
d764a8e6 | 3455 | /* Check whether TYPE is either 'id' or 'Class'. */ |
6c65299b | 3456 | |
d764a8e6 IS |
3457 | tree |
3458 | objc_is_id (tree type) | |
6408ef33 | 3459 | { |
0d8a2528 NP |
3460 | if (type && TREE_CODE (type) == IDENTIFIER_NODE) |
3461 | { | |
3462 | tree t = identifier_global_value (type); | |
3463 | if (t) | |
3464 | type = t; | |
3465 | } | |
6c65299b | 3466 | |
d764a8e6 IS |
3467 | if (type && TREE_CODE (type) == TYPE_DECL) |
3468 | type = TREE_TYPE (type); | |
6c65299b | 3469 | |
d764a8e6 IS |
3470 | /* NB: This function may be called before the ObjC front-end has |
3471 | been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */ | |
3472 | return (objc_object_type && type | |
3473 | && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type)) | |
3474 | ? type | |
3475 | : NULL_TREE); | |
6408ef33 | 3476 | } |
e31c7eec | 3477 | |
d764a8e6 IS |
3478 | /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC |
3479 | class instance. This is needed by other parts of the compiler to | |
3480 | handle ObjC types gracefully. */ | |
6408ef33 | 3481 | |
d764a8e6 IS |
3482 | tree |
3483 | objc_is_object_ptr (tree type) | |
6408ef33 | 3484 | { |
d764a8e6 | 3485 | tree ret; |
6408ef33 | 3486 | |
d764a8e6 IS |
3487 | type = TYPE_MAIN_VARIANT (type); |
3488 | if (!POINTER_TYPE_P (type)) | |
3489 | return 0; | |
6408ef33 | 3490 | |
d764a8e6 IS |
3491 | ret = objc_is_id (type); |
3492 | if (!ret) | |
3493 | ret = objc_is_class_name (TREE_TYPE (type)); | |
6408ef33 | 3494 | |
d764a8e6 | 3495 | return ret; |
6c65299b | 3496 | } |
51900510 | 3497 | |
d764a8e6 IS |
3498 | static int |
3499 | objc_is_gcable_type (tree type, int or_strong_p) | |
4cb8c14b | 3500 | { |
d764a8e6 | 3501 | tree name; |
4cb8c14b | 3502 | |
d764a8e6 IS |
3503 | if (!TYPE_P (type)) |
3504 | return 0; | |
3505 | if (objc_is_id (TYPE_MAIN_VARIANT (type))) | |
3506 | return 1; | |
3507 | if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type))) | |
3508 | return 1; | |
3509 | if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF) | |
3510 | return 0; | |
3511 | type = TREE_TYPE (type); | |
3512 | if (TREE_CODE (type) != RECORD_TYPE) | |
3513 | return 0; | |
3514 | name = TYPE_NAME (type); | |
3515 | return (objc_is_class_name (name) != NULL_TREE); | |
4cb8c14b RK |
3516 | } |
3517 | ||
d764a8e6 IS |
3518 | static tree |
3519 | objc_substitute_decl (tree expr, tree oldexpr, tree newexpr) | |
4cb8c14b | 3520 | { |
d764a8e6 IS |
3521 | if (expr == oldexpr) |
3522 | return newexpr; | |
4cb8c14b | 3523 | |
d764a8e6 | 3524 | switch (TREE_CODE (expr)) |
4cb8c14b | 3525 | { |
d764a8e6 IS |
3526 | case COMPONENT_REF: |
3527 | return objc_build_component_ref | |
3528 | (objc_substitute_decl (TREE_OPERAND (expr, 0), | |
3529 | oldexpr, | |
3530 | newexpr), | |
3531 | DECL_NAME (TREE_OPERAND (expr, 1))); | |
3532 | case ARRAY_REF: | |
3533 | return build_array_ref (input_location, | |
3534 | objc_substitute_decl (TREE_OPERAND (expr, 0), | |
3535 | oldexpr, | |
3536 | newexpr), | |
3537 | TREE_OPERAND (expr, 1)); | |
3538 | case INDIRECT_REF: | |
3539 | return build_indirect_ref (input_location, | |
3540 | objc_substitute_decl (TREE_OPERAND (expr, 0), | |
3541 | oldexpr, | |
3542 | newexpr), RO_ARROW); | |
3543 | default: | |
3544 | return expr; | |
4cb8c14b | 3545 | } |
4cb8c14b RK |
3546 | } |
3547 | ||
6c65299b | 3548 | static tree |
d764a8e6 | 3549 | objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs) |
6c65299b | 3550 | { |
d764a8e6 IS |
3551 | tree func_params; |
3552 | /* The LHS parameter contains the expression 'outervar->memberspec'; | |
3553 | we need to transform it into '&((typeof(outervar) *) 0)->memberspec', | |
3554 | where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]'). | |
3555 | */ | |
3556 | tree offs | |
3557 | = objc_substitute_decl | |
3558 | (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node)); | |
3559 | tree func | |
3560 | = (flag_objc_direct_dispatch | |
3561 | ? objc_assign_ivar_fast_decl | |
3562 | : objc_assign_ivar_decl); | |
6c65299b | 3563 | |
d764a8e6 IS |
3564 | offs = convert (integer_type_node, build_unary_op (input_location, |
3565 | ADDR_EXPR, offs, 0)); | |
3566 | offs = fold (offs); | |
3567 | func_params = tree_cons (NULL_TREE, | |
3568 | convert (objc_object_type, rhs), | |
3569 | tree_cons (NULL_TREE, convert (objc_object_type, outervar), | |
3570 | tree_cons (NULL_TREE, offs, | |
3571 | NULL_TREE))); | |
6c65299b | 3572 | |
d764a8e6 | 3573 | return build_function_call (input_location, func, func_params); |
6408ef33 | 3574 | } |
6c65299b | 3575 | |
d764a8e6 IS |
3576 | static tree |
3577 | objc_build_global_assignment (tree lhs, tree rhs) | |
6408ef33 | 3578 | { |
d764a8e6 IS |
3579 | tree func_params = tree_cons (NULL_TREE, |
3580 | convert (objc_object_type, rhs), | |
3581 | tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type), | |
3582 | build_unary_op (input_location, ADDR_EXPR, lhs, 0)), | |
3583 | NULL_TREE)); | |
e31c7eec | 3584 | |
944fb799 | 3585 | return build_function_call (input_location, |
d764a8e6 | 3586 | objc_assign_global_decl, func_params); |
6c65299b | 3587 | } |
e31c7eec | 3588 | |
e31c7eec | 3589 | static tree |
d764a8e6 | 3590 | objc_build_strong_cast_assignment (tree lhs, tree rhs) |
e31c7eec | 3591 | { |
d764a8e6 IS |
3592 | tree func_params = tree_cons (NULL_TREE, |
3593 | convert (objc_object_type, rhs), | |
3594 | tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type), | |
3595 | build_unary_op (input_location, ADDR_EXPR, lhs, 0)), | |
3596 | NULL_TREE)); | |
3597 | ||
d764a8e6 IS |
3598 | return build_function_call (input_location, |
3599 | objc_assign_strong_cast_decl, func_params); | |
e31c7eec TW |
3600 | } |
3601 | ||
d764a8e6 IS |
3602 | static int |
3603 | objc_is_gcable_p (tree expr) | |
6c65299b | 3604 | { |
d764a8e6 IS |
3605 | return (TREE_CODE (expr) == COMPONENT_REF |
3606 | ? objc_is_gcable_p (TREE_OPERAND (expr, 1)) | |
3607 | : TREE_CODE (expr) == ARRAY_REF | |
3608 | ? (objc_is_gcable_p (TREE_TYPE (expr)) | |
3609 | || objc_is_gcable_p (TREE_OPERAND (expr, 0))) | |
3610 | : TREE_CODE (expr) == ARRAY_TYPE | |
3611 | ? objc_is_gcable_p (TREE_TYPE (expr)) | |
3612 | : TYPE_P (expr) | |
3613 | ? objc_is_gcable_type (expr, 1) | |
3614 | : (objc_is_gcable_p (TREE_TYPE (expr)) | |
3615 | || (DECL_P (expr) | |
3616 | && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr))))); | |
058bfe53 IS |
3617 | } |
3618 | ||
d764a8e6 IS |
3619 | static int |
3620 | objc_is_ivar_reference_p (tree expr) | |
058bfe53 | 3621 | { |
d764a8e6 IS |
3622 | return (TREE_CODE (expr) == ARRAY_REF |
3623 | ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0)) | |
3624 | : TREE_CODE (expr) == COMPONENT_REF | |
3625 | ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL | |
3626 | : 0); | |
058bfe53 IS |
3627 | } |
3628 | ||
d764a8e6 IS |
3629 | static int |
3630 | objc_is_global_reference_p (tree expr) | |
058bfe53 | 3631 | { |
d764a8e6 IS |
3632 | return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR |
3633 | ? objc_is_global_reference_p (TREE_OPERAND (expr, 0)) | |
3634 | : DECL_P (expr) | |
3635 | ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr)) | |
3636 | : 0); | |
3637 | } | |
058bfe53 | 3638 | |
d764a8e6 IS |
3639 | tree |
3640 | objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs) | |
3641 | { | |
3642 | tree result = NULL_TREE, outer; | |
3643 | int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0; | |
058bfe53 | 3644 | |
d764a8e6 IS |
3645 | /* This function is currently only used with the next runtime with |
3646 | garbage collection enabled (-fobjc-gc). */ | |
3647 | gcc_assert (flag_next_runtime); | |
058bfe53 | 3648 | |
d764a8e6 IS |
3649 | /* See if we have any lhs casts, and strip them out. NB: The lvalue casts |
3650 | will have been transformed to the form '*(type *)&expr'. */ | |
3651 | if (TREE_CODE (lhs) == INDIRECT_REF) | |
3652 | { | |
3653 | outer = TREE_OPERAND (lhs, 0); | |
058bfe53 | 3654 | |
d764a8e6 IS |
3655 | while (!strong_cast_p |
3656 | && (CONVERT_EXPR_P (outer) | |
3657 | || TREE_CODE (outer) == NON_LVALUE_EXPR)) | |
5d0f30f7 | 3658 | { |
d764a8e6 | 3659 | tree lhstype = TREE_TYPE (outer); |
011d50d9 | 3660 | |
d764a8e6 IS |
3661 | /* Descend down the cast chain, and record the first objc_gc |
3662 | attribute found. */ | |
3663 | if (POINTER_TYPE_P (lhstype)) | |
3664 | { | |
3665 | tree attr | |
3666 | = lookup_attribute ("objc_gc", | |
3667 | TYPE_ATTRIBUTES (TREE_TYPE (lhstype))); | |
6c65299b | 3668 | |
d764a8e6 IS |
3669 | if (attr) |
3670 | strong_cast_p = 1; | |
3671 | } | |
076a7055 | 3672 | |
d764a8e6 | 3673 | outer = TREE_OPERAND (outer, 0); |
076a7055 | 3674 | } |
e31c7eec | 3675 | } |
e31c7eec | 3676 | |
d764a8e6 IS |
3677 | /* If we have a __strong cast, it trumps all else. */ |
3678 | if (strong_cast_p) | |
5d0f30f7 | 3679 | { |
d764a8e6 IS |
3680 | if (modifycode != NOP_EXPR) |
3681 | goto invalid_pointer_arithmetic; | |
e31c7eec | 3682 | |
d764a8e6 IS |
3683 | if (warn_assign_intercept) |
3684 | warning (0, "strong-cast assignment has been intercepted"); | |
5d0f30f7 | 3685 | |
d764a8e6 | 3686 | result = objc_build_strong_cast_assignment (lhs, rhs); |
51900510 | 3687 | |
d764a8e6 | 3688 | goto exit_point; |
5d0f30f7 KKT |
3689 | } |
3690 | ||
d764a8e6 IS |
3691 | /* the lhs must be of a suitable type, regardless of its underlying |
3692 | structure. */ | |
3693 | if (!objc_is_gcable_p (lhs)) | |
3694 | goto exit_point; | |
5d0f30f7 | 3695 | |
d764a8e6 | 3696 | outer = lhs; |
5d0f30f7 | 3697 | |
d764a8e6 IS |
3698 | while (outer |
3699 | && (TREE_CODE (outer) == COMPONENT_REF | |
3700 | || TREE_CODE (outer) == ARRAY_REF)) | |
3701 | outer = TREE_OPERAND (outer, 0); | |
e31c7eec | 3702 | |
d764a8e6 | 3703 | if (TREE_CODE (outer) == INDIRECT_REF) |
e31c7eec | 3704 | { |
d764a8e6 IS |
3705 | outer = TREE_OPERAND (outer, 0); |
3706 | indirect_p = 1; | |
e31c7eec TW |
3707 | } |
3708 | ||
d764a8e6 | 3709 | outer_gc_p = objc_is_gcable_p (outer); |
e31c7eec | 3710 | |
d764a8e6 IS |
3711 | /* Handle ivar assignments. */ |
3712 | if (objc_is_ivar_reference_p (lhs)) | |
3713 | { | |
3714 | /* if the struct to the left of the ivar is not an Objective-C object (__strong | |
3715 | doesn't cut it here), the best we can do here is suggest a cast. */ | |
3716 | if (!objc_is_gcable_type (TREE_TYPE (outer), 0)) | |
3717 | { | |
3718 | /* We may still be able to use the global write barrier... */ | |
3719 | if (!indirect_p && objc_is_global_reference_p (outer)) | |
3720 | goto global_reference; | |
264fa2db | 3721 | |
d764a8e6 IS |
3722 | suggest_cast: |
3723 | if (modifycode == NOP_EXPR) | |
3724 | { | |
3725 | if (warn_assign_intercept) | |
3726 | warning (0, "strong-cast may possibly be needed"); | |
3727 | } | |
e31c7eec | 3728 | |
d764a8e6 IS |
3729 | goto exit_point; |
3730 | } | |
e31c7eec | 3731 | |
d764a8e6 IS |
3732 | if (modifycode != NOP_EXPR) |
3733 | goto invalid_pointer_arithmetic; | |
6c65299b | 3734 | |
d764a8e6 IS |
3735 | if (warn_assign_intercept) |
3736 | warning (0, "instance variable assignment has been intercepted"); | |
e31c7eec | 3737 | |
d764a8e6 | 3738 | result = objc_build_ivar_assignment (outer, lhs, rhs); |
6c65299b | 3739 | |
d764a8e6 IS |
3740 | goto exit_point; |
3741 | } | |
3742 | ||
3743 | /* Likewise, intercept assignment to global/static variables if their type is | |
3744 | GC-marked. */ | |
3745 | if (objc_is_global_reference_p (outer)) | |
6c65299b | 3746 | { |
d764a8e6 IS |
3747 | if (indirect_p) |
3748 | goto suggest_cast; | |
6c65299b | 3749 | |
d764a8e6 IS |
3750 | global_reference: |
3751 | if (modifycode != NOP_EXPR) | |
3752 | { | |
3753 | invalid_pointer_arithmetic: | |
3754 | if (outer_gc_p) | |
3755 | warning (0, "pointer arithmetic for garbage-collected objects not allowed"); | |
6c65299b | 3756 | |
d764a8e6 IS |
3757 | goto exit_point; |
3758 | } | |
3759 | ||
3760 | if (warn_assign_intercept) | |
3761 | warning (0, "global/static variable assignment has been intercepted"); | |
3762 | ||
3763 | result = objc_build_global_assignment (lhs, rhs); | |
6c65299b | 3764 | } |
d764a8e6 IS |
3765 | |
3766 | /* In all other cases, fall back to the normal mechanism. */ | |
3767 | exit_point: | |
3768 | return result; | |
6c65299b RS |
3769 | } |
3770 | ||
3cc2dd4b NP |
3771 | /* Implementation of the table mapping a class name (as an identifier) |
3772 | to a class node. The two public functions for it are | |
3773 | lookup_interface() and add_interface(). add_interface() is only | |
3774 | used in this file, so we can make it static. */ | |
264fa2db | 3775 | |
3cc2dd4b | 3776 | static GTY(()) objc_map_t interface_map; |
d11dd684 | 3777 | |
3cc2dd4b NP |
3778 | static void |
3779 | interface_hash_init (void) | |
d764a8e6 | 3780 | { |
3cc2dd4b | 3781 | interface_map = objc_map_alloc_ggc (200); |
d764a8e6 IS |
3782 | } |
3783 | ||
3cc2dd4b NP |
3784 | static tree |
3785 | add_interface (tree class_name, tree name) | |
d764a8e6 | 3786 | { |
3cc2dd4b NP |
3787 | /* Put interfaces on list in reverse order. */ |
3788 | TREE_CHAIN (class_name) = interface_chain; | |
3789 | interface_chain = class_name; | |
3790 | ||
3791 | /* Add it to the map. */ | |
3792 | objc_map_put (interface_map, name, class_name); | |
3793 | ||
3794 | return interface_chain; | |
d764a8e6 | 3795 | } |
38b9c8c3 | 3796 | |
d764a8e6 IS |
3797 | tree |
3798 | lookup_interface (tree ident) | |
3799 | { | |
d11dd684 | 3800 | #ifdef OBJCPLUS |
d764a8e6 IS |
3801 | if (ident && TREE_CODE (ident) == TYPE_DECL) |
3802 | ident = DECL_NAME (ident); | |
264fa2db | 3803 | #endif |
264fa2db | 3804 | |
d764a8e6 IS |
3805 | if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE) |
3806 | return NULL_TREE; | |
264fa2db | 3807 | |
d764a8e6 | 3808 | { |
3cc2dd4b | 3809 | tree interface = objc_map_get (interface_map, ident); |
6c65299b | 3810 | |
3cc2dd4b NP |
3811 | if (interface == OBJC_MAP_NOT_FOUND) |
3812 | return NULL_TREE; | |
3813 | else | |
3814 | return interface; | |
d764a8e6 IS |
3815 | } |
3816 | } | |
51900510 | 3817 | |
0dc33c3c NP |
3818 | |
3819 | ||
d764a8e6 | 3820 | /* Implement @defs (<classname>) within struct bodies. */ |
6c65299b | 3821 | |
d764a8e6 IS |
3822 | tree |
3823 | objc_get_class_ivars (tree class_name) | |
3824 | { | |
3825 | tree interface = lookup_interface (class_name); | |
6c65299b | 3826 | |
d764a8e6 IS |
3827 | if (interface) |
3828 | return get_class_ivars (interface, true); | |
7651f8f7 | 3829 | |
d764a8e6 IS |
3830 | error ("cannot find interface declaration for %qE", |
3831 | class_name); | |
6c65299b | 3832 | |
d764a8e6 | 3833 | return error_mark_node; |
e31c7eec TW |
3834 | } |
3835 | ||
0dc33c3c NP |
3836 | |
3837 | /* Functions used by the hashtable for field duplicates in | |
3838 | objc_detect_field_duplicates(). Ideally, we'd use a standard | |
3839 | key-value dictionary hashtable , and store as keys the field names, | |
3840 | and as values the actual declarations (used to print nice error | |
3841 | messages with the locations). But, the hashtable we are using only | |
3842 | allows us to store keys in the hashtable, without values (it looks | |
3843 | more like a set). So, we store the DECLs, but define equality as | |
3844 | DECLs having the same name, and hash as the hash of the name. */ | |
703c8606 LC |
3845 | |
3846 | struct decl_name_hash : typed_noop_remove <tree_node> | |
3847 | { | |
5831a5f0 LC |
3848 | typedef tree_node value_type; |
3849 | typedef tree_node compare_type; | |
3850 | static inline hashval_t hash (const value_type *); | |
3851 | static inline bool equal (const value_type *, const compare_type *); | |
703c8606 LC |
3852 | }; |
3853 | ||
3854 | inline hashval_t | |
5831a5f0 | 3855 | decl_name_hash::hash (const value_type *q) |
0dc33c3c | 3856 | { |
0dc33c3c NP |
3857 | return (hashval_t) ((intptr_t)(DECL_NAME (q)) >> 3); |
3858 | } | |
3859 | ||
703c8606 | 3860 | inline bool |
5831a5f0 | 3861 | decl_name_hash::equal (const value_type *a, const compare_type *b) |
0dc33c3c | 3862 | { |
0dc33c3c NP |
3863 | return DECL_NAME (a) == DECL_NAME (b); |
3864 | } | |
3865 | ||
d764a8e6 | 3866 | /* Called when checking the variables in a struct. If we are not |
0dc33c3c NP |
3867 | doing the ivars list inside an @interface context, then return |
3868 | false. Else, perform the check for duplicate ivars, then return | |
3869 | true. The check for duplicates checks if an instance variable with | |
3870 | the same name exists in the class or in a superclass. If | |
3871 | 'check_superclasses_only' is set to true, then it is assumed that | |
3872 | checks for instance variables in the same class has already been | |
3873 | performed (this is the case for ObjC++) and only the instance | |
3874 | variables of superclasses are checked. */ | |
3875 | bool | |
3876 | objc_detect_field_duplicates (bool check_superclasses_only) | |
d764a8e6 | 3877 | { |
944fb799 | 3878 | if (!objc_collecting_ivars || !objc_interface_context |
0dc33c3c NP |
3879 | || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE) |
3880 | return false; | |
3881 | ||
3882 | /* We have two ways of doing this check: | |
944fb799 | 3883 | |
0dc33c3c NP |
3884 | "direct comparison": we iterate over the instance variables and |
3885 | compare them directly. This works great for small numbers of | |
3886 | instance variables (such as 10 or 20), which are extremely common. | |
3887 | But it will potentially take forever for the pathological case with | |
3888 | a huge number (eg, 10k) of instance variables. | |
944fb799 | 3889 | |
0dc33c3c NP |
3890 | "hashtable": we use a hashtable, which requires a single sweep |
3891 | through the list of instances variables. This is much slower for a | |
3892 | small number of variables, and we only use it for large numbers. | |
3893 | ||
3894 | To decide which one to use, we need to get an idea of how many | |
3895 | instance variables we have to compare. */ | |
3896 | { | |
3897 | unsigned int number_of_ivars_to_check = 0; | |
3898 | { | |
3899 | tree ivar; | |
3900 | for (ivar = CLASS_RAW_IVARS (objc_interface_context); | |
3901 | ivar; ivar = DECL_CHAIN (ivar)) | |
3902 | { | |
3903 | /* Ignore anonymous ivars. */ | |
3904 | if (DECL_NAME (ivar)) | |
3905 | number_of_ivars_to_check++; | |
3906 | } | |
3907 | } | |
d764a8e6 | 3908 | |
0dc33c3c NP |
3909 | /* Exit if there is nothing to do. */ |
3910 | if (number_of_ivars_to_check == 0) | |
3911 | return true; | |
944fb799 | 3912 | |
0dc33c3c NP |
3913 | /* In case that there are only 1 or 2 instance variables to check, |
3914 | we always use direct comparison. If there are more, it is | |
3915 | worth iterating over the instance variables in the superclass | |
3916 | to count how many there are (note that this has the same cost | |
3917 | as checking 1 instance variable by direct comparison, which is | |
3918 | why we skip this check in the case of 1 or 2 ivars and just do | |
3919 | the direct comparison) and then decide if it worth using a | |
3920 | hashtable. */ | |
3921 | if (number_of_ivars_to_check > 2) | |
3922 | { | |
3923 | unsigned int number_of_superclass_ivars = 0; | |
3924 | { | |
3925 | tree interface; | |
3926 | for (interface = lookup_interface (CLASS_SUPER_NAME (objc_interface_context)); | |
3927 | interface; interface = lookup_interface (CLASS_SUPER_NAME (interface))) | |
3928 | { | |
3929 | tree ivar; | |
3930 | for (ivar = CLASS_RAW_IVARS (interface); | |
3931 | ivar; ivar = DECL_CHAIN (ivar)) | |
3932 | number_of_superclass_ivars++; | |
3933 | } | |
3934 | } | |
3935 | ||
3936 | /* We use a hashtable if we have over 10k comparisons. */ | |
944fb799 | 3937 | if (number_of_ivars_to_check * (number_of_superclass_ivars |
0dc33c3c NP |
3938 | + (number_of_ivars_to_check / 2)) |
3939 | > 10000) | |
3940 | { | |
3941 | /* First, build the hashtable by putting all the instance | |
3942 | variables of superclasses in it. */ | |
c203e8a7 | 3943 | hash_table<decl_name_hash> htab (37); |
0dc33c3c NP |
3944 | tree interface; |
3945 | for (interface = lookup_interface (CLASS_SUPER_NAME | |
3946 | (objc_interface_context)); | |
3947 | interface; interface = lookup_interface | |
3948 | (CLASS_SUPER_NAME (interface))) | |
3949 | { | |
3950 | tree ivar; | |
3951 | for (ivar = CLASS_RAW_IVARS (interface); ivar; | |
3952 | ivar = DECL_CHAIN (ivar)) | |
3953 | { | |
3954 | if (DECL_NAME (ivar) != NULL_TREE) | |
3955 | { | |
703c8606 | 3956 | tree_node **slot = htab.find_slot (ivar, INSERT); |
0dc33c3c NP |
3957 | /* Do not check for duplicate instance |
3958 | variables in superclasses. Errors have | |
3959 | already been generated. */ | |
3960 | *slot = ivar; | |
3961 | } | |
3962 | } | |
3963 | } | |
944fb799 | 3964 | |
0dc33c3c NP |
3965 | /* Now, we go through all the instance variables in the |
3966 | class, and check that they are not in the | |
3967 | hashtable. */ | |
3968 | if (check_superclasses_only) | |
3969 | { | |
3970 | tree ivar; | |
3971 | for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar; | |
3972 | ivar = DECL_CHAIN (ivar)) | |
3973 | { | |
3974 | if (DECL_NAME (ivar) != NULL_TREE) | |
3975 | { | |
703c8606 | 3976 | tree duplicate_ivar = htab.find (ivar); |
0dc33c3c NP |
3977 | if (duplicate_ivar != HTAB_EMPTY_ENTRY) |
3978 | { | |
3979 | error_at (DECL_SOURCE_LOCATION (ivar), | |
3980 | "duplicate instance variable %q+D", | |
3981 | ivar); | |
3982 | inform (DECL_SOURCE_LOCATION (duplicate_ivar), | |
3983 | "previous declaration of %q+D", | |
3984 | duplicate_ivar); | |
3985 | /* FIXME: Do we need the following ? */ | |
3986 | /* DECL_NAME (ivar) = NULL_TREE; */ | |
3987 | } | |
3988 | } | |
3989 | } | |
3990 | } | |
3991 | else | |
3992 | { | |
3993 | /* If we're checking for duplicates in the class as | |
3994 | well, we insert variables in the hashtable as we | |
3995 | check them, so if a duplicate follows, it will be | |
3996 | caught. */ | |
3997 | tree ivar; | |
3998 | for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar; | |
3999 | ivar = DECL_CHAIN (ivar)) | |
4000 | { | |
4001 | if (DECL_NAME (ivar) != NULL_TREE) | |
4002 | { | |
703c8606 | 4003 | tree_node **slot = htab.find_slot (ivar, INSERT); |
0dc33c3c NP |
4004 | if (*slot) |
4005 | { | |
4006 | tree duplicate_ivar = (tree)(*slot); | |
4007 | error_at (DECL_SOURCE_LOCATION (ivar), | |
4008 | "duplicate instance variable %q+D", | |
4009 | ivar); | |
4010 | inform (DECL_SOURCE_LOCATION (duplicate_ivar), | |
4011 | "previous declaration of %q+D", | |
4012 | duplicate_ivar); | |
4013 | /* FIXME: Do we need the following ? */ | |
4014 | /* DECL_NAME (ivar) = NULL_TREE; */ | |
4015 | } | |
4016 | *slot = ivar; | |
4017 | } | |
4018 | } | |
4019 | } | |
0dc33c3c NP |
4020 | return true; |
4021 | } | |
4022 | } | |
4023 | } | |
944fb799 | 4024 | |
0dc33c3c NP |
4025 | /* This is the "direct comparison" approach, which is used in most |
4026 | non-pathological cases. */ | |
4027 | { | |
4028 | /* Walk up to class hierarchy, starting with this class (this is | |
4029 | the external loop, because lookup_interface() is expensive, and | |
4030 | we want to do it few times). */ | |
4031 | tree interface = objc_interface_context; | |
4032 | ||
4033 | if (check_superclasses_only) | |
4034 | interface = lookup_interface (CLASS_SUPER_NAME (interface)); | |
944fb799 | 4035 | |
0dc33c3c NP |
4036 | for ( ; interface; interface = lookup_interface |
4037 | (CLASS_SUPER_NAME (interface))) | |
4038 | { | |
4039 | tree ivar_being_checked; | |
4040 | ||
4041 | for (ivar_being_checked = CLASS_RAW_IVARS (objc_interface_context); | |
4042 | ivar_being_checked; | |
4043 | ivar_being_checked = DECL_CHAIN (ivar_being_checked)) | |
4044 | { | |
4045 | tree decl; | |
944fb799 | 4046 | |
0dc33c3c NP |
4047 | /* Ignore anonymous ivars. */ |
4048 | if (DECL_NAME (ivar_being_checked) == NULL_TREE) | |
4049 | continue; | |
4050 | ||
4051 | /* Note how we stop when we find the ivar we are checking | |
4052 | (this can only happen in the main class, not | |
4053 | superclasses), to avoid comparing things twice | |
4054 | (otherwise, for each ivar, you'd compare A to B then B | |
4055 | to A, and get duplicated error messages). */ | |
4056 | for (decl = CLASS_RAW_IVARS (interface); | |
4057 | decl && decl != ivar_being_checked; | |
4058 | decl = DECL_CHAIN (decl)) | |
4059 | { | |
4060 | if (DECL_NAME (ivar_being_checked) == DECL_NAME (decl)) | |
4061 | { | |
4062 | error_at (DECL_SOURCE_LOCATION (ivar_being_checked), | |
4063 | "duplicate instance variable %q+D", | |
4064 | ivar_being_checked); | |
4065 | inform (DECL_SOURCE_LOCATION (decl), | |
4066 | "previous declaration of %q+D", | |
4067 | decl); | |
4068 | /* FIXME: Do we need the following ? */ | |
4069 | /* DECL_NAME (ivar_being_checked) = NULL_TREE; */ | |
4070 | } | |
4071 | } | |
4072 | } | |
4073 | } | |
4074 | } | |
4075 | return true; | |
d764a8e6 | 4076 | } |
e31c7eec | 4077 | |
d764a8e6 IS |
4078 | /* Used by: build_private_template, continue_class, |
4079 | and for @defs constructs. */ | |
058bfe53 | 4080 | |
e31c7eec | 4081 | static tree |
d764a8e6 | 4082 | get_class_ivars (tree interface, bool inherited) |
e31c7eec | 4083 | { |
d764a8e6 IS |
4084 | tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface)); |
4085 | ||
4086 | /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared | |
4087 | by the current class (i.e., they do not include super-class ivars). | |
4088 | However, the CLASS_IVARS list will be side-effected by a call to | |
4089 | finish_struct(), which will fill in field offsets. */ | |
4090 | if (!CLASS_IVARS (interface)) | |
4091 | CLASS_IVARS (interface) = ivar_chain; | |
4092 | ||
4093 | if (!inherited) | |
4094 | return ivar_chain; | |
4095 | ||
4096 | while (CLASS_SUPER_NAME (interface)) | |
058bfe53 | 4097 | { |
d764a8e6 IS |
4098 | /* Prepend super-class ivars. */ |
4099 | interface = lookup_interface (CLASS_SUPER_NAME (interface)); | |
4100 | ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)), | |
4101 | ivar_chain); | |
058bfe53 | 4102 | } |
e31c7eec | 4103 | |
d764a8e6 IS |
4104 | return ivar_chain; |
4105 | } | |
4106 | ||
4107 | void | |
4108 | objc_maybe_warn_exceptions (location_t loc) | |
4109 | { | |
4110 | /* -fobjc-exceptions is required to enable Objective-C exceptions. | |
4111 | For example, on Darwin, ObjC exceptions require a sufficiently | |
4112 | recent version of the runtime, so the user must ask for them | |
4113 | explicitly. On other platforms, at the moment -fobjc-exceptions | |
4114 | triggers -fexceptions which again is required for exceptions to | |
4115 | work. */ | |
4116 | if (!flag_objc_exceptions) | |
e31c7eec | 4117 | { |
d764a8e6 IS |
4118 | /* Warn only once per compilation unit. */ |
4119 | static bool warned = false; | |
e31c7eec | 4120 | |
d764a8e6 IS |
4121 | if (!warned) |
4122 | { | |
4123 | error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax"); | |
4124 | warned = true; | |
4125 | } | |
6c65299b | 4126 | } |
d764a8e6 IS |
4127 | } |
4128 | ||
4129 | static struct objc_try_context *cur_try_context; | |
e31c7eec | 4130 | |
d764a8e6 IS |
4131 | /* Called just after parsing the @try and its associated BODY. We now |
4132 | must prepare for the tricky bits -- handling the catches and finally. */ | |
e31c7eec | 4133 | |
d764a8e6 IS |
4134 | void |
4135 | objc_begin_try_stmt (location_t try_locus, tree body) | |
4136 | { | |
4137 | struct objc_try_context *c = XCNEW (struct objc_try_context); | |
4138 | c->outer = cur_try_context; | |
4139 | c->try_body = body; | |
4140 | c->try_locus = try_locus; | |
4141 | c->end_try_locus = input_location; | |
4142 | cur_try_context = c; | |
e31c7eec | 4143 | |
d764a8e6 IS |
4144 | /* Collect the list of local variables. We'll mark them as volatile |
4145 | at the end of compilation of this function to prevent them being | |
4146 | clobbered by setjmp/longjmp. */ | |
4147 | if (flag_objc_sjlj_exceptions) | |
4148 | objc_mark_locals_volatile (NULL); | |
e31c7eec TW |
4149 | } |
4150 | ||
d764a8e6 IS |
4151 | /* Called just after parsing "@catch (parm)". Open a binding level, |
4152 | enter DECL into the binding level, and initialize it. Leave the | |
4153 | binding level open while the body of the compound statement is | |
4154 | parsed. If DECL is NULL_TREE, then we are compiling "@catch(...)" | |
4155 | which we compile as "@catch(id tmp_variable)". */ | |
4156 | ||
e31c7eec | 4157 | void |
d764a8e6 | 4158 | objc_begin_catch_clause (tree decl) |
e31c7eec | 4159 | { |
d764a8e6 IS |
4160 | tree compound, type, t; |
4161 | bool ellipsis = false; | |
264fa2db | 4162 | |
d764a8e6 IS |
4163 | /* Begin a new scope that the entire catch clause will live in. */ |
4164 | compound = c_begin_compound_stmt (true); | |
264fa2db | 4165 | |
d764a8e6 IS |
4166 | /* Create the appropriate declaration for the argument. */ |
4167 | if (decl == error_mark_node) | |
4168 | type = error_mark_node; | |
4169 | else | |
4170 | { | |
4171 | if (decl == NULL_TREE) | |
4172 | { | |
4173 | /* If @catch(...) was specified, create a temporary variable of | |
4174 | type 'id' and use it. */ | |
4175 | decl = objc_create_temporary_var (objc_object_type, "__objc_generic_catch_var"); | |
4176 | DECL_SOURCE_LOCATION (decl) = input_location; | |
4177 | /* ... but allow the runtime to differentiate between ellipsis and the | |
4178 | case of @catch (id xyz). */ | |
4179 | ellipsis = true; | |
4180 | } | |
4181 | else | |
4182 | { | |
4183 | /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */ | |
4184 | decl = build_decl (input_location, | |
4185 | VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl)); | |
4186 | } | |
4187 | lang_hooks.decls.pushdecl (decl); | |
e31c7eec | 4188 | |
d764a8e6 IS |
4189 | /* Mark the declaration as used so you never any warnings whether |
4190 | you use the exception argument or not. TODO: Implement a | |
4191 | -Wunused-exception-parameter flag, which would cause warnings | |
4192 | if exception parameter is not used. */ | |
4193 | TREE_USED (decl) = 1; | |
4194 | DECL_READ_P (decl) = 1; | |
e31c7eec | 4195 | |
d764a8e6 IS |
4196 | type = TREE_TYPE (decl); |
4197 | } | |
e31c7eec | 4198 | |
d764a8e6 IS |
4199 | /* Verify that the type of the catch is valid. It must be a pointer |
4200 | to an Objective-C class, or "id" (which is catch-all). */ | |
4201 | if (type == error_mark_node) | |
4202 | { | |
4203 | ;/* Just keep going. */ | |
4204 | } | |
4205 | else if (!objc_type_valid_for_messaging (type, false)) | |
4206 | { | |
4207 | error ("@catch parameter is not a known Objective-C class type"); | |
4208 | type = error_mark_node; | |
4209 | } | |
4210 | else if (TYPE_HAS_OBJC_INFO (TREE_TYPE (type)) | |
4211 | && TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (type))) | |
4212 | { | |
4213 | error ("@catch parameter can not be protocol-qualified"); | |
944fb799 | 4214 | type = error_mark_node; |
d764a8e6 IS |
4215 | } |
4216 | else if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type))) | |
4217 | /* @catch (id xyz) or @catch (...) but we note this for runtimes that | |
4218 | identify 'id'. */ | |
4219 | ; | |
4220 | else | |
4221 | { | |
4222 | /* If 'type' was built using typedefs, we need to get rid of | |
4223 | them and get a simple pointer to the class. */ | |
4224 | bool is_typedef = false; | |
4225 | tree x = TYPE_MAIN_VARIANT (type); | |
944fb799 | 4226 | |
d764a8e6 IS |
4227 | /* Skip from the pointer to the pointee. */ |
4228 | if (TREE_CODE (x) == POINTER_TYPE) | |
4229 | x = TREE_TYPE (x); | |
944fb799 | 4230 | |
d764a8e6 IS |
4231 | /* Traverse typedef aliases */ |
4232 | while (TREE_CODE (x) == RECORD_TYPE && OBJC_TYPE_NAME (x) | |
4233 | && TREE_CODE (OBJC_TYPE_NAME (x)) == TYPE_DECL | |
4234 | && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x))) | |
e31c7eec | 4235 | { |
d764a8e6 IS |
4236 | is_typedef = true; |
4237 | x = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x)); | |
4238 | } | |
035e8f01 | 4239 | |
d764a8e6 IS |
4240 | /* If it was a typedef, build a pointer to the final, original |
4241 | class. */ | |
4242 | if (is_typedef) | |
4243 | type = build_pointer_type (x); | |
035e8f01 | 4244 | |
d764a8e6 IS |
4245 | if (cur_try_context->catch_list) |
4246 | { | |
4247 | /* Examine previous @catch clauses and see if we've already | |
4248 | caught the type in question. */ | |
4249 | tree_stmt_iterator i = tsi_start (cur_try_context->catch_list); | |
4250 | for (; !tsi_end_p (i); tsi_next (&i)) | |
4251 | { | |
4252 | tree stmt = tsi_stmt (i); | |
4253 | t = CATCH_TYPES (stmt); | |
4254 | if (t == error_mark_node) | |
4255 | continue; | |
4256 | if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type))) | |
035e8f01 | 4257 | { |
d764a8e6 IS |
4258 | warning (0, "exception of type %<%T%> will be caught", |
4259 | TREE_TYPE (type)); | |
4260 | warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>", | |
4261 | TREE_TYPE (t ? t : objc_object_type)); | |
4262 | break; | |
035e8f01 | 4263 | } |
264fa2db | 4264 | } |
e31c7eec TW |
4265 | } |
4266 | } | |
6c65299b | 4267 | |
d764a8e6 IS |
4268 | t = (*runtime.begin_catch) (&cur_try_context, type, decl, compound, ellipsis); |
4269 | add_stmt (t); | |
4270 | } | |
6c65299b | 4271 | |
d764a8e6 IS |
4272 | /* Called just after parsing the closing brace of a @catch clause. Close |
4273 | the open binding level, and record a CATCH_EXPR for it. */ | |
264fa2db | 4274 | |
d764a8e6 IS |
4275 | void |
4276 | objc_finish_catch_clause (void) | |
4277 | { | |
4278 | tree c = cur_try_context->current_catch; | |
4279 | cur_try_context->current_catch = NULL; | |
4280 | cur_try_context->end_catch_locus = input_location; | |
264fa2db | 4281 | |
d764a8e6 | 4282 | CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1); |
6c65299b | 4283 | |
d764a8e6 IS |
4284 | (*runtime.finish_catch) (&cur_try_context, c); |
4285 | } | |
6c65299b | 4286 | |
d764a8e6 IS |
4287 | /* Called after parsing a @finally clause and its associated BODY. |
4288 | Record the body for later placement. */ | |
6c65299b | 4289 | |
d764a8e6 IS |
4290 | void |
4291 | objc_build_finally_clause (location_t finally_locus, tree body) | |
4292 | { | |
4293 | cur_try_context->finally_body = body; | |
4294 | cur_try_context->finally_locus = finally_locus; | |
4295 | cur_try_context->end_finally_locus = input_location; | |
370ce32a ZL |
4296 | } |
4297 | ||
d764a8e6 | 4298 | /* Called to finalize a @try construct. */ |
6408ef33 ZL |
4299 | |
4300 | tree | |
d764a8e6 | 4301 | objc_finish_try_stmt (void) |
6408ef33 | 4302 | { |
d764a8e6 IS |
4303 | struct objc_try_context *c = cur_try_context; |
4304 | tree stmt; | |
6408ef33 | 4305 | |
d764a8e6 IS |
4306 | if (c->catch_list == NULL && c->finally_body == NULL) |
4307 | error ("%<@try%> without %<@catch%> or %<@finally%>"); | |
6408ef33 | 4308 | |
d764a8e6 IS |
4309 | stmt = (*runtime.finish_try_stmt) (&cur_try_context); |
4310 | add_stmt (stmt); | |
6408ef33 | 4311 | |
d764a8e6 IS |
4312 | cur_try_context = c->outer; |
4313 | free (c); | |
4314 | return stmt; | |
4315 | } | |
264fa2db | 4316 | |
370ce32a | 4317 | tree |
d764a8e6 | 4318 | objc_build_throw_stmt (location_t loc, tree throw_expr) |
370ce32a | 4319 | { |
d764a8e6 | 4320 | bool rethrown = false; |
6408ef33 | 4321 | |
d764a8e6 | 4322 | objc_maybe_warn_exceptions (loc); |
6408ef33 | 4323 | |
d764a8e6 IS |
4324 | /* Don't waste time trying to build something if we're already dead. */ |
4325 | if (throw_expr == error_mark_node) | |
4326 | return error_mark_node; | |
6408ef33 | 4327 | |
d764a8e6 IS |
4328 | if (throw_expr == NULL) |
4329 | { | |
4330 | /* If we're not inside a @catch block, there is no "current | |
4331 | exception" to be rethrown. */ | |
4332 | if (cur_try_context == NULL | |
4333 | || cur_try_context->current_catch == NULL) | |
4334 | { | |
4335 | error_at (loc, "%<@throw%> (rethrow) used outside of a @catch block"); | |
4336 | return error_mark_node; | |
4337 | } | |
4338 | ||
4339 | /* Otherwise the object is still sitting in the EXC_PTR_EXPR | |
4340 | value that we get from the runtime. */ | |
4341 | throw_expr = (*runtime.build_exc_ptr) (&cur_try_context); | |
4342 | rethrown = true; | |
4343 | } | |
4344 | else | |
4345 | { | |
4346 | if (!objc_type_valid_for_messaging (TREE_TYPE (throw_expr), true)) | |
4347 | { | |
4348 | error_at (loc, "%<@throw%> argument is not an object"); | |
4349 | return error_mark_node; | |
4350 | } | |
4351 | } | |
4352 | ||
4353 | return (*runtime.build_throw_stmt) (loc, throw_expr, rethrown); | |
6c65299b RS |
4354 | } |
4355 | ||
d764a8e6 IS |
4356 | tree |
4357 | objc_build_synchronized (location_t start_locus, tree object_expr, tree body) | |
38b9c8c3 | 4358 | { |
d764a8e6 IS |
4359 | /* object_expr should never be NULL; but in case it is, convert it to |
4360 | error_mark_node. */ | |
4361 | if (object_expr == NULL) | |
4362 | object_expr = error_mark_node; | |
38b9c8c3 | 4363 | |
d764a8e6 IS |
4364 | /* Validate object_expr. If not valid, set it to error_mark_node. */ |
4365 | if (object_expr != error_mark_node) | |
38b9c8c3 | 4366 | { |
d764a8e6 IS |
4367 | if (!objc_type_valid_for_messaging (TREE_TYPE (object_expr), true)) |
4368 | { | |
4369 | error_at (start_locus, "%<@synchronized%> argument is not an object"); | |
4370 | object_expr = error_mark_node; | |
4371 | } | |
38b9c8c3 | 4372 | } |
944fb799 | 4373 | |
d764a8e6 IS |
4374 | if (object_expr == error_mark_node) |
4375 | { | |
4376 | /* If we found an error, we simply ignore the '@synchronized'. | |
4377 | Compile the body so we can keep going with minimal | |
4378 | casualties. */ | |
4379 | return add_stmt (body); | |
4380 | } | |
4381 | else | |
4382 | { | |
4383 | tree call; | |
4384 | tree args; | |
38b9c8c3 | 4385 | |
944fb799 | 4386 | /* objc_sync_enter (object_expr); */ |
d764a8e6 IS |
4387 | object_expr = save_expr (object_expr); |
4388 | args = tree_cons (NULL, object_expr, NULL); | |
4389 | call = build_function_call (input_location, | |
4390 | objc_sync_enter_decl, args); | |
4391 | SET_EXPR_LOCATION (call, start_locus); | |
4392 | add_stmt (call); | |
38b9c8c3 | 4393 | |
d764a8e6 IS |
4394 | /* Build "objc_sync_exit (object_expr);" but do not add it yet; |
4395 | it goes inside the @finalize() clause. */ | |
4396 | args = tree_cons (NULL, object_expr, NULL); | |
4397 | call = build_function_call (input_location, | |
4398 | objc_sync_exit_decl, args); | |
4399 | SET_EXPR_LOCATION (call, input_location); | |
38b9c8c3 | 4400 | |
d764a8e6 IS |
4401 | /* @try { body; } */ |
4402 | objc_begin_try_stmt (start_locus, body); | |
944fb799 | 4403 | |
d764a8e6 IS |
4404 | /* @finally { objc_sync_exit (object_expr); } */ |
4405 | objc_build_finally_clause (input_location, call); | |
944fb799 | 4406 | |
d764a8e6 IS |
4407 | /* End of try statement. */ |
4408 | return objc_finish_try_stmt (); | |
4409 | } | |
38b9c8c3 ZL |
4410 | } |
4411 | ||
d764a8e6 IS |
4412 | /* Construct a C struct corresponding to ObjC class CLASS, with the same |
4413 | name as the class: | |
38b9c8c3 | 4414 | |
d764a8e6 IS |
4415 | struct <classname> { |
4416 | struct _objc_class *isa; | |
4417 | ... | |
4418 | }; */ | |
38b9c8c3 | 4419 | |
d764a8e6 IS |
4420 | static void |
4421 | build_private_template (tree klass) | |
38b9c8c3 | 4422 | { |
d764a8e6 IS |
4423 | if (!CLASS_STATIC_TEMPLATE (klass)) |
4424 | { | |
4425 | tree record = objc_build_struct (klass, | |
4426 | get_class_ivars (klass, false), | |
4427 | CLASS_SUPER_NAME (klass)); | |
38b9c8c3 | 4428 | |
d764a8e6 IS |
4429 | /* Set the TREE_USED bit for this struct, so that stab generator |
4430 | can emit stabs for this struct type. */ | |
4431 | if (flag_debug_only_used_symbols && TYPE_STUB_DECL (record)) | |
4432 | TREE_USED (TYPE_STUB_DECL (record)) = 1; | |
38b9c8c3 | 4433 | |
d764a8e6 IS |
4434 | /* Copy the attributes from the class to the type. */ |
4435 | if (TREE_DEPRECATED (klass)) | |
4436 | TREE_DEPRECATED (record) = 1; | |
d764a8e6 | 4437 | } |
38b9c8c3 ZL |
4438 | } |
4439 | ||
d764a8e6 IS |
4440 | /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the |
4441 | current class. */ | |
4442 | #ifdef OBJCPLUS | |
4443 | static void | |
4444 | objc_generate_cxx_ctor_or_dtor (bool dtor) | |
4445 | { | |
4446 | tree fn, body, compound_stmt, ivar; | |
38b9c8c3 | 4447 | |
d764a8e6 IS |
4448 | /* - (id) .cxx_construct { ... return self; } */ |
4449 | /* - (void) .cxx_construct { ... } */ | |
38b9c8c3 | 4450 | |
d764a8e6 IS |
4451 | objc_start_method_definition |
4452 | (false /* is_class_method */, | |
4453 | objc_build_method_signature (false /* is_class_method */, | |
4454 | build_tree_list (NULL_TREE, | |
4455 | dtor | |
4456 | ? void_type_node | |
4457 | : objc_object_type), | |
4458 | get_identifier (dtor | |
4459 | ? TAG_CXX_DESTRUCT | |
4460 | : TAG_CXX_CONSTRUCT), | |
4461 | make_node (TREE_LIST), | |
a04a722b | 4462 | false), NULL, NULL_TREE); |
d764a8e6 IS |
4463 | body = begin_function_body (); |
4464 | compound_stmt = begin_compound_stmt (0); | |
38b9c8c3 | 4465 | |
d764a8e6 IS |
4466 | ivar = CLASS_IVARS (implementation_template); |
4467 | /* Destroy ivars in reverse order. */ | |
4468 | if (dtor) | |
4469 | ivar = nreverse (copy_list (ivar)); | |
64ee9490 | 4470 | |
d764a8e6 | 4471 | for (; ivar; ivar = TREE_CHAIN (ivar)) |
38b9c8c3 | 4472 | { |
d764a8e6 | 4473 | if (TREE_CODE (ivar) == FIELD_DECL) |
38b9c8c3 | 4474 | { |
d764a8e6 | 4475 | tree type = TREE_TYPE (ivar); |
38b9c8c3 | 4476 | |
d764a8e6 IS |
4477 | /* Call the ivar's default constructor or destructor. Do not |
4478 | call the destructor unless a corresponding constructor call | |
4479 | has also been made (or is not needed). */ | |
4480 | if (MAYBE_CLASS_TYPE_P (type) | |
4481 | && (dtor | |
4482 | ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) | |
4483 | && (!TYPE_NEEDS_CONSTRUCTING (type) | |
4484 | || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))) | |
4485 | : (TYPE_NEEDS_CONSTRUCTING (type) | |
4486 | && TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))) | |
4487 | finish_expr_stmt | |
4488 | (build_special_member_call | |
4489 | (build_ivar_reference (DECL_NAME (ivar)), | |
4490 | dtor ? complete_dtor_identifier : complete_ctor_identifier, | |
4491 | NULL, type, LOOKUP_NORMAL, tf_warning_or_error)); | |
38b9c8c3 | 4492 | } |
d764a8e6 | 4493 | } |
38b9c8c3 | 4494 | |
d764a8e6 IS |
4495 | /* The constructor returns 'self'. */ |
4496 | if (!dtor) | |
4497 | finish_return_stmt (self_decl); | |
38b9c8c3 | 4498 | |
d764a8e6 IS |
4499 | finish_compound_stmt (compound_stmt); |
4500 | finish_function_body (body); | |
4501 | fn = current_function_decl; | |
4502 | finish_function (); | |
4503 | objc_finish_method_definition (fn); | |
4504 | } | |
38b9c8c3 | 4505 | |
d764a8e6 IS |
4506 | /* The following routine will examine the current @interface for any |
4507 | non-POD C++ ivars requiring non-trivial construction and/or | |
4508 | destruction, and then synthesize special '- .cxx_construct' and/or | |
4509 | '- .cxx_destruct' methods which will run the appropriate | |
4510 | construction or destruction code. Note that ivars inherited from | |
4511 | super-classes are _not_ considered. */ | |
4512 | static void | |
4513 | objc_generate_cxx_cdtors (void) | |
4514 | { | |
4515 | bool need_ctor = false, need_dtor = false; | |
4516 | tree ivar; | |
38b9c8c3 | 4517 | |
d764a8e6 IS |
4518 | /* Error case, due to possibly an extra @end. */ |
4519 | if (!objc_implementation_context) | |
4520 | return; | |
38b9c8c3 | 4521 | |
d764a8e6 IS |
4522 | /* We do not want to do this for categories, since they do not have |
4523 | their own ivars. */ | |
38b9c8c3 | 4524 | |
d764a8e6 IS |
4525 | if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE) |
4526 | return; | |
38b9c8c3 | 4527 | |
d764a8e6 | 4528 | /* First, determine if we even need a constructor and/or destructor. */ |
38b9c8c3 | 4529 | |
d764a8e6 IS |
4530 | for (ivar = CLASS_IVARS (implementation_template); ivar; |
4531 | ivar = TREE_CHAIN (ivar)) | |
4532 | { | |
4533 | if (TREE_CODE (ivar) == FIELD_DECL) | |
4534 | { | |
4535 | tree type = TREE_TYPE (ivar); | |
38b9c8c3 | 4536 | |
d764a8e6 IS |
4537 | if (MAYBE_CLASS_TYPE_P (type)) |
4538 | { | |
4539 | if (TYPE_NEEDS_CONSTRUCTING (type) | |
4540 | && TYPE_HAS_DEFAULT_CONSTRUCTOR (type)) | |
4541 | /* NB: If a default constructor is not available, we will not | |
4542 | be able to initialize this ivar; the add_instance_variable() | |
4543 | routine will already have warned about this. */ | |
4544 | need_ctor = true; | |
4545 | ||
4546 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) | |
4547 | && (!TYPE_NEEDS_CONSTRUCTING (type) | |
4548 | || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))) | |
4549 | /* NB: If a default constructor is not available, we will not | |
4550 | call the destructor either, for symmetry. */ | |
4551 | need_dtor = true; | |
4552 | } | |
4553 | } | |
38b9c8c3 ZL |
4554 | } |
4555 | ||
d764a8e6 | 4556 | /* Generate '- .cxx_construct' if needed. */ |
38b9c8c3 | 4557 | |
d764a8e6 IS |
4558 | if (need_ctor) |
4559 | objc_generate_cxx_ctor_or_dtor (false); | |
38b9c8c3 | 4560 | |
d764a8e6 | 4561 | /* Generate '- .cxx_destruct' if needed. */ |
55cb0169 | 4562 | |
d764a8e6 IS |
4563 | if (need_dtor) |
4564 | objc_generate_cxx_ctor_or_dtor (true); | |
4565 | ||
4566 | /* The 'imp_list' variable points at an imp_entry record for the current | |
4567 | @implementation. Record the existence of '- .cxx_construct' and/or | |
4568 | '- .cxx_destruct' methods therein; it will be included in the | |
4569 | metadata for the class if the runtime needs it. */ | |
4570 | imp_list->has_cxx_cdtors = (need_ctor || need_dtor); | |
38b9c8c3 | 4571 | } |
d764a8e6 | 4572 | #endif |
38b9c8c3 | 4573 | |
d764a8e6 IS |
4574 | static void |
4575 | error_with_ivar (const char *message, tree decl) | |
38b9c8c3 | 4576 | { |
d764a8e6 IS |
4577 | error_at (DECL_SOURCE_LOCATION (decl), "%s %qs", |
4578 | message, identifier_to_locale (gen_declaration (decl))); | |
4579 | ||
38b9c8c3 ZL |
4580 | } |
4581 | ||
d764a8e6 IS |
4582 | static void |
4583 | check_ivars (tree inter, tree imp) | |
38b9c8c3 | 4584 | { |
d764a8e6 IS |
4585 | tree intdecls = CLASS_RAW_IVARS (inter); |
4586 | tree impdecls = CLASS_RAW_IVARS (imp); | |
4587 | ||
4588 | while (1) | |
4589 | { | |
4590 | tree t1, t2; | |
4591 | ||
264fa2db | 4592 | #ifdef OBJCPLUS |
d764a8e6 IS |
4593 | if (intdecls && TREE_CODE (intdecls) == TYPE_DECL) |
4594 | intdecls = TREE_CHAIN (intdecls); | |
264fa2db | 4595 | #endif |
d764a8e6 IS |
4596 | if (intdecls == 0 && impdecls == 0) |
4597 | break; | |
4598 | if (intdecls == 0 || impdecls == 0) | |
4599 | { | |
4600 | error ("inconsistent instance variable specification"); | |
4601 | break; | |
4602 | } | |
38b9c8c3 | 4603 | |
d764a8e6 | 4604 | t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls); |
38b9c8c3 | 4605 | |
d764a8e6 IS |
4606 | if (!comptypes (t1, t2) |
4607 | || !tree_int_cst_equal (DECL_INITIAL (intdecls), | |
4608 | DECL_INITIAL (impdecls))) | |
4609 | { | |
4610 | if (DECL_NAME (intdecls) == DECL_NAME (impdecls)) | |
4611 | { | |
4612 | error_with_ivar ("conflicting instance variable type", | |
4613 | impdecls); | |
4614 | error_with_ivar ("previous declaration of", | |
4615 | intdecls); | |
4616 | } | |
4617 | else /* both the type and the name don't match */ | |
4618 | { | |
4619 | error ("inconsistent instance variable specification"); | |
4620 | break; | |
4621 | } | |
4622 | } | |
264fa2db | 4623 | |
d764a8e6 IS |
4624 | else if (DECL_NAME (intdecls) != DECL_NAME (impdecls)) |
4625 | { | |
4626 | error_with_ivar ("conflicting instance variable name", | |
4627 | impdecls); | |
4628 | error_with_ivar ("previous declaration of", | |
4629 | intdecls); | |
4630 | } | |
264fa2db | 4631 | |
d764a8e6 IS |
4632 | intdecls = DECL_CHAIN (intdecls); |
4633 | impdecls = DECL_CHAIN (impdecls); | |
4634 | } | |
264fa2db ZL |
4635 | } |
4636 | ||
d764a8e6 IS |
4637 | |
4638 | static void | |
4639 | mark_referenced_methods (void) | |
c0c24aa4 | 4640 | { |
d764a8e6 IS |
4641 | struct imp_entry *impent; |
4642 | tree chain; | |
c0c24aa4 | 4643 | |
d764a8e6 IS |
4644 | for (impent = imp_list; impent; impent = impent->next) |
4645 | { | |
4646 | chain = CLASS_CLS_METHODS (impent->imp_context); | |
4647 | while (chain) | |
4648 | { | |
ead84f73 | 4649 | cgraph_mark_force_output_node ( |
a358e188 | 4650 | cgraph_get_create_node (METHOD_DEFINITION (chain))); |
d764a8e6 IS |
4651 | chain = DECL_CHAIN (chain); |
4652 | } | |
c0c24aa4 | 4653 | |
d764a8e6 IS |
4654 | chain = CLASS_NST_METHODS (impent->imp_context); |
4655 | while (chain) | |
4656 | { | |
ead84f73 | 4657 | cgraph_mark_force_output_node ( |
a358e188 | 4658 | cgraph_get_create_node (METHOD_DEFINITION (chain))); |
d764a8e6 IS |
4659 | chain = DECL_CHAIN (chain); |
4660 | } | |
4661 | } | |
4662 | } | |
e31c7eec | 4663 | |
d764a8e6 IS |
4664 | /* If type is empty or only type qualifiers are present, add default |
4665 | type of id (otherwise grokdeclarator will default to int). */ | |
4666 | static inline tree | |
4667 | adjust_type_for_id_default (tree type) | |
e31c7eec | 4668 | { |
d764a8e6 IS |
4669 | if (!type) |
4670 | type = make_node (TREE_LIST); | |
d11dd684 | 4671 | |
d764a8e6 IS |
4672 | if (!TREE_VALUE (type)) |
4673 | TREE_VALUE (type) = objc_object_type; | |
4674 | else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE | |
4675 | && TYPED_OBJECT (TREE_VALUE (type))) | |
4676 | error ("can not use an object as parameter to a method"); | |
264fa2db | 4677 | |
d764a8e6 | 4678 | return type; |
264fa2db ZL |
4679 | } |
4680 | ||
d764a8e6 IS |
4681 | /* Return a KEYWORD_DECL built using the specified key_name, arg_type, |
4682 | arg_name and attributes. (TODO: Rename KEYWORD_DECL to | |
4683 | OBJC_METHOD_PARM_DECL ?) | |
264fa2db | 4684 | |
d764a8e6 IS |
4685 | A KEYWORD_DECL is a tree representing the declaration of a |
4686 | parameter of an Objective-C method. It is produced when parsing a | |
4687 | fragment of Objective-C method declaration of the form | |
264fa2db | 4688 | |
d764a8e6 IS |
4689 | keyworddecl: |
4690 | selector ':' '(' typename ')' identifier | |
264fa2db | 4691 | |
d764a8e6 | 4692 | For example, take the Objective-C method |
264fa2db | 4693 | |
944fb799 | 4694 | -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type; |
264fa2db | 4695 | |
d764a8e6 IS |
4696 | the two fragments "pathForResource:(NSString *)resource" and |
4697 | "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The | |
4698 | KEYWORD_DECL stores the 'key_name' (eg, identifier for | |
4699 | "pathForResource"), the 'arg_type' (eg, tree representing a | |
4700 | NSString *), the 'arg_name' (eg identifier for "resource") and | |
4701 | potentially some attributes (for example, a tree representing | |
4702 | __attribute__ ((unused)) if such an attribute was attached to a | |
4703 | certain parameter). You can access this information using the | |
4704 | TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name), | |
4705 | KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes). | |
264fa2db | 4706 | |
d764a8e6 IS |
4707 | 'key_name' is an identifier node (and is optional as you can omit |
4708 | it in Objective-C methods). | |
4709 | 'arg_type' is a tree list (and is optional too if no parameter type | |
4710 | was specified). | |
4711 | 'arg_name' is an identifier node and is required. | |
4712 | 'attributes' is an optional tree containing parameter attributes. */ | |
4713 | tree | |
944fb799 | 4714 | objc_build_keyword_decl (tree key_name, tree arg_type, |
d764a8e6 IS |
4715 | tree arg_name, tree attributes) |
4716 | { | |
4717 | tree keyword_decl; | |
264fa2db | 4718 | |
d764a8e6 IS |
4719 | if (flag_objc1_only && attributes) |
4720 | error_at (input_location, "method argument attributes are not available in Objective-C 1.0"); | |
264fa2db | 4721 | |
d764a8e6 IS |
4722 | /* If no type is specified, default to "id". */ |
4723 | arg_type = adjust_type_for_id_default (arg_type); | |
325c3691 | 4724 | |
d764a8e6 | 4725 | keyword_decl = make_node (KEYWORD_DECL); |
f9417da1 | 4726 | |
d764a8e6 IS |
4727 | TREE_TYPE (keyword_decl) = arg_type; |
4728 | KEYWORD_ARG_NAME (keyword_decl) = arg_name; | |
4729 | KEYWORD_KEY_NAME (keyword_decl) = key_name; | |
4730 | DECL_ATTRIBUTES (keyword_decl) = attributes; | |
264fa2db | 4731 | |
d764a8e6 | 4732 | return keyword_decl; |
264fa2db | 4733 | } |
093c7153 | 4734 | |
d764a8e6 IS |
4735 | /* Given a chain of keyword_decl's, synthesize the full keyword selector. */ |
4736 | static tree | |
4737 | build_keyword_selector (tree selector) | |
264fa2db | 4738 | { |
d764a8e6 IS |
4739 | int len = 0; |
4740 | tree key_chain, key_name; | |
4741 | char *buf; | |
264fa2db | 4742 | |
d764a8e6 IS |
4743 | /* Scan the selector to see how much space we'll need. */ |
4744 | for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain)) | |
5287cfd5 | 4745 | { |
d764a8e6 | 4746 | switch (TREE_CODE (selector)) |
46270f14 | 4747 | { |
d764a8e6 IS |
4748 | case KEYWORD_DECL: |
4749 | key_name = KEYWORD_KEY_NAME (key_chain); | |
4750 | break; | |
4751 | case TREE_LIST: | |
4752 | key_name = TREE_PURPOSE (key_chain); | |
4753 | break; | |
4754 | default: | |
4755 | gcc_unreachable (); | |
46270f14 | 4756 | } |
d764a8e6 IS |
4757 | |
4758 | if (key_name) | |
4759 | len += IDENTIFIER_LENGTH (key_name) + 1; | |
4760 | else | |
4761 | /* Just a ':' arg. */ | |
4762 | len++; | |
46270f14 | 4763 | } |
5287cfd5 | 4764 | |
d764a8e6 IS |
4765 | buf = (char *) alloca (len + 1); |
4766 | /* Start the buffer out as an empty string. */ | |
4767 | buf[0] = '\0'; | |
093c7153 | 4768 | |
d764a8e6 | 4769 | for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain)) |
093c7153 | 4770 | { |
d764a8e6 | 4771 | switch (TREE_CODE (selector)) |
093c7153 | 4772 | { |
d764a8e6 IS |
4773 | case KEYWORD_DECL: |
4774 | key_name = KEYWORD_KEY_NAME (key_chain); | |
4775 | break; | |
4776 | case TREE_LIST: | |
4777 | key_name = TREE_PURPOSE (key_chain); | |
4778 | /* The keyword decl chain will later be used as a function | |
4779 | argument chain. Unhook the selector itself so as to not | |
4780 | confuse other parts of the compiler. */ | |
4781 | TREE_PURPOSE (key_chain) = NULL_TREE; | |
4782 | break; | |
4783 | default: | |
4784 | gcc_unreachable (); | |
093c7153 | 4785 | } |
264fa2db | 4786 | |
d764a8e6 IS |
4787 | if (key_name) |
4788 | strcat (buf, IDENTIFIER_POINTER (key_name)); | |
4789 | strcat (buf, ":"); | |
4790 | } | |
264fa2db | 4791 | |
1328049a | 4792 | return get_identifier_with_length (buf, len); |
264fa2db ZL |
4793 | } |
4794 | ||
d764a8e6 | 4795 | /* Used for declarations and definitions. */ |
093c7153 RH |
4796 | |
4797 | static tree | |
d764a8e6 IS |
4798 | build_method_decl (enum tree_code code, tree ret_type, tree selector, |
4799 | tree add_args, bool ellipsis) | |
264fa2db | 4800 | { |
d764a8e6 | 4801 | tree method_decl; |
264fa2db | 4802 | |
d764a8e6 IS |
4803 | /* If no type is specified, default to "id". */ |
4804 | ret_type = adjust_type_for_id_default (ret_type); | |
264fa2db | 4805 | |
d764a8e6 IS |
4806 | /* Note how a method_decl has a TREE_TYPE which is not the function |
4807 | type of the function implementing the method, but only the return | |
4808 | type of the method. We may want to change this, and store the | |
4809 | entire function type in there (eg, it may be used to simplify | |
4810 | dealing with attributes below). */ | |
4811 | method_decl = make_node (code); | |
4812 | TREE_TYPE (method_decl) = ret_type; | |
264fa2db | 4813 | |
d764a8e6 IS |
4814 | /* If we have a keyword selector, create an identifier_node that |
4815 | represents the full selector name (`:' included)... */ | |
4816 | if (TREE_CODE (selector) == KEYWORD_DECL) | |
4817 | { | |
4818 | METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector); | |
4819 | METHOD_SEL_ARGS (method_decl) = selector; | |
4820 | METHOD_ADD_ARGS (method_decl) = add_args; | |
4821 | METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis; | |
4822 | } | |
4823 | else | |
4824 | { | |
4825 | METHOD_SEL_NAME (method_decl) = selector; | |
4826 | METHOD_SEL_ARGS (method_decl) = NULL_TREE; | |
4827 | METHOD_ADD_ARGS (method_decl) = NULL_TREE; | |
4828 | } | |
264fa2db | 4829 | |
d764a8e6 | 4830 | return method_decl; |
264fa2db ZL |
4831 | } |
4832 | ||
d764a8e6 | 4833 | /* This routine processes objective-c method attributes. */ |
64ee9490 | 4834 | |
d764a8e6 IS |
4835 | static void |
4836 | objc_decl_method_attributes (tree *node, tree attributes, int flags) | |
264fa2db | 4837 | { |
d764a8e6 IS |
4838 | /* TODO: Replace the hackery below. An idea would be to store the |
4839 | full function type in the method declaration (for example in | |
4840 | TREE_TYPE) and then expose ObjC method declarations to c-family | |
4841 | and they could deal with them by simply treating them as | |
4842 | functions. */ | |
264fa2db | 4843 | |
d764a8e6 IS |
4844 | /* Because of the dangers in the hackery below, we filter out any |
4845 | attribute that we do not know about. For the ones we know about, | |
4846 | we know that they work with the hackery. For the other ones, | |
4847 | there is no guarantee, so we have to filter them out. */ | |
4848 | tree filtered_attributes = NULL_TREE; | |
264fa2db | 4849 | |
d764a8e6 | 4850 | if (attributes) |
264fa2db | 4851 | { |
d764a8e6 IS |
4852 | tree attribute; |
4853 | for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute)) | |
093c7153 | 4854 | { |
d764a8e6 | 4855 | tree name = TREE_PURPOSE (attribute); |
944fb799 | 4856 | |
d764a8e6 IS |
4857 | if (is_attribute_p ("deprecated", name) |
4858 | || is_attribute_p ("sentinel", name) | |
4859 | || is_attribute_p ("noreturn", name)) | |
093c7153 | 4860 | { |
d764a8e6 IS |
4861 | /* An attribute that we support; add it to the filtered |
4862 | attributes. */ | |
944fb799 | 4863 | filtered_attributes = chainon (filtered_attributes, |
d764a8e6 | 4864 | copy_node (attribute)); |
093c7153 | 4865 | } |
d764a8e6 IS |
4866 | else if (is_attribute_p ("format", name)) |
4867 | { | |
4868 | /* "format" is special because before adding it to the | |
4869 | filtered attributes we need to adjust the specified | |
4870 | format by adding the hidden function parameters for | |
4871 | an Objective-C method (self, _cmd). */ | |
4872 | tree new_attribute = copy_node (attribute); | |
4873 | ||
4874 | /* Check the arguments specified with the attribute, and | |
4875 | modify them adding 2 for the two hidden arguments. | |
4876 | Note how this differs from C++; according to the | |
4877 | specs, C++ does not do it so you have to add the +1 | |
4878 | yourself. For Objective-C, instead, the compiler | |
4879 | adds the +2 for you. */ | |
4880 | ||
4881 | /* The attribute arguments have not been checked yet, so | |
4882 | we need to be careful as they could be missing or | |
4883 | invalid. If anything looks wrong, we skip the | |
4884 | process and the compiler will complain about it later | |
4885 | when it validates the attribute. */ | |
4886 | /* Check that we have at least three arguments. */ | |
4887 | if (TREE_VALUE (new_attribute) | |
4888 | && TREE_CHAIN (TREE_VALUE (new_attribute)) | |
4889 | && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (new_attribute)))) | |
4890 | { | |
4891 | tree second_argument = TREE_CHAIN (TREE_VALUE (new_attribute)); | |
4892 | tree third_argument = TREE_CHAIN (second_argument); | |
4893 | tree number; | |
264fa2db | 4894 | |
d764a8e6 IS |
4895 | /* This is the second argument, the "string-index", |
4896 | which specifies the index of the format string | |
4897 | argument. Add 2. */ | |
4898 | number = TREE_VALUE (second_argument); | |
4899 | if (number | |
4900 | && TREE_CODE (number) == INTEGER_CST | |
807e902e KZ |
4901 | && !wi::eq_p (number, 0)) |
4902 | TREE_VALUE (second_argument) | |
4903 | = wide_int_to_tree (TREE_TYPE (number), | |
4904 | wi::add (number, 2)); | |
944fb799 | 4905 | |
d764a8e6 IS |
4906 | /* This is the third argument, the "first-to-check", |
4907 | which specifies the index of the first argument to | |
4908 | check. This could be 0, meaning it is not available, | |
4909 | in which case we don't need to add 2. Add 2 if not | |
4910 | 0. */ | |
4911 | number = TREE_VALUE (third_argument); | |
4912 | if (number | |
4913 | && TREE_CODE (number) == INTEGER_CST | |
807e902e KZ |
4914 | && !wi::eq_p (number, 0)) |
4915 | TREE_VALUE (third_argument) | |
4916 | = wide_int_to_tree (TREE_TYPE (number), | |
4917 | wi::add (number, 2)); | |
d764a8e6 IS |
4918 | } |
4919 | filtered_attributes = chainon (filtered_attributes, | |
4920 | new_attribute); | |
4921 | } | |
98ab0248 NP |
4922 | else if (is_attribute_p ("nonnull", name)) |
4923 | { | |
4924 | /* We need to fixup all the argument indexes by adding 2 | |
4925 | for the two hidden arguments of an Objective-C method | |
4926 | invocation, similat to what we do above for the | |
4927 | "format" attribute. */ | |
4928 | /* FIXME: This works great in terms of implementing the | |
4929 | functionality, but the warnings that are produced by | |
4930 | nonnull do mention the argument index (while the | |
4931 | format ones don't). For example, you could get | |
4932 | "warning: null argument where non-null required | |
4933 | (argument 3)". Now in that message, "argument 3" | |
4934 | includes the 2 hidden arguments; it would be much | |
4935 | more friendly to call it "argument 1", as that would | |
4936 | be consistent with __attribute__ ((nonnnull (1))). | |
4937 | To do this, we'd need to have the C family code that | |
4938 | checks the arguments know about adding/removing 2 to | |
4939 | the argument index ... or alternatively we could | |
4940 | maybe store the "printable" argument index in | |
4941 | addition to the actual argument index ? Some | |
4942 | refactoring is needed to do this elegantly. */ | |
4943 | tree new_attribute = copy_node (attribute); | |
4944 | tree argument = TREE_VALUE (attribute); | |
4945 | while (argument != NULL_TREE) | |
4946 | { | |
4947 | /* Get the value of the argument and add 2. */ | |
4948 | tree number = TREE_VALUE (argument); | |
807e902e KZ |
4949 | if (number && TREE_CODE (number) == INTEGER_CST |
4950 | && !wi::eq_p (number, 0)) | |
4951 | TREE_VALUE (argument) | |
4952 | = wide_int_to_tree (TREE_TYPE (number), | |
4953 | wi::add (number, 2)); | |
98ab0248 NP |
4954 | argument = TREE_CHAIN (argument); |
4955 | } | |
4956 | ||
4957 | filtered_attributes = chainon (filtered_attributes, | |
4958 | new_attribute); | |
4959 | } | |
d764a8e6 IS |
4960 | else |
4961 | warning (OPT_Wattributes, "%qE attribute directive ignored", name); | |
093c7153 RH |
4962 | } |
4963 | } | |
264fa2db | 4964 | |
d764a8e6 | 4965 | if (filtered_attributes) |
264fa2db | 4966 | { |
d764a8e6 IS |
4967 | /* This hackery changes the TREE_TYPE of the ObjC method |
4968 | declaration to be a function type, so that decl_attributes | |
4969 | will treat the ObjC method as if it was a function. Some | |
4970 | attributes (sentinel, format) will be applied to the function | |
4971 | type, changing it in place; so after calling decl_attributes, | |
4972 | we extract the function type attributes and store them in | |
4973 | METHOD_TYPE_ATTRIBUTES. Some other attributes (noreturn, | |
4974 | deprecated) are applied directly to the method declaration | |
4975 | (by setting TREE_DEPRECATED and TREE_THIS_VOLATILE) so there | |
4976 | is nothing to do. */ | |
4977 | tree saved_type = TREE_TYPE (*node); | |
6174da1b NF |
4978 | TREE_TYPE (*node) |
4979 | = build_function_type_for_method (TREE_VALUE (saved_type), *node, | |
4980 | METHOD_REF, 0); | |
d764a8e6 IS |
4981 | decl_attributes (node, filtered_attributes, flags); |
4982 | METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node)); | |
4983 | TREE_TYPE (*node) = saved_type; | |
264fa2db | 4984 | } |
264fa2db ZL |
4985 | } |
4986 | ||
944fb799 | 4987 | bool |
d764a8e6 IS |
4988 | objc_method_decl (enum tree_code opcode) |
4989 | { | |
4990 | return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL; | |
4991 | } | |
264fa2db | 4992 | |
6174da1b NF |
4993 | /* Return a function type for METHOD with RETURN_TYPE. CONTEXT is |
4994 | either METHOD_DEF or METHOD_REF, indicating whether we are defining a | |
4995 | method or calling one. SUPER_FLAG indicates whether this is a send | |
4996 | to super; this makes a difference for the NeXT calling sequence in | |
4997 | which the lookup and the method call are done together. If METHOD is | |
4998 | NULL, user-defined arguments (i.e., beyond self and _cmd) shall be | |
4999 | represented as varargs. */ | |
264fa2db | 5000 | |
d764a8e6 | 5001 | tree |
6174da1b NF |
5002 | build_function_type_for_method (tree return_type, tree method, |
5003 | int context, bool super_flag) | |
093c7153 | 5004 | { |
9771b263 | 5005 | vec<tree, va_gc> *argtypes = make_tree_vector (); |
6174da1b NF |
5006 | tree t, ftype; |
5007 | bool is_varargs = false; | |
093c7153 | 5008 | |
6174da1b | 5009 | (*runtime.get_arg_type_list_base) (&argtypes, method, context, super_flag); |
093c7153 | 5010 | |
6174da1b NF |
5011 | /* No actual method prototype given; remaining args passed as varargs. */ |
5012 | if (method == NULL_TREE) | |
5013 | { | |
5014 | is_varargs = true; | |
5015 | goto build_ftype; | |
5016 | } | |
093c7153 | 5017 | |
6174da1b | 5018 | for (t = METHOD_SEL_ARGS (method); t; t = DECL_CHAIN (t)) |
d764a8e6 | 5019 | { |
6174da1b | 5020 | tree arg_type = TREE_VALUE (TREE_TYPE (t)); |
093c7153 | 5021 | |
6174da1b NF |
5022 | /* Decay argument types for the underlying C function as |
5023 | appropriate. */ | |
d764a8e6 | 5024 | arg_type = objc_decay_parm_type (arg_type); |
093c7153 | 5025 | |
9771b263 | 5026 | vec_safe_push (argtypes, arg_type); |
d764a8e6 | 5027 | } |
093c7153 | 5028 | |
6174da1b | 5029 | if (METHOD_ADD_ARGS (method)) |
264fa2db | 5030 | { |
6174da1b NF |
5031 | for (t = TREE_CHAIN (METHOD_ADD_ARGS (method)); |
5032 | t; t = TREE_CHAIN (t)) | |
d764a8e6 | 5033 | { |
6174da1b | 5034 | tree arg_type = TREE_TYPE (TREE_VALUE (t)); |
d764a8e6 IS |
5035 | |
5036 | arg_type = objc_decay_parm_type (arg_type); | |
093c7153 | 5037 | |
9771b263 | 5038 | vec_safe_push (argtypes, arg_type); |
d764a8e6 | 5039 | } |
093c7153 | 5040 | |
6174da1b NF |
5041 | if (METHOD_ADD_ARGS_ELLIPSIS_P (method)) |
5042 | is_varargs = true; | |
264fa2db | 5043 | } |
6174da1b NF |
5044 | |
5045 | build_ftype: | |
5046 | if (is_varargs) | |
5047 | ftype = build_varargs_function_type_vec (return_type, argtypes); | |
093c7153 | 5048 | else |
6174da1b | 5049 | ftype = build_function_type_vec (return_type, argtypes); |
264fa2db | 5050 | |
6174da1b NF |
5051 | release_tree_vector (argtypes); |
5052 | return ftype; | |
d764a8e6 | 5053 | } |
264fa2db | 5054 | |
3cc2dd4b NP |
5055 | /* The 'method' argument is a tree; this tree could either be a single |
5056 | method, which is returned, or could be a TREE_VEC containing a list | |
5057 | of methods. In that case, the first one is returned, and warnings | |
5058 | are issued as appropriate. */ | |
d764a8e6 | 5059 | static tree |
3cc2dd4b | 5060 | check_duplicates (tree method, int methods, int is_class) |
093c7153 | 5061 | { |
3cc2dd4b NP |
5062 | tree first_method; |
5063 | size_t i; | |
264fa2db | 5064 | |
3cc2dd4b NP |
5065 | if (method == NULL_TREE) |
5066 | return NULL_TREE; | |
d764a8e6 | 5067 | |
3cc2dd4b NP |
5068 | if (TREE_CODE (method) != TREE_VEC) |
5069 | return method; | |
5070 | ||
5071 | /* We have two or more methods with the same name but different | |
5072 | types. */ | |
5073 | first_method = TREE_VEC_ELT (method, 0); | |
5074 | ||
5075 | /* But just how different are those types? If | |
5076 | -Wno-strict-selector-match is specified, we shall not complain if | |
5077 | the differences are solely among types with identical size and | |
5078 | alignment. */ | |
5079 | if (!warn_strict_selector_match) | |
5080 | { | |
5b6b2942 | 5081 | for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++) |
3cc2dd4b NP |
5082 | if (!comp_proto_with_proto (first_method, TREE_VEC_ELT (method, i), 0)) |
5083 | goto issue_warning; | |
5084 | ||
5085 | return first_method; | |
093c7153 | 5086 | } |
3cc2dd4b NP |
5087 | |
5088 | issue_warning: | |
5089 | if (methods) | |
5090 | { | |
5091 | bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL; | |
5092 | ||
5093 | warning_at (input_location, 0, | |
5094 | "multiple methods named %<%c%E%> found", | |
5095 | (is_class ? '+' : '-'), | |
5096 | METHOD_SEL_NAME (first_method)); | |
5097 | inform (DECL_SOURCE_LOCATION (first_method), "using %<%c%s%>", | |
5098 | (type ? '-' : '+'), | |
5099 | identifier_to_locale (gen_method_decl (first_method))); | |
5100 | } | |
5101 | else | |
5102 | { | |
5103 | bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL; | |
5104 | ||
5105 | warning_at (input_location, 0, | |
5106 | "multiple selectors named %<%c%E%> found", | |
5107 | (is_class ? '+' : '-'), | |
5108 | METHOD_SEL_NAME (first_method)); | |
5109 | inform (DECL_SOURCE_LOCATION (first_method), "found %<%c%s%>", | |
5110 | (type ? '-' : '+'), | |
5111 | identifier_to_locale (gen_method_decl (first_method))); | |
5112 | } | |
5113 | ||
5b6b2942 | 5114 | for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++) |
3cc2dd4b NP |
5115 | { |
5116 | bool type = TREE_CODE (TREE_VEC_ELT (method, i)) == INSTANCE_METHOD_DECL; | |
5117 | ||
5118 | inform (DECL_SOURCE_LOCATION (TREE_VEC_ELT (method, i)), "also found %<%c%s%>", | |
5119 | (type ? '-' : '+'), | |
5120 | identifier_to_locale (gen_method_decl (TREE_VEC_ELT (method, i)))); | |
5121 | } | |
5122 | ||
5123 | return first_method; | |
093c7153 | 5124 | } |
264fa2db | 5125 | |
d764a8e6 IS |
5126 | /* If RECEIVER is a class reference, return the identifier node for |
5127 | the referenced class. RECEIVER is created by objc_get_class_reference, | |
5128 | so we check the exact form created depending on which runtimes are | |
5129 | used. */ | |
264fa2db | 5130 | |
d764a8e6 IS |
5131 | static tree |
5132 | receiver_is_class_object (tree receiver, int self, int super) | |
093c7153 | 5133 | { |
d764a8e6 | 5134 | tree exp, arg; |
264fa2db | 5135 | |
d764a8e6 IS |
5136 | /* The receiver is 'self' or 'super' in the context of a class method. */ |
5137 | if (objc_method_context | |
5138 | && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL | |
5139 | && (self || super)) | |
5140 | return (super | |
5141 | ? CLASS_SUPER_NAME (implementation_template) | |
5142 | : CLASS_NAME (implementation_template)); | |
5143 | ||
5144 | /* The runtime might encapsulate things its own way. */ | |
5145 | exp = (*runtime.receiver_is_class_object) (receiver); | |
5146 | if (exp) | |
5147 | return exp; | |
5148 | ||
5149 | /* The receiver is a function call that returns an id. Check if | |
419b55d0 NP |
5150 | it is a call to objc_getClass, if so, pick up the class name. |
5151 | ||
5152 | This is required by the GNU runtime, which compiles | |
5153 | ||
5154 | [NSObject alloc] | |
5155 | ||
5156 | into | |
5157 | ||
5158 | [objc_get_class ("NSObject") alloc]; | |
5159 | ||
5160 | and then, to check that the receiver responds to the +alloc | |
5161 | method, needs to be able to determine that the objc_get_class() | |
5162 | call returns the NSObject class and not just a generic Class | |
5163 | pointer. | |
5164 | ||
5165 | But, traditionally this is enabled for all runtimes, not just the | |
5166 | GNU one, which means that the compiler is smarter than you'd | |
5167 | expect when dealing with objc_getClass(). For example, with the | |
5168 | Apple runtime, in the code | |
5169 | ||
5170 | [objc_getClass ("NSObject") alloc]; | |
5171 | ||
5172 | the compiler will recognize the objc_getClass() call as special | |
5173 | (due to the code below) and so will know that +alloc is called on | |
5174 | the 'NSObject' class, and can perform the corresponding checks. | |
5175 | ||
5176 | Programmers can disable this behaviour by casting the results of | |
5177 | objc_getClass() to 'Class' (this may seem weird because | |
5178 | objc_getClass() is already declared to return 'Class', but the | |
5179 | compiler treats it as a special function). This may be useful if | |
5180 | the class is never declared, and the compiler would complain | |
5181 | about a missing @interface for it. Then, you can do | |
5182 | ||
5183 | [(Class)objc_getClass ("MyClassNeverDeclared") alloc]; | |
5184 | ||
5185 | to silence the warnings. */ | |
d764a8e6 IS |
5186 | if (TREE_CODE (receiver) == CALL_EXPR |
5187 | && (exp = CALL_EXPR_FN (receiver)) | |
5188 | && TREE_CODE (exp) == ADDR_EXPR | |
5189 | && (exp = TREE_OPERAND (exp, 0)) | |
5190 | && TREE_CODE (exp) == FUNCTION_DECL | |
5191 | /* For some reason, we sometimes wind up with multiple FUNCTION_DECL | |
5192 | prototypes for objc_get_class(). Thankfully, they seem to share the | |
5193 | same function type. */ | |
5194 | && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl) | |
5195 | && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), runtime.tag_getclass) | |
5196 | /* We have a call to objc_get_class/objc_getClass! */ | |
5197 | && (arg = CALL_EXPR_ARG (receiver, 0))) | |
5198 | { | |
5199 | STRIP_NOPS (arg); | |
5200 | if (TREE_CODE (arg) == ADDR_EXPR | |
5201 | && (arg = TREE_OPERAND (arg, 0)) | |
5202 | && TREE_CODE (arg) == STRING_CST) | |
5203 | /* Finally, we have the class name. */ | |
5204 | return get_identifier (TREE_STRING_POINTER (arg)); | |
5205 | } | |
5206 | return 0; | |
264fa2db ZL |
5207 | } |
5208 | ||
d764a8e6 IS |
5209 | /* If we are currently building a message expr, this holds |
5210 | the identifier of the selector of the message. This is | |
5211 | used when printing warnings about argument mismatches. */ | |
264fa2db | 5212 | |
d764a8e6 IS |
5213 | static tree current_objc_message_selector = 0; |
5214 | ||
5215 | tree | |
5216 | objc_message_selector (void) | |
093c7153 | 5217 | { |
d764a8e6 | 5218 | return current_objc_message_selector; |
093c7153 | 5219 | } |
264fa2db | 5220 | |
d764a8e6 IS |
5221 | /* Construct an expression for sending a message. |
5222 | MESS has the object to send to in TREE_PURPOSE | |
5223 | and the argument list (including selector) in TREE_VALUE. | |
5224 | ||
5225 | (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...); | |
5226 | (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */ | |
264fa2db | 5227 | |
38b9c8c3 | 5228 | tree |
eb345401 | 5229 | objc_build_message_expr (tree receiver, tree message_args) |
093c7153 | 5230 | { |
d764a8e6 IS |
5231 | tree sel_name; |
5232 | #ifdef OBJCPLUS | |
eb345401 | 5233 | tree args = TREE_PURPOSE (message_args); |
d764a8e6 | 5234 | #else |
eb345401 | 5235 | tree args = message_args; |
d764a8e6 IS |
5236 | #endif |
5237 | tree method_params = NULL_TREE; | |
093c7153 | 5238 | |
d764a8e6 IS |
5239 | if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK) |
5240 | return error_mark_node; | |
264fa2db | 5241 | |
d764a8e6 IS |
5242 | /* Obtain the full selector name. */ |
5243 | switch (TREE_CODE (args)) | |
093c7153 | 5244 | { |
d764a8e6 IS |
5245 | case IDENTIFIER_NODE: |
5246 | /* A unary selector. */ | |
5247 | sel_name = args; | |
5248 | break; | |
5249 | case TREE_LIST: | |
5250 | sel_name = build_keyword_selector (args); | |
5251 | break; | |
5252 | default: | |
5253 | gcc_unreachable (); | |
093c7153 | 5254 | } |
d764a8e6 IS |
5255 | |
5256 | /* Build the parameter list to give to the method. */ | |
5257 | if (TREE_CODE (args) == TREE_LIST) | |
5258 | #ifdef OBJCPLUS | |
eb345401 | 5259 | method_params = chainon (args, TREE_VALUE (message_args)); |
d764a8e6 | 5260 | #else |
093c7153 | 5261 | { |
d764a8e6 IS |
5262 | tree chain = args, prev = NULL_TREE; |
5263 | ||
5264 | /* We have a keyword selector--check for comma expressions. */ | |
5265 | while (chain) | |
093c7153 | 5266 | { |
d764a8e6 IS |
5267 | tree element = TREE_VALUE (chain); |
5268 | ||
5269 | /* We have a comma expression, must collapse... */ | |
5270 | if (TREE_CODE (element) == TREE_LIST) | |
5271 | { | |
5272 | if (prev) | |
5273 | TREE_CHAIN (prev) = element; | |
5274 | else | |
5275 | args = element; | |
5276 | } | |
5277 | prev = chain; | |
5278 | chain = TREE_CHAIN (chain); | |
5279 | } | |
5280 | method_params = args; | |
093c7153 | 5281 | } |
d764a8e6 | 5282 | #endif |
093c7153 | 5283 | |
d764a8e6 IS |
5284 | #ifdef OBJCPLUS |
5285 | if (processing_template_decl) | |
5286 | /* Must wait until template instantiation time. */ | |
d7faa110 PC |
5287 | return build_min_nt_loc (UNKNOWN_LOCATION, MESSAGE_SEND_EXPR, receiver, |
5288 | sel_name, method_params); | |
d764a8e6 IS |
5289 | #endif |
5290 | ||
5291 | return objc_finish_message_expr (receiver, sel_name, method_params, NULL); | |
264fa2db ZL |
5292 | } |
5293 | ||
d764a8e6 IS |
5294 | /* Look up method SEL_NAME that would be suitable for receiver |
5295 | of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is | |
5296 | nonzero), and report on any duplicates. */ | |
506e2710 | 5297 | |
d764a8e6 IS |
5298 | static tree |
5299 | lookup_method_in_hash_lists (tree sel_name, int is_class) | |
5300 | { | |
3cc2dd4b | 5301 | tree method_prototype = OBJC_MAP_NOT_FOUND; |
264fa2db | 5302 | |
d764a8e6 | 5303 | if (!is_class) |
3cc2dd4b NP |
5304 | method_prototype = objc_map_get (instance_method_map, sel_name); |
5305 | ||
5306 | if (method_prototype == OBJC_MAP_NOT_FOUND) | |
6347cf31 | 5307 | { |
3cc2dd4b | 5308 | method_prototype = objc_map_get (class_method_map, sel_name); |
d764a8e6 | 5309 | is_class = 1; |
3cc2dd4b NP |
5310 | |
5311 | if (method_prototype == OBJC_MAP_NOT_FOUND) | |
5312 | return NULL_TREE; | |
6347cf31 | 5313 | } |
093c7153 | 5314 | |
d764a8e6 | 5315 | return check_duplicates (method_prototype, 1, is_class); |
264fa2db ZL |
5316 | } |
5317 | ||
d764a8e6 IS |
5318 | /* The 'objc_finish_message_expr' routine is called from within |
5319 | 'objc_build_message_expr' for non-template functions. In the case of | |
5320 | C++ template functions, it is called from 'build_expr_from_tree' | |
5321 | (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded. | |
5322 | ||
5323 | If the DEPRECATED_METHOD_PROTOTYPE argument is NULL, then we warn | |
5324 | if the method being used is deprecated. If it is not NULL, instead | |
5325 | of deprecating, we set *DEPRECATED_METHOD_PROTOTYPE to the method | |
5326 | prototype that was used and is deprecated. This is useful for | |
5327 | getter calls that are always generated when compiling dot-syntax | |
5328 | expressions, even if they may not be used. In that case, we don't | |
5329 | want the warning immediately; we produce it (if needed) at gimplify | |
5330 | stage when we are sure that the deprecated getter is being | |
5331 | used. */ | |
38b9c8c3 | 5332 | tree |
d764a8e6 IS |
5333 | objc_finish_message_expr (tree receiver, tree sel_name, tree method_params, |
5334 | tree *deprecated_method_prototype) | |
37153b1e | 5335 | { |
d764a8e6 IS |
5336 | tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype; |
5337 | tree retval, class_tree; | |
5338 | int self, super, have_cast; | |
37153b1e | 5339 | |
d764a8e6 IS |
5340 | /* We have used the receiver, so mark it as read. */ |
5341 | mark_exp_read (receiver); | |
5342 | ||
5343 | /* Extract the receiver of the message, as well as its type | |
5344 | (where the latter may take the form of a cast or be inferred | |
5345 | from the implementation context). */ | |
5346 | rtype = receiver; | |
5347 | while (TREE_CODE (rtype) == COMPOUND_EXPR | |
bf79cedb NP |
5348 | || TREE_CODE (rtype) == MODIFY_EXPR |
5349 | || CONVERT_EXPR_P (rtype) | |
5350 | || TREE_CODE (rtype) == COMPONENT_REF) | |
d764a8e6 IS |
5351 | rtype = TREE_OPERAND (rtype, 0); |
5352 | ||
bf79cedb | 5353 | /* self is 1 if this is a message to self, 0 otherwise */ |
d764a8e6 | 5354 | self = (rtype == self_decl); |
bf79cedb NP |
5355 | |
5356 | /* super is 1 if this is a message to super, 0 otherwise. */ | |
d764a8e6 | 5357 | super = (rtype == UOBJC_SUPER_decl); |
bf79cedb NP |
5358 | |
5359 | /* rtype is the type of the receiver. */ | |
d764a8e6 IS |
5360 | rtype = TREE_TYPE (receiver); |
5361 | ||
bf79cedb | 5362 | /* have_cast is 1 if the receiver is casted. */ |
d764a8e6 IS |
5363 | have_cast = (TREE_CODE (receiver) == NOP_EXPR |
5364 | || (TREE_CODE (receiver) == COMPOUND_EXPR | |
5365 | && !IS_SUPER (rtype))); | |
5366 | ||
5367 | /* If we are calling [super dealloc], reset our warning flag. */ | |
5368 | if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name))) | |
5369 | should_call_super_dealloc = 0; | |
5370 | ||
5371 | /* If the receiver is a class object, retrieve the corresponding | |
bf79cedb NP |
5372 | @interface, if one exists. class_tree is the class name |
5373 | identifier, or NULL_TREE if this is not a class method or the | |
5374 | class name could not be determined (as in the case "Class c; [c | |
5375 | method];"). */ | |
d764a8e6 IS |
5376 | class_tree = receiver_is_class_object (receiver, self, super); |
5377 | ||
5378 | /* Now determine the receiver type (if an explicit cast has not been | |
5379 | provided). */ | |
5380 | if (!have_cast) | |
37153b1e | 5381 | { |
d764a8e6 | 5382 | if (class_tree) |
bf79cedb NP |
5383 | { |
5384 | /* We are here when we have no cast, and we have a class | |
5385 | name. So, this is a plain method to a class object, as | |
5386 | in [NSObject alloc]. Find the interface corresponding to | |
5387 | the class name. */ | |
5388 | rtype = lookup_interface (class_tree); | |
5389 | ||
5390 | if (rtype == NULL_TREE) | |
5391 | { | |
5392 | /* If 'rtype' is NULL_TREE at this point it means that | |
5393 | we have seen no @interface corresponding to that | |
419b55d0 NP |
5394 | class name, only a @class declaration (alternatively, |
5395 | this was a call such as [objc_getClass("SomeClass") | |
5396 | alloc], where we've never seen the @interface of | |
5397 | SomeClass). So, we have a class name (class_tree) | |
5398 | but no actual details of the class methods. We won't | |
5399 | be able to check that the class responds to the | |
5400 | method, and we will have to guess the method | |
5401 | prototype. Emit a warning, then keep going (this | |
5402 | will use any method with a matching name, as if the | |
5403 | receiver was of type 'Class'). */ | |
bf79cedb NP |
5404 | warning (0, "@interface of class %qE not found", class_tree); |
5405 | } | |
5406 | } | |
d764a8e6 IS |
5407 | /* Handle `self' and `super'. */ |
5408 | else if (super) | |
37153b1e | 5409 | { |
d764a8e6 IS |
5410 | if (!CLASS_SUPER_NAME (implementation_template)) |
5411 | { | |
5412 | error ("no super class declared in @interface for %qE", | |
5413 | CLASS_NAME (implementation_template)); | |
5414 | return error_mark_node; | |
5415 | } | |
5416 | rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template)); | |
37153b1e | 5417 | } |
d764a8e6 IS |
5418 | else if (self) |
5419 | rtype = lookup_interface (CLASS_NAME (implementation_template)); | |
37153b1e | 5420 | } |
d764a8e6 | 5421 | |
d764a8e6 | 5422 | if (objc_is_id (rtype)) |
37153b1e | 5423 | { |
bf79cedb NP |
5424 | /* The receiver is of type 'id' or 'Class' (with or without some |
5425 | protocols attached to it). */ | |
5426 | ||
5427 | /* We set class_tree to the identifier for 'Class' if this is a | |
5428 | class method, and to NULL_TREE if not. */ | |
d764a8e6 | 5429 | class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE); |
bf79cedb NP |
5430 | |
5431 | /* 'rprotos' is the list of protocols that the receiver | |
5432 | supports. */ | |
d764a8e6 IS |
5433 | rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype)) |
5434 | ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype)) | |
5435 | : NULL_TREE); | |
bf79cedb NP |
5436 | |
5437 | /* We have no information on the type, and we set it to | |
5438 | NULL_TREE. */ | |
d764a8e6 IS |
5439 | rtype = NULL_TREE; |
5440 | ||
bf79cedb NP |
5441 | /* If there are any protocols, check that the method we are |
5442 | calling appears in the protocol list. If there are no | |
5443 | protocols, this is a message to 'id' or 'Class' and we accept | |
5444 | any method that exists. */ | |
d764a8e6 IS |
5445 | if (rprotos) |
5446 | { | |
bf79cedb NP |
5447 | /* If messaging 'id <Protos>' or 'Class <Proto>', first |
5448 | search in protocols themselves for the method | |
5449 | prototype. */ | |
d764a8e6 IS |
5450 | method_prototype |
5451 | = lookup_method_in_protocol_list (rprotos, sel_name, | |
5452 | class_tree != NULL_TREE); | |
5453 | ||
bf79cedb NP |
5454 | /* If messaging 'Class <Proto>' but did not find a class |
5455 | method prototype, search for an instance method instead, | |
5456 | and warn about having done so. */ | |
d764a8e6 IS |
5457 | if (!method_prototype && !rtype && class_tree != NULL_TREE) |
5458 | { | |
5459 | method_prototype | |
5460 | = lookup_method_in_protocol_list (rprotos, sel_name, 0); | |
5461 | ||
5462 | if (method_prototype) | |
5463 | warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)", | |
5464 | sel_name, sel_name); | |
5465 | } | |
5466 | } | |
37153b1e | 5467 | } |
d764a8e6 | 5468 | else if (rtype) |
37153b1e | 5469 | { |
bf79cedb NP |
5470 | /* We have a receiver type which is more specific than 'id' or |
5471 | 'Class'. */ | |
d764a8e6 | 5472 | tree orig_rtype = rtype; |
37153b1e | 5473 | |
d764a8e6 IS |
5474 | if (TREE_CODE (rtype) == POINTER_TYPE) |
5475 | rtype = TREE_TYPE (rtype); | |
5476 | /* Traverse typedef aliases */ | |
5477 | while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype) | |
5478 | && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL | |
5479 | && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype))) | |
5480 | rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)); | |
5481 | if (TYPED_OBJECT (rtype)) | |
5482 | { | |
5483 | rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype); | |
5484 | rtype = TYPE_OBJC_INTERFACE (rtype); | |
5485 | } | |
d764a8e6 IS |
5486 | if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE) |
5487 | { | |
bf79cedb NP |
5488 | /* If we could not find an @interface declaration, we must |
5489 | have only seen a @class declaration; so, we cannot say | |
5490 | anything more intelligent about which methods the | |
5491 | receiver will understand. Note that this only happens | |
5492 | for instance methods; for class methods to a class where | |
5493 | we have only seen a @class declaration, | |
5494 | lookup_interface() above would have set rtype to | |
5495 | NULL_TREE. */ | |
5496 | if (rprotos) | |
5497 | { | |
5498 | /* We could not find an @interface declaration, yet, if | |
5499 | there are protocols attached to the type, we can | |
5500 | still look up the method in the protocols. Ie, we | |
5501 | are in the following case: | |
5502 | ||
5503 | @class MyClass; | |
5504 | MyClass<MyProtocol> *x; | |
5505 | [x method]; | |
5506 | ||
5507 | If 'MyProtocol' has the method 'method', we can check | |
5508 | and retrieve the method prototype. */ | |
5509 | method_prototype | |
5510 | = lookup_method_in_protocol_list (rprotos, sel_name, 0); | |
5511 | ||
5512 | /* At this point, if we have found the method_prototype, | |
5513 | we are quite happy. The details of the class are | |
5514 | irrelevant. If we haven't found it, a warning will | |
5515 | have been produced that the method could not be found | |
5516 | in the protocol, and we won't produce further | |
5517 | warnings (please note that this means that "@class | |
5518 | MyClass; MyClass <MyProtocol> *x;" is exactly | |
5519 | equivalent to "id <MyProtocol> x", which isn't too | |
5520 | satisfactory but it's not easy to see how to do | |
5521 | better). */ | |
5522 | } | |
5523 | else | |
5524 | { | |
5525 | if (rtype) | |
5526 | { | |
5527 | /* We could not find an @interface declaration, and | |
5528 | there are no protocols attached to the receiver, | |
5529 | so we can't complete the check that the receiver | |
5530 | responds to the method, and we can't retrieve the | |
5531 | method prototype. But, because the receiver has | |
5532 | a well-specified class, the programmer did want | |
5533 | this check to be performed. Emit a warning, then | |
5534 | keep going as if it was an 'id'. To remove the | |
5535 | warning, either include an @interface for the | |
5536 | class, or cast the receiver to 'id'. Note that | |
5537 | rtype is an IDENTIFIER_NODE at this point. */ | |
5538 | warning (0, "@interface of class %qE not found", rtype); | |
5539 | } | |
5540 | } | |
5541 | ||
d764a8e6 | 5542 | rtype = NULL_TREE; |
d764a8e6 IS |
5543 | } |
5544 | else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE | |
5545 | || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE) | |
5546 | { | |
bf79cedb NP |
5547 | /* We have a valid ObjC class name with an associated |
5548 | @interface. Look up the method name in the published | |
5549 | @interface for the class (and its superclasses). */ | |
d764a8e6 IS |
5550 | method_prototype |
5551 | = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE); | |
37153b1e | 5552 | |
d764a8e6 IS |
5553 | /* If the method was not found in the @interface, it may still |
5554 | exist locally as part of the @implementation. */ | |
5555 | if (!method_prototype && objc_implementation_context | |
5556 | && CLASS_NAME (objc_implementation_context) | |
5557 | == OBJC_TYPE_NAME (rtype)) | |
5558 | method_prototype | |
5559 | = lookup_method | |
5560 | ((class_tree | |
5561 | ? CLASS_CLS_METHODS (objc_implementation_context) | |
5562 | : CLASS_NST_METHODS (objc_implementation_context)), | |
5563 | sel_name); | |
37153b1e | 5564 | |
d764a8e6 IS |
5565 | /* If we haven't found a candidate method by now, try looking for |
5566 | it in the protocol list. */ | |
5567 | if (!method_prototype && rprotos) | |
5568 | method_prototype | |
5569 | = lookup_method_in_protocol_list (rprotos, sel_name, | |
5570 | class_tree != NULL_TREE); | |
5571 | } | |
5572 | else | |
5573 | { | |
bf79cedb | 5574 | /* We have a type, but it's not an Objective-C type (!). */ |
d764a8e6 IS |
5575 | warning (0, "invalid receiver type %qs", |
5576 | identifier_to_locale (gen_type_name (orig_rtype))); | |
5577 | /* After issuing the "invalid receiver" warning, perform method | |
5578 | lookup as if we were messaging 'id'. */ | |
5579 | rtype = rprotos = NULL_TREE; | |
5580 | } | |
37153b1e | 5581 | } |
bf79cedb NP |
5582 | /* Note that rtype could also be NULL_TREE. This happens if we are |
5583 | messaging a class by name, but the class was only | |
5584 | forward-declared using @class. */ | |
264fa2db | 5585 | |
bf79cedb NP |
5586 | /* For 'id' or 'Class' receivers, search in the global hash table as |
5587 | a last resort. For all receivers, warn if protocol searches have | |
5588 | failed. */ | |
d764a8e6 IS |
5589 | if (!method_prototype) |
5590 | { | |
5591 | if (rprotos) | |
5592 | warning (0, "%<%c%E%> not found in protocol(s)", | |
5593 | (class_tree ? '+' : '-'), | |
5594 | sel_name); | |
264fa2db | 5595 | |
d764a8e6 IS |
5596 | if (!rtype) |
5597 | method_prototype | |
5598 | = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE); | |
5599 | } | |
264fa2db | 5600 | |
944fb799 | 5601 | if (!method_prototype) |
d764a8e6 IS |
5602 | { |
5603 | static bool warn_missing_methods = false; | |
264fa2db | 5604 | |
d764a8e6 IS |
5605 | if (rtype) |
5606 | warning (0, "%qE may not respond to %<%c%E%>", | |
5607 | OBJC_TYPE_NAME (rtype), | |
5608 | (class_tree ? '+' : '-'), | |
5609 | sel_name); | |
5610 | /* If we are messaging an 'id' or 'Class' object and made it here, | |
5611 | then we have failed to find _any_ instance or class method, | |
5612 | respectively. */ | |
5613 | else | |
5614 | warning (0, "no %<%c%E%> method found", | |
5615 | (class_tree ? '+' : '-'), | |
5616 | sel_name); | |
38b9c8c3 | 5617 | |
d764a8e6 IS |
5618 | if (!warn_missing_methods) |
5619 | { | |
944fb799 | 5620 | warning_at (input_location, |
d764a8e6 | 5621 | 0, "(Messages without a matching method signature"); |
944fb799 | 5622 | warning_at (input_location, |
d764a8e6 | 5623 | 0, "will be assumed to return %<id%> and accept"); |
944fb799 | 5624 | warning_at (input_location, |
d764a8e6 IS |
5625 | 0, "%<...%> as arguments.)"); |
5626 | warn_missing_methods = true; | |
5627 | } | |
5628 | } | |
5629 | else | |
5630 | { | |
5631 | /* Warn if the method is deprecated, but not if the receiver is | |
5632 | a generic 'id'. 'id' is used to cast an object to a generic | |
5633 | object of an unspecified class; in that case, we'll use | |
5634 | whatever method prototype we can find to get the method | |
5635 | argument and return types, but it is not appropriate to | |
5636 | produce deprecation warnings since we don't know the class | |
5637 | that the object will be of at runtime. The @interface(s) for | |
5638 | that class may not even be available to the compiler right | |
5639 | now, and it is perfectly possible that the method is marked | |
5640 | as non-deprecated in such @interface(s). | |
e31c7eec | 5641 | |
d764a8e6 IS |
5642 | In practice this makes sense since casting an object to 'id' |
5643 | is often used precisely to turn off warnings associated with | |
5644 | the object being of a particular class. */ | |
5645 | if (TREE_DEPRECATED (method_prototype) && rtype != NULL_TREE) | |
5646 | { | |
5647 | if (deprecated_method_prototype) | |
5648 | *deprecated_method_prototype = method_prototype; | |
5649 | else | |
5650 | warn_deprecated_use (method_prototype, NULL_TREE); | |
5651 | } | |
5652 | } | |
093c7153 | 5653 | |
d764a8e6 IS |
5654 | /* Save the selector name for printing error messages. */ |
5655 | current_objc_message_selector = sel_name; | |
093c7153 | 5656 | |
d764a8e6 | 5657 | /* Build the method call. |
944fb799 | 5658 | TODO: Get the location from somewhere that will work for delayed |
d764a8e6 | 5659 | expansion. */ |
944fb799 | 5660 | |
d764a8e6 IS |
5661 | retval = (*runtime.build_objc_method_call) (input_location, method_prototype, |
5662 | receiver, rtype, sel_name, | |
5663 | method_params, super); | |
093c7153 | 5664 | |
d764a8e6 | 5665 | current_objc_message_selector = 0; |
093c7153 | 5666 | |
d764a8e6 IS |
5667 | return retval; |
5668 | } | |
5669 | \f | |
e31c7eec | 5670 | |
944fb799 | 5671 | /* This routine creates a static variable used to implement @protocol(MyProtocol) |
d764a8e6 IS |
5672 | expression. This variable will be initialized to global protocol_t meta-data |
5673 | pointer. */ | |
e31c7eec | 5674 | |
d764a8e6 IS |
5675 | /* This function is called by the parser when (and only when) a |
5676 | @protocol() expression is found, in order to compile it. */ | |
5677 | tree | |
5678 | objc_build_protocol_expr (tree protoname) | |
5679 | { | |
5680 | tree p = lookup_protocol (protoname, /* warn if deprecated */ true, | |
5681 | /* definition_required */ false); | |
d4e72c58 | 5682 | |
d764a8e6 IS |
5683 | if (!p) |
5684 | { | |
5685 | error ("cannot find protocol declaration for %qE", protoname); | |
5686 | return error_mark_node; | |
e31c7eec | 5687 | } |
e31c7eec | 5688 | |
d764a8e6 IS |
5689 | return (*runtime.get_protocol_reference) (input_location, p); |
5690 | } | |
e31c7eec | 5691 | |
d764a8e6 IS |
5692 | /* This function is called by the parser when a @selector() expression |
5693 | is found, in order to compile it. It is only called by the parser | |
5694 | and only to compile a @selector(). LOC is the location of the | |
5695 | @selector. */ | |
5696 | tree | |
5697 | objc_build_selector_expr (location_t loc, tree selnamelist) | |
e31c7eec | 5698 | { |
d764a8e6 | 5699 | tree selname; |
e31c7eec | 5700 | |
d764a8e6 IS |
5701 | /* Obtain the full selector name. */ |
5702 | switch (TREE_CODE (selnamelist)) | |
5703 | { | |
5704 | case IDENTIFIER_NODE: | |
5705 | /* A unary selector. */ | |
5706 | selname = selnamelist; | |
5707 | break; | |
5708 | case TREE_LIST: | |
5709 | selname = build_keyword_selector (selnamelist); | |
5710 | break; | |
5711 | default: | |
5712 | gcc_unreachable (); | |
5713 | } | |
e31c7eec | 5714 | |
d764a8e6 IS |
5715 | /* If we are required to check @selector() expressions as they |
5716 | are found, check that the selector has been declared. */ | |
5717 | if (warn_undeclared_selector) | |
5718 | { | |
5719 | /* Look the selector up in the list of all known class and | |
5720 | instance methods (up to this line) to check that the selector | |
5721 | exists. */ | |
3cc2dd4b | 5722 | tree method; |
e31c7eec | 5723 | |
d764a8e6 | 5724 | /* First try with instance methods. */ |
3cc2dd4b | 5725 | method = objc_map_get (instance_method_map, selname); |
e31c7eec | 5726 | |
d764a8e6 | 5727 | /* If not found, try with class methods. */ |
3cc2dd4b | 5728 | if (method == OBJC_MAP_NOT_FOUND) |
d764a8e6 | 5729 | { |
3cc2dd4b | 5730 | method = objc_map_get (class_method_map, selname); |
e31c7eec | 5731 | |
3cc2dd4b NP |
5732 | /* If still not found, print out a warning. */ |
5733 | if (method == OBJC_MAP_NOT_FOUND) | |
5734 | warning (0, "undeclared selector %qE", selname); | |
d764a8e6 IS |
5735 | } |
5736 | } | |
e31c7eec | 5737 | |
d764a8e6 IS |
5738 | /* The runtimes do this differently, most particularly, GNU has typed |
5739 | selectors, whilst NeXT does not. */ | |
5740 | return (*runtime.build_selector_reference) (loc, selname, NULL_TREE); | |
e31c7eec TW |
5741 | } |
5742 | ||
d764a8e6 IS |
5743 | static tree |
5744 | build_ivar_reference (tree id) | |
5745 | { | |
5746 | tree base; | |
5747 | if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL) | |
5748 | { | |
5749 | /* Historically, a class method that produced objects (factory | |
5750 | method) would assign `self' to the instance that it | |
5751 | allocated. This would effectively turn the class method into | |
5752 | an instance method. Following this assignment, the instance | |
5753 | variables could be accessed. That practice, while safe, | |
5754 | violates the simple rule that a class method should not refer | |
5755 | to an instance variable. It's better to catch the cases | |
5756 | where this is done unknowingly than to support the above | |
5757 | paradigm. */ | |
5758 | warning (0, "instance variable %qE accessed in class method", | |
5759 | id); | |
5760 | self_decl = convert (objc_instance_type, self_decl); /* cast */ | |
e31c7eec | 5761 | } |
e31c7eec | 5762 | |
d764a8e6 IS |
5763 | base = build_indirect_ref (input_location, self_decl, RO_ARROW); |
5764 | return (*runtime.build_ivar_reference) (input_location, base, id); | |
e31c7eec TW |
5765 | } |
5766 | ||
d764a8e6 IS |
5767 | static void |
5768 | hash_init (void) | |
5769 | { | |
3cc2dd4b NP |
5770 | instance_method_map = objc_map_alloc_ggc (1000); |
5771 | class_method_map = objc_map_alloc_ggc (1000); | |
e31c7eec | 5772 | |
3cc2dd4b NP |
5773 | class_name_map = objc_map_alloc_ggc (200); |
5774 | alias_name_map = objc_map_alloc_ggc (200); | |
e31c7eec | 5775 | |
d764a8e6 IS |
5776 | /* Initialize the hash table used to hold the constant string objects. */ |
5777 | string_htab = htab_create_ggc (31, string_hash, | |
5778 | string_eq, NULL); | |
e31c7eec TW |
5779 | } |
5780 | ||
3cc2dd4b NP |
5781 | /* Use the following to add a method to class_method_map or |
5782 | instance_method_map. It will add the method, keyed by the | |
5783 | METHOD_SEL_NAME. If the method already exists, but with one or | |
5784 | more different prototypes, it will store a TREE_VEC in the map, | |
5785 | with the method prototypes in the vector. */ | |
d764a8e6 | 5786 | static void |
3cc2dd4b | 5787 | insert_method_into_method_map (bool class_method, tree method) |
d764a8e6 | 5788 | { |
3cc2dd4b NP |
5789 | tree method_name = METHOD_SEL_NAME (method); |
5790 | tree existing_entry; | |
5791 | objc_map_t map; | |
e31c7eec | 5792 | |
3cc2dd4b NP |
5793 | if (class_method) |
5794 | map = class_method_map; | |
d764a8e6 | 5795 | else |
3cc2dd4b | 5796 | map = instance_method_map; |
e31c7eec | 5797 | |
3cc2dd4b NP |
5798 | /* Check if the method already exists in the map. */ |
5799 | existing_entry = objc_map_get (map, method_name); | |
d764a8e6 | 5800 | |
3cc2dd4b NP |
5801 | /* If not, we simply add it to the map. */ |
5802 | if (existing_entry == OBJC_MAP_NOT_FOUND) | |
5803 | objc_map_put (map, method_name, method); | |
5804 | else | |
d764a8e6 | 5805 | { |
3cc2dd4b NP |
5806 | tree new_entry; |
5807 | ||
5808 | /* If an entry already exists, it's more complicated. We'll | |
5809 | have to check whether the method prototype is the same or | |
5810 | not. */ | |
5811 | if (TREE_CODE (existing_entry) != TREE_VEC) | |
5812 | { | |
5813 | /* If the method prototypes are the same, there is nothing | |
5814 | to do. */ | |
5815 | if (comp_proto_with_proto (method, existing_entry, 1)) | |
5816 | return; | |
d764a8e6 | 5817 | |
3cc2dd4b NP |
5818 | /* If not, create a vector to store both the method already |
5819 | in the map, and the new one that we are adding. */ | |
5820 | new_entry = make_tree_vec (2); | |
5821 | ||
5822 | TREE_VEC_ELT (new_entry, 0) = existing_entry; | |
5823 | TREE_VEC_ELT (new_entry, 1) = method; | |
5824 | } | |
5825 | else | |
5826 | { | |
5827 | /* An entry already exists, and it's already a vector. This | |
5828 | means that at least 2 different method prototypes were | |
5829 | already found, and we're considering registering yet | |
5830 | another one. */ | |
5831 | size_t i; | |
5832 | ||
5833 | /* Check all the existing prototypes. If any matches the | |
5834 | one we need to add, there is nothing to do because it's | |
5835 | already there. */ | |
5b6b2942 | 5836 | for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++) |
3cc2dd4b NP |
5837 | if (comp_proto_with_proto (method, TREE_VEC_ELT (existing_entry, i), 1)) |
5838 | return; | |
d764a8e6 | 5839 | |
3cc2dd4b NP |
5840 | /* Else, create a new, bigger vector and add the new method |
5841 | at the end of it. This is inefficient but extremely | |
5842 | rare; in any sane program most methods have a single | |
5843 | prototype, and very few, if any, will have more than | |
5844 | 2! */ | |
5845 | new_entry = make_tree_vec (TREE_VEC_LENGTH (existing_entry) + 1); | |
5846 | ||
5847 | /* Copy the methods from the existing vector. */ | |
5b6b2942 | 5848 | for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++) |
3cc2dd4b NP |
5849 | TREE_VEC_ELT (new_entry, i) = TREE_VEC_ELT (existing_entry, i); |
5850 | ||
5851 | /* Add the new method at the end. */ | |
5852 | TREE_VEC_ELT (new_entry, i) = method; | |
5853 | } | |
e31c7eec | 5854 | |
3cc2dd4b NP |
5855 | /* Store the new vector in the map. */ |
5856 | objc_map_put (map, method_name, new_entry); | |
e31c7eec | 5857 | } |
d764a8e6 | 5858 | } |
e31c7eec | 5859 | |
d764a8e6 IS |
5860 | \f |
5861 | static tree | |
5862 | lookup_method (tree mchain, tree method) | |
5863 | { | |
5864 | tree key; | |
e31c7eec | 5865 | |
d764a8e6 IS |
5866 | if (TREE_CODE (method) == IDENTIFIER_NODE) |
5867 | key = method; | |
5868 | else | |
5869 | key = METHOD_SEL_NAME (method); | |
e31c7eec | 5870 | |
d764a8e6 IS |
5871 | while (mchain) |
5872 | { | |
5873 | if (METHOD_SEL_NAME (mchain) == key) | |
5874 | return mchain; | |
011d50d9 | 5875 | |
d764a8e6 | 5876 | mchain = DECL_CHAIN (mchain); |
e31c7eec | 5877 | } |
d764a8e6 | 5878 | return NULL_TREE; |
e31c7eec TW |
5879 | } |
5880 | ||
d764a8e6 IS |
5881 | /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance |
5882 | method in INTERFACE, along with any categories and protocols | |
5883 | attached thereto. If method is not found, and the | |
5884 | OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS, recursively examine the | |
5885 | INTERFACE's superclass. If OBJC_LOOKUP_CLASS is set, | |
5886 | OBJC_LOOKUP_NO_SUPER is clear, and no suitable class method could | |
5887 | be found in INTERFACE or any of its superclasses, look for an | |
5888 | _instance_ method of the same name in the root class as a last | |
5889 | resort. This behaviour can be turned off by using | |
5890 | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS. | |
e31c7eec | 5891 | |
d764a8e6 | 5892 | If a suitable method cannot be found, return NULL_TREE. */ |
e31c7eec | 5893 | |
d764a8e6 IS |
5894 | static tree |
5895 | lookup_method_static (tree interface, tree ident, int flags) | |
5896 | { | |
5897 | tree meth = NULL_TREE, root_inter = NULL_TREE; | |
5898 | tree inter = interface; | |
5899 | int is_class = (flags & OBJC_LOOKUP_CLASS); | |
5900 | int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER); | |
5901 | int no_instance_methods_of_root_class = (flags & OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS); | |
e31c7eec | 5902 | |
d764a8e6 IS |
5903 | while (inter) |
5904 | { | |
5905 | tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter); | |
5906 | tree category = inter; | |
e31c7eec | 5907 | |
d764a8e6 IS |
5908 | /* First, look up the method in the class itself. */ |
5909 | if ((meth = lookup_method (chain, ident))) | |
5910 | return meth; | |
e31c7eec | 5911 | |
d764a8e6 IS |
5912 | /* Failing that, look for the method in each category of the class. */ |
5913 | while ((category = CLASS_CATEGORY_LIST (category))) | |
5914 | { | |
5915 | chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category); | |
e31c7eec | 5916 | |
d764a8e6 IS |
5917 | /* Check directly in each category. */ |
5918 | if ((meth = lookup_method (chain, ident))) | |
5919 | return meth; | |
e31c7eec | 5920 | |
d764a8e6 IS |
5921 | /* Failing that, check in each category's protocols. */ |
5922 | if (CLASS_PROTOCOL_LIST (category)) | |
5923 | { | |
5924 | if ((meth = (lookup_method_in_protocol_list | |
5925 | (CLASS_PROTOCOL_LIST (category), ident, is_class)))) | |
5926 | return meth; | |
5927 | } | |
5928 | } | |
e31c7eec | 5929 | |
d764a8e6 IS |
5930 | /* If not found in categories, check in protocols of the main class. */ |
5931 | if (CLASS_PROTOCOL_LIST (inter)) | |
5932 | { | |
5933 | if ((meth = (lookup_method_in_protocol_list | |
5934 | (CLASS_PROTOCOL_LIST (inter), ident, is_class)))) | |
5935 | return meth; | |
5936 | } | |
e31c7eec | 5937 | |
d764a8e6 IS |
5938 | /* If we were instructed not to look in superclasses, don't. */ |
5939 | if (no_superclasses) | |
5940 | return NULL_TREE; | |
89ef1046 | 5941 | |
d764a8e6 IS |
5942 | /* Failing that, climb up the inheritance hierarchy. */ |
5943 | root_inter = inter; | |
5944 | inter = lookup_interface (CLASS_SUPER_NAME (inter)); | |
e31c7eec | 5945 | } |
d764a8e6 | 5946 | while (inter); |
e31c7eec | 5947 | |
d764a8e6 | 5948 | if (is_class && !no_instance_methods_of_root_class) |
e31c7eec | 5949 | { |
d764a8e6 IS |
5950 | /* If no class (factory) method was found, check if an _instance_ |
5951 | method of the same name exists in the root class. This is what | |
5952 | the Objective-C runtime will do. */ | |
5953 | return lookup_method_static (root_inter, ident, 0); | |
e31c7eec TW |
5954 | } |
5955 | else | |
d764a8e6 | 5956 | { |
944fb799 | 5957 | /* If an instance method was not found, return 0. */ |
d764a8e6 IS |
5958 | return NULL_TREE; |
5959 | } | |
e31c7eec TW |
5960 | } |
5961 | ||
d764a8e6 IS |
5962 | static tree |
5963 | objc_add_method (tree klass, tree method, int is_class, bool is_optional) | |
38b9c8c3 | 5964 | { |
d764a8e6 | 5965 | tree existing_method = NULL_TREE; |
38b9c8c3 | 5966 | |
d764a8e6 IS |
5967 | /* The first thing we do is look up the method in the list of |
5968 | methods already defined in the interface (or implementation). */ | |
5969 | if (is_class) | |
5970 | existing_method = lookup_method (CLASS_CLS_METHODS (klass), method); | |
5971 | else | |
5972 | existing_method = lookup_method (CLASS_NST_METHODS (klass), method); | |
38b9c8c3 | 5973 | |
d764a8e6 IS |
5974 | /* In the case of protocols, we have a second list of methods to |
5975 | consider, the list of optional ones. */ | |
5976 | if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE) | |
38b9c8c3 | 5977 | { |
d764a8e6 IS |
5978 | /* @required methods are added to the protocol's normal list. |
5979 | @optional methods are added to the protocol's OPTIONAL lists. | |
5980 | Note that adding the methods to the optional lists disables | |
5981 | checking that the methods are implemented by classes | |
5982 | implementing the protocol, since these checks only use the | |
5983 | CLASS_CLS_METHODS and CLASS_NST_METHODS. */ | |
38b9c8c3 | 5984 | |
d764a8e6 IS |
5985 | /* First of all, if the method to add is @optional, and we found |
5986 | it already existing as @required, emit an error. */ | |
5987 | if (is_optional && existing_method) | |
5988 | { | |
5989 | error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time", | |
5990 | (is_class ? '+' : '-'), | |
5991 | METHOD_SEL_NAME (existing_method)); | |
5992 | inform (DECL_SOURCE_LOCATION (existing_method), | |
5993 | "previous declaration of %<%c%E%> as %<@required%>", | |
5994 | (is_class ? '+' : '-'), | |
5995 | METHOD_SEL_NAME (existing_method)); | |
38b9c8c3 | 5996 | } |
38b9c8c3 | 5997 | |
d764a8e6 IS |
5998 | /* Now check the list of @optional methods if we didn't find the |
5999 | method in the @required list. */ | |
6000 | if (!existing_method) | |
38b9c8c3 | 6001 | { |
d764a8e6 IS |
6002 | if (is_class) |
6003 | existing_method = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (klass), method); | |
6004 | else | |
6005 | existing_method = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (klass), method); | |
944fb799 | 6006 | |
d764a8e6 | 6007 | if (!is_optional && existing_method) |
38b9c8c3 | 6008 | { |
d764a8e6 IS |
6009 | error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time", |
6010 | (is_class ? '+' : '-'), | |
6011 | METHOD_SEL_NAME (existing_method)); | |
6012 | inform (DECL_SOURCE_LOCATION (existing_method), | |
6013 | "previous declaration of %<%c%E%> as %<@optional%>", | |
6014 | (is_class ? '+' : '-'), | |
6015 | METHOD_SEL_NAME (existing_method)); | |
38b9c8c3 ZL |
6016 | } |
6017 | } | |
6018 | } | |
6019 | ||
d764a8e6 IS |
6020 | /* If the method didn't exist already, add it. */ |
6021 | if (!existing_method) | |
6c65299b | 6022 | { |
d764a8e6 | 6023 | if (is_optional) |
e31c7eec | 6024 | { |
d764a8e6 IS |
6025 | if (is_class) |
6026 | { | |
6027 | /* Put the method on the list in reverse order. */ | |
6028 | TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass); | |
6029 | PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method; | |
6030 | } | |
6031 | else | |
5d0f30f7 | 6032 | { |
d764a8e6 IS |
6033 | TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass); |
6034 | PROTOCOL_OPTIONAL_NST_METHODS (klass) = method; | |
5d0f30f7 | 6035 | } |
e31c7eec | 6036 | } |
d764a8e6 | 6037 | else |
e31c7eec | 6038 | { |
d764a8e6 IS |
6039 | if (is_class) |
6040 | { | |
6041 | DECL_CHAIN (method) = CLASS_CLS_METHODS (klass); | |
6042 | CLASS_CLS_METHODS (klass) = method; | |
6043 | } | |
6044 | else | |
5d0f30f7 | 6045 | { |
d764a8e6 IS |
6046 | DECL_CHAIN (method) = CLASS_NST_METHODS (klass); |
6047 | CLASS_NST_METHODS (klass) = method; | |
5d0f30f7 | 6048 | } |
e31c7eec | 6049 | } |
6c65299b | 6050 | } |
e31c7eec | 6051 | else |
6c65299b | 6052 | { |
d764a8e6 IS |
6053 | /* The method was already defined. Check that the types match |
6054 | for an @interface for a class or category, or for a | |
6055 | @protocol. Give hard errors on methods with identical | |
6056 | selectors but differing argument and/or return types. We do | |
6057 | not do this for @implementations, because C/C++ will do it | |
6058 | for us (i.e., there will be duplicate function definition | |
6059 | errors). */ | |
6060 | if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE | |
6061 | || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE | |
6062 | /* Starting with GCC 4.6, we emit the same error for | |
6063 | protocols too. The situation is identical to | |
6064 | @interfaces as there is no possible meaningful reason | |
6065 | for defining the same method with different signatures | |
6066 | in the very same @protocol. If that was allowed, | |
6067 | whenever the protocol is used (both at compile and run | |
6068 | time) there wouldn't be any meaningful way to decide | |
6069 | which of the two method signatures should be used. */ | |
6070 | || TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE) | |
6071 | && !comp_proto_with_proto (method, existing_method, 1)) | |
6072 | { | |
6073 | error ("duplicate declaration of method %<%c%E%> with conflicting types", | |
6074 | (is_class ? '+' : '-'), | |
6075 | METHOD_SEL_NAME (existing_method)); | |
6076 | inform (DECL_SOURCE_LOCATION (existing_method), | |
6077 | "previous declaration of %<%c%E%>", | |
6078 | (is_class ? '+' : '-'), | |
6079 | METHOD_SEL_NAME (existing_method)); | |
6080 | } | |
6c65299b | 6081 | } |
51900510 | 6082 | |
d764a8e6 | 6083 | if (is_class) |
3cc2dd4b | 6084 | insert_method_into_method_map (true, method); |
6c65299b RS |
6085 | else |
6086 | { | |
3cc2dd4b | 6087 | insert_method_into_method_map (false, method); |
d764a8e6 IS |
6088 | |
6089 | /* Instance methods in root classes (and categories thereof) | |
6090 | may act as class methods as a last resort. We also add | |
6091 | instance methods listed in @protocol declarations to | |
6092 | the class hash table, on the assumption that @protocols | |
6093 | may be adopted by root classes or categories. */ | |
6094 | if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE | |
6095 | || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE) | |
6096 | klass = lookup_interface (CLASS_NAME (klass)); | |
6097 | ||
6098 | if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE | |
6099 | || !CLASS_SUPER_NAME (klass)) | |
3cc2dd4b | 6100 | insert_method_into_method_map (true, method); |
6c65299b | 6101 | } |
51900510 | 6102 | |
d764a8e6 | 6103 | return method; |
6c65299b RS |
6104 | } |
6105 | ||
5d0f30f7 | 6106 | static void |
d764a8e6 | 6107 | add_category (tree klass, tree category) |
5d0f30f7 | 6108 | { |
d764a8e6 IS |
6109 | /* Put categories on list in reverse order. */ |
6110 | tree cat = lookup_category (klass, CLASS_SUPER_NAME (category)); | |
6c65299b | 6111 | |
d764a8e6 | 6112 | if (cat) |
e31c7eec | 6113 | { |
d764a8e6 IS |
6114 | warning (0, "duplicate interface declaration for category %<%E(%E)%>", |
6115 | CLASS_NAME (klass), | |
6116 | CLASS_SUPER_NAME (category)); | |
e31c7eec TW |
6117 | } |
6118 | else | |
6119 | { | |
d764a8e6 IS |
6120 | CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass); |
6121 | CLASS_CATEGORY_LIST (klass) = category; | |
264fa2db | 6122 | } |
6c65299b RS |
6123 | } |
6124 | ||
4597541a NP |
6125 | #ifndef OBJCPLUS |
6126 | /* A flexible array member is a C99 extension where you can use | |
6127 | "type[]" at the end of a struct to mean a variable-length array. | |
6128 | ||
6129 | In Objective-C, instance variables are fundamentally members of a | |
6130 | struct, but the struct can always be extended by subclassing; hence | |
6131 | we need to detect and forbid all instance variables declared using | |
6132 | flexible array members. | |
6133 | ||
6134 | No check for this is needed in Objective-C++, since C++ does not | |
6135 | have flexible array members. */ | |
6136 | ||
6137 | /* Determine whether TYPE is a structure with a flexible array member, | |
6138 | a union containing such a structure (possibly recursively) or an | |
6139 | array of such structures or unions. These are all invalid as | |
6140 | instance variable. */ | |
6141 | static bool | |
6142 | flexible_array_type_p (tree type) | |
6143 | { | |
6144 | tree x; | |
6145 | switch (TREE_CODE (type)) | |
6146 | { | |
6147 | case RECORD_TYPE: | |
6148 | x = TYPE_FIELDS (type); | |
6149 | if (x == NULL_TREE) | |
6150 | return false; | |
6151 | while (DECL_CHAIN (x) != NULL_TREE) | |
6152 | x = DECL_CHAIN (x); | |
6153 | if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE | |
6154 | && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE | |
6155 | && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE | |
6156 | && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) | |
6157 | return true; | |
6158 | return false; | |
6159 | case UNION_TYPE: | |
6160 | for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x)) | |
6161 | { | |
6162 | if (flexible_array_type_p (TREE_TYPE (x))) | |
6163 | return true; | |
6164 | } | |
6165 | return false; | |
6166 | /* Note that we also check for arrays of something that uses a flexible array member. */ | |
6167 | case ARRAY_TYPE: | |
6168 | if (flexible_array_type_p (TREE_TYPE (type))) | |
6169 | return true; | |
6170 | return false; | |
6171 | default: | |
6172 | return false; | |
6173 | } | |
6174 | } | |
6175 | #endif | |
6176 | ||
cf3caeaf NP |
6177 | /* Produce a printable version of an ivar name. This is only used |
6178 | inside add_instance_variable. */ | |
6179 | static const char * | |
6180 | printable_ivar_name (tree field_decl) | |
6181 | { | |
6182 | if (DECL_NAME (field_decl)) | |
6183 | return identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl))); | |
6184 | else | |
6185 | return _("<unnamed>"); | |
6186 | } | |
6187 | ||
d764a8e6 IS |
6188 | /* Called after parsing each instance variable declaration. Necessary to |
6189 | preserve typedefs and implement public/private... | |
6c65299b | 6190 | |
d764a8e6 | 6191 | VISIBILITY is 1 for public, 0 for protected, and 2 for private. */ |
6c65299b | 6192 | |
d764a8e6 | 6193 | static tree |
944fb799 | 6194 | add_instance_variable (tree klass, objc_ivar_visibility_kind visibility, |
d764a8e6 | 6195 | tree field_decl) |
6c65299b | 6196 | { |
d764a8e6 | 6197 | tree field_type = TREE_TYPE (field_decl); |
6c65299b | 6198 | |
d764a8e6 IS |
6199 | #ifdef OBJCPLUS |
6200 | if (TREE_CODE (field_type) == REFERENCE_TYPE) | |
6201 | { | |
6202 | error ("illegal reference type specified for instance variable %qs", | |
cf3caeaf | 6203 | printable_ivar_name (field_decl)); |
d764a8e6 IS |
6204 | /* Return class as is without adding this ivar. */ |
6205 | return klass; | |
6206 | } | |
6207 | #endif | |
6c65299b | 6208 | |
d764a8e6 IS |
6209 | if (field_type == error_mark_node || !TYPE_SIZE (field_type) |
6210 | || TYPE_SIZE (field_type) == error_mark_node) | |
6211 | /* 'type[0]' is allowed, but 'type[]' is not! */ | |
6c65299b | 6212 | { |
cf3caeaf NP |
6213 | error ("instance variable %qs has unknown size", |
6214 | printable_ivar_name (field_decl)); | |
d764a8e6 IS |
6215 | /* Return class as is without adding this ivar. */ |
6216 | return klass; | |
6217 | } | |
6c65299b | 6218 | |
4597541a NP |
6219 | #ifndef OBJCPLUS |
6220 | /* Also, in C reject a struct with a flexible array member. Ie, | |
6221 | ||
6222 | struct A { int x; int[] y; }; | |
6223 | ||
6224 | @interface X | |
6225 | { | |
6226 | struct A instance_variable; | |
6227 | } | |
6228 | @end | |
6229 | ||
6230 | is not valid because if the class is subclassed, we wouldn't be able | |
6231 | to calculate the offset of the next instance variable. */ | |
6232 | if (flexible_array_type_p (field_type)) | |
6233 | { | |
cf3caeaf NP |
6234 | error ("instance variable %qs uses flexible array member", |
6235 | printable_ivar_name (field_decl)); | |
4597541a | 6236 | /* Return class as is without adding this ivar. */ |
944fb799 | 6237 | return klass; |
4597541a NP |
6238 | } |
6239 | #endif | |
6240 | ||
264fa2db | 6241 | #ifdef OBJCPLUS |
d764a8e6 IS |
6242 | /* Check if the ivar being added has a non-POD C++ type. If so, we will |
6243 | need to either (1) warn the user about it or (2) generate suitable | |
6244 | constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct' | |
6245 | methods (if '-fobjc-call-cxx-cdtors' was specified). */ | |
6246 | if (MAYBE_CLASS_TYPE_P (field_type) | |
6247 | && (TYPE_NEEDS_CONSTRUCTING (field_type) | |
6248 | || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type) | |
6249 | || TYPE_POLYMORPHIC_P (field_type))) | |
6250 | { | |
6251 | tree type_name = OBJC_TYPE_NAME (field_type); | |
51900510 | 6252 | |
d764a8e6 IS |
6253 | if (flag_objc_call_cxx_cdtors) |
6254 | { | |
6255 | /* Since the ObjC runtime will be calling the constructors and | |
6256 | destructors for us, the only thing we can't handle is the lack | |
6257 | of a default constructor. */ | |
6258 | if (TYPE_NEEDS_CONSTRUCTING (field_type) | |
6259 | && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type)) | |
6260 | { | |
6261 | warning (0, "type %qE has no default constructor to call", | |
6262 | type_name); | |
6c65299b | 6263 | |
d764a8e6 IS |
6264 | /* If we cannot call a constructor, we should also avoid |
6265 | calling the destructor, for symmetry. */ | |
6266 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)) | |
6267 | warning (0, "destructor for %qE shall not be run either", | |
6268 | type_name); | |
6269 | } | |
6270 | } | |
6271 | else | |
6c65299b | 6272 | { |
d764a8e6 IS |
6273 | static bool warn_cxx_ivars = false; |
6274 | ||
6275 | if (TYPE_POLYMORPHIC_P (field_type)) | |
6c65299b | 6276 | { |
d764a8e6 IS |
6277 | /* Vtable pointers are Real Bad(tm), since Obj-C cannot |
6278 | initialize them. */ | |
6279 | error ("type %qE has virtual member functions", type_name); | |
6280 | error ("illegal aggregate type %qE specified " | |
6281 | "for instance variable %qs", | |
cf3caeaf | 6282 | type_name, printable_ivar_name (field_decl)); |
d764a8e6 IS |
6283 | /* Return class as is without adding this ivar. */ |
6284 | return klass; | |
6c65299b | 6285 | } |
d764a8e6 IS |
6286 | |
6287 | /* User-defined constructors and destructors are not known to Obj-C | |
6288 | and hence will not be called. This may or may not be a problem. */ | |
6289 | if (TYPE_NEEDS_CONSTRUCTING (field_type)) | |
6290 | warning (0, "type %qE has a user-defined constructor", type_name); | |
6291 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)) | |
6292 | warning (0, "type %qE has a user-defined destructor", type_name); | |
6293 | ||
6294 | if (!warn_cxx_ivars) | |
6c65299b | 6295 | { |
d764a8e6 IS |
6296 | warning (0, "C++ constructors and destructors will not " |
6297 | "be invoked for Objective-C fields"); | |
6298 | warn_cxx_ivars = true; | |
6c65299b RS |
6299 | } |
6300 | } | |
6c65299b | 6301 | } |
d764a8e6 | 6302 | #endif |
6c65299b | 6303 | |
d764a8e6 IS |
6304 | /* Overload the public attribute, it is not used for FIELD_DECLs. */ |
6305 | switch (visibility) | |
6306 | { | |
6307 | case OBJC_IVAR_VIS_PROTECTED: | |
6308 | TREE_PUBLIC (field_decl) = 0; | |
6309 | TREE_PRIVATE (field_decl) = 0; | |
6310 | TREE_PROTECTED (field_decl) = 1; | |
6311 | break; | |
6c65299b | 6312 | |
d764a8e6 IS |
6313 | case OBJC_IVAR_VIS_PACKAGE: |
6314 | /* TODO: Implement the package variant. */ | |
6315 | case OBJC_IVAR_VIS_PUBLIC: | |
6316 | TREE_PUBLIC (field_decl) = 1; | |
6317 | TREE_PRIVATE (field_decl) = 0; | |
6318 | TREE_PROTECTED (field_decl) = 0; | |
6319 | break; | |
6c65299b | 6320 | |
d764a8e6 IS |
6321 | case OBJC_IVAR_VIS_PRIVATE: |
6322 | TREE_PUBLIC (field_decl) = 0; | |
6323 | TREE_PRIVATE (field_decl) = 1; | |
6324 | TREE_PROTECTED (field_decl) = 0; | |
6325 | break; | |
6c65299b | 6326 | |
d764a8e6 | 6327 | } |
6c65299b | 6328 | |
d764a8e6 | 6329 | CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl); |
6c65299b | 6330 | |
d764a8e6 | 6331 | return klass; |
6c65299b RS |
6332 | } |
6333 | ||
d764a8e6 | 6334 | /* True if the ivar is private and we are not in its implementation. */ |
e31c7eec | 6335 | |
d764a8e6 IS |
6336 | static int |
6337 | is_private (tree decl) | |
6c65299b | 6338 | { |
d764a8e6 IS |
6339 | return (TREE_PRIVATE (decl) |
6340 | && ! is_ivar (CLASS_IVARS (implementation_template), | |
6341 | DECL_NAME (decl))); | |
6c65299b RS |
6342 | } |
6343 | ||
5678a5a3 NP |
6344 | /* Searches all the instance variables of 'klass' and of its |
6345 | superclasses for an instance variable whose name (identifier) is | |
6346 | 'ivar_name_ident'. Return the declaration (DECL) of the instance | |
6347 | variable, if found, or NULL_TREE, if not found. */ | |
6348 | static inline tree | |
6349 | ivar_of_class (tree klass, tree ivar_name_ident) | |
6350 | { | |
6351 | /* First, look up the ivar in CLASS_RAW_IVARS. */ | |
6352 | tree decl_chain = CLASS_RAW_IVARS (klass); | |
6353 | ||
6354 | for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain)) | |
6355 | if (DECL_NAME (decl_chain) == ivar_name_ident) | |
6356 | return decl_chain; | |
6357 | ||
6358 | /* If not found, search up the class hierarchy. */ | |
6359 | while (CLASS_SUPER_NAME (klass)) | |
6360 | { | |
6361 | klass = lookup_interface (CLASS_SUPER_NAME (klass)); | |
944fb799 | 6362 | |
5678a5a3 | 6363 | decl_chain = CLASS_RAW_IVARS (klass); |
944fb799 | 6364 | |
5678a5a3 NP |
6365 | for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain)) |
6366 | if (DECL_NAME (decl_chain) == ivar_name_ident) | |
6367 | return decl_chain; | |
6368 | } | |
6369 | ||
6370 | return NULL_TREE; | |
6371 | } | |
6372 | ||
d764a8e6 | 6373 | /* We have an instance variable reference;, check to see if it is public. */ |
e31c7eec | 6374 | |
d764a8e6 IS |
6375 | int |
6376 | objc_is_public (tree expr, tree identifier) | |
6c65299b | 6377 | { |
d764a8e6 | 6378 | tree basetype, decl; |
6c65299b | 6379 | |
d764a8e6 IS |
6380 | #ifdef OBJCPLUS |
6381 | if (processing_template_decl) | |
6382 | return 1; | |
6383 | #endif | |
6c65299b | 6384 | |
d764a8e6 IS |
6385 | if (TREE_TYPE (expr) == error_mark_node) |
6386 | return 1; | |
6c65299b | 6387 | |
d764a8e6 | 6388 | basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr)); |
6c65299b | 6389 | |
d764a8e6 IS |
6390 | if (basetype && TREE_CODE (basetype) == RECORD_TYPE) |
6391 | { | |
6392 | if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype)) | |
6393 | { | |
6394 | tree klass = lookup_interface (OBJC_TYPE_NAME (basetype)); | |
6c65299b | 6395 | |
d764a8e6 IS |
6396 | if (!klass) |
6397 | { | |
6398 | error ("cannot find interface declaration for %qE", | |
6399 | OBJC_TYPE_NAME (basetype)); | |
6400 | return 0; | |
6401 | } | |
6c65299b | 6402 | |
5678a5a3 | 6403 | if ((decl = ivar_of_class (klass, identifier))) |
d764a8e6 IS |
6404 | { |
6405 | if (TREE_PUBLIC (decl)) | |
6406 | return 1; | |
6c65299b | 6407 | |
d764a8e6 IS |
6408 | /* Important difference between the Stepstone translator: |
6409 | all instance variables should be public within the context | |
6410 | of the implementation. */ | |
6411 | if (objc_implementation_context | |
6412 | && ((TREE_CODE (objc_implementation_context) | |
6413 | == CLASS_IMPLEMENTATION_TYPE) | |
6414 | || (TREE_CODE (objc_implementation_context) | |
6415 | == CATEGORY_IMPLEMENTATION_TYPE))) | |
6416 | { | |
6417 | tree curtype = TYPE_MAIN_VARIANT | |
6418 | (CLASS_STATIC_TEMPLATE | |
6419 | (implementation_template)); | |
6c65299b | 6420 | |
d764a8e6 IS |
6421 | if (basetype == curtype |
6422 | || DERIVED_FROM_P (basetype, curtype)) | |
6423 | { | |
6424 | int priv = is_private (decl); | |
6c65299b | 6425 | |
d764a8e6 IS |
6426 | if (priv) |
6427 | error ("instance variable %qE is declared private", | |
6428 | DECL_NAME (decl)); | |
6c65299b | 6429 | |
d764a8e6 IS |
6430 | return !priv; |
6431 | } | |
6432 | } | |
6c65299b | 6433 | |
d764a8e6 IS |
6434 | /* The 2.95.2 compiler sometimes allowed C functions to access |
6435 | non-@public ivars. We will let this slide for now... */ | |
6436 | if (!objc_method_context) | |
6437 | { | |
6438 | warning (0, "instance variable %qE is %s; " | |
6439 | "this will be a hard error in the future", | |
6440 | identifier, | |
6441 | TREE_PRIVATE (decl) ? "@private" : "@protected"); | |
6442 | return 1; | |
6443 | } | |
6c65299b | 6444 | |
d764a8e6 IS |
6445 | error ("instance variable %qE is declared %s", |
6446 | identifier, | |
6447 | TREE_PRIVATE (decl) ? "private" : "protected"); | |
6448 | return 0; | |
6449 | } | |
6450 | } | |
6451 | } | |
6c65299b | 6452 | |
d764a8e6 | 6453 | return 1; |
6c65299b | 6454 | } |
d764a8e6 IS |
6455 | \f |
6456 | /* Make sure all methods in CHAIN (a list of method declarations from | |
6457 | an @interface or a @protocol) are in IMPLEMENTATION (the | |
6458 | implementation context). This is used to check for example that | |
6459 | all methods declared in an @interface were implemented in an | |
6460 | @implementation. | |
6c65299b | 6461 | |
d764a8e6 IS |
6462 | Some special methods (property setters/getters) are special and if |
6463 | they are not found in IMPLEMENTATION, we look them up in its | |
6464 | superclasses. */ | |
96e5d745 | 6465 | |
264fa2db | 6466 | static int |
d764a8e6 | 6467 | check_methods (tree chain, tree implementation, int mtype) |
6c65299b | 6468 | { |
d764a8e6 IS |
6469 | int first = 1; |
6470 | tree list; | |
6c65299b | 6471 | |
d764a8e6 IS |
6472 | if (mtype == (int)'+') |
6473 | list = CLASS_CLS_METHODS (implementation); | |
6c65299b | 6474 | else |
d764a8e6 | 6475 | list = CLASS_NST_METHODS (implementation); |
6c65299b | 6476 | |
d764a8e6 | 6477 | while (chain) |
6c65299b | 6478 | { |
d764a8e6 IS |
6479 | /* If the method is associated with a dynamic property, then it |
6480 | is Ok not to have the method implementation, as it will be | |
6481 | generated dynamically at runtime. To decide if the method is | |
6482 | associated with a @dynamic property, we search the list of | |
6483 | @synthesize and @dynamic for this implementation, and look | |
6484 | for any @dynamic property with the same setter or getter name | |
6485 | as this method. */ | |
6486 | tree x; | |
6487 | for (x = IMPL_PROPERTY_DECL (implementation); x; x = TREE_CHAIN (x)) | |
6488 | if (PROPERTY_DYNAMIC (x) | |
6489 | && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain) | |
6490 | || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain))) | |
6491 | break; | |
944fb799 | 6492 | |
d764a8e6 IS |
6493 | if (x != NULL_TREE) |
6494 | { | |
6495 | chain = TREE_CHAIN (chain); /* next method... */ | |
6496 | continue; | |
6497 | } | |
6c65299b | 6498 | |
d764a8e6 IS |
6499 | if (!lookup_method (list, chain)) |
6500 | { | |
6501 | /* If the method is a property setter/getter, we'll still | |
6502 | allow it to be missing if it is implemented by | |
6503 | 'interface' or any of its superclasses. */ | |
6504 | tree property = METHOD_PROPERTY_CONTEXT (chain); | |
6505 | if (property) | |
6506 | { | |
6507 | /* Note that since this is a property getter/setter, it | |
6508 | is obviously an instance method. */ | |
6509 | tree interface = NULL_TREE; | |
6c65299b | 6510 | |
d764a8e6 IS |
6511 | /* For a category, first check the main class |
6512 | @interface. */ | |
6513 | if (TREE_CODE (implementation) == CATEGORY_IMPLEMENTATION_TYPE) | |
6514 | { | |
6515 | interface = lookup_interface (CLASS_NAME (implementation)); | |
30c0e2df | 6516 | |
d764a8e6 IS |
6517 | /* If the method is found in the main class, it's Ok. */ |
6518 | if (lookup_method (CLASS_NST_METHODS (interface), chain)) | |
6519 | { | |
6520 | chain = DECL_CHAIN (chain); | |
944fb799 | 6521 | continue; |
d764a8e6 | 6522 | } |
6c65299b | 6523 | |
d764a8e6 IS |
6524 | /* Else, get the superclass. */ |
6525 | if (CLASS_SUPER_NAME (interface)) | |
6526 | interface = lookup_interface (CLASS_SUPER_NAME (interface)); | |
6527 | else | |
6528 | interface = NULL_TREE; | |
6529 | } | |
51a56a7d | 6530 | |
d764a8e6 IS |
6531 | /* Get the superclass for classes. */ |
6532 | if (TREE_CODE (implementation) == CLASS_IMPLEMENTATION_TYPE) | |
6533 | { | |
6534 | if (CLASS_SUPER_NAME (implementation)) | |
6535 | interface = lookup_interface (CLASS_SUPER_NAME (implementation)); | |
6536 | else | |
6537 | interface = NULL_TREE; | |
6538 | } | |
6c65299b | 6539 | |
d764a8e6 IS |
6540 | /* Now, interface is the superclass, if any; go check it. */ |
6541 | if (interface) | |
6542 | { | |
6543 | if (lookup_method_static (interface, chain, 0)) | |
6544 | { | |
6545 | chain = DECL_CHAIN (chain); | |
6546 | continue; | |
6547 | } | |
6548 | } | |
6549 | /* Else, fall through - warn. */ | |
6550 | } | |
6551 | if (first) | |
6552 | { | |
6553 | switch (TREE_CODE (implementation)) | |
6554 | { | |
6555 | case CLASS_IMPLEMENTATION_TYPE: | |
6556 | warning (0, "incomplete implementation of class %qE", | |
6557 | CLASS_NAME (implementation)); | |
6558 | break; | |
6559 | case CATEGORY_IMPLEMENTATION_TYPE: | |
6560 | warning (0, "incomplete implementation of category %qE", | |
6561 | CLASS_SUPER_NAME (implementation)); | |
6562 | break; | |
6563 | default: | |
6564 | gcc_unreachable (); | |
6565 | } | |
6566 | first = 0; | |
6567 | } | |
30c0e2df | 6568 | |
d764a8e6 IS |
6569 | warning (0, "method definition for %<%c%E%> not found", |
6570 | mtype, METHOD_SEL_NAME (chain)); | |
6571 | } | |
6c65299b | 6572 | |
d764a8e6 | 6573 | chain = DECL_CHAIN (chain); |
6c65299b | 6574 | } |
6c65299b | 6575 | |
d764a8e6 | 6576 | return first; |
6c65299b RS |
6577 | } |
6578 | ||
d764a8e6 | 6579 | /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */ |
e31c7eec | 6580 | |
d764a8e6 IS |
6581 | static int |
6582 | conforms_to_protocol (tree klass, tree protocol) | |
6c65299b | 6583 | { |
d764a8e6 IS |
6584 | if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE) |
6585 | { | |
6586 | tree p = CLASS_PROTOCOL_LIST (klass); | |
6587 | while (p && TREE_VALUE (p) != protocol) | |
6588 | p = TREE_CHAIN (p); | |
6c65299b | 6589 | |
d764a8e6 IS |
6590 | if (!p) |
6591 | { | |
6592 | tree super = (CLASS_SUPER_NAME (klass) | |
6593 | ? lookup_interface (CLASS_SUPER_NAME (klass)) | |
6594 | : NULL_TREE); | |
6595 | int tmp = super ? conforms_to_protocol (super, protocol) : 0; | |
6596 | if (!tmp) | |
6597 | return 0; | |
6598 | } | |
6599 | } | |
6c65299b | 6600 | |
d764a8e6 | 6601 | return 1; |
6c65299b RS |
6602 | } |
6603 | ||
d764a8e6 IS |
6604 | /* Make sure all methods in CHAIN are accessible as MTYPE methods in |
6605 | CONTEXT. This is one of two mechanisms to check protocol integrity. */ | |
6c65299b | 6606 | |
d764a8e6 IS |
6607 | static int |
6608 | check_methods_accessible (tree chain, tree context, int mtype) | |
0d446150 | 6609 | { |
d764a8e6 IS |
6610 | int first = 1; |
6611 | tree list; | |
6612 | tree base_context = context; | |
0d446150 | 6613 | |
d764a8e6 | 6614 | while (chain) |
0d446150 | 6615 | { |
d764a8e6 IS |
6616 | /* If the method is associated with a dynamic property, then it |
6617 | is Ok not to have the method implementation, as it will be | |
6618 | generated dynamically at runtime. Search for any @dynamic | |
6619 | property with the same setter or getter name as this | |
6620 | method. TODO: Use a hashtable lookup. */ | |
6621 | tree x; | |
6622 | for (x = IMPL_PROPERTY_DECL (base_context); x; x = TREE_CHAIN (x)) | |
6623 | if (PROPERTY_DYNAMIC (x) | |
6624 | && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain) | |
6625 | || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain))) | |
6626 | break; | |
944fb799 | 6627 | |
d764a8e6 | 6628 | if (x != NULL_TREE) |
0d446150 | 6629 | { |
d764a8e6 IS |
6630 | chain = TREE_CHAIN (chain); /* next method... */ |
6631 | continue; | |
944fb799 | 6632 | } |
8dafba3c | 6633 | |
d764a8e6 IS |
6634 | context = base_context; |
6635 | while (context) | |
0d446150 | 6636 | { |
d764a8e6 IS |
6637 | if (mtype == '+') |
6638 | list = CLASS_CLS_METHODS (context); | |
6639 | else | |
6640 | list = CLASS_NST_METHODS (context); | |
e31c7eec | 6641 | |
d764a8e6 IS |
6642 | if (lookup_method (list, chain)) |
6643 | break; | |
6c65299b | 6644 | |
d764a8e6 IS |
6645 | switch (TREE_CODE (context)) |
6646 | { | |
6647 | case CLASS_IMPLEMENTATION_TYPE: | |
6648 | case CLASS_INTERFACE_TYPE: | |
6649 | context = (CLASS_SUPER_NAME (context) | |
6650 | ? lookup_interface (CLASS_SUPER_NAME (context)) | |
6651 | : NULL_TREE); | |
6652 | break; | |
6653 | case CATEGORY_IMPLEMENTATION_TYPE: | |
6654 | case CATEGORY_INTERFACE_TYPE: | |
6655 | context = (CLASS_NAME (context) | |
6656 | ? lookup_interface (CLASS_NAME (context)) | |
6657 | : NULL_TREE); | |
6658 | break; | |
6659 | default: | |
6660 | gcc_unreachable (); | |
6661 | } | |
6662 | } | |
30c0e2df | 6663 | |
d764a8e6 IS |
6664 | if (context == NULL_TREE) |
6665 | { | |
6666 | if (first) | |
6667 | { | |
6668 | switch (TREE_CODE (objc_implementation_context)) | |
6669 | { | |
6670 | case CLASS_IMPLEMENTATION_TYPE: | |
6671 | warning (0, "incomplete implementation of class %qE", | |
6672 | CLASS_NAME (objc_implementation_context)); | |
6673 | break; | |
6674 | case CATEGORY_IMPLEMENTATION_TYPE: | |
6675 | warning (0, "incomplete implementation of category %qE", | |
6676 | CLASS_SUPER_NAME (objc_implementation_context)); | |
6677 | break; | |
6678 | default: | |
6679 | gcc_unreachable (); | |
6680 | } | |
6681 | first = 0; | |
6682 | } | |
6683 | warning (0, "method definition for %<%c%E%> not found", | |
6684 | mtype, METHOD_SEL_NAME (chain)); | |
6685 | } | |
6c65299b | 6686 | |
d764a8e6 | 6687 | chain = TREE_CHAIN (chain); /* next method... */ |
6c65299b | 6688 | } |
d764a8e6 | 6689 | return first; |
e31c7eec TW |
6690 | } |
6691 | ||
d764a8e6 IS |
6692 | /* Check whether the current interface (accessible via |
6693 | 'objc_implementation_context') actually implements protocol P, along | |
6694 | with any protocols that P inherits. */ | |
e31c7eec | 6695 | |
d764a8e6 IS |
6696 | static void |
6697 | check_protocol (tree p, const char *type, tree name) | |
6698 | { | |
6699 | if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE) | |
50ea5861 | 6700 | { |
d764a8e6 | 6701 | int f1, f2; |
e31c7eec | 6702 | |
d764a8e6 IS |
6703 | /* Ensure that all protocols have bodies! */ |
6704 | if (warn_protocol) | |
6705 | { | |
6706 | f1 = check_methods (PROTOCOL_CLS_METHODS (p), | |
6707 | objc_implementation_context, | |
6708 | '+'); | |
6709 | f2 = check_methods (PROTOCOL_NST_METHODS (p), | |
6710 | objc_implementation_context, | |
6711 | '-'); | |
6712 | } | |
6713 | else | |
6714 | { | |
6715 | f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p), | |
6716 | objc_implementation_context, | |
6717 | '+'); | |
6718 | f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p), | |
6719 | objc_implementation_context, | |
6720 | '-'); | |
6721 | } | |
e31c7eec | 6722 | |
d764a8e6 IS |
6723 | if (!f1 || !f2) |
6724 | warning (0, "%s %qE does not fully implement the %qE protocol", | |
6725 | type, name, PROTOCOL_NAME (p)); | |
6726 | } | |
e31c7eec | 6727 | |
d764a8e6 IS |
6728 | /* Check protocols recursively. */ |
6729 | if (PROTOCOL_LIST (p)) | |
e31c7eec | 6730 | { |
d764a8e6 IS |
6731 | tree subs = PROTOCOL_LIST (p); |
6732 | tree super_class = | |
6733 | lookup_interface (CLASS_SUPER_NAME (implementation_template)); | |
e31c7eec | 6734 | |
d764a8e6 | 6735 | while (subs) |
e31c7eec | 6736 | { |
d764a8e6 IS |
6737 | tree sub = TREE_VALUE (subs); |
6738 | ||
6739 | /* If the superclass does not conform to the protocols | |
6740 | inherited by P, then we must! */ | |
6741 | if (!super_class || !conforms_to_protocol (super_class, sub)) | |
6742 | check_protocol (sub, type, name); | |
6743 | subs = TREE_CHAIN (subs); | |
e31c7eec TW |
6744 | } |
6745 | } | |
d764a8e6 | 6746 | } |
e31c7eec | 6747 | |
d764a8e6 IS |
6748 | /* Check whether the current interface (accessible via |
6749 | 'objc_implementation_context') actually implements the protocols listed | |
6750 | in PROTO_LIST. */ | |
e31c7eec | 6751 | |
d764a8e6 IS |
6752 | static void |
6753 | check_protocols (tree proto_list, const char *type, tree name) | |
6754 | { | |
6755 | for ( ; proto_list; proto_list = TREE_CHAIN (proto_list)) | |
50ea5861 | 6756 | { |
d764a8e6 | 6757 | tree p = TREE_VALUE (proto_list); |
e31c7eec | 6758 | |
d764a8e6 IS |
6759 | check_protocol (p, type, name); |
6760 | } | |
6c65299b | 6761 | } |
d764a8e6 IS |
6762 | \f |
6763 | /* Make sure that the class CLASS_NAME is defined CODE says which kind | |
6764 | of thing CLASS_NAME ought to be. It can be CLASS_INTERFACE_TYPE, | |
6765 | CLASS_IMPLEMENTATION_TYPE, CATEGORY_INTERFACE_TYPE, or | |
6766 | CATEGORY_IMPLEMENTATION_TYPE. For a CATEGORY_INTERFACE_TYPE, | |
6767 | SUPER_NAME is the name of the category. For a class extension, | |
6768 | CODE is CATEGORY_INTERFACE_TYPE and SUPER_NAME is NULL_TREE. */ | |
6c65299b | 6769 | static tree |
d764a8e6 IS |
6770 | start_class (enum tree_code code, tree class_name, tree super_name, |
6771 | tree protocol_list, tree attributes) | |
6c65299b | 6772 | { |
d764a8e6 IS |
6773 | tree klass = NULL_TREE; |
6774 | tree decl; | |
6c65299b | 6775 | |
d764a8e6 IS |
6776 | #ifdef OBJCPLUS |
6777 | if (current_namespace != global_namespace) | |
6c65299b | 6778 | { |
d764a8e6 | 6779 | error ("Objective-C declarations may only appear in global scope"); |
6c65299b | 6780 | } |
d764a8e6 IS |
6781 | #endif /* OBJCPLUS */ |
6782 | ||
6783 | if (objc_implementation_context) | |
6c65299b | 6784 | { |
d764a8e6 IS |
6785 | warning (0, "%<@end%> missing in implementation context"); |
6786 | finish_class (objc_implementation_context); | |
6787 | objc_ivar_chain = NULL_TREE; | |
6788 | objc_implementation_context = NULL_TREE; | |
6c65299b | 6789 | } |
e31c7eec | 6790 | |
d764a8e6 IS |
6791 | /* If this is a class extension, we'll be "reopening" the existing |
6792 | CLASS_INTERFACE_TYPE, so in that case there is no need to create | |
6793 | a new node. */ | |
6794 | if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE) | |
6408ef33 | 6795 | { |
d764a8e6 IS |
6796 | klass = make_node (code); |
6797 | TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS); | |
6798 | } | |
6c65299b | 6799 | |
d764a8e6 IS |
6800 | /* Check for existence of the super class, if one was specified. Note |
6801 | that we must have seen an @interface, not just a @class. If we | |
6802 | are looking at a @compatibility_alias, traverse it first. */ | |
6803 | if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE) | |
6804 | && super_name) | |
6c65299b | 6805 | { |
d764a8e6 IS |
6806 | tree super = objc_is_class_name (super_name); |
6807 | tree super_interface = NULL_TREE; | |
6808 | ||
6809 | if (super) | |
6810 | super_interface = lookup_interface (super); | |
944fb799 | 6811 | |
d764a8e6 IS |
6812 | if (!super_interface) |
6813 | { | |
6814 | error ("cannot find interface declaration for %qE, superclass of %qE", | |
6815 | super ? super : super_name, | |
6816 | class_name); | |
6817 | super_name = NULL_TREE; | |
6818 | } | |
6819 | else | |
6820 | { | |
6821 | if (TREE_DEPRECATED (super_interface)) | |
944fb799 | 6822 | warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", |
d764a8e6 IS |
6823 | super); |
6824 | super_name = super; | |
6825 | } | |
6c65299b RS |
6826 | } |
6827 | ||
d764a8e6 | 6828 | if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE) |
6c65299b | 6829 | { |
d764a8e6 IS |
6830 | CLASS_NAME (klass) = class_name; |
6831 | CLASS_SUPER_NAME (klass) = super_name; | |
6832 | CLASS_CLS_METHODS (klass) = NULL_TREE; | |
6c65299b RS |
6833 | } |
6834 | ||
d764a8e6 IS |
6835 | if (! objc_is_class_name (class_name) |
6836 | && (decl = lookup_name (class_name))) | |
e31c7eec | 6837 | { |
d764a8e6 IS |
6838 | error ("%qE redeclared as different kind of symbol", |
6839 | class_name); | |
6840 | error ("previous declaration of %q+D", | |
6841 | decl); | |
e31c7eec TW |
6842 | } |
6843 | ||
d764a8e6 | 6844 | switch (code) |
6408ef33 | 6845 | { |
d764a8e6 IS |
6846 | case CLASS_IMPLEMENTATION_TYPE: |
6847 | { | |
6848 | tree chain; | |
944fb799 | 6849 | |
d764a8e6 IS |
6850 | for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain)) |
6851 | if (TREE_VALUE (chain) == class_name) | |
6852 | { | |
6853 | error ("reimplementation of class %qE", | |
6854 | class_name); | |
6855 | /* TODO: error message saying where it was previously | |
6856 | implemented. */ | |
6857 | break; | |
6858 | } | |
6859 | if (chain == NULL_TREE) | |
6860 | implemented_classes = tree_cons (NULL_TREE, class_name, | |
6861 | implemented_classes); | |
6862 | } | |
6c65299b | 6863 | |
d764a8e6 IS |
6864 | /* Reset for multiple classes per file. */ |
6865 | method_slot = 0; | |
264fa2db | 6866 | |
d764a8e6 | 6867 | objc_implementation_context = klass; |
d8bb17c8 | 6868 | |
d764a8e6 | 6869 | /* Lookup the interface for this implementation. */ |
6c65299b | 6870 | |
d764a8e6 IS |
6871 | if (!(implementation_template = lookup_interface (class_name))) |
6872 | { | |
6873 | warning (0, "cannot find interface declaration for %qE", | |
6874 | class_name); | |
3cc2dd4b NP |
6875 | add_interface (implementation_template = objc_implementation_context, |
6876 | class_name); | |
d764a8e6 | 6877 | } |
e31c7eec | 6878 | |
d764a8e6 IS |
6879 | /* If a super class has been specified in the implementation, |
6880 | insure it conforms to the one specified in the interface. */ | |
6c65299b | 6881 | |
d764a8e6 IS |
6882 | if (super_name |
6883 | && (super_name != CLASS_SUPER_NAME (implementation_template))) | |
6c65299b | 6884 | { |
d764a8e6 IS |
6885 | tree previous_name = CLASS_SUPER_NAME (implementation_template); |
6886 | error ("conflicting super class name %qE", | |
6887 | super_name); | |
6888 | if (previous_name) | |
6889 | error ("previous declaration of %qE", previous_name); | |
6c65299b | 6890 | else |
d764a8e6 | 6891 | error ("previous declaration"); |
6c65299b | 6892 | } |
6c65299b | 6893 | |
d764a8e6 IS |
6894 | else if (! super_name) |
6895 | { | |
6896 | CLASS_SUPER_NAME (objc_implementation_context) | |
6897 | = CLASS_SUPER_NAME (implementation_template); | |
6898 | } | |
6899 | break; | |
6c65299b | 6900 | |
d764a8e6 IS |
6901 | case CLASS_INTERFACE_TYPE: |
6902 | if (lookup_interface (class_name)) | |
6903 | #ifdef OBJCPLUS | |
6904 | error ("duplicate interface declaration for class %qE", class_name); | |
6905 | #else | |
6906 | warning (0, "duplicate interface declaration for class %qE", class_name); | |
6907 | #endif | |
6908 | else | |
3cc2dd4b | 6909 | add_interface (klass, class_name); |
944fb799 | 6910 | |
d764a8e6 IS |
6911 | if (protocol_list) |
6912 | CLASS_PROTOCOL_LIST (klass) | |
6913 | = lookup_and_install_protocols (protocol_list, /* definition_required */ true); | |
e31c7eec | 6914 | |
d764a8e6 IS |
6915 | if (attributes) |
6916 | { | |
6917 | tree attribute; | |
6918 | for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute)) | |
6919 | { | |
6920 | tree name = TREE_PURPOSE (attribute); | |
944fb799 | 6921 | |
d4c433f9 | 6922 | /* TODO: Document what the objc_exception attribute is/does. */ |
d764a8e6 IS |
6923 | /* We handle the 'deprecated' and (undocumented) 'objc_exception' |
6924 | attributes. */ | |
6925 | if (is_attribute_p ("deprecated", name)) | |
6926 | TREE_DEPRECATED (klass) = 1; | |
6927 | else if (is_attribute_p ("objc_exception", name)) | |
6928 | CLASS_HAS_EXCEPTION_ATTR (klass) = 1; | |
6929 | else | |
6930 | /* Warn about and ignore all others for now, but store them. */ | |
6931 | warning (OPT_Wattributes, "%qE attribute directive ignored", name); | |
6932 | } | |
6933 | TYPE_ATTRIBUTES (klass) = attributes; | |
6934 | } | |
944fb799 | 6935 | break; |
6c65299b | 6936 | |
d764a8e6 IS |
6937 | case CATEGORY_INTERFACE_TYPE: |
6938 | { | |
6939 | tree class_category_is_assoc_with; | |
944fb799 | 6940 | |
d764a8e6 IS |
6941 | /* For a category, class_name is really the name of the class that |
6942 | the following set of methods will be associated with. We must | |
6943 | find the interface so that can derive the objects template. */ | |
6944 | if (!(class_category_is_assoc_with = lookup_interface (class_name))) | |
6945 | { | |
6946 | error ("cannot find interface declaration for %qE", | |
6947 | class_name); | |
6948 | exit (FATAL_EXIT_CODE); | |
6949 | } | |
6950 | else | |
6951 | { | |
6952 | if (TREE_DEPRECATED (class_category_is_assoc_with)) | |
944fb799 | 6953 | warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", |
d764a8e6 | 6954 | class_name); |
6c65299b | 6955 | |
d764a8e6 IS |
6956 | if (super_name == NULL_TREE) |
6957 | { | |
6958 | /* This is a class extension. Get the original | |
6959 | interface, and continue working on it. */ | |
6960 | objc_in_class_extension = true; | |
6961 | klass = class_category_is_assoc_with; | |
6c65299b | 6962 | |
d764a8e6 IS |
6963 | if (protocol_list) |
6964 | { | |
6965 | /* Append protocols to the original protocol | |
6966 | list. */ | |
6967 | CLASS_PROTOCOL_LIST (klass) | |
6968 | = chainon (CLASS_PROTOCOL_LIST (klass), | |
6969 | lookup_and_install_protocols | |
6970 | (protocol_list, | |
6971 | /* definition_required */ true)); | |
6972 | } | |
6973 | } | |
6974 | else | |
6975 | { | |
6976 | add_category (class_category_is_assoc_with, klass); | |
944fb799 | 6977 | |
d764a8e6 IS |
6978 | if (protocol_list) |
6979 | CLASS_PROTOCOL_LIST (klass) | |
6980 | = lookup_and_install_protocols | |
6981 | (protocol_list, /* definition_required */ true); | |
6982 | } | |
6983 | } | |
6984 | } | |
6985 | break; | |
944fb799 | 6986 | |
d764a8e6 IS |
6987 | case CATEGORY_IMPLEMENTATION_TYPE: |
6988 | /* Reset for multiple classes per file. */ | |
6989 | method_slot = 0; | |
6c65299b | 6990 | |
d764a8e6 | 6991 | objc_implementation_context = klass; |
6c65299b | 6992 | |
d764a8e6 IS |
6993 | /* For a category, class_name is really the name of the class that |
6994 | the following set of methods will be associated with. We must | |
6995 | find the interface so that can derive the objects template. */ | |
6c65299b | 6996 | |
d764a8e6 IS |
6997 | if (!(implementation_template = lookup_interface (class_name))) |
6998 | { | |
6999 | error ("cannot find interface declaration for %qE", | |
7000 | class_name); | |
7001 | exit (FATAL_EXIT_CODE); | |
7002 | } | |
7003 | break; | |
7004 | default: | |
7005 | gcc_unreachable (); | |
7006 | } | |
7007 | return klass; | |
6c65299b RS |
7008 | } |
7009 | ||
d764a8e6 IS |
7010 | static tree |
7011 | continue_class (tree klass) | |
6c65299b | 7012 | { |
d764a8e6 | 7013 | switch (TREE_CODE (klass)) |
6e3f0724 | 7014 | { |
50ea5861 | 7015 | case CLASS_IMPLEMENTATION_TYPE: |
50ea5861 | 7016 | case CATEGORY_IMPLEMENTATION_TYPE: |
50ea5861 | 7017 | { |
d764a8e6 IS |
7018 | struct imp_entry *imp_entry; |
7019 | ||
7020 | /* Check consistency of the instance variables. */ | |
7021 | ||
7022 | if (CLASS_RAW_IVARS (klass)) | |
7023 | check_ivars (implementation_template, klass); | |
944fb799 | 7024 | |
d764a8e6 IS |
7025 | /* code generation */ |
7026 | #ifdef OBJCPLUS | |
7027 | push_lang_context (lang_name_c); | |
7028 | #endif | |
7029 | build_private_template (implementation_template); | |
7030 | uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template); | |
7031 | objc_instance_type = build_pointer_type (uprivate_record); | |
7032 | ||
766090c2 | 7033 | imp_entry = ggc_alloc<struct imp_entry> (); |
d764a8e6 IS |
7034 | |
7035 | imp_entry->next = imp_list; | |
7036 | imp_entry->imp_context = klass; | |
7037 | imp_entry->imp_template = implementation_template; | |
7038 | ucls_super_ref = uucls_super_ref = NULL; | |
7039 | if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE) | |
7040 | { | |
7041 | imp_entry->class_decl = (*runtime.class_decl) (klass); | |
7042 | imp_entry->meta_decl = (*runtime.metaclass_decl) (klass); | |
7043 | } | |
7044 | else | |
7045 | { | |
7046 | imp_entry->class_decl = (*runtime.category_decl) (klass); | |
7047 | imp_entry->meta_decl = NULL; | |
7048 | } | |
7049 | imp_entry->has_cxx_cdtors = 0; | |
7050 | ||
7051 | /* Append to front and increment count. */ | |
7052 | imp_list = imp_entry; | |
7053 | if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE) | |
7054 | imp_count++; | |
7055 | else | |
7056 | cat_count++; | |
7057 | #ifdef OBJCPLUS | |
7058 | pop_lang_context (); | |
7059 | #endif /* OBJCPLUS */ | |
944fb799 | 7060 | |
d764a8e6 | 7061 | return get_class_ivars (implementation_template, true); |
50ea5861 | 7062 | break; |
d764a8e6 IS |
7063 | } |
7064 | case CLASS_INTERFACE_TYPE: | |
50ea5861 | 7065 | { |
d764a8e6 IS |
7066 | if (objc_in_class_extension) |
7067 | return NULL_TREE; | |
7068 | #ifdef OBJCPLUS | |
7069 | push_lang_context (lang_name_c); | |
7070 | #endif /* OBJCPLUS */ | |
7071 | objc_collecting_ivars = 1; | |
7072 | build_private_template (klass); | |
7073 | objc_collecting_ivars = 0; | |
7074 | #ifdef OBJCPLUS | |
7075 | pop_lang_context (); | |
7076 | #endif /* OBJCPLUS */ | |
7077 | return NULL_TREE; | |
50ea5861 NP |
7078 | break; |
7079 | } | |
7080 | default: | |
d764a8e6 | 7081 | return error_mark_node; |
6e3f0724 | 7082 | } |
e31c7eec TW |
7083 | } |
7084 | ||
d764a8e6 | 7085 | /* This routine builds name of the setter synthesized function. */ |
f027ee7c | 7086 | char * |
d764a8e6 | 7087 | objc_build_property_setter_name (tree ident) |
e31c7eec | 7088 | { |
d764a8e6 IS |
7089 | /* TODO: Use alloca to allocate buffer of appropriate size. */ |
7090 | static char string[BUFSIZE]; | |
7091 | sprintf (string, "set%s:", IDENTIFIER_POINTER (ident)); | |
7092 | string[3] = TOUPPER (string[3]); | |
7093 | return string; | |
e31c7eec TW |
7094 | } |
7095 | ||
d764a8e6 IS |
7096 | /* This routine prepares the declarations of the property accessor |
7097 | helper functions (objc_getProperty(), etc) that are used when | |
944fb799 MS |
7098 | @synthesize is used. |
7099 | ||
7100 | runtime-specific routines are built in the respective runtime | |
d764a8e6 | 7101 | initialize functions. */ |
944fb799 | 7102 | static void |
d764a8e6 IS |
7103 | build_common_objc_property_accessor_helpers (void) |
7104 | { | |
7105 | tree type; | |
011d50d9 | 7106 | |
d764a8e6 IS |
7107 | /* Declare the following function: |
7108 | id | |
944fb799 | 7109 | objc_getProperty (id self, SEL _cmd, |
d764a8e6 IS |
7110 | ptrdiff_t offset, BOOL is_atomic); */ |
7111 | type = build_function_type_list (objc_object_type, | |
7112 | objc_object_type, | |
7113 | objc_selector_type, | |
7114 | ptrdiff_type_node, | |
7115 | boolean_type_node, | |
7116 | NULL_TREE); | |
7117 | objc_getProperty_decl = add_builtin_function ("objc_getProperty", | |
7118 | type, 0, NOT_BUILT_IN, | |
7119 | NULL, NULL_TREE); | |
7120 | TREE_NOTHROW (objc_getProperty_decl) = 0; | |
944fb799 | 7121 | |
d764a8e6 IS |
7122 | /* Declare the following function: |
7123 | void | |
944fb799 MS |
7124 | objc_setProperty (id self, SEL _cmd, |
7125 | ptrdiff_t offset, id new_value, | |
d764a8e6 IS |
7126 | BOOL is_atomic, BOOL should_copy); */ |
7127 | type = build_function_type_list (void_type_node, | |
7128 | objc_object_type, | |
7129 | objc_selector_type, | |
7130 | ptrdiff_type_node, | |
7131 | objc_object_type, | |
7132 | boolean_type_node, | |
7133 | boolean_type_node, | |
7134 | NULL_TREE); | |
7135 | objc_setProperty_decl = add_builtin_function ("objc_setProperty", | |
7136 | type, 0, NOT_BUILT_IN, | |
7137 | NULL, NULL_TREE); | |
7138 | TREE_NOTHROW (objc_setProperty_decl) = 0; | |
6c65299b RS |
7139 | } |
7140 | ||
d764a8e6 | 7141 | /* This looks up an ivar in a class (including superclasses). */ |
6c65299b | 7142 | static tree |
d764a8e6 | 7143 | lookup_ivar (tree interface, tree instance_variable_name) |
6c65299b | 7144 | { |
d764a8e6 | 7145 | while (interface) |
6c65299b | 7146 | { |
d764a8e6 | 7147 | tree decl_chain; |
944fb799 | 7148 | |
d764a8e6 IS |
7149 | for (decl_chain = CLASS_IVARS (interface); decl_chain; decl_chain = DECL_CHAIN (decl_chain)) |
7150 | if (DECL_NAME (decl_chain) == instance_variable_name) | |
7151 | return decl_chain; | |
944fb799 | 7152 | |
d764a8e6 IS |
7153 | /* Not found. Search superclass if any. */ |
7154 | if (CLASS_SUPER_NAME (interface)) | |
7155 | interface = lookup_interface (CLASS_SUPER_NAME (interface)); | |
6c65299b | 7156 | } |
944fb799 | 7157 | |
d764a8e6 | 7158 | return NULL_TREE; |
6c65299b RS |
7159 | } |
7160 | ||
d764a8e6 IS |
7161 | /* This routine synthesizes a 'getter' method. This is only called |
7162 | for @synthesize properties. */ | |
7163 | static void | |
7164 | objc_synthesize_getter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property) | |
6c65299b | 7165 | { |
d764a8e6 IS |
7166 | location_t location = DECL_SOURCE_LOCATION (property); |
7167 | tree fn, decl; | |
7168 | tree body; | |
7169 | tree ret_val; | |
6c65299b | 7170 | |
d764a8e6 IS |
7171 | /* If user has implemented a getter with same name then do nothing. */ |
7172 | if (lookup_method (CLASS_NST_METHODS (objc_implementation_context), | |
7173 | PROPERTY_GETTER_NAME (property))) | |
7174 | return; | |
6c65299b | 7175 | |
d764a8e6 IS |
7176 | /* Find declaration of the property getter in the interface (or |
7177 | superclass, or protocol). There must be one. */ | |
7178 | decl = lookup_method_static (klass, PROPERTY_GETTER_NAME (property), 0); | |
6c65299b | 7179 | |
d764a8e6 IS |
7180 | /* If one not declared in the interface, this condition has already |
7181 | been reported as user error (because property was not declared in | |
7182 | the interface). */ | |
7183 | if (!decl) | |
7184 | return; | |
6c65299b | 7185 | |
d764a8e6 IS |
7186 | /* Adapt the 'decl'. Use the source location of the @synthesize |
7187 | statement for error messages. */ | |
7188 | decl = copy_node (decl); | |
7189 | DECL_SOURCE_LOCATION (decl) = location; | |
6c65299b | 7190 | |
a04a722b JM |
7191 | objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE, |
7192 | NULL_TREE); | |
d764a8e6 | 7193 | body = c_begin_compound_stmt (true); |
e31c7eec | 7194 | |
d764a8e6 IS |
7195 | /* Now we need to decide how we build the getter. There are three |
7196 | cases: | |
a1178b30 | 7197 | |
d764a8e6 IS |
7198 | for 'copy' or 'retain' properties we need to use the |
7199 | objc_getProperty() accessor helper which knows about retain and | |
7200 | copy. It supports both 'nonatomic' and 'atomic' access. | |
2debdb4f | 7201 | |
d764a8e6 IS |
7202 | for 'nonatomic, assign' properties we can access the instance |
7203 | variable directly. 'nonatomic' means we don't have to use locks, | |
7204 | and 'assign' means we don't have to worry about retain or copy. | |
7205 | If you combine the two, it means we can just access the instance | |
7206 | variable directly. | |
2debdb4f | 7207 | |
d764a8e6 IS |
7208 | for 'atomic, assign' properties we use objc_copyStruct() (for the |
7209 | next runtime) or objc_getPropertyStruct() (for the GNU runtime). */ | |
7210 | switch (PROPERTY_ASSIGN_SEMANTICS (property)) | |
a1178b30 | 7211 | { |
d764a8e6 IS |
7212 | case OBJC_PROPERTY_RETAIN: |
7213 | case OBJC_PROPERTY_COPY: | |
7214 | { | |
7215 | /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */ | |
7216 | tree cmd, ivar, offset, is_atomic; | |
7217 | cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)); | |
2debdb4f | 7218 | |
d764a8e6 IS |
7219 | /* Find the ivar to compute the offset. */ |
7220 | ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property)); | |
7221 | if (!ivar || is_private (ivar)) | |
7222 | { | |
7223 | /* This should never happen. */ | |
7224 | error_at (location, | |
7225 | "can not find instance variable associated with property"); | |
7226 | ret_val = error_mark_node; | |
7227 | break; | |
7228 | } | |
7229 | offset = byte_position (ivar); | |
2debdb4f | 7230 | |
d764a8e6 IS |
7231 | if (PROPERTY_NONATOMIC (property)) |
7232 | is_atomic = boolean_false_node; | |
7233 | else | |
7234 | is_atomic = boolean_true_node; | |
7235 | ||
7236 | ret_val = build_function_call | |
7237 | (location, | |
7238 | /* Function prototype. */ | |
7239 | objc_getProperty_decl, | |
7240 | /* Parameters. */ | |
7241 | tree_cons /* self */ | |
7242 | (NULL_TREE, self_decl, | |
7243 | tree_cons /* _cmd */ | |
7244 | (NULL_TREE, cmd, | |
7245 | tree_cons /* offset */ | |
7246 | (NULL_TREE, offset, | |
7247 | tree_cons /* is_atomic */ | |
7248 | (NULL_TREE, is_atomic, NULL_TREE))))); | |
7249 | } | |
7250 | break; | |
944fb799 | 7251 | case OBJC_PROPERTY_ASSIGN: |
d764a8e6 IS |
7252 | if (PROPERTY_NONATOMIC (property)) |
7253 | { | |
7254 | /* We build "return self->PROPERTY_IVAR_NAME;" */ | |
7255 | ret_val = objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)); | |
7256 | break; | |
7257 | } | |
7258 | else | |
7259 | { | |
7260 | /* We build | |
7261 | <property type> __objc_property_temp; | |
7262 | objc_getPropertyStruct (&__objc_property_temp, | |
7263 | &(self->PROPERTY_IVAR_NAME), | |
7264 | sizeof (type of self->PROPERTY_IVAR_NAME), | |
7265 | is_atomic, | |
7266 | false) | |
7267 | return __objc_property_temp; | |
7268 | ||
7269 | For the NeXT runtime, we need to use objc_copyStruct | |
7270 | instead of objc_getPropertyStruct. */ | |
7271 | tree objc_property_temp_decl, function_decl, function_call; | |
7272 | tree size_of, is_atomic; | |
7273 | ||
7274 | objc_property_temp_decl = objc_create_temporary_var (TREE_TYPE (property), "__objc_property_temp"); | |
7275 | DECL_SOURCE_LOCATION (objc_property_temp_decl) = location; | |
7276 | objc_property_temp_decl = lang_hooks.decls.pushdecl (objc_property_temp_decl); | |
7277 | ||
7278 | /* sizeof (ivar type). Since the ivar and the property have | |
7279 | the same type, there is no need to lookup the ivar. */ | |
7280 | size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property), | |
7281 | true /* is_sizeof */, | |
296674db | 7282 | false /* min_alignof */, |
d764a8e6 | 7283 | false /* complain */); |
944fb799 | 7284 | |
d764a8e6 IS |
7285 | if (PROPERTY_NONATOMIC (property)) |
7286 | is_atomic = boolean_false_node; | |
2debdb4f | 7287 | else |
d764a8e6 | 7288 | is_atomic = boolean_true_node; |
944fb799 | 7289 | |
d764a8e6 IS |
7290 | if (objc_copyStruct_decl) |
7291 | function_decl = objc_copyStruct_decl; | |
7292 | else | |
7293 | function_decl = objc_getPropertyStruct_decl; | |
7294 | ||
7295 | function_call = build_function_call | |
7296 | (location, | |
7297 | /* Function prototype. */ | |
7298 | function_decl, | |
7299 | /* Parameters. */ | |
7300 | tree_cons /* &__objc_property_temp_decl */ | |
7301 | /* Warning: note that using build_fold_addr_expr_loc() | |
7302 | here causes invalid code to be generated. */ | |
7303 | (NULL_TREE, build_unary_op (location, ADDR_EXPR, objc_property_temp_decl, 0), | |
7304 | tree_cons /* &(self->PROPERTY_IVAR_NAME); */ | |
944fb799 MS |
7305 | (NULL_TREE, build_fold_addr_expr_loc (location, |
7306 | objc_lookup_ivar | |
d764a8e6 IS |
7307 | (NULL_TREE, PROPERTY_IVAR_NAME (property))), |
7308 | tree_cons /* sizeof (PROPERTY_IVAR) */ | |
7309 | (NULL_TREE, size_of, | |
7310 | tree_cons /* is_atomic */ | |
7311 | (NULL_TREE, is_atomic, | |
7312 | /* TODO: This is currently ignored by the GNU | |
7313 | runtime, but what about the next one ? */ | |
7314 | tree_cons /* has_strong */ | |
7315 | (NULL_TREE, boolean_true_node, NULL_TREE)))))); | |
7316 | ||
7317 | add_stmt (function_call); | |
7318 | ||
7319 | ret_val = objc_property_temp_decl; | |
2debdb4f | 7320 | } |
d764a8e6 IS |
7321 | break; |
7322 | default: | |
7323 | gcc_unreachable (); | |
2debdb4f NP |
7324 | } |
7325 | ||
d764a8e6 | 7326 | gcc_assert (ret_val); |
a1178b30 | 7327 | |
d764a8e6 IS |
7328 | #ifdef OBJCPLUS |
7329 | finish_return_stmt (ret_val); | |
7330 | #else | |
7331 | c_finish_return (location, ret_val, NULL_TREE); | |
7332 | #endif | |
7333 | ||
7334 | add_stmt (c_end_compound_stmt (location, body, true)); | |
7335 | fn = current_function_decl; | |
7336 | #ifdef OBJCPLUS | |
7337 | finish_function (); | |
7338 | #endif | |
7339 | objc_finish_method_definition (fn); | |
a1178b30 IS |
7340 | } |
7341 | ||
d764a8e6 | 7342 | /* This routine synthesizes a 'setter' method. */ |
af35aeb2 | 7343 | |
d764a8e6 IS |
7344 | static void |
7345 | objc_synthesize_setter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property) | |
6c65299b | 7346 | { |
d764a8e6 IS |
7347 | location_t location = DECL_SOURCE_LOCATION (property); |
7348 | tree fn, decl; | |
7349 | tree body; | |
7350 | tree new_value, statement; | |
6c65299b | 7351 | |
d764a8e6 IS |
7352 | /* If user has implemented a setter with same name then do nothing. */ |
7353 | if (lookup_method (CLASS_NST_METHODS (objc_implementation_context), | |
7354 | PROPERTY_SETTER_NAME (property))) | |
7355 | return; | |
6c65299b | 7356 | |
d764a8e6 IS |
7357 | /* Find declaration of the property setter in the interface (or |
7358 | superclass, or protocol). There must be one. */ | |
7359 | decl = lookup_method_static (klass, PROPERTY_SETTER_NAME (property), 0); | |
6c65299b | 7360 | |
d764a8e6 IS |
7361 | /* If one not declared in the interface, this condition has already |
7362 | been reported as user error (because property was not declared in | |
7363 | the interface). */ | |
7364 | if (!decl) | |
7365 | return; | |
264fa2db | 7366 | |
d764a8e6 IS |
7367 | /* Adapt the 'decl'. Use the source location of the @synthesize |
7368 | statement for error messages. */ | |
7369 | decl = copy_node (decl); | |
7370 | DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (property); | |
6408ef33 | 7371 | |
a04a722b JM |
7372 | objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE, |
7373 | NULL_TREE); | |
9eb892ec | 7374 | |
d764a8e6 | 7375 | body = c_begin_compound_stmt (true); |
6c65299b | 7376 | |
d764a8e6 IS |
7377 | /* The 'new_value' is the only argument to the method, which is the |
7378 | 3rd argument of the function, after self and _cmd. We use twice | |
7379 | TREE_CHAIN to move forward two arguments. */ | |
7380 | new_value = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl))); | |
7381 | ||
7382 | /* This would presumably happen if the user has specified a | |
7383 | prototype for the setter that does not have an argument! */ | |
7384 | if (new_value == NULL_TREE) | |
6c65299b | 7385 | { |
d764a8e6 IS |
7386 | /* TODO: This should be caught much earlier than this. */ |
7387 | error_at (DECL_SOURCE_LOCATION (decl), "invalid setter, it must have one argument"); | |
7388 | /* Try to recover somehow. */ | |
7389 | new_value = error_mark_node; | |
7390 | } | |
6408ef33 | 7391 | |
d764a8e6 IS |
7392 | /* Now we need to decide how we build the setter. There are three |
7393 | cases: | |
26877873 | 7394 | |
d764a8e6 IS |
7395 | for 'copy' or 'retain' properties we need to use the |
7396 | objc_setProperty() accessor helper which knows about retain and | |
7397 | copy. It supports both 'nonatomic' and 'atomic' access. | |
6408ef33 | 7398 | |
d764a8e6 IS |
7399 | for 'nonatomic, assign' properties we can access the instance |
7400 | variable directly. 'nonatomic' means we don't have to use locks, | |
7401 | and 'assign' means we don't have to worry about retain or copy. | |
7402 | If you combine the two, it means we can just access the instance | |
7403 | variable directly. | |
6c65299b | 7404 | |
d764a8e6 IS |
7405 | for 'atomic, assign' properties we use objc_copyStruct() (for the |
7406 | next runtime) or objc_setPropertyStruct() (for the GNU runtime). */ | |
7407 | switch (PROPERTY_ASSIGN_SEMANTICS (property)) | |
6c65299b | 7408 | { |
d764a8e6 IS |
7409 | case OBJC_PROPERTY_RETAIN: |
7410 | case OBJC_PROPERTY_COPY: | |
7411 | { | |
7412 | /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */ | |
7413 | tree cmd, ivar, offset, is_atomic, should_copy; | |
7414 | cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)); | |
6c65299b | 7415 | |
d764a8e6 IS |
7416 | /* Find the ivar to compute the offset. */ |
7417 | ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property)); | |
7418 | if (!ivar || is_private (ivar)) | |
7419 | { | |
7420 | error_at (location, | |
7421 | "can not find instance variable associated with property"); | |
7422 | statement = error_mark_node; | |
7423 | break; | |
7424 | } | |
7425 | offset = byte_position (ivar); | |
6c65299b | 7426 | |
d764a8e6 IS |
7427 | if (PROPERTY_NONATOMIC (property)) |
7428 | is_atomic = boolean_false_node; | |
7429 | else | |
7430 | is_atomic = boolean_true_node; | |
944fb799 | 7431 | |
d764a8e6 IS |
7432 | if (PROPERTY_ASSIGN_SEMANTICS (property) == OBJC_PROPERTY_COPY) |
7433 | should_copy = boolean_true_node; | |
7434 | else | |
7435 | should_copy = boolean_false_node; | |
38b9c8c3 | 7436 | |
d764a8e6 IS |
7437 | statement = build_function_call |
7438 | (location, | |
7439 | /* Function prototype. */ | |
7440 | objc_setProperty_decl, | |
7441 | /* Parameters. */ | |
7442 | tree_cons /* self */ | |
7443 | (NULL_TREE, self_decl, | |
7444 | tree_cons /* _cmd */ | |
7445 | (NULL_TREE, cmd, | |
7446 | tree_cons /* offset */ | |
7447 | (NULL_TREE, offset, | |
7448 | tree_cons /* new_value */ | |
7449 | (NULL_TREE, new_value, | |
7450 | tree_cons /* is_atomic */ | |
944fb799 | 7451 | (NULL_TREE, is_atomic, |
d764a8e6 IS |
7452 | tree_cons /* should_copy */ |
7453 | (NULL_TREE, should_copy, NULL_TREE))))))); | |
7454 | } | |
7455 | break; | |
944fb799 | 7456 | case OBJC_PROPERTY_ASSIGN: |
d764a8e6 IS |
7457 | if (PROPERTY_NONATOMIC (property)) |
7458 | { | |
7459 | /* We build "self->PROPERTY_IVAR_NAME = new_value;" */ | |
7460 | statement = build_modify_expr | |
7461 | (location, | |
7462 | objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)), | |
944fb799 | 7463 | NULL_TREE, NOP_EXPR, |
d764a8e6 IS |
7464 | location, new_value, NULL_TREE); |
7465 | break; | |
7466 | } | |
7467 | else | |
7468 | { | |
7469 | /* We build | |
7470 | objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME), | |
7471 | &new_value, | |
7472 | sizeof (type of self->PROPERTY_IVAR_NAME), | |
7473 | is_atomic, | |
7474 | false) | |
38b9c8c3 | 7475 | |
d764a8e6 IS |
7476 | For the NeXT runtime, we need to use objc_copyStruct |
7477 | instead of objc_getPropertyStruct. */ | |
7478 | tree function_decl, size_of, is_atomic; | |
52daca75 | 7479 | |
d764a8e6 IS |
7480 | /* sizeof (ivar type). Since the ivar and the property have |
7481 | the same type, there is no need to lookup the ivar. */ | |
7482 | size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property), | |
7483 | true /* is_sizeof */, | |
296674db | 7484 | false /* min_alignof */, |
d764a8e6 | 7485 | false /* complain */); |
944fb799 | 7486 | |
d764a8e6 IS |
7487 | if (PROPERTY_NONATOMIC (property)) |
7488 | is_atomic = boolean_false_node; | |
52daca75 | 7489 | else |
d764a8e6 | 7490 | is_atomic = boolean_true_node; |
944fb799 | 7491 | |
d764a8e6 IS |
7492 | if (objc_copyStruct_decl) |
7493 | function_decl = objc_copyStruct_decl; | |
7494 | else | |
7495 | function_decl = objc_setPropertyStruct_decl; | |
52daca75 | 7496 | |
944fb799 | 7497 | statement = build_function_call |
d764a8e6 IS |
7498 | (location, |
7499 | /* Function prototype. */ | |
7500 | function_decl, | |
7501 | /* Parameters. */ | |
7502 | tree_cons /* &(self->PROPERTY_IVAR_NAME); */ | |
944fb799 MS |
7503 | (NULL_TREE, build_fold_addr_expr_loc (location, |
7504 | objc_lookup_ivar | |
d764a8e6 IS |
7505 | (NULL_TREE, PROPERTY_IVAR_NAME (property))), |
7506 | tree_cons /* &new_value */ | |
7507 | (NULL_TREE, build_fold_addr_expr_loc (location, new_value), | |
7508 | tree_cons /* sizeof (PROPERTY_IVAR) */ | |
7509 | (NULL_TREE, size_of, | |
7510 | tree_cons /* is_atomic */ | |
7511 | (NULL_TREE, is_atomic, | |
7512 | /* TODO: This is currently ignored by the GNU | |
7513 | runtime, but what about the next one ? */ | |
7514 | tree_cons /* has_strong */ | |
7515 | (NULL_TREE, boolean_true_node, NULL_TREE)))))); | |
7516 | } | |
7517 | break; | |
7518 | default: | |
7519 | gcc_unreachable (); | |
6c65299b | 7520 | } |
d764a8e6 | 7521 | gcc_assert (statement); |
6c65299b | 7522 | |
944fb799 | 7523 | add_stmt (statement); |
d764a8e6 IS |
7524 | add_stmt (c_end_compound_stmt (location, body, true)); |
7525 | fn = current_function_decl; | |
7526 | #ifdef OBJCPLUS | |
7527 | finish_function (); | |
7528 | #endif | |
7529 | objc_finish_method_definition (fn); | |
7530 | } | |
59ce3fc8 | 7531 | |
d764a8e6 IS |
7532 | /* This function is a sub-routine of objc_add_synthesize_declaration. |
7533 | It is called for each property to synthesize once we have | |
7534 | determined that the context is Ok. */ | |
7535 | static void | |
7536 | objc_add_synthesize_declaration_for_property (location_t location, tree interface, | |
7537 | tree property_name, tree ivar_name) | |
6c65299b | 7538 | { |
d764a8e6 IS |
7539 | /* Find the @property declaration. */ |
7540 | tree property; | |
7541 | tree x; | |
f2e6e530 | 7542 | |
d764a8e6 IS |
7543 | /* Check that synthesize or dynamic has not already been used for |
7544 | the same property. */ | |
7545 | for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property)) | |
7546 | if (PROPERTY_NAME (property) == property_name) | |
7547 | { | |
7548 | location_t original_location = DECL_SOURCE_LOCATION (property); | |
944fb799 | 7549 | |
d764a8e6 | 7550 | if (PROPERTY_DYNAMIC (property)) |
944fb799 | 7551 | error_at (location, "property %qs already specified in %<@dynamic%>", |
d764a8e6 IS |
7552 | IDENTIFIER_POINTER (property_name)); |
7553 | else | |
944fb799 | 7554 | error_at (location, "property %qs already specified in %<@synthesize%>", |
d764a8e6 | 7555 | IDENTIFIER_POINTER (property_name)); |
944fb799 | 7556 | |
d764a8e6 IS |
7557 | if (original_location != UNKNOWN_LOCATION) |
7558 | inform (original_location, "originally specified here"); | |
7559 | return; | |
7560 | } | |
011d50d9 | 7561 | |
d764a8e6 IS |
7562 | /* Check that the property is declared in the interface. It could |
7563 | also be declared in a superclass or protocol. */ | |
7564 | property = lookup_property (interface, property_name); | |
264fa2db | 7565 | |
d764a8e6 | 7566 | if (!property) |
264fa2db | 7567 | { |
944fb799 | 7568 | error_at (location, "no declaration of property %qs found in the interface", |
d764a8e6 IS |
7569 | IDENTIFIER_POINTER (property_name)); |
7570 | return; | |
7571 | } | |
7572 | else | |
7573 | { | |
7574 | /* We have to copy the property, because we want to chain it to | |
7575 | the implementation context, and we want to store the source | |
7576 | location of the @synthesize, not of the original | |
7577 | @property. */ | |
7578 | property = copy_node (property); | |
7579 | DECL_SOURCE_LOCATION (property) = location; | |
6c65299b | 7580 | } |
6c65299b | 7581 | |
d764a8e6 IS |
7582 | /* Determine PROPERTY_IVAR_NAME. */ |
7583 | if (ivar_name == NULL_TREE) | |
7584 | ivar_name = property_name; | |
6c65299b | 7585 | |
d764a8e6 IS |
7586 | /* Check that the instance variable exists. You can only use an |
7587 | instance variable from the same class, not one from the | |
7588 | superclass (this makes sense as it allows us to check that an | |
7589 | instance variable is only used in one synthesized property). */ | |
7590 | { | |
7591 | tree ivar = is_ivar (CLASS_IVARS (interface), ivar_name); | |
7592 | tree type_of_ivar; | |
7593 | if (!ivar) | |
7594 | { | |
944fb799 | 7595 | error_at (location, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar", |
d764a8e6 IS |
7596 | IDENTIFIER_POINTER (property_name)); |
7597 | return; | |
7598 | } | |
6c65299b | 7599 | |
d764a8e6 IS |
7600 | if (DECL_BIT_FIELD_TYPE (ivar)) |
7601 | type_of_ivar = DECL_BIT_FIELD_TYPE (ivar); | |
7602 | else | |
7603 | type_of_ivar = TREE_TYPE (ivar); | |
944fb799 | 7604 | |
d764a8e6 IS |
7605 | /* If the instance variable has a different C type, we throw an error ... */ |
7606 | if (!comptypes (TREE_TYPE (property), type_of_ivar) | |
7607 | /* ... unless the property is readonly, in which case we allow | |
7608 | the instance variable to be more specialized (this means we | |
7609 | can generate the getter all right and it works). */ | |
7610 | && (!PROPERTY_READONLY (property) | |
7611 | || !objc_compare_types (TREE_TYPE (property), | |
7612 | type_of_ivar, -5, NULL_TREE))) | |
7613 | { | |
7614 | location_t original_location = DECL_SOURCE_LOCATION (ivar); | |
944fb799 | 7615 | |
d764a8e6 IS |
7616 | error_at (location, "property %qs is using instance variable %qs of incompatible type", |
7617 | IDENTIFIER_POINTER (property_name), | |
7618 | IDENTIFIER_POINTER (ivar_name)); | |
944fb799 | 7619 | |
d764a8e6 IS |
7620 | if (original_location != UNKNOWN_LOCATION) |
7621 | inform (original_location, "originally specified here"); | |
7622 | } | |
e31c7eec | 7623 | |
d764a8e6 IS |
7624 | /* If the instance variable is a bitfield, the property must be |
7625 | 'assign', 'nonatomic' because the runtime getter/setter helper | |
7626 | do not work with bitfield instance variables. */ | |
7627 | if (DECL_BIT_FIELD_TYPE (ivar)) | |
7628 | { | |
7629 | /* If there is an error, we return and not generate any | |
7630 | getter/setter because trying to set up the runtime | |
7631 | getter/setter helper calls with bitfields is at high risk | |
7632 | of ICE. */ | |
6c65299b | 7633 | |
d764a8e6 IS |
7634 | if (PROPERTY_ASSIGN_SEMANTICS (property) != OBJC_PROPERTY_ASSIGN) |
7635 | { | |
7636 | location_t original_location = DECL_SOURCE_LOCATION (ivar); | |
944fb799 | 7637 | |
d764a8e6 IS |
7638 | error_at (location, "'assign' property %qs is using bit-field instance variable %qs", |
7639 | IDENTIFIER_POINTER (property_name), | |
7640 | IDENTIFIER_POINTER (ivar_name)); | |
944fb799 | 7641 | |
d764a8e6 IS |
7642 | if (original_location != UNKNOWN_LOCATION) |
7643 | inform (original_location, "originally specified here"); | |
7644 | return; | |
7645 | } | |
6c65299b | 7646 | |
d764a8e6 IS |
7647 | if (!PROPERTY_NONATOMIC (property)) |
7648 | { | |
7649 | location_t original_location = DECL_SOURCE_LOCATION (ivar); | |
944fb799 | 7650 | |
d764a8e6 IS |
7651 | error_at (location, "'atomic' property %qs is using bit-field instance variable %qs", |
7652 | IDENTIFIER_POINTER (property_name), | |
7653 | IDENTIFIER_POINTER (ivar_name)); | |
944fb799 | 7654 | |
d764a8e6 IS |
7655 | if (original_location != UNKNOWN_LOCATION) |
7656 | inform (original_location, "originally specified here"); | |
7657 | return; | |
7658 | } | |
7659 | } | |
7660 | } | |
6c65299b | 7661 | |
d764a8e6 IS |
7662 | /* Check that no other property is using the same instance |
7663 | variable. */ | |
7664 | for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x)) | |
7665 | if (PROPERTY_IVAR_NAME (x) == ivar_name) | |
7666 | { | |
7667 | location_t original_location = DECL_SOURCE_LOCATION (x); | |
944fb799 | 7668 | |
d764a8e6 IS |
7669 | error_at (location, "property %qs is using the same instance variable as property %qs", |
7670 | IDENTIFIER_POINTER (property_name), | |
7671 | IDENTIFIER_POINTER (PROPERTY_NAME (x))); | |
944fb799 | 7672 | |
d764a8e6 IS |
7673 | if (original_location != UNKNOWN_LOCATION) |
7674 | inform (original_location, "originally specified here"); | |
944fb799 | 7675 | |
d764a8e6 IS |
7676 | /* We keep going on. This won't cause the compiler to fail; |
7677 | the failure would most likely be at runtime. */ | |
7678 | } | |
6c65299b | 7679 | |
d764a8e6 IS |
7680 | /* Note that a @synthesize (and only a @synthesize) always sets |
7681 | PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a | |
7682 | @synthesize by that. */ | |
7683 | PROPERTY_IVAR_NAME (property) = ivar_name; | |
944fb799 | 7684 | |
d764a8e6 IS |
7685 | /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the |
7686 | original declaration; they are always set (with the exception of | |
7687 | PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */ | |
6c65299b | 7688 | |
d764a8e6 IS |
7689 | /* Add the property to the list of properties for current implementation. */ |
7690 | TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context); | |
7691 | IMPL_PROPERTY_DECL (objc_implementation_context) = property; | |
6c65299b | 7692 | |
d764a8e6 IS |
7693 | /* Note how we don't actually synthesize the getter/setter here; it |
7694 | would be very natural, but we may miss the fact that the user has | |
7695 | implemented his own getter/setter later on in the @implementation | |
7696 | (in which case we shouldn't generate getter/setter). We wait | |
7697 | until we have parsed it all before generating the code. */ | |
7698 | } | |
6c65299b | 7699 | |
d764a8e6 IS |
7700 | /* This function is called by the parser after a @synthesize |
7701 | expression is parsed. 'location' is the location of the | |
7702 | @synthesize expression, and 'property_and_ivar_list' is a chained | |
7703 | list of the property and ivar names. */ | |
7704 | void | |
7705 | objc_add_synthesize_declaration (location_t location, tree property_and_ivar_list) | |
7706 | { | |
7707 | tree interface, chain; | |
264fa2db | 7708 | |
d764a8e6 IS |
7709 | if (flag_objc1_only) |
7710 | error_at (input_location, "%<@synthesize%> is not available in Objective-C 1.0"); | |
8607f1bc | 7711 | |
d764a8e6 IS |
7712 | if (property_and_ivar_list == error_mark_node) |
7713 | return; | |
81ff723b | 7714 | |
d764a8e6 IS |
7715 | if (!objc_implementation_context) |
7716 | { | |
7717 | /* We can get here only in Objective-C; the Objective-C++ parser | |
7718 | detects the problem while parsing, outputs the error | |
7719 | "misplaced '@synthesize' Objective-C++ construct" and skips | |
7720 | the declaration. */ | |
7721 | error_at (location, "%<@synthesize%> not in @implementation context"); | |
7722 | return; | |
7723 | } | |
81ff723b | 7724 | |
d764a8e6 IS |
7725 | if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE) |
7726 | { | |
7727 | error_at (location, "%<@synthesize%> can not be used in categories"); | |
7728 | return; | |
7729 | } | |
64ee9490 | 7730 | |
d764a8e6 IS |
7731 | interface = lookup_interface (CLASS_NAME (objc_implementation_context)); |
7732 | if (!interface) | |
81ff723b | 7733 | { |
d764a8e6 | 7734 | /* I can't see how this could happen, but it is good as a safety check. */ |
944fb799 | 7735 | error_at (location, |
d764a8e6 IS |
7736 | "%<@synthesize%> requires the @interface of the class to be available"); |
7737 | return; | |
81ff723b | 7738 | } |
264fa2db | 7739 | |
d764a8e6 IS |
7740 | /* Now, iterate over the properties and do each of them. */ |
7741 | for (chain = property_and_ivar_list; chain; chain = TREE_CHAIN (chain)) | |
7742 | { | |
944fb799 | 7743 | objc_add_synthesize_declaration_for_property (location, interface, TREE_VALUE (chain), |
d764a8e6 IS |
7744 | TREE_PURPOSE (chain)); |
7745 | } | |
264fa2db ZL |
7746 | } |
7747 | ||
d764a8e6 IS |
7748 | /* This function is a sub-routine of objc_add_dynamic_declaration. It |
7749 | is called for each property to mark as dynamic once we have | |
7750 | determined that the context is Ok. */ | |
7751 | static void | |
7752 | objc_add_dynamic_declaration_for_property (location_t location, tree interface, | |
7753 | tree property_name) | |
011d50d9 | 7754 | { |
d764a8e6 IS |
7755 | /* Find the @property declaration. */ |
7756 | tree property; | |
668ea4b1 | 7757 | |
d764a8e6 IS |
7758 | /* Check that synthesize or dynamic has not already been used for |
7759 | the same property. */ | |
7760 | for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property)) | |
7761 | if (PROPERTY_NAME (property) == property_name) | |
7762 | { | |
7763 | location_t original_location = DECL_SOURCE_LOCATION (property); | |
944fb799 | 7764 | |
d764a8e6 | 7765 | if (PROPERTY_DYNAMIC (property)) |
944fb799 | 7766 | error_at (location, "property %qs already specified in %<@dynamic%>", |
d764a8e6 IS |
7767 | IDENTIFIER_POINTER (property_name)); |
7768 | else | |
7769 | error_at (location, "property %qs already specified in %<@synthesize%>", | |
7770 | IDENTIFIER_POINTER (property_name)); | |
264fa2db | 7771 | |
d764a8e6 IS |
7772 | if (original_location != UNKNOWN_LOCATION) |
7773 | inform (original_location, "originally specified here"); | |
7774 | return; | |
7775 | } | |
38b9c8c3 | 7776 | |
d764a8e6 IS |
7777 | /* Check that the property is declared in the interface. It could |
7778 | also be declared in a superclass or protocol. */ | |
7779 | property = lookup_property (interface, property_name); | |
264fa2db | 7780 | |
d764a8e6 | 7781 | if (!property) |
264fa2db | 7782 | { |
d764a8e6 IS |
7783 | error_at (location, "no declaration of property %qs found in the interface", |
7784 | IDENTIFIER_POINTER (property_name)); | |
7785 | return; | |
8607f1bc | 7786 | } |
d764a8e6 | 7787 | else |
6c65299b | 7788 | { |
d764a8e6 IS |
7789 | /* We have to copy the property, because we want to chain it to |
7790 | the implementation context, and we want to store the source | |
7791 | location of the @synthesize, not of the original | |
7792 | @property. */ | |
7793 | property = copy_node (property); | |
7794 | DECL_SOURCE_LOCATION (property) = location; | |
7795 | } | |
e31c7eec | 7796 | |
d764a8e6 IS |
7797 | /* Note that a @dynamic (and only a @dynamic) always sets |
7798 | PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that. | |
7799 | (actually, as explained above, PROPERTY_DECL generated by | |
7800 | @property and associated with a @dynamic property are also marked | |
7801 | as PROPERTY_DYNAMIC). */ | |
7802 | PROPERTY_DYNAMIC (property) = 1; | |
3a3589b4 | 7803 | |
d764a8e6 IS |
7804 | /* Add the property to the list of properties for current implementation. */ |
7805 | TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context); | |
7806 | IMPL_PROPERTY_DECL (objc_implementation_context) = property; | |
7807 | } | |
3a3589b4 | 7808 | |
d764a8e6 IS |
7809 | /* This function is called by the parser after a @dynamic expression |
7810 | is parsed. 'location' is the location of the @dynamic expression, | |
7811 | and 'property_list' is a chained list of all the property | |
7812 | names. */ | |
7813 | void | |
7814 | objc_add_dynamic_declaration (location_t location, tree property_list) | |
7815 | { | |
7816 | tree interface, chain; | |
264fa2db | 7817 | |
d764a8e6 IS |
7818 | if (flag_objc1_only) |
7819 | error_at (input_location, "%<@dynamic%> is not available in Objective-C 1.0"); | |
e31c7eec | 7820 | |
d764a8e6 IS |
7821 | if (property_list == error_mark_node) |
7822 | return; | |
264fa2db | 7823 | |
d764a8e6 IS |
7824 | if (!objc_implementation_context) |
7825 | { | |
7826 | /* We can get here only in Objective-C; the Objective-C++ parser | |
7827 | detects the problem while parsing, outputs the error | |
7828 | "misplaced '@dynamic' Objective-C++ construct" and skips the | |
7829 | declaration. */ | |
7830 | error_at (location, "%<@dynamic%> not in @implementation context"); | |
7831 | return; | |
64ee9490 | 7832 | } |
e31c7eec | 7833 | |
d764a8e6 IS |
7834 | /* @dynamic is allowed in categories. */ |
7835 | switch (TREE_CODE (objc_implementation_context)) | |
3a3589b4 | 7836 | { |
d764a8e6 IS |
7837 | case CLASS_IMPLEMENTATION_TYPE: |
7838 | interface = lookup_interface (CLASS_NAME (objc_implementation_context)); | |
7839 | break; | |
7840 | case CATEGORY_IMPLEMENTATION_TYPE: | |
944fb799 | 7841 | interface = lookup_category (implementation_template, |
d764a8e6 IS |
7842 | CLASS_SUPER_NAME (objc_implementation_context)); |
7843 | break; | |
7844 | default: | |
7845 | gcc_unreachable (); | |
7846 | } | |
3a3589b4 | 7847 | |
d764a8e6 IS |
7848 | if (!interface) |
7849 | { | |
7850 | /* I can't see how this could happen, but it is good as a safety check. */ | |
7851 | error_at (location, | |
7852 | "%<@dynamic%> requires the @interface of the class to be available"); | |
7853 | return; | |
3a3589b4 ZL |
7854 | } |
7855 | ||
d764a8e6 IS |
7856 | /* Now, iterate over the properties and do each of them. */ |
7857 | for (chain = property_list; chain; chain = TREE_CHAIN (chain)) | |
6c65299b | 7858 | { |
d764a8e6 IS |
7859 | objc_add_dynamic_declaration_for_property (location, interface, TREE_VALUE (chain)); |
7860 | } | |
7861 | } | |
011d50d9 | 7862 | |
944fb799 | 7863 | /* Main routine to generate code/data for all the property information for |
d764a8e6 IS |
7864 | current implementation (class or category). CLASS is the interface where |
7865 | ivars are declared. CLASS_METHODS is where methods are found which | |
7866 | could be a class or a category depending on whether we are implementing | |
7867 | property of a class or a category. */ | |
3a3589b4 | 7868 | |
d764a8e6 IS |
7869 | static void |
7870 | objc_gen_property_data (tree klass, tree class_methods) | |
7871 | { | |
7872 | tree x; | |
2debdb4f | 7873 | |
d764a8e6 IS |
7874 | for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x)) |
7875 | { | |
7876 | /* @dynamic property - nothing to check or synthesize. */ | |
7877 | if (PROPERTY_DYNAMIC (x)) | |
7878 | continue; | |
944fb799 | 7879 | |
d764a8e6 IS |
7880 | /* @synthesize property - need to synthesize the accessors. */ |
7881 | if (PROPERTY_IVAR_NAME (x)) | |
b4f588c4 | 7882 | { |
d764a8e6 | 7883 | objc_synthesize_getter (klass, class_methods, x); |
944fb799 | 7884 | |
d764a8e6 IS |
7885 | if (PROPERTY_READONLY (x) == 0) |
7886 | objc_synthesize_setter (klass, class_methods, x); | |
7887 | ||
7888 | continue; | |
b4f588c4 | 7889 | } |
2debdb4f | 7890 | |
d764a8e6 IS |
7891 | gcc_unreachable (); |
7892 | } | |
7893 | } | |
6c65299b | 7894 | |
d764a8e6 | 7895 | /* This is called once we see the "@end" in an interface/implementation. */ |
6c65299b | 7896 | |
d764a8e6 IS |
7897 | static void |
7898 | finish_class (tree klass) | |
7899 | { | |
7900 | switch (TREE_CODE (klass)) | |
7901 | { | |
7902 | case CLASS_IMPLEMENTATION_TYPE: | |
7903 | { | |
7904 | /* All metadata generation is done in runtime.generate_metadata(). */ | |
944fb799 | 7905 | |
d764a8e6 IS |
7906 | /* Generate what needed for property; setters, getters, etc. */ |
7907 | objc_gen_property_data (implementation_template, implementation_template); | |
5d0f30f7 | 7908 | |
d764a8e6 IS |
7909 | if (implementation_template != objc_implementation_context) |
7910 | { | |
7911 | /* Ensure that all method listed in the interface contain bodies. */ | |
7912 | check_methods (CLASS_CLS_METHODS (implementation_template), | |
7913 | objc_implementation_context, '+'); | |
7914 | check_methods (CLASS_NST_METHODS (implementation_template), | |
7915 | objc_implementation_context, '-'); | |
5d0f30f7 | 7916 | |
d764a8e6 IS |
7917 | if (CLASS_PROTOCOL_LIST (implementation_template)) |
7918 | check_protocols (CLASS_PROTOCOL_LIST (implementation_template), | |
7919 | "class", | |
7920 | CLASS_NAME (objc_implementation_context)); | |
7921 | } | |
7922 | break; | |
7923 | } | |
7924 | case CATEGORY_IMPLEMENTATION_TYPE: | |
7925 | { | |
7926 | tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass)); | |
944fb799 | 7927 | |
d764a8e6 IS |
7928 | if (category) |
7929 | { | |
7930 | /* Generate what needed for property; setters, getters, etc. */ | |
7931 | objc_gen_property_data (implementation_template, category); | |
6c65299b | 7932 | |
d764a8e6 IS |
7933 | /* Ensure all method listed in the interface contain bodies. */ |
7934 | check_methods (CLASS_CLS_METHODS (category), | |
7935 | objc_implementation_context, '+'); | |
7936 | check_methods (CLASS_NST_METHODS (category), | |
7937 | objc_implementation_context, '-'); | |
944fb799 | 7938 | |
d764a8e6 IS |
7939 | if (CLASS_PROTOCOL_LIST (category)) |
7940 | check_protocols (CLASS_PROTOCOL_LIST (category), | |
7941 | "category", | |
7942 | CLASS_SUPER_NAME (objc_implementation_context)); | |
7943 | } | |
7944 | break; | |
7945 | } | |
7946 | case CLASS_INTERFACE_TYPE: | |
7947 | case CATEGORY_INTERFACE_TYPE: | |
7948 | case PROTOCOL_INTERFACE_TYPE: | |
7949 | { | |
7950 | /* Process properties of the class. */ | |
7951 | tree x; | |
7952 | for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x)) | |
7953 | { | |
7954 | /* Now we check that the appropriate getter is declared, | |
7955 | and if not, we declare one ourselves. */ | |
7956 | tree getter_decl = lookup_method (CLASS_NST_METHODS (klass), | |
7957 | PROPERTY_GETTER_NAME (x)); | |
944fb799 | 7958 | |
d764a8e6 IS |
7959 | if (getter_decl) |
7960 | { | |
7961 | /* TODO: Check that the declaration is consistent with the property. */ | |
7962 | ; | |
7963 | } | |
7964 | else | |
7965 | { | |
7966 | /* Generate an instance method declaration for the | |
7967 | getter; for example "- (id) name;". In general it | |
7968 | will be of the form | |
7969 | -(type)property_getter_name; */ | |
7970 | tree rettype = build_tree_list (NULL_TREE, TREE_TYPE (x)); | |
944fb799 MS |
7971 | getter_decl = build_method_decl (INSTANCE_METHOD_DECL, |
7972 | rettype, PROPERTY_GETTER_NAME (x), | |
d764a8e6 IS |
7973 | NULL_TREE, false); |
7974 | if (PROPERTY_OPTIONAL (x)) | |
7975 | objc_add_method (objc_interface_context, getter_decl, false, true); | |
7976 | else | |
7977 | objc_add_method (objc_interface_context, getter_decl, false, false); | |
7978 | TREE_DEPRECATED (getter_decl) = TREE_DEPRECATED (x); | |
7979 | METHOD_PROPERTY_CONTEXT (getter_decl) = x; | |
7980 | } | |
6c65299b | 7981 | |
d764a8e6 IS |
7982 | if (PROPERTY_READONLY (x) == 0) |
7983 | { | |
7984 | /* Now we check that the appropriate setter is declared, | |
7985 | and if not, we declare on ourselves. */ | |
944fb799 | 7986 | tree setter_decl = lookup_method (CLASS_NST_METHODS (klass), |
d764a8e6 | 7987 | PROPERTY_SETTER_NAME (x)); |
944fb799 | 7988 | |
d764a8e6 IS |
7989 | if (setter_decl) |
7990 | { | |
7991 | /* TODO: Check that the declaration is consistent with the property. */ | |
7992 | ; | |
7993 | } | |
7994 | else | |
7995 | { | |
7996 | /* The setter name is something like 'setName:'. | |
7997 | We need the substring 'setName' to build the | |
7998 | method declaration due to how the declaration | |
7999 | works. TODO: build_method_decl() will then | |
8000 | generate back 'setName:' from 'setName'; it | |
8001 | would be more efficient to hook into there. */ | |
8002 | const char *full_setter_name = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x)); | |
8003 | size_t length = strlen (full_setter_name); | |
8004 | char *setter_name = (char *) alloca (length); | |
8005 | tree ret_type, selector, arg_type, arg_name; | |
944fb799 | 8006 | |
d764a8e6 IS |
8007 | strcpy (setter_name, full_setter_name); |
8008 | setter_name[length - 1] = '\0'; | |
8009 | ret_type = build_tree_list (NULL_TREE, void_type_node); | |
8010 | arg_type = build_tree_list (NULL_TREE, TREE_TYPE (x)); | |
8011 | arg_name = get_identifier ("_value"); | |
8012 | selector = objc_build_keyword_decl (get_identifier (setter_name), | |
8013 | arg_type, arg_name, NULL); | |
944fb799 | 8014 | setter_decl = build_method_decl (INSTANCE_METHOD_DECL, |
d764a8e6 IS |
8015 | ret_type, selector, |
8016 | build_tree_list (NULL_TREE, NULL_TREE), | |
8017 | false); | |
8018 | if (PROPERTY_OPTIONAL (x)) | |
8019 | objc_add_method (objc_interface_context, setter_decl, false, true); | |
8020 | else | |
8021 | objc_add_method (objc_interface_context, setter_decl, false, false); | |
8022 | TREE_DEPRECATED (setter_decl) = TREE_DEPRECATED (x); | |
8023 | METHOD_PROPERTY_CONTEXT (setter_decl) = x; | |
944fb799 | 8024 | } |
d764a8e6 IS |
8025 | } |
8026 | } | |
8027 | break; | |
8028 | } | |
8029 | default: | |
8030 | gcc_unreachable (); | |
8031 | break; | |
8032 | } | |
6c65299b | 8033 | } |
6c65299b RS |
8034 | |
8035 | static tree | |
d764a8e6 IS |
8036 | add_protocol (tree protocol) |
8037 | { | |
8038 | /* Put protocol on list in reverse order. */ | |
8039 | TREE_CHAIN (protocol) = protocol_chain; | |
8040 | protocol_chain = protocol; | |
8041 | return protocol_chain; | |
8042 | } | |
7427ba36 | 8043 | |
d764a8e6 IS |
8044 | /* Check that a protocol is defined, and, recursively, that all |
8045 | protocols that this protocol conforms to are defined too. */ | |
8046 | static void | |
8047 | check_that_protocol_is_defined (tree protocol) | |
8048 | { | |
8049 | if (!PROTOCOL_DEFINED (protocol)) | |
8050 | warning (0, "definition of protocol %qE not found", | |
8051 | PROTOCOL_NAME (protocol)); | |
8052 | ||
8053 | /* If the protocol itself conforms to other protocols, check them | |
8054 | too, recursively. */ | |
8055 | if (PROTOCOL_LIST (protocol)) | |
6c65299b | 8056 | { |
d764a8e6 | 8057 | tree p; |
7427ba36 | 8058 | |
d764a8e6 IS |
8059 | for (p = PROTOCOL_LIST (protocol); p; p = TREE_CHAIN (p)) |
8060 | check_that_protocol_is_defined (TREE_VALUE (p)); | |
7427ba36 | 8061 | } |
e31c7eec | 8062 | } |
7427ba36 | 8063 | |
d764a8e6 IS |
8064 | /* Looks up a protocol. If 'warn_if_deprecated' is true, a warning is |
8065 | emitted if the protocol is deprecated. If 'definition_required' is | |
8066 | true, a warning is emitted if a full @protocol definition has not | |
8067 | been seen. */ | |
8068 | static tree | |
8069 | lookup_protocol (tree ident, bool warn_if_deprecated, bool definition_required) | |
e31c7eec | 8070 | { |
d764a8e6 | 8071 | tree chain; |
e31c7eec | 8072 | |
d764a8e6 IS |
8073 | for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain)) |
8074 | if (ident == PROTOCOL_NAME (chain)) | |
8075 | { | |
8076 | if (warn_if_deprecated && TREE_DEPRECATED (chain)) | |
8077 | { | |
8078 | /* It would be nice to use warn_deprecated_use() here, but | |
8079 | we are using TREE_CHAIN (which is supposed to be the | |
8080 | TYPE_STUB_DECL for a TYPE) for something different. */ | |
944fb799 | 8081 | warning (OPT_Wdeprecated_declarations, "protocol %qE is deprecated", |
d764a8e6 IS |
8082 | PROTOCOL_NAME (chain)); |
8083 | } | |
e31c7eec | 8084 | |
d764a8e6 IS |
8085 | if (definition_required) |
8086 | check_that_protocol_is_defined (chain); | |
e31c7eec | 8087 | |
d764a8e6 IS |
8088 | return chain; |
8089 | } | |
e31c7eec | 8090 | |
d764a8e6 IS |
8091 | return NULL_TREE; |
8092 | } | |
8093 | ||
8094 | /* This function forward declares the protocols named by NAMES. If | |
8095 | they are already declared or defined, the function has no effect. */ | |
8096 | ||
8097 | void | |
c59633d9 | 8098 | objc_declare_protocol (tree name, tree attributes) |
e31c7eec | 8099 | { |
d764a8e6 | 8100 | bool deprecated = false; |
e31c7eec | 8101 | |
d764a8e6 IS |
8102 | #ifdef OBJCPLUS |
8103 | if (current_namespace != global_namespace) { | |
8104 | error ("Objective-C declarations may only appear in global scope"); | |
8105 | } | |
8106 | #endif /* OBJCPLUS */ | |
8107 | ||
8108 | /* Determine if 'deprecated', the only attribute we recognize for | |
8109 | protocols, was used. Ignore all other attributes. */ | |
8110 | if (attributes) | |
e31c7eec | 8111 | { |
d764a8e6 IS |
8112 | tree attribute; |
8113 | for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute)) | |
8114 | { | |
8115 | tree name = TREE_PURPOSE (attribute); | |
944fb799 | 8116 | |
d764a8e6 IS |
8117 | if (is_attribute_p ("deprecated", name)) |
8118 | deprecated = true; | |
8119 | else | |
8120 | warning (OPT_Wattributes, "%qE attribute directive ignored", name); | |
8121 | } | |
6c65299b | 8122 | } |
e31c7eec | 8123 | |
c59633d9 NP |
8124 | if (lookup_protocol (name, /* warn if deprecated */ false, |
8125 | /* definition_required */ false) == NULL_TREE) | |
d764a8e6 | 8126 | { |
c59633d9 | 8127 | tree protocol = make_node (PROTOCOL_INTERFACE_TYPE); |
944fb799 | 8128 | |
c59633d9 NP |
8129 | TYPE_LANG_SLOT_1 (protocol) |
8130 | = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS); | |
8131 | PROTOCOL_NAME (protocol) = name; | |
8132 | PROTOCOL_LIST (protocol) = NULL_TREE; | |
8133 | add_protocol (protocol); | |
8134 | PROTOCOL_DEFINED (protocol) = 0; | |
8135 | PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE; | |
944fb799 | 8136 | |
c59633d9 | 8137 | if (attributes) |
5873a4c1 | 8138 | { |
c59633d9 NP |
8139 | /* TODO: Do we need to store the attributes here ? */ |
8140 | TYPE_ATTRIBUTES (protocol) = attributes; | |
8141 | if (deprecated) | |
8142 | TREE_DEPRECATED (protocol) = 1; | |
d764a8e6 | 8143 | } |
5873a4c1 | 8144 | } |
6c65299b | 8145 | } |
e31c7eec | 8146 | |
d764a8e6 IS |
8147 | static tree |
8148 | start_protocol (enum tree_code code, tree name, tree list, tree attributes) | |
6c65299b | 8149 | { |
d764a8e6 IS |
8150 | tree protocol; |
8151 | bool deprecated = false; | |
6c65299b | 8152 | |
d764a8e6 IS |
8153 | #ifdef OBJCPLUS |
8154 | if (current_namespace != global_namespace) { | |
8155 | error ("Objective-C declarations may only appear in global scope"); | |
8156 | } | |
8157 | #endif /* OBJCPLUS */ | |
6c65299b | 8158 | |
d764a8e6 IS |
8159 | /* Determine if 'deprecated', the only attribute we recognize for |
8160 | protocols, was used. Ignore all other attributes. */ | |
8161 | if (attributes) | |
ece4ce85 | 8162 | { |
d764a8e6 IS |
8163 | tree attribute; |
8164 | for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute)) | |
ece4ce85 | 8165 | { |
d764a8e6 | 8166 | tree name = TREE_PURPOSE (attribute); |
944fb799 | 8167 | |
d764a8e6 IS |
8168 | if (is_attribute_p ("deprecated", name)) |
8169 | deprecated = true; | |
8170 | else | |
8171 | warning (OPT_Wattributes, "%qE attribute directive ignored", name); | |
ece4ce85 NP |
8172 | } |
8173 | } | |
011d50d9 | 8174 | |
d764a8e6 IS |
8175 | protocol = lookup_protocol (name, /* warn_if_deprecated */ false, |
8176 | /* definition_required */ false); | |
ece4ce85 | 8177 | |
d764a8e6 IS |
8178 | if (!protocol) |
8179 | { | |
8180 | protocol = make_node (code); | |
8181 | TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS); | |
6c65299b | 8182 | |
d764a8e6 IS |
8183 | PROTOCOL_NAME (protocol) = name; |
8184 | PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false); | |
8185 | add_protocol (protocol); | |
8186 | PROTOCOL_DEFINED (protocol) = 1; | |
8187 | PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE; | |
9dee6758 | 8188 | |
d764a8e6 IS |
8189 | check_protocol_recursively (protocol, list); |
8190 | } | |
8191 | else if (! PROTOCOL_DEFINED (protocol)) | |
8192 | { | |
8193 | PROTOCOL_DEFINED (protocol) = 1; | |
8194 | PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false); | |
6c65299b | 8195 | |
d764a8e6 IS |
8196 | check_protocol_recursively (protocol, list); |
8197 | } | |
8198 | else | |
8199 | { | |
8200 | warning (0, "duplicate declaration for protocol %qE", | |
8201 | name); | |
8202 | } | |
6c65299b | 8203 | |
d764a8e6 | 8204 | if (attributes) |
a878dab7 | 8205 | { |
d764a8e6 IS |
8206 | TYPE_ATTRIBUTES (protocol) = attributes; |
8207 | if (deprecated) | |
8208 | TREE_DEPRECATED (protocol) = 1; | |
a878dab7 | 8209 | } |
6c65299b | 8210 | |
d764a8e6 | 8211 | return protocol; |
6c65299b | 8212 | } |
011d50d9 | 8213 | |
d764a8e6 | 8214 | /* Decay array and function parameters into pointers. */ |
011d50d9 | 8215 | |
d764a8e6 IS |
8216 | static tree |
8217 | objc_decay_parm_type (tree type) | |
6c65299b | 8218 | { |
d764a8e6 IS |
8219 | if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE) |
8220 | type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE | |
8221 | ? TREE_TYPE (type) | |
8222 | : type); | |
660820d1 | 8223 | |
d764a8e6 | 8224 | return type; |
6c65299b RS |
8225 | } |
8226 | ||
d764a8e6 | 8227 | static GTY(()) tree objc_parmlist = NULL_TREE; |
660820d1 | 8228 | |
d764a8e6 IS |
8229 | /* Append PARM to a list of formal parameters of a method, making a necessary |
8230 | array-to-pointer adjustment along the way. */ | |
8231 | ||
8232 | void | |
8233 | objc_push_parm (tree parm) | |
660820d1 | 8234 | { |
d764a8e6 | 8235 | tree type; |
660820d1 | 8236 | |
d764a8e6 | 8237 | if (TREE_TYPE (parm) == error_mark_node) |
660820d1 | 8238 | { |
d764a8e6 IS |
8239 | objc_parmlist = chainon (objc_parmlist, parm); |
8240 | return; | |
660820d1 | 8241 | } |
660820d1 | 8242 | |
d764a8e6 IS |
8243 | /* Decay arrays and functions into pointers. */ |
8244 | type = objc_decay_parm_type (TREE_TYPE (parm)); | |
660820d1 | 8245 | |
d764a8e6 IS |
8246 | /* If the parameter type has been decayed, a new PARM_DECL needs to be |
8247 | built as well. */ | |
8248 | if (type != TREE_TYPE (parm)) | |
8249 | parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type); | |
660820d1 | 8250 | |
d764a8e6 IS |
8251 | DECL_ARG_TYPE (parm) |
8252 | = lang_hooks.types.type_promotes_to (TREE_TYPE (parm)); | |
660820d1 | 8253 | |
d764a8e6 IS |
8254 | /* Record constancy and volatility. */ |
8255 | c_apply_type_quals_to_decl | |
8256 | ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0) | |
8257 | | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0) | |
267bac10 | 8258 | | (TYPE_ATOMIC (TREE_TYPE (parm)) ? TYPE_QUAL_ATOMIC : 0) |
d764a8e6 | 8259 | | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm); |
660820d1 | 8260 | |
d764a8e6 | 8261 | objc_parmlist = chainon (objc_parmlist, parm); |
660820d1 NP |
8262 | } |
8263 | ||
d764a8e6 IS |
8264 | /* Retrieve the formal parameter list constructed via preceding calls to |
8265 | objc_push_parm(). */ | |
4ce693cd | 8266 | |
d764a8e6 IS |
8267 | #ifdef OBJCPLUS |
8268 | tree | |
a04a722b JM |
8269 | objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED, |
8270 | tree expr ATTRIBUTE_UNUSED) | |
6c65299b | 8271 | { |
d764a8e6 IS |
8272 | tree parm_info = objc_parmlist; |
8273 | objc_parmlist = NULL_TREE; | |
6c65299b | 8274 | |
d764a8e6 | 8275 | return parm_info; |
6c65299b | 8276 | } |
d764a8e6 IS |
8277 | #else |
8278 | struct c_arg_info * | |
a04a722b | 8279 | objc_get_parm_info (int have_ellipsis, tree expr) |
6c65299b | 8280 | { |
d764a8e6 IS |
8281 | tree parm_info = objc_parmlist; |
8282 | struct c_arg_info *arg_info; | |
8283 | /* The C front-end requires an elaborate song and dance at | |
8284 | this point. */ | |
8285 | push_scope (); | |
8286 | declare_parm_level (); | |
8287 | while (parm_info) | |
6c65299b | 8288 | { |
d764a8e6 | 8289 | tree next = DECL_CHAIN (parm_info); |
6c65299b | 8290 | |
d764a8e6 IS |
8291 | DECL_CHAIN (parm_info) = NULL_TREE; |
8292 | parm_info = pushdecl (parm_info); | |
8293 | finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE); | |
8294 | parm_info = next; | |
6c65299b | 8295 | } |
a04a722b | 8296 | arg_info = get_parm_info (have_ellipsis, expr); |
d764a8e6 IS |
8297 | pop_scope (); |
8298 | objc_parmlist = NULL_TREE; | |
8299 | return arg_info; | |
6c65299b | 8300 | } |
d764a8e6 | 8301 | #endif |
6c65299b | 8302 | |
d764a8e6 IS |
8303 | /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC |
8304 | method definitions. In the case of instance methods, we can be more | |
8305 | specific as to the type of 'self'. */ | |
6c65299b | 8306 | |
d764a8e6 IS |
8307 | static void |
8308 | synth_self_and_ucmd_args (void) | |
6c65299b | 8309 | { |
d764a8e6 | 8310 | tree self_type; |
6c65299b | 8311 | |
d764a8e6 IS |
8312 | if (objc_method_context |
8313 | && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL) | |
8314 | self_type = objc_instance_type; | |
6c65299b | 8315 | else |
d764a8e6 IS |
8316 | /* Really a `struct objc_class *'. However, we allow people to |
8317 | assign to self, which changes its type midstream. */ | |
8318 | self_type = objc_object_type; | |
6c65299b | 8319 | |
d764a8e6 IS |
8320 | /* id self; */ |
8321 | objc_push_parm (build_decl (input_location, | |
8322 | PARM_DECL, self_id, self_type)); | |
ece4ce85 | 8323 | |
d764a8e6 IS |
8324 | /* SEL _cmd; */ |
8325 | objc_push_parm (build_decl (input_location, | |
8326 | PARM_DECL, ucmd_id, objc_selector_type)); | |
6c65299b RS |
8327 | } |
8328 | ||
d764a8e6 IS |
8329 | /* Transform an Objective-C method definition into a static C function |
8330 | definition, synthesizing the first two arguments, "self" and "_cmd", | |
a04a722b JM |
8331 | in the process. EXPR is NULL or an expression that needs to be |
8332 | evaluated for the side effects of array size expressions in the | |
8333 | parameters. */ | |
64ee9490 | 8334 | |
d764a8e6 | 8335 | static void |
a04a722b | 8336 | start_method_def (tree method, tree expr) |
6c65299b | 8337 | { |
d764a8e6 IS |
8338 | tree parmlist; |
8339 | #ifdef OBJCPLUS | |
8340 | tree parm_info; | |
8341 | #else | |
8342 | struct c_arg_info *parm_info; | |
8343 | #endif | |
8344 | int have_ellipsis = 0; | |
6c65299b | 8345 | |
d764a8e6 IS |
8346 | /* If we are defining a "dealloc" method in a non-root class, we |
8347 | will need to check if a [super dealloc] is missing, and warn if | |
8348 | it is. */ | |
8349 | if(CLASS_SUPER_NAME (objc_implementation_context) | |
8350 | && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method)))) | |
8351 | should_call_super_dealloc = 1; | |
8352 | else | |
8353 | should_call_super_dealloc = 0; | |
264fa2db | 8354 | |
d764a8e6 IS |
8355 | /* Required to implement _msgSuper. */ |
8356 | objc_method_context = method; | |
8357 | UOBJC_SUPER_decl = NULL_TREE; | |
6c65299b | 8358 | |
d764a8e6 IS |
8359 | /* Generate prototype declarations for arguments..."new-style". */ |
8360 | synth_self_and_ucmd_args (); | |
e31c7eec | 8361 | |
d764a8e6 IS |
8362 | /* Generate argument declarations if a keyword_decl. */ |
8363 | parmlist = METHOD_SEL_ARGS (method); | |
8364 | while (parmlist) | |
8365 | { | |
8366 | /* parmlist is a KEYWORD_DECL. */ | |
8367 | tree type = TREE_VALUE (TREE_TYPE (parmlist)); | |
8368 | tree parm; | |
264fa2db | 8369 | |
d764a8e6 IS |
8370 | parm = build_decl (input_location, |
8371 | PARM_DECL, KEYWORD_ARG_NAME (parmlist), type); | |
8372 | decl_attributes (&parm, DECL_ATTRIBUTES (parmlist), 0); | |
8373 | objc_push_parm (parm); | |
8374 | parmlist = DECL_CHAIN (parmlist); | |
8375 | } | |
6c65299b | 8376 | |
d764a8e6 IS |
8377 | if (METHOD_ADD_ARGS (method)) |
8378 | { | |
8379 | tree akey; | |
8380 | ||
8381 | for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method)); | |
8382 | akey; akey = TREE_CHAIN (akey)) | |
e31c7eec | 8383 | { |
d764a8e6 | 8384 | objc_push_parm (TREE_VALUE (akey)); |
e31c7eec TW |
8385 | } |
8386 | ||
d764a8e6 IS |
8387 | if (METHOD_ADD_ARGS_ELLIPSIS_P (method)) |
8388 | have_ellipsis = 1; | |
6c65299b | 8389 | } |
6c65299b | 8390 | |
a04a722b | 8391 | parm_info = objc_get_parm_info (have_ellipsis, expr); |
6c65299b | 8392 | |
d764a8e6 IS |
8393 | really_start_method (objc_method_context, parm_info); |
8394 | } | |
96e5d745 | 8395 | |
d764a8e6 IS |
8396 | /* Return 1 if TYPE1 is equivalent to TYPE2 for purposes of method |
8397 | overloading. */ | |
8398 | static int | |
8399 | objc_types_are_equivalent (tree type1, tree type2) | |
81ff723b | 8400 | { |
d764a8e6 IS |
8401 | if (type1 == type2) |
8402 | return 1; | |
81ff723b | 8403 | |
d764a8e6 IS |
8404 | /* Strip away indirections. */ |
8405 | while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE) | |
8406 | && (TREE_CODE (type1) == TREE_CODE (type2))) | |
8407 | type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2); | |
8408 | if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2)) | |
8409 | return 0; | |
81ff723b | 8410 | |
d764a8e6 IS |
8411 | /* Compare the protocol lists. */ |
8412 | type1 = (TYPE_HAS_OBJC_INFO (type1) | |
8413 | ? TYPE_OBJC_PROTOCOL_LIST (type1) | |
8414 | : NULL_TREE); | |
8415 | type2 = (TYPE_HAS_OBJC_INFO (type2) | |
8416 | ? TYPE_OBJC_PROTOCOL_LIST (type2) | |
8417 | : NULL_TREE); | |
223706ad | 8418 | |
d764a8e6 IS |
8419 | /* If there are no protocols (most common case), the types are |
8420 | identical. */ | |
8421 | if (type1 == NULL_TREE && type2 == NULL_TREE) | |
8422 | return 1; | |
944fb799 | 8423 | |
d764a8e6 IS |
8424 | /* If one has protocols, and the other one hasn't, they are not |
8425 | identical. */ | |
8426 | if ((type1 == NULL_TREE && type2 != NULL_TREE) | |
8427 | || (type1 != NULL_TREE && type2 == NULL_TREE)) | |
8428 | return 0; | |
223706ad | 8429 | else |
223706ad | 8430 | { |
d764a8e6 IS |
8431 | /* Else, both have protocols, and we need to do the full |
8432 | comparison. It is possible that either type1 or type2 | |
8433 | contain some duplicate protocols in the list, so we can't | |
8434 | even just compare list_length as a first check. */ | |
8435 | tree t; | |
223706ad | 8436 | |
d764a8e6 IS |
8437 | for (t = type2; t; t = TREE_CHAIN (t)) |
8438 | if (!lookup_protocol_in_reflist (type1, TREE_VALUE (t))) | |
8439 | return 0; | |
944fb799 | 8440 | |
d764a8e6 IS |
8441 | for (t = type1; t; t = TREE_CHAIN (t)) |
8442 | if (!lookup_protocol_in_reflist (type2, TREE_VALUE (t))) | |
8443 | return 0; | |
944fb799 | 8444 | |
d764a8e6 | 8445 | return 1; |
223706ad | 8446 | } |
d764a8e6 | 8447 | } |
223706ad | 8448 | |
d764a8e6 | 8449 | /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */ |
6c65299b | 8450 | |
d764a8e6 IS |
8451 | static int |
8452 | objc_types_share_size_and_alignment (tree type1, tree type2) | |
8453 | { | |
8454 | return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)) | |
8455 | && TYPE_ALIGN (type1) == TYPE_ALIGN (type2)); | |
8456 | } | |
81ff723b | 8457 | |
d764a8e6 IS |
8458 | /* Return 1 if PROTO1 is equivalent to PROTO2 |
8459 | for purposes of method overloading. Ordinarily, the type signatures | |
8460 | should match up exactly, unless STRICT is zero, in which case we | |
8461 | shall allow differences in which the size and alignment of a type | |
8462 | is the same. */ | |
81ff723b | 8463 | |
d764a8e6 IS |
8464 | static int |
8465 | comp_proto_with_proto (tree proto1, tree proto2, int strict) | |
8466 | { | |
dc2dc512 NP |
8467 | tree type1, type2; |
8468 | ||
d764a8e6 IS |
8469 | /* The following test is needed in case there are hashing |
8470 | collisions. */ | |
8471 | if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2)) | |
8472 | return 0; | |
81ff723b | 8473 | |
d764a8e6 IS |
8474 | /* Compare return types. */ |
8475 | type1 = TREE_VALUE (TREE_TYPE (proto1)); | |
8476 | type2 = TREE_VALUE (TREE_TYPE (proto2)); | |
38b9c8c3 | 8477 | |
d764a8e6 IS |
8478 | if (!objc_types_are_equivalent (type1, type2) |
8479 | && (strict || !objc_types_share_size_and_alignment (type1, type2))) | |
8480 | return 0; | |
8481 | ||
8482 | /* Compare argument types. */ | |
38b9c8c3 | 8483 | |
dc2dc512 NP |
8484 | /* The first argument (objc_object_type) is always the same, no need |
8485 | to compare. */ | |
8486 | ||
8487 | /* The second argument (objc_selector_type) is always the same, no | |
8488 | need to compare. */ | |
8489 | ||
8490 | /* Compare the other arguments. */ | |
8491 | { | |
8492 | tree arg1, arg2; | |
8493 | ||
8494 | /* Compare METHOD_SEL_ARGS. */ | |
8495 | for (arg1 = METHOD_SEL_ARGS (proto1), arg2 = METHOD_SEL_ARGS (proto2); | |
8496 | arg1 && arg2; | |
8497 | arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2)) | |
8498 | { | |
8499 | type1 = TREE_VALUE (TREE_TYPE (arg1)); | |
8500 | type2 = TREE_VALUE (TREE_TYPE (arg2)); | |
944fb799 | 8501 | |
dc2dc512 NP |
8502 | /* FIXME: Do we need to decay argument types to compare them ? */ |
8503 | type1 = objc_decay_parm_type (type1); | |
8504 | type2 = objc_decay_parm_type (type2); | |
944fb799 | 8505 | |
dc2dc512 NP |
8506 | if (!objc_types_are_equivalent (type1, type2) |
8507 | && (strict || !objc_types_share_size_and_alignment (type1, type2))) | |
8508 | return 0; | |
8509 | } | |
944fb799 | 8510 | |
dc2dc512 NP |
8511 | /* The loop ends when arg1 or arg2 are NULL. Make sure they are |
8512 | both NULL. */ | |
8513 | if (arg1 != arg2) | |
8514 | return 0; | |
8515 | ||
8516 | /* Compare METHOD_ADD_ARGS. */ | |
8517 | if ((METHOD_ADD_ARGS (proto1) && !METHOD_ADD_ARGS (proto2)) | |
8518 | || (METHOD_ADD_ARGS (proto2) && !METHOD_ADD_ARGS (proto1))) | |
8519 | return 0; | |
8520 | ||
8521 | if (METHOD_ADD_ARGS (proto1)) | |
8522 | { | |
8523 | for (arg1 = TREE_CHAIN (METHOD_ADD_ARGS (proto1)), arg2 = TREE_CHAIN (METHOD_ADD_ARGS (proto2)); | |
8524 | arg1 && arg2; | |
8525 | arg1 = TREE_CHAIN (arg1), arg2 = TREE_CHAIN (arg2)) | |
8526 | { | |
8527 | type1 = TREE_TYPE (TREE_VALUE (arg1)); | |
8528 | type2 = TREE_TYPE (TREE_VALUE (arg2)); | |
944fb799 | 8529 | |
dc2dc512 NP |
8530 | /* FIXME: Do we need to decay argument types to compare them ? */ |
8531 | type1 = objc_decay_parm_type (type1); | |
8532 | type2 = objc_decay_parm_type (type2); | |
944fb799 | 8533 | |
dc2dc512 NP |
8534 | if (!objc_types_are_equivalent (type1, type2) |
8535 | && (strict || !objc_types_share_size_and_alignment (type1, type2))) | |
8536 | return 0; | |
8537 | } | |
8538 | } | |
944fb799 | 8539 | |
dc2dc512 NP |
8540 | /* The loop ends when arg1 or arg2 are NULL. Make sure they are |
8541 | both NULL. */ | |
8542 | if (arg1 != arg2) | |
8543 | return 0; | |
8544 | ||
8545 | /* Compare METHOD_ADD_ARGS_ELLIPSIS_P. */ | |
8546 | if (METHOD_ADD_ARGS_ELLIPSIS_P (proto1) != METHOD_ADD_ARGS_ELLIPSIS_P (proto2)) | |
8547 | return 0; | |
8548 | } | |
8549 | ||
8550 | /* Success. */ | |
8551 | return 1; | |
6c65299b RS |
8552 | } |
8553 | ||
d764a8e6 IS |
8554 | /* This routine returns true if TYPE is a valid objc object type, |
8555 | suitable for messaging; false otherwise. If 'accept_class' is | |
8556 | 'true', then a Class object is considered valid for messaging and | |
8557 | 'true' is returned if 'type' refers to a Class. If 'accept_class' | |
8558 | is 'false', then a Class object is not considered valid for | |
8559 | messaging and 'false' is returned in that case. */ | |
8560 | ||
8561 | static bool | |
8562 | objc_type_valid_for_messaging (tree type, bool accept_classes) | |
6c65299b | 8563 | { |
d764a8e6 IS |
8564 | if (!POINTER_TYPE_P (type)) |
8565 | return false; | |
51900510 | 8566 | |
d764a8e6 IS |
8567 | /* Remove the pointer indirection; don't remove more than one |
8568 | otherwise we'd consider "NSObject **" a valid type for messaging, | |
8569 | which it isn't. */ | |
8570 | type = TREE_TYPE (type); | |
6c65299b | 8571 | |
d764a8e6 IS |
8572 | if (TREE_CODE (type) != RECORD_TYPE) |
8573 | return false; | |
e31c7eec | 8574 | |
d764a8e6 IS |
8575 | if (objc_is_object_id (type)) |
8576 | return true; | |
e31c7eec | 8577 | |
d764a8e6 IS |
8578 | if (objc_is_class_id (type)) |
8579 | return accept_classes; | |
6c65299b | 8580 | |
d764a8e6 IS |
8581 | if (TYPE_HAS_OBJC_INFO (type)) |
8582 | return true; | |
6c65299b | 8583 | |
d764a8e6 IS |
8584 | return false; |
8585 | } | |
264fa2db | 8586 | |
d764a8e6 IS |
8587 | void |
8588 | objc_start_function (tree name, tree type, tree attrs, | |
8589 | #ifdef OBJCPLUS | |
8590 | tree params | |
8591 | #else | |
8592 | struct c_arg_info *params | |
8593 | #endif | |
8594 | ) | |
8595 | { | |
8596 | tree fndecl = build_decl (input_location, | |
8597 | FUNCTION_DECL, name, type); | |
6c65299b | 8598 | |
d764a8e6 IS |
8599 | #ifdef OBJCPLUS |
8600 | DECL_ARGUMENTS (fndecl) = params; | |
8601 | DECL_INITIAL (fndecl) = error_mark_node; | |
8602 | DECL_EXTERNAL (fndecl) = 0; | |
8603 | TREE_STATIC (fndecl) = 1; | |
8604 | retrofit_lang_decl (fndecl); | |
8605 | cplus_decl_attributes (&fndecl, attrs, 0); | |
8606 | start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT); | |
8607 | #else | |
8608 | current_function_returns_value = 0; /* Assume, until we see it does. */ | |
8609 | current_function_returns_null = 0; | |
8610 | decl_attributes (&fndecl, attrs, 0); | |
8611 | announce_function (fndecl); | |
8612 | DECL_INITIAL (fndecl) = error_mark_node; | |
8613 | DECL_EXTERNAL (fndecl) = 0; | |
8614 | TREE_STATIC (fndecl) = 1; | |
8615 | current_function_decl = pushdecl (fndecl); | |
8616 | push_scope (); | |
8617 | declare_parm_level (); | |
8618 | DECL_RESULT (current_function_decl) | |
8619 | = build_decl (input_location, | |
8620 | RESULT_DECL, NULL_TREE, | |
8621 | TREE_TYPE (TREE_TYPE (current_function_decl))); | |
8622 | DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1; | |
8623 | DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1; | |
8624 | start_fname_decls (); | |
8625 | store_parm_decls_from (params); | |
8626 | #endif | |
6408ef33 | 8627 | |
d764a8e6 | 8628 | TREE_USED (current_function_decl) = 1; |
6c65299b | 8629 | } |
8f07a2aa | 8630 | |
d764a8e6 IS |
8631 | /* - Generate an identifier for the function. the format is "_n_cls", |
8632 | where 1 <= n <= nMethods, and cls is the name the implementation we | |
8633 | are processing. | |
8634 | - Install the return type from the method declaration. | |
8635 | - If we have a prototype, check for type consistency. */ | |
8636 | ||
8637 | static void | |
8638 | really_start_method (tree method, | |
8639 | #ifdef OBJCPLUS | |
8640 | tree parmlist | |
8641 | #else | |
8642 | struct c_arg_info *parmlist | |
8643 | #endif | |
8644 | ) | |
6c65299b | 8645 | { |
d764a8e6 IS |
8646 | tree ret_type, meth_type; |
8647 | tree method_id; | |
8648 | const char *sel_name, *class_name, *cat_name; | |
8649 | char *buf; | |
6c65299b | 8650 | |
d764a8e6 IS |
8651 | /* Synth the storage class & assemble the return type. */ |
8652 | ret_type = TREE_VALUE (TREE_TYPE (method)); | |
e31c7eec | 8653 | |
d764a8e6 IS |
8654 | sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method)); |
8655 | class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context)); | |
8656 | cat_name = ((TREE_CODE (objc_implementation_context) | |
8657 | == CLASS_IMPLEMENTATION_TYPE) | |
8658 | ? NULL | |
8659 | : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context))); | |
8660 | method_slot++; | |
e31c7eec | 8661 | |
d764a8e6 IS |
8662 | /* Make sure this is big enough for any plausible method label. */ |
8663 | buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name) | |
8664 | + (cat_name ? strlen (cat_name) : 0)); | |
6c65299b | 8665 | |
d764a8e6 IS |
8666 | OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL, |
8667 | class_name, cat_name, sel_name, method_slot); | |
8668 | ||
8669 | method_id = get_identifier (buf); | |
d11dd684 ZL |
8670 | |
8671 | #ifdef OBJCPLUS | |
d764a8e6 IS |
8672 | /* Objective-C methods cannot be overloaded, so we don't need |
8673 | the type encoding appended. It looks bad anyway... */ | |
8674 | push_lang_context (lang_name_c); | |
d11dd684 ZL |
8675 | #endif |
8676 | ||
6174da1b | 8677 | meth_type = build_function_type_for_method (ret_type, method, METHOD_DEF, 0); |
d764a8e6 | 8678 | objc_start_function (method_id, meth_type, NULL_TREE, parmlist); |
b2072208 | 8679 | |
d764a8e6 IS |
8680 | /* Set self_decl from the first argument. */ |
8681 | self_decl = DECL_ARGUMENTS (current_function_decl); | |
6c65299b | 8682 | |
d764a8e6 IS |
8683 | /* Suppress unused warnings. */ |
8684 | TREE_USED (self_decl) = 1; | |
8685 | DECL_READ_P (self_decl) = 1; | |
8686 | TREE_USED (DECL_CHAIN (self_decl)) = 1; | |
8687 | DECL_READ_P (DECL_CHAIN (self_decl)) = 1; | |
8688 | #ifdef OBJCPLUS | |
8689 | pop_lang_context (); | |
8690 | #endif | |
38b9c8c3 | 8691 | |
d764a8e6 | 8692 | METHOD_DEFINITION (method) = current_function_decl; |
6c65299b | 8693 | |
d764a8e6 | 8694 | /* Check consistency...start_function, pushdecl, duplicate_decls. */ |
6c65299b | 8695 | |
d764a8e6 IS |
8696 | if (implementation_template != objc_implementation_context) |
8697 | { | |
8698 | tree proto | |
8699 | = lookup_method_static (implementation_template, | |
8700 | METHOD_SEL_NAME (method), | |
8701 | ((TREE_CODE (method) == CLASS_METHOD_DECL) | |
8702 | | OBJC_LOOKUP_NO_SUPER)); | |
61c627ed | 8703 | |
d764a8e6 IS |
8704 | if (proto) |
8705 | { | |
8706 | if (!comp_proto_with_proto (method, proto, 1)) | |
8707 | { | |
8708 | bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL; | |
d11dd684 | 8709 | |
d764a8e6 IS |
8710 | warning_at (DECL_SOURCE_LOCATION (method), 0, |
8711 | "conflicting types for %<%c%s%>", | |
8712 | (type ? '-' : '+'), | |
8713 | identifier_to_locale (gen_method_decl (method))); | |
8714 | inform (DECL_SOURCE_LOCATION (proto), | |
8715 | "previous declaration of %<%c%s%>", | |
8716 | (type ? '-' : '+'), | |
8717 | identifier_to_locale (gen_method_decl (proto))); | |
8718 | } | |
8719 | else | |
8720 | { | |
8721 | /* If the method in the @interface was deprecated, mark | |
8722 | the implemented method as deprecated too. It should | |
8723 | never be used for messaging (when the deprecation | |
8724 | warnings are produced), but just in case. */ | |
8725 | if (TREE_DEPRECATED (proto)) | |
8726 | TREE_DEPRECATED (method) = 1; | |
d11dd684 | 8727 | |
d764a8e6 IS |
8728 | /* If the method in the @interface was marked as |
8729 | 'noreturn', mark the function implementing the method | |
8730 | as 'noreturn' too. */ | |
8731 | TREE_THIS_VOLATILE (current_function_decl) = TREE_THIS_VOLATILE (proto); | |
8732 | } | |
8733 | } | |
8734 | else | |
8735 | { | |
8736 | /* We have a method @implementation even though we did not | |
8737 | see a corresponding @interface declaration (which is allowed | |
8738 | by Objective-C rules). Go ahead and place the method in | |
8739 | the @interface anyway, so that message dispatch lookups | |
8740 | will see it. */ | |
8741 | tree interface = implementation_template; | |
e31c7eec | 8742 | |
d764a8e6 IS |
8743 | if (TREE_CODE (objc_implementation_context) |
8744 | == CATEGORY_IMPLEMENTATION_TYPE) | |
8745 | interface = lookup_category | |
8746 | (interface, | |
8747 | CLASS_SUPER_NAME (objc_implementation_context)); | |
64ee9490 | 8748 | |
d764a8e6 IS |
8749 | if (interface) |
8750 | objc_add_method (interface, copy_node (method), | |
944fb799 | 8751 | TREE_CODE (method) == CLASS_METHOD_DECL, |
d764a8e6 | 8752 | /* is_optional= */ false); |
6c65299b | 8753 | } |
6c65299b | 8754 | } |
6c65299b | 8755 | } |
4ca5d2a7 | 8756 | |
d764a8e6 | 8757 | static void *UOBJC_SUPER_scope = 0; |
6c65299b | 8758 | |
d764a8e6 IS |
8759 | /* _n_Method (id self, SEL sel, ...) |
8760 | { | |
8761 | struct objc_super _S; | |
8762 | _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...); | |
8763 | } */ | |
8764 | ||
8765 | static tree | |
8766 | get_super_receiver (void) | |
6c65299b | 8767 | { |
d764a8e6 IS |
8768 | if (objc_method_context) |
8769 | { | |
8770 | tree super_expr, super_expr_list, class_expr; | |
8771 | bool inst_meth; | |
8772 | if (!UOBJC_SUPER_decl) | |
8773 | { | |
8774 | UOBJC_SUPER_decl = build_decl (input_location, | |
8775 | VAR_DECL, get_identifier (TAG_SUPER), | |
8776 | objc_super_template); | |
8777 | /* This prevents `unused variable' warnings when compiling with -Wall. */ | |
8778 | TREE_USED (UOBJC_SUPER_decl) = 1; | |
8779 | DECL_READ_P (UOBJC_SUPER_decl) = 1; | |
8780 | lang_hooks.decls.pushdecl (UOBJC_SUPER_decl); | |
8781 | finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE, | |
944fb799 | 8782 | NULL_TREE); |
d764a8e6 IS |
8783 | UOBJC_SUPER_scope = objc_get_current_scope (); |
8784 | } | |
4ca5d2a7 | 8785 | |
d764a8e6 IS |
8786 | /* Set receiver to self. */ |
8787 | super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id); | |
8788 | super_expr = build_modify_expr (input_location, super_expr, NULL_TREE, | |
8789 | NOP_EXPR, input_location, self_decl, | |
8790 | NULL_TREE); | |
8791 | super_expr_list = super_expr; | |
6c65299b | 8792 | |
d764a8e6 IS |
8793 | /* Set class to begin searching. */ |
8794 | /* Get the ident for the superclass class field & build a ref to it. | |
8795 | ??? maybe we should just name the field the same for all runtimes. */ | |
8796 | super_expr = (*runtime.super_superclassfield_ident) (); | |
8797 | super_expr = objc_build_component_ref (UOBJC_SUPER_decl, super_expr); | |
46a88c12 | 8798 | |
d764a8e6 IS |
8799 | gcc_assert (imp_list->imp_context == objc_implementation_context |
8800 | && imp_list->imp_template == implementation_template); | |
8801 | inst_meth = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL); | |
8802 | ||
8803 | if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE) | |
944fb799 | 8804 | class_expr = (*runtime.get_class_super_ref) (input_location, |
d764a8e6 IS |
8805 | imp_list, inst_meth); |
8806 | else | |
8807 | /* We have a category. */ | |
6c65299b | 8808 | { |
d764a8e6 IS |
8809 | tree super_name = CLASS_SUPER_NAME (imp_list->imp_template); |
8810 | tree super_class; | |
8811 | ||
8812 | /* Barf if super used in a category of a root object. */ | |
8813 | if (!super_name) | |
4ca5d2a7 | 8814 | { |
d764a8e6 IS |
8815 | error ("no super class declared in interface for %qE", |
8816 | CLASS_NAME (imp_list->imp_template)); | |
8817 | return error_mark_node; | |
8818 | } | |
4ca5d2a7 | 8819 | |
944fb799 | 8820 | super_class = (*runtime.get_category_super_ref) (input_location, |
d764a8e6 | 8821 | imp_list, inst_meth); |
944fb799 | 8822 | class_expr = build_c_cast (input_location, |
d764a8e6 IS |
8823 | TREE_TYPE (super_expr), super_class); |
8824 | } | |
4ca5d2a7 | 8825 | |
944fb799 | 8826 | super_expr = build_modify_expr (input_location, super_expr, NULL_TREE, |
d764a8e6 IS |
8827 | NOP_EXPR, |
8828 | input_location, class_expr, NULL_TREE); | |
4ca5d2a7 | 8829 | |
944fb799 | 8830 | super_expr_list = build_compound_expr (input_location, |
d764a8e6 | 8831 | super_expr_list, super_expr); |
4ca5d2a7 | 8832 | |
944fb799 | 8833 | super_expr = build_unary_op (input_location, |
d764a8e6 IS |
8834 | ADDR_EXPR, UOBJC_SUPER_decl, 0); |
8835 | super_expr_list = build_compound_expr (input_location, | |
8836 | super_expr_list, super_expr); | |
4ca5d2a7 | 8837 | |
d764a8e6 IS |
8838 | return super_expr_list; |
8839 | } | |
8840 | else | |
8841 | { | |
8842 | error ("[super ...] must appear in a method context"); | |
8843 | return error_mark_node; | |
6c65299b | 8844 | } |
e31c7eec TW |
8845 | } |
8846 | ||
d764a8e6 IS |
8847 | /* When exiting a scope, sever links to a 'super' declaration (if any) |
8848 | therein contained. */ | |
f2e6e530 | 8849 | |
d764a8e6 IS |
8850 | void |
8851 | objc_clear_super_receiver (void) | |
e31c7eec | 8852 | { |
d764a8e6 | 8853 | if (objc_method_context |
d4c433f9 NP |
8854 | && UOBJC_SUPER_scope == objc_get_current_scope ()) |
8855 | { | |
8856 | UOBJC_SUPER_decl = 0; | |
8857 | UOBJC_SUPER_scope = 0; | |
8858 | } | |
d764a8e6 | 8859 | } |
51900510 | 8860 | |
d764a8e6 IS |
8861 | void |
8862 | objc_finish_method_definition (tree fndecl) | |
8863 | { | |
8864 | /* We cannot validly inline ObjC methods, at least not without a language | |
8865 | extension to declare that a method need not be dynamically | |
8866 | dispatched, so suppress all thoughts of doing so. */ | |
8867 | DECL_UNINLINABLE (fndecl) = 1; | |
51900510 | 8868 | |
d764a8e6 IS |
8869 | #ifndef OBJCPLUS |
8870 | /* The C++ front-end will have called finish_function() for us. */ | |
8871 | finish_function (); | |
8872 | #endif | |
8873 | ||
8874 | METHOD_ENCODING (objc_method_context) | |
8875 | = encode_method_prototype (objc_method_context); | |
8876 | ||
8877 | /* Required to implement _msgSuper. This must be done AFTER finish_function, | |
8878 | since the optimizer may find "may be used before set" errors. */ | |
8879 | objc_method_context = NULL_TREE; | |
8880 | ||
8881 | if (should_call_super_dealloc) | |
8882 | warning (0, "method possibly missing a [super dealloc] call"); | |
e31c7eec TW |
8883 | } |
8884 | ||
d764a8e6 IS |
8885 | /* Given a tree DECL node, produce a printable description of it in the given |
8886 | buffer, overwriting the buffer. */ | |
143dc646 | 8887 | |
d764a8e6 IS |
8888 | static char * |
8889 | gen_declaration (tree decl) | |
143dc646 | 8890 | { |
d764a8e6 | 8891 | errbuf[0] = '\0'; |
143dc646 | 8892 | |
d764a8e6 | 8893 | if (DECL_P (decl)) |
143dc646 | 8894 | { |
d764a8e6 | 8895 | gen_type_name_0 (TREE_TYPE (decl)); |
46a88c12 | 8896 | |
d764a8e6 | 8897 | if (DECL_NAME (decl)) |
143dc646 | 8898 | { |
d764a8e6 IS |
8899 | if (!POINTER_TYPE_P (TREE_TYPE (decl))) |
8900 | strcat (errbuf, " "); | |
143dc646 | 8901 | |
d764a8e6 | 8902 | strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl))); |
143dc646 KKT |
8903 | } |
8904 | ||
d764a8e6 IS |
8905 | if (DECL_INITIAL (decl) |
8906 | && TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST) | |
8907 | sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC, | |
8908 | TREE_INT_CST_LOW (DECL_INITIAL (decl))); | |
143dc646 | 8909 | } |
d764a8e6 IS |
8910 | |
8911 | return errbuf; | |
143dc646 KKT |
8912 | } |
8913 | ||
d764a8e6 IS |
8914 | /* Given a tree TYPE node, produce a printable description of it in the given |
8915 | buffer, overwriting the buffer. */ | |
011d50d9 | 8916 | |
d764a8e6 IS |
8917 | static char * |
8918 | gen_type_name_0 (tree type) | |
e31c7eec | 8919 | { |
d764a8e6 IS |
8920 | tree orig = type, proto; |
8921 | ||
8922 | if (TYPE_P (type) && TYPE_NAME (type)) | |
8923 | type = TYPE_NAME (type); | |
8924 | else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE) | |
e31c7eec | 8925 | { |
d764a8e6 | 8926 | tree inner = TREE_TYPE (type); |
e31c7eec | 8927 | |
d764a8e6 IS |
8928 | while (TREE_CODE (inner) == ARRAY_TYPE) |
8929 | inner = TREE_TYPE (inner); | |
e31c7eec | 8930 | |
d764a8e6 | 8931 | gen_type_name_0 (inner); |
011d50d9 | 8932 | |
d764a8e6 IS |
8933 | if (!POINTER_TYPE_P (inner)) |
8934 | strcat (errbuf, " "); | |
ad074d60 | 8935 | |
d764a8e6 IS |
8936 | if (POINTER_TYPE_P (type)) |
8937 | strcat (errbuf, "*"); | |
8938 | else | |
8939 | while (type != inner) | |
8940 | { | |
8941 | strcat (errbuf, "["); | |
f2e6e530 | 8942 | |
d764a8e6 IS |
8943 | if (TYPE_DOMAIN (type)) |
8944 | { | |
8945 | char sz[20]; | |
011d50d9 | 8946 | |
d764a8e6 IS |
8947 | sprintf (sz, HOST_WIDE_INT_PRINT_DEC, |
8948 | (TREE_INT_CST_LOW | |
8949 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1)); | |
8950 | strcat (errbuf, sz); | |
8951 | } | |
011d50d9 | 8952 | |
d764a8e6 IS |
8953 | strcat (errbuf, "]"); |
8954 | type = TREE_TYPE (type); | |
8955 | } | |
f2e6e530 | 8956 | |
d764a8e6 | 8957 | goto exit_function; |
f2e6e530 | 8958 | } |
6c65299b | 8959 | |
d764a8e6 IS |
8960 | if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type)) |
8961 | type = DECL_NAME (type); | |
264fa2db | 8962 | |
d764a8e6 | 8963 | strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE |
944fb799 | 8964 | ? IDENTIFIER_POINTER (type) |
d764a8e6 IS |
8965 | : ""); |
8966 | ||
8967 | /* For 'id' and 'Class', adopted protocols are stored in the pointee. */ | |
8968 | if (objc_is_id (orig)) | |
8969 | orig = TREE_TYPE (orig); | |
8970 | ||
8971 | proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE; | |
8972 | ||
8973 | if (proto) | |
e804281e | 8974 | { |
d764a8e6 IS |
8975 | strcat (errbuf, " <"); |
8976 | ||
8977 | while (proto) { | |
8978 | strcat (errbuf, | |
8979 | IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto)))); | |
8980 | proto = TREE_CHAIN (proto); | |
8981 | strcat (errbuf, proto ? ", " : ">"); | |
8982 | } | |
e804281e SS |
8983 | } |
8984 | ||
d764a8e6 IS |
8985 | exit_function: |
8986 | return errbuf; | |
8987 | } | |
8988 | ||
8989 | static char * | |
8990 | gen_type_name (tree type) | |
8991 | { | |
8992 | errbuf[0] = '\0'; | |
8993 | ||
8994 | return gen_type_name_0 (type); | |
8995 | } | |
8996 | ||
8997 | /* Given a method tree, put a printable description into the given | |
8998 | buffer (overwriting) and return a pointer to the buffer. */ | |
8999 | ||
9000 | static char * | |
9001 | gen_method_decl (tree method) | |
9002 | { | |
9003 | tree chain; | |
9004 | ||
9005 | strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */ | |
9006 | gen_type_name_0 (TREE_VALUE (TREE_TYPE (method))); | |
9007 | strcat (errbuf, ")"); | |
9008 | chain = METHOD_SEL_ARGS (method); | |
9009 | ||
9010 | if (chain) | |
ec3e9f82 | 9011 | { |
d764a8e6 IS |
9012 | /* We have a chain of keyword_decls. */ |
9013 | do | |
9014 | { | |
9015 | if (KEYWORD_KEY_NAME (chain)) | |
9016 | strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain))); | |
9017 | ||
9018 | strcat (errbuf, ":("); | |
9019 | gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain))); | |
9020 | strcat (errbuf, ")"); | |
9021 | ||
9022 | strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain))); | |
9023 | if ((chain = DECL_CHAIN (chain))) | |
9024 | strcat (errbuf, " "); | |
9025 | } | |
9026 | while (chain); | |
9027 | ||
9028 | if (METHOD_ADD_ARGS (method)) | |
9029 | { | |
9030 | chain = TREE_CHAIN (METHOD_ADD_ARGS (method)); | |
9031 | ||
9032 | /* Know we have a chain of parm_decls. */ | |
9033 | while (chain) | |
9034 | { | |
9035 | strcat (errbuf, ", "); | |
9036 | gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain))); | |
9037 | chain = TREE_CHAIN (chain); | |
9038 | } | |
9039 | ||
9040 | if (METHOD_ADD_ARGS_ELLIPSIS_P (method)) | |
9041 | strcat (errbuf, ", ..."); | |
9042 | } | |
ec3e9f82 | 9043 | } |
6c65299b | 9044 | |
d764a8e6 IS |
9045 | else |
9046 | /* We have a unary selector. */ | |
9047 | strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method))); | |
9048 | ||
9049 | return errbuf; | |
9050 | } | |
9051 | \f | |
9052 | /* Debug info. */ | |
9053 | ||
9054 | ||
9055 | /* Dump an @interface declaration of the supplied class CHAIN to the | |
9056 | supplied file FP. Used to implement the -gen-decls option (which | |
9057 | prints out an @interface declaration of all classes compiled in | |
9058 | this run); potentially useful for debugging the compiler too. */ | |
9059 | void | |
9060 | dump_interface (FILE *fp, tree chain) | |
9061 | { | |
9062 | /* FIXME: A heap overflow here whenever a method (or ivar) | |
9063 | declaration is so long that it doesn't fit in the buffer. The | |
9064 | code and all the related functions should be rewritten to avoid | |
9065 | using fixed size buffers. */ | |
9066 | const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain)); | |
9067 | tree ivar_decls = CLASS_RAW_IVARS (chain); | |
9068 | tree nst_methods = CLASS_NST_METHODS (chain); | |
9069 | tree cls_methods = CLASS_CLS_METHODS (chain); | |
9070 | ||
9071 | fprintf (fp, "\n@interface %s", my_name); | |
9072 | ||
9073 | /* CLASS_SUPER_NAME is used to store the superclass name for | |
9074 | classes, and the category name for categories. */ | |
9075 | if (CLASS_SUPER_NAME (chain)) | |
6408ef33 | 9076 | { |
d764a8e6 | 9077 | const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain)); |
38b9c8c3 | 9078 | |
d764a8e6 | 9079 | switch (TREE_CODE (chain)) |
38b9c8c3 | 9080 | { |
d764a8e6 IS |
9081 | case CATEGORY_IMPLEMENTATION_TYPE: |
9082 | case CATEGORY_INTERFACE_TYPE: | |
9083 | fprintf (fp, " (%s)\n", name); | |
9084 | break; | |
9085 | default: | |
9086 | fprintf (fp, " : %s\n", name); | |
9087 | break; | |
38b9c8c3 | 9088 | } |
d764a8e6 IS |
9089 | } |
9090 | else | |
9091 | fprintf (fp, "\n"); | |
9092 | ||
9093 | /* FIXME - the following doesn't seem to work at the moment. */ | |
9094 | if (ivar_decls) | |
9095 | { | |
9096 | fprintf (fp, "{\n"); | |
9097 | do | |
d4e72c58 | 9098 | { |
d764a8e6 IS |
9099 | fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls)); |
9100 | ivar_decls = TREE_CHAIN (ivar_decls); | |
d4e72c58 | 9101 | } |
d764a8e6 IS |
9102 | while (ivar_decls); |
9103 | fprintf (fp, "}\n"); | |
6408ef33 ZL |
9104 | } |
9105 | ||
d764a8e6 | 9106 | while (nst_methods) |
ec3e9f82 | 9107 | { |
d764a8e6 IS |
9108 | fprintf (fp, "- %s;\n", gen_method_decl (nst_methods)); |
9109 | nst_methods = TREE_CHAIN (nst_methods); | |
ec3e9f82 | 9110 | } |
6c65299b | 9111 | |
d764a8e6 | 9112 | while (cls_methods) |
e31c7eec | 9113 | { |
d764a8e6 IS |
9114 | fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods)); |
9115 | cls_methods = TREE_CHAIN (cls_methods); | |
e31c7eec TW |
9116 | } |
9117 | ||
d764a8e6 IS |
9118 | fprintf (fp, "@end\n"); |
9119 | } | |
e31c7eec | 9120 | |
d764a8e6 IS |
9121 | #if 0 |
9122 | /* Produce the pretty printing for an Objective-C method. This is | |
9123 | currently unused, but could be handy while reorganizing the pretty | |
9124 | printing to be more robust. */ | |
9125 | static const char * | |
9126 | objc_pretty_print_method (bool is_class_method, | |
9127 | const char *class_name, | |
9128 | const char *category_name, | |
9129 | const char *selector) | |
9130 | { | |
9131 | if (category_name) | |
9132 | { | |
944fb799 | 9133 | char *result = XNEWVEC (char, strlen (class_name) + strlen (category_name) |
d764a8e6 | 9134 | + strlen (selector) + 7); |
6c65299b | 9135 | |
d764a8e6 IS |
9136 | if (is_class_method) |
9137 | sprintf (result, "+[%s(%s) %s]", class_name, category_name, selector); | |
9138 | else | |
9139 | sprintf (result, "-[%s(%s) %s]", class_name, category_name, selector); | |
6c65299b | 9140 | |
d764a8e6 IS |
9141 | return result; |
9142 | } | |
9143 | else | |
9144 | { | |
9145 | char *result = XNEWVEC (char, strlen (class_name) | |
9146 | + strlen (selector) + 5); | |
6c65299b | 9147 | |
d764a8e6 IS |
9148 | if (is_class_method) |
9149 | sprintf (result, "+[%s %s]", class_name, selector); | |
9150 | else | |
9151 | sprintf (result, "-[%s %s]", class_name, selector); | |
6c65299b | 9152 | |
944fb799 | 9153 | return result; |
d764a8e6 IS |
9154 | } |
9155 | } | |
9156 | #endif | |
6c65299b | 9157 | |
d764a8e6 IS |
9158 | /* Demangle function for Objective-C. Attempt to demangle the |
9159 | function name associated with a method (eg, going from | |
9160 | "_i_NSObject__class" to "-[NSObject class]"); usually for the | |
9161 | purpose of pretty printing or error messages. Return the demangled | |
9162 | name, or NULL if the string is not an Objective-C mangled method | |
9163 | name. | |
9164 | ||
9165 | Because of how the mangling is done, any method that has a '_' in | |
9166 | its original name is at risk of being demangled incorrectly. In | |
9167 | some cases there are multiple valid ways to demangle a method name | |
9168 | and there is no way we can decide. | |
9169 | ||
9170 | TODO: objc_demangle() can't always get it right; the right way to | |
9171 | get this correct for all method names would be to store the | |
9172 | Objective-C method name somewhere in the function decl. Then, | |
9173 | there is no demangling to do; we'd just pull the method name out of | |
9174 | the decl. As an additional bonus, when printing error messages we | |
9175 | could check for such a method name, and if we find it, we know the | |
9176 | function is actually an Objective-C method and we could print error | |
9177 | messages saying "In method '+[NSObject class]" instead of "In | |
9178 | function '+[NSObject class]" as we do now. */ | |
9179 | static const char * | |
9180 | objc_demangle (const char *mangled) | |
9181 | { | |
9182 | char *demangled, *cp; | |
9183 | ||
9184 | /* First of all, if the name is too short it can't be an Objective-C | |
9185 | mangled method name. */ | |
9186 | if (mangled[0] == '\0' || mangled[1] == '\0' || mangled[2] == '\0') | |
9187 | return NULL; | |
9188 | ||
9189 | /* If the name looks like an already demangled one, return it | |
9190 | unchanged. This should only happen on Darwin, where method names | |
9191 | are mangled differently into a pretty-print form (such as | |
9192 | '+[NSObject class]', see darwin.h). In that case, demangling is | |
9193 | a no-op, but we need to return the demangled name if it was an | |
9194 | ObjC one, and return NULL if not. We should be safe as no C/C++ | |
9195 | function can start with "-[" or "+[". */ | |
9196 | if ((mangled[0] == '-' || mangled[0] == '+') | |
9197 | && (mangled[1] == '[')) | |
9198 | return mangled; | |
9199 | ||
9200 | if (mangled[0] == '_' && | |
9201 | (mangled[1] == 'i' || mangled[1] == 'c') && | |
9202 | mangled[2] == '_') | |
9203 | { | |
9204 | cp = demangled = XNEWVEC (char, strlen(mangled) + 2); | |
9205 | if (mangled[1] == 'i') | |
9206 | *cp++ = '-'; /* for instance method */ | |
9207 | else | |
9208 | *cp++ = '+'; /* for class method */ | |
9209 | *cp++ = '['; /* opening left brace */ | |
9210 | strcpy(cp, mangled+3); /* tack on the rest of the mangled name */ | |
9211 | while (*cp && *cp == '_') | |
9212 | cp++; /* skip any initial underbars in class name */ | |
9213 | cp = strchr(cp, '_'); /* find first non-initial underbar */ | |
9214 | if (cp == NULL) | |
50ea5861 | 9215 | { |
d764a8e6 IS |
9216 | free(demangled); /* not mangled name */ |
9217 | return NULL; | |
50ea5861 | 9218 | } |
d764a8e6 | 9219 | if (cp[1] == '_') /* easy case: no category name */ |
143dc646 | 9220 | { |
d764a8e6 IS |
9221 | *cp++ = ' '; /* replace two '_' with one ' ' */ |
9222 | strcpy(cp, mangled + (cp - demangled) + 2); | |
143dc646 | 9223 | } |
6c65299b | 9224 | else |
d4e72c58 | 9225 | { |
d764a8e6 IS |
9226 | *cp++ = '('; /* less easy case: category name */ |
9227 | cp = strchr(cp, '_'); | |
9228 | if (cp == 0) | |
d4e72c58 | 9229 | { |
d764a8e6 IS |
9230 | free(demangled); /* not mangled name */ |
9231 | return NULL; | |
d4e72c58 | 9232 | } |
d764a8e6 IS |
9233 | *cp++ = ')'; |
9234 | *cp++ = ' '; /* overwriting 1st char of method name... */ | |
9235 | strcpy(cp, mangled + (cp - demangled)); /* get it back */ | |
d4e72c58 | 9236 | } |
d764a8e6 IS |
9237 | /* Now we have the method name. We need to generally replace |
9238 | '_' with ':' but trying to preserve '_' if it could only have | |
9239 | been in the mangled string because it was already in the | |
9240 | original name. In cases where it's ambiguous, we assume that | |
9241 | any '_' originated from a ':'. */ | |
51900510 | 9242 | |
d764a8e6 IS |
9243 | /* Initial '_'s in method name can't have been generating by |
9244 | converting ':'s. Skip them. */ | |
9245 | while (*cp && *cp == '_') | |
9246 | cp++; | |
d4e72c58 | 9247 | |
d764a8e6 IS |
9248 | /* If the method name does not end with '_', then it has no |
9249 | arguments and there was no replacement of ':'s with '_'s | |
9250 | during mangling. Check for that case, and skip any | |
9251 | replacement if so. This at least guarantees that methods | |
9252 | with no arguments are always demangled correctly (unless the | |
9253 | original name ends with '_'). */ | |
9254 | if (*(mangled + strlen (mangled) - 1) != '_') | |
9255 | { | |
9256 | /* Skip to the end. */ | |
9257 | for (; *cp; cp++) | |
9258 | ; | |
9259 | } | |
9260 | else | |
9261 | { | |
9262 | /* Replace remaining '_' with ':'. This may get it wrong if | |
9263 | there were '_'s in the original name. In most cases it | |
9264 | is impossible to disambiguate. */ | |
9265 | for (; *cp; cp++) | |
9266 | if (*cp == '_') | |
944fb799 | 9267 | *cp = ':'; |
d764a8e6 IS |
9268 | } |
9269 | *cp++ = ']'; /* closing right brace */ | |
9270 | *cp++ = 0; /* string terminator */ | |
9271 | return demangled; | |
9272 | } | |
9273 | else | |
9274 | return NULL; /* not an objc mangled name */ | |
9275 | } | |
ec3e9f82 | 9276 | |
d764a8e6 IS |
9277 | /* Try to pretty-print a decl. If the 'decl' is an Objective-C |
9278 | specific decl, return the printable name for it. If not, return | |
9279 | NULL. */ | |
9280 | const char * | |
9281 | objc_maybe_printable_name (tree decl, int v ATTRIBUTE_UNUSED) | |
9282 | { | |
9283 | switch (TREE_CODE (decl)) | |
9284 | { | |
9285 | case FUNCTION_DECL: | |
9286 | return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl))); | |
50ea5861 | 9287 | break; |
6c65299b | 9288 | |
d764a8e6 IS |
9289 | /* The following happens when we are printing a deprecation |
9290 | warning for a method. The warn_deprecation() will end up | |
9291 | trying to print the decl for INSTANCE_METHOD_DECL or | |
9292 | CLASS_METHOD_DECL. It would be nice to be able to print | |
9293 | "-[NSObject autorelease] is deprecated", but to do that, we'd | |
9294 | need to store the class and method name in the method decl, | |
9295 | which we currently don't do. For now, just return the name | |
9296 | of the method. We don't return NULL, because that may | |
9297 | trigger further attempts to pretty-print the decl in C/C++, | |
9298 | but they wouldn't know how to pretty-print it. */ | |
9299 | case INSTANCE_METHOD_DECL: | |
9300 | case CLASS_METHOD_DECL: | |
9301 | return IDENTIFIER_POINTER (DECL_NAME (decl)); | |
9302 | break; | |
9303 | /* This happens when printing a deprecation warning for a | |
9304 | property. We may want to consider some sort of pretty | |
9305 | printing (eg, include the class name where it was declared | |
9306 | ?). */ | |
9307 | case PROPERTY_DECL: | |
9308 | return IDENTIFIER_POINTER (PROPERTY_NAME (decl)); | |
50ea5861 NP |
9309 | break; |
9310 | default: | |
d764a8e6 IS |
9311 | return NULL; |
9312 | break; | |
6c65299b | 9313 | } |
6c65299b RS |
9314 | } |
9315 | ||
d764a8e6 IS |
9316 | /* Return a printable name for 'decl'. This first tries |
9317 | objc_maybe_printable_name(), and if that fails, it returns the name | |
9318 | in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for | |
9319 | Objective-C; in Objective-C++, setting the hook is not enough | |
9320 | because lots of C++ Front-End code calls cxx_printable_name, | |
9321 | dump_decl and other C++ functions directly. So instead we have | |
9322 | modified dump_decl to call objc_maybe_printable_name directly. */ | |
9323 | const char * | |
9324 | objc_printable_name (tree decl, int v) | |
9325 | { | |
9326 | const char *demangled_name = objc_maybe_printable_name (decl, v); | |
6c65299b | 9327 | |
d764a8e6 IS |
9328 | if (demangled_name != NULL) |
9329 | return demangled_name; | |
9330 | else | |
9331 | return IDENTIFIER_POINTER (DECL_NAME (decl)); | |
9332 | } | |
6c65299b | 9333 | |
944fb799 MS |
9334 | /* Routine is called to issue diagnostic when reference to a private |
9335 | ivar is made and no other variable with same name is found in | |
d764a8e6 IS |
9336 | current scope. */ |
9337 | bool | |
9338 | objc_diagnose_private_ivar (tree id) | |
9339 | { | |
9340 | tree ivar; | |
9341 | if (!objc_method_context) | |
9342 | return false; | |
9343 | ivar = is_ivar (objc_ivar_chain, id); | |
9344 | if (ivar && is_private (ivar)) | |
9345 | { | |
944fb799 | 9346 | error ("instance variable %qs is declared private", |
d764a8e6 IS |
9347 | IDENTIFIER_POINTER (id)); |
9348 | return true; | |
9349 | } | |
9350 | return false; | |
9351 | } | |
6c65299b | 9352 | |
d764a8e6 IS |
9353 | /* Look up ID as an instance variable. OTHER contains the result of |
9354 | the C or C++ lookup, which we may want to use instead. */ | |
9355 | /* To use properties inside an instance method, use self.property. */ | |
9356 | tree | |
9357 | objc_lookup_ivar (tree other, tree id) | |
9358 | { | |
9359 | tree ivar; | |
6c65299b | 9360 | |
d764a8e6 IS |
9361 | /* If we are not inside of an ObjC method, ivar lookup makes no sense. */ |
9362 | if (!objc_method_context) | |
9363 | return other; | |
6c65299b | 9364 | |
d764a8e6 IS |
9365 | if (!strcmp (IDENTIFIER_POINTER (id), "super")) |
9366 | /* We have a message to super. */ | |
9367 | return get_super_receiver (); | |
6c65299b | 9368 | |
d764a8e6 IS |
9369 | /* In a class method, look up an instance variable only as a last |
9370 | resort. */ | |
9371 | if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL | |
9372 | && other && other != error_mark_node) | |
9373 | return other; | |
9374 | ||
dcaaa5a0 DP |
9375 | /* Don't look up the ivar if the user has explicitly advised against |
9376 | it with -fno-local-ivars. */ | |
9377 | ||
9378 | if (!flag_local_ivars) | |
9379 | return other; | |
9380 | ||
d764a8e6 IS |
9381 | /* Look up the ivar, but do not use it if it is not accessible. */ |
9382 | ivar = is_ivar (objc_ivar_chain, id); | |
944fb799 | 9383 | |
d764a8e6 IS |
9384 | if (!ivar || is_private (ivar)) |
9385 | return other; | |
9386 | ||
9387 | /* In an instance method, a local variable (or parameter) may hide the | |
9388 | instance variable. */ | |
9389 | if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL | |
9390 | && other && other != error_mark_node | |
6408ef33 | 9391 | #ifdef OBJCPLUS |
d764a8e6 IS |
9392 | && CP_DECL_CONTEXT (other) != global_namespace) |
9393 | #else | |
9394 | && !DECL_FILE_SCOPE_P (other)) | |
9395 | #endif | |
9396 | { | |
dcaaa5a0 DP |
9397 | if (warn_shadow_ivar == 1 || (warn_shadow && warn_shadow_ivar != 0)) { |
9398 | warning (warn_shadow_ivar ? OPT_Wshadow_ivar : OPT_Wshadow, | |
9399 | "local declaration of %qE hides instance variable", id); | |
9400 | } | |
9401 | ||
d764a8e6 | 9402 | return other; |
6c65299b | 9403 | } |
6c65299b | 9404 | |
d764a8e6 IS |
9405 | /* At this point, we are either in an instance method with no obscuring |
9406 | local definitions, or in a class method with no alternate definitions | |
9407 | at all. */ | |
9408 | return build_ivar_reference (id); | |
668ea4b1 IS |
9409 | } |
9410 | ||
d764a8e6 IS |
9411 | /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This |
9412 | needs to be done if we are calling a function through a cast. */ | |
9413 | ||
9414 | tree | |
9415 | objc_rewrite_function_call (tree function, tree first_param) | |
8f07a2aa | 9416 | { |
d764a8e6 IS |
9417 | if (TREE_CODE (function) == NOP_EXPR |
9418 | && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR | |
9419 | && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0)) | |
9420 | == FUNCTION_DECL) | |
9421 | { | |
9422 | function = build3 (OBJ_TYPE_REF, TREE_TYPE (function), | |
9423 | TREE_OPERAND (function, 0), | |
9424 | first_param, size_zero_node); | |
9425 | } | |
8f07a2aa | 9426 | |
d764a8e6 IS |
9427 | return function; |
9428 | } | |
8f07a2aa | 9429 | |
d764a8e6 IS |
9430 | /* This is called to "gimplify" a PROPERTY_REF node. It builds the |
9431 | corresponding 'getter' function call. Note that we assume the | |
9432 | PROPERTY_REF to be valid since we generated it while parsing. */ | |
9433 | static void | |
9434 | objc_gimplify_property_ref (tree *expr_p) | |
9435 | { | |
9436 | tree getter = PROPERTY_REF_GETTER_CALL (*expr_p); | |
9437 | tree call_exp; | |
8f07a2aa | 9438 | |
d764a8e6 | 9439 | if (getter == NULL_TREE) |
8f07a2aa | 9440 | { |
d764a8e6 IS |
9441 | tree property_decl = PROPERTY_REF_PROPERTY_DECL (*expr_p); |
9442 | /* This can happen if DECL_ARTIFICIAL (*expr_p), but | |
9443 | should be impossible for real properties, which always | |
9444 | have a getter. */ | |
9445 | error_at (EXPR_LOCATION (*expr_p), "no %qs getter found", | |
9446 | IDENTIFIER_POINTER (PROPERTY_NAME (property_decl))); | |
9447 | /* Try to recover from the error to prevent an ICE. We take | |
9448 | zero and cast it to the type of the property. */ | |
9449 | *expr_p = convert (TREE_TYPE (property_decl), | |
9450 | integer_zero_node); | |
9451 | return; | |
8f07a2aa | 9452 | } |
8f07a2aa | 9453 | |
d764a8e6 IS |
9454 | if (PROPERTY_REF_DEPRECATED_GETTER (*expr_p)) |
9455 | { | |
9456 | /* PROPERTY_REF_DEPRECATED_GETTER contains the method prototype | |
9457 | that is deprecated. */ | |
9458 | warn_deprecated_use (PROPERTY_REF_DEPRECATED_GETTER (*expr_p), | |
9459 | NULL_TREE); | |
8f07a2aa | 9460 | } |
8f07a2aa | 9461 | |
d764a8e6 IS |
9462 | call_exp = getter; |
9463 | #ifdef OBJCPLUS | |
9464 | /* In C++, a getter which returns an aggregate value results in a | |
9465 | target_expr which initializes a temporary to the call | |
9466 | expression. */ | |
9467 | if (TREE_CODE (getter) == TARGET_EXPR) | |
8f07a2aa | 9468 | { |
d764a8e6 IS |
9469 | gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter))); |
9470 | gcc_assert (TREE_CODE (TREE_OPERAND (getter, 0)) == VAR_DECL); | |
9471 | call_exp = TREE_OPERAND (getter, 1); | |
8f07a2aa | 9472 | } |
d764a8e6 IS |
9473 | #endif |
9474 | gcc_assert (TREE_CODE (call_exp) == CALL_EXPR); | |
944fb799 | 9475 | |
d764a8e6 | 9476 | *expr_p = call_exp; |
8f07a2aa NP |
9477 | } |
9478 | ||
d764a8e6 IS |
9479 | /* This is called when "gimplifying" the trees. We need to gimplify |
9480 | the Objective-C/Objective-C++ specific trees, then hand over the | |
9481 | process to C/C++. */ | |
9482 | int | |
9483 | objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p) | |
668ea4b1 | 9484 | { |
d764a8e6 IS |
9485 | enum tree_code code = TREE_CODE (*expr_p); |
9486 | switch (code) | |
8f07a2aa | 9487 | { |
d764a8e6 IS |
9488 | /* Look for the special case of OBJC_TYPE_REF with the address |
9489 | of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend | |
9490 | or one of its cousins). */ | |
9491 | case OBJ_TYPE_REF: | |
9492 | if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR | |
9493 | && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0)) | |
9494 | == FUNCTION_DECL) | |
8f07a2aa | 9495 | { |
d764a8e6 | 9496 | enum gimplify_status r0, r1; |
8f07a2aa | 9497 | |
d764a8e6 IS |
9498 | /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the |
9499 | value of the OBJ_TYPE_REF, so force them to be emitted | |
9500 | during subexpression evaluation rather than after the | |
9501 | OBJ_TYPE_REF. This permits objc_msgSend calls in | |
9502 | Objective C to use direct rather than indirect calls when | |
9503 | the object expression has a postincrement. */ | |
9504 | r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL, | |
9505 | is_gimple_val, fb_rvalue); | |
9506 | r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p, | |
9507 | is_gimple_val, fb_rvalue); | |
944fb799 | 9508 | |
d764a8e6 | 9509 | return MIN (r0, r1); |
8f07a2aa NP |
9510 | } |
9511 | break; | |
d764a8e6 IS |
9512 | case PROPERTY_REF: |
9513 | objc_gimplify_property_ref (expr_p); | |
9514 | /* Do not return yet; let C/C++ gimplify the resulting expression. */ | |
9515 | break; | |
8f07a2aa | 9516 | default: |
d764a8e6 | 9517 | break; |
8f07a2aa | 9518 | } |
46a88c12 | 9519 | |
668ea4b1 | 9520 | #ifdef OBJCPLUS |
d764a8e6 | 9521 | return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p); |
668ea4b1 | 9522 | #else |
d764a8e6 | 9523 | return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p); |
668ea4b1 | 9524 | #endif |
668ea4b1 IS |
9525 | } |
9526 | ||
d764a8e6 IS |
9527 | /* --- FAST ENUMERATION --- */ |
9528 | /* Begin code generation for fast enumeration (foreach) ... */ | |
668ea4b1 | 9529 | |
d764a8e6 | 9530 | /* Defines |
668ea4b1 | 9531 | |
d764a8e6 IS |
9532 | struct __objcFastEnumerationState |
9533 | { | |
9534 | unsigned long state; | |
9535 | id *itemsPtr; | |
9536 | unsigned long *mutationsPtr; | |
9537 | unsigned long extra[5]; | |
9538 | }; | |
668ea4b1 | 9539 | |
d764a8e6 | 9540 | Confusingly enough, NSFastEnumeration is then defined by libraries |
944fb799 | 9541 | to be the same structure. |
d764a8e6 | 9542 | */ |
668ea4b1 | 9543 | |
d764a8e6 IS |
9544 | static void |
9545 | build_fast_enumeration_state_template (void) | |
9546 | { | |
9547 | tree decls, *chain = NULL; | |
46a88c12 | 9548 | |
d764a8e6 | 9549 | /* { */ |
944fb799 | 9550 | objc_fast_enumeration_state_template = objc_start_struct (get_identifier |
d764a8e6 | 9551 | (TAG_FAST_ENUMERATION_STATE)); |
668ea4b1 | 9552 | |
d764a8e6 IS |
9553 | /* unsigned long state; */ |
9554 | decls = add_field_decl (long_unsigned_type_node, "state", &chain); | |
46a88c12 | 9555 | |
d764a8e6 | 9556 | /* id *itemsPtr; */ |
944fb799 | 9557 | add_field_decl (build_pointer_type (objc_object_type), |
d764a8e6 | 9558 | "itemsPtr", &chain); |
46a88c12 | 9559 | |
d764a8e6 | 9560 | /* unsigned long *mutationsPtr; */ |
944fb799 | 9561 | add_field_decl (build_pointer_type (long_unsigned_type_node), |
d764a8e6 | 9562 | "mutationsPtr", &chain); |
8f07a2aa | 9563 | |
d764a8e6 | 9564 | /* unsigned long extra[5]; */ |
944fb799 | 9565 | add_field_decl (build_sized_array_type (long_unsigned_type_node, 5), |
d764a8e6 | 9566 | "extra", &chain); |
8f07a2aa | 9567 | |
d764a8e6 IS |
9568 | /* } */ |
9569 | objc_finish_struct (objc_fast_enumeration_state_template, decls); | |
9570 | } | |
8f07a2aa | 9571 | |
d764a8e6 IS |
9572 | /* |
9573 | 'objc_finish_foreach_loop()' generates the code for an Objective-C | |
9574 | foreach loop. The 'location' argument is the location of the 'for' | |
9575 | that starts the loop. The 'object_expression' is the expression of | |
9576 | the 'object' that iterates; the 'collection_expression' is the | |
9577 | expression of the collection that we iterate over (we need to make | |
9578 | sure we evaluate this only once); the 'for_body' is the set of | |
9579 | statements to be executed in each iteration; 'break_label' and | |
9580 | 'continue_label' are the break and continue labels which we need to | |
9581 | emit since the <statements> may be jumping to 'break_label' (if they | |
9582 | contain 'break') or to 'continue_label' (if they contain | |
9583 | 'continue'). | |
8f07a2aa | 9584 | |
d764a8e6 | 9585 | The syntax is |
944fb799 | 9586 | |
d764a8e6 IS |
9587 | for (<object expression> in <collection expression>) |
9588 | <statements> | |
8f07a2aa | 9589 | |
d764a8e6 IS |
9590 | which is compiled into the following blurb: |
9591 | ||
9592 | { | |
9593 | id __objc_foreach_collection; | |
9594 | __objc_fast_enumeration_state __objc_foreach_enum_state; | |
9595 | unsigned long __objc_foreach_batchsize; | |
9596 | id __objc_foreach_items[16]; | |
9597 | __objc_foreach_collection = <collection expression>; | |
9598 | __objc_foreach_enum_state = { 0 }; | |
9599 | __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; | |
944fb799 | 9600 | |
d764a8e6 IS |
9601 | if (__objc_foreach_batchsize == 0) |
9602 | <object expression> = nil; | |
9603 | else | |
9604 | { | |
9605 | unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; | |
9606 | next_batch: | |
8f07a2aa | 9607 | { |
d764a8e6 IS |
9608 | unsigned long __objc_foreach_index; |
9609 | __objc_foreach_index = 0; | |
8f07a2aa | 9610 | |
d764a8e6 IS |
9611 | next_object: |
9612 | if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); | |
9613 | <object expression> = enumState.itemsPtr[__objc_foreach_index]; | |
9614 | <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] | |
8f07a2aa | 9615 | |
d764a8e6 IS |
9616 | continue_label: |
9617 | __objc_foreach_index++; | |
9618 | if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; | |
9619 | __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; | |
9620 | } | |
9621 | if (__objc_foreach_batchsize != 0) goto next_batch; | |
9622 | <object expression> = nil; | |
9623 | break_label: | |
8f07a2aa | 9624 | } |
d764a8e6 | 9625 | } |
8f07a2aa | 9626 | |
d764a8e6 IS |
9627 | 'statements' may contain a 'continue' or 'break' instruction, which |
9628 | the user expects to 'continue' or 'break' the entire foreach loop. | |
9629 | We are provided the labels that 'break' and 'continue' jump to, so | |
9630 | we place them where we want them to jump to when they pick them. | |
944fb799 | 9631 | |
d764a8e6 IS |
9632 | Optimization TODO: we could cache the IMP of |
9633 | countByEnumeratingWithState:objects:count:. | |
9634 | */ | |
8f07a2aa | 9635 | |
d764a8e6 IS |
9636 | /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */ |
9637 | /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */ | |
8f07a2aa | 9638 | |
d764a8e6 IS |
9639 | #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP |
9640 | #include "tree-pretty-print.h" | |
668ea4b1 | 9641 | #endif |
668ea4b1 | 9642 | |
d764a8e6 | 9643 | void |
944fb799 | 9644 | objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body, |
d764a8e6 | 9645 | tree break_label, tree continue_label) |
46a88c12 | 9646 | { |
d764a8e6 IS |
9647 | /* A tree representing the __objcFastEnumerationState struct type, |
9648 | or NSFastEnumerationState struct, whatever we are using. */ | |
9649 | tree objc_fast_enumeration_state_type; | |
46a88c12 | 9650 | |
d764a8e6 IS |
9651 | /* The trees representing the declarations of each of the local variables. */ |
9652 | tree objc_foreach_collection_decl; | |
9653 | tree objc_foreach_enum_state_decl; | |
9654 | tree objc_foreach_items_decl; | |
9655 | tree objc_foreach_batchsize_decl; | |
9656 | tree objc_foreach_mutations_pointer_decl; | |
9657 | tree objc_foreach_index_decl; | |
46a88c12 | 9658 | |
d764a8e6 IS |
9659 | /* A tree representing the selector countByEnumeratingWithState:objects:count:. */ |
9660 | tree selector_name; | |
46a88c12 | 9661 | |
d764a8e6 IS |
9662 | /* A tree representing the local bind. */ |
9663 | tree bind; | |
46a88c12 | 9664 | |
d764a8e6 IS |
9665 | /* A tree representing the external 'if (__objc_foreach_batchsize)' */ |
9666 | tree first_if; | |
46a88c12 | 9667 | |
d764a8e6 IS |
9668 | /* A tree representing the 'else' part of 'first_if' */ |
9669 | tree first_else; | |
46a88c12 | 9670 | |
d764a8e6 IS |
9671 | /* A tree representing the 'next_batch' label. */ |
9672 | tree next_batch_label_decl; | |
46a88c12 | 9673 | |
d764a8e6 IS |
9674 | /* A tree representing the binding after the 'next_batch' label. */ |
9675 | tree next_batch_bind; | |
46a88c12 | 9676 | |
d764a8e6 IS |
9677 | /* A tree representing the 'next_object' label. */ |
9678 | tree next_object_label_decl; | |
46a88c12 | 9679 | |
d764a8e6 IS |
9680 | /* Temporary variables. */ |
9681 | tree t; | |
9682 | int i; | |
46a88c12 | 9683 | |
22d8d616 | 9684 | if (flag_objc1_only) |
d764a8e6 | 9685 | error_at (location, "fast enumeration is not available in Objective-C 1.0"); |
22d8d616 | 9686 | |
d764a8e6 | 9687 | if (object_expression == error_mark_node) |
da57d1b9 NP |
9688 | return; |
9689 | ||
d764a8e6 IS |
9690 | if (collection_expression == error_mark_node) |
9691 | return; | |
46a88c12 | 9692 | |
d764a8e6 | 9693 | if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression), true)) |
46a88c12 | 9694 | { |
d764a8e6 | 9695 | error_at (location, "iterating variable in fast enumeration is not an object"); |
46a88c12 NP |
9696 | return; |
9697 | } | |
9698 | ||
d764a8e6 | 9699 | if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression), true)) |
46a88c12 | 9700 | { |
d764a8e6 | 9701 | error_at (location, "collection in fast enumeration is not an object"); |
46a88c12 NP |
9702 | return; |
9703 | } | |
9704 | ||
d764a8e6 IS |
9705 | /* TODO: Check that object_expression is either a variable |
9706 | declaration, or an lvalue. */ | |
46a88c12 | 9707 | |
d764a8e6 IS |
9708 | /* This kludge is an idea from apple. We use the |
9709 | __objcFastEnumerationState struct implicitly defined by the | |
9710 | compiler, unless a NSFastEnumerationState struct has been defined | |
9711 | (by a Foundation library such as GNUstep Base) in which case, we | |
9712 | use that one. | |
9713 | */ | |
9714 | objc_fast_enumeration_state_type = objc_fast_enumeration_state_template; | |
9715 | { | |
9716 | tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState")); | |
46a88c12 | 9717 | |
d764a8e6 | 9718 | if (objc_NSFastEnumeration_type) |
46a88c12 | 9719 | { |
d764a8e6 IS |
9720 | /* TODO: We really need to check that |
9721 | objc_NSFastEnumeration_type is the same as ours! */ | |
9722 | if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL) | |
9723 | { | |
9724 | /* If it's a typedef, use the original type. */ | |
9725 | if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type)) | |
9726 | objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type); | |
9727 | else | |
944fb799 | 9728 | objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type); |
d764a8e6 | 9729 | } |
46a88c12 | 9730 | } |
d764a8e6 | 9731 | } |
46a88c12 | 9732 | |
d764a8e6 IS |
9733 | /* { */ |
9734 | /* Done by c-parser.c. */ | |
46a88c12 | 9735 | |
d764a8e6 IS |
9736 | /* type object; */ |
9737 | /* Done by c-parser.c. */ | |
da57d1b9 | 9738 | |
d764a8e6 | 9739 | /* Disable warnings that 'object' is unused. For example the code |
46a88c12 | 9740 | |
d764a8e6 IS |
9741 | for (id object in collection) |
9742 | i++; | |
da57d1b9 | 9743 | |
d764a8e6 IS |
9744 | which can be used to count how many objects there are in the |
9745 | collection is fine and should generate no warnings even if | |
9746 | 'object' is technically unused. */ | |
9747 | TREE_USED (object_expression) = 1; | |
9748 | if (DECL_P (object_expression)) | |
9749 | DECL_READ_P (object_expression) = 1; | |
46a88c12 | 9750 | |
d764a8e6 IS |
9751 | /* id __objc_foreach_collection */ |
9752 | objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection"); | |
22d8d616 | 9753 | |
d764a8e6 IS |
9754 | /* __objcFastEnumerationState __objc_foreach_enum_state; */ |
9755 | objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state"); | |
9756 | TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl; | |
da57d1b9 | 9757 | |
d764a8e6 IS |
9758 | /* id __objc_foreach_items[16]; */ |
9759 | objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items"); | |
9760 | TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl; | |
da57d1b9 | 9761 | |
d764a8e6 IS |
9762 | /* unsigned long __objc_foreach_batchsize; */ |
9763 | objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize"); | |
9764 | TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl; | |
9a179d01 | 9765 | |
d764a8e6 IS |
9766 | /* Generate the local variable binding. */ |
9767 | bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL); | |
9768 | SET_EXPR_LOCATION (bind, location); | |
9769 | TREE_SIDE_EFFECTS (bind) = 1; | |
944fb799 | 9770 | |
d764a8e6 IS |
9771 | /* __objc_foreach_collection = <collection expression>; */ |
9772 | t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression); | |
9773 | SET_EXPR_LOCATION (t, location); | |
9774 | append_to_statement_list (t, &BIND_EXPR_BODY (bind)); | |
9775 | /* We have used 'collection_expression'. */ | |
9776 | mark_exp_read (collection_expression); | |
46a88c12 | 9777 | |
d764a8e6 | 9778 | /* __objc_foreach_enum_state.state = 0; */ |
944fb799 | 9779 | t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl, |
d764a8e6 IS |
9780 | get_identifier ("state")), |
9781 | build_int_cst (long_unsigned_type_node, 0)); | |
9782 | SET_EXPR_LOCATION (t, location); | |
9783 | append_to_statement_list (t, &BIND_EXPR_BODY (bind)); | |
9784 | ||
9785 | /* __objc_foreach_enum_state.itemsPtr = NULL; */ | |
944fb799 | 9786 | t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl, |
d764a8e6 IS |
9787 | get_identifier ("itemsPtr")), |
9788 | null_pointer_node); | |
9789 | SET_EXPR_LOCATION (t, location); | |
9790 | append_to_statement_list (t, &BIND_EXPR_BODY (bind)); | |
9791 | ||
9792 | /* __objc_foreach_enum_state.mutationsPtr = NULL; */ | |
944fb799 | 9793 | t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl, |
d764a8e6 IS |
9794 | get_identifier ("mutationsPtr")), |
9795 | null_pointer_node); | |
9796 | SET_EXPR_LOCATION (t, location); | |
9797 | append_to_statement_list (t, &BIND_EXPR_BODY (bind)); | |
9798 | ||
9799 | /* __objc_foreach_enum_state.extra[0] = 0; */ | |
9800 | /* __objc_foreach_enum_state.extra[1] = 0; */ | |
9801 | /* __objc_foreach_enum_state.extra[2] = 0; */ | |
9802 | /* __objc_foreach_enum_state.extra[3] = 0; */ | |
9803 | /* __objc_foreach_enum_state.extra[4] = 0; */ | |
9804 | for (i = 0; i < 5 ; i++) | |
46a88c12 | 9805 | { |
d764a8e6 | 9806 | t = build2 (MODIFY_EXPR, void_type_node, |
944fb799 | 9807 | build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl, |
d764a8e6 IS |
9808 | get_identifier ("extra")), |
9809 | build_int_cst (NULL_TREE, i)), | |
9810 | build_int_cst (long_unsigned_type_node, 0)); | |
9811 | SET_EXPR_LOCATION (t, location); | |
9812 | append_to_statement_list (t, &BIND_EXPR_BODY (bind)); | |
46a88c12 | 9813 | } |
944fb799 | 9814 | |
d764a8e6 IS |
9815 | /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */ |
9816 | selector_name = get_identifier ("countByEnumeratingWithState:objects:count:"); | |
9817 | #ifdef OBJCPLUS | |
9818 | t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name, | |
9819 | /* Parameters. */ | |
9820 | tree_cons /* &__objc_foreach_enum_state */ | |
9821 | (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl), | |
9822 | tree_cons /* __objc_foreach_items */ | |
9823 | (NULL_TREE, objc_foreach_items_decl, | |
9824 | tree_cons /* 16 */ | |
9825 | (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL); | |
9826 | #else | |
9827 | /* In C, we need to decay the __objc_foreach_items array that we are passing. */ | |
9828 | { | |
9829 | struct c_expr array; | |
9830 | array.value = objc_foreach_items_decl; | |
9831 | t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name, | |
9832 | /* Parameters. */ | |
9833 | tree_cons /* &__objc_foreach_enum_state */ | |
9834 | (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl), | |
9835 | tree_cons /* __objc_foreach_items */ | |
9836 | (NULL_TREE, default_function_array_conversion (location, array).value, | |
9837 | tree_cons /* 16 */ | |
9838 | (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL); | |
9839 | } | |
9840 | #endif | |
9841 | t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl, | |
9842 | convert (long_unsigned_type_node, t)); | |
9843 | SET_EXPR_LOCATION (t, location); | |
9844 | append_to_statement_list (t, &BIND_EXPR_BODY (bind)); | |
da57d1b9 | 9845 | |
d764a8e6 | 9846 | /* if (__objc_foreach_batchsize == 0) */ |
944fb799 | 9847 | first_if = build3 (COND_EXPR, void_type_node, |
d764a8e6 | 9848 | /* Condition. */ |
944fb799 MS |
9849 | c_fully_fold |
9850 | (c_common_truthvalue_conversion | |
9851 | (location, | |
d764a8e6 | 9852 | build_binary_op (location, |
944fb799 | 9853 | EQ_EXPR, |
d764a8e6 IS |
9854 | objc_foreach_batchsize_decl, |
9855 | build_int_cst (long_unsigned_type_node, 0), 1)), | |
9856 | false, NULL), | |
9857 | /* Then block (we fill it in later). */ | |
9858 | NULL_TREE, | |
9859 | /* Else block (we fill it in later). */ | |
9860 | NULL_TREE); | |
9861 | SET_EXPR_LOCATION (first_if, location); | |
9862 | append_to_statement_list (first_if, &BIND_EXPR_BODY (bind)); | |
668ea4b1 | 9863 | |
d764a8e6 IS |
9864 | /* then <object expression> = nil; */ |
9865 | t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node)); | |
9866 | SET_EXPR_LOCATION (t, location); | |
9867 | COND_EXPR_THEN (first_if) = t; | |
668ea4b1 | 9868 | |
d764a8e6 IS |
9869 | /* Now we build the 'else' part of the if; once we finish building |
9870 | it, we attach it to first_if as the 'else' part. */ | |
46a88c12 | 9871 | |
d764a8e6 IS |
9872 | /* else */ |
9873 | /* { */ | |
46a88c12 | 9874 | |
d764a8e6 IS |
9875 | /* unsigned long __objc_foreach_mutations_pointer; */ |
9876 | objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer"); | |
668ea4b1 | 9877 | |
d764a8e6 IS |
9878 | /* Generate the local variable binding. */ |
9879 | first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL); | |
9880 | SET_EXPR_LOCATION (first_else, location); | |
9881 | TREE_SIDE_EFFECTS (first_else) = 1; | |
e31c7eec | 9882 | |
d764a8e6 | 9883 | /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */ |
944fb799 MS |
9884 | t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, |
9885 | build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl, | |
d764a8e6 IS |
9886 | get_identifier ("mutationsPtr")), |
9887 | RO_UNARY_STAR)); | |
9888 | SET_EXPR_LOCATION (t, location); | |
9889 | append_to_statement_list (t, &BIND_EXPR_BODY (first_else)); | |
668ea4b1 | 9890 | |
d764a8e6 IS |
9891 | /* next_batch: */ |
9892 | next_batch_label_decl = create_artificial_label (location); | |
944fb799 | 9893 | t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl); |
d764a8e6 IS |
9894 | SET_EXPR_LOCATION (t, location); |
9895 | append_to_statement_list (t, &BIND_EXPR_BODY (first_else)); | |
944fb799 | 9896 | |
d764a8e6 | 9897 | /* { */ |
50ea5861 | 9898 | |
d764a8e6 IS |
9899 | /* unsigned long __objc_foreach_index; */ |
9900 | objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index"); | |
4ca5d2a7 | 9901 | |
d764a8e6 IS |
9902 | /* Generate the local variable binding. */ |
9903 | next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL); | |
9904 | SET_EXPR_LOCATION (next_batch_bind, location); | |
9905 | TREE_SIDE_EFFECTS (next_batch_bind) = 1; | |
9906 | append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else)); | |
f05b9d93 | 9907 | |
d764a8e6 IS |
9908 | /* __objc_foreach_index = 0; */ |
9909 | t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl, | |
9910 | build_int_cst (long_unsigned_type_node, 0)); | |
9911 | SET_EXPR_LOCATION (t, location); | |
9912 | append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind)); | |
f05b9d93 | 9913 | |
d764a8e6 IS |
9914 | /* next_object: */ |
9915 | next_object_label_decl = create_artificial_label (location); | |
9916 | t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl); | |
9917 | SET_EXPR_LOCATION (t, location); | |
9918 | append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind)); | |
f05b9d93 | 9919 | |
d764a8e6 | 9920 | /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */ |
944fb799 | 9921 | t = build3 (COND_EXPR, void_type_node, |
d764a8e6 | 9922 | /* Condition. */ |
944fb799 MS |
9923 | c_fully_fold |
9924 | (c_common_truthvalue_conversion | |
9925 | (location, | |
9926 | build_binary_op | |
d764a8e6 | 9927 | (location, |
944fb799 | 9928 | NE_EXPR, |
d764a8e6 | 9929 | objc_foreach_mutations_pointer_decl, |
944fb799 MS |
9930 | build_indirect_ref (location, |
9931 | objc_build_component_ref (objc_foreach_enum_state_decl, | |
d764a8e6 IS |
9932 | get_identifier ("mutationsPtr")), |
9933 | RO_UNARY_STAR), 1)), | |
9934 | false, NULL), | |
9935 | /* Then block. */ | |
9936 | build_function_call (input_location, | |
9937 | objc_enumeration_mutation_decl, | |
9938 | tree_cons (NULL, collection_expression, NULL)), | |
9939 | /* Else block. */ | |
9940 | NULL_TREE); | |
9941 | SET_EXPR_LOCATION (t, location); | |
9942 | append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind)); | |
f05b9d93 | 9943 | |
d764a8e6 | 9944 | /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */ |
944fb799 MS |
9945 | t = build2 (MODIFY_EXPR, void_type_node, object_expression, |
9946 | build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl, | |
d764a8e6 IS |
9947 | get_identifier ("itemsPtr")), |
9948 | objc_foreach_index_decl)); | |
9949 | SET_EXPR_LOCATION (t, location); | |
9950 | append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind)); | |
f05b9d93 | 9951 | |
d764a8e6 IS |
9952 | /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */ |
9953 | append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind)); | |
f05b9d93 | 9954 | |
d764a8e6 IS |
9955 | /* continue_label: */ |
9956 | if (continue_label) | |
9957 | { | |
9958 | t = build1 (LABEL_EXPR, void_type_node, continue_label); | |
9959 | SET_EXPR_LOCATION (t, location); | |
9960 | append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind)); | |
9961 | } | |
f05b9d93 | 9962 | |
d764a8e6 | 9963 | /* __objc_foreach_index++; */ |
944fb799 | 9964 | t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl, |
d764a8e6 IS |
9965 | build_binary_op (location, |
9966 | PLUS_EXPR, | |
9967 | objc_foreach_index_decl, | |
9968 | build_int_cst (long_unsigned_type_node, 1), 1)); | |
9969 | SET_EXPR_LOCATION (t, location); | |
9970 | append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind)); | |
f05b9d93 | 9971 | |
d764a8e6 | 9972 | /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */ |
944fb799 | 9973 | t = build3 (COND_EXPR, void_type_node, |
d764a8e6 | 9974 | /* Condition. */ |
944fb799 MS |
9975 | c_fully_fold |
9976 | (c_common_truthvalue_conversion | |
9977 | (location, | |
d764a8e6 | 9978 | build_binary_op (location, |
944fb799 | 9979 | LT_EXPR, |
d764a8e6 IS |
9980 | objc_foreach_index_decl, |
9981 | objc_foreach_batchsize_decl, 1)), | |
9982 | false, NULL), | |
9983 | /* Then block. */ | |
9984 | build1 (GOTO_EXPR, void_type_node, next_object_label_decl), | |
9985 | /* Else block. */ | |
9986 | NULL_TREE); | |
9987 | SET_EXPR_LOCATION (t, location); | |
9988 | append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind)); | |
944fb799 | 9989 | |
d764a8e6 IS |
9990 | /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */ |
9991 | #ifdef OBJCPLUS | |
9992 | t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name, | |
9993 | /* Parameters. */ | |
9994 | tree_cons /* &__objc_foreach_enum_state */ | |
9995 | (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl), | |
9996 | tree_cons /* __objc_foreach_items */ | |
9997 | (NULL_TREE, objc_foreach_items_decl, | |
9998 | tree_cons /* 16 */ | |
9999 | (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL); | |
10000 | #else | |
10001 | /* In C, we need to decay the __objc_foreach_items array that we are passing. */ | |
f05b9d93 | 10002 | { |
d764a8e6 IS |
10003 | struct c_expr array; |
10004 | array.value = objc_foreach_items_decl; | |
10005 | t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name, | |
10006 | /* Parameters. */ | |
10007 | tree_cons /* &__objc_foreach_enum_state */ | |
10008 | (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl), | |
10009 | tree_cons /* __objc_foreach_items */ | |
10010 | (NULL_TREE, default_function_array_conversion (location, array).value, | |
10011 | tree_cons /* 16 */ | |
10012 | (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL); | |
10013 | } | |
10014 | #endif | |
944fb799 | 10015 | t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl, |
d764a8e6 IS |
10016 | convert (long_unsigned_type_node, t)); |
10017 | SET_EXPR_LOCATION (t, location); | |
10018 | append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind)); | |
f05b9d93 | 10019 | |
d764a8e6 | 10020 | /* } */ |
f05b9d93 | 10021 | |
d764a8e6 | 10022 | /* if (__objc_foreach_batchsize != 0) goto next_batch; */ |
944fb799 | 10023 | t = build3 (COND_EXPR, void_type_node, |
d764a8e6 | 10024 | /* Condition. */ |
944fb799 MS |
10025 | c_fully_fold |
10026 | (c_common_truthvalue_conversion | |
10027 | (location, | |
d764a8e6 | 10028 | build_binary_op (location, |
944fb799 | 10029 | NE_EXPR, |
d764a8e6 IS |
10030 | objc_foreach_batchsize_decl, |
10031 | build_int_cst (long_unsigned_type_node, 0), 1)), | |
10032 | false, NULL), | |
10033 | /* Then block. */ | |
10034 | build1 (GOTO_EXPR, void_type_node, next_batch_label_decl), | |
10035 | /* Else block. */ | |
10036 | NULL_TREE); | |
10037 | SET_EXPR_LOCATION (t, location); | |
10038 | append_to_statement_list (t, &BIND_EXPR_BODY (first_else)); | |
f05b9d93 | 10039 | |
d764a8e6 IS |
10040 | /* <object expression> = nil; */ |
10041 | t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node)); | |
10042 | SET_EXPR_LOCATION (t, location); | |
10043 | append_to_statement_list (t, &BIND_EXPR_BODY (first_else)); | |
10044 | ||
10045 | /* break_label: */ | |
10046 | if (break_label) | |
10047 | { | |
10048 | t = build1 (LABEL_EXPR, void_type_node, break_label); | |
10049 | SET_EXPR_LOCATION (t, location); | |
10050 | append_to_statement_list (t, &BIND_EXPR_BODY (first_else)); | |
10051 | } | |
f05b9d93 | 10052 | |
d764a8e6 IS |
10053 | /* } */ |
10054 | COND_EXPR_ELSE (first_if) = first_else; | |
10055 | ||
10056 | /* Do the whole thing. */ | |
10057 | add_stmt (bind); | |
f05b9d93 NP |
10058 | |
10059 | #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP | |
d764a8e6 IS |
10060 | /* This will print to stderr the whole blurb generated by the |
10061 | compiler while compiling (assuming the compiler doesn't crash | |
10062 | before getting here). | |
10063 | */ | |
10064 | debug_generic_stmt (bind); | |
f05b9d93 NP |
10065 | #endif |
10066 | ||
d764a8e6 IS |
10067 | /* } */ |
10068 | /* Done by c-parser.c */ | |
10069 | } | |
10070 | ||
10071 | /* --- SUPPORT FOR FORMAT ARG CHECKING --- */ | |
10072 | /* Return true if we have an NxString object pointer. */ | |
10073 | ||
10074 | bool | |
10075 | objc_string_ref_type_p (tree strp) | |
10076 | { | |
10077 | tree tmv; | |
10078 | if (!strp || TREE_CODE (strp) != POINTER_TYPE) | |
10079 | return false; | |
10080 | ||
10081 | tmv = TYPE_MAIN_VARIANT (TREE_TYPE (strp)); | |
10082 | tmv = OBJC_TYPE_NAME (tmv); | |
10083 | return (tmv | |
944fb799 MS |
10084 | && TREE_CODE (tmv) == IDENTIFIER_NODE |
10085 | && IDENTIFIER_POINTER (tmv) | |
d764a8e6 IS |
10086 | && !strncmp (IDENTIFIER_POINTER (tmv), "NSString", 8)); |
10087 | } | |
10088 | ||
10089 | /* At present the behavior of this is undefined and it does nothing. */ | |
f05b9d93 | 10090 | void |
944fb799 | 10091 | objc_check_format_arg (tree ARG_UNUSED (format_arg), |
d764a8e6 | 10092 | tree ARG_UNUSED (args_list)) |
f05b9d93 | 10093 | { |
d764a8e6 | 10094 | } |
f05b9d93 | 10095 | |
81f653d6 NF |
10096 | void |
10097 | objc_common_init_ts (void) | |
10098 | { | |
10099 | c_common_init_ts (); | |
10100 | ||
10101 | MARK_TS_DECL_NON_COMMON (CLASS_METHOD_DECL); | |
10102 | MARK_TS_DECL_NON_COMMON (INSTANCE_METHOD_DECL); | |
10103 | MARK_TS_DECL_NON_COMMON (KEYWORD_DECL); | |
10104 | MARK_TS_DECL_NON_COMMON (PROPERTY_DECL); | |
10105 | ||
10106 | MARK_TS_COMMON (CLASS_INTERFACE_TYPE); | |
10107 | MARK_TS_COMMON (PROTOCOL_INTERFACE_TYPE); | |
10108 | MARK_TS_COMMON (CLASS_IMPLEMENTATION_TYPE); | |
10109 | ||
10110 | MARK_TS_TYPED (MESSAGE_SEND_EXPR); | |
10111 | MARK_TS_TYPED (PROPERTY_REF); | |
10112 | } | |
10113 | ||
8643e92d | 10114 | #include "gt-objc-objc-act.h" |