]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/objc/objc-act.c
Update copyright years.
[thirdparty/gcc.git] / gcc / objc / objc-act.c
1 /* Implement classes and message passing for Objective C.
2 Copyright (C) 1992-2019 Free Software Foundation, Inc.
3 Contributed by Steve Naroff.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
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
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "stringpool.h"
27 #include "stor-layout.h"
28 #include "attribs.h"
29
30 #ifdef OBJCPLUS
31 #include "cp/cp-tree.h"
32 #else
33 #include "c/c-tree.h"
34 #include "c/c-lang.h"
35 #endif
36
37 #include "c-family/c-objc.h"
38 #include "langhooks.h"
39 #include "objc-act.h"
40 #include "objc-map.h"
41 #include "function.h"
42 #include "toplev.h"
43 #include "debug.h"
44 #include "c-family/c-target.h"
45 #include "intl.h"
46 #include "cgraph.h"
47 #include "tree-iterator.h"
48 /* Different initialization, code gen and meta data generation for each
49 runtime. */
50 #include "objc-runtime-hooks.h"
51 /* Routines used mainly by the runtimes. */
52 #include "objc-runtime-shared-support.h"
53 /* For default_tree_printer (). */
54
55 /* For enum gimplify_status */
56 #include "gimple-expr.h"
57 #include "gimplify.h"
58
59 /* For encode_method_prototype(). */
60 #include "objc-encoding.h"
61
62 static unsigned int should_call_super_dealloc = 0;
63
64 /* When building Objective-C++, we are not linking against the C front-end
65 and so need to replicate the C tree-construction functions in some way. */
66 #ifdef OBJCPLUS
67 #define OBJCP_REMAP_FUNCTIONS
68 #include "objcp-decl.h"
69 #endif /* OBJCPLUS */
70
71 /* This is the default way of generating a method name. */
72 /* This has the problem that "test_method:argument:" and
73 "test:method_argument:" will generate the same name
74 ("_i_Test__test_method_argument_" for an instance method of the
75 class "Test"), so you can't have them both in the same class!
76 Moreover, the demangling (going from
77 "_i_Test__test_method_argument" back to the original name) is
78 undefined because there are two correct ways of demangling the
79 name. */
80 #ifndef OBJC_GEN_METHOD_LABEL
81 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
82 do { \
83 char *temp; \
84 sprintf ((BUF), "_%s_%s_%s_%s", \
85 ((IS_INST) ? "i" : "c"), \
86 (CLASS_NAME), \
87 ((CAT_NAME)? (CAT_NAME) : ""), \
88 (SEL_NAME)); \
89 for (temp = (BUF); *temp; temp++) \
90 if (*temp == ':') *temp = '_'; \
91 } while (0)
92 #endif
93
94 /* These need specifying. */
95 #ifndef OBJC_FORWARDING_STACK_OFFSET
96 #define OBJC_FORWARDING_STACK_OFFSET 0
97 #endif
98
99 #ifndef OBJC_FORWARDING_MIN_OFFSET
100 #define OBJC_FORWARDING_MIN_OFFSET 0
101 #endif
102
103 /*** Private Interface (procedures) ***/
104
105 /* Init stuff. */
106 static void synth_module_prologue (void);
107
108 /* Code generation. */
109
110 static tree start_class (enum tree_code, tree, tree, tree, tree);
111 static tree continue_class (tree);
112 static void finish_class (tree);
113 static void start_method_def (tree, tree);
114
115 static tree start_protocol (enum tree_code, tree, tree, tree);
116 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
117 static tree objc_add_method (tree, tree, int, bool);
118 static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree);
119 static tree build_ivar_reference (tree);
120 static tree is_ivar (tree, tree);
121
122 /* We only need the following for ObjC; ObjC++ will use C++'s definition
123 of DERIVED_FROM_P. */
124 #ifndef OBJCPLUS
125 static bool objc_derived_from_p (tree, tree);
126 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
127 #endif
128
129 /* Property. */
130 static void objc_gen_property_data (tree, tree);
131 static void objc_synthesize_getter (tree, tree, tree);
132 static void objc_synthesize_setter (tree, tree, tree);
133 static tree lookup_property (tree, tree);
134 static tree lookup_property_in_list (tree, tree);
135 static tree lookup_property_in_protocol_list (tree, tree);
136 static void build_common_objc_property_accessor_helpers (void);
137
138 static void objc_xref_basetypes (tree, tree);
139
140 static tree get_class_ivars (tree, bool);
141
142 static void build_fast_enumeration_state_template (void);
143
144 #ifdef OBJCPLUS
145 static void objc_generate_cxx_cdtors (void);
146 #endif
147
148 /* objc attribute */
149 static void objc_decl_method_attributes (tree*, tree, int);
150 static tree build_keyword_selector (tree);
151
152 static void hash_init (void);
153
154 /* Hash tables to manage the global pool of method prototypes. Each
155 of these maps map a method name (selector) identifier to either a
156 single tree (for methods with a single method prototype) or a
157 TREE_VEC (for methods with multiple method prototypes). */
158 static GTY(()) objc_map_t instance_method_map = 0;
159 static GTY(()) objc_map_t class_method_map = 0;
160
161 /* Hash tables to manage the global pool of class names. */
162
163 static GTY(()) objc_map_t class_name_map = 0;
164 static GTY(()) objc_map_t alias_name_map = 0;
165
166 static tree lookup_method (tree, tree);
167 static tree lookup_method_static (tree, tree, int);
168
169 static void interface_hash_init (void);
170 static tree add_interface (tree, tree);
171 static void add_category (tree, tree);
172 static inline tree lookup_category (tree, tree);
173
174 /* Protocols. */
175
176 static tree lookup_protocol (tree, bool, bool);
177 static tree lookup_and_install_protocols (tree, bool);
178
179 #ifdef OBJCPLUS
180 static void really_start_method (tree, tree);
181 #else
182 static void really_start_method (tree, struct c_arg_info *);
183 #endif
184 static int comp_proto_with_proto (tree, tree, int);
185 static tree objc_decay_parm_type (tree);
186
187 /* Utilities for debugging and error diagnostics. */
188
189 static char *gen_type_name (tree);
190 static char *gen_type_name_0 (tree);
191 static char *gen_method_decl (tree);
192 static char *gen_declaration (tree);
193
194 /* Everything else. */
195
196 static void generate_struct_by_value_array (void) ATTRIBUTE_NORETURN;
197
198 static void mark_referenced_methods (void);
199 static bool objc_type_valid_for_messaging (tree type, bool allow_classes);
200 static tree check_duplicates (tree, int, int);
201
202 /*** Private Interface (data) ***/
203 /* Flags for lookup_method_static(). */
204
205 /* Look for class methods. */
206 #define OBJC_LOOKUP_CLASS 1
207 /* Do not examine superclasses. */
208 #define OBJC_LOOKUP_NO_SUPER 2
209 /* Disable returning an instance method of a root class when a class
210 method can't be found. */
211 #define OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS 4
212
213 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
214 tree objc_global_trees[OCTI_MAX];
215
216 struct imp_entry *imp_list = 0;
217 int imp_count = 0; /* `@implementation' */
218 int cat_count = 0; /* `@category' */
219
220 objc_ivar_visibility_kind objc_ivar_visibility, objc_default_ivar_visibility;
221
222 /* Use to generate method labels. */
223 static int method_slot = 0;
224
225 /* Flag to say whether methods in a protocol are optional or
226 required. */
227 static bool objc_method_optional_flag = false;
228
229 static int objc_collecting_ivars = 0;
230
231 /* Flag that is set to 'true' while we are processing a class
232 extension. Since a class extension just "reopens" the main
233 @interface, this can be used to determine if we are in the main
234 @interface, or in a class extension. */
235 static bool objc_in_class_extension = false;
236
237 static char *errbuf; /* Buffer for error diagnostics */
238
239 /* An array of all the local variables in the current function that
240 need to be marked as volatile. */
241 vec<tree, va_gc> *local_variables_to_volatilize = NULL;
242
243 /* Store all constructed constant strings in a hash table so that
244 they get uniqued properly. */
245
246 struct GTY((for_user)) string_descriptor {
247 /* The literal argument . */
248 tree literal;
249
250 /* The resulting constant string. */
251 tree constructor;
252 };
253
254 struct objc_string_hasher : ggc_ptr_hash<string_descriptor>
255 {
256 static hashval_t hash (string_descriptor *);
257 static bool equal (string_descriptor *, string_descriptor *);
258 };
259
260 static GTY(()) hash_table<objc_string_hasher> *string_htab;
261
262 FILE *gen_declaration_file;
263
264 /* Hooks for stuff that differs between runtimes. */
265 objc_runtime_hooks runtime;
266
267 /* Create a temporary variable of type 'type'. If 'name' is set, uses
268 the specified name, else use no name. Returns the declaration of
269 the type. The 'name' is mostly useful for debugging.
270 */
271 tree
272 objc_create_temporary_var (tree type, const char *name)
273 {
274 tree decl;
275
276 if (name != NULL)
277 {
278 decl = build_decl (input_location,
279 VAR_DECL, get_identifier (name), type);
280 }
281 else
282 {
283 decl = build_decl (input_location,
284 VAR_DECL, NULL_TREE, type);
285 }
286 TREE_USED (decl) = 1;
287 DECL_ARTIFICIAL (decl) = 1;
288 DECL_IGNORED_P (decl) = 1;
289 DECL_CONTEXT (decl) = current_function_decl;
290
291 return decl;
292 }
293
294 /* Some platforms pass small structures through registers versus
295 through an invisible pointer. Determine at what size structure is
296 the transition point between the two possibilities. */
297
298 static void
299 generate_struct_by_value_array (void)
300 {
301 tree type;
302 tree decls;
303 int i, j;
304 int aggregate_in_mem[32];
305 int found = 0;
306
307 /* Presumably no platform passes 32 byte structures in a register. */
308 /* ??? As an example, m64/ppc/Darwin can pass up to 8*long+13*double
309 in registers. */
310 for (i = 1; i < 32; i++)
311 {
312 char buffer[5];
313 tree *chain = NULL;
314
315 /* Create an unnamed struct that has `i' character components */
316 type = objc_start_struct (NULL_TREE);
317
318 strcpy (buffer, "c1");
319 decls = add_field_decl (char_type_node, buffer, &chain);
320
321 for (j = 1; j < i; j++)
322 {
323 sprintf (buffer, "c%d", j + 1);
324 add_field_decl (char_type_node, buffer, &chain);
325 }
326 objc_finish_struct (type, decls);
327
328 aggregate_in_mem[i] = aggregate_value_p (type, 0);
329 if (!aggregate_in_mem[i])
330 found = 1;
331 }
332
333 /* We found some structures that are returned in registers instead of memory
334 so output the necessary data. */
335 if (found)
336 {
337 for (i = 31; i >= 0; i--)
338 if (!aggregate_in_mem[i])
339 break;
340 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n", i);
341 }
342
343 exit (0);
344 }
345
346 bool
347 objc_init (void)
348 {
349 bool ok;
350 #ifdef OBJCPLUS
351 if (cxx_init () == false)
352 #else
353 if (c_objc_common_init () == false)
354 #endif
355 return false;
356
357 /* print_struct_values is triggered by -print-runtime-info (used
358 when building libobjc, with an empty file as input). It does not
359 require any ObjC setup, and it never returns.
360
361 -fcompare-debug is used to check the compiler output; we are
362 executed twice, once with flag_compare_debug set, and once with
363 it not set. If the flag is used together with
364 -print-runtime-info, we want to print the runtime info only once,
365 else it would be output in duplicate. So we check
366 flag_compare_debug to output it in only one of the invocations.
367
368 As a side effect, this also that means -fcompare-debug
369 -print-runtime-info will run the compiler twice, and compare the
370 generated assembler file; the first time the compiler exits
371 immediately (producing no file), and the second time it compiles
372 an empty file. This checks, as a side effect, that compiling an
373 empty file produces no assembler output. */
374 if (print_struct_values && !flag_compare_debug)
375 generate_struct_by_value_array ();
376
377 /* Set up stuff used by FE parser and all runtimes. */
378 errbuf = XNEWVEC (char, 1024 * 10);
379 interface_hash_init ();
380 hash_init ();
381 objc_encoding_init ();
382 /* ... and then check flags and set-up for the selected runtime ... */
383 if (flag_next_runtime && flag_objc_abi >= 2)
384 ok = objc_next_runtime_abi_02_init (&runtime);
385 else if (flag_next_runtime)
386 ok = objc_next_runtime_abi_01_init (&runtime);
387 else
388 ok = objc_gnu_runtime_abi_01_init (&runtime);
389
390 /* If that part of the setup failed - bail out immediately. */
391 if (!ok)
392 return false;
393
394 /* Determine the default visibility for instance variables. */
395 switch (default_ivar_visibility)
396 {
397 case IVAR_VISIBILITY_PRIVATE:
398 objc_default_ivar_visibility = OBJC_IVAR_VIS_PRIVATE;
399 break;
400 case IVAR_VISIBILITY_PUBLIC:
401 objc_default_ivar_visibility = OBJC_IVAR_VIS_PUBLIC;
402 break;
403 case IVAR_VISIBILITY_PACKAGE:
404 objc_default_ivar_visibility = OBJC_IVAR_VIS_PACKAGE;
405 break;
406 default:
407 objc_default_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
408 }
409
410 /* Generate general types and push runtime-specific decls to file scope. */
411 synth_module_prologue ();
412
413 return true;
414 }
415
416 /* This is called at the end of parsing by the C/C++ parsers. */
417 void
418 objc_write_global_declarations (void)
419 {
420 mark_referenced_methods ();
421
422 /* A missing @end might not be detected by the parser. */
423 if (objc_implementation_context)
424 {
425 warning (0, "%<@end%> missing in implementation context");
426 finish_class (objc_implementation_context);
427 objc_ivar_chain = NULL_TREE;
428 objc_implementation_context = NULL_TREE;
429 }
430
431 if (warn_selector)
432 {
433 objc_map_iterator_t i;
434
435 objc_map_iterator_initialize (class_method_map, &i);
436 while (objc_map_iterator_move_to_next (class_method_map, &i))
437 check_duplicates (objc_map_iterator_current_value (class_method_map, i), 0, 1);
438
439 objc_map_iterator_initialize (instance_method_map, &i);
440 while (objc_map_iterator_move_to_next (instance_method_map, &i))
441 check_duplicates (objc_map_iterator_current_value (instance_method_map, i), 0, 0);
442 }
443
444 /* TODO: consider an early exit here if either errorcount or sorrycount
445 is non-zero. Not only is it wasting time to generate the metadata,
446 it needlessly imposes need to re-check for things that are already
447 determined to be errors. */
448
449 /* Finalize Objective-C runtime data. No need to generate tables
450 and code if only checking syntax, or if generating a PCH file. */
451 if (!flag_syntax_only && !pch_file)
452 {
453 location_t saved_location;
454
455 /* If gen_declaration desired, open the output file. */
456 if (flag_gen_declaration)
457 {
458 char * const dumpname = concat (dump_base_name, ".decl", NULL);
459 gen_declaration_file = fopen (dumpname, "w");
460 if (gen_declaration_file == 0)
461 fatal_error (input_location, "can%'t open %s: %m", dumpname);
462 free (dumpname);
463 }
464
465 /* Set the input location to BUILTINS_LOCATION. This is good
466 for error messages, in case any is generated while producing
467 the metadata, but it also silences warnings that would be
468 produced when compiling with -Wpadded in case when padding is
469 automatically added to the built-in runtime data structure
470 declarations. We know about this padding, and it is fine; we
471 don't want users to see any warnings about it if they use
472 -Wpadded. */
473 saved_location = input_location;
474 input_location = BUILTINS_LOCATION;
475
476 /* Compute and emit the meta-data tables for this runtime. */
477 (*runtime.generate_metadata) ();
478
479 /* Restore the original location, just in case it mattered. */
480 input_location = saved_location;
481
482 /* ... and then close any declaration file we opened. */
483 if (gen_declaration_file)
484 fclose (gen_declaration_file);
485 }
486 }
487
488 /* Return the first occurrence of a method declaration corresponding
489 to sel_name in rproto_list. Search rproto_list recursively.
490 If is_class is 0, search for instance methods, otherwise for class
491 methods. */
492 static tree
493 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
494 int is_class)
495 {
496 tree rproto, p, m;
497
498 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
499 {
500 p = TREE_VALUE (rproto);
501 m = NULL_TREE;
502
503 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
504 {
505 /* First, search the @required protocol methods. */
506 if (is_class)
507 m = lookup_method (PROTOCOL_CLS_METHODS (p), sel_name);
508 else
509 m = lookup_method (PROTOCOL_NST_METHODS (p), sel_name);
510
511 if (m)
512 return m;
513
514 /* If still not found, search the @optional protocol methods. */
515 if (is_class)
516 m = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (p), sel_name);
517 else
518 m = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (p), sel_name);
519
520 if (m)
521 return m;
522
523 /* If still not found, search the attached protocols. */
524 if (PROTOCOL_LIST (p))
525 m = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
526 sel_name, is_class);
527 if (m)
528 return m;
529 }
530 else
531 {
532 ; /* An identifier...if we could not find a protocol. */
533 }
534 }
535
536 return 0;
537 }
538
539 static tree
540 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
541 {
542 tree rproto, p;
543
544 /* Make sure the protocol is supported by the object on the rhs. */
545 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
546 {
547 tree fnd = 0;
548 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
549 {
550 p = TREE_VALUE (rproto);
551
552 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
553 {
554 if (lproto == p)
555 fnd = lproto;
556
557 else if (PROTOCOL_LIST (p))
558 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
559 }
560
561 if (fnd)
562 return fnd;
563 }
564 }
565 else
566 {
567 ; /* An identifier...if we could not find a protocol. */
568 }
569
570 return 0;
571 }
572
573 void
574 objc_start_class_interface (tree klass, tree super_class,
575 tree protos, tree attributes)
576 {
577 if (flag_objc1_only && attributes)
578 error_at (input_location, "class attributes are not available in Objective-C 1.0");
579
580 objc_interface_context
581 = objc_ivar_context
582 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos, attributes);
583 objc_ivar_visibility = objc_default_ivar_visibility;
584 }
585
586 void
587 objc_start_category_interface (tree klass, tree categ,
588 tree protos, tree attributes)
589 {
590 if (attributes)
591 {
592 if (flag_objc1_only)
593 error_at (input_location, "category attributes are not available in Objective-C 1.0");
594 else
595 warning_at (input_location, OPT_Wattributes,
596 "category attributes are not available in this version"
597 " of the compiler, (ignored)");
598 }
599 if (categ == NULL_TREE)
600 {
601 if (flag_objc1_only)
602 error_at (input_location, "class extensions are not available in Objective-C 1.0");
603 else
604 {
605 /* Iterate over all the classes and categories implemented
606 up to now in this compilation unit. */
607 struct imp_entry *t;
608
609 for (t = imp_list; t; t = t->next)
610 {
611 /* If we find a class @implementation with the same name
612 as the one we are extending, produce an error. */
613 if (TREE_CODE (t->imp_context) == CLASS_IMPLEMENTATION_TYPE
614 && IDENTIFIER_POINTER (CLASS_NAME (t->imp_context)) == IDENTIFIER_POINTER (klass))
615 error_at (input_location,
616 "class extension for class %qE declared after its %<@implementation%>",
617 klass);
618 }
619 }
620 }
621 objc_interface_context
622 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos, NULL_TREE);
623 objc_ivar_chain
624 = continue_class (objc_interface_context);
625 }
626
627 void
628 objc_start_protocol (tree name, tree protos, tree attributes)
629 {
630 if (flag_objc1_only && attributes)
631 error_at (input_location, "protocol attributes are not available in Objective-C 1.0");
632
633 objc_interface_context
634 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos, attributes);
635 objc_method_optional_flag = false;
636 }
637
638 void
639 objc_continue_interface (void)
640 {
641 objc_ivar_chain
642 = continue_class (objc_interface_context);
643 }
644
645 void
646 objc_finish_interface (void)
647 {
648 finish_class (objc_interface_context);
649 objc_interface_context = NULL_TREE;
650 objc_method_optional_flag = false;
651 objc_in_class_extension = false;
652 }
653
654 void
655 objc_start_class_implementation (tree klass, tree super_class)
656 {
657 objc_implementation_context
658 = objc_ivar_context
659 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE,
660 NULL_TREE);
661 objc_ivar_visibility = objc_default_ivar_visibility;
662 }
663
664 void
665 objc_start_category_implementation (tree klass, tree categ)
666 {
667 objc_implementation_context
668 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE,
669 NULL_TREE);
670 objc_ivar_chain
671 = continue_class (objc_implementation_context);
672 }
673
674 void
675 objc_continue_implementation (void)
676 {
677 objc_ivar_chain
678 = continue_class (objc_implementation_context);
679 }
680
681 void
682 objc_finish_implementation (void)
683 {
684 #ifdef OBJCPLUS
685 if (flag_objc_call_cxx_cdtors)
686 objc_generate_cxx_cdtors ();
687 #endif
688
689 if (objc_implementation_context)
690 {
691 finish_class (objc_implementation_context);
692 objc_ivar_chain = NULL_TREE;
693 objc_implementation_context = NULL_TREE;
694 }
695 else
696 warning (0, "%<@end%> must appear in an @implementation context");
697 }
698
699 void
700 objc_set_visibility (objc_ivar_visibility_kind visibility)
701 {
702 if (visibility == OBJC_IVAR_VIS_PACKAGE)
703 {
704 if (flag_objc1_only)
705 error ("%<@package%> is not available in Objective-C 1.0");
706 else
707 warning (0, "%<@package%> presently has the same effect as %<@public%>");
708 }
709 objc_ivar_visibility = visibility;
710 }
711
712 void
713 objc_set_method_opt (bool optional)
714 {
715 if (flag_objc1_only)
716 {
717 if (optional)
718 error_at (input_location, "%<@optional%> is not available in Objective-C 1.0");
719 else
720 error_at (input_location, "%<@required%> is not available in Objective-C 1.0");
721 }
722
723 objc_method_optional_flag = optional;
724 if (!objc_interface_context
725 || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
726 {
727 if (optional)
728 error ("%<@optional%> is allowed in @protocol context only");
729 else
730 error ("%<@required%> is allowed in @protocol context only");
731 objc_method_optional_flag = false;
732 }
733 }
734
735 /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
736 PROTOCOL. */
737 static tree
738 lookup_property_in_list (tree chain, tree property)
739 {
740 tree x;
741 for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x))
742 if (PROPERTY_NAME (x) == property)
743 return x;
744 return NULL_TREE;
745 }
746
747 /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
748 static tree lookup_property_in_protocol_list (tree rproto_list, tree property)
749 {
750 tree rproto, x;
751 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
752 {
753 tree p = TREE_VALUE (rproto);
754 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
755 {
756 if ((x = lookup_property_in_list (p, property)))
757 return x;
758 if (PROTOCOL_LIST (p))
759 return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property);
760 }
761 else
762 {
763 ; /* An identifier...if we could not find a protocol. */
764 }
765 }
766 return NULL_TREE;
767 }
768
769 /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
770 chain of interface hierarchy. */
771 static tree
772 lookup_property (tree interface_type, tree property)
773 {
774 tree inter = interface_type;
775 while (inter)
776 {
777 tree x, category;
778 if ((x = lookup_property_in_list (inter, property)))
779 return x;
780 /* Failing that, look for the property in each category of the class. */
781 category = inter;
782 while ((category = CLASS_CATEGORY_LIST (category)))
783 {
784 if ((x = lookup_property_in_list (category, property)))
785 return x;
786
787 /* When checking a category, also check the protocols
788 attached with the category itself. */
789 if (CLASS_PROTOCOL_LIST (category)
790 && (x = lookup_property_in_protocol_list
791 (CLASS_PROTOCOL_LIST (category), property)))
792 return x;
793 }
794
795 /* Failing to find in categories, look for property in protocol list. */
796 if (CLASS_PROTOCOL_LIST (inter)
797 && (x = lookup_property_in_protocol_list
798 (CLASS_PROTOCOL_LIST (inter), property)))
799 return x;
800
801 /* Failing that, climb up the inheritance hierarchy. */
802 inter = lookup_interface (CLASS_SUPER_NAME (inter));
803 }
804 return inter;
805 }
806
807 /* This routine is called by the parser when a
808 @property... declaration is found. 'decl' is the declaration of
809 the property (type/identifier), and the other arguments represent
810 property attributes that may have been specified in the Objective-C
811 declaration. 'parsed_property_readonly' is 'true' if the attribute
812 'readonly' was specified, and 'false' if not; similarly for the
813 other bool parameters. 'parsed_property_getter_ident' is NULL_TREE
814 if the attribute 'getter' was not specified, and is the identifier
815 corresponding to the specified getter if it was; similarly for
816 'parsed_property_setter_ident'. */
817 void
818 objc_add_property_declaration (location_t location, tree decl,
819 bool parsed_property_readonly, bool parsed_property_readwrite,
820 bool parsed_property_assign, bool parsed_property_retain,
821 bool parsed_property_copy, bool parsed_property_nonatomic,
822 tree parsed_property_getter_ident, tree parsed_property_setter_ident)
823 {
824 tree property_decl;
825 tree x;
826 /* 'property_readonly' and 'property_assign_semantics' are the final
827 attributes of the property after all parsed attributes have been
828 considered (eg, if we parsed no 'readonly' and no 'readwrite', ie
829 parsed_property_readonly = false and parsed_property_readwrite =
830 false, then property_readonly will be false because the default
831 is readwrite). */
832 bool property_readonly = false;
833 objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN;
834 bool property_extension_in_class_extension = false;
835
836 if (flag_objc1_only)
837 error_at (input_location, "%<@property%> is not available in Objective-C 1.0");
838
839 if (parsed_property_readonly && parsed_property_readwrite)
840 {
841 error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute");
842 /* In case of conflicting attributes (here and below), after
843 producing an error, we pick one of the attributes and keep
844 going. */
845 property_readonly = false;
846 }
847 else
848 {
849 if (parsed_property_readonly)
850 property_readonly = true;
851
852 if (parsed_property_readwrite)
853 property_readonly = false;
854 }
855
856 if (parsed_property_readonly && parsed_property_setter_ident)
857 {
858 error_at (location, "%<readonly%> attribute conflicts with %<setter%> attribute");
859 property_readonly = false;
860 }
861
862 if (parsed_property_assign && parsed_property_retain)
863 {
864 error_at (location, "%<assign%> attribute conflicts with %<retain%> attribute");
865 property_assign_semantics = OBJC_PROPERTY_RETAIN;
866 }
867 else if (parsed_property_assign && parsed_property_copy)
868 {
869 error_at (location, "%<assign%> attribute conflicts with %<copy%> attribute");
870 property_assign_semantics = OBJC_PROPERTY_COPY;
871 }
872 else if (parsed_property_retain && parsed_property_copy)
873 {
874 error_at (location, "%<retain%> attribute conflicts with %<copy%> attribute");
875 property_assign_semantics = OBJC_PROPERTY_COPY;
876 }
877 else
878 {
879 if (parsed_property_assign)
880 property_assign_semantics = OBJC_PROPERTY_ASSIGN;
881
882 if (parsed_property_retain)
883 property_assign_semantics = OBJC_PROPERTY_RETAIN;
884
885 if (parsed_property_copy)
886 property_assign_semantics = OBJC_PROPERTY_COPY;
887 }
888
889 if (!objc_interface_context)
890 {
891 error_at (location, "property declaration not in @interface or @protocol context");
892 return;
893 }
894
895 /* At this point we know that we are either in an interface, a
896 category, or a protocol. */
897
898 /* We expect a FIELD_DECL from the parser. Make sure we didn't get
899 something else, as that would confuse the checks below. */
900 if (TREE_CODE (decl) != FIELD_DECL)
901 {
902 error_at (location, "invalid property declaration");
903 return;
904 }
905
906 /* Do some spot-checks for the most obvious invalid types. */
907
908 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
909 {
910 error_at (location, "property can not be an array");
911 return;
912 }
913
914 /* The C++/ObjC++ parser seems to reject the ':' for a bitfield when
915 parsing, while the C/ObjC parser accepts it and gives us a
916 FIELD_DECL with a DECL_INITIAL set. So we use the DECL_INITIAL
917 to check for a bitfield when doing ObjC. */
918 #ifndef OBJCPLUS
919 if (DECL_INITIAL (decl))
920 {
921 /* A @property is not an actual variable, but it is a way to
922 describe a pair of accessor methods, so its type (which is
923 the type of the return value of the getter and the first
924 argument of the setter) can't be a bitfield (as return values
925 and arguments of functions can not be bitfields). The
926 underlying instance variable could be a bitfield, but that is
927 a different matter. */
928 error_at (location, "property can not be a bit-field");
929 return;
930 }
931 #endif
932
933 /* TODO: Check that the property type is an Objective-C object or a
934 "POD". */
935
936 /* Implement -Wproperty-assign-default (which is enabled by default). */
937 if (warn_property_assign_default
938 /* If garbage collection is not being used, then 'assign' is
939 valid for objects (and typically used for delegates) but it
940 is wrong in most cases (since most objects need to be
941 retained or copied in setters). Warn users when 'assign' is
942 used implicitly. */
943 && property_assign_semantics == OBJC_PROPERTY_ASSIGN
944 /* Read-only properties are never assigned, so the assignment
945 semantics do not matter in that case. */
946 && !property_readonly
947 && !flag_objc_gc)
948 {
949 /* Please note that it would make sense to default to 'assign'
950 for non-{Objective-C objects}, and to 'retain' for
951 Objective-C objects. But that would break compatibility with
952 other compilers. */
953 if (!parsed_property_assign && !parsed_property_retain && !parsed_property_copy)
954 {
955 /* Use 'false' so we do not warn for Class objects. */
956 if (objc_type_valid_for_messaging (TREE_TYPE (decl), false))
957 {
958 warning_at (location,
959 0,
960 "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>",
961 decl);
962 inform (location,
963 "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it");
964 }
965 }
966 }
967
968 if (property_assign_semantics == OBJC_PROPERTY_RETAIN
969 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
970 error_at (location, "%<retain%> attribute is only valid for Objective-C objects");
971
972 if (property_assign_semantics == OBJC_PROPERTY_COPY
973 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
974 error_at (location, "%<copy%> attribute is only valid for Objective-C objects");
975
976 /* Now determine the final property getter and setter names. They
977 will be stored in the PROPERTY_DECL, from which they'll always be
978 extracted and used. */
979
980 /* Adjust, or fill in, setter and getter names. We overwrite the
981 parsed_property_setter_ident and parsed_property_getter_ident
982 with the final setter and getter identifiers that will be
983 used. */
984 if (parsed_property_setter_ident)
985 {
986 /* The setter should be terminated by ':', but the parser only
987 gives us an identifier without ':'. So, we need to add ':'
988 at the end. */
989 const char *parsed_setter = IDENTIFIER_POINTER (parsed_property_setter_ident);
990 size_t length = strlen (parsed_setter);
991 char *final_setter = (char *)alloca (length + 2);
992
993 sprintf (final_setter, "%s:", parsed_setter);
994 parsed_property_setter_ident = get_identifier (final_setter);
995 }
996 else
997 {
998 if (!property_readonly)
999 parsed_property_setter_ident = get_identifier (objc_build_property_setter_name
1000 (DECL_NAME (decl)));
1001 }
1002
1003 if (!parsed_property_getter_ident)
1004 parsed_property_getter_ident = DECL_NAME (decl);
1005
1006 /* Check for duplicate property declarations. We first check the
1007 immediate context for a property with the same name. Any such
1008 declarations are an error, unless this is a class extension and
1009 we are extending a property from readonly to readwrite. */
1010 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
1011 {
1012 if (PROPERTY_NAME (x) == DECL_NAME (decl))
1013 {
1014 if (objc_in_class_extension
1015 && property_readonly == 0
1016 && PROPERTY_READONLY (x) == 1)
1017 {
1018 /* This is a class extension, and we are extending an
1019 existing readonly property to a readwrite one.
1020 That's fine. :-) */
1021 property_extension_in_class_extension = true;
1022 break;
1023 }
1024 else
1025 {
1026 location_t original_location = DECL_SOURCE_LOCATION (x);
1027
1028 error_at (location, "redeclaration of property %qD", decl);
1029
1030 if (original_location != UNKNOWN_LOCATION)
1031 inform (original_location, "originally specified here");
1032 return;
1033 }
1034 }
1035 }
1036
1037 /* If x is not NULL_TREE, we must be in a class extension and we're
1038 extending a readonly property. In that case, no point in
1039 searching for another declaration. */
1040 if (x == NULL_TREE)
1041 {
1042 /* We now need to check for existing property declarations (in
1043 the superclass, other categories or protocols) and check that
1044 the new declaration is not in conflict with existing
1045 ones. */
1046
1047 /* Search for a previous, existing declaration of a property
1048 with the same name in superclasses, protocols etc. If one is
1049 found, it will be in the 'x' variable. */
1050
1051 /* Note that, for simplicity, the following may search again the
1052 local context. That's Ok as nothing will be found (else we'd
1053 have thrown an error above); it's only a little inefficient,
1054 but the code is simpler. */
1055 switch (TREE_CODE (objc_interface_context))
1056 {
1057 case CLASS_INTERFACE_TYPE:
1058 /* Look up the property in the current @interface (which
1059 will find nothing), then its protocols and categories and
1060 superclasses. */
1061 x = lookup_property (objc_interface_context, DECL_NAME (decl));
1062 break;
1063 case CATEGORY_INTERFACE_TYPE:
1064 /* Look up the property in the main @interface, then
1065 protocols and categories (one of them is ours, and will
1066 find nothing) and superclasses. */
1067 x = lookup_property (lookup_interface (CLASS_NAME (objc_interface_context)),
1068 DECL_NAME (decl));
1069 break;
1070 case PROTOCOL_INTERFACE_TYPE:
1071 /* Looks up the property in any protocols attached to the
1072 current protocol. */
1073 if (PROTOCOL_LIST (objc_interface_context))
1074 {
1075 x = lookup_property_in_protocol_list (PROTOCOL_LIST (objc_interface_context),
1076 DECL_NAME (decl));
1077 }
1078 break;
1079 default:
1080 gcc_unreachable ();
1081 }
1082 }
1083
1084 if (x != NULL_TREE)
1085 {
1086 /* An existing property was found; check that it has the same
1087 types, or it is compatible. */
1088 location_t original_location = DECL_SOURCE_LOCATION (x);
1089
1090 if (PROPERTY_NONATOMIC (x) != parsed_property_nonatomic)
1091 {
1092 warning_at (location, 0,
1093 "'nonatomic' attribute of property %qD conflicts with previous declaration", decl);
1094
1095 if (original_location != UNKNOWN_LOCATION)
1096 inform (original_location, "originally specified here");
1097 return;
1098 }
1099
1100 if (PROPERTY_GETTER_NAME (x) != parsed_property_getter_ident)
1101 {
1102 warning_at (location, 0,
1103 "'getter' attribute of property %qD conflicts with previous declaration", decl);
1104
1105 if (original_location != UNKNOWN_LOCATION)
1106 inform (original_location, "originally specified here");
1107 return;
1108 }
1109
1110 /* We can only compare the setter names if both the old and new property have a setter. */
1111 if (!property_readonly && !PROPERTY_READONLY(x))
1112 {
1113 if (PROPERTY_SETTER_NAME (x) != parsed_property_setter_ident)
1114 {
1115 warning_at (location, 0,
1116 "'setter' attribute of property %qD conflicts with previous declaration", decl);
1117
1118 if (original_location != UNKNOWN_LOCATION)
1119 inform (original_location, "originally specified here");
1120 return;
1121 }
1122 }
1123
1124 if (PROPERTY_ASSIGN_SEMANTICS (x) != property_assign_semantics)
1125 {
1126 warning_at (location, 0,
1127 "assign semantics attributes of property %qD conflict with previous declaration", decl);
1128
1129 if (original_location != UNKNOWN_LOCATION)
1130 inform (original_location, "originally specified here");
1131 return;
1132 }
1133
1134 /* It's ok to have a readonly property that becomes a readwrite, but not vice versa. */
1135 if (PROPERTY_READONLY (x) == 0 && property_readonly == 1)
1136 {
1137 warning_at (location, 0,
1138 "'readonly' attribute of property %qD conflicts with previous declaration", decl);
1139
1140 if (original_location != UNKNOWN_LOCATION)
1141 inform (original_location, "originally specified here");
1142 return;
1143 }
1144
1145 /* We now check that the new and old property declarations have
1146 the same types (or compatible one). In the Objective-C
1147 tradition of loose type checking, we do type-checking but
1148 only generate warnings (not errors) if they do not match.
1149 For non-readonly properties, the types must match exactly;
1150 for readonly properties, it is allowed to use a "more
1151 specialized" type in the new property declaration. Eg, the
1152 superclass has a getter returning (NSArray *) and the
1153 subclass a getter returning (NSMutableArray *). The object's
1154 getter returns an (NSMutableArray *); but if you cast the
1155 object to the superclass, which is allowed, you'd still
1156 expect the getter to return an (NSArray *), which works since
1157 an (NSMutableArray *) is an (NSArray *) too. So, the set of
1158 objects belonging to the type of the new @property should be
1159 a subset of the set of objects belonging to the type of the
1160 old @property. This is what "specialization" means. And the
1161 reason it only applies to readonly properties is that for a
1162 readwrite property the setter would have the opposite
1163 requirement - ie that the superclass type is more specialized
1164 then the subclass one; hence the only way to satisfy both
1165 constraints is that the types match. */
1166
1167 /* If the types are not the same in the C sense, we warn ... */
1168 if (!comptypes (TREE_TYPE (x), TREE_TYPE (decl))
1169 /* ... unless the property is readonly, in which case we
1170 allow a new, more specialized, declaration. */
1171 && (!property_readonly
1172 || !objc_compare_types (TREE_TYPE (x),
1173 TREE_TYPE (decl), -5, NULL_TREE)))
1174 {
1175 warning_at (location, 0,
1176 "type of property %qD conflicts with previous declaration", decl);
1177 if (original_location != UNKNOWN_LOCATION)
1178 inform (original_location, "originally specified here");
1179 return;
1180 }
1181
1182 /* If we are in a class extension and we're extending a readonly
1183 property in the main @interface, we'll just update the
1184 existing property with the readwrite flag and potentially the
1185 new setter name. */
1186 if (property_extension_in_class_extension)
1187 {
1188 PROPERTY_READONLY (x) = 0;
1189 PROPERTY_SETTER_NAME (x) = parsed_property_setter_ident;
1190 return;
1191 }
1192 }
1193
1194 /* Create a PROPERTY_DECL node. */
1195 property_decl = make_node (PROPERTY_DECL);
1196
1197 /* Copy the basic information from the original decl. */
1198 TREE_TYPE (property_decl) = TREE_TYPE (decl);
1199 DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl);
1200 TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl);
1201
1202 /* Add property-specific information. */
1203 PROPERTY_NAME (property_decl) = DECL_NAME (decl);
1204 PROPERTY_GETTER_NAME (property_decl) = parsed_property_getter_ident;
1205 PROPERTY_SETTER_NAME (property_decl) = parsed_property_setter_ident;
1206 PROPERTY_READONLY (property_decl) = property_readonly;
1207 PROPERTY_NONATOMIC (property_decl) = parsed_property_nonatomic;
1208 PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics;
1209 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1210 PROPERTY_DYNAMIC (property_decl) = 0;
1211
1212 /* Remember the fact that the property was found in the @optional
1213 section in a @protocol, or not. */
1214 if (objc_method_optional_flag)
1215 PROPERTY_OPTIONAL (property_decl) = 1;
1216 else
1217 PROPERTY_OPTIONAL (property_decl) = 0;
1218
1219 /* Note that PROPERTY_GETTER_NAME is always set for all
1220 PROPERTY_DECLs, and PROPERTY_SETTER_NAME is always set for all
1221 PROPERTY_DECLs where PROPERTY_READONLY == 0. Any time we deal
1222 with a getter or setter, we should get the PROPERTY_DECL and use
1223 PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know the correct
1224 names. */
1225
1226 /* Add the PROPERTY_DECL to the list of properties for the class. */
1227 TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context);
1228 CLASS_PROPERTY_DECL (objc_interface_context) = property_decl;
1229 }
1230
1231 /* This is a subroutine of objc_maybe_build_component_ref. Search the
1232 list of methods in the interface (and, failing that, the local list
1233 in the implementation, and failing that, the protocol list)
1234 provided for a 'setter' or 'getter' for 'component' with default
1235 names (ie, if 'component' is "name", then search for "name" and
1236 "setName:"). It is also possible to specify a different
1237 'getter_name' (this is used for @optional readonly properties). If
1238 any is found, then create an artificial property that uses them.
1239 Return NULL_TREE if 'getter' or 'setter' could not be found. */
1240 static tree
1241 maybe_make_artificial_property_decl (tree interface, tree implementation,
1242 tree protocol_list, tree component, bool is_class,
1243 tree getter_name)
1244 {
1245 tree setter_name = get_identifier (objc_build_property_setter_name (component));
1246 tree getter = NULL_TREE;
1247 tree setter = NULL_TREE;
1248
1249 if (getter_name == NULL_TREE)
1250 getter_name = component;
1251
1252 /* First, check the @interface and all superclasses. */
1253 if (interface)
1254 {
1255 int flags = 0;
1256
1257 /* Using instance methods of the root class as accessors is most
1258 likely unwanted and can be extremely confusing (and, most
1259 importantly, other Objective-C 2.0 compilers do not do it).
1260 Turn it off. */
1261 if (is_class)
1262 flags = OBJC_LOOKUP_CLASS | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS;
1263
1264 getter = lookup_method_static (interface, getter_name, flags);
1265 setter = lookup_method_static (interface, setter_name, flags);
1266 }
1267
1268 /* Second, check the local @implementation context. */
1269 if (!getter && !setter)
1270 {
1271 if (implementation)
1272 {
1273 if (is_class)
1274 {
1275 getter = lookup_method (CLASS_CLS_METHODS (implementation), getter_name);
1276 setter = lookup_method (CLASS_CLS_METHODS (implementation), setter_name);
1277 }
1278 else
1279 {
1280 getter = lookup_method (CLASS_NST_METHODS (implementation), getter_name);
1281 setter = lookup_method (CLASS_NST_METHODS (implementation), setter_name);
1282 }
1283 }
1284 }
1285
1286 /* Try the protocol_list if we didn't find anything in the
1287 @interface and in the @implementation. */
1288 if (!getter && !setter)
1289 {
1290 getter = lookup_method_in_protocol_list (protocol_list, getter_name, is_class);
1291 setter = lookup_method_in_protocol_list (protocol_list, setter_name, is_class);
1292 }
1293
1294 /* There needs to be at least a getter or setter for this to be a
1295 valid 'object.component' syntax. */
1296 if (getter || setter)
1297 {
1298 /* Yes ... determine the type of the expression. */
1299 tree property_decl;
1300 tree type;
1301
1302 if (getter)
1303 type = TREE_VALUE (TREE_TYPE (getter));
1304 else
1305 type = TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter)));
1306
1307 /* Create an artificial property declaration with the
1308 information we collected on the type and getter/setter
1309 names. */
1310 property_decl = make_node (PROPERTY_DECL);
1311
1312 TREE_TYPE (property_decl) = type;
1313 DECL_SOURCE_LOCATION (property_decl) = input_location;
1314 TREE_DEPRECATED (property_decl) = 0;
1315 DECL_ARTIFICIAL (property_decl) = 1;
1316
1317 /* Add property-specific information. Note that one of
1318 PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a
1319 non-existing method; this will generate an error when the
1320 expression is later compiled. At this stage we don't know if
1321 the getter or setter will be used, so we can't generate an
1322 error. */
1323 PROPERTY_NAME (property_decl) = component;
1324 PROPERTY_GETTER_NAME (property_decl) = getter_name;
1325 PROPERTY_SETTER_NAME (property_decl) = setter_name;
1326 PROPERTY_READONLY (property_decl) = 0;
1327 PROPERTY_NONATOMIC (property_decl) = 0;
1328 PROPERTY_ASSIGN_SEMANTICS (property_decl) = 0;
1329 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1330 PROPERTY_DYNAMIC (property_decl) = 0;
1331 PROPERTY_OPTIONAL (property_decl) = 0;
1332
1333 if (!getter)
1334 PROPERTY_HAS_NO_GETTER (property_decl) = 1;
1335
1336 /* The following is currently unused, but it's nice to have
1337 there. We may use it if we need in the future. */
1338 if (!setter)
1339 PROPERTY_HAS_NO_SETTER (property_decl) = 1;
1340
1341 return property_decl;
1342 }
1343
1344 return NULL_TREE;
1345 }
1346
1347 /* This hook routine is invoked by the parser when an expression such
1348 as 'xxx.yyy' is parsed. We get a chance to process these
1349 expressions in a way that is specified to Objective-C (to implement
1350 the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars).
1351 If the expression is not an Objective-C specified expression, we
1352 should return NULL_TREE; else we return the expression.
1353
1354 At the moment this only implements dot-syntax and properties (not
1355 non-fragile ivars yet), ie 'object.property' or 'object.component'
1356 where 'component' is not a declared property, but a valid getter or
1357 setter for it could be found. */
1358 tree
1359 objc_maybe_build_component_ref (tree object, tree property_ident)
1360 {
1361 tree x = NULL_TREE;
1362 tree rtype;
1363
1364 /* If we are in Objective-C 1.0 mode, dot-syntax and properties are
1365 not available. */
1366 if (flag_objc1_only)
1367 return NULL_TREE;
1368
1369 /* Try to determine if 'object' is an Objective-C object or not. If
1370 not, return. */
1371 if (object == NULL_TREE || object == error_mark_node
1372 || (rtype = TREE_TYPE (object)) == NULL_TREE)
1373 return NULL_TREE;
1374
1375 if (property_ident == NULL_TREE || property_ident == error_mark_node
1376 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1377 return NULL_TREE;
1378
1379 /* The following analysis of 'object' is similar to the one used for
1380 the 'receiver' of a method invocation. We need to determine what
1381 'object' is and find the appropriate property (either declared,
1382 or artificial) for it (in the same way as we need to find the
1383 appropriate method prototype for a method invocation). There are
1384 some simplifications here though: "object.property" is invalid if
1385 "object" has a type of "id" or "Class"; it must at least have a
1386 protocol attached to it, and "object" is never a class name as
1387 that is done by objc_build_class_component_ref. Finally, we
1388 don't know if this really is a dot-syntax expression, so we want
1389 to make a quick exit if it is not; for this reason, we try to
1390 postpone checks after determining that 'object' looks like an
1391 Objective-C object. */
1392
1393 if (objc_is_id (rtype))
1394 {
1395 /* This is the case that the 'object' is of type 'id' or
1396 'Class'. */
1397
1398 /* Check if at least it is of type 'id <Protocol>' or 'Class
1399 <Protocol>'; if so, look the property up in the
1400 protocols. */
1401 if (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype)))
1402 {
1403 tree rprotos = TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype));
1404
1405 if (rprotos)
1406 {
1407 /* No point looking up declared @properties if we are
1408 dealing with a class. Classes have no declared
1409 properties. */
1410 if (!IS_CLASS (rtype))
1411 x = lookup_property_in_protocol_list (rprotos, property_ident);
1412
1413 if (x == NULL_TREE)
1414 {
1415 /* Ok, no property. Maybe it was an
1416 object.component dot-syntax without a declared
1417 property (this is valid for classes too). Look
1418 for getter/setter methods and internally declare
1419 an artificial property based on them if found. */
1420 x = maybe_make_artificial_property_decl (NULL_TREE,
1421 NULL_TREE,
1422 rprotos,
1423 property_ident,
1424 IS_CLASS (rtype),
1425 NULL_TREE);
1426 }
1427 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1428 {
1429 /* This is a special, complicated case. If the
1430 property is optional, and is read-only, then the
1431 property is always used for reading, but an
1432 eventual existing non-property setter can be used
1433 for writing. We create an artificial property
1434 decl copying the getter from the optional
1435 property, and looking up the setter in the
1436 interface. */
1437 x = maybe_make_artificial_property_decl (NULL_TREE,
1438 NULL_TREE,
1439 rprotos,
1440 property_ident,
1441 false,
1442 PROPERTY_GETTER_NAME (x));
1443 }
1444 }
1445 }
1446 else if (objc_method_context)
1447 {
1448 /* Else, if we are inside a method it could be the case of
1449 'super' or 'self'. */
1450 tree interface_type = NULL_TREE;
1451 tree t = object;
1452 while (TREE_CODE (t) == COMPOUND_EXPR
1453 || TREE_CODE (t) == MODIFY_EXPR
1454 || CONVERT_EXPR_P (t)
1455 || TREE_CODE (t) == COMPONENT_REF)
1456 t = TREE_OPERAND (t, 0);
1457
1458 STRIP_ANY_LOCATION_WRAPPER (t);
1459
1460 if (t == UOBJC_SUPER_decl)
1461 interface_type = lookup_interface (CLASS_SUPER_NAME (implementation_template));
1462 else if (t == self_decl)
1463 interface_type = lookup_interface (CLASS_NAME (implementation_template));
1464
1465 if (interface_type)
1466 {
1467 if (TREE_CODE (objc_method_context) != CLASS_METHOD_DECL)
1468 x = lookup_property (interface_type, property_ident);
1469
1470 if (x == NULL_TREE)
1471 {
1472 /* Try the dot-syntax without a declared property.
1473 If this is an access to 'self', it is possible
1474 that they may refer to a setter/getter that is
1475 not declared in the interface, but exists locally
1476 in the implementation. In that case, get the
1477 implementation context and use it. */
1478 tree implementation = NULL_TREE;
1479
1480 if (t == self_decl)
1481 implementation = objc_implementation_context;
1482
1483 x = maybe_make_artificial_property_decl
1484 (interface_type, implementation, NULL_TREE,
1485 property_ident,
1486 (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL),
1487 NULL_TREE);
1488 }
1489 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1490 {
1491 tree implementation = NULL_TREE;
1492
1493 if (t == self_decl)
1494 implementation = objc_implementation_context;
1495
1496 x = maybe_make_artificial_property_decl (interface_type,
1497 implementation,
1498 NULL_TREE,
1499 property_ident,
1500 false,
1501 PROPERTY_GETTER_NAME (x));
1502 }
1503 }
1504 }
1505 }
1506 else
1507 {
1508 /* This is the case where we have more information on 'rtype'. */
1509 tree basetype = TYPE_MAIN_VARIANT (rtype);
1510
1511 /* Skip the pointer - if none, it's not an Objective-C object or
1512 class. */
1513 if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE)
1514 basetype = TREE_TYPE (basetype);
1515 else
1516 return NULL_TREE;
1517
1518 /* Traverse typedefs. */
1519 while (basetype != NULL_TREE
1520 && TREE_CODE (basetype) == RECORD_TYPE
1521 && OBJC_TYPE_NAME (basetype)
1522 && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL
1523 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)))
1524 basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype));
1525
1526 if (basetype != NULL_TREE && TYPED_OBJECT (basetype))
1527 {
1528 tree interface_type = TYPE_OBJC_INTERFACE (basetype);
1529 tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype);
1530
1531 if (interface_type
1532 && (TREE_CODE (interface_type) == CLASS_INTERFACE_TYPE
1533 || TREE_CODE (interface_type) == CATEGORY_INTERFACE_TYPE
1534 || TREE_CODE (interface_type) == PROTOCOL_INTERFACE_TYPE))
1535 {
1536 /* Not sure 'rtype' could ever be a class here! Just
1537 for safety we keep the checks. */
1538 if (!IS_CLASS (rtype))
1539 {
1540 x = lookup_property (interface_type, property_ident);
1541
1542 if (x == NULL_TREE)
1543 x = lookup_property_in_protocol_list (protocol_list,
1544 property_ident);
1545 }
1546
1547 if (x == NULL_TREE)
1548 {
1549 /* Try the dot-syntax without a declared property.
1550 If we are inside a method implementation, it is
1551 possible that they may refer to a setter/getter
1552 that is not declared in the interface, but exists
1553 locally in the implementation. In that case, get
1554 the implementation context and use it. */
1555 tree implementation = NULL_TREE;
1556
1557 if (objc_implementation_context
1558 && CLASS_NAME (objc_implementation_context)
1559 == OBJC_TYPE_NAME (interface_type))
1560 implementation = objc_implementation_context;
1561
1562 x = maybe_make_artificial_property_decl (interface_type,
1563 implementation,
1564 protocol_list,
1565 property_ident,
1566 IS_CLASS (rtype),
1567 NULL_TREE);
1568 }
1569 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1570 {
1571 tree implementation = NULL_TREE;
1572
1573 if (objc_implementation_context
1574 && CLASS_NAME (objc_implementation_context)
1575 == OBJC_TYPE_NAME (interface_type))
1576 implementation = objc_implementation_context;
1577
1578 x = maybe_make_artificial_property_decl (interface_type,
1579 implementation,
1580 protocol_list,
1581 property_ident,
1582 false,
1583 PROPERTY_GETTER_NAME (x));
1584 }
1585 }
1586 }
1587 }
1588
1589 if (x)
1590 {
1591 tree expression;
1592 tree getter_call;
1593 tree deprecated_method_prototype = NULL_TREE;
1594
1595 /* We have an additional nasty problem here; if this
1596 PROPERTY_REF needs to become a 'getter', then the conversion
1597 from PROPERTY_REF into a getter call happens in gimplify,
1598 after the selector table has already been generated and when
1599 it is too late to add another selector to it. To work around
1600 the problem, we always create the getter call at this stage,
1601 which puts the selector in the table. Note that if the
1602 PROPERTY_REF becomes a 'setter' instead of a 'getter', then
1603 we have added a selector too many to the selector table.
1604 This is a little inefficient.
1605
1606 Also note that method calls to 'self' and 'super' require the
1607 context (self_decl, UOBJS_SUPER_decl,
1608 objc_implementation_context etc) to be built correctly; this
1609 is yet another reason why building the call at the gimplify
1610 stage (when this context has been lost) is not very
1611 practical. If we build it at this stage, we know it will
1612 always be built correctly.
1613
1614 If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial
1615 property decl created to deal with a dotsyntax not really
1616 referring to an existing property) then do not try to build a
1617 call to the getter as there is no getter. */
1618 if (PROPERTY_HAS_NO_GETTER (x))
1619 getter_call = NULL_TREE;
1620 else
1621 getter_call = objc_finish_message_expr
1622 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1623 /* Disable the immediate deprecation warning if the getter
1624 is deprecated, but record the fact that the getter is
1625 deprecated by setting PROPERTY_REF_DEPRECATED_GETTER to
1626 the method prototype. */
1627 &deprecated_method_prototype);
1628
1629 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1630 deprecated_method_prototype);
1631 SET_EXPR_LOCATION (expression, input_location);
1632 TREE_SIDE_EFFECTS (expression) = 1;
1633
1634 return expression;
1635 }
1636
1637 return NULL_TREE;
1638 }
1639
1640 /* This hook routine is invoked by the parser when an expression such
1641 as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the
1642 Objective-C 2.0 dot-syntax applied to classes, so we need to
1643 convert it into a setter/getter call on the class. */
1644 tree
1645 objc_build_class_component_ref (tree class_name, tree property_ident)
1646 {
1647 tree x = NULL_TREE;
1648 tree object, rtype;
1649
1650 if (flag_objc1_only)
1651 error_at (input_location, "the dot syntax is not available in Objective-C 1.0");
1652
1653 if (class_name == NULL_TREE || class_name == error_mark_node
1654 || TREE_CODE (class_name) != IDENTIFIER_NODE)
1655 return error_mark_node;
1656
1657 if (property_ident == NULL_TREE || property_ident == error_mark_node
1658 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1659 return NULL_TREE;
1660
1661 object = objc_get_class_reference (class_name);
1662 if (!object)
1663 {
1664 /* We know that 'class_name' is an Objective-C class name as the
1665 parser won't call this function if it is not. This is only a
1666 double-check for safety. */
1667 error_at (input_location, "could not find class %qE", class_name);
1668 return error_mark_node;
1669 }
1670
1671 rtype = lookup_interface (class_name);
1672 if (!rtype)
1673 {
1674 /* Again, this should never happen, but we do check. */
1675 error_at (input_location, "could not find interface for class %qE", class_name);
1676 return error_mark_node;
1677 }
1678 else
1679 {
1680 if (TREE_DEPRECATED (rtype))
1681 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", class_name);
1682 }
1683
1684 x = maybe_make_artificial_property_decl (rtype, NULL_TREE, NULL_TREE,
1685 property_ident,
1686 true, NULL_TREE);
1687
1688 if (x)
1689 {
1690 tree expression;
1691 tree getter_call;
1692 tree deprecated_method_prototype = NULL_TREE;
1693
1694 if (PROPERTY_HAS_NO_GETTER (x))
1695 getter_call = NULL_TREE;
1696 else
1697 getter_call = objc_finish_message_expr
1698 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1699 &deprecated_method_prototype);
1700
1701 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1702 deprecated_method_prototype);
1703 SET_EXPR_LOCATION (expression, input_location);
1704 TREE_SIDE_EFFECTS (expression) = 1;
1705
1706 return expression;
1707 }
1708 else
1709 {
1710 error_at (input_location, "could not find setter/getter for %qE in class %qE",
1711 property_ident, class_name);
1712 return error_mark_node;
1713 }
1714
1715 return NULL_TREE;
1716 }
1717
1718
1719
1720 /* This is used because we don't want to expose PROPERTY_REF to the
1721 C/C++ frontends. Maybe we should! */
1722 bool
1723 objc_is_property_ref (tree node)
1724 {
1725 if (node && TREE_CODE (node) == PROPERTY_REF)
1726 return true;
1727 else
1728 return false;
1729 }
1730
1731 /* This function builds a setter call for a PROPERTY_REF (real, for a
1732 declared property, or artificial, for a dot-syntax accessor which
1733 is not corresponding to a property). 'lhs' must be a PROPERTY_REF
1734 (the caller must check this beforehand). 'rhs' is the value to
1735 assign to the property. A plain setter call is returned, or
1736 error_mark_node if the property is readonly. */
1737
1738 static tree
1739 objc_build_setter_call (tree lhs, tree rhs)
1740 {
1741 tree object_expr = PROPERTY_REF_OBJECT (lhs);
1742 tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs);
1743
1744 if (PROPERTY_READONLY (property_decl))
1745 {
1746 error ("readonly property can not be set");
1747 return error_mark_node;
1748 }
1749 else
1750 {
1751 tree setter_argument = build_tree_list (NULL_TREE, rhs);
1752 tree setter;
1753
1754 /* TODO: Check that the setter return type is 'void'. */
1755
1756 /* TODO: Decay arguments in C. */
1757 setter = objc_finish_message_expr (object_expr,
1758 PROPERTY_SETTER_NAME (property_decl),
1759 setter_argument, NULL);
1760 return setter;
1761 }
1762
1763 /* Unreachable, but the compiler may not realize. */
1764 return error_mark_node;
1765 }
1766
1767 /* This hook routine is called when a MODIFY_EXPR is being built. We
1768 check what is being modified; if it is a PROPERTY_REF, we need to
1769 generate a 'setter' function call for the property. If this is not
1770 a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go
1771 on creating their MODIFY_EXPR.
1772
1773 This is used for example if you write
1774
1775 object.count = 1;
1776
1777 where 'count' is a property. The left-hand side creates a
1778 PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR
1779 to assign something to it. We intercept that here, and generate a
1780 call to the 'setter' method instead. */
1781 tree
1782 objc_maybe_build_modify_expr (tree lhs, tree rhs)
1783 {
1784 if (lhs && TREE_CODE (lhs) == PROPERTY_REF)
1785 {
1786 /* Building a simple call to the setter method would work for cases such as
1787
1788 object.count = 1;
1789
1790 but wouldn't work for cases such as
1791
1792 count = object2.count = 1;
1793
1794 to get these to work with very little effort, we build a
1795 compound statement which does the setter call (to set the
1796 property to 'rhs'), but which can also be evaluated returning
1797 the 'rhs'. If the 'rhs' has no side effects, we can simply
1798 evaluate it twice, building
1799
1800 ([object setProperty: rhs]; rhs)
1801
1802 If it has side effects, we put it in a temporary variable first,
1803 so we create the following:
1804
1805 (temp = rhs; [object setProperty: temp]; temp)
1806
1807 setter_argument is rhs in the first case, and temp in the second
1808 case.
1809 */
1810 tree setter_argument;
1811
1812 /* s1, s2 and s3 are the tree statements that we need in the
1813 compound expression. */
1814 tree s1, s2, s3, compound_expr;
1815
1816 if (TREE_SIDE_EFFECTS (rhs))
1817 {
1818 tree bind;
1819
1820 /* Declare __objc_property_temp in a local bind. */
1821 setter_argument = objc_create_temporary_var (TREE_TYPE (rhs), "__objc_property_temp");
1822 DECL_SOURCE_LOCATION (setter_argument) = input_location;
1823 bind = build3 (BIND_EXPR, void_type_node, setter_argument, NULL, NULL);
1824 SET_EXPR_LOCATION (bind, input_location);
1825 TREE_SIDE_EFFECTS (bind) = 1;
1826 add_stmt (bind);
1827
1828 /* s1: x = rhs */
1829 s1 = build_modify_expr (input_location, setter_argument, NULL_TREE,
1830 NOP_EXPR,
1831 input_location, rhs, NULL_TREE);
1832 SET_EXPR_LOCATION (s1, input_location);
1833 }
1834 else
1835 {
1836 /* No s1. */
1837 setter_argument = rhs;
1838 s1 = NULL_TREE;
1839 }
1840
1841 /* Now build the compound statement. */
1842
1843 /* s2: [object setProperty: x] */
1844 s2 = objc_build_setter_call (lhs, setter_argument);
1845
1846 /* This happens if building the setter failed because the
1847 property is readonly. */
1848 if (s2 == error_mark_node)
1849 return error_mark_node;
1850
1851 SET_EXPR_LOCATION (s2, input_location);
1852
1853 /* s3: x */
1854 s3 = convert (TREE_TYPE (lhs), setter_argument);
1855
1856 /* Now build the compound statement (s1, s2, s3) or (s2, s3) as
1857 appropriate. */
1858 if (s1)
1859 compound_expr = build_compound_expr (input_location, build_compound_expr (input_location, s1, s2), s3);
1860 else
1861 compound_expr = build_compound_expr (input_location, s2, s3);
1862
1863 /* Without this, with -Wall you get a 'valued computed is not
1864 used' every time there is a "object.property = x" where the
1865 value of the resulting MODIFY_EXPR is not used. That is
1866 correct (maybe a more sophisticated implementation could
1867 avoid generating the compound expression if not needed), but
1868 we need to turn it off. */
1869 TREE_NO_WARNING (compound_expr) = 1;
1870 return compound_expr;
1871 }
1872 else
1873 return NULL_TREE;
1874 }
1875
1876 /* This hook is called by the frontend when one of the four unary
1877 expressions PREINCREMENT_EXPR, POSTINCREMENT_EXPR,
1878 PREDECREMENT_EXPR and POSTDECREMENT_EXPR is being built with an
1879 argument which is a PROPERTY_REF. For example, this happens if you have
1880
1881 object.count++;
1882
1883 where 'count' is a property. We need to use the 'getter' and
1884 'setter' for the property in an appropriate way to build the
1885 appropriate expression. 'code' is the code for the expression (one
1886 of the four mentioned above); 'argument' is the PROPERTY_REF, and
1887 'increment' is how much we need to add or subtract. */
1888 tree
1889 objc_build_incr_expr_for_property_ref (location_t location,
1890 enum tree_code code,
1891 tree argument, tree increment)
1892 {
1893 /* Here are the expressions that we want to build:
1894
1895 For PREINCREMENT_EXPR / PREDECREMENT_EXPR:
1896 (temp = [object property] +/- increment, [object setProperty: temp], temp)
1897
1898 For POSTINCREMENT_EXPR / POSTECREMENT_EXPR:
1899 (temp = [object property], [object setProperty: temp +/- increment], temp) */
1900
1901 tree temp_variable_decl, bind;
1902 /* s1, s2 and s3 are the tree statements that we need in the
1903 compound expression. */
1904 tree s1, s2, s3, compound_expr;
1905
1906 /* Safety check. */
1907 if (!argument || TREE_CODE (argument) != PROPERTY_REF)
1908 return error_mark_node;
1909
1910 /* Declare __objc_property_temp in a local bind. */
1911 temp_variable_decl = objc_create_temporary_var (TREE_TYPE (argument), "__objc_property_temp");
1912 DECL_SOURCE_LOCATION (temp_variable_decl) = location;
1913 bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL);
1914 SET_EXPR_LOCATION (bind, location);
1915 TREE_SIDE_EFFECTS (bind) = 1;
1916 add_stmt (bind);
1917
1918 /* Now build the compound statement. */
1919
1920 /* Note that the 'getter' is generated at gimplify time; at this
1921 time, we can simply put the property_ref (ie, argument) wherever
1922 we want the getter ultimately to be. */
1923
1924 /* s1: __objc_property_temp = [object property] <+/- increment> */
1925 switch (code)
1926 {
1927 case PREINCREMENT_EXPR:
1928 /* __objc_property_temp = [object property] + increment */
1929 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1930 NOP_EXPR,
1931 location, build2 (PLUS_EXPR, TREE_TYPE (argument),
1932 argument, increment), NULL_TREE);
1933 break;
1934 case PREDECREMENT_EXPR:
1935 /* __objc_property_temp = [object property] - increment */
1936 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1937 NOP_EXPR,
1938 location, build2 (MINUS_EXPR, TREE_TYPE (argument),
1939 argument, increment), NULL_TREE);
1940 break;
1941 case POSTINCREMENT_EXPR:
1942 case POSTDECREMENT_EXPR:
1943 /* __objc_property_temp = [object property] */
1944 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1945 NOP_EXPR,
1946 location, argument, NULL_TREE);
1947 break;
1948 default:
1949 gcc_unreachable ();
1950 }
1951
1952 /* s2: [object setProperty: __objc_property_temp <+/- increment>] */
1953 switch (code)
1954 {
1955 case PREINCREMENT_EXPR:
1956 case PREDECREMENT_EXPR:
1957 /* [object setProperty: __objc_property_temp] */
1958 s2 = objc_build_setter_call (argument, temp_variable_decl);
1959 break;
1960 case POSTINCREMENT_EXPR:
1961 /* [object setProperty: __objc_property_temp + increment] */
1962 s2 = objc_build_setter_call (argument,
1963 build2 (PLUS_EXPR, TREE_TYPE (argument),
1964 temp_variable_decl, increment));
1965 break;
1966 case POSTDECREMENT_EXPR:
1967 /* [object setProperty: __objc_property_temp - increment] */
1968 s2 = objc_build_setter_call (argument,
1969 build2 (MINUS_EXPR, TREE_TYPE (argument),
1970 temp_variable_decl, increment));
1971 break;
1972 default:
1973 gcc_unreachable ();
1974 }
1975
1976 /* This happens if building the setter failed because the property
1977 is readonly. */
1978 if (s2 == error_mark_node)
1979 return error_mark_node;
1980
1981 SET_EXPR_LOCATION (s2, location);
1982
1983 /* s3: __objc_property_temp */
1984 s3 = convert (TREE_TYPE (argument), temp_variable_decl);
1985
1986 /* Now build the compound statement (s1, s2, s3) */
1987 compound_expr = build_compound_expr (location, build_compound_expr (location, s1, s2), s3);
1988
1989 /* Prevent C++ from warning with -Wall that "right operand of comma
1990 operator has no effect". */
1991 TREE_NO_WARNING (compound_expr) = 1;
1992 return compound_expr;
1993 }
1994
1995 tree
1996 objc_build_method_signature (bool is_class_method, tree rettype, tree selector,
1997 tree optparms, bool ellipsis)
1998 {
1999 if (is_class_method)
2000 return build_method_decl (CLASS_METHOD_DECL, rettype, selector,
2001 optparms, ellipsis);
2002 else
2003 return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector,
2004 optparms, ellipsis);
2005 }
2006
2007 void
2008 objc_add_method_declaration (bool is_class_method, tree decl, tree attributes)
2009 {
2010 if (!objc_interface_context)
2011 {
2012 /* PS: At the moment, due to how the parser works, it should be
2013 impossible to get here. But it's good to have the check in
2014 case the parser changes.
2015 */
2016 fatal_error (input_location,
2017 "method declaration not in @interface context");
2018 }
2019
2020 if (flag_objc1_only && attributes)
2021 error_at (input_location, "method attributes are not available in Objective-C 1.0");
2022
2023 objc_decl_method_attributes (&decl, attributes, 0);
2024 objc_add_method (objc_interface_context,
2025 decl,
2026 is_class_method,
2027 objc_method_optional_flag);
2028 }
2029
2030 /* Return 'true' if the method definition could be started, and
2031 'false' if not (because we are outside an @implementation context).
2032 EXPR is NULL or an expression that needs to be evaluated for the
2033 side effects of array size expressions in the parameters.
2034 */
2035 bool
2036 objc_start_method_definition (bool is_class_method, tree decl, tree attributes,
2037 tree expr)
2038 {
2039 if (!objc_implementation_context)
2040 {
2041 error ("method definition not in @implementation context");
2042 return false;
2043 }
2044
2045 if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node)
2046 return false;
2047
2048 #ifndef OBJCPLUS
2049 /* Indicate no valid break/continue context by setting these variables
2050 to some non-null, non-label value. We'll notice and emit the proper
2051 error message in c_finish_bc_stmt. */
2052 c_break_label = c_cont_label = size_zero_node;
2053 #endif
2054
2055 if (attributes)
2056 warning_at (input_location, 0, "method attributes can not be specified in @implementation context");
2057 else
2058 objc_decl_method_attributes (&decl, attributes, 0);
2059
2060 objc_add_method (objc_implementation_context,
2061 decl,
2062 is_class_method,
2063 /* is optional */ false);
2064 start_method_def (decl, expr);
2065 return true;
2066 }
2067
2068 void
2069 objc_add_instance_variable (tree decl)
2070 {
2071 (void) add_instance_variable (objc_ivar_context,
2072 objc_ivar_visibility,
2073 decl);
2074 }
2075
2076 /* Construct a C struct with same name as KLASS, a base struct with tag
2077 SUPER_NAME (if any), and FIELDS indicated. */
2078
2079 static tree
2080 objc_build_struct (tree klass, tree fields, tree super_name)
2081 {
2082 tree name = CLASS_NAME (klass);
2083 tree s = objc_start_struct (name);
2084 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
2085 tree t;
2086 vec<tree> objc_info = vNULL;
2087 int i;
2088
2089 if (super)
2090 {
2091 /* Prepend a packed variant of the base class into the layout. This
2092 is necessary to preserve ObjC ABI compatibility. */
2093 tree base = build_decl (input_location,
2094 FIELD_DECL, NULL_TREE, super);
2095 tree field = TYPE_FIELDS (super);
2096
2097 while (field && DECL_CHAIN (field)
2098 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
2099 field = DECL_CHAIN (field);
2100
2101 /* For ObjC ABI purposes, the "packed" size of a base class is
2102 the sum of the offset and the size (in bits) of the last field
2103 in the class. */
2104 DECL_SIZE (base)
2105 = (field && TREE_CODE (field) == FIELD_DECL
2106 ? size_binop (PLUS_EXPR,
2107 size_binop (PLUS_EXPR,
2108 size_binop
2109 (MULT_EXPR,
2110 convert (bitsizetype,
2111 DECL_FIELD_OFFSET (field)),
2112 bitsize_int (BITS_PER_UNIT)),
2113 DECL_FIELD_BIT_OFFSET (field)),
2114 DECL_SIZE (field))
2115 : bitsize_zero_node);
2116 DECL_SIZE_UNIT (base)
2117 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
2118 size_int (BITS_PER_UNIT));
2119 DECL_ARTIFICIAL (base) = 1;
2120 SET_DECL_ALIGN (base, 1);
2121 DECL_FIELD_CONTEXT (base) = s;
2122 #ifdef OBJCPLUS
2123 DECL_FIELD_IS_BASE (base) = 1;
2124
2125 if (fields)
2126 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
2127 #endif /* are following the ObjC ABI here. */
2128 DECL_CHAIN (base) = fields;
2129 fields = base;
2130 }
2131
2132 /* NB: Calling finish_struct() may cause type TYPE_OBJC_INFO
2133 information in all variants of this RECORD_TYPE to be destroyed
2134 (this is because the C frontend manipulates TYPE_LANG_SPECIFIC
2135 for something else and then will change all variants to use the
2136 same resulting TYPE_LANG_SPECIFIC, ignoring the fact that we use
2137 it for ObjC protocols and that such propagation will make all
2138 variants use the same objc_info), but it is therein that we store
2139 protocol conformance info (e.g., 'NSObject <MyProtocol>').
2140 Hence, we must save the ObjC-specific information before calling
2141 finish_struct(), and then reinstate it afterwards. */
2142
2143 for (t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
2144 {
2145 INIT_TYPE_OBJC_INFO (t);
2146 objc_info.safe_push (TYPE_OBJC_INFO (t));
2147 }
2148
2149 s = objc_finish_struct (s, fields);
2150
2151 for (i = 0, t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
2152 {
2153 /* We now want to restore the different TYPE_OBJC_INFO, but we
2154 have the additional problem that the C frontend doesn't just
2155 copy TYPE_LANG_SPECIFIC from one variant to the other; it
2156 actually makes all of them the *same* TYPE_LANG_SPECIFIC. As
2157 we need a different TYPE_OBJC_INFO for each (and
2158 TYPE_OBJC_INFO is a field in TYPE_LANG_SPECIFIC), we need to
2159 make a copy of each TYPE_LANG_SPECIFIC before we modify
2160 TYPE_OBJC_INFO. */
2161 if (TYPE_LANG_SPECIFIC (t))
2162 {
2163 /* Create a copy of TYPE_LANG_SPECIFIC. */
2164 struct lang_type *old_lang_type = TYPE_LANG_SPECIFIC (t);
2165 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2166 memcpy (TYPE_LANG_SPECIFIC (t), old_lang_type,
2167 SIZEOF_OBJC_TYPE_LANG_SPECIFIC);
2168 }
2169 else
2170 {
2171 /* Just create a new one. */
2172 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2173 }
2174 /* Replace TYPE_OBJC_INFO with the saved one. This restores any
2175 protocol information that may have been associated with the
2176 type. */
2177 TYPE_OBJC_INFO (t) = objc_info[i];
2178 /* Replace the IDENTIFIER_NODE with an actual @interface now
2179 that we have it. */
2180 TYPE_OBJC_INTERFACE (t) = klass;
2181 }
2182 objc_info.release ();
2183
2184 /* Use TYPE_BINFO structures to point at the super class, if any. */
2185 objc_xref_basetypes (s, super);
2186
2187 /* Mark this struct as a class template. */
2188 CLASS_STATIC_TEMPLATE (klass) = s;
2189
2190 return s;
2191 }
2192
2193 /* Mark DECL as being 'volatile' for purposes of Darwin
2194 _setjmp()/_longjmp() exception handling. Called from
2195 objc_mark_locals_volatile(). */
2196 void
2197 objc_volatilize_decl (tree decl)
2198 {
2199 /* Do not mess with variables that are 'static' or (already)
2200 'volatile'. */
2201 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
2202 && (TREE_CODE (decl) == VAR_DECL
2203 || TREE_CODE (decl) == PARM_DECL))
2204 {
2205 if (local_variables_to_volatilize == NULL)
2206 vec_alloc (local_variables_to_volatilize, 8);
2207
2208 vec_safe_push (local_variables_to_volatilize, decl);
2209 }
2210 }
2211
2212 /* Called when parsing of a function completes; if any local variables
2213 in the function were marked as variables to volatilize, change them
2214 to volatile. We do this at the end of the function when the
2215 warnings about discarding 'volatile' have already been produced.
2216 We are making the variables as volatile just to force the compiler
2217 to preserve them between setjmp/longjmp, but we don't want warnings
2218 for them as they aren't really volatile. */
2219 void
2220 objc_finish_function (void)
2221 {
2222 /* If there are any local variables to volatilize, volatilize them. */
2223 if (local_variables_to_volatilize)
2224 {
2225 int i;
2226 tree decl;
2227 FOR_EACH_VEC_ELT (*local_variables_to_volatilize, i, decl)
2228 {
2229 tree t = TREE_TYPE (decl);
2230
2231 t = build_qualified_type (t, TYPE_QUALS (t) | TYPE_QUAL_VOLATILE);
2232 TREE_TYPE (decl) = t;
2233 TREE_THIS_VOLATILE (decl) = 1;
2234 TREE_SIDE_EFFECTS (decl) = 1;
2235 DECL_REGISTER (decl) = 0;
2236 #ifndef OBJCPLUS
2237 C_DECL_REGISTER (decl) = 0;
2238 #endif
2239 }
2240
2241 /* Now we delete the vector. This sets it to NULL as well. */
2242 vec_free (local_variables_to_volatilize);
2243 }
2244 }
2245
2246 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
2247 (including its categories and superclasses) or by object type TYP.
2248 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
2249
2250 static bool
2251 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
2252 {
2253 bool class_type = (cls != NULL_TREE);
2254
2255 while (cls)
2256 {
2257 tree c;
2258
2259 /* Check protocols adopted by the class and its categories. */
2260 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
2261 {
2262 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
2263 return true;
2264 }
2265
2266 /* Repeat for superclasses. */
2267 cls = lookup_interface (CLASS_SUPER_NAME (cls));
2268 }
2269
2270 /* Check for any protocols attached directly to the object type. */
2271 if (TYPE_HAS_OBJC_INFO (typ))
2272 {
2273 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
2274 return true;
2275 }
2276
2277 if (warn)
2278 {
2279 *errbuf = 0;
2280 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
2281 /* NB: Types 'id' and 'Class' cannot reasonably be described as
2282 "implementing" a given protocol, since they do not have an
2283 implementation. */
2284 if (class_type)
2285 warning (0, "class %qs does not implement the %qE protocol",
2286 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2287 else
2288 warning (0, "type %qs does not conform to the %qE protocol",
2289 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2290 }
2291
2292 return false;
2293 }
2294
2295 /* Check if class RCLS and instance struct type RTYP conform to at least the
2296 same protocols that LCLS and LTYP conform to. */
2297
2298 static bool
2299 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
2300 {
2301 tree p;
2302 bool have_lproto = false;
2303
2304 while (lcls)
2305 {
2306 /* NB: We do _not_ look at categories defined for LCLS; these may or
2307 may not get loaded in, and therefore it is unreasonable to require
2308 that RCLS/RTYP must implement any of their protocols. */
2309 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
2310 {
2311 have_lproto = true;
2312
2313 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2314 return warn;
2315 }
2316
2317 /* Repeat for superclasses. */
2318 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
2319 }
2320
2321 /* Check for any protocols attached directly to the object type. */
2322 if (TYPE_HAS_OBJC_INFO (ltyp))
2323 {
2324 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
2325 {
2326 have_lproto = true;
2327
2328 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2329 return warn;
2330 }
2331 }
2332
2333 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
2334 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
2335 away with simply checking for 'id' or 'Class' (!RCLS), since this
2336 routine will not get called in other cases. */
2337 return have_lproto || (rcls != NULL_TREE);
2338 }
2339
2340 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
2341 Both TYPE1 and TYPE2 must be pointers, and already determined to be
2342 compatible by objc_compare_types() below. */
2343
2344 tree
2345 objc_common_type (tree type1, tree type2)
2346 {
2347 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
2348
2349 while (POINTER_TYPE_P (inner1))
2350 {
2351 inner1 = TREE_TYPE (inner1);
2352 inner2 = TREE_TYPE (inner2);
2353 }
2354
2355 /* If one type is derived from another, return the base type. */
2356 if (DERIVED_FROM_P (inner1, inner2))
2357 return type1;
2358 else if (DERIVED_FROM_P (inner2, inner1))
2359 return type2;
2360
2361 /* If both types are 'Class', return 'Class'. */
2362 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
2363 return objc_class_type;
2364
2365 /* Otherwise, return 'id'. */
2366 return objc_object_type;
2367 }
2368
2369 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
2370 an instance of RTYP to an instance of LTYP or to compare the two
2371 (if ARGNO is equal to -3), per ObjC type system rules. Before
2372 returning 'true', this routine may issue warnings related to, e.g.,
2373 protocol conformance. When returning 'false', the routine must
2374 produce absolutely no warnings; the C or C++ front-end will do so
2375 instead, if needed. If either LTYP or RTYP is not an Objective-C
2376 type, the routine must return 'false'.
2377
2378 The ARGNO parameter is encoded as follows:
2379 >= 1 Parameter number (CALLEE contains function being called);
2380 0 Return value;
2381 -1 Assignment;
2382 -2 Initialization;
2383 -3 Comparison (LTYP and RTYP may match in either direction);
2384 -4 Silent comparison (for C++ overload resolution);
2385 -5 Silent "specialization" comparison for RTYP to be a "specialization"
2386 of LTYP (a specialization means that RTYP is LTYP plus some constraints,
2387 so that each object of type RTYP is also of type LTYP). This is used
2388 when comparing property types. */
2389
2390 bool
2391 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
2392 {
2393 tree lcls, rcls, lproto, rproto;
2394 bool pointers_compatible;
2395
2396 /* We must be dealing with pointer types */
2397 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
2398 return false;
2399
2400 do
2401 {
2402 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2403 rtyp = TREE_TYPE (rtyp);
2404 }
2405 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2406
2407 /* We must also handle function pointers, since ObjC is a bit more
2408 lenient than C or C++ on this. */
2409 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
2410 {
2411 function_args_iterator liter, riter;
2412
2413 /* Return types must be covariant. */
2414 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
2415 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
2416 argno, callee))
2417 return false;
2418
2419 /* Argument types must be contravariant. */
2420 function_args_iter_init (&liter, ltyp);
2421 function_args_iter_init (&riter, rtyp);
2422
2423 while (1)
2424 {
2425 ltyp = function_args_iter_cond (&liter);
2426 rtyp = function_args_iter_cond (&riter);
2427
2428 /* If we've exhaused both lists simulateously, we're done. */
2429 if (ltyp == NULL_TREE && rtyp == NULL_TREE)
2430 break;
2431
2432 /* If one list is shorter than the other, they fail to match. */
2433 if (ltyp == NULL_TREE || rtyp == NULL_TREE)
2434 return false;
2435
2436 if (!comptypes (rtyp, ltyp)
2437 && !objc_compare_types (rtyp, ltyp, argno, callee))
2438 return false;
2439
2440 function_args_iter_next (&liter);
2441 function_args_iter_next (&riter);
2442 }
2443
2444 return true;
2445 }
2446
2447 /* Past this point, we are only interested in ObjC class instances,
2448 or 'id' or 'Class'. */
2449 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
2450 return false;
2451
2452 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
2453 && !TYPE_HAS_OBJC_INFO (ltyp))
2454 return false;
2455
2456 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
2457 && !TYPE_HAS_OBJC_INFO (rtyp))
2458 return false;
2459
2460 /* Past this point, we are committed to returning 'true' to the caller
2461 (unless performing a silent comparison; see below). However, we can
2462 still warn about type and/or protocol mismatches. */
2463
2464 if (TYPE_HAS_OBJC_INFO (ltyp))
2465 {
2466 lcls = TYPE_OBJC_INTERFACE (ltyp);
2467 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
2468 }
2469 else
2470 lcls = lproto = NULL_TREE;
2471
2472 if (TYPE_HAS_OBJC_INFO (rtyp))
2473 {
2474 rcls = TYPE_OBJC_INTERFACE (rtyp);
2475 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
2476 }
2477 else
2478 rcls = rproto = NULL_TREE;
2479
2480 /* If we could not find an @interface declaration, we must have
2481 only seen a @class declaration; for purposes of type comparison,
2482 treat it as a stand-alone (root) class. */
2483
2484 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
2485 lcls = NULL_TREE;
2486
2487 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
2488 rcls = NULL_TREE;
2489
2490 /* If either type is an unqualified 'id', we're done. This is because
2491 an 'id' can be assigned to or from any type with no warnings. */
2492 if (argno != -5)
2493 {
2494 if ((!lproto && objc_is_object_id (ltyp))
2495 || (!rproto && objc_is_object_id (rtyp)))
2496 return true;
2497 }
2498 else
2499 {
2500 /* For property checks, though, an 'id' is considered the most
2501 general type of object, hence if you try to specialize an
2502 'NSArray *' (ltyp) property with an 'id' (rtyp) one, we need
2503 to warn. */
2504 if (!lproto && objc_is_object_id (ltyp))
2505 return true;
2506 }
2507
2508 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
2509
2510 /* If the underlying types are the same, and at most one of them has
2511 a protocol list, we do not need to issue any diagnostics. */
2512 if (pointers_compatible && (!lproto || !rproto))
2513 return true;
2514
2515 /* If exactly one of the types is 'Class', issue a diagnostic; any
2516 exceptions of this rule have already been handled. */
2517 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
2518 pointers_compatible = false;
2519 /* Otherwise, check for inheritance relations. */
2520 else
2521 {
2522 if (!pointers_compatible)
2523 {
2524 /* Again, if any of the two is an 'id', we're satisfied,
2525 unless we're comparing properties, in which case only an
2526 'id' on the left-hand side (old property) is good
2527 enough. */
2528 if (argno != -5)
2529 pointers_compatible
2530 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
2531 else
2532 pointers_compatible = objc_is_object_id (ltyp);
2533 }
2534
2535 if (!pointers_compatible)
2536 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
2537
2538 if (!pointers_compatible && (argno == -3 || argno == -4))
2539 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
2540 }
2541
2542 /* If the pointers match modulo protocols, check for protocol conformance
2543 mismatches. */
2544 if (pointers_compatible)
2545 {
2546 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
2547 argno != -3);
2548
2549 if (!pointers_compatible && argno == -3)
2550 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
2551 argno != -3);
2552 }
2553
2554 if (!pointers_compatible)
2555 {
2556 /* The two pointers are not exactly compatible. Issue a warning, unless
2557 we are performing a silent comparison, in which case return 'false'
2558 instead. */
2559 /* NB: For the time being, we shall make our warnings look like their
2560 C counterparts. In the future, we may wish to make them more
2561 ObjC-specific. */
2562 switch (argno)
2563 {
2564 case -5:
2565 case -4:
2566 return false;
2567
2568 case -3:
2569 warning (0, "comparison of distinct Objective-C types lacks a cast");
2570 break;
2571
2572 case -2:
2573 warning (0, "initialization from distinct Objective-C type");
2574 break;
2575
2576 case -1:
2577 warning (0, "assignment from distinct Objective-C type");
2578 break;
2579
2580 case 0:
2581 warning (0, "distinct Objective-C type in return");
2582 break;
2583
2584 default:
2585 warning (0, "passing argument %d of %qE from distinct "
2586 "Objective-C type", argno, callee);
2587 break;
2588 }
2589 }
2590
2591 return true;
2592 }
2593
2594 /* This routine is similar to objc_compare_types except that function-pointers are
2595 excluded. This is because, caller assumes that common types are of (id, Object*)
2596 variety and calls objc_common_type to obtain a common type. There is no commonolty
2597 between two function-pointers in this regard. */
2598
2599 bool
2600 objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
2601 {
2602 if (objc_compare_types (ltyp, rtyp, argno, callee))
2603 {
2604 /* exclude function-pointer types. */
2605 do
2606 {
2607 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2608 rtyp = TREE_TYPE (rtyp);
2609 }
2610 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2611 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
2612 }
2613 return false;
2614 }
2615
2616 #ifndef OBJCPLUS
2617 /* Determine if CHILD is derived from PARENT. The routine assumes that
2618 both parameters are RECORD_TYPEs, and is non-reflexive. */
2619
2620 static bool
2621 objc_derived_from_p (tree parent, tree child)
2622 {
2623 parent = TYPE_MAIN_VARIANT (parent);
2624
2625 for (child = TYPE_MAIN_VARIANT (child);
2626 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
2627 {
2628 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
2629 (TYPE_BINFO (child),
2630 0)));
2631
2632 if (child == parent)
2633 return true;
2634 }
2635
2636 return false;
2637 }
2638 #endif
2639
2640 tree
2641 objc_build_component_ref (tree datum, tree component)
2642 {
2643 /* If COMPONENT is NULL, the caller is referring to the anonymous
2644 base class field. */
2645 if (!component)
2646 {
2647 tree base = TYPE_FIELDS (TREE_TYPE (datum));
2648
2649 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
2650 }
2651
2652 /* The 'build_component_ref' routine has been removed from the C++
2653 front-end, but 'finish_class_member_access_expr' seems to be
2654 a worthy substitute. */
2655 #ifdef OBJCPLUS
2656 return finish_class_member_access_expr (datum, component, false,
2657 tf_warning_or_error);
2658 #else
2659 return build_component_ref (input_location, datum, component,
2660 UNKNOWN_LOCATION);
2661 #endif
2662 }
2663
2664 /* Recursively copy inheritance information rooted at BINFO. To do this,
2665 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
2666
2667 static tree
2668 objc_copy_binfo (tree binfo)
2669 {
2670 tree btype = BINFO_TYPE (binfo);
2671 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
2672 tree base_binfo;
2673 int ix;
2674
2675 BINFO_TYPE (binfo2) = btype;
2676 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
2677 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
2678
2679 /* Recursively copy base binfos of BINFO. */
2680 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2681 {
2682 tree base_binfo2 = objc_copy_binfo (base_binfo);
2683
2684 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
2685 BINFO_BASE_APPEND (binfo2, base_binfo2);
2686 }
2687
2688 return binfo2;
2689 }
2690
2691 /* Record superclass information provided in BASETYPE for ObjC class REF.
2692 This is loosely based on cp/decl.c:xref_basetypes(). */
2693
2694 static void
2695 objc_xref_basetypes (tree ref, tree basetype)
2696 {
2697 tree variant;
2698 tree binfo = make_tree_binfo (basetype ? 1 : 0);
2699 TYPE_BINFO (ref) = binfo;
2700 BINFO_OFFSET (binfo) = size_zero_node;
2701 BINFO_TYPE (binfo) = ref;
2702
2703 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
2704 for (variant = ref; variant; variant = TYPE_NEXT_VARIANT (variant))
2705 TYPE_BINFO (variant) = binfo;
2706
2707 if (basetype)
2708 {
2709 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
2710
2711 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
2712 vec_alloc (BINFO_BASE_ACCESSES (binfo), 1);
2713 BINFO_BASE_APPEND (binfo, base_binfo);
2714 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
2715 }
2716 }
2717
2718 /* Called from finish_decl. */
2719
2720 void
2721 objc_check_decl (tree decl)
2722 {
2723 tree type = TREE_TYPE (decl);
2724
2725 if (TREE_CODE (type) != RECORD_TYPE)
2726 return;
2727 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
2728 error ("statically allocated instance of Objective-C class %qE",
2729 type);
2730 }
2731
2732 void
2733 objc_check_global_decl (tree decl)
2734 {
2735 tree id = DECL_NAME (decl);
2736 if (objc_is_class_name (id) && global_bindings_p())
2737 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id));
2738 }
2739
2740 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where
2741 INTERFACE may either name an Objective-C class, or refer to the
2742 special 'id' or 'Class' types. If INTERFACE is not a valid ObjC
2743 type, just return it unchanged. This function is often called when
2744 PROTOCOLS is NULL_TREE, in which case we simply look up the
2745 appropriate INTERFACE. */
2746
2747 tree
2748 objc_get_protocol_qualified_type (tree interface, tree protocols)
2749 {
2750 /* If INTERFACE is not provided, default to 'id'. */
2751 tree type = (interface ? objc_is_id (interface) : objc_object_type);
2752 bool is_ptr = (type != NULL_TREE);
2753
2754 if (!is_ptr)
2755 {
2756 type = objc_is_class_name (interface);
2757
2758 if (type)
2759 {
2760 /* If looking at a typedef, retrieve the precise type it
2761 describes. */
2762 if (TREE_CODE (interface) == IDENTIFIER_NODE)
2763 interface = identifier_global_value (interface);
2764
2765 type = ((interface && TREE_CODE (interface) == TYPE_DECL
2766 && DECL_ORIGINAL_TYPE (interface))
2767 ? DECL_ORIGINAL_TYPE (interface)
2768 : xref_tag (RECORD_TYPE, type));
2769 }
2770 else
2771 {
2772 /* This case happens when we are given an 'interface' which
2773 is not a valid class name. For example if a typedef was
2774 used, and 'interface' really is the identifier of the
2775 typedef, but when you resolve it you don't get an
2776 Objective-C class, but something else, such as 'int'.
2777 This is an error; protocols make no sense unless you use
2778 them with Objective-C objects. */
2779 error_at (input_location, "only Objective-C object types can be qualified with a protocol");
2780
2781 /* Try to recover. Ignore the invalid class name, and treat
2782 the object as an 'id' to silence further warnings about
2783 the class. */
2784 type = objc_object_type;
2785 is_ptr = true;
2786 }
2787 }
2788
2789 if (protocols)
2790 {
2791 type = build_variant_type_copy (type);
2792
2793 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
2794 to the pointee. */
2795 if (is_ptr)
2796 {
2797 tree orig_pointee_type = TREE_TYPE (type);
2798 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
2799
2800 /* Set up the canonical type information. */
2801 TYPE_CANONICAL (type)
2802 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
2803
2804 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
2805 type = TREE_TYPE (type);
2806 }
2807
2808 /* Look up protocols and install in lang specific list. */
2809 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
2810 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols
2811 (protocols, /* definition_required */ false);
2812
2813 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
2814 return the pointer to the new pointee variant. */
2815 if (is_ptr)
2816 type = TYPE_POINTER_TO (type);
2817 else
2818 TYPE_OBJC_INTERFACE (type)
2819 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
2820 }
2821
2822 return type;
2823 }
2824
2825 /* Check for circular dependencies in protocols. The arguments are
2826 PROTO, the protocol to check, and LIST, a list of protocol it
2827 conforms to. */
2828
2829 static void
2830 check_protocol_recursively (tree proto, tree list)
2831 {
2832 tree p;
2833
2834 for (p = list; p; p = TREE_CHAIN (p))
2835 {
2836 tree pp = TREE_VALUE (p);
2837
2838 if (TREE_CODE (pp) == IDENTIFIER_NODE)
2839 pp = lookup_protocol (pp, /* warn if deprecated */ false,
2840 /* definition_required */ false);
2841
2842 if (pp == proto)
2843 fatal_error (input_location, "protocol %qE has circular dependency",
2844 PROTOCOL_NAME (pp));
2845 if (pp)
2846 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
2847 }
2848 }
2849
2850 /* Look up PROTOCOLS, and return a list of those that are found. If
2851 none are found, return NULL. Note that this function will emit a
2852 warning if a protocol is found and is deprecated. If
2853 'definition_required', then warn if the protocol is found but is
2854 not defined (ie, if we only saw a forward-declaration of the
2855 protocol (as in "@protocol NSObject;") not a real definition with
2856 the list of methods). */
2857 static tree
2858 lookup_and_install_protocols (tree protocols, bool definition_required)
2859 {
2860 tree proto;
2861 tree return_value = NULL_TREE;
2862
2863 if (protocols == error_mark_node)
2864 return NULL;
2865
2866 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
2867 {
2868 tree ident = TREE_VALUE (proto);
2869 tree p = lookup_protocol (ident, /* warn_if_deprecated */ true,
2870 definition_required);
2871
2872 if (p)
2873 return_value = chainon (return_value,
2874 build_tree_list (NULL_TREE, p));
2875 else if (ident != error_mark_node)
2876 error ("cannot find protocol declaration for %qE",
2877 ident);
2878 }
2879
2880 return return_value;
2881 }
2882
2883 static void
2884 build_common_objc_exception_stuff (void)
2885 {
2886 tree noreturn_list, nothrow_list, temp_type;
2887
2888 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
2889 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
2890
2891 /* void objc_exception_throw(id) __attribute__((noreturn)); */
2892 /* void objc_sync_enter(id); */
2893 /* void objc_sync_exit(id); */
2894 temp_type = build_function_type_list (void_type_node,
2895 objc_object_type,
2896 NULL_TREE);
2897 objc_exception_throw_decl
2898 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
2899 noreturn_list);
2900 /* Make sure that objc_exception_throw (id) claims that it may throw an
2901 exception. */
2902 TREE_NOTHROW (objc_exception_throw_decl) = 0;
2903
2904 objc_sync_enter_decl
2905 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
2906 NULL, nothrow_list);
2907
2908 objc_sync_exit_decl
2909 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
2910 NULL, nothrow_list);
2911 }
2912
2913 /* Purpose: "play" parser, creating/installing representations
2914 of the declarations that are required by Objective-C.
2915
2916 Model:
2917
2918 type_spec--------->sc_spec
2919 (tree_list) (tree_list)
2920 | |
2921 | |
2922 identifier_node identifier_node */
2923
2924 static void
2925 synth_module_prologue (void)
2926 {
2927 tree type;
2928 enum debug_info_type save_write_symbols = write_symbols;
2929 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
2930
2931 /* Suppress outputting debug symbols, because
2932 dbxout_init hasn't been called yet. */
2933 write_symbols = NO_DEBUG;
2934 debug_hooks = &do_nothing_debug_hooks;
2935
2936 #ifdef OBJCPLUS
2937 push_lang_context (lang_name_c); /* extern "C" */
2938 #endif
2939
2940 /* The following are also defined in <objc/objc.h> and friends. */
2941
2942 objc_object_id = get_identifier (TAG_OBJECT);
2943 objc_class_id = get_identifier (TAG_CLASS);
2944
2945 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
2946 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
2947
2948 objc_object_type = build_pointer_type (objc_object_reference);
2949 objc_class_type = build_pointer_type (objc_class_reference);
2950
2951 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
2952 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
2953
2954 /* Declare the 'id' and 'Class' typedefs. */
2955 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2956 TYPE_DECL,
2957 objc_object_name,
2958 objc_object_type));
2959 TREE_NO_WARNING (type) = 1;
2960
2961 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2962 TYPE_DECL,
2963 objc_class_name,
2964 objc_class_type));
2965 TREE_NO_WARNING (type) = 1;
2966
2967 /* Forward-declare '@interface Protocol'. */
2968 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
2969 objc_declare_class (type);
2970 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE, type));
2971
2972 /* Declare receiver type used for dispatching messages to 'super'. */
2973 /* `struct objc_super *' */
2974 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
2975 get_identifier (TAG_SUPER)));
2976
2977 /* Declare pointers to method and ivar lists. */
2978 objc_method_list_ptr = build_pointer_type
2979 (xref_tag (RECORD_TYPE,
2980 get_identifier (UTAG_METHOD_LIST)));
2981 objc_method_proto_list_ptr
2982 = build_pointer_type (xref_tag (RECORD_TYPE,
2983 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
2984 objc_ivar_list_ptr = build_pointer_type
2985 (xref_tag (RECORD_TYPE,
2986 get_identifier (UTAG_IVAR_LIST)));
2987
2988 build_common_objc_exception_stuff ();
2989
2990 /* Set-up runtime-specific templates, message and exception stuff. */
2991 (*runtime.initialize) ();
2992
2993 /* Declare objc_getProperty, object_setProperty and other property
2994 accessor helpers. */
2995 build_common_objc_property_accessor_helpers ();
2996
2997 /* Forward declare constant_string_id and constant_string_type. */
2998 if (!constant_string_class_name)
2999 constant_string_class_name = runtime.default_constant_string_class_name;
3000 constant_string_id = get_identifier (constant_string_class_name);
3001 objc_declare_class (constant_string_id);
3002
3003 /* Pre-build the following entities - for speed/convenience. */
3004 self_id = get_identifier ("self");
3005 ucmd_id = get_identifier ("_cmd");
3006
3007 /* Declare struct _objc_fast_enumeration_state { ... }; */
3008 build_fast_enumeration_state_template ();
3009
3010 /* void objc_enumeration_mutation (id) */
3011 type = build_function_type_list (void_type_node,
3012 objc_object_type, NULL_TREE);
3013 objc_enumeration_mutation_decl
3014 = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN,
3015 NULL, NULL_TREE);
3016 TREE_NOTHROW (objc_enumeration_mutation_decl) = 0;
3017
3018 #ifdef OBJCPLUS
3019 pop_lang_context ();
3020 #endif
3021
3022 write_symbols = save_write_symbols;
3023 debug_hooks = save_hooks;
3024 }
3025
3026 /* --- const strings --- */
3027
3028 /* Ensure that the ivar list for NSConstantString/NXConstantString
3029 (or whatever was specified via `-fconstant-string-class')
3030 contains fields at least as large as the following three, so that
3031 the runtime can stomp on them with confidence:
3032
3033 struct STRING_OBJECT_CLASS_NAME
3034 {
3035 Object isa;
3036 char *cString;
3037 unsigned int length;
3038 }; */
3039
3040 static int
3041 check_string_class_template (void)
3042 {
3043 tree field_decl = objc_get_class_ivars (constant_string_id);
3044
3045 #define AT_LEAST_AS_LARGE_AS(F, T) \
3046 (F && TREE_CODE (F) == FIELD_DECL \
3047 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
3048 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
3049
3050 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3051 return 0;
3052
3053 field_decl = DECL_CHAIN (field_decl);
3054 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3055 return 0;
3056
3057 field_decl = DECL_CHAIN (field_decl);
3058 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
3059
3060 #undef AT_LEAST_AS_LARGE_AS
3061 }
3062
3063 /* Avoid calling `check_string_class_template ()' more than once. */
3064 static GTY(()) int string_layout_checked;
3065
3066 /* Construct an internal string layout to be used as a template for
3067 creating NSConstantString/NXConstantString instances. */
3068
3069 static tree
3070 objc_build_internal_const_str_type (void)
3071 {
3072 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
3073 tree fields = build_decl (input_location,
3074 FIELD_DECL, NULL_TREE, ptr_type_node);
3075 tree field = build_decl (input_location,
3076 FIELD_DECL, NULL_TREE, ptr_type_node);
3077
3078 DECL_CHAIN (field) = fields; fields = field;
3079 field = build_decl (input_location,
3080 FIELD_DECL, NULL_TREE, unsigned_type_node);
3081 DECL_CHAIN (field) = fields; fields = field;
3082 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
3083 reverse order! */
3084 finish_builtin_struct (type, "__builtin_ObjCString",
3085 fields, NULL_TREE);
3086
3087 return type;
3088 }
3089
3090 /* Custom build_string which sets TREE_TYPE! */
3091
3092 tree
3093 my_build_string (int len, const char *str)
3094 {
3095 return fix_string_type (build_string (len, str));
3096 }
3097
3098 /* Build a string with contents STR and length LEN and convert it to a
3099 pointer. */
3100
3101 tree
3102 my_build_string_pointer (int len, const char *str)
3103 {
3104 tree string = my_build_string (len, str);
3105 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
3106 return build1 (ADDR_EXPR, ptrtype, string);
3107 }
3108
3109 hashval_t
3110 objc_string_hasher::hash (string_descriptor *ptr)
3111 {
3112 const_tree const str = ptr->literal;
3113 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3114 int i, len = TREE_STRING_LENGTH (str);
3115 hashval_t h = len;
3116
3117 for (i = 0; i < len; i++)
3118 h = ((h * 613) + p[i]);
3119
3120 return h;
3121 }
3122
3123 bool
3124 objc_string_hasher::equal (string_descriptor *ptr1, string_descriptor *ptr2)
3125 {
3126 const_tree const str1 = ptr1->literal;
3127 const_tree const str2 = ptr2->literal;
3128 int len1 = TREE_STRING_LENGTH (str1);
3129
3130 return (len1 == TREE_STRING_LENGTH (str2)
3131 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3132 len1));
3133 }
3134
3135 /* Given a chain of STRING_CST's, build a static instance of
3136 NXConstantString which points at the concatenation of those
3137 strings. We place the string object in the __string_objects
3138 section of the __OBJC segment. The Objective-C runtime will
3139 initialize the isa pointers of the string objects to point at the
3140 NXConstantString class object. */
3141
3142 tree
3143 objc_build_string_object (tree string)
3144 {
3145 tree constant_string_class;
3146 int length;
3147 tree addr;
3148 struct string_descriptor *desc, key;
3149
3150 /* We should be passed a STRING_CST. */
3151 gcc_checking_assert (TREE_CODE (string) == STRING_CST);
3152 length = TREE_STRING_LENGTH (string) - 1;
3153
3154 /* The target may have different ideas on how to construct an ObjC string
3155 literal. On Darwin (Mac OS X), for example, we may wish to obtain a
3156 constant CFString reference instead.
3157 At present, this is only supported for the NeXT runtime. */
3158 if (flag_next_runtime
3159 && targetcm.objc_construct_string_object)
3160 {
3161 tree constructor = (*targetcm.objc_construct_string_object) (string);
3162 if (constructor)
3163 return build1 (NOP_EXPR, objc_object_type, constructor);
3164 }
3165
3166 /* Check whether the string class being used actually exists and has the
3167 correct ivar layout. */
3168 if (!string_layout_checked)
3169 {
3170 string_layout_checked = -1;
3171 constant_string_class = lookup_interface (constant_string_id);
3172 internal_const_str_type = objc_build_internal_const_str_type ();
3173
3174 if (!constant_string_class
3175 || !(constant_string_type
3176 = CLASS_STATIC_TEMPLATE (constant_string_class)))
3177 error ("cannot find interface declaration for %qE",
3178 constant_string_id);
3179 /* The NSConstantString/NXConstantString ivar layout is now known. */
3180 else if (!check_string_class_template ())
3181 error ("interface %qE does not have valid constant string layout",
3182 constant_string_id);
3183 /* If the runtime can generate a literal reference to the string class,
3184 don't need to run a constructor. */
3185 else if (!(*runtime.setup_const_string_class_decl)())
3186 error ("cannot find reference tag for class %qE", constant_string_id);
3187 else
3188 {
3189 string_layout_checked = 1; /* Success! */
3190 add_class_reference (constant_string_id);
3191 }
3192 }
3193
3194 if (string_layout_checked == -1)
3195 return error_mark_node;
3196
3197 /* Perhaps we already constructed a constant string just like this one? */
3198 key.literal = string;
3199 string_descriptor **loc = string_htab->find_slot (&key, INSERT);
3200 desc = *loc;
3201
3202 if (!desc)
3203 {
3204 *loc = desc = ggc_alloc<string_descriptor> ();
3205 desc->literal = string;
3206 desc->constructor =
3207 (*runtime.build_const_string_constructor) (input_location, string, length);
3208 }
3209
3210 addr = convert (build_pointer_type (constant_string_type),
3211 build_unary_op (input_location,
3212 ADDR_EXPR, desc->constructor, 1));
3213
3214 return addr;
3215 }
3216
3217 /* Build a static constant CONSTRUCTOR
3218 with type TYPE and elements ELTS. */
3219
3220 tree
3221 objc_build_constructor (tree type, vec<constructor_elt, va_gc> *elts)
3222 {
3223 tree constructor = build_constructor (type, elts);
3224
3225 TREE_CONSTANT (constructor) = 1;
3226 TREE_STATIC (constructor) = 1;
3227 TREE_READONLY (constructor) = 1;
3228
3229 #ifdef OBJCPLUS
3230 /* Adjust for impedance mismatch. We should figure out how to build
3231 CONSTRUCTORs that consistently please both the C and C++ gods. */
3232 if (!(*elts)[0].index)
3233 TREE_TYPE (constructor) = init_list_type_node;
3234 #endif
3235
3236 return constructor;
3237 }
3238
3239 /* Return the DECL of the string IDENT in the SECTION. */
3240
3241 tree
3242 get_objc_string_decl (tree ident, enum string_section section)
3243 {
3244 tree chain;
3245
3246 switch (section)
3247 {
3248 case class_names:
3249 chain = class_names_chain;
3250 break;
3251 case meth_var_names:
3252 chain = meth_var_names_chain;
3253 break;
3254 case meth_var_types:
3255 chain = meth_var_types_chain;
3256 break;
3257 case prop_names_attr:
3258 chain = prop_names_attr_chain;
3259 break;
3260 default:
3261 gcc_unreachable ();
3262 }
3263
3264 for (; chain != 0; chain = TREE_CHAIN (chain))
3265 if (TREE_VALUE (chain) == ident)
3266 return (TREE_PURPOSE (chain));
3267
3268 /* We didn't find the entry. */
3269 return NULL_TREE;
3270 }
3271
3272 /* Create a class reference, but don't create a variable to reference
3273 it. */
3274
3275 void
3276 add_class_reference (tree ident)
3277 {
3278 tree chain;
3279
3280 if ((chain = cls_ref_chain))
3281 {
3282 tree tail;
3283 do
3284 {
3285 if (ident == TREE_VALUE (chain))
3286 return;
3287
3288 tail = chain;
3289 chain = TREE_CHAIN (chain);
3290 }
3291 while (chain);
3292
3293 /* Append to the end of the list */
3294 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
3295 }
3296 else
3297 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
3298 }
3299
3300 /* Get a class reference, creating it if necessary. Also create the
3301 reference variable. */
3302 tree
3303 objc_get_class_reference (tree ident)
3304 {
3305 tree orig_ident = (DECL_P (ident)
3306 ? DECL_NAME (ident)
3307 : TYPE_P (ident)
3308 ? OBJC_TYPE_NAME (ident)
3309 : ident);
3310 bool local_scope = false;
3311
3312 #ifdef OBJCPLUS
3313 if (processing_template_decl)
3314 /* Must wait until template instantiation time. */
3315 return build_min_nt_loc (UNKNOWN_LOCATION, CLASS_REFERENCE_EXPR, ident);
3316 #endif
3317
3318 if (TREE_CODE (ident) == TYPE_DECL)
3319 ident = (DECL_ORIGINAL_TYPE (ident)
3320 ? DECL_ORIGINAL_TYPE (ident)
3321 : TREE_TYPE (ident));
3322
3323 #ifdef OBJCPLUS
3324 if (TYPE_P (ident)
3325 && CP_TYPE_CONTEXT (ident) != global_namespace)
3326 local_scope = true;
3327 #endif
3328
3329 if (local_scope || !(ident = objc_is_class_name (ident)))
3330 {
3331 error ("%qE is not an Objective-C class name or alias",
3332 orig_ident);
3333 return error_mark_node;
3334 }
3335
3336 return (*runtime.get_class_reference) (ident);
3337 }
3338
3339 void
3340 objc_declare_alias (tree alias_ident, tree class_ident)
3341 {
3342 tree underlying_class;
3343
3344 #ifdef OBJCPLUS
3345 if (current_namespace != global_namespace) {
3346 error ("Objective-C declarations may only appear in global scope");
3347 }
3348 #endif /* OBJCPLUS */
3349
3350 if (!(underlying_class = objc_is_class_name (class_ident)))
3351 warning (0, "cannot find class %qE", class_ident);
3352 else if (objc_is_class_name (alias_ident))
3353 warning (0, "class %qE already exists", alias_ident);
3354 else
3355 {
3356 /* Implement @compatibility_alias as a typedef. */
3357 #ifdef OBJCPLUS
3358 push_lang_context (lang_name_c); /* extern "C" */
3359 #endif
3360 lang_hooks.decls.pushdecl (build_decl
3361 (input_location,
3362 TYPE_DECL,
3363 alias_ident,
3364 xref_tag (RECORD_TYPE, underlying_class)));
3365 #ifdef OBJCPLUS
3366 pop_lang_context ();
3367 #endif
3368 objc_map_put (alias_name_map, alias_ident, underlying_class);
3369 }
3370 }
3371
3372 void
3373 objc_declare_class (tree identifier)
3374 {
3375 #ifdef OBJCPLUS
3376 if (current_namespace != global_namespace) {
3377 error ("Objective-C declarations may only appear in global scope");
3378 }
3379 #endif /* OBJCPLUS */
3380
3381 if (! objc_is_class_name (identifier))
3382 {
3383 tree record = lookup_name (identifier), type = record;
3384
3385 if (record)
3386 {
3387 if (TREE_CODE (record) == TYPE_DECL)
3388 type = DECL_ORIGINAL_TYPE (record)
3389 ? DECL_ORIGINAL_TYPE (record)
3390 : TREE_TYPE (record);
3391
3392 if (!TYPE_HAS_OBJC_INFO (type)
3393 || !TYPE_OBJC_INTERFACE (type))
3394 {
3395 error ("%qE redeclared as different kind of symbol",
3396 identifier);
3397 error ("previous declaration of %q+D",
3398 record);
3399 }
3400 }
3401
3402 record = xref_tag (RECORD_TYPE, identifier);
3403 INIT_TYPE_OBJC_INFO (record);
3404 /* In the case of a @class declaration, we store the ident in
3405 the TYPE_OBJC_INTERFACE. If later an @interface is found,
3406 we'll replace the ident with the interface. */
3407 TYPE_OBJC_INTERFACE (record) = identifier;
3408 objc_map_put (class_name_map, identifier, NULL_TREE);
3409 }
3410 }
3411
3412 tree
3413 objc_is_class_name (tree ident)
3414 {
3415 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE)
3416 {
3417 tree t = identifier_global_value (ident);
3418 if (t)
3419 ident = t;
3420 }
3421
3422 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
3423 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
3424
3425 if (ident && TREE_CODE (ident) == RECORD_TYPE)
3426 ident = OBJC_TYPE_NAME (ident);
3427 #ifdef OBJCPLUS
3428 if (ident && TREE_CODE (ident) == TYPE_DECL)
3429 {
3430 tree type = TREE_TYPE (ident);
3431 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
3432 return NULL_TREE;
3433 ident = DECL_NAME (ident);
3434 }
3435 #endif
3436 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
3437 return NULL_TREE;
3438
3439 if (lookup_interface (ident))
3440 return ident;
3441
3442 {
3443 tree target;
3444
3445 target = objc_map_get (class_name_map, ident);
3446 if (target != OBJC_MAP_NOT_FOUND)
3447 return ident;
3448
3449 target = objc_map_get (alias_name_map, ident);
3450 if (target != OBJC_MAP_NOT_FOUND)
3451 return target;
3452 }
3453
3454 return 0;
3455 }
3456
3457 /* Check whether TYPE is either 'id' or 'Class'. */
3458
3459 tree
3460 objc_is_id (tree type)
3461 {
3462 if (type && TREE_CODE (type) == IDENTIFIER_NODE)
3463 {
3464 tree t = identifier_global_value (type);
3465 if (t)
3466 type = t;
3467 }
3468
3469 if (type && TREE_CODE (type) == TYPE_DECL)
3470 type = TREE_TYPE (type);
3471
3472 /* NB: This function may be called before the ObjC front-end has
3473 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3474 return (objc_object_type && type
3475 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
3476 ? type
3477 : NULL_TREE);
3478 }
3479
3480 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3481 class instance. This is needed by other parts of the compiler to
3482 handle ObjC types gracefully. */
3483
3484 tree
3485 objc_is_object_ptr (tree type)
3486 {
3487 tree ret;
3488
3489 type = TYPE_MAIN_VARIANT (type);
3490 if (!POINTER_TYPE_P (type))
3491 return 0;
3492
3493 ret = objc_is_id (type);
3494 if (!ret)
3495 ret = objc_is_class_name (TREE_TYPE (type));
3496
3497 return ret;
3498 }
3499
3500 static int
3501 objc_is_gcable_type (tree type, int or_strong_p)
3502 {
3503 tree name;
3504
3505 if (!TYPE_P (type))
3506 return 0;
3507 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
3508 return 1;
3509 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
3510 return 1;
3511 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
3512 return 0;
3513 type = TREE_TYPE (type);
3514 if (TREE_CODE (type) != RECORD_TYPE)
3515 return 0;
3516 name = TYPE_NAME (type);
3517 return (objc_is_class_name (name) != NULL_TREE);
3518 }
3519
3520 static tree
3521 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
3522 {
3523 if (expr == oldexpr)
3524 return newexpr;
3525
3526 switch (TREE_CODE (expr))
3527 {
3528 case COMPONENT_REF:
3529 return objc_build_component_ref
3530 (objc_substitute_decl (TREE_OPERAND (expr, 0),
3531 oldexpr,
3532 newexpr),
3533 DECL_NAME (TREE_OPERAND (expr, 1)));
3534 case ARRAY_REF:
3535 return build_array_ref (input_location,
3536 objc_substitute_decl (TREE_OPERAND (expr, 0),
3537 oldexpr,
3538 newexpr),
3539 TREE_OPERAND (expr, 1));
3540 case INDIRECT_REF:
3541 return build_indirect_ref (input_location,
3542 objc_substitute_decl (TREE_OPERAND (expr, 0),
3543 oldexpr,
3544 newexpr), RO_ARROW);
3545 default:
3546 return expr;
3547 }
3548 }
3549
3550 static tree
3551 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
3552 {
3553 tree func_params;
3554 /* The LHS parameter contains the expression 'outervar->memberspec';
3555 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3556 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3557 */
3558 tree offs
3559 = objc_substitute_decl
3560 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
3561 tree func
3562 = (flag_objc_direct_dispatch
3563 ? objc_assign_ivar_fast_decl
3564 : objc_assign_ivar_decl);
3565
3566 offs = convert (integer_type_node, build_unary_op (input_location,
3567 ADDR_EXPR, offs, 0));
3568 offs = fold (offs);
3569 func_params = tree_cons (NULL_TREE,
3570 convert (objc_object_type, rhs),
3571 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
3572 tree_cons (NULL_TREE, offs,
3573 NULL_TREE)));
3574
3575 return build_function_call (input_location, func, func_params);
3576 }
3577
3578 static tree
3579 objc_build_global_assignment (tree lhs, tree rhs)
3580 {
3581 tree func_params = tree_cons (NULL_TREE,
3582 convert (objc_object_type, rhs),
3583 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3584 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3585 NULL_TREE));
3586
3587 return build_function_call (input_location,
3588 objc_assign_global_decl, func_params);
3589 }
3590
3591 static tree
3592 objc_build_strong_cast_assignment (tree lhs, tree rhs)
3593 {
3594 tree func_params = tree_cons (NULL_TREE,
3595 convert (objc_object_type, rhs),
3596 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3597 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3598 NULL_TREE));
3599
3600 return build_function_call (input_location,
3601 objc_assign_strong_cast_decl, func_params);
3602 }
3603
3604 static int
3605 objc_is_gcable_p (tree expr)
3606 {
3607 return (TREE_CODE (expr) == COMPONENT_REF
3608 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
3609 : TREE_CODE (expr) == ARRAY_REF
3610 ? (objc_is_gcable_p (TREE_TYPE (expr))
3611 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
3612 : TREE_CODE (expr) == ARRAY_TYPE
3613 ? objc_is_gcable_p (TREE_TYPE (expr))
3614 : TYPE_P (expr)
3615 ? objc_is_gcable_type (expr, 1)
3616 : (objc_is_gcable_p (TREE_TYPE (expr))
3617 || (DECL_P (expr)
3618 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
3619 }
3620
3621 static int
3622 objc_is_ivar_reference_p (tree expr)
3623 {
3624 return (TREE_CODE (expr) == ARRAY_REF
3625 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
3626 : TREE_CODE (expr) == COMPONENT_REF
3627 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
3628 : 0);
3629 }
3630
3631 static int
3632 objc_is_global_reference_p (tree expr)
3633 {
3634 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
3635 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
3636 : DECL_P (expr)
3637 ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr))
3638 : 0);
3639 }
3640
3641 tree
3642 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
3643 {
3644 tree result = NULL_TREE, outer;
3645 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
3646
3647 /* This function is currently only used with the next runtime with
3648 garbage collection enabled (-fobjc-gc). */
3649 gcc_assert (flag_next_runtime);
3650
3651 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3652 will have been transformed to the form '*(type *)&expr'. */
3653 if (TREE_CODE (lhs) == INDIRECT_REF)
3654 {
3655 outer = TREE_OPERAND (lhs, 0);
3656
3657 while (!strong_cast_p
3658 && (CONVERT_EXPR_P (outer)
3659 || TREE_CODE (outer) == NON_LVALUE_EXPR))
3660 {
3661 tree lhstype = TREE_TYPE (outer);
3662
3663 /* Descend down the cast chain, and record the first objc_gc
3664 attribute found. */
3665 if (POINTER_TYPE_P (lhstype))
3666 {
3667 tree attr
3668 = lookup_attribute ("objc_gc",
3669 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
3670
3671 if (attr)
3672 strong_cast_p = 1;
3673 }
3674
3675 outer = TREE_OPERAND (outer, 0);
3676 }
3677 }
3678
3679 /* If we have a __strong cast, it trumps all else. */
3680 if (strong_cast_p)
3681 {
3682 if (modifycode != NOP_EXPR)
3683 goto invalid_pointer_arithmetic;
3684
3685 if (warn_assign_intercept)
3686 warning (0, "strong-cast assignment has been intercepted");
3687
3688 result = objc_build_strong_cast_assignment (lhs, rhs);
3689
3690 goto exit_point;
3691 }
3692
3693 /* the lhs must be of a suitable type, regardless of its underlying
3694 structure. */
3695 if (!objc_is_gcable_p (lhs))
3696 goto exit_point;
3697
3698 outer = lhs;
3699
3700 while (outer
3701 && (TREE_CODE (outer) == COMPONENT_REF
3702 || TREE_CODE (outer) == ARRAY_REF))
3703 outer = TREE_OPERAND (outer, 0);
3704
3705 if (TREE_CODE (outer) == INDIRECT_REF)
3706 {
3707 outer = TREE_OPERAND (outer, 0);
3708 indirect_p = 1;
3709 }
3710
3711 outer_gc_p = objc_is_gcable_p (outer);
3712
3713 /* Handle ivar assignments. */
3714 if (objc_is_ivar_reference_p (lhs))
3715 {
3716 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3717 doesn't cut it here), the best we can do here is suggest a cast. */
3718 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
3719 {
3720 /* We may still be able to use the global write barrier... */
3721 if (!indirect_p && objc_is_global_reference_p (outer))
3722 goto global_reference;
3723
3724 suggest_cast:
3725 if (modifycode == NOP_EXPR)
3726 {
3727 if (warn_assign_intercept)
3728 warning (0, "strong-cast may possibly be needed");
3729 }
3730
3731 goto exit_point;
3732 }
3733
3734 if (modifycode != NOP_EXPR)
3735 goto invalid_pointer_arithmetic;
3736
3737 if (warn_assign_intercept)
3738 warning (0, "instance variable assignment has been intercepted");
3739
3740 result = objc_build_ivar_assignment (outer, lhs, rhs);
3741
3742 goto exit_point;
3743 }
3744
3745 /* Likewise, intercept assignment to global/static variables if their type is
3746 GC-marked. */
3747 if (objc_is_global_reference_p (outer))
3748 {
3749 if (indirect_p)
3750 goto suggest_cast;
3751
3752 global_reference:
3753 if (modifycode != NOP_EXPR)
3754 {
3755 invalid_pointer_arithmetic:
3756 if (outer_gc_p)
3757 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3758
3759 goto exit_point;
3760 }
3761
3762 if (warn_assign_intercept)
3763 warning (0, "global/static variable assignment has been intercepted");
3764
3765 result = objc_build_global_assignment (lhs, rhs);
3766 }
3767
3768 /* In all other cases, fall back to the normal mechanism. */
3769 exit_point:
3770 return result;
3771 }
3772
3773 /* Implementation of the table mapping a class name (as an identifier)
3774 to a class node. The two public functions for it are
3775 lookup_interface() and add_interface(). add_interface() is only
3776 used in this file, so we can make it static. */
3777
3778 static GTY(()) objc_map_t interface_map;
3779
3780 static void
3781 interface_hash_init (void)
3782 {
3783 interface_map = objc_map_alloc_ggc (200);
3784 }
3785
3786 static tree
3787 add_interface (tree class_name, tree name)
3788 {
3789 /* Put interfaces on list in reverse order. */
3790 TREE_CHAIN (class_name) = interface_chain;
3791 interface_chain = class_name;
3792
3793 /* Add it to the map. */
3794 objc_map_put (interface_map, name, class_name);
3795
3796 return interface_chain;
3797 }
3798
3799 tree
3800 lookup_interface (tree ident)
3801 {
3802 #ifdef OBJCPLUS
3803 if (ident && TREE_CODE (ident) == TYPE_DECL)
3804 ident = DECL_NAME (ident);
3805 #endif
3806
3807 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
3808 return NULL_TREE;
3809
3810 {
3811 tree interface = objc_map_get (interface_map, ident);
3812
3813 if (interface == OBJC_MAP_NOT_FOUND)
3814 return NULL_TREE;
3815 else
3816 return interface;
3817 }
3818 }
3819
3820
3821
3822 /* Implement @defs (<classname>) within struct bodies. */
3823
3824 tree
3825 objc_get_class_ivars (tree class_name)
3826 {
3827 tree interface = lookup_interface (class_name);
3828
3829 if (interface)
3830 return get_class_ivars (interface, true);
3831
3832 error ("cannot find interface declaration for %qE",
3833 class_name);
3834
3835 return error_mark_node;
3836 }
3837
3838
3839 /* Functions used by the hashtable for field duplicates in
3840 objc_detect_field_duplicates(). Ideally, we'd use a standard
3841 key-value dictionary hashtable , and store as keys the field names,
3842 and as values the actual declarations (used to print nice error
3843 messages with the locations). But, the hashtable we are using only
3844 allows us to store keys in the hashtable, without values (it looks
3845 more like a set). So, we store the DECLs, but define equality as
3846 DECLs having the same name, and hash as the hash of the name. */
3847
3848 struct decl_name_hash : nofree_ptr_hash <tree_node>
3849 {
3850 static inline hashval_t hash (const tree_node *);
3851 static inline bool equal (const tree_node *, const tree_node *);
3852 };
3853
3854 inline hashval_t
3855 decl_name_hash::hash (const tree_node *q)
3856 {
3857 return (hashval_t) ((intptr_t)(DECL_NAME (q)) >> 3);
3858 }
3859
3860 inline bool
3861 decl_name_hash::equal (const tree_node *a, const tree_node *b)
3862 {
3863 return DECL_NAME (a) == DECL_NAME (b);
3864 }
3865
3866 /* Called when checking the variables in a struct. If we are not
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)
3877 {
3878 if (!objc_collecting_ivars || !objc_interface_context
3879 || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE)
3880 return false;
3881
3882 /* We have two ways of doing this check:
3883
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.
3889
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 }
3908
3909 /* Exit if there is nothing to do. */
3910 if (number_of_ivars_to_check == 0)
3911 return true;
3912
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. */
3937 if (number_of_ivars_to_check * (number_of_superclass_ivars
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. */
3943 hash_table<decl_name_hash> htab (37);
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 {
3956 tree_node **slot = htab.find_slot (ivar, INSERT);
3957 /* Do not check for duplicate instance
3958 variables in superclasses. Errors have
3959 already been generated. */
3960 *slot = ivar;
3961 }
3962 }
3963 }
3964
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 {
3976 tree duplicate_ivar = htab.find (ivar);
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 {
4003 tree_node **slot = htab.find_slot (ivar, INSERT);
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 }
4020 return true;
4021 }
4022 }
4023 }
4024
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));
4035
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;
4046
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;
4076 }
4077
4078 /* Used by: build_private_template, continue_class,
4079 and for @defs constructs. */
4080
4081 static tree
4082 get_class_ivars (tree interface, bool inherited)
4083 {
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))
4097 {
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);
4102 }
4103
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)
4117 {
4118 /* Warn only once per compilation unit. */
4119 static bool warned = false;
4120
4121 if (!warned)
4122 {
4123 error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4124 warned = true;
4125 }
4126 }
4127 }
4128
4129 static struct objc_try_context *cur_try_context;
4130
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. */
4133
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;
4143
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);
4149 }
4150
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
4157 void
4158 objc_begin_catch_clause (tree decl)
4159 {
4160 tree compound, type, t;
4161 bool ellipsis = false;
4162
4163 /* Begin a new scope that the entire catch clause will live in. */
4164 compound = c_begin_compound_stmt (true);
4165
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);
4188
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;
4195
4196 type = TREE_TYPE (decl);
4197 }
4198
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");
4214 type = error_mark_node;
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);
4226
4227 /* Skip from the pointer to the pointee. */
4228 if (TREE_CODE (x) == POINTER_TYPE)
4229 x = TREE_TYPE (x);
4230
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)))
4235 {
4236 is_typedef = true;
4237 x = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x));
4238 }
4239
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);
4244
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)))
4257 {
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;
4263 }
4264 }
4265 }
4266 }
4267
4268 t = (*runtime.begin_catch) (&cur_try_context, type, decl, compound, ellipsis);
4269 add_stmt (t);
4270 }
4271
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. */
4274
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;
4281
4282 CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1);
4283
4284 (*runtime.finish_catch) (&cur_try_context, c);
4285 }
4286
4287 /* Called after parsing a @finally clause and its associated BODY.
4288 Record the body for later placement. */
4289
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;
4296 }
4297
4298 /* Called to finalize a @try construct. */
4299
4300 tree
4301 objc_finish_try_stmt (void)
4302 {
4303 struct objc_try_context *c = cur_try_context;
4304 tree stmt;
4305
4306 if (c->catch_list == NULL && c->finally_body == NULL)
4307 error ("%<@try%> without %<@catch%> or %<@finally%>");
4308
4309 stmt = (*runtime.finish_try_stmt) (&cur_try_context);
4310 add_stmt (stmt);
4311
4312 cur_try_context = c->outer;
4313 free (c);
4314 return stmt;
4315 }
4316
4317 tree
4318 objc_build_throw_stmt (location_t loc, tree throw_expr)
4319 {
4320 bool rethrown = false;
4321
4322 objc_maybe_warn_exceptions (loc);
4323
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;
4327
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);
4354 }
4355
4356 tree
4357 objc_build_synchronized (location_t start_locus, tree object_expr, tree body)
4358 {
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;
4363
4364 /* Validate object_expr. If not valid, set it to error_mark_node. */
4365 if (object_expr != error_mark_node)
4366 {
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 }
4372 }
4373
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;
4385
4386 /* objc_sync_enter (object_expr); */
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);
4393
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);
4400
4401 /* @try { body; } */
4402 objc_begin_try_stmt (start_locus, body);
4403
4404 /* @finally { objc_sync_exit (object_expr); } */
4405 objc_build_finally_clause (input_location, call);
4406
4407 /* End of try statement. */
4408 return objc_finish_try_stmt ();
4409 }
4410 }
4411
4412 /* Construct a C struct corresponding to ObjC class CLASS, with the same
4413 name as the class:
4414
4415 struct <classname> {
4416 struct _objc_class *isa;
4417 ...
4418 }; */
4419
4420 static void
4421 build_private_template (tree klass)
4422 {
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));
4428
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;
4433
4434 /* Copy the attributes from the class to the type. */
4435 if (TREE_DEPRECATED (klass))
4436 TREE_DEPRECATED (record) = 1;
4437 }
4438 }
4439
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;
4447
4448 /* - (id) .cxx_construct { ... return self; } */
4449 /* - (void) .cxx_construct { ... } */
4450
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),
4462 false), NULL, NULL_TREE);
4463 body = begin_function_body ();
4464 compound_stmt = begin_compound_stmt (0);
4465
4466 ivar = CLASS_IVARS (implementation_template);
4467 /* Destroy ivars in reverse order. */
4468 if (dtor)
4469 ivar = nreverse (copy_list (ivar));
4470
4471 for (; ivar; ivar = TREE_CHAIN (ivar))
4472 {
4473 if (TREE_CODE (ivar) == FIELD_DECL)
4474 {
4475 tree type = TREE_TYPE (ivar);
4476
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));
4492 }
4493 }
4494
4495 /* The constructor returns 'self'. */
4496 if (!dtor)
4497 finish_return_stmt (self_decl);
4498
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 }
4505
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;
4517
4518 /* Error case, due to possibly an extra @end. */
4519 if (!objc_implementation_context)
4520 return;
4521
4522 /* We do not want to do this for categories, since they do not have
4523 their own ivars. */
4524
4525 if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE)
4526 return;
4527
4528 /* First, determine if we even need a constructor and/or destructor. */
4529
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);
4536
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 }
4554 }
4555
4556 /* Generate '- .cxx_construct' if needed. */
4557
4558 if (need_ctor)
4559 objc_generate_cxx_ctor_or_dtor (false);
4560
4561 /* Generate '- .cxx_destruct' if needed. */
4562
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);
4571 }
4572 #endif
4573
4574 static void
4575 error_with_ivar (const char *message, tree decl)
4576 {
4577 error_at (DECL_SOURCE_LOCATION (decl), "%s %qs",
4578 message, identifier_to_locale (gen_declaration (decl)));
4579
4580 }
4581
4582 static void
4583 check_ivars (tree inter, tree imp)
4584 {
4585 tree intdecls = CLASS_RAW_IVARS (inter);
4586 tree impdecls = CLASS_RAW_IVARS (imp);
4587
4588 while (1)
4589 {
4590 tree t1, t2;
4591
4592 #ifdef OBJCPLUS
4593 if (intdecls && TREE_CODE (intdecls) == TYPE_DECL)
4594 intdecls = TREE_CHAIN (intdecls);
4595 #endif
4596 if (intdecls == 0 && impdecls == 0)
4597 break;
4598 if (intdecls == 0 || impdecls == 0)
4599 {
4600 error ("inconsistent instance variable specification");
4601 break;
4602 }
4603
4604 t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
4605
4606 if (!comptypes (t1, t2)
4607 #ifdef OBJCPLUS
4608 || !tree_int_cst_equal (DECL_BIT_FIELD_REPRESENTATIVE (intdecls),
4609 DECL_BIT_FIELD_REPRESENTATIVE (impdecls))
4610 #else
4611 || !tree_int_cst_equal (DECL_INITIAL (intdecls),
4612 DECL_INITIAL (impdecls))
4613 #endif
4614 )
4615 {
4616 if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
4617 {
4618 error_with_ivar ("conflicting instance variable type",
4619 impdecls);
4620 error_with_ivar ("previous declaration of",
4621 intdecls);
4622 }
4623 else /* both the type and the name don't match */
4624 {
4625 error ("inconsistent instance variable specification");
4626 break;
4627 }
4628 }
4629
4630 else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
4631 {
4632 error_with_ivar ("conflicting instance variable name",
4633 impdecls);
4634 error_with_ivar ("previous declaration of",
4635 intdecls);
4636 }
4637
4638 intdecls = DECL_CHAIN (intdecls);
4639 impdecls = DECL_CHAIN (impdecls);
4640 }
4641 }
4642
4643
4644 static void
4645 mark_referenced_methods (void)
4646 {
4647 struct imp_entry *impent;
4648 tree chain;
4649
4650 for (impent = imp_list; impent; impent = impent->next)
4651 {
4652 chain = CLASS_CLS_METHODS (impent->imp_context);
4653 while (chain)
4654 {
4655 cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output ();
4656 chain = DECL_CHAIN (chain);
4657 }
4658
4659 chain = CLASS_NST_METHODS (impent->imp_context);
4660 while (chain)
4661 {
4662 cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output ();
4663 chain = DECL_CHAIN (chain);
4664 }
4665 }
4666 }
4667
4668 /* If type is empty or only type qualifiers are present, add default
4669 type of id (otherwise grokdeclarator will default to int). */
4670 static inline tree
4671 adjust_type_for_id_default (tree type)
4672 {
4673 if (!type)
4674 type = make_node (TREE_LIST);
4675
4676 if (!TREE_VALUE (type))
4677 TREE_VALUE (type) = objc_object_type;
4678 else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE
4679 && TYPED_OBJECT (TREE_VALUE (type)))
4680 error ("can not use an object as parameter to a method");
4681
4682 return type;
4683 }
4684
4685 /* Return a KEYWORD_DECL built using the specified key_name, arg_type,
4686 arg_name and attributes. (TODO: Rename KEYWORD_DECL to
4687 OBJC_METHOD_PARM_DECL ?)
4688
4689 A KEYWORD_DECL is a tree representing the declaration of a
4690 parameter of an Objective-C method. It is produced when parsing a
4691 fragment of Objective-C method declaration of the form
4692
4693 keyworddecl:
4694 selector ':' '(' typename ')' identifier
4695
4696 For example, take the Objective-C method
4697
4698 -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type;
4699
4700 the two fragments "pathForResource:(NSString *)resource" and
4701 "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The
4702 KEYWORD_DECL stores the 'key_name' (eg, identifier for
4703 "pathForResource"), the 'arg_type' (eg, tree representing a
4704 NSString *), the 'arg_name' (eg identifier for "resource") and
4705 potentially some attributes (for example, a tree representing
4706 __attribute__ ((unused)) if such an attribute was attached to a
4707 certain parameter). You can access this information using the
4708 TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name),
4709 KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes).
4710
4711 'key_name' is an identifier node (and is optional as you can omit
4712 it in Objective-C methods).
4713 'arg_type' is a tree list (and is optional too if no parameter type
4714 was specified).
4715 'arg_name' is an identifier node and is required.
4716 'attributes' is an optional tree containing parameter attributes. */
4717 tree
4718 objc_build_keyword_decl (tree key_name, tree arg_type,
4719 tree arg_name, tree attributes)
4720 {
4721 tree keyword_decl;
4722
4723 if (flag_objc1_only && attributes)
4724 error_at (input_location, "method argument attributes are not available in Objective-C 1.0");
4725
4726 /* If no type is specified, default to "id". */
4727 arg_type = adjust_type_for_id_default (arg_type);
4728
4729 keyword_decl = make_node (KEYWORD_DECL);
4730
4731 TREE_TYPE (keyword_decl) = arg_type;
4732 KEYWORD_ARG_NAME (keyword_decl) = arg_name;
4733 KEYWORD_KEY_NAME (keyword_decl) = key_name;
4734 DECL_ATTRIBUTES (keyword_decl) = attributes;
4735
4736 return keyword_decl;
4737 }
4738
4739 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
4740 static tree
4741 build_keyword_selector (tree selector)
4742 {
4743 int len = 0;
4744 tree key_chain, key_name;
4745 char *buf;
4746
4747 /* Scan the selector to see how much space we'll need. */
4748 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
4749 {
4750 switch (TREE_CODE (selector))
4751 {
4752 case KEYWORD_DECL:
4753 key_name = KEYWORD_KEY_NAME (key_chain);
4754 break;
4755 case TREE_LIST:
4756 key_name = TREE_PURPOSE (key_chain);
4757 break;
4758 default:
4759 gcc_unreachable ();
4760 }
4761
4762 if (key_name)
4763 len += IDENTIFIER_LENGTH (key_name) + 1;
4764 else
4765 /* Just a ':' arg. */
4766 len++;
4767 }
4768
4769 buf = (char *) alloca (len + 1);
4770 /* Start the buffer out as an empty string. */
4771 buf[0] = '\0';
4772
4773 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
4774 {
4775 switch (TREE_CODE (selector))
4776 {
4777 case KEYWORD_DECL:
4778 key_name = KEYWORD_KEY_NAME (key_chain);
4779 break;
4780 case TREE_LIST:
4781 key_name = TREE_PURPOSE (key_chain);
4782 /* The keyword decl chain will later be used as a function
4783 argument chain. Unhook the selector itself so as to not
4784 confuse other parts of the compiler. */
4785 TREE_PURPOSE (key_chain) = NULL_TREE;
4786 break;
4787 default:
4788 gcc_unreachable ();
4789 }
4790
4791 if (key_name)
4792 strcat (buf, IDENTIFIER_POINTER (key_name));
4793 strcat (buf, ":");
4794 }
4795
4796 return get_identifier_with_length (buf, len);
4797 }
4798
4799 /* Used for declarations and definitions. */
4800
4801 static tree
4802 build_method_decl (enum tree_code code, tree ret_type, tree selector,
4803 tree add_args, bool ellipsis)
4804 {
4805 tree method_decl;
4806
4807 /* If no type is specified, default to "id". */
4808 ret_type = adjust_type_for_id_default (ret_type);
4809
4810 /* Note how a method_decl has a TREE_TYPE which is not the function
4811 type of the function implementing the method, but only the return
4812 type of the method. We may want to change this, and store the
4813 entire function type in there (eg, it may be used to simplify
4814 dealing with attributes below). */
4815 method_decl = make_node (code);
4816 TREE_TYPE (method_decl) = ret_type;
4817
4818 /* If we have a keyword selector, create an identifier_node that
4819 represents the full selector name (`:' included)... */
4820 if (TREE_CODE (selector) == KEYWORD_DECL)
4821 {
4822 METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
4823 METHOD_SEL_ARGS (method_decl) = selector;
4824 METHOD_ADD_ARGS (method_decl) = add_args;
4825 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis;
4826 }
4827 else
4828 {
4829 METHOD_SEL_NAME (method_decl) = selector;
4830 METHOD_SEL_ARGS (method_decl) = NULL_TREE;
4831 METHOD_ADD_ARGS (method_decl) = NULL_TREE;
4832 }
4833
4834 return method_decl;
4835 }
4836
4837 /* This routine processes objective-c method attributes. */
4838
4839 static void
4840 objc_decl_method_attributes (tree *node, tree attributes, int flags)
4841 {
4842 /* TODO: Replace the hackery below. An idea would be to store the
4843 full function type in the method declaration (for example in
4844 TREE_TYPE) and then expose ObjC method declarations to c-family
4845 and they could deal with them by simply treating them as
4846 functions. */
4847
4848 /* Because of the dangers in the hackery below, we filter out any
4849 attribute that we do not know about. For the ones we know about,
4850 we know that they work with the hackery. For the other ones,
4851 there is no guarantee, so we have to filter them out. */
4852 tree filtered_attributes = NULL_TREE;
4853
4854 if (attributes)
4855 {
4856 tree attribute;
4857 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
4858 {
4859 tree name = TREE_PURPOSE (attribute);
4860
4861 if (is_attribute_p ("deprecated", name)
4862 || is_attribute_p ("sentinel", name)
4863 || is_attribute_p ("noreturn", name))
4864 {
4865 /* An attribute that we support; add it to the filtered
4866 attributes. */
4867 filtered_attributes = chainon (filtered_attributes,
4868 copy_node (attribute));
4869 }
4870 else if (is_attribute_p ("format", name))
4871 {
4872 /* "format" is special because before adding it to the
4873 filtered attributes we need to adjust the specified
4874 format by adding the hidden function parameters for
4875 an Objective-C method (self, _cmd). */
4876 tree new_attribute = copy_node (attribute);
4877
4878 /* Check the arguments specified with the attribute, and
4879 modify them adding 2 for the two hidden arguments.
4880 Note how this differs from C++; according to the
4881 specs, C++ does not do it so you have to add the +1
4882 yourself. For Objective-C, instead, the compiler
4883 adds the +2 for you. */
4884
4885 /* The attribute arguments have not been checked yet, so
4886 we need to be careful as they could be missing or
4887 invalid. If anything looks wrong, we skip the
4888 process and the compiler will complain about it later
4889 when it validates the attribute. */
4890 /* Check that we have at least three arguments. */
4891 if (TREE_VALUE (new_attribute)
4892 && TREE_CHAIN (TREE_VALUE (new_attribute))
4893 && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (new_attribute))))
4894 {
4895 tree second_argument = TREE_CHAIN (TREE_VALUE (new_attribute));
4896 tree third_argument = TREE_CHAIN (second_argument);
4897 tree number;
4898
4899 /* This is the second argument, the "string-index",
4900 which specifies the index of the format string
4901 argument. Add 2. */
4902 number = TREE_VALUE (second_argument);
4903 if (number
4904 && TREE_CODE (number) == INTEGER_CST
4905 && wi::to_wide (number) != 0)
4906 TREE_VALUE (second_argument)
4907 = wide_int_to_tree (TREE_TYPE (number),
4908 wi::to_wide (number) + 2);
4909
4910 /* This is the third argument, the "first-to-check",
4911 which specifies the index of the first argument to
4912 check. This could be 0, meaning it is not available,
4913 in which case we don't need to add 2. Add 2 if not
4914 0. */
4915 number = TREE_VALUE (third_argument);
4916 if (number
4917 && TREE_CODE (number) == INTEGER_CST
4918 && wi::to_wide (number) != 0)
4919 TREE_VALUE (third_argument)
4920 = wide_int_to_tree (TREE_TYPE (number),
4921 wi::to_wide (number) + 2);
4922 }
4923 filtered_attributes = chainon (filtered_attributes,
4924 new_attribute);
4925 }
4926 else if (is_attribute_p ("nonnull", name))
4927 {
4928 /* We need to fixup all the argument indexes by adding 2
4929 for the two hidden arguments of an Objective-C method
4930 invocation, similat to what we do above for the
4931 "format" attribute. */
4932 /* FIXME: This works great in terms of implementing the
4933 functionality, but the warnings that are produced by
4934 nonnull do mention the argument index (while the
4935 format ones don't). For example, you could get
4936 "warning: null argument where non-null required
4937 (argument 3)". Now in that message, "argument 3"
4938 includes the 2 hidden arguments; it would be much
4939 more friendly to call it "argument 1", as that would
4940 be consistent with __attribute__ ((nonnnull (1))).
4941 To do this, we'd need to have the C family code that
4942 checks the arguments know about adding/removing 2 to
4943 the argument index ... or alternatively we could
4944 maybe store the "printable" argument index in
4945 addition to the actual argument index ? Some
4946 refactoring is needed to do this elegantly. */
4947 tree new_attribute = copy_node (attribute);
4948 tree argument = TREE_VALUE (attribute);
4949 while (argument != NULL_TREE)
4950 {
4951 /* Get the value of the argument and add 2. */
4952 tree number = TREE_VALUE (argument);
4953 if (number && TREE_CODE (number) == INTEGER_CST
4954 && wi::to_wide (number) != 0)
4955 TREE_VALUE (argument)
4956 = wide_int_to_tree (TREE_TYPE (number),
4957 wi::to_wide (number) + 2);
4958 argument = TREE_CHAIN (argument);
4959 }
4960
4961 filtered_attributes = chainon (filtered_attributes,
4962 new_attribute);
4963 }
4964 else
4965 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
4966 }
4967 }
4968
4969 if (filtered_attributes)
4970 {
4971 /* This hackery changes the TREE_TYPE of the ObjC method
4972 declaration to be a function type, so that decl_attributes
4973 will treat the ObjC method as if it was a function. Some
4974 attributes (sentinel, format) will be applied to the function
4975 type, changing it in place; so after calling decl_attributes,
4976 we extract the function type attributes and store them in
4977 METHOD_TYPE_ATTRIBUTES. Some other attributes (noreturn,
4978 deprecated) are applied directly to the method declaration
4979 (by setting TREE_DEPRECATED and TREE_THIS_VOLATILE) so there
4980 is nothing to do. */
4981 tree saved_type = TREE_TYPE (*node);
4982 TREE_TYPE (*node)
4983 = build_function_type_for_method (TREE_VALUE (saved_type), *node,
4984 METHOD_REF, 0);
4985 decl_attributes (node, filtered_attributes, flags);
4986 METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node));
4987 TREE_TYPE (*node) = saved_type;
4988 }
4989 }
4990
4991 bool
4992 objc_method_decl (enum tree_code opcode)
4993 {
4994 return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL;
4995 }
4996
4997 /* Return a function type for METHOD with RETURN_TYPE. CONTEXT is
4998 either METHOD_DEF or METHOD_REF, indicating whether we are defining a
4999 method or calling one. SUPER_FLAG indicates whether this is a send
5000 to super; this makes a difference for the NeXT calling sequence in
5001 which the lookup and the method call are done together. If METHOD is
5002 NULL, user-defined arguments (i.e., beyond self and _cmd) shall be
5003 represented as varargs. */
5004
5005 tree
5006 build_function_type_for_method (tree return_type, tree method,
5007 int context, bool super_flag)
5008 {
5009 vec<tree, va_gc> *argtypes = make_tree_vector ();
5010 tree t, ftype;
5011 bool is_varargs = false;
5012
5013 (*runtime.get_arg_type_list_base) (&argtypes, method, context, super_flag);
5014
5015 /* No actual method prototype given; remaining args passed as varargs. */
5016 if (method == NULL_TREE)
5017 {
5018 is_varargs = true;
5019 goto build_ftype;
5020 }
5021
5022 for (t = METHOD_SEL_ARGS (method); t; t = DECL_CHAIN (t))
5023 {
5024 tree arg_type = TREE_VALUE (TREE_TYPE (t));
5025
5026 /* Decay argument types for the underlying C function as
5027 appropriate. */
5028 arg_type = objc_decay_parm_type (arg_type);
5029
5030 vec_safe_push (argtypes, arg_type);
5031 }
5032
5033 if (METHOD_ADD_ARGS (method))
5034 {
5035 for (t = TREE_CHAIN (METHOD_ADD_ARGS (method));
5036 t; t = TREE_CHAIN (t))
5037 {
5038 tree arg_type = TREE_TYPE (TREE_VALUE (t));
5039
5040 arg_type = objc_decay_parm_type (arg_type);
5041
5042 vec_safe_push (argtypes, arg_type);
5043 }
5044
5045 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
5046 is_varargs = true;
5047 }
5048
5049 build_ftype:
5050 if (is_varargs)
5051 ftype = build_varargs_function_type_vec (return_type, argtypes);
5052 else
5053 ftype = build_function_type_vec (return_type, argtypes);
5054
5055 release_tree_vector (argtypes);
5056 return ftype;
5057 }
5058
5059 /* The 'method' argument is a tree; this tree could either be a single
5060 method, which is returned, or could be a TREE_VEC containing a list
5061 of methods. In that case, the first one is returned, and warnings
5062 are issued as appropriate. */
5063 static tree
5064 check_duplicates (tree method, int methods, int is_class)
5065 {
5066 tree first_method;
5067 size_t i;
5068
5069 if (method == NULL_TREE)
5070 return NULL_TREE;
5071
5072 if (TREE_CODE (method) != TREE_VEC)
5073 return method;
5074
5075 /* We have two or more methods with the same name but different
5076 types. */
5077 first_method = TREE_VEC_ELT (method, 0);
5078
5079 /* But just how different are those types? If
5080 -Wno-strict-selector-match is specified, we shall not complain if
5081 the differences are solely among types with identical size and
5082 alignment. */
5083 if (!warn_strict_selector_match)
5084 {
5085 for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++)
5086 if (!comp_proto_with_proto (first_method, TREE_VEC_ELT (method, i), 0))
5087 goto issue_warning;
5088
5089 return first_method;
5090 }
5091
5092 issue_warning:
5093 if (methods)
5094 {
5095 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5096
5097 warning_at (input_location, 0,
5098 "multiple methods named %<%c%E%> found",
5099 (is_class ? '+' : '-'),
5100 METHOD_SEL_NAME (first_method));
5101 inform (DECL_SOURCE_LOCATION (first_method), "using %<%c%s%>",
5102 (type ? '-' : '+'),
5103 identifier_to_locale (gen_method_decl (first_method)));
5104 }
5105 else
5106 {
5107 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5108
5109 warning_at (input_location, 0,
5110 "multiple selectors named %<%c%E%> found",
5111 (is_class ? '+' : '-'),
5112 METHOD_SEL_NAME (first_method));
5113 inform (DECL_SOURCE_LOCATION (first_method), "found %<%c%s%>",
5114 (type ? '-' : '+'),
5115 identifier_to_locale (gen_method_decl (first_method)));
5116 }
5117
5118 for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++)
5119 {
5120 bool type = TREE_CODE (TREE_VEC_ELT (method, i)) == INSTANCE_METHOD_DECL;
5121
5122 inform (DECL_SOURCE_LOCATION (TREE_VEC_ELT (method, i)), "also found %<%c%s%>",
5123 (type ? '-' : '+'),
5124 identifier_to_locale (gen_method_decl (TREE_VEC_ELT (method, i))));
5125 }
5126
5127 return first_method;
5128 }
5129
5130 /* If RECEIVER is a class reference, return the identifier node for
5131 the referenced class. RECEIVER is created by objc_get_class_reference,
5132 so we check the exact form created depending on which runtimes are
5133 used. */
5134
5135 static tree
5136 receiver_is_class_object (tree receiver, int self, int super)
5137 {
5138 tree exp, arg;
5139
5140 /* The receiver is 'self' or 'super' in the context of a class method. */
5141 if (objc_method_context
5142 && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
5143 && (self || super))
5144 return (super
5145 ? CLASS_SUPER_NAME (implementation_template)
5146 : CLASS_NAME (implementation_template));
5147
5148 /* The runtime might encapsulate things its own way. */
5149 exp = (*runtime.receiver_is_class_object) (receiver);
5150 if (exp)
5151 return exp;
5152
5153 /* The receiver is a function call that returns an id. Check if
5154 it is a call to objc_getClass, if so, pick up the class name.
5155
5156 This is required by the GNU runtime, which compiles
5157
5158 [NSObject alloc]
5159
5160 into
5161
5162 [objc_get_class ("NSObject") alloc];
5163
5164 and then, to check that the receiver responds to the +alloc
5165 method, needs to be able to determine that the objc_get_class()
5166 call returns the NSObject class and not just a generic Class
5167 pointer.
5168
5169 But, traditionally this is enabled for all runtimes, not just the
5170 GNU one, which means that the compiler is smarter than you'd
5171 expect when dealing with objc_getClass(). For example, with the
5172 Apple runtime, in the code
5173
5174 [objc_getClass ("NSObject") alloc];
5175
5176 the compiler will recognize the objc_getClass() call as special
5177 (due to the code below) and so will know that +alloc is called on
5178 the 'NSObject' class, and can perform the corresponding checks.
5179
5180 Programmers can disable this behavior by casting the results of
5181 objc_getClass() to 'Class' (this may seem weird because
5182 objc_getClass() is already declared to return 'Class', but the
5183 compiler treats it as a special function). This may be useful if
5184 the class is never declared, and the compiler would complain
5185 about a missing @interface for it. Then, you can do
5186
5187 [(Class)objc_getClass ("MyClassNeverDeclared") alloc];
5188
5189 to silence the warnings. */
5190 if (TREE_CODE (receiver) == CALL_EXPR
5191 && (exp = CALL_EXPR_FN (receiver))
5192 && TREE_CODE (exp) == ADDR_EXPR
5193 && (exp = TREE_OPERAND (exp, 0))
5194 && TREE_CODE (exp) == FUNCTION_DECL
5195 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
5196 prototypes for objc_get_class(). Thankfully, they seem to share the
5197 same function type. */
5198 && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl)
5199 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), runtime.tag_getclass)
5200 /* We have a call to objc_get_class/objc_getClass! */
5201 && (arg = CALL_EXPR_ARG (receiver, 0)))
5202 {
5203 STRIP_NOPS (arg);
5204 if (TREE_CODE (arg) == ADDR_EXPR
5205 && (arg = TREE_OPERAND (arg, 0))
5206 && TREE_CODE (arg) == STRING_CST)
5207 /* Finally, we have the class name. */
5208 return get_identifier (TREE_STRING_POINTER (arg));
5209 }
5210 return 0;
5211 }
5212
5213 /* If we are currently building a message expr, this holds
5214 the identifier of the selector of the message. This is
5215 used when printing warnings about argument mismatches. */
5216
5217 static tree current_objc_message_selector = 0;
5218
5219 tree
5220 objc_message_selector (void)
5221 {
5222 return current_objc_message_selector;
5223 }
5224
5225 /* Construct an expression for sending a message.
5226 MESS has the object to send to in TREE_PURPOSE
5227 and the argument list (including selector) in TREE_VALUE.
5228
5229 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
5230 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
5231
5232 tree
5233 objc_build_message_expr (tree receiver, tree message_args)
5234 {
5235 tree sel_name;
5236 #ifdef OBJCPLUS
5237 tree args = TREE_PURPOSE (message_args);
5238 #else
5239 tree args = message_args;
5240 #endif
5241 tree method_params = NULL_TREE;
5242
5243 if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK)
5244 return error_mark_node;
5245
5246 /* Obtain the full selector name. */
5247 switch (TREE_CODE (args))
5248 {
5249 case IDENTIFIER_NODE:
5250 /* A unary selector. */
5251 sel_name = args;
5252 break;
5253 case TREE_LIST:
5254 sel_name = build_keyword_selector (args);
5255 break;
5256 default:
5257 gcc_unreachable ();
5258 }
5259
5260 /* Build the parameter list to give to the method. */
5261 if (TREE_CODE (args) == TREE_LIST)
5262 #ifdef OBJCPLUS
5263 method_params = chainon (args, TREE_VALUE (message_args));
5264 #else
5265 {
5266 tree chain = args, prev = NULL_TREE;
5267
5268 /* We have a keyword selector--check for comma expressions. */
5269 while (chain)
5270 {
5271 tree element = TREE_VALUE (chain);
5272
5273 /* We have a comma expression, must collapse... */
5274 if (TREE_CODE (element) == TREE_LIST)
5275 {
5276 if (prev)
5277 TREE_CHAIN (prev) = element;
5278 else
5279 args = element;
5280 }
5281 prev = chain;
5282 chain = TREE_CHAIN (chain);
5283 }
5284 method_params = args;
5285 }
5286 #endif
5287
5288 #ifdef OBJCPLUS
5289 if (processing_template_decl)
5290 /* Must wait until template instantiation time. */
5291 return build_min_nt_loc (UNKNOWN_LOCATION, MESSAGE_SEND_EXPR, receiver,
5292 sel_name, method_params);
5293 #endif
5294
5295 return objc_finish_message_expr (receiver, sel_name, method_params, NULL);
5296 }
5297
5298 /* Look up method SEL_NAME that would be suitable for receiver
5299 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
5300 nonzero), and report on any duplicates. */
5301
5302 static tree
5303 lookup_method_in_hash_lists (tree sel_name, int is_class)
5304 {
5305 tree method_prototype = OBJC_MAP_NOT_FOUND;
5306
5307 if (!is_class)
5308 method_prototype = objc_map_get (instance_method_map, sel_name);
5309
5310 if (method_prototype == OBJC_MAP_NOT_FOUND)
5311 {
5312 method_prototype = objc_map_get (class_method_map, sel_name);
5313 is_class = 1;
5314
5315 if (method_prototype == OBJC_MAP_NOT_FOUND)
5316 return NULL_TREE;
5317 }
5318
5319 return check_duplicates (method_prototype, 1, is_class);
5320 }
5321
5322 /* The 'objc_finish_message_expr' routine is called from within
5323 'objc_build_message_expr' for non-template functions. In the case of
5324 C++ template functions, it is called from 'build_expr_from_tree'
5325 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded.
5326
5327 If the DEPRECATED_METHOD_PROTOTYPE argument is NULL, then we warn
5328 if the method being used is deprecated. If it is not NULL, instead
5329 of deprecating, we set *DEPRECATED_METHOD_PROTOTYPE to the method
5330 prototype that was used and is deprecated. This is useful for
5331 getter calls that are always generated when compiling dot-syntax
5332 expressions, even if they may not be used. In that case, we don't
5333 want the warning immediately; we produce it (if needed) at gimplify
5334 stage when we are sure that the deprecated getter is being
5335 used. */
5336 tree
5337 objc_finish_message_expr (tree receiver, tree sel_name, tree method_params,
5338 tree *deprecated_method_prototype)
5339 {
5340 tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype;
5341 tree retval, class_tree;
5342 int self, super, have_cast;
5343
5344 STRIP_ANY_LOCATION_WRAPPER (receiver);
5345
5346 /* We have used the receiver, so mark it as read. */
5347 mark_exp_read (receiver);
5348
5349 /* Extract the receiver of the message, as well as its type
5350 (where the latter may take the form of a cast or be inferred
5351 from the implementation context). */
5352 rtype = receiver;
5353 while (TREE_CODE (rtype) == COMPOUND_EXPR
5354 || TREE_CODE (rtype) == MODIFY_EXPR
5355 || CONVERT_EXPR_P (rtype)
5356 || TREE_CODE (rtype) == COMPONENT_REF)
5357 rtype = TREE_OPERAND (rtype, 0);
5358
5359 /* self is 1 if this is a message to self, 0 otherwise */
5360 self = (rtype == self_decl);
5361
5362 /* super is 1 if this is a message to super, 0 otherwise. */
5363 super = (rtype == UOBJC_SUPER_decl);
5364
5365 /* rtype is the type of the receiver. */
5366 rtype = TREE_TYPE (receiver);
5367
5368 /* have_cast is 1 if the receiver is casted. */
5369 have_cast = (TREE_CODE (receiver) == NOP_EXPR
5370 || (TREE_CODE (receiver) == COMPOUND_EXPR
5371 && !IS_SUPER (rtype)));
5372
5373 /* If we are calling [super dealloc], reset our warning flag. */
5374 if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name)))
5375 should_call_super_dealloc = 0;
5376
5377 /* If the receiver is a class object, retrieve the corresponding
5378 @interface, if one exists. class_tree is the class name
5379 identifier, or NULL_TREE if this is not a class method or the
5380 class name could not be determined (as in the case "Class c; [c
5381 method];"). */
5382 class_tree = receiver_is_class_object (receiver, self, super);
5383
5384 /* Now determine the receiver type (if an explicit cast has not been
5385 provided). */
5386 if (!have_cast)
5387 {
5388 if (class_tree)
5389 {
5390 /* We are here when we have no cast, and we have a class
5391 name. So, this is a plain method to a class object, as
5392 in [NSObject alloc]. Find the interface corresponding to
5393 the class name. */
5394 rtype = lookup_interface (class_tree);
5395
5396 if (rtype == NULL_TREE)
5397 {
5398 /* If 'rtype' is NULL_TREE at this point it means that
5399 we have seen no @interface corresponding to that
5400 class name, only a @class declaration (alternatively,
5401 this was a call such as [objc_getClass("SomeClass")
5402 alloc], where we've never seen the @interface of
5403 SomeClass). So, we have a class name (class_tree)
5404 but no actual details of the class methods. We won't
5405 be able to check that the class responds to the
5406 method, and we will have to guess the method
5407 prototype. Emit a warning, then keep going (this
5408 will use any method with a matching name, as if the
5409 receiver was of type 'Class'). */
5410 warning (0, "@interface of class %qE not found", class_tree);
5411 }
5412 }
5413 /* Handle `self' and `super'. */
5414 else if (super)
5415 {
5416 if (!CLASS_SUPER_NAME (implementation_template))
5417 {
5418 error ("no super class declared in @interface for %qE",
5419 CLASS_NAME (implementation_template));
5420 return error_mark_node;
5421 }
5422 rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template));
5423 }
5424 else if (self)
5425 rtype = lookup_interface (CLASS_NAME (implementation_template));
5426 }
5427
5428 if (objc_is_id (rtype))
5429 {
5430 /* The receiver is of type 'id' or 'Class' (with or without some
5431 protocols attached to it). */
5432
5433 /* We set class_tree to the identifier for 'Class' if this is a
5434 class method, and to NULL_TREE if not. */
5435 class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE);
5436
5437 /* 'rprotos' is the list of protocols that the receiver
5438 supports. */
5439 rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
5440 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
5441 : NULL_TREE);
5442
5443 /* We have no information on the type, and we set it to
5444 NULL_TREE. */
5445 rtype = NULL_TREE;
5446
5447 /* If there are any protocols, check that the method we are
5448 calling appears in the protocol list. If there are no
5449 protocols, this is a message to 'id' or 'Class' and we accept
5450 any method that exists. */
5451 if (rprotos)
5452 {
5453 /* If messaging 'id <Protos>' or 'Class <Proto>', first
5454 search in protocols themselves for the method
5455 prototype. */
5456 method_prototype
5457 = lookup_method_in_protocol_list (rprotos, sel_name,
5458 class_tree != NULL_TREE);
5459
5460 /* If messaging 'Class <Proto>' but did not find a class
5461 method prototype, search for an instance method instead,
5462 and warn about having done so. */
5463 if (!method_prototype && !rtype && class_tree != NULL_TREE)
5464 {
5465 method_prototype
5466 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5467
5468 if (method_prototype)
5469 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
5470 sel_name, sel_name);
5471 }
5472 }
5473 }
5474 else if (rtype)
5475 {
5476 /* We have a receiver type which is more specific than 'id' or
5477 'Class'. */
5478 tree orig_rtype = rtype;
5479
5480 if (TREE_CODE (rtype) == POINTER_TYPE)
5481 rtype = TREE_TYPE (rtype);
5482 /* Traverse typedef aliases */
5483 while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype)
5484 && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL
5485 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)))
5486 rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype));
5487 if (TYPED_OBJECT (rtype))
5488 {
5489 rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype);
5490 rtype = TYPE_OBJC_INTERFACE (rtype);
5491 }
5492 if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE)
5493 {
5494 /* If we could not find an @interface declaration, we must
5495 have only seen a @class declaration; so, we cannot say
5496 anything more intelligent about which methods the
5497 receiver will understand. Note that this only happens
5498 for instance methods; for class methods to a class where
5499 we have only seen a @class declaration,
5500 lookup_interface() above would have set rtype to
5501 NULL_TREE. */
5502 if (rprotos)
5503 {
5504 /* We could not find an @interface declaration, yet, if
5505 there are protocols attached to the type, we can
5506 still look up the method in the protocols. Ie, we
5507 are in the following case:
5508
5509 @class MyClass;
5510 MyClass<MyProtocol> *x;
5511 [x method];
5512
5513 If 'MyProtocol' has the method 'method', we can check
5514 and retrieve the method prototype. */
5515 method_prototype
5516 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5517
5518 /* At this point, if we have found the method_prototype,
5519 we are quite happy. The details of the class are
5520 irrelevant. If we haven't found it, a warning will
5521 have been produced that the method could not be found
5522 in the protocol, and we won't produce further
5523 warnings (please note that this means that "@class
5524 MyClass; MyClass <MyProtocol> *x;" is exactly
5525 equivalent to "id <MyProtocol> x", which isn't too
5526 satisfactory but it's not easy to see how to do
5527 better). */
5528 }
5529 else
5530 {
5531 if (rtype)
5532 {
5533 /* We could not find an @interface declaration, and
5534 there are no protocols attached to the receiver,
5535 so we can't complete the check that the receiver
5536 responds to the method, and we can't retrieve the
5537 method prototype. But, because the receiver has
5538 a well-specified class, the programmer did want
5539 this check to be performed. Emit a warning, then
5540 keep going as if it was an 'id'. To remove the
5541 warning, either include an @interface for the
5542 class, or cast the receiver to 'id'. Note that
5543 rtype is an IDENTIFIER_NODE at this point. */
5544 warning (0, "@interface of class %qE not found", rtype);
5545 }
5546 }
5547
5548 rtype = NULL_TREE;
5549 }
5550 else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE
5551 || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE)
5552 {
5553 /* We have a valid ObjC class name with an associated
5554 @interface. Look up the method name in the published
5555 @interface for the class (and its superclasses). */
5556 method_prototype
5557 = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE);
5558
5559 /* If the method was not found in the @interface, it may still
5560 exist locally as part of the @implementation. */
5561 if (!method_prototype && objc_implementation_context
5562 && CLASS_NAME (objc_implementation_context)
5563 == OBJC_TYPE_NAME (rtype))
5564 method_prototype
5565 = lookup_method
5566 ((class_tree
5567 ? CLASS_CLS_METHODS (objc_implementation_context)
5568 : CLASS_NST_METHODS (objc_implementation_context)),
5569 sel_name);
5570
5571 /* If we haven't found a candidate method by now, try looking for
5572 it in the protocol list. */
5573 if (!method_prototype && rprotos)
5574 method_prototype
5575 = lookup_method_in_protocol_list (rprotos, sel_name,
5576 class_tree != NULL_TREE);
5577 }
5578 else
5579 {
5580 /* We have a type, but it's not an Objective-C type (!). */
5581 warning (0, "invalid receiver type %qs",
5582 identifier_to_locale (gen_type_name (orig_rtype)));
5583 /* After issuing the "invalid receiver" warning, perform method
5584 lookup as if we were messaging 'id'. */
5585 rtype = rprotos = NULL_TREE;
5586 }
5587 }
5588 /* Note that rtype could also be NULL_TREE. This happens if we are
5589 messaging a class by name, but the class was only
5590 forward-declared using @class. */
5591
5592 /* For 'id' or 'Class' receivers, search in the global hash table as
5593 a last resort. For all receivers, warn if protocol searches have
5594 failed. */
5595 if (!method_prototype)
5596 {
5597 if (rprotos)
5598 warning (0, "%<%c%E%> not found in protocol(s)",
5599 (class_tree ? '+' : '-'),
5600 sel_name);
5601
5602 if (!rtype)
5603 method_prototype
5604 = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE);
5605 }
5606
5607 if (!method_prototype)
5608 {
5609 static bool warn_missing_methods = false;
5610
5611 if (rtype)
5612 warning (0, "%qE may not respond to %<%c%E%>",
5613 OBJC_TYPE_NAME (rtype),
5614 (class_tree ? '+' : '-'),
5615 sel_name);
5616 /* If we are messaging an 'id' or 'Class' object and made it here,
5617 then we have failed to find _any_ instance or class method,
5618 respectively. */
5619 else
5620 warning (0, "no %<%c%E%> method found",
5621 (class_tree ? '+' : '-'),
5622 sel_name);
5623
5624 if (!warn_missing_methods)
5625 {
5626 warning_at (input_location,
5627 0, "(Messages without a matching method signature");
5628 warning_at (input_location,
5629 0, "will be assumed to return %<id%> and accept");
5630 warning_at (input_location,
5631 0, "%<...%> as arguments.)");
5632 warn_missing_methods = true;
5633 }
5634 }
5635 else
5636 {
5637 /* Warn if the method is deprecated, but not if the receiver is
5638 a generic 'id'. 'id' is used to cast an object to a generic
5639 object of an unspecified class; in that case, we'll use
5640 whatever method prototype we can find to get the method
5641 argument and return types, but it is not appropriate to
5642 produce deprecation warnings since we don't know the class
5643 that the object will be of at runtime. The @interface(s) for
5644 that class may not even be available to the compiler right
5645 now, and it is perfectly possible that the method is marked
5646 as non-deprecated in such @interface(s).
5647
5648 In practice this makes sense since casting an object to 'id'
5649 is often used precisely to turn off warnings associated with
5650 the object being of a particular class. */
5651 if (TREE_DEPRECATED (method_prototype) && rtype != NULL_TREE)
5652 {
5653 if (deprecated_method_prototype)
5654 *deprecated_method_prototype = method_prototype;
5655 else
5656 warn_deprecated_use (method_prototype, NULL_TREE);
5657 }
5658 }
5659
5660 /* Save the selector name for printing error messages. */
5661 current_objc_message_selector = sel_name;
5662
5663 /* Build the method call.
5664 TODO: Get the location from somewhere that will work for delayed
5665 expansion. */
5666
5667 retval = (*runtime.build_objc_method_call) (input_location, method_prototype,
5668 receiver, rtype, sel_name,
5669 method_params, super);
5670
5671 current_objc_message_selector = 0;
5672
5673 return retval;
5674 }
5675 \f
5676
5677 /* This routine creates a static variable used to implement @protocol(MyProtocol)
5678 expression. This variable will be initialized to global protocol_t meta-data
5679 pointer. */
5680
5681 /* This function is called by the parser when (and only when) a
5682 @protocol() expression is found, in order to compile it. */
5683 tree
5684 objc_build_protocol_expr (tree protoname)
5685 {
5686 tree p = lookup_protocol (protoname, /* warn if deprecated */ true,
5687 /* definition_required */ false);
5688
5689 if (!p)
5690 {
5691 error ("cannot find protocol declaration for %qE", protoname);
5692 return error_mark_node;
5693 }
5694
5695 return (*runtime.get_protocol_reference) (input_location, p);
5696 }
5697
5698 /* This function is called by the parser when a @selector() expression
5699 is found, in order to compile it. It is only called by the parser
5700 and only to compile a @selector(). LOC is the location of the
5701 @selector. */
5702 tree
5703 objc_build_selector_expr (location_t loc, tree selnamelist)
5704 {
5705 tree selname;
5706
5707 /* Obtain the full selector name. */
5708 switch (TREE_CODE (selnamelist))
5709 {
5710 case IDENTIFIER_NODE:
5711 /* A unary selector. */
5712 selname = selnamelist;
5713 break;
5714 case TREE_LIST:
5715 selname = build_keyword_selector (selnamelist);
5716 break;
5717 default:
5718 gcc_unreachable ();
5719 }
5720
5721 /* If we are required to check @selector() expressions as they
5722 are found, check that the selector has been declared. */
5723 if (warn_undeclared_selector)
5724 {
5725 /* Look the selector up in the list of all known class and
5726 instance methods (up to this line) to check that the selector
5727 exists. */
5728 tree method;
5729
5730 /* First try with instance methods. */
5731 method = objc_map_get (instance_method_map, selname);
5732
5733 /* If not found, try with class methods. */
5734 if (method == OBJC_MAP_NOT_FOUND)
5735 {
5736 method = objc_map_get (class_method_map, selname);
5737
5738 /* If still not found, print out a warning. */
5739 if (method == OBJC_MAP_NOT_FOUND)
5740 warning (0, "undeclared selector %qE", selname);
5741 }
5742 }
5743
5744 /* The runtimes do this differently, most particularly, GNU has typed
5745 selectors, whilst NeXT does not. */
5746 return (*runtime.build_selector_reference) (loc, selname, NULL_TREE);
5747 }
5748
5749 static tree
5750 build_ivar_reference (tree id)
5751 {
5752 tree base;
5753 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
5754 {
5755 /* Historically, a class method that produced objects (factory
5756 method) would assign `self' to the instance that it
5757 allocated. This would effectively turn the class method into
5758 an instance method. Following this assignment, the instance
5759 variables could be accessed. That practice, while safe,
5760 violates the simple rule that a class method should not refer
5761 to an instance variable. It's better to catch the cases
5762 where this is done unknowingly than to support the above
5763 paradigm. */
5764 warning (0, "instance variable %qE accessed in class method",
5765 id);
5766 self_decl = convert (objc_instance_type, self_decl); /* cast */
5767 }
5768
5769 base = build_indirect_ref (input_location, self_decl, RO_ARROW);
5770 return (*runtime.build_ivar_reference) (input_location, base, id);
5771 }
5772
5773 static void
5774 hash_init (void)
5775 {
5776 instance_method_map = objc_map_alloc_ggc (1000);
5777 class_method_map = objc_map_alloc_ggc (1000);
5778
5779 class_name_map = objc_map_alloc_ggc (200);
5780 alias_name_map = objc_map_alloc_ggc (200);
5781
5782 /* Initialize the hash table used to hold the constant string objects. */
5783 string_htab = hash_table<objc_string_hasher>::create_ggc (31);
5784 }
5785
5786 /* Use the following to add a method to class_method_map or
5787 instance_method_map. It will add the method, keyed by the
5788 METHOD_SEL_NAME. If the method already exists, but with one or
5789 more different prototypes, it will store a TREE_VEC in the map,
5790 with the method prototypes in the vector. */
5791 static void
5792 insert_method_into_method_map (bool class_method, tree method)
5793 {
5794 tree method_name = METHOD_SEL_NAME (method);
5795 tree existing_entry;
5796 objc_map_t map;
5797
5798 if (class_method)
5799 map = class_method_map;
5800 else
5801 map = instance_method_map;
5802
5803 /* Check if the method already exists in the map. */
5804 existing_entry = objc_map_get (map, method_name);
5805
5806 /* If not, we simply add it to the map. */
5807 if (existing_entry == OBJC_MAP_NOT_FOUND)
5808 objc_map_put (map, method_name, method);
5809 else
5810 {
5811 tree new_entry;
5812
5813 /* If an entry already exists, it's more complicated. We'll
5814 have to check whether the method prototype is the same or
5815 not. */
5816 if (TREE_CODE (existing_entry) != TREE_VEC)
5817 {
5818 /* If the method prototypes are the same, there is nothing
5819 to do. */
5820 if (comp_proto_with_proto (method, existing_entry, 1))
5821 return;
5822
5823 /* If not, create a vector to store both the method already
5824 in the map, and the new one that we are adding. */
5825 new_entry = make_tree_vec (2);
5826
5827 TREE_VEC_ELT (new_entry, 0) = existing_entry;
5828 TREE_VEC_ELT (new_entry, 1) = method;
5829 }
5830 else
5831 {
5832 /* An entry already exists, and it's already a vector. This
5833 means that at least 2 different method prototypes were
5834 already found, and we're considering registering yet
5835 another one. */
5836 size_t i;
5837
5838 /* Check all the existing prototypes. If any matches the
5839 one we need to add, there is nothing to do because it's
5840 already there. */
5841 for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++)
5842 if (comp_proto_with_proto (method, TREE_VEC_ELT (existing_entry, i), 1))
5843 return;
5844
5845 /* Else, create a new, bigger vector and add the new method
5846 at the end of it. This is inefficient but extremely
5847 rare; in any sane program most methods have a single
5848 prototype, and very few, if any, will have more than
5849 2! */
5850 new_entry = make_tree_vec (TREE_VEC_LENGTH (existing_entry) + 1);
5851
5852 /* Copy the methods from the existing vector. */
5853 for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++)
5854 TREE_VEC_ELT (new_entry, i) = TREE_VEC_ELT (existing_entry, i);
5855
5856 /* Add the new method at the end. */
5857 TREE_VEC_ELT (new_entry, i) = method;
5858 }
5859
5860 /* Store the new vector in the map. */
5861 objc_map_put (map, method_name, new_entry);
5862 }
5863 }
5864
5865 \f
5866 static tree
5867 lookup_method (tree mchain, tree method)
5868 {
5869 tree key;
5870
5871 if (TREE_CODE (method) == IDENTIFIER_NODE)
5872 key = method;
5873 else
5874 key = METHOD_SEL_NAME (method);
5875
5876 while (mchain)
5877 {
5878 if (METHOD_SEL_NAME (mchain) == key)
5879 return mchain;
5880
5881 mchain = DECL_CHAIN (mchain);
5882 }
5883 return NULL_TREE;
5884 }
5885
5886 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance
5887 method in INTERFACE, along with any categories and protocols
5888 attached thereto. If method is not found, and the
5889 OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS, recursively examine the
5890 INTERFACE's superclass. If OBJC_LOOKUP_CLASS is set,
5891 OBJC_LOOKUP_NO_SUPER is clear, and no suitable class method could
5892 be found in INTERFACE or any of its superclasses, look for an
5893 _instance_ method of the same name in the root class as a last
5894 resort. This behavior can be turned off by using
5895 OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS.
5896
5897 If a suitable method cannot be found, return NULL_TREE. */
5898
5899 static tree
5900 lookup_method_static (tree interface, tree ident, int flags)
5901 {
5902 tree meth = NULL_TREE, root_inter = NULL_TREE;
5903 tree inter = interface;
5904 int is_class = (flags & OBJC_LOOKUP_CLASS);
5905 int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER);
5906 int no_instance_methods_of_root_class = (flags & OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS);
5907
5908 while (inter)
5909 {
5910 tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter);
5911 tree category = inter;
5912
5913 /* First, look up the method in the class itself. */
5914 if ((meth = lookup_method (chain, ident)))
5915 return meth;
5916
5917 /* Failing that, look for the method in each category of the class. */
5918 while ((category = CLASS_CATEGORY_LIST (category)))
5919 {
5920 chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category);
5921
5922 /* Check directly in each category. */
5923 if ((meth = lookup_method (chain, ident)))
5924 return meth;
5925
5926 /* Failing that, check in each category's protocols. */
5927 if (CLASS_PROTOCOL_LIST (category))
5928 {
5929 if ((meth = (lookup_method_in_protocol_list
5930 (CLASS_PROTOCOL_LIST (category), ident, is_class))))
5931 return meth;
5932 }
5933 }
5934
5935 /* If not found in categories, check in protocols of the main class. */
5936 if (CLASS_PROTOCOL_LIST (inter))
5937 {
5938 if ((meth = (lookup_method_in_protocol_list
5939 (CLASS_PROTOCOL_LIST (inter), ident, is_class))))
5940 return meth;
5941 }
5942
5943 /* If we were instructed not to look in superclasses, don't. */
5944 if (no_superclasses)
5945 return NULL_TREE;
5946
5947 /* Failing that, climb up the inheritance hierarchy. */
5948 root_inter = inter;
5949 inter = lookup_interface (CLASS_SUPER_NAME (inter));
5950 }
5951 while (inter);
5952
5953 if (is_class && !no_instance_methods_of_root_class)
5954 {
5955 /* If no class (factory) method was found, check if an _instance_
5956 method of the same name exists in the root class. This is what
5957 the Objective-C runtime will do. */
5958 return lookup_method_static (root_inter, ident, 0);
5959 }
5960 else
5961 {
5962 /* If an instance method was not found, return 0. */
5963 return NULL_TREE;
5964 }
5965 }
5966
5967 static tree
5968 objc_add_method (tree klass, tree method, int is_class, bool is_optional)
5969 {
5970 tree existing_method = NULL_TREE;
5971
5972 /* The first thing we do is look up the method in the list of
5973 methods already defined in the interface (or implementation). */
5974 if (is_class)
5975 existing_method = lookup_method (CLASS_CLS_METHODS (klass), method);
5976 else
5977 existing_method = lookup_method (CLASS_NST_METHODS (klass), method);
5978
5979 /* In the case of protocols, we have a second list of methods to
5980 consider, the list of optional ones. */
5981 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
5982 {
5983 /* @required methods are added to the protocol's normal list.
5984 @optional methods are added to the protocol's OPTIONAL lists.
5985 Note that adding the methods to the optional lists disables
5986 checking that the methods are implemented by classes
5987 implementing the protocol, since these checks only use the
5988 CLASS_CLS_METHODS and CLASS_NST_METHODS. */
5989
5990 /* First of all, if the method to add is @optional, and we found
5991 it already existing as @required, emit an error. */
5992 if (is_optional && existing_method)
5993 {
5994 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
5995 (is_class ? '+' : '-'),
5996 METHOD_SEL_NAME (existing_method));
5997 inform (DECL_SOURCE_LOCATION (existing_method),
5998 "previous declaration of %<%c%E%> as %<@required%>",
5999 (is_class ? '+' : '-'),
6000 METHOD_SEL_NAME (existing_method));
6001 }
6002
6003 /* Now check the list of @optional methods if we didn't find the
6004 method in the @required list. */
6005 if (!existing_method)
6006 {
6007 if (is_class)
6008 existing_method = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (klass), method);
6009 else
6010 existing_method = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (klass), method);
6011
6012 if (!is_optional && existing_method)
6013 {
6014 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
6015 (is_class ? '+' : '-'),
6016 METHOD_SEL_NAME (existing_method));
6017 inform (DECL_SOURCE_LOCATION (existing_method),
6018 "previous declaration of %<%c%E%> as %<@optional%>",
6019 (is_class ? '+' : '-'),
6020 METHOD_SEL_NAME (existing_method));
6021 }
6022 }
6023 }
6024
6025 /* If the method didn't exist already, add it. */
6026 if (!existing_method)
6027 {
6028 if (is_optional)
6029 {
6030 if (is_class)
6031 {
6032 /* Put the method on the list in reverse order. */
6033 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass);
6034 PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method;
6035 }
6036 else
6037 {
6038 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass);
6039 PROTOCOL_OPTIONAL_NST_METHODS (klass) = method;
6040 }
6041 }
6042 else
6043 {
6044 if (is_class)
6045 {
6046 DECL_CHAIN (method) = CLASS_CLS_METHODS (klass);
6047 CLASS_CLS_METHODS (klass) = method;
6048 }
6049 else
6050 {
6051 DECL_CHAIN (method) = CLASS_NST_METHODS (klass);
6052 CLASS_NST_METHODS (klass) = method;
6053 }
6054 }
6055 }
6056 else
6057 {
6058 /* The method was already defined. Check that the types match
6059 for an @interface for a class or category, or for a
6060 @protocol. Give hard errors on methods with identical
6061 selectors but differing argument and/or return types. We do
6062 not do this for @implementations, because C/C++ will do it
6063 for us (i.e., there will be duplicate function definition
6064 errors). */
6065 if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE
6066 || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6067 /* Starting with GCC 4.6, we emit the same error for
6068 protocols too. The situation is identical to
6069 @interfaces as there is no possible meaningful reason
6070 for defining the same method with different signatures
6071 in the very same @protocol. If that was allowed,
6072 whenever the protocol is used (both at compile and run
6073 time) there wouldn't be any meaningful way to decide
6074 which of the two method signatures should be used. */
6075 || TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
6076 && !comp_proto_with_proto (method, existing_method, 1))
6077 {
6078 error ("duplicate declaration of method %<%c%E%> with conflicting types",
6079 (is_class ? '+' : '-'),
6080 METHOD_SEL_NAME (existing_method));
6081 inform (DECL_SOURCE_LOCATION (existing_method),
6082 "previous declaration of %<%c%E%>",
6083 (is_class ? '+' : '-'),
6084 METHOD_SEL_NAME (existing_method));
6085 }
6086 }
6087
6088 if (is_class)
6089 insert_method_into_method_map (true, method);
6090 else
6091 {
6092 insert_method_into_method_map (false, method);
6093
6094 /* Instance methods in root classes (and categories thereof)
6095 may act as class methods as a last resort. We also add
6096 instance methods listed in @protocol declarations to
6097 the class hash table, on the assumption that @protocols
6098 may be adopted by root classes or categories. */
6099 if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6100 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
6101 klass = lookup_interface (CLASS_NAME (klass));
6102
6103 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE
6104 || !CLASS_SUPER_NAME (klass))
6105 insert_method_into_method_map (true, method);
6106 }
6107
6108 return method;
6109 }
6110
6111 static void
6112 add_category (tree klass, tree category)
6113 {
6114 /* Put categories on list in reverse order. */
6115 tree cat = lookup_category (klass, CLASS_SUPER_NAME (category));
6116
6117 if (cat)
6118 {
6119 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
6120 CLASS_NAME (klass),
6121 CLASS_SUPER_NAME (category));
6122 }
6123 else
6124 {
6125 CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass);
6126 CLASS_CATEGORY_LIST (klass) = category;
6127 }
6128 }
6129
6130 #ifndef OBJCPLUS
6131 /* A flexible array member is a C99 extension where you can use
6132 "type[]" at the end of a struct to mean a variable-length array.
6133
6134 In Objective-C, instance variables are fundamentally members of a
6135 struct, but the struct can always be extended by subclassing; hence
6136 we need to detect and forbid all instance variables declared using
6137 flexible array members.
6138
6139 No check for this is needed in Objective-C++, since C++ does not
6140 have flexible array members. */
6141
6142 /* Determine whether TYPE is a structure with a flexible array member,
6143 a union containing such a structure (possibly recursively) or an
6144 array of such structures or unions. These are all invalid as
6145 instance variable. */
6146 static bool
6147 flexible_array_type_p (tree type)
6148 {
6149 tree x;
6150 switch (TREE_CODE (type))
6151 {
6152 case RECORD_TYPE:
6153 x = TYPE_FIELDS (type);
6154 if (x == NULL_TREE)
6155 return false;
6156 while (DECL_CHAIN (x) != NULL_TREE)
6157 x = DECL_CHAIN (x);
6158 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6159 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6160 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6161 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6162 return true;
6163 return false;
6164 case UNION_TYPE:
6165 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
6166 {
6167 if (flexible_array_type_p (TREE_TYPE (x)))
6168 return true;
6169 }
6170 return false;
6171 /* Note that we also check for arrays of something that uses a flexible array member. */
6172 case ARRAY_TYPE:
6173 if (flexible_array_type_p (TREE_TYPE (type)))
6174 return true;
6175 return false;
6176 default:
6177 return false;
6178 }
6179 }
6180 #endif
6181
6182 /* Produce a printable version of an ivar name. This is only used
6183 inside add_instance_variable. */
6184 static const char *
6185 printable_ivar_name (tree field_decl)
6186 {
6187 if (DECL_NAME (field_decl))
6188 return identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl)));
6189 else
6190 return _("<unnamed>");
6191 }
6192
6193 /* Called after parsing each instance variable declaration. Necessary to
6194 preserve typedefs and implement public/private...
6195
6196 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
6197
6198 static tree
6199 add_instance_variable (tree klass, objc_ivar_visibility_kind visibility,
6200 tree field_decl)
6201 {
6202 tree field_type = TREE_TYPE (field_decl);
6203
6204 #ifdef OBJCPLUS
6205 if (TREE_CODE (field_type) == REFERENCE_TYPE)
6206 {
6207 error ("illegal reference type specified for instance variable %qs",
6208 printable_ivar_name (field_decl));
6209 /* Return class as is without adding this ivar. */
6210 return klass;
6211 }
6212 #endif
6213
6214 if (field_type == error_mark_node || !TYPE_SIZE (field_type)
6215 || TYPE_SIZE (field_type) == error_mark_node)
6216 /* 'type[0]' is allowed, but 'type[]' is not! */
6217 {
6218 error ("instance variable %qs has unknown size",
6219 printable_ivar_name (field_decl));
6220 /* Return class as is without adding this ivar. */
6221 return klass;
6222 }
6223
6224 #ifndef OBJCPLUS
6225 /* Also, in C reject a struct with a flexible array member. Ie,
6226
6227 struct A { int x; int[] y; };
6228
6229 @interface X
6230 {
6231 struct A instance_variable;
6232 }
6233 @end
6234
6235 is not valid because if the class is subclassed, we wouldn't be able
6236 to calculate the offset of the next instance variable. */
6237 if (flexible_array_type_p (field_type))
6238 {
6239 error ("instance variable %qs uses flexible array member",
6240 printable_ivar_name (field_decl));
6241 /* Return class as is without adding this ivar. */
6242 return klass;
6243 }
6244 #endif
6245
6246 #ifdef OBJCPLUS
6247 /* Check if the ivar being added has a non-POD C++ type. If so, we will
6248 need to either (1) warn the user about it or (2) generate suitable
6249 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
6250 methods (if '-fobjc-call-cxx-cdtors' was specified). */
6251 if (MAYBE_CLASS_TYPE_P (field_type)
6252 && (TYPE_NEEDS_CONSTRUCTING (field_type)
6253 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)
6254 || TYPE_POLYMORPHIC_P (field_type)))
6255 {
6256 tree type_name = OBJC_TYPE_NAME (field_type);
6257
6258 if (flag_objc_call_cxx_cdtors)
6259 {
6260 /* Since the ObjC runtime will be calling the constructors and
6261 destructors for us, the only thing we can't handle is the lack
6262 of a default constructor. */
6263 if (TYPE_NEEDS_CONSTRUCTING (field_type)
6264 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type))
6265 {
6266 warning (0, "type %qE has no default constructor to call",
6267 type_name);
6268
6269 /* If we cannot call a constructor, we should also avoid
6270 calling the destructor, for symmetry. */
6271 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6272 warning (0, "destructor for %qE shall not be run either",
6273 type_name);
6274 }
6275 }
6276 else
6277 {
6278 static bool warn_cxx_ivars = false;
6279
6280 if (TYPE_POLYMORPHIC_P (field_type))
6281 {
6282 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
6283 initialize them. */
6284 error ("type %qE has virtual member functions", type_name);
6285 error ("illegal aggregate type %qE specified "
6286 "for instance variable %qs",
6287 type_name, printable_ivar_name (field_decl));
6288 /* Return class as is without adding this ivar. */
6289 return klass;
6290 }
6291
6292 /* User-defined constructors and destructors are not known to Obj-C
6293 and hence will not be called. This may or may not be a problem. */
6294 if (TYPE_NEEDS_CONSTRUCTING (field_type))
6295 warning (0, "type %qE has a user-defined constructor", type_name);
6296 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6297 warning (0, "type %qE has a user-defined destructor", type_name);
6298
6299 if (!warn_cxx_ivars)
6300 {
6301 warning (0, "C++ constructors and destructors will not "
6302 "be invoked for Objective-C fields");
6303 warn_cxx_ivars = true;
6304 }
6305 }
6306 }
6307 #endif
6308
6309 /* Overload the public attribute, it is not used for FIELD_DECLs. */
6310 switch (visibility)
6311 {
6312 case OBJC_IVAR_VIS_PROTECTED:
6313 TREE_PUBLIC (field_decl) = 0;
6314 TREE_PRIVATE (field_decl) = 0;
6315 TREE_PROTECTED (field_decl) = 1;
6316 break;
6317
6318 case OBJC_IVAR_VIS_PACKAGE:
6319 /* TODO: Implement the package variant. */
6320 case OBJC_IVAR_VIS_PUBLIC:
6321 TREE_PUBLIC (field_decl) = 1;
6322 TREE_PRIVATE (field_decl) = 0;
6323 TREE_PROTECTED (field_decl) = 0;
6324 break;
6325
6326 case OBJC_IVAR_VIS_PRIVATE:
6327 TREE_PUBLIC (field_decl) = 0;
6328 TREE_PRIVATE (field_decl) = 1;
6329 TREE_PROTECTED (field_decl) = 0;
6330 break;
6331
6332 }
6333
6334 CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl);
6335
6336 return klass;
6337 }
6338
6339 /* True if the ivar is private and we are not in its implementation. */
6340
6341 static int
6342 is_private (tree decl)
6343 {
6344 return (TREE_PRIVATE (decl)
6345 && ! is_ivar (CLASS_IVARS (implementation_template),
6346 DECL_NAME (decl)));
6347 }
6348
6349 /* Searches all the instance variables of 'klass' and of its
6350 superclasses for an instance variable whose name (identifier) is
6351 'ivar_name_ident'. Return the declaration (DECL) of the instance
6352 variable, if found, or NULL_TREE, if not found. */
6353 static inline tree
6354 ivar_of_class (tree klass, tree ivar_name_ident)
6355 {
6356 /* First, look up the ivar in CLASS_RAW_IVARS. */
6357 tree decl_chain = CLASS_RAW_IVARS (klass);
6358
6359 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6360 if (DECL_NAME (decl_chain) == ivar_name_ident)
6361 return decl_chain;
6362
6363 /* If not found, search up the class hierarchy. */
6364 while (CLASS_SUPER_NAME (klass))
6365 {
6366 klass = lookup_interface (CLASS_SUPER_NAME (klass));
6367
6368 decl_chain = CLASS_RAW_IVARS (klass);
6369
6370 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6371 if (DECL_NAME (decl_chain) == ivar_name_ident)
6372 return decl_chain;
6373 }
6374
6375 return NULL_TREE;
6376 }
6377
6378 /* We have an instance variable reference;, check to see if it is public. */
6379
6380 int
6381 objc_is_public (tree expr, tree identifier)
6382 {
6383 tree basetype, decl;
6384
6385 #ifdef OBJCPLUS
6386 if (processing_template_decl)
6387 return 1;
6388 #endif
6389
6390 if (TREE_TYPE (expr) == error_mark_node)
6391 return 1;
6392
6393 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
6394
6395 if (basetype && TREE_CODE (basetype) == RECORD_TYPE)
6396 {
6397 if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype))
6398 {
6399 tree klass = lookup_interface (OBJC_TYPE_NAME (basetype));
6400
6401 if (!klass)
6402 {
6403 error ("cannot find interface declaration for %qE",
6404 OBJC_TYPE_NAME (basetype));
6405 return 0;
6406 }
6407
6408 if ((decl = ivar_of_class (klass, identifier)))
6409 {
6410 if (TREE_PUBLIC (decl))
6411 return 1;
6412
6413 /* Important difference between the Stepstone translator:
6414 all instance variables should be public within the context
6415 of the implementation. */
6416 if (objc_implementation_context
6417 && ((TREE_CODE (objc_implementation_context)
6418 == CLASS_IMPLEMENTATION_TYPE)
6419 || (TREE_CODE (objc_implementation_context)
6420 == CATEGORY_IMPLEMENTATION_TYPE)))
6421 {
6422 tree curtype = TYPE_MAIN_VARIANT
6423 (CLASS_STATIC_TEMPLATE
6424 (implementation_template));
6425
6426 if (basetype == curtype
6427 || DERIVED_FROM_P (basetype, curtype))
6428 {
6429 int priv = is_private (decl);
6430
6431 if (priv)
6432 error ("instance variable %qE is declared private",
6433 DECL_NAME (decl));
6434
6435 return !priv;
6436 }
6437 }
6438
6439 /* The 2.95.2 compiler sometimes allowed C functions to access
6440 non-@public ivars. We will let this slide for now... */
6441 if (!objc_method_context)
6442 {
6443 warning (0, "instance variable %qE is %s; "
6444 "this will be a hard error in the future",
6445 identifier,
6446 TREE_PRIVATE (decl) ? "@private" : "@protected");
6447 return 1;
6448 }
6449
6450 error ("instance variable %qE is declared %s",
6451 identifier,
6452 TREE_PRIVATE (decl) ? "private" : "protected");
6453 return 0;
6454 }
6455 }
6456 }
6457
6458 return 1;
6459 }
6460 \f
6461 /* Make sure all methods in CHAIN (a list of method declarations from
6462 an @interface or a @protocol) are in IMPLEMENTATION (the
6463 implementation context). This is used to check for example that
6464 all methods declared in an @interface were implemented in an
6465 @implementation.
6466
6467 Some special methods (property setters/getters) are special and if
6468 they are not found in IMPLEMENTATION, we look them up in its
6469 superclasses. */
6470
6471 static int
6472 check_methods (tree chain, tree implementation, int mtype)
6473 {
6474 int first = 1;
6475 tree list;
6476
6477 if (mtype == (int)'+')
6478 list = CLASS_CLS_METHODS (implementation);
6479 else
6480 list = CLASS_NST_METHODS (implementation);
6481
6482 while (chain)
6483 {
6484 /* If the method is associated with a dynamic property, then it
6485 is Ok not to have the method implementation, as it will be
6486 generated dynamically at runtime. To decide if the method is
6487 associated with a @dynamic property, we search the list of
6488 @synthesize and @dynamic for this implementation, and look
6489 for any @dynamic property with the same setter or getter name
6490 as this method. */
6491 tree x;
6492 for (x = IMPL_PROPERTY_DECL (implementation); x; x = TREE_CHAIN (x))
6493 if (PROPERTY_DYNAMIC (x)
6494 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6495 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6496 break;
6497
6498 if (x != NULL_TREE)
6499 {
6500 chain = TREE_CHAIN (chain); /* next method... */
6501 continue;
6502 }
6503
6504 if (!lookup_method (list, chain))
6505 {
6506 /* If the method is a property setter/getter, we'll still
6507 allow it to be missing if it is implemented by
6508 'interface' or any of its superclasses. */
6509 tree property = METHOD_PROPERTY_CONTEXT (chain);
6510 if (property)
6511 {
6512 /* Note that since this is a property getter/setter, it
6513 is obviously an instance method. */
6514 tree interface = NULL_TREE;
6515
6516 /* For a category, first check the main class
6517 @interface. */
6518 if (TREE_CODE (implementation) == CATEGORY_IMPLEMENTATION_TYPE)
6519 {
6520 interface = lookup_interface (CLASS_NAME (implementation));
6521
6522 /* If the method is found in the main class, it's Ok. */
6523 if (lookup_method (CLASS_NST_METHODS (interface), chain))
6524 {
6525 chain = DECL_CHAIN (chain);
6526 continue;
6527 }
6528
6529 /* Else, get the superclass. */
6530 if (CLASS_SUPER_NAME (interface))
6531 interface = lookup_interface (CLASS_SUPER_NAME (interface));
6532 else
6533 interface = NULL_TREE;
6534 }
6535
6536 /* Get the superclass for classes. */
6537 if (TREE_CODE (implementation) == CLASS_IMPLEMENTATION_TYPE)
6538 {
6539 if (CLASS_SUPER_NAME (implementation))
6540 interface = lookup_interface (CLASS_SUPER_NAME (implementation));
6541 else
6542 interface = NULL_TREE;
6543 }
6544
6545 /* Now, interface is the superclass, if any; go check it. */
6546 if (interface)
6547 {
6548 if (lookup_method_static (interface, chain, 0))
6549 {
6550 chain = DECL_CHAIN (chain);
6551 continue;
6552 }
6553 }
6554 /* Else, fall through - warn. */
6555 }
6556 if (first)
6557 {
6558 switch (TREE_CODE (implementation))
6559 {
6560 case CLASS_IMPLEMENTATION_TYPE:
6561 warning (0, "incomplete implementation of class %qE",
6562 CLASS_NAME (implementation));
6563 break;
6564 case CATEGORY_IMPLEMENTATION_TYPE:
6565 warning (0, "incomplete implementation of category %qE",
6566 CLASS_SUPER_NAME (implementation));
6567 break;
6568 default:
6569 gcc_unreachable ();
6570 }
6571 first = 0;
6572 }
6573
6574 warning (0, "method definition for %<%c%E%> not found",
6575 mtype, METHOD_SEL_NAME (chain));
6576 }
6577
6578 chain = DECL_CHAIN (chain);
6579 }
6580
6581 return first;
6582 }
6583
6584 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
6585
6586 static int
6587 conforms_to_protocol (tree klass, tree protocol)
6588 {
6589 if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE)
6590 {
6591 tree p = CLASS_PROTOCOL_LIST (klass);
6592 while (p && TREE_VALUE (p) != protocol)
6593 p = TREE_CHAIN (p);
6594
6595 if (!p)
6596 {
6597 tree super = (CLASS_SUPER_NAME (klass)
6598 ? lookup_interface (CLASS_SUPER_NAME (klass))
6599 : NULL_TREE);
6600 int tmp = super ? conforms_to_protocol (super, protocol) : 0;
6601 if (!tmp)
6602 return 0;
6603 }
6604 }
6605
6606 return 1;
6607 }
6608
6609 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
6610 CONTEXT. This is one of two mechanisms to check protocol integrity. */
6611
6612 static int
6613 check_methods_accessible (tree chain, tree context, int mtype)
6614 {
6615 int first = 1;
6616 tree list;
6617 tree base_context = context;
6618
6619 while (chain)
6620 {
6621 /* If the method is associated with a dynamic property, then it
6622 is Ok not to have the method implementation, as it will be
6623 generated dynamically at runtime. Search for any @dynamic
6624 property with the same setter or getter name as this
6625 method. TODO: Use a hashtable lookup. */
6626 tree x;
6627 for (x = IMPL_PROPERTY_DECL (base_context); x; x = TREE_CHAIN (x))
6628 if (PROPERTY_DYNAMIC (x)
6629 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6630 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6631 break;
6632
6633 if (x != NULL_TREE)
6634 {
6635 chain = TREE_CHAIN (chain); /* next method... */
6636 continue;
6637 }
6638
6639 context = base_context;
6640 while (context)
6641 {
6642 if (mtype == '+')
6643 list = CLASS_CLS_METHODS (context);
6644 else
6645 list = CLASS_NST_METHODS (context);
6646
6647 if (lookup_method (list, chain))
6648 break;
6649
6650 switch (TREE_CODE (context))
6651 {
6652 case CLASS_IMPLEMENTATION_TYPE:
6653 case CLASS_INTERFACE_TYPE:
6654 context = (CLASS_SUPER_NAME (context)
6655 ? lookup_interface (CLASS_SUPER_NAME (context))
6656 : NULL_TREE);
6657 break;
6658 case CATEGORY_IMPLEMENTATION_TYPE:
6659 case CATEGORY_INTERFACE_TYPE:
6660 context = (CLASS_NAME (context)
6661 ? lookup_interface (CLASS_NAME (context))
6662 : NULL_TREE);
6663 break;
6664 default:
6665 gcc_unreachable ();
6666 }
6667 }
6668
6669 if (context == NULL_TREE)
6670 {
6671 if (first)
6672 {
6673 switch (TREE_CODE (objc_implementation_context))
6674 {
6675 case CLASS_IMPLEMENTATION_TYPE:
6676 warning (0, "incomplete implementation of class %qE",
6677 CLASS_NAME (objc_implementation_context));
6678 break;
6679 case CATEGORY_IMPLEMENTATION_TYPE:
6680 warning (0, "incomplete implementation of category %qE",
6681 CLASS_SUPER_NAME (objc_implementation_context));
6682 break;
6683 default:
6684 gcc_unreachable ();
6685 }
6686 first = 0;
6687 }
6688 warning (0, "method definition for %<%c%E%> not found",
6689 mtype, METHOD_SEL_NAME (chain));
6690 }
6691
6692 chain = TREE_CHAIN (chain); /* next method... */
6693 }
6694 return first;
6695 }
6696
6697 /* Check whether the current interface (accessible via
6698 'objc_implementation_context') actually implements protocol P, along
6699 with any protocols that P inherits. */
6700
6701 static void
6702 check_protocol (tree p, const char *type, tree name)
6703 {
6704 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
6705 {
6706 int f1, f2;
6707
6708 /* Ensure that all protocols have bodies! */
6709 if (warn_protocol)
6710 {
6711 f1 = check_methods (PROTOCOL_CLS_METHODS (p),
6712 objc_implementation_context,
6713 '+');
6714 f2 = check_methods (PROTOCOL_NST_METHODS (p),
6715 objc_implementation_context,
6716 '-');
6717 }
6718 else
6719 {
6720 f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
6721 objc_implementation_context,
6722 '+');
6723 f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
6724 objc_implementation_context,
6725 '-');
6726 }
6727
6728 if (!f1 || !f2)
6729 warning (0, "%s %qE does not fully implement the %qE protocol",
6730 type, name, PROTOCOL_NAME (p));
6731 }
6732
6733 /* Check protocols recursively. */
6734 if (PROTOCOL_LIST (p))
6735 {
6736 tree subs = PROTOCOL_LIST (p);
6737 tree super_class =
6738 lookup_interface (CLASS_SUPER_NAME (implementation_template));
6739
6740 while (subs)
6741 {
6742 tree sub = TREE_VALUE (subs);
6743
6744 /* If the superclass does not conform to the protocols
6745 inherited by P, then we must! */
6746 if (!super_class || !conforms_to_protocol (super_class, sub))
6747 check_protocol (sub, type, name);
6748 subs = TREE_CHAIN (subs);
6749 }
6750 }
6751 }
6752
6753 /* Check whether the current interface (accessible via
6754 'objc_implementation_context') actually implements the protocols listed
6755 in PROTO_LIST. */
6756
6757 static void
6758 check_protocols (tree proto_list, const char *type, tree name)
6759 {
6760 for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
6761 {
6762 tree p = TREE_VALUE (proto_list);
6763
6764 check_protocol (p, type, name);
6765 }
6766 }
6767 \f
6768 /* Make sure that the class CLASS_NAME is defined CODE says which kind
6769 of thing CLASS_NAME ought to be. It can be CLASS_INTERFACE_TYPE,
6770 CLASS_IMPLEMENTATION_TYPE, CATEGORY_INTERFACE_TYPE, or
6771 CATEGORY_IMPLEMENTATION_TYPE. For a CATEGORY_INTERFACE_TYPE,
6772 SUPER_NAME is the name of the category. For a class extension,
6773 CODE is CATEGORY_INTERFACE_TYPE and SUPER_NAME is NULL_TREE. */
6774 static tree
6775 start_class (enum tree_code code, tree class_name, tree super_name,
6776 tree protocol_list, tree attributes)
6777 {
6778 tree klass = NULL_TREE;
6779 tree decl;
6780
6781 #ifdef OBJCPLUS
6782 if (current_namespace != global_namespace)
6783 {
6784 error ("Objective-C declarations may only appear in global scope");
6785 }
6786 #endif /* OBJCPLUS */
6787
6788 if (objc_implementation_context)
6789 {
6790 warning (0, "%<@end%> missing in implementation context");
6791 finish_class (objc_implementation_context);
6792 objc_ivar_chain = NULL_TREE;
6793 objc_implementation_context = NULL_TREE;
6794 }
6795
6796 /* If this is a class extension, we'll be "reopening" the existing
6797 CLASS_INTERFACE_TYPE, so in that case there is no need to create
6798 a new node. */
6799 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6800 {
6801 klass = make_node (code);
6802 TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS);
6803 }
6804
6805 /* Check for existence of the super class, if one was specified. Note
6806 that we must have seen an @interface, not just a @class. If we
6807 are looking at a @compatibility_alias, traverse it first. */
6808 if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE)
6809 && super_name)
6810 {
6811 tree super = objc_is_class_name (super_name);
6812 tree super_interface = NULL_TREE;
6813
6814 if (super)
6815 super_interface = lookup_interface (super);
6816
6817 if (!super_interface)
6818 {
6819 error ("cannot find interface declaration for %qE, superclass of %qE",
6820 super ? super : super_name,
6821 class_name);
6822 super_name = NULL_TREE;
6823 }
6824 else
6825 {
6826 if (TREE_DEPRECATED (super_interface))
6827 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
6828 super);
6829 super_name = super;
6830 }
6831 }
6832
6833 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6834 {
6835 CLASS_NAME (klass) = class_name;
6836 CLASS_SUPER_NAME (klass) = super_name;
6837 CLASS_CLS_METHODS (klass) = NULL_TREE;
6838 }
6839
6840 if (! objc_is_class_name (class_name)
6841 && (decl = lookup_name (class_name)))
6842 {
6843 error ("%qE redeclared as different kind of symbol",
6844 class_name);
6845 error ("previous declaration of %q+D",
6846 decl);
6847 }
6848
6849 switch (code)
6850 {
6851 case CLASS_IMPLEMENTATION_TYPE:
6852 {
6853 tree chain;
6854
6855 for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
6856 if (TREE_VALUE (chain) == class_name)
6857 {
6858 error ("reimplementation of class %qE",
6859 class_name);
6860 /* TODO: error message saying where it was previously
6861 implemented. */
6862 break;
6863 }
6864 if (chain == NULL_TREE)
6865 implemented_classes = tree_cons (NULL_TREE, class_name,
6866 implemented_classes);
6867 }
6868
6869 /* Reset for multiple classes per file. */
6870 method_slot = 0;
6871
6872 objc_implementation_context = klass;
6873
6874 /* Lookup the interface for this implementation. */
6875
6876 if (!(implementation_template = lookup_interface (class_name)))
6877 {
6878 warning (0, "cannot find interface declaration for %qE",
6879 class_name);
6880 add_interface (implementation_template = objc_implementation_context,
6881 class_name);
6882 }
6883
6884 /* If a super class has been specified in the implementation,
6885 insure it conforms to the one specified in the interface. */
6886
6887 if (super_name
6888 && (super_name != CLASS_SUPER_NAME (implementation_template)))
6889 {
6890 tree previous_name = CLASS_SUPER_NAME (implementation_template);
6891 error ("conflicting super class name %qE",
6892 super_name);
6893 if (previous_name)
6894 error ("previous declaration of %qE", previous_name);
6895 else
6896 error ("previous declaration");
6897 }
6898
6899 else if (! super_name)
6900 {
6901 CLASS_SUPER_NAME (objc_implementation_context)
6902 = CLASS_SUPER_NAME (implementation_template);
6903 }
6904 break;
6905
6906 case CLASS_INTERFACE_TYPE:
6907 if (lookup_interface (class_name))
6908 #ifdef OBJCPLUS
6909 error ("duplicate interface declaration for class %qE", class_name);
6910 #else
6911 warning (0, "duplicate interface declaration for class %qE", class_name);
6912 #endif
6913 else
6914 add_interface (klass, class_name);
6915
6916 if (protocol_list)
6917 CLASS_PROTOCOL_LIST (klass)
6918 = lookup_and_install_protocols (protocol_list, /* definition_required */ true);
6919
6920 if (attributes)
6921 {
6922 tree attribute;
6923 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
6924 {
6925 tree name = TREE_PURPOSE (attribute);
6926
6927 /* TODO: Document what the objc_exception attribute is/does. */
6928 /* We handle the 'deprecated' and (undocumented) 'objc_exception'
6929 attributes. */
6930 if (is_attribute_p ("deprecated", name))
6931 TREE_DEPRECATED (klass) = 1;
6932 else if (is_attribute_p ("objc_exception", name))
6933 CLASS_HAS_EXCEPTION_ATTR (klass) = 1;
6934 else
6935 /* Warn about and ignore all others for now, but store them. */
6936 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
6937 }
6938 TYPE_ATTRIBUTES (klass) = attributes;
6939 }
6940 break;
6941
6942 case CATEGORY_INTERFACE_TYPE:
6943 {
6944 tree class_category_is_assoc_with;
6945
6946 /* For a category, class_name is really the name of the class that
6947 the following set of methods will be associated with. We must
6948 find the interface so that can derive the objects template. */
6949 if (!(class_category_is_assoc_with = lookup_interface (class_name)))
6950 {
6951 error ("cannot find interface declaration for %qE",
6952 class_name);
6953 exit (FATAL_EXIT_CODE);
6954 }
6955 else
6956 {
6957 if (TREE_DEPRECATED (class_category_is_assoc_with))
6958 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
6959 class_name);
6960
6961 if (super_name == NULL_TREE)
6962 {
6963 /* This is a class extension. Get the original
6964 interface, and continue working on it. */
6965 objc_in_class_extension = true;
6966 klass = class_category_is_assoc_with;
6967
6968 if (protocol_list)
6969 {
6970 /* Append protocols to the original protocol
6971 list. */
6972 CLASS_PROTOCOL_LIST (klass)
6973 = chainon (CLASS_PROTOCOL_LIST (klass),
6974 lookup_and_install_protocols
6975 (protocol_list,
6976 /* definition_required */ true));
6977 }
6978 }
6979 else
6980 {
6981 add_category (class_category_is_assoc_with, klass);
6982
6983 if (protocol_list)
6984 CLASS_PROTOCOL_LIST (klass)
6985 = lookup_and_install_protocols
6986 (protocol_list, /* definition_required */ true);
6987 }
6988 }
6989 }
6990 break;
6991
6992 case CATEGORY_IMPLEMENTATION_TYPE:
6993 /* Reset for multiple classes per file. */
6994 method_slot = 0;
6995
6996 objc_implementation_context = klass;
6997
6998 /* For a category, class_name is really the name of the class that
6999 the following set of methods will be associated with. We must
7000 find the interface so that can derive the objects template. */
7001
7002 if (!(implementation_template = lookup_interface (class_name)))
7003 {
7004 error ("cannot find interface declaration for %qE",
7005 class_name);
7006 exit (FATAL_EXIT_CODE);
7007 }
7008 break;
7009 default:
7010 gcc_unreachable ();
7011 }
7012 return klass;
7013 }
7014
7015 static tree
7016 continue_class (tree klass)
7017 {
7018 switch (TREE_CODE (klass))
7019 {
7020 case CLASS_IMPLEMENTATION_TYPE:
7021 case CATEGORY_IMPLEMENTATION_TYPE:
7022 {
7023 struct imp_entry *imp_entry;
7024
7025 /* Check consistency of the instance variables. */
7026
7027 if (CLASS_RAW_IVARS (klass))
7028 check_ivars (implementation_template, klass);
7029
7030 /* code generation */
7031 #ifdef OBJCPLUS
7032 push_lang_context (lang_name_c);
7033 #endif
7034 build_private_template (implementation_template);
7035 uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template);
7036 objc_instance_type = build_pointer_type (uprivate_record);
7037
7038 imp_entry = ggc_alloc<struct imp_entry> ();
7039
7040 imp_entry->next = imp_list;
7041 imp_entry->imp_context = klass;
7042 imp_entry->imp_template = implementation_template;
7043 ucls_super_ref = uucls_super_ref = NULL;
7044 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7045 {
7046 imp_entry->class_decl = (*runtime.class_decl) (klass);
7047 imp_entry->meta_decl = (*runtime.metaclass_decl) (klass);
7048 }
7049 else
7050 {
7051 imp_entry->class_decl = (*runtime.category_decl) (klass);
7052 imp_entry->meta_decl = NULL;
7053 }
7054 imp_entry->has_cxx_cdtors = 0;
7055
7056 /* Append to front and increment count. */
7057 imp_list = imp_entry;
7058 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7059 imp_count++;
7060 else
7061 cat_count++;
7062 #ifdef OBJCPLUS
7063 pop_lang_context ();
7064 #endif /* OBJCPLUS */
7065
7066 return get_class_ivars (implementation_template, true);
7067 }
7068 case CLASS_INTERFACE_TYPE:
7069 {
7070 if (objc_in_class_extension)
7071 return NULL_TREE;
7072 #ifdef OBJCPLUS
7073 push_lang_context (lang_name_c);
7074 #endif /* OBJCPLUS */
7075 objc_collecting_ivars = 1;
7076 build_private_template (klass);
7077 objc_collecting_ivars = 0;
7078 #ifdef OBJCPLUS
7079 pop_lang_context ();
7080 #endif /* OBJCPLUS */
7081 return NULL_TREE;
7082 }
7083 default:
7084 return error_mark_node;
7085 }
7086 }
7087
7088 /* This routine builds name of the setter synthesized function. */
7089 char *
7090 objc_build_property_setter_name (tree ident)
7091 {
7092 /* TODO: Use alloca to allocate buffer of appropriate size. */
7093 static char string[BUFSIZE];
7094 sprintf (string, "set%s:", IDENTIFIER_POINTER (ident));
7095 string[3] = TOUPPER (string[3]);
7096 return string;
7097 }
7098
7099 /* This routine prepares the declarations of the property accessor
7100 helper functions (objc_getProperty(), etc) that are used when
7101 @synthesize is used.
7102
7103 runtime-specific routines are built in the respective runtime
7104 initialize functions. */
7105 static void
7106 build_common_objc_property_accessor_helpers (void)
7107 {
7108 tree type;
7109
7110 /* Declare the following function:
7111 id
7112 objc_getProperty (id self, SEL _cmd,
7113 ptrdiff_t offset, BOOL is_atomic); */
7114 type = build_function_type_list (objc_object_type,
7115 objc_object_type,
7116 objc_selector_type,
7117 ptrdiff_type_node,
7118 boolean_type_node,
7119 NULL_TREE);
7120 objc_getProperty_decl = add_builtin_function ("objc_getProperty",
7121 type, 0, NOT_BUILT_IN,
7122 NULL, NULL_TREE);
7123 TREE_NOTHROW (objc_getProperty_decl) = 0;
7124
7125 /* Declare the following function:
7126 void
7127 objc_setProperty (id self, SEL _cmd,
7128 ptrdiff_t offset, id new_value,
7129 BOOL is_atomic, BOOL should_copy); */
7130 type = build_function_type_list (void_type_node,
7131 objc_object_type,
7132 objc_selector_type,
7133 ptrdiff_type_node,
7134 objc_object_type,
7135 boolean_type_node,
7136 boolean_type_node,
7137 NULL_TREE);
7138 objc_setProperty_decl = add_builtin_function ("objc_setProperty",
7139 type, 0, NOT_BUILT_IN,
7140 NULL, NULL_TREE);
7141 TREE_NOTHROW (objc_setProperty_decl) = 0;
7142 }
7143
7144 /* This looks up an ivar in a class (including superclasses). */
7145 static tree
7146 lookup_ivar (tree interface, tree instance_variable_name)
7147 {
7148 while (interface)
7149 {
7150 tree decl_chain;
7151
7152 for (decl_chain = CLASS_IVARS (interface); decl_chain; decl_chain = DECL_CHAIN (decl_chain))
7153 if (DECL_NAME (decl_chain) == instance_variable_name)
7154 return decl_chain;
7155
7156 /* Not found. Search superclass if any. */
7157 if (CLASS_SUPER_NAME (interface))
7158 interface = lookup_interface (CLASS_SUPER_NAME (interface));
7159 }
7160
7161 return NULL_TREE;
7162 }
7163
7164 /* This routine synthesizes a 'getter' method. This is only called
7165 for @synthesize properties. */
7166 static void
7167 objc_synthesize_getter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
7168 {
7169 location_t location = DECL_SOURCE_LOCATION (property);
7170 tree fn, decl;
7171 tree body;
7172 tree ret_val;
7173
7174 /* If user has implemented a getter with same name then do nothing. */
7175 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7176 PROPERTY_GETTER_NAME (property)))
7177 return;
7178
7179 /* Find declaration of the property getter in the interface (or
7180 superclass, or protocol). There must be one. */
7181 decl = lookup_method_static (klass, PROPERTY_GETTER_NAME (property), 0);
7182
7183 /* If one not declared in the interface, this condition has already
7184 been reported as user error (because property was not declared in
7185 the interface). */
7186 if (!decl)
7187 return;
7188
7189 /* Adapt the 'decl'. Use the source location of the @synthesize
7190 statement for error messages. */
7191 decl = copy_node (decl);
7192 DECL_SOURCE_LOCATION (decl) = location;
7193
7194 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7195 NULL_TREE);
7196 body = c_begin_compound_stmt (true);
7197
7198 /* Now we need to decide how we build the getter. There are three
7199 cases:
7200
7201 for 'copy' or 'retain' properties we need to use the
7202 objc_getProperty() accessor helper which knows about retain and
7203 copy. It supports both 'nonatomic' and 'atomic' access.
7204
7205 for 'nonatomic, assign' properties we can access the instance
7206 variable directly. 'nonatomic' means we don't have to use locks,
7207 and 'assign' means we don't have to worry about retain or copy.
7208 If you combine the two, it means we can just access the instance
7209 variable directly.
7210
7211 for 'atomic, assign' properties we use objc_copyStruct() (for the
7212 next runtime) or objc_getPropertyStruct() (for the GNU runtime). */
7213 switch (PROPERTY_ASSIGN_SEMANTICS (property))
7214 {
7215 case OBJC_PROPERTY_RETAIN:
7216 case OBJC_PROPERTY_COPY:
7217 {
7218 /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */
7219 tree cmd, ivar, offset, is_atomic;
7220 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
7221
7222 /* Find the ivar to compute the offset. */
7223 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7224 if (!ivar || is_private (ivar))
7225 {
7226 /* This should never happen. */
7227 error_at (location,
7228 "can not find instance variable associated with property");
7229 ret_val = error_mark_node;
7230 break;
7231 }
7232 offset = byte_position (ivar);
7233
7234 if (PROPERTY_NONATOMIC (property))
7235 is_atomic = boolean_false_node;
7236 else
7237 is_atomic = boolean_true_node;
7238
7239 ret_val = build_function_call
7240 (location,
7241 /* Function prototype. */
7242 objc_getProperty_decl,
7243 /* Parameters. */
7244 tree_cons /* self */
7245 (NULL_TREE, self_decl,
7246 tree_cons /* _cmd */
7247 (NULL_TREE, cmd,
7248 tree_cons /* offset */
7249 (NULL_TREE, offset,
7250 tree_cons /* is_atomic */
7251 (NULL_TREE, is_atomic, NULL_TREE)))));
7252 }
7253 break;
7254 case OBJC_PROPERTY_ASSIGN:
7255 if (PROPERTY_NONATOMIC (property))
7256 {
7257 /* We build "return self->PROPERTY_IVAR_NAME;" */
7258 ret_val = objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property));
7259 break;
7260 }
7261 else
7262 {
7263 /* We build
7264 <property type> __objc_property_temp;
7265 objc_getPropertyStruct (&__objc_property_temp,
7266 &(self->PROPERTY_IVAR_NAME),
7267 sizeof (type of self->PROPERTY_IVAR_NAME),
7268 is_atomic,
7269 false)
7270 return __objc_property_temp;
7271
7272 For the NeXT runtime, we need to use objc_copyStruct
7273 instead of objc_getPropertyStruct. */
7274 tree objc_property_temp_decl, function_decl, function_call;
7275 tree size_of, is_atomic;
7276
7277 objc_property_temp_decl = objc_create_temporary_var (TREE_TYPE (property), "__objc_property_temp");
7278 DECL_SOURCE_LOCATION (objc_property_temp_decl) = location;
7279 objc_property_temp_decl = lang_hooks.decls.pushdecl (objc_property_temp_decl);
7280
7281 /* sizeof (ivar type). Since the ivar and the property have
7282 the same type, there is no need to lookup the ivar. */
7283 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7284 true /* is_sizeof */,
7285 false /* min_alignof */,
7286 false /* complain */);
7287
7288 if (PROPERTY_NONATOMIC (property))
7289 is_atomic = boolean_false_node;
7290 else
7291 is_atomic = boolean_true_node;
7292
7293 if (objc_copyStruct_decl)
7294 function_decl = objc_copyStruct_decl;
7295 else
7296 function_decl = objc_getPropertyStruct_decl;
7297
7298 function_call = build_function_call
7299 (location,
7300 /* Function prototype. */
7301 function_decl,
7302 /* Parameters. */
7303 tree_cons /* &__objc_property_temp_decl */
7304 /* Warning: note that using build_fold_addr_expr_loc()
7305 here causes invalid code to be generated. */
7306 (NULL_TREE, build_unary_op (location, ADDR_EXPR, objc_property_temp_decl, 0),
7307 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
7308 (NULL_TREE, build_fold_addr_expr_loc (location,
7309 objc_lookup_ivar
7310 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7311 tree_cons /* sizeof (PROPERTY_IVAR) */
7312 (NULL_TREE, size_of,
7313 tree_cons /* is_atomic */
7314 (NULL_TREE, is_atomic,
7315 /* TODO: This is currently ignored by the GNU
7316 runtime, but what about the next one ? */
7317 tree_cons /* has_strong */
7318 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7319
7320 add_stmt (function_call);
7321
7322 ret_val = objc_property_temp_decl;
7323 }
7324 break;
7325 default:
7326 gcc_unreachable ();
7327 }
7328
7329 gcc_assert (ret_val);
7330
7331 #ifdef OBJCPLUS
7332 finish_return_stmt (ret_val);
7333 #else
7334 c_finish_return (location, ret_val, NULL_TREE);
7335 #endif
7336
7337 add_stmt (c_end_compound_stmt (location, body, true));
7338 fn = current_function_decl;
7339 #ifdef OBJCPLUS
7340 finish_function ();
7341 #endif
7342 objc_finish_method_definition (fn);
7343 }
7344
7345 /* This routine synthesizes a 'setter' method. */
7346
7347 static void
7348 objc_synthesize_setter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
7349 {
7350 location_t location = DECL_SOURCE_LOCATION (property);
7351 tree fn, decl;
7352 tree body;
7353 tree new_value, statement;
7354
7355 /* If user has implemented a setter with same name then do nothing. */
7356 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7357 PROPERTY_SETTER_NAME (property)))
7358 return;
7359
7360 /* Find declaration of the property setter in the interface (or
7361 superclass, or protocol). There must be one. */
7362 decl = lookup_method_static (klass, PROPERTY_SETTER_NAME (property), 0);
7363
7364 /* If one not declared in the interface, this condition has already
7365 been reported as user error (because property was not declared in
7366 the interface). */
7367 if (!decl)
7368 return;
7369
7370 /* Adapt the 'decl'. Use the source location of the @synthesize
7371 statement for error messages. */
7372 decl = copy_node (decl);
7373 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (property);
7374
7375 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7376 NULL_TREE);
7377
7378 body = c_begin_compound_stmt (true);
7379
7380 /* The 'new_value' is the only argument to the method, which is the
7381 3rd argument of the function, after self and _cmd. We use twice
7382 TREE_CHAIN to move forward two arguments. */
7383 new_value = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)));
7384
7385 /* This would presumably happen if the user has specified a
7386 prototype for the setter that does not have an argument! */
7387 if (new_value == NULL_TREE)
7388 {
7389 /* TODO: This should be caught much earlier than this. */
7390 error_at (DECL_SOURCE_LOCATION (decl), "invalid setter, it must have one argument");
7391 /* Try to recover somehow. */
7392 new_value = error_mark_node;
7393 }
7394
7395 /* Now we need to decide how we build the setter. There are three
7396 cases:
7397
7398 for 'copy' or 'retain' properties we need to use the
7399 objc_setProperty() accessor helper which knows about retain and
7400 copy. It supports both 'nonatomic' and 'atomic' access.
7401
7402 for 'nonatomic, assign' properties we can access the instance
7403 variable directly. 'nonatomic' means we don't have to use locks,
7404 and 'assign' means we don't have to worry about retain or copy.
7405 If you combine the two, it means we can just access the instance
7406 variable directly.
7407
7408 for 'atomic, assign' properties we use objc_copyStruct() (for the
7409 next runtime) or objc_setPropertyStruct() (for the GNU runtime). */
7410 switch (PROPERTY_ASSIGN_SEMANTICS (property))
7411 {
7412 case OBJC_PROPERTY_RETAIN:
7413 case OBJC_PROPERTY_COPY:
7414 {
7415 /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */
7416 tree cmd, ivar, offset, is_atomic, should_copy;
7417 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
7418
7419 /* Find the ivar to compute the offset. */
7420 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7421 if (!ivar || is_private (ivar))
7422 {
7423 error_at (location,
7424 "can not find instance variable associated with property");
7425 statement = error_mark_node;
7426 break;
7427 }
7428 offset = byte_position (ivar);
7429
7430 if (PROPERTY_NONATOMIC (property))
7431 is_atomic = boolean_false_node;
7432 else
7433 is_atomic = boolean_true_node;
7434
7435 if (PROPERTY_ASSIGN_SEMANTICS (property) == OBJC_PROPERTY_COPY)
7436 should_copy = boolean_true_node;
7437 else
7438 should_copy = boolean_false_node;
7439
7440 statement = build_function_call
7441 (location,
7442 /* Function prototype. */
7443 objc_setProperty_decl,
7444 /* Parameters. */
7445 tree_cons /* self */
7446 (NULL_TREE, self_decl,
7447 tree_cons /* _cmd */
7448 (NULL_TREE, cmd,
7449 tree_cons /* offset */
7450 (NULL_TREE, offset,
7451 tree_cons /* new_value */
7452 (NULL_TREE, new_value,
7453 tree_cons /* is_atomic */
7454 (NULL_TREE, is_atomic,
7455 tree_cons /* should_copy */
7456 (NULL_TREE, should_copy, NULL_TREE)))))));
7457 }
7458 break;
7459 case OBJC_PROPERTY_ASSIGN:
7460 if (PROPERTY_NONATOMIC (property))
7461 {
7462 /* We build "self->PROPERTY_IVAR_NAME = new_value;" */
7463 statement = build_modify_expr
7464 (location,
7465 objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)),
7466 NULL_TREE, NOP_EXPR,
7467 location, new_value, NULL_TREE);
7468 break;
7469 }
7470 else
7471 {
7472 /* We build
7473 objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME),
7474 &new_value,
7475 sizeof (type of self->PROPERTY_IVAR_NAME),
7476 is_atomic,
7477 false)
7478
7479 For the NeXT runtime, we need to use objc_copyStruct
7480 instead of objc_getPropertyStruct. */
7481 tree function_decl, size_of, is_atomic;
7482
7483 /* sizeof (ivar type). Since the ivar and the property have
7484 the same type, there is no need to lookup the ivar. */
7485 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7486 true /* is_sizeof */,
7487 false /* min_alignof */,
7488 false /* complain */);
7489
7490 if (PROPERTY_NONATOMIC (property))
7491 is_atomic = boolean_false_node;
7492 else
7493 is_atomic = boolean_true_node;
7494
7495 if (objc_copyStruct_decl)
7496 function_decl = objc_copyStruct_decl;
7497 else
7498 function_decl = objc_setPropertyStruct_decl;
7499
7500 statement = build_function_call
7501 (location,
7502 /* Function prototype. */
7503 function_decl,
7504 /* Parameters. */
7505 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
7506 (NULL_TREE, build_fold_addr_expr_loc (location,
7507 objc_lookup_ivar
7508 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7509 tree_cons /* &new_value */
7510 (NULL_TREE, build_fold_addr_expr_loc (location, new_value),
7511 tree_cons /* sizeof (PROPERTY_IVAR) */
7512 (NULL_TREE, size_of,
7513 tree_cons /* is_atomic */
7514 (NULL_TREE, is_atomic,
7515 /* TODO: This is currently ignored by the GNU
7516 runtime, but what about the next one ? */
7517 tree_cons /* has_strong */
7518 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7519 }
7520 break;
7521 default:
7522 gcc_unreachable ();
7523 }
7524 gcc_assert (statement);
7525
7526 add_stmt (statement);
7527 add_stmt (c_end_compound_stmt (location, body, true));
7528 fn = current_function_decl;
7529 #ifdef OBJCPLUS
7530 finish_function ();
7531 #endif
7532 objc_finish_method_definition (fn);
7533 }
7534
7535 /* This function is a sub-routine of objc_add_synthesize_declaration.
7536 It is called for each property to synthesize once we have
7537 determined that the context is Ok. */
7538 static void
7539 objc_add_synthesize_declaration_for_property (location_t location, tree interface,
7540 tree property_name, tree ivar_name)
7541 {
7542 /* Find the @property declaration. */
7543 tree property;
7544 tree x;
7545
7546 /* Check that synthesize or dynamic has not already been used for
7547 the same property. */
7548 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7549 if (PROPERTY_NAME (property) == property_name)
7550 {
7551 location_t original_location = DECL_SOURCE_LOCATION (property);
7552
7553 if (PROPERTY_DYNAMIC (property))
7554 error_at (location, "property %qs already specified in %<@dynamic%>",
7555 IDENTIFIER_POINTER (property_name));
7556 else
7557 error_at (location, "property %qs already specified in %<@synthesize%>",
7558 IDENTIFIER_POINTER (property_name));
7559
7560 if (original_location != UNKNOWN_LOCATION)
7561 inform (original_location, "originally specified here");
7562 return;
7563 }
7564
7565 /* Check that the property is declared in the interface. It could
7566 also be declared in a superclass or protocol. */
7567 property = lookup_property (interface, property_name);
7568
7569 if (!property)
7570 {
7571 error_at (location, "no declaration of property %qs found in the interface",
7572 IDENTIFIER_POINTER (property_name));
7573 return;
7574 }
7575 else
7576 {
7577 /* We have to copy the property, because we want to chain it to
7578 the implementation context, and we want to store the source
7579 location of the @synthesize, not of the original
7580 @property. */
7581 property = copy_node (property);
7582 DECL_SOURCE_LOCATION (property) = location;
7583 }
7584
7585 /* Determine PROPERTY_IVAR_NAME. */
7586 if (ivar_name == NULL_TREE)
7587 ivar_name = property_name;
7588
7589 /* Check that the instance variable exists. You can only use an
7590 instance variable from the same class, not one from the
7591 superclass (this makes sense as it allows us to check that an
7592 instance variable is only used in one synthesized property). */
7593 {
7594 tree ivar = is_ivar (CLASS_IVARS (interface), ivar_name);
7595 tree type_of_ivar;
7596 if (!ivar)
7597 {
7598 error_at (location, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar",
7599 IDENTIFIER_POINTER (property_name));
7600 return;
7601 }
7602
7603 if (DECL_BIT_FIELD_TYPE (ivar))
7604 type_of_ivar = DECL_BIT_FIELD_TYPE (ivar);
7605 else
7606 type_of_ivar = TREE_TYPE (ivar);
7607
7608 /* If the instance variable has a different C type, we throw an error ... */
7609 if (!comptypes (TREE_TYPE (property), type_of_ivar)
7610 /* ... unless the property is readonly, in which case we allow
7611 the instance variable to be more specialized (this means we
7612 can generate the getter all right and it works). */
7613 && (!PROPERTY_READONLY (property)
7614 || !objc_compare_types (TREE_TYPE (property),
7615 type_of_ivar, -5, NULL_TREE)))
7616 {
7617 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7618
7619 error_at (location, "property %qs is using instance variable %qs of incompatible type",
7620 IDENTIFIER_POINTER (property_name),
7621 IDENTIFIER_POINTER (ivar_name));
7622
7623 if (original_location != UNKNOWN_LOCATION)
7624 inform (original_location, "originally specified here");
7625 }
7626
7627 /* If the instance variable is a bitfield, the property must be
7628 'assign', 'nonatomic' because the runtime getter/setter helper
7629 do not work with bitfield instance variables. */
7630 if (DECL_BIT_FIELD_TYPE (ivar))
7631 {
7632 /* If there is an error, we return and not generate any
7633 getter/setter because trying to set up the runtime
7634 getter/setter helper calls with bitfields is at high risk
7635 of ICE. */
7636
7637 if (PROPERTY_ASSIGN_SEMANTICS (property) != OBJC_PROPERTY_ASSIGN)
7638 {
7639 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7640
7641 error_at (location, "'assign' property %qs is using bit-field instance variable %qs",
7642 IDENTIFIER_POINTER (property_name),
7643 IDENTIFIER_POINTER (ivar_name));
7644
7645 if (original_location != UNKNOWN_LOCATION)
7646 inform (original_location, "originally specified here");
7647 return;
7648 }
7649
7650 if (!PROPERTY_NONATOMIC (property))
7651 {
7652 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7653
7654 error_at (location, "'atomic' property %qs is using bit-field instance variable %qs",
7655 IDENTIFIER_POINTER (property_name),
7656 IDENTIFIER_POINTER (ivar_name));
7657
7658 if (original_location != UNKNOWN_LOCATION)
7659 inform (original_location, "originally specified here");
7660 return;
7661 }
7662 }
7663 }
7664
7665 /* Check that no other property is using the same instance
7666 variable. */
7667 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7668 if (PROPERTY_IVAR_NAME (x) == ivar_name)
7669 {
7670 location_t original_location = DECL_SOURCE_LOCATION (x);
7671
7672 error_at (location, "property %qs is using the same instance variable as property %qs",
7673 IDENTIFIER_POINTER (property_name),
7674 IDENTIFIER_POINTER (PROPERTY_NAME (x)));
7675
7676 if (original_location != UNKNOWN_LOCATION)
7677 inform (original_location, "originally specified here");
7678
7679 /* We keep going on. This won't cause the compiler to fail;
7680 the failure would most likely be at runtime. */
7681 }
7682
7683 /* Note that a @synthesize (and only a @synthesize) always sets
7684 PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a
7685 @synthesize by that. */
7686 PROPERTY_IVAR_NAME (property) = ivar_name;
7687
7688 /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the
7689 original declaration; they are always set (with the exception of
7690 PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */
7691
7692 /* Add the property to the list of properties for current implementation. */
7693 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7694 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7695
7696 /* Note how we don't actually synthesize the getter/setter here; it
7697 would be very natural, but we may miss the fact that the user has
7698 implemented his own getter/setter later on in the @implementation
7699 (in which case we shouldn't generate getter/setter). We wait
7700 until we have parsed it all before generating the code. */
7701 }
7702
7703 /* This function is called by the parser after a @synthesize
7704 expression is parsed. 'location' is the location of the
7705 @synthesize expression, and 'property_and_ivar_list' is a chained
7706 list of the property and ivar names. */
7707 void
7708 objc_add_synthesize_declaration (location_t location, tree property_and_ivar_list)
7709 {
7710 tree interface, chain;
7711
7712 if (flag_objc1_only)
7713 error_at (input_location, "%<@synthesize%> is not available in Objective-C 1.0");
7714
7715 if (property_and_ivar_list == error_mark_node)
7716 return;
7717
7718 if (!objc_implementation_context)
7719 {
7720 /* We can get here only in Objective-C; the Objective-C++ parser
7721 detects the problem while parsing, outputs the error
7722 "misplaced '@synthesize' Objective-C++ construct" and skips
7723 the declaration. */
7724 error_at (location, "%<@synthesize%> not in @implementation context");
7725 return;
7726 }
7727
7728 if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE)
7729 {
7730 error_at (location, "%<@synthesize%> can not be used in categories");
7731 return;
7732 }
7733
7734 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7735 if (!interface)
7736 {
7737 /* I can't see how this could happen, but it is good as a safety check. */
7738 error_at (location,
7739 "%<@synthesize%> requires the @interface of the class to be available");
7740 return;
7741 }
7742
7743 /* Now, iterate over the properties and do each of them. */
7744 for (chain = property_and_ivar_list; chain; chain = TREE_CHAIN (chain))
7745 {
7746 objc_add_synthesize_declaration_for_property (location, interface, TREE_VALUE (chain),
7747 TREE_PURPOSE (chain));
7748 }
7749 }
7750
7751 /* This function is a sub-routine of objc_add_dynamic_declaration. It
7752 is called for each property to mark as dynamic once we have
7753 determined that the context is Ok. */
7754 static void
7755 objc_add_dynamic_declaration_for_property (location_t location, tree interface,
7756 tree property_name)
7757 {
7758 /* Find the @property declaration. */
7759 tree property;
7760
7761 /* Check that synthesize or dynamic has not already been used for
7762 the same property. */
7763 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7764 if (PROPERTY_NAME (property) == property_name)
7765 {
7766 location_t original_location = DECL_SOURCE_LOCATION (property);
7767
7768 if (PROPERTY_DYNAMIC (property))
7769 error_at (location, "property %qs already specified in %<@dynamic%>",
7770 IDENTIFIER_POINTER (property_name));
7771 else
7772 error_at (location, "property %qs already specified in %<@synthesize%>",
7773 IDENTIFIER_POINTER (property_name));
7774
7775 if (original_location != UNKNOWN_LOCATION)
7776 inform (original_location, "originally specified here");
7777 return;
7778 }
7779
7780 /* Check that the property is declared in the interface. It could
7781 also be declared in a superclass or protocol. */
7782 property = lookup_property (interface, property_name);
7783
7784 if (!property)
7785 {
7786 error_at (location, "no declaration of property %qs found in the interface",
7787 IDENTIFIER_POINTER (property_name));
7788 return;
7789 }
7790 else
7791 {
7792 /* We have to copy the property, because we want to chain it to
7793 the implementation context, and we want to store the source
7794 location of the @synthesize, not of the original
7795 @property. */
7796 property = copy_node (property);
7797 DECL_SOURCE_LOCATION (property) = location;
7798 }
7799
7800 /* Note that a @dynamic (and only a @dynamic) always sets
7801 PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that.
7802 (actually, as explained above, PROPERTY_DECL generated by
7803 @property and associated with a @dynamic property are also marked
7804 as PROPERTY_DYNAMIC). */
7805 PROPERTY_DYNAMIC (property) = 1;
7806
7807 /* Add the property to the list of properties for current implementation. */
7808 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7809 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7810 }
7811
7812 /* This function is called by the parser after a @dynamic expression
7813 is parsed. 'location' is the location of the @dynamic expression,
7814 and 'property_list' is a chained list of all the property
7815 names. */
7816 void
7817 objc_add_dynamic_declaration (location_t location, tree property_list)
7818 {
7819 tree interface, chain;
7820
7821 if (flag_objc1_only)
7822 error_at (input_location, "%<@dynamic%> is not available in Objective-C 1.0");
7823
7824 if (property_list == error_mark_node)
7825 return;
7826
7827 if (!objc_implementation_context)
7828 {
7829 /* We can get here only in Objective-C; the Objective-C++ parser
7830 detects the problem while parsing, outputs the error
7831 "misplaced '@dynamic' Objective-C++ construct" and skips the
7832 declaration. */
7833 error_at (location, "%<@dynamic%> not in @implementation context");
7834 return;
7835 }
7836
7837 /* @dynamic is allowed in categories. */
7838 switch (TREE_CODE (objc_implementation_context))
7839 {
7840 case CLASS_IMPLEMENTATION_TYPE:
7841 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7842 break;
7843 case CATEGORY_IMPLEMENTATION_TYPE:
7844 interface = lookup_category (implementation_template,
7845 CLASS_SUPER_NAME (objc_implementation_context));
7846 break;
7847 default:
7848 gcc_unreachable ();
7849 }
7850
7851 if (!interface)
7852 {
7853 /* I can't see how this could happen, but it is good as a safety check. */
7854 error_at (location,
7855 "%<@dynamic%> requires the @interface of the class to be available");
7856 return;
7857 }
7858
7859 /* Now, iterate over the properties and do each of them. */
7860 for (chain = property_list; chain; chain = TREE_CHAIN (chain))
7861 {
7862 objc_add_dynamic_declaration_for_property (location, interface, TREE_VALUE (chain));
7863 }
7864 }
7865
7866 /* Main routine to generate code/data for all the property information for
7867 current implementation (class or category). CLASS is the interface where
7868 ivars are declared. CLASS_METHODS is where methods are found which
7869 could be a class or a category depending on whether we are implementing
7870 property of a class or a category. */
7871
7872 static void
7873 objc_gen_property_data (tree klass, tree class_methods)
7874 {
7875 tree x;
7876
7877 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7878 {
7879 /* @dynamic property - nothing to check or synthesize. */
7880 if (PROPERTY_DYNAMIC (x))
7881 continue;
7882
7883 /* @synthesize property - need to synthesize the accessors. */
7884 if (PROPERTY_IVAR_NAME (x))
7885 {
7886 objc_synthesize_getter (klass, class_methods, x);
7887
7888 if (PROPERTY_READONLY (x) == 0)
7889 objc_synthesize_setter (klass, class_methods, x);
7890
7891 continue;
7892 }
7893
7894 gcc_unreachable ();
7895 }
7896 }
7897
7898 /* This is called once we see the "@end" in an interface/implementation. */
7899
7900 static void
7901 finish_class (tree klass)
7902 {
7903 switch (TREE_CODE (klass))
7904 {
7905 case CLASS_IMPLEMENTATION_TYPE:
7906 {
7907 /* All metadata generation is done in runtime.generate_metadata(). */
7908
7909 /* Generate what needed for property; setters, getters, etc. */
7910 objc_gen_property_data (implementation_template, implementation_template);
7911
7912 if (implementation_template != objc_implementation_context)
7913 {
7914 /* Ensure that all method listed in the interface contain bodies. */
7915 check_methods (CLASS_CLS_METHODS (implementation_template),
7916 objc_implementation_context, '+');
7917 check_methods (CLASS_NST_METHODS (implementation_template),
7918 objc_implementation_context, '-');
7919
7920 if (CLASS_PROTOCOL_LIST (implementation_template))
7921 check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
7922 "class",
7923 CLASS_NAME (objc_implementation_context));
7924 }
7925 break;
7926 }
7927 case CATEGORY_IMPLEMENTATION_TYPE:
7928 {
7929 tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass));
7930
7931 if (category)
7932 {
7933 /* Generate what needed for property; setters, getters, etc. */
7934 objc_gen_property_data (implementation_template, category);
7935
7936 /* Ensure all method listed in the interface contain bodies. */
7937 check_methods (CLASS_CLS_METHODS (category),
7938 objc_implementation_context, '+');
7939 check_methods (CLASS_NST_METHODS (category),
7940 objc_implementation_context, '-');
7941
7942 if (CLASS_PROTOCOL_LIST (category))
7943 check_protocols (CLASS_PROTOCOL_LIST (category),
7944 "category",
7945 CLASS_SUPER_NAME (objc_implementation_context));
7946 }
7947 break;
7948 }
7949 case CLASS_INTERFACE_TYPE:
7950 case CATEGORY_INTERFACE_TYPE:
7951 case PROTOCOL_INTERFACE_TYPE:
7952 {
7953 /* Process properties of the class. */
7954 tree x;
7955 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
7956 {
7957 /* Now we check that the appropriate getter is declared,
7958 and if not, we declare one ourselves. */
7959 tree getter_decl = lookup_method (CLASS_NST_METHODS (klass),
7960 PROPERTY_GETTER_NAME (x));
7961
7962 if (getter_decl)
7963 {
7964 /* TODO: Check that the declaration is consistent with the property. */
7965 ;
7966 }
7967 else
7968 {
7969 /* Generate an instance method declaration for the
7970 getter; for example "- (id) name;". In general it
7971 will be of the form
7972 -(type)property_getter_name; */
7973 tree rettype = build_tree_list (NULL_TREE, TREE_TYPE (x));
7974 getter_decl = build_method_decl (INSTANCE_METHOD_DECL,
7975 rettype, PROPERTY_GETTER_NAME (x),
7976 NULL_TREE, false);
7977 if (PROPERTY_OPTIONAL (x))
7978 objc_add_method (objc_interface_context, getter_decl, false, true);
7979 else
7980 objc_add_method (objc_interface_context, getter_decl, false, false);
7981 TREE_DEPRECATED (getter_decl) = TREE_DEPRECATED (x);
7982 METHOD_PROPERTY_CONTEXT (getter_decl) = x;
7983 }
7984
7985 if (PROPERTY_READONLY (x) == 0)
7986 {
7987 /* Now we check that the appropriate setter is declared,
7988 and if not, we declare on ourselves. */
7989 tree setter_decl = lookup_method (CLASS_NST_METHODS (klass),
7990 PROPERTY_SETTER_NAME (x));
7991
7992 if (setter_decl)
7993 {
7994 /* TODO: Check that the declaration is consistent with the property. */
7995 ;
7996 }
7997 else
7998 {
7999 /* The setter name is something like 'setName:'.
8000 We need the substring 'setName' to build the
8001 method declaration due to how the declaration
8002 works. TODO: build_method_decl() will then
8003 generate back 'setName:' from 'setName'; it
8004 would be more efficient to hook into there. */
8005 const char *full_setter_name = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x));
8006 size_t length = strlen (full_setter_name);
8007 char *setter_name = (char *) alloca (length);
8008 tree ret_type, selector, arg_type, arg_name;
8009
8010 memcpy (setter_name, full_setter_name, length - 1);
8011 setter_name[length - 1] = '\0';
8012 ret_type = build_tree_list (NULL_TREE, void_type_node);
8013 arg_type = build_tree_list (NULL_TREE, TREE_TYPE (x));
8014 arg_name = get_identifier ("_value");
8015 selector = objc_build_keyword_decl (get_identifier (setter_name),
8016 arg_type, arg_name, NULL);
8017 setter_decl = build_method_decl (INSTANCE_METHOD_DECL,
8018 ret_type, selector,
8019 build_tree_list (NULL_TREE, NULL_TREE),
8020 false);
8021 if (PROPERTY_OPTIONAL (x))
8022 objc_add_method (objc_interface_context, setter_decl, false, true);
8023 else
8024 objc_add_method (objc_interface_context, setter_decl, false, false);
8025 TREE_DEPRECATED (setter_decl) = TREE_DEPRECATED (x);
8026 METHOD_PROPERTY_CONTEXT (setter_decl) = x;
8027 }
8028 }
8029 }
8030 break;
8031 }
8032 default:
8033 gcc_unreachable ();
8034 break;
8035 }
8036 }
8037
8038 static tree
8039 add_protocol (tree protocol)
8040 {
8041 /* Put protocol on list in reverse order. */
8042 TREE_CHAIN (protocol) = protocol_chain;
8043 protocol_chain = protocol;
8044 return protocol_chain;
8045 }
8046
8047 /* Check that a protocol is defined, and, recursively, that all
8048 protocols that this protocol conforms to are defined too. */
8049 static void
8050 check_that_protocol_is_defined (tree protocol)
8051 {
8052 if (!PROTOCOL_DEFINED (protocol))
8053 warning (0, "definition of protocol %qE not found",
8054 PROTOCOL_NAME (protocol));
8055
8056 /* If the protocol itself conforms to other protocols, check them
8057 too, recursively. */
8058 if (PROTOCOL_LIST (protocol))
8059 {
8060 tree p;
8061
8062 for (p = PROTOCOL_LIST (protocol); p; p = TREE_CHAIN (p))
8063 check_that_protocol_is_defined (TREE_VALUE (p));
8064 }
8065 }
8066
8067 /* Looks up a protocol. If 'warn_if_deprecated' is true, a warning is
8068 emitted if the protocol is deprecated. If 'definition_required' is
8069 true, a warning is emitted if a full @protocol definition has not
8070 been seen. */
8071 static tree
8072 lookup_protocol (tree ident, bool warn_if_deprecated, bool definition_required)
8073 {
8074 tree chain;
8075
8076 for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
8077 if (ident == PROTOCOL_NAME (chain))
8078 {
8079 if (warn_if_deprecated && TREE_DEPRECATED (chain))
8080 {
8081 /* It would be nice to use warn_deprecated_use() here, but
8082 we are using TREE_CHAIN (which is supposed to be the
8083 TYPE_STUB_DECL for a TYPE) for something different. */
8084 warning (OPT_Wdeprecated_declarations, "protocol %qE is deprecated",
8085 PROTOCOL_NAME (chain));
8086 }
8087
8088 if (definition_required)
8089 check_that_protocol_is_defined (chain);
8090
8091 return chain;
8092 }
8093
8094 return NULL_TREE;
8095 }
8096
8097 /* This function forward declares the protocols named by NAMES. If
8098 they are already declared or defined, the function has no effect. */
8099
8100 void
8101 objc_declare_protocol (tree name, tree attributes)
8102 {
8103 bool deprecated = false;
8104
8105 #ifdef OBJCPLUS
8106 if (current_namespace != global_namespace) {
8107 error ("Objective-C declarations may only appear in global scope");
8108 }
8109 #endif /* OBJCPLUS */
8110
8111 /* Determine if 'deprecated', the only attribute we recognize for
8112 protocols, was used. Ignore all other attributes. */
8113 if (attributes)
8114 {
8115 tree attribute;
8116 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8117 {
8118 tree name = TREE_PURPOSE (attribute);
8119
8120 if (is_attribute_p ("deprecated", name))
8121 deprecated = true;
8122 else
8123 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8124 }
8125 }
8126
8127 if (lookup_protocol (name, /* warn if deprecated */ false,
8128 /* definition_required */ false) == NULL_TREE)
8129 {
8130 tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
8131
8132 TYPE_LANG_SLOT_1 (protocol)
8133 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8134 PROTOCOL_NAME (protocol) = name;
8135 PROTOCOL_LIST (protocol) = NULL_TREE;
8136 add_protocol (protocol);
8137 PROTOCOL_DEFINED (protocol) = 0;
8138 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8139
8140 if (attributes)
8141 {
8142 /* TODO: Do we need to store the attributes here ? */
8143 TYPE_ATTRIBUTES (protocol) = attributes;
8144 if (deprecated)
8145 TREE_DEPRECATED (protocol) = 1;
8146 }
8147 }
8148 }
8149
8150 static tree
8151 start_protocol (enum tree_code code, tree name, tree list, tree attributes)
8152 {
8153 tree protocol;
8154 bool deprecated = false;
8155
8156 #ifdef OBJCPLUS
8157 if (current_namespace != global_namespace) {
8158 error ("Objective-C declarations may only appear in global scope");
8159 }
8160 #endif /* OBJCPLUS */
8161
8162 /* Determine if 'deprecated', the only attribute we recognize for
8163 protocols, was used. Ignore all other attributes. */
8164 if (attributes)
8165 {
8166 tree attribute;
8167 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8168 {
8169 tree name = TREE_PURPOSE (attribute);
8170
8171 if (is_attribute_p ("deprecated", name))
8172 deprecated = true;
8173 else
8174 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8175 }
8176 }
8177
8178 protocol = lookup_protocol (name, /* warn_if_deprecated */ false,
8179 /* definition_required */ false);
8180
8181 if (!protocol)
8182 {
8183 protocol = make_node (code);
8184 TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8185
8186 PROTOCOL_NAME (protocol) = name;
8187 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8188 add_protocol (protocol);
8189 PROTOCOL_DEFINED (protocol) = 1;
8190 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8191
8192 check_protocol_recursively (protocol, list);
8193 }
8194 else if (! PROTOCOL_DEFINED (protocol))
8195 {
8196 PROTOCOL_DEFINED (protocol) = 1;
8197 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8198
8199 check_protocol_recursively (protocol, list);
8200 }
8201 else
8202 {
8203 warning (0, "duplicate declaration for protocol %qE",
8204 name);
8205 }
8206
8207 if (attributes)
8208 {
8209 TYPE_ATTRIBUTES (protocol) = attributes;
8210 if (deprecated)
8211 TREE_DEPRECATED (protocol) = 1;
8212 }
8213
8214 return protocol;
8215 }
8216
8217 /* Decay array and function parameters into pointers. */
8218
8219 static tree
8220 objc_decay_parm_type (tree type)
8221 {
8222 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE)
8223 type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE
8224 ? TREE_TYPE (type)
8225 : type);
8226
8227 return type;
8228 }
8229
8230 static GTY(()) tree objc_parmlist = NULL_TREE;
8231
8232 /* Append PARM to a list of formal parameters of a method, making a necessary
8233 array-to-pointer adjustment along the way. */
8234
8235 void
8236 objc_push_parm (tree parm)
8237 {
8238 tree type;
8239
8240 if (TREE_TYPE (parm) == error_mark_node)
8241 {
8242 objc_parmlist = chainon (objc_parmlist, parm);
8243 return;
8244 }
8245
8246 /* Decay arrays and functions into pointers. */
8247 type = objc_decay_parm_type (TREE_TYPE (parm));
8248
8249 /* If the parameter type has been decayed, a new PARM_DECL needs to be
8250 built as well. */
8251 if (type != TREE_TYPE (parm))
8252 parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type);
8253
8254 DECL_ARG_TYPE (parm)
8255 = lang_hooks.types.type_promotes_to (TREE_TYPE (parm));
8256
8257 /* Record constancy and volatility. */
8258 c_apply_type_quals_to_decl
8259 ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0)
8260 | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0)
8261 | (TYPE_ATOMIC (TREE_TYPE (parm)) ? TYPE_QUAL_ATOMIC : 0)
8262 | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm);
8263
8264 objc_parmlist = chainon (objc_parmlist, parm);
8265 }
8266
8267 /* Retrieve the formal parameter list constructed via preceding calls to
8268 objc_push_parm(). */
8269
8270 #ifdef OBJCPLUS
8271 tree
8272 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED,
8273 tree expr ATTRIBUTE_UNUSED)
8274 {
8275 tree parm_info = objc_parmlist;
8276 objc_parmlist = NULL_TREE;
8277
8278 return parm_info;
8279 }
8280 #else
8281 struct c_arg_info *
8282 objc_get_parm_info (int have_ellipsis, tree expr)
8283 {
8284 tree parm_info = objc_parmlist;
8285 struct c_arg_info *arg_info;
8286 /* The C front-end requires an elaborate song and dance at
8287 this point. */
8288 push_scope ();
8289 declare_parm_level ();
8290 while (parm_info)
8291 {
8292 tree next = DECL_CHAIN (parm_info);
8293
8294 DECL_CHAIN (parm_info) = NULL_TREE;
8295 parm_info = pushdecl (parm_info);
8296 finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
8297 parm_info = next;
8298 }
8299 arg_info = get_parm_info (have_ellipsis, expr);
8300 pop_scope ();
8301 objc_parmlist = NULL_TREE;
8302 return arg_info;
8303 }
8304 #endif
8305
8306 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
8307 method definitions. In the case of instance methods, we can be more
8308 specific as to the type of 'self'. */
8309
8310 static void
8311 synth_self_and_ucmd_args (void)
8312 {
8313 tree self_type;
8314
8315 if (objc_method_context
8316 && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL)
8317 self_type = objc_instance_type;
8318 else
8319 /* Really a `struct objc_class *'. However, we allow people to
8320 assign to self, which changes its type midstream. */
8321 self_type = objc_object_type;
8322
8323 /* id self; */
8324 objc_push_parm (build_decl (input_location,
8325 PARM_DECL, self_id, self_type));
8326
8327 /* SEL _cmd; */
8328 objc_push_parm (build_decl (input_location,
8329 PARM_DECL, ucmd_id, objc_selector_type));
8330 }
8331
8332 /* Transform an Objective-C method definition into a static C function
8333 definition, synthesizing the first two arguments, "self" and "_cmd",
8334 in the process. EXPR is NULL or an expression that needs to be
8335 evaluated for the side effects of array size expressions in the
8336 parameters. */
8337
8338 static void
8339 start_method_def (tree method, tree expr)
8340 {
8341 tree parmlist;
8342 #ifdef OBJCPLUS
8343 tree parm_info;
8344 #else
8345 struct c_arg_info *parm_info;
8346 #endif
8347 int have_ellipsis = 0;
8348
8349 /* If we are defining a "dealloc" method in a non-root class, we
8350 will need to check if a [super dealloc] is missing, and warn if
8351 it is. */
8352 if(CLASS_SUPER_NAME (objc_implementation_context)
8353 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method))))
8354 should_call_super_dealloc = 1;
8355 else
8356 should_call_super_dealloc = 0;
8357
8358 /* Required to implement _msgSuper. */
8359 objc_method_context = method;
8360 UOBJC_SUPER_decl = NULL_TREE;
8361
8362 /* Generate prototype declarations for arguments..."new-style". */
8363 synth_self_and_ucmd_args ();
8364
8365 /* Generate argument declarations if a keyword_decl. */
8366 parmlist = METHOD_SEL_ARGS (method);
8367 while (parmlist)
8368 {
8369 /* parmlist is a KEYWORD_DECL. */
8370 tree type = TREE_VALUE (TREE_TYPE (parmlist));
8371 tree parm;
8372
8373 parm = build_decl (input_location,
8374 PARM_DECL, KEYWORD_ARG_NAME (parmlist), type);
8375 decl_attributes (&parm, DECL_ATTRIBUTES (parmlist), 0);
8376 objc_push_parm (parm);
8377 parmlist = DECL_CHAIN (parmlist);
8378 }
8379
8380 if (METHOD_ADD_ARGS (method))
8381 {
8382 tree akey;
8383
8384 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method));
8385 akey; akey = TREE_CHAIN (akey))
8386 {
8387 objc_push_parm (TREE_VALUE (akey));
8388 }
8389
8390 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
8391 have_ellipsis = 1;
8392 }
8393
8394 parm_info = objc_get_parm_info (have_ellipsis, expr);
8395
8396 really_start_method (objc_method_context, parm_info);
8397 }
8398
8399 /* Return 1 if TYPE1 is equivalent to TYPE2 for purposes of method
8400 overloading. */
8401 static int
8402 objc_types_are_equivalent (tree type1, tree type2)
8403 {
8404 if (type1 == type2)
8405 return 1;
8406
8407 /* Strip away indirections. */
8408 while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE)
8409 && (TREE_CODE (type1) == TREE_CODE (type2)))
8410 type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2);
8411 if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
8412 return 0;
8413
8414 /* Compare the protocol lists. */
8415 type1 = (TYPE_HAS_OBJC_INFO (type1)
8416 ? TYPE_OBJC_PROTOCOL_LIST (type1)
8417 : NULL_TREE);
8418 type2 = (TYPE_HAS_OBJC_INFO (type2)
8419 ? TYPE_OBJC_PROTOCOL_LIST (type2)
8420 : NULL_TREE);
8421
8422 /* If there are no protocols (most common case), the types are
8423 identical. */
8424 if (type1 == NULL_TREE && type2 == NULL_TREE)
8425 return 1;
8426
8427 /* If one has protocols, and the other one hasn't, they are not
8428 identical. */
8429 if ((type1 == NULL_TREE && type2 != NULL_TREE)
8430 || (type1 != NULL_TREE && type2 == NULL_TREE))
8431 return 0;
8432 else
8433 {
8434 /* Else, both have protocols, and we need to do the full
8435 comparison. It is possible that either type1 or type2
8436 contain some duplicate protocols in the list, so we can't
8437 even just compare list_length as a first check. */
8438 tree t;
8439
8440 for (t = type2; t; t = TREE_CHAIN (t))
8441 if (!lookup_protocol_in_reflist (type1, TREE_VALUE (t)))
8442 return 0;
8443
8444 for (t = type1; t; t = TREE_CHAIN (t))
8445 if (!lookup_protocol_in_reflist (type2, TREE_VALUE (t)))
8446 return 0;
8447
8448 return 1;
8449 }
8450 }
8451
8452 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
8453
8454 static int
8455 objc_types_share_size_and_alignment (tree type1, tree type2)
8456 {
8457 return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
8458 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2));
8459 }
8460
8461 /* Return 1 if PROTO1 is equivalent to PROTO2
8462 for purposes of method overloading. Ordinarily, the type signatures
8463 should match up exactly, unless STRICT is zero, in which case we
8464 shall allow differences in which the size and alignment of a type
8465 is the same. */
8466
8467 static int
8468 comp_proto_with_proto (tree proto1, tree proto2, int strict)
8469 {
8470 tree type1, type2;
8471
8472 /* The following test is needed in case there are hashing
8473 collisions. */
8474 if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2))
8475 return 0;
8476
8477 /* Compare return types. */
8478 type1 = TREE_VALUE (TREE_TYPE (proto1));
8479 type2 = TREE_VALUE (TREE_TYPE (proto2));
8480
8481 if (!objc_types_are_equivalent (type1, type2)
8482 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8483 return 0;
8484
8485 /* Compare argument types. */
8486
8487 /* The first argument (objc_object_type) is always the same, no need
8488 to compare. */
8489
8490 /* The second argument (objc_selector_type) is always the same, no
8491 need to compare. */
8492
8493 /* Compare the other arguments. */
8494 {
8495 tree arg1, arg2;
8496
8497 /* Compare METHOD_SEL_ARGS. */
8498 for (arg1 = METHOD_SEL_ARGS (proto1), arg2 = METHOD_SEL_ARGS (proto2);
8499 arg1 && arg2;
8500 arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2))
8501 {
8502 type1 = TREE_VALUE (TREE_TYPE (arg1));
8503 type2 = TREE_VALUE (TREE_TYPE (arg2));
8504
8505 /* FIXME: Do we need to decay argument types to compare them ? */
8506 type1 = objc_decay_parm_type (type1);
8507 type2 = objc_decay_parm_type (type2);
8508
8509 if (!objc_types_are_equivalent (type1, type2)
8510 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8511 return 0;
8512 }
8513
8514 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8515 both NULL. */
8516 if (arg1 != arg2)
8517 return 0;
8518
8519 /* Compare METHOD_ADD_ARGS. */
8520 if ((METHOD_ADD_ARGS (proto1) && !METHOD_ADD_ARGS (proto2))
8521 || (METHOD_ADD_ARGS (proto2) && !METHOD_ADD_ARGS (proto1)))
8522 return 0;
8523
8524 if (METHOD_ADD_ARGS (proto1))
8525 {
8526 for (arg1 = TREE_CHAIN (METHOD_ADD_ARGS (proto1)), arg2 = TREE_CHAIN (METHOD_ADD_ARGS (proto2));
8527 arg1 && arg2;
8528 arg1 = TREE_CHAIN (arg1), arg2 = TREE_CHAIN (arg2))
8529 {
8530 type1 = TREE_TYPE (TREE_VALUE (arg1));
8531 type2 = TREE_TYPE (TREE_VALUE (arg2));
8532
8533 /* FIXME: Do we need to decay argument types to compare them ? */
8534 type1 = objc_decay_parm_type (type1);
8535 type2 = objc_decay_parm_type (type2);
8536
8537 if (!objc_types_are_equivalent (type1, type2)
8538 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8539 return 0;
8540 }
8541 }
8542
8543 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8544 both NULL. */
8545 if (arg1 != arg2)
8546 return 0;
8547
8548 /* Compare METHOD_ADD_ARGS_ELLIPSIS_P. */
8549 if (METHOD_ADD_ARGS_ELLIPSIS_P (proto1) != METHOD_ADD_ARGS_ELLIPSIS_P (proto2))
8550 return 0;
8551 }
8552
8553 /* Success. */
8554 return 1;
8555 }
8556
8557 /* This routine returns true if TYPE is a valid objc object type,
8558 suitable for messaging; false otherwise. If 'accept_class' is
8559 'true', then a Class object is considered valid for messaging and
8560 'true' is returned if 'type' refers to a Class. If 'accept_class'
8561 is 'false', then a Class object is not considered valid for
8562 messaging and 'false' is returned in that case. */
8563
8564 static bool
8565 objc_type_valid_for_messaging (tree type, bool accept_classes)
8566 {
8567 if (!POINTER_TYPE_P (type))
8568 return false;
8569
8570 /* Remove the pointer indirection; don't remove more than one
8571 otherwise we'd consider "NSObject **" a valid type for messaging,
8572 which it isn't. */
8573 type = TREE_TYPE (type);
8574
8575 if (TREE_CODE (type) != RECORD_TYPE)
8576 return false;
8577
8578 if (objc_is_object_id (type))
8579 return true;
8580
8581 if (objc_is_class_id (type))
8582 return accept_classes;
8583
8584 if (TYPE_HAS_OBJC_INFO (type))
8585 return true;
8586
8587 return false;
8588 }
8589
8590 void
8591 objc_start_function (tree name, tree type, tree attrs,
8592 #ifdef OBJCPLUS
8593 tree params
8594 #else
8595 struct c_arg_info *params
8596 #endif
8597 )
8598 {
8599 tree fndecl = build_decl (input_location,
8600 FUNCTION_DECL, name, type);
8601
8602 #ifdef OBJCPLUS
8603 DECL_ARGUMENTS (fndecl) = params;
8604 DECL_INITIAL (fndecl) = error_mark_node;
8605 DECL_EXTERNAL (fndecl) = 0;
8606 TREE_STATIC (fndecl) = 1;
8607 retrofit_lang_decl (fndecl);
8608 cplus_decl_attributes (&fndecl, attrs, 0);
8609 start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT);
8610 #else
8611 current_function_returns_value = 0; /* Assume, until we see it does. */
8612 current_function_returns_null = 0;
8613 decl_attributes (&fndecl, attrs, 0);
8614 announce_function (fndecl);
8615 DECL_INITIAL (fndecl) = error_mark_node;
8616 DECL_EXTERNAL (fndecl) = 0;
8617 TREE_STATIC (fndecl) = 1;
8618 current_function_decl = pushdecl (fndecl);
8619 push_scope ();
8620 declare_parm_level ();
8621 DECL_RESULT (current_function_decl)
8622 = build_decl (input_location,
8623 RESULT_DECL, NULL_TREE,
8624 TREE_TYPE (TREE_TYPE (current_function_decl)));
8625 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1;
8626 DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1;
8627 start_fname_decls ();
8628 store_parm_decls_from (params);
8629 #endif
8630
8631 TREE_USED (current_function_decl) = 1;
8632 }
8633
8634 /* - Generate an identifier for the function. the format is "_n_cls",
8635 where 1 <= n <= nMethods, and cls is the name the implementation we
8636 are processing.
8637 - Install the return type from the method declaration.
8638 - If we have a prototype, check for type consistency. */
8639
8640 static void
8641 really_start_method (tree method,
8642 #ifdef OBJCPLUS
8643 tree parmlist
8644 #else
8645 struct c_arg_info *parmlist
8646 #endif
8647 )
8648 {
8649 tree ret_type, meth_type;
8650 tree method_id;
8651 const char *sel_name, *class_name, *cat_name;
8652 char *buf;
8653
8654 /* Synth the storage class & assemble the return type. */
8655 ret_type = TREE_VALUE (TREE_TYPE (method));
8656
8657 sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
8658 class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
8659 cat_name = ((TREE_CODE (objc_implementation_context)
8660 == CLASS_IMPLEMENTATION_TYPE)
8661 ? NULL
8662 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context)));
8663 method_slot++;
8664
8665 /* Make sure this is big enough for any plausible method label. */
8666 buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
8667 + (cat_name ? strlen (cat_name) : 0));
8668
8669 OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
8670 class_name, cat_name, sel_name, method_slot);
8671
8672 method_id = get_identifier (buf);
8673
8674 #ifdef OBJCPLUS
8675 /* Objective-C methods cannot be overloaded, so we don't need
8676 the type encoding appended. It looks bad anyway... */
8677 push_lang_context (lang_name_c);
8678 #endif
8679
8680 meth_type = build_function_type_for_method (ret_type, method, METHOD_DEF, 0);
8681 objc_start_function (method_id, meth_type, NULL_TREE, parmlist);
8682
8683 /* Set self_decl from the first argument. */
8684 self_decl = DECL_ARGUMENTS (current_function_decl);
8685
8686 /* Suppress unused warnings. */
8687 TREE_USED (self_decl) = 1;
8688 DECL_READ_P (self_decl) = 1;
8689 TREE_USED (DECL_CHAIN (self_decl)) = 1;
8690 DECL_READ_P (DECL_CHAIN (self_decl)) = 1;
8691 #ifdef OBJCPLUS
8692 pop_lang_context ();
8693 #endif
8694
8695 METHOD_DEFINITION (method) = current_function_decl;
8696
8697 /* Check consistency...start_function, pushdecl, duplicate_decls. */
8698
8699 if (implementation_template != objc_implementation_context)
8700 {
8701 tree proto
8702 = lookup_method_static (implementation_template,
8703 METHOD_SEL_NAME (method),
8704 ((TREE_CODE (method) == CLASS_METHOD_DECL)
8705 | OBJC_LOOKUP_NO_SUPER));
8706
8707 if (proto)
8708 {
8709 if (!comp_proto_with_proto (method, proto, 1))
8710 {
8711 bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL;
8712
8713 warning_at (DECL_SOURCE_LOCATION (method), 0,
8714 "conflicting types for %<%c%s%>",
8715 (type ? '-' : '+'),
8716 identifier_to_locale (gen_method_decl (method)));
8717 inform (DECL_SOURCE_LOCATION (proto),
8718 "previous declaration of %<%c%s%>",
8719 (type ? '-' : '+'),
8720 identifier_to_locale (gen_method_decl (proto)));
8721 }
8722 else
8723 {
8724 /* If the method in the @interface was deprecated, mark
8725 the implemented method as deprecated too. It should
8726 never be used for messaging (when the deprecation
8727 warnings are produced), but just in case. */
8728 if (TREE_DEPRECATED (proto))
8729 TREE_DEPRECATED (method) = 1;
8730
8731 /* If the method in the @interface was marked as
8732 'noreturn', mark the function implementing the method
8733 as 'noreturn' too. */
8734 TREE_THIS_VOLATILE (current_function_decl) = TREE_THIS_VOLATILE (proto);
8735 }
8736 }
8737 else
8738 {
8739 /* We have a method @implementation even though we did not
8740 see a corresponding @interface declaration (which is allowed
8741 by Objective-C rules). Go ahead and place the method in
8742 the @interface anyway, so that message dispatch lookups
8743 will see it. */
8744 tree interface = implementation_template;
8745
8746 if (TREE_CODE (objc_implementation_context)
8747 == CATEGORY_IMPLEMENTATION_TYPE)
8748 interface = lookup_category
8749 (interface,
8750 CLASS_SUPER_NAME (objc_implementation_context));
8751
8752 if (interface)
8753 objc_add_method (interface, copy_node (method),
8754 TREE_CODE (method) == CLASS_METHOD_DECL,
8755 /* is_optional= */ false);
8756 }
8757 }
8758 }
8759
8760 static void *UOBJC_SUPER_scope = 0;
8761
8762 /* _n_Method (id self, SEL sel, ...)
8763 {
8764 struct objc_super _S;
8765 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
8766 } */
8767
8768 static tree
8769 get_super_receiver (void)
8770 {
8771 if (objc_method_context)
8772 {
8773 tree super_expr, super_expr_list, class_expr;
8774 bool inst_meth;
8775 if (!UOBJC_SUPER_decl)
8776 {
8777 UOBJC_SUPER_decl = build_decl (input_location,
8778 VAR_DECL, get_identifier (TAG_SUPER),
8779 objc_super_template);
8780 /* This prevents `unused variable' warnings when compiling with -Wall. */
8781 TREE_USED (UOBJC_SUPER_decl) = 1;
8782 DECL_READ_P (UOBJC_SUPER_decl) = 1;
8783 lang_hooks.decls.pushdecl (UOBJC_SUPER_decl);
8784 finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE,
8785 NULL_TREE);
8786 UOBJC_SUPER_scope = objc_get_current_scope ();
8787 }
8788
8789 /* Set receiver to self. */
8790 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id);
8791 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8792 NOP_EXPR, input_location, self_decl,
8793 NULL_TREE);
8794 super_expr_list = super_expr;
8795
8796 /* Set class to begin searching. */
8797 /* Get the ident for the superclass class field & build a ref to it.
8798 ??? maybe we should just name the field the same for all runtimes. */
8799 super_expr = (*runtime.super_superclassfield_ident) ();
8800 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, super_expr);
8801
8802 gcc_assert (imp_list->imp_context == objc_implementation_context
8803 && imp_list->imp_template == implementation_template);
8804 inst_meth = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL);
8805
8806 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
8807 class_expr = (*runtime.get_class_super_ref) (input_location,
8808 imp_list, inst_meth);
8809 else
8810 /* We have a category. */
8811 {
8812 tree super_name = CLASS_SUPER_NAME (imp_list->imp_template);
8813 tree super_class;
8814
8815 /* Barf if super used in a category of a root object. */
8816 if (!super_name)
8817 {
8818 error ("no super class declared in interface for %qE",
8819 CLASS_NAME (imp_list->imp_template));
8820 return error_mark_node;
8821 }
8822
8823 super_class = (*runtime.get_category_super_ref) (input_location,
8824 imp_list, inst_meth);
8825 class_expr = build_c_cast (input_location,
8826 TREE_TYPE (super_expr), super_class);
8827 }
8828
8829 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8830 NOP_EXPR,
8831 input_location, class_expr, NULL_TREE);
8832
8833 super_expr_list = build_compound_expr (input_location,
8834 super_expr_list, super_expr);
8835
8836 super_expr = build_unary_op (input_location,
8837 ADDR_EXPR, UOBJC_SUPER_decl, 0);
8838 super_expr_list = build_compound_expr (input_location,
8839 super_expr_list, super_expr);
8840
8841 return super_expr_list;
8842 }
8843 else
8844 {
8845 error ("[super ...] must appear in a method context");
8846 return error_mark_node;
8847 }
8848 }
8849
8850 /* When exiting a scope, sever links to a 'super' declaration (if any)
8851 therein contained. */
8852
8853 void
8854 objc_clear_super_receiver (void)
8855 {
8856 if (objc_method_context
8857 && UOBJC_SUPER_scope == objc_get_current_scope ())
8858 {
8859 UOBJC_SUPER_decl = 0;
8860 UOBJC_SUPER_scope = 0;
8861 }
8862 }
8863
8864 void
8865 objc_finish_method_definition (tree fndecl)
8866 {
8867 /* We cannot validly inline ObjC methods, at least not without a language
8868 extension to declare that a method need not be dynamically
8869 dispatched, so suppress all thoughts of doing so. */
8870 DECL_UNINLINABLE (fndecl) = 1;
8871
8872 #ifndef OBJCPLUS
8873 /* The C++ front-end will have called finish_function() for us. */
8874 finish_function ();
8875 #endif
8876
8877 METHOD_ENCODING (objc_method_context)
8878 = encode_method_prototype (objc_method_context);
8879
8880 /* Required to implement _msgSuper. This must be done AFTER finish_function,
8881 since the optimizer may find "may be used before set" errors. */
8882 objc_method_context = NULL_TREE;
8883
8884 if (should_call_super_dealloc)
8885 warning (0, "method possibly missing a [super dealloc] call");
8886 }
8887
8888 /* Given a tree DECL node, produce a printable description of it in the given
8889 buffer, overwriting the buffer. */
8890
8891 static char *
8892 gen_declaration (tree decl)
8893 {
8894 errbuf[0] = '\0';
8895
8896 if (DECL_P (decl))
8897 {
8898 gen_type_name_0 (TREE_TYPE (decl));
8899
8900 if (DECL_NAME (decl))
8901 {
8902 if (!POINTER_TYPE_P (TREE_TYPE (decl)))
8903 strcat (errbuf, " ");
8904
8905 strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
8906 }
8907
8908 #ifdef OBJCPLUS
8909 tree w = DECL_BIT_FIELD_REPRESENTATIVE (decl);
8910 #else
8911 tree w = DECL_INITIAL (decl);
8912 #endif
8913 if (w)
8914 {
8915 STRIP_ANY_LOCATION_WRAPPER (w);
8916 if (TREE_CODE (w) == INTEGER_CST)
8917 sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
8918 TREE_INT_CST_LOW (w));
8919 }
8920 }
8921
8922 return errbuf;
8923 }
8924
8925 /* Given a tree TYPE node, produce a printable description of it in the given
8926 buffer, overwriting the buffer. */
8927
8928 static char *
8929 gen_type_name_0 (tree type)
8930 {
8931 tree orig = type, proto;
8932
8933 if (TYPE_P (type) && TYPE_NAME (type))
8934 type = TYPE_NAME (type);
8935 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
8936 {
8937 tree inner = TREE_TYPE (type);
8938
8939 while (TREE_CODE (inner) == ARRAY_TYPE)
8940 inner = TREE_TYPE (inner);
8941
8942 gen_type_name_0 (inner);
8943
8944 if (!POINTER_TYPE_P (inner))
8945 strcat (errbuf, " ");
8946
8947 if (POINTER_TYPE_P (type))
8948 strcat (errbuf, "*");
8949 else
8950 while (type != inner)
8951 {
8952 strcat (errbuf, "[");
8953
8954 if (TYPE_DOMAIN (type))
8955 {
8956 char sz[20];
8957
8958 sprintf (sz, HOST_WIDE_INT_PRINT_DEC,
8959 (TREE_INT_CST_LOW
8960 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1));
8961 strcat (errbuf, sz);
8962 }
8963
8964 strcat (errbuf, "]");
8965 type = TREE_TYPE (type);
8966 }
8967
8968 goto exit_function;
8969 }
8970
8971 if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type))
8972 type = DECL_NAME (type);
8973
8974 strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE
8975 ? IDENTIFIER_POINTER (type)
8976 : "");
8977
8978 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
8979 if (objc_is_id (orig))
8980 orig = TREE_TYPE (orig);
8981
8982 proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE;
8983
8984 if (proto)
8985 {
8986 strcat (errbuf, " <");
8987
8988 while (proto) {
8989 strcat (errbuf,
8990 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto))));
8991 proto = TREE_CHAIN (proto);
8992 strcat (errbuf, proto ? ", " : ">");
8993 }
8994 }
8995
8996 exit_function:
8997 return errbuf;
8998 }
8999
9000 static char *
9001 gen_type_name (tree type)
9002 {
9003 errbuf[0] = '\0';
9004
9005 return gen_type_name_0 (type);
9006 }
9007
9008 /* Given a method tree, put a printable description into the given
9009 buffer (overwriting) and return a pointer to the buffer. */
9010
9011 static char *
9012 gen_method_decl (tree method)
9013 {
9014 tree chain;
9015
9016 strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */
9017 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method)));
9018 strcat (errbuf, ")");
9019 chain = METHOD_SEL_ARGS (method);
9020
9021 if (chain)
9022 {
9023 /* We have a chain of keyword_decls. */
9024 do
9025 {
9026 if (KEYWORD_KEY_NAME (chain))
9027 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
9028
9029 strcat (errbuf, ":(");
9030 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain)));
9031 strcat (errbuf, ")");
9032
9033 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
9034 if ((chain = DECL_CHAIN (chain)))
9035 strcat (errbuf, " ");
9036 }
9037 while (chain);
9038
9039 if (METHOD_ADD_ARGS (method))
9040 {
9041 chain = TREE_CHAIN (METHOD_ADD_ARGS (method));
9042
9043 /* Know we have a chain of parm_decls. */
9044 while (chain)
9045 {
9046 strcat (errbuf, ", ");
9047 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain)));
9048 chain = TREE_CHAIN (chain);
9049 }
9050
9051 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
9052 strcat (errbuf, ", ...");
9053 }
9054 }
9055
9056 else
9057 /* We have a unary selector. */
9058 strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
9059
9060 return errbuf;
9061 }
9062 \f
9063 /* Debug info. */
9064
9065
9066 /* Dump an @interface declaration of the supplied class CHAIN to the
9067 supplied file FP. Used to implement the -gen-decls option (which
9068 prints out an @interface declaration of all classes compiled in
9069 this run); potentially useful for debugging the compiler too. */
9070 void
9071 dump_interface (FILE *fp, tree chain)
9072 {
9073 /* FIXME: A heap overflow here whenever a method (or ivar)
9074 declaration is so long that it doesn't fit in the buffer. The
9075 code and all the related functions should be rewritten to avoid
9076 using fixed size buffers. */
9077 const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
9078 tree ivar_decls = CLASS_RAW_IVARS (chain);
9079 tree nst_methods = CLASS_NST_METHODS (chain);
9080 tree cls_methods = CLASS_CLS_METHODS (chain);
9081
9082 fprintf (fp, "\n@interface %s", my_name);
9083
9084 /* CLASS_SUPER_NAME is used to store the superclass name for
9085 classes, and the category name for categories. */
9086 if (CLASS_SUPER_NAME (chain))
9087 {
9088 const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
9089
9090 switch (TREE_CODE (chain))
9091 {
9092 case CATEGORY_IMPLEMENTATION_TYPE:
9093 case CATEGORY_INTERFACE_TYPE:
9094 fprintf (fp, " (%s)\n", name);
9095 break;
9096 default:
9097 fprintf (fp, " : %s\n", name);
9098 break;
9099 }
9100 }
9101 else
9102 fprintf (fp, "\n");
9103
9104 /* FIXME - the following doesn't seem to work at the moment. */
9105 if (ivar_decls)
9106 {
9107 fprintf (fp, "{\n");
9108 do
9109 {
9110 fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls));
9111 ivar_decls = TREE_CHAIN (ivar_decls);
9112 }
9113 while (ivar_decls);
9114 fprintf (fp, "}\n");
9115 }
9116
9117 while (nst_methods)
9118 {
9119 fprintf (fp, "- %s;\n", gen_method_decl (nst_methods));
9120 nst_methods = TREE_CHAIN (nst_methods);
9121 }
9122
9123 while (cls_methods)
9124 {
9125 fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods));
9126 cls_methods = TREE_CHAIN (cls_methods);
9127 }
9128
9129 fprintf (fp, "@end\n");
9130 }
9131
9132 #if 0
9133 /* Produce the pretty printing for an Objective-C method. This is
9134 currently unused, but could be handy while reorganizing the pretty
9135 printing to be more robust. */
9136 static const char *
9137 objc_pretty_print_method (bool is_class_method,
9138 const char *class_name,
9139 const char *category_name,
9140 const char *selector)
9141 {
9142 if (category_name)
9143 {
9144 char *result = XNEWVEC (char, strlen (class_name) + strlen (category_name)
9145 + strlen (selector) + 7);
9146
9147 if (is_class_method)
9148 sprintf (result, "+[%s(%s) %s]", class_name, category_name, selector);
9149 else
9150 sprintf (result, "-[%s(%s) %s]", class_name, category_name, selector);
9151
9152 return result;
9153 }
9154 else
9155 {
9156 char *result = XNEWVEC (char, strlen (class_name)
9157 + strlen (selector) + 5);
9158
9159 if (is_class_method)
9160 sprintf (result, "+[%s %s]", class_name, selector);
9161 else
9162 sprintf (result, "-[%s %s]", class_name, selector);
9163
9164 return result;
9165 }
9166 }
9167 #endif
9168
9169 /* Demangle function for Objective-C. Attempt to demangle the
9170 function name associated with a method (eg, going from
9171 "_i_NSObject__class" to "-[NSObject class]"); usually for the
9172 purpose of pretty printing or error messages. Return the demangled
9173 name, or NULL if the string is not an Objective-C mangled method
9174 name.
9175
9176 Because of how the mangling is done, any method that has a '_' in
9177 its original name is at risk of being demangled incorrectly. In
9178 some cases there are multiple valid ways to demangle a method name
9179 and there is no way we can decide.
9180
9181 TODO: objc_demangle() can't always get it right; the right way to
9182 get this correct for all method names would be to store the
9183 Objective-C method name somewhere in the function decl. Then,
9184 there is no demangling to do; we'd just pull the method name out of
9185 the decl. As an additional bonus, when printing error messages we
9186 could check for such a method name, and if we find it, we know the
9187 function is actually an Objective-C method and we could print error
9188 messages saying "In method '+[NSObject class]" instead of "In
9189 function '+[NSObject class]" as we do now. */
9190 static const char *
9191 objc_demangle (const char *mangled)
9192 {
9193 char *demangled, *cp;
9194
9195 /* First of all, if the name is too short it can't be an Objective-C
9196 mangled method name. */
9197 if (mangled[0] == '\0' || mangled[1] == '\0' || mangled[2] == '\0')
9198 return NULL;
9199
9200 /* If the name looks like an already demangled one, return it
9201 unchanged. This should only happen on Darwin, where method names
9202 are mangled differently into a pretty-print form (such as
9203 '+[NSObject class]', see darwin.h). In that case, demangling is
9204 a no-op, but we need to return the demangled name if it was an
9205 ObjC one, and return NULL if not. We should be safe as no C/C++
9206 function can start with "-[" or "+[". */
9207 if ((mangled[0] == '-' || mangled[0] == '+')
9208 && (mangled[1] == '['))
9209 return mangled;
9210
9211 if (mangled[0] == '_' &&
9212 (mangled[1] == 'i' || mangled[1] == 'c') &&
9213 mangled[2] == '_')
9214 {
9215 cp = demangled = XNEWVEC (char, strlen(mangled) + 2);
9216 if (mangled[1] == 'i')
9217 *cp++ = '-'; /* for instance method */
9218 else
9219 *cp++ = '+'; /* for class method */
9220 *cp++ = '['; /* opening left brace */
9221 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
9222 while (*cp && *cp == '_')
9223 cp++; /* skip any initial underbars in class name */
9224 cp = strchr(cp, '_'); /* find first non-initial underbar */
9225 if (cp == NULL)
9226 {
9227 free(demangled); /* not mangled name */
9228 return NULL;
9229 }
9230 if (cp[1] == '_') /* easy case: no category name */
9231 {
9232 *cp++ = ' '; /* replace two '_' with one ' ' */
9233 strcpy(cp, mangled + (cp - demangled) + 2);
9234 }
9235 else
9236 {
9237 *cp++ = '('; /* less easy case: category name */
9238 cp = strchr(cp, '_');
9239 if (cp == 0)
9240 {
9241 free(demangled); /* not mangled name */
9242 return NULL;
9243 }
9244 *cp++ = ')';
9245 *cp++ = ' '; /* overwriting 1st char of method name... */
9246 strcpy(cp, mangled + (cp - demangled)); /* get it back */
9247 }
9248 /* Now we have the method name. We need to generally replace
9249 '_' with ':' but trying to preserve '_' if it could only have
9250 been in the mangled string because it was already in the
9251 original name. In cases where it's ambiguous, we assume that
9252 any '_' originated from a ':'. */
9253
9254 /* Initial '_'s in method name can't have been generating by
9255 converting ':'s. Skip them. */
9256 while (*cp && *cp == '_')
9257 cp++;
9258
9259 /* If the method name does not end with '_', then it has no
9260 arguments and there was no replacement of ':'s with '_'s
9261 during mangling. Check for that case, and skip any
9262 replacement if so. This at least guarantees that methods
9263 with no arguments are always demangled correctly (unless the
9264 original name ends with '_'). */
9265 if (*(mangled + strlen (mangled) - 1) != '_')
9266 {
9267 /* Skip to the end. */
9268 for (; *cp; cp++)
9269 ;
9270 }
9271 else
9272 {
9273 /* Replace remaining '_' with ':'. This may get it wrong if
9274 there were '_'s in the original name. In most cases it
9275 is impossible to disambiguate. */
9276 for (; *cp; cp++)
9277 if (*cp == '_')
9278 *cp = ':';
9279 }
9280 *cp++ = ']'; /* closing right brace */
9281 *cp++ = 0; /* string terminator */
9282 return demangled;
9283 }
9284 else
9285 return NULL; /* not an objc mangled name */
9286 }
9287
9288 /* Try to pretty-print a decl. If the 'decl' is an Objective-C
9289 specific decl, return the printable name for it. If not, return
9290 NULL. */
9291 const char *
9292 objc_maybe_printable_name (tree decl, int v ATTRIBUTE_UNUSED)
9293 {
9294 switch (TREE_CODE (decl))
9295 {
9296 case FUNCTION_DECL:
9297 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)));
9298
9299 /* The following happens when we are printing a deprecation
9300 warning for a method. The warn_deprecation() will end up
9301 trying to print the decl for INSTANCE_METHOD_DECL or
9302 CLASS_METHOD_DECL. It would be nice to be able to print
9303 "-[NSObject autorelease] is deprecated", but to do that, we'd
9304 need to store the class and method name in the method decl,
9305 which we currently don't do. For now, just return the name
9306 of the method. We don't return NULL, because that may
9307 trigger further attempts to pretty-print the decl in C/C++,
9308 but they wouldn't know how to pretty-print it. */
9309 case INSTANCE_METHOD_DECL:
9310 case CLASS_METHOD_DECL:
9311 return IDENTIFIER_POINTER (DECL_NAME (decl));
9312 /* This happens when printing a deprecation warning for a
9313 property. We may want to consider some sort of pretty
9314 printing (eg, include the class name where it was declared
9315 ?). */
9316 case PROPERTY_DECL:
9317 return IDENTIFIER_POINTER (PROPERTY_NAME (decl));
9318 default:
9319 return NULL;
9320 }
9321 }
9322
9323 /* Return a printable name for 'decl'. This first tries
9324 objc_maybe_printable_name(), and if that fails, it returns the name
9325 in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for
9326 Objective-C; in Objective-C++, setting the hook is not enough
9327 because lots of C++ Front-End code calls cxx_printable_name,
9328 dump_decl and other C++ functions directly. So instead we have
9329 modified dump_decl to call objc_maybe_printable_name directly. */
9330 const char *
9331 objc_printable_name (tree decl, int v)
9332 {
9333 const char *demangled_name = objc_maybe_printable_name (decl, v);
9334
9335 if (demangled_name != NULL)
9336 return demangled_name;
9337 else
9338 return IDENTIFIER_POINTER (DECL_NAME (decl));
9339 }
9340
9341 /* Routine is called to issue diagnostic when reference to a private
9342 ivar is made and no other variable with same name is found in
9343 current scope. */
9344 bool
9345 objc_diagnose_private_ivar (tree id)
9346 {
9347 tree ivar;
9348 if (!objc_method_context)
9349 return false;
9350 ivar = is_ivar (objc_ivar_chain, id);
9351 if (ivar && is_private (ivar))
9352 {
9353 error ("instance variable %qs is declared private",
9354 IDENTIFIER_POINTER (id));
9355 return true;
9356 }
9357 return false;
9358 }
9359
9360 /* Look up ID as an instance variable. OTHER contains the result of
9361 the C or C++ lookup, which we may want to use instead. */
9362 /* To use properties inside an instance method, use self.property. */
9363 tree
9364 objc_lookup_ivar (tree other, tree id)
9365 {
9366 tree ivar;
9367
9368 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
9369 if (!objc_method_context)
9370 return other;
9371
9372 if (!strcmp (IDENTIFIER_POINTER (id), "super"))
9373 /* We have a message to super. */
9374 return get_super_receiver ();
9375
9376 /* In a class method, look up an instance variable only as a last
9377 resort. */
9378 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
9379 && other && other != error_mark_node)
9380 return other;
9381
9382 /* Don't look up the ivar if the user has explicitly advised against
9383 it with -fno-local-ivars. */
9384
9385 if (!flag_local_ivars)
9386 return other;
9387
9388 /* Look up the ivar, but do not use it if it is not accessible. */
9389 ivar = is_ivar (objc_ivar_chain, id);
9390
9391 if (!ivar || is_private (ivar))
9392 return other;
9393
9394 /* In an instance method, a local variable (or parameter) may hide the
9395 instance variable. */
9396 if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
9397 && other && other != error_mark_node
9398 #ifdef OBJCPLUS
9399 && CP_DECL_CONTEXT (other) != global_namespace)
9400 #else
9401 && !DECL_FILE_SCOPE_P (other))
9402 #endif
9403 {
9404 if (warn_shadow_ivar == 1 || (warn_shadow && warn_shadow_ivar != 0)) {
9405 warning (warn_shadow_ivar ? OPT_Wshadow_ivar : OPT_Wshadow,
9406 "local declaration of %qE hides instance variable", id);
9407 }
9408
9409 return other;
9410 }
9411
9412 /* At this point, we are either in an instance method with no obscuring
9413 local definitions, or in a class method with no alternate definitions
9414 at all. */
9415 return build_ivar_reference (id);
9416 }
9417
9418 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
9419 needs to be done if we are calling a function through a cast. */
9420
9421 tree
9422 objc_rewrite_function_call (tree function, tree first_param)
9423 {
9424 if (TREE_CODE (function) == NOP_EXPR
9425 && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR
9426 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0))
9427 == FUNCTION_DECL)
9428 {
9429 function = build3 (OBJ_TYPE_REF, TREE_TYPE (function),
9430 TREE_OPERAND (function, 0),
9431 first_param, size_zero_node);
9432 }
9433
9434 return function;
9435 }
9436
9437 /* This is called to "gimplify" a PROPERTY_REF node. It builds the
9438 corresponding 'getter' function call. Note that we assume the
9439 PROPERTY_REF to be valid since we generated it while parsing. */
9440 static void
9441 objc_gimplify_property_ref (tree *expr_p)
9442 {
9443 tree getter = PROPERTY_REF_GETTER_CALL (*expr_p);
9444 tree call_exp;
9445
9446 if (getter == NULL_TREE)
9447 {
9448 tree property_decl = PROPERTY_REF_PROPERTY_DECL (*expr_p);
9449 /* This can happen if DECL_ARTIFICIAL (*expr_p), but
9450 should be impossible for real properties, which always
9451 have a getter. */
9452 error_at (EXPR_LOCATION (*expr_p), "no %qs getter found",
9453 IDENTIFIER_POINTER (PROPERTY_NAME (property_decl)));
9454 /* Try to recover from the error to prevent an ICE. We take
9455 zero and cast it to the type of the property. */
9456 *expr_p = convert (TREE_TYPE (property_decl),
9457 integer_zero_node);
9458 return;
9459 }
9460
9461 if (PROPERTY_REF_DEPRECATED_GETTER (*expr_p))
9462 {
9463 /* PROPERTY_REF_DEPRECATED_GETTER contains the method prototype
9464 that is deprecated. */
9465 warn_deprecated_use (PROPERTY_REF_DEPRECATED_GETTER (*expr_p),
9466 NULL_TREE);
9467 }
9468
9469 call_exp = getter;
9470 #ifdef OBJCPLUS
9471 /* In C++, a getter which returns an aggregate value results in a
9472 target_expr which initializes a temporary to the call
9473 expression. */
9474 if (TREE_CODE (getter) == TARGET_EXPR)
9475 {
9476 gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter)));
9477 gcc_assert (TREE_CODE (TREE_OPERAND (getter, 0)) == VAR_DECL);
9478 call_exp = TREE_OPERAND (getter, 1);
9479 }
9480 #endif
9481 gcc_assert (TREE_CODE (call_exp) == CALL_EXPR);
9482
9483 *expr_p = call_exp;
9484 }
9485
9486 /* This is called when "gimplifying" the trees. We need to gimplify
9487 the Objective-C/Objective-C++ specific trees, then hand over the
9488 process to C/C++. */
9489 int
9490 objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9491 {
9492 enum tree_code code = TREE_CODE (*expr_p);
9493 switch (code)
9494 {
9495 /* Look for the special case of OBJC_TYPE_REF with the address
9496 of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend
9497 or one of its cousins). */
9498 case OBJ_TYPE_REF:
9499 if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR
9500 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0))
9501 == FUNCTION_DECL)
9502 {
9503 enum gimplify_status r0, r1;
9504
9505 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
9506 value of the OBJ_TYPE_REF, so force them to be emitted
9507 during subexpression evaluation rather than after the
9508 OBJ_TYPE_REF. This permits objc_msgSend calls in
9509 Objective C to use direct rather than indirect calls when
9510 the object expression has a postincrement. */
9511 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL,
9512 is_gimple_val, fb_rvalue);
9513 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
9514 is_gimple_val, fb_rvalue);
9515
9516 return MIN (r0, r1);
9517 }
9518 break;
9519 case PROPERTY_REF:
9520 objc_gimplify_property_ref (expr_p);
9521 /* Do not return yet; let C/C++ gimplify the resulting expression. */
9522 break;
9523 default:
9524 break;
9525 }
9526
9527 #ifdef OBJCPLUS
9528 return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p);
9529 #else
9530 return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
9531 #endif
9532 }
9533
9534 /* --- FAST ENUMERATION --- */
9535 /* Begin code generation for fast enumeration (foreach) ... */
9536
9537 /* Defines
9538
9539 struct __objcFastEnumerationState
9540 {
9541 unsigned long state;
9542 id *itemsPtr;
9543 unsigned long *mutationsPtr;
9544 unsigned long extra[5];
9545 };
9546
9547 Confusingly enough, NSFastEnumeration is then defined by libraries
9548 to be the same structure.
9549 */
9550
9551 static void
9552 build_fast_enumeration_state_template (void)
9553 {
9554 tree decls, *chain = NULL;
9555
9556 /* { */
9557 objc_fast_enumeration_state_template = objc_start_struct (get_identifier
9558 (TAG_FAST_ENUMERATION_STATE));
9559
9560 /* unsigned long state; */
9561 decls = add_field_decl (long_unsigned_type_node, "state", &chain);
9562
9563 /* id *itemsPtr; */
9564 add_field_decl (build_pointer_type (objc_object_type),
9565 "itemsPtr", &chain);
9566
9567 /* unsigned long *mutationsPtr; */
9568 add_field_decl (build_pointer_type (long_unsigned_type_node),
9569 "mutationsPtr", &chain);
9570
9571 /* unsigned long extra[5]; */
9572 add_field_decl (build_sized_array_type (long_unsigned_type_node, 5),
9573 "extra", &chain);
9574
9575 /* } */
9576 objc_finish_struct (objc_fast_enumeration_state_template, decls);
9577 }
9578
9579 /*
9580 'objc_finish_foreach_loop()' generates the code for an Objective-C
9581 foreach loop. The 'location' argument is the location of the 'for'
9582 that starts the loop. The 'object_expression' is the expression of
9583 the 'object' that iterates; the 'collection_expression' is the
9584 expression of the collection that we iterate over (we need to make
9585 sure we evaluate this only once); the 'for_body' is the set of
9586 statements to be executed in each iteration; 'break_label' and
9587 'continue_label' are the break and continue labels which we need to
9588 emit since the <statements> may be jumping to 'break_label' (if they
9589 contain 'break') or to 'continue_label' (if they contain
9590 'continue').
9591
9592 The syntax is
9593
9594 for (<object expression> in <collection expression>)
9595 <statements>
9596
9597 which is compiled into the following blurb:
9598
9599 {
9600 id __objc_foreach_collection;
9601 __objc_fast_enumeration_state __objc_foreach_enum_state;
9602 unsigned long __objc_foreach_batchsize;
9603 id __objc_foreach_items[16];
9604 __objc_foreach_collection = <collection expression>;
9605 __objc_foreach_enum_state = { 0 };
9606 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9607
9608 if (__objc_foreach_batchsize == 0)
9609 <object expression> = nil;
9610 else
9611 {
9612 unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr;
9613 next_batch:
9614 {
9615 unsigned long __objc_foreach_index;
9616 __objc_foreach_index = 0;
9617
9618 next_object:
9619 if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>);
9620 <object expression> = enumState.itemsPtr[__objc_foreach_index];
9621 <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label]
9622
9623 continue_label:
9624 __objc_foreach_index++;
9625 if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object;
9626 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9627 }
9628 if (__objc_foreach_batchsize != 0) goto next_batch;
9629 <object expression> = nil;
9630 break_label:
9631 }
9632 }
9633
9634 'statements' may contain a 'continue' or 'break' instruction, which
9635 the user expects to 'continue' or 'break' the entire foreach loop.
9636 We are provided the labels that 'break' and 'continue' jump to, so
9637 we place them where we want them to jump to when they pick them.
9638
9639 Optimization TODO: we could cache the IMP of
9640 countByEnumeratingWithState:objects:count:.
9641 */
9642
9643 /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */
9644 /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */
9645
9646 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
9647 #include "tree-pretty-print.h"
9648 #endif
9649
9650 void
9651 objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body,
9652 tree break_label, tree continue_label)
9653 {
9654 /* A tree representing the __objcFastEnumerationState struct type,
9655 or NSFastEnumerationState struct, whatever we are using. */
9656 tree objc_fast_enumeration_state_type;
9657
9658 /* The trees representing the declarations of each of the local variables. */
9659 tree objc_foreach_collection_decl;
9660 tree objc_foreach_enum_state_decl;
9661 tree objc_foreach_items_decl;
9662 tree objc_foreach_batchsize_decl;
9663 tree objc_foreach_mutations_pointer_decl;
9664 tree objc_foreach_index_decl;
9665
9666 /* A tree representing the selector countByEnumeratingWithState:objects:count:. */
9667 tree selector_name;
9668
9669 /* A tree representing the local bind. */
9670 tree bind;
9671
9672 /* A tree representing the external 'if (__objc_foreach_batchsize)' */
9673 tree first_if;
9674
9675 /* A tree representing the 'else' part of 'first_if' */
9676 tree first_else;
9677
9678 /* A tree representing the 'next_batch' label. */
9679 tree next_batch_label_decl;
9680
9681 /* A tree representing the binding after the 'next_batch' label. */
9682 tree next_batch_bind;
9683
9684 /* A tree representing the 'next_object' label. */
9685 tree next_object_label_decl;
9686
9687 /* Temporary variables. */
9688 tree t;
9689 int i;
9690
9691 if (flag_objc1_only)
9692 error_at (location, "fast enumeration is not available in Objective-C 1.0");
9693
9694 if (object_expression == error_mark_node)
9695 return;
9696
9697 if (collection_expression == error_mark_node)
9698 return;
9699
9700 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression), true))
9701 {
9702 error_at (location, "iterating variable in fast enumeration is not an object");
9703 return;
9704 }
9705
9706 if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression), true))
9707 {
9708 error_at (location, "collection in fast enumeration is not an object");
9709 return;
9710 }
9711
9712 /* TODO: Check that object_expression is either a variable
9713 declaration, or an lvalue. */
9714
9715 /* This kludge is an idea from apple. We use the
9716 __objcFastEnumerationState struct implicitly defined by the
9717 compiler, unless a NSFastEnumerationState struct has been defined
9718 (by a Foundation library such as GNUstep Base) in which case, we
9719 use that one.
9720 */
9721 objc_fast_enumeration_state_type = objc_fast_enumeration_state_template;
9722 {
9723 tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState"));
9724
9725 if (objc_NSFastEnumeration_type)
9726 {
9727 /* TODO: We really need to check that
9728 objc_NSFastEnumeration_type is the same as ours! */
9729 if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL)
9730 {
9731 /* If it's a typedef, use the original type. */
9732 if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type))
9733 objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type);
9734 else
9735 objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type);
9736 }
9737 }
9738 }
9739
9740 /* { */
9741 /* Done by c-parser.c. */
9742
9743 /* type object; */
9744 /* Done by c-parser.c. */
9745
9746 /* Disable warnings that 'object' is unused. For example the code
9747
9748 for (id object in collection)
9749 i++;
9750
9751 which can be used to count how many objects there are in the
9752 collection is fine and should generate no warnings even if
9753 'object' is technically unused. */
9754 TREE_USED (object_expression) = 1;
9755 if (DECL_P (object_expression))
9756 DECL_READ_P (object_expression) = 1;
9757
9758 /* id __objc_foreach_collection */
9759 objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection");
9760
9761 /* __objcFastEnumerationState __objc_foreach_enum_state; */
9762 objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state");
9763 TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl;
9764
9765 /* id __objc_foreach_items[16]; */
9766 objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items");
9767 TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl;
9768
9769 /* unsigned long __objc_foreach_batchsize; */
9770 objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize");
9771 TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl;
9772
9773 /* Generate the local variable binding. */
9774 bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL);
9775 SET_EXPR_LOCATION (bind, location);
9776 TREE_SIDE_EFFECTS (bind) = 1;
9777
9778 /* __objc_foreach_collection = <collection expression>; */
9779 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression);
9780 SET_EXPR_LOCATION (t, location);
9781 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9782 /* We have used 'collection_expression'. */
9783 mark_exp_read (collection_expression);
9784
9785 /* __objc_foreach_enum_state.state = 0; */
9786 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9787 get_identifier ("state")),
9788 build_int_cst (long_unsigned_type_node, 0));
9789 SET_EXPR_LOCATION (t, location);
9790 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9791
9792 /* __objc_foreach_enum_state.itemsPtr = NULL; */
9793 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9794 get_identifier ("itemsPtr")),
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.mutationsPtr = NULL; */
9800 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9801 get_identifier ("mutationsPtr")),
9802 null_pointer_node);
9803 SET_EXPR_LOCATION (t, location);
9804 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9805
9806 /* __objc_foreach_enum_state.extra[0] = 0; */
9807 /* __objc_foreach_enum_state.extra[1] = 0; */
9808 /* __objc_foreach_enum_state.extra[2] = 0; */
9809 /* __objc_foreach_enum_state.extra[3] = 0; */
9810 /* __objc_foreach_enum_state.extra[4] = 0; */
9811 for (i = 0; i < 5 ; i++)
9812 {
9813 t = build2 (MODIFY_EXPR, void_type_node,
9814 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9815 get_identifier ("extra")),
9816 build_int_cst (NULL_TREE, i)),
9817 build_int_cst (long_unsigned_type_node, 0));
9818 SET_EXPR_LOCATION (t, location);
9819 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9820 }
9821
9822 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
9823 selector_name = get_identifier ("countByEnumeratingWithState:objects:count:");
9824 #ifdef OBJCPLUS
9825 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9826 /* Parameters. */
9827 tree_cons /* &__objc_foreach_enum_state */
9828 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9829 tree_cons /* __objc_foreach_items */
9830 (NULL_TREE, objc_foreach_items_decl,
9831 tree_cons /* 16 */
9832 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9833 #else
9834 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
9835 {
9836 struct c_expr array;
9837 array.value = objc_foreach_items_decl;
9838 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9839 /* Parameters. */
9840 tree_cons /* &__objc_foreach_enum_state */
9841 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9842 tree_cons /* __objc_foreach_items */
9843 (NULL_TREE, default_function_array_conversion (location, array).value,
9844 tree_cons /* 16 */
9845 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9846 }
9847 #endif
9848 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
9849 convert (long_unsigned_type_node, t));
9850 SET_EXPR_LOCATION (t, location);
9851 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9852
9853 /* if (__objc_foreach_batchsize == 0) */
9854 first_if = build3 (COND_EXPR, void_type_node,
9855 /* Condition. */
9856 c_fully_fold
9857 (c_common_truthvalue_conversion
9858 (location,
9859 build_binary_op (location,
9860 EQ_EXPR,
9861 objc_foreach_batchsize_decl,
9862 build_int_cst (long_unsigned_type_node, 0), 1)),
9863 false, NULL),
9864 /* Then block (we fill it in later). */
9865 NULL_TREE,
9866 /* Else block (we fill it in later). */
9867 NULL_TREE);
9868 SET_EXPR_LOCATION (first_if, location);
9869 append_to_statement_list (first_if, &BIND_EXPR_BODY (bind));
9870
9871 /* then <object expression> = nil; */
9872 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
9873 SET_EXPR_LOCATION (t, location);
9874 COND_EXPR_THEN (first_if) = t;
9875
9876 /* Now we build the 'else' part of the if; once we finish building
9877 it, we attach it to first_if as the 'else' part. */
9878
9879 /* else */
9880 /* { */
9881
9882 /* unsigned long __objc_foreach_mutations_pointer; */
9883 objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer");
9884
9885 /* Generate the local variable binding. */
9886 first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL);
9887 SET_EXPR_LOCATION (first_else, location);
9888 TREE_SIDE_EFFECTS (first_else) = 1;
9889
9890 /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */
9891 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl,
9892 build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9893 get_identifier ("mutationsPtr")),
9894 RO_UNARY_STAR));
9895 SET_EXPR_LOCATION (t, location);
9896 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
9897
9898 /* next_batch: */
9899 next_batch_label_decl = create_artificial_label (location);
9900 t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl);
9901 SET_EXPR_LOCATION (t, location);
9902 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
9903
9904 /* { */
9905
9906 /* unsigned long __objc_foreach_index; */
9907 objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index");
9908
9909 /* Generate the local variable binding. */
9910 next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL);
9911 SET_EXPR_LOCATION (next_batch_bind, location);
9912 TREE_SIDE_EFFECTS (next_batch_bind) = 1;
9913 append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else));
9914
9915 /* __objc_foreach_index = 0; */
9916 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
9917 build_int_cst (long_unsigned_type_node, 0));
9918 SET_EXPR_LOCATION (t, location);
9919 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9920
9921 /* next_object: */
9922 next_object_label_decl = create_artificial_label (location);
9923 t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl);
9924 SET_EXPR_LOCATION (t, location);
9925 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9926
9927 /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */
9928 t = build3 (COND_EXPR, void_type_node,
9929 /* Condition. */
9930 c_fully_fold
9931 (c_common_truthvalue_conversion
9932 (location,
9933 build_binary_op
9934 (location,
9935 NE_EXPR,
9936 objc_foreach_mutations_pointer_decl,
9937 build_indirect_ref (location,
9938 objc_build_component_ref (objc_foreach_enum_state_decl,
9939 get_identifier ("mutationsPtr")),
9940 RO_UNARY_STAR), 1)),
9941 false, NULL),
9942 /* Then block. */
9943 build_function_call (input_location,
9944 objc_enumeration_mutation_decl,
9945 tree_cons (NULL, collection_expression, NULL)),
9946 /* Else block. */
9947 NULL_TREE);
9948 SET_EXPR_LOCATION (t, location);
9949 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9950
9951 /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */
9952 t = build2 (MODIFY_EXPR, void_type_node, object_expression,
9953 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9954 get_identifier ("itemsPtr")),
9955 objc_foreach_index_decl));
9956 SET_EXPR_LOCATION (t, location);
9957 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9958
9959 /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */
9960 append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind));
9961
9962 /* continue_label: */
9963 if (continue_label)
9964 {
9965 t = build1 (LABEL_EXPR, void_type_node, continue_label);
9966 SET_EXPR_LOCATION (t, location);
9967 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9968 }
9969
9970 /* __objc_foreach_index++; */
9971 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
9972 build_binary_op (location,
9973 PLUS_EXPR,
9974 objc_foreach_index_decl,
9975 build_int_cst (long_unsigned_type_node, 1), 1));
9976 SET_EXPR_LOCATION (t, location);
9977 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9978
9979 /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */
9980 t = build3 (COND_EXPR, void_type_node,
9981 /* Condition. */
9982 c_fully_fold
9983 (c_common_truthvalue_conversion
9984 (location,
9985 build_binary_op (location,
9986 LT_EXPR,
9987 objc_foreach_index_decl,
9988 objc_foreach_batchsize_decl, 1)),
9989 false, NULL),
9990 /* Then block. */
9991 build1 (GOTO_EXPR, void_type_node, next_object_label_decl),
9992 /* Else block. */
9993 NULL_TREE);
9994 SET_EXPR_LOCATION (t, location);
9995 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9996
9997 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
9998 #ifdef OBJCPLUS
9999 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
10000 /* Parameters. */
10001 tree_cons /* &__objc_foreach_enum_state */
10002 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
10003 tree_cons /* __objc_foreach_items */
10004 (NULL_TREE, objc_foreach_items_decl,
10005 tree_cons /* 16 */
10006 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
10007 #else
10008 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
10009 {
10010 struct c_expr array;
10011 array.value = objc_foreach_items_decl;
10012 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
10013 /* Parameters. */
10014 tree_cons /* &__objc_foreach_enum_state */
10015 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
10016 tree_cons /* __objc_foreach_items */
10017 (NULL_TREE, default_function_array_conversion (location, array).value,
10018 tree_cons /* 16 */
10019 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
10020 }
10021 #endif
10022 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
10023 convert (long_unsigned_type_node, t));
10024 SET_EXPR_LOCATION (t, location);
10025 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10026
10027 /* } */
10028
10029 /* if (__objc_foreach_batchsize != 0) goto next_batch; */
10030 t = build3 (COND_EXPR, void_type_node,
10031 /* Condition. */
10032 c_fully_fold
10033 (c_common_truthvalue_conversion
10034 (location,
10035 build_binary_op (location,
10036 NE_EXPR,
10037 objc_foreach_batchsize_decl,
10038 build_int_cst (long_unsigned_type_node, 0), 1)),
10039 false, NULL),
10040 /* Then block. */
10041 build1 (GOTO_EXPR, void_type_node, next_batch_label_decl),
10042 /* Else block. */
10043 NULL_TREE);
10044 SET_EXPR_LOCATION (t, location);
10045 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10046
10047 /* <object expression> = nil; */
10048 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
10049 SET_EXPR_LOCATION (t, location);
10050 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10051
10052 /* break_label: */
10053 if (break_label)
10054 {
10055 t = build1 (LABEL_EXPR, void_type_node, break_label);
10056 SET_EXPR_LOCATION (t, location);
10057 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10058 }
10059
10060 /* } */
10061 COND_EXPR_ELSE (first_if) = first_else;
10062
10063 /* Do the whole thing. */
10064 add_stmt (bind);
10065
10066 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
10067 /* This will print to stderr the whole blurb generated by the
10068 compiler while compiling (assuming the compiler doesn't crash
10069 before getting here).
10070 */
10071 debug_generic_stmt (bind);
10072 #endif
10073
10074 /* } */
10075 /* Done by c-parser.c */
10076 }
10077
10078 /* --- SUPPORT FOR FORMAT ARG CHECKING --- */
10079 /* Return true if we have an NxString object pointer. */
10080
10081 bool
10082 objc_string_ref_type_p (tree strp)
10083 {
10084 tree tmv;
10085 if (!strp || TREE_CODE (strp) != POINTER_TYPE)
10086 return false;
10087
10088 tmv = TYPE_MAIN_VARIANT (TREE_TYPE (strp));
10089 tmv = OBJC_TYPE_NAME (tmv);
10090 return (tmv
10091 && TREE_CODE (tmv) == IDENTIFIER_NODE
10092 && IDENTIFIER_POINTER (tmv)
10093 && !strncmp (IDENTIFIER_POINTER (tmv), "NSString", 8));
10094 }
10095
10096 /* At present the behavior of this is undefined and it does nothing. */
10097 void
10098 objc_check_format_arg (tree ARG_UNUSED (format_arg),
10099 tree ARG_UNUSED (args_list))
10100 {
10101 }
10102
10103 void
10104 objc_common_init_ts (void)
10105 {
10106 c_common_init_ts ();
10107
10108 MARK_TS_DECL_NON_COMMON (CLASS_METHOD_DECL);
10109 MARK_TS_DECL_NON_COMMON (INSTANCE_METHOD_DECL);
10110 MARK_TS_DECL_NON_COMMON (KEYWORD_DECL);
10111 MARK_TS_DECL_NON_COMMON (PROPERTY_DECL);
10112
10113 MARK_TS_COMMON (CLASS_INTERFACE_TYPE);
10114 MARK_TS_COMMON (PROTOCOL_INTERFACE_TYPE);
10115 MARK_TS_COMMON (CLASS_IMPLEMENTATION_TYPE);
10116
10117 MARK_TS_TYPED (MESSAGE_SEND_EXPR);
10118 MARK_TS_TYPED (PROPERTY_REF);
10119 }
10120
10121 size_t
10122 objc_common_tree_size (enum tree_code code)
10123 {
10124 switch (code)
10125 {
10126 case CLASS_METHOD_DECL:
10127 case INSTANCE_METHOD_DECL:
10128 case KEYWORD_DECL:
10129 case PROPERTY_DECL: return sizeof (tree_decl_non_common);
10130 case CLASS_INTERFACE_TYPE:
10131 case CLASS_IMPLEMENTATION_TYPE:
10132 case CATEGORY_INTERFACE_TYPE:
10133 case CATEGORY_IMPLEMENTATION_TYPE:
10134 case PROTOCOL_INTERFACE_TYPE: return sizeof (tree_type_non_common);
10135 default:
10136 gcc_unreachable ();
10137 }
10138 }
10139
10140
10141 #include "gt-objc-objc-act.h"