In gcc/: 2011-10-14 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / gcc / objc / objc-act.c
blob396d748849f39730da7b81c16fd4b8d51bbb91bb
1 /* Implement classes and message passing for Objective C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Steve Naroff.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
29 #ifdef OBJCPLUS
30 #include "cp-tree.h"
31 #else
32 #include "c-tree.h"
33 #include "c-lang.h"
34 #endif
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "c-family/c-pragma.h"
39 #include "c-family/c-format.h"
40 #include "flags.h"
41 #include "langhooks.h"
42 #include "objc-act.h"
43 #include "objc-map.h"
44 #include "input.h"
45 #include "function.h"
46 #include "output.h"
47 #include "toplev.h"
48 #include "ggc.h"
49 #include "debug.h"
50 #include "c-family/c-target.h"
51 #include "diagnostic-core.h"
52 #include "intl.h"
53 #include "cgraph.h"
54 #include "tree-iterator.h"
55 #include "hashtab.h"
56 #include "langhooks-def.h"
57 /* Different initialization, code gen and meta data generation for each
58 runtime. */
59 #include "objc-runtime-hooks.h"
60 /* Routines used mainly by the runtimes. */
61 #include "objc-runtime-shared-support.h"
62 /* For default_tree_printer (). */
63 #include "tree-pretty-print.h"
65 /* For enum gimplify_status */
66 #include "gimple.h"
68 /* For encode_method_prototype(). */
69 #include "objc-encoding.h"
71 static unsigned int should_call_super_dealloc = 0;
73 /* When building Objective-C++, we are not linking against the C front-end
74 and so need to replicate the C tree-construction functions in some way. */
75 #ifdef OBJCPLUS
76 #define OBJCP_REMAP_FUNCTIONS
77 #include "objcp-decl.h"
78 #endif /* OBJCPLUS */
80 /* This is the default way of generating a method name. */
81 /* This has the problem that "test_method:argument:" and
82 "test:method_argument:" will generate the same name
83 ("_i_Test__test_method_argument_" for an instance method of the
84 class "Test"), so you can't have them both in the same class!
85 Moreover, the demangling (going from
86 "_i_Test__test_method_argument" back to the original name) is
87 undefined because there are two correct ways of demangling the
88 name. */
89 #ifndef OBJC_GEN_METHOD_LABEL
90 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
91 do { \
92 char *temp; \
93 sprintf ((BUF), "_%s_%s_%s_%s", \
94 ((IS_INST) ? "i" : "c"), \
95 (CLASS_NAME), \
96 ((CAT_NAME)? (CAT_NAME) : ""), \
97 (SEL_NAME)); \
98 for (temp = (BUF); *temp; temp++) \
99 if (*temp == ':') *temp = '_'; \
100 } while (0)
101 #endif
103 /* These need specifying. */
104 #ifndef OBJC_FORWARDING_STACK_OFFSET
105 #define OBJC_FORWARDING_STACK_OFFSET 0
106 #endif
108 #ifndef OBJC_FORWARDING_MIN_OFFSET
109 #define OBJC_FORWARDING_MIN_OFFSET 0
110 #endif
112 /*** Private Interface (procedures) ***/
114 /* Init stuff. */
115 static void synth_module_prologue (void);
117 /* Code generation. */
119 static tree start_class (enum tree_code, tree, tree, tree, tree);
120 static tree continue_class (tree);
121 static void finish_class (tree);
122 static void start_method_def (tree, tree);
124 static tree start_protocol (enum tree_code, tree, tree, tree);
125 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
126 static tree objc_add_method (tree, tree, int, bool);
127 static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree);
128 static tree build_ivar_reference (tree);
129 static tree is_ivar (tree, tree);
131 /* We only need the following for ObjC; ObjC++ will use C++'s definition
132 of DERIVED_FROM_P. */
133 #ifndef OBJCPLUS
134 static bool objc_derived_from_p (tree, tree);
135 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
136 #endif
138 /* Property. */
139 static void objc_gen_property_data (tree, tree);
140 static void objc_synthesize_getter (tree, tree, tree);
141 static void objc_synthesize_setter (tree, tree, tree);
142 static tree lookup_property (tree, tree);
143 static tree lookup_property_in_list (tree, tree);
144 static tree lookup_property_in_protocol_list (tree, tree);
145 static void build_common_objc_property_accessor_helpers (void);
147 static void objc_xref_basetypes (tree, tree);
149 static tree get_class_ivars (tree, bool);
151 static void build_fast_enumeration_state_template (void);
153 #ifdef OBJCPLUS
154 static void objc_generate_cxx_cdtors (void);
155 #endif
157 /* objc attribute */
158 static void objc_decl_method_attributes (tree*, tree, int);
159 static tree build_keyword_selector (tree);
161 static void hash_init (void);
163 /* Hash tables to manage the global pool of method prototypes. Each
164 of these maps map a method name (selector) identifier to either a
165 single tree (for methods with a single method prototype) or a
166 TREE_VEC (for methods with multiple method prototypes). */
167 static GTY(()) objc_map_t instance_method_map = 0;
168 static GTY(()) objc_map_t class_method_map = 0;
170 /* Hash tables to manage the global pool of class names. */
172 static GTY(()) objc_map_t class_name_map = 0;
173 static GTY(()) objc_map_t alias_name_map = 0;
175 static tree lookup_method (tree, tree);
176 static tree lookup_method_static (tree, tree, int);
178 static void interface_hash_init (void);
179 static tree add_interface (tree, tree);
180 static void add_category (tree, tree);
181 static inline tree lookup_category (tree, tree);
183 /* Protocols. */
185 static tree lookup_protocol (tree, bool, bool);
186 static tree lookup_and_install_protocols (tree, bool);
188 #ifdef OBJCPLUS
189 static void really_start_method (tree, tree);
190 #else
191 static void really_start_method (tree, struct c_arg_info *);
192 #endif
193 static int comp_proto_with_proto (tree, tree, int);
194 static tree objc_decay_parm_type (tree);
196 /* Utilities for debugging and error diagnostics. */
198 static char *gen_type_name (tree);
199 static char *gen_type_name_0 (tree);
200 static char *gen_method_decl (tree);
201 static char *gen_declaration (tree);
203 /* Everything else. */
205 static void generate_struct_by_value_array (void) ATTRIBUTE_NORETURN;
207 static void mark_referenced_methods (void);
208 static bool objc_type_valid_for_messaging (tree type, bool allow_classes);
209 static tree check_duplicates (tree, int, int);
211 /*** Private Interface (data) ***/
212 /* Flags for lookup_method_static(). */
214 /* Look for class methods. */
215 #define OBJC_LOOKUP_CLASS 1
216 /* Do not examine superclasses. */
217 #define OBJC_LOOKUP_NO_SUPER 2
218 /* Disable returning an instance method of a root class when a class
219 method can't be found. */
220 #define OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS 4
222 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
223 tree objc_global_trees[OCTI_MAX];
225 struct imp_entry *imp_list = 0;
226 int imp_count = 0; /* `@implementation' */
227 int cat_count = 0; /* `@category' */
229 objc_ivar_visibility_kind objc_ivar_visibility;
231 /* Use to generate method labels. */
232 static int method_slot = 0;
234 /* Flag to say whether methods in a protocol are optional or
235 required. */
236 static bool objc_method_optional_flag = false;
238 static int objc_collecting_ivars = 0;
240 /* Flag that is set to 'true' while we are processing a class
241 extension. Since a class extension just "reopens" the main
242 @interface, this can be used to determine if we are in the main
243 @interface, or in a class extension. */
244 static bool objc_in_class_extension = false;
246 static char *errbuf; /* Buffer for error diagnostics */
248 /* An array of all the local variables in the current function that
249 need to be marked as volatile. */
250 VEC(tree,gc) *local_variables_to_volatilize = NULL;
252 /* Store all constructed constant strings in a hash table so that
253 they get uniqued properly. */
255 struct GTY(()) string_descriptor {
256 /* The literal argument . */
257 tree literal;
259 /* The resulting constant string. */
260 tree constructor;
263 static GTY((param_is (struct string_descriptor))) htab_t string_htab;
265 FILE *gen_declaration_file;
267 /* Hooks for stuff that differs between runtimes. */
268 objc_runtime_hooks runtime;
270 /* Create a temporary variable of type 'type'. If 'name' is set, uses
271 the specified name, else use no name. Returns the declaration of
272 the type. The 'name' is mostly useful for debugging.
274 tree
275 objc_create_temporary_var (tree type, const char *name)
277 tree decl;
279 if (name != NULL)
281 decl = build_decl (input_location,
282 VAR_DECL, get_identifier (name), type);
284 else
286 decl = build_decl (input_location,
287 VAR_DECL, NULL_TREE, type);
289 TREE_USED (decl) = 1;
290 DECL_ARTIFICIAL (decl) = 1;
291 DECL_IGNORED_P (decl) = 1;
292 DECL_CONTEXT (decl) = current_function_decl;
294 return decl;
297 /* Some platforms pass small structures through registers versus
298 through an invisible pointer. Determine at what size structure is
299 the transition point between the two possibilities. */
301 static void
302 generate_struct_by_value_array (void)
304 tree type;
305 tree decls;
306 int i, j;
307 int aggregate_in_mem[32];
308 int found = 0;
310 /* Presumably no platform passes 32 byte structures in a register. */
311 /* ??? As an example, m64/ppc/Darwin can pass up to 8*long+13*double
312 in registers. */
313 for (i = 1; i < 32; i++)
315 char buffer[5];
316 tree *chain = NULL;
318 /* Create an unnamed struct that has `i' character components */
319 type = objc_start_struct (NULL_TREE);
321 strcpy (buffer, "c1");
322 decls = add_field_decl (char_type_node, buffer, &chain);
324 for (j = 1; j < i; j++)
326 sprintf (buffer, "c%d", j + 1);
327 add_field_decl (char_type_node, buffer, &chain);
329 objc_finish_struct (type, decls);
331 aggregate_in_mem[i] = aggregate_value_p (type, 0);
332 if (!aggregate_in_mem[i])
333 found = 1;
336 /* We found some structures that are returned in registers instead of memory
337 so output the necessary data. */
338 if (found)
340 for (i = 31; i >= 0; i--)
341 if (!aggregate_in_mem[i])
342 break;
343 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n", i);
346 exit (0);
349 bool
350 objc_init (void)
352 bool ok;
353 #ifdef OBJCPLUS
354 if (cxx_init () == false)
355 #else
356 if (c_objc_common_init () == false)
357 #endif
358 return false;
360 /* print_struct_values is triggered by -print-runtime-info (used
361 when building libobjc, with an empty file as input). It does not
362 require any ObjC setup, and it never returns.
364 -fcompare-debug is used to check the compiler output; we are
365 executed twice, once with flag_compare_debug set, and once with
366 it not set. If the flag is used together with
367 -print-runtime-info, we want to print the runtime info only once,
368 else it would be output in duplicate. So we check
369 flag_compare_debug to output it in only one of the invocations.
371 As a side effect, this also that means -fcompare-debug
372 -print-runtime-info will run the compiler twice, and compare the
373 generated assembler file; the first time the compiler exits
374 immediately (producing no file), and the second time it compiles
375 an empty file. This checks, as a side effect, that compiling an
376 empty file produces no assembler output. */
377 if (print_struct_values && !flag_compare_debug)
378 generate_struct_by_value_array ();
380 /* Set up stuff used by FE parser and all runtimes. */
381 errbuf = XNEWVEC (char, 1024 * 10);
382 interface_hash_init ();
383 hash_init ();
384 objc_encoding_init ();
385 /* ... and then check flags and set-up for the selected runtime ... */
386 if (flag_next_runtime && flag_objc_abi >= 2)
387 ok = objc_next_runtime_abi_02_init (&runtime);
388 else if (flag_next_runtime)
389 ok = objc_next_runtime_abi_01_init (&runtime);
390 else
391 ok = objc_gnu_runtime_abi_01_init (&runtime);
393 /* If that part of the setup failed - bail out immediately. */
394 if (!ok)
395 return false;
397 /* Generate general types and push runtime-specific decls to file scope. */
398 synth_module_prologue ();
400 return true;
403 /* This is called automatically (at the very end of compilation) by
404 c_write_global_declarations and cp_write_global_declarations. */
405 void
406 objc_write_global_declarations (void)
408 mark_referenced_methods ();
410 /* A missing @end might not be detected by the parser. */
411 if (objc_implementation_context)
413 warning (0, "%<@end%> missing in implementation context");
414 finish_class (objc_implementation_context);
415 objc_ivar_chain = NULL_TREE;
416 objc_implementation_context = NULL_TREE;
419 if (warn_selector)
421 objc_map_iterator_t i;
423 objc_map_iterator_initialize (class_method_map, &i);
424 while (objc_map_iterator_move_to_next (class_method_map, &i))
425 check_duplicates (objc_map_iterator_current_value (class_method_map, i), 0, 1);
427 objc_map_iterator_initialize (instance_method_map, &i);
428 while (objc_map_iterator_move_to_next (instance_method_map, &i))
429 check_duplicates (objc_map_iterator_current_value (instance_method_map, i), 0, 0);
432 /* TODO: consider an early exit here if either errorcount or sorrycount
433 is non-zero. Not only is it wasting time to generate the metadata,
434 it needlessly imposes need to re-check for things that are already
435 determined to be errors. */
437 /* Finalize Objective-C runtime data. No need to generate tables
438 and code if only checking syntax, or if generating a PCH file. */
439 if (!flag_syntax_only && !pch_file)
441 location_t saved_location;
443 /* If gen_declaration desired, open the output file. */
444 if (flag_gen_declaration)
446 char * const dumpname = concat (dump_base_name, ".decl", NULL);
447 gen_declaration_file = fopen (dumpname, "w");
448 if (gen_declaration_file == 0)
449 fatal_error ("can%'t open %s: %m", dumpname);
450 free (dumpname);
453 /* Set the input location to BUILTINS_LOCATION. This is good
454 for error messages, in case any is generated while producing
455 the metadata, but it also silences warnings that would be
456 produced when compiling with -Wpadded in case when padding is
457 automatically added to the built-in runtime data structure
458 declarations. We know about this padding, and it is fine; we
459 don't want users to see any warnings about it if they use
460 -Wpadded. */
461 saved_location = input_location;
462 input_location = BUILTINS_LOCATION;
464 /* Compute and emit the meta-data tables for this runtime. */
465 (*runtime.generate_metadata) ();
467 /* Restore the original location, just in case it mattered. */
468 input_location = saved_location;
470 /* ... and then close any declaration file we opened. */
471 if (gen_declaration_file)
472 fclose (gen_declaration_file);
476 /* Return the first occurrence of a method declaration corresponding
477 to sel_name in rproto_list. Search rproto_list recursively.
478 If is_class is 0, search for instance methods, otherwise for class
479 methods. */
480 static tree
481 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
482 int is_class)
484 tree rproto, p, m;
486 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
488 p = TREE_VALUE (rproto);
489 m = NULL_TREE;
491 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
493 /* First, search the @required protocol methods. */
494 if (is_class)
495 m = lookup_method (PROTOCOL_CLS_METHODS (p), sel_name);
496 else
497 m = lookup_method (PROTOCOL_NST_METHODS (p), sel_name);
499 if (m)
500 return m;
502 /* If still not found, search the @optional protocol methods. */
503 if (is_class)
504 m = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (p), sel_name);
505 else
506 m = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (p), sel_name);
508 if (m)
509 return m;
511 /* If still not found, search the attached protocols. */
512 if (PROTOCOL_LIST (p))
513 m = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
514 sel_name, is_class);
515 if (m)
516 return m;
518 else
520 ; /* An identifier...if we could not find a protocol. */
524 return 0;
527 static tree
528 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
530 tree rproto, p;
532 /* Make sure the protocol is supported by the object on the rhs. */
533 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
535 tree fnd = 0;
536 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
538 p = TREE_VALUE (rproto);
540 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
542 if (lproto == p)
543 fnd = lproto;
545 else if (PROTOCOL_LIST (p))
546 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
549 if (fnd)
550 return fnd;
553 else
555 ; /* An identifier...if we could not find a protocol. */
558 return 0;
561 void
562 objc_start_class_interface (tree klass, tree super_class,
563 tree protos, tree attributes)
565 if (flag_objc1_only && attributes)
566 error_at (input_location, "class attributes are not available in Objective-C 1.0");
568 objc_interface_context
569 = objc_ivar_context
570 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos, attributes);
571 objc_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
574 void
575 objc_start_category_interface (tree klass, tree categ,
576 tree protos, tree attributes)
578 if (attributes)
580 if (flag_objc1_only)
581 error_at (input_location, "category attributes are not available in Objective-C 1.0");
582 else
583 warning_at (input_location, OPT_Wattributes,
584 "category attributes are not available in this version"
585 " of the compiler, (ignored)");
587 if (categ == NULL_TREE)
589 if (flag_objc1_only)
590 error_at (input_location, "class extensions are not available in Objective-C 1.0");
591 else
593 /* Iterate over all the classes and categories implemented
594 up to now in this compilation unit. */
595 struct imp_entry *t;
597 for (t = imp_list; t; t = t->next)
599 /* If we find a class @implementation with the same name
600 as the one we are extending, produce an error. */
601 if (TREE_CODE (t->imp_context) == CLASS_IMPLEMENTATION_TYPE
602 && IDENTIFIER_POINTER (CLASS_NAME (t->imp_context)) == IDENTIFIER_POINTER (klass))
603 error_at (input_location,
604 "class extension for class %qE declared after its %<@implementation%>",
605 klass);
609 objc_interface_context
610 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos, NULL_TREE);
611 objc_ivar_chain
612 = continue_class (objc_interface_context);
615 void
616 objc_start_protocol (tree name, tree protos, tree attributes)
618 if (flag_objc1_only && attributes)
619 error_at (input_location, "protocol attributes are not available in Objective-C 1.0");
621 objc_interface_context
622 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos, attributes);
623 objc_method_optional_flag = false;
626 void
627 objc_continue_interface (void)
629 objc_ivar_chain
630 = continue_class (objc_interface_context);
633 void
634 objc_finish_interface (void)
636 finish_class (objc_interface_context);
637 objc_interface_context = NULL_TREE;
638 objc_method_optional_flag = false;
639 objc_in_class_extension = false;
642 void
643 objc_start_class_implementation (tree klass, tree super_class)
645 objc_implementation_context
646 = objc_ivar_context
647 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE,
648 NULL_TREE);
649 objc_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
652 void
653 objc_start_category_implementation (tree klass, tree categ)
655 objc_implementation_context
656 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE,
657 NULL_TREE);
658 objc_ivar_chain
659 = continue_class (objc_implementation_context);
662 void
663 objc_continue_implementation (void)
665 objc_ivar_chain
666 = continue_class (objc_implementation_context);
669 void
670 objc_finish_implementation (void)
672 #ifdef OBJCPLUS
673 if (flag_objc_call_cxx_cdtors)
674 objc_generate_cxx_cdtors ();
675 #endif
677 if (objc_implementation_context)
679 finish_class (objc_implementation_context);
680 objc_ivar_chain = NULL_TREE;
681 objc_implementation_context = NULL_TREE;
683 else
684 warning (0, "%<@end%> must appear in an @implementation context");
687 void
688 objc_set_visibility (objc_ivar_visibility_kind visibility)
690 if (visibility == OBJC_IVAR_VIS_PACKAGE)
692 if (flag_objc1_only)
693 error ("%<@package%> is not available in Objective-C 1.0");
694 else
695 warning (0, "%<@package%> presently has the same effect as %<@public%>");
697 objc_ivar_visibility = visibility;
700 void
701 objc_set_method_opt (bool optional)
703 if (flag_objc1_only)
705 if (optional)
706 error_at (input_location, "%<@optional%> is not available in Objective-C 1.0");
707 else
708 error_at (input_location, "%<@required%> is not available in Objective-C 1.0");
711 objc_method_optional_flag = optional;
712 if (!objc_interface_context
713 || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
715 if (optional)
716 error ("%<@optional%> is allowed in @protocol context only");
717 else
718 error ("%<@required%> is allowed in @protocol context only");
719 objc_method_optional_flag = false;
723 /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
724 PROTOCOL. */
725 static tree
726 lookup_property_in_list (tree chain, tree property)
728 tree x;
729 for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x))
730 if (PROPERTY_NAME (x) == property)
731 return x;
732 return NULL_TREE;
735 /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
736 static tree lookup_property_in_protocol_list (tree rproto_list, tree property)
738 tree rproto, x;
739 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
741 tree p = TREE_VALUE (rproto);
742 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
744 if ((x = lookup_property_in_list (p, property)))
745 return x;
746 if (PROTOCOL_LIST (p))
747 return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property);
749 else
751 ; /* An identifier...if we could not find a protocol. */
754 return NULL_TREE;
757 /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
758 chain of interface hierarchy. */
759 static tree
760 lookup_property (tree interface_type, tree property)
762 tree inter = interface_type;
763 while (inter)
765 tree x, category;
766 if ((x = lookup_property_in_list (inter, property)))
767 return x;
768 /* Failing that, look for the property in each category of the class. */
769 category = inter;
770 while ((category = CLASS_CATEGORY_LIST (category)))
772 if ((x = lookup_property_in_list (category, property)))
773 return x;
775 /* When checking a category, also check the protocols
776 attached with the category itself. */
777 if (CLASS_PROTOCOL_LIST (category)
778 && (x = lookup_property_in_protocol_list
779 (CLASS_PROTOCOL_LIST (category), property)))
780 return x;
783 /* Failing to find in categories, look for property in protocol list. */
784 if (CLASS_PROTOCOL_LIST (inter)
785 && (x = lookup_property_in_protocol_list
786 (CLASS_PROTOCOL_LIST (inter), property)))
787 return x;
789 /* Failing that, climb up the inheritance hierarchy. */
790 inter = lookup_interface (CLASS_SUPER_NAME (inter));
792 return inter;
795 /* This routine is called by the parser when a
796 @property... declaration is found. 'decl' is the declaration of
797 the property (type/identifier), and the other arguments represent
798 property attributes that may have been specified in the Objective-C
799 declaration. 'parsed_property_readonly' is 'true' if the attribute
800 'readonly' was specified, and 'false' if not; similarly for the
801 other bool parameters. 'parsed_property_getter_ident' is NULL_TREE
802 if the attribute 'getter' was not specified, and is the identifier
803 corresponding to the specified getter if it was; similarly for
804 'parsed_property_setter_ident'. */
805 void
806 objc_add_property_declaration (location_t location, tree decl,
807 bool parsed_property_readonly, bool parsed_property_readwrite,
808 bool parsed_property_assign, bool parsed_property_retain,
809 bool parsed_property_copy, bool parsed_property_nonatomic,
810 tree parsed_property_getter_ident, tree parsed_property_setter_ident)
812 tree property_decl;
813 tree x;
814 /* 'property_readonly' and 'property_assign_semantics' are the final
815 attributes of the property after all parsed attributes have been
816 considered (eg, if we parsed no 'readonly' and no 'readwrite', ie
817 parsed_property_readonly = false and parsed_property_readwrite =
818 false, then property_readonly will be false because the default
819 is readwrite). */
820 bool property_readonly = false;
821 objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN;
822 bool property_extension_in_class_extension = false;
824 if (flag_objc1_only)
825 error_at (input_location, "%<@property%> is not available in Objective-C 1.0");
827 if (parsed_property_readonly && parsed_property_readwrite)
829 error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute");
830 /* In case of conflicting attributes (here and below), after
831 producing an error, we pick one of the attributes and keep
832 going. */
833 property_readonly = false;
835 else
837 if (parsed_property_readonly)
838 property_readonly = true;
840 if (parsed_property_readwrite)
841 property_readonly = false;
844 if (parsed_property_readonly && parsed_property_setter_ident)
846 error_at (location, "%<readonly%> attribute conflicts with %<setter%> attribute");
847 property_readonly = false;
850 if (parsed_property_assign && parsed_property_retain)
852 error_at (location, "%<assign%> attribute conflicts with %<retain%> attribute");
853 property_assign_semantics = OBJC_PROPERTY_RETAIN;
855 else if (parsed_property_assign && parsed_property_copy)
857 error_at (location, "%<assign%> attribute conflicts with %<copy%> attribute");
858 property_assign_semantics = OBJC_PROPERTY_COPY;
860 else if (parsed_property_retain && parsed_property_copy)
862 error_at (location, "%<retain%> attribute conflicts with %<copy%> attribute");
863 property_assign_semantics = OBJC_PROPERTY_COPY;
865 else
867 if (parsed_property_assign)
868 property_assign_semantics = OBJC_PROPERTY_ASSIGN;
870 if (parsed_property_retain)
871 property_assign_semantics = OBJC_PROPERTY_RETAIN;
873 if (parsed_property_copy)
874 property_assign_semantics = OBJC_PROPERTY_COPY;
877 if (!objc_interface_context)
879 error_at (location, "property declaration not in @interface or @protocol context");
880 return;
883 /* At this point we know that we are either in an interface, a
884 category, or a protocol. */
886 /* We expect a FIELD_DECL from the parser. Make sure we didn't get
887 something else, as that would confuse the checks below. */
888 if (TREE_CODE (decl) != FIELD_DECL)
890 error_at (location, "invalid property declaration");
891 return;
894 /* Do some spot-checks for the most obvious invalid types. */
896 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
898 error_at (location, "property can not be an array");
899 return;
902 /* The C++/ObjC++ parser seems to reject the ':' for a bitfield when
903 parsing, while the C/ObjC parser accepts it and gives us a
904 FIELD_DECL with a DECL_INITIAL set. So we use the DECL_INITIAL
905 to check for a bitfield when doing ObjC. */
906 #ifndef OBJCPLUS
907 if (DECL_INITIAL (decl))
909 /* A @property is not an actual variable, but it is a way to
910 describe a pair of accessor methods, so its type (which is
911 the type of the return value of the getter and the first
912 argument of the setter) can't be a bitfield (as return values
913 and arguments of functions can not be bitfields). The
914 underlying instance variable could be a bitfield, but that is
915 a different matter. */
916 error_at (location, "property can not be a bit-field");
917 return;
919 #endif
921 /* TODO: Check that the property type is an Objective-C object or a
922 "POD". */
924 /* Implement -Wproperty-assign-default (which is enabled by default). */
925 if (warn_property_assign_default
926 /* If garbage collection is not being used, then 'assign' is
927 valid for objects (and typically used for delegates) but it
928 is wrong in most cases (since most objects need to be
929 retained or copied in setters). Warn users when 'assign' is
930 used implicitly. */
931 && property_assign_semantics == OBJC_PROPERTY_ASSIGN
932 /* Read-only properties are never assigned, so the assignment
933 semantics do not matter in that case. */
934 && !property_readonly
935 && !flag_objc_gc)
937 /* Please note that it would make sense to default to 'assign'
938 for non-{Objective-C objects}, and to 'retain' for
939 Objective-C objects. But that would break compatibility with
940 other compilers. */
941 if (!parsed_property_assign && !parsed_property_retain && !parsed_property_copy)
943 /* Use 'false' so we do not warn for Class objects. */
944 if (objc_type_valid_for_messaging (TREE_TYPE (decl), false))
946 warning_at (location,
948 "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>",
949 decl);
950 inform (location,
951 "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it");
956 if (property_assign_semantics == OBJC_PROPERTY_RETAIN
957 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
958 error_at (location, "%<retain%> attribute is only valid for Objective-C objects");
960 if (property_assign_semantics == OBJC_PROPERTY_COPY
961 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
962 error_at (location, "%<copy%> attribute is only valid for Objective-C objects");
964 /* Now determine the final property getter and setter names. They
965 will be stored in the PROPERTY_DECL, from which they'll always be
966 extracted and used. */
968 /* Adjust, or fill in, setter and getter names. We overwrite the
969 parsed_property_setter_ident and parsed_property_getter_ident
970 with the final setter and getter identifiers that will be
971 used. */
972 if (parsed_property_setter_ident)
974 /* The setter should be terminated by ':', but the parser only
975 gives us an identifier without ':'. So, we need to add ':'
976 at the end. */
977 const char *parsed_setter = IDENTIFIER_POINTER (parsed_property_setter_ident);
978 size_t length = strlen (parsed_setter);
979 char *final_setter = (char *)alloca (length + 2);
981 sprintf (final_setter, "%s:", parsed_setter);
982 parsed_property_setter_ident = get_identifier (final_setter);
984 else
986 if (!property_readonly)
987 parsed_property_setter_ident = get_identifier (objc_build_property_setter_name
988 (DECL_NAME (decl)));
991 if (!parsed_property_getter_ident)
992 parsed_property_getter_ident = DECL_NAME (decl);
994 /* Check for duplicate property declarations. We first check the
995 immediate context for a property with the same name. Any such
996 declarations are an error, unless this is a class extension and
997 we are extending a property from readonly to readwrite. */
998 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
1000 if (PROPERTY_NAME (x) == DECL_NAME (decl))
1002 if (objc_in_class_extension
1003 && property_readonly == 0
1004 && PROPERTY_READONLY (x) == 1)
1006 /* This is a class extension, and we are extending an
1007 existing readonly property to a readwrite one.
1008 That's fine. :-) */
1009 property_extension_in_class_extension = true;
1010 break;
1012 else
1014 location_t original_location = DECL_SOURCE_LOCATION (x);
1016 error_at (location, "redeclaration of property %qD", decl);
1018 if (original_location != UNKNOWN_LOCATION)
1019 inform (original_location, "originally specified here");
1020 return;
1025 /* If x is not NULL_TREE, we must be in a class extension and we're
1026 extending a readonly property. In that case, no point in
1027 searching for another declaration. */
1028 if (x == NULL_TREE)
1030 /* We now need to check for existing property declarations (in
1031 the superclass, other categories or protocols) and check that
1032 the new declaration is not in conflict with existing
1033 ones. */
1035 /* Search for a previous, existing declaration of a property
1036 with the same name in superclasses, protocols etc. If one is
1037 found, it will be in the 'x' variable. */
1039 /* Note that, for simplicity, the following may search again the
1040 local context. That's Ok as nothing will be found (else we'd
1041 have thrown an error above); it's only a little inefficient,
1042 but the code is simpler. */
1043 switch (TREE_CODE (objc_interface_context))
1045 case CLASS_INTERFACE_TYPE:
1046 /* Look up the property in the current @interface (which
1047 will find nothing), then its protocols and categories and
1048 superclasses. */
1049 x = lookup_property (objc_interface_context, DECL_NAME (decl));
1050 break;
1051 case CATEGORY_INTERFACE_TYPE:
1052 /* Look up the property in the main @interface, then
1053 protocols and categories (one of them is ours, and will
1054 find nothing) and superclasses. */
1055 x = lookup_property (lookup_interface (CLASS_NAME (objc_interface_context)),
1056 DECL_NAME (decl));
1057 break;
1058 case PROTOCOL_INTERFACE_TYPE:
1059 /* Looks up the property in any protocols attached to the
1060 current protocol. */
1061 if (PROTOCOL_LIST (objc_interface_context))
1063 x = lookup_property_in_protocol_list (PROTOCOL_LIST (objc_interface_context),
1064 DECL_NAME (decl));
1066 break;
1067 default:
1068 gcc_unreachable ();
1072 if (x != NULL_TREE)
1074 /* An existing property was found; check that it has the same
1075 types, or it is compatible. */
1076 location_t original_location = DECL_SOURCE_LOCATION (x);
1078 if (PROPERTY_NONATOMIC (x) != parsed_property_nonatomic)
1080 warning_at (location, 0,
1081 "'nonatomic' attribute of property %qD conflicts with previous declaration", decl);
1083 if (original_location != UNKNOWN_LOCATION)
1084 inform (original_location, "originally specified here");
1085 return;
1088 if (PROPERTY_GETTER_NAME (x) != parsed_property_getter_ident)
1090 warning_at (location, 0,
1091 "'getter' attribute of property %qD conflicts with previous declaration", decl);
1093 if (original_location != UNKNOWN_LOCATION)
1094 inform (original_location, "originally specified here");
1095 return;
1098 /* We can only compare the setter names if both the old and new property have a setter. */
1099 if (!property_readonly && !PROPERTY_READONLY(x))
1101 if (PROPERTY_SETTER_NAME (x) != parsed_property_setter_ident)
1103 warning_at (location, 0,
1104 "'setter' attribute of property %qD conflicts with previous declaration", decl);
1106 if (original_location != UNKNOWN_LOCATION)
1107 inform (original_location, "originally specified here");
1108 return;
1112 if (PROPERTY_ASSIGN_SEMANTICS (x) != property_assign_semantics)
1114 warning_at (location, 0,
1115 "assign semantics attributes of property %qD conflict with previous declaration", decl);
1117 if (original_location != UNKNOWN_LOCATION)
1118 inform (original_location, "originally specified here");
1119 return;
1122 /* It's ok to have a readonly property that becomes a readwrite, but not vice versa. */
1123 if (PROPERTY_READONLY (x) == 0 && property_readonly == 1)
1125 warning_at (location, 0,
1126 "'readonly' attribute of property %qD conflicts with previous declaration", decl);
1128 if (original_location != UNKNOWN_LOCATION)
1129 inform (original_location, "originally specified here");
1130 return;
1133 /* We now check that the new and old property declarations have
1134 the same types (or compatible one). In the Objective-C
1135 tradition of loose type checking, we do type-checking but
1136 only generate warnings (not errors) if they do not match.
1137 For non-readonly properties, the types must match exactly;
1138 for readonly properties, it is allowed to use a "more
1139 specialized" type in the new property declaration. Eg, the
1140 superclass has a getter returning (NSArray *) and the
1141 subclass a getter returning (NSMutableArray *). The object's
1142 getter returns an (NSMutableArray *); but if you cast the
1143 object to the superclass, which is allowed, you'd still
1144 expect the getter to return an (NSArray *), which works since
1145 an (NSMutableArray *) is an (NSArray *) too. So, the set of
1146 objects belonging to the type of the new @property should be
1147 a subset of the set of objects belonging to the type of the
1148 old @property. This is what "specialization" means. And the
1149 reason it only applies to readonly properties is that for a
1150 readwrite property the setter would have the opposite
1151 requirement - ie that the superclass type is more specialized
1152 then the subclass one; hence the only way to satisfy both
1153 constraints is that the types match. */
1155 /* If the types are not the same in the C sense, we warn ... */
1156 if (!comptypes (TREE_TYPE (x), TREE_TYPE (decl))
1157 /* ... unless the property is readonly, in which case we
1158 allow a new, more specialized, declaration. */
1159 && (!property_readonly
1160 || !objc_compare_types (TREE_TYPE (x),
1161 TREE_TYPE (decl), -5, NULL_TREE)))
1163 warning_at (location, 0,
1164 "type of property %qD conflicts with previous declaration", decl);
1165 if (original_location != UNKNOWN_LOCATION)
1166 inform (original_location, "originally specified here");
1167 return;
1170 /* If we are in a class extension and we're extending a readonly
1171 property in the main @interface, we'll just update the
1172 existing property with the readwrite flag and potentially the
1173 new setter name. */
1174 if (property_extension_in_class_extension)
1176 PROPERTY_READONLY (x) = 0;
1177 PROPERTY_SETTER_NAME (x) = parsed_property_setter_ident;
1178 return;
1182 /* Create a PROPERTY_DECL node. */
1183 property_decl = make_node (PROPERTY_DECL);
1185 /* Copy the basic information from the original decl. */
1186 TREE_TYPE (property_decl) = TREE_TYPE (decl);
1187 DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl);
1188 TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl);
1190 /* Add property-specific information. */
1191 PROPERTY_NAME (property_decl) = DECL_NAME (decl);
1192 PROPERTY_GETTER_NAME (property_decl) = parsed_property_getter_ident;
1193 PROPERTY_SETTER_NAME (property_decl) = parsed_property_setter_ident;
1194 PROPERTY_READONLY (property_decl) = property_readonly;
1195 PROPERTY_NONATOMIC (property_decl) = parsed_property_nonatomic;
1196 PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics;
1197 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1198 PROPERTY_DYNAMIC (property_decl) = 0;
1200 /* Remember the fact that the property was found in the @optional
1201 section in a @protocol, or not. */
1202 if (objc_method_optional_flag)
1203 PROPERTY_OPTIONAL (property_decl) = 1;
1204 else
1205 PROPERTY_OPTIONAL (property_decl) = 0;
1207 /* Note that PROPERTY_GETTER_NAME is always set for all
1208 PROPERTY_DECLs, and PROPERTY_SETTER_NAME is always set for all
1209 PROPERTY_DECLs where PROPERTY_READONLY == 0. Any time we deal
1210 with a getter or setter, we should get the PROPERTY_DECL and use
1211 PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know the correct
1212 names. */
1214 /* Add the PROPERTY_DECL to the list of properties for the class. */
1215 TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context);
1216 CLASS_PROPERTY_DECL (objc_interface_context) = property_decl;
1219 /* This is a subroutine of objc_maybe_build_component_ref. Search the
1220 list of methods in the interface (and, failing that, the local list
1221 in the implementation, and failing that, the protocol list)
1222 provided for a 'setter' or 'getter' for 'component' with default
1223 names (ie, if 'component' is "name", then search for "name" and
1224 "setName:"). It is also possible to specify a different
1225 'getter_name' (this is used for @optional readonly properties). If
1226 any is found, then create an artificial property that uses them.
1227 Return NULL_TREE if 'getter' or 'setter' could not be found. */
1228 static tree
1229 maybe_make_artificial_property_decl (tree interface, tree implementation,
1230 tree protocol_list, tree component, bool is_class,
1231 tree getter_name)
1233 tree setter_name = get_identifier (objc_build_property_setter_name (component));
1234 tree getter = NULL_TREE;
1235 tree setter = NULL_TREE;
1237 if (getter_name == NULL_TREE)
1238 getter_name = component;
1240 /* First, check the @interface and all superclasses. */
1241 if (interface)
1243 int flags = 0;
1245 /* Using instance methods of the root class as accessors is most
1246 likely unwanted and can be extremely confusing (and, most
1247 importantly, other Objective-C 2.0 compilers do not do it).
1248 Turn it off. */
1249 if (is_class)
1250 flags = OBJC_LOOKUP_CLASS | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS;
1252 getter = lookup_method_static (interface, getter_name, flags);
1253 setter = lookup_method_static (interface, setter_name, flags);
1256 /* Second, check the local @implementation context. */
1257 if (!getter && !setter)
1259 if (implementation)
1261 if (is_class)
1263 getter = lookup_method (CLASS_CLS_METHODS (implementation), getter_name);
1264 setter = lookup_method (CLASS_CLS_METHODS (implementation), setter_name);
1266 else
1268 getter = lookup_method (CLASS_NST_METHODS (implementation), getter_name);
1269 setter = lookup_method (CLASS_NST_METHODS (implementation), setter_name);
1274 /* Try the protocol_list if we didn't find anything in the
1275 @interface and in the @implementation. */
1276 if (!getter && !setter)
1278 getter = lookup_method_in_protocol_list (protocol_list, getter_name, is_class);
1279 setter = lookup_method_in_protocol_list (protocol_list, setter_name, is_class);
1282 /* There needs to be at least a getter or setter for this to be a
1283 valid 'object.component' syntax. */
1284 if (getter || setter)
1286 /* Yes ... determine the type of the expression. */
1287 tree property_decl;
1288 tree type;
1290 if (getter)
1291 type = TREE_VALUE (TREE_TYPE (getter));
1292 else
1293 type = TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter)));
1295 /* Create an artificial property declaration with the
1296 information we collected on the type and getter/setter
1297 names. */
1298 property_decl = make_node (PROPERTY_DECL);
1300 TREE_TYPE (property_decl) = type;
1301 DECL_SOURCE_LOCATION (property_decl) = input_location;
1302 TREE_DEPRECATED (property_decl) = 0;
1303 DECL_ARTIFICIAL (property_decl) = 1;
1305 /* Add property-specific information. Note that one of
1306 PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a
1307 non-existing method; this will generate an error when the
1308 expression is later compiled. At this stage we don't know if
1309 the getter or setter will be used, so we can't generate an
1310 error. */
1311 PROPERTY_NAME (property_decl) = component;
1312 PROPERTY_GETTER_NAME (property_decl) = getter_name;
1313 PROPERTY_SETTER_NAME (property_decl) = setter_name;
1314 PROPERTY_READONLY (property_decl) = 0;
1315 PROPERTY_NONATOMIC (property_decl) = 0;
1316 PROPERTY_ASSIGN_SEMANTICS (property_decl) = 0;
1317 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1318 PROPERTY_DYNAMIC (property_decl) = 0;
1319 PROPERTY_OPTIONAL (property_decl) = 0;
1321 if (!getter)
1322 PROPERTY_HAS_NO_GETTER (property_decl) = 1;
1324 /* The following is currently unused, but it's nice to have
1325 there. We may use it if we need in the future. */
1326 if (!setter)
1327 PROPERTY_HAS_NO_SETTER (property_decl) = 1;
1329 return property_decl;
1332 return NULL_TREE;
1335 /* This hook routine is invoked by the parser when an expression such
1336 as 'xxx.yyy' is parsed. We get a chance to process these
1337 expressions in a way that is specified to Objective-C (to implement
1338 the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars).
1339 If the expression is not an Objective-C specified expression, we
1340 should return NULL_TREE; else we return the expression.
1342 At the moment this only implements dot-syntax and properties (not
1343 non-fragile ivars yet), ie 'object.property' or 'object.component'
1344 where 'component' is not a declared property, but a valid getter or
1345 setter for it could be found. */
1346 tree
1347 objc_maybe_build_component_ref (tree object, tree property_ident)
1349 tree x = NULL_TREE;
1350 tree rtype;
1352 /* If we are in Objective-C 1.0 mode, dot-syntax and properties are
1353 not available. */
1354 if (flag_objc1_only)
1355 return NULL_TREE;
1357 /* Try to determine if 'object' is an Objective-C object or not. If
1358 not, return. */
1359 if (object == NULL_TREE || object == error_mark_node
1360 || (rtype = TREE_TYPE (object)) == NULL_TREE)
1361 return NULL_TREE;
1363 if (property_ident == NULL_TREE || property_ident == error_mark_node
1364 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1365 return NULL_TREE;
1367 /* The following analysis of 'object' is similar to the one used for
1368 the 'receiver' of a method invocation. We need to determine what
1369 'object' is and find the appropriate property (either declared,
1370 or artificial) for it (in the same way as we need to find the
1371 appropriate method prototype for a method invocation). There are
1372 some simplifications here though: "object.property" is invalid if
1373 "object" has a type of "id" or "Class"; it must at least have a
1374 protocol attached to it, and "object" is never a class name as
1375 that is done by objc_build_class_component_ref. Finally, we
1376 don't know if this really is a dot-syntax expression, so we want
1377 to make a quick exit if it is not; for this reason, we try to
1378 postpone checks after determining that 'object' looks like an
1379 Objective-C object. */
1381 if (objc_is_id (rtype))
1383 /* This is the case that the 'object' is of type 'id' or
1384 'Class'. */
1386 /* Check if at least it is of type 'id <Protocol>' or 'Class
1387 <Protocol>'; if so, look the property up in the
1388 protocols. */
1389 if (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype)))
1391 tree rprotos = TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype));
1393 if (rprotos)
1395 /* No point looking up declared @properties if we are
1396 dealing with a class. Classes have no declared
1397 properties. */
1398 if (!IS_CLASS (rtype))
1399 x = lookup_property_in_protocol_list (rprotos, property_ident);
1401 if (x == NULL_TREE)
1403 /* Ok, no property. Maybe it was an
1404 object.component dot-syntax without a declared
1405 property (this is valid for classes too). Look
1406 for getter/setter methods and internally declare
1407 an artifical property based on them if found. */
1408 x = maybe_make_artificial_property_decl (NULL_TREE,
1409 NULL_TREE,
1410 rprotos,
1411 property_ident,
1412 IS_CLASS (rtype),
1413 NULL_TREE);
1415 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1417 /* This is a special, complicated case. If the
1418 property is optional, and is read-only, then the
1419 property is always used for reading, but an
1420 eventual existing non-property setter can be used
1421 for writing. We create an artificial property
1422 decl copying the getter from the optional
1423 property, and looking up the setter in the
1424 interface. */
1425 x = maybe_make_artificial_property_decl (NULL_TREE,
1426 NULL_TREE,
1427 rprotos,
1428 property_ident,
1429 false,
1430 PROPERTY_GETTER_NAME (x));
1434 else if (objc_method_context)
1436 /* Else, if we are inside a method it could be the case of
1437 'super' or 'self'. */
1438 tree interface_type = NULL_TREE;
1439 tree t = object;
1440 while (TREE_CODE (t) == COMPOUND_EXPR
1441 || TREE_CODE (t) == MODIFY_EXPR
1442 || CONVERT_EXPR_P (t)
1443 || TREE_CODE (t) == COMPONENT_REF)
1444 t = TREE_OPERAND (t, 0);
1446 if (t == UOBJC_SUPER_decl)
1447 interface_type = lookup_interface (CLASS_SUPER_NAME (implementation_template));
1448 else if (t == self_decl)
1449 interface_type = lookup_interface (CLASS_NAME (implementation_template));
1451 if (interface_type)
1453 if (TREE_CODE (objc_method_context) != CLASS_METHOD_DECL)
1454 x = lookup_property (interface_type, property_ident);
1456 if (x == NULL_TREE)
1458 /* Try the dot-syntax without a declared property.
1459 If this is an access to 'self', it is possible
1460 that they may refer to a setter/getter that is
1461 not declared in the interface, but exists locally
1462 in the implementation. In that case, get the
1463 implementation context and use it. */
1464 tree implementation = NULL_TREE;
1466 if (t == self_decl)
1467 implementation = objc_implementation_context;
1469 x = maybe_make_artificial_property_decl
1470 (interface_type, implementation, NULL_TREE,
1471 property_ident,
1472 (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL),
1473 NULL_TREE);
1475 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1477 tree implementation = NULL_TREE;
1479 if (t == self_decl)
1480 implementation = objc_implementation_context;
1482 x = maybe_make_artificial_property_decl (interface_type,
1483 implementation,
1484 NULL_TREE,
1485 property_ident,
1486 false,
1487 PROPERTY_GETTER_NAME (x));
1492 else
1494 /* This is the case where we have more information on 'rtype'. */
1495 tree basetype = TYPE_MAIN_VARIANT (rtype);
1497 /* Skip the pointer - if none, it's not an Objective-C object or
1498 class. */
1499 if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE)
1500 basetype = TREE_TYPE (basetype);
1501 else
1502 return NULL_TREE;
1504 /* Traverse typedefs. */
1505 while (basetype != NULL_TREE
1506 && TREE_CODE (basetype) == RECORD_TYPE
1507 && OBJC_TYPE_NAME (basetype)
1508 && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL
1509 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)))
1510 basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype));
1512 if (basetype != NULL_TREE && TYPED_OBJECT (basetype))
1514 tree interface_type = TYPE_OBJC_INTERFACE (basetype);
1515 tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype);
1517 if (interface_type
1518 && (TREE_CODE (interface_type) == CLASS_INTERFACE_TYPE
1519 || TREE_CODE (interface_type) == CATEGORY_INTERFACE_TYPE
1520 || TREE_CODE (interface_type) == PROTOCOL_INTERFACE_TYPE))
1522 /* Not sure 'rtype' could ever be a class here! Just
1523 for safety we keep the checks. */
1524 if (!IS_CLASS (rtype))
1526 x = lookup_property (interface_type, property_ident);
1528 if (x == NULL_TREE)
1529 x = lookup_property_in_protocol_list (protocol_list,
1530 property_ident);
1533 if (x == NULL_TREE)
1535 /* Try the dot-syntax without a declared property.
1536 If we are inside a method implementation, it is
1537 possible that they may refer to a setter/getter
1538 that is not declared in the interface, but exists
1539 locally in the implementation. In that case, get
1540 the implementation context and use it. */
1541 tree implementation = NULL_TREE;
1543 if (objc_implementation_context
1544 && CLASS_NAME (objc_implementation_context)
1545 == OBJC_TYPE_NAME (interface_type))
1546 implementation = objc_implementation_context;
1548 x = maybe_make_artificial_property_decl (interface_type,
1549 implementation,
1550 protocol_list,
1551 property_ident,
1552 IS_CLASS (rtype),
1553 NULL_TREE);
1555 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1557 tree implementation = NULL_TREE;
1559 if (objc_implementation_context
1560 && CLASS_NAME (objc_implementation_context)
1561 == OBJC_TYPE_NAME (interface_type))
1562 implementation = objc_implementation_context;
1564 x = maybe_make_artificial_property_decl (interface_type,
1565 implementation,
1566 protocol_list,
1567 property_ident,
1568 false,
1569 PROPERTY_GETTER_NAME (x));
1575 if (x)
1577 tree expression;
1578 tree getter_call;
1579 tree deprecated_method_prototype = NULL_TREE;
1581 /* We have an additional nasty problem here; if this
1582 PROPERTY_REF needs to become a 'getter', then the conversion
1583 from PROPERTY_REF into a getter call happens in gimplify,
1584 after the selector table has already been generated and when
1585 it is too late to add another selector to it. To work around
1586 the problem, we always create the getter call at this stage,
1587 which puts the selector in the table. Note that if the
1588 PROPERTY_REF becomes a 'setter' instead of a 'getter', then
1589 we have added a selector too many to the selector table.
1590 This is a little inefficient.
1592 Also note that method calls to 'self' and 'super' require the
1593 context (self_decl, UOBJS_SUPER_decl,
1594 objc_implementation_context etc) to be built correctly; this
1595 is yet another reason why building the call at the gimplify
1596 stage (when this context has been lost) is not very
1597 practical. If we build it at this stage, we know it will
1598 always be built correctly.
1600 If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial
1601 property decl created to deal with a dotsyntax not really
1602 referring to an existing property) then do not try to build a
1603 call to the getter as there is no getter. */
1604 if (PROPERTY_HAS_NO_GETTER (x))
1605 getter_call = NULL_TREE;
1606 else
1607 getter_call = objc_finish_message_expr
1608 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1609 /* Disable the immediate deprecation warning if the getter
1610 is deprecated, but record the fact that the getter is
1611 deprecated by setting PROPERTY_REF_DEPRECATED_GETTER to
1612 the method prototype. */
1613 &deprecated_method_prototype);
1615 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1616 deprecated_method_prototype);
1617 SET_EXPR_LOCATION (expression, input_location);
1618 TREE_SIDE_EFFECTS (expression) = 1;
1620 return expression;
1623 return NULL_TREE;
1626 /* This hook routine is invoked by the parser when an expression such
1627 as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the
1628 Objective-C 2.0 dot-syntax applied to classes, so we need to
1629 convert it into a setter/getter call on the class. */
1630 tree
1631 objc_build_class_component_ref (tree class_name, tree property_ident)
1633 tree x = NULL_TREE;
1634 tree object, rtype;
1636 if (flag_objc1_only)
1637 error_at (input_location, "the dot syntax is not available in Objective-C 1.0");
1639 if (class_name == NULL_TREE || class_name == error_mark_node
1640 || TREE_CODE (class_name) != IDENTIFIER_NODE)
1641 return error_mark_node;
1643 if (property_ident == NULL_TREE || property_ident == error_mark_node
1644 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1645 return NULL_TREE;
1647 object = objc_get_class_reference (class_name);
1648 if (!object)
1650 /* We know that 'class_name' is an Objective-C class name as the
1651 parser won't call this function if it is not. This is only a
1652 double-check for safety. */
1653 error_at (input_location, "could not find class %qE", class_name);
1654 return error_mark_node;
1657 rtype = lookup_interface (class_name);
1658 if (!rtype)
1660 /* Again, this should never happen, but we do check. */
1661 error_at (input_location, "could not find interface for class %qE", class_name);
1662 return error_mark_node;
1664 else
1666 if (TREE_DEPRECATED (rtype))
1667 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", class_name);
1670 x = maybe_make_artificial_property_decl (rtype, NULL_TREE, NULL_TREE,
1671 property_ident,
1672 true, NULL_TREE);
1674 if (x)
1676 tree expression;
1677 tree getter_call;
1678 tree deprecated_method_prototype = NULL_TREE;
1680 if (PROPERTY_HAS_NO_GETTER (x))
1681 getter_call = NULL_TREE;
1682 else
1683 getter_call = objc_finish_message_expr
1684 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1685 &deprecated_method_prototype);
1687 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1688 deprecated_method_prototype);
1689 SET_EXPR_LOCATION (expression, input_location);
1690 TREE_SIDE_EFFECTS (expression) = 1;
1692 return expression;
1694 else
1696 error_at (input_location, "could not find setter/getter for %qE in class %qE",
1697 property_ident, class_name);
1698 return error_mark_node;
1701 return NULL_TREE;
1706 /* This is used because we don't want to expose PROPERTY_REF to the
1707 C/C++ frontends. Maybe we should! */
1708 bool
1709 objc_is_property_ref (tree node)
1711 if (node && TREE_CODE (node) == PROPERTY_REF)
1712 return true;
1713 else
1714 return false;
1717 /* This function builds a setter call for a PROPERTY_REF (real, for a
1718 declared property, or artificial, for a dot-syntax accessor which
1719 is not corresponding to a property). 'lhs' must be a PROPERTY_REF
1720 (the caller must check this beforehand). 'rhs' is the value to
1721 assign to the property. A plain setter call is returned, or
1722 error_mark_node if the property is readonly. */
1724 static tree
1725 objc_build_setter_call (tree lhs, tree rhs)
1727 tree object_expr = PROPERTY_REF_OBJECT (lhs);
1728 tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs);
1730 if (PROPERTY_READONLY (property_decl))
1732 error ("readonly property can not be set");
1733 return error_mark_node;
1735 else
1737 tree setter_argument = build_tree_list (NULL_TREE, rhs);
1738 tree setter;
1740 /* TODO: Check that the setter return type is 'void'. */
1742 /* TODO: Decay arguments in C. */
1743 setter = objc_finish_message_expr (object_expr,
1744 PROPERTY_SETTER_NAME (property_decl),
1745 setter_argument, NULL);
1746 return setter;
1749 /* Unreachable, but the compiler may not realize. */
1750 return error_mark_node;
1753 /* This hook routine is called when a MODIFY_EXPR is being built. We
1754 check what is being modified; if it is a PROPERTY_REF, we need to
1755 generate a 'setter' function call for the property. If this is not
1756 a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go
1757 on creating their MODIFY_EXPR.
1759 This is used for example if you write
1761 object.count = 1;
1763 where 'count' is a property. The left-hand side creates a
1764 PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR
1765 to assign something to it. We intercept that here, and generate a
1766 call to the 'setter' method instead. */
1767 tree
1768 objc_maybe_build_modify_expr (tree lhs, tree rhs)
1770 if (lhs && TREE_CODE (lhs) == PROPERTY_REF)
1772 /* Building a simple call to the setter method would work for cases such as
1774 object.count = 1;
1776 but wouldn't work for cases such as
1778 count = object2.count = 1;
1780 to get these to work with very little effort, we build a
1781 compound statement which does the setter call (to set the
1782 property to 'rhs'), but which can also be evaluated returning
1783 the 'rhs'. If the 'rhs' has no side effects, we can simply
1784 evaluate it twice, building
1786 ([object setProperty: rhs]; rhs)
1788 If it has side effects, we put it in a temporary variable first,
1789 so we create the following:
1791 (temp = rhs; [object setProperty: temp]; temp)
1793 setter_argument is rhs in the first case, and temp in the second
1794 case.
1796 tree setter_argument;
1798 /* s1, s2 and s3 are the tree statements that we need in the
1799 compound expression. */
1800 tree s1, s2, s3, compound_expr;
1802 if (TREE_SIDE_EFFECTS (rhs))
1804 tree bind;
1806 /* Declare __objc_property_temp in a local bind. */
1807 setter_argument = objc_create_temporary_var (TREE_TYPE (rhs), "__objc_property_temp");
1808 DECL_SOURCE_LOCATION (setter_argument) = input_location;
1809 bind = build3 (BIND_EXPR, void_type_node, setter_argument, NULL, NULL);
1810 SET_EXPR_LOCATION (bind, input_location);
1811 TREE_SIDE_EFFECTS (bind) = 1;
1812 add_stmt (bind);
1814 /* s1: x = rhs */
1815 s1 = build_modify_expr (input_location, setter_argument, NULL_TREE,
1816 NOP_EXPR,
1817 input_location, rhs, NULL_TREE);
1818 SET_EXPR_LOCATION (s1, input_location);
1820 else
1822 /* No s1. */
1823 setter_argument = rhs;
1824 s1 = NULL_TREE;
1827 /* Now build the compound statement. */
1829 /* s2: [object setProperty: x] */
1830 s2 = objc_build_setter_call (lhs, setter_argument);
1832 /* This happens if building the setter failed because the
1833 property is readonly. */
1834 if (s2 == error_mark_node)
1835 return error_mark_node;
1837 SET_EXPR_LOCATION (s2, input_location);
1839 /* s3: x */
1840 s3 = convert (TREE_TYPE (lhs), setter_argument);
1842 /* Now build the compound statement (s1, s2, s3) or (s2, s3) as
1843 appropriate. */
1844 if (s1)
1845 compound_expr = build_compound_expr (input_location, build_compound_expr (input_location, s1, s2), s3);
1846 else
1847 compound_expr = build_compound_expr (input_location, s2, s3);
1849 /* Without this, with -Wall you get a 'valued computed is not
1850 used' every time there is a "object.property = x" where the
1851 value of the resulting MODIFY_EXPR is not used. That is
1852 correct (maybe a more sophisticated implementation could
1853 avoid generating the compound expression if not needed), but
1854 we need to turn it off. */
1855 TREE_NO_WARNING (compound_expr) = 1;
1856 return compound_expr;
1858 else
1859 return NULL_TREE;
1862 /* This hook is called by the frontend when one of the four unary
1863 expressions PREINCREMENT_EXPR, POSTINCREMENT_EXPR,
1864 PREDECREMENT_EXPR and POSTDECREMENT_EXPR is being built with an
1865 argument which is a PROPERTY_REF. For example, this happens if you have
1867 object.count++;
1869 where 'count' is a property. We need to use the 'getter' and
1870 'setter' for the property in an appropriate way to build the
1871 appropriate expression. 'code' is the code for the expression (one
1872 of the four mentioned above); 'argument' is the PROPERTY_REF, and
1873 'increment' is how much we need to add or subtract. */
1874 tree
1875 objc_build_incr_expr_for_property_ref (location_t location,
1876 enum tree_code code,
1877 tree argument, tree increment)
1879 /* Here are the expressions that we want to build:
1881 For PREINCREMENT_EXPR / PREDECREMENT_EXPR:
1882 (temp = [object property] +/- increment, [object setProperty: temp], temp)
1884 For POSTINCREMENT_EXPR / POSTECREMENT_EXPR:
1885 (temp = [object property], [object setProperty: temp +/- increment], temp) */
1887 tree temp_variable_decl, bind;
1888 /* s1, s2 and s3 are the tree statements that we need in the
1889 compound expression. */
1890 tree s1, s2, s3, compound_expr;
1892 /* Safety check. */
1893 if (!argument || TREE_CODE (argument) != PROPERTY_REF)
1894 return error_mark_node;
1896 /* Declare __objc_property_temp in a local bind. */
1897 temp_variable_decl = objc_create_temporary_var (TREE_TYPE (argument), "__objc_property_temp");
1898 DECL_SOURCE_LOCATION (temp_variable_decl) = location;
1899 bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL);
1900 SET_EXPR_LOCATION (bind, location);
1901 TREE_SIDE_EFFECTS (bind) = 1;
1902 add_stmt (bind);
1904 /* Now build the compound statement. */
1906 /* Note that the 'getter' is generated at gimplify time; at this
1907 time, we can simply put the property_ref (ie, argument) wherever
1908 we want the getter ultimately to be. */
1910 /* s1: __objc_property_temp = [object property] <+/- increment> */
1911 switch (code)
1913 case PREINCREMENT_EXPR:
1914 /* __objc_property_temp = [object property] + increment */
1915 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1916 NOP_EXPR,
1917 location, build2 (PLUS_EXPR, TREE_TYPE (argument),
1918 argument, increment), NULL_TREE);
1919 break;
1920 case PREDECREMENT_EXPR:
1921 /* __objc_property_temp = [object property] - increment */
1922 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1923 NOP_EXPR,
1924 location, build2 (MINUS_EXPR, TREE_TYPE (argument),
1925 argument, increment), NULL_TREE);
1926 break;
1927 case POSTINCREMENT_EXPR:
1928 case POSTDECREMENT_EXPR:
1929 /* __objc_property_temp = [object property] */
1930 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1931 NOP_EXPR,
1932 location, argument, NULL_TREE);
1933 break;
1934 default:
1935 gcc_unreachable ();
1938 /* s2: [object setProperty: __objc_property_temp <+/- increment>] */
1939 switch (code)
1941 case PREINCREMENT_EXPR:
1942 case PREDECREMENT_EXPR:
1943 /* [object setProperty: __objc_property_temp] */
1944 s2 = objc_build_setter_call (argument, temp_variable_decl);
1945 break;
1946 case POSTINCREMENT_EXPR:
1947 /* [object setProperty: __objc_property_temp + increment] */
1948 s2 = objc_build_setter_call (argument,
1949 build2 (PLUS_EXPR, TREE_TYPE (argument),
1950 temp_variable_decl, increment));
1951 break;
1952 case POSTDECREMENT_EXPR:
1953 /* [object setProperty: __objc_property_temp - increment] */
1954 s2 = objc_build_setter_call (argument,
1955 build2 (MINUS_EXPR, TREE_TYPE (argument),
1956 temp_variable_decl, increment));
1957 break;
1958 default:
1959 gcc_unreachable ();
1962 /* This happens if building the setter failed because the property
1963 is readonly. */
1964 if (s2 == error_mark_node)
1965 return error_mark_node;
1967 SET_EXPR_LOCATION (s2, location);
1969 /* s3: __objc_property_temp */
1970 s3 = convert (TREE_TYPE (argument), temp_variable_decl);
1972 /* Now build the compound statement (s1, s2, s3) */
1973 compound_expr = build_compound_expr (location, build_compound_expr (location, s1, s2), s3);
1975 /* Prevent C++ from warning with -Wall that "right operand of comma
1976 operator has no effect". */
1977 TREE_NO_WARNING (compound_expr) = 1;
1978 return compound_expr;
1981 tree
1982 objc_build_method_signature (bool is_class_method, tree rettype, tree selector,
1983 tree optparms, bool ellipsis)
1985 if (is_class_method)
1986 return build_method_decl (CLASS_METHOD_DECL, rettype, selector,
1987 optparms, ellipsis);
1988 else
1989 return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector,
1990 optparms, ellipsis);
1993 void
1994 objc_add_method_declaration (bool is_class_method, tree decl, tree attributes)
1996 if (!objc_interface_context)
1998 /* PS: At the moment, due to how the parser works, it should be
1999 impossible to get here. But it's good to have the check in
2000 case the parser changes.
2002 fatal_error ("method declaration not in @interface context");
2005 if (flag_objc1_only && attributes)
2006 error_at (input_location, "method attributes are not available in Objective-C 1.0");
2008 objc_decl_method_attributes (&decl, attributes, 0);
2009 objc_add_method (objc_interface_context,
2010 decl,
2011 is_class_method,
2012 objc_method_optional_flag);
2015 /* Return 'true' if the method definition could be started, and
2016 'false' if not (because we are outside an @implementation context).
2017 EXPR is NULL or an expression that needs to be evaluated for the
2018 side effects of array size expressions in the parameters.
2020 bool
2021 objc_start_method_definition (bool is_class_method, tree decl, tree attributes,
2022 tree expr)
2024 if (!objc_implementation_context)
2026 error ("method definition not in @implementation context");
2027 return false;
2030 if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node)
2031 return false;
2033 #ifndef OBJCPLUS
2034 /* Indicate no valid break/continue context by setting these variables
2035 to some non-null, non-label value. We'll notice and emit the proper
2036 error message in c_finish_bc_stmt. */
2037 c_break_label = c_cont_label = size_zero_node;
2038 #endif
2040 if (attributes)
2041 warning_at (input_location, 0, "method attributes can not be specified in @implementation context");
2042 else
2043 objc_decl_method_attributes (&decl, attributes, 0);
2045 objc_add_method (objc_implementation_context,
2046 decl,
2047 is_class_method,
2048 /* is optional */ false);
2049 start_method_def (decl, expr);
2050 return true;
2053 void
2054 objc_add_instance_variable (tree decl)
2056 (void) add_instance_variable (objc_ivar_context,
2057 objc_ivar_visibility,
2058 decl);
2061 /* Construct a C struct with same name as KLASS, a base struct with tag
2062 SUPER_NAME (if any), and FIELDS indicated. */
2064 static tree
2065 objc_build_struct (tree klass, tree fields, tree super_name)
2067 tree name = CLASS_NAME (klass);
2068 tree s = objc_start_struct (name);
2069 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
2070 tree t;
2071 VEC(tree,heap) *objc_info = NULL;
2072 int i;
2074 if (super)
2076 /* Prepend a packed variant of the base class into the layout. This
2077 is necessary to preserve ObjC ABI compatibility. */
2078 tree base = build_decl (input_location,
2079 FIELD_DECL, NULL_TREE, super);
2080 tree field = TYPE_FIELDS (super);
2082 while (field && DECL_CHAIN (field)
2083 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
2084 field = DECL_CHAIN (field);
2086 /* For ObjC ABI purposes, the "packed" size of a base class is
2087 the sum of the offset and the size (in bits) of the last field
2088 in the class. */
2089 DECL_SIZE (base)
2090 = (field && TREE_CODE (field) == FIELD_DECL
2091 ? size_binop (PLUS_EXPR,
2092 size_binop (PLUS_EXPR,
2093 size_binop
2094 (MULT_EXPR,
2095 convert (bitsizetype,
2096 DECL_FIELD_OFFSET (field)),
2097 bitsize_int (BITS_PER_UNIT)),
2098 DECL_FIELD_BIT_OFFSET (field)),
2099 DECL_SIZE (field))
2100 : bitsize_zero_node);
2101 DECL_SIZE_UNIT (base)
2102 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
2103 size_int (BITS_PER_UNIT));
2104 DECL_ARTIFICIAL (base) = 1;
2105 DECL_ALIGN (base) = 1;
2106 DECL_FIELD_CONTEXT (base) = s;
2107 #ifdef OBJCPLUS
2108 DECL_FIELD_IS_BASE (base) = 1;
2110 if (fields)
2111 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
2112 #endif /* are following the ObjC ABI here. */
2113 DECL_CHAIN (base) = fields;
2114 fields = base;
2117 /* NB: Calling finish_struct() may cause type TYPE_OBJC_INFO
2118 information in all variants of this RECORD_TYPE to be destroyed
2119 (this is because the C frontend manipulates TYPE_LANG_SPECIFIC
2120 for something else and then will change all variants to use the
2121 same resulting TYPE_LANG_SPECIFIC, ignoring the fact that we use
2122 it for ObjC protocols and that such propagation will make all
2123 variants use the same objc_info), but it is therein that we store
2124 protocol conformance info (e.g., 'NSObject <MyProtocol>').
2125 Hence, we must save the ObjC-specific information before calling
2126 finish_struct(), and then reinstate it afterwards. */
2128 for (t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
2130 INIT_TYPE_OBJC_INFO (t);
2131 VEC_safe_push (tree, heap, objc_info, TYPE_OBJC_INFO (t));
2134 s = objc_finish_struct (s, fields);
2136 for (i = 0, t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
2138 /* We now want to restore the different TYPE_OBJC_INFO, but we
2139 have the additional problem that the C frontend doesn't just
2140 copy TYPE_LANG_SPECIFIC from one variant to the other; it
2141 actually makes all of them the *same* TYPE_LANG_SPECIFIC. As
2142 we need a different TYPE_OBJC_INFO for each (and
2143 TYPE_OBJC_INFO is a field in TYPE_LANG_SPECIFIC), we need to
2144 make a copy of each TYPE_LANG_SPECIFIC before we modify
2145 TYPE_OBJC_INFO. */
2146 if (TYPE_LANG_SPECIFIC (t))
2148 /* Create a copy of TYPE_LANG_SPECIFIC. */
2149 struct lang_type *old_lang_type = TYPE_LANG_SPECIFIC (t);
2150 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2151 memcpy (TYPE_LANG_SPECIFIC (t), old_lang_type,
2152 SIZEOF_OBJC_TYPE_LANG_SPECIFIC);
2154 else
2156 /* Just create a new one. */
2157 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2159 /* Replace TYPE_OBJC_INFO with the saved one. This restores any
2160 protocol information that may have been associated with the
2161 type. */
2162 TYPE_OBJC_INFO (t) = VEC_index (tree, objc_info, i);
2163 /* Replace the IDENTIFIER_NODE with an actual @interface now
2164 that we have it. */
2165 TYPE_OBJC_INTERFACE (t) = klass;
2167 VEC_free (tree, heap, objc_info);
2169 /* Use TYPE_BINFO structures to point at the super class, if any. */
2170 objc_xref_basetypes (s, super);
2172 /* Mark this struct as a class template. */
2173 CLASS_STATIC_TEMPLATE (klass) = s;
2175 return s;
2178 /* Mark DECL as being 'volatile' for purposes of Darwin
2179 _setjmp()/_longjmp() exception handling. Called from
2180 objc_mark_locals_volatile(). */
2181 void
2182 objc_volatilize_decl (tree decl)
2184 /* Do not mess with variables that are 'static' or (already)
2185 'volatile'. */
2186 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
2187 && (TREE_CODE (decl) == VAR_DECL
2188 || TREE_CODE (decl) == PARM_DECL))
2190 if (local_variables_to_volatilize == NULL)
2191 local_variables_to_volatilize = VEC_alloc (tree, gc, 8);
2193 VEC_safe_push (tree, gc, local_variables_to_volatilize, decl);
2197 /* Called when parsing of a function completes; if any local variables
2198 in the function were marked as variables to volatilize, change them
2199 to volatile. We do this at the end of the function when the
2200 warnings about discarding 'volatile' have already been produced.
2201 We are making the variables as volatile just to force the compiler
2202 to preserve them between setjmp/longjmp, but we don't want warnings
2203 for them as they aren't really volatile. */
2204 void
2205 objc_finish_function (void)
2207 /* If there are any local variables to volatilize, volatilize them. */
2208 if (local_variables_to_volatilize)
2210 int i;
2211 tree decl;
2212 FOR_EACH_VEC_ELT (tree, local_variables_to_volatilize, i, decl)
2214 tree t = TREE_TYPE (decl);
2216 t = build_qualified_type (t, TYPE_QUALS (t) | TYPE_QUAL_VOLATILE);
2217 TREE_TYPE (decl) = t;
2218 TREE_THIS_VOLATILE (decl) = 1;
2219 TREE_SIDE_EFFECTS (decl) = 1;
2220 DECL_REGISTER (decl) = 0;
2221 #ifndef OBJCPLUS
2222 C_DECL_REGISTER (decl) = 0;
2223 #endif
2226 /* Now we delete the vector. This sets it to NULL as well. */
2227 VEC_free (tree, gc, local_variables_to_volatilize);
2231 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
2232 (including its categories and superclasses) or by object type TYP.
2233 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
2235 static bool
2236 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
2238 bool class_type = (cls != NULL_TREE);
2240 while (cls)
2242 tree c;
2244 /* Check protocols adopted by the class and its categories. */
2245 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
2247 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
2248 return true;
2251 /* Repeat for superclasses. */
2252 cls = lookup_interface (CLASS_SUPER_NAME (cls));
2255 /* Check for any protocols attached directly to the object type. */
2256 if (TYPE_HAS_OBJC_INFO (typ))
2258 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
2259 return true;
2262 if (warn)
2264 *errbuf = 0;
2265 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
2266 /* NB: Types 'id' and 'Class' cannot reasonably be described as
2267 "implementing" a given protocol, since they do not have an
2268 implementation. */
2269 if (class_type)
2270 warning (0, "class %qs does not implement the %qE protocol",
2271 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2272 else
2273 warning (0, "type %qs does not conform to the %qE protocol",
2274 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2277 return false;
2280 /* Check if class RCLS and instance struct type RTYP conform to at least the
2281 same protocols that LCLS and LTYP conform to. */
2283 static bool
2284 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
2286 tree p;
2287 bool have_lproto = false;
2289 while (lcls)
2291 /* NB: We do _not_ look at categories defined for LCLS; these may or
2292 may not get loaded in, and therefore it is unreasonable to require
2293 that RCLS/RTYP must implement any of their protocols. */
2294 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
2296 have_lproto = true;
2298 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2299 return warn;
2302 /* Repeat for superclasses. */
2303 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
2306 /* Check for any protocols attached directly to the object type. */
2307 if (TYPE_HAS_OBJC_INFO (ltyp))
2309 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
2311 have_lproto = true;
2313 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2314 return warn;
2318 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
2319 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
2320 away with simply checking for 'id' or 'Class' (!RCLS), since this
2321 routine will not get called in other cases. */
2322 return have_lproto || (rcls != NULL_TREE);
2325 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
2326 Both TYPE1 and TYPE2 must be pointers, and already determined to be
2327 compatible by objc_compare_types() below. */
2329 tree
2330 objc_common_type (tree type1, tree type2)
2332 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
2334 while (POINTER_TYPE_P (inner1))
2336 inner1 = TREE_TYPE (inner1);
2337 inner2 = TREE_TYPE (inner2);
2340 /* If one type is derived from another, return the base type. */
2341 if (DERIVED_FROM_P (inner1, inner2))
2342 return type1;
2343 else if (DERIVED_FROM_P (inner2, inner1))
2344 return type2;
2346 /* If both types are 'Class', return 'Class'. */
2347 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
2348 return objc_class_type;
2350 /* Otherwise, return 'id'. */
2351 return objc_object_type;
2354 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
2355 an instance of RTYP to an instance of LTYP or to compare the two
2356 (if ARGNO is equal to -3), per ObjC type system rules. Before
2357 returning 'true', this routine may issue warnings related to, e.g.,
2358 protocol conformance. When returning 'false', the routine must
2359 produce absolutely no warnings; the C or C++ front-end will do so
2360 instead, if needed. If either LTYP or RTYP is not an Objective-C
2361 type, the routine must return 'false'.
2363 The ARGNO parameter is encoded as follows:
2364 >= 1 Parameter number (CALLEE contains function being called);
2365 0 Return value;
2366 -1 Assignment;
2367 -2 Initialization;
2368 -3 Comparison (LTYP and RTYP may match in either direction);
2369 -4 Silent comparison (for C++ overload resolution);
2370 -5 Silent "specialization" comparison for RTYP to be a "specialization"
2371 of LTYP (a specialization means that RTYP is LTYP plus some constraints,
2372 so that each object of type RTYP is also of type LTYP). This is used
2373 when comparing property types. */
2375 bool
2376 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
2378 tree lcls, rcls, lproto, rproto;
2379 bool pointers_compatible;
2381 /* We must be dealing with pointer types */
2382 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
2383 return false;
2387 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2388 rtyp = TREE_TYPE (rtyp);
2390 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2392 /* We must also handle function pointers, since ObjC is a bit more
2393 lenient than C or C++ on this. */
2394 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
2396 function_args_iterator liter, riter;
2398 /* Return types must be covariant. */
2399 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
2400 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
2401 argno, callee))
2402 return false;
2404 /* Argument types must be contravariant. */
2405 function_args_iter_init (&liter, ltyp);
2406 function_args_iter_init (&riter, rtyp);
2408 while (1)
2410 ltyp = function_args_iter_cond (&liter);
2411 rtyp = function_args_iter_cond (&riter);
2413 /* If we've exhaused both lists simulateously, we're done. */
2414 if (ltyp == NULL_TREE && rtyp == NULL_TREE)
2415 break;
2417 /* If one list is shorter than the other, they fail to match. */
2418 if (ltyp == NULL_TREE || rtyp == NULL_TREE)
2419 return false;
2421 if (!comptypes (rtyp, ltyp)
2422 && !objc_compare_types (rtyp, ltyp, argno, callee))
2423 return false;
2425 function_args_iter_next (&liter);
2426 function_args_iter_next (&riter);
2429 return true;
2432 /* Past this point, we are only interested in ObjC class instances,
2433 or 'id' or 'Class'. */
2434 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
2435 return false;
2437 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
2438 && !TYPE_HAS_OBJC_INFO (ltyp))
2439 return false;
2441 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
2442 && !TYPE_HAS_OBJC_INFO (rtyp))
2443 return false;
2445 /* Past this point, we are committed to returning 'true' to the caller
2446 (unless performing a silent comparison; see below). However, we can
2447 still warn about type and/or protocol mismatches. */
2449 if (TYPE_HAS_OBJC_INFO (ltyp))
2451 lcls = TYPE_OBJC_INTERFACE (ltyp);
2452 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
2454 else
2455 lcls = lproto = NULL_TREE;
2457 if (TYPE_HAS_OBJC_INFO (rtyp))
2459 rcls = TYPE_OBJC_INTERFACE (rtyp);
2460 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
2462 else
2463 rcls = rproto = NULL_TREE;
2465 /* If we could not find an @interface declaration, we must have
2466 only seen a @class declaration; for purposes of type comparison,
2467 treat it as a stand-alone (root) class. */
2469 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
2470 lcls = NULL_TREE;
2472 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
2473 rcls = NULL_TREE;
2475 /* If either type is an unqualified 'id', we're done. This is because
2476 an 'id' can be assigned to or from any type with no warnings. */
2477 if (argno != -5)
2479 if ((!lproto && objc_is_object_id (ltyp))
2480 || (!rproto && objc_is_object_id (rtyp)))
2481 return true;
2483 else
2485 /* For property checks, though, an 'id' is considered the most
2486 general type of object, hence if you try to specialize an
2487 'NSArray *' (ltyp) property with an 'id' (rtyp) one, we need
2488 to warn. */
2489 if (!lproto && objc_is_object_id (ltyp))
2490 return true;
2493 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
2495 /* If the underlying types are the same, and at most one of them has
2496 a protocol list, we do not need to issue any diagnostics. */
2497 if (pointers_compatible && (!lproto || !rproto))
2498 return true;
2500 /* If exactly one of the types is 'Class', issue a diagnostic; any
2501 exceptions of this rule have already been handled. */
2502 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
2503 pointers_compatible = false;
2504 /* Otherwise, check for inheritance relations. */
2505 else
2507 if (!pointers_compatible)
2509 /* Again, if any of the two is an 'id', we're satisfied,
2510 unless we're comparing properties, in which case only an
2511 'id' on the left-hand side (old property) is good
2512 enough. */
2513 if (argno != -5)
2514 pointers_compatible
2515 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
2516 else
2517 pointers_compatible = objc_is_object_id (ltyp);
2520 if (!pointers_compatible)
2521 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
2523 if (!pointers_compatible && (argno == -3 || argno == -4))
2524 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
2527 /* If the pointers match modulo protocols, check for protocol conformance
2528 mismatches. */
2529 if (pointers_compatible)
2531 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
2532 argno != -3);
2534 if (!pointers_compatible && argno == -3)
2535 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
2536 argno != -3);
2539 if (!pointers_compatible)
2541 /* The two pointers are not exactly compatible. Issue a warning, unless
2542 we are performing a silent comparison, in which case return 'false'
2543 instead. */
2544 /* NB: For the time being, we shall make our warnings look like their
2545 C counterparts. In the future, we may wish to make them more
2546 ObjC-specific. */
2547 switch (argno)
2549 case -5:
2550 case -4:
2551 return false;
2553 case -3:
2554 warning (0, "comparison of distinct Objective-C types lacks a cast");
2555 break;
2557 case -2:
2558 warning (0, "initialization from distinct Objective-C type");
2559 break;
2561 case -1:
2562 warning (0, "assignment from distinct Objective-C type");
2563 break;
2565 case 0:
2566 warning (0, "distinct Objective-C type in return");
2567 break;
2569 default:
2570 warning (0, "passing argument %d of %qE from distinct "
2571 "Objective-C type", argno, callee);
2572 break;
2576 return true;
2579 /* This routine is similar to objc_compare_types except that function-pointers are
2580 excluded. This is because, caller assumes that common types are of (id, Object*)
2581 variety and calls objc_common_type to obtain a common type. There is no commonolty
2582 between two function-pointers in this regard. */
2584 bool
2585 objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
2587 if (objc_compare_types (ltyp, rtyp, argno, callee))
2589 /* exclude function-pointer types. */
2592 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2593 rtyp = TREE_TYPE (rtyp);
2595 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2596 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
2598 return false;
2601 #ifndef OBJCPLUS
2602 /* Determine if CHILD is derived from PARENT. The routine assumes that
2603 both parameters are RECORD_TYPEs, and is non-reflexive. */
2605 static bool
2606 objc_derived_from_p (tree parent, tree child)
2608 parent = TYPE_MAIN_VARIANT (parent);
2610 for (child = TYPE_MAIN_VARIANT (child);
2611 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
2613 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
2614 (TYPE_BINFO (child),
2615 0)));
2617 if (child == parent)
2618 return true;
2621 return false;
2623 #endif
2625 tree
2626 objc_build_component_ref (tree datum, tree component)
2628 /* If COMPONENT is NULL, the caller is referring to the anonymous
2629 base class field. */
2630 if (!component)
2632 tree base = TYPE_FIELDS (TREE_TYPE (datum));
2634 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
2637 /* The 'build_component_ref' routine has been removed from the C++
2638 front-end, but 'finish_class_member_access_expr' seems to be
2639 a worthy substitute. */
2640 #ifdef OBJCPLUS
2641 return finish_class_member_access_expr (datum, component, false,
2642 tf_warning_or_error);
2643 #else
2644 return build_component_ref (input_location, datum, component);
2645 #endif
2648 /* Recursively copy inheritance information rooted at BINFO. To do this,
2649 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
2651 static tree
2652 objc_copy_binfo (tree binfo)
2654 tree btype = BINFO_TYPE (binfo);
2655 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
2656 tree base_binfo;
2657 int ix;
2659 BINFO_TYPE (binfo2) = btype;
2660 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
2661 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
2663 /* Recursively copy base binfos of BINFO. */
2664 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2666 tree base_binfo2 = objc_copy_binfo (base_binfo);
2668 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
2669 BINFO_BASE_APPEND (binfo2, base_binfo2);
2672 return binfo2;
2675 /* Record superclass information provided in BASETYPE for ObjC class REF.
2676 This is loosely based on cp/decl.c:xref_basetypes(). */
2678 static void
2679 objc_xref_basetypes (tree ref, tree basetype)
2681 tree binfo = make_tree_binfo (basetype ? 1 : 0);
2683 TYPE_BINFO (ref) = binfo;
2684 BINFO_OFFSET (binfo) = size_zero_node;
2685 BINFO_TYPE (binfo) = ref;
2687 if (basetype)
2689 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
2691 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
2692 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, 1);
2693 BINFO_BASE_APPEND (binfo, base_binfo);
2694 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
2698 /* Called from finish_decl. */
2700 void
2701 objc_check_decl (tree decl)
2703 tree type = TREE_TYPE (decl);
2705 if (TREE_CODE (type) != RECORD_TYPE)
2706 return;
2707 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
2708 error ("statically allocated instance of Objective-C class %qE",
2709 type);
2712 void
2713 objc_check_global_decl (tree decl)
2715 tree id = DECL_NAME (decl);
2716 if (objc_is_class_name (id) && global_bindings_p())
2717 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id));
2720 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where
2721 INTERFACE may either name an Objective-C class, or refer to the
2722 special 'id' or 'Class' types. If INTERFACE is not a valid ObjC
2723 type, just return it unchanged. This function is often called when
2724 PROTOCOLS is NULL_TREE, in which case we simply look up the
2725 appropriate INTERFACE. */
2727 tree
2728 objc_get_protocol_qualified_type (tree interface, tree protocols)
2730 /* If INTERFACE is not provided, default to 'id'. */
2731 tree type = (interface ? objc_is_id (interface) : objc_object_type);
2732 bool is_ptr = (type != NULL_TREE);
2734 if (!is_ptr)
2736 type = objc_is_class_name (interface);
2738 if (type)
2740 /* If looking at a typedef, retrieve the precise type it
2741 describes. */
2742 if (TREE_CODE (interface) == IDENTIFIER_NODE)
2743 interface = identifier_global_value (interface);
2745 type = ((interface && TREE_CODE (interface) == TYPE_DECL
2746 && DECL_ORIGINAL_TYPE (interface))
2747 ? DECL_ORIGINAL_TYPE (interface)
2748 : xref_tag (RECORD_TYPE, type));
2750 else
2752 /* This case happens when we are given an 'interface' which
2753 is not a valid class name. For example if a typedef was
2754 used, and 'interface' really is the identifier of the
2755 typedef, but when you resolve it you don't get an
2756 Objective-C class, but something else, such as 'int'.
2757 This is an error; protocols make no sense unless you use
2758 them with Objective-C objects. */
2759 error_at (input_location, "only Objective-C object types can be qualified with a protocol");
2761 /* Try to recover. Ignore the invalid class name, and treat
2762 the object as an 'id' to silence further warnings about
2763 the class. */
2764 type = objc_object_type;
2765 is_ptr = true;
2769 if (protocols)
2771 type = build_variant_type_copy (type);
2773 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
2774 to the pointee. */
2775 if (is_ptr)
2777 tree orig_pointee_type = TREE_TYPE (type);
2778 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
2780 /* Set up the canonical type information. */
2781 TYPE_CANONICAL (type)
2782 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
2784 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
2785 type = TREE_TYPE (type);
2788 /* Look up protocols and install in lang specific list. */
2789 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
2790 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols
2791 (protocols, /* definition_required */ false);
2793 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
2794 return the pointer to the new pointee variant. */
2795 if (is_ptr)
2796 type = TYPE_POINTER_TO (type);
2797 else
2798 TYPE_OBJC_INTERFACE (type)
2799 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
2802 return type;
2805 /* Check for circular dependencies in protocols. The arguments are
2806 PROTO, the protocol to check, and LIST, a list of protocol it
2807 conforms to. */
2809 static void
2810 check_protocol_recursively (tree proto, tree list)
2812 tree p;
2814 for (p = list; p; p = TREE_CHAIN (p))
2816 tree pp = TREE_VALUE (p);
2818 if (TREE_CODE (pp) == IDENTIFIER_NODE)
2819 pp = lookup_protocol (pp, /* warn if deprecated */ false,
2820 /* definition_required */ false);
2822 if (pp == proto)
2823 fatal_error ("protocol %qE has circular dependency",
2824 PROTOCOL_NAME (pp));
2825 if (pp)
2826 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
2830 /* Look up PROTOCOLS, and return a list of those that are found. If
2831 none are found, return NULL. Note that this function will emit a
2832 warning if a protocol is found and is deprecated. If
2833 'definition_required', then warn if the protocol is found but is
2834 not defined (ie, if we only saw a forward-declaration of the
2835 protocol (as in "@protocol NSObject;") not a real definition with
2836 the list of methods). */
2837 static tree
2838 lookup_and_install_protocols (tree protocols, bool definition_required)
2840 tree proto;
2841 tree return_value = NULL_TREE;
2843 if (protocols == error_mark_node)
2844 return NULL;
2846 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
2848 tree ident = TREE_VALUE (proto);
2849 tree p = lookup_protocol (ident, /* warn_if_deprecated */ true,
2850 definition_required);
2852 if (p)
2853 return_value = chainon (return_value,
2854 build_tree_list (NULL_TREE, p));
2855 else if (ident != error_mark_node)
2856 error ("cannot find protocol declaration for %qE",
2857 ident);
2860 return return_value;
2863 static void
2864 build_common_objc_exception_stuff (void)
2866 tree noreturn_list, nothrow_list, temp_type;
2868 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
2869 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
2871 /* void objc_exception_throw(id) __attribute__((noreturn)); */
2872 /* void objc_sync_enter(id); */
2873 /* void objc_sync_exit(id); */
2874 temp_type = build_function_type_list (void_type_node,
2875 objc_object_type,
2876 NULL_TREE);
2877 objc_exception_throw_decl
2878 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
2879 noreturn_list);
2880 /* Make sure that objc_exception_throw (id) claims that it may throw an
2881 exception. */
2882 TREE_NOTHROW (objc_exception_throw_decl) = 0;
2884 objc_sync_enter_decl
2885 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
2886 NULL, nothrow_list);
2888 objc_sync_exit_decl
2889 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
2890 NULL, nothrow_list);
2893 /* Purpose: "play" parser, creating/installing representations
2894 of the declarations that are required by Objective-C.
2896 Model:
2898 type_spec--------->sc_spec
2899 (tree_list) (tree_list)
2902 identifier_node identifier_node */
2904 static void
2905 synth_module_prologue (void)
2907 tree type;
2908 enum debug_info_type save_write_symbols = write_symbols;
2909 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
2911 /* Suppress outputting debug symbols, because
2912 dbxout_init hasn't been called yet. */
2913 write_symbols = NO_DEBUG;
2914 debug_hooks = &do_nothing_debug_hooks;
2916 #ifdef OBJCPLUS
2917 push_lang_context (lang_name_c); /* extern "C" */
2918 #endif
2920 /* The following are also defined in <objc/objc.h> and friends. */
2922 objc_object_id = get_identifier (TAG_OBJECT);
2923 objc_class_id = get_identifier (TAG_CLASS);
2925 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
2926 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
2928 objc_object_type = build_pointer_type (objc_object_reference);
2929 objc_class_type = build_pointer_type (objc_class_reference);
2931 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
2932 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
2934 /* Declare the 'id' and 'Class' typedefs. */
2935 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2936 TYPE_DECL,
2937 objc_object_name,
2938 objc_object_type));
2939 TREE_NO_WARNING (type) = 1;
2941 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2942 TYPE_DECL,
2943 objc_class_name,
2944 objc_class_type));
2945 TREE_NO_WARNING (type) = 1;
2947 /* Forward-declare '@interface Protocol'. */
2948 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
2949 objc_declare_class (type);
2950 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE, type));
2952 /* Declare receiver type used for dispatching messages to 'super'. */
2953 /* `struct objc_super *' */
2954 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
2955 get_identifier (TAG_SUPER)));
2957 /* Declare pointers to method and ivar lists. */
2958 objc_method_list_ptr = build_pointer_type
2959 (xref_tag (RECORD_TYPE,
2960 get_identifier (UTAG_METHOD_LIST)));
2961 objc_method_proto_list_ptr
2962 = build_pointer_type (xref_tag (RECORD_TYPE,
2963 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
2964 objc_ivar_list_ptr = build_pointer_type
2965 (xref_tag (RECORD_TYPE,
2966 get_identifier (UTAG_IVAR_LIST)));
2968 build_common_objc_exception_stuff ();
2970 /* Set-up runtime-specific templates, message and exception stuff. */
2971 (*runtime.initialize) ();
2973 /* Declare objc_getProperty, object_setProperty and other property
2974 accessor helpers. */
2975 build_common_objc_property_accessor_helpers ();
2977 /* Forward declare constant_string_id and constant_string_type. */
2978 if (!constant_string_class_name)
2979 constant_string_class_name = runtime.default_constant_string_class_name;
2980 constant_string_id = get_identifier (constant_string_class_name);
2981 objc_declare_class (constant_string_id);
2983 /* Pre-build the following entities - for speed/convenience. */
2984 self_id = get_identifier ("self");
2985 ucmd_id = get_identifier ("_cmd");
2987 /* Declare struct _objc_fast_enumeration_state { ... }; */
2988 build_fast_enumeration_state_template ();
2990 /* void objc_enumeration_mutation (id) */
2991 type = build_function_type_list (void_type_node,
2992 objc_object_type, NULL_TREE);
2993 objc_enumeration_mutation_decl
2994 = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN,
2995 NULL, NULL_TREE);
2996 TREE_NOTHROW (objc_enumeration_mutation_decl) = 0;
2998 #ifdef OBJCPLUS
2999 pop_lang_context ();
3000 #endif
3002 write_symbols = save_write_symbols;
3003 debug_hooks = save_hooks;
3006 /* --- const strings --- */
3008 /* Ensure that the ivar list for NSConstantString/NXConstantString
3009 (or whatever was specified via `-fconstant-string-class')
3010 contains fields at least as large as the following three, so that
3011 the runtime can stomp on them with confidence:
3013 struct STRING_OBJECT_CLASS_NAME
3015 Object isa;
3016 char *cString;
3017 unsigned int length;
3018 }; */
3020 static int
3021 check_string_class_template (void)
3023 tree field_decl = objc_get_class_ivars (constant_string_id);
3025 #define AT_LEAST_AS_LARGE_AS(F, T) \
3026 (F && TREE_CODE (F) == FIELD_DECL \
3027 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
3028 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
3030 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3031 return 0;
3033 field_decl = DECL_CHAIN (field_decl);
3034 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3035 return 0;
3037 field_decl = DECL_CHAIN (field_decl);
3038 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
3040 #undef AT_LEAST_AS_LARGE_AS
3043 /* Avoid calling `check_string_class_template ()' more than once. */
3044 static GTY(()) int string_layout_checked;
3046 /* Construct an internal string layout to be used as a template for
3047 creating NSConstantString/NXConstantString instances. */
3049 static tree
3050 objc_build_internal_const_str_type (void)
3052 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
3053 tree fields = build_decl (input_location,
3054 FIELD_DECL, NULL_TREE, ptr_type_node);
3055 tree field = build_decl (input_location,
3056 FIELD_DECL, NULL_TREE, ptr_type_node);
3058 DECL_CHAIN (field) = fields; fields = field;
3059 field = build_decl (input_location,
3060 FIELD_DECL, NULL_TREE, unsigned_type_node);
3061 DECL_CHAIN (field) = fields; fields = field;
3062 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
3063 reverse order! */
3064 finish_builtin_struct (type, "__builtin_ObjCString",
3065 fields, NULL_TREE);
3067 return type;
3070 /* Custom build_string which sets TREE_TYPE! */
3072 tree
3073 my_build_string (int len, const char *str)
3075 return fix_string_type (build_string (len, str));
3078 /* Build a string with contents STR and length LEN and convert it to a
3079 pointer. */
3081 tree
3082 my_build_string_pointer (int len, const char *str)
3084 tree string = my_build_string (len, str);
3085 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
3086 return build1 (ADDR_EXPR, ptrtype, string);
3089 static hashval_t
3090 string_hash (const void *ptr)
3092 const_tree const str = ((const struct string_descriptor *)ptr)->literal;
3093 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3094 int i, len = TREE_STRING_LENGTH (str);
3095 hashval_t h = len;
3097 for (i = 0; i < len; i++)
3098 h = ((h * 613) + p[i]);
3100 return h;
3103 static int
3104 string_eq (const void *ptr1, const void *ptr2)
3106 const_tree const str1 = ((const struct string_descriptor *)ptr1)->literal;
3107 const_tree const str2 = ((const struct string_descriptor *)ptr2)->literal;
3108 int len1 = TREE_STRING_LENGTH (str1);
3110 return (len1 == TREE_STRING_LENGTH (str2)
3111 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3112 len1));
3115 /* Given a chain of STRING_CST's, build a static instance of
3116 NXConstantString which points at the concatenation of those
3117 strings. We place the string object in the __string_objects
3118 section of the __OBJC segment. The Objective-C runtime will
3119 initialize the isa pointers of the string objects to point at the
3120 NXConstantString class object. */
3122 tree
3123 objc_build_string_object (tree string)
3125 tree constant_string_class;
3126 int length;
3127 tree addr;
3128 struct string_descriptor *desc, key;
3129 void **loc;
3131 /* Prep the string argument. */
3132 string = fix_string_type (string);
3133 TREE_SET_CODE (string, STRING_CST);
3134 length = TREE_STRING_LENGTH (string) - 1;
3136 /* The target may have different ideas on how to construct an ObjC string
3137 literal. On Darwin (Mac OS X), for example, we may wish to obtain a
3138 constant CFString reference instead.
3139 At present, this is only supported for the NeXT runtime. */
3140 if (flag_next_runtime
3141 && targetcm.objc_construct_string_object)
3143 tree constructor = (*targetcm.objc_construct_string_object) (string);
3144 if (constructor)
3145 return build1 (NOP_EXPR, objc_object_type, constructor);
3148 /* Check whether the string class being used actually exists and has the
3149 correct ivar layout. */
3150 if (!string_layout_checked)
3152 string_layout_checked = -1;
3153 constant_string_class = lookup_interface (constant_string_id);
3154 internal_const_str_type = objc_build_internal_const_str_type ();
3156 if (!constant_string_class
3157 || !(constant_string_type
3158 = CLASS_STATIC_TEMPLATE (constant_string_class)))
3159 error ("cannot find interface declaration for %qE",
3160 constant_string_id);
3161 /* The NSConstantString/NXConstantString ivar layout is now known. */
3162 else if (!check_string_class_template ())
3163 error ("interface %qE does not have valid constant string layout",
3164 constant_string_id);
3165 /* If the runtime can generate a literal reference to the string class,
3166 don't need to run a constructor. */
3167 else if (!(*runtime.setup_const_string_class_decl)())
3168 error ("cannot find reference tag for class %qE", constant_string_id);
3169 else
3171 string_layout_checked = 1; /* Success! */
3172 add_class_reference (constant_string_id);
3176 if (string_layout_checked == -1)
3177 return error_mark_node;
3179 /* Perhaps we already constructed a constant string just like this one? */
3180 key.literal = string;
3181 loc = htab_find_slot (string_htab, &key, INSERT);
3182 desc = (struct string_descriptor *) *loc;
3184 if (!desc)
3186 *loc = desc = ggc_alloc_string_descriptor ();
3187 desc->literal = string;
3188 desc->constructor =
3189 (*runtime.build_const_string_constructor) (input_location, string, length);
3192 addr = convert (build_pointer_type (constant_string_type),
3193 build_unary_op (input_location,
3194 ADDR_EXPR, desc->constructor, 1));
3196 return addr;
3199 /* Build a static constant CONSTRUCTOR
3200 with type TYPE and elements ELTS. */
3202 tree
3203 objc_build_constructor (tree type, VEC(constructor_elt,gc) *elts)
3205 tree constructor = build_constructor (type, elts);
3207 TREE_CONSTANT (constructor) = 1;
3208 TREE_STATIC (constructor) = 1;
3209 TREE_READONLY (constructor) = 1;
3211 #ifdef OBJCPLUS
3212 /* Adjust for impedance mismatch. We should figure out how to build
3213 CONSTRUCTORs that consistently please both the C and C++ gods. */
3214 if (!VEC_index (constructor_elt, elts, 0)->index)
3215 TREE_TYPE (constructor) = init_list_type_node;
3216 #endif
3218 return constructor;
3221 /* Return the DECL of the string IDENT in the SECTION. */
3223 tree
3224 get_objc_string_decl (tree ident, enum string_section section)
3226 tree chain;
3228 switch (section)
3230 case class_names:
3231 chain = class_names_chain;
3232 break;
3233 case meth_var_names:
3234 chain = meth_var_names_chain;
3235 break;
3236 case meth_var_types:
3237 chain = meth_var_types_chain;
3238 break;
3239 case prop_names_attr:
3240 chain = prop_names_attr_chain;
3241 break;
3242 default:
3243 gcc_unreachable ();
3246 for (; chain != 0; chain = TREE_CHAIN (chain))
3247 if (TREE_VALUE (chain) == ident)
3248 return (TREE_PURPOSE (chain));
3250 /* We didn't find the entry. */
3251 return NULL_TREE;
3254 /* Create a class reference, but don't create a variable to reference
3255 it. */
3257 void
3258 add_class_reference (tree ident)
3260 tree chain;
3262 if ((chain = cls_ref_chain))
3264 tree tail;
3267 if (ident == TREE_VALUE (chain))
3268 return;
3270 tail = chain;
3271 chain = TREE_CHAIN (chain);
3273 while (chain);
3275 /* Append to the end of the list */
3276 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
3278 else
3279 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
3282 /* Get a class reference, creating it if necessary. Also create the
3283 reference variable. */
3284 tree
3285 objc_get_class_reference (tree ident)
3287 tree orig_ident = (DECL_P (ident)
3288 ? DECL_NAME (ident)
3289 : TYPE_P (ident)
3290 ? OBJC_TYPE_NAME (ident)
3291 : ident);
3292 bool local_scope = false;
3294 #ifdef OBJCPLUS
3295 if (processing_template_decl)
3296 /* Must wait until template instantiation time. */
3297 return build_min_nt (CLASS_REFERENCE_EXPR, ident);
3298 #endif
3300 if (TREE_CODE (ident) == TYPE_DECL)
3301 ident = (DECL_ORIGINAL_TYPE (ident)
3302 ? DECL_ORIGINAL_TYPE (ident)
3303 : TREE_TYPE (ident));
3305 #ifdef OBJCPLUS
3306 if (TYPE_P (ident)
3307 && CP_TYPE_CONTEXT (ident) != global_namespace)
3308 local_scope = true;
3309 #endif
3311 if (local_scope || !(ident = objc_is_class_name (ident)))
3313 error ("%qE is not an Objective-C class name or alias",
3314 orig_ident);
3315 return error_mark_node;
3318 return (*runtime.get_class_reference) (ident);
3321 void
3322 objc_declare_alias (tree alias_ident, tree class_ident)
3324 tree underlying_class;
3326 #ifdef OBJCPLUS
3327 if (current_namespace != global_namespace) {
3328 error ("Objective-C declarations may only appear in global scope");
3330 #endif /* OBJCPLUS */
3332 if (!(underlying_class = objc_is_class_name (class_ident)))
3333 warning (0, "cannot find class %qE", class_ident);
3334 else if (objc_is_class_name (alias_ident))
3335 warning (0, "class %qE already exists", alias_ident);
3336 else
3338 /* Implement @compatibility_alias as a typedef. */
3339 #ifdef OBJCPLUS
3340 push_lang_context (lang_name_c); /* extern "C" */
3341 #endif
3342 lang_hooks.decls.pushdecl (build_decl
3343 (input_location,
3344 TYPE_DECL,
3345 alias_ident,
3346 xref_tag (RECORD_TYPE, underlying_class)));
3347 #ifdef OBJCPLUS
3348 pop_lang_context ();
3349 #endif
3350 objc_map_put (alias_name_map, alias_ident, underlying_class);
3354 void
3355 objc_declare_class (tree identifier)
3357 #ifdef OBJCPLUS
3358 if (current_namespace != global_namespace) {
3359 error ("Objective-C declarations may only appear in global scope");
3361 #endif /* OBJCPLUS */
3363 if (! objc_is_class_name (identifier))
3365 tree record = lookup_name (identifier), type = record;
3367 if (record)
3369 if (TREE_CODE (record) == TYPE_DECL)
3370 type = DECL_ORIGINAL_TYPE (record)
3371 ? DECL_ORIGINAL_TYPE (record)
3372 : TREE_TYPE (record);
3374 if (!TYPE_HAS_OBJC_INFO (type)
3375 || !TYPE_OBJC_INTERFACE (type))
3377 error ("%qE redeclared as different kind of symbol",
3378 identifier);
3379 error ("previous declaration of %q+D",
3380 record);
3384 record = xref_tag (RECORD_TYPE, identifier);
3385 INIT_TYPE_OBJC_INFO (record);
3386 /* In the case of a @class declaration, we store the ident in
3387 the TYPE_OBJC_INTERFACE. If later an @interface is found,
3388 we'll replace the ident with the interface. */
3389 TYPE_OBJC_INTERFACE (record) = identifier;
3390 objc_map_put (class_name_map, identifier, NULL_TREE);
3394 tree
3395 objc_is_class_name (tree ident)
3397 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE)
3399 tree t = identifier_global_value (ident);
3400 if (t)
3401 ident = t;
3404 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
3405 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
3407 if (ident && TREE_CODE (ident) == RECORD_TYPE)
3408 ident = OBJC_TYPE_NAME (ident);
3409 #ifdef OBJCPLUS
3410 if (ident && TREE_CODE (ident) == TYPE_DECL)
3412 tree type = TREE_TYPE (ident);
3413 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
3414 return NULL_TREE;
3415 ident = DECL_NAME (ident);
3417 #endif
3418 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
3419 return NULL_TREE;
3421 if (lookup_interface (ident))
3422 return ident;
3425 tree target;
3427 target = objc_map_get (class_name_map, ident);
3428 if (target != OBJC_MAP_NOT_FOUND)
3429 return ident;
3431 target = objc_map_get (alias_name_map, ident);
3432 if (target != OBJC_MAP_NOT_FOUND)
3433 return target;
3436 return 0;
3439 /* Check whether TYPE is either 'id' or 'Class'. */
3441 tree
3442 objc_is_id (tree type)
3444 if (type && TREE_CODE (type) == IDENTIFIER_NODE)
3446 tree t = identifier_global_value (type);
3447 if (t)
3448 type = t;
3451 if (type && TREE_CODE (type) == TYPE_DECL)
3452 type = TREE_TYPE (type);
3454 /* NB: This function may be called before the ObjC front-end has
3455 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3456 return (objc_object_type && type
3457 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
3458 ? type
3459 : NULL_TREE);
3462 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3463 class instance. This is needed by other parts of the compiler to
3464 handle ObjC types gracefully. */
3466 tree
3467 objc_is_object_ptr (tree type)
3469 tree ret;
3471 type = TYPE_MAIN_VARIANT (type);
3472 if (!POINTER_TYPE_P (type))
3473 return 0;
3475 ret = objc_is_id (type);
3476 if (!ret)
3477 ret = objc_is_class_name (TREE_TYPE (type));
3479 return ret;
3482 static int
3483 objc_is_gcable_type (tree type, int or_strong_p)
3485 tree name;
3487 if (!TYPE_P (type))
3488 return 0;
3489 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
3490 return 1;
3491 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
3492 return 1;
3493 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
3494 return 0;
3495 type = TREE_TYPE (type);
3496 if (TREE_CODE (type) != RECORD_TYPE)
3497 return 0;
3498 name = TYPE_NAME (type);
3499 return (objc_is_class_name (name) != NULL_TREE);
3502 static tree
3503 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
3505 if (expr == oldexpr)
3506 return newexpr;
3508 switch (TREE_CODE (expr))
3510 case COMPONENT_REF:
3511 return objc_build_component_ref
3512 (objc_substitute_decl (TREE_OPERAND (expr, 0),
3513 oldexpr,
3514 newexpr),
3515 DECL_NAME (TREE_OPERAND (expr, 1)));
3516 case ARRAY_REF:
3517 return build_array_ref (input_location,
3518 objc_substitute_decl (TREE_OPERAND (expr, 0),
3519 oldexpr,
3520 newexpr),
3521 TREE_OPERAND (expr, 1));
3522 case INDIRECT_REF:
3523 return build_indirect_ref (input_location,
3524 objc_substitute_decl (TREE_OPERAND (expr, 0),
3525 oldexpr,
3526 newexpr), RO_ARROW);
3527 default:
3528 return expr;
3532 static tree
3533 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
3535 tree func_params;
3536 /* The LHS parameter contains the expression 'outervar->memberspec';
3537 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3538 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3540 tree offs
3541 = objc_substitute_decl
3542 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
3543 tree func
3544 = (flag_objc_direct_dispatch
3545 ? objc_assign_ivar_fast_decl
3546 : objc_assign_ivar_decl);
3548 offs = convert (integer_type_node, build_unary_op (input_location,
3549 ADDR_EXPR, offs, 0));
3550 offs = fold (offs);
3551 func_params = tree_cons (NULL_TREE,
3552 convert (objc_object_type, rhs),
3553 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
3554 tree_cons (NULL_TREE, offs,
3555 NULL_TREE)));
3557 assemble_external (func);
3558 return build_function_call (input_location, func, func_params);
3561 static tree
3562 objc_build_global_assignment (tree lhs, tree rhs)
3564 tree func_params = tree_cons (NULL_TREE,
3565 convert (objc_object_type, rhs),
3566 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3567 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3568 NULL_TREE));
3570 assemble_external (objc_assign_global_decl);
3571 return build_function_call (input_location,
3572 objc_assign_global_decl, func_params);
3575 static tree
3576 objc_build_strong_cast_assignment (tree lhs, tree rhs)
3578 tree func_params = tree_cons (NULL_TREE,
3579 convert (objc_object_type, rhs),
3580 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3581 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3582 NULL_TREE));
3584 assemble_external (objc_assign_strong_cast_decl);
3585 return build_function_call (input_location,
3586 objc_assign_strong_cast_decl, func_params);
3589 static int
3590 objc_is_gcable_p (tree expr)
3592 return (TREE_CODE (expr) == COMPONENT_REF
3593 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
3594 : TREE_CODE (expr) == ARRAY_REF
3595 ? (objc_is_gcable_p (TREE_TYPE (expr))
3596 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
3597 : TREE_CODE (expr) == ARRAY_TYPE
3598 ? objc_is_gcable_p (TREE_TYPE (expr))
3599 : TYPE_P (expr)
3600 ? objc_is_gcable_type (expr, 1)
3601 : (objc_is_gcable_p (TREE_TYPE (expr))
3602 || (DECL_P (expr)
3603 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
3606 static int
3607 objc_is_ivar_reference_p (tree expr)
3609 return (TREE_CODE (expr) == ARRAY_REF
3610 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
3611 : TREE_CODE (expr) == COMPONENT_REF
3612 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
3613 : 0);
3616 static int
3617 objc_is_global_reference_p (tree expr)
3619 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
3620 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
3621 : DECL_P (expr)
3622 ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr))
3623 : 0);
3626 tree
3627 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
3629 tree result = NULL_TREE, outer;
3630 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
3632 /* This function is currently only used with the next runtime with
3633 garbage collection enabled (-fobjc-gc). */
3634 gcc_assert (flag_next_runtime);
3636 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3637 will have been transformed to the form '*(type *)&expr'. */
3638 if (TREE_CODE (lhs) == INDIRECT_REF)
3640 outer = TREE_OPERAND (lhs, 0);
3642 while (!strong_cast_p
3643 && (CONVERT_EXPR_P (outer)
3644 || TREE_CODE (outer) == NON_LVALUE_EXPR))
3646 tree lhstype = TREE_TYPE (outer);
3648 /* Descend down the cast chain, and record the first objc_gc
3649 attribute found. */
3650 if (POINTER_TYPE_P (lhstype))
3652 tree attr
3653 = lookup_attribute ("objc_gc",
3654 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
3656 if (attr)
3657 strong_cast_p = 1;
3660 outer = TREE_OPERAND (outer, 0);
3664 /* If we have a __strong cast, it trumps all else. */
3665 if (strong_cast_p)
3667 if (modifycode != NOP_EXPR)
3668 goto invalid_pointer_arithmetic;
3670 if (warn_assign_intercept)
3671 warning (0, "strong-cast assignment has been intercepted");
3673 result = objc_build_strong_cast_assignment (lhs, rhs);
3675 goto exit_point;
3678 /* the lhs must be of a suitable type, regardless of its underlying
3679 structure. */
3680 if (!objc_is_gcable_p (lhs))
3681 goto exit_point;
3683 outer = lhs;
3685 while (outer
3686 && (TREE_CODE (outer) == COMPONENT_REF
3687 || TREE_CODE (outer) == ARRAY_REF))
3688 outer = TREE_OPERAND (outer, 0);
3690 if (TREE_CODE (outer) == INDIRECT_REF)
3692 outer = TREE_OPERAND (outer, 0);
3693 indirect_p = 1;
3696 outer_gc_p = objc_is_gcable_p (outer);
3698 /* Handle ivar assignments. */
3699 if (objc_is_ivar_reference_p (lhs))
3701 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3702 doesn't cut it here), the best we can do here is suggest a cast. */
3703 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
3705 /* We may still be able to use the global write barrier... */
3706 if (!indirect_p && objc_is_global_reference_p (outer))
3707 goto global_reference;
3709 suggest_cast:
3710 if (modifycode == NOP_EXPR)
3712 if (warn_assign_intercept)
3713 warning (0, "strong-cast may possibly be needed");
3716 goto exit_point;
3719 if (modifycode != NOP_EXPR)
3720 goto invalid_pointer_arithmetic;
3722 if (warn_assign_intercept)
3723 warning (0, "instance variable assignment has been intercepted");
3725 result = objc_build_ivar_assignment (outer, lhs, rhs);
3727 goto exit_point;
3730 /* Likewise, intercept assignment to global/static variables if their type is
3731 GC-marked. */
3732 if (objc_is_global_reference_p (outer))
3734 if (indirect_p)
3735 goto suggest_cast;
3737 global_reference:
3738 if (modifycode != NOP_EXPR)
3740 invalid_pointer_arithmetic:
3741 if (outer_gc_p)
3742 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3744 goto exit_point;
3747 if (warn_assign_intercept)
3748 warning (0, "global/static variable assignment has been intercepted");
3750 result = objc_build_global_assignment (lhs, rhs);
3753 /* In all other cases, fall back to the normal mechanism. */
3754 exit_point:
3755 return result;
3758 /* Implementation of the table mapping a class name (as an identifier)
3759 to a class node. The two public functions for it are
3760 lookup_interface() and add_interface(). add_interface() is only
3761 used in this file, so we can make it static. */
3763 static GTY(()) objc_map_t interface_map;
3765 static void
3766 interface_hash_init (void)
3768 interface_map = objc_map_alloc_ggc (200);
3771 static tree
3772 add_interface (tree class_name, tree name)
3774 /* Put interfaces on list in reverse order. */
3775 TREE_CHAIN (class_name) = interface_chain;
3776 interface_chain = class_name;
3778 /* Add it to the map. */
3779 objc_map_put (interface_map, name, class_name);
3781 return interface_chain;
3784 tree
3785 lookup_interface (tree ident)
3787 #ifdef OBJCPLUS
3788 if (ident && TREE_CODE (ident) == TYPE_DECL)
3789 ident = DECL_NAME (ident);
3790 #endif
3792 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
3793 return NULL_TREE;
3796 tree interface = objc_map_get (interface_map, ident);
3798 if (interface == OBJC_MAP_NOT_FOUND)
3799 return NULL_TREE;
3800 else
3801 return interface;
3807 /* Implement @defs (<classname>) within struct bodies. */
3809 tree
3810 objc_get_class_ivars (tree class_name)
3812 tree interface = lookup_interface (class_name);
3814 if (interface)
3815 return get_class_ivars (interface, true);
3817 error ("cannot find interface declaration for %qE",
3818 class_name);
3820 return error_mark_node;
3824 /* Functions used by the hashtable for field duplicates in
3825 objc_detect_field_duplicates(). Ideally, we'd use a standard
3826 key-value dictionary hashtable , and store as keys the field names,
3827 and as values the actual declarations (used to print nice error
3828 messages with the locations). But, the hashtable we are using only
3829 allows us to store keys in the hashtable, without values (it looks
3830 more like a set). So, we store the DECLs, but define equality as
3831 DECLs having the same name, and hash as the hash of the name. */
3832 static hashval_t
3833 hash_instance_variable (const PTR p)
3835 const_tree q = (const_tree)p;
3836 return (hashval_t) ((intptr_t)(DECL_NAME (q)) >> 3);
3839 static int
3840 eq_instance_variable (const PTR p1, const PTR p2)
3842 const_tree a = (const_tree)p1;
3843 const_tree b = (const_tree)p2;
3844 return DECL_NAME (a) == DECL_NAME (b);
3847 /* Called when checking the variables in a struct. If we are not
3848 doing the ivars list inside an @interface context, then return
3849 false. Else, perform the check for duplicate ivars, then return
3850 true. The check for duplicates checks if an instance variable with
3851 the same name exists in the class or in a superclass. If
3852 'check_superclasses_only' is set to true, then it is assumed that
3853 checks for instance variables in the same class has already been
3854 performed (this is the case for ObjC++) and only the instance
3855 variables of superclasses are checked. */
3856 bool
3857 objc_detect_field_duplicates (bool check_superclasses_only)
3859 if (!objc_collecting_ivars || !objc_interface_context
3860 || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE)
3861 return false;
3863 /* We have two ways of doing this check:
3865 "direct comparison": we iterate over the instance variables and
3866 compare them directly. This works great for small numbers of
3867 instance variables (such as 10 or 20), which are extremely common.
3868 But it will potentially take forever for the pathological case with
3869 a huge number (eg, 10k) of instance variables.
3871 "hashtable": we use a hashtable, which requires a single sweep
3872 through the list of instances variables. This is much slower for a
3873 small number of variables, and we only use it for large numbers.
3875 To decide which one to use, we need to get an idea of how many
3876 instance variables we have to compare. */
3878 unsigned int number_of_ivars_to_check = 0;
3880 tree ivar;
3881 for (ivar = CLASS_RAW_IVARS (objc_interface_context);
3882 ivar; ivar = DECL_CHAIN (ivar))
3884 /* Ignore anonymous ivars. */
3885 if (DECL_NAME (ivar))
3886 number_of_ivars_to_check++;
3890 /* Exit if there is nothing to do. */
3891 if (number_of_ivars_to_check == 0)
3892 return true;
3894 /* In case that there are only 1 or 2 instance variables to check,
3895 we always use direct comparison. If there are more, it is
3896 worth iterating over the instance variables in the superclass
3897 to count how many there are (note that this has the same cost
3898 as checking 1 instance variable by direct comparison, which is
3899 why we skip this check in the case of 1 or 2 ivars and just do
3900 the direct comparison) and then decide if it worth using a
3901 hashtable. */
3902 if (number_of_ivars_to_check > 2)
3904 unsigned int number_of_superclass_ivars = 0;
3906 tree interface;
3907 for (interface = lookup_interface (CLASS_SUPER_NAME (objc_interface_context));
3908 interface; interface = lookup_interface (CLASS_SUPER_NAME (interface)))
3910 tree ivar;
3911 for (ivar = CLASS_RAW_IVARS (interface);
3912 ivar; ivar = DECL_CHAIN (ivar))
3913 number_of_superclass_ivars++;
3917 /* We use a hashtable if we have over 10k comparisons. */
3918 if (number_of_ivars_to_check * (number_of_superclass_ivars
3919 + (number_of_ivars_to_check / 2))
3920 > 10000)
3922 /* First, build the hashtable by putting all the instance
3923 variables of superclasses in it. */
3924 htab_t htab = htab_create (37, hash_instance_variable,
3925 eq_instance_variable, NULL);
3926 tree interface;
3927 for (interface = lookup_interface (CLASS_SUPER_NAME
3928 (objc_interface_context));
3929 interface; interface = lookup_interface
3930 (CLASS_SUPER_NAME (interface)))
3932 tree ivar;
3933 for (ivar = CLASS_RAW_IVARS (interface); ivar;
3934 ivar = DECL_CHAIN (ivar))
3936 if (DECL_NAME (ivar) != NULL_TREE)
3938 void **slot = htab_find_slot (htab, ivar, INSERT);
3939 /* Do not check for duplicate instance
3940 variables in superclasses. Errors have
3941 already been generated. */
3942 *slot = ivar;
3947 /* Now, we go through all the instance variables in the
3948 class, and check that they are not in the
3949 hashtable. */
3950 if (check_superclasses_only)
3952 tree ivar;
3953 for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar;
3954 ivar = DECL_CHAIN (ivar))
3956 if (DECL_NAME (ivar) != NULL_TREE)
3958 tree duplicate_ivar = (tree)(htab_find (htab, ivar));
3959 if (duplicate_ivar != HTAB_EMPTY_ENTRY)
3961 error_at (DECL_SOURCE_LOCATION (ivar),
3962 "duplicate instance variable %q+D",
3963 ivar);
3964 inform (DECL_SOURCE_LOCATION (duplicate_ivar),
3965 "previous declaration of %q+D",
3966 duplicate_ivar);
3967 /* FIXME: Do we need the following ? */
3968 /* DECL_NAME (ivar) = NULL_TREE; */
3973 else
3975 /* If we're checking for duplicates in the class as
3976 well, we insert variables in the hashtable as we
3977 check them, so if a duplicate follows, it will be
3978 caught. */
3979 tree ivar;
3980 for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar;
3981 ivar = DECL_CHAIN (ivar))
3983 if (DECL_NAME (ivar) != NULL_TREE)
3985 void **slot = htab_find_slot (htab, ivar, INSERT);
3986 if (*slot)
3988 tree duplicate_ivar = (tree)(*slot);
3989 error_at (DECL_SOURCE_LOCATION (ivar),
3990 "duplicate instance variable %q+D",
3991 ivar);
3992 inform (DECL_SOURCE_LOCATION (duplicate_ivar),
3993 "previous declaration of %q+D",
3994 duplicate_ivar);
3995 /* FIXME: Do we need the following ? */
3996 /* DECL_NAME (ivar) = NULL_TREE; */
3998 *slot = ivar;
4002 htab_delete (htab);
4003 return true;
4008 /* This is the "direct comparison" approach, which is used in most
4009 non-pathological cases. */
4011 /* Walk up to class hierarchy, starting with this class (this is
4012 the external loop, because lookup_interface() is expensive, and
4013 we want to do it few times). */
4014 tree interface = objc_interface_context;
4016 if (check_superclasses_only)
4017 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4019 for ( ; interface; interface = lookup_interface
4020 (CLASS_SUPER_NAME (interface)))
4022 tree ivar_being_checked;
4024 for (ivar_being_checked = CLASS_RAW_IVARS (objc_interface_context);
4025 ivar_being_checked;
4026 ivar_being_checked = DECL_CHAIN (ivar_being_checked))
4028 tree decl;
4030 /* Ignore anonymous ivars. */
4031 if (DECL_NAME (ivar_being_checked) == NULL_TREE)
4032 continue;
4034 /* Note how we stop when we find the ivar we are checking
4035 (this can only happen in the main class, not
4036 superclasses), to avoid comparing things twice
4037 (otherwise, for each ivar, you'd compare A to B then B
4038 to A, and get duplicated error messages). */
4039 for (decl = CLASS_RAW_IVARS (interface);
4040 decl && decl != ivar_being_checked;
4041 decl = DECL_CHAIN (decl))
4043 if (DECL_NAME (ivar_being_checked) == DECL_NAME (decl))
4045 error_at (DECL_SOURCE_LOCATION (ivar_being_checked),
4046 "duplicate instance variable %q+D",
4047 ivar_being_checked);
4048 inform (DECL_SOURCE_LOCATION (decl),
4049 "previous declaration of %q+D",
4050 decl);
4051 /* FIXME: Do we need the following ? */
4052 /* DECL_NAME (ivar_being_checked) = NULL_TREE; */
4058 return true;
4061 /* Used by: build_private_template, continue_class,
4062 and for @defs constructs. */
4064 static tree
4065 get_class_ivars (tree interface, bool inherited)
4067 tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface));
4069 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
4070 by the current class (i.e., they do not include super-class ivars).
4071 However, the CLASS_IVARS list will be side-effected by a call to
4072 finish_struct(), which will fill in field offsets. */
4073 if (!CLASS_IVARS (interface))
4074 CLASS_IVARS (interface) = ivar_chain;
4076 if (!inherited)
4077 return ivar_chain;
4079 while (CLASS_SUPER_NAME (interface))
4081 /* Prepend super-class ivars. */
4082 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4083 ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)),
4084 ivar_chain);
4087 return ivar_chain;
4090 void
4091 objc_maybe_warn_exceptions (location_t loc)
4093 /* -fobjc-exceptions is required to enable Objective-C exceptions.
4094 For example, on Darwin, ObjC exceptions require a sufficiently
4095 recent version of the runtime, so the user must ask for them
4096 explicitly. On other platforms, at the moment -fobjc-exceptions
4097 triggers -fexceptions which again is required for exceptions to
4098 work. */
4099 if (!flag_objc_exceptions)
4101 /* Warn only once per compilation unit. */
4102 static bool warned = false;
4104 if (!warned)
4106 error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4107 warned = true;
4112 static struct objc_try_context *cur_try_context;
4114 /* Called just after parsing the @try and its associated BODY. We now
4115 must prepare for the tricky bits -- handling the catches and finally. */
4117 void
4118 objc_begin_try_stmt (location_t try_locus, tree body)
4120 struct objc_try_context *c = XCNEW (struct objc_try_context);
4121 c->outer = cur_try_context;
4122 c->try_body = body;
4123 c->try_locus = try_locus;
4124 c->end_try_locus = input_location;
4125 cur_try_context = c;
4127 /* Collect the list of local variables. We'll mark them as volatile
4128 at the end of compilation of this function to prevent them being
4129 clobbered by setjmp/longjmp. */
4130 if (flag_objc_sjlj_exceptions)
4131 objc_mark_locals_volatile (NULL);
4134 /* Called just after parsing "@catch (parm)". Open a binding level,
4135 enter DECL into the binding level, and initialize it. Leave the
4136 binding level open while the body of the compound statement is
4137 parsed. If DECL is NULL_TREE, then we are compiling "@catch(...)"
4138 which we compile as "@catch(id tmp_variable)". */
4140 void
4141 objc_begin_catch_clause (tree decl)
4143 tree compound, type, t;
4144 bool ellipsis = false;
4146 /* Begin a new scope that the entire catch clause will live in. */
4147 compound = c_begin_compound_stmt (true);
4149 /* Create the appropriate declaration for the argument. */
4150 if (decl == error_mark_node)
4151 type = error_mark_node;
4152 else
4154 if (decl == NULL_TREE)
4156 /* If @catch(...) was specified, create a temporary variable of
4157 type 'id' and use it. */
4158 decl = objc_create_temporary_var (objc_object_type, "__objc_generic_catch_var");
4159 DECL_SOURCE_LOCATION (decl) = input_location;
4160 /* ... but allow the runtime to differentiate between ellipsis and the
4161 case of @catch (id xyz). */
4162 ellipsis = true;
4164 else
4166 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
4167 decl = build_decl (input_location,
4168 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
4170 lang_hooks.decls.pushdecl (decl);
4172 /* Mark the declaration as used so you never any warnings whether
4173 you use the exception argument or not. TODO: Implement a
4174 -Wunused-exception-parameter flag, which would cause warnings
4175 if exception parameter is not used. */
4176 TREE_USED (decl) = 1;
4177 DECL_READ_P (decl) = 1;
4179 type = TREE_TYPE (decl);
4182 /* Verify that the type of the catch is valid. It must be a pointer
4183 to an Objective-C class, or "id" (which is catch-all). */
4184 if (type == error_mark_node)
4186 ;/* Just keep going. */
4188 else if (!objc_type_valid_for_messaging (type, false))
4190 error ("@catch parameter is not a known Objective-C class type");
4191 type = error_mark_node;
4193 else if (TYPE_HAS_OBJC_INFO (TREE_TYPE (type))
4194 && TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (type)))
4196 error ("@catch parameter can not be protocol-qualified");
4197 type = error_mark_node;
4199 else if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type)))
4200 /* @catch (id xyz) or @catch (...) but we note this for runtimes that
4201 identify 'id'. */
4203 else
4205 /* If 'type' was built using typedefs, we need to get rid of
4206 them and get a simple pointer to the class. */
4207 bool is_typedef = false;
4208 tree x = TYPE_MAIN_VARIANT (type);
4210 /* Skip from the pointer to the pointee. */
4211 if (TREE_CODE (x) == POINTER_TYPE)
4212 x = TREE_TYPE (x);
4214 /* Traverse typedef aliases */
4215 while (TREE_CODE (x) == RECORD_TYPE && OBJC_TYPE_NAME (x)
4216 && TREE_CODE (OBJC_TYPE_NAME (x)) == TYPE_DECL
4217 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x)))
4219 is_typedef = true;
4220 x = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x));
4223 /* If it was a typedef, build a pointer to the final, original
4224 class. */
4225 if (is_typedef)
4226 type = build_pointer_type (x);
4228 if (cur_try_context->catch_list)
4230 /* Examine previous @catch clauses and see if we've already
4231 caught the type in question. */
4232 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
4233 for (; !tsi_end_p (i); tsi_next (&i))
4235 tree stmt = tsi_stmt (i);
4236 t = CATCH_TYPES (stmt);
4237 if (t == error_mark_node)
4238 continue;
4239 if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type)))
4241 warning (0, "exception of type %<%T%> will be caught",
4242 TREE_TYPE (type));
4243 warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>",
4244 TREE_TYPE (t ? t : objc_object_type));
4245 break;
4251 t = (*runtime.begin_catch) (&cur_try_context, type, decl, compound, ellipsis);
4252 add_stmt (t);
4255 /* Called just after parsing the closing brace of a @catch clause. Close
4256 the open binding level, and record a CATCH_EXPR for it. */
4258 void
4259 objc_finish_catch_clause (void)
4261 tree c = cur_try_context->current_catch;
4262 cur_try_context->current_catch = NULL;
4263 cur_try_context->end_catch_locus = input_location;
4265 CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1);
4267 (*runtime.finish_catch) (&cur_try_context, c);
4270 /* Called after parsing a @finally clause and its associated BODY.
4271 Record the body for later placement. */
4273 void
4274 objc_build_finally_clause (location_t finally_locus, tree body)
4276 cur_try_context->finally_body = body;
4277 cur_try_context->finally_locus = finally_locus;
4278 cur_try_context->end_finally_locus = input_location;
4281 /* Called to finalize a @try construct. */
4283 tree
4284 objc_finish_try_stmt (void)
4286 struct objc_try_context *c = cur_try_context;
4287 tree stmt;
4289 if (c->catch_list == NULL && c->finally_body == NULL)
4290 error ("%<@try%> without %<@catch%> or %<@finally%>");
4292 stmt = (*runtime.finish_try_stmt) (&cur_try_context);
4293 add_stmt (stmt);
4295 cur_try_context = c->outer;
4296 free (c);
4297 return stmt;
4300 tree
4301 objc_build_throw_stmt (location_t loc, tree throw_expr)
4303 bool rethrown = false;
4305 objc_maybe_warn_exceptions (loc);
4307 /* Don't waste time trying to build something if we're already dead. */
4308 if (throw_expr == error_mark_node)
4309 return error_mark_node;
4311 if (throw_expr == NULL)
4313 /* If we're not inside a @catch block, there is no "current
4314 exception" to be rethrown. */
4315 if (cur_try_context == NULL
4316 || cur_try_context->current_catch == NULL)
4318 error_at (loc, "%<@throw%> (rethrow) used outside of a @catch block");
4319 return error_mark_node;
4322 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
4323 value that we get from the runtime. */
4324 throw_expr = (*runtime.build_exc_ptr) (&cur_try_context);
4325 rethrown = true;
4327 else
4329 if (!objc_type_valid_for_messaging (TREE_TYPE (throw_expr), true))
4331 error_at (loc, "%<@throw%> argument is not an object");
4332 return error_mark_node;
4336 return (*runtime.build_throw_stmt) (loc, throw_expr, rethrown);
4339 tree
4340 objc_build_synchronized (location_t start_locus, tree object_expr, tree body)
4342 /* object_expr should never be NULL; but in case it is, convert it to
4343 error_mark_node. */
4344 if (object_expr == NULL)
4345 object_expr = error_mark_node;
4347 /* Validate object_expr. If not valid, set it to error_mark_node. */
4348 if (object_expr != error_mark_node)
4350 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expr), true))
4352 error_at (start_locus, "%<@synchronized%> argument is not an object");
4353 object_expr = error_mark_node;
4357 if (object_expr == error_mark_node)
4359 /* If we found an error, we simply ignore the '@synchronized'.
4360 Compile the body so we can keep going with minimal
4361 casualties. */
4362 return add_stmt (body);
4364 else
4366 tree call;
4367 tree args;
4369 /* objc_sync_enter (object_expr); */
4370 object_expr = save_expr (object_expr);
4371 args = tree_cons (NULL, object_expr, NULL);
4372 call = build_function_call (input_location,
4373 objc_sync_enter_decl, args);
4374 SET_EXPR_LOCATION (call, start_locus);
4375 add_stmt (call);
4377 /* Build "objc_sync_exit (object_expr);" but do not add it yet;
4378 it goes inside the @finalize() clause. */
4379 args = tree_cons (NULL, object_expr, NULL);
4380 call = build_function_call (input_location,
4381 objc_sync_exit_decl, args);
4382 SET_EXPR_LOCATION (call, input_location);
4384 /* @try { body; } */
4385 objc_begin_try_stmt (start_locus, body);
4387 /* @finally { objc_sync_exit (object_expr); } */
4388 objc_build_finally_clause (input_location, call);
4390 /* End of try statement. */
4391 return objc_finish_try_stmt ();
4395 /* Construct a C struct corresponding to ObjC class CLASS, with the same
4396 name as the class:
4398 struct <classname> {
4399 struct _objc_class *isa;
4401 }; */
4403 static void
4404 build_private_template (tree klass)
4406 if (!CLASS_STATIC_TEMPLATE (klass))
4408 tree record = objc_build_struct (klass,
4409 get_class_ivars (klass, false),
4410 CLASS_SUPER_NAME (klass));
4412 /* Set the TREE_USED bit for this struct, so that stab generator
4413 can emit stabs for this struct type. */
4414 if (flag_debug_only_used_symbols && TYPE_STUB_DECL (record))
4415 TREE_USED (TYPE_STUB_DECL (record)) = 1;
4417 /* Copy the attributes from the class to the type. */
4418 if (TREE_DEPRECATED (klass))
4419 TREE_DEPRECATED (record) = 1;
4423 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
4424 current class. */
4425 #ifdef OBJCPLUS
4426 static void
4427 objc_generate_cxx_ctor_or_dtor (bool dtor)
4429 tree fn, body, compound_stmt, ivar;
4431 /* - (id) .cxx_construct { ... return self; } */
4432 /* - (void) .cxx_construct { ... } */
4434 objc_start_method_definition
4435 (false /* is_class_method */,
4436 objc_build_method_signature (false /* is_class_method */,
4437 build_tree_list (NULL_TREE,
4438 dtor
4439 ? void_type_node
4440 : objc_object_type),
4441 get_identifier (dtor
4442 ? TAG_CXX_DESTRUCT
4443 : TAG_CXX_CONSTRUCT),
4444 make_node (TREE_LIST),
4445 false), NULL, NULL_TREE);
4446 body = begin_function_body ();
4447 compound_stmt = begin_compound_stmt (0);
4449 ivar = CLASS_IVARS (implementation_template);
4450 /* Destroy ivars in reverse order. */
4451 if (dtor)
4452 ivar = nreverse (copy_list (ivar));
4454 for (; ivar; ivar = TREE_CHAIN (ivar))
4456 if (TREE_CODE (ivar) == FIELD_DECL)
4458 tree type = TREE_TYPE (ivar);
4460 /* Call the ivar's default constructor or destructor. Do not
4461 call the destructor unless a corresponding constructor call
4462 has also been made (or is not needed). */
4463 if (MAYBE_CLASS_TYPE_P (type)
4464 && (dtor
4465 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4466 && (!TYPE_NEEDS_CONSTRUCTING (type)
4467 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4468 : (TYPE_NEEDS_CONSTRUCTING (type)
4469 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))))
4470 finish_expr_stmt
4471 (build_special_member_call
4472 (build_ivar_reference (DECL_NAME (ivar)),
4473 dtor ? complete_dtor_identifier : complete_ctor_identifier,
4474 NULL, type, LOOKUP_NORMAL, tf_warning_or_error));
4478 /* The constructor returns 'self'. */
4479 if (!dtor)
4480 finish_return_stmt (self_decl);
4482 finish_compound_stmt (compound_stmt);
4483 finish_function_body (body);
4484 fn = current_function_decl;
4485 finish_function ();
4486 objc_finish_method_definition (fn);
4489 /* The following routine will examine the current @interface for any
4490 non-POD C++ ivars requiring non-trivial construction and/or
4491 destruction, and then synthesize special '- .cxx_construct' and/or
4492 '- .cxx_destruct' methods which will run the appropriate
4493 construction or destruction code. Note that ivars inherited from
4494 super-classes are _not_ considered. */
4495 static void
4496 objc_generate_cxx_cdtors (void)
4498 bool need_ctor = false, need_dtor = false;
4499 tree ivar;
4501 /* Error case, due to possibly an extra @end. */
4502 if (!objc_implementation_context)
4503 return;
4505 /* We do not want to do this for categories, since they do not have
4506 their own ivars. */
4508 if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE)
4509 return;
4511 /* First, determine if we even need a constructor and/or destructor. */
4513 for (ivar = CLASS_IVARS (implementation_template); ivar;
4514 ivar = TREE_CHAIN (ivar))
4516 if (TREE_CODE (ivar) == FIELD_DECL)
4518 tree type = TREE_TYPE (ivar);
4520 if (MAYBE_CLASS_TYPE_P (type))
4522 if (TYPE_NEEDS_CONSTRUCTING (type)
4523 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
4524 /* NB: If a default constructor is not available, we will not
4525 be able to initialize this ivar; the add_instance_variable()
4526 routine will already have warned about this. */
4527 need_ctor = true;
4529 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4530 && (!TYPE_NEEDS_CONSTRUCTING (type)
4531 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4532 /* NB: If a default constructor is not available, we will not
4533 call the destructor either, for symmetry. */
4534 need_dtor = true;
4539 /* Generate '- .cxx_construct' if needed. */
4541 if (need_ctor)
4542 objc_generate_cxx_ctor_or_dtor (false);
4544 /* Generate '- .cxx_destruct' if needed. */
4546 if (need_dtor)
4547 objc_generate_cxx_ctor_or_dtor (true);
4549 /* The 'imp_list' variable points at an imp_entry record for the current
4550 @implementation. Record the existence of '- .cxx_construct' and/or
4551 '- .cxx_destruct' methods therein; it will be included in the
4552 metadata for the class if the runtime needs it. */
4553 imp_list->has_cxx_cdtors = (need_ctor || need_dtor);
4555 #endif
4557 static void
4558 error_with_ivar (const char *message, tree decl)
4560 error_at (DECL_SOURCE_LOCATION (decl), "%s %qs",
4561 message, identifier_to_locale (gen_declaration (decl)));
4565 static void
4566 check_ivars (tree inter, tree imp)
4568 tree intdecls = CLASS_RAW_IVARS (inter);
4569 tree impdecls = CLASS_RAW_IVARS (imp);
4571 while (1)
4573 tree t1, t2;
4575 #ifdef OBJCPLUS
4576 if (intdecls && TREE_CODE (intdecls) == TYPE_DECL)
4577 intdecls = TREE_CHAIN (intdecls);
4578 #endif
4579 if (intdecls == 0 && impdecls == 0)
4580 break;
4581 if (intdecls == 0 || impdecls == 0)
4583 error ("inconsistent instance variable specification");
4584 break;
4587 t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
4589 if (!comptypes (t1, t2)
4590 || !tree_int_cst_equal (DECL_INITIAL (intdecls),
4591 DECL_INITIAL (impdecls)))
4593 if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
4595 error_with_ivar ("conflicting instance variable type",
4596 impdecls);
4597 error_with_ivar ("previous declaration of",
4598 intdecls);
4600 else /* both the type and the name don't match */
4602 error ("inconsistent instance variable specification");
4603 break;
4607 else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
4609 error_with_ivar ("conflicting instance variable name",
4610 impdecls);
4611 error_with_ivar ("previous declaration of",
4612 intdecls);
4615 intdecls = DECL_CHAIN (intdecls);
4616 impdecls = DECL_CHAIN (impdecls);
4621 static void
4622 mark_referenced_methods (void)
4624 struct imp_entry *impent;
4625 tree chain;
4627 for (impent = imp_list; impent; impent = impent->next)
4629 chain = CLASS_CLS_METHODS (impent->imp_context);
4630 while (chain)
4632 cgraph_mark_needed_node (
4633 cgraph_get_create_node (METHOD_DEFINITION (chain)));
4634 chain = DECL_CHAIN (chain);
4637 chain = CLASS_NST_METHODS (impent->imp_context);
4638 while (chain)
4640 cgraph_mark_needed_node (
4641 cgraph_get_create_node (METHOD_DEFINITION (chain)));
4642 chain = DECL_CHAIN (chain);
4647 /* If type is empty or only type qualifiers are present, add default
4648 type of id (otherwise grokdeclarator will default to int). */
4649 static inline tree
4650 adjust_type_for_id_default (tree type)
4652 if (!type)
4653 type = make_node (TREE_LIST);
4655 if (!TREE_VALUE (type))
4656 TREE_VALUE (type) = objc_object_type;
4657 else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE
4658 && TYPED_OBJECT (TREE_VALUE (type)))
4659 error ("can not use an object as parameter to a method");
4661 return type;
4664 /* Return a KEYWORD_DECL built using the specified key_name, arg_type,
4665 arg_name and attributes. (TODO: Rename KEYWORD_DECL to
4666 OBJC_METHOD_PARM_DECL ?)
4668 A KEYWORD_DECL is a tree representing the declaration of a
4669 parameter of an Objective-C method. It is produced when parsing a
4670 fragment of Objective-C method declaration of the form
4672 keyworddecl:
4673 selector ':' '(' typename ')' identifier
4675 For example, take the Objective-C method
4677 -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type;
4679 the two fragments "pathForResource:(NSString *)resource" and
4680 "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The
4681 KEYWORD_DECL stores the 'key_name' (eg, identifier for
4682 "pathForResource"), the 'arg_type' (eg, tree representing a
4683 NSString *), the 'arg_name' (eg identifier for "resource") and
4684 potentially some attributes (for example, a tree representing
4685 __attribute__ ((unused)) if such an attribute was attached to a
4686 certain parameter). You can access this information using the
4687 TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name),
4688 KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes).
4690 'key_name' is an identifier node (and is optional as you can omit
4691 it in Objective-C methods).
4692 'arg_type' is a tree list (and is optional too if no parameter type
4693 was specified).
4694 'arg_name' is an identifier node and is required.
4695 'attributes' is an optional tree containing parameter attributes. */
4696 tree
4697 objc_build_keyword_decl (tree key_name, tree arg_type,
4698 tree arg_name, tree attributes)
4700 tree keyword_decl;
4702 if (flag_objc1_only && attributes)
4703 error_at (input_location, "method argument attributes are not available in Objective-C 1.0");
4705 /* If no type is specified, default to "id". */
4706 arg_type = adjust_type_for_id_default (arg_type);
4708 keyword_decl = make_node (KEYWORD_DECL);
4710 TREE_TYPE (keyword_decl) = arg_type;
4711 KEYWORD_ARG_NAME (keyword_decl) = arg_name;
4712 KEYWORD_KEY_NAME (keyword_decl) = key_name;
4713 DECL_ATTRIBUTES (keyword_decl) = attributes;
4715 return keyword_decl;
4718 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
4719 static tree
4720 build_keyword_selector (tree selector)
4722 int len = 0;
4723 tree key_chain, key_name;
4724 char *buf;
4726 /* Scan the selector to see how much space we'll need. */
4727 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
4729 switch (TREE_CODE (selector))
4731 case KEYWORD_DECL:
4732 key_name = KEYWORD_KEY_NAME (key_chain);
4733 break;
4734 case TREE_LIST:
4735 key_name = TREE_PURPOSE (key_chain);
4736 break;
4737 default:
4738 gcc_unreachable ();
4741 if (key_name)
4742 len += IDENTIFIER_LENGTH (key_name) + 1;
4743 else
4744 /* Just a ':' arg. */
4745 len++;
4748 buf = (char *) alloca (len + 1);
4749 /* Start the buffer out as an empty string. */
4750 buf[0] = '\0';
4752 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
4754 switch (TREE_CODE (selector))
4756 case KEYWORD_DECL:
4757 key_name = KEYWORD_KEY_NAME (key_chain);
4758 break;
4759 case TREE_LIST:
4760 key_name = TREE_PURPOSE (key_chain);
4761 /* The keyword decl chain will later be used as a function
4762 argument chain. Unhook the selector itself so as to not
4763 confuse other parts of the compiler. */
4764 TREE_PURPOSE (key_chain) = NULL_TREE;
4765 break;
4766 default:
4767 gcc_unreachable ();
4770 if (key_name)
4771 strcat (buf, IDENTIFIER_POINTER (key_name));
4772 strcat (buf, ":");
4775 return get_identifier_with_length (buf, len);
4778 /* Used for declarations and definitions. */
4780 static tree
4781 build_method_decl (enum tree_code code, tree ret_type, tree selector,
4782 tree add_args, bool ellipsis)
4784 tree method_decl;
4786 /* If no type is specified, default to "id". */
4787 ret_type = adjust_type_for_id_default (ret_type);
4789 /* Note how a method_decl has a TREE_TYPE which is not the function
4790 type of the function implementing the method, but only the return
4791 type of the method. We may want to change this, and store the
4792 entire function type in there (eg, it may be used to simplify
4793 dealing with attributes below). */
4794 method_decl = make_node (code);
4795 TREE_TYPE (method_decl) = ret_type;
4797 /* If we have a keyword selector, create an identifier_node that
4798 represents the full selector name (`:' included)... */
4799 if (TREE_CODE (selector) == KEYWORD_DECL)
4801 METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
4802 METHOD_SEL_ARGS (method_decl) = selector;
4803 METHOD_ADD_ARGS (method_decl) = add_args;
4804 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis;
4806 else
4808 METHOD_SEL_NAME (method_decl) = selector;
4809 METHOD_SEL_ARGS (method_decl) = NULL_TREE;
4810 METHOD_ADD_ARGS (method_decl) = NULL_TREE;
4813 return method_decl;
4816 /* This routine processes objective-c method attributes. */
4818 static void
4819 objc_decl_method_attributes (tree *node, tree attributes, int flags)
4821 /* TODO: Replace the hackery below. An idea would be to store the
4822 full function type in the method declaration (for example in
4823 TREE_TYPE) and then expose ObjC method declarations to c-family
4824 and they could deal with them by simply treating them as
4825 functions. */
4827 /* Because of the dangers in the hackery below, we filter out any
4828 attribute that we do not know about. For the ones we know about,
4829 we know that they work with the hackery. For the other ones,
4830 there is no guarantee, so we have to filter them out. */
4831 tree filtered_attributes = NULL_TREE;
4833 if (attributes)
4835 tree attribute;
4836 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
4838 tree name = TREE_PURPOSE (attribute);
4840 if (is_attribute_p ("deprecated", name)
4841 || is_attribute_p ("sentinel", name)
4842 || is_attribute_p ("noreturn", name))
4844 /* An attribute that we support; add it to the filtered
4845 attributes. */
4846 filtered_attributes = chainon (filtered_attributes,
4847 copy_node (attribute));
4849 else if (is_attribute_p ("format", name))
4851 /* "format" is special because before adding it to the
4852 filtered attributes we need to adjust the specified
4853 format by adding the hidden function parameters for
4854 an Objective-C method (self, _cmd). */
4855 tree new_attribute = copy_node (attribute);
4857 /* Check the arguments specified with the attribute, and
4858 modify them adding 2 for the two hidden arguments.
4859 Note how this differs from C++; according to the
4860 specs, C++ does not do it so you have to add the +1
4861 yourself. For Objective-C, instead, the compiler
4862 adds the +2 for you. */
4864 /* The attribute arguments have not been checked yet, so
4865 we need to be careful as they could be missing or
4866 invalid. If anything looks wrong, we skip the
4867 process and the compiler will complain about it later
4868 when it validates the attribute. */
4869 /* Check that we have at least three arguments. */
4870 if (TREE_VALUE (new_attribute)
4871 && TREE_CHAIN (TREE_VALUE (new_attribute))
4872 && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (new_attribute))))
4874 tree second_argument = TREE_CHAIN (TREE_VALUE (new_attribute));
4875 tree third_argument = TREE_CHAIN (second_argument);
4876 tree number;
4878 /* This is the second argument, the "string-index",
4879 which specifies the index of the format string
4880 argument. Add 2. */
4881 number = TREE_VALUE (second_argument);
4882 if (number
4883 && TREE_CODE (number) == INTEGER_CST
4884 && TREE_INT_CST_HIGH (number) == 0)
4886 TREE_VALUE (second_argument)
4887 = build_int_cst (integer_type_node,
4888 TREE_INT_CST_LOW (number) + 2);
4891 /* This is the third argument, the "first-to-check",
4892 which specifies the index of the first argument to
4893 check. This could be 0, meaning it is not available,
4894 in which case we don't need to add 2. Add 2 if not
4895 0. */
4896 number = TREE_VALUE (third_argument);
4897 if (number
4898 && TREE_CODE (number) == INTEGER_CST
4899 && TREE_INT_CST_HIGH (number) == 0
4900 && TREE_INT_CST_LOW (number) != 0)
4902 TREE_VALUE (third_argument)
4903 = build_int_cst (integer_type_node,
4904 TREE_INT_CST_LOW (number) + 2);
4907 filtered_attributes = chainon (filtered_attributes,
4908 new_attribute);
4910 else if (is_attribute_p ("nonnull", name))
4912 /* We need to fixup all the argument indexes by adding 2
4913 for the two hidden arguments of an Objective-C method
4914 invocation, similat to what we do above for the
4915 "format" attribute. */
4916 /* FIXME: This works great in terms of implementing the
4917 functionality, but the warnings that are produced by
4918 nonnull do mention the argument index (while the
4919 format ones don't). For example, you could get
4920 "warning: null argument where non-null required
4921 (argument 3)". Now in that message, "argument 3"
4922 includes the 2 hidden arguments; it would be much
4923 more friendly to call it "argument 1", as that would
4924 be consistent with __attribute__ ((nonnnull (1))).
4925 To do this, we'd need to have the C family code that
4926 checks the arguments know about adding/removing 2 to
4927 the argument index ... or alternatively we could
4928 maybe store the "printable" argument index in
4929 addition to the actual argument index ? Some
4930 refactoring is needed to do this elegantly. */
4931 tree new_attribute = copy_node (attribute);
4932 tree argument = TREE_VALUE (attribute);
4933 while (argument != NULL_TREE)
4935 /* Get the value of the argument and add 2. */
4936 tree number = TREE_VALUE (argument);
4937 if (number
4938 && TREE_CODE (number) == INTEGER_CST
4939 && TREE_INT_CST_HIGH (number) == 0
4940 && TREE_INT_CST_LOW (number) != 0)
4942 TREE_VALUE (argument)
4943 = build_int_cst (integer_type_node,
4944 TREE_INT_CST_LOW (number) + 2);
4946 argument = TREE_CHAIN (argument);
4949 filtered_attributes = chainon (filtered_attributes,
4950 new_attribute);
4952 else
4953 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
4957 if (filtered_attributes)
4959 /* This hackery changes the TREE_TYPE of the ObjC method
4960 declaration to be a function type, so that decl_attributes
4961 will treat the ObjC method as if it was a function. Some
4962 attributes (sentinel, format) will be applied to the function
4963 type, changing it in place; so after calling decl_attributes,
4964 we extract the function type attributes and store them in
4965 METHOD_TYPE_ATTRIBUTES. Some other attributes (noreturn,
4966 deprecated) are applied directly to the method declaration
4967 (by setting TREE_DEPRECATED and TREE_THIS_VOLATILE) so there
4968 is nothing to do. */
4969 tree saved_type = TREE_TYPE (*node);
4970 TREE_TYPE (*node)
4971 = build_function_type_for_method (TREE_VALUE (saved_type), *node,
4972 METHOD_REF, 0);
4973 decl_attributes (node, filtered_attributes, flags);
4974 METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node));
4975 TREE_TYPE (*node) = saved_type;
4979 bool
4980 objc_method_decl (enum tree_code opcode)
4982 return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL;
4985 /* Return a function type for METHOD with RETURN_TYPE. CONTEXT is
4986 either METHOD_DEF or METHOD_REF, indicating whether we are defining a
4987 method or calling one. SUPER_FLAG indicates whether this is a send
4988 to super; this makes a difference for the NeXT calling sequence in
4989 which the lookup and the method call are done together. If METHOD is
4990 NULL, user-defined arguments (i.e., beyond self and _cmd) shall be
4991 represented as varargs. */
4993 tree
4994 build_function_type_for_method (tree return_type, tree method,
4995 int context, bool super_flag)
4997 VEC(tree,gc) *argtypes = make_tree_vector ();
4998 tree t, ftype;
4999 bool is_varargs = false;
5001 (*runtime.get_arg_type_list_base) (&argtypes, method, context, super_flag);
5003 /* No actual method prototype given; remaining args passed as varargs. */
5004 if (method == NULL_TREE)
5006 is_varargs = true;
5007 goto build_ftype;
5010 for (t = METHOD_SEL_ARGS (method); t; t = DECL_CHAIN (t))
5012 tree arg_type = TREE_VALUE (TREE_TYPE (t));
5014 /* Decay argument types for the underlying C function as
5015 appropriate. */
5016 arg_type = objc_decay_parm_type (arg_type);
5018 VEC_safe_push (tree, gc, argtypes, arg_type);
5021 if (METHOD_ADD_ARGS (method))
5023 for (t = TREE_CHAIN (METHOD_ADD_ARGS (method));
5024 t; t = TREE_CHAIN (t))
5026 tree arg_type = TREE_TYPE (TREE_VALUE (t));
5028 arg_type = objc_decay_parm_type (arg_type);
5030 VEC_safe_push (tree, gc, argtypes, arg_type);
5033 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
5034 is_varargs = true;
5037 build_ftype:
5038 if (is_varargs)
5039 ftype = build_varargs_function_type_vec (return_type, argtypes);
5040 else
5041 ftype = build_function_type_vec (return_type, argtypes);
5043 release_tree_vector (argtypes);
5044 return ftype;
5047 /* The 'method' argument is a tree; this tree could either be a single
5048 method, which is returned, or could be a TREE_VEC containing a list
5049 of methods. In that case, the first one is returned, and warnings
5050 are issued as appropriate. */
5051 static tree
5052 check_duplicates (tree method, int methods, int is_class)
5054 tree first_method;
5055 size_t i;
5057 if (method == NULL_TREE)
5058 return NULL_TREE;
5060 if (TREE_CODE (method) != TREE_VEC)
5061 return method;
5063 /* We have two or more methods with the same name but different
5064 types. */
5065 first_method = TREE_VEC_ELT (method, 0);
5067 /* But just how different are those types? If
5068 -Wno-strict-selector-match is specified, we shall not complain if
5069 the differences are solely among types with identical size and
5070 alignment. */
5071 if (!warn_strict_selector_match)
5073 for (i = 0; i < TREE_VEC_LENGTH (method); i++)
5074 if (!comp_proto_with_proto (first_method, TREE_VEC_ELT (method, i), 0))
5075 goto issue_warning;
5077 return first_method;
5080 issue_warning:
5081 if (methods)
5083 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5085 warning_at (input_location, 0,
5086 "multiple methods named %<%c%E%> found",
5087 (is_class ? '+' : '-'),
5088 METHOD_SEL_NAME (first_method));
5089 inform (DECL_SOURCE_LOCATION (first_method), "using %<%c%s%>",
5090 (type ? '-' : '+'),
5091 identifier_to_locale (gen_method_decl (first_method)));
5093 else
5095 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5097 warning_at (input_location, 0,
5098 "multiple selectors named %<%c%E%> found",
5099 (is_class ? '+' : '-'),
5100 METHOD_SEL_NAME (first_method));
5101 inform (DECL_SOURCE_LOCATION (first_method), "found %<%c%s%>",
5102 (type ? '-' : '+'),
5103 identifier_to_locale (gen_method_decl (first_method)));
5106 for (i = 0; i < TREE_VEC_LENGTH (method); i++)
5108 bool type = TREE_CODE (TREE_VEC_ELT (method, i)) == INSTANCE_METHOD_DECL;
5110 inform (DECL_SOURCE_LOCATION (TREE_VEC_ELT (method, i)), "also found %<%c%s%>",
5111 (type ? '-' : '+'),
5112 identifier_to_locale (gen_method_decl (TREE_VEC_ELT (method, i))));
5115 return first_method;
5118 /* If RECEIVER is a class reference, return the identifier node for
5119 the referenced class. RECEIVER is created by objc_get_class_reference,
5120 so we check the exact form created depending on which runtimes are
5121 used. */
5123 static tree
5124 receiver_is_class_object (tree receiver, int self, int super)
5126 tree exp, arg;
5128 /* The receiver is 'self' or 'super' in the context of a class method. */
5129 if (objc_method_context
5130 && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
5131 && (self || super))
5132 return (super
5133 ? CLASS_SUPER_NAME (implementation_template)
5134 : CLASS_NAME (implementation_template));
5136 /* The runtime might encapsulate things its own way. */
5137 exp = (*runtime.receiver_is_class_object) (receiver);
5138 if (exp)
5139 return exp;
5141 /* The receiver is a function call that returns an id. Check if
5142 it is a call to objc_getClass, if so, pick up the class name.
5144 This is required by the GNU runtime, which compiles
5146 [NSObject alloc]
5148 into
5150 [objc_get_class ("NSObject") alloc];
5152 and then, to check that the receiver responds to the +alloc
5153 method, needs to be able to determine that the objc_get_class()
5154 call returns the NSObject class and not just a generic Class
5155 pointer.
5157 But, traditionally this is enabled for all runtimes, not just the
5158 GNU one, which means that the compiler is smarter than you'd
5159 expect when dealing with objc_getClass(). For example, with the
5160 Apple runtime, in the code
5162 [objc_getClass ("NSObject") alloc];
5164 the compiler will recognize the objc_getClass() call as special
5165 (due to the code below) and so will know that +alloc is called on
5166 the 'NSObject' class, and can perform the corresponding checks.
5168 Programmers can disable this behaviour by casting the results of
5169 objc_getClass() to 'Class' (this may seem weird because
5170 objc_getClass() is already declared to return 'Class', but the
5171 compiler treats it as a special function). This may be useful if
5172 the class is never declared, and the compiler would complain
5173 about a missing @interface for it. Then, you can do
5175 [(Class)objc_getClass ("MyClassNeverDeclared") alloc];
5177 to silence the warnings. */
5178 if (TREE_CODE (receiver) == CALL_EXPR
5179 && (exp = CALL_EXPR_FN (receiver))
5180 && TREE_CODE (exp) == ADDR_EXPR
5181 && (exp = TREE_OPERAND (exp, 0))
5182 && TREE_CODE (exp) == FUNCTION_DECL
5183 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
5184 prototypes for objc_get_class(). Thankfully, they seem to share the
5185 same function type. */
5186 && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl)
5187 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), runtime.tag_getclass)
5188 /* We have a call to objc_get_class/objc_getClass! */
5189 && (arg = CALL_EXPR_ARG (receiver, 0)))
5191 STRIP_NOPS (arg);
5192 if (TREE_CODE (arg) == ADDR_EXPR
5193 && (arg = TREE_OPERAND (arg, 0))
5194 && TREE_CODE (arg) == STRING_CST)
5195 /* Finally, we have the class name. */
5196 return get_identifier (TREE_STRING_POINTER (arg));
5198 return 0;
5201 /* If we are currently building a message expr, this holds
5202 the identifier of the selector of the message. This is
5203 used when printing warnings about argument mismatches. */
5205 static tree current_objc_message_selector = 0;
5207 tree
5208 objc_message_selector (void)
5210 return current_objc_message_selector;
5213 /* Construct an expression for sending a message.
5214 MESS has the object to send to in TREE_PURPOSE
5215 and the argument list (including selector) in TREE_VALUE.
5217 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
5218 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
5220 tree
5221 objc_build_message_expr (tree receiver, tree message_args)
5223 tree sel_name;
5224 #ifdef OBJCPLUS
5225 tree args = TREE_PURPOSE (message_args);
5226 #else
5227 tree args = message_args;
5228 #endif
5229 tree method_params = NULL_TREE;
5231 if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK)
5232 return error_mark_node;
5234 /* Obtain the full selector name. */
5235 switch (TREE_CODE (args))
5237 case IDENTIFIER_NODE:
5238 /* A unary selector. */
5239 sel_name = args;
5240 break;
5241 case TREE_LIST:
5242 sel_name = build_keyword_selector (args);
5243 break;
5244 default:
5245 gcc_unreachable ();
5248 /* Build the parameter list to give to the method. */
5249 if (TREE_CODE (args) == TREE_LIST)
5250 #ifdef OBJCPLUS
5251 method_params = chainon (args, TREE_VALUE (message_args));
5252 #else
5254 tree chain = args, prev = NULL_TREE;
5256 /* We have a keyword selector--check for comma expressions. */
5257 while (chain)
5259 tree element = TREE_VALUE (chain);
5261 /* We have a comma expression, must collapse... */
5262 if (TREE_CODE (element) == TREE_LIST)
5264 if (prev)
5265 TREE_CHAIN (prev) = element;
5266 else
5267 args = element;
5269 prev = chain;
5270 chain = TREE_CHAIN (chain);
5272 method_params = args;
5274 #endif
5276 #ifdef OBJCPLUS
5277 if (processing_template_decl)
5278 /* Must wait until template instantiation time. */
5279 return build_min_nt (MESSAGE_SEND_EXPR, receiver, sel_name,
5280 method_params);
5281 #endif
5283 return objc_finish_message_expr (receiver, sel_name, method_params, NULL);
5286 /* Look up method SEL_NAME that would be suitable for receiver
5287 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
5288 nonzero), and report on any duplicates. */
5290 static tree
5291 lookup_method_in_hash_lists (tree sel_name, int is_class)
5293 tree method_prototype = OBJC_MAP_NOT_FOUND;
5295 if (!is_class)
5296 method_prototype = objc_map_get (instance_method_map, sel_name);
5298 if (method_prototype == OBJC_MAP_NOT_FOUND)
5300 method_prototype = objc_map_get (class_method_map, sel_name);
5301 is_class = 1;
5303 if (method_prototype == OBJC_MAP_NOT_FOUND)
5304 return NULL_TREE;
5307 return check_duplicates (method_prototype, 1, is_class);
5310 /* The 'objc_finish_message_expr' routine is called from within
5311 'objc_build_message_expr' for non-template functions. In the case of
5312 C++ template functions, it is called from 'build_expr_from_tree'
5313 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded.
5315 If the DEPRECATED_METHOD_PROTOTYPE argument is NULL, then we warn
5316 if the method being used is deprecated. If it is not NULL, instead
5317 of deprecating, we set *DEPRECATED_METHOD_PROTOTYPE to the method
5318 prototype that was used and is deprecated. This is useful for
5319 getter calls that are always generated when compiling dot-syntax
5320 expressions, even if they may not be used. In that case, we don't
5321 want the warning immediately; we produce it (if needed) at gimplify
5322 stage when we are sure that the deprecated getter is being
5323 used. */
5324 tree
5325 objc_finish_message_expr (tree receiver, tree sel_name, tree method_params,
5326 tree *deprecated_method_prototype)
5328 tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype;
5329 tree retval, class_tree;
5330 int self, super, have_cast;
5332 /* We have used the receiver, so mark it as read. */
5333 mark_exp_read (receiver);
5335 /* Extract the receiver of the message, as well as its type
5336 (where the latter may take the form of a cast or be inferred
5337 from the implementation context). */
5338 rtype = receiver;
5339 while (TREE_CODE (rtype) == COMPOUND_EXPR
5340 || TREE_CODE (rtype) == MODIFY_EXPR
5341 || CONVERT_EXPR_P (rtype)
5342 || TREE_CODE (rtype) == COMPONENT_REF)
5343 rtype = TREE_OPERAND (rtype, 0);
5345 /* self is 1 if this is a message to self, 0 otherwise */
5346 self = (rtype == self_decl);
5348 /* super is 1 if this is a message to super, 0 otherwise. */
5349 super = (rtype == UOBJC_SUPER_decl);
5351 /* rtype is the type of the receiver. */
5352 rtype = TREE_TYPE (receiver);
5354 /* have_cast is 1 if the receiver is casted. */
5355 have_cast = (TREE_CODE (receiver) == NOP_EXPR
5356 || (TREE_CODE (receiver) == COMPOUND_EXPR
5357 && !IS_SUPER (rtype)));
5359 /* If we are calling [super dealloc], reset our warning flag. */
5360 if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name)))
5361 should_call_super_dealloc = 0;
5363 /* If the receiver is a class object, retrieve the corresponding
5364 @interface, if one exists. class_tree is the class name
5365 identifier, or NULL_TREE if this is not a class method or the
5366 class name could not be determined (as in the case "Class c; [c
5367 method];"). */
5368 class_tree = receiver_is_class_object (receiver, self, super);
5370 /* Now determine the receiver type (if an explicit cast has not been
5371 provided). */
5372 if (!have_cast)
5374 if (class_tree)
5376 /* We are here when we have no cast, and we have a class
5377 name. So, this is a plain method to a class object, as
5378 in [NSObject alloc]. Find the interface corresponding to
5379 the class name. */
5380 rtype = lookup_interface (class_tree);
5382 if (rtype == NULL_TREE)
5384 /* If 'rtype' is NULL_TREE at this point it means that
5385 we have seen no @interface corresponding to that
5386 class name, only a @class declaration (alternatively,
5387 this was a call such as [objc_getClass("SomeClass")
5388 alloc], where we've never seen the @interface of
5389 SomeClass). So, we have a class name (class_tree)
5390 but no actual details of the class methods. We won't
5391 be able to check that the class responds to the
5392 method, and we will have to guess the method
5393 prototype. Emit a warning, then keep going (this
5394 will use any method with a matching name, as if the
5395 receiver was of type 'Class'). */
5396 warning (0, "@interface of class %qE not found", class_tree);
5399 /* Handle `self' and `super'. */
5400 else if (super)
5402 if (!CLASS_SUPER_NAME (implementation_template))
5404 error ("no super class declared in @interface for %qE",
5405 CLASS_NAME (implementation_template));
5406 return error_mark_node;
5408 rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template));
5410 else if (self)
5411 rtype = lookup_interface (CLASS_NAME (implementation_template));
5414 if (objc_is_id (rtype))
5416 /* The receiver is of type 'id' or 'Class' (with or without some
5417 protocols attached to it). */
5419 /* We set class_tree to the identifier for 'Class' if this is a
5420 class method, and to NULL_TREE if not. */
5421 class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE);
5423 /* 'rprotos' is the list of protocols that the receiver
5424 supports. */
5425 rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
5426 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
5427 : NULL_TREE);
5429 /* We have no information on the type, and we set it to
5430 NULL_TREE. */
5431 rtype = NULL_TREE;
5433 /* If there are any protocols, check that the method we are
5434 calling appears in the protocol list. If there are no
5435 protocols, this is a message to 'id' or 'Class' and we accept
5436 any method that exists. */
5437 if (rprotos)
5439 /* If messaging 'id <Protos>' or 'Class <Proto>', first
5440 search in protocols themselves for the method
5441 prototype. */
5442 method_prototype
5443 = lookup_method_in_protocol_list (rprotos, sel_name,
5444 class_tree != NULL_TREE);
5446 /* If messaging 'Class <Proto>' but did not find a class
5447 method prototype, search for an instance method instead,
5448 and warn about having done so. */
5449 if (!method_prototype && !rtype && class_tree != NULL_TREE)
5451 method_prototype
5452 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5454 if (method_prototype)
5455 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
5456 sel_name, sel_name);
5460 else if (rtype)
5462 /* We have a receiver type which is more specific than 'id' or
5463 'Class'. */
5464 tree orig_rtype = rtype;
5466 if (TREE_CODE (rtype) == POINTER_TYPE)
5467 rtype = TREE_TYPE (rtype);
5468 /* Traverse typedef aliases */
5469 while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype)
5470 && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL
5471 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)))
5472 rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype));
5473 if (TYPED_OBJECT (rtype))
5475 rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype);
5476 rtype = TYPE_OBJC_INTERFACE (rtype);
5478 if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE)
5480 /* If we could not find an @interface declaration, we must
5481 have only seen a @class declaration; so, we cannot say
5482 anything more intelligent about which methods the
5483 receiver will understand. Note that this only happens
5484 for instance methods; for class methods to a class where
5485 we have only seen a @class declaration,
5486 lookup_interface() above would have set rtype to
5487 NULL_TREE. */
5488 if (rprotos)
5490 /* We could not find an @interface declaration, yet, if
5491 there are protocols attached to the type, we can
5492 still look up the method in the protocols. Ie, we
5493 are in the following case:
5495 @class MyClass;
5496 MyClass<MyProtocol> *x;
5497 [x method];
5499 If 'MyProtocol' has the method 'method', we can check
5500 and retrieve the method prototype. */
5501 method_prototype
5502 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5504 /* At this point, if we have found the method_prototype,
5505 we are quite happy. The details of the class are
5506 irrelevant. If we haven't found it, a warning will
5507 have been produced that the method could not be found
5508 in the protocol, and we won't produce further
5509 warnings (please note that this means that "@class
5510 MyClass; MyClass <MyProtocol> *x;" is exactly
5511 equivalent to "id <MyProtocol> x", which isn't too
5512 satisfactory but it's not easy to see how to do
5513 better). */
5515 else
5517 if (rtype)
5519 /* We could not find an @interface declaration, and
5520 there are no protocols attached to the receiver,
5521 so we can't complete the check that the receiver
5522 responds to the method, and we can't retrieve the
5523 method prototype. But, because the receiver has
5524 a well-specified class, the programmer did want
5525 this check to be performed. Emit a warning, then
5526 keep going as if it was an 'id'. To remove the
5527 warning, either include an @interface for the
5528 class, or cast the receiver to 'id'. Note that
5529 rtype is an IDENTIFIER_NODE at this point. */
5530 warning (0, "@interface of class %qE not found", rtype);
5534 rtype = NULL_TREE;
5536 else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE
5537 || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE)
5539 /* We have a valid ObjC class name with an associated
5540 @interface. Look up the method name in the published
5541 @interface for the class (and its superclasses). */
5542 method_prototype
5543 = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE);
5545 /* If the method was not found in the @interface, it may still
5546 exist locally as part of the @implementation. */
5547 if (!method_prototype && objc_implementation_context
5548 && CLASS_NAME (objc_implementation_context)
5549 == OBJC_TYPE_NAME (rtype))
5550 method_prototype
5551 = lookup_method
5552 ((class_tree
5553 ? CLASS_CLS_METHODS (objc_implementation_context)
5554 : CLASS_NST_METHODS (objc_implementation_context)),
5555 sel_name);
5557 /* If we haven't found a candidate method by now, try looking for
5558 it in the protocol list. */
5559 if (!method_prototype && rprotos)
5560 method_prototype
5561 = lookup_method_in_protocol_list (rprotos, sel_name,
5562 class_tree != NULL_TREE);
5564 else
5566 /* We have a type, but it's not an Objective-C type (!). */
5567 warning (0, "invalid receiver type %qs",
5568 identifier_to_locale (gen_type_name (orig_rtype)));
5569 /* After issuing the "invalid receiver" warning, perform method
5570 lookup as if we were messaging 'id'. */
5571 rtype = rprotos = NULL_TREE;
5574 /* Note that rtype could also be NULL_TREE. This happens if we are
5575 messaging a class by name, but the class was only
5576 forward-declared using @class. */
5578 /* For 'id' or 'Class' receivers, search in the global hash table as
5579 a last resort. For all receivers, warn if protocol searches have
5580 failed. */
5581 if (!method_prototype)
5583 if (rprotos)
5584 warning (0, "%<%c%E%> not found in protocol(s)",
5585 (class_tree ? '+' : '-'),
5586 sel_name);
5588 if (!rtype)
5589 method_prototype
5590 = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE);
5593 if (!method_prototype)
5595 static bool warn_missing_methods = false;
5597 if (rtype)
5598 warning (0, "%qE may not respond to %<%c%E%>",
5599 OBJC_TYPE_NAME (rtype),
5600 (class_tree ? '+' : '-'),
5601 sel_name);
5602 /* If we are messaging an 'id' or 'Class' object and made it here,
5603 then we have failed to find _any_ instance or class method,
5604 respectively. */
5605 else
5606 warning (0, "no %<%c%E%> method found",
5607 (class_tree ? '+' : '-'),
5608 sel_name);
5610 if (!warn_missing_methods)
5612 warning_at (input_location,
5613 0, "(Messages without a matching method signature");
5614 warning_at (input_location,
5615 0, "will be assumed to return %<id%> and accept");
5616 warning_at (input_location,
5617 0, "%<...%> as arguments.)");
5618 warn_missing_methods = true;
5621 else
5623 /* Warn if the method is deprecated, but not if the receiver is
5624 a generic 'id'. 'id' is used to cast an object to a generic
5625 object of an unspecified class; in that case, we'll use
5626 whatever method prototype we can find to get the method
5627 argument and return types, but it is not appropriate to
5628 produce deprecation warnings since we don't know the class
5629 that the object will be of at runtime. The @interface(s) for
5630 that class may not even be available to the compiler right
5631 now, and it is perfectly possible that the method is marked
5632 as non-deprecated in such @interface(s).
5634 In practice this makes sense since casting an object to 'id'
5635 is often used precisely to turn off warnings associated with
5636 the object being of a particular class. */
5637 if (TREE_DEPRECATED (method_prototype) && rtype != NULL_TREE)
5639 if (deprecated_method_prototype)
5640 *deprecated_method_prototype = method_prototype;
5641 else
5642 warn_deprecated_use (method_prototype, NULL_TREE);
5646 /* Save the selector name for printing error messages. */
5647 current_objc_message_selector = sel_name;
5649 /* Build the method call.
5650 TODO: Get the location from somewhere that will work for delayed
5651 expansion. */
5653 retval = (*runtime.build_objc_method_call) (input_location, method_prototype,
5654 receiver, rtype, sel_name,
5655 method_params, super);
5657 current_objc_message_selector = 0;
5659 return retval;
5663 /* This routine creates a static variable used to implement @protocol(MyProtocol)
5664 expression. This variable will be initialized to global protocol_t meta-data
5665 pointer. */
5667 /* This function is called by the parser when (and only when) a
5668 @protocol() expression is found, in order to compile it. */
5669 tree
5670 objc_build_protocol_expr (tree protoname)
5672 tree p = lookup_protocol (protoname, /* warn if deprecated */ true,
5673 /* definition_required */ false);
5675 if (!p)
5677 error ("cannot find protocol declaration for %qE", protoname);
5678 return error_mark_node;
5681 return (*runtime.get_protocol_reference) (input_location, p);
5684 /* This function is called by the parser when a @selector() expression
5685 is found, in order to compile it. It is only called by the parser
5686 and only to compile a @selector(). LOC is the location of the
5687 @selector. */
5688 tree
5689 objc_build_selector_expr (location_t loc, tree selnamelist)
5691 tree selname;
5693 /* Obtain the full selector name. */
5694 switch (TREE_CODE (selnamelist))
5696 case IDENTIFIER_NODE:
5697 /* A unary selector. */
5698 selname = selnamelist;
5699 break;
5700 case TREE_LIST:
5701 selname = build_keyword_selector (selnamelist);
5702 break;
5703 default:
5704 gcc_unreachable ();
5707 /* If we are required to check @selector() expressions as they
5708 are found, check that the selector has been declared. */
5709 if (warn_undeclared_selector)
5711 /* Look the selector up in the list of all known class and
5712 instance methods (up to this line) to check that the selector
5713 exists. */
5714 tree method;
5716 /* First try with instance methods. */
5717 method = objc_map_get (instance_method_map, selname);
5719 /* If not found, try with class methods. */
5720 if (method == OBJC_MAP_NOT_FOUND)
5722 method = objc_map_get (class_method_map, selname);
5724 /* If still not found, print out a warning. */
5725 if (method == OBJC_MAP_NOT_FOUND)
5726 warning (0, "undeclared selector %qE", selname);
5730 /* The runtimes do this differently, most particularly, GNU has typed
5731 selectors, whilst NeXT does not. */
5732 return (*runtime.build_selector_reference) (loc, selname, NULL_TREE);
5735 static tree
5736 build_ivar_reference (tree id)
5738 tree base;
5739 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
5741 /* Historically, a class method that produced objects (factory
5742 method) would assign `self' to the instance that it
5743 allocated. This would effectively turn the class method into
5744 an instance method. Following this assignment, the instance
5745 variables could be accessed. That practice, while safe,
5746 violates the simple rule that a class method should not refer
5747 to an instance variable. It's better to catch the cases
5748 where this is done unknowingly than to support the above
5749 paradigm. */
5750 warning (0, "instance variable %qE accessed in class method",
5751 id);
5752 self_decl = convert (objc_instance_type, self_decl); /* cast */
5755 base = build_indirect_ref (input_location, self_decl, RO_ARROW);
5756 return (*runtime.build_ivar_reference) (input_location, base, id);
5759 static void
5760 hash_init (void)
5762 instance_method_map = objc_map_alloc_ggc (1000);
5763 class_method_map = objc_map_alloc_ggc (1000);
5765 class_name_map = objc_map_alloc_ggc (200);
5766 alias_name_map = objc_map_alloc_ggc (200);
5768 /* Initialize the hash table used to hold the constant string objects. */
5769 string_htab = htab_create_ggc (31, string_hash,
5770 string_eq, NULL);
5773 /* Use the following to add a method to class_method_map or
5774 instance_method_map. It will add the method, keyed by the
5775 METHOD_SEL_NAME. If the method already exists, but with one or
5776 more different prototypes, it will store a TREE_VEC in the map,
5777 with the method prototypes in the vector. */
5778 static void
5779 insert_method_into_method_map (bool class_method, tree method)
5781 tree method_name = METHOD_SEL_NAME (method);
5782 tree existing_entry;
5783 objc_map_t map;
5785 if (class_method)
5786 map = class_method_map;
5787 else
5788 map = instance_method_map;
5790 /* Check if the method already exists in the map. */
5791 existing_entry = objc_map_get (map, method_name);
5793 /* If not, we simply add it to the map. */
5794 if (existing_entry == OBJC_MAP_NOT_FOUND)
5795 objc_map_put (map, method_name, method);
5796 else
5798 tree new_entry;
5800 /* If an entry already exists, it's more complicated. We'll
5801 have to check whether the method prototype is the same or
5802 not. */
5803 if (TREE_CODE (existing_entry) != TREE_VEC)
5805 /* If the method prototypes are the same, there is nothing
5806 to do. */
5807 if (comp_proto_with_proto (method, existing_entry, 1))
5808 return;
5810 /* If not, create a vector to store both the method already
5811 in the map, and the new one that we are adding. */
5812 new_entry = make_tree_vec (2);
5814 TREE_VEC_ELT (new_entry, 0) = existing_entry;
5815 TREE_VEC_ELT (new_entry, 1) = method;
5817 else
5819 /* An entry already exists, and it's already a vector. This
5820 means that at least 2 different method prototypes were
5821 already found, and we're considering registering yet
5822 another one. */
5823 size_t i;
5825 /* Check all the existing prototypes. If any matches the
5826 one we need to add, there is nothing to do because it's
5827 already there. */
5828 for (i = 0; i < TREE_VEC_LENGTH (existing_entry); i++)
5829 if (comp_proto_with_proto (method, TREE_VEC_ELT (existing_entry, i), 1))
5830 return;
5832 /* Else, create a new, bigger vector and add the new method
5833 at the end of it. This is inefficient but extremely
5834 rare; in any sane program most methods have a single
5835 prototype, and very few, if any, will have more than
5836 2! */
5837 new_entry = make_tree_vec (TREE_VEC_LENGTH (existing_entry) + 1);
5839 /* Copy the methods from the existing vector. */
5840 for (i = 0; i < TREE_VEC_LENGTH (existing_entry); i++)
5841 TREE_VEC_ELT (new_entry, i) = TREE_VEC_ELT (existing_entry, i);
5843 /* Add the new method at the end. */
5844 TREE_VEC_ELT (new_entry, i) = method;
5847 /* Store the new vector in the map. */
5848 objc_map_put (map, method_name, new_entry);
5853 static tree
5854 lookup_method (tree mchain, tree method)
5856 tree key;
5858 if (TREE_CODE (method) == IDENTIFIER_NODE)
5859 key = method;
5860 else
5861 key = METHOD_SEL_NAME (method);
5863 while (mchain)
5865 if (METHOD_SEL_NAME (mchain) == key)
5866 return mchain;
5868 mchain = DECL_CHAIN (mchain);
5870 return NULL_TREE;
5873 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance
5874 method in INTERFACE, along with any categories and protocols
5875 attached thereto. If method is not found, and the
5876 OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS, recursively examine the
5877 INTERFACE's superclass. If OBJC_LOOKUP_CLASS is set,
5878 OBJC_LOOKUP_NO_SUPER is clear, and no suitable class method could
5879 be found in INTERFACE or any of its superclasses, look for an
5880 _instance_ method of the same name in the root class as a last
5881 resort. This behaviour can be turned off by using
5882 OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS.
5884 If a suitable method cannot be found, return NULL_TREE. */
5886 static tree
5887 lookup_method_static (tree interface, tree ident, int flags)
5889 tree meth = NULL_TREE, root_inter = NULL_TREE;
5890 tree inter = interface;
5891 int is_class = (flags & OBJC_LOOKUP_CLASS);
5892 int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER);
5893 int no_instance_methods_of_root_class = (flags & OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS);
5895 while (inter)
5897 tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter);
5898 tree category = inter;
5900 /* First, look up the method in the class itself. */
5901 if ((meth = lookup_method (chain, ident)))
5902 return meth;
5904 /* Failing that, look for the method in each category of the class. */
5905 while ((category = CLASS_CATEGORY_LIST (category)))
5907 chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category);
5909 /* Check directly in each category. */
5910 if ((meth = lookup_method (chain, ident)))
5911 return meth;
5913 /* Failing that, check in each category's protocols. */
5914 if (CLASS_PROTOCOL_LIST (category))
5916 if ((meth = (lookup_method_in_protocol_list
5917 (CLASS_PROTOCOL_LIST (category), ident, is_class))))
5918 return meth;
5922 /* If not found in categories, check in protocols of the main class. */
5923 if (CLASS_PROTOCOL_LIST (inter))
5925 if ((meth = (lookup_method_in_protocol_list
5926 (CLASS_PROTOCOL_LIST (inter), ident, is_class))))
5927 return meth;
5930 /* If we were instructed not to look in superclasses, don't. */
5931 if (no_superclasses)
5932 return NULL_TREE;
5934 /* Failing that, climb up the inheritance hierarchy. */
5935 root_inter = inter;
5936 inter = lookup_interface (CLASS_SUPER_NAME (inter));
5938 while (inter);
5940 if (is_class && !no_instance_methods_of_root_class)
5942 /* If no class (factory) method was found, check if an _instance_
5943 method of the same name exists in the root class. This is what
5944 the Objective-C runtime will do. */
5945 return lookup_method_static (root_inter, ident, 0);
5947 else
5949 /* If an instance method was not found, return 0. */
5950 return NULL_TREE;
5954 static tree
5955 objc_add_method (tree klass, tree method, int is_class, bool is_optional)
5957 tree existing_method = NULL_TREE;
5959 /* The first thing we do is look up the method in the list of
5960 methods already defined in the interface (or implementation). */
5961 if (is_class)
5962 existing_method = lookup_method (CLASS_CLS_METHODS (klass), method);
5963 else
5964 existing_method = lookup_method (CLASS_NST_METHODS (klass), method);
5966 /* In the case of protocols, we have a second list of methods to
5967 consider, the list of optional ones. */
5968 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
5970 /* @required methods are added to the protocol's normal list.
5971 @optional methods are added to the protocol's OPTIONAL lists.
5972 Note that adding the methods to the optional lists disables
5973 checking that the methods are implemented by classes
5974 implementing the protocol, since these checks only use the
5975 CLASS_CLS_METHODS and CLASS_NST_METHODS. */
5977 /* First of all, if the method to add is @optional, and we found
5978 it already existing as @required, emit an error. */
5979 if (is_optional && existing_method)
5981 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
5982 (is_class ? '+' : '-'),
5983 METHOD_SEL_NAME (existing_method));
5984 inform (DECL_SOURCE_LOCATION (existing_method),
5985 "previous declaration of %<%c%E%> as %<@required%>",
5986 (is_class ? '+' : '-'),
5987 METHOD_SEL_NAME (existing_method));
5990 /* Now check the list of @optional methods if we didn't find the
5991 method in the @required list. */
5992 if (!existing_method)
5994 if (is_class)
5995 existing_method = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (klass), method);
5996 else
5997 existing_method = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (klass), method);
5999 if (!is_optional && existing_method)
6001 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
6002 (is_class ? '+' : '-'),
6003 METHOD_SEL_NAME (existing_method));
6004 inform (DECL_SOURCE_LOCATION (existing_method),
6005 "previous declaration of %<%c%E%> as %<@optional%>",
6006 (is_class ? '+' : '-'),
6007 METHOD_SEL_NAME (existing_method));
6012 /* If the method didn't exist already, add it. */
6013 if (!existing_method)
6015 if (is_optional)
6017 if (is_class)
6019 /* Put the method on the list in reverse order. */
6020 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass);
6021 PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method;
6023 else
6025 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass);
6026 PROTOCOL_OPTIONAL_NST_METHODS (klass) = method;
6029 else
6031 if (is_class)
6033 DECL_CHAIN (method) = CLASS_CLS_METHODS (klass);
6034 CLASS_CLS_METHODS (klass) = method;
6036 else
6038 DECL_CHAIN (method) = CLASS_NST_METHODS (klass);
6039 CLASS_NST_METHODS (klass) = method;
6043 else
6045 /* The method was already defined. Check that the types match
6046 for an @interface for a class or category, or for a
6047 @protocol. Give hard errors on methods with identical
6048 selectors but differing argument and/or return types. We do
6049 not do this for @implementations, because C/C++ will do it
6050 for us (i.e., there will be duplicate function definition
6051 errors). */
6052 if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE
6053 || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6054 /* Starting with GCC 4.6, we emit the same error for
6055 protocols too. The situation is identical to
6056 @interfaces as there is no possible meaningful reason
6057 for defining the same method with different signatures
6058 in the very same @protocol. If that was allowed,
6059 whenever the protocol is used (both at compile and run
6060 time) there wouldn't be any meaningful way to decide
6061 which of the two method signatures should be used. */
6062 || TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
6063 && !comp_proto_with_proto (method, existing_method, 1))
6065 error ("duplicate declaration of method %<%c%E%> with conflicting types",
6066 (is_class ? '+' : '-'),
6067 METHOD_SEL_NAME (existing_method));
6068 inform (DECL_SOURCE_LOCATION (existing_method),
6069 "previous declaration of %<%c%E%>",
6070 (is_class ? '+' : '-'),
6071 METHOD_SEL_NAME (existing_method));
6075 if (is_class)
6076 insert_method_into_method_map (true, method);
6077 else
6079 insert_method_into_method_map (false, method);
6081 /* Instance methods in root classes (and categories thereof)
6082 may act as class methods as a last resort. We also add
6083 instance methods listed in @protocol declarations to
6084 the class hash table, on the assumption that @protocols
6085 may be adopted by root classes or categories. */
6086 if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6087 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
6088 klass = lookup_interface (CLASS_NAME (klass));
6090 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE
6091 || !CLASS_SUPER_NAME (klass))
6092 insert_method_into_method_map (true, method);
6095 return method;
6098 static void
6099 add_category (tree klass, tree category)
6101 /* Put categories on list in reverse order. */
6102 tree cat = lookup_category (klass, CLASS_SUPER_NAME (category));
6104 if (cat)
6106 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
6107 CLASS_NAME (klass),
6108 CLASS_SUPER_NAME (category));
6110 else
6112 CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass);
6113 CLASS_CATEGORY_LIST (klass) = category;
6117 #ifndef OBJCPLUS
6118 /* A flexible array member is a C99 extension where you can use
6119 "type[]" at the end of a struct to mean a variable-length array.
6121 In Objective-C, instance variables are fundamentally members of a
6122 struct, but the struct can always be extended by subclassing; hence
6123 we need to detect and forbid all instance variables declared using
6124 flexible array members.
6126 No check for this is needed in Objective-C++, since C++ does not
6127 have flexible array members. */
6129 /* Determine whether TYPE is a structure with a flexible array member,
6130 a union containing such a structure (possibly recursively) or an
6131 array of such structures or unions. These are all invalid as
6132 instance variable. */
6133 static bool
6134 flexible_array_type_p (tree type)
6136 tree x;
6137 switch (TREE_CODE (type))
6139 case RECORD_TYPE:
6140 x = TYPE_FIELDS (type);
6141 if (x == NULL_TREE)
6142 return false;
6143 while (DECL_CHAIN (x) != NULL_TREE)
6144 x = DECL_CHAIN (x);
6145 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6146 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6147 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6148 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6149 return true;
6150 return false;
6151 case UNION_TYPE:
6152 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
6154 if (flexible_array_type_p (TREE_TYPE (x)))
6155 return true;
6157 return false;
6158 /* Note that we also check for arrays of something that uses a flexible array member. */
6159 case ARRAY_TYPE:
6160 if (flexible_array_type_p (TREE_TYPE (type)))
6161 return true;
6162 return false;
6163 default:
6164 return false;
6167 #endif
6169 /* Produce a printable version of an ivar name. This is only used
6170 inside add_instance_variable. */
6171 static const char *
6172 printable_ivar_name (tree field_decl)
6174 if (DECL_NAME (field_decl))
6175 return identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl)));
6176 else
6177 return _("<unnamed>");
6180 /* Called after parsing each instance variable declaration. Necessary to
6181 preserve typedefs and implement public/private...
6183 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
6185 static tree
6186 add_instance_variable (tree klass, objc_ivar_visibility_kind visibility,
6187 tree field_decl)
6189 tree field_type = TREE_TYPE (field_decl);
6191 #ifdef OBJCPLUS
6192 if (TREE_CODE (field_type) == REFERENCE_TYPE)
6194 error ("illegal reference type specified for instance variable %qs",
6195 printable_ivar_name (field_decl));
6196 /* Return class as is without adding this ivar. */
6197 return klass;
6199 #endif
6201 if (field_type == error_mark_node || !TYPE_SIZE (field_type)
6202 || TYPE_SIZE (field_type) == error_mark_node)
6203 /* 'type[0]' is allowed, but 'type[]' is not! */
6205 error ("instance variable %qs has unknown size",
6206 printable_ivar_name (field_decl));
6207 /* Return class as is without adding this ivar. */
6208 return klass;
6211 #ifndef OBJCPLUS
6212 /* Also, in C reject a struct with a flexible array member. Ie,
6214 struct A { int x; int[] y; };
6216 @interface X
6218 struct A instance_variable;
6220 @end
6222 is not valid because if the class is subclassed, we wouldn't be able
6223 to calculate the offset of the next instance variable. */
6224 if (flexible_array_type_p (field_type))
6226 error ("instance variable %qs uses flexible array member",
6227 printable_ivar_name (field_decl));
6228 /* Return class as is without adding this ivar. */
6229 return klass;
6231 #endif
6233 #ifdef OBJCPLUS
6234 /* Check if the ivar being added has a non-POD C++ type. If so, we will
6235 need to either (1) warn the user about it or (2) generate suitable
6236 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
6237 methods (if '-fobjc-call-cxx-cdtors' was specified). */
6238 if (MAYBE_CLASS_TYPE_P (field_type)
6239 && (TYPE_NEEDS_CONSTRUCTING (field_type)
6240 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)
6241 || TYPE_POLYMORPHIC_P (field_type)))
6243 tree type_name = OBJC_TYPE_NAME (field_type);
6245 if (flag_objc_call_cxx_cdtors)
6247 /* Since the ObjC runtime will be calling the constructors and
6248 destructors for us, the only thing we can't handle is the lack
6249 of a default constructor. */
6250 if (TYPE_NEEDS_CONSTRUCTING (field_type)
6251 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type))
6253 warning (0, "type %qE has no default constructor to call",
6254 type_name);
6256 /* If we cannot call a constructor, we should also avoid
6257 calling the destructor, for symmetry. */
6258 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6259 warning (0, "destructor for %qE shall not be run either",
6260 type_name);
6263 else
6265 static bool warn_cxx_ivars = false;
6267 if (TYPE_POLYMORPHIC_P (field_type))
6269 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
6270 initialize them. */
6271 error ("type %qE has virtual member functions", type_name);
6272 error ("illegal aggregate type %qE specified "
6273 "for instance variable %qs",
6274 type_name, printable_ivar_name (field_decl));
6275 /* Return class as is without adding this ivar. */
6276 return klass;
6279 /* User-defined constructors and destructors are not known to Obj-C
6280 and hence will not be called. This may or may not be a problem. */
6281 if (TYPE_NEEDS_CONSTRUCTING (field_type))
6282 warning (0, "type %qE has a user-defined constructor", type_name);
6283 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6284 warning (0, "type %qE has a user-defined destructor", type_name);
6286 if (!warn_cxx_ivars)
6288 warning (0, "C++ constructors and destructors will not "
6289 "be invoked for Objective-C fields");
6290 warn_cxx_ivars = true;
6294 #endif
6296 /* Overload the public attribute, it is not used for FIELD_DECLs. */
6297 switch (visibility)
6299 case OBJC_IVAR_VIS_PROTECTED:
6300 TREE_PUBLIC (field_decl) = 0;
6301 TREE_PRIVATE (field_decl) = 0;
6302 TREE_PROTECTED (field_decl) = 1;
6303 break;
6305 case OBJC_IVAR_VIS_PACKAGE:
6306 /* TODO: Implement the package variant. */
6307 case OBJC_IVAR_VIS_PUBLIC:
6308 TREE_PUBLIC (field_decl) = 1;
6309 TREE_PRIVATE (field_decl) = 0;
6310 TREE_PROTECTED (field_decl) = 0;
6311 break;
6313 case OBJC_IVAR_VIS_PRIVATE:
6314 TREE_PUBLIC (field_decl) = 0;
6315 TREE_PRIVATE (field_decl) = 1;
6316 TREE_PROTECTED (field_decl) = 0;
6317 break;
6321 CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl);
6323 return klass;
6326 /* True if the ivar is private and we are not in its implementation. */
6328 static int
6329 is_private (tree decl)
6331 return (TREE_PRIVATE (decl)
6332 && ! is_ivar (CLASS_IVARS (implementation_template),
6333 DECL_NAME (decl)));
6336 /* Searches all the instance variables of 'klass' and of its
6337 superclasses for an instance variable whose name (identifier) is
6338 'ivar_name_ident'. Return the declaration (DECL) of the instance
6339 variable, if found, or NULL_TREE, if not found. */
6340 static inline tree
6341 ivar_of_class (tree klass, tree ivar_name_ident)
6343 /* First, look up the ivar in CLASS_RAW_IVARS. */
6344 tree decl_chain = CLASS_RAW_IVARS (klass);
6346 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6347 if (DECL_NAME (decl_chain) == ivar_name_ident)
6348 return decl_chain;
6350 /* If not found, search up the class hierarchy. */
6351 while (CLASS_SUPER_NAME (klass))
6353 klass = lookup_interface (CLASS_SUPER_NAME (klass));
6355 decl_chain = CLASS_RAW_IVARS (klass);
6357 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6358 if (DECL_NAME (decl_chain) == ivar_name_ident)
6359 return decl_chain;
6362 return NULL_TREE;
6365 /* We have an instance variable reference;, check to see if it is public. */
6368 objc_is_public (tree expr, tree identifier)
6370 tree basetype, decl;
6372 #ifdef OBJCPLUS
6373 if (processing_template_decl)
6374 return 1;
6375 #endif
6377 if (TREE_TYPE (expr) == error_mark_node)
6378 return 1;
6380 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
6382 if (basetype && TREE_CODE (basetype) == RECORD_TYPE)
6384 if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype))
6386 tree klass = lookup_interface (OBJC_TYPE_NAME (basetype));
6388 if (!klass)
6390 error ("cannot find interface declaration for %qE",
6391 OBJC_TYPE_NAME (basetype));
6392 return 0;
6395 if ((decl = ivar_of_class (klass, identifier)))
6397 if (TREE_PUBLIC (decl))
6398 return 1;
6400 /* Important difference between the Stepstone translator:
6401 all instance variables should be public within the context
6402 of the implementation. */
6403 if (objc_implementation_context
6404 && ((TREE_CODE (objc_implementation_context)
6405 == CLASS_IMPLEMENTATION_TYPE)
6406 || (TREE_CODE (objc_implementation_context)
6407 == CATEGORY_IMPLEMENTATION_TYPE)))
6409 tree curtype = TYPE_MAIN_VARIANT
6410 (CLASS_STATIC_TEMPLATE
6411 (implementation_template));
6413 if (basetype == curtype
6414 || DERIVED_FROM_P (basetype, curtype))
6416 int priv = is_private (decl);
6418 if (priv)
6419 error ("instance variable %qE is declared private",
6420 DECL_NAME (decl));
6422 return !priv;
6426 /* The 2.95.2 compiler sometimes allowed C functions to access
6427 non-@public ivars. We will let this slide for now... */
6428 if (!objc_method_context)
6430 warning (0, "instance variable %qE is %s; "
6431 "this will be a hard error in the future",
6432 identifier,
6433 TREE_PRIVATE (decl) ? "@private" : "@protected");
6434 return 1;
6437 error ("instance variable %qE is declared %s",
6438 identifier,
6439 TREE_PRIVATE (decl) ? "private" : "protected");
6440 return 0;
6445 return 1;
6448 /* Make sure all methods in CHAIN (a list of method declarations from
6449 an @interface or a @protocol) are in IMPLEMENTATION (the
6450 implementation context). This is used to check for example that
6451 all methods declared in an @interface were implemented in an
6452 @implementation.
6454 Some special methods (property setters/getters) are special and if
6455 they are not found in IMPLEMENTATION, we look them up in its
6456 superclasses. */
6458 static int
6459 check_methods (tree chain, tree implementation, int mtype)
6461 int first = 1;
6462 tree list;
6464 if (mtype == (int)'+')
6465 list = CLASS_CLS_METHODS (implementation);
6466 else
6467 list = CLASS_NST_METHODS (implementation);
6469 while (chain)
6471 /* If the method is associated with a dynamic property, then it
6472 is Ok not to have the method implementation, as it will be
6473 generated dynamically at runtime. To decide if the method is
6474 associated with a @dynamic property, we search the list of
6475 @synthesize and @dynamic for this implementation, and look
6476 for any @dynamic property with the same setter or getter name
6477 as this method. */
6478 tree x;
6479 for (x = IMPL_PROPERTY_DECL (implementation); x; x = TREE_CHAIN (x))
6480 if (PROPERTY_DYNAMIC (x)
6481 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6482 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6483 break;
6485 if (x != NULL_TREE)
6487 chain = TREE_CHAIN (chain); /* next method... */
6488 continue;
6491 if (!lookup_method (list, chain))
6493 /* If the method is a property setter/getter, we'll still
6494 allow it to be missing if it is implemented by
6495 'interface' or any of its superclasses. */
6496 tree property = METHOD_PROPERTY_CONTEXT (chain);
6497 if (property)
6499 /* Note that since this is a property getter/setter, it
6500 is obviously an instance method. */
6501 tree interface = NULL_TREE;
6503 /* For a category, first check the main class
6504 @interface. */
6505 if (TREE_CODE (implementation) == CATEGORY_IMPLEMENTATION_TYPE)
6507 interface = lookup_interface (CLASS_NAME (implementation));
6509 /* If the method is found in the main class, it's Ok. */
6510 if (lookup_method (CLASS_NST_METHODS (interface), chain))
6512 chain = DECL_CHAIN (chain);
6513 continue;
6516 /* Else, get the superclass. */
6517 if (CLASS_SUPER_NAME (interface))
6518 interface = lookup_interface (CLASS_SUPER_NAME (interface));
6519 else
6520 interface = NULL_TREE;
6523 /* Get the superclass for classes. */
6524 if (TREE_CODE (implementation) == CLASS_IMPLEMENTATION_TYPE)
6526 if (CLASS_SUPER_NAME (implementation))
6527 interface = lookup_interface (CLASS_SUPER_NAME (implementation));
6528 else
6529 interface = NULL_TREE;
6532 /* Now, interface is the superclass, if any; go check it. */
6533 if (interface)
6535 if (lookup_method_static (interface, chain, 0))
6537 chain = DECL_CHAIN (chain);
6538 continue;
6541 /* Else, fall through - warn. */
6543 if (first)
6545 switch (TREE_CODE (implementation))
6547 case CLASS_IMPLEMENTATION_TYPE:
6548 warning (0, "incomplete implementation of class %qE",
6549 CLASS_NAME (implementation));
6550 break;
6551 case CATEGORY_IMPLEMENTATION_TYPE:
6552 warning (0, "incomplete implementation of category %qE",
6553 CLASS_SUPER_NAME (implementation));
6554 break;
6555 default:
6556 gcc_unreachable ();
6558 first = 0;
6561 warning (0, "method definition for %<%c%E%> not found",
6562 mtype, METHOD_SEL_NAME (chain));
6565 chain = DECL_CHAIN (chain);
6568 return first;
6571 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
6573 static int
6574 conforms_to_protocol (tree klass, tree protocol)
6576 if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE)
6578 tree p = CLASS_PROTOCOL_LIST (klass);
6579 while (p && TREE_VALUE (p) != protocol)
6580 p = TREE_CHAIN (p);
6582 if (!p)
6584 tree super = (CLASS_SUPER_NAME (klass)
6585 ? lookup_interface (CLASS_SUPER_NAME (klass))
6586 : NULL_TREE);
6587 int tmp = super ? conforms_to_protocol (super, protocol) : 0;
6588 if (!tmp)
6589 return 0;
6593 return 1;
6596 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
6597 CONTEXT. This is one of two mechanisms to check protocol integrity. */
6599 static int
6600 check_methods_accessible (tree chain, tree context, int mtype)
6602 int first = 1;
6603 tree list;
6604 tree base_context = context;
6606 while (chain)
6608 /* If the method is associated with a dynamic property, then it
6609 is Ok not to have the method implementation, as it will be
6610 generated dynamically at runtime. Search for any @dynamic
6611 property with the same setter or getter name as this
6612 method. TODO: Use a hashtable lookup. */
6613 tree x;
6614 for (x = IMPL_PROPERTY_DECL (base_context); x; x = TREE_CHAIN (x))
6615 if (PROPERTY_DYNAMIC (x)
6616 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6617 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6618 break;
6620 if (x != NULL_TREE)
6622 chain = TREE_CHAIN (chain); /* next method... */
6623 continue;
6626 context = base_context;
6627 while (context)
6629 if (mtype == '+')
6630 list = CLASS_CLS_METHODS (context);
6631 else
6632 list = CLASS_NST_METHODS (context);
6634 if (lookup_method (list, chain))
6635 break;
6637 switch (TREE_CODE (context))
6639 case CLASS_IMPLEMENTATION_TYPE:
6640 case CLASS_INTERFACE_TYPE:
6641 context = (CLASS_SUPER_NAME (context)
6642 ? lookup_interface (CLASS_SUPER_NAME (context))
6643 : NULL_TREE);
6644 break;
6645 case CATEGORY_IMPLEMENTATION_TYPE:
6646 case CATEGORY_INTERFACE_TYPE:
6647 context = (CLASS_NAME (context)
6648 ? lookup_interface (CLASS_NAME (context))
6649 : NULL_TREE);
6650 break;
6651 default:
6652 gcc_unreachable ();
6656 if (context == NULL_TREE)
6658 if (first)
6660 switch (TREE_CODE (objc_implementation_context))
6662 case CLASS_IMPLEMENTATION_TYPE:
6663 warning (0, "incomplete implementation of class %qE",
6664 CLASS_NAME (objc_implementation_context));
6665 break;
6666 case CATEGORY_IMPLEMENTATION_TYPE:
6667 warning (0, "incomplete implementation of category %qE",
6668 CLASS_SUPER_NAME (objc_implementation_context));
6669 break;
6670 default:
6671 gcc_unreachable ();
6673 first = 0;
6675 warning (0, "method definition for %<%c%E%> not found",
6676 mtype, METHOD_SEL_NAME (chain));
6679 chain = TREE_CHAIN (chain); /* next method... */
6681 return first;
6684 /* Check whether the current interface (accessible via
6685 'objc_implementation_context') actually implements protocol P, along
6686 with any protocols that P inherits. */
6688 static void
6689 check_protocol (tree p, const char *type, tree name)
6691 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
6693 int f1, f2;
6695 /* Ensure that all protocols have bodies! */
6696 if (warn_protocol)
6698 f1 = check_methods (PROTOCOL_CLS_METHODS (p),
6699 objc_implementation_context,
6700 '+');
6701 f2 = check_methods (PROTOCOL_NST_METHODS (p),
6702 objc_implementation_context,
6703 '-');
6705 else
6707 f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
6708 objc_implementation_context,
6709 '+');
6710 f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
6711 objc_implementation_context,
6712 '-');
6715 if (!f1 || !f2)
6716 warning (0, "%s %qE does not fully implement the %qE protocol",
6717 type, name, PROTOCOL_NAME (p));
6720 /* Check protocols recursively. */
6721 if (PROTOCOL_LIST (p))
6723 tree subs = PROTOCOL_LIST (p);
6724 tree super_class =
6725 lookup_interface (CLASS_SUPER_NAME (implementation_template));
6727 while (subs)
6729 tree sub = TREE_VALUE (subs);
6731 /* If the superclass does not conform to the protocols
6732 inherited by P, then we must! */
6733 if (!super_class || !conforms_to_protocol (super_class, sub))
6734 check_protocol (sub, type, name);
6735 subs = TREE_CHAIN (subs);
6740 /* Check whether the current interface (accessible via
6741 'objc_implementation_context') actually implements the protocols listed
6742 in PROTO_LIST. */
6744 static void
6745 check_protocols (tree proto_list, const char *type, tree name)
6747 for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
6749 tree p = TREE_VALUE (proto_list);
6751 check_protocol (p, type, name);
6755 /* Make sure that the class CLASS_NAME is defined CODE says which kind
6756 of thing CLASS_NAME ought to be. It can be CLASS_INTERFACE_TYPE,
6757 CLASS_IMPLEMENTATION_TYPE, CATEGORY_INTERFACE_TYPE, or
6758 CATEGORY_IMPLEMENTATION_TYPE. For a CATEGORY_INTERFACE_TYPE,
6759 SUPER_NAME is the name of the category. For a class extension,
6760 CODE is CATEGORY_INTERFACE_TYPE and SUPER_NAME is NULL_TREE. */
6761 static tree
6762 start_class (enum tree_code code, tree class_name, tree super_name,
6763 tree protocol_list, tree attributes)
6765 tree klass = NULL_TREE;
6766 tree decl;
6768 #ifdef OBJCPLUS
6769 if (current_namespace != global_namespace)
6771 error ("Objective-C declarations may only appear in global scope");
6773 #endif /* OBJCPLUS */
6775 if (objc_implementation_context)
6777 warning (0, "%<@end%> missing in implementation context");
6778 finish_class (objc_implementation_context);
6779 objc_ivar_chain = NULL_TREE;
6780 objc_implementation_context = NULL_TREE;
6783 /* If this is a class extension, we'll be "reopening" the existing
6784 CLASS_INTERFACE_TYPE, so in that case there is no need to create
6785 a new node. */
6786 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6788 klass = make_node (code);
6789 TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS);
6792 /* Check for existence of the super class, if one was specified. Note
6793 that we must have seen an @interface, not just a @class. If we
6794 are looking at a @compatibility_alias, traverse it first. */
6795 if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE)
6796 && super_name)
6798 tree super = objc_is_class_name (super_name);
6799 tree super_interface = NULL_TREE;
6801 if (super)
6802 super_interface = lookup_interface (super);
6804 if (!super_interface)
6806 error ("cannot find interface declaration for %qE, superclass of %qE",
6807 super ? super : super_name,
6808 class_name);
6809 super_name = NULL_TREE;
6811 else
6813 if (TREE_DEPRECATED (super_interface))
6814 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
6815 super);
6816 super_name = super;
6820 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6822 CLASS_NAME (klass) = class_name;
6823 CLASS_SUPER_NAME (klass) = super_name;
6824 CLASS_CLS_METHODS (klass) = NULL_TREE;
6827 if (! objc_is_class_name (class_name)
6828 && (decl = lookup_name (class_name)))
6830 error ("%qE redeclared as different kind of symbol",
6831 class_name);
6832 error ("previous declaration of %q+D",
6833 decl);
6836 switch (code)
6838 case CLASS_IMPLEMENTATION_TYPE:
6840 tree chain;
6842 for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
6843 if (TREE_VALUE (chain) == class_name)
6845 error ("reimplementation of class %qE",
6846 class_name);
6847 /* TODO: error message saying where it was previously
6848 implemented. */
6849 break;
6851 if (chain == NULL_TREE)
6852 implemented_classes = tree_cons (NULL_TREE, class_name,
6853 implemented_classes);
6856 /* Reset for multiple classes per file. */
6857 method_slot = 0;
6859 objc_implementation_context = klass;
6861 /* Lookup the interface for this implementation. */
6863 if (!(implementation_template = lookup_interface (class_name)))
6865 warning (0, "cannot find interface declaration for %qE",
6866 class_name);
6867 add_interface (implementation_template = objc_implementation_context,
6868 class_name);
6871 /* If a super class has been specified in the implementation,
6872 insure it conforms to the one specified in the interface. */
6874 if (super_name
6875 && (super_name != CLASS_SUPER_NAME (implementation_template)))
6877 tree previous_name = CLASS_SUPER_NAME (implementation_template);
6878 error ("conflicting super class name %qE",
6879 super_name);
6880 if (previous_name)
6881 error ("previous declaration of %qE", previous_name);
6882 else
6883 error ("previous declaration");
6886 else if (! super_name)
6888 CLASS_SUPER_NAME (objc_implementation_context)
6889 = CLASS_SUPER_NAME (implementation_template);
6891 break;
6893 case CLASS_INTERFACE_TYPE:
6894 if (lookup_interface (class_name))
6895 #ifdef OBJCPLUS
6896 error ("duplicate interface declaration for class %qE", class_name);
6897 #else
6898 warning (0, "duplicate interface declaration for class %qE", class_name);
6899 #endif
6900 else
6901 add_interface (klass, class_name);
6903 if (protocol_list)
6904 CLASS_PROTOCOL_LIST (klass)
6905 = lookup_and_install_protocols (protocol_list, /* definition_required */ true);
6907 if (attributes)
6909 tree attribute;
6910 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
6912 tree name = TREE_PURPOSE (attribute);
6914 /* TODO: Document what the objc_exception attribute is/does. */
6915 /* We handle the 'deprecated' and (undocumented) 'objc_exception'
6916 attributes. */
6917 if (is_attribute_p ("deprecated", name))
6918 TREE_DEPRECATED (klass) = 1;
6919 else if (is_attribute_p ("objc_exception", name))
6920 CLASS_HAS_EXCEPTION_ATTR (klass) = 1;
6921 else
6922 /* Warn about and ignore all others for now, but store them. */
6923 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
6925 TYPE_ATTRIBUTES (klass) = attributes;
6927 break;
6929 case CATEGORY_INTERFACE_TYPE:
6931 tree class_category_is_assoc_with;
6933 /* For a category, class_name is really the name of the class that
6934 the following set of methods will be associated with. We must
6935 find the interface so that can derive the objects template. */
6936 if (!(class_category_is_assoc_with = lookup_interface (class_name)))
6938 error ("cannot find interface declaration for %qE",
6939 class_name);
6940 exit (FATAL_EXIT_CODE);
6942 else
6944 if (TREE_DEPRECATED (class_category_is_assoc_with))
6945 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
6946 class_name);
6948 if (super_name == NULL_TREE)
6950 /* This is a class extension. Get the original
6951 interface, and continue working on it. */
6952 objc_in_class_extension = true;
6953 klass = class_category_is_assoc_with;
6955 if (protocol_list)
6957 /* Append protocols to the original protocol
6958 list. */
6959 CLASS_PROTOCOL_LIST (klass)
6960 = chainon (CLASS_PROTOCOL_LIST (klass),
6961 lookup_and_install_protocols
6962 (protocol_list,
6963 /* definition_required */ true));
6966 else
6968 add_category (class_category_is_assoc_with, klass);
6970 if (protocol_list)
6971 CLASS_PROTOCOL_LIST (klass)
6972 = lookup_and_install_protocols
6973 (protocol_list, /* definition_required */ true);
6977 break;
6979 case CATEGORY_IMPLEMENTATION_TYPE:
6980 /* Reset for multiple classes per file. */
6981 method_slot = 0;
6983 objc_implementation_context = klass;
6985 /* For a category, class_name is really the name of the class that
6986 the following set of methods will be associated with. We must
6987 find the interface so that can derive the objects template. */
6989 if (!(implementation_template = lookup_interface (class_name)))
6991 error ("cannot find interface declaration for %qE",
6992 class_name);
6993 exit (FATAL_EXIT_CODE);
6995 break;
6996 default:
6997 gcc_unreachable ();
6999 return klass;
7002 static tree
7003 continue_class (tree klass)
7005 switch (TREE_CODE (klass))
7007 case CLASS_IMPLEMENTATION_TYPE:
7008 case CATEGORY_IMPLEMENTATION_TYPE:
7010 struct imp_entry *imp_entry;
7012 /* Check consistency of the instance variables. */
7014 if (CLASS_RAW_IVARS (klass))
7015 check_ivars (implementation_template, klass);
7017 /* code generation */
7018 #ifdef OBJCPLUS
7019 push_lang_context (lang_name_c);
7020 #endif
7021 build_private_template (implementation_template);
7022 uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template);
7023 objc_instance_type = build_pointer_type (uprivate_record);
7025 imp_entry = ggc_alloc_imp_entry ();
7027 imp_entry->next = imp_list;
7028 imp_entry->imp_context = klass;
7029 imp_entry->imp_template = implementation_template;
7030 ucls_super_ref = uucls_super_ref = NULL;
7031 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7033 imp_entry->class_decl = (*runtime.class_decl) (klass);
7034 imp_entry->meta_decl = (*runtime.metaclass_decl) (klass);
7036 else
7038 imp_entry->class_decl = (*runtime.category_decl) (klass);
7039 imp_entry->meta_decl = NULL;
7041 imp_entry->has_cxx_cdtors = 0;
7043 /* Append to front and increment count. */
7044 imp_list = imp_entry;
7045 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7046 imp_count++;
7047 else
7048 cat_count++;
7049 #ifdef OBJCPLUS
7050 pop_lang_context ();
7051 #endif /* OBJCPLUS */
7053 return get_class_ivars (implementation_template, true);
7054 break;
7056 case CLASS_INTERFACE_TYPE:
7058 if (objc_in_class_extension)
7059 return NULL_TREE;
7060 #ifdef OBJCPLUS
7061 push_lang_context (lang_name_c);
7062 #endif /* OBJCPLUS */
7063 objc_collecting_ivars = 1;
7064 build_private_template (klass);
7065 objc_collecting_ivars = 0;
7066 #ifdef OBJCPLUS
7067 pop_lang_context ();
7068 #endif /* OBJCPLUS */
7069 return NULL_TREE;
7070 break;
7072 default:
7073 return error_mark_node;
7077 /* This routine builds name of the setter synthesized function. */
7078 char *
7079 objc_build_property_setter_name (tree ident)
7081 /* TODO: Use alloca to allocate buffer of appropriate size. */
7082 static char string[BUFSIZE];
7083 sprintf (string, "set%s:", IDENTIFIER_POINTER (ident));
7084 string[3] = TOUPPER (string[3]);
7085 return string;
7088 /* This routine prepares the declarations of the property accessor
7089 helper functions (objc_getProperty(), etc) that are used when
7090 @synthesize is used.
7092 runtime-specific routines are built in the respective runtime
7093 initialize functions. */
7094 static void
7095 build_common_objc_property_accessor_helpers (void)
7097 tree type;
7099 /* Declare the following function:
7101 objc_getProperty (id self, SEL _cmd,
7102 ptrdiff_t offset, BOOL is_atomic); */
7103 type = build_function_type_list (objc_object_type,
7104 objc_object_type,
7105 objc_selector_type,
7106 ptrdiff_type_node,
7107 boolean_type_node,
7108 NULL_TREE);
7109 objc_getProperty_decl = add_builtin_function ("objc_getProperty",
7110 type, 0, NOT_BUILT_IN,
7111 NULL, NULL_TREE);
7112 TREE_NOTHROW (objc_getProperty_decl) = 0;
7114 /* Declare the following function:
7115 void
7116 objc_setProperty (id self, SEL _cmd,
7117 ptrdiff_t offset, id new_value,
7118 BOOL is_atomic, BOOL should_copy); */
7119 type = build_function_type_list (void_type_node,
7120 objc_object_type,
7121 objc_selector_type,
7122 ptrdiff_type_node,
7123 objc_object_type,
7124 boolean_type_node,
7125 boolean_type_node,
7126 NULL_TREE);
7127 objc_setProperty_decl = add_builtin_function ("objc_setProperty",
7128 type, 0, NOT_BUILT_IN,
7129 NULL, NULL_TREE);
7130 TREE_NOTHROW (objc_setProperty_decl) = 0;
7133 /* This looks up an ivar in a class (including superclasses). */
7134 static tree
7135 lookup_ivar (tree interface, tree instance_variable_name)
7137 while (interface)
7139 tree decl_chain;
7141 for (decl_chain = CLASS_IVARS (interface); decl_chain; decl_chain = DECL_CHAIN (decl_chain))
7142 if (DECL_NAME (decl_chain) == instance_variable_name)
7143 return decl_chain;
7145 /* Not found. Search superclass if any. */
7146 if (CLASS_SUPER_NAME (interface))
7147 interface = lookup_interface (CLASS_SUPER_NAME (interface));
7150 return NULL_TREE;
7153 /* This routine synthesizes a 'getter' method. This is only called
7154 for @synthesize properties. */
7155 static void
7156 objc_synthesize_getter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
7158 location_t location = DECL_SOURCE_LOCATION (property);
7159 tree fn, decl;
7160 tree body;
7161 tree ret_val;
7163 /* If user has implemented a getter with same name then do nothing. */
7164 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7165 PROPERTY_GETTER_NAME (property)))
7166 return;
7168 /* Find declaration of the property getter in the interface (or
7169 superclass, or protocol). There must be one. */
7170 decl = lookup_method_static (klass, PROPERTY_GETTER_NAME (property), 0);
7172 /* If one not declared in the interface, this condition has already
7173 been reported as user error (because property was not declared in
7174 the interface). */
7175 if (!decl)
7176 return;
7178 /* Adapt the 'decl'. Use the source location of the @synthesize
7179 statement for error messages. */
7180 decl = copy_node (decl);
7181 DECL_SOURCE_LOCATION (decl) = location;
7183 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7184 NULL_TREE);
7185 body = c_begin_compound_stmt (true);
7187 /* Now we need to decide how we build the getter. There are three
7188 cases:
7190 for 'copy' or 'retain' properties we need to use the
7191 objc_getProperty() accessor helper which knows about retain and
7192 copy. It supports both 'nonatomic' and 'atomic' access.
7194 for 'nonatomic, assign' properties we can access the instance
7195 variable directly. 'nonatomic' means we don't have to use locks,
7196 and 'assign' means we don't have to worry about retain or copy.
7197 If you combine the two, it means we can just access the instance
7198 variable directly.
7200 for 'atomic, assign' properties we use objc_copyStruct() (for the
7201 next runtime) or objc_getPropertyStruct() (for the GNU runtime). */
7202 switch (PROPERTY_ASSIGN_SEMANTICS (property))
7204 case OBJC_PROPERTY_RETAIN:
7205 case OBJC_PROPERTY_COPY:
7207 /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */
7208 tree cmd, ivar, offset, is_atomic;
7209 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
7211 /* Find the ivar to compute the offset. */
7212 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7213 if (!ivar || is_private (ivar))
7215 /* This should never happen. */
7216 error_at (location,
7217 "can not find instance variable associated with property");
7218 ret_val = error_mark_node;
7219 break;
7221 offset = byte_position (ivar);
7223 if (PROPERTY_NONATOMIC (property))
7224 is_atomic = boolean_false_node;
7225 else
7226 is_atomic = boolean_true_node;
7228 ret_val = build_function_call
7229 (location,
7230 /* Function prototype. */
7231 objc_getProperty_decl,
7232 /* Parameters. */
7233 tree_cons /* self */
7234 (NULL_TREE, self_decl,
7235 tree_cons /* _cmd */
7236 (NULL_TREE, cmd,
7237 tree_cons /* offset */
7238 (NULL_TREE, offset,
7239 tree_cons /* is_atomic */
7240 (NULL_TREE, is_atomic, NULL_TREE)))));
7242 break;
7243 case OBJC_PROPERTY_ASSIGN:
7244 if (PROPERTY_NONATOMIC (property))
7246 /* We build "return self->PROPERTY_IVAR_NAME;" */
7247 ret_val = objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property));
7248 break;
7250 else
7252 /* We build
7253 <property type> __objc_property_temp;
7254 objc_getPropertyStruct (&__objc_property_temp,
7255 &(self->PROPERTY_IVAR_NAME),
7256 sizeof (type of self->PROPERTY_IVAR_NAME),
7257 is_atomic,
7258 false)
7259 return __objc_property_temp;
7261 For the NeXT runtime, we need to use objc_copyStruct
7262 instead of objc_getPropertyStruct. */
7263 tree objc_property_temp_decl, function_decl, function_call;
7264 tree size_of, is_atomic;
7266 objc_property_temp_decl = objc_create_temporary_var (TREE_TYPE (property), "__objc_property_temp");
7267 DECL_SOURCE_LOCATION (objc_property_temp_decl) = location;
7268 objc_property_temp_decl = lang_hooks.decls.pushdecl (objc_property_temp_decl);
7270 /* sizeof (ivar type). Since the ivar and the property have
7271 the same type, there is no need to lookup the ivar. */
7272 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7273 true /* is_sizeof */,
7274 false /* complain */);
7276 if (PROPERTY_NONATOMIC (property))
7277 is_atomic = boolean_false_node;
7278 else
7279 is_atomic = boolean_true_node;
7281 if (objc_copyStruct_decl)
7282 function_decl = objc_copyStruct_decl;
7283 else
7284 function_decl = objc_getPropertyStruct_decl;
7286 function_call = build_function_call
7287 (location,
7288 /* Function prototype. */
7289 function_decl,
7290 /* Parameters. */
7291 tree_cons /* &__objc_property_temp_decl */
7292 /* Warning: note that using build_fold_addr_expr_loc()
7293 here causes invalid code to be generated. */
7294 (NULL_TREE, build_unary_op (location, ADDR_EXPR, objc_property_temp_decl, 0),
7295 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
7296 (NULL_TREE, build_fold_addr_expr_loc (location,
7297 objc_lookup_ivar
7298 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7299 tree_cons /* sizeof (PROPERTY_IVAR) */
7300 (NULL_TREE, size_of,
7301 tree_cons /* is_atomic */
7302 (NULL_TREE, is_atomic,
7303 /* TODO: This is currently ignored by the GNU
7304 runtime, but what about the next one ? */
7305 tree_cons /* has_strong */
7306 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7308 add_stmt (function_call);
7310 ret_val = objc_property_temp_decl;
7312 break;
7313 default:
7314 gcc_unreachable ();
7317 gcc_assert (ret_val);
7319 #ifdef OBJCPLUS
7320 finish_return_stmt (ret_val);
7321 #else
7322 c_finish_return (location, ret_val, NULL_TREE);
7323 #endif
7325 add_stmt (c_end_compound_stmt (location, body, true));
7326 fn = current_function_decl;
7327 #ifdef OBJCPLUS
7328 finish_function ();
7329 #endif
7330 objc_finish_method_definition (fn);
7333 /* This routine synthesizes a 'setter' method. */
7335 static void
7336 objc_synthesize_setter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
7338 location_t location = DECL_SOURCE_LOCATION (property);
7339 tree fn, decl;
7340 tree body;
7341 tree new_value, statement;
7343 /* If user has implemented a setter with same name then do nothing. */
7344 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7345 PROPERTY_SETTER_NAME (property)))
7346 return;
7348 /* Find declaration of the property setter in the interface (or
7349 superclass, or protocol). There must be one. */
7350 decl = lookup_method_static (klass, PROPERTY_SETTER_NAME (property), 0);
7352 /* If one not declared in the interface, this condition has already
7353 been reported as user error (because property was not declared in
7354 the interface). */
7355 if (!decl)
7356 return;
7358 /* Adapt the 'decl'. Use the source location of the @synthesize
7359 statement for error messages. */
7360 decl = copy_node (decl);
7361 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (property);
7363 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7364 NULL_TREE);
7366 body = c_begin_compound_stmt (true);
7368 /* The 'new_value' is the only argument to the method, which is the
7369 3rd argument of the function, after self and _cmd. We use twice
7370 TREE_CHAIN to move forward two arguments. */
7371 new_value = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)));
7373 /* This would presumably happen if the user has specified a
7374 prototype for the setter that does not have an argument! */
7375 if (new_value == NULL_TREE)
7377 /* TODO: This should be caught much earlier than this. */
7378 error_at (DECL_SOURCE_LOCATION (decl), "invalid setter, it must have one argument");
7379 /* Try to recover somehow. */
7380 new_value = error_mark_node;
7383 /* Now we need to decide how we build the setter. There are three
7384 cases:
7386 for 'copy' or 'retain' properties we need to use the
7387 objc_setProperty() accessor helper which knows about retain and
7388 copy. It supports both 'nonatomic' and 'atomic' access.
7390 for 'nonatomic, assign' properties we can access the instance
7391 variable directly. 'nonatomic' means we don't have to use locks,
7392 and 'assign' means we don't have to worry about retain or copy.
7393 If you combine the two, it means we can just access the instance
7394 variable directly.
7396 for 'atomic, assign' properties we use objc_copyStruct() (for the
7397 next runtime) or objc_setPropertyStruct() (for the GNU runtime). */
7398 switch (PROPERTY_ASSIGN_SEMANTICS (property))
7400 case OBJC_PROPERTY_RETAIN:
7401 case OBJC_PROPERTY_COPY:
7403 /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */
7404 tree cmd, ivar, offset, is_atomic, should_copy;
7405 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
7407 /* Find the ivar to compute the offset. */
7408 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7409 if (!ivar || is_private (ivar))
7411 error_at (location,
7412 "can not find instance variable associated with property");
7413 statement = error_mark_node;
7414 break;
7416 offset = byte_position (ivar);
7418 if (PROPERTY_NONATOMIC (property))
7419 is_atomic = boolean_false_node;
7420 else
7421 is_atomic = boolean_true_node;
7423 if (PROPERTY_ASSIGN_SEMANTICS (property) == OBJC_PROPERTY_COPY)
7424 should_copy = boolean_true_node;
7425 else
7426 should_copy = boolean_false_node;
7428 statement = build_function_call
7429 (location,
7430 /* Function prototype. */
7431 objc_setProperty_decl,
7432 /* Parameters. */
7433 tree_cons /* self */
7434 (NULL_TREE, self_decl,
7435 tree_cons /* _cmd */
7436 (NULL_TREE, cmd,
7437 tree_cons /* offset */
7438 (NULL_TREE, offset,
7439 tree_cons /* new_value */
7440 (NULL_TREE, new_value,
7441 tree_cons /* is_atomic */
7442 (NULL_TREE, is_atomic,
7443 tree_cons /* should_copy */
7444 (NULL_TREE, should_copy, NULL_TREE)))))));
7446 break;
7447 case OBJC_PROPERTY_ASSIGN:
7448 if (PROPERTY_NONATOMIC (property))
7450 /* We build "self->PROPERTY_IVAR_NAME = new_value;" */
7451 statement = build_modify_expr
7452 (location,
7453 objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)),
7454 NULL_TREE, NOP_EXPR,
7455 location, new_value, NULL_TREE);
7456 break;
7458 else
7460 /* We build
7461 objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME),
7462 &new_value,
7463 sizeof (type of self->PROPERTY_IVAR_NAME),
7464 is_atomic,
7465 false)
7467 For the NeXT runtime, we need to use objc_copyStruct
7468 instead of objc_getPropertyStruct. */
7469 tree function_decl, size_of, is_atomic;
7471 /* sizeof (ivar type). Since the ivar and the property have
7472 the same type, there is no need to lookup the ivar. */
7473 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7474 true /* is_sizeof */,
7475 false /* complain */);
7477 if (PROPERTY_NONATOMIC (property))
7478 is_atomic = boolean_false_node;
7479 else
7480 is_atomic = boolean_true_node;
7482 if (objc_copyStruct_decl)
7483 function_decl = objc_copyStruct_decl;
7484 else
7485 function_decl = objc_setPropertyStruct_decl;
7487 statement = build_function_call
7488 (location,
7489 /* Function prototype. */
7490 function_decl,
7491 /* Parameters. */
7492 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
7493 (NULL_TREE, build_fold_addr_expr_loc (location,
7494 objc_lookup_ivar
7495 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7496 tree_cons /* &new_value */
7497 (NULL_TREE, build_fold_addr_expr_loc (location, new_value),
7498 tree_cons /* sizeof (PROPERTY_IVAR) */
7499 (NULL_TREE, size_of,
7500 tree_cons /* is_atomic */
7501 (NULL_TREE, is_atomic,
7502 /* TODO: This is currently ignored by the GNU
7503 runtime, but what about the next one ? */
7504 tree_cons /* has_strong */
7505 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7507 break;
7508 default:
7509 gcc_unreachable ();
7511 gcc_assert (statement);
7513 add_stmt (statement);
7514 add_stmt (c_end_compound_stmt (location, body, true));
7515 fn = current_function_decl;
7516 #ifdef OBJCPLUS
7517 finish_function ();
7518 #endif
7519 objc_finish_method_definition (fn);
7522 /* This function is a sub-routine of objc_add_synthesize_declaration.
7523 It is called for each property to synthesize once we have
7524 determined that the context is Ok. */
7525 static void
7526 objc_add_synthesize_declaration_for_property (location_t location, tree interface,
7527 tree property_name, tree ivar_name)
7529 /* Find the @property declaration. */
7530 tree property;
7531 tree x;
7533 /* Check that synthesize or dynamic has not already been used for
7534 the same property. */
7535 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7536 if (PROPERTY_NAME (property) == property_name)
7538 location_t original_location = DECL_SOURCE_LOCATION (property);
7540 if (PROPERTY_DYNAMIC (property))
7541 error_at (location, "property %qs already specified in %<@dynamic%>",
7542 IDENTIFIER_POINTER (property_name));
7543 else
7544 error_at (location, "property %qs already specified in %<@synthesize%>",
7545 IDENTIFIER_POINTER (property_name));
7547 if (original_location != UNKNOWN_LOCATION)
7548 inform (original_location, "originally specified here");
7549 return;
7552 /* Check that the property is declared in the interface. It could
7553 also be declared in a superclass or protocol. */
7554 property = lookup_property (interface, property_name);
7556 if (!property)
7558 error_at (location, "no declaration of property %qs found in the interface",
7559 IDENTIFIER_POINTER (property_name));
7560 return;
7562 else
7564 /* We have to copy the property, because we want to chain it to
7565 the implementation context, and we want to store the source
7566 location of the @synthesize, not of the original
7567 @property. */
7568 property = copy_node (property);
7569 DECL_SOURCE_LOCATION (property) = location;
7572 /* Determine PROPERTY_IVAR_NAME. */
7573 if (ivar_name == NULL_TREE)
7574 ivar_name = property_name;
7576 /* Check that the instance variable exists. You can only use an
7577 instance variable from the same class, not one from the
7578 superclass (this makes sense as it allows us to check that an
7579 instance variable is only used in one synthesized property). */
7581 tree ivar = is_ivar (CLASS_IVARS (interface), ivar_name);
7582 tree type_of_ivar;
7583 if (!ivar)
7585 error_at (location, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar",
7586 IDENTIFIER_POINTER (property_name));
7587 return;
7590 if (DECL_BIT_FIELD_TYPE (ivar))
7591 type_of_ivar = DECL_BIT_FIELD_TYPE (ivar);
7592 else
7593 type_of_ivar = TREE_TYPE (ivar);
7595 /* If the instance variable has a different C type, we throw an error ... */
7596 if (!comptypes (TREE_TYPE (property), type_of_ivar)
7597 /* ... unless the property is readonly, in which case we allow
7598 the instance variable to be more specialized (this means we
7599 can generate the getter all right and it works). */
7600 && (!PROPERTY_READONLY (property)
7601 || !objc_compare_types (TREE_TYPE (property),
7602 type_of_ivar, -5, NULL_TREE)))
7604 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7606 error_at (location, "property %qs is using instance variable %qs of incompatible type",
7607 IDENTIFIER_POINTER (property_name),
7608 IDENTIFIER_POINTER (ivar_name));
7610 if (original_location != UNKNOWN_LOCATION)
7611 inform (original_location, "originally specified here");
7614 /* If the instance variable is a bitfield, the property must be
7615 'assign', 'nonatomic' because the runtime getter/setter helper
7616 do not work with bitfield instance variables. */
7617 if (DECL_BIT_FIELD_TYPE (ivar))
7619 /* If there is an error, we return and not generate any
7620 getter/setter because trying to set up the runtime
7621 getter/setter helper calls with bitfields is at high risk
7622 of ICE. */
7624 if (PROPERTY_ASSIGN_SEMANTICS (property) != OBJC_PROPERTY_ASSIGN)
7626 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7628 error_at (location, "'assign' property %qs is using bit-field instance variable %qs",
7629 IDENTIFIER_POINTER (property_name),
7630 IDENTIFIER_POINTER (ivar_name));
7632 if (original_location != UNKNOWN_LOCATION)
7633 inform (original_location, "originally specified here");
7634 return;
7637 if (!PROPERTY_NONATOMIC (property))
7639 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7641 error_at (location, "'atomic' property %qs is using bit-field instance variable %qs",
7642 IDENTIFIER_POINTER (property_name),
7643 IDENTIFIER_POINTER (ivar_name));
7645 if (original_location != UNKNOWN_LOCATION)
7646 inform (original_location, "originally specified here");
7647 return;
7652 /* Check that no other property is using the same instance
7653 variable. */
7654 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7655 if (PROPERTY_IVAR_NAME (x) == ivar_name)
7657 location_t original_location = DECL_SOURCE_LOCATION (x);
7659 error_at (location, "property %qs is using the same instance variable as property %qs",
7660 IDENTIFIER_POINTER (property_name),
7661 IDENTIFIER_POINTER (PROPERTY_NAME (x)));
7663 if (original_location != UNKNOWN_LOCATION)
7664 inform (original_location, "originally specified here");
7666 /* We keep going on. This won't cause the compiler to fail;
7667 the failure would most likely be at runtime. */
7670 /* Note that a @synthesize (and only a @synthesize) always sets
7671 PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a
7672 @synthesize by that. */
7673 PROPERTY_IVAR_NAME (property) = ivar_name;
7675 /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the
7676 original declaration; they are always set (with the exception of
7677 PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */
7679 /* Add the property to the list of properties for current implementation. */
7680 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7681 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7683 /* Note how we don't actually synthesize the getter/setter here; it
7684 would be very natural, but we may miss the fact that the user has
7685 implemented his own getter/setter later on in the @implementation
7686 (in which case we shouldn't generate getter/setter). We wait
7687 until we have parsed it all before generating the code. */
7690 /* This function is called by the parser after a @synthesize
7691 expression is parsed. 'location' is the location of the
7692 @synthesize expression, and 'property_and_ivar_list' is a chained
7693 list of the property and ivar names. */
7694 void
7695 objc_add_synthesize_declaration (location_t location, tree property_and_ivar_list)
7697 tree interface, chain;
7699 if (flag_objc1_only)
7700 error_at (input_location, "%<@synthesize%> is not available in Objective-C 1.0");
7702 if (property_and_ivar_list == error_mark_node)
7703 return;
7705 if (!objc_implementation_context)
7707 /* We can get here only in Objective-C; the Objective-C++ parser
7708 detects the problem while parsing, outputs the error
7709 "misplaced '@synthesize' Objective-C++ construct" and skips
7710 the declaration. */
7711 error_at (location, "%<@synthesize%> not in @implementation context");
7712 return;
7715 if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE)
7717 error_at (location, "%<@synthesize%> can not be used in categories");
7718 return;
7721 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7722 if (!interface)
7724 /* I can't see how this could happen, but it is good as a safety check. */
7725 error_at (location,
7726 "%<@synthesize%> requires the @interface of the class to be available");
7727 return;
7730 /* Now, iterate over the properties and do each of them. */
7731 for (chain = property_and_ivar_list; chain; chain = TREE_CHAIN (chain))
7733 objc_add_synthesize_declaration_for_property (location, interface, TREE_VALUE (chain),
7734 TREE_PURPOSE (chain));
7738 /* This function is a sub-routine of objc_add_dynamic_declaration. It
7739 is called for each property to mark as dynamic once we have
7740 determined that the context is Ok. */
7741 static void
7742 objc_add_dynamic_declaration_for_property (location_t location, tree interface,
7743 tree property_name)
7745 /* Find the @property declaration. */
7746 tree property;
7748 /* Check that synthesize or dynamic has not already been used for
7749 the same property. */
7750 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7751 if (PROPERTY_NAME (property) == property_name)
7753 location_t original_location = DECL_SOURCE_LOCATION (property);
7755 if (PROPERTY_DYNAMIC (property))
7756 error_at (location, "property %qs already specified in %<@dynamic%>",
7757 IDENTIFIER_POINTER (property_name));
7758 else
7759 error_at (location, "property %qs already specified in %<@synthesize%>",
7760 IDENTIFIER_POINTER (property_name));
7762 if (original_location != UNKNOWN_LOCATION)
7763 inform (original_location, "originally specified here");
7764 return;
7767 /* Check that the property is declared in the interface. It could
7768 also be declared in a superclass or protocol. */
7769 property = lookup_property (interface, property_name);
7771 if (!property)
7773 error_at (location, "no declaration of property %qs found in the interface",
7774 IDENTIFIER_POINTER (property_name));
7775 return;
7777 else
7779 /* We have to copy the property, because we want to chain it to
7780 the implementation context, and we want to store the source
7781 location of the @synthesize, not of the original
7782 @property. */
7783 property = copy_node (property);
7784 DECL_SOURCE_LOCATION (property) = location;
7787 /* Note that a @dynamic (and only a @dynamic) always sets
7788 PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that.
7789 (actually, as explained above, PROPERTY_DECL generated by
7790 @property and associated with a @dynamic property are also marked
7791 as PROPERTY_DYNAMIC). */
7792 PROPERTY_DYNAMIC (property) = 1;
7794 /* Add the property to the list of properties for current implementation. */
7795 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7796 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7799 /* This function is called by the parser after a @dynamic expression
7800 is parsed. 'location' is the location of the @dynamic expression,
7801 and 'property_list' is a chained list of all the property
7802 names. */
7803 void
7804 objc_add_dynamic_declaration (location_t location, tree property_list)
7806 tree interface, chain;
7808 if (flag_objc1_only)
7809 error_at (input_location, "%<@dynamic%> is not available in Objective-C 1.0");
7811 if (property_list == error_mark_node)
7812 return;
7814 if (!objc_implementation_context)
7816 /* We can get here only in Objective-C; the Objective-C++ parser
7817 detects the problem while parsing, outputs the error
7818 "misplaced '@dynamic' Objective-C++ construct" and skips the
7819 declaration. */
7820 error_at (location, "%<@dynamic%> not in @implementation context");
7821 return;
7824 /* @dynamic is allowed in categories. */
7825 switch (TREE_CODE (objc_implementation_context))
7827 case CLASS_IMPLEMENTATION_TYPE:
7828 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7829 break;
7830 case CATEGORY_IMPLEMENTATION_TYPE:
7831 interface = lookup_category (implementation_template,
7832 CLASS_SUPER_NAME (objc_implementation_context));
7833 break;
7834 default:
7835 gcc_unreachable ();
7838 if (!interface)
7840 /* I can't see how this could happen, but it is good as a safety check. */
7841 error_at (location,
7842 "%<@dynamic%> requires the @interface of the class to be available");
7843 return;
7846 /* Now, iterate over the properties and do each of them. */
7847 for (chain = property_list; chain; chain = TREE_CHAIN (chain))
7849 objc_add_dynamic_declaration_for_property (location, interface, TREE_VALUE (chain));
7853 /* Main routine to generate code/data for all the property information for
7854 current implementation (class or category). CLASS is the interface where
7855 ivars are declared. CLASS_METHODS is where methods are found which
7856 could be a class or a category depending on whether we are implementing
7857 property of a class or a category. */
7859 static void
7860 objc_gen_property_data (tree klass, tree class_methods)
7862 tree x;
7864 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7866 /* @dynamic property - nothing to check or synthesize. */
7867 if (PROPERTY_DYNAMIC (x))
7868 continue;
7870 /* @synthesize property - need to synthesize the accessors. */
7871 if (PROPERTY_IVAR_NAME (x))
7873 objc_synthesize_getter (klass, class_methods, x);
7875 if (PROPERTY_READONLY (x) == 0)
7876 objc_synthesize_setter (klass, class_methods, x);
7878 continue;
7881 gcc_unreachable ();
7885 /* This is called once we see the "@end" in an interface/implementation. */
7887 static void
7888 finish_class (tree klass)
7890 switch (TREE_CODE (klass))
7892 case CLASS_IMPLEMENTATION_TYPE:
7894 /* All metadata generation is done in runtime.generate_metadata(). */
7896 /* Generate what needed for property; setters, getters, etc. */
7897 objc_gen_property_data (implementation_template, implementation_template);
7899 if (implementation_template != objc_implementation_context)
7901 /* Ensure that all method listed in the interface contain bodies. */
7902 check_methods (CLASS_CLS_METHODS (implementation_template),
7903 objc_implementation_context, '+');
7904 check_methods (CLASS_NST_METHODS (implementation_template),
7905 objc_implementation_context, '-');
7907 if (CLASS_PROTOCOL_LIST (implementation_template))
7908 check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
7909 "class",
7910 CLASS_NAME (objc_implementation_context));
7912 break;
7914 case CATEGORY_IMPLEMENTATION_TYPE:
7916 tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass));
7918 if (category)
7920 /* Generate what needed for property; setters, getters, etc. */
7921 objc_gen_property_data (implementation_template, category);
7923 /* Ensure all method listed in the interface contain bodies. */
7924 check_methods (CLASS_CLS_METHODS (category),
7925 objc_implementation_context, '+');
7926 check_methods (CLASS_NST_METHODS (category),
7927 objc_implementation_context, '-');
7929 if (CLASS_PROTOCOL_LIST (category))
7930 check_protocols (CLASS_PROTOCOL_LIST (category),
7931 "category",
7932 CLASS_SUPER_NAME (objc_implementation_context));
7934 break;
7936 case CLASS_INTERFACE_TYPE:
7937 case CATEGORY_INTERFACE_TYPE:
7938 case PROTOCOL_INTERFACE_TYPE:
7940 /* Process properties of the class. */
7941 tree x;
7942 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
7944 /* Now we check that the appropriate getter is declared,
7945 and if not, we declare one ourselves. */
7946 tree getter_decl = lookup_method (CLASS_NST_METHODS (klass),
7947 PROPERTY_GETTER_NAME (x));
7949 if (getter_decl)
7951 /* TODO: Check that the declaration is consistent with the property. */
7954 else
7956 /* Generate an instance method declaration for the
7957 getter; for example "- (id) name;". In general it
7958 will be of the form
7959 -(type)property_getter_name; */
7960 tree rettype = build_tree_list (NULL_TREE, TREE_TYPE (x));
7961 getter_decl = build_method_decl (INSTANCE_METHOD_DECL,
7962 rettype, PROPERTY_GETTER_NAME (x),
7963 NULL_TREE, false);
7964 if (PROPERTY_OPTIONAL (x))
7965 objc_add_method (objc_interface_context, getter_decl, false, true);
7966 else
7967 objc_add_method (objc_interface_context, getter_decl, false, false);
7968 TREE_DEPRECATED (getter_decl) = TREE_DEPRECATED (x);
7969 METHOD_PROPERTY_CONTEXT (getter_decl) = x;
7972 if (PROPERTY_READONLY (x) == 0)
7974 /* Now we check that the appropriate setter is declared,
7975 and if not, we declare on ourselves. */
7976 tree setter_decl = lookup_method (CLASS_NST_METHODS (klass),
7977 PROPERTY_SETTER_NAME (x));
7979 if (setter_decl)
7981 /* TODO: Check that the declaration is consistent with the property. */
7984 else
7986 /* The setter name is something like 'setName:'.
7987 We need the substring 'setName' to build the
7988 method declaration due to how the declaration
7989 works. TODO: build_method_decl() will then
7990 generate back 'setName:' from 'setName'; it
7991 would be more efficient to hook into there. */
7992 const char *full_setter_name = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x));
7993 size_t length = strlen (full_setter_name);
7994 char *setter_name = (char *) alloca (length);
7995 tree ret_type, selector, arg_type, arg_name;
7997 strcpy (setter_name, full_setter_name);
7998 setter_name[length - 1] = '\0';
7999 ret_type = build_tree_list (NULL_TREE, void_type_node);
8000 arg_type = build_tree_list (NULL_TREE, TREE_TYPE (x));
8001 arg_name = get_identifier ("_value");
8002 selector = objc_build_keyword_decl (get_identifier (setter_name),
8003 arg_type, arg_name, NULL);
8004 setter_decl = build_method_decl (INSTANCE_METHOD_DECL,
8005 ret_type, selector,
8006 build_tree_list (NULL_TREE, NULL_TREE),
8007 false);
8008 if (PROPERTY_OPTIONAL (x))
8009 objc_add_method (objc_interface_context, setter_decl, false, true);
8010 else
8011 objc_add_method (objc_interface_context, setter_decl, false, false);
8012 TREE_DEPRECATED (setter_decl) = TREE_DEPRECATED (x);
8013 METHOD_PROPERTY_CONTEXT (setter_decl) = x;
8017 break;
8019 default:
8020 gcc_unreachable ();
8021 break;
8025 static tree
8026 add_protocol (tree protocol)
8028 /* Put protocol on list in reverse order. */
8029 TREE_CHAIN (protocol) = protocol_chain;
8030 protocol_chain = protocol;
8031 return protocol_chain;
8034 /* Check that a protocol is defined, and, recursively, that all
8035 protocols that this protocol conforms to are defined too. */
8036 static void
8037 check_that_protocol_is_defined (tree protocol)
8039 if (!PROTOCOL_DEFINED (protocol))
8040 warning (0, "definition of protocol %qE not found",
8041 PROTOCOL_NAME (protocol));
8043 /* If the protocol itself conforms to other protocols, check them
8044 too, recursively. */
8045 if (PROTOCOL_LIST (protocol))
8047 tree p;
8049 for (p = PROTOCOL_LIST (protocol); p; p = TREE_CHAIN (p))
8050 check_that_protocol_is_defined (TREE_VALUE (p));
8054 /* Looks up a protocol. If 'warn_if_deprecated' is true, a warning is
8055 emitted if the protocol is deprecated. If 'definition_required' is
8056 true, a warning is emitted if a full @protocol definition has not
8057 been seen. */
8058 static tree
8059 lookup_protocol (tree ident, bool warn_if_deprecated, bool definition_required)
8061 tree chain;
8063 for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
8064 if (ident == PROTOCOL_NAME (chain))
8066 if (warn_if_deprecated && TREE_DEPRECATED (chain))
8068 /* It would be nice to use warn_deprecated_use() here, but
8069 we are using TREE_CHAIN (which is supposed to be the
8070 TYPE_STUB_DECL for a TYPE) for something different. */
8071 warning (OPT_Wdeprecated_declarations, "protocol %qE is deprecated",
8072 PROTOCOL_NAME (chain));
8075 if (definition_required)
8076 check_that_protocol_is_defined (chain);
8078 return chain;
8081 return NULL_TREE;
8084 /* This function forward declares the protocols named by NAMES. If
8085 they are already declared or defined, the function has no effect. */
8087 void
8088 objc_declare_protocol (tree name, tree attributes)
8090 bool deprecated = false;
8092 #ifdef OBJCPLUS
8093 if (current_namespace != global_namespace) {
8094 error ("Objective-C declarations may only appear in global scope");
8096 #endif /* OBJCPLUS */
8098 /* Determine if 'deprecated', the only attribute we recognize for
8099 protocols, was used. Ignore all other attributes. */
8100 if (attributes)
8102 tree attribute;
8103 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8105 tree name = TREE_PURPOSE (attribute);
8107 if (is_attribute_p ("deprecated", name))
8108 deprecated = true;
8109 else
8110 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8114 if (lookup_protocol (name, /* warn if deprecated */ false,
8115 /* definition_required */ false) == NULL_TREE)
8117 tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
8119 TYPE_LANG_SLOT_1 (protocol)
8120 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8121 PROTOCOL_NAME (protocol) = name;
8122 PROTOCOL_LIST (protocol) = NULL_TREE;
8123 add_protocol (protocol);
8124 PROTOCOL_DEFINED (protocol) = 0;
8125 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8127 if (attributes)
8129 /* TODO: Do we need to store the attributes here ? */
8130 TYPE_ATTRIBUTES (protocol) = attributes;
8131 if (deprecated)
8132 TREE_DEPRECATED (protocol) = 1;
8137 static tree
8138 start_protocol (enum tree_code code, tree name, tree list, tree attributes)
8140 tree protocol;
8141 bool deprecated = false;
8143 #ifdef OBJCPLUS
8144 if (current_namespace != global_namespace) {
8145 error ("Objective-C declarations may only appear in global scope");
8147 #endif /* OBJCPLUS */
8149 /* Determine if 'deprecated', the only attribute we recognize for
8150 protocols, was used. Ignore all other attributes. */
8151 if (attributes)
8153 tree attribute;
8154 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8156 tree name = TREE_PURPOSE (attribute);
8158 if (is_attribute_p ("deprecated", name))
8159 deprecated = true;
8160 else
8161 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8165 protocol = lookup_protocol (name, /* warn_if_deprecated */ false,
8166 /* definition_required */ false);
8168 if (!protocol)
8170 protocol = make_node (code);
8171 TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8173 PROTOCOL_NAME (protocol) = name;
8174 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8175 add_protocol (protocol);
8176 PROTOCOL_DEFINED (protocol) = 1;
8177 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8179 check_protocol_recursively (protocol, list);
8181 else if (! PROTOCOL_DEFINED (protocol))
8183 PROTOCOL_DEFINED (protocol) = 1;
8184 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8186 check_protocol_recursively (protocol, list);
8188 else
8190 warning (0, "duplicate declaration for protocol %qE",
8191 name);
8194 if (attributes)
8196 TYPE_ATTRIBUTES (protocol) = attributes;
8197 if (deprecated)
8198 TREE_DEPRECATED (protocol) = 1;
8201 return protocol;
8204 /* Decay array and function parameters into pointers. */
8206 static tree
8207 objc_decay_parm_type (tree type)
8209 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE)
8210 type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE
8211 ? TREE_TYPE (type)
8212 : type);
8214 return type;
8217 static GTY(()) tree objc_parmlist = NULL_TREE;
8219 /* Append PARM to a list of formal parameters of a method, making a necessary
8220 array-to-pointer adjustment along the way. */
8222 void
8223 objc_push_parm (tree parm)
8225 tree type;
8227 if (TREE_TYPE (parm) == error_mark_node)
8229 objc_parmlist = chainon (objc_parmlist, parm);
8230 return;
8233 /* Decay arrays and functions into pointers. */
8234 type = objc_decay_parm_type (TREE_TYPE (parm));
8236 /* If the parameter type has been decayed, a new PARM_DECL needs to be
8237 built as well. */
8238 if (type != TREE_TYPE (parm))
8239 parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type);
8241 DECL_ARG_TYPE (parm)
8242 = lang_hooks.types.type_promotes_to (TREE_TYPE (parm));
8244 /* Record constancy and volatility. */
8245 c_apply_type_quals_to_decl
8246 ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0)
8247 | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0)
8248 | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm);
8250 objc_parmlist = chainon (objc_parmlist, parm);
8253 /* Retrieve the formal parameter list constructed via preceding calls to
8254 objc_push_parm(). */
8256 #ifdef OBJCPLUS
8257 tree
8258 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED,
8259 tree expr ATTRIBUTE_UNUSED)
8261 tree parm_info = objc_parmlist;
8262 objc_parmlist = NULL_TREE;
8264 return parm_info;
8266 #else
8267 struct c_arg_info *
8268 objc_get_parm_info (int have_ellipsis, tree expr)
8270 tree parm_info = objc_parmlist;
8271 struct c_arg_info *arg_info;
8272 /* The C front-end requires an elaborate song and dance at
8273 this point. */
8274 push_scope ();
8275 declare_parm_level ();
8276 while (parm_info)
8278 tree next = DECL_CHAIN (parm_info);
8280 DECL_CHAIN (parm_info) = NULL_TREE;
8281 parm_info = pushdecl (parm_info);
8282 finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
8283 parm_info = next;
8285 arg_info = get_parm_info (have_ellipsis, expr);
8286 pop_scope ();
8287 objc_parmlist = NULL_TREE;
8288 return arg_info;
8290 #endif
8292 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
8293 method definitions. In the case of instance methods, we can be more
8294 specific as to the type of 'self'. */
8296 static void
8297 synth_self_and_ucmd_args (void)
8299 tree self_type;
8301 if (objc_method_context
8302 && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL)
8303 self_type = objc_instance_type;
8304 else
8305 /* Really a `struct objc_class *'. However, we allow people to
8306 assign to self, which changes its type midstream. */
8307 self_type = objc_object_type;
8309 /* id self; */
8310 objc_push_parm (build_decl (input_location,
8311 PARM_DECL, self_id, self_type));
8313 /* SEL _cmd; */
8314 objc_push_parm (build_decl (input_location,
8315 PARM_DECL, ucmd_id, objc_selector_type));
8318 /* Transform an Objective-C method definition into a static C function
8319 definition, synthesizing the first two arguments, "self" and "_cmd",
8320 in the process. EXPR is NULL or an expression that needs to be
8321 evaluated for the side effects of array size expressions in the
8322 parameters. */
8324 static void
8325 start_method_def (tree method, tree expr)
8327 tree parmlist;
8328 #ifdef OBJCPLUS
8329 tree parm_info;
8330 #else
8331 struct c_arg_info *parm_info;
8332 #endif
8333 int have_ellipsis = 0;
8335 /* If we are defining a "dealloc" method in a non-root class, we
8336 will need to check if a [super dealloc] is missing, and warn if
8337 it is. */
8338 if(CLASS_SUPER_NAME (objc_implementation_context)
8339 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method))))
8340 should_call_super_dealloc = 1;
8341 else
8342 should_call_super_dealloc = 0;
8344 /* Required to implement _msgSuper. */
8345 objc_method_context = method;
8346 UOBJC_SUPER_decl = NULL_TREE;
8348 /* Generate prototype declarations for arguments..."new-style". */
8349 synth_self_and_ucmd_args ();
8351 /* Generate argument declarations if a keyword_decl. */
8352 parmlist = METHOD_SEL_ARGS (method);
8353 while (parmlist)
8355 /* parmlist is a KEYWORD_DECL. */
8356 tree type = TREE_VALUE (TREE_TYPE (parmlist));
8357 tree parm;
8359 parm = build_decl (input_location,
8360 PARM_DECL, KEYWORD_ARG_NAME (parmlist), type);
8361 decl_attributes (&parm, DECL_ATTRIBUTES (parmlist), 0);
8362 objc_push_parm (parm);
8363 parmlist = DECL_CHAIN (parmlist);
8366 if (METHOD_ADD_ARGS (method))
8368 tree akey;
8370 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method));
8371 akey; akey = TREE_CHAIN (akey))
8373 objc_push_parm (TREE_VALUE (akey));
8376 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
8377 have_ellipsis = 1;
8380 parm_info = objc_get_parm_info (have_ellipsis, expr);
8382 really_start_method (objc_method_context, parm_info);
8385 /* Return 1 if TYPE1 is equivalent to TYPE2 for purposes of method
8386 overloading. */
8387 static int
8388 objc_types_are_equivalent (tree type1, tree type2)
8390 if (type1 == type2)
8391 return 1;
8393 /* Strip away indirections. */
8394 while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE)
8395 && (TREE_CODE (type1) == TREE_CODE (type2)))
8396 type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2);
8397 if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
8398 return 0;
8400 /* Compare the protocol lists. */
8401 type1 = (TYPE_HAS_OBJC_INFO (type1)
8402 ? TYPE_OBJC_PROTOCOL_LIST (type1)
8403 : NULL_TREE);
8404 type2 = (TYPE_HAS_OBJC_INFO (type2)
8405 ? TYPE_OBJC_PROTOCOL_LIST (type2)
8406 : NULL_TREE);
8408 /* If there are no protocols (most common case), the types are
8409 identical. */
8410 if (type1 == NULL_TREE && type2 == NULL_TREE)
8411 return 1;
8413 /* If one has protocols, and the other one hasn't, they are not
8414 identical. */
8415 if ((type1 == NULL_TREE && type2 != NULL_TREE)
8416 || (type1 != NULL_TREE && type2 == NULL_TREE))
8417 return 0;
8418 else
8420 /* Else, both have protocols, and we need to do the full
8421 comparison. It is possible that either type1 or type2
8422 contain some duplicate protocols in the list, so we can't
8423 even just compare list_length as a first check. */
8424 tree t;
8426 for (t = type2; t; t = TREE_CHAIN (t))
8427 if (!lookup_protocol_in_reflist (type1, TREE_VALUE (t)))
8428 return 0;
8430 for (t = type1; t; t = TREE_CHAIN (t))
8431 if (!lookup_protocol_in_reflist (type2, TREE_VALUE (t)))
8432 return 0;
8434 return 1;
8438 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
8440 static int
8441 objc_types_share_size_and_alignment (tree type1, tree type2)
8443 return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
8444 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2));
8447 /* Return 1 if PROTO1 is equivalent to PROTO2
8448 for purposes of method overloading. Ordinarily, the type signatures
8449 should match up exactly, unless STRICT is zero, in which case we
8450 shall allow differences in which the size and alignment of a type
8451 is the same. */
8453 static int
8454 comp_proto_with_proto (tree proto1, tree proto2, int strict)
8456 tree type1, type2;
8458 /* The following test is needed in case there are hashing
8459 collisions. */
8460 if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2))
8461 return 0;
8463 /* Compare return types. */
8464 type1 = TREE_VALUE (TREE_TYPE (proto1));
8465 type2 = TREE_VALUE (TREE_TYPE (proto2));
8467 if (!objc_types_are_equivalent (type1, type2)
8468 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8469 return 0;
8471 /* Compare argument types. */
8473 /* The first argument (objc_object_type) is always the same, no need
8474 to compare. */
8476 /* The second argument (objc_selector_type) is always the same, no
8477 need to compare. */
8479 /* Compare the other arguments. */
8481 tree arg1, arg2;
8483 /* Compare METHOD_SEL_ARGS. */
8484 for (arg1 = METHOD_SEL_ARGS (proto1), arg2 = METHOD_SEL_ARGS (proto2);
8485 arg1 && arg2;
8486 arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2))
8488 type1 = TREE_VALUE (TREE_TYPE (arg1));
8489 type2 = TREE_VALUE (TREE_TYPE (arg2));
8491 /* FIXME: Do we need to decay argument types to compare them ? */
8492 type1 = objc_decay_parm_type (type1);
8493 type2 = objc_decay_parm_type (type2);
8495 if (!objc_types_are_equivalent (type1, type2)
8496 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8497 return 0;
8500 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8501 both NULL. */
8502 if (arg1 != arg2)
8503 return 0;
8505 /* Compare METHOD_ADD_ARGS. */
8506 if ((METHOD_ADD_ARGS (proto1) && !METHOD_ADD_ARGS (proto2))
8507 || (METHOD_ADD_ARGS (proto2) && !METHOD_ADD_ARGS (proto1)))
8508 return 0;
8510 if (METHOD_ADD_ARGS (proto1))
8512 for (arg1 = TREE_CHAIN (METHOD_ADD_ARGS (proto1)), arg2 = TREE_CHAIN (METHOD_ADD_ARGS (proto2));
8513 arg1 && arg2;
8514 arg1 = TREE_CHAIN (arg1), arg2 = TREE_CHAIN (arg2))
8516 type1 = TREE_TYPE (TREE_VALUE (arg1));
8517 type2 = TREE_TYPE (TREE_VALUE (arg2));
8519 /* FIXME: Do we need to decay argument types to compare them ? */
8520 type1 = objc_decay_parm_type (type1);
8521 type2 = objc_decay_parm_type (type2);
8523 if (!objc_types_are_equivalent (type1, type2)
8524 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8525 return 0;
8529 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8530 both NULL. */
8531 if (arg1 != arg2)
8532 return 0;
8534 /* Compare METHOD_ADD_ARGS_ELLIPSIS_P. */
8535 if (METHOD_ADD_ARGS_ELLIPSIS_P (proto1) != METHOD_ADD_ARGS_ELLIPSIS_P (proto2))
8536 return 0;
8539 /* Success. */
8540 return 1;
8543 /* This routine returns true if TYPE is a valid objc object type,
8544 suitable for messaging; false otherwise. If 'accept_class' is
8545 'true', then a Class object is considered valid for messaging and
8546 'true' is returned if 'type' refers to a Class. If 'accept_class'
8547 is 'false', then a Class object is not considered valid for
8548 messaging and 'false' is returned in that case. */
8550 static bool
8551 objc_type_valid_for_messaging (tree type, bool accept_classes)
8553 if (!POINTER_TYPE_P (type))
8554 return false;
8556 /* Remove the pointer indirection; don't remove more than one
8557 otherwise we'd consider "NSObject **" a valid type for messaging,
8558 which it isn't. */
8559 type = TREE_TYPE (type);
8561 if (TREE_CODE (type) != RECORD_TYPE)
8562 return false;
8564 if (objc_is_object_id (type))
8565 return true;
8567 if (objc_is_class_id (type))
8568 return accept_classes;
8570 if (TYPE_HAS_OBJC_INFO (type))
8571 return true;
8573 return false;
8576 void
8577 objc_start_function (tree name, tree type, tree attrs,
8578 #ifdef OBJCPLUS
8579 tree params
8580 #else
8581 struct c_arg_info *params
8582 #endif
8585 tree fndecl = build_decl (input_location,
8586 FUNCTION_DECL, name, type);
8588 #ifdef OBJCPLUS
8589 DECL_ARGUMENTS (fndecl) = params;
8590 DECL_INITIAL (fndecl) = error_mark_node;
8591 DECL_EXTERNAL (fndecl) = 0;
8592 TREE_STATIC (fndecl) = 1;
8593 retrofit_lang_decl (fndecl);
8594 cplus_decl_attributes (&fndecl, attrs, 0);
8595 start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT);
8596 #else
8597 current_function_returns_value = 0; /* Assume, until we see it does. */
8598 current_function_returns_null = 0;
8599 decl_attributes (&fndecl, attrs, 0);
8600 announce_function (fndecl);
8601 DECL_INITIAL (fndecl) = error_mark_node;
8602 DECL_EXTERNAL (fndecl) = 0;
8603 TREE_STATIC (fndecl) = 1;
8604 current_function_decl = pushdecl (fndecl);
8605 push_scope ();
8606 declare_parm_level ();
8607 DECL_RESULT (current_function_decl)
8608 = build_decl (input_location,
8609 RESULT_DECL, NULL_TREE,
8610 TREE_TYPE (TREE_TYPE (current_function_decl)));
8611 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1;
8612 DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1;
8613 start_fname_decls ();
8614 store_parm_decls_from (params);
8615 #endif
8617 TREE_USED (current_function_decl) = 1;
8620 /* - Generate an identifier for the function. the format is "_n_cls",
8621 where 1 <= n <= nMethods, and cls is the name the implementation we
8622 are processing.
8623 - Install the return type from the method declaration.
8624 - If we have a prototype, check for type consistency. */
8626 static void
8627 really_start_method (tree method,
8628 #ifdef OBJCPLUS
8629 tree parmlist
8630 #else
8631 struct c_arg_info *parmlist
8632 #endif
8635 tree ret_type, meth_type;
8636 tree method_id;
8637 const char *sel_name, *class_name, *cat_name;
8638 char *buf;
8640 /* Synth the storage class & assemble the return type. */
8641 ret_type = TREE_VALUE (TREE_TYPE (method));
8643 sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
8644 class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
8645 cat_name = ((TREE_CODE (objc_implementation_context)
8646 == CLASS_IMPLEMENTATION_TYPE)
8647 ? NULL
8648 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context)));
8649 method_slot++;
8651 /* Make sure this is big enough for any plausible method label. */
8652 buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
8653 + (cat_name ? strlen (cat_name) : 0));
8655 OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
8656 class_name, cat_name, sel_name, method_slot);
8658 method_id = get_identifier (buf);
8660 #ifdef OBJCPLUS
8661 /* Objective-C methods cannot be overloaded, so we don't need
8662 the type encoding appended. It looks bad anyway... */
8663 push_lang_context (lang_name_c);
8664 #endif
8666 meth_type = build_function_type_for_method (ret_type, method, METHOD_DEF, 0);
8667 objc_start_function (method_id, meth_type, NULL_TREE, parmlist);
8669 /* Set self_decl from the first argument. */
8670 self_decl = DECL_ARGUMENTS (current_function_decl);
8672 /* Suppress unused warnings. */
8673 TREE_USED (self_decl) = 1;
8674 DECL_READ_P (self_decl) = 1;
8675 TREE_USED (DECL_CHAIN (self_decl)) = 1;
8676 DECL_READ_P (DECL_CHAIN (self_decl)) = 1;
8677 #ifdef OBJCPLUS
8678 pop_lang_context ();
8679 #endif
8681 METHOD_DEFINITION (method) = current_function_decl;
8683 /* Check consistency...start_function, pushdecl, duplicate_decls. */
8685 if (implementation_template != objc_implementation_context)
8687 tree proto
8688 = lookup_method_static (implementation_template,
8689 METHOD_SEL_NAME (method),
8690 ((TREE_CODE (method) == CLASS_METHOD_DECL)
8691 | OBJC_LOOKUP_NO_SUPER));
8693 if (proto)
8695 if (!comp_proto_with_proto (method, proto, 1))
8697 bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL;
8699 warning_at (DECL_SOURCE_LOCATION (method), 0,
8700 "conflicting types for %<%c%s%>",
8701 (type ? '-' : '+'),
8702 identifier_to_locale (gen_method_decl (method)));
8703 inform (DECL_SOURCE_LOCATION (proto),
8704 "previous declaration of %<%c%s%>",
8705 (type ? '-' : '+'),
8706 identifier_to_locale (gen_method_decl (proto)));
8708 else
8710 /* If the method in the @interface was deprecated, mark
8711 the implemented method as deprecated too. It should
8712 never be used for messaging (when the deprecation
8713 warnings are produced), but just in case. */
8714 if (TREE_DEPRECATED (proto))
8715 TREE_DEPRECATED (method) = 1;
8717 /* If the method in the @interface was marked as
8718 'noreturn', mark the function implementing the method
8719 as 'noreturn' too. */
8720 TREE_THIS_VOLATILE (current_function_decl) = TREE_THIS_VOLATILE (proto);
8723 else
8725 /* We have a method @implementation even though we did not
8726 see a corresponding @interface declaration (which is allowed
8727 by Objective-C rules). Go ahead and place the method in
8728 the @interface anyway, so that message dispatch lookups
8729 will see it. */
8730 tree interface = implementation_template;
8732 if (TREE_CODE (objc_implementation_context)
8733 == CATEGORY_IMPLEMENTATION_TYPE)
8734 interface = lookup_category
8735 (interface,
8736 CLASS_SUPER_NAME (objc_implementation_context));
8738 if (interface)
8739 objc_add_method (interface, copy_node (method),
8740 TREE_CODE (method) == CLASS_METHOD_DECL,
8741 /* is_optional= */ false);
8746 static void *UOBJC_SUPER_scope = 0;
8748 /* _n_Method (id self, SEL sel, ...)
8750 struct objc_super _S;
8751 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
8752 } */
8754 static tree
8755 get_super_receiver (void)
8757 if (objc_method_context)
8759 tree super_expr, super_expr_list, class_expr;
8760 bool inst_meth;
8761 if (!UOBJC_SUPER_decl)
8763 UOBJC_SUPER_decl = build_decl (input_location,
8764 VAR_DECL, get_identifier (TAG_SUPER),
8765 objc_super_template);
8766 /* This prevents `unused variable' warnings when compiling with -Wall. */
8767 TREE_USED (UOBJC_SUPER_decl) = 1;
8768 DECL_READ_P (UOBJC_SUPER_decl) = 1;
8769 lang_hooks.decls.pushdecl (UOBJC_SUPER_decl);
8770 finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE,
8771 NULL_TREE);
8772 UOBJC_SUPER_scope = objc_get_current_scope ();
8775 /* Set receiver to self. */
8776 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id);
8777 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8778 NOP_EXPR, input_location, self_decl,
8779 NULL_TREE);
8780 super_expr_list = super_expr;
8782 /* Set class to begin searching. */
8783 /* Get the ident for the superclass class field & build a ref to it.
8784 ??? maybe we should just name the field the same for all runtimes. */
8785 super_expr = (*runtime.super_superclassfield_ident) ();
8786 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, super_expr);
8788 gcc_assert (imp_list->imp_context == objc_implementation_context
8789 && imp_list->imp_template == implementation_template);
8790 inst_meth = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL);
8792 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
8793 class_expr = (*runtime.get_class_super_ref) (input_location,
8794 imp_list, inst_meth);
8795 else
8796 /* We have a category. */
8798 tree super_name = CLASS_SUPER_NAME (imp_list->imp_template);
8799 tree super_class;
8801 /* Barf if super used in a category of a root object. */
8802 if (!super_name)
8804 error ("no super class declared in interface for %qE",
8805 CLASS_NAME (imp_list->imp_template));
8806 return error_mark_node;
8809 super_class = (*runtime.get_category_super_ref) (input_location,
8810 imp_list, inst_meth);
8811 class_expr = build_c_cast (input_location,
8812 TREE_TYPE (super_expr), super_class);
8815 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8816 NOP_EXPR,
8817 input_location, class_expr, NULL_TREE);
8819 super_expr_list = build_compound_expr (input_location,
8820 super_expr_list, super_expr);
8822 super_expr = build_unary_op (input_location,
8823 ADDR_EXPR, UOBJC_SUPER_decl, 0);
8824 super_expr_list = build_compound_expr (input_location,
8825 super_expr_list, super_expr);
8827 return super_expr_list;
8829 else
8831 error ("[super ...] must appear in a method context");
8832 return error_mark_node;
8836 /* When exiting a scope, sever links to a 'super' declaration (if any)
8837 therein contained. */
8839 void
8840 objc_clear_super_receiver (void)
8842 if (objc_method_context
8843 && UOBJC_SUPER_scope == objc_get_current_scope ())
8845 UOBJC_SUPER_decl = 0;
8846 UOBJC_SUPER_scope = 0;
8850 void
8851 objc_finish_method_definition (tree fndecl)
8853 /* We cannot validly inline ObjC methods, at least not without a language
8854 extension to declare that a method need not be dynamically
8855 dispatched, so suppress all thoughts of doing so. */
8856 DECL_UNINLINABLE (fndecl) = 1;
8858 #ifndef OBJCPLUS
8859 /* The C++ front-end will have called finish_function() for us. */
8860 finish_function ();
8861 #endif
8863 METHOD_ENCODING (objc_method_context)
8864 = encode_method_prototype (objc_method_context);
8866 /* Required to implement _msgSuper. This must be done AFTER finish_function,
8867 since the optimizer may find "may be used before set" errors. */
8868 objc_method_context = NULL_TREE;
8870 if (should_call_super_dealloc)
8871 warning (0, "method possibly missing a [super dealloc] call");
8874 /* Given a tree DECL node, produce a printable description of it in the given
8875 buffer, overwriting the buffer. */
8877 static char *
8878 gen_declaration (tree decl)
8880 errbuf[0] = '\0';
8882 if (DECL_P (decl))
8884 gen_type_name_0 (TREE_TYPE (decl));
8886 if (DECL_NAME (decl))
8888 if (!POINTER_TYPE_P (TREE_TYPE (decl)))
8889 strcat (errbuf, " ");
8891 strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
8894 if (DECL_INITIAL (decl)
8895 && TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST)
8896 sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
8897 TREE_INT_CST_LOW (DECL_INITIAL (decl)));
8900 return errbuf;
8903 /* Given a tree TYPE node, produce a printable description of it in the given
8904 buffer, overwriting the buffer. */
8906 static char *
8907 gen_type_name_0 (tree type)
8909 tree orig = type, proto;
8911 if (TYPE_P (type) && TYPE_NAME (type))
8912 type = TYPE_NAME (type);
8913 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
8915 tree inner = TREE_TYPE (type);
8917 while (TREE_CODE (inner) == ARRAY_TYPE)
8918 inner = TREE_TYPE (inner);
8920 gen_type_name_0 (inner);
8922 if (!POINTER_TYPE_P (inner))
8923 strcat (errbuf, " ");
8925 if (POINTER_TYPE_P (type))
8926 strcat (errbuf, "*");
8927 else
8928 while (type != inner)
8930 strcat (errbuf, "[");
8932 if (TYPE_DOMAIN (type))
8934 char sz[20];
8936 sprintf (sz, HOST_WIDE_INT_PRINT_DEC,
8937 (TREE_INT_CST_LOW
8938 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1));
8939 strcat (errbuf, sz);
8942 strcat (errbuf, "]");
8943 type = TREE_TYPE (type);
8946 goto exit_function;
8949 if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type))
8950 type = DECL_NAME (type);
8952 strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE
8953 ? IDENTIFIER_POINTER (type)
8954 : "");
8956 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
8957 if (objc_is_id (orig))
8958 orig = TREE_TYPE (orig);
8960 proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE;
8962 if (proto)
8964 strcat (errbuf, " <");
8966 while (proto) {
8967 strcat (errbuf,
8968 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto))));
8969 proto = TREE_CHAIN (proto);
8970 strcat (errbuf, proto ? ", " : ">");
8974 exit_function:
8975 return errbuf;
8978 static char *
8979 gen_type_name (tree type)
8981 errbuf[0] = '\0';
8983 return gen_type_name_0 (type);
8986 /* Given a method tree, put a printable description into the given
8987 buffer (overwriting) and return a pointer to the buffer. */
8989 static char *
8990 gen_method_decl (tree method)
8992 tree chain;
8994 strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */
8995 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method)));
8996 strcat (errbuf, ")");
8997 chain = METHOD_SEL_ARGS (method);
8999 if (chain)
9001 /* We have a chain of keyword_decls. */
9004 if (KEYWORD_KEY_NAME (chain))
9005 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
9007 strcat (errbuf, ":(");
9008 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain)));
9009 strcat (errbuf, ")");
9011 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
9012 if ((chain = DECL_CHAIN (chain)))
9013 strcat (errbuf, " ");
9015 while (chain);
9017 if (METHOD_ADD_ARGS (method))
9019 chain = TREE_CHAIN (METHOD_ADD_ARGS (method));
9021 /* Know we have a chain of parm_decls. */
9022 while (chain)
9024 strcat (errbuf, ", ");
9025 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain)));
9026 chain = TREE_CHAIN (chain);
9029 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
9030 strcat (errbuf, ", ...");
9034 else
9035 /* We have a unary selector. */
9036 strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
9038 return errbuf;
9041 /* Debug info. */
9044 /* Dump an @interface declaration of the supplied class CHAIN to the
9045 supplied file FP. Used to implement the -gen-decls option (which
9046 prints out an @interface declaration of all classes compiled in
9047 this run); potentially useful for debugging the compiler too. */
9048 void
9049 dump_interface (FILE *fp, tree chain)
9051 /* FIXME: A heap overflow here whenever a method (or ivar)
9052 declaration is so long that it doesn't fit in the buffer. The
9053 code and all the related functions should be rewritten to avoid
9054 using fixed size buffers. */
9055 const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
9056 tree ivar_decls = CLASS_RAW_IVARS (chain);
9057 tree nst_methods = CLASS_NST_METHODS (chain);
9058 tree cls_methods = CLASS_CLS_METHODS (chain);
9060 fprintf (fp, "\n@interface %s", my_name);
9062 /* CLASS_SUPER_NAME is used to store the superclass name for
9063 classes, and the category name for categories. */
9064 if (CLASS_SUPER_NAME (chain))
9066 const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
9068 switch (TREE_CODE (chain))
9070 case CATEGORY_IMPLEMENTATION_TYPE:
9071 case CATEGORY_INTERFACE_TYPE:
9072 fprintf (fp, " (%s)\n", name);
9073 break;
9074 default:
9075 fprintf (fp, " : %s\n", name);
9076 break;
9079 else
9080 fprintf (fp, "\n");
9082 /* FIXME - the following doesn't seem to work at the moment. */
9083 if (ivar_decls)
9085 fprintf (fp, "{\n");
9088 fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls));
9089 ivar_decls = TREE_CHAIN (ivar_decls);
9091 while (ivar_decls);
9092 fprintf (fp, "}\n");
9095 while (nst_methods)
9097 fprintf (fp, "- %s;\n", gen_method_decl (nst_methods));
9098 nst_methods = TREE_CHAIN (nst_methods);
9101 while (cls_methods)
9103 fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods));
9104 cls_methods = TREE_CHAIN (cls_methods);
9107 fprintf (fp, "@end\n");
9110 #if 0
9111 /* Produce the pretty printing for an Objective-C method. This is
9112 currently unused, but could be handy while reorganizing the pretty
9113 printing to be more robust. */
9114 static const char *
9115 objc_pretty_print_method (bool is_class_method,
9116 const char *class_name,
9117 const char *category_name,
9118 const char *selector)
9120 if (category_name)
9122 char *result = XNEWVEC (char, strlen (class_name) + strlen (category_name)
9123 + strlen (selector) + 7);
9125 if (is_class_method)
9126 sprintf (result, "+[%s(%s) %s]", class_name, category_name, selector);
9127 else
9128 sprintf (result, "-[%s(%s) %s]", class_name, category_name, selector);
9130 return result;
9132 else
9134 char *result = XNEWVEC (char, strlen (class_name)
9135 + strlen (selector) + 5);
9137 if (is_class_method)
9138 sprintf (result, "+[%s %s]", class_name, selector);
9139 else
9140 sprintf (result, "-[%s %s]", class_name, selector);
9142 return result;
9145 #endif
9147 /* Demangle function for Objective-C. Attempt to demangle the
9148 function name associated with a method (eg, going from
9149 "_i_NSObject__class" to "-[NSObject class]"); usually for the
9150 purpose of pretty printing or error messages. Return the demangled
9151 name, or NULL if the string is not an Objective-C mangled method
9152 name.
9154 Because of how the mangling is done, any method that has a '_' in
9155 its original name is at risk of being demangled incorrectly. In
9156 some cases there are multiple valid ways to demangle a method name
9157 and there is no way we can decide.
9159 TODO: objc_demangle() can't always get it right; the right way to
9160 get this correct for all method names would be to store the
9161 Objective-C method name somewhere in the function decl. Then,
9162 there is no demangling to do; we'd just pull the method name out of
9163 the decl. As an additional bonus, when printing error messages we
9164 could check for such a method name, and if we find it, we know the
9165 function is actually an Objective-C method and we could print error
9166 messages saying "In method '+[NSObject class]" instead of "In
9167 function '+[NSObject class]" as we do now. */
9168 static const char *
9169 objc_demangle (const char *mangled)
9171 char *demangled, *cp;
9173 /* First of all, if the name is too short it can't be an Objective-C
9174 mangled method name. */
9175 if (mangled[0] == '\0' || mangled[1] == '\0' || mangled[2] == '\0')
9176 return NULL;
9178 /* If the name looks like an already demangled one, return it
9179 unchanged. This should only happen on Darwin, where method names
9180 are mangled differently into a pretty-print form (such as
9181 '+[NSObject class]', see darwin.h). In that case, demangling is
9182 a no-op, but we need to return the demangled name if it was an
9183 ObjC one, and return NULL if not. We should be safe as no C/C++
9184 function can start with "-[" or "+[". */
9185 if ((mangled[0] == '-' || mangled[0] == '+')
9186 && (mangled[1] == '['))
9187 return mangled;
9189 if (mangled[0] == '_' &&
9190 (mangled[1] == 'i' || mangled[1] == 'c') &&
9191 mangled[2] == '_')
9193 cp = demangled = XNEWVEC (char, strlen(mangled) + 2);
9194 if (mangled[1] == 'i')
9195 *cp++ = '-'; /* for instance method */
9196 else
9197 *cp++ = '+'; /* for class method */
9198 *cp++ = '['; /* opening left brace */
9199 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
9200 while (*cp && *cp == '_')
9201 cp++; /* skip any initial underbars in class name */
9202 cp = strchr(cp, '_'); /* find first non-initial underbar */
9203 if (cp == NULL)
9205 free(demangled); /* not mangled name */
9206 return NULL;
9208 if (cp[1] == '_') /* easy case: no category name */
9210 *cp++ = ' '; /* replace two '_' with one ' ' */
9211 strcpy(cp, mangled + (cp - demangled) + 2);
9213 else
9215 *cp++ = '('; /* less easy case: category name */
9216 cp = strchr(cp, '_');
9217 if (cp == 0)
9219 free(demangled); /* not mangled name */
9220 return NULL;
9222 *cp++ = ')';
9223 *cp++ = ' '; /* overwriting 1st char of method name... */
9224 strcpy(cp, mangled + (cp - demangled)); /* get it back */
9226 /* Now we have the method name. We need to generally replace
9227 '_' with ':' but trying to preserve '_' if it could only have
9228 been in the mangled string because it was already in the
9229 original name. In cases where it's ambiguous, we assume that
9230 any '_' originated from a ':'. */
9232 /* Initial '_'s in method name can't have been generating by
9233 converting ':'s. Skip them. */
9234 while (*cp && *cp == '_')
9235 cp++;
9237 /* If the method name does not end with '_', then it has no
9238 arguments and there was no replacement of ':'s with '_'s
9239 during mangling. Check for that case, and skip any
9240 replacement if so. This at least guarantees that methods
9241 with no arguments are always demangled correctly (unless the
9242 original name ends with '_'). */
9243 if (*(mangled + strlen (mangled) - 1) != '_')
9245 /* Skip to the end. */
9246 for (; *cp; cp++)
9249 else
9251 /* Replace remaining '_' with ':'. This may get it wrong if
9252 there were '_'s in the original name. In most cases it
9253 is impossible to disambiguate. */
9254 for (; *cp; cp++)
9255 if (*cp == '_')
9256 *cp = ':';
9258 *cp++ = ']'; /* closing right brace */
9259 *cp++ = 0; /* string terminator */
9260 return demangled;
9262 else
9263 return NULL; /* not an objc mangled name */
9266 /* Try to pretty-print a decl. If the 'decl' is an Objective-C
9267 specific decl, return the printable name for it. If not, return
9268 NULL. */
9269 const char *
9270 objc_maybe_printable_name (tree decl, int v ATTRIBUTE_UNUSED)
9272 switch (TREE_CODE (decl))
9274 case FUNCTION_DECL:
9275 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)));
9276 break;
9278 /* The following happens when we are printing a deprecation
9279 warning for a method. The warn_deprecation() will end up
9280 trying to print the decl for INSTANCE_METHOD_DECL or
9281 CLASS_METHOD_DECL. It would be nice to be able to print
9282 "-[NSObject autorelease] is deprecated", but to do that, we'd
9283 need to store the class and method name in the method decl,
9284 which we currently don't do. For now, just return the name
9285 of the method. We don't return NULL, because that may
9286 trigger further attempts to pretty-print the decl in C/C++,
9287 but they wouldn't know how to pretty-print it. */
9288 case INSTANCE_METHOD_DECL:
9289 case CLASS_METHOD_DECL:
9290 return IDENTIFIER_POINTER (DECL_NAME (decl));
9291 break;
9292 /* This happens when printing a deprecation warning for a
9293 property. We may want to consider some sort of pretty
9294 printing (eg, include the class name where it was declared
9295 ?). */
9296 case PROPERTY_DECL:
9297 return IDENTIFIER_POINTER (PROPERTY_NAME (decl));
9298 break;
9299 default:
9300 return NULL;
9301 break;
9305 /* Return a printable name for 'decl'. This first tries
9306 objc_maybe_printable_name(), and if that fails, it returns the name
9307 in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for
9308 Objective-C; in Objective-C++, setting the hook is not enough
9309 because lots of C++ Front-End code calls cxx_printable_name,
9310 dump_decl and other C++ functions directly. So instead we have
9311 modified dump_decl to call objc_maybe_printable_name directly. */
9312 const char *
9313 objc_printable_name (tree decl, int v)
9315 const char *demangled_name = objc_maybe_printable_name (decl, v);
9317 if (demangled_name != NULL)
9318 return demangled_name;
9319 else
9320 return IDENTIFIER_POINTER (DECL_NAME (decl));
9323 /* Routine is called to issue diagnostic when reference to a private
9324 ivar is made and no other variable with same name is found in
9325 current scope. */
9326 bool
9327 objc_diagnose_private_ivar (tree id)
9329 tree ivar;
9330 if (!objc_method_context)
9331 return false;
9332 ivar = is_ivar (objc_ivar_chain, id);
9333 if (ivar && is_private (ivar))
9335 error ("instance variable %qs is declared private",
9336 IDENTIFIER_POINTER (id));
9337 return true;
9339 return false;
9342 /* Look up ID as an instance variable. OTHER contains the result of
9343 the C or C++ lookup, which we may want to use instead. */
9344 /* To use properties inside an instance method, use self.property. */
9345 tree
9346 objc_lookup_ivar (tree other, tree id)
9348 tree ivar;
9350 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
9351 if (!objc_method_context)
9352 return other;
9354 if (!strcmp (IDENTIFIER_POINTER (id), "super"))
9355 /* We have a message to super. */
9356 return get_super_receiver ();
9358 /* In a class method, look up an instance variable only as a last
9359 resort. */
9360 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
9361 && other && other != error_mark_node)
9362 return other;
9364 /* Look up the ivar, but do not use it if it is not accessible. */
9365 ivar = is_ivar (objc_ivar_chain, id);
9367 if (!ivar || is_private (ivar))
9368 return other;
9370 /* In an instance method, a local variable (or parameter) may hide the
9371 instance variable. */
9372 if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
9373 && other && other != error_mark_node
9374 #ifdef OBJCPLUS
9375 && CP_DECL_CONTEXT (other) != global_namespace)
9376 #else
9377 && !DECL_FILE_SCOPE_P (other))
9378 #endif
9380 warning (0, "local declaration of %qE hides instance variable", id);
9382 return other;
9385 /* At this point, we are either in an instance method with no obscuring
9386 local definitions, or in a class method with no alternate definitions
9387 at all. */
9388 return build_ivar_reference (id);
9391 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
9392 needs to be done if we are calling a function through a cast. */
9394 tree
9395 objc_rewrite_function_call (tree function, tree first_param)
9397 if (TREE_CODE (function) == NOP_EXPR
9398 && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR
9399 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0))
9400 == FUNCTION_DECL)
9402 function = build3 (OBJ_TYPE_REF, TREE_TYPE (function),
9403 TREE_OPERAND (function, 0),
9404 first_param, size_zero_node);
9407 return function;
9410 /* This is called to "gimplify" a PROPERTY_REF node. It builds the
9411 corresponding 'getter' function call. Note that we assume the
9412 PROPERTY_REF to be valid since we generated it while parsing. */
9413 static void
9414 objc_gimplify_property_ref (tree *expr_p)
9416 tree getter = PROPERTY_REF_GETTER_CALL (*expr_p);
9417 tree call_exp;
9419 if (getter == NULL_TREE)
9421 tree property_decl = PROPERTY_REF_PROPERTY_DECL (*expr_p);
9422 /* This can happen if DECL_ARTIFICIAL (*expr_p), but
9423 should be impossible for real properties, which always
9424 have a getter. */
9425 error_at (EXPR_LOCATION (*expr_p), "no %qs getter found",
9426 IDENTIFIER_POINTER (PROPERTY_NAME (property_decl)));
9427 /* Try to recover from the error to prevent an ICE. We take
9428 zero and cast it to the type of the property. */
9429 *expr_p = convert (TREE_TYPE (property_decl),
9430 integer_zero_node);
9431 return;
9434 if (PROPERTY_REF_DEPRECATED_GETTER (*expr_p))
9436 /* PROPERTY_REF_DEPRECATED_GETTER contains the method prototype
9437 that is deprecated. */
9438 warn_deprecated_use (PROPERTY_REF_DEPRECATED_GETTER (*expr_p),
9439 NULL_TREE);
9442 call_exp = getter;
9443 #ifdef OBJCPLUS
9444 /* In C++, a getter which returns an aggregate value results in a
9445 target_expr which initializes a temporary to the call
9446 expression. */
9447 if (TREE_CODE (getter) == TARGET_EXPR)
9449 gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter)));
9450 gcc_assert (TREE_CODE (TREE_OPERAND (getter, 0)) == VAR_DECL);
9451 call_exp = TREE_OPERAND (getter, 1);
9453 #endif
9454 gcc_assert (TREE_CODE (call_exp) == CALL_EXPR);
9456 *expr_p = call_exp;
9459 /* This is called when "gimplifying" the trees. We need to gimplify
9460 the Objective-C/Objective-C++ specific trees, then hand over the
9461 process to C/C++. */
9463 objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9465 enum tree_code code = TREE_CODE (*expr_p);
9466 switch (code)
9468 /* Look for the special case of OBJC_TYPE_REF with the address
9469 of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend
9470 or one of its cousins). */
9471 case OBJ_TYPE_REF:
9472 if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR
9473 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0))
9474 == FUNCTION_DECL)
9476 enum gimplify_status r0, r1;
9478 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
9479 value of the OBJ_TYPE_REF, so force them to be emitted
9480 during subexpression evaluation rather than after the
9481 OBJ_TYPE_REF. This permits objc_msgSend calls in
9482 Objective C to use direct rather than indirect calls when
9483 the object expression has a postincrement. */
9484 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL,
9485 is_gimple_val, fb_rvalue);
9486 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
9487 is_gimple_val, fb_rvalue);
9489 return MIN (r0, r1);
9491 break;
9492 case PROPERTY_REF:
9493 objc_gimplify_property_ref (expr_p);
9494 /* Do not return yet; let C/C++ gimplify the resulting expression. */
9495 break;
9496 default:
9497 break;
9500 #ifdef OBJCPLUS
9501 return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p);
9502 #else
9503 return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
9504 #endif
9507 /* --- FAST ENUMERATION --- */
9508 /* Begin code generation for fast enumeration (foreach) ... */
9510 /* Defines
9512 struct __objcFastEnumerationState
9514 unsigned long state;
9515 id *itemsPtr;
9516 unsigned long *mutationsPtr;
9517 unsigned long extra[5];
9520 Confusingly enough, NSFastEnumeration is then defined by libraries
9521 to be the same structure.
9524 static void
9525 build_fast_enumeration_state_template (void)
9527 tree decls, *chain = NULL;
9529 /* { */
9530 objc_fast_enumeration_state_template = objc_start_struct (get_identifier
9531 (TAG_FAST_ENUMERATION_STATE));
9533 /* unsigned long state; */
9534 decls = add_field_decl (long_unsigned_type_node, "state", &chain);
9536 /* id *itemsPtr; */
9537 add_field_decl (build_pointer_type (objc_object_type),
9538 "itemsPtr", &chain);
9540 /* unsigned long *mutationsPtr; */
9541 add_field_decl (build_pointer_type (long_unsigned_type_node),
9542 "mutationsPtr", &chain);
9544 /* unsigned long extra[5]; */
9545 add_field_decl (build_sized_array_type (long_unsigned_type_node, 5),
9546 "extra", &chain);
9548 /* } */
9549 objc_finish_struct (objc_fast_enumeration_state_template, decls);
9553 'objc_finish_foreach_loop()' generates the code for an Objective-C
9554 foreach loop. The 'location' argument is the location of the 'for'
9555 that starts the loop. The 'object_expression' is the expression of
9556 the 'object' that iterates; the 'collection_expression' is the
9557 expression of the collection that we iterate over (we need to make
9558 sure we evaluate this only once); the 'for_body' is the set of
9559 statements to be executed in each iteration; 'break_label' and
9560 'continue_label' are the break and continue labels which we need to
9561 emit since the <statements> may be jumping to 'break_label' (if they
9562 contain 'break') or to 'continue_label' (if they contain
9563 'continue').
9565 The syntax is
9567 for (<object expression> in <collection expression>)
9568 <statements>
9570 which is compiled into the following blurb:
9573 id __objc_foreach_collection;
9574 __objc_fast_enumeration_state __objc_foreach_enum_state;
9575 unsigned long __objc_foreach_batchsize;
9576 id __objc_foreach_items[16];
9577 __objc_foreach_collection = <collection expression>;
9578 __objc_foreach_enum_state = { 0 };
9579 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9581 if (__objc_foreach_batchsize == 0)
9582 <object expression> = nil;
9583 else
9585 unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr;
9586 next_batch:
9588 unsigned long __objc_foreach_index;
9589 __objc_foreach_index = 0;
9591 next_object:
9592 if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>);
9593 <object expression> = enumState.itemsPtr[__objc_foreach_index];
9594 <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label]
9596 continue_label:
9597 __objc_foreach_index++;
9598 if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object;
9599 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9601 if (__objc_foreach_batchsize != 0) goto next_batch;
9602 <object expression> = nil;
9603 break_label:
9607 'statements' may contain a 'continue' or 'break' instruction, which
9608 the user expects to 'continue' or 'break' the entire foreach loop.
9609 We are provided the labels that 'break' and 'continue' jump to, so
9610 we place them where we want them to jump to when they pick them.
9612 Optimization TODO: we could cache the IMP of
9613 countByEnumeratingWithState:objects:count:.
9616 /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */
9617 /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */
9619 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
9620 #include "tree-pretty-print.h"
9621 #endif
9623 void
9624 objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body,
9625 tree break_label, tree continue_label)
9627 /* A tree representing the __objcFastEnumerationState struct type,
9628 or NSFastEnumerationState struct, whatever we are using. */
9629 tree objc_fast_enumeration_state_type;
9631 /* The trees representing the declarations of each of the local variables. */
9632 tree objc_foreach_collection_decl;
9633 tree objc_foreach_enum_state_decl;
9634 tree objc_foreach_items_decl;
9635 tree objc_foreach_batchsize_decl;
9636 tree objc_foreach_mutations_pointer_decl;
9637 tree objc_foreach_index_decl;
9639 /* A tree representing the selector countByEnumeratingWithState:objects:count:. */
9640 tree selector_name;
9642 /* A tree representing the local bind. */
9643 tree bind;
9645 /* A tree representing the external 'if (__objc_foreach_batchsize)' */
9646 tree first_if;
9648 /* A tree representing the 'else' part of 'first_if' */
9649 tree first_else;
9651 /* A tree representing the 'next_batch' label. */
9652 tree next_batch_label_decl;
9654 /* A tree representing the binding after the 'next_batch' label. */
9655 tree next_batch_bind;
9657 /* A tree representing the 'next_object' label. */
9658 tree next_object_label_decl;
9660 /* Temporary variables. */
9661 tree t;
9662 int i;
9664 if (flag_objc1_only)
9665 error_at (location, "fast enumeration is not available in Objective-C 1.0");
9667 if (object_expression == error_mark_node)
9668 return;
9670 if (collection_expression == error_mark_node)
9671 return;
9673 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression), true))
9675 error_at (location, "iterating variable in fast enumeration is not an object");
9676 return;
9679 if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression), true))
9681 error_at (location, "collection in fast enumeration is not an object");
9682 return;
9685 /* TODO: Check that object_expression is either a variable
9686 declaration, or an lvalue. */
9688 /* This kludge is an idea from apple. We use the
9689 __objcFastEnumerationState struct implicitly defined by the
9690 compiler, unless a NSFastEnumerationState struct has been defined
9691 (by a Foundation library such as GNUstep Base) in which case, we
9692 use that one.
9694 objc_fast_enumeration_state_type = objc_fast_enumeration_state_template;
9696 tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState"));
9698 if (objc_NSFastEnumeration_type)
9700 /* TODO: We really need to check that
9701 objc_NSFastEnumeration_type is the same as ours! */
9702 if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL)
9704 /* If it's a typedef, use the original type. */
9705 if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type))
9706 objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type);
9707 else
9708 objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type);
9713 /* { */
9714 /* Done by c-parser.c. */
9716 /* type object; */
9717 /* Done by c-parser.c. */
9719 /* Disable warnings that 'object' is unused. For example the code
9721 for (id object in collection)
9722 i++;
9724 which can be used to count how many objects there are in the
9725 collection is fine and should generate no warnings even if
9726 'object' is technically unused. */
9727 TREE_USED (object_expression) = 1;
9728 if (DECL_P (object_expression))
9729 DECL_READ_P (object_expression) = 1;
9731 /* id __objc_foreach_collection */
9732 objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection");
9734 /* __objcFastEnumerationState __objc_foreach_enum_state; */
9735 objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state");
9736 TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl;
9738 /* id __objc_foreach_items[16]; */
9739 objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items");
9740 TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl;
9742 /* unsigned long __objc_foreach_batchsize; */
9743 objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize");
9744 TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl;
9746 /* Generate the local variable binding. */
9747 bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL);
9748 SET_EXPR_LOCATION (bind, location);
9749 TREE_SIDE_EFFECTS (bind) = 1;
9751 /* __objc_foreach_collection = <collection expression>; */
9752 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression);
9753 SET_EXPR_LOCATION (t, location);
9754 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9755 /* We have used 'collection_expression'. */
9756 mark_exp_read (collection_expression);
9758 /* __objc_foreach_enum_state.state = 0; */
9759 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9760 get_identifier ("state")),
9761 build_int_cst (long_unsigned_type_node, 0));
9762 SET_EXPR_LOCATION (t, location);
9763 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9765 /* __objc_foreach_enum_state.itemsPtr = NULL; */
9766 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9767 get_identifier ("itemsPtr")),
9768 null_pointer_node);
9769 SET_EXPR_LOCATION (t, location);
9770 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9772 /* __objc_foreach_enum_state.mutationsPtr = NULL; */
9773 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9774 get_identifier ("mutationsPtr")),
9775 null_pointer_node);
9776 SET_EXPR_LOCATION (t, location);
9777 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9779 /* __objc_foreach_enum_state.extra[0] = 0; */
9780 /* __objc_foreach_enum_state.extra[1] = 0; */
9781 /* __objc_foreach_enum_state.extra[2] = 0; */
9782 /* __objc_foreach_enum_state.extra[3] = 0; */
9783 /* __objc_foreach_enum_state.extra[4] = 0; */
9784 for (i = 0; i < 5 ; i++)
9786 t = build2 (MODIFY_EXPR, void_type_node,
9787 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9788 get_identifier ("extra")),
9789 build_int_cst (NULL_TREE, i)),
9790 build_int_cst (long_unsigned_type_node, 0));
9791 SET_EXPR_LOCATION (t, location);
9792 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9795 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
9796 selector_name = get_identifier ("countByEnumeratingWithState:objects:count:");
9797 #ifdef OBJCPLUS
9798 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9799 /* Parameters. */
9800 tree_cons /* &__objc_foreach_enum_state */
9801 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9802 tree_cons /* __objc_foreach_items */
9803 (NULL_TREE, objc_foreach_items_decl,
9804 tree_cons /* 16 */
9805 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9806 #else
9807 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
9809 struct c_expr array;
9810 array.value = objc_foreach_items_decl;
9811 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9812 /* Parameters. */
9813 tree_cons /* &__objc_foreach_enum_state */
9814 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9815 tree_cons /* __objc_foreach_items */
9816 (NULL_TREE, default_function_array_conversion (location, array).value,
9817 tree_cons /* 16 */
9818 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9820 #endif
9821 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
9822 convert (long_unsigned_type_node, t));
9823 SET_EXPR_LOCATION (t, location);
9824 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9826 /* if (__objc_foreach_batchsize == 0) */
9827 first_if = build3 (COND_EXPR, void_type_node,
9828 /* Condition. */
9829 c_fully_fold
9830 (c_common_truthvalue_conversion
9831 (location,
9832 build_binary_op (location,
9833 EQ_EXPR,
9834 objc_foreach_batchsize_decl,
9835 build_int_cst (long_unsigned_type_node, 0), 1)),
9836 false, NULL),
9837 /* Then block (we fill it in later). */
9838 NULL_TREE,
9839 /* Else block (we fill it in later). */
9840 NULL_TREE);
9841 SET_EXPR_LOCATION (first_if, location);
9842 append_to_statement_list (first_if, &BIND_EXPR_BODY (bind));
9844 /* then <object expression> = nil; */
9845 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
9846 SET_EXPR_LOCATION (t, location);
9847 COND_EXPR_THEN (first_if) = t;
9849 /* Now we build the 'else' part of the if; once we finish building
9850 it, we attach it to first_if as the 'else' part. */
9852 /* else */
9853 /* { */
9855 /* unsigned long __objc_foreach_mutations_pointer; */
9856 objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer");
9858 /* Generate the local variable binding. */
9859 first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL);
9860 SET_EXPR_LOCATION (first_else, location);
9861 TREE_SIDE_EFFECTS (first_else) = 1;
9863 /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */
9864 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl,
9865 build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9866 get_identifier ("mutationsPtr")),
9867 RO_UNARY_STAR));
9868 SET_EXPR_LOCATION (t, location);
9869 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
9871 /* next_batch: */
9872 next_batch_label_decl = create_artificial_label (location);
9873 t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl);
9874 SET_EXPR_LOCATION (t, location);
9875 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
9877 /* { */
9879 /* unsigned long __objc_foreach_index; */
9880 objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index");
9882 /* Generate the local variable binding. */
9883 next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL);
9884 SET_EXPR_LOCATION (next_batch_bind, location);
9885 TREE_SIDE_EFFECTS (next_batch_bind) = 1;
9886 append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else));
9888 /* __objc_foreach_index = 0; */
9889 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
9890 build_int_cst (long_unsigned_type_node, 0));
9891 SET_EXPR_LOCATION (t, location);
9892 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9894 /* next_object: */
9895 next_object_label_decl = create_artificial_label (location);
9896 t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl);
9897 SET_EXPR_LOCATION (t, location);
9898 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9900 /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */
9901 t = build3 (COND_EXPR, void_type_node,
9902 /* Condition. */
9903 c_fully_fold
9904 (c_common_truthvalue_conversion
9905 (location,
9906 build_binary_op
9907 (location,
9908 NE_EXPR,
9909 objc_foreach_mutations_pointer_decl,
9910 build_indirect_ref (location,
9911 objc_build_component_ref (objc_foreach_enum_state_decl,
9912 get_identifier ("mutationsPtr")),
9913 RO_UNARY_STAR), 1)),
9914 false, NULL),
9915 /* Then block. */
9916 build_function_call (input_location,
9917 objc_enumeration_mutation_decl,
9918 tree_cons (NULL, collection_expression, NULL)),
9919 /* Else block. */
9920 NULL_TREE);
9921 SET_EXPR_LOCATION (t, location);
9922 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9924 /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */
9925 t = build2 (MODIFY_EXPR, void_type_node, object_expression,
9926 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9927 get_identifier ("itemsPtr")),
9928 objc_foreach_index_decl));
9929 SET_EXPR_LOCATION (t, location);
9930 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9932 /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */
9933 append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind));
9935 /* continue_label: */
9936 if (continue_label)
9938 t = build1 (LABEL_EXPR, void_type_node, continue_label);
9939 SET_EXPR_LOCATION (t, location);
9940 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9943 /* __objc_foreach_index++; */
9944 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
9945 build_binary_op (location,
9946 PLUS_EXPR,
9947 objc_foreach_index_decl,
9948 build_int_cst (long_unsigned_type_node, 1), 1));
9949 SET_EXPR_LOCATION (t, location);
9950 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9952 /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */
9953 t = build3 (COND_EXPR, void_type_node,
9954 /* Condition. */
9955 c_fully_fold
9956 (c_common_truthvalue_conversion
9957 (location,
9958 build_binary_op (location,
9959 LT_EXPR,
9960 objc_foreach_index_decl,
9961 objc_foreach_batchsize_decl, 1)),
9962 false, NULL),
9963 /* Then block. */
9964 build1 (GOTO_EXPR, void_type_node, next_object_label_decl),
9965 /* Else block. */
9966 NULL_TREE);
9967 SET_EXPR_LOCATION (t, location);
9968 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9970 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
9971 #ifdef OBJCPLUS
9972 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9973 /* Parameters. */
9974 tree_cons /* &__objc_foreach_enum_state */
9975 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9976 tree_cons /* __objc_foreach_items */
9977 (NULL_TREE, objc_foreach_items_decl,
9978 tree_cons /* 16 */
9979 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9980 #else
9981 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
9983 struct c_expr array;
9984 array.value = objc_foreach_items_decl;
9985 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9986 /* Parameters. */
9987 tree_cons /* &__objc_foreach_enum_state */
9988 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9989 tree_cons /* __objc_foreach_items */
9990 (NULL_TREE, default_function_array_conversion (location, array).value,
9991 tree_cons /* 16 */
9992 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9994 #endif
9995 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
9996 convert (long_unsigned_type_node, t));
9997 SET_EXPR_LOCATION (t, location);
9998 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10000 /* } */
10002 /* if (__objc_foreach_batchsize != 0) goto next_batch; */
10003 t = build3 (COND_EXPR, void_type_node,
10004 /* Condition. */
10005 c_fully_fold
10006 (c_common_truthvalue_conversion
10007 (location,
10008 build_binary_op (location,
10009 NE_EXPR,
10010 objc_foreach_batchsize_decl,
10011 build_int_cst (long_unsigned_type_node, 0), 1)),
10012 false, NULL),
10013 /* Then block. */
10014 build1 (GOTO_EXPR, void_type_node, next_batch_label_decl),
10015 /* Else block. */
10016 NULL_TREE);
10017 SET_EXPR_LOCATION (t, location);
10018 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10020 /* <object expression> = nil; */
10021 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
10022 SET_EXPR_LOCATION (t, location);
10023 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10025 /* break_label: */
10026 if (break_label)
10028 t = build1 (LABEL_EXPR, void_type_node, break_label);
10029 SET_EXPR_LOCATION (t, location);
10030 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10033 /* } */
10034 COND_EXPR_ELSE (first_if) = first_else;
10036 /* Do the whole thing. */
10037 add_stmt (bind);
10039 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
10040 /* This will print to stderr the whole blurb generated by the
10041 compiler while compiling (assuming the compiler doesn't crash
10042 before getting here).
10044 debug_generic_stmt (bind);
10045 #endif
10047 /* } */
10048 /* Done by c-parser.c */
10051 /* --- SUPPORT FOR FORMAT ARG CHECKING --- */
10052 /* Return true if we have an NxString object pointer. */
10054 bool
10055 objc_string_ref_type_p (tree strp)
10057 tree tmv;
10058 if (!strp || TREE_CODE (strp) != POINTER_TYPE)
10059 return false;
10061 tmv = TYPE_MAIN_VARIANT (TREE_TYPE (strp));
10062 tmv = OBJC_TYPE_NAME (tmv);
10063 return (tmv
10064 && TREE_CODE (tmv) == IDENTIFIER_NODE
10065 && IDENTIFIER_POINTER (tmv)
10066 && !strncmp (IDENTIFIER_POINTER (tmv), "NSString", 8));
10069 /* At present the behavior of this is undefined and it does nothing. */
10070 void
10071 objc_check_format_arg (tree ARG_UNUSED (format_arg),
10072 tree ARG_UNUSED (args_list))
10076 void
10077 objc_common_init_ts (void)
10079 c_common_init_ts ();
10081 MARK_TS_DECL_NON_COMMON (CLASS_METHOD_DECL);
10082 MARK_TS_DECL_NON_COMMON (INSTANCE_METHOD_DECL);
10083 MARK_TS_DECL_NON_COMMON (KEYWORD_DECL);
10084 MARK_TS_DECL_NON_COMMON (PROPERTY_DECL);
10086 MARK_TS_COMMON (CLASS_INTERFACE_TYPE);
10087 MARK_TS_COMMON (PROTOCOL_INTERFACE_TYPE);
10088 MARK_TS_COMMON (CLASS_IMPLEMENTATION_TYPE);
10090 MARK_TS_TYPED (MESSAGE_SEND_EXPR);
10091 MARK_TS_TYPED (PROPERTY_REF);
10094 #include "gt-objc-objc-act.h"