Daily bump.
[official-gcc.git] / gcc / objc / objc-act.c
blobdf599810fa91b7e6a1029631073c4d5246142c88
1 /* Implement classes and message passing for Objective C.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Contributed by Steve Naroff.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "stringpool.h"
27 #include "stor-layout.h"
28 #include "attribs.h"
30 #ifdef OBJCPLUS
31 #include "cp/cp-tree.h"
32 #else
33 #include "c/c-tree.h"
34 #include "c/c-lang.h"
35 #endif
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
39 #include "c-family/c-pragma.h"
40 #include "c-family/c-format.h"
41 #include "flags.h"
42 #include "langhooks.h"
43 #include "objc-act.h"
44 #include "objc-map.h"
45 #include "input.h"
46 #include "function.h"
47 #include "toplev.h"
48 #include "debug.h"
49 #include "c-family/c-target.h"
50 #include "diagnostic-core.h"
51 #include "intl.h"
52 #include "cgraph.h"
53 #include "tree-iterator.h"
54 #include "hash-table.h"
55 #include "wide-int.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-expr.h"
67 #include "gimplify.h"
69 /* For encode_method_prototype(). */
70 #include "objc-encoding.h"
72 static unsigned int should_call_super_dealloc = 0;
74 /* When building Objective-C++, we are not linking against the C front-end
75 and so need to replicate the C tree-construction functions in some way. */
76 #ifdef OBJCPLUS
77 #define OBJCP_REMAP_FUNCTIONS
78 #include "objcp-decl.h"
79 #endif /* OBJCPLUS */
81 /* This is the default way of generating a method name. */
82 /* This has the problem that "test_method:argument:" and
83 "test:method_argument:" will generate the same name
84 ("_i_Test__test_method_argument_" for an instance method of the
85 class "Test"), so you can't have them both in the same class!
86 Moreover, the demangling (going from
87 "_i_Test__test_method_argument" back to the original name) is
88 undefined because there are two correct ways of demangling the
89 name. */
90 #ifndef OBJC_GEN_METHOD_LABEL
91 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
92 do { \
93 char *temp; \
94 sprintf ((BUF), "_%s_%s_%s_%s", \
95 ((IS_INST) ? "i" : "c"), \
96 (CLASS_NAME), \
97 ((CAT_NAME)? (CAT_NAME) : ""), \
98 (SEL_NAME)); \
99 for (temp = (BUF); *temp; temp++) \
100 if (*temp == ':') *temp = '_'; \
101 } while (0)
102 #endif
104 /* These need specifying. */
105 #ifndef OBJC_FORWARDING_STACK_OFFSET
106 #define OBJC_FORWARDING_STACK_OFFSET 0
107 #endif
109 #ifndef OBJC_FORWARDING_MIN_OFFSET
110 #define OBJC_FORWARDING_MIN_OFFSET 0
111 #endif
113 /*** Private Interface (procedures) ***/
115 /* Init stuff. */
116 static void synth_module_prologue (void);
118 /* Code generation. */
120 static tree start_class (enum tree_code, tree, tree, tree, tree);
121 static tree continue_class (tree);
122 static void finish_class (tree);
123 static void start_method_def (tree, tree);
125 static tree start_protocol (enum tree_code, tree, tree, tree);
126 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
127 static tree objc_add_method (tree, tree, int, bool);
128 static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree);
129 static tree build_ivar_reference (tree);
130 static tree is_ivar (tree, tree);
132 /* We only need the following for ObjC; ObjC++ will use C++'s definition
133 of DERIVED_FROM_P. */
134 #ifndef OBJCPLUS
135 static bool objc_derived_from_p (tree, tree);
136 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
137 #endif
139 /* Property. */
140 static void objc_gen_property_data (tree, tree);
141 static void objc_synthesize_getter (tree, tree, tree);
142 static void objc_synthesize_setter (tree, tree, tree);
143 static tree lookup_property (tree, tree);
144 static tree lookup_property_in_list (tree, tree);
145 static tree lookup_property_in_protocol_list (tree, tree);
146 static void build_common_objc_property_accessor_helpers (void);
148 static void objc_xref_basetypes (tree, tree);
150 static tree get_class_ivars (tree, bool);
152 static void build_fast_enumeration_state_template (void);
154 #ifdef OBJCPLUS
155 static void objc_generate_cxx_cdtors (void);
156 #endif
158 /* objc attribute */
159 static void objc_decl_method_attributes (tree*, tree, int);
160 static tree build_keyword_selector (tree);
162 static void hash_init (void);
164 /* Hash tables to manage the global pool of method prototypes. Each
165 of these maps map a method name (selector) identifier to either a
166 single tree (for methods with a single method prototype) or a
167 TREE_VEC (for methods with multiple method prototypes). */
168 static GTY(()) objc_map_t instance_method_map = 0;
169 static GTY(()) objc_map_t class_method_map = 0;
171 /* Hash tables to manage the global pool of class names. */
173 static GTY(()) objc_map_t class_name_map = 0;
174 static GTY(()) objc_map_t alias_name_map = 0;
176 static tree lookup_method (tree, tree);
177 static tree lookup_method_static (tree, tree, int);
179 static void interface_hash_init (void);
180 static tree add_interface (tree, tree);
181 static void add_category (tree, tree);
182 static inline tree lookup_category (tree, tree);
184 /* Protocols. */
186 static tree lookup_protocol (tree, bool, bool);
187 static tree lookup_and_install_protocols (tree, bool);
189 #ifdef OBJCPLUS
190 static void really_start_method (tree, tree);
191 #else
192 static void really_start_method (tree, struct c_arg_info *);
193 #endif
194 static int comp_proto_with_proto (tree, tree, int);
195 static tree objc_decay_parm_type (tree);
197 /* Utilities for debugging and error diagnostics. */
199 static char *gen_type_name (tree);
200 static char *gen_type_name_0 (tree);
201 static char *gen_method_decl (tree);
202 static char *gen_declaration (tree);
204 /* Everything else. */
206 static void generate_struct_by_value_array (void) ATTRIBUTE_NORETURN;
208 static void mark_referenced_methods (void);
209 static bool objc_type_valid_for_messaging (tree type, bool allow_classes);
210 static tree check_duplicates (tree, int, int);
212 /*** Private Interface (data) ***/
213 /* Flags for lookup_method_static(). */
215 /* Look for class methods. */
216 #define OBJC_LOOKUP_CLASS 1
217 /* Do not examine superclasses. */
218 #define OBJC_LOOKUP_NO_SUPER 2
219 /* Disable returning an instance method of a root class when a class
220 method can't be found. */
221 #define OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS 4
223 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
224 tree objc_global_trees[OCTI_MAX];
226 struct imp_entry *imp_list = 0;
227 int imp_count = 0; /* `@implementation' */
228 int cat_count = 0; /* `@category' */
230 objc_ivar_visibility_kind objc_ivar_visibility, objc_default_ivar_visibility;
232 /* Use to generate method labels. */
233 static int method_slot = 0;
235 /* Flag to say whether methods in a protocol are optional or
236 required. */
237 static bool objc_method_optional_flag = false;
239 static int objc_collecting_ivars = 0;
241 /* Flag that is set to 'true' while we are processing a class
242 extension. Since a class extension just "reopens" the main
243 @interface, this can be used to determine if we are in the main
244 @interface, or in a class extension. */
245 static bool objc_in_class_extension = false;
247 static char *errbuf; /* Buffer for error diagnostics */
249 /* An array of all the local variables in the current function that
250 need to be marked as volatile. */
251 vec<tree, va_gc> *local_variables_to_volatilize = NULL;
253 /* Store all constructed constant strings in a hash table so that
254 they get uniqued properly. */
256 struct GTY(()) string_descriptor {
257 /* The literal argument . */
258 tree literal;
260 /* The resulting constant string. */
261 tree constructor;
264 static GTY((param_is (struct string_descriptor))) htab_t string_htab;
266 FILE *gen_declaration_file;
268 /* Hooks for stuff that differs between runtimes. */
269 objc_runtime_hooks runtime;
271 /* Create a temporary variable of type 'type'. If 'name' is set, uses
272 the specified name, else use no name. Returns the declaration of
273 the type. The 'name' is mostly useful for debugging.
275 tree
276 objc_create_temporary_var (tree type, const char *name)
278 tree decl;
280 if (name != NULL)
282 decl = build_decl (input_location,
283 VAR_DECL, get_identifier (name), type);
285 else
287 decl = build_decl (input_location,
288 VAR_DECL, NULL_TREE, type);
290 TREE_USED (decl) = 1;
291 DECL_ARTIFICIAL (decl) = 1;
292 DECL_IGNORED_P (decl) = 1;
293 DECL_CONTEXT (decl) = current_function_decl;
295 return decl;
298 /* Some platforms pass small structures through registers versus
299 through an invisible pointer. Determine at what size structure is
300 the transition point between the two possibilities. */
302 static void
303 generate_struct_by_value_array (void)
305 tree type;
306 tree decls;
307 int i, j;
308 int aggregate_in_mem[32];
309 int found = 0;
311 /* Presumably no platform passes 32 byte structures in a register. */
312 /* ??? As an example, m64/ppc/Darwin can pass up to 8*long+13*double
313 in registers. */
314 for (i = 1; i < 32; i++)
316 char buffer[5];
317 tree *chain = NULL;
319 /* Create an unnamed struct that has `i' character components */
320 type = objc_start_struct (NULL_TREE);
322 strcpy (buffer, "c1");
323 decls = add_field_decl (char_type_node, buffer, &chain);
325 for (j = 1; j < i; j++)
327 sprintf (buffer, "c%d", j + 1);
328 add_field_decl (char_type_node, buffer, &chain);
330 objc_finish_struct (type, decls);
332 aggregate_in_mem[i] = aggregate_value_p (type, 0);
333 if (!aggregate_in_mem[i])
334 found = 1;
337 /* We found some structures that are returned in registers instead of memory
338 so output the necessary data. */
339 if (found)
341 for (i = 31; i >= 0; i--)
342 if (!aggregate_in_mem[i])
343 break;
344 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n", i);
347 exit (0);
350 bool
351 objc_init (void)
353 bool ok;
354 #ifdef OBJCPLUS
355 if (cxx_init () == false)
356 #else
357 if (c_objc_common_init () == false)
358 #endif
359 return false;
361 /* print_struct_values is triggered by -print-runtime-info (used
362 when building libobjc, with an empty file as input). It does not
363 require any ObjC setup, and it never returns.
365 -fcompare-debug is used to check the compiler output; we are
366 executed twice, once with flag_compare_debug set, and once with
367 it not set. If the flag is used together with
368 -print-runtime-info, we want to print the runtime info only once,
369 else it would be output in duplicate. So we check
370 flag_compare_debug to output it in only one of the invocations.
372 As a side effect, this also that means -fcompare-debug
373 -print-runtime-info will run the compiler twice, and compare the
374 generated assembler file; the first time the compiler exits
375 immediately (producing no file), and the second time it compiles
376 an empty file. This checks, as a side effect, that compiling an
377 empty file produces no assembler output. */
378 if (print_struct_values && !flag_compare_debug)
379 generate_struct_by_value_array ();
381 /* Set up stuff used by FE parser and all runtimes. */
382 errbuf = XNEWVEC (char, 1024 * 10);
383 interface_hash_init ();
384 hash_init ();
385 objc_encoding_init ();
386 /* ... and then check flags and set-up for the selected runtime ... */
387 if (flag_next_runtime && flag_objc_abi >= 2)
388 ok = objc_next_runtime_abi_02_init (&runtime);
389 else if (flag_next_runtime)
390 ok = objc_next_runtime_abi_01_init (&runtime);
391 else
392 ok = objc_gnu_runtime_abi_01_init (&runtime);
394 /* If that part of the setup failed - bail out immediately. */
395 if (!ok)
396 return false;
398 /* Determine the default visibility for instance variables. */
399 switch (default_ivar_visibility)
401 case IVAR_VISIBILITY_PRIVATE:
402 objc_default_ivar_visibility = OBJC_IVAR_VIS_PRIVATE;
403 break;
404 case IVAR_VISIBILITY_PUBLIC:
405 objc_default_ivar_visibility = OBJC_IVAR_VIS_PUBLIC;
406 break;
407 case IVAR_VISIBILITY_PACKAGE:
408 objc_default_ivar_visibility = OBJC_IVAR_VIS_PACKAGE;
409 break;
410 default:
411 objc_default_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
414 /* Generate general types and push runtime-specific decls to file scope. */
415 synth_module_prologue ();
417 return true;
420 /* This is called automatically (at the very end of compilation) by
421 c_write_global_declarations and cp_write_global_declarations. */
422 void
423 objc_write_global_declarations (void)
425 mark_referenced_methods ();
427 /* A missing @end might not be detected by the parser. */
428 if (objc_implementation_context)
430 warning (0, "%<@end%> missing in implementation context");
431 finish_class (objc_implementation_context);
432 objc_ivar_chain = NULL_TREE;
433 objc_implementation_context = NULL_TREE;
436 if (warn_selector)
438 objc_map_iterator_t i;
440 objc_map_iterator_initialize (class_method_map, &i);
441 while (objc_map_iterator_move_to_next (class_method_map, &i))
442 check_duplicates (objc_map_iterator_current_value (class_method_map, i), 0, 1);
444 objc_map_iterator_initialize (instance_method_map, &i);
445 while (objc_map_iterator_move_to_next (instance_method_map, &i))
446 check_duplicates (objc_map_iterator_current_value (instance_method_map, i), 0, 0);
449 /* TODO: consider an early exit here if either errorcount or sorrycount
450 is non-zero. Not only is it wasting time to generate the metadata,
451 it needlessly imposes need to re-check for things that are already
452 determined to be errors. */
454 /* Finalize Objective-C runtime data. No need to generate tables
455 and code if only checking syntax, or if generating a PCH file. */
456 if (!flag_syntax_only && !pch_file)
458 location_t saved_location;
460 /* If gen_declaration desired, open the output file. */
461 if (flag_gen_declaration)
463 char * const dumpname = concat (dump_base_name, ".decl", NULL);
464 gen_declaration_file = fopen (dumpname, "w");
465 if (gen_declaration_file == 0)
466 fatal_error ("can%'t open %s: %m", dumpname);
467 free (dumpname);
470 /* Set the input location to BUILTINS_LOCATION. This is good
471 for error messages, in case any is generated while producing
472 the metadata, but it also silences warnings that would be
473 produced when compiling with -Wpadded in case when padding is
474 automatically added to the built-in runtime data structure
475 declarations. We know about this padding, and it is fine; we
476 don't want users to see any warnings about it if they use
477 -Wpadded. */
478 saved_location = input_location;
479 input_location = BUILTINS_LOCATION;
481 /* Compute and emit the meta-data tables for this runtime. */
482 (*runtime.generate_metadata) ();
484 /* Restore the original location, just in case it mattered. */
485 input_location = saved_location;
487 /* ... and then close any declaration file we opened. */
488 if (gen_declaration_file)
489 fclose (gen_declaration_file);
493 /* Return the first occurrence of a method declaration corresponding
494 to sel_name in rproto_list. Search rproto_list recursively.
495 If is_class is 0, search for instance methods, otherwise for class
496 methods. */
497 static tree
498 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
499 int is_class)
501 tree rproto, p, m;
503 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
505 p = TREE_VALUE (rproto);
506 m = NULL_TREE;
508 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
510 /* First, search the @required protocol methods. */
511 if (is_class)
512 m = lookup_method (PROTOCOL_CLS_METHODS (p), sel_name);
513 else
514 m = lookup_method (PROTOCOL_NST_METHODS (p), sel_name);
516 if (m)
517 return m;
519 /* If still not found, search the @optional protocol methods. */
520 if (is_class)
521 m = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (p), sel_name);
522 else
523 m = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (p), sel_name);
525 if (m)
526 return m;
528 /* If still not found, search the attached protocols. */
529 if (PROTOCOL_LIST (p))
530 m = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
531 sel_name, is_class);
532 if (m)
533 return m;
535 else
537 ; /* An identifier...if we could not find a protocol. */
541 return 0;
544 static tree
545 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
547 tree rproto, p;
549 /* Make sure the protocol is supported by the object on the rhs. */
550 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
552 tree fnd = 0;
553 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
555 p = TREE_VALUE (rproto);
557 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
559 if (lproto == p)
560 fnd = lproto;
562 else if (PROTOCOL_LIST (p))
563 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
566 if (fnd)
567 return fnd;
570 else
572 ; /* An identifier...if we could not find a protocol. */
575 return 0;
578 void
579 objc_start_class_interface (tree klass, tree super_class,
580 tree protos, tree attributes)
582 if (flag_objc1_only && attributes)
583 error_at (input_location, "class attributes are not available in Objective-C 1.0");
585 objc_interface_context
586 = objc_ivar_context
587 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos, attributes);
588 objc_ivar_visibility = objc_default_ivar_visibility;
591 void
592 objc_start_category_interface (tree klass, tree categ,
593 tree protos, tree attributes)
595 if (attributes)
597 if (flag_objc1_only)
598 error_at (input_location, "category attributes are not available in Objective-C 1.0");
599 else
600 warning_at (input_location, OPT_Wattributes,
601 "category attributes are not available in this version"
602 " of the compiler, (ignored)");
604 if (categ == NULL_TREE)
606 if (flag_objc1_only)
607 error_at (input_location, "class extensions are not available in Objective-C 1.0");
608 else
610 /* Iterate over all the classes and categories implemented
611 up to now in this compilation unit. */
612 struct imp_entry *t;
614 for (t = imp_list; t; t = t->next)
616 /* If we find a class @implementation with the same name
617 as the one we are extending, produce an error. */
618 if (TREE_CODE (t->imp_context) == CLASS_IMPLEMENTATION_TYPE
619 && IDENTIFIER_POINTER (CLASS_NAME (t->imp_context)) == IDENTIFIER_POINTER (klass))
620 error_at (input_location,
621 "class extension for class %qE declared after its %<@implementation%>",
622 klass);
626 objc_interface_context
627 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos, NULL_TREE);
628 objc_ivar_chain
629 = continue_class (objc_interface_context);
632 void
633 objc_start_protocol (tree name, tree protos, tree attributes)
635 if (flag_objc1_only && attributes)
636 error_at (input_location, "protocol attributes are not available in Objective-C 1.0");
638 objc_interface_context
639 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos, attributes);
640 objc_method_optional_flag = false;
643 void
644 objc_continue_interface (void)
646 objc_ivar_chain
647 = continue_class (objc_interface_context);
650 void
651 objc_finish_interface (void)
653 finish_class (objc_interface_context);
654 objc_interface_context = NULL_TREE;
655 objc_method_optional_flag = false;
656 objc_in_class_extension = false;
659 void
660 objc_start_class_implementation (tree klass, tree super_class)
662 objc_implementation_context
663 = objc_ivar_context
664 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE,
665 NULL_TREE);
666 objc_ivar_visibility = objc_default_ivar_visibility;
669 void
670 objc_start_category_implementation (tree klass, tree categ)
672 objc_implementation_context
673 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE,
674 NULL_TREE);
675 objc_ivar_chain
676 = continue_class (objc_implementation_context);
679 void
680 objc_continue_implementation (void)
682 objc_ivar_chain
683 = continue_class (objc_implementation_context);
686 void
687 objc_finish_implementation (void)
689 #ifdef OBJCPLUS
690 if (flag_objc_call_cxx_cdtors)
691 objc_generate_cxx_cdtors ();
692 #endif
694 if (objc_implementation_context)
696 finish_class (objc_implementation_context);
697 objc_ivar_chain = NULL_TREE;
698 objc_implementation_context = NULL_TREE;
700 else
701 warning (0, "%<@end%> must appear in an @implementation context");
704 void
705 objc_set_visibility (objc_ivar_visibility_kind visibility)
707 if (visibility == OBJC_IVAR_VIS_PACKAGE)
709 if (flag_objc1_only)
710 error ("%<@package%> is not available in Objective-C 1.0");
711 else
712 warning (0, "%<@package%> presently has the same effect as %<@public%>");
714 objc_ivar_visibility = visibility;
717 void
718 objc_set_method_opt (bool optional)
720 if (flag_objc1_only)
722 if (optional)
723 error_at (input_location, "%<@optional%> is not available in Objective-C 1.0");
724 else
725 error_at (input_location, "%<@required%> is not available in Objective-C 1.0");
728 objc_method_optional_flag = optional;
729 if (!objc_interface_context
730 || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
732 if (optional)
733 error ("%<@optional%> is allowed in @protocol context only");
734 else
735 error ("%<@required%> is allowed in @protocol context only");
736 objc_method_optional_flag = false;
740 /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
741 PROTOCOL. */
742 static tree
743 lookup_property_in_list (tree chain, tree property)
745 tree x;
746 for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x))
747 if (PROPERTY_NAME (x) == property)
748 return x;
749 return NULL_TREE;
752 /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
753 static tree lookup_property_in_protocol_list (tree rproto_list, tree property)
755 tree rproto, x;
756 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
758 tree p = TREE_VALUE (rproto);
759 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
761 if ((x = lookup_property_in_list (p, property)))
762 return x;
763 if (PROTOCOL_LIST (p))
764 return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property);
766 else
768 ; /* An identifier...if we could not find a protocol. */
771 return NULL_TREE;
774 /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
775 chain of interface hierarchy. */
776 static tree
777 lookup_property (tree interface_type, tree property)
779 tree inter = interface_type;
780 while (inter)
782 tree x, category;
783 if ((x = lookup_property_in_list (inter, property)))
784 return x;
785 /* Failing that, look for the property in each category of the class. */
786 category = inter;
787 while ((category = CLASS_CATEGORY_LIST (category)))
789 if ((x = lookup_property_in_list (category, property)))
790 return x;
792 /* When checking a category, also check the protocols
793 attached with the category itself. */
794 if (CLASS_PROTOCOL_LIST (category)
795 && (x = lookup_property_in_protocol_list
796 (CLASS_PROTOCOL_LIST (category), property)))
797 return x;
800 /* Failing to find in categories, look for property in protocol list. */
801 if (CLASS_PROTOCOL_LIST (inter)
802 && (x = lookup_property_in_protocol_list
803 (CLASS_PROTOCOL_LIST (inter), property)))
804 return x;
806 /* Failing that, climb up the inheritance hierarchy. */
807 inter = lookup_interface (CLASS_SUPER_NAME (inter));
809 return inter;
812 /* This routine is called by the parser when a
813 @property... declaration is found. 'decl' is the declaration of
814 the property (type/identifier), and the other arguments represent
815 property attributes that may have been specified in the Objective-C
816 declaration. 'parsed_property_readonly' is 'true' if the attribute
817 'readonly' was specified, and 'false' if not; similarly for the
818 other bool parameters. 'parsed_property_getter_ident' is NULL_TREE
819 if the attribute 'getter' was not specified, and is the identifier
820 corresponding to the specified getter if it was; similarly for
821 'parsed_property_setter_ident'. */
822 void
823 objc_add_property_declaration (location_t location, tree decl,
824 bool parsed_property_readonly, bool parsed_property_readwrite,
825 bool parsed_property_assign, bool parsed_property_retain,
826 bool parsed_property_copy, bool parsed_property_nonatomic,
827 tree parsed_property_getter_ident, tree parsed_property_setter_ident)
829 tree property_decl;
830 tree x;
831 /* 'property_readonly' and 'property_assign_semantics' are the final
832 attributes of the property after all parsed attributes have been
833 considered (eg, if we parsed no 'readonly' and no 'readwrite', ie
834 parsed_property_readonly = false and parsed_property_readwrite =
835 false, then property_readonly will be false because the default
836 is readwrite). */
837 bool property_readonly = false;
838 objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN;
839 bool property_extension_in_class_extension = false;
841 if (flag_objc1_only)
842 error_at (input_location, "%<@property%> is not available in Objective-C 1.0");
844 if (parsed_property_readonly && parsed_property_readwrite)
846 error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute");
847 /* In case of conflicting attributes (here and below), after
848 producing an error, we pick one of the attributes and keep
849 going. */
850 property_readonly = false;
852 else
854 if (parsed_property_readonly)
855 property_readonly = true;
857 if (parsed_property_readwrite)
858 property_readonly = false;
861 if (parsed_property_readonly && parsed_property_setter_ident)
863 error_at (location, "%<readonly%> attribute conflicts with %<setter%> attribute");
864 property_readonly = false;
867 if (parsed_property_assign && parsed_property_retain)
869 error_at (location, "%<assign%> attribute conflicts with %<retain%> attribute");
870 property_assign_semantics = OBJC_PROPERTY_RETAIN;
872 else if (parsed_property_assign && parsed_property_copy)
874 error_at (location, "%<assign%> attribute conflicts with %<copy%> attribute");
875 property_assign_semantics = OBJC_PROPERTY_COPY;
877 else if (parsed_property_retain && parsed_property_copy)
879 error_at (location, "%<retain%> attribute conflicts with %<copy%> attribute");
880 property_assign_semantics = OBJC_PROPERTY_COPY;
882 else
884 if (parsed_property_assign)
885 property_assign_semantics = OBJC_PROPERTY_ASSIGN;
887 if (parsed_property_retain)
888 property_assign_semantics = OBJC_PROPERTY_RETAIN;
890 if (parsed_property_copy)
891 property_assign_semantics = OBJC_PROPERTY_COPY;
894 if (!objc_interface_context)
896 error_at (location, "property declaration not in @interface or @protocol context");
897 return;
900 /* At this point we know that we are either in an interface, a
901 category, or a protocol. */
903 /* We expect a FIELD_DECL from the parser. Make sure we didn't get
904 something else, as that would confuse the checks below. */
905 if (TREE_CODE (decl) != FIELD_DECL)
907 error_at (location, "invalid property declaration");
908 return;
911 /* Do some spot-checks for the most obvious invalid types. */
913 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
915 error_at (location, "property can not be an array");
916 return;
919 /* The C++/ObjC++ parser seems to reject the ':' for a bitfield when
920 parsing, while the C/ObjC parser accepts it and gives us a
921 FIELD_DECL with a DECL_INITIAL set. So we use the DECL_INITIAL
922 to check for a bitfield when doing ObjC. */
923 #ifndef OBJCPLUS
924 if (DECL_INITIAL (decl))
926 /* A @property is not an actual variable, but it is a way to
927 describe a pair of accessor methods, so its type (which is
928 the type of the return value of the getter and the first
929 argument of the setter) can't be a bitfield (as return values
930 and arguments of functions can not be bitfields). The
931 underlying instance variable could be a bitfield, but that is
932 a different matter. */
933 error_at (location, "property can not be a bit-field");
934 return;
936 #endif
938 /* TODO: Check that the property type is an Objective-C object or a
939 "POD". */
941 /* Implement -Wproperty-assign-default (which is enabled by default). */
942 if (warn_property_assign_default
943 /* If garbage collection is not being used, then 'assign' is
944 valid for objects (and typically used for delegates) but it
945 is wrong in most cases (since most objects need to be
946 retained or copied in setters). Warn users when 'assign' is
947 used implicitly. */
948 && property_assign_semantics == OBJC_PROPERTY_ASSIGN
949 /* Read-only properties are never assigned, so the assignment
950 semantics do not matter in that case. */
951 && !property_readonly
952 && !flag_objc_gc)
954 /* Please note that it would make sense to default to 'assign'
955 for non-{Objective-C objects}, and to 'retain' for
956 Objective-C objects. But that would break compatibility with
957 other compilers. */
958 if (!parsed_property_assign && !parsed_property_retain && !parsed_property_copy)
960 /* Use 'false' so we do not warn for Class objects. */
961 if (objc_type_valid_for_messaging (TREE_TYPE (decl), false))
963 warning_at (location,
965 "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>",
966 decl);
967 inform (location,
968 "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it");
973 if (property_assign_semantics == OBJC_PROPERTY_RETAIN
974 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
975 error_at (location, "%<retain%> attribute is only valid for Objective-C objects");
977 if (property_assign_semantics == OBJC_PROPERTY_COPY
978 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
979 error_at (location, "%<copy%> attribute is only valid for Objective-C objects");
981 /* Now determine the final property getter and setter names. They
982 will be stored in the PROPERTY_DECL, from which they'll always be
983 extracted and used. */
985 /* Adjust, or fill in, setter and getter names. We overwrite the
986 parsed_property_setter_ident and parsed_property_getter_ident
987 with the final setter and getter identifiers that will be
988 used. */
989 if (parsed_property_setter_ident)
991 /* The setter should be terminated by ':', but the parser only
992 gives us an identifier without ':'. So, we need to add ':'
993 at the end. */
994 const char *parsed_setter = IDENTIFIER_POINTER (parsed_property_setter_ident);
995 size_t length = strlen (parsed_setter);
996 char *final_setter = (char *)alloca (length + 2);
998 sprintf (final_setter, "%s:", parsed_setter);
999 parsed_property_setter_ident = get_identifier (final_setter);
1001 else
1003 if (!property_readonly)
1004 parsed_property_setter_ident = get_identifier (objc_build_property_setter_name
1005 (DECL_NAME (decl)));
1008 if (!parsed_property_getter_ident)
1009 parsed_property_getter_ident = DECL_NAME (decl);
1011 /* Check for duplicate property declarations. We first check the
1012 immediate context for a property with the same name. Any such
1013 declarations are an error, unless this is a class extension and
1014 we are extending a property from readonly to readwrite. */
1015 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
1017 if (PROPERTY_NAME (x) == DECL_NAME (decl))
1019 if (objc_in_class_extension
1020 && property_readonly == 0
1021 && PROPERTY_READONLY (x) == 1)
1023 /* This is a class extension, and we are extending an
1024 existing readonly property to a readwrite one.
1025 That's fine. :-) */
1026 property_extension_in_class_extension = true;
1027 break;
1029 else
1031 location_t original_location = DECL_SOURCE_LOCATION (x);
1033 error_at (location, "redeclaration of property %qD", decl);
1035 if (original_location != UNKNOWN_LOCATION)
1036 inform (original_location, "originally specified here");
1037 return;
1042 /* If x is not NULL_TREE, we must be in a class extension and we're
1043 extending a readonly property. In that case, no point in
1044 searching for another declaration. */
1045 if (x == NULL_TREE)
1047 /* We now need to check for existing property declarations (in
1048 the superclass, other categories or protocols) and check that
1049 the new declaration is not in conflict with existing
1050 ones. */
1052 /* Search for a previous, existing declaration of a property
1053 with the same name in superclasses, protocols etc. If one is
1054 found, it will be in the 'x' variable. */
1056 /* Note that, for simplicity, the following may search again the
1057 local context. That's Ok as nothing will be found (else we'd
1058 have thrown an error above); it's only a little inefficient,
1059 but the code is simpler. */
1060 switch (TREE_CODE (objc_interface_context))
1062 case CLASS_INTERFACE_TYPE:
1063 /* Look up the property in the current @interface (which
1064 will find nothing), then its protocols and categories and
1065 superclasses. */
1066 x = lookup_property (objc_interface_context, DECL_NAME (decl));
1067 break;
1068 case CATEGORY_INTERFACE_TYPE:
1069 /* Look up the property in the main @interface, then
1070 protocols and categories (one of them is ours, and will
1071 find nothing) and superclasses. */
1072 x = lookup_property (lookup_interface (CLASS_NAME (objc_interface_context)),
1073 DECL_NAME (decl));
1074 break;
1075 case PROTOCOL_INTERFACE_TYPE:
1076 /* Looks up the property in any protocols attached to the
1077 current protocol. */
1078 if (PROTOCOL_LIST (objc_interface_context))
1080 x = lookup_property_in_protocol_list (PROTOCOL_LIST (objc_interface_context),
1081 DECL_NAME (decl));
1083 break;
1084 default:
1085 gcc_unreachable ();
1089 if (x != NULL_TREE)
1091 /* An existing property was found; check that it has the same
1092 types, or it is compatible. */
1093 location_t original_location = DECL_SOURCE_LOCATION (x);
1095 if (PROPERTY_NONATOMIC (x) != parsed_property_nonatomic)
1097 warning_at (location, 0,
1098 "'nonatomic' attribute of property %qD conflicts with previous declaration", decl);
1100 if (original_location != UNKNOWN_LOCATION)
1101 inform (original_location, "originally specified here");
1102 return;
1105 if (PROPERTY_GETTER_NAME (x) != parsed_property_getter_ident)
1107 warning_at (location, 0,
1108 "'getter' attribute of property %qD conflicts with previous declaration", decl);
1110 if (original_location != UNKNOWN_LOCATION)
1111 inform (original_location, "originally specified here");
1112 return;
1115 /* We can only compare the setter names if both the old and new property have a setter. */
1116 if (!property_readonly && !PROPERTY_READONLY(x))
1118 if (PROPERTY_SETTER_NAME (x) != parsed_property_setter_ident)
1120 warning_at (location, 0,
1121 "'setter' attribute of property %qD conflicts with previous declaration", decl);
1123 if (original_location != UNKNOWN_LOCATION)
1124 inform (original_location, "originally specified here");
1125 return;
1129 if (PROPERTY_ASSIGN_SEMANTICS (x) != property_assign_semantics)
1131 warning_at (location, 0,
1132 "assign semantics attributes of property %qD conflict with previous declaration", decl);
1134 if (original_location != UNKNOWN_LOCATION)
1135 inform (original_location, "originally specified here");
1136 return;
1139 /* It's ok to have a readonly property that becomes a readwrite, but not vice versa. */
1140 if (PROPERTY_READONLY (x) == 0 && property_readonly == 1)
1142 warning_at (location, 0,
1143 "'readonly' attribute of property %qD conflicts with previous declaration", decl);
1145 if (original_location != UNKNOWN_LOCATION)
1146 inform (original_location, "originally specified here");
1147 return;
1150 /* We now check that the new and old property declarations have
1151 the same types (or compatible one). In the Objective-C
1152 tradition of loose type checking, we do type-checking but
1153 only generate warnings (not errors) if they do not match.
1154 For non-readonly properties, the types must match exactly;
1155 for readonly properties, it is allowed to use a "more
1156 specialized" type in the new property declaration. Eg, the
1157 superclass has a getter returning (NSArray *) and the
1158 subclass a getter returning (NSMutableArray *). The object's
1159 getter returns an (NSMutableArray *); but if you cast the
1160 object to the superclass, which is allowed, you'd still
1161 expect the getter to return an (NSArray *), which works since
1162 an (NSMutableArray *) is an (NSArray *) too. So, the set of
1163 objects belonging to the type of the new @property should be
1164 a subset of the set of objects belonging to the type of the
1165 old @property. This is what "specialization" means. And the
1166 reason it only applies to readonly properties is that for a
1167 readwrite property the setter would have the opposite
1168 requirement - ie that the superclass type is more specialized
1169 then the subclass one; hence the only way to satisfy both
1170 constraints is that the types match. */
1172 /* If the types are not the same in the C sense, we warn ... */
1173 if (!comptypes (TREE_TYPE (x), TREE_TYPE (decl))
1174 /* ... unless the property is readonly, in which case we
1175 allow a new, more specialized, declaration. */
1176 && (!property_readonly
1177 || !objc_compare_types (TREE_TYPE (x),
1178 TREE_TYPE (decl), -5, NULL_TREE)))
1180 warning_at (location, 0,
1181 "type of property %qD conflicts with previous declaration", decl);
1182 if (original_location != UNKNOWN_LOCATION)
1183 inform (original_location, "originally specified here");
1184 return;
1187 /* If we are in a class extension and we're extending a readonly
1188 property in the main @interface, we'll just update the
1189 existing property with the readwrite flag and potentially the
1190 new setter name. */
1191 if (property_extension_in_class_extension)
1193 PROPERTY_READONLY (x) = 0;
1194 PROPERTY_SETTER_NAME (x) = parsed_property_setter_ident;
1195 return;
1199 /* Create a PROPERTY_DECL node. */
1200 property_decl = make_node (PROPERTY_DECL);
1202 /* Copy the basic information from the original decl. */
1203 TREE_TYPE (property_decl) = TREE_TYPE (decl);
1204 DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl);
1205 TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl);
1207 /* Add property-specific information. */
1208 PROPERTY_NAME (property_decl) = DECL_NAME (decl);
1209 PROPERTY_GETTER_NAME (property_decl) = parsed_property_getter_ident;
1210 PROPERTY_SETTER_NAME (property_decl) = parsed_property_setter_ident;
1211 PROPERTY_READONLY (property_decl) = property_readonly;
1212 PROPERTY_NONATOMIC (property_decl) = parsed_property_nonatomic;
1213 PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics;
1214 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1215 PROPERTY_DYNAMIC (property_decl) = 0;
1217 /* Remember the fact that the property was found in the @optional
1218 section in a @protocol, or not. */
1219 if (objc_method_optional_flag)
1220 PROPERTY_OPTIONAL (property_decl) = 1;
1221 else
1222 PROPERTY_OPTIONAL (property_decl) = 0;
1224 /* Note that PROPERTY_GETTER_NAME is always set for all
1225 PROPERTY_DECLs, and PROPERTY_SETTER_NAME is always set for all
1226 PROPERTY_DECLs where PROPERTY_READONLY == 0. Any time we deal
1227 with a getter or setter, we should get the PROPERTY_DECL and use
1228 PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know the correct
1229 names. */
1231 /* Add the PROPERTY_DECL to the list of properties for the class. */
1232 TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context);
1233 CLASS_PROPERTY_DECL (objc_interface_context) = property_decl;
1236 /* This is a subroutine of objc_maybe_build_component_ref. Search the
1237 list of methods in the interface (and, failing that, the local list
1238 in the implementation, and failing that, the protocol list)
1239 provided for a 'setter' or 'getter' for 'component' with default
1240 names (ie, if 'component' is "name", then search for "name" and
1241 "setName:"). It is also possible to specify a different
1242 'getter_name' (this is used for @optional readonly properties). If
1243 any is found, then create an artificial property that uses them.
1244 Return NULL_TREE if 'getter' or 'setter' could not be found. */
1245 static tree
1246 maybe_make_artificial_property_decl (tree interface, tree implementation,
1247 tree protocol_list, tree component, bool is_class,
1248 tree getter_name)
1250 tree setter_name = get_identifier (objc_build_property_setter_name (component));
1251 tree getter = NULL_TREE;
1252 tree setter = NULL_TREE;
1254 if (getter_name == NULL_TREE)
1255 getter_name = component;
1257 /* First, check the @interface and all superclasses. */
1258 if (interface)
1260 int flags = 0;
1262 /* Using instance methods of the root class as accessors is most
1263 likely unwanted and can be extremely confusing (and, most
1264 importantly, other Objective-C 2.0 compilers do not do it).
1265 Turn it off. */
1266 if (is_class)
1267 flags = OBJC_LOOKUP_CLASS | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS;
1269 getter = lookup_method_static (interface, getter_name, flags);
1270 setter = lookup_method_static (interface, setter_name, flags);
1273 /* Second, check the local @implementation context. */
1274 if (!getter && !setter)
1276 if (implementation)
1278 if (is_class)
1280 getter = lookup_method (CLASS_CLS_METHODS (implementation), getter_name);
1281 setter = lookup_method (CLASS_CLS_METHODS (implementation), setter_name);
1283 else
1285 getter = lookup_method (CLASS_NST_METHODS (implementation), getter_name);
1286 setter = lookup_method (CLASS_NST_METHODS (implementation), setter_name);
1291 /* Try the protocol_list if we didn't find anything in the
1292 @interface and in the @implementation. */
1293 if (!getter && !setter)
1295 getter = lookup_method_in_protocol_list (protocol_list, getter_name, is_class);
1296 setter = lookup_method_in_protocol_list (protocol_list, setter_name, is_class);
1299 /* There needs to be at least a getter or setter for this to be a
1300 valid 'object.component' syntax. */
1301 if (getter || setter)
1303 /* Yes ... determine the type of the expression. */
1304 tree property_decl;
1305 tree type;
1307 if (getter)
1308 type = TREE_VALUE (TREE_TYPE (getter));
1309 else
1310 type = TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter)));
1312 /* Create an artificial property declaration with the
1313 information we collected on the type and getter/setter
1314 names. */
1315 property_decl = make_node (PROPERTY_DECL);
1317 TREE_TYPE (property_decl) = type;
1318 DECL_SOURCE_LOCATION (property_decl) = input_location;
1319 TREE_DEPRECATED (property_decl) = 0;
1320 DECL_ARTIFICIAL (property_decl) = 1;
1322 /* Add property-specific information. Note that one of
1323 PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a
1324 non-existing method; this will generate an error when the
1325 expression is later compiled. At this stage we don't know if
1326 the getter or setter will be used, so we can't generate an
1327 error. */
1328 PROPERTY_NAME (property_decl) = component;
1329 PROPERTY_GETTER_NAME (property_decl) = getter_name;
1330 PROPERTY_SETTER_NAME (property_decl) = setter_name;
1331 PROPERTY_READONLY (property_decl) = 0;
1332 PROPERTY_NONATOMIC (property_decl) = 0;
1333 PROPERTY_ASSIGN_SEMANTICS (property_decl) = 0;
1334 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1335 PROPERTY_DYNAMIC (property_decl) = 0;
1336 PROPERTY_OPTIONAL (property_decl) = 0;
1338 if (!getter)
1339 PROPERTY_HAS_NO_GETTER (property_decl) = 1;
1341 /* The following is currently unused, but it's nice to have
1342 there. We may use it if we need in the future. */
1343 if (!setter)
1344 PROPERTY_HAS_NO_SETTER (property_decl) = 1;
1346 return property_decl;
1349 return NULL_TREE;
1352 /* This hook routine is invoked by the parser when an expression such
1353 as 'xxx.yyy' is parsed. We get a chance to process these
1354 expressions in a way that is specified to Objective-C (to implement
1355 the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars).
1356 If the expression is not an Objective-C specified expression, we
1357 should return NULL_TREE; else we return the expression.
1359 At the moment this only implements dot-syntax and properties (not
1360 non-fragile ivars yet), ie 'object.property' or 'object.component'
1361 where 'component' is not a declared property, but a valid getter or
1362 setter for it could be found. */
1363 tree
1364 objc_maybe_build_component_ref (tree object, tree property_ident)
1366 tree x = NULL_TREE;
1367 tree rtype;
1369 /* If we are in Objective-C 1.0 mode, dot-syntax and properties are
1370 not available. */
1371 if (flag_objc1_only)
1372 return NULL_TREE;
1374 /* Try to determine if 'object' is an Objective-C object or not. If
1375 not, return. */
1376 if (object == NULL_TREE || object == error_mark_node
1377 || (rtype = TREE_TYPE (object)) == NULL_TREE)
1378 return NULL_TREE;
1380 if (property_ident == NULL_TREE || property_ident == error_mark_node
1381 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1382 return NULL_TREE;
1384 /* The following analysis of 'object' is similar to the one used for
1385 the 'receiver' of a method invocation. We need to determine what
1386 'object' is and find the appropriate property (either declared,
1387 or artificial) for it (in the same way as we need to find the
1388 appropriate method prototype for a method invocation). There are
1389 some simplifications here though: "object.property" is invalid if
1390 "object" has a type of "id" or "Class"; it must at least have a
1391 protocol attached to it, and "object" is never a class name as
1392 that is done by objc_build_class_component_ref. Finally, we
1393 don't know if this really is a dot-syntax expression, so we want
1394 to make a quick exit if it is not; for this reason, we try to
1395 postpone checks after determining that 'object' looks like an
1396 Objective-C object. */
1398 if (objc_is_id (rtype))
1400 /* This is the case that the 'object' is of type 'id' or
1401 'Class'. */
1403 /* Check if at least it is of type 'id <Protocol>' or 'Class
1404 <Protocol>'; if so, look the property up in the
1405 protocols. */
1406 if (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype)))
1408 tree rprotos = TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype));
1410 if (rprotos)
1412 /* No point looking up declared @properties if we are
1413 dealing with a class. Classes have no declared
1414 properties. */
1415 if (!IS_CLASS (rtype))
1416 x = lookup_property_in_protocol_list (rprotos, property_ident);
1418 if (x == NULL_TREE)
1420 /* Ok, no property. Maybe it was an
1421 object.component dot-syntax without a declared
1422 property (this is valid for classes too). Look
1423 for getter/setter methods and internally declare
1424 an artificial property based on them if found. */
1425 x = maybe_make_artificial_property_decl (NULL_TREE,
1426 NULL_TREE,
1427 rprotos,
1428 property_ident,
1429 IS_CLASS (rtype),
1430 NULL_TREE);
1432 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1434 /* This is a special, complicated case. If the
1435 property is optional, and is read-only, then the
1436 property is always used for reading, but an
1437 eventual existing non-property setter can be used
1438 for writing. We create an artificial property
1439 decl copying the getter from the optional
1440 property, and looking up the setter in the
1441 interface. */
1442 x = maybe_make_artificial_property_decl (NULL_TREE,
1443 NULL_TREE,
1444 rprotos,
1445 property_ident,
1446 false,
1447 PROPERTY_GETTER_NAME (x));
1451 else if (objc_method_context)
1453 /* Else, if we are inside a method it could be the case of
1454 'super' or 'self'. */
1455 tree interface_type = NULL_TREE;
1456 tree t = object;
1457 while (TREE_CODE (t) == COMPOUND_EXPR
1458 || TREE_CODE (t) == MODIFY_EXPR
1459 || CONVERT_EXPR_P (t)
1460 || TREE_CODE (t) == COMPONENT_REF)
1461 t = TREE_OPERAND (t, 0);
1463 if (t == UOBJC_SUPER_decl)
1464 interface_type = lookup_interface (CLASS_SUPER_NAME (implementation_template));
1465 else if (t == self_decl)
1466 interface_type = lookup_interface (CLASS_NAME (implementation_template));
1468 if (interface_type)
1470 if (TREE_CODE (objc_method_context) != CLASS_METHOD_DECL)
1471 x = lookup_property (interface_type, property_ident);
1473 if (x == NULL_TREE)
1475 /* Try the dot-syntax without a declared property.
1476 If this is an access to 'self', it is possible
1477 that they may refer to a setter/getter that is
1478 not declared in the interface, but exists locally
1479 in the implementation. In that case, get the
1480 implementation context and use it. */
1481 tree implementation = NULL_TREE;
1483 if (t == self_decl)
1484 implementation = objc_implementation_context;
1486 x = maybe_make_artificial_property_decl
1487 (interface_type, implementation, NULL_TREE,
1488 property_ident,
1489 (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL),
1490 NULL_TREE);
1492 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1494 tree implementation = NULL_TREE;
1496 if (t == self_decl)
1497 implementation = objc_implementation_context;
1499 x = maybe_make_artificial_property_decl (interface_type,
1500 implementation,
1501 NULL_TREE,
1502 property_ident,
1503 false,
1504 PROPERTY_GETTER_NAME (x));
1509 else
1511 /* This is the case where we have more information on 'rtype'. */
1512 tree basetype = TYPE_MAIN_VARIANT (rtype);
1514 /* Skip the pointer - if none, it's not an Objective-C object or
1515 class. */
1516 if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE)
1517 basetype = TREE_TYPE (basetype);
1518 else
1519 return NULL_TREE;
1521 /* Traverse typedefs. */
1522 while (basetype != NULL_TREE
1523 && TREE_CODE (basetype) == RECORD_TYPE
1524 && OBJC_TYPE_NAME (basetype)
1525 && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL
1526 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)))
1527 basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype));
1529 if (basetype != NULL_TREE && TYPED_OBJECT (basetype))
1531 tree interface_type = TYPE_OBJC_INTERFACE (basetype);
1532 tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype);
1534 if (interface_type
1535 && (TREE_CODE (interface_type) == CLASS_INTERFACE_TYPE
1536 || TREE_CODE (interface_type) == CATEGORY_INTERFACE_TYPE
1537 || TREE_CODE (interface_type) == PROTOCOL_INTERFACE_TYPE))
1539 /* Not sure 'rtype' could ever be a class here! Just
1540 for safety we keep the checks. */
1541 if (!IS_CLASS (rtype))
1543 x = lookup_property (interface_type, property_ident);
1545 if (x == NULL_TREE)
1546 x = lookup_property_in_protocol_list (protocol_list,
1547 property_ident);
1550 if (x == NULL_TREE)
1552 /* Try the dot-syntax without a declared property.
1553 If we are inside a method implementation, it is
1554 possible that they may refer to a setter/getter
1555 that is not declared in the interface, but exists
1556 locally in the implementation. In that case, get
1557 the implementation context and use it. */
1558 tree implementation = NULL_TREE;
1560 if (objc_implementation_context
1561 && CLASS_NAME (objc_implementation_context)
1562 == OBJC_TYPE_NAME (interface_type))
1563 implementation = objc_implementation_context;
1565 x = maybe_make_artificial_property_decl (interface_type,
1566 implementation,
1567 protocol_list,
1568 property_ident,
1569 IS_CLASS (rtype),
1570 NULL_TREE);
1572 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1574 tree implementation = NULL_TREE;
1576 if (objc_implementation_context
1577 && CLASS_NAME (objc_implementation_context)
1578 == OBJC_TYPE_NAME (interface_type))
1579 implementation = objc_implementation_context;
1581 x = maybe_make_artificial_property_decl (interface_type,
1582 implementation,
1583 protocol_list,
1584 property_ident,
1585 false,
1586 PROPERTY_GETTER_NAME (x));
1592 if (x)
1594 tree expression;
1595 tree getter_call;
1596 tree deprecated_method_prototype = NULL_TREE;
1598 /* We have an additional nasty problem here; if this
1599 PROPERTY_REF needs to become a 'getter', then the conversion
1600 from PROPERTY_REF into a getter call happens in gimplify,
1601 after the selector table has already been generated and when
1602 it is too late to add another selector to it. To work around
1603 the problem, we always create the getter call at this stage,
1604 which puts the selector in the table. Note that if the
1605 PROPERTY_REF becomes a 'setter' instead of a 'getter', then
1606 we have added a selector too many to the selector table.
1607 This is a little inefficient.
1609 Also note that method calls to 'self' and 'super' require the
1610 context (self_decl, UOBJS_SUPER_decl,
1611 objc_implementation_context etc) to be built correctly; this
1612 is yet another reason why building the call at the gimplify
1613 stage (when this context has been lost) is not very
1614 practical. If we build it at this stage, we know it will
1615 always be built correctly.
1617 If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial
1618 property decl created to deal with a dotsyntax not really
1619 referring to an existing property) then do not try to build a
1620 call to the getter as there is no getter. */
1621 if (PROPERTY_HAS_NO_GETTER (x))
1622 getter_call = NULL_TREE;
1623 else
1624 getter_call = objc_finish_message_expr
1625 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1626 /* Disable the immediate deprecation warning if the getter
1627 is deprecated, but record the fact that the getter is
1628 deprecated by setting PROPERTY_REF_DEPRECATED_GETTER to
1629 the method prototype. */
1630 &deprecated_method_prototype);
1632 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1633 deprecated_method_prototype);
1634 SET_EXPR_LOCATION (expression, input_location);
1635 TREE_SIDE_EFFECTS (expression) = 1;
1637 return expression;
1640 return NULL_TREE;
1643 /* This hook routine is invoked by the parser when an expression such
1644 as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the
1645 Objective-C 2.0 dot-syntax applied to classes, so we need to
1646 convert it into a setter/getter call on the class. */
1647 tree
1648 objc_build_class_component_ref (tree class_name, tree property_ident)
1650 tree x = NULL_TREE;
1651 tree object, rtype;
1653 if (flag_objc1_only)
1654 error_at (input_location, "the dot syntax is not available in Objective-C 1.0");
1656 if (class_name == NULL_TREE || class_name == error_mark_node
1657 || TREE_CODE (class_name) != IDENTIFIER_NODE)
1658 return error_mark_node;
1660 if (property_ident == NULL_TREE || property_ident == error_mark_node
1661 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1662 return NULL_TREE;
1664 object = objc_get_class_reference (class_name);
1665 if (!object)
1667 /* We know that 'class_name' is an Objective-C class name as the
1668 parser won't call this function if it is not. This is only a
1669 double-check for safety. */
1670 error_at (input_location, "could not find class %qE", class_name);
1671 return error_mark_node;
1674 rtype = lookup_interface (class_name);
1675 if (!rtype)
1677 /* Again, this should never happen, but we do check. */
1678 error_at (input_location, "could not find interface for class %qE", class_name);
1679 return error_mark_node;
1681 else
1683 if (TREE_DEPRECATED (rtype))
1684 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", class_name);
1687 x = maybe_make_artificial_property_decl (rtype, NULL_TREE, NULL_TREE,
1688 property_ident,
1689 true, NULL_TREE);
1691 if (x)
1693 tree expression;
1694 tree getter_call;
1695 tree deprecated_method_prototype = NULL_TREE;
1697 if (PROPERTY_HAS_NO_GETTER (x))
1698 getter_call = NULL_TREE;
1699 else
1700 getter_call = objc_finish_message_expr
1701 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1702 &deprecated_method_prototype);
1704 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1705 deprecated_method_prototype);
1706 SET_EXPR_LOCATION (expression, input_location);
1707 TREE_SIDE_EFFECTS (expression) = 1;
1709 return expression;
1711 else
1713 error_at (input_location, "could not find setter/getter for %qE in class %qE",
1714 property_ident, class_name);
1715 return error_mark_node;
1718 return NULL_TREE;
1723 /* This is used because we don't want to expose PROPERTY_REF to the
1724 C/C++ frontends. Maybe we should! */
1725 bool
1726 objc_is_property_ref (tree node)
1728 if (node && TREE_CODE (node) == PROPERTY_REF)
1729 return true;
1730 else
1731 return false;
1734 /* This function builds a setter call for a PROPERTY_REF (real, for a
1735 declared property, or artificial, for a dot-syntax accessor which
1736 is not corresponding to a property). 'lhs' must be a PROPERTY_REF
1737 (the caller must check this beforehand). 'rhs' is the value to
1738 assign to the property. A plain setter call is returned, or
1739 error_mark_node if the property is readonly. */
1741 static tree
1742 objc_build_setter_call (tree lhs, tree rhs)
1744 tree object_expr = PROPERTY_REF_OBJECT (lhs);
1745 tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs);
1747 if (PROPERTY_READONLY (property_decl))
1749 error ("readonly property can not be set");
1750 return error_mark_node;
1752 else
1754 tree setter_argument = build_tree_list (NULL_TREE, rhs);
1755 tree setter;
1757 /* TODO: Check that the setter return type is 'void'. */
1759 /* TODO: Decay arguments in C. */
1760 setter = objc_finish_message_expr (object_expr,
1761 PROPERTY_SETTER_NAME (property_decl),
1762 setter_argument, NULL);
1763 return setter;
1766 /* Unreachable, but the compiler may not realize. */
1767 return error_mark_node;
1770 /* This hook routine is called when a MODIFY_EXPR is being built. We
1771 check what is being modified; if it is a PROPERTY_REF, we need to
1772 generate a 'setter' function call for the property. If this is not
1773 a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go
1774 on creating their MODIFY_EXPR.
1776 This is used for example if you write
1778 object.count = 1;
1780 where 'count' is a property. The left-hand side creates a
1781 PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR
1782 to assign something to it. We intercept that here, and generate a
1783 call to the 'setter' method instead. */
1784 tree
1785 objc_maybe_build_modify_expr (tree lhs, tree rhs)
1787 if (lhs && TREE_CODE (lhs) == PROPERTY_REF)
1789 /* Building a simple call to the setter method would work for cases such as
1791 object.count = 1;
1793 but wouldn't work for cases such as
1795 count = object2.count = 1;
1797 to get these to work with very little effort, we build a
1798 compound statement which does the setter call (to set the
1799 property to 'rhs'), but which can also be evaluated returning
1800 the 'rhs'. If the 'rhs' has no side effects, we can simply
1801 evaluate it twice, building
1803 ([object setProperty: rhs]; rhs)
1805 If it has side effects, we put it in a temporary variable first,
1806 so we create the following:
1808 (temp = rhs; [object setProperty: temp]; temp)
1810 setter_argument is rhs in the first case, and temp in the second
1811 case.
1813 tree setter_argument;
1815 /* s1, s2 and s3 are the tree statements that we need in the
1816 compound expression. */
1817 tree s1, s2, s3, compound_expr;
1819 if (TREE_SIDE_EFFECTS (rhs))
1821 tree bind;
1823 /* Declare __objc_property_temp in a local bind. */
1824 setter_argument = objc_create_temporary_var (TREE_TYPE (rhs), "__objc_property_temp");
1825 DECL_SOURCE_LOCATION (setter_argument) = input_location;
1826 bind = build3 (BIND_EXPR, void_type_node, setter_argument, NULL, NULL);
1827 SET_EXPR_LOCATION (bind, input_location);
1828 TREE_SIDE_EFFECTS (bind) = 1;
1829 add_stmt (bind);
1831 /* s1: x = rhs */
1832 s1 = build_modify_expr (input_location, setter_argument, NULL_TREE,
1833 NOP_EXPR,
1834 input_location, rhs, NULL_TREE);
1835 SET_EXPR_LOCATION (s1, input_location);
1837 else
1839 /* No s1. */
1840 setter_argument = rhs;
1841 s1 = NULL_TREE;
1844 /* Now build the compound statement. */
1846 /* s2: [object setProperty: x] */
1847 s2 = objc_build_setter_call (lhs, setter_argument);
1849 /* This happens if building the setter failed because the
1850 property is readonly. */
1851 if (s2 == error_mark_node)
1852 return error_mark_node;
1854 SET_EXPR_LOCATION (s2, input_location);
1856 /* s3: x */
1857 s3 = convert (TREE_TYPE (lhs), setter_argument);
1859 /* Now build the compound statement (s1, s2, s3) or (s2, s3) as
1860 appropriate. */
1861 if (s1)
1862 compound_expr = build_compound_expr (input_location, build_compound_expr (input_location, s1, s2), s3);
1863 else
1864 compound_expr = build_compound_expr (input_location, s2, s3);
1866 /* Without this, with -Wall you get a 'valued computed is not
1867 used' every time there is a "object.property = x" where the
1868 value of the resulting MODIFY_EXPR is not used. That is
1869 correct (maybe a more sophisticated implementation could
1870 avoid generating the compound expression if not needed), but
1871 we need to turn it off. */
1872 TREE_NO_WARNING (compound_expr) = 1;
1873 return compound_expr;
1875 else
1876 return NULL_TREE;
1879 /* This hook is called by the frontend when one of the four unary
1880 expressions PREINCREMENT_EXPR, POSTINCREMENT_EXPR,
1881 PREDECREMENT_EXPR and POSTDECREMENT_EXPR is being built with an
1882 argument which is a PROPERTY_REF. For example, this happens if you have
1884 object.count++;
1886 where 'count' is a property. We need to use the 'getter' and
1887 'setter' for the property in an appropriate way to build the
1888 appropriate expression. 'code' is the code for the expression (one
1889 of the four mentioned above); 'argument' is the PROPERTY_REF, and
1890 'increment' is how much we need to add or subtract. */
1891 tree
1892 objc_build_incr_expr_for_property_ref (location_t location,
1893 enum tree_code code,
1894 tree argument, tree increment)
1896 /* Here are the expressions that we want to build:
1898 For PREINCREMENT_EXPR / PREDECREMENT_EXPR:
1899 (temp = [object property] +/- increment, [object setProperty: temp], temp)
1901 For POSTINCREMENT_EXPR / POSTECREMENT_EXPR:
1902 (temp = [object property], [object setProperty: temp +/- increment], temp) */
1904 tree temp_variable_decl, bind;
1905 /* s1, s2 and s3 are the tree statements that we need in the
1906 compound expression. */
1907 tree s1, s2, s3, compound_expr;
1909 /* Safety check. */
1910 if (!argument || TREE_CODE (argument) != PROPERTY_REF)
1911 return error_mark_node;
1913 /* Declare __objc_property_temp in a local bind. */
1914 temp_variable_decl = objc_create_temporary_var (TREE_TYPE (argument), "__objc_property_temp");
1915 DECL_SOURCE_LOCATION (temp_variable_decl) = location;
1916 bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL);
1917 SET_EXPR_LOCATION (bind, location);
1918 TREE_SIDE_EFFECTS (bind) = 1;
1919 add_stmt (bind);
1921 /* Now build the compound statement. */
1923 /* Note that the 'getter' is generated at gimplify time; at this
1924 time, we can simply put the property_ref (ie, argument) wherever
1925 we want the getter ultimately to be. */
1927 /* s1: __objc_property_temp = [object property] <+/- increment> */
1928 switch (code)
1930 case PREINCREMENT_EXPR:
1931 /* __objc_property_temp = [object property] + increment */
1932 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1933 NOP_EXPR,
1934 location, build2 (PLUS_EXPR, TREE_TYPE (argument),
1935 argument, increment), NULL_TREE);
1936 break;
1937 case PREDECREMENT_EXPR:
1938 /* __objc_property_temp = [object property] - increment */
1939 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1940 NOP_EXPR,
1941 location, build2 (MINUS_EXPR, TREE_TYPE (argument),
1942 argument, increment), NULL_TREE);
1943 break;
1944 case POSTINCREMENT_EXPR:
1945 case POSTDECREMENT_EXPR:
1946 /* __objc_property_temp = [object property] */
1947 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1948 NOP_EXPR,
1949 location, argument, NULL_TREE);
1950 break;
1951 default:
1952 gcc_unreachable ();
1955 /* s2: [object setProperty: __objc_property_temp <+/- increment>] */
1956 switch (code)
1958 case PREINCREMENT_EXPR:
1959 case PREDECREMENT_EXPR:
1960 /* [object setProperty: __objc_property_temp] */
1961 s2 = objc_build_setter_call (argument, temp_variable_decl);
1962 break;
1963 case POSTINCREMENT_EXPR:
1964 /* [object setProperty: __objc_property_temp + increment] */
1965 s2 = objc_build_setter_call (argument,
1966 build2 (PLUS_EXPR, TREE_TYPE (argument),
1967 temp_variable_decl, increment));
1968 break;
1969 case POSTDECREMENT_EXPR:
1970 /* [object setProperty: __objc_property_temp - increment] */
1971 s2 = objc_build_setter_call (argument,
1972 build2 (MINUS_EXPR, TREE_TYPE (argument),
1973 temp_variable_decl, increment));
1974 break;
1975 default:
1976 gcc_unreachable ();
1979 /* This happens if building the setter failed because the property
1980 is readonly. */
1981 if (s2 == error_mark_node)
1982 return error_mark_node;
1984 SET_EXPR_LOCATION (s2, location);
1986 /* s3: __objc_property_temp */
1987 s3 = convert (TREE_TYPE (argument), temp_variable_decl);
1989 /* Now build the compound statement (s1, s2, s3) */
1990 compound_expr = build_compound_expr (location, build_compound_expr (location, s1, s2), s3);
1992 /* Prevent C++ from warning with -Wall that "right operand of comma
1993 operator has no effect". */
1994 TREE_NO_WARNING (compound_expr) = 1;
1995 return compound_expr;
1998 tree
1999 objc_build_method_signature (bool is_class_method, tree rettype, tree selector,
2000 tree optparms, bool ellipsis)
2002 if (is_class_method)
2003 return build_method_decl (CLASS_METHOD_DECL, rettype, selector,
2004 optparms, ellipsis);
2005 else
2006 return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector,
2007 optparms, ellipsis);
2010 void
2011 objc_add_method_declaration (bool is_class_method, tree decl, tree attributes)
2013 if (!objc_interface_context)
2015 /* PS: At the moment, due to how the parser works, it should be
2016 impossible to get here. But it's good to have the check in
2017 case the parser changes.
2019 fatal_error ("method declaration not in @interface context");
2022 if (flag_objc1_only && attributes)
2023 error_at (input_location, "method attributes are not available in Objective-C 1.0");
2025 objc_decl_method_attributes (&decl, attributes, 0);
2026 objc_add_method (objc_interface_context,
2027 decl,
2028 is_class_method,
2029 objc_method_optional_flag);
2032 /* Return 'true' if the method definition could be started, and
2033 'false' if not (because we are outside an @implementation context).
2034 EXPR is NULL or an expression that needs to be evaluated for the
2035 side effects of array size expressions in the parameters.
2037 bool
2038 objc_start_method_definition (bool is_class_method, tree decl, tree attributes,
2039 tree expr)
2041 if (!objc_implementation_context)
2043 error ("method definition not in @implementation context");
2044 return false;
2047 if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node)
2048 return false;
2050 #ifndef OBJCPLUS
2051 /* Indicate no valid break/continue context by setting these variables
2052 to some non-null, non-label value. We'll notice and emit the proper
2053 error message in c_finish_bc_stmt. */
2054 c_break_label = c_cont_label = size_zero_node;
2055 #endif
2057 if (attributes)
2058 warning_at (input_location, 0, "method attributes can not be specified in @implementation context");
2059 else
2060 objc_decl_method_attributes (&decl, attributes, 0);
2062 objc_add_method (objc_implementation_context,
2063 decl,
2064 is_class_method,
2065 /* is optional */ false);
2066 start_method_def (decl, expr);
2067 return true;
2070 void
2071 objc_add_instance_variable (tree decl)
2073 (void) add_instance_variable (objc_ivar_context,
2074 objc_ivar_visibility,
2075 decl);
2078 /* Construct a C struct with same name as KLASS, a base struct with tag
2079 SUPER_NAME (if any), and FIELDS indicated. */
2081 static tree
2082 objc_build_struct (tree klass, tree fields, tree super_name)
2084 tree name = CLASS_NAME (klass);
2085 tree s = objc_start_struct (name);
2086 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
2087 tree t;
2088 vec<tree> objc_info = vNULL;
2089 int i;
2091 if (super)
2093 /* Prepend a packed variant of the base class into the layout. This
2094 is necessary to preserve ObjC ABI compatibility. */
2095 tree base = build_decl (input_location,
2096 FIELD_DECL, NULL_TREE, super);
2097 tree field = TYPE_FIELDS (super);
2099 while (field && DECL_CHAIN (field)
2100 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
2101 field = DECL_CHAIN (field);
2103 /* For ObjC ABI purposes, the "packed" size of a base class is
2104 the sum of the offset and the size (in bits) of the last field
2105 in the class. */
2106 DECL_SIZE (base)
2107 = (field && TREE_CODE (field) == FIELD_DECL
2108 ? size_binop (PLUS_EXPR,
2109 size_binop (PLUS_EXPR,
2110 size_binop
2111 (MULT_EXPR,
2112 convert (bitsizetype,
2113 DECL_FIELD_OFFSET (field)),
2114 bitsize_int (BITS_PER_UNIT)),
2115 DECL_FIELD_BIT_OFFSET (field)),
2116 DECL_SIZE (field))
2117 : bitsize_zero_node);
2118 DECL_SIZE_UNIT (base)
2119 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
2120 size_int (BITS_PER_UNIT));
2121 DECL_ARTIFICIAL (base) = 1;
2122 DECL_ALIGN (base) = 1;
2123 DECL_FIELD_CONTEXT (base) = s;
2124 #ifdef OBJCPLUS
2125 DECL_FIELD_IS_BASE (base) = 1;
2127 if (fields)
2128 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
2129 #endif /* are following the ObjC ABI here. */
2130 DECL_CHAIN (base) = fields;
2131 fields = base;
2134 /* NB: Calling finish_struct() may cause type TYPE_OBJC_INFO
2135 information in all variants of this RECORD_TYPE to be destroyed
2136 (this is because the C frontend manipulates TYPE_LANG_SPECIFIC
2137 for something else and then will change all variants to use the
2138 same resulting TYPE_LANG_SPECIFIC, ignoring the fact that we use
2139 it for ObjC protocols and that such propagation will make all
2140 variants use the same objc_info), but it is therein that we store
2141 protocol conformance info (e.g., 'NSObject <MyProtocol>').
2142 Hence, we must save the ObjC-specific information before calling
2143 finish_struct(), and then reinstate it afterwards. */
2145 for (t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
2147 INIT_TYPE_OBJC_INFO (t);
2148 objc_info.safe_push (TYPE_OBJC_INFO (t));
2151 s = objc_finish_struct (s, fields);
2153 for (i = 0, t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
2155 /* We now want to restore the different TYPE_OBJC_INFO, but we
2156 have the additional problem that the C frontend doesn't just
2157 copy TYPE_LANG_SPECIFIC from one variant to the other; it
2158 actually makes all of them the *same* TYPE_LANG_SPECIFIC. As
2159 we need a different TYPE_OBJC_INFO for each (and
2160 TYPE_OBJC_INFO is a field in TYPE_LANG_SPECIFIC), we need to
2161 make a copy of each TYPE_LANG_SPECIFIC before we modify
2162 TYPE_OBJC_INFO. */
2163 if (TYPE_LANG_SPECIFIC (t))
2165 /* Create a copy of TYPE_LANG_SPECIFIC. */
2166 struct lang_type *old_lang_type = TYPE_LANG_SPECIFIC (t);
2167 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2168 memcpy (TYPE_LANG_SPECIFIC (t), old_lang_type,
2169 SIZEOF_OBJC_TYPE_LANG_SPECIFIC);
2171 else
2173 /* Just create a new one. */
2174 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2176 /* Replace TYPE_OBJC_INFO with the saved one. This restores any
2177 protocol information that may have been associated with the
2178 type. */
2179 TYPE_OBJC_INFO (t) = objc_info[i];
2180 /* Replace the IDENTIFIER_NODE with an actual @interface now
2181 that we have it. */
2182 TYPE_OBJC_INTERFACE (t) = klass;
2184 objc_info.release ();
2186 /* Use TYPE_BINFO structures to point at the super class, if any. */
2187 objc_xref_basetypes (s, super);
2189 /* Mark this struct as a class template. */
2190 CLASS_STATIC_TEMPLATE (klass) = s;
2192 return s;
2195 /* Mark DECL as being 'volatile' for purposes of Darwin
2196 _setjmp()/_longjmp() exception handling. Called from
2197 objc_mark_locals_volatile(). */
2198 void
2199 objc_volatilize_decl (tree decl)
2201 /* Do not mess with variables that are 'static' or (already)
2202 'volatile'. */
2203 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
2204 && (TREE_CODE (decl) == VAR_DECL
2205 || TREE_CODE (decl) == PARM_DECL))
2207 if (local_variables_to_volatilize == NULL)
2208 vec_alloc (local_variables_to_volatilize, 8);
2210 vec_safe_push (local_variables_to_volatilize, decl);
2214 /* Called when parsing of a function completes; if any local variables
2215 in the function were marked as variables to volatilize, change them
2216 to volatile. We do this at the end of the function when the
2217 warnings about discarding 'volatile' have already been produced.
2218 We are making the variables as volatile just to force the compiler
2219 to preserve them between setjmp/longjmp, but we don't want warnings
2220 for them as they aren't really volatile. */
2221 void
2222 objc_finish_function (void)
2224 /* If there are any local variables to volatilize, volatilize them. */
2225 if (local_variables_to_volatilize)
2227 int i;
2228 tree decl;
2229 FOR_EACH_VEC_ELT (*local_variables_to_volatilize, i, decl)
2231 tree t = TREE_TYPE (decl);
2233 t = build_qualified_type (t, TYPE_QUALS (t) | TYPE_QUAL_VOLATILE);
2234 TREE_TYPE (decl) = t;
2235 TREE_THIS_VOLATILE (decl) = 1;
2236 TREE_SIDE_EFFECTS (decl) = 1;
2237 DECL_REGISTER (decl) = 0;
2238 #ifndef OBJCPLUS
2239 C_DECL_REGISTER (decl) = 0;
2240 #endif
2243 /* Now we delete the vector. This sets it to NULL as well. */
2244 vec_free (local_variables_to_volatilize);
2248 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
2249 (including its categories and superclasses) or by object type TYP.
2250 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
2252 static bool
2253 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
2255 bool class_type = (cls != NULL_TREE);
2257 while (cls)
2259 tree c;
2261 /* Check protocols adopted by the class and its categories. */
2262 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
2264 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
2265 return true;
2268 /* Repeat for superclasses. */
2269 cls = lookup_interface (CLASS_SUPER_NAME (cls));
2272 /* Check for any protocols attached directly to the object type. */
2273 if (TYPE_HAS_OBJC_INFO (typ))
2275 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
2276 return true;
2279 if (warn)
2281 *errbuf = 0;
2282 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
2283 /* NB: Types 'id' and 'Class' cannot reasonably be described as
2284 "implementing" a given protocol, since they do not have an
2285 implementation. */
2286 if (class_type)
2287 warning (0, "class %qs does not implement the %qE protocol",
2288 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2289 else
2290 warning (0, "type %qs does not conform to the %qE protocol",
2291 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2294 return false;
2297 /* Check if class RCLS and instance struct type RTYP conform to at least the
2298 same protocols that LCLS and LTYP conform to. */
2300 static bool
2301 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
2303 tree p;
2304 bool have_lproto = false;
2306 while (lcls)
2308 /* NB: We do _not_ look at categories defined for LCLS; these may or
2309 may not get loaded in, and therefore it is unreasonable to require
2310 that RCLS/RTYP must implement any of their protocols. */
2311 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
2313 have_lproto = true;
2315 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2316 return warn;
2319 /* Repeat for superclasses. */
2320 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
2323 /* Check for any protocols attached directly to the object type. */
2324 if (TYPE_HAS_OBJC_INFO (ltyp))
2326 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
2328 have_lproto = true;
2330 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2331 return warn;
2335 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
2336 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
2337 away with simply checking for 'id' or 'Class' (!RCLS), since this
2338 routine will not get called in other cases. */
2339 return have_lproto || (rcls != NULL_TREE);
2342 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
2343 Both TYPE1 and TYPE2 must be pointers, and already determined to be
2344 compatible by objc_compare_types() below. */
2346 tree
2347 objc_common_type (tree type1, tree type2)
2349 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
2351 while (POINTER_TYPE_P (inner1))
2353 inner1 = TREE_TYPE (inner1);
2354 inner2 = TREE_TYPE (inner2);
2357 /* If one type is derived from another, return the base type. */
2358 if (DERIVED_FROM_P (inner1, inner2))
2359 return type1;
2360 else if (DERIVED_FROM_P (inner2, inner1))
2361 return type2;
2363 /* If both types are 'Class', return 'Class'. */
2364 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
2365 return objc_class_type;
2367 /* Otherwise, return 'id'. */
2368 return objc_object_type;
2371 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
2372 an instance of RTYP to an instance of LTYP or to compare the two
2373 (if ARGNO is equal to -3), per ObjC type system rules. Before
2374 returning 'true', this routine may issue warnings related to, e.g.,
2375 protocol conformance. When returning 'false', the routine must
2376 produce absolutely no warnings; the C or C++ front-end will do so
2377 instead, if needed. If either LTYP or RTYP is not an Objective-C
2378 type, the routine must return 'false'.
2380 The ARGNO parameter is encoded as follows:
2381 >= 1 Parameter number (CALLEE contains function being called);
2382 0 Return value;
2383 -1 Assignment;
2384 -2 Initialization;
2385 -3 Comparison (LTYP and RTYP may match in either direction);
2386 -4 Silent comparison (for C++ overload resolution);
2387 -5 Silent "specialization" comparison for RTYP to be a "specialization"
2388 of LTYP (a specialization means that RTYP is LTYP plus some constraints,
2389 so that each object of type RTYP is also of type LTYP). This is used
2390 when comparing property types. */
2392 bool
2393 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
2395 tree lcls, rcls, lproto, rproto;
2396 bool pointers_compatible;
2398 /* We must be dealing with pointer types */
2399 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
2400 return false;
2404 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2405 rtyp = TREE_TYPE (rtyp);
2407 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2409 /* We must also handle function pointers, since ObjC is a bit more
2410 lenient than C or C++ on this. */
2411 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
2413 function_args_iterator liter, riter;
2415 /* Return types must be covariant. */
2416 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
2417 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
2418 argno, callee))
2419 return false;
2421 /* Argument types must be contravariant. */
2422 function_args_iter_init (&liter, ltyp);
2423 function_args_iter_init (&riter, rtyp);
2425 while (1)
2427 ltyp = function_args_iter_cond (&liter);
2428 rtyp = function_args_iter_cond (&riter);
2430 /* If we've exhaused both lists simulateously, we're done. */
2431 if (ltyp == NULL_TREE && rtyp == NULL_TREE)
2432 break;
2434 /* If one list is shorter than the other, they fail to match. */
2435 if (ltyp == NULL_TREE || rtyp == NULL_TREE)
2436 return false;
2438 if (!comptypes (rtyp, ltyp)
2439 && !objc_compare_types (rtyp, ltyp, argno, callee))
2440 return false;
2442 function_args_iter_next (&liter);
2443 function_args_iter_next (&riter);
2446 return true;
2449 /* Past this point, we are only interested in ObjC class instances,
2450 or 'id' or 'Class'. */
2451 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
2452 return false;
2454 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
2455 && !TYPE_HAS_OBJC_INFO (ltyp))
2456 return false;
2458 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
2459 && !TYPE_HAS_OBJC_INFO (rtyp))
2460 return false;
2462 /* Past this point, we are committed to returning 'true' to the caller
2463 (unless performing a silent comparison; see below). However, we can
2464 still warn about type and/or protocol mismatches. */
2466 if (TYPE_HAS_OBJC_INFO (ltyp))
2468 lcls = TYPE_OBJC_INTERFACE (ltyp);
2469 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
2471 else
2472 lcls = lproto = NULL_TREE;
2474 if (TYPE_HAS_OBJC_INFO (rtyp))
2476 rcls = TYPE_OBJC_INTERFACE (rtyp);
2477 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
2479 else
2480 rcls = rproto = NULL_TREE;
2482 /* If we could not find an @interface declaration, we must have
2483 only seen a @class declaration; for purposes of type comparison,
2484 treat it as a stand-alone (root) class. */
2486 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
2487 lcls = NULL_TREE;
2489 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
2490 rcls = NULL_TREE;
2492 /* If either type is an unqualified 'id', we're done. This is because
2493 an 'id' can be assigned to or from any type with no warnings. */
2494 if (argno != -5)
2496 if ((!lproto && objc_is_object_id (ltyp))
2497 || (!rproto && objc_is_object_id (rtyp)))
2498 return true;
2500 else
2502 /* For property checks, though, an 'id' is considered the most
2503 general type of object, hence if you try to specialize an
2504 'NSArray *' (ltyp) property with an 'id' (rtyp) one, we need
2505 to warn. */
2506 if (!lproto && objc_is_object_id (ltyp))
2507 return true;
2510 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
2512 /* If the underlying types are the same, and at most one of them has
2513 a protocol list, we do not need to issue any diagnostics. */
2514 if (pointers_compatible && (!lproto || !rproto))
2515 return true;
2517 /* If exactly one of the types is 'Class', issue a diagnostic; any
2518 exceptions of this rule have already been handled. */
2519 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
2520 pointers_compatible = false;
2521 /* Otherwise, check for inheritance relations. */
2522 else
2524 if (!pointers_compatible)
2526 /* Again, if any of the two is an 'id', we're satisfied,
2527 unless we're comparing properties, in which case only an
2528 'id' on the left-hand side (old property) is good
2529 enough. */
2530 if (argno != -5)
2531 pointers_compatible
2532 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
2533 else
2534 pointers_compatible = objc_is_object_id (ltyp);
2537 if (!pointers_compatible)
2538 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
2540 if (!pointers_compatible && (argno == -3 || argno == -4))
2541 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
2544 /* If the pointers match modulo protocols, check for protocol conformance
2545 mismatches. */
2546 if (pointers_compatible)
2548 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
2549 argno != -3);
2551 if (!pointers_compatible && argno == -3)
2552 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
2553 argno != -3);
2556 if (!pointers_compatible)
2558 /* The two pointers are not exactly compatible. Issue a warning, unless
2559 we are performing a silent comparison, in which case return 'false'
2560 instead. */
2561 /* NB: For the time being, we shall make our warnings look like their
2562 C counterparts. In the future, we may wish to make them more
2563 ObjC-specific. */
2564 switch (argno)
2566 case -5:
2567 case -4:
2568 return false;
2570 case -3:
2571 warning (0, "comparison of distinct Objective-C types lacks a cast");
2572 break;
2574 case -2:
2575 warning (0, "initialization from distinct Objective-C type");
2576 break;
2578 case -1:
2579 warning (0, "assignment from distinct Objective-C type");
2580 break;
2582 case 0:
2583 warning (0, "distinct Objective-C type in return");
2584 break;
2586 default:
2587 warning (0, "passing argument %d of %qE from distinct "
2588 "Objective-C type", argno, callee);
2589 break;
2593 return true;
2596 /* This routine is similar to objc_compare_types except that function-pointers are
2597 excluded. This is because, caller assumes that common types are of (id, Object*)
2598 variety and calls objc_common_type to obtain a common type. There is no commonolty
2599 between two function-pointers in this regard. */
2601 bool
2602 objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
2604 if (objc_compare_types (ltyp, rtyp, argno, callee))
2606 /* exclude function-pointer types. */
2609 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2610 rtyp = TREE_TYPE (rtyp);
2612 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2613 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
2615 return false;
2618 #ifndef OBJCPLUS
2619 /* Determine if CHILD is derived from PARENT. The routine assumes that
2620 both parameters are RECORD_TYPEs, and is non-reflexive. */
2622 static bool
2623 objc_derived_from_p (tree parent, tree child)
2625 parent = TYPE_MAIN_VARIANT (parent);
2627 for (child = TYPE_MAIN_VARIANT (child);
2628 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
2630 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
2631 (TYPE_BINFO (child),
2632 0)));
2634 if (child == parent)
2635 return true;
2638 return false;
2640 #endif
2642 tree
2643 objc_build_component_ref (tree datum, tree component)
2645 /* If COMPONENT is NULL, the caller is referring to the anonymous
2646 base class field. */
2647 if (!component)
2649 tree base = TYPE_FIELDS (TREE_TYPE (datum));
2651 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
2654 /* The 'build_component_ref' routine has been removed from the C++
2655 front-end, but 'finish_class_member_access_expr' seems to be
2656 a worthy substitute. */
2657 #ifdef OBJCPLUS
2658 return finish_class_member_access_expr (datum, component, false,
2659 tf_warning_or_error);
2660 #else
2661 return build_component_ref (input_location, datum, component);
2662 #endif
2665 /* Recursively copy inheritance information rooted at BINFO. To do this,
2666 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
2668 static tree
2669 objc_copy_binfo (tree binfo)
2671 tree btype = BINFO_TYPE (binfo);
2672 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
2673 tree base_binfo;
2674 int ix;
2676 BINFO_TYPE (binfo2) = btype;
2677 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
2678 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
2680 /* Recursively copy base binfos of BINFO. */
2681 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2683 tree base_binfo2 = objc_copy_binfo (base_binfo);
2685 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
2686 BINFO_BASE_APPEND (binfo2, base_binfo2);
2689 return binfo2;
2692 /* Record superclass information provided in BASETYPE for ObjC class REF.
2693 This is loosely based on cp/decl.c:xref_basetypes(). */
2695 static void
2696 objc_xref_basetypes (tree ref, tree basetype)
2698 tree variant;
2699 tree binfo = make_tree_binfo (basetype ? 1 : 0);
2700 TYPE_BINFO (ref) = binfo;
2701 BINFO_OFFSET (binfo) = size_zero_node;
2702 BINFO_TYPE (binfo) = ref;
2704 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
2705 for (variant = ref; variant; variant = TYPE_NEXT_VARIANT (variant))
2706 TYPE_BINFO (variant) = binfo;
2708 if (basetype)
2710 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
2712 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
2713 vec_alloc (BINFO_BASE_ACCESSES (binfo), 1);
2714 BINFO_BASE_APPEND (binfo, base_binfo);
2715 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
2719 /* Called from finish_decl. */
2721 void
2722 objc_check_decl (tree decl)
2724 tree type = TREE_TYPE (decl);
2726 if (TREE_CODE (type) != RECORD_TYPE)
2727 return;
2728 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
2729 error ("statically allocated instance of Objective-C class %qE",
2730 type);
2733 void
2734 objc_check_global_decl (tree decl)
2736 tree id = DECL_NAME (decl);
2737 if (objc_is_class_name (id) && global_bindings_p())
2738 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id));
2741 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where
2742 INTERFACE may either name an Objective-C class, or refer to the
2743 special 'id' or 'Class' types. If INTERFACE is not a valid ObjC
2744 type, just return it unchanged. This function is often called when
2745 PROTOCOLS is NULL_TREE, in which case we simply look up the
2746 appropriate INTERFACE. */
2748 tree
2749 objc_get_protocol_qualified_type (tree interface, tree protocols)
2751 /* If INTERFACE is not provided, default to 'id'. */
2752 tree type = (interface ? objc_is_id (interface) : objc_object_type);
2753 bool is_ptr = (type != NULL_TREE);
2755 if (!is_ptr)
2757 type = objc_is_class_name (interface);
2759 if (type)
2761 /* If looking at a typedef, retrieve the precise type it
2762 describes. */
2763 if (TREE_CODE (interface) == IDENTIFIER_NODE)
2764 interface = identifier_global_value (interface);
2766 type = ((interface && TREE_CODE (interface) == TYPE_DECL
2767 && DECL_ORIGINAL_TYPE (interface))
2768 ? DECL_ORIGINAL_TYPE (interface)
2769 : xref_tag (RECORD_TYPE, type));
2771 else
2773 /* This case happens when we are given an 'interface' which
2774 is not a valid class name. For example if a typedef was
2775 used, and 'interface' really is the identifier of the
2776 typedef, but when you resolve it you don't get an
2777 Objective-C class, but something else, such as 'int'.
2778 This is an error; protocols make no sense unless you use
2779 them with Objective-C objects. */
2780 error_at (input_location, "only Objective-C object types can be qualified with a protocol");
2782 /* Try to recover. Ignore the invalid class name, and treat
2783 the object as an 'id' to silence further warnings about
2784 the class. */
2785 type = objc_object_type;
2786 is_ptr = true;
2790 if (protocols)
2792 type = build_variant_type_copy (type);
2794 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
2795 to the pointee. */
2796 if (is_ptr)
2798 tree orig_pointee_type = TREE_TYPE (type);
2799 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
2801 /* Set up the canonical type information. */
2802 TYPE_CANONICAL (type)
2803 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
2805 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
2806 type = TREE_TYPE (type);
2809 /* Look up protocols and install in lang specific list. */
2810 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
2811 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols
2812 (protocols, /* definition_required */ false);
2814 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
2815 return the pointer to the new pointee variant. */
2816 if (is_ptr)
2817 type = TYPE_POINTER_TO (type);
2818 else
2819 TYPE_OBJC_INTERFACE (type)
2820 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
2823 return type;
2826 /* Check for circular dependencies in protocols. The arguments are
2827 PROTO, the protocol to check, and LIST, a list of protocol it
2828 conforms to. */
2830 static void
2831 check_protocol_recursively (tree proto, tree list)
2833 tree p;
2835 for (p = list; p; p = TREE_CHAIN (p))
2837 tree pp = TREE_VALUE (p);
2839 if (TREE_CODE (pp) == IDENTIFIER_NODE)
2840 pp = lookup_protocol (pp, /* warn if deprecated */ false,
2841 /* definition_required */ false);
2843 if (pp == proto)
2844 fatal_error ("protocol %qE has circular dependency",
2845 PROTOCOL_NAME (pp));
2846 if (pp)
2847 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
2851 /* Look up PROTOCOLS, and return a list of those that are found. If
2852 none are found, return NULL. Note that this function will emit a
2853 warning if a protocol is found and is deprecated. If
2854 'definition_required', then warn if the protocol is found but is
2855 not defined (ie, if we only saw a forward-declaration of the
2856 protocol (as in "@protocol NSObject;") not a real definition with
2857 the list of methods). */
2858 static tree
2859 lookup_and_install_protocols (tree protocols, bool definition_required)
2861 tree proto;
2862 tree return_value = NULL_TREE;
2864 if (protocols == error_mark_node)
2865 return NULL;
2867 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
2869 tree ident = TREE_VALUE (proto);
2870 tree p = lookup_protocol (ident, /* warn_if_deprecated */ true,
2871 definition_required);
2873 if (p)
2874 return_value = chainon (return_value,
2875 build_tree_list (NULL_TREE, p));
2876 else if (ident != error_mark_node)
2877 error ("cannot find protocol declaration for %qE",
2878 ident);
2881 return return_value;
2884 static void
2885 build_common_objc_exception_stuff (void)
2887 tree noreturn_list, nothrow_list, temp_type;
2889 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
2890 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
2892 /* void objc_exception_throw(id) __attribute__((noreturn)); */
2893 /* void objc_sync_enter(id); */
2894 /* void objc_sync_exit(id); */
2895 temp_type = build_function_type_list (void_type_node,
2896 objc_object_type,
2897 NULL_TREE);
2898 objc_exception_throw_decl
2899 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
2900 noreturn_list);
2901 /* Make sure that objc_exception_throw (id) claims that it may throw an
2902 exception. */
2903 TREE_NOTHROW (objc_exception_throw_decl) = 0;
2905 objc_sync_enter_decl
2906 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
2907 NULL, nothrow_list);
2909 objc_sync_exit_decl
2910 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
2911 NULL, nothrow_list);
2914 /* Purpose: "play" parser, creating/installing representations
2915 of the declarations that are required by Objective-C.
2917 Model:
2919 type_spec--------->sc_spec
2920 (tree_list) (tree_list)
2923 identifier_node identifier_node */
2925 static void
2926 synth_module_prologue (void)
2928 tree type;
2929 enum debug_info_type save_write_symbols = write_symbols;
2930 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
2932 /* Suppress outputting debug symbols, because
2933 dbxout_init hasn't been called yet. */
2934 write_symbols = NO_DEBUG;
2935 debug_hooks = &do_nothing_debug_hooks;
2937 #ifdef OBJCPLUS
2938 push_lang_context (lang_name_c); /* extern "C" */
2939 #endif
2941 /* The following are also defined in <objc/objc.h> and friends. */
2943 objc_object_id = get_identifier (TAG_OBJECT);
2944 objc_class_id = get_identifier (TAG_CLASS);
2946 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
2947 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
2949 objc_object_type = build_pointer_type (objc_object_reference);
2950 objc_class_type = build_pointer_type (objc_class_reference);
2952 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
2953 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
2955 /* Declare the 'id' and 'Class' typedefs. */
2956 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2957 TYPE_DECL,
2958 objc_object_name,
2959 objc_object_type));
2960 TREE_NO_WARNING (type) = 1;
2962 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2963 TYPE_DECL,
2964 objc_class_name,
2965 objc_class_type));
2966 TREE_NO_WARNING (type) = 1;
2968 /* Forward-declare '@interface Protocol'. */
2969 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
2970 objc_declare_class (type);
2971 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE, type));
2973 /* Declare receiver type used for dispatching messages to 'super'. */
2974 /* `struct objc_super *' */
2975 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
2976 get_identifier (TAG_SUPER)));
2978 /* Declare pointers to method and ivar lists. */
2979 objc_method_list_ptr = build_pointer_type
2980 (xref_tag (RECORD_TYPE,
2981 get_identifier (UTAG_METHOD_LIST)));
2982 objc_method_proto_list_ptr
2983 = build_pointer_type (xref_tag (RECORD_TYPE,
2984 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
2985 objc_ivar_list_ptr = build_pointer_type
2986 (xref_tag (RECORD_TYPE,
2987 get_identifier (UTAG_IVAR_LIST)));
2989 build_common_objc_exception_stuff ();
2991 /* Set-up runtime-specific templates, message and exception stuff. */
2992 (*runtime.initialize) ();
2994 /* Declare objc_getProperty, object_setProperty and other property
2995 accessor helpers. */
2996 build_common_objc_property_accessor_helpers ();
2998 /* Forward declare constant_string_id and constant_string_type. */
2999 if (!constant_string_class_name)
3000 constant_string_class_name = runtime.default_constant_string_class_name;
3001 constant_string_id = get_identifier (constant_string_class_name);
3002 objc_declare_class (constant_string_id);
3004 /* Pre-build the following entities - for speed/convenience. */
3005 self_id = get_identifier ("self");
3006 ucmd_id = get_identifier ("_cmd");
3008 /* Declare struct _objc_fast_enumeration_state { ... }; */
3009 build_fast_enumeration_state_template ();
3011 /* void objc_enumeration_mutation (id) */
3012 type = build_function_type_list (void_type_node,
3013 objc_object_type, NULL_TREE);
3014 objc_enumeration_mutation_decl
3015 = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN,
3016 NULL, NULL_TREE);
3017 TREE_NOTHROW (objc_enumeration_mutation_decl) = 0;
3019 #ifdef OBJCPLUS
3020 pop_lang_context ();
3021 #endif
3023 write_symbols = save_write_symbols;
3024 debug_hooks = save_hooks;
3027 /* --- const strings --- */
3029 /* Ensure that the ivar list for NSConstantString/NXConstantString
3030 (or whatever was specified via `-fconstant-string-class')
3031 contains fields at least as large as the following three, so that
3032 the runtime can stomp on them with confidence:
3034 struct STRING_OBJECT_CLASS_NAME
3036 Object isa;
3037 char *cString;
3038 unsigned int length;
3039 }; */
3041 static int
3042 check_string_class_template (void)
3044 tree field_decl = objc_get_class_ivars (constant_string_id);
3046 #define AT_LEAST_AS_LARGE_AS(F, T) \
3047 (F && TREE_CODE (F) == FIELD_DECL \
3048 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
3049 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
3051 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3052 return 0;
3054 field_decl = DECL_CHAIN (field_decl);
3055 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3056 return 0;
3058 field_decl = DECL_CHAIN (field_decl);
3059 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
3061 #undef AT_LEAST_AS_LARGE_AS
3064 /* Avoid calling `check_string_class_template ()' more than once. */
3065 static GTY(()) int string_layout_checked;
3067 /* Construct an internal string layout to be used as a template for
3068 creating NSConstantString/NXConstantString instances. */
3070 static tree
3071 objc_build_internal_const_str_type (void)
3073 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
3074 tree fields = build_decl (input_location,
3075 FIELD_DECL, NULL_TREE, ptr_type_node);
3076 tree field = build_decl (input_location,
3077 FIELD_DECL, NULL_TREE, ptr_type_node);
3079 DECL_CHAIN (field) = fields; fields = field;
3080 field = build_decl (input_location,
3081 FIELD_DECL, NULL_TREE, unsigned_type_node);
3082 DECL_CHAIN (field) = fields; fields = field;
3083 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
3084 reverse order! */
3085 finish_builtin_struct (type, "__builtin_ObjCString",
3086 fields, NULL_TREE);
3088 return type;
3091 /* Custom build_string which sets TREE_TYPE! */
3093 tree
3094 my_build_string (int len, const char *str)
3096 return fix_string_type (build_string (len, str));
3099 /* Build a string with contents STR and length LEN and convert it to a
3100 pointer. */
3102 tree
3103 my_build_string_pointer (int len, const char *str)
3105 tree string = my_build_string (len, str);
3106 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
3107 return build1 (ADDR_EXPR, ptrtype, string);
3110 static hashval_t
3111 string_hash (const void *ptr)
3113 const_tree const str = ((const struct string_descriptor *)ptr)->literal;
3114 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3115 int i, len = TREE_STRING_LENGTH (str);
3116 hashval_t h = len;
3118 for (i = 0; i < len; i++)
3119 h = ((h * 613) + p[i]);
3121 return h;
3124 static int
3125 string_eq (const void *ptr1, const void *ptr2)
3127 const_tree const str1 = ((const struct string_descriptor *)ptr1)->literal;
3128 const_tree const str2 = ((const struct string_descriptor *)ptr2)->literal;
3129 int len1 = TREE_STRING_LENGTH (str1);
3131 return (len1 == TREE_STRING_LENGTH (str2)
3132 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3133 len1));
3136 /* Given a chain of STRING_CST's, build a static instance of
3137 NXConstantString which points at the concatenation of those
3138 strings. We place the string object in the __string_objects
3139 section of the __OBJC segment. The Objective-C runtime will
3140 initialize the isa pointers of the string objects to point at the
3141 NXConstantString class object. */
3143 tree
3144 objc_build_string_object (tree string)
3146 tree constant_string_class;
3147 int length;
3148 tree addr;
3149 struct string_descriptor *desc, key;
3150 void **loc;
3152 /* We should be passed a STRING_CST. */
3153 gcc_checking_assert (TREE_CODE (string) == STRING_CST);
3154 length = TREE_STRING_LENGTH (string) - 1;
3156 /* The target may have different ideas on how to construct an ObjC string
3157 literal. On Darwin (Mac OS X), for example, we may wish to obtain a
3158 constant CFString reference instead.
3159 At present, this is only supported for the NeXT runtime. */
3160 if (flag_next_runtime
3161 && targetcm.objc_construct_string_object)
3163 tree constructor = (*targetcm.objc_construct_string_object) (string);
3164 if (constructor)
3165 return build1 (NOP_EXPR, objc_object_type, constructor);
3168 /* Check whether the string class being used actually exists and has the
3169 correct ivar layout. */
3170 if (!string_layout_checked)
3172 string_layout_checked = -1;
3173 constant_string_class = lookup_interface (constant_string_id);
3174 internal_const_str_type = objc_build_internal_const_str_type ();
3176 if (!constant_string_class
3177 || !(constant_string_type
3178 = CLASS_STATIC_TEMPLATE (constant_string_class)))
3179 error ("cannot find interface declaration for %qE",
3180 constant_string_id);
3181 /* The NSConstantString/NXConstantString ivar layout is now known. */
3182 else if (!check_string_class_template ())
3183 error ("interface %qE does not have valid constant string layout",
3184 constant_string_id);
3185 /* If the runtime can generate a literal reference to the string class,
3186 don't need to run a constructor. */
3187 else if (!(*runtime.setup_const_string_class_decl)())
3188 error ("cannot find reference tag for class %qE", constant_string_id);
3189 else
3191 string_layout_checked = 1; /* Success! */
3192 add_class_reference (constant_string_id);
3196 if (string_layout_checked == -1)
3197 return error_mark_node;
3199 /* Perhaps we already constructed a constant string just like this one? */
3200 key.literal = string;
3201 loc = htab_find_slot (string_htab, &key, INSERT);
3202 desc = (struct string_descriptor *) *loc;
3204 if (!desc)
3206 *loc = desc = ggc_alloc<string_descriptor> ();
3207 desc->literal = string;
3208 desc->constructor =
3209 (*runtime.build_const_string_constructor) (input_location, string, length);
3212 addr = convert (build_pointer_type (constant_string_type),
3213 build_unary_op (input_location,
3214 ADDR_EXPR, desc->constructor, 1));
3216 return addr;
3219 /* Build a static constant CONSTRUCTOR
3220 with type TYPE and elements ELTS. */
3222 tree
3223 objc_build_constructor (tree type, vec<constructor_elt, va_gc> *elts)
3225 tree constructor = build_constructor (type, elts);
3227 TREE_CONSTANT (constructor) = 1;
3228 TREE_STATIC (constructor) = 1;
3229 TREE_READONLY (constructor) = 1;
3231 #ifdef OBJCPLUS
3232 /* Adjust for impedance mismatch. We should figure out how to build
3233 CONSTRUCTORs that consistently please both the C and C++ gods. */
3234 if (!(*elts)[0].index)
3235 TREE_TYPE (constructor) = init_list_type_node;
3236 #endif
3238 return constructor;
3241 /* Return the DECL of the string IDENT in the SECTION. */
3243 tree
3244 get_objc_string_decl (tree ident, enum string_section section)
3246 tree chain;
3248 switch (section)
3250 case class_names:
3251 chain = class_names_chain;
3252 break;
3253 case meth_var_names:
3254 chain = meth_var_names_chain;
3255 break;
3256 case meth_var_types:
3257 chain = meth_var_types_chain;
3258 break;
3259 case prop_names_attr:
3260 chain = prop_names_attr_chain;
3261 break;
3262 default:
3263 gcc_unreachable ();
3266 for (; chain != 0; chain = TREE_CHAIN (chain))
3267 if (TREE_VALUE (chain) == ident)
3268 return (TREE_PURPOSE (chain));
3270 /* We didn't find the entry. */
3271 return NULL_TREE;
3274 /* Create a class reference, but don't create a variable to reference
3275 it. */
3277 void
3278 add_class_reference (tree ident)
3280 tree chain;
3282 if ((chain = cls_ref_chain))
3284 tree tail;
3287 if (ident == TREE_VALUE (chain))
3288 return;
3290 tail = chain;
3291 chain = TREE_CHAIN (chain);
3293 while (chain);
3295 /* Append to the end of the list */
3296 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
3298 else
3299 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
3302 /* Get a class reference, creating it if necessary. Also create the
3303 reference variable. */
3304 tree
3305 objc_get_class_reference (tree ident)
3307 tree orig_ident = (DECL_P (ident)
3308 ? DECL_NAME (ident)
3309 : TYPE_P (ident)
3310 ? OBJC_TYPE_NAME (ident)
3311 : ident);
3312 bool local_scope = false;
3314 #ifdef OBJCPLUS
3315 if (processing_template_decl)
3316 /* Must wait until template instantiation time. */
3317 return build_min_nt_loc (UNKNOWN_LOCATION, CLASS_REFERENCE_EXPR, ident);
3318 #endif
3320 if (TREE_CODE (ident) == TYPE_DECL)
3321 ident = (DECL_ORIGINAL_TYPE (ident)
3322 ? DECL_ORIGINAL_TYPE (ident)
3323 : TREE_TYPE (ident));
3325 #ifdef OBJCPLUS
3326 if (TYPE_P (ident)
3327 && CP_TYPE_CONTEXT (ident) != global_namespace)
3328 local_scope = true;
3329 #endif
3331 if (local_scope || !(ident = objc_is_class_name (ident)))
3333 error ("%qE is not an Objective-C class name or alias",
3334 orig_ident);
3335 return error_mark_node;
3338 return (*runtime.get_class_reference) (ident);
3341 void
3342 objc_declare_alias (tree alias_ident, tree class_ident)
3344 tree underlying_class;
3346 #ifdef OBJCPLUS
3347 if (current_namespace != global_namespace) {
3348 error ("Objective-C declarations may only appear in global scope");
3350 #endif /* OBJCPLUS */
3352 if (!(underlying_class = objc_is_class_name (class_ident)))
3353 warning (0, "cannot find class %qE", class_ident);
3354 else if (objc_is_class_name (alias_ident))
3355 warning (0, "class %qE already exists", alias_ident);
3356 else
3358 /* Implement @compatibility_alias as a typedef. */
3359 #ifdef OBJCPLUS
3360 push_lang_context (lang_name_c); /* extern "C" */
3361 #endif
3362 lang_hooks.decls.pushdecl (build_decl
3363 (input_location,
3364 TYPE_DECL,
3365 alias_ident,
3366 xref_tag (RECORD_TYPE, underlying_class)));
3367 #ifdef OBJCPLUS
3368 pop_lang_context ();
3369 #endif
3370 objc_map_put (alias_name_map, alias_ident, underlying_class);
3374 void
3375 objc_declare_class (tree identifier)
3377 #ifdef OBJCPLUS
3378 if (current_namespace != global_namespace) {
3379 error ("Objective-C declarations may only appear in global scope");
3381 #endif /* OBJCPLUS */
3383 if (! objc_is_class_name (identifier))
3385 tree record = lookup_name (identifier), type = record;
3387 if (record)
3389 if (TREE_CODE (record) == TYPE_DECL)
3390 type = DECL_ORIGINAL_TYPE (record)
3391 ? DECL_ORIGINAL_TYPE (record)
3392 : TREE_TYPE (record);
3394 if (!TYPE_HAS_OBJC_INFO (type)
3395 || !TYPE_OBJC_INTERFACE (type))
3397 error ("%qE redeclared as different kind of symbol",
3398 identifier);
3399 error ("previous declaration of %q+D",
3400 record);
3404 record = xref_tag (RECORD_TYPE, identifier);
3405 INIT_TYPE_OBJC_INFO (record);
3406 /* In the case of a @class declaration, we store the ident in
3407 the TYPE_OBJC_INTERFACE. If later an @interface is found,
3408 we'll replace the ident with the interface. */
3409 TYPE_OBJC_INTERFACE (record) = identifier;
3410 objc_map_put (class_name_map, identifier, NULL_TREE);
3414 tree
3415 objc_is_class_name (tree ident)
3417 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE)
3419 tree t = identifier_global_value (ident);
3420 if (t)
3421 ident = t;
3424 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
3425 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
3427 if (ident && TREE_CODE (ident) == RECORD_TYPE)
3428 ident = OBJC_TYPE_NAME (ident);
3429 #ifdef OBJCPLUS
3430 if (ident && TREE_CODE (ident) == TYPE_DECL)
3432 tree type = TREE_TYPE (ident);
3433 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
3434 return NULL_TREE;
3435 ident = DECL_NAME (ident);
3437 #endif
3438 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
3439 return NULL_TREE;
3441 if (lookup_interface (ident))
3442 return ident;
3445 tree target;
3447 target = objc_map_get (class_name_map, ident);
3448 if (target != OBJC_MAP_NOT_FOUND)
3449 return ident;
3451 target = objc_map_get (alias_name_map, ident);
3452 if (target != OBJC_MAP_NOT_FOUND)
3453 return target;
3456 return 0;
3459 /* Check whether TYPE is either 'id' or 'Class'. */
3461 tree
3462 objc_is_id (tree type)
3464 if (type && TREE_CODE (type) == IDENTIFIER_NODE)
3466 tree t = identifier_global_value (type);
3467 if (t)
3468 type = t;
3471 if (type && TREE_CODE (type) == TYPE_DECL)
3472 type = TREE_TYPE (type);
3474 /* NB: This function may be called before the ObjC front-end has
3475 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3476 return (objc_object_type && type
3477 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
3478 ? type
3479 : NULL_TREE);
3482 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3483 class instance. This is needed by other parts of the compiler to
3484 handle ObjC types gracefully. */
3486 tree
3487 objc_is_object_ptr (tree type)
3489 tree ret;
3491 type = TYPE_MAIN_VARIANT (type);
3492 if (!POINTER_TYPE_P (type))
3493 return 0;
3495 ret = objc_is_id (type);
3496 if (!ret)
3497 ret = objc_is_class_name (TREE_TYPE (type));
3499 return ret;
3502 static int
3503 objc_is_gcable_type (tree type, int or_strong_p)
3505 tree name;
3507 if (!TYPE_P (type))
3508 return 0;
3509 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
3510 return 1;
3511 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
3512 return 1;
3513 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
3514 return 0;
3515 type = TREE_TYPE (type);
3516 if (TREE_CODE (type) != RECORD_TYPE)
3517 return 0;
3518 name = TYPE_NAME (type);
3519 return (objc_is_class_name (name) != NULL_TREE);
3522 static tree
3523 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
3525 if (expr == oldexpr)
3526 return newexpr;
3528 switch (TREE_CODE (expr))
3530 case COMPONENT_REF:
3531 return objc_build_component_ref
3532 (objc_substitute_decl (TREE_OPERAND (expr, 0),
3533 oldexpr,
3534 newexpr),
3535 DECL_NAME (TREE_OPERAND (expr, 1)));
3536 case ARRAY_REF:
3537 return build_array_ref (input_location,
3538 objc_substitute_decl (TREE_OPERAND (expr, 0),
3539 oldexpr,
3540 newexpr),
3541 TREE_OPERAND (expr, 1));
3542 case INDIRECT_REF:
3543 return build_indirect_ref (input_location,
3544 objc_substitute_decl (TREE_OPERAND (expr, 0),
3545 oldexpr,
3546 newexpr), RO_ARROW);
3547 default:
3548 return expr;
3552 static tree
3553 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
3555 tree func_params;
3556 /* The LHS parameter contains the expression 'outervar->memberspec';
3557 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3558 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3560 tree offs
3561 = objc_substitute_decl
3562 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
3563 tree func
3564 = (flag_objc_direct_dispatch
3565 ? objc_assign_ivar_fast_decl
3566 : objc_assign_ivar_decl);
3568 offs = convert (integer_type_node, build_unary_op (input_location,
3569 ADDR_EXPR, offs, 0));
3570 offs = fold (offs);
3571 func_params = tree_cons (NULL_TREE,
3572 convert (objc_object_type, rhs),
3573 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
3574 tree_cons (NULL_TREE, offs,
3575 NULL_TREE)));
3577 return build_function_call (input_location, func, func_params);
3580 static tree
3581 objc_build_global_assignment (tree lhs, tree rhs)
3583 tree func_params = tree_cons (NULL_TREE,
3584 convert (objc_object_type, rhs),
3585 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3586 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3587 NULL_TREE));
3589 return build_function_call (input_location,
3590 objc_assign_global_decl, func_params);
3593 static tree
3594 objc_build_strong_cast_assignment (tree lhs, tree rhs)
3596 tree func_params = tree_cons (NULL_TREE,
3597 convert (objc_object_type, rhs),
3598 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3599 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3600 NULL_TREE));
3602 return build_function_call (input_location,
3603 objc_assign_strong_cast_decl, func_params);
3606 static int
3607 objc_is_gcable_p (tree expr)
3609 return (TREE_CODE (expr) == COMPONENT_REF
3610 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
3611 : TREE_CODE (expr) == ARRAY_REF
3612 ? (objc_is_gcable_p (TREE_TYPE (expr))
3613 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
3614 : TREE_CODE (expr) == ARRAY_TYPE
3615 ? objc_is_gcable_p (TREE_TYPE (expr))
3616 : TYPE_P (expr)
3617 ? objc_is_gcable_type (expr, 1)
3618 : (objc_is_gcable_p (TREE_TYPE (expr))
3619 || (DECL_P (expr)
3620 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
3623 static int
3624 objc_is_ivar_reference_p (tree expr)
3626 return (TREE_CODE (expr) == ARRAY_REF
3627 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
3628 : TREE_CODE (expr) == COMPONENT_REF
3629 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
3630 : 0);
3633 static int
3634 objc_is_global_reference_p (tree expr)
3636 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
3637 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
3638 : DECL_P (expr)
3639 ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr))
3640 : 0);
3643 tree
3644 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
3646 tree result = NULL_TREE, outer;
3647 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
3649 /* This function is currently only used with the next runtime with
3650 garbage collection enabled (-fobjc-gc). */
3651 gcc_assert (flag_next_runtime);
3653 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3654 will have been transformed to the form '*(type *)&expr'. */
3655 if (TREE_CODE (lhs) == INDIRECT_REF)
3657 outer = TREE_OPERAND (lhs, 0);
3659 while (!strong_cast_p
3660 && (CONVERT_EXPR_P (outer)
3661 || TREE_CODE (outer) == NON_LVALUE_EXPR))
3663 tree lhstype = TREE_TYPE (outer);
3665 /* Descend down the cast chain, and record the first objc_gc
3666 attribute found. */
3667 if (POINTER_TYPE_P (lhstype))
3669 tree attr
3670 = lookup_attribute ("objc_gc",
3671 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
3673 if (attr)
3674 strong_cast_p = 1;
3677 outer = TREE_OPERAND (outer, 0);
3681 /* If we have a __strong cast, it trumps all else. */
3682 if (strong_cast_p)
3684 if (modifycode != NOP_EXPR)
3685 goto invalid_pointer_arithmetic;
3687 if (warn_assign_intercept)
3688 warning (0, "strong-cast assignment has been intercepted");
3690 result = objc_build_strong_cast_assignment (lhs, rhs);
3692 goto exit_point;
3695 /* the lhs must be of a suitable type, regardless of its underlying
3696 structure. */
3697 if (!objc_is_gcable_p (lhs))
3698 goto exit_point;
3700 outer = lhs;
3702 while (outer
3703 && (TREE_CODE (outer) == COMPONENT_REF
3704 || TREE_CODE (outer) == ARRAY_REF))
3705 outer = TREE_OPERAND (outer, 0);
3707 if (TREE_CODE (outer) == INDIRECT_REF)
3709 outer = TREE_OPERAND (outer, 0);
3710 indirect_p = 1;
3713 outer_gc_p = objc_is_gcable_p (outer);
3715 /* Handle ivar assignments. */
3716 if (objc_is_ivar_reference_p (lhs))
3718 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3719 doesn't cut it here), the best we can do here is suggest a cast. */
3720 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
3722 /* We may still be able to use the global write barrier... */
3723 if (!indirect_p && objc_is_global_reference_p (outer))
3724 goto global_reference;
3726 suggest_cast:
3727 if (modifycode == NOP_EXPR)
3729 if (warn_assign_intercept)
3730 warning (0, "strong-cast may possibly be needed");
3733 goto exit_point;
3736 if (modifycode != NOP_EXPR)
3737 goto invalid_pointer_arithmetic;
3739 if (warn_assign_intercept)
3740 warning (0, "instance variable assignment has been intercepted");
3742 result = objc_build_ivar_assignment (outer, lhs, rhs);
3744 goto exit_point;
3747 /* Likewise, intercept assignment to global/static variables if their type is
3748 GC-marked. */
3749 if (objc_is_global_reference_p (outer))
3751 if (indirect_p)
3752 goto suggest_cast;
3754 global_reference:
3755 if (modifycode != NOP_EXPR)
3757 invalid_pointer_arithmetic:
3758 if (outer_gc_p)
3759 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3761 goto exit_point;
3764 if (warn_assign_intercept)
3765 warning (0, "global/static variable assignment has been intercepted");
3767 result = objc_build_global_assignment (lhs, rhs);
3770 /* In all other cases, fall back to the normal mechanism. */
3771 exit_point:
3772 return result;
3775 /* Implementation of the table mapping a class name (as an identifier)
3776 to a class node. The two public functions for it are
3777 lookup_interface() and add_interface(). add_interface() is only
3778 used in this file, so we can make it static. */
3780 static GTY(()) objc_map_t interface_map;
3782 static void
3783 interface_hash_init (void)
3785 interface_map = objc_map_alloc_ggc (200);
3788 static tree
3789 add_interface (tree class_name, tree name)
3791 /* Put interfaces on list in reverse order. */
3792 TREE_CHAIN (class_name) = interface_chain;
3793 interface_chain = class_name;
3795 /* Add it to the map. */
3796 objc_map_put (interface_map, name, class_name);
3798 return interface_chain;
3801 tree
3802 lookup_interface (tree ident)
3804 #ifdef OBJCPLUS
3805 if (ident && TREE_CODE (ident) == TYPE_DECL)
3806 ident = DECL_NAME (ident);
3807 #endif
3809 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
3810 return NULL_TREE;
3813 tree interface = objc_map_get (interface_map, ident);
3815 if (interface == OBJC_MAP_NOT_FOUND)
3816 return NULL_TREE;
3817 else
3818 return interface;
3824 /* Implement @defs (<classname>) within struct bodies. */
3826 tree
3827 objc_get_class_ivars (tree class_name)
3829 tree interface = lookup_interface (class_name);
3831 if (interface)
3832 return get_class_ivars (interface, true);
3834 error ("cannot find interface declaration for %qE",
3835 class_name);
3837 return error_mark_node;
3841 /* Functions used by the hashtable for field duplicates in
3842 objc_detect_field_duplicates(). Ideally, we'd use a standard
3843 key-value dictionary hashtable , and store as keys the field names,
3844 and as values the actual declarations (used to print nice error
3845 messages with the locations). But, the hashtable we are using only
3846 allows us to store keys in the hashtable, without values (it looks
3847 more like a set). So, we store the DECLs, but define equality as
3848 DECLs having the same name, and hash as the hash of the name. */
3850 struct decl_name_hash : typed_noop_remove <tree_node>
3852 typedef tree_node value_type;
3853 typedef tree_node compare_type;
3854 static inline hashval_t hash (const value_type *);
3855 static inline bool equal (const value_type *, const compare_type *);
3858 inline hashval_t
3859 decl_name_hash::hash (const value_type *q)
3861 return (hashval_t) ((intptr_t)(DECL_NAME (q)) >> 3);
3864 inline bool
3865 decl_name_hash::equal (const value_type *a, const compare_type *b)
3867 return DECL_NAME (a) == DECL_NAME (b);
3870 /* Called when checking the variables in a struct. If we are not
3871 doing the ivars list inside an @interface context, then return
3872 false. Else, perform the check for duplicate ivars, then return
3873 true. The check for duplicates checks if an instance variable with
3874 the same name exists in the class or in a superclass. If
3875 'check_superclasses_only' is set to true, then it is assumed that
3876 checks for instance variables in the same class has already been
3877 performed (this is the case for ObjC++) and only the instance
3878 variables of superclasses are checked. */
3879 bool
3880 objc_detect_field_duplicates (bool check_superclasses_only)
3882 if (!objc_collecting_ivars || !objc_interface_context
3883 || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE)
3884 return false;
3886 /* We have two ways of doing this check:
3888 "direct comparison": we iterate over the instance variables and
3889 compare them directly. This works great for small numbers of
3890 instance variables (such as 10 or 20), which are extremely common.
3891 But it will potentially take forever for the pathological case with
3892 a huge number (eg, 10k) of instance variables.
3894 "hashtable": we use a hashtable, which requires a single sweep
3895 through the list of instances variables. This is much slower for a
3896 small number of variables, and we only use it for large numbers.
3898 To decide which one to use, we need to get an idea of how many
3899 instance variables we have to compare. */
3901 unsigned int number_of_ivars_to_check = 0;
3903 tree ivar;
3904 for (ivar = CLASS_RAW_IVARS (objc_interface_context);
3905 ivar; ivar = DECL_CHAIN (ivar))
3907 /* Ignore anonymous ivars. */
3908 if (DECL_NAME (ivar))
3909 number_of_ivars_to_check++;
3913 /* Exit if there is nothing to do. */
3914 if (number_of_ivars_to_check == 0)
3915 return true;
3917 /* In case that there are only 1 or 2 instance variables to check,
3918 we always use direct comparison. If there are more, it is
3919 worth iterating over the instance variables in the superclass
3920 to count how many there are (note that this has the same cost
3921 as checking 1 instance variable by direct comparison, which is
3922 why we skip this check in the case of 1 or 2 ivars and just do
3923 the direct comparison) and then decide if it worth using a
3924 hashtable. */
3925 if (number_of_ivars_to_check > 2)
3927 unsigned int number_of_superclass_ivars = 0;
3929 tree interface;
3930 for (interface = lookup_interface (CLASS_SUPER_NAME (objc_interface_context));
3931 interface; interface = lookup_interface (CLASS_SUPER_NAME (interface)))
3933 tree ivar;
3934 for (ivar = CLASS_RAW_IVARS (interface);
3935 ivar; ivar = DECL_CHAIN (ivar))
3936 number_of_superclass_ivars++;
3940 /* We use a hashtable if we have over 10k comparisons. */
3941 if (number_of_ivars_to_check * (number_of_superclass_ivars
3942 + (number_of_ivars_to_check / 2))
3943 > 10000)
3945 /* First, build the hashtable by putting all the instance
3946 variables of superclasses in it. */
3947 hash_table<decl_name_hash> htab (37);
3948 tree interface;
3949 for (interface = lookup_interface (CLASS_SUPER_NAME
3950 (objc_interface_context));
3951 interface; interface = lookup_interface
3952 (CLASS_SUPER_NAME (interface)))
3954 tree ivar;
3955 for (ivar = CLASS_RAW_IVARS (interface); ivar;
3956 ivar = DECL_CHAIN (ivar))
3958 if (DECL_NAME (ivar) != NULL_TREE)
3960 tree_node **slot = htab.find_slot (ivar, INSERT);
3961 /* Do not check for duplicate instance
3962 variables in superclasses. Errors have
3963 already been generated. */
3964 *slot = ivar;
3969 /* Now, we go through all the instance variables in the
3970 class, and check that they are not in the
3971 hashtable. */
3972 if (check_superclasses_only)
3974 tree ivar;
3975 for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar;
3976 ivar = DECL_CHAIN (ivar))
3978 if (DECL_NAME (ivar) != NULL_TREE)
3980 tree duplicate_ivar = htab.find (ivar);
3981 if (duplicate_ivar != HTAB_EMPTY_ENTRY)
3983 error_at (DECL_SOURCE_LOCATION (ivar),
3984 "duplicate instance variable %q+D",
3985 ivar);
3986 inform (DECL_SOURCE_LOCATION (duplicate_ivar),
3987 "previous declaration of %q+D",
3988 duplicate_ivar);
3989 /* FIXME: Do we need the following ? */
3990 /* DECL_NAME (ivar) = NULL_TREE; */
3995 else
3997 /* If we're checking for duplicates in the class as
3998 well, we insert variables in the hashtable as we
3999 check them, so if a duplicate follows, it will be
4000 caught. */
4001 tree ivar;
4002 for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar;
4003 ivar = DECL_CHAIN (ivar))
4005 if (DECL_NAME (ivar) != NULL_TREE)
4007 tree_node **slot = htab.find_slot (ivar, INSERT);
4008 if (*slot)
4010 tree duplicate_ivar = (tree)(*slot);
4011 error_at (DECL_SOURCE_LOCATION (ivar),
4012 "duplicate instance variable %q+D",
4013 ivar);
4014 inform (DECL_SOURCE_LOCATION (duplicate_ivar),
4015 "previous declaration of %q+D",
4016 duplicate_ivar);
4017 /* FIXME: Do we need the following ? */
4018 /* DECL_NAME (ivar) = NULL_TREE; */
4020 *slot = ivar;
4024 return true;
4029 /* This is the "direct comparison" approach, which is used in most
4030 non-pathological cases. */
4032 /* Walk up to class hierarchy, starting with this class (this is
4033 the external loop, because lookup_interface() is expensive, and
4034 we want to do it few times). */
4035 tree interface = objc_interface_context;
4037 if (check_superclasses_only)
4038 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4040 for ( ; interface; interface = lookup_interface
4041 (CLASS_SUPER_NAME (interface)))
4043 tree ivar_being_checked;
4045 for (ivar_being_checked = CLASS_RAW_IVARS (objc_interface_context);
4046 ivar_being_checked;
4047 ivar_being_checked = DECL_CHAIN (ivar_being_checked))
4049 tree decl;
4051 /* Ignore anonymous ivars. */
4052 if (DECL_NAME (ivar_being_checked) == NULL_TREE)
4053 continue;
4055 /* Note how we stop when we find the ivar we are checking
4056 (this can only happen in the main class, not
4057 superclasses), to avoid comparing things twice
4058 (otherwise, for each ivar, you'd compare A to B then B
4059 to A, and get duplicated error messages). */
4060 for (decl = CLASS_RAW_IVARS (interface);
4061 decl && decl != ivar_being_checked;
4062 decl = DECL_CHAIN (decl))
4064 if (DECL_NAME (ivar_being_checked) == DECL_NAME (decl))
4066 error_at (DECL_SOURCE_LOCATION (ivar_being_checked),
4067 "duplicate instance variable %q+D",
4068 ivar_being_checked);
4069 inform (DECL_SOURCE_LOCATION (decl),
4070 "previous declaration of %q+D",
4071 decl);
4072 /* FIXME: Do we need the following ? */
4073 /* DECL_NAME (ivar_being_checked) = NULL_TREE; */
4079 return true;
4082 /* Used by: build_private_template, continue_class,
4083 and for @defs constructs. */
4085 static tree
4086 get_class_ivars (tree interface, bool inherited)
4088 tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface));
4090 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
4091 by the current class (i.e., they do not include super-class ivars).
4092 However, the CLASS_IVARS list will be side-effected by a call to
4093 finish_struct(), which will fill in field offsets. */
4094 if (!CLASS_IVARS (interface))
4095 CLASS_IVARS (interface) = ivar_chain;
4097 if (!inherited)
4098 return ivar_chain;
4100 while (CLASS_SUPER_NAME (interface))
4102 /* Prepend super-class ivars. */
4103 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4104 ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)),
4105 ivar_chain);
4108 return ivar_chain;
4111 void
4112 objc_maybe_warn_exceptions (location_t loc)
4114 /* -fobjc-exceptions is required to enable Objective-C exceptions.
4115 For example, on Darwin, ObjC exceptions require a sufficiently
4116 recent version of the runtime, so the user must ask for them
4117 explicitly. On other platforms, at the moment -fobjc-exceptions
4118 triggers -fexceptions which again is required for exceptions to
4119 work. */
4120 if (!flag_objc_exceptions)
4122 /* Warn only once per compilation unit. */
4123 static bool warned = false;
4125 if (!warned)
4127 error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4128 warned = true;
4133 static struct objc_try_context *cur_try_context;
4135 /* Called just after parsing the @try and its associated BODY. We now
4136 must prepare for the tricky bits -- handling the catches and finally. */
4138 void
4139 objc_begin_try_stmt (location_t try_locus, tree body)
4141 struct objc_try_context *c = XCNEW (struct objc_try_context);
4142 c->outer = cur_try_context;
4143 c->try_body = body;
4144 c->try_locus = try_locus;
4145 c->end_try_locus = input_location;
4146 cur_try_context = c;
4148 /* Collect the list of local variables. We'll mark them as volatile
4149 at the end of compilation of this function to prevent them being
4150 clobbered by setjmp/longjmp. */
4151 if (flag_objc_sjlj_exceptions)
4152 objc_mark_locals_volatile (NULL);
4155 /* Called just after parsing "@catch (parm)". Open a binding level,
4156 enter DECL into the binding level, and initialize it. Leave the
4157 binding level open while the body of the compound statement is
4158 parsed. If DECL is NULL_TREE, then we are compiling "@catch(...)"
4159 which we compile as "@catch(id tmp_variable)". */
4161 void
4162 objc_begin_catch_clause (tree decl)
4164 tree compound, type, t;
4165 bool ellipsis = false;
4167 /* Begin a new scope that the entire catch clause will live in. */
4168 compound = c_begin_compound_stmt (true);
4170 /* Create the appropriate declaration for the argument. */
4171 if (decl == error_mark_node)
4172 type = error_mark_node;
4173 else
4175 if (decl == NULL_TREE)
4177 /* If @catch(...) was specified, create a temporary variable of
4178 type 'id' and use it. */
4179 decl = objc_create_temporary_var (objc_object_type, "__objc_generic_catch_var");
4180 DECL_SOURCE_LOCATION (decl) = input_location;
4181 /* ... but allow the runtime to differentiate between ellipsis and the
4182 case of @catch (id xyz). */
4183 ellipsis = true;
4185 else
4187 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
4188 decl = build_decl (input_location,
4189 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
4191 lang_hooks.decls.pushdecl (decl);
4193 /* Mark the declaration as used so you never any warnings whether
4194 you use the exception argument or not. TODO: Implement a
4195 -Wunused-exception-parameter flag, which would cause warnings
4196 if exception parameter is not used. */
4197 TREE_USED (decl) = 1;
4198 DECL_READ_P (decl) = 1;
4200 type = TREE_TYPE (decl);
4203 /* Verify that the type of the catch is valid. It must be a pointer
4204 to an Objective-C class, or "id" (which is catch-all). */
4205 if (type == error_mark_node)
4207 ;/* Just keep going. */
4209 else if (!objc_type_valid_for_messaging (type, false))
4211 error ("@catch parameter is not a known Objective-C class type");
4212 type = error_mark_node;
4214 else if (TYPE_HAS_OBJC_INFO (TREE_TYPE (type))
4215 && TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (type)))
4217 error ("@catch parameter can not be protocol-qualified");
4218 type = error_mark_node;
4220 else if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type)))
4221 /* @catch (id xyz) or @catch (...) but we note this for runtimes that
4222 identify 'id'. */
4224 else
4226 /* If 'type' was built using typedefs, we need to get rid of
4227 them and get a simple pointer to the class. */
4228 bool is_typedef = false;
4229 tree x = TYPE_MAIN_VARIANT (type);
4231 /* Skip from the pointer to the pointee. */
4232 if (TREE_CODE (x) == POINTER_TYPE)
4233 x = TREE_TYPE (x);
4235 /* Traverse typedef aliases */
4236 while (TREE_CODE (x) == RECORD_TYPE && OBJC_TYPE_NAME (x)
4237 && TREE_CODE (OBJC_TYPE_NAME (x)) == TYPE_DECL
4238 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x)))
4240 is_typedef = true;
4241 x = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x));
4244 /* If it was a typedef, build a pointer to the final, original
4245 class. */
4246 if (is_typedef)
4247 type = build_pointer_type (x);
4249 if (cur_try_context->catch_list)
4251 /* Examine previous @catch clauses and see if we've already
4252 caught the type in question. */
4253 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
4254 for (; !tsi_end_p (i); tsi_next (&i))
4256 tree stmt = tsi_stmt (i);
4257 t = CATCH_TYPES (stmt);
4258 if (t == error_mark_node)
4259 continue;
4260 if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type)))
4262 warning (0, "exception of type %<%T%> will be caught",
4263 TREE_TYPE (type));
4264 warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>",
4265 TREE_TYPE (t ? t : objc_object_type));
4266 break;
4272 t = (*runtime.begin_catch) (&cur_try_context, type, decl, compound, ellipsis);
4273 add_stmt (t);
4276 /* Called just after parsing the closing brace of a @catch clause. Close
4277 the open binding level, and record a CATCH_EXPR for it. */
4279 void
4280 objc_finish_catch_clause (void)
4282 tree c = cur_try_context->current_catch;
4283 cur_try_context->current_catch = NULL;
4284 cur_try_context->end_catch_locus = input_location;
4286 CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1);
4288 (*runtime.finish_catch) (&cur_try_context, c);
4291 /* Called after parsing a @finally clause and its associated BODY.
4292 Record the body for later placement. */
4294 void
4295 objc_build_finally_clause (location_t finally_locus, tree body)
4297 cur_try_context->finally_body = body;
4298 cur_try_context->finally_locus = finally_locus;
4299 cur_try_context->end_finally_locus = input_location;
4302 /* Called to finalize a @try construct. */
4304 tree
4305 objc_finish_try_stmt (void)
4307 struct objc_try_context *c = cur_try_context;
4308 tree stmt;
4310 if (c->catch_list == NULL && c->finally_body == NULL)
4311 error ("%<@try%> without %<@catch%> or %<@finally%>");
4313 stmt = (*runtime.finish_try_stmt) (&cur_try_context);
4314 add_stmt (stmt);
4316 cur_try_context = c->outer;
4317 free (c);
4318 return stmt;
4321 tree
4322 objc_build_throw_stmt (location_t loc, tree throw_expr)
4324 bool rethrown = false;
4326 objc_maybe_warn_exceptions (loc);
4328 /* Don't waste time trying to build something if we're already dead. */
4329 if (throw_expr == error_mark_node)
4330 return error_mark_node;
4332 if (throw_expr == NULL)
4334 /* If we're not inside a @catch block, there is no "current
4335 exception" to be rethrown. */
4336 if (cur_try_context == NULL
4337 || cur_try_context->current_catch == NULL)
4339 error_at (loc, "%<@throw%> (rethrow) used outside of a @catch block");
4340 return error_mark_node;
4343 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
4344 value that we get from the runtime. */
4345 throw_expr = (*runtime.build_exc_ptr) (&cur_try_context);
4346 rethrown = true;
4348 else
4350 if (!objc_type_valid_for_messaging (TREE_TYPE (throw_expr), true))
4352 error_at (loc, "%<@throw%> argument is not an object");
4353 return error_mark_node;
4357 return (*runtime.build_throw_stmt) (loc, throw_expr, rethrown);
4360 tree
4361 objc_build_synchronized (location_t start_locus, tree object_expr, tree body)
4363 /* object_expr should never be NULL; but in case it is, convert it to
4364 error_mark_node. */
4365 if (object_expr == NULL)
4366 object_expr = error_mark_node;
4368 /* Validate object_expr. If not valid, set it to error_mark_node. */
4369 if (object_expr != error_mark_node)
4371 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expr), true))
4373 error_at (start_locus, "%<@synchronized%> argument is not an object");
4374 object_expr = error_mark_node;
4378 if (object_expr == error_mark_node)
4380 /* If we found an error, we simply ignore the '@synchronized'.
4381 Compile the body so we can keep going with minimal
4382 casualties. */
4383 return add_stmt (body);
4385 else
4387 tree call;
4388 tree args;
4390 /* objc_sync_enter (object_expr); */
4391 object_expr = save_expr (object_expr);
4392 args = tree_cons (NULL, object_expr, NULL);
4393 call = build_function_call (input_location,
4394 objc_sync_enter_decl, args);
4395 SET_EXPR_LOCATION (call, start_locus);
4396 add_stmt (call);
4398 /* Build "objc_sync_exit (object_expr);" but do not add it yet;
4399 it goes inside the @finalize() clause. */
4400 args = tree_cons (NULL, object_expr, NULL);
4401 call = build_function_call (input_location,
4402 objc_sync_exit_decl, args);
4403 SET_EXPR_LOCATION (call, input_location);
4405 /* @try { body; } */
4406 objc_begin_try_stmt (start_locus, body);
4408 /* @finally { objc_sync_exit (object_expr); } */
4409 objc_build_finally_clause (input_location, call);
4411 /* End of try statement. */
4412 return objc_finish_try_stmt ();
4416 /* Construct a C struct corresponding to ObjC class CLASS, with the same
4417 name as the class:
4419 struct <classname> {
4420 struct _objc_class *isa;
4422 }; */
4424 static void
4425 build_private_template (tree klass)
4427 if (!CLASS_STATIC_TEMPLATE (klass))
4429 tree record = objc_build_struct (klass,
4430 get_class_ivars (klass, false),
4431 CLASS_SUPER_NAME (klass));
4433 /* Set the TREE_USED bit for this struct, so that stab generator
4434 can emit stabs for this struct type. */
4435 if (flag_debug_only_used_symbols && TYPE_STUB_DECL (record))
4436 TREE_USED (TYPE_STUB_DECL (record)) = 1;
4438 /* Copy the attributes from the class to the type. */
4439 if (TREE_DEPRECATED (klass))
4440 TREE_DEPRECATED (record) = 1;
4444 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
4445 current class. */
4446 #ifdef OBJCPLUS
4447 static void
4448 objc_generate_cxx_ctor_or_dtor (bool dtor)
4450 tree fn, body, compound_stmt, ivar;
4452 /* - (id) .cxx_construct { ... return self; } */
4453 /* - (void) .cxx_construct { ... } */
4455 objc_start_method_definition
4456 (false /* is_class_method */,
4457 objc_build_method_signature (false /* is_class_method */,
4458 build_tree_list (NULL_TREE,
4459 dtor
4460 ? void_type_node
4461 : objc_object_type),
4462 get_identifier (dtor
4463 ? TAG_CXX_DESTRUCT
4464 : TAG_CXX_CONSTRUCT),
4465 make_node (TREE_LIST),
4466 false), NULL, NULL_TREE);
4467 body = begin_function_body ();
4468 compound_stmt = begin_compound_stmt (0);
4470 ivar = CLASS_IVARS (implementation_template);
4471 /* Destroy ivars in reverse order. */
4472 if (dtor)
4473 ivar = nreverse (copy_list (ivar));
4475 for (; ivar; ivar = TREE_CHAIN (ivar))
4477 if (TREE_CODE (ivar) == FIELD_DECL)
4479 tree type = TREE_TYPE (ivar);
4481 /* Call the ivar's default constructor or destructor. Do not
4482 call the destructor unless a corresponding constructor call
4483 has also been made (or is not needed). */
4484 if (MAYBE_CLASS_TYPE_P (type)
4485 && (dtor
4486 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4487 && (!TYPE_NEEDS_CONSTRUCTING (type)
4488 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4489 : (TYPE_NEEDS_CONSTRUCTING (type)
4490 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))))
4491 finish_expr_stmt
4492 (build_special_member_call
4493 (build_ivar_reference (DECL_NAME (ivar)),
4494 dtor ? complete_dtor_identifier : complete_ctor_identifier,
4495 NULL, type, LOOKUP_NORMAL, tf_warning_or_error));
4499 /* The constructor returns 'self'. */
4500 if (!dtor)
4501 finish_return_stmt (self_decl);
4503 finish_compound_stmt (compound_stmt);
4504 finish_function_body (body);
4505 fn = current_function_decl;
4506 finish_function ();
4507 objc_finish_method_definition (fn);
4510 /* The following routine will examine the current @interface for any
4511 non-POD C++ ivars requiring non-trivial construction and/or
4512 destruction, and then synthesize special '- .cxx_construct' and/or
4513 '- .cxx_destruct' methods which will run the appropriate
4514 construction or destruction code. Note that ivars inherited from
4515 super-classes are _not_ considered. */
4516 static void
4517 objc_generate_cxx_cdtors (void)
4519 bool need_ctor = false, need_dtor = false;
4520 tree ivar;
4522 /* Error case, due to possibly an extra @end. */
4523 if (!objc_implementation_context)
4524 return;
4526 /* We do not want to do this for categories, since they do not have
4527 their own ivars. */
4529 if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE)
4530 return;
4532 /* First, determine if we even need a constructor and/or destructor. */
4534 for (ivar = CLASS_IVARS (implementation_template); ivar;
4535 ivar = TREE_CHAIN (ivar))
4537 if (TREE_CODE (ivar) == FIELD_DECL)
4539 tree type = TREE_TYPE (ivar);
4541 if (MAYBE_CLASS_TYPE_P (type))
4543 if (TYPE_NEEDS_CONSTRUCTING (type)
4544 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
4545 /* NB: If a default constructor is not available, we will not
4546 be able to initialize this ivar; the add_instance_variable()
4547 routine will already have warned about this. */
4548 need_ctor = true;
4550 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4551 && (!TYPE_NEEDS_CONSTRUCTING (type)
4552 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4553 /* NB: If a default constructor is not available, we will not
4554 call the destructor either, for symmetry. */
4555 need_dtor = true;
4560 /* Generate '- .cxx_construct' if needed. */
4562 if (need_ctor)
4563 objc_generate_cxx_ctor_or_dtor (false);
4565 /* Generate '- .cxx_destruct' if needed. */
4567 if (need_dtor)
4568 objc_generate_cxx_ctor_or_dtor (true);
4570 /* The 'imp_list' variable points at an imp_entry record for the current
4571 @implementation. Record the existence of '- .cxx_construct' and/or
4572 '- .cxx_destruct' methods therein; it will be included in the
4573 metadata for the class if the runtime needs it. */
4574 imp_list->has_cxx_cdtors = (need_ctor || need_dtor);
4576 #endif
4578 static void
4579 error_with_ivar (const char *message, tree decl)
4581 error_at (DECL_SOURCE_LOCATION (decl), "%s %qs",
4582 message, identifier_to_locale (gen_declaration (decl)));
4586 static void
4587 check_ivars (tree inter, tree imp)
4589 tree intdecls = CLASS_RAW_IVARS (inter);
4590 tree impdecls = CLASS_RAW_IVARS (imp);
4592 while (1)
4594 tree t1, t2;
4596 #ifdef OBJCPLUS
4597 if (intdecls && TREE_CODE (intdecls) == TYPE_DECL)
4598 intdecls = TREE_CHAIN (intdecls);
4599 #endif
4600 if (intdecls == 0 && impdecls == 0)
4601 break;
4602 if (intdecls == 0 || impdecls == 0)
4604 error ("inconsistent instance variable specification");
4605 break;
4608 t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
4610 if (!comptypes (t1, t2)
4611 || !tree_int_cst_equal (DECL_INITIAL (intdecls),
4612 DECL_INITIAL (impdecls)))
4614 if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
4616 error_with_ivar ("conflicting instance variable type",
4617 impdecls);
4618 error_with_ivar ("previous declaration of",
4619 intdecls);
4621 else /* both the type and the name don't match */
4623 error ("inconsistent instance variable specification");
4624 break;
4628 else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
4630 error_with_ivar ("conflicting instance variable name",
4631 impdecls);
4632 error_with_ivar ("previous declaration of",
4633 intdecls);
4636 intdecls = DECL_CHAIN (intdecls);
4637 impdecls = DECL_CHAIN (impdecls);
4642 static void
4643 mark_referenced_methods (void)
4645 struct imp_entry *impent;
4646 tree chain;
4648 for (impent = imp_list; impent; impent = impent->next)
4650 chain = CLASS_CLS_METHODS (impent->imp_context);
4651 while (chain)
4653 cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output ();
4654 chain = DECL_CHAIN (chain);
4657 chain = CLASS_NST_METHODS (impent->imp_context);
4658 while (chain)
4660 cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output ();
4661 chain = DECL_CHAIN (chain);
4666 /* If type is empty or only type qualifiers are present, add default
4667 type of id (otherwise grokdeclarator will default to int). */
4668 static inline tree
4669 adjust_type_for_id_default (tree type)
4671 if (!type)
4672 type = make_node (TREE_LIST);
4674 if (!TREE_VALUE (type))
4675 TREE_VALUE (type) = objc_object_type;
4676 else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE
4677 && TYPED_OBJECT (TREE_VALUE (type)))
4678 error ("can not use an object as parameter to a method");
4680 return type;
4683 /* Return a KEYWORD_DECL built using the specified key_name, arg_type,
4684 arg_name and attributes. (TODO: Rename KEYWORD_DECL to
4685 OBJC_METHOD_PARM_DECL ?)
4687 A KEYWORD_DECL is a tree representing the declaration of a
4688 parameter of an Objective-C method. It is produced when parsing a
4689 fragment of Objective-C method declaration of the form
4691 keyworddecl:
4692 selector ':' '(' typename ')' identifier
4694 For example, take the Objective-C method
4696 -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type;
4698 the two fragments "pathForResource:(NSString *)resource" and
4699 "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The
4700 KEYWORD_DECL stores the 'key_name' (eg, identifier for
4701 "pathForResource"), the 'arg_type' (eg, tree representing a
4702 NSString *), the 'arg_name' (eg identifier for "resource") and
4703 potentially some attributes (for example, a tree representing
4704 __attribute__ ((unused)) if such an attribute was attached to a
4705 certain parameter). You can access this information using the
4706 TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name),
4707 KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes).
4709 'key_name' is an identifier node (and is optional as you can omit
4710 it in Objective-C methods).
4711 'arg_type' is a tree list (and is optional too if no parameter type
4712 was specified).
4713 'arg_name' is an identifier node and is required.
4714 'attributes' is an optional tree containing parameter attributes. */
4715 tree
4716 objc_build_keyword_decl (tree key_name, tree arg_type,
4717 tree arg_name, tree attributes)
4719 tree keyword_decl;
4721 if (flag_objc1_only && attributes)
4722 error_at (input_location, "method argument attributes are not available in Objective-C 1.0");
4724 /* If no type is specified, default to "id". */
4725 arg_type = adjust_type_for_id_default (arg_type);
4727 keyword_decl = make_node (KEYWORD_DECL);
4729 TREE_TYPE (keyword_decl) = arg_type;
4730 KEYWORD_ARG_NAME (keyword_decl) = arg_name;
4731 KEYWORD_KEY_NAME (keyword_decl) = key_name;
4732 DECL_ATTRIBUTES (keyword_decl) = attributes;
4734 return keyword_decl;
4737 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
4738 static tree
4739 build_keyword_selector (tree selector)
4741 int len = 0;
4742 tree key_chain, key_name;
4743 char *buf;
4745 /* Scan the selector to see how much space we'll need. */
4746 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
4748 switch (TREE_CODE (selector))
4750 case KEYWORD_DECL:
4751 key_name = KEYWORD_KEY_NAME (key_chain);
4752 break;
4753 case TREE_LIST:
4754 key_name = TREE_PURPOSE (key_chain);
4755 break;
4756 default:
4757 gcc_unreachable ();
4760 if (key_name)
4761 len += IDENTIFIER_LENGTH (key_name) + 1;
4762 else
4763 /* Just a ':' arg. */
4764 len++;
4767 buf = (char *) alloca (len + 1);
4768 /* Start the buffer out as an empty string. */
4769 buf[0] = '\0';
4771 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
4773 switch (TREE_CODE (selector))
4775 case KEYWORD_DECL:
4776 key_name = KEYWORD_KEY_NAME (key_chain);
4777 break;
4778 case TREE_LIST:
4779 key_name = TREE_PURPOSE (key_chain);
4780 /* The keyword decl chain will later be used as a function
4781 argument chain. Unhook the selector itself so as to not
4782 confuse other parts of the compiler. */
4783 TREE_PURPOSE (key_chain) = NULL_TREE;
4784 break;
4785 default:
4786 gcc_unreachable ();
4789 if (key_name)
4790 strcat (buf, IDENTIFIER_POINTER (key_name));
4791 strcat (buf, ":");
4794 return get_identifier_with_length (buf, len);
4797 /* Used for declarations and definitions. */
4799 static tree
4800 build_method_decl (enum tree_code code, tree ret_type, tree selector,
4801 tree add_args, bool ellipsis)
4803 tree method_decl;
4805 /* If no type is specified, default to "id". */
4806 ret_type = adjust_type_for_id_default (ret_type);
4808 /* Note how a method_decl has a TREE_TYPE which is not the function
4809 type of the function implementing the method, but only the return
4810 type of the method. We may want to change this, and store the
4811 entire function type in there (eg, it may be used to simplify
4812 dealing with attributes below). */
4813 method_decl = make_node (code);
4814 TREE_TYPE (method_decl) = ret_type;
4816 /* If we have a keyword selector, create an identifier_node that
4817 represents the full selector name (`:' included)... */
4818 if (TREE_CODE (selector) == KEYWORD_DECL)
4820 METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
4821 METHOD_SEL_ARGS (method_decl) = selector;
4822 METHOD_ADD_ARGS (method_decl) = add_args;
4823 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis;
4825 else
4827 METHOD_SEL_NAME (method_decl) = selector;
4828 METHOD_SEL_ARGS (method_decl) = NULL_TREE;
4829 METHOD_ADD_ARGS (method_decl) = NULL_TREE;
4832 return method_decl;
4835 /* This routine processes objective-c method attributes. */
4837 static void
4838 objc_decl_method_attributes (tree *node, tree attributes, int flags)
4840 /* TODO: Replace the hackery below. An idea would be to store the
4841 full function type in the method declaration (for example in
4842 TREE_TYPE) and then expose ObjC method declarations to c-family
4843 and they could deal with them by simply treating them as
4844 functions. */
4846 /* Because of the dangers in the hackery below, we filter out any
4847 attribute that we do not know about. For the ones we know about,
4848 we know that they work with the hackery. For the other ones,
4849 there is no guarantee, so we have to filter them out. */
4850 tree filtered_attributes = NULL_TREE;
4852 if (attributes)
4854 tree attribute;
4855 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
4857 tree name = TREE_PURPOSE (attribute);
4859 if (is_attribute_p ("deprecated", name)
4860 || is_attribute_p ("sentinel", name)
4861 || is_attribute_p ("noreturn", name))
4863 /* An attribute that we support; add it to the filtered
4864 attributes. */
4865 filtered_attributes = chainon (filtered_attributes,
4866 copy_node (attribute));
4868 else if (is_attribute_p ("format", name))
4870 /* "format" is special because before adding it to the
4871 filtered attributes we need to adjust the specified
4872 format by adding the hidden function parameters for
4873 an Objective-C method (self, _cmd). */
4874 tree new_attribute = copy_node (attribute);
4876 /* Check the arguments specified with the attribute, and
4877 modify them adding 2 for the two hidden arguments.
4878 Note how this differs from C++; according to the
4879 specs, C++ does not do it so you have to add the +1
4880 yourself. For Objective-C, instead, the compiler
4881 adds the +2 for you. */
4883 /* The attribute arguments have not been checked yet, so
4884 we need to be careful as they could be missing or
4885 invalid. If anything looks wrong, we skip the
4886 process and the compiler will complain about it later
4887 when it validates the attribute. */
4888 /* Check that we have at least three arguments. */
4889 if (TREE_VALUE (new_attribute)
4890 && TREE_CHAIN (TREE_VALUE (new_attribute))
4891 && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (new_attribute))))
4893 tree second_argument = TREE_CHAIN (TREE_VALUE (new_attribute));
4894 tree third_argument = TREE_CHAIN (second_argument);
4895 tree number;
4897 /* This is the second argument, the "string-index",
4898 which specifies the index of the format string
4899 argument. Add 2. */
4900 number = TREE_VALUE (second_argument);
4901 if (number
4902 && TREE_CODE (number) == INTEGER_CST
4903 && !wi::eq_p (number, 0))
4904 TREE_VALUE (second_argument)
4905 = wide_int_to_tree (TREE_TYPE (number),
4906 wi::add (number, 2));
4908 /* This is the third argument, the "first-to-check",
4909 which specifies the index of the first argument to
4910 check. This could be 0, meaning it is not available,
4911 in which case we don't need to add 2. Add 2 if not
4912 0. */
4913 number = TREE_VALUE (third_argument);
4914 if (number
4915 && TREE_CODE (number) == INTEGER_CST
4916 && !wi::eq_p (number, 0))
4917 TREE_VALUE (third_argument)
4918 = wide_int_to_tree (TREE_TYPE (number),
4919 wi::add (number, 2));
4921 filtered_attributes = chainon (filtered_attributes,
4922 new_attribute);
4924 else if (is_attribute_p ("nonnull", name))
4926 /* We need to fixup all the argument indexes by adding 2
4927 for the two hidden arguments of an Objective-C method
4928 invocation, similat to what we do above for the
4929 "format" attribute. */
4930 /* FIXME: This works great in terms of implementing the
4931 functionality, but the warnings that are produced by
4932 nonnull do mention the argument index (while the
4933 format ones don't). For example, you could get
4934 "warning: null argument where non-null required
4935 (argument 3)". Now in that message, "argument 3"
4936 includes the 2 hidden arguments; it would be much
4937 more friendly to call it "argument 1", as that would
4938 be consistent with __attribute__ ((nonnnull (1))).
4939 To do this, we'd need to have the C family code that
4940 checks the arguments know about adding/removing 2 to
4941 the argument index ... or alternatively we could
4942 maybe store the "printable" argument index in
4943 addition to the actual argument index ? Some
4944 refactoring is needed to do this elegantly. */
4945 tree new_attribute = copy_node (attribute);
4946 tree argument = TREE_VALUE (attribute);
4947 while (argument != NULL_TREE)
4949 /* Get the value of the argument and add 2. */
4950 tree number = TREE_VALUE (argument);
4951 if (number && TREE_CODE (number) == INTEGER_CST
4952 && !wi::eq_p (number, 0))
4953 TREE_VALUE (argument)
4954 = wide_int_to_tree (TREE_TYPE (number),
4955 wi::add (number, 2));
4956 argument = TREE_CHAIN (argument);
4959 filtered_attributes = chainon (filtered_attributes,
4960 new_attribute);
4962 else
4963 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
4967 if (filtered_attributes)
4969 /* This hackery changes the TREE_TYPE of the ObjC method
4970 declaration to be a function type, so that decl_attributes
4971 will treat the ObjC method as if it was a function. Some
4972 attributes (sentinel, format) will be applied to the function
4973 type, changing it in place; so after calling decl_attributes,
4974 we extract the function type attributes and store them in
4975 METHOD_TYPE_ATTRIBUTES. Some other attributes (noreturn,
4976 deprecated) are applied directly to the method declaration
4977 (by setting TREE_DEPRECATED and TREE_THIS_VOLATILE) so there
4978 is nothing to do. */
4979 tree saved_type = TREE_TYPE (*node);
4980 TREE_TYPE (*node)
4981 = build_function_type_for_method (TREE_VALUE (saved_type), *node,
4982 METHOD_REF, 0);
4983 decl_attributes (node, filtered_attributes, flags);
4984 METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node));
4985 TREE_TYPE (*node) = saved_type;
4989 bool
4990 objc_method_decl (enum tree_code opcode)
4992 return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL;
4995 /* Return a function type for METHOD with RETURN_TYPE. CONTEXT is
4996 either METHOD_DEF or METHOD_REF, indicating whether we are defining a
4997 method or calling one. SUPER_FLAG indicates whether this is a send
4998 to super; this makes a difference for the NeXT calling sequence in
4999 which the lookup and the method call are done together. If METHOD is
5000 NULL, user-defined arguments (i.e., beyond self and _cmd) shall be
5001 represented as varargs. */
5003 tree
5004 build_function_type_for_method (tree return_type, tree method,
5005 int context, bool super_flag)
5007 vec<tree, va_gc> *argtypes = make_tree_vector ();
5008 tree t, ftype;
5009 bool is_varargs = false;
5011 (*runtime.get_arg_type_list_base) (&argtypes, method, context, super_flag);
5013 /* No actual method prototype given; remaining args passed as varargs. */
5014 if (method == NULL_TREE)
5016 is_varargs = true;
5017 goto build_ftype;
5020 for (t = METHOD_SEL_ARGS (method); t; t = DECL_CHAIN (t))
5022 tree arg_type = TREE_VALUE (TREE_TYPE (t));
5024 /* Decay argument types for the underlying C function as
5025 appropriate. */
5026 arg_type = objc_decay_parm_type (arg_type);
5028 vec_safe_push (argtypes, arg_type);
5031 if (METHOD_ADD_ARGS (method))
5033 for (t = TREE_CHAIN (METHOD_ADD_ARGS (method));
5034 t; t = TREE_CHAIN (t))
5036 tree arg_type = TREE_TYPE (TREE_VALUE (t));
5038 arg_type = objc_decay_parm_type (arg_type);
5040 vec_safe_push (argtypes, arg_type);
5043 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
5044 is_varargs = true;
5047 build_ftype:
5048 if (is_varargs)
5049 ftype = build_varargs_function_type_vec (return_type, argtypes);
5050 else
5051 ftype = build_function_type_vec (return_type, argtypes);
5053 release_tree_vector (argtypes);
5054 return ftype;
5057 /* The 'method' argument is a tree; this tree could either be a single
5058 method, which is returned, or could be a TREE_VEC containing a list
5059 of methods. In that case, the first one is returned, and warnings
5060 are issued as appropriate. */
5061 static tree
5062 check_duplicates (tree method, int methods, int is_class)
5064 tree first_method;
5065 size_t i;
5067 if (method == NULL_TREE)
5068 return NULL_TREE;
5070 if (TREE_CODE (method) != TREE_VEC)
5071 return method;
5073 /* We have two or more methods with the same name but different
5074 types. */
5075 first_method = TREE_VEC_ELT (method, 0);
5077 /* But just how different are those types? If
5078 -Wno-strict-selector-match is specified, we shall not complain if
5079 the differences are solely among types with identical size and
5080 alignment. */
5081 if (!warn_strict_selector_match)
5083 for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++)
5084 if (!comp_proto_with_proto (first_method, TREE_VEC_ELT (method, i), 0))
5085 goto issue_warning;
5087 return first_method;
5090 issue_warning:
5091 if (methods)
5093 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5095 warning_at (input_location, 0,
5096 "multiple methods named %<%c%E%> found",
5097 (is_class ? '+' : '-'),
5098 METHOD_SEL_NAME (first_method));
5099 inform (DECL_SOURCE_LOCATION (first_method), "using %<%c%s%>",
5100 (type ? '-' : '+'),
5101 identifier_to_locale (gen_method_decl (first_method)));
5103 else
5105 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5107 warning_at (input_location, 0,
5108 "multiple selectors named %<%c%E%> found",
5109 (is_class ? '+' : '-'),
5110 METHOD_SEL_NAME (first_method));
5111 inform (DECL_SOURCE_LOCATION (first_method), "found %<%c%s%>",
5112 (type ? '-' : '+'),
5113 identifier_to_locale (gen_method_decl (first_method)));
5116 for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++)
5118 bool type = TREE_CODE (TREE_VEC_ELT (method, i)) == INSTANCE_METHOD_DECL;
5120 inform (DECL_SOURCE_LOCATION (TREE_VEC_ELT (method, i)), "also found %<%c%s%>",
5121 (type ? '-' : '+'),
5122 identifier_to_locale (gen_method_decl (TREE_VEC_ELT (method, i))));
5125 return first_method;
5128 /* If RECEIVER is a class reference, return the identifier node for
5129 the referenced class. RECEIVER is created by objc_get_class_reference,
5130 so we check the exact form created depending on which runtimes are
5131 used. */
5133 static tree
5134 receiver_is_class_object (tree receiver, int self, int super)
5136 tree exp, arg;
5138 /* The receiver is 'self' or 'super' in the context of a class method. */
5139 if (objc_method_context
5140 && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
5141 && (self || super))
5142 return (super
5143 ? CLASS_SUPER_NAME (implementation_template)
5144 : CLASS_NAME (implementation_template));
5146 /* The runtime might encapsulate things its own way. */
5147 exp = (*runtime.receiver_is_class_object) (receiver);
5148 if (exp)
5149 return exp;
5151 /* The receiver is a function call that returns an id. Check if
5152 it is a call to objc_getClass, if so, pick up the class name.
5154 This is required by the GNU runtime, which compiles
5156 [NSObject alloc]
5158 into
5160 [objc_get_class ("NSObject") alloc];
5162 and then, to check that the receiver responds to the +alloc
5163 method, needs to be able to determine that the objc_get_class()
5164 call returns the NSObject class and not just a generic Class
5165 pointer.
5167 But, traditionally this is enabled for all runtimes, not just the
5168 GNU one, which means that the compiler is smarter than you'd
5169 expect when dealing with objc_getClass(). For example, with the
5170 Apple runtime, in the code
5172 [objc_getClass ("NSObject") alloc];
5174 the compiler will recognize the objc_getClass() call as special
5175 (due to the code below) and so will know that +alloc is called on
5176 the 'NSObject' class, and can perform the corresponding checks.
5178 Programmers can disable this behaviour by casting the results of
5179 objc_getClass() to 'Class' (this may seem weird because
5180 objc_getClass() is already declared to return 'Class', but the
5181 compiler treats it as a special function). This may be useful if
5182 the class is never declared, and the compiler would complain
5183 about a missing @interface for it. Then, you can do
5185 [(Class)objc_getClass ("MyClassNeverDeclared") alloc];
5187 to silence the warnings. */
5188 if (TREE_CODE (receiver) == CALL_EXPR
5189 && (exp = CALL_EXPR_FN (receiver))
5190 && TREE_CODE (exp) == ADDR_EXPR
5191 && (exp = TREE_OPERAND (exp, 0))
5192 && TREE_CODE (exp) == FUNCTION_DECL
5193 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
5194 prototypes for objc_get_class(). Thankfully, they seem to share the
5195 same function type. */
5196 && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl)
5197 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), runtime.tag_getclass)
5198 /* We have a call to objc_get_class/objc_getClass! */
5199 && (arg = CALL_EXPR_ARG (receiver, 0)))
5201 STRIP_NOPS (arg);
5202 if (TREE_CODE (arg) == ADDR_EXPR
5203 && (arg = TREE_OPERAND (arg, 0))
5204 && TREE_CODE (arg) == STRING_CST)
5205 /* Finally, we have the class name. */
5206 return get_identifier (TREE_STRING_POINTER (arg));
5208 return 0;
5211 /* If we are currently building a message expr, this holds
5212 the identifier of the selector of the message. This is
5213 used when printing warnings about argument mismatches. */
5215 static tree current_objc_message_selector = 0;
5217 tree
5218 objc_message_selector (void)
5220 return current_objc_message_selector;
5223 /* Construct an expression for sending a message.
5224 MESS has the object to send to in TREE_PURPOSE
5225 and the argument list (including selector) in TREE_VALUE.
5227 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
5228 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
5230 tree
5231 objc_build_message_expr (tree receiver, tree message_args)
5233 tree sel_name;
5234 #ifdef OBJCPLUS
5235 tree args = TREE_PURPOSE (message_args);
5236 #else
5237 tree args = message_args;
5238 #endif
5239 tree method_params = NULL_TREE;
5241 if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK)
5242 return error_mark_node;
5244 /* Obtain the full selector name. */
5245 switch (TREE_CODE (args))
5247 case IDENTIFIER_NODE:
5248 /* A unary selector. */
5249 sel_name = args;
5250 break;
5251 case TREE_LIST:
5252 sel_name = build_keyword_selector (args);
5253 break;
5254 default:
5255 gcc_unreachable ();
5258 /* Build the parameter list to give to the method. */
5259 if (TREE_CODE (args) == TREE_LIST)
5260 #ifdef OBJCPLUS
5261 method_params = chainon (args, TREE_VALUE (message_args));
5262 #else
5264 tree chain = args, prev = NULL_TREE;
5266 /* We have a keyword selector--check for comma expressions. */
5267 while (chain)
5269 tree element = TREE_VALUE (chain);
5271 /* We have a comma expression, must collapse... */
5272 if (TREE_CODE (element) == TREE_LIST)
5274 if (prev)
5275 TREE_CHAIN (prev) = element;
5276 else
5277 args = element;
5279 prev = chain;
5280 chain = TREE_CHAIN (chain);
5282 method_params = args;
5284 #endif
5286 #ifdef OBJCPLUS
5287 if (processing_template_decl)
5288 /* Must wait until template instantiation time. */
5289 return build_min_nt_loc (UNKNOWN_LOCATION, MESSAGE_SEND_EXPR, receiver,
5290 sel_name, method_params);
5291 #endif
5293 return objc_finish_message_expr (receiver, sel_name, method_params, NULL);
5296 /* Look up method SEL_NAME that would be suitable for receiver
5297 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
5298 nonzero), and report on any duplicates. */
5300 static tree
5301 lookup_method_in_hash_lists (tree sel_name, int is_class)
5303 tree method_prototype = OBJC_MAP_NOT_FOUND;
5305 if (!is_class)
5306 method_prototype = objc_map_get (instance_method_map, sel_name);
5308 if (method_prototype == OBJC_MAP_NOT_FOUND)
5310 method_prototype = objc_map_get (class_method_map, sel_name);
5311 is_class = 1;
5313 if (method_prototype == OBJC_MAP_NOT_FOUND)
5314 return NULL_TREE;
5317 return check_duplicates (method_prototype, 1, is_class);
5320 /* The 'objc_finish_message_expr' routine is called from within
5321 'objc_build_message_expr' for non-template functions. In the case of
5322 C++ template functions, it is called from 'build_expr_from_tree'
5323 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded.
5325 If the DEPRECATED_METHOD_PROTOTYPE argument is NULL, then we warn
5326 if the method being used is deprecated. If it is not NULL, instead
5327 of deprecating, we set *DEPRECATED_METHOD_PROTOTYPE to the method
5328 prototype that was used and is deprecated. This is useful for
5329 getter calls that are always generated when compiling dot-syntax
5330 expressions, even if they may not be used. In that case, we don't
5331 want the warning immediately; we produce it (if needed) at gimplify
5332 stage when we are sure that the deprecated getter is being
5333 used. */
5334 tree
5335 objc_finish_message_expr (tree receiver, tree sel_name, tree method_params,
5336 tree *deprecated_method_prototype)
5338 tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype;
5339 tree retval, class_tree;
5340 int self, super, have_cast;
5342 /* We have used the receiver, so mark it as read. */
5343 mark_exp_read (receiver);
5345 /* Extract the receiver of the message, as well as its type
5346 (where the latter may take the form of a cast or be inferred
5347 from the implementation context). */
5348 rtype = receiver;
5349 while (TREE_CODE (rtype) == COMPOUND_EXPR
5350 || TREE_CODE (rtype) == MODIFY_EXPR
5351 || CONVERT_EXPR_P (rtype)
5352 || TREE_CODE (rtype) == COMPONENT_REF)
5353 rtype = TREE_OPERAND (rtype, 0);
5355 /* self is 1 if this is a message to self, 0 otherwise */
5356 self = (rtype == self_decl);
5358 /* super is 1 if this is a message to super, 0 otherwise. */
5359 super = (rtype == UOBJC_SUPER_decl);
5361 /* rtype is the type of the receiver. */
5362 rtype = TREE_TYPE (receiver);
5364 /* have_cast is 1 if the receiver is casted. */
5365 have_cast = (TREE_CODE (receiver) == NOP_EXPR
5366 || (TREE_CODE (receiver) == COMPOUND_EXPR
5367 && !IS_SUPER (rtype)));
5369 /* If we are calling [super dealloc], reset our warning flag. */
5370 if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name)))
5371 should_call_super_dealloc = 0;
5373 /* If the receiver is a class object, retrieve the corresponding
5374 @interface, if one exists. class_tree is the class name
5375 identifier, or NULL_TREE if this is not a class method or the
5376 class name could not be determined (as in the case "Class c; [c
5377 method];"). */
5378 class_tree = receiver_is_class_object (receiver, self, super);
5380 /* Now determine the receiver type (if an explicit cast has not been
5381 provided). */
5382 if (!have_cast)
5384 if (class_tree)
5386 /* We are here when we have no cast, and we have a class
5387 name. So, this is a plain method to a class object, as
5388 in [NSObject alloc]. Find the interface corresponding to
5389 the class name. */
5390 rtype = lookup_interface (class_tree);
5392 if (rtype == NULL_TREE)
5394 /* If 'rtype' is NULL_TREE at this point it means that
5395 we have seen no @interface corresponding to that
5396 class name, only a @class declaration (alternatively,
5397 this was a call such as [objc_getClass("SomeClass")
5398 alloc], where we've never seen the @interface of
5399 SomeClass). So, we have a class name (class_tree)
5400 but no actual details of the class methods. We won't
5401 be able to check that the class responds to the
5402 method, and we will have to guess the method
5403 prototype. Emit a warning, then keep going (this
5404 will use any method with a matching name, as if the
5405 receiver was of type 'Class'). */
5406 warning (0, "@interface of class %qE not found", class_tree);
5409 /* Handle `self' and `super'. */
5410 else if (super)
5412 if (!CLASS_SUPER_NAME (implementation_template))
5414 error ("no super class declared in @interface for %qE",
5415 CLASS_NAME (implementation_template));
5416 return error_mark_node;
5418 rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template));
5420 else if (self)
5421 rtype = lookup_interface (CLASS_NAME (implementation_template));
5424 if (objc_is_id (rtype))
5426 /* The receiver is of type 'id' or 'Class' (with or without some
5427 protocols attached to it). */
5429 /* We set class_tree to the identifier for 'Class' if this is a
5430 class method, and to NULL_TREE if not. */
5431 class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE);
5433 /* 'rprotos' is the list of protocols that the receiver
5434 supports. */
5435 rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
5436 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
5437 : NULL_TREE);
5439 /* We have no information on the type, and we set it to
5440 NULL_TREE. */
5441 rtype = NULL_TREE;
5443 /* If there are any protocols, check that the method we are
5444 calling appears in the protocol list. If there are no
5445 protocols, this is a message to 'id' or 'Class' and we accept
5446 any method that exists. */
5447 if (rprotos)
5449 /* If messaging 'id <Protos>' or 'Class <Proto>', first
5450 search in protocols themselves for the method
5451 prototype. */
5452 method_prototype
5453 = lookup_method_in_protocol_list (rprotos, sel_name,
5454 class_tree != NULL_TREE);
5456 /* If messaging 'Class <Proto>' but did not find a class
5457 method prototype, search for an instance method instead,
5458 and warn about having done so. */
5459 if (!method_prototype && !rtype && class_tree != NULL_TREE)
5461 method_prototype
5462 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5464 if (method_prototype)
5465 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
5466 sel_name, sel_name);
5470 else if (rtype)
5472 /* We have a receiver type which is more specific than 'id' or
5473 'Class'. */
5474 tree orig_rtype = rtype;
5476 if (TREE_CODE (rtype) == POINTER_TYPE)
5477 rtype = TREE_TYPE (rtype);
5478 /* Traverse typedef aliases */
5479 while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype)
5480 && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL
5481 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)))
5482 rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype));
5483 if (TYPED_OBJECT (rtype))
5485 rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype);
5486 rtype = TYPE_OBJC_INTERFACE (rtype);
5488 if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE)
5490 /* If we could not find an @interface declaration, we must
5491 have only seen a @class declaration; so, we cannot say
5492 anything more intelligent about which methods the
5493 receiver will understand. Note that this only happens
5494 for instance methods; for class methods to a class where
5495 we have only seen a @class declaration,
5496 lookup_interface() above would have set rtype to
5497 NULL_TREE. */
5498 if (rprotos)
5500 /* We could not find an @interface declaration, yet, if
5501 there are protocols attached to the type, we can
5502 still look up the method in the protocols. Ie, we
5503 are in the following case:
5505 @class MyClass;
5506 MyClass<MyProtocol> *x;
5507 [x method];
5509 If 'MyProtocol' has the method 'method', we can check
5510 and retrieve the method prototype. */
5511 method_prototype
5512 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5514 /* At this point, if we have found the method_prototype,
5515 we are quite happy. The details of the class are
5516 irrelevant. If we haven't found it, a warning will
5517 have been produced that the method could not be found
5518 in the protocol, and we won't produce further
5519 warnings (please note that this means that "@class
5520 MyClass; MyClass <MyProtocol> *x;" is exactly
5521 equivalent to "id <MyProtocol> x", which isn't too
5522 satisfactory but it's not easy to see how to do
5523 better). */
5525 else
5527 if (rtype)
5529 /* We could not find an @interface declaration, and
5530 there are no protocols attached to the receiver,
5531 so we can't complete the check that the receiver
5532 responds to the method, and we can't retrieve the
5533 method prototype. But, because the receiver has
5534 a well-specified class, the programmer did want
5535 this check to be performed. Emit a warning, then
5536 keep going as if it was an 'id'. To remove the
5537 warning, either include an @interface for the
5538 class, or cast the receiver to 'id'. Note that
5539 rtype is an IDENTIFIER_NODE at this point. */
5540 warning (0, "@interface of class %qE not found", rtype);
5544 rtype = NULL_TREE;
5546 else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE
5547 || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE)
5549 /* We have a valid ObjC class name with an associated
5550 @interface. Look up the method name in the published
5551 @interface for the class (and its superclasses). */
5552 method_prototype
5553 = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE);
5555 /* If the method was not found in the @interface, it may still
5556 exist locally as part of the @implementation. */
5557 if (!method_prototype && objc_implementation_context
5558 && CLASS_NAME (objc_implementation_context)
5559 == OBJC_TYPE_NAME (rtype))
5560 method_prototype
5561 = lookup_method
5562 ((class_tree
5563 ? CLASS_CLS_METHODS (objc_implementation_context)
5564 : CLASS_NST_METHODS (objc_implementation_context)),
5565 sel_name);
5567 /* If we haven't found a candidate method by now, try looking for
5568 it in the protocol list. */
5569 if (!method_prototype && rprotos)
5570 method_prototype
5571 = lookup_method_in_protocol_list (rprotos, sel_name,
5572 class_tree != NULL_TREE);
5574 else
5576 /* We have a type, but it's not an Objective-C type (!). */
5577 warning (0, "invalid receiver type %qs",
5578 identifier_to_locale (gen_type_name (orig_rtype)));
5579 /* After issuing the "invalid receiver" warning, perform method
5580 lookup as if we were messaging 'id'. */
5581 rtype = rprotos = NULL_TREE;
5584 /* Note that rtype could also be NULL_TREE. This happens if we are
5585 messaging a class by name, but the class was only
5586 forward-declared using @class. */
5588 /* For 'id' or 'Class' receivers, search in the global hash table as
5589 a last resort. For all receivers, warn if protocol searches have
5590 failed. */
5591 if (!method_prototype)
5593 if (rprotos)
5594 warning (0, "%<%c%E%> not found in protocol(s)",
5595 (class_tree ? '+' : '-'),
5596 sel_name);
5598 if (!rtype)
5599 method_prototype
5600 = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE);
5603 if (!method_prototype)
5605 static bool warn_missing_methods = false;
5607 if (rtype)
5608 warning (0, "%qE may not respond to %<%c%E%>",
5609 OBJC_TYPE_NAME (rtype),
5610 (class_tree ? '+' : '-'),
5611 sel_name);
5612 /* If we are messaging an 'id' or 'Class' object and made it here,
5613 then we have failed to find _any_ instance or class method,
5614 respectively. */
5615 else
5616 warning (0, "no %<%c%E%> method found",
5617 (class_tree ? '+' : '-'),
5618 sel_name);
5620 if (!warn_missing_methods)
5622 warning_at (input_location,
5623 0, "(Messages without a matching method signature");
5624 warning_at (input_location,
5625 0, "will be assumed to return %<id%> and accept");
5626 warning_at (input_location,
5627 0, "%<...%> as arguments.)");
5628 warn_missing_methods = true;
5631 else
5633 /* Warn if the method is deprecated, but not if the receiver is
5634 a generic 'id'. 'id' is used to cast an object to a generic
5635 object of an unspecified class; in that case, we'll use
5636 whatever method prototype we can find to get the method
5637 argument and return types, but it is not appropriate to
5638 produce deprecation warnings since we don't know the class
5639 that the object will be of at runtime. The @interface(s) for
5640 that class may not even be available to the compiler right
5641 now, and it is perfectly possible that the method is marked
5642 as non-deprecated in such @interface(s).
5644 In practice this makes sense since casting an object to 'id'
5645 is often used precisely to turn off warnings associated with
5646 the object being of a particular class. */
5647 if (TREE_DEPRECATED (method_prototype) && rtype != NULL_TREE)
5649 if (deprecated_method_prototype)
5650 *deprecated_method_prototype = method_prototype;
5651 else
5652 warn_deprecated_use (method_prototype, NULL_TREE);
5656 /* Save the selector name for printing error messages. */
5657 current_objc_message_selector = sel_name;
5659 /* Build the method call.
5660 TODO: Get the location from somewhere that will work for delayed
5661 expansion. */
5663 retval = (*runtime.build_objc_method_call) (input_location, method_prototype,
5664 receiver, rtype, sel_name,
5665 method_params, super);
5667 current_objc_message_selector = 0;
5669 return retval;
5673 /* This routine creates a static variable used to implement @protocol(MyProtocol)
5674 expression. This variable will be initialized to global protocol_t meta-data
5675 pointer. */
5677 /* This function is called by the parser when (and only when) a
5678 @protocol() expression is found, in order to compile it. */
5679 tree
5680 objc_build_protocol_expr (tree protoname)
5682 tree p = lookup_protocol (protoname, /* warn if deprecated */ true,
5683 /* definition_required */ false);
5685 if (!p)
5687 error ("cannot find protocol declaration for %qE", protoname);
5688 return error_mark_node;
5691 return (*runtime.get_protocol_reference) (input_location, p);
5694 /* This function is called by the parser when a @selector() expression
5695 is found, in order to compile it. It is only called by the parser
5696 and only to compile a @selector(). LOC is the location of the
5697 @selector. */
5698 tree
5699 objc_build_selector_expr (location_t loc, tree selnamelist)
5701 tree selname;
5703 /* Obtain the full selector name. */
5704 switch (TREE_CODE (selnamelist))
5706 case IDENTIFIER_NODE:
5707 /* A unary selector. */
5708 selname = selnamelist;
5709 break;
5710 case TREE_LIST:
5711 selname = build_keyword_selector (selnamelist);
5712 break;
5713 default:
5714 gcc_unreachable ();
5717 /* If we are required to check @selector() expressions as they
5718 are found, check that the selector has been declared. */
5719 if (warn_undeclared_selector)
5721 /* Look the selector up in the list of all known class and
5722 instance methods (up to this line) to check that the selector
5723 exists. */
5724 tree method;
5726 /* First try with instance methods. */
5727 method = objc_map_get (instance_method_map, selname);
5729 /* If not found, try with class methods. */
5730 if (method == OBJC_MAP_NOT_FOUND)
5732 method = objc_map_get (class_method_map, selname);
5734 /* If still not found, print out a warning. */
5735 if (method == OBJC_MAP_NOT_FOUND)
5736 warning (0, "undeclared selector %qE", selname);
5740 /* The runtimes do this differently, most particularly, GNU has typed
5741 selectors, whilst NeXT does not. */
5742 return (*runtime.build_selector_reference) (loc, selname, NULL_TREE);
5745 static tree
5746 build_ivar_reference (tree id)
5748 tree base;
5749 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
5751 /* Historically, a class method that produced objects (factory
5752 method) would assign `self' to the instance that it
5753 allocated. This would effectively turn the class method into
5754 an instance method. Following this assignment, the instance
5755 variables could be accessed. That practice, while safe,
5756 violates the simple rule that a class method should not refer
5757 to an instance variable. It's better to catch the cases
5758 where this is done unknowingly than to support the above
5759 paradigm. */
5760 warning (0, "instance variable %qE accessed in class method",
5761 id);
5762 self_decl = convert (objc_instance_type, self_decl); /* cast */
5765 base = build_indirect_ref (input_location, self_decl, RO_ARROW);
5766 return (*runtime.build_ivar_reference) (input_location, base, id);
5769 static void
5770 hash_init (void)
5772 instance_method_map = objc_map_alloc_ggc (1000);
5773 class_method_map = objc_map_alloc_ggc (1000);
5775 class_name_map = objc_map_alloc_ggc (200);
5776 alias_name_map = objc_map_alloc_ggc (200);
5778 /* Initialize the hash table used to hold the constant string objects. */
5779 string_htab = htab_create_ggc (31, string_hash,
5780 string_eq, NULL);
5783 /* Use the following to add a method to class_method_map or
5784 instance_method_map. It will add the method, keyed by the
5785 METHOD_SEL_NAME. If the method already exists, but with one or
5786 more different prototypes, it will store a TREE_VEC in the map,
5787 with the method prototypes in the vector. */
5788 static void
5789 insert_method_into_method_map (bool class_method, tree method)
5791 tree method_name = METHOD_SEL_NAME (method);
5792 tree existing_entry;
5793 objc_map_t map;
5795 if (class_method)
5796 map = class_method_map;
5797 else
5798 map = instance_method_map;
5800 /* Check if the method already exists in the map. */
5801 existing_entry = objc_map_get (map, method_name);
5803 /* If not, we simply add it to the map. */
5804 if (existing_entry == OBJC_MAP_NOT_FOUND)
5805 objc_map_put (map, method_name, method);
5806 else
5808 tree new_entry;
5810 /* If an entry already exists, it's more complicated. We'll
5811 have to check whether the method prototype is the same or
5812 not. */
5813 if (TREE_CODE (existing_entry) != TREE_VEC)
5815 /* If the method prototypes are the same, there is nothing
5816 to do. */
5817 if (comp_proto_with_proto (method, existing_entry, 1))
5818 return;
5820 /* If not, create a vector to store both the method already
5821 in the map, and the new one that we are adding. */
5822 new_entry = make_tree_vec (2);
5824 TREE_VEC_ELT (new_entry, 0) = existing_entry;
5825 TREE_VEC_ELT (new_entry, 1) = method;
5827 else
5829 /* An entry already exists, and it's already a vector. This
5830 means that at least 2 different method prototypes were
5831 already found, and we're considering registering yet
5832 another one. */
5833 size_t i;
5835 /* Check all the existing prototypes. If any matches the
5836 one we need to add, there is nothing to do because it's
5837 already there. */
5838 for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++)
5839 if (comp_proto_with_proto (method, TREE_VEC_ELT (existing_entry, i), 1))
5840 return;
5842 /* Else, create a new, bigger vector and add the new method
5843 at the end of it. This is inefficient but extremely
5844 rare; in any sane program most methods have a single
5845 prototype, and very few, if any, will have more than
5846 2! */
5847 new_entry = make_tree_vec (TREE_VEC_LENGTH (existing_entry) + 1);
5849 /* Copy the methods from the existing vector. */
5850 for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++)
5851 TREE_VEC_ELT (new_entry, i) = TREE_VEC_ELT (existing_entry, i);
5853 /* Add the new method at the end. */
5854 TREE_VEC_ELT (new_entry, i) = method;
5857 /* Store the new vector in the map. */
5858 objc_map_put (map, method_name, new_entry);
5863 static tree
5864 lookup_method (tree mchain, tree method)
5866 tree key;
5868 if (TREE_CODE (method) == IDENTIFIER_NODE)
5869 key = method;
5870 else
5871 key = METHOD_SEL_NAME (method);
5873 while (mchain)
5875 if (METHOD_SEL_NAME (mchain) == key)
5876 return mchain;
5878 mchain = DECL_CHAIN (mchain);
5880 return NULL_TREE;
5883 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance
5884 method in INTERFACE, along with any categories and protocols
5885 attached thereto. If method is not found, and the
5886 OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS, recursively examine the
5887 INTERFACE's superclass. If OBJC_LOOKUP_CLASS is set,
5888 OBJC_LOOKUP_NO_SUPER is clear, and no suitable class method could
5889 be found in INTERFACE or any of its superclasses, look for an
5890 _instance_ method of the same name in the root class as a last
5891 resort. This behaviour can be turned off by using
5892 OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS.
5894 If a suitable method cannot be found, return NULL_TREE. */
5896 static tree
5897 lookup_method_static (tree interface, tree ident, int flags)
5899 tree meth = NULL_TREE, root_inter = NULL_TREE;
5900 tree inter = interface;
5901 int is_class = (flags & OBJC_LOOKUP_CLASS);
5902 int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER);
5903 int no_instance_methods_of_root_class = (flags & OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS);
5905 while (inter)
5907 tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter);
5908 tree category = inter;
5910 /* First, look up the method in the class itself. */
5911 if ((meth = lookup_method (chain, ident)))
5912 return meth;
5914 /* Failing that, look for the method in each category of the class. */
5915 while ((category = CLASS_CATEGORY_LIST (category)))
5917 chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category);
5919 /* Check directly in each category. */
5920 if ((meth = lookup_method (chain, ident)))
5921 return meth;
5923 /* Failing that, check in each category's protocols. */
5924 if (CLASS_PROTOCOL_LIST (category))
5926 if ((meth = (lookup_method_in_protocol_list
5927 (CLASS_PROTOCOL_LIST (category), ident, is_class))))
5928 return meth;
5932 /* If not found in categories, check in protocols of the main class. */
5933 if (CLASS_PROTOCOL_LIST (inter))
5935 if ((meth = (lookup_method_in_protocol_list
5936 (CLASS_PROTOCOL_LIST (inter), ident, is_class))))
5937 return meth;
5940 /* If we were instructed not to look in superclasses, don't. */
5941 if (no_superclasses)
5942 return NULL_TREE;
5944 /* Failing that, climb up the inheritance hierarchy. */
5945 root_inter = inter;
5946 inter = lookup_interface (CLASS_SUPER_NAME (inter));
5948 while (inter);
5950 if (is_class && !no_instance_methods_of_root_class)
5952 /* If no class (factory) method was found, check if an _instance_
5953 method of the same name exists in the root class. This is what
5954 the Objective-C runtime will do. */
5955 return lookup_method_static (root_inter, ident, 0);
5957 else
5959 /* If an instance method was not found, return 0. */
5960 return NULL_TREE;
5964 static tree
5965 objc_add_method (tree klass, tree method, int is_class, bool is_optional)
5967 tree existing_method = NULL_TREE;
5969 /* The first thing we do is look up the method in the list of
5970 methods already defined in the interface (or implementation). */
5971 if (is_class)
5972 existing_method = lookup_method (CLASS_CLS_METHODS (klass), method);
5973 else
5974 existing_method = lookup_method (CLASS_NST_METHODS (klass), method);
5976 /* In the case of protocols, we have a second list of methods to
5977 consider, the list of optional ones. */
5978 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
5980 /* @required methods are added to the protocol's normal list.
5981 @optional methods are added to the protocol's OPTIONAL lists.
5982 Note that adding the methods to the optional lists disables
5983 checking that the methods are implemented by classes
5984 implementing the protocol, since these checks only use the
5985 CLASS_CLS_METHODS and CLASS_NST_METHODS. */
5987 /* First of all, if the method to add is @optional, and we found
5988 it already existing as @required, emit an error. */
5989 if (is_optional && existing_method)
5991 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
5992 (is_class ? '+' : '-'),
5993 METHOD_SEL_NAME (existing_method));
5994 inform (DECL_SOURCE_LOCATION (existing_method),
5995 "previous declaration of %<%c%E%> as %<@required%>",
5996 (is_class ? '+' : '-'),
5997 METHOD_SEL_NAME (existing_method));
6000 /* Now check the list of @optional methods if we didn't find the
6001 method in the @required list. */
6002 if (!existing_method)
6004 if (is_class)
6005 existing_method = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (klass), method);
6006 else
6007 existing_method = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (klass), method);
6009 if (!is_optional && existing_method)
6011 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
6012 (is_class ? '+' : '-'),
6013 METHOD_SEL_NAME (existing_method));
6014 inform (DECL_SOURCE_LOCATION (existing_method),
6015 "previous declaration of %<%c%E%> as %<@optional%>",
6016 (is_class ? '+' : '-'),
6017 METHOD_SEL_NAME (existing_method));
6022 /* If the method didn't exist already, add it. */
6023 if (!existing_method)
6025 if (is_optional)
6027 if (is_class)
6029 /* Put the method on the list in reverse order. */
6030 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass);
6031 PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method;
6033 else
6035 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass);
6036 PROTOCOL_OPTIONAL_NST_METHODS (klass) = method;
6039 else
6041 if (is_class)
6043 DECL_CHAIN (method) = CLASS_CLS_METHODS (klass);
6044 CLASS_CLS_METHODS (klass) = method;
6046 else
6048 DECL_CHAIN (method) = CLASS_NST_METHODS (klass);
6049 CLASS_NST_METHODS (klass) = method;
6053 else
6055 /* The method was already defined. Check that the types match
6056 for an @interface for a class or category, or for a
6057 @protocol. Give hard errors on methods with identical
6058 selectors but differing argument and/or return types. We do
6059 not do this for @implementations, because C/C++ will do it
6060 for us (i.e., there will be duplicate function definition
6061 errors). */
6062 if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE
6063 || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6064 /* Starting with GCC 4.6, we emit the same error for
6065 protocols too. The situation is identical to
6066 @interfaces as there is no possible meaningful reason
6067 for defining the same method with different signatures
6068 in the very same @protocol. If that was allowed,
6069 whenever the protocol is used (both at compile and run
6070 time) there wouldn't be any meaningful way to decide
6071 which of the two method signatures should be used. */
6072 || TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
6073 && !comp_proto_with_proto (method, existing_method, 1))
6075 error ("duplicate declaration of method %<%c%E%> with conflicting types",
6076 (is_class ? '+' : '-'),
6077 METHOD_SEL_NAME (existing_method));
6078 inform (DECL_SOURCE_LOCATION (existing_method),
6079 "previous declaration of %<%c%E%>",
6080 (is_class ? '+' : '-'),
6081 METHOD_SEL_NAME (existing_method));
6085 if (is_class)
6086 insert_method_into_method_map (true, method);
6087 else
6089 insert_method_into_method_map (false, method);
6091 /* Instance methods in root classes (and categories thereof)
6092 may act as class methods as a last resort. We also add
6093 instance methods listed in @protocol declarations to
6094 the class hash table, on the assumption that @protocols
6095 may be adopted by root classes or categories. */
6096 if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6097 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
6098 klass = lookup_interface (CLASS_NAME (klass));
6100 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE
6101 || !CLASS_SUPER_NAME (klass))
6102 insert_method_into_method_map (true, method);
6105 return method;
6108 static void
6109 add_category (tree klass, tree category)
6111 /* Put categories on list in reverse order. */
6112 tree cat = lookup_category (klass, CLASS_SUPER_NAME (category));
6114 if (cat)
6116 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
6117 CLASS_NAME (klass),
6118 CLASS_SUPER_NAME (category));
6120 else
6122 CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass);
6123 CLASS_CATEGORY_LIST (klass) = category;
6127 #ifndef OBJCPLUS
6128 /* A flexible array member is a C99 extension where you can use
6129 "type[]" at the end of a struct to mean a variable-length array.
6131 In Objective-C, instance variables are fundamentally members of a
6132 struct, but the struct can always be extended by subclassing; hence
6133 we need to detect and forbid all instance variables declared using
6134 flexible array members.
6136 No check for this is needed in Objective-C++, since C++ does not
6137 have flexible array members. */
6139 /* Determine whether TYPE is a structure with a flexible array member,
6140 a union containing such a structure (possibly recursively) or an
6141 array of such structures or unions. These are all invalid as
6142 instance variable. */
6143 static bool
6144 flexible_array_type_p (tree type)
6146 tree x;
6147 switch (TREE_CODE (type))
6149 case RECORD_TYPE:
6150 x = TYPE_FIELDS (type);
6151 if (x == NULL_TREE)
6152 return false;
6153 while (DECL_CHAIN (x) != NULL_TREE)
6154 x = DECL_CHAIN (x);
6155 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6156 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6157 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6158 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6159 return true;
6160 return false;
6161 case UNION_TYPE:
6162 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
6164 if (flexible_array_type_p (TREE_TYPE (x)))
6165 return true;
6167 return false;
6168 /* Note that we also check for arrays of something that uses a flexible array member. */
6169 case ARRAY_TYPE:
6170 if (flexible_array_type_p (TREE_TYPE (type)))
6171 return true;
6172 return false;
6173 default:
6174 return false;
6177 #endif
6179 /* Produce a printable version of an ivar name. This is only used
6180 inside add_instance_variable. */
6181 static const char *
6182 printable_ivar_name (tree field_decl)
6184 if (DECL_NAME (field_decl))
6185 return identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl)));
6186 else
6187 return _("<unnamed>");
6190 /* Called after parsing each instance variable declaration. Necessary to
6191 preserve typedefs and implement public/private...
6193 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
6195 static tree
6196 add_instance_variable (tree klass, objc_ivar_visibility_kind visibility,
6197 tree field_decl)
6199 tree field_type = TREE_TYPE (field_decl);
6201 #ifdef OBJCPLUS
6202 if (TREE_CODE (field_type) == REFERENCE_TYPE)
6204 error ("illegal reference type specified for instance variable %qs",
6205 printable_ivar_name (field_decl));
6206 /* Return class as is without adding this ivar. */
6207 return klass;
6209 #endif
6211 if (field_type == error_mark_node || !TYPE_SIZE (field_type)
6212 || TYPE_SIZE (field_type) == error_mark_node)
6213 /* 'type[0]' is allowed, but 'type[]' is not! */
6215 error ("instance variable %qs has unknown size",
6216 printable_ivar_name (field_decl));
6217 /* Return class as is without adding this ivar. */
6218 return klass;
6221 #ifndef OBJCPLUS
6222 /* Also, in C reject a struct with a flexible array member. Ie,
6224 struct A { int x; int[] y; };
6226 @interface X
6228 struct A instance_variable;
6230 @end
6232 is not valid because if the class is subclassed, we wouldn't be able
6233 to calculate the offset of the next instance variable. */
6234 if (flexible_array_type_p (field_type))
6236 error ("instance variable %qs uses flexible array member",
6237 printable_ivar_name (field_decl));
6238 /* Return class as is without adding this ivar. */
6239 return klass;
6241 #endif
6243 #ifdef OBJCPLUS
6244 /* Check if the ivar being added has a non-POD C++ type. If so, we will
6245 need to either (1) warn the user about it or (2) generate suitable
6246 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
6247 methods (if '-fobjc-call-cxx-cdtors' was specified). */
6248 if (MAYBE_CLASS_TYPE_P (field_type)
6249 && (TYPE_NEEDS_CONSTRUCTING (field_type)
6250 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)
6251 || TYPE_POLYMORPHIC_P (field_type)))
6253 tree type_name = OBJC_TYPE_NAME (field_type);
6255 if (flag_objc_call_cxx_cdtors)
6257 /* Since the ObjC runtime will be calling the constructors and
6258 destructors for us, the only thing we can't handle is the lack
6259 of a default constructor. */
6260 if (TYPE_NEEDS_CONSTRUCTING (field_type)
6261 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type))
6263 warning (0, "type %qE has no default constructor to call",
6264 type_name);
6266 /* If we cannot call a constructor, we should also avoid
6267 calling the destructor, for symmetry. */
6268 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6269 warning (0, "destructor for %qE shall not be run either",
6270 type_name);
6273 else
6275 static bool warn_cxx_ivars = false;
6277 if (TYPE_POLYMORPHIC_P (field_type))
6279 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
6280 initialize them. */
6281 error ("type %qE has virtual member functions", type_name);
6282 error ("illegal aggregate type %qE specified "
6283 "for instance variable %qs",
6284 type_name, printable_ivar_name (field_decl));
6285 /* Return class as is without adding this ivar. */
6286 return klass;
6289 /* User-defined constructors and destructors are not known to Obj-C
6290 and hence will not be called. This may or may not be a problem. */
6291 if (TYPE_NEEDS_CONSTRUCTING (field_type))
6292 warning (0, "type %qE has a user-defined constructor", type_name);
6293 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6294 warning (0, "type %qE has a user-defined destructor", type_name);
6296 if (!warn_cxx_ivars)
6298 warning (0, "C++ constructors and destructors will not "
6299 "be invoked for Objective-C fields");
6300 warn_cxx_ivars = true;
6304 #endif
6306 /* Overload the public attribute, it is not used for FIELD_DECLs. */
6307 switch (visibility)
6309 case OBJC_IVAR_VIS_PROTECTED:
6310 TREE_PUBLIC (field_decl) = 0;
6311 TREE_PRIVATE (field_decl) = 0;
6312 TREE_PROTECTED (field_decl) = 1;
6313 break;
6315 case OBJC_IVAR_VIS_PACKAGE:
6316 /* TODO: Implement the package variant. */
6317 case OBJC_IVAR_VIS_PUBLIC:
6318 TREE_PUBLIC (field_decl) = 1;
6319 TREE_PRIVATE (field_decl) = 0;
6320 TREE_PROTECTED (field_decl) = 0;
6321 break;
6323 case OBJC_IVAR_VIS_PRIVATE:
6324 TREE_PUBLIC (field_decl) = 0;
6325 TREE_PRIVATE (field_decl) = 1;
6326 TREE_PROTECTED (field_decl) = 0;
6327 break;
6331 CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl);
6333 return klass;
6336 /* True if the ivar is private and we are not in its implementation. */
6338 static int
6339 is_private (tree decl)
6341 return (TREE_PRIVATE (decl)
6342 && ! is_ivar (CLASS_IVARS (implementation_template),
6343 DECL_NAME (decl)));
6346 /* Searches all the instance variables of 'klass' and of its
6347 superclasses for an instance variable whose name (identifier) is
6348 'ivar_name_ident'. Return the declaration (DECL) of the instance
6349 variable, if found, or NULL_TREE, if not found. */
6350 static inline tree
6351 ivar_of_class (tree klass, tree ivar_name_ident)
6353 /* First, look up the ivar in CLASS_RAW_IVARS. */
6354 tree decl_chain = CLASS_RAW_IVARS (klass);
6356 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6357 if (DECL_NAME (decl_chain) == ivar_name_ident)
6358 return decl_chain;
6360 /* If not found, search up the class hierarchy. */
6361 while (CLASS_SUPER_NAME (klass))
6363 klass = lookup_interface (CLASS_SUPER_NAME (klass));
6365 decl_chain = CLASS_RAW_IVARS (klass);
6367 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6368 if (DECL_NAME (decl_chain) == ivar_name_ident)
6369 return decl_chain;
6372 return NULL_TREE;
6375 /* We have an instance variable reference;, check to see if it is public. */
6378 objc_is_public (tree expr, tree identifier)
6380 tree basetype, decl;
6382 #ifdef OBJCPLUS
6383 if (processing_template_decl)
6384 return 1;
6385 #endif
6387 if (TREE_TYPE (expr) == error_mark_node)
6388 return 1;
6390 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
6392 if (basetype && TREE_CODE (basetype) == RECORD_TYPE)
6394 if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype))
6396 tree klass = lookup_interface (OBJC_TYPE_NAME (basetype));
6398 if (!klass)
6400 error ("cannot find interface declaration for %qE",
6401 OBJC_TYPE_NAME (basetype));
6402 return 0;
6405 if ((decl = ivar_of_class (klass, identifier)))
6407 if (TREE_PUBLIC (decl))
6408 return 1;
6410 /* Important difference between the Stepstone translator:
6411 all instance variables should be public within the context
6412 of the implementation. */
6413 if (objc_implementation_context
6414 && ((TREE_CODE (objc_implementation_context)
6415 == CLASS_IMPLEMENTATION_TYPE)
6416 || (TREE_CODE (objc_implementation_context)
6417 == CATEGORY_IMPLEMENTATION_TYPE)))
6419 tree curtype = TYPE_MAIN_VARIANT
6420 (CLASS_STATIC_TEMPLATE
6421 (implementation_template));
6423 if (basetype == curtype
6424 || DERIVED_FROM_P (basetype, curtype))
6426 int priv = is_private (decl);
6428 if (priv)
6429 error ("instance variable %qE is declared private",
6430 DECL_NAME (decl));
6432 return !priv;
6436 /* The 2.95.2 compiler sometimes allowed C functions to access
6437 non-@public ivars. We will let this slide for now... */
6438 if (!objc_method_context)
6440 warning (0, "instance variable %qE is %s; "
6441 "this will be a hard error in the future",
6442 identifier,
6443 TREE_PRIVATE (decl) ? "@private" : "@protected");
6444 return 1;
6447 error ("instance variable %qE is declared %s",
6448 identifier,
6449 TREE_PRIVATE (decl) ? "private" : "protected");
6450 return 0;
6455 return 1;
6458 /* Make sure all methods in CHAIN (a list of method declarations from
6459 an @interface or a @protocol) are in IMPLEMENTATION (the
6460 implementation context). This is used to check for example that
6461 all methods declared in an @interface were implemented in an
6462 @implementation.
6464 Some special methods (property setters/getters) are special and if
6465 they are not found in IMPLEMENTATION, we look them up in its
6466 superclasses. */
6468 static int
6469 check_methods (tree chain, tree implementation, int mtype)
6471 int first = 1;
6472 tree list;
6474 if (mtype == (int)'+')
6475 list = CLASS_CLS_METHODS (implementation);
6476 else
6477 list = CLASS_NST_METHODS (implementation);
6479 while (chain)
6481 /* If the method is associated with a dynamic property, then it
6482 is Ok not to have the method implementation, as it will be
6483 generated dynamically at runtime. To decide if the method is
6484 associated with a @dynamic property, we search the list of
6485 @synthesize and @dynamic for this implementation, and look
6486 for any @dynamic property with the same setter or getter name
6487 as this method. */
6488 tree x;
6489 for (x = IMPL_PROPERTY_DECL (implementation); x; x = TREE_CHAIN (x))
6490 if (PROPERTY_DYNAMIC (x)
6491 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6492 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6493 break;
6495 if (x != NULL_TREE)
6497 chain = TREE_CHAIN (chain); /* next method... */
6498 continue;
6501 if (!lookup_method (list, chain))
6503 /* If the method is a property setter/getter, we'll still
6504 allow it to be missing if it is implemented by
6505 'interface' or any of its superclasses. */
6506 tree property = METHOD_PROPERTY_CONTEXT (chain);
6507 if (property)
6509 /* Note that since this is a property getter/setter, it
6510 is obviously an instance method. */
6511 tree interface = NULL_TREE;
6513 /* For a category, first check the main class
6514 @interface. */
6515 if (TREE_CODE (implementation) == CATEGORY_IMPLEMENTATION_TYPE)
6517 interface = lookup_interface (CLASS_NAME (implementation));
6519 /* If the method is found in the main class, it's Ok. */
6520 if (lookup_method (CLASS_NST_METHODS (interface), chain))
6522 chain = DECL_CHAIN (chain);
6523 continue;
6526 /* Else, get the superclass. */
6527 if (CLASS_SUPER_NAME (interface))
6528 interface = lookup_interface (CLASS_SUPER_NAME (interface));
6529 else
6530 interface = NULL_TREE;
6533 /* Get the superclass for classes. */
6534 if (TREE_CODE (implementation) == CLASS_IMPLEMENTATION_TYPE)
6536 if (CLASS_SUPER_NAME (implementation))
6537 interface = lookup_interface (CLASS_SUPER_NAME (implementation));
6538 else
6539 interface = NULL_TREE;
6542 /* Now, interface is the superclass, if any; go check it. */
6543 if (interface)
6545 if (lookup_method_static (interface, chain, 0))
6547 chain = DECL_CHAIN (chain);
6548 continue;
6551 /* Else, fall through - warn. */
6553 if (first)
6555 switch (TREE_CODE (implementation))
6557 case CLASS_IMPLEMENTATION_TYPE:
6558 warning (0, "incomplete implementation of class %qE",
6559 CLASS_NAME (implementation));
6560 break;
6561 case CATEGORY_IMPLEMENTATION_TYPE:
6562 warning (0, "incomplete implementation of category %qE",
6563 CLASS_SUPER_NAME (implementation));
6564 break;
6565 default:
6566 gcc_unreachable ();
6568 first = 0;
6571 warning (0, "method definition for %<%c%E%> not found",
6572 mtype, METHOD_SEL_NAME (chain));
6575 chain = DECL_CHAIN (chain);
6578 return first;
6581 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
6583 static int
6584 conforms_to_protocol (tree klass, tree protocol)
6586 if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE)
6588 tree p = CLASS_PROTOCOL_LIST (klass);
6589 while (p && TREE_VALUE (p) != protocol)
6590 p = TREE_CHAIN (p);
6592 if (!p)
6594 tree super = (CLASS_SUPER_NAME (klass)
6595 ? lookup_interface (CLASS_SUPER_NAME (klass))
6596 : NULL_TREE);
6597 int tmp = super ? conforms_to_protocol (super, protocol) : 0;
6598 if (!tmp)
6599 return 0;
6603 return 1;
6606 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
6607 CONTEXT. This is one of two mechanisms to check protocol integrity. */
6609 static int
6610 check_methods_accessible (tree chain, tree context, int mtype)
6612 int first = 1;
6613 tree list;
6614 tree base_context = context;
6616 while (chain)
6618 /* If the method is associated with a dynamic property, then it
6619 is Ok not to have the method implementation, as it will be
6620 generated dynamically at runtime. Search for any @dynamic
6621 property with the same setter or getter name as this
6622 method. TODO: Use a hashtable lookup. */
6623 tree x;
6624 for (x = IMPL_PROPERTY_DECL (base_context); x; x = TREE_CHAIN (x))
6625 if (PROPERTY_DYNAMIC (x)
6626 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6627 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6628 break;
6630 if (x != NULL_TREE)
6632 chain = TREE_CHAIN (chain); /* next method... */
6633 continue;
6636 context = base_context;
6637 while (context)
6639 if (mtype == '+')
6640 list = CLASS_CLS_METHODS (context);
6641 else
6642 list = CLASS_NST_METHODS (context);
6644 if (lookup_method (list, chain))
6645 break;
6647 switch (TREE_CODE (context))
6649 case CLASS_IMPLEMENTATION_TYPE:
6650 case CLASS_INTERFACE_TYPE:
6651 context = (CLASS_SUPER_NAME (context)
6652 ? lookup_interface (CLASS_SUPER_NAME (context))
6653 : NULL_TREE);
6654 break;
6655 case CATEGORY_IMPLEMENTATION_TYPE:
6656 case CATEGORY_INTERFACE_TYPE:
6657 context = (CLASS_NAME (context)
6658 ? lookup_interface (CLASS_NAME (context))
6659 : NULL_TREE);
6660 break;
6661 default:
6662 gcc_unreachable ();
6666 if (context == NULL_TREE)
6668 if (first)
6670 switch (TREE_CODE (objc_implementation_context))
6672 case CLASS_IMPLEMENTATION_TYPE:
6673 warning (0, "incomplete implementation of class %qE",
6674 CLASS_NAME (objc_implementation_context));
6675 break;
6676 case CATEGORY_IMPLEMENTATION_TYPE:
6677 warning (0, "incomplete implementation of category %qE",
6678 CLASS_SUPER_NAME (objc_implementation_context));
6679 break;
6680 default:
6681 gcc_unreachable ();
6683 first = 0;
6685 warning (0, "method definition for %<%c%E%> not found",
6686 mtype, METHOD_SEL_NAME (chain));
6689 chain = TREE_CHAIN (chain); /* next method... */
6691 return first;
6694 /* Check whether the current interface (accessible via
6695 'objc_implementation_context') actually implements protocol P, along
6696 with any protocols that P inherits. */
6698 static void
6699 check_protocol (tree p, const char *type, tree name)
6701 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
6703 int f1, f2;
6705 /* Ensure that all protocols have bodies! */
6706 if (warn_protocol)
6708 f1 = check_methods (PROTOCOL_CLS_METHODS (p),
6709 objc_implementation_context,
6710 '+');
6711 f2 = check_methods (PROTOCOL_NST_METHODS (p),
6712 objc_implementation_context,
6713 '-');
6715 else
6717 f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
6718 objc_implementation_context,
6719 '+');
6720 f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
6721 objc_implementation_context,
6722 '-');
6725 if (!f1 || !f2)
6726 warning (0, "%s %qE does not fully implement the %qE protocol",
6727 type, name, PROTOCOL_NAME (p));
6730 /* Check protocols recursively. */
6731 if (PROTOCOL_LIST (p))
6733 tree subs = PROTOCOL_LIST (p);
6734 tree super_class =
6735 lookup_interface (CLASS_SUPER_NAME (implementation_template));
6737 while (subs)
6739 tree sub = TREE_VALUE (subs);
6741 /* If the superclass does not conform to the protocols
6742 inherited by P, then we must! */
6743 if (!super_class || !conforms_to_protocol (super_class, sub))
6744 check_protocol (sub, type, name);
6745 subs = TREE_CHAIN (subs);
6750 /* Check whether the current interface (accessible via
6751 'objc_implementation_context') actually implements the protocols listed
6752 in PROTO_LIST. */
6754 static void
6755 check_protocols (tree proto_list, const char *type, tree name)
6757 for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
6759 tree p = TREE_VALUE (proto_list);
6761 check_protocol (p, type, name);
6765 /* Make sure that the class CLASS_NAME is defined CODE says which kind
6766 of thing CLASS_NAME ought to be. It can be CLASS_INTERFACE_TYPE,
6767 CLASS_IMPLEMENTATION_TYPE, CATEGORY_INTERFACE_TYPE, or
6768 CATEGORY_IMPLEMENTATION_TYPE. For a CATEGORY_INTERFACE_TYPE,
6769 SUPER_NAME is the name of the category. For a class extension,
6770 CODE is CATEGORY_INTERFACE_TYPE and SUPER_NAME is NULL_TREE. */
6771 static tree
6772 start_class (enum tree_code code, tree class_name, tree super_name,
6773 tree protocol_list, tree attributes)
6775 tree klass = NULL_TREE;
6776 tree decl;
6778 #ifdef OBJCPLUS
6779 if (current_namespace != global_namespace)
6781 error ("Objective-C declarations may only appear in global scope");
6783 #endif /* OBJCPLUS */
6785 if (objc_implementation_context)
6787 warning (0, "%<@end%> missing in implementation context");
6788 finish_class (objc_implementation_context);
6789 objc_ivar_chain = NULL_TREE;
6790 objc_implementation_context = NULL_TREE;
6793 /* If this is a class extension, we'll be "reopening" the existing
6794 CLASS_INTERFACE_TYPE, so in that case there is no need to create
6795 a new node. */
6796 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6798 klass = make_node (code);
6799 TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS);
6802 /* Check for existence of the super class, if one was specified. Note
6803 that we must have seen an @interface, not just a @class. If we
6804 are looking at a @compatibility_alias, traverse it first. */
6805 if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE)
6806 && super_name)
6808 tree super = objc_is_class_name (super_name);
6809 tree super_interface = NULL_TREE;
6811 if (super)
6812 super_interface = lookup_interface (super);
6814 if (!super_interface)
6816 error ("cannot find interface declaration for %qE, superclass of %qE",
6817 super ? super : super_name,
6818 class_name);
6819 super_name = NULL_TREE;
6821 else
6823 if (TREE_DEPRECATED (super_interface))
6824 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
6825 super);
6826 super_name = super;
6830 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6832 CLASS_NAME (klass) = class_name;
6833 CLASS_SUPER_NAME (klass) = super_name;
6834 CLASS_CLS_METHODS (klass) = NULL_TREE;
6837 if (! objc_is_class_name (class_name)
6838 && (decl = lookup_name (class_name)))
6840 error ("%qE redeclared as different kind of symbol",
6841 class_name);
6842 error ("previous declaration of %q+D",
6843 decl);
6846 switch (code)
6848 case CLASS_IMPLEMENTATION_TYPE:
6850 tree chain;
6852 for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
6853 if (TREE_VALUE (chain) == class_name)
6855 error ("reimplementation of class %qE",
6856 class_name);
6857 /* TODO: error message saying where it was previously
6858 implemented. */
6859 break;
6861 if (chain == NULL_TREE)
6862 implemented_classes = tree_cons (NULL_TREE, class_name,
6863 implemented_classes);
6866 /* Reset for multiple classes per file. */
6867 method_slot = 0;
6869 objc_implementation_context = klass;
6871 /* Lookup the interface for this implementation. */
6873 if (!(implementation_template = lookup_interface (class_name)))
6875 warning (0, "cannot find interface declaration for %qE",
6876 class_name);
6877 add_interface (implementation_template = objc_implementation_context,
6878 class_name);
6881 /* If a super class has been specified in the implementation,
6882 insure it conforms to the one specified in the interface. */
6884 if (super_name
6885 && (super_name != CLASS_SUPER_NAME (implementation_template)))
6887 tree previous_name = CLASS_SUPER_NAME (implementation_template);
6888 error ("conflicting super class name %qE",
6889 super_name);
6890 if (previous_name)
6891 error ("previous declaration of %qE", previous_name);
6892 else
6893 error ("previous declaration");
6896 else if (! super_name)
6898 CLASS_SUPER_NAME (objc_implementation_context)
6899 = CLASS_SUPER_NAME (implementation_template);
6901 break;
6903 case CLASS_INTERFACE_TYPE:
6904 if (lookup_interface (class_name))
6905 #ifdef OBJCPLUS
6906 error ("duplicate interface declaration for class %qE", class_name);
6907 #else
6908 warning (0, "duplicate interface declaration for class %qE", class_name);
6909 #endif
6910 else
6911 add_interface (klass, class_name);
6913 if (protocol_list)
6914 CLASS_PROTOCOL_LIST (klass)
6915 = lookup_and_install_protocols (protocol_list, /* definition_required */ true);
6917 if (attributes)
6919 tree attribute;
6920 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
6922 tree name = TREE_PURPOSE (attribute);
6924 /* TODO: Document what the objc_exception attribute is/does. */
6925 /* We handle the 'deprecated' and (undocumented) 'objc_exception'
6926 attributes. */
6927 if (is_attribute_p ("deprecated", name))
6928 TREE_DEPRECATED (klass) = 1;
6929 else if (is_attribute_p ("objc_exception", name))
6930 CLASS_HAS_EXCEPTION_ATTR (klass) = 1;
6931 else
6932 /* Warn about and ignore all others for now, but store them. */
6933 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
6935 TYPE_ATTRIBUTES (klass) = attributes;
6937 break;
6939 case CATEGORY_INTERFACE_TYPE:
6941 tree class_category_is_assoc_with;
6943 /* For a category, class_name is really the name of the class that
6944 the following set of methods will be associated with. We must
6945 find the interface so that can derive the objects template. */
6946 if (!(class_category_is_assoc_with = lookup_interface (class_name)))
6948 error ("cannot find interface declaration for %qE",
6949 class_name);
6950 exit (FATAL_EXIT_CODE);
6952 else
6954 if (TREE_DEPRECATED (class_category_is_assoc_with))
6955 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
6956 class_name);
6958 if (super_name == NULL_TREE)
6960 /* This is a class extension. Get the original
6961 interface, and continue working on it. */
6962 objc_in_class_extension = true;
6963 klass = class_category_is_assoc_with;
6965 if (protocol_list)
6967 /* Append protocols to the original protocol
6968 list. */
6969 CLASS_PROTOCOL_LIST (klass)
6970 = chainon (CLASS_PROTOCOL_LIST (klass),
6971 lookup_and_install_protocols
6972 (protocol_list,
6973 /* definition_required */ true));
6976 else
6978 add_category (class_category_is_assoc_with, klass);
6980 if (protocol_list)
6981 CLASS_PROTOCOL_LIST (klass)
6982 = lookup_and_install_protocols
6983 (protocol_list, /* definition_required */ true);
6987 break;
6989 case CATEGORY_IMPLEMENTATION_TYPE:
6990 /* Reset for multiple classes per file. */
6991 method_slot = 0;
6993 objc_implementation_context = klass;
6995 /* For a category, class_name is really the name of the class that
6996 the following set of methods will be associated with. We must
6997 find the interface so that can derive the objects template. */
6999 if (!(implementation_template = lookup_interface (class_name)))
7001 error ("cannot find interface declaration for %qE",
7002 class_name);
7003 exit (FATAL_EXIT_CODE);
7005 break;
7006 default:
7007 gcc_unreachable ();
7009 return klass;
7012 static tree
7013 continue_class (tree klass)
7015 switch (TREE_CODE (klass))
7017 case CLASS_IMPLEMENTATION_TYPE:
7018 case CATEGORY_IMPLEMENTATION_TYPE:
7020 struct imp_entry *imp_entry;
7022 /* Check consistency of the instance variables. */
7024 if (CLASS_RAW_IVARS (klass))
7025 check_ivars (implementation_template, klass);
7027 /* code generation */
7028 #ifdef OBJCPLUS
7029 push_lang_context (lang_name_c);
7030 #endif
7031 build_private_template (implementation_template);
7032 uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template);
7033 objc_instance_type = build_pointer_type (uprivate_record);
7035 imp_entry = ggc_alloc<struct imp_entry> ();
7037 imp_entry->next = imp_list;
7038 imp_entry->imp_context = klass;
7039 imp_entry->imp_template = implementation_template;
7040 ucls_super_ref = uucls_super_ref = NULL;
7041 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7043 imp_entry->class_decl = (*runtime.class_decl) (klass);
7044 imp_entry->meta_decl = (*runtime.metaclass_decl) (klass);
7046 else
7048 imp_entry->class_decl = (*runtime.category_decl) (klass);
7049 imp_entry->meta_decl = NULL;
7051 imp_entry->has_cxx_cdtors = 0;
7053 /* Append to front and increment count. */
7054 imp_list = imp_entry;
7055 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7056 imp_count++;
7057 else
7058 cat_count++;
7059 #ifdef OBJCPLUS
7060 pop_lang_context ();
7061 #endif /* OBJCPLUS */
7063 return get_class_ivars (implementation_template, true);
7064 break;
7066 case CLASS_INTERFACE_TYPE:
7068 if (objc_in_class_extension)
7069 return NULL_TREE;
7070 #ifdef OBJCPLUS
7071 push_lang_context (lang_name_c);
7072 #endif /* OBJCPLUS */
7073 objc_collecting_ivars = 1;
7074 build_private_template (klass);
7075 objc_collecting_ivars = 0;
7076 #ifdef OBJCPLUS
7077 pop_lang_context ();
7078 #endif /* OBJCPLUS */
7079 return NULL_TREE;
7080 break;
7082 default:
7083 return error_mark_node;
7087 /* This routine builds name of the setter synthesized function. */
7088 char *
7089 objc_build_property_setter_name (tree ident)
7091 /* TODO: Use alloca to allocate buffer of appropriate size. */
7092 static char string[BUFSIZE];
7093 sprintf (string, "set%s:", IDENTIFIER_POINTER (ident));
7094 string[3] = TOUPPER (string[3]);
7095 return string;
7098 /* This routine prepares the declarations of the property accessor
7099 helper functions (objc_getProperty(), etc) that are used when
7100 @synthesize is used.
7102 runtime-specific routines are built in the respective runtime
7103 initialize functions. */
7104 static void
7105 build_common_objc_property_accessor_helpers (void)
7107 tree type;
7109 /* Declare the following function:
7111 objc_getProperty (id self, SEL _cmd,
7112 ptrdiff_t offset, BOOL is_atomic); */
7113 type = build_function_type_list (objc_object_type,
7114 objc_object_type,
7115 objc_selector_type,
7116 ptrdiff_type_node,
7117 boolean_type_node,
7118 NULL_TREE);
7119 objc_getProperty_decl = add_builtin_function ("objc_getProperty",
7120 type, 0, NOT_BUILT_IN,
7121 NULL, NULL_TREE);
7122 TREE_NOTHROW (objc_getProperty_decl) = 0;
7124 /* Declare the following function:
7125 void
7126 objc_setProperty (id self, SEL _cmd,
7127 ptrdiff_t offset, id new_value,
7128 BOOL is_atomic, BOOL should_copy); */
7129 type = build_function_type_list (void_type_node,
7130 objc_object_type,
7131 objc_selector_type,
7132 ptrdiff_type_node,
7133 objc_object_type,
7134 boolean_type_node,
7135 boolean_type_node,
7136 NULL_TREE);
7137 objc_setProperty_decl = add_builtin_function ("objc_setProperty",
7138 type, 0, NOT_BUILT_IN,
7139 NULL, NULL_TREE);
7140 TREE_NOTHROW (objc_setProperty_decl) = 0;
7143 /* This looks up an ivar in a class (including superclasses). */
7144 static tree
7145 lookup_ivar (tree interface, tree instance_variable_name)
7147 while (interface)
7149 tree decl_chain;
7151 for (decl_chain = CLASS_IVARS (interface); decl_chain; decl_chain = DECL_CHAIN (decl_chain))
7152 if (DECL_NAME (decl_chain) == instance_variable_name)
7153 return decl_chain;
7155 /* Not found. Search superclass if any. */
7156 if (CLASS_SUPER_NAME (interface))
7157 interface = lookup_interface (CLASS_SUPER_NAME (interface));
7160 return NULL_TREE;
7163 /* This routine synthesizes a 'getter' method. This is only called
7164 for @synthesize properties. */
7165 static void
7166 objc_synthesize_getter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
7168 location_t location = DECL_SOURCE_LOCATION (property);
7169 tree fn, decl;
7170 tree body;
7171 tree ret_val;
7173 /* If user has implemented a getter with same name then do nothing. */
7174 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7175 PROPERTY_GETTER_NAME (property)))
7176 return;
7178 /* Find declaration of the property getter in the interface (or
7179 superclass, or protocol). There must be one. */
7180 decl = lookup_method_static (klass, PROPERTY_GETTER_NAME (property), 0);
7182 /* If one not declared in the interface, this condition has already
7183 been reported as user error (because property was not declared in
7184 the interface). */
7185 if (!decl)
7186 return;
7188 /* Adapt the 'decl'. Use the source location of the @synthesize
7189 statement for error messages. */
7190 decl = copy_node (decl);
7191 DECL_SOURCE_LOCATION (decl) = location;
7193 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7194 NULL_TREE);
7195 body = c_begin_compound_stmt (true);
7197 /* Now we need to decide how we build the getter. There are three
7198 cases:
7200 for 'copy' or 'retain' properties we need to use the
7201 objc_getProperty() accessor helper which knows about retain and
7202 copy. It supports both 'nonatomic' and 'atomic' access.
7204 for 'nonatomic, assign' properties we can access the instance
7205 variable directly. 'nonatomic' means we don't have to use locks,
7206 and 'assign' means we don't have to worry about retain or copy.
7207 If you combine the two, it means we can just access the instance
7208 variable directly.
7210 for 'atomic, assign' properties we use objc_copyStruct() (for the
7211 next runtime) or objc_getPropertyStruct() (for the GNU runtime). */
7212 switch (PROPERTY_ASSIGN_SEMANTICS (property))
7214 case OBJC_PROPERTY_RETAIN:
7215 case OBJC_PROPERTY_COPY:
7217 /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */
7218 tree cmd, ivar, offset, is_atomic;
7219 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
7221 /* Find the ivar to compute the offset. */
7222 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7223 if (!ivar || is_private (ivar))
7225 /* This should never happen. */
7226 error_at (location,
7227 "can not find instance variable associated with property");
7228 ret_val = error_mark_node;
7229 break;
7231 offset = byte_position (ivar);
7233 if (PROPERTY_NONATOMIC (property))
7234 is_atomic = boolean_false_node;
7235 else
7236 is_atomic = boolean_true_node;
7238 ret_val = build_function_call
7239 (location,
7240 /* Function prototype. */
7241 objc_getProperty_decl,
7242 /* Parameters. */
7243 tree_cons /* self */
7244 (NULL_TREE, self_decl,
7245 tree_cons /* _cmd */
7246 (NULL_TREE, cmd,
7247 tree_cons /* offset */
7248 (NULL_TREE, offset,
7249 tree_cons /* is_atomic */
7250 (NULL_TREE, is_atomic, NULL_TREE)))));
7252 break;
7253 case OBJC_PROPERTY_ASSIGN:
7254 if (PROPERTY_NONATOMIC (property))
7256 /* We build "return self->PROPERTY_IVAR_NAME;" */
7257 ret_val = objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property));
7258 break;
7260 else
7262 /* We build
7263 <property type> __objc_property_temp;
7264 objc_getPropertyStruct (&__objc_property_temp,
7265 &(self->PROPERTY_IVAR_NAME),
7266 sizeof (type of self->PROPERTY_IVAR_NAME),
7267 is_atomic,
7268 false)
7269 return __objc_property_temp;
7271 For the NeXT runtime, we need to use objc_copyStruct
7272 instead of objc_getPropertyStruct. */
7273 tree objc_property_temp_decl, function_decl, function_call;
7274 tree size_of, is_atomic;
7276 objc_property_temp_decl = objc_create_temporary_var (TREE_TYPE (property), "__objc_property_temp");
7277 DECL_SOURCE_LOCATION (objc_property_temp_decl) = location;
7278 objc_property_temp_decl = lang_hooks.decls.pushdecl (objc_property_temp_decl);
7280 /* sizeof (ivar type). Since the ivar and the property have
7281 the same type, there is no need to lookup the ivar. */
7282 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7283 true /* is_sizeof */,
7284 false /* min_alignof */,
7285 false /* complain */);
7287 if (PROPERTY_NONATOMIC (property))
7288 is_atomic = boolean_false_node;
7289 else
7290 is_atomic = boolean_true_node;
7292 if (objc_copyStruct_decl)
7293 function_decl = objc_copyStruct_decl;
7294 else
7295 function_decl = objc_getPropertyStruct_decl;
7297 function_call = build_function_call
7298 (location,
7299 /* Function prototype. */
7300 function_decl,
7301 /* Parameters. */
7302 tree_cons /* &__objc_property_temp_decl */
7303 /* Warning: note that using build_fold_addr_expr_loc()
7304 here causes invalid code to be generated. */
7305 (NULL_TREE, build_unary_op (location, ADDR_EXPR, objc_property_temp_decl, 0),
7306 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
7307 (NULL_TREE, build_fold_addr_expr_loc (location,
7308 objc_lookup_ivar
7309 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7310 tree_cons /* sizeof (PROPERTY_IVAR) */
7311 (NULL_TREE, size_of,
7312 tree_cons /* is_atomic */
7313 (NULL_TREE, is_atomic,
7314 /* TODO: This is currently ignored by the GNU
7315 runtime, but what about the next one ? */
7316 tree_cons /* has_strong */
7317 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7319 add_stmt (function_call);
7321 ret_val = objc_property_temp_decl;
7323 break;
7324 default:
7325 gcc_unreachable ();
7328 gcc_assert (ret_val);
7330 #ifdef OBJCPLUS
7331 finish_return_stmt (ret_val);
7332 #else
7333 c_finish_return (location, ret_val, NULL_TREE);
7334 #endif
7336 add_stmt (c_end_compound_stmt (location, body, true));
7337 fn = current_function_decl;
7338 #ifdef OBJCPLUS
7339 finish_function ();
7340 #endif
7341 objc_finish_method_definition (fn);
7344 /* This routine synthesizes a 'setter' method. */
7346 static void
7347 objc_synthesize_setter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
7349 location_t location = DECL_SOURCE_LOCATION (property);
7350 tree fn, decl;
7351 tree body;
7352 tree new_value, statement;
7354 /* If user has implemented a setter with same name then do nothing. */
7355 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7356 PROPERTY_SETTER_NAME (property)))
7357 return;
7359 /* Find declaration of the property setter in the interface (or
7360 superclass, or protocol). There must be one. */
7361 decl = lookup_method_static (klass, PROPERTY_SETTER_NAME (property), 0);
7363 /* If one not declared in the interface, this condition has already
7364 been reported as user error (because property was not declared in
7365 the interface). */
7366 if (!decl)
7367 return;
7369 /* Adapt the 'decl'. Use the source location of the @synthesize
7370 statement for error messages. */
7371 decl = copy_node (decl);
7372 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (property);
7374 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7375 NULL_TREE);
7377 body = c_begin_compound_stmt (true);
7379 /* The 'new_value' is the only argument to the method, which is the
7380 3rd argument of the function, after self and _cmd. We use twice
7381 TREE_CHAIN to move forward two arguments. */
7382 new_value = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)));
7384 /* This would presumably happen if the user has specified a
7385 prototype for the setter that does not have an argument! */
7386 if (new_value == NULL_TREE)
7388 /* TODO: This should be caught much earlier than this. */
7389 error_at (DECL_SOURCE_LOCATION (decl), "invalid setter, it must have one argument");
7390 /* Try to recover somehow. */
7391 new_value = error_mark_node;
7394 /* Now we need to decide how we build the setter. There are three
7395 cases:
7397 for 'copy' or 'retain' properties we need to use the
7398 objc_setProperty() accessor helper which knows about retain and
7399 copy. It supports both 'nonatomic' and 'atomic' access.
7401 for 'nonatomic, assign' properties we can access the instance
7402 variable directly. 'nonatomic' means we don't have to use locks,
7403 and 'assign' means we don't have to worry about retain or copy.
7404 If you combine the two, it means we can just access the instance
7405 variable directly.
7407 for 'atomic, assign' properties we use objc_copyStruct() (for the
7408 next runtime) or objc_setPropertyStruct() (for the GNU runtime). */
7409 switch (PROPERTY_ASSIGN_SEMANTICS (property))
7411 case OBJC_PROPERTY_RETAIN:
7412 case OBJC_PROPERTY_COPY:
7414 /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */
7415 tree cmd, ivar, offset, is_atomic, should_copy;
7416 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
7418 /* Find the ivar to compute the offset. */
7419 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7420 if (!ivar || is_private (ivar))
7422 error_at (location,
7423 "can not find instance variable associated with property");
7424 statement = error_mark_node;
7425 break;
7427 offset = byte_position (ivar);
7429 if (PROPERTY_NONATOMIC (property))
7430 is_atomic = boolean_false_node;
7431 else
7432 is_atomic = boolean_true_node;
7434 if (PROPERTY_ASSIGN_SEMANTICS (property) == OBJC_PROPERTY_COPY)
7435 should_copy = boolean_true_node;
7436 else
7437 should_copy = boolean_false_node;
7439 statement = build_function_call
7440 (location,
7441 /* Function prototype. */
7442 objc_setProperty_decl,
7443 /* Parameters. */
7444 tree_cons /* self */
7445 (NULL_TREE, self_decl,
7446 tree_cons /* _cmd */
7447 (NULL_TREE, cmd,
7448 tree_cons /* offset */
7449 (NULL_TREE, offset,
7450 tree_cons /* new_value */
7451 (NULL_TREE, new_value,
7452 tree_cons /* is_atomic */
7453 (NULL_TREE, is_atomic,
7454 tree_cons /* should_copy */
7455 (NULL_TREE, should_copy, NULL_TREE)))))));
7457 break;
7458 case OBJC_PROPERTY_ASSIGN:
7459 if (PROPERTY_NONATOMIC (property))
7461 /* We build "self->PROPERTY_IVAR_NAME = new_value;" */
7462 statement = build_modify_expr
7463 (location,
7464 objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)),
7465 NULL_TREE, NOP_EXPR,
7466 location, new_value, NULL_TREE);
7467 break;
7469 else
7471 /* We build
7472 objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME),
7473 &new_value,
7474 sizeof (type of self->PROPERTY_IVAR_NAME),
7475 is_atomic,
7476 false)
7478 For the NeXT runtime, we need to use objc_copyStruct
7479 instead of objc_getPropertyStruct. */
7480 tree function_decl, size_of, is_atomic;
7482 /* sizeof (ivar type). Since the ivar and the property have
7483 the same type, there is no need to lookup the ivar. */
7484 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7485 true /* is_sizeof */,
7486 false /* min_alignof */,
7487 false /* complain */);
7489 if (PROPERTY_NONATOMIC (property))
7490 is_atomic = boolean_false_node;
7491 else
7492 is_atomic = boolean_true_node;
7494 if (objc_copyStruct_decl)
7495 function_decl = objc_copyStruct_decl;
7496 else
7497 function_decl = objc_setPropertyStruct_decl;
7499 statement = build_function_call
7500 (location,
7501 /* Function prototype. */
7502 function_decl,
7503 /* Parameters. */
7504 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
7505 (NULL_TREE, build_fold_addr_expr_loc (location,
7506 objc_lookup_ivar
7507 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7508 tree_cons /* &new_value */
7509 (NULL_TREE, build_fold_addr_expr_loc (location, new_value),
7510 tree_cons /* sizeof (PROPERTY_IVAR) */
7511 (NULL_TREE, size_of,
7512 tree_cons /* is_atomic */
7513 (NULL_TREE, is_atomic,
7514 /* TODO: This is currently ignored by the GNU
7515 runtime, but what about the next one ? */
7516 tree_cons /* has_strong */
7517 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7519 break;
7520 default:
7521 gcc_unreachable ();
7523 gcc_assert (statement);
7525 add_stmt (statement);
7526 add_stmt (c_end_compound_stmt (location, body, true));
7527 fn = current_function_decl;
7528 #ifdef OBJCPLUS
7529 finish_function ();
7530 #endif
7531 objc_finish_method_definition (fn);
7534 /* This function is a sub-routine of objc_add_synthesize_declaration.
7535 It is called for each property to synthesize once we have
7536 determined that the context is Ok. */
7537 static void
7538 objc_add_synthesize_declaration_for_property (location_t location, tree interface,
7539 tree property_name, tree ivar_name)
7541 /* Find the @property declaration. */
7542 tree property;
7543 tree x;
7545 /* Check that synthesize or dynamic has not already been used for
7546 the same property. */
7547 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7548 if (PROPERTY_NAME (property) == property_name)
7550 location_t original_location = DECL_SOURCE_LOCATION (property);
7552 if (PROPERTY_DYNAMIC (property))
7553 error_at (location, "property %qs already specified in %<@dynamic%>",
7554 IDENTIFIER_POINTER (property_name));
7555 else
7556 error_at (location, "property %qs already specified in %<@synthesize%>",
7557 IDENTIFIER_POINTER (property_name));
7559 if (original_location != UNKNOWN_LOCATION)
7560 inform (original_location, "originally specified here");
7561 return;
7564 /* Check that the property is declared in the interface. It could
7565 also be declared in a superclass or protocol. */
7566 property = lookup_property (interface, property_name);
7568 if (!property)
7570 error_at (location, "no declaration of property %qs found in the interface",
7571 IDENTIFIER_POINTER (property_name));
7572 return;
7574 else
7576 /* We have to copy the property, because we want to chain it to
7577 the implementation context, and we want to store the source
7578 location of the @synthesize, not of the original
7579 @property. */
7580 property = copy_node (property);
7581 DECL_SOURCE_LOCATION (property) = location;
7584 /* Determine PROPERTY_IVAR_NAME. */
7585 if (ivar_name == NULL_TREE)
7586 ivar_name = property_name;
7588 /* Check that the instance variable exists. You can only use an
7589 instance variable from the same class, not one from the
7590 superclass (this makes sense as it allows us to check that an
7591 instance variable is only used in one synthesized property). */
7593 tree ivar = is_ivar (CLASS_IVARS (interface), ivar_name);
7594 tree type_of_ivar;
7595 if (!ivar)
7597 error_at (location, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar",
7598 IDENTIFIER_POINTER (property_name));
7599 return;
7602 if (DECL_BIT_FIELD_TYPE (ivar))
7603 type_of_ivar = DECL_BIT_FIELD_TYPE (ivar);
7604 else
7605 type_of_ivar = TREE_TYPE (ivar);
7607 /* If the instance variable has a different C type, we throw an error ... */
7608 if (!comptypes (TREE_TYPE (property), type_of_ivar)
7609 /* ... unless the property is readonly, in which case we allow
7610 the instance variable to be more specialized (this means we
7611 can generate the getter all right and it works). */
7612 && (!PROPERTY_READONLY (property)
7613 || !objc_compare_types (TREE_TYPE (property),
7614 type_of_ivar, -5, NULL_TREE)))
7616 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7618 error_at (location, "property %qs is using instance variable %qs of incompatible type",
7619 IDENTIFIER_POINTER (property_name),
7620 IDENTIFIER_POINTER (ivar_name));
7622 if (original_location != UNKNOWN_LOCATION)
7623 inform (original_location, "originally specified here");
7626 /* If the instance variable is a bitfield, the property must be
7627 'assign', 'nonatomic' because the runtime getter/setter helper
7628 do not work with bitfield instance variables. */
7629 if (DECL_BIT_FIELD_TYPE (ivar))
7631 /* If there is an error, we return and not generate any
7632 getter/setter because trying to set up the runtime
7633 getter/setter helper calls with bitfields is at high risk
7634 of ICE. */
7636 if (PROPERTY_ASSIGN_SEMANTICS (property) != OBJC_PROPERTY_ASSIGN)
7638 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7640 error_at (location, "'assign' property %qs is using bit-field instance variable %qs",
7641 IDENTIFIER_POINTER (property_name),
7642 IDENTIFIER_POINTER (ivar_name));
7644 if (original_location != UNKNOWN_LOCATION)
7645 inform (original_location, "originally specified here");
7646 return;
7649 if (!PROPERTY_NONATOMIC (property))
7651 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7653 error_at (location, "'atomic' property %qs is using bit-field instance variable %qs",
7654 IDENTIFIER_POINTER (property_name),
7655 IDENTIFIER_POINTER (ivar_name));
7657 if (original_location != UNKNOWN_LOCATION)
7658 inform (original_location, "originally specified here");
7659 return;
7664 /* Check that no other property is using the same instance
7665 variable. */
7666 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7667 if (PROPERTY_IVAR_NAME (x) == ivar_name)
7669 location_t original_location = DECL_SOURCE_LOCATION (x);
7671 error_at (location, "property %qs is using the same instance variable as property %qs",
7672 IDENTIFIER_POINTER (property_name),
7673 IDENTIFIER_POINTER (PROPERTY_NAME (x)));
7675 if (original_location != UNKNOWN_LOCATION)
7676 inform (original_location, "originally specified here");
7678 /* We keep going on. This won't cause the compiler to fail;
7679 the failure would most likely be at runtime. */
7682 /* Note that a @synthesize (and only a @synthesize) always sets
7683 PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a
7684 @synthesize by that. */
7685 PROPERTY_IVAR_NAME (property) = ivar_name;
7687 /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the
7688 original declaration; they are always set (with the exception of
7689 PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */
7691 /* Add the property to the list of properties for current implementation. */
7692 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7693 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7695 /* Note how we don't actually synthesize the getter/setter here; it
7696 would be very natural, but we may miss the fact that the user has
7697 implemented his own getter/setter later on in the @implementation
7698 (in which case we shouldn't generate getter/setter). We wait
7699 until we have parsed it all before generating the code. */
7702 /* This function is called by the parser after a @synthesize
7703 expression is parsed. 'location' is the location of the
7704 @synthesize expression, and 'property_and_ivar_list' is a chained
7705 list of the property and ivar names. */
7706 void
7707 objc_add_synthesize_declaration (location_t location, tree property_and_ivar_list)
7709 tree interface, chain;
7711 if (flag_objc1_only)
7712 error_at (input_location, "%<@synthesize%> is not available in Objective-C 1.0");
7714 if (property_and_ivar_list == error_mark_node)
7715 return;
7717 if (!objc_implementation_context)
7719 /* We can get here only in Objective-C; the Objective-C++ parser
7720 detects the problem while parsing, outputs the error
7721 "misplaced '@synthesize' Objective-C++ construct" and skips
7722 the declaration. */
7723 error_at (location, "%<@synthesize%> not in @implementation context");
7724 return;
7727 if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE)
7729 error_at (location, "%<@synthesize%> can not be used in categories");
7730 return;
7733 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7734 if (!interface)
7736 /* I can't see how this could happen, but it is good as a safety check. */
7737 error_at (location,
7738 "%<@synthesize%> requires the @interface of the class to be available");
7739 return;
7742 /* Now, iterate over the properties and do each of them. */
7743 for (chain = property_and_ivar_list; chain; chain = TREE_CHAIN (chain))
7745 objc_add_synthesize_declaration_for_property (location, interface, TREE_VALUE (chain),
7746 TREE_PURPOSE (chain));
7750 /* This function is a sub-routine of objc_add_dynamic_declaration. It
7751 is called for each property to mark as dynamic once we have
7752 determined that the context is Ok. */
7753 static void
7754 objc_add_dynamic_declaration_for_property (location_t location, tree interface,
7755 tree property_name)
7757 /* Find the @property declaration. */
7758 tree property;
7760 /* Check that synthesize or dynamic has not already been used for
7761 the same property. */
7762 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7763 if (PROPERTY_NAME (property) == property_name)
7765 location_t original_location = DECL_SOURCE_LOCATION (property);
7767 if (PROPERTY_DYNAMIC (property))
7768 error_at (location, "property %qs already specified in %<@dynamic%>",
7769 IDENTIFIER_POINTER (property_name));
7770 else
7771 error_at (location, "property %qs already specified in %<@synthesize%>",
7772 IDENTIFIER_POINTER (property_name));
7774 if (original_location != UNKNOWN_LOCATION)
7775 inform (original_location, "originally specified here");
7776 return;
7779 /* Check that the property is declared in the interface. It could
7780 also be declared in a superclass or protocol. */
7781 property = lookup_property (interface, property_name);
7783 if (!property)
7785 error_at (location, "no declaration of property %qs found in the interface",
7786 IDENTIFIER_POINTER (property_name));
7787 return;
7789 else
7791 /* We have to copy the property, because we want to chain it to
7792 the implementation context, and we want to store the source
7793 location of the @synthesize, not of the original
7794 @property. */
7795 property = copy_node (property);
7796 DECL_SOURCE_LOCATION (property) = location;
7799 /* Note that a @dynamic (and only a @dynamic) always sets
7800 PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that.
7801 (actually, as explained above, PROPERTY_DECL generated by
7802 @property and associated with a @dynamic property are also marked
7803 as PROPERTY_DYNAMIC). */
7804 PROPERTY_DYNAMIC (property) = 1;
7806 /* Add the property to the list of properties for current implementation. */
7807 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7808 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7811 /* This function is called by the parser after a @dynamic expression
7812 is parsed. 'location' is the location of the @dynamic expression,
7813 and 'property_list' is a chained list of all the property
7814 names. */
7815 void
7816 objc_add_dynamic_declaration (location_t location, tree property_list)
7818 tree interface, chain;
7820 if (flag_objc1_only)
7821 error_at (input_location, "%<@dynamic%> is not available in Objective-C 1.0");
7823 if (property_list == error_mark_node)
7824 return;
7826 if (!objc_implementation_context)
7828 /* We can get here only in Objective-C; the Objective-C++ parser
7829 detects the problem while parsing, outputs the error
7830 "misplaced '@dynamic' Objective-C++ construct" and skips the
7831 declaration. */
7832 error_at (location, "%<@dynamic%> not in @implementation context");
7833 return;
7836 /* @dynamic is allowed in categories. */
7837 switch (TREE_CODE (objc_implementation_context))
7839 case CLASS_IMPLEMENTATION_TYPE:
7840 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7841 break;
7842 case CATEGORY_IMPLEMENTATION_TYPE:
7843 interface = lookup_category (implementation_template,
7844 CLASS_SUPER_NAME (objc_implementation_context));
7845 break;
7846 default:
7847 gcc_unreachable ();
7850 if (!interface)
7852 /* I can't see how this could happen, but it is good as a safety check. */
7853 error_at (location,
7854 "%<@dynamic%> requires the @interface of the class to be available");
7855 return;
7858 /* Now, iterate over the properties and do each of them. */
7859 for (chain = property_list; chain; chain = TREE_CHAIN (chain))
7861 objc_add_dynamic_declaration_for_property (location, interface, TREE_VALUE (chain));
7865 /* Main routine to generate code/data for all the property information for
7866 current implementation (class or category). CLASS is the interface where
7867 ivars are declared. CLASS_METHODS is where methods are found which
7868 could be a class or a category depending on whether we are implementing
7869 property of a class or a category. */
7871 static void
7872 objc_gen_property_data (tree klass, tree class_methods)
7874 tree x;
7876 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7878 /* @dynamic property - nothing to check or synthesize. */
7879 if (PROPERTY_DYNAMIC (x))
7880 continue;
7882 /* @synthesize property - need to synthesize the accessors. */
7883 if (PROPERTY_IVAR_NAME (x))
7885 objc_synthesize_getter (klass, class_methods, x);
7887 if (PROPERTY_READONLY (x) == 0)
7888 objc_synthesize_setter (klass, class_methods, x);
7890 continue;
7893 gcc_unreachable ();
7897 /* This is called once we see the "@end" in an interface/implementation. */
7899 static void
7900 finish_class (tree klass)
7902 switch (TREE_CODE (klass))
7904 case CLASS_IMPLEMENTATION_TYPE:
7906 /* All metadata generation is done in runtime.generate_metadata(). */
7908 /* Generate what needed for property; setters, getters, etc. */
7909 objc_gen_property_data (implementation_template, implementation_template);
7911 if (implementation_template != objc_implementation_context)
7913 /* Ensure that all method listed in the interface contain bodies. */
7914 check_methods (CLASS_CLS_METHODS (implementation_template),
7915 objc_implementation_context, '+');
7916 check_methods (CLASS_NST_METHODS (implementation_template),
7917 objc_implementation_context, '-');
7919 if (CLASS_PROTOCOL_LIST (implementation_template))
7920 check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
7921 "class",
7922 CLASS_NAME (objc_implementation_context));
7924 break;
7926 case CATEGORY_IMPLEMENTATION_TYPE:
7928 tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass));
7930 if (category)
7932 /* Generate what needed for property; setters, getters, etc. */
7933 objc_gen_property_data (implementation_template, category);
7935 /* Ensure all method listed in the interface contain bodies. */
7936 check_methods (CLASS_CLS_METHODS (category),
7937 objc_implementation_context, '+');
7938 check_methods (CLASS_NST_METHODS (category),
7939 objc_implementation_context, '-');
7941 if (CLASS_PROTOCOL_LIST (category))
7942 check_protocols (CLASS_PROTOCOL_LIST (category),
7943 "category",
7944 CLASS_SUPER_NAME (objc_implementation_context));
7946 break;
7948 case CLASS_INTERFACE_TYPE:
7949 case CATEGORY_INTERFACE_TYPE:
7950 case PROTOCOL_INTERFACE_TYPE:
7952 /* Process properties of the class. */
7953 tree x;
7954 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
7956 /* Now we check that the appropriate getter is declared,
7957 and if not, we declare one ourselves. */
7958 tree getter_decl = lookup_method (CLASS_NST_METHODS (klass),
7959 PROPERTY_GETTER_NAME (x));
7961 if (getter_decl)
7963 /* TODO: Check that the declaration is consistent with the property. */
7966 else
7968 /* Generate an instance method declaration for the
7969 getter; for example "- (id) name;". In general it
7970 will be of the form
7971 -(type)property_getter_name; */
7972 tree rettype = build_tree_list (NULL_TREE, TREE_TYPE (x));
7973 getter_decl = build_method_decl (INSTANCE_METHOD_DECL,
7974 rettype, PROPERTY_GETTER_NAME (x),
7975 NULL_TREE, false);
7976 if (PROPERTY_OPTIONAL (x))
7977 objc_add_method (objc_interface_context, getter_decl, false, true);
7978 else
7979 objc_add_method (objc_interface_context, getter_decl, false, false);
7980 TREE_DEPRECATED (getter_decl) = TREE_DEPRECATED (x);
7981 METHOD_PROPERTY_CONTEXT (getter_decl) = x;
7984 if (PROPERTY_READONLY (x) == 0)
7986 /* Now we check that the appropriate setter is declared,
7987 and if not, we declare on ourselves. */
7988 tree setter_decl = lookup_method (CLASS_NST_METHODS (klass),
7989 PROPERTY_SETTER_NAME (x));
7991 if (setter_decl)
7993 /* TODO: Check that the declaration is consistent with the property. */
7996 else
7998 /* The setter name is something like 'setName:'.
7999 We need the substring 'setName' to build the
8000 method declaration due to how the declaration
8001 works. TODO: build_method_decl() will then
8002 generate back 'setName:' from 'setName'; it
8003 would be more efficient to hook into there. */
8004 const char *full_setter_name = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x));
8005 size_t length = strlen (full_setter_name);
8006 char *setter_name = (char *) alloca (length);
8007 tree ret_type, selector, arg_type, arg_name;
8009 strcpy (setter_name, full_setter_name);
8010 setter_name[length - 1] = '\0';
8011 ret_type = build_tree_list (NULL_TREE, void_type_node);
8012 arg_type = build_tree_list (NULL_TREE, TREE_TYPE (x));
8013 arg_name = get_identifier ("_value");
8014 selector = objc_build_keyword_decl (get_identifier (setter_name),
8015 arg_type, arg_name, NULL);
8016 setter_decl = build_method_decl (INSTANCE_METHOD_DECL,
8017 ret_type, selector,
8018 build_tree_list (NULL_TREE, NULL_TREE),
8019 false);
8020 if (PROPERTY_OPTIONAL (x))
8021 objc_add_method (objc_interface_context, setter_decl, false, true);
8022 else
8023 objc_add_method (objc_interface_context, setter_decl, false, false);
8024 TREE_DEPRECATED (setter_decl) = TREE_DEPRECATED (x);
8025 METHOD_PROPERTY_CONTEXT (setter_decl) = x;
8029 break;
8031 default:
8032 gcc_unreachable ();
8033 break;
8037 static tree
8038 add_protocol (tree protocol)
8040 /* Put protocol on list in reverse order. */
8041 TREE_CHAIN (protocol) = protocol_chain;
8042 protocol_chain = protocol;
8043 return protocol_chain;
8046 /* Check that a protocol is defined, and, recursively, that all
8047 protocols that this protocol conforms to are defined too. */
8048 static void
8049 check_that_protocol_is_defined (tree protocol)
8051 if (!PROTOCOL_DEFINED (protocol))
8052 warning (0, "definition of protocol %qE not found",
8053 PROTOCOL_NAME (protocol));
8055 /* If the protocol itself conforms to other protocols, check them
8056 too, recursively. */
8057 if (PROTOCOL_LIST (protocol))
8059 tree p;
8061 for (p = PROTOCOL_LIST (protocol); p; p = TREE_CHAIN (p))
8062 check_that_protocol_is_defined (TREE_VALUE (p));
8066 /* Looks up a protocol. If 'warn_if_deprecated' is true, a warning is
8067 emitted if the protocol is deprecated. If 'definition_required' is
8068 true, a warning is emitted if a full @protocol definition has not
8069 been seen. */
8070 static tree
8071 lookup_protocol (tree ident, bool warn_if_deprecated, bool definition_required)
8073 tree chain;
8075 for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
8076 if (ident == PROTOCOL_NAME (chain))
8078 if (warn_if_deprecated && TREE_DEPRECATED (chain))
8080 /* It would be nice to use warn_deprecated_use() here, but
8081 we are using TREE_CHAIN (which is supposed to be the
8082 TYPE_STUB_DECL for a TYPE) for something different. */
8083 warning (OPT_Wdeprecated_declarations, "protocol %qE is deprecated",
8084 PROTOCOL_NAME (chain));
8087 if (definition_required)
8088 check_that_protocol_is_defined (chain);
8090 return chain;
8093 return NULL_TREE;
8096 /* This function forward declares the protocols named by NAMES. If
8097 they are already declared or defined, the function has no effect. */
8099 void
8100 objc_declare_protocol (tree name, tree attributes)
8102 bool deprecated = false;
8104 #ifdef OBJCPLUS
8105 if (current_namespace != global_namespace) {
8106 error ("Objective-C declarations may only appear in global scope");
8108 #endif /* OBJCPLUS */
8110 /* Determine if 'deprecated', the only attribute we recognize for
8111 protocols, was used. Ignore all other attributes. */
8112 if (attributes)
8114 tree attribute;
8115 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8117 tree name = TREE_PURPOSE (attribute);
8119 if (is_attribute_p ("deprecated", name))
8120 deprecated = true;
8121 else
8122 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8126 if (lookup_protocol (name, /* warn if deprecated */ false,
8127 /* definition_required */ false) == NULL_TREE)
8129 tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
8131 TYPE_LANG_SLOT_1 (protocol)
8132 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8133 PROTOCOL_NAME (protocol) = name;
8134 PROTOCOL_LIST (protocol) = NULL_TREE;
8135 add_protocol (protocol);
8136 PROTOCOL_DEFINED (protocol) = 0;
8137 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8139 if (attributes)
8141 /* TODO: Do we need to store the attributes here ? */
8142 TYPE_ATTRIBUTES (protocol) = attributes;
8143 if (deprecated)
8144 TREE_DEPRECATED (protocol) = 1;
8149 static tree
8150 start_protocol (enum tree_code code, tree name, tree list, tree attributes)
8152 tree protocol;
8153 bool deprecated = false;
8155 #ifdef OBJCPLUS
8156 if (current_namespace != global_namespace) {
8157 error ("Objective-C declarations may only appear in global scope");
8159 #endif /* OBJCPLUS */
8161 /* Determine if 'deprecated', the only attribute we recognize for
8162 protocols, was used. Ignore all other attributes. */
8163 if (attributes)
8165 tree attribute;
8166 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8168 tree name = TREE_PURPOSE (attribute);
8170 if (is_attribute_p ("deprecated", name))
8171 deprecated = true;
8172 else
8173 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8177 protocol = lookup_protocol (name, /* warn_if_deprecated */ false,
8178 /* definition_required */ false);
8180 if (!protocol)
8182 protocol = make_node (code);
8183 TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8185 PROTOCOL_NAME (protocol) = name;
8186 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8187 add_protocol (protocol);
8188 PROTOCOL_DEFINED (protocol) = 1;
8189 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8191 check_protocol_recursively (protocol, list);
8193 else if (! PROTOCOL_DEFINED (protocol))
8195 PROTOCOL_DEFINED (protocol) = 1;
8196 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8198 check_protocol_recursively (protocol, list);
8200 else
8202 warning (0, "duplicate declaration for protocol %qE",
8203 name);
8206 if (attributes)
8208 TYPE_ATTRIBUTES (protocol) = attributes;
8209 if (deprecated)
8210 TREE_DEPRECATED (protocol) = 1;
8213 return protocol;
8216 /* Decay array and function parameters into pointers. */
8218 static tree
8219 objc_decay_parm_type (tree type)
8221 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE)
8222 type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE
8223 ? TREE_TYPE (type)
8224 : type);
8226 return type;
8229 static GTY(()) tree objc_parmlist = NULL_TREE;
8231 /* Append PARM to a list of formal parameters of a method, making a necessary
8232 array-to-pointer adjustment along the way. */
8234 void
8235 objc_push_parm (tree parm)
8237 tree type;
8239 if (TREE_TYPE (parm) == error_mark_node)
8241 objc_parmlist = chainon (objc_parmlist, parm);
8242 return;
8245 /* Decay arrays and functions into pointers. */
8246 type = objc_decay_parm_type (TREE_TYPE (parm));
8248 /* If the parameter type has been decayed, a new PARM_DECL needs to be
8249 built as well. */
8250 if (type != TREE_TYPE (parm))
8251 parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type);
8253 DECL_ARG_TYPE (parm)
8254 = lang_hooks.types.type_promotes_to (TREE_TYPE (parm));
8256 /* Record constancy and volatility. */
8257 c_apply_type_quals_to_decl
8258 ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0)
8259 | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0)
8260 | (TYPE_ATOMIC (TREE_TYPE (parm)) ? TYPE_QUAL_ATOMIC : 0)
8261 | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm);
8263 objc_parmlist = chainon (objc_parmlist, parm);
8266 /* Retrieve the formal parameter list constructed via preceding calls to
8267 objc_push_parm(). */
8269 #ifdef OBJCPLUS
8270 tree
8271 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED,
8272 tree expr ATTRIBUTE_UNUSED)
8274 tree parm_info = objc_parmlist;
8275 objc_parmlist = NULL_TREE;
8277 return parm_info;
8279 #else
8280 struct c_arg_info *
8281 objc_get_parm_info (int have_ellipsis, tree expr)
8283 tree parm_info = objc_parmlist;
8284 struct c_arg_info *arg_info;
8285 /* The C front-end requires an elaborate song and dance at
8286 this point. */
8287 push_scope ();
8288 declare_parm_level ();
8289 while (parm_info)
8291 tree next = DECL_CHAIN (parm_info);
8293 DECL_CHAIN (parm_info) = NULL_TREE;
8294 parm_info = pushdecl (parm_info);
8295 finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
8296 parm_info = next;
8298 arg_info = get_parm_info (have_ellipsis, expr);
8299 pop_scope ();
8300 objc_parmlist = NULL_TREE;
8301 return arg_info;
8303 #endif
8305 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
8306 method definitions. In the case of instance methods, we can be more
8307 specific as to the type of 'self'. */
8309 static void
8310 synth_self_and_ucmd_args (void)
8312 tree self_type;
8314 if (objc_method_context
8315 && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL)
8316 self_type = objc_instance_type;
8317 else
8318 /* Really a `struct objc_class *'. However, we allow people to
8319 assign to self, which changes its type midstream. */
8320 self_type = objc_object_type;
8322 /* id self; */
8323 objc_push_parm (build_decl (input_location,
8324 PARM_DECL, self_id, self_type));
8326 /* SEL _cmd; */
8327 objc_push_parm (build_decl (input_location,
8328 PARM_DECL, ucmd_id, objc_selector_type));
8331 /* Transform an Objective-C method definition into a static C function
8332 definition, synthesizing the first two arguments, "self" and "_cmd",
8333 in the process. EXPR is NULL or an expression that needs to be
8334 evaluated for the side effects of array size expressions in the
8335 parameters. */
8337 static void
8338 start_method_def (tree method, tree expr)
8340 tree parmlist;
8341 #ifdef OBJCPLUS
8342 tree parm_info;
8343 #else
8344 struct c_arg_info *parm_info;
8345 #endif
8346 int have_ellipsis = 0;
8348 /* If we are defining a "dealloc" method in a non-root class, we
8349 will need to check if a [super dealloc] is missing, and warn if
8350 it is. */
8351 if(CLASS_SUPER_NAME (objc_implementation_context)
8352 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method))))
8353 should_call_super_dealloc = 1;
8354 else
8355 should_call_super_dealloc = 0;
8357 /* Required to implement _msgSuper. */
8358 objc_method_context = method;
8359 UOBJC_SUPER_decl = NULL_TREE;
8361 /* Generate prototype declarations for arguments..."new-style". */
8362 synth_self_and_ucmd_args ();
8364 /* Generate argument declarations if a keyword_decl. */
8365 parmlist = METHOD_SEL_ARGS (method);
8366 while (parmlist)
8368 /* parmlist is a KEYWORD_DECL. */
8369 tree type = TREE_VALUE (TREE_TYPE (parmlist));
8370 tree parm;
8372 parm = build_decl (input_location,
8373 PARM_DECL, KEYWORD_ARG_NAME (parmlist), type);
8374 decl_attributes (&parm, DECL_ATTRIBUTES (parmlist), 0);
8375 objc_push_parm (parm);
8376 parmlist = DECL_CHAIN (parmlist);
8379 if (METHOD_ADD_ARGS (method))
8381 tree akey;
8383 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method));
8384 akey; akey = TREE_CHAIN (akey))
8386 objc_push_parm (TREE_VALUE (akey));
8389 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
8390 have_ellipsis = 1;
8393 parm_info = objc_get_parm_info (have_ellipsis, expr);
8395 really_start_method (objc_method_context, parm_info);
8398 /* Return 1 if TYPE1 is equivalent to TYPE2 for purposes of method
8399 overloading. */
8400 static int
8401 objc_types_are_equivalent (tree type1, tree type2)
8403 if (type1 == type2)
8404 return 1;
8406 /* Strip away indirections. */
8407 while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE)
8408 && (TREE_CODE (type1) == TREE_CODE (type2)))
8409 type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2);
8410 if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
8411 return 0;
8413 /* Compare the protocol lists. */
8414 type1 = (TYPE_HAS_OBJC_INFO (type1)
8415 ? TYPE_OBJC_PROTOCOL_LIST (type1)
8416 : NULL_TREE);
8417 type2 = (TYPE_HAS_OBJC_INFO (type2)
8418 ? TYPE_OBJC_PROTOCOL_LIST (type2)
8419 : NULL_TREE);
8421 /* If there are no protocols (most common case), the types are
8422 identical. */
8423 if (type1 == NULL_TREE && type2 == NULL_TREE)
8424 return 1;
8426 /* If one has protocols, and the other one hasn't, they are not
8427 identical. */
8428 if ((type1 == NULL_TREE && type2 != NULL_TREE)
8429 || (type1 != NULL_TREE && type2 == NULL_TREE))
8430 return 0;
8431 else
8433 /* Else, both have protocols, and we need to do the full
8434 comparison. It is possible that either type1 or type2
8435 contain some duplicate protocols in the list, so we can't
8436 even just compare list_length as a first check. */
8437 tree t;
8439 for (t = type2; t; t = TREE_CHAIN (t))
8440 if (!lookup_protocol_in_reflist (type1, TREE_VALUE (t)))
8441 return 0;
8443 for (t = type1; t; t = TREE_CHAIN (t))
8444 if (!lookup_protocol_in_reflist (type2, TREE_VALUE (t)))
8445 return 0;
8447 return 1;
8451 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
8453 static int
8454 objc_types_share_size_and_alignment (tree type1, tree type2)
8456 return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
8457 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2));
8460 /* Return 1 if PROTO1 is equivalent to PROTO2
8461 for purposes of method overloading. Ordinarily, the type signatures
8462 should match up exactly, unless STRICT is zero, in which case we
8463 shall allow differences in which the size and alignment of a type
8464 is the same. */
8466 static int
8467 comp_proto_with_proto (tree proto1, tree proto2, int strict)
8469 tree type1, type2;
8471 /* The following test is needed in case there are hashing
8472 collisions. */
8473 if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2))
8474 return 0;
8476 /* Compare return types. */
8477 type1 = TREE_VALUE (TREE_TYPE (proto1));
8478 type2 = TREE_VALUE (TREE_TYPE (proto2));
8480 if (!objc_types_are_equivalent (type1, type2)
8481 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8482 return 0;
8484 /* Compare argument types. */
8486 /* The first argument (objc_object_type) is always the same, no need
8487 to compare. */
8489 /* The second argument (objc_selector_type) is always the same, no
8490 need to compare. */
8492 /* Compare the other arguments. */
8494 tree arg1, arg2;
8496 /* Compare METHOD_SEL_ARGS. */
8497 for (arg1 = METHOD_SEL_ARGS (proto1), arg2 = METHOD_SEL_ARGS (proto2);
8498 arg1 && arg2;
8499 arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2))
8501 type1 = TREE_VALUE (TREE_TYPE (arg1));
8502 type2 = TREE_VALUE (TREE_TYPE (arg2));
8504 /* FIXME: Do we need to decay argument types to compare them ? */
8505 type1 = objc_decay_parm_type (type1);
8506 type2 = objc_decay_parm_type (type2);
8508 if (!objc_types_are_equivalent (type1, type2)
8509 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8510 return 0;
8513 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8514 both NULL. */
8515 if (arg1 != arg2)
8516 return 0;
8518 /* Compare METHOD_ADD_ARGS. */
8519 if ((METHOD_ADD_ARGS (proto1) && !METHOD_ADD_ARGS (proto2))
8520 || (METHOD_ADD_ARGS (proto2) && !METHOD_ADD_ARGS (proto1)))
8521 return 0;
8523 if (METHOD_ADD_ARGS (proto1))
8525 for (arg1 = TREE_CHAIN (METHOD_ADD_ARGS (proto1)), arg2 = TREE_CHAIN (METHOD_ADD_ARGS (proto2));
8526 arg1 && arg2;
8527 arg1 = TREE_CHAIN (arg1), arg2 = TREE_CHAIN (arg2))
8529 type1 = TREE_TYPE (TREE_VALUE (arg1));
8530 type2 = TREE_TYPE (TREE_VALUE (arg2));
8532 /* FIXME: Do we need to decay argument types to compare them ? */
8533 type1 = objc_decay_parm_type (type1);
8534 type2 = objc_decay_parm_type (type2);
8536 if (!objc_types_are_equivalent (type1, type2)
8537 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8538 return 0;
8542 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8543 both NULL. */
8544 if (arg1 != arg2)
8545 return 0;
8547 /* Compare METHOD_ADD_ARGS_ELLIPSIS_P. */
8548 if (METHOD_ADD_ARGS_ELLIPSIS_P (proto1) != METHOD_ADD_ARGS_ELLIPSIS_P (proto2))
8549 return 0;
8552 /* Success. */
8553 return 1;
8556 /* This routine returns true if TYPE is a valid objc object type,
8557 suitable for messaging; false otherwise. If 'accept_class' is
8558 'true', then a Class object is considered valid for messaging and
8559 'true' is returned if 'type' refers to a Class. If 'accept_class'
8560 is 'false', then a Class object is not considered valid for
8561 messaging and 'false' is returned in that case. */
8563 static bool
8564 objc_type_valid_for_messaging (tree type, bool accept_classes)
8566 if (!POINTER_TYPE_P (type))
8567 return false;
8569 /* Remove the pointer indirection; don't remove more than one
8570 otherwise we'd consider "NSObject **" a valid type for messaging,
8571 which it isn't. */
8572 type = TREE_TYPE (type);
8574 if (TREE_CODE (type) != RECORD_TYPE)
8575 return false;
8577 if (objc_is_object_id (type))
8578 return true;
8580 if (objc_is_class_id (type))
8581 return accept_classes;
8583 if (TYPE_HAS_OBJC_INFO (type))
8584 return true;
8586 return false;
8589 void
8590 objc_start_function (tree name, tree type, tree attrs,
8591 #ifdef OBJCPLUS
8592 tree params
8593 #else
8594 struct c_arg_info *params
8595 #endif
8598 tree fndecl = build_decl (input_location,
8599 FUNCTION_DECL, name, type);
8601 #ifdef OBJCPLUS
8602 DECL_ARGUMENTS (fndecl) = params;
8603 DECL_INITIAL (fndecl) = error_mark_node;
8604 DECL_EXTERNAL (fndecl) = 0;
8605 TREE_STATIC (fndecl) = 1;
8606 retrofit_lang_decl (fndecl);
8607 cplus_decl_attributes (&fndecl, attrs, 0);
8608 start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT);
8609 #else
8610 current_function_returns_value = 0; /* Assume, until we see it does. */
8611 current_function_returns_null = 0;
8612 decl_attributes (&fndecl, attrs, 0);
8613 announce_function (fndecl);
8614 DECL_INITIAL (fndecl) = error_mark_node;
8615 DECL_EXTERNAL (fndecl) = 0;
8616 TREE_STATIC (fndecl) = 1;
8617 current_function_decl = pushdecl (fndecl);
8618 push_scope ();
8619 declare_parm_level ();
8620 DECL_RESULT (current_function_decl)
8621 = build_decl (input_location,
8622 RESULT_DECL, NULL_TREE,
8623 TREE_TYPE (TREE_TYPE (current_function_decl)));
8624 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1;
8625 DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1;
8626 start_fname_decls ();
8627 store_parm_decls_from (params);
8628 #endif
8630 TREE_USED (current_function_decl) = 1;
8633 /* - Generate an identifier for the function. the format is "_n_cls",
8634 where 1 <= n <= nMethods, and cls is the name the implementation we
8635 are processing.
8636 - Install the return type from the method declaration.
8637 - If we have a prototype, check for type consistency. */
8639 static void
8640 really_start_method (tree method,
8641 #ifdef OBJCPLUS
8642 tree parmlist
8643 #else
8644 struct c_arg_info *parmlist
8645 #endif
8648 tree ret_type, meth_type;
8649 tree method_id;
8650 const char *sel_name, *class_name, *cat_name;
8651 char *buf;
8653 /* Synth the storage class & assemble the return type. */
8654 ret_type = TREE_VALUE (TREE_TYPE (method));
8656 sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
8657 class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
8658 cat_name = ((TREE_CODE (objc_implementation_context)
8659 == CLASS_IMPLEMENTATION_TYPE)
8660 ? NULL
8661 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context)));
8662 method_slot++;
8664 /* Make sure this is big enough for any plausible method label. */
8665 buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
8666 + (cat_name ? strlen (cat_name) : 0));
8668 OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
8669 class_name, cat_name, sel_name, method_slot);
8671 method_id = get_identifier (buf);
8673 #ifdef OBJCPLUS
8674 /* Objective-C methods cannot be overloaded, so we don't need
8675 the type encoding appended. It looks bad anyway... */
8676 push_lang_context (lang_name_c);
8677 #endif
8679 meth_type = build_function_type_for_method (ret_type, method, METHOD_DEF, 0);
8680 objc_start_function (method_id, meth_type, NULL_TREE, parmlist);
8682 /* Set self_decl from the first argument. */
8683 self_decl = DECL_ARGUMENTS (current_function_decl);
8685 /* Suppress unused warnings. */
8686 TREE_USED (self_decl) = 1;
8687 DECL_READ_P (self_decl) = 1;
8688 TREE_USED (DECL_CHAIN (self_decl)) = 1;
8689 DECL_READ_P (DECL_CHAIN (self_decl)) = 1;
8690 #ifdef OBJCPLUS
8691 pop_lang_context ();
8692 #endif
8694 METHOD_DEFINITION (method) = current_function_decl;
8696 /* Check consistency...start_function, pushdecl, duplicate_decls. */
8698 if (implementation_template != objc_implementation_context)
8700 tree proto
8701 = lookup_method_static (implementation_template,
8702 METHOD_SEL_NAME (method),
8703 ((TREE_CODE (method) == CLASS_METHOD_DECL)
8704 | OBJC_LOOKUP_NO_SUPER));
8706 if (proto)
8708 if (!comp_proto_with_proto (method, proto, 1))
8710 bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL;
8712 warning_at (DECL_SOURCE_LOCATION (method), 0,
8713 "conflicting types for %<%c%s%>",
8714 (type ? '-' : '+'),
8715 identifier_to_locale (gen_method_decl (method)));
8716 inform (DECL_SOURCE_LOCATION (proto),
8717 "previous declaration of %<%c%s%>",
8718 (type ? '-' : '+'),
8719 identifier_to_locale (gen_method_decl (proto)));
8721 else
8723 /* If the method in the @interface was deprecated, mark
8724 the implemented method as deprecated too. It should
8725 never be used for messaging (when the deprecation
8726 warnings are produced), but just in case. */
8727 if (TREE_DEPRECATED (proto))
8728 TREE_DEPRECATED (method) = 1;
8730 /* If the method in the @interface was marked as
8731 'noreturn', mark the function implementing the method
8732 as 'noreturn' too. */
8733 TREE_THIS_VOLATILE (current_function_decl) = TREE_THIS_VOLATILE (proto);
8736 else
8738 /* We have a method @implementation even though we did not
8739 see a corresponding @interface declaration (which is allowed
8740 by Objective-C rules). Go ahead and place the method in
8741 the @interface anyway, so that message dispatch lookups
8742 will see it. */
8743 tree interface = implementation_template;
8745 if (TREE_CODE (objc_implementation_context)
8746 == CATEGORY_IMPLEMENTATION_TYPE)
8747 interface = lookup_category
8748 (interface,
8749 CLASS_SUPER_NAME (objc_implementation_context));
8751 if (interface)
8752 objc_add_method (interface, copy_node (method),
8753 TREE_CODE (method) == CLASS_METHOD_DECL,
8754 /* is_optional= */ false);
8759 static void *UOBJC_SUPER_scope = 0;
8761 /* _n_Method (id self, SEL sel, ...)
8763 struct objc_super _S;
8764 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
8765 } */
8767 static tree
8768 get_super_receiver (void)
8770 if (objc_method_context)
8772 tree super_expr, super_expr_list, class_expr;
8773 bool inst_meth;
8774 if (!UOBJC_SUPER_decl)
8776 UOBJC_SUPER_decl = build_decl (input_location,
8777 VAR_DECL, get_identifier (TAG_SUPER),
8778 objc_super_template);
8779 /* This prevents `unused variable' warnings when compiling with -Wall. */
8780 TREE_USED (UOBJC_SUPER_decl) = 1;
8781 DECL_READ_P (UOBJC_SUPER_decl) = 1;
8782 lang_hooks.decls.pushdecl (UOBJC_SUPER_decl);
8783 finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE,
8784 NULL_TREE);
8785 UOBJC_SUPER_scope = objc_get_current_scope ();
8788 /* Set receiver to self. */
8789 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id);
8790 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8791 NOP_EXPR, input_location, self_decl,
8792 NULL_TREE);
8793 super_expr_list = super_expr;
8795 /* Set class to begin searching. */
8796 /* Get the ident for the superclass class field & build a ref to it.
8797 ??? maybe we should just name the field the same for all runtimes. */
8798 super_expr = (*runtime.super_superclassfield_ident) ();
8799 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, super_expr);
8801 gcc_assert (imp_list->imp_context == objc_implementation_context
8802 && imp_list->imp_template == implementation_template);
8803 inst_meth = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL);
8805 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
8806 class_expr = (*runtime.get_class_super_ref) (input_location,
8807 imp_list, inst_meth);
8808 else
8809 /* We have a category. */
8811 tree super_name = CLASS_SUPER_NAME (imp_list->imp_template);
8812 tree super_class;
8814 /* Barf if super used in a category of a root object. */
8815 if (!super_name)
8817 error ("no super class declared in interface for %qE",
8818 CLASS_NAME (imp_list->imp_template));
8819 return error_mark_node;
8822 super_class = (*runtime.get_category_super_ref) (input_location,
8823 imp_list, inst_meth);
8824 class_expr = build_c_cast (input_location,
8825 TREE_TYPE (super_expr), super_class);
8828 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8829 NOP_EXPR,
8830 input_location, class_expr, NULL_TREE);
8832 super_expr_list = build_compound_expr (input_location,
8833 super_expr_list, super_expr);
8835 super_expr = build_unary_op (input_location,
8836 ADDR_EXPR, UOBJC_SUPER_decl, 0);
8837 super_expr_list = build_compound_expr (input_location,
8838 super_expr_list, super_expr);
8840 return super_expr_list;
8842 else
8844 error ("[super ...] must appear in a method context");
8845 return error_mark_node;
8849 /* When exiting a scope, sever links to a 'super' declaration (if any)
8850 therein contained. */
8852 void
8853 objc_clear_super_receiver (void)
8855 if (objc_method_context
8856 && UOBJC_SUPER_scope == objc_get_current_scope ())
8858 UOBJC_SUPER_decl = 0;
8859 UOBJC_SUPER_scope = 0;
8863 void
8864 objc_finish_method_definition (tree fndecl)
8866 /* We cannot validly inline ObjC methods, at least not without a language
8867 extension to declare that a method need not be dynamically
8868 dispatched, so suppress all thoughts of doing so. */
8869 DECL_UNINLINABLE (fndecl) = 1;
8871 #ifndef OBJCPLUS
8872 /* The C++ front-end will have called finish_function() for us. */
8873 finish_function ();
8874 #endif
8876 METHOD_ENCODING (objc_method_context)
8877 = encode_method_prototype (objc_method_context);
8879 /* Required to implement _msgSuper. This must be done AFTER finish_function,
8880 since the optimizer may find "may be used before set" errors. */
8881 objc_method_context = NULL_TREE;
8883 if (should_call_super_dealloc)
8884 warning (0, "method possibly missing a [super dealloc] call");
8887 /* Given a tree DECL node, produce a printable description of it in the given
8888 buffer, overwriting the buffer. */
8890 static char *
8891 gen_declaration (tree decl)
8893 errbuf[0] = '\0';
8895 if (DECL_P (decl))
8897 gen_type_name_0 (TREE_TYPE (decl));
8899 if (DECL_NAME (decl))
8901 if (!POINTER_TYPE_P (TREE_TYPE (decl)))
8902 strcat (errbuf, " ");
8904 strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
8907 if (DECL_INITIAL (decl)
8908 && TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST)
8909 sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
8910 TREE_INT_CST_LOW (DECL_INITIAL (decl)));
8913 return errbuf;
8916 /* Given a tree TYPE node, produce a printable description of it in the given
8917 buffer, overwriting the buffer. */
8919 static char *
8920 gen_type_name_0 (tree type)
8922 tree orig = type, proto;
8924 if (TYPE_P (type) && TYPE_NAME (type))
8925 type = TYPE_NAME (type);
8926 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
8928 tree inner = TREE_TYPE (type);
8930 while (TREE_CODE (inner) == ARRAY_TYPE)
8931 inner = TREE_TYPE (inner);
8933 gen_type_name_0 (inner);
8935 if (!POINTER_TYPE_P (inner))
8936 strcat (errbuf, " ");
8938 if (POINTER_TYPE_P (type))
8939 strcat (errbuf, "*");
8940 else
8941 while (type != inner)
8943 strcat (errbuf, "[");
8945 if (TYPE_DOMAIN (type))
8947 char sz[20];
8949 sprintf (sz, HOST_WIDE_INT_PRINT_DEC,
8950 (TREE_INT_CST_LOW
8951 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1));
8952 strcat (errbuf, sz);
8955 strcat (errbuf, "]");
8956 type = TREE_TYPE (type);
8959 goto exit_function;
8962 if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type))
8963 type = DECL_NAME (type);
8965 strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE
8966 ? IDENTIFIER_POINTER (type)
8967 : "");
8969 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
8970 if (objc_is_id (orig))
8971 orig = TREE_TYPE (orig);
8973 proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE;
8975 if (proto)
8977 strcat (errbuf, " <");
8979 while (proto) {
8980 strcat (errbuf,
8981 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto))));
8982 proto = TREE_CHAIN (proto);
8983 strcat (errbuf, proto ? ", " : ">");
8987 exit_function:
8988 return errbuf;
8991 static char *
8992 gen_type_name (tree type)
8994 errbuf[0] = '\0';
8996 return gen_type_name_0 (type);
8999 /* Given a method tree, put a printable description into the given
9000 buffer (overwriting) and return a pointer to the buffer. */
9002 static char *
9003 gen_method_decl (tree method)
9005 tree chain;
9007 strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */
9008 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method)));
9009 strcat (errbuf, ")");
9010 chain = METHOD_SEL_ARGS (method);
9012 if (chain)
9014 /* We have a chain of keyword_decls. */
9017 if (KEYWORD_KEY_NAME (chain))
9018 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
9020 strcat (errbuf, ":(");
9021 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain)));
9022 strcat (errbuf, ")");
9024 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
9025 if ((chain = DECL_CHAIN (chain)))
9026 strcat (errbuf, " ");
9028 while (chain);
9030 if (METHOD_ADD_ARGS (method))
9032 chain = TREE_CHAIN (METHOD_ADD_ARGS (method));
9034 /* Know we have a chain of parm_decls. */
9035 while (chain)
9037 strcat (errbuf, ", ");
9038 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain)));
9039 chain = TREE_CHAIN (chain);
9042 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
9043 strcat (errbuf, ", ...");
9047 else
9048 /* We have a unary selector. */
9049 strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
9051 return errbuf;
9054 /* Debug info. */
9057 /* Dump an @interface declaration of the supplied class CHAIN to the
9058 supplied file FP. Used to implement the -gen-decls option (which
9059 prints out an @interface declaration of all classes compiled in
9060 this run); potentially useful for debugging the compiler too. */
9061 void
9062 dump_interface (FILE *fp, tree chain)
9064 /* FIXME: A heap overflow here whenever a method (or ivar)
9065 declaration is so long that it doesn't fit in the buffer. The
9066 code and all the related functions should be rewritten to avoid
9067 using fixed size buffers. */
9068 const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
9069 tree ivar_decls = CLASS_RAW_IVARS (chain);
9070 tree nst_methods = CLASS_NST_METHODS (chain);
9071 tree cls_methods = CLASS_CLS_METHODS (chain);
9073 fprintf (fp, "\n@interface %s", my_name);
9075 /* CLASS_SUPER_NAME is used to store the superclass name for
9076 classes, and the category name for categories. */
9077 if (CLASS_SUPER_NAME (chain))
9079 const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
9081 switch (TREE_CODE (chain))
9083 case CATEGORY_IMPLEMENTATION_TYPE:
9084 case CATEGORY_INTERFACE_TYPE:
9085 fprintf (fp, " (%s)\n", name);
9086 break;
9087 default:
9088 fprintf (fp, " : %s\n", name);
9089 break;
9092 else
9093 fprintf (fp, "\n");
9095 /* FIXME - the following doesn't seem to work at the moment. */
9096 if (ivar_decls)
9098 fprintf (fp, "{\n");
9101 fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls));
9102 ivar_decls = TREE_CHAIN (ivar_decls);
9104 while (ivar_decls);
9105 fprintf (fp, "}\n");
9108 while (nst_methods)
9110 fprintf (fp, "- %s;\n", gen_method_decl (nst_methods));
9111 nst_methods = TREE_CHAIN (nst_methods);
9114 while (cls_methods)
9116 fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods));
9117 cls_methods = TREE_CHAIN (cls_methods);
9120 fprintf (fp, "@end\n");
9123 #if 0
9124 /* Produce the pretty printing for an Objective-C method. This is
9125 currently unused, but could be handy while reorganizing the pretty
9126 printing to be more robust. */
9127 static const char *
9128 objc_pretty_print_method (bool is_class_method,
9129 const char *class_name,
9130 const char *category_name,
9131 const char *selector)
9133 if (category_name)
9135 char *result = XNEWVEC (char, strlen (class_name) + strlen (category_name)
9136 + strlen (selector) + 7);
9138 if (is_class_method)
9139 sprintf (result, "+[%s(%s) %s]", class_name, category_name, selector);
9140 else
9141 sprintf (result, "-[%s(%s) %s]", class_name, category_name, selector);
9143 return result;
9145 else
9147 char *result = XNEWVEC (char, strlen (class_name)
9148 + strlen (selector) + 5);
9150 if (is_class_method)
9151 sprintf (result, "+[%s %s]", class_name, selector);
9152 else
9153 sprintf (result, "-[%s %s]", class_name, selector);
9155 return result;
9158 #endif
9160 /* Demangle function for Objective-C. Attempt to demangle the
9161 function name associated with a method (eg, going from
9162 "_i_NSObject__class" to "-[NSObject class]"); usually for the
9163 purpose of pretty printing or error messages. Return the demangled
9164 name, or NULL if the string is not an Objective-C mangled method
9165 name.
9167 Because of how the mangling is done, any method that has a '_' in
9168 its original name is at risk of being demangled incorrectly. In
9169 some cases there are multiple valid ways to demangle a method name
9170 and there is no way we can decide.
9172 TODO: objc_demangle() can't always get it right; the right way to
9173 get this correct for all method names would be to store the
9174 Objective-C method name somewhere in the function decl. Then,
9175 there is no demangling to do; we'd just pull the method name out of
9176 the decl. As an additional bonus, when printing error messages we
9177 could check for such a method name, and if we find it, we know the
9178 function is actually an Objective-C method and we could print error
9179 messages saying "In method '+[NSObject class]" instead of "In
9180 function '+[NSObject class]" as we do now. */
9181 static const char *
9182 objc_demangle (const char *mangled)
9184 char *demangled, *cp;
9186 /* First of all, if the name is too short it can't be an Objective-C
9187 mangled method name. */
9188 if (mangled[0] == '\0' || mangled[1] == '\0' || mangled[2] == '\0')
9189 return NULL;
9191 /* If the name looks like an already demangled one, return it
9192 unchanged. This should only happen on Darwin, where method names
9193 are mangled differently into a pretty-print form (such as
9194 '+[NSObject class]', see darwin.h). In that case, demangling is
9195 a no-op, but we need to return the demangled name if it was an
9196 ObjC one, and return NULL if not. We should be safe as no C/C++
9197 function can start with "-[" or "+[". */
9198 if ((mangled[0] == '-' || mangled[0] == '+')
9199 && (mangled[1] == '['))
9200 return mangled;
9202 if (mangled[0] == '_' &&
9203 (mangled[1] == 'i' || mangled[1] == 'c') &&
9204 mangled[2] == '_')
9206 cp = demangled = XNEWVEC (char, strlen(mangled) + 2);
9207 if (mangled[1] == 'i')
9208 *cp++ = '-'; /* for instance method */
9209 else
9210 *cp++ = '+'; /* for class method */
9211 *cp++ = '['; /* opening left brace */
9212 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
9213 while (*cp && *cp == '_')
9214 cp++; /* skip any initial underbars in class name */
9215 cp = strchr(cp, '_'); /* find first non-initial underbar */
9216 if (cp == NULL)
9218 free(demangled); /* not mangled name */
9219 return NULL;
9221 if (cp[1] == '_') /* easy case: no category name */
9223 *cp++ = ' '; /* replace two '_' with one ' ' */
9224 strcpy(cp, mangled + (cp - demangled) + 2);
9226 else
9228 *cp++ = '('; /* less easy case: category name */
9229 cp = strchr(cp, '_');
9230 if (cp == 0)
9232 free(demangled); /* not mangled name */
9233 return NULL;
9235 *cp++ = ')';
9236 *cp++ = ' '; /* overwriting 1st char of method name... */
9237 strcpy(cp, mangled + (cp - demangled)); /* get it back */
9239 /* Now we have the method name. We need to generally replace
9240 '_' with ':' but trying to preserve '_' if it could only have
9241 been in the mangled string because it was already in the
9242 original name. In cases where it's ambiguous, we assume that
9243 any '_' originated from a ':'. */
9245 /* Initial '_'s in method name can't have been generating by
9246 converting ':'s. Skip them. */
9247 while (*cp && *cp == '_')
9248 cp++;
9250 /* If the method name does not end with '_', then it has no
9251 arguments and there was no replacement of ':'s with '_'s
9252 during mangling. Check for that case, and skip any
9253 replacement if so. This at least guarantees that methods
9254 with no arguments are always demangled correctly (unless the
9255 original name ends with '_'). */
9256 if (*(mangled + strlen (mangled) - 1) != '_')
9258 /* Skip to the end. */
9259 for (; *cp; cp++)
9262 else
9264 /* Replace remaining '_' with ':'. This may get it wrong if
9265 there were '_'s in the original name. In most cases it
9266 is impossible to disambiguate. */
9267 for (; *cp; cp++)
9268 if (*cp == '_')
9269 *cp = ':';
9271 *cp++ = ']'; /* closing right brace */
9272 *cp++ = 0; /* string terminator */
9273 return demangled;
9275 else
9276 return NULL; /* not an objc mangled name */
9279 /* Try to pretty-print a decl. If the 'decl' is an Objective-C
9280 specific decl, return the printable name for it. If not, return
9281 NULL. */
9282 const char *
9283 objc_maybe_printable_name (tree decl, int v ATTRIBUTE_UNUSED)
9285 switch (TREE_CODE (decl))
9287 case FUNCTION_DECL:
9288 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)));
9289 break;
9291 /* The following happens when we are printing a deprecation
9292 warning for a method. The warn_deprecation() will end up
9293 trying to print the decl for INSTANCE_METHOD_DECL or
9294 CLASS_METHOD_DECL. It would be nice to be able to print
9295 "-[NSObject autorelease] is deprecated", but to do that, we'd
9296 need to store the class and method name in the method decl,
9297 which we currently don't do. For now, just return the name
9298 of the method. We don't return NULL, because that may
9299 trigger further attempts to pretty-print the decl in C/C++,
9300 but they wouldn't know how to pretty-print it. */
9301 case INSTANCE_METHOD_DECL:
9302 case CLASS_METHOD_DECL:
9303 return IDENTIFIER_POINTER (DECL_NAME (decl));
9304 break;
9305 /* This happens when printing a deprecation warning for a
9306 property. We may want to consider some sort of pretty
9307 printing (eg, include the class name where it was declared
9308 ?). */
9309 case PROPERTY_DECL:
9310 return IDENTIFIER_POINTER (PROPERTY_NAME (decl));
9311 break;
9312 default:
9313 return NULL;
9314 break;
9318 /* Return a printable name for 'decl'. This first tries
9319 objc_maybe_printable_name(), and if that fails, it returns the name
9320 in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for
9321 Objective-C; in Objective-C++, setting the hook is not enough
9322 because lots of C++ Front-End code calls cxx_printable_name,
9323 dump_decl and other C++ functions directly. So instead we have
9324 modified dump_decl to call objc_maybe_printable_name directly. */
9325 const char *
9326 objc_printable_name (tree decl, int v)
9328 const char *demangled_name = objc_maybe_printable_name (decl, v);
9330 if (demangled_name != NULL)
9331 return demangled_name;
9332 else
9333 return IDENTIFIER_POINTER (DECL_NAME (decl));
9336 /* Routine is called to issue diagnostic when reference to a private
9337 ivar is made and no other variable with same name is found in
9338 current scope. */
9339 bool
9340 objc_diagnose_private_ivar (tree id)
9342 tree ivar;
9343 if (!objc_method_context)
9344 return false;
9345 ivar = is_ivar (objc_ivar_chain, id);
9346 if (ivar && is_private (ivar))
9348 error ("instance variable %qs is declared private",
9349 IDENTIFIER_POINTER (id));
9350 return true;
9352 return false;
9355 /* Look up ID as an instance variable. OTHER contains the result of
9356 the C or C++ lookup, which we may want to use instead. */
9357 /* To use properties inside an instance method, use self.property. */
9358 tree
9359 objc_lookup_ivar (tree other, tree id)
9361 tree ivar;
9363 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
9364 if (!objc_method_context)
9365 return other;
9367 if (!strcmp (IDENTIFIER_POINTER (id), "super"))
9368 /* We have a message to super. */
9369 return get_super_receiver ();
9371 /* In a class method, look up an instance variable only as a last
9372 resort. */
9373 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
9374 && other && other != error_mark_node)
9375 return other;
9377 /* Don't look up the ivar if the user has explicitly advised against
9378 it with -fno-local-ivars. */
9380 if (!flag_local_ivars)
9381 return other;
9383 /* Look up the ivar, but do not use it if it is not accessible. */
9384 ivar = is_ivar (objc_ivar_chain, id);
9386 if (!ivar || is_private (ivar))
9387 return other;
9389 /* In an instance method, a local variable (or parameter) may hide the
9390 instance variable. */
9391 if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
9392 && other && other != error_mark_node
9393 #ifdef OBJCPLUS
9394 && CP_DECL_CONTEXT (other) != global_namespace)
9395 #else
9396 && !DECL_FILE_SCOPE_P (other))
9397 #endif
9399 if (warn_shadow_ivar == 1 || (warn_shadow && warn_shadow_ivar != 0)) {
9400 warning (warn_shadow_ivar ? OPT_Wshadow_ivar : OPT_Wshadow,
9401 "local declaration of %qE hides instance variable", id);
9404 return other;
9407 /* At this point, we are either in an instance method with no obscuring
9408 local definitions, or in a class method with no alternate definitions
9409 at all. */
9410 return build_ivar_reference (id);
9413 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
9414 needs to be done if we are calling a function through a cast. */
9416 tree
9417 objc_rewrite_function_call (tree function, tree first_param)
9419 if (TREE_CODE (function) == NOP_EXPR
9420 && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR
9421 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0))
9422 == FUNCTION_DECL)
9424 function = build3 (OBJ_TYPE_REF, TREE_TYPE (function),
9425 TREE_OPERAND (function, 0),
9426 first_param, size_zero_node);
9429 return function;
9432 /* This is called to "gimplify" a PROPERTY_REF node. It builds the
9433 corresponding 'getter' function call. Note that we assume the
9434 PROPERTY_REF to be valid since we generated it while parsing. */
9435 static void
9436 objc_gimplify_property_ref (tree *expr_p)
9438 tree getter = PROPERTY_REF_GETTER_CALL (*expr_p);
9439 tree call_exp;
9441 if (getter == NULL_TREE)
9443 tree property_decl = PROPERTY_REF_PROPERTY_DECL (*expr_p);
9444 /* This can happen if DECL_ARTIFICIAL (*expr_p), but
9445 should be impossible for real properties, which always
9446 have a getter. */
9447 error_at (EXPR_LOCATION (*expr_p), "no %qs getter found",
9448 IDENTIFIER_POINTER (PROPERTY_NAME (property_decl)));
9449 /* Try to recover from the error to prevent an ICE. We take
9450 zero and cast it to the type of the property. */
9451 *expr_p = convert (TREE_TYPE (property_decl),
9452 integer_zero_node);
9453 return;
9456 if (PROPERTY_REF_DEPRECATED_GETTER (*expr_p))
9458 /* PROPERTY_REF_DEPRECATED_GETTER contains the method prototype
9459 that is deprecated. */
9460 warn_deprecated_use (PROPERTY_REF_DEPRECATED_GETTER (*expr_p),
9461 NULL_TREE);
9464 call_exp = getter;
9465 #ifdef OBJCPLUS
9466 /* In C++, a getter which returns an aggregate value results in a
9467 target_expr which initializes a temporary to the call
9468 expression. */
9469 if (TREE_CODE (getter) == TARGET_EXPR)
9471 gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter)));
9472 gcc_assert (TREE_CODE (TREE_OPERAND (getter, 0)) == VAR_DECL);
9473 call_exp = TREE_OPERAND (getter, 1);
9475 #endif
9476 gcc_assert (TREE_CODE (call_exp) == CALL_EXPR);
9478 *expr_p = call_exp;
9481 /* This is called when "gimplifying" the trees. We need to gimplify
9482 the Objective-C/Objective-C++ specific trees, then hand over the
9483 process to C/C++. */
9485 objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9487 enum tree_code code = TREE_CODE (*expr_p);
9488 switch (code)
9490 /* Look for the special case of OBJC_TYPE_REF with the address
9491 of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend
9492 or one of its cousins). */
9493 case OBJ_TYPE_REF:
9494 if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR
9495 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0))
9496 == FUNCTION_DECL)
9498 enum gimplify_status r0, r1;
9500 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
9501 value of the OBJ_TYPE_REF, so force them to be emitted
9502 during subexpression evaluation rather than after the
9503 OBJ_TYPE_REF. This permits objc_msgSend calls in
9504 Objective C to use direct rather than indirect calls when
9505 the object expression has a postincrement. */
9506 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL,
9507 is_gimple_val, fb_rvalue);
9508 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
9509 is_gimple_val, fb_rvalue);
9511 return MIN (r0, r1);
9513 break;
9514 case PROPERTY_REF:
9515 objc_gimplify_property_ref (expr_p);
9516 /* Do not return yet; let C/C++ gimplify the resulting expression. */
9517 break;
9518 default:
9519 break;
9522 #ifdef OBJCPLUS
9523 return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p);
9524 #else
9525 return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
9526 #endif
9529 /* --- FAST ENUMERATION --- */
9530 /* Begin code generation for fast enumeration (foreach) ... */
9532 /* Defines
9534 struct __objcFastEnumerationState
9536 unsigned long state;
9537 id *itemsPtr;
9538 unsigned long *mutationsPtr;
9539 unsigned long extra[5];
9542 Confusingly enough, NSFastEnumeration is then defined by libraries
9543 to be the same structure.
9546 static void
9547 build_fast_enumeration_state_template (void)
9549 tree decls, *chain = NULL;
9551 /* { */
9552 objc_fast_enumeration_state_template = objc_start_struct (get_identifier
9553 (TAG_FAST_ENUMERATION_STATE));
9555 /* unsigned long state; */
9556 decls = add_field_decl (long_unsigned_type_node, "state", &chain);
9558 /* id *itemsPtr; */
9559 add_field_decl (build_pointer_type (objc_object_type),
9560 "itemsPtr", &chain);
9562 /* unsigned long *mutationsPtr; */
9563 add_field_decl (build_pointer_type (long_unsigned_type_node),
9564 "mutationsPtr", &chain);
9566 /* unsigned long extra[5]; */
9567 add_field_decl (build_sized_array_type (long_unsigned_type_node, 5),
9568 "extra", &chain);
9570 /* } */
9571 objc_finish_struct (objc_fast_enumeration_state_template, decls);
9575 'objc_finish_foreach_loop()' generates the code for an Objective-C
9576 foreach loop. The 'location' argument is the location of the 'for'
9577 that starts the loop. The 'object_expression' is the expression of
9578 the 'object' that iterates; the 'collection_expression' is the
9579 expression of the collection that we iterate over (we need to make
9580 sure we evaluate this only once); the 'for_body' is the set of
9581 statements to be executed in each iteration; 'break_label' and
9582 'continue_label' are the break and continue labels which we need to
9583 emit since the <statements> may be jumping to 'break_label' (if they
9584 contain 'break') or to 'continue_label' (if they contain
9585 'continue').
9587 The syntax is
9589 for (<object expression> in <collection expression>)
9590 <statements>
9592 which is compiled into the following blurb:
9595 id __objc_foreach_collection;
9596 __objc_fast_enumeration_state __objc_foreach_enum_state;
9597 unsigned long __objc_foreach_batchsize;
9598 id __objc_foreach_items[16];
9599 __objc_foreach_collection = <collection expression>;
9600 __objc_foreach_enum_state = { 0 };
9601 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9603 if (__objc_foreach_batchsize == 0)
9604 <object expression> = nil;
9605 else
9607 unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr;
9608 next_batch:
9610 unsigned long __objc_foreach_index;
9611 __objc_foreach_index = 0;
9613 next_object:
9614 if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>);
9615 <object expression> = enumState.itemsPtr[__objc_foreach_index];
9616 <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label]
9618 continue_label:
9619 __objc_foreach_index++;
9620 if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object;
9621 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9623 if (__objc_foreach_batchsize != 0) goto next_batch;
9624 <object expression> = nil;
9625 break_label:
9629 'statements' may contain a 'continue' or 'break' instruction, which
9630 the user expects to 'continue' or 'break' the entire foreach loop.
9631 We are provided the labels that 'break' and 'continue' jump to, so
9632 we place them where we want them to jump to when they pick them.
9634 Optimization TODO: we could cache the IMP of
9635 countByEnumeratingWithState:objects:count:.
9638 /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */
9639 /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */
9641 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
9642 #include "tree-pretty-print.h"
9643 #endif
9645 void
9646 objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body,
9647 tree break_label, tree continue_label)
9649 /* A tree representing the __objcFastEnumerationState struct type,
9650 or NSFastEnumerationState struct, whatever we are using. */
9651 tree objc_fast_enumeration_state_type;
9653 /* The trees representing the declarations of each of the local variables. */
9654 tree objc_foreach_collection_decl;
9655 tree objc_foreach_enum_state_decl;
9656 tree objc_foreach_items_decl;
9657 tree objc_foreach_batchsize_decl;
9658 tree objc_foreach_mutations_pointer_decl;
9659 tree objc_foreach_index_decl;
9661 /* A tree representing the selector countByEnumeratingWithState:objects:count:. */
9662 tree selector_name;
9664 /* A tree representing the local bind. */
9665 tree bind;
9667 /* A tree representing the external 'if (__objc_foreach_batchsize)' */
9668 tree first_if;
9670 /* A tree representing the 'else' part of 'first_if' */
9671 tree first_else;
9673 /* A tree representing the 'next_batch' label. */
9674 tree next_batch_label_decl;
9676 /* A tree representing the binding after the 'next_batch' label. */
9677 tree next_batch_bind;
9679 /* A tree representing the 'next_object' label. */
9680 tree next_object_label_decl;
9682 /* Temporary variables. */
9683 tree t;
9684 int i;
9686 if (flag_objc1_only)
9687 error_at (location, "fast enumeration is not available in Objective-C 1.0");
9689 if (object_expression == error_mark_node)
9690 return;
9692 if (collection_expression == error_mark_node)
9693 return;
9695 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression), true))
9697 error_at (location, "iterating variable in fast enumeration is not an object");
9698 return;
9701 if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression), true))
9703 error_at (location, "collection in fast enumeration is not an object");
9704 return;
9707 /* TODO: Check that object_expression is either a variable
9708 declaration, or an lvalue. */
9710 /* This kludge is an idea from apple. We use the
9711 __objcFastEnumerationState struct implicitly defined by the
9712 compiler, unless a NSFastEnumerationState struct has been defined
9713 (by a Foundation library such as GNUstep Base) in which case, we
9714 use that one.
9716 objc_fast_enumeration_state_type = objc_fast_enumeration_state_template;
9718 tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState"));
9720 if (objc_NSFastEnumeration_type)
9722 /* TODO: We really need to check that
9723 objc_NSFastEnumeration_type is the same as ours! */
9724 if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL)
9726 /* If it's a typedef, use the original type. */
9727 if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type))
9728 objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type);
9729 else
9730 objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type);
9735 /* { */
9736 /* Done by c-parser.c. */
9738 /* type object; */
9739 /* Done by c-parser.c. */
9741 /* Disable warnings that 'object' is unused. For example the code
9743 for (id object in collection)
9744 i++;
9746 which can be used to count how many objects there are in the
9747 collection is fine and should generate no warnings even if
9748 'object' is technically unused. */
9749 TREE_USED (object_expression) = 1;
9750 if (DECL_P (object_expression))
9751 DECL_READ_P (object_expression) = 1;
9753 /* id __objc_foreach_collection */
9754 objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection");
9756 /* __objcFastEnumerationState __objc_foreach_enum_state; */
9757 objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state");
9758 TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl;
9760 /* id __objc_foreach_items[16]; */
9761 objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items");
9762 TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl;
9764 /* unsigned long __objc_foreach_batchsize; */
9765 objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize");
9766 TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl;
9768 /* Generate the local variable binding. */
9769 bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL);
9770 SET_EXPR_LOCATION (bind, location);
9771 TREE_SIDE_EFFECTS (bind) = 1;
9773 /* __objc_foreach_collection = <collection expression>; */
9774 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression);
9775 SET_EXPR_LOCATION (t, location);
9776 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9777 /* We have used 'collection_expression'. */
9778 mark_exp_read (collection_expression);
9780 /* __objc_foreach_enum_state.state = 0; */
9781 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9782 get_identifier ("state")),
9783 build_int_cst (long_unsigned_type_node, 0));
9784 SET_EXPR_LOCATION (t, location);
9785 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9787 /* __objc_foreach_enum_state.itemsPtr = NULL; */
9788 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9789 get_identifier ("itemsPtr")),
9790 null_pointer_node);
9791 SET_EXPR_LOCATION (t, location);
9792 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9794 /* __objc_foreach_enum_state.mutationsPtr = NULL; */
9795 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9796 get_identifier ("mutationsPtr")),
9797 null_pointer_node);
9798 SET_EXPR_LOCATION (t, location);
9799 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9801 /* __objc_foreach_enum_state.extra[0] = 0; */
9802 /* __objc_foreach_enum_state.extra[1] = 0; */
9803 /* __objc_foreach_enum_state.extra[2] = 0; */
9804 /* __objc_foreach_enum_state.extra[3] = 0; */
9805 /* __objc_foreach_enum_state.extra[4] = 0; */
9806 for (i = 0; i < 5 ; i++)
9808 t = build2 (MODIFY_EXPR, void_type_node,
9809 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9810 get_identifier ("extra")),
9811 build_int_cst (NULL_TREE, i)),
9812 build_int_cst (long_unsigned_type_node, 0));
9813 SET_EXPR_LOCATION (t, location);
9814 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9817 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
9818 selector_name = get_identifier ("countByEnumeratingWithState:objects:count:");
9819 #ifdef OBJCPLUS
9820 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9821 /* Parameters. */
9822 tree_cons /* &__objc_foreach_enum_state */
9823 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9824 tree_cons /* __objc_foreach_items */
9825 (NULL_TREE, objc_foreach_items_decl,
9826 tree_cons /* 16 */
9827 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9828 #else
9829 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
9831 struct c_expr array;
9832 array.value = objc_foreach_items_decl;
9833 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9834 /* Parameters. */
9835 tree_cons /* &__objc_foreach_enum_state */
9836 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9837 tree_cons /* __objc_foreach_items */
9838 (NULL_TREE, default_function_array_conversion (location, array).value,
9839 tree_cons /* 16 */
9840 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9842 #endif
9843 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
9844 convert (long_unsigned_type_node, t));
9845 SET_EXPR_LOCATION (t, location);
9846 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9848 /* if (__objc_foreach_batchsize == 0) */
9849 first_if = build3 (COND_EXPR, void_type_node,
9850 /* Condition. */
9851 c_fully_fold
9852 (c_common_truthvalue_conversion
9853 (location,
9854 build_binary_op (location,
9855 EQ_EXPR,
9856 objc_foreach_batchsize_decl,
9857 build_int_cst (long_unsigned_type_node, 0), 1)),
9858 false, NULL),
9859 /* Then block (we fill it in later). */
9860 NULL_TREE,
9861 /* Else block (we fill it in later). */
9862 NULL_TREE);
9863 SET_EXPR_LOCATION (first_if, location);
9864 append_to_statement_list (first_if, &BIND_EXPR_BODY (bind));
9866 /* then <object expression> = nil; */
9867 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
9868 SET_EXPR_LOCATION (t, location);
9869 COND_EXPR_THEN (first_if) = t;
9871 /* Now we build the 'else' part of the if; once we finish building
9872 it, we attach it to first_if as the 'else' part. */
9874 /* else */
9875 /* { */
9877 /* unsigned long __objc_foreach_mutations_pointer; */
9878 objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer");
9880 /* Generate the local variable binding. */
9881 first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL);
9882 SET_EXPR_LOCATION (first_else, location);
9883 TREE_SIDE_EFFECTS (first_else) = 1;
9885 /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */
9886 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl,
9887 build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9888 get_identifier ("mutationsPtr")),
9889 RO_UNARY_STAR));
9890 SET_EXPR_LOCATION (t, location);
9891 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
9893 /* next_batch: */
9894 next_batch_label_decl = create_artificial_label (location);
9895 t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl);
9896 SET_EXPR_LOCATION (t, location);
9897 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
9899 /* { */
9901 /* unsigned long __objc_foreach_index; */
9902 objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index");
9904 /* Generate the local variable binding. */
9905 next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL);
9906 SET_EXPR_LOCATION (next_batch_bind, location);
9907 TREE_SIDE_EFFECTS (next_batch_bind) = 1;
9908 append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else));
9910 /* __objc_foreach_index = 0; */
9911 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
9912 build_int_cst (long_unsigned_type_node, 0));
9913 SET_EXPR_LOCATION (t, location);
9914 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9916 /* next_object: */
9917 next_object_label_decl = create_artificial_label (location);
9918 t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl);
9919 SET_EXPR_LOCATION (t, location);
9920 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9922 /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */
9923 t = build3 (COND_EXPR, void_type_node,
9924 /* Condition. */
9925 c_fully_fold
9926 (c_common_truthvalue_conversion
9927 (location,
9928 build_binary_op
9929 (location,
9930 NE_EXPR,
9931 objc_foreach_mutations_pointer_decl,
9932 build_indirect_ref (location,
9933 objc_build_component_ref (objc_foreach_enum_state_decl,
9934 get_identifier ("mutationsPtr")),
9935 RO_UNARY_STAR), 1)),
9936 false, NULL),
9937 /* Then block. */
9938 build_function_call (input_location,
9939 objc_enumeration_mutation_decl,
9940 tree_cons (NULL, collection_expression, NULL)),
9941 /* Else block. */
9942 NULL_TREE);
9943 SET_EXPR_LOCATION (t, location);
9944 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9946 /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */
9947 t = build2 (MODIFY_EXPR, void_type_node, object_expression,
9948 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9949 get_identifier ("itemsPtr")),
9950 objc_foreach_index_decl));
9951 SET_EXPR_LOCATION (t, location);
9952 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9954 /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */
9955 append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind));
9957 /* continue_label: */
9958 if (continue_label)
9960 t = build1 (LABEL_EXPR, void_type_node, continue_label);
9961 SET_EXPR_LOCATION (t, location);
9962 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9965 /* __objc_foreach_index++; */
9966 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
9967 build_binary_op (location,
9968 PLUS_EXPR,
9969 objc_foreach_index_decl,
9970 build_int_cst (long_unsigned_type_node, 1), 1));
9971 SET_EXPR_LOCATION (t, location);
9972 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9974 /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */
9975 t = build3 (COND_EXPR, void_type_node,
9976 /* Condition. */
9977 c_fully_fold
9978 (c_common_truthvalue_conversion
9979 (location,
9980 build_binary_op (location,
9981 LT_EXPR,
9982 objc_foreach_index_decl,
9983 objc_foreach_batchsize_decl, 1)),
9984 false, NULL),
9985 /* Then block. */
9986 build1 (GOTO_EXPR, void_type_node, next_object_label_decl),
9987 /* Else block. */
9988 NULL_TREE);
9989 SET_EXPR_LOCATION (t, location);
9990 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9992 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
9993 #ifdef OBJCPLUS
9994 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9995 /* Parameters. */
9996 tree_cons /* &__objc_foreach_enum_state */
9997 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9998 tree_cons /* __objc_foreach_items */
9999 (NULL_TREE, objc_foreach_items_decl,
10000 tree_cons /* 16 */
10001 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
10002 #else
10003 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
10005 struct c_expr array;
10006 array.value = objc_foreach_items_decl;
10007 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
10008 /* Parameters. */
10009 tree_cons /* &__objc_foreach_enum_state */
10010 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
10011 tree_cons /* __objc_foreach_items */
10012 (NULL_TREE, default_function_array_conversion (location, array).value,
10013 tree_cons /* 16 */
10014 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
10016 #endif
10017 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
10018 convert (long_unsigned_type_node, t));
10019 SET_EXPR_LOCATION (t, location);
10020 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10022 /* } */
10024 /* if (__objc_foreach_batchsize != 0) goto next_batch; */
10025 t = build3 (COND_EXPR, void_type_node,
10026 /* Condition. */
10027 c_fully_fold
10028 (c_common_truthvalue_conversion
10029 (location,
10030 build_binary_op (location,
10031 NE_EXPR,
10032 objc_foreach_batchsize_decl,
10033 build_int_cst (long_unsigned_type_node, 0), 1)),
10034 false, NULL),
10035 /* Then block. */
10036 build1 (GOTO_EXPR, void_type_node, next_batch_label_decl),
10037 /* Else block. */
10038 NULL_TREE);
10039 SET_EXPR_LOCATION (t, location);
10040 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10042 /* <object expression> = nil; */
10043 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
10044 SET_EXPR_LOCATION (t, location);
10045 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10047 /* break_label: */
10048 if (break_label)
10050 t = build1 (LABEL_EXPR, void_type_node, break_label);
10051 SET_EXPR_LOCATION (t, location);
10052 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10055 /* } */
10056 COND_EXPR_ELSE (first_if) = first_else;
10058 /* Do the whole thing. */
10059 add_stmt (bind);
10061 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
10062 /* This will print to stderr the whole blurb generated by the
10063 compiler while compiling (assuming the compiler doesn't crash
10064 before getting here).
10066 debug_generic_stmt (bind);
10067 #endif
10069 /* } */
10070 /* Done by c-parser.c */
10073 /* --- SUPPORT FOR FORMAT ARG CHECKING --- */
10074 /* Return true if we have an NxString object pointer. */
10076 bool
10077 objc_string_ref_type_p (tree strp)
10079 tree tmv;
10080 if (!strp || TREE_CODE (strp) != POINTER_TYPE)
10081 return false;
10083 tmv = TYPE_MAIN_VARIANT (TREE_TYPE (strp));
10084 tmv = OBJC_TYPE_NAME (tmv);
10085 return (tmv
10086 && TREE_CODE (tmv) == IDENTIFIER_NODE
10087 && IDENTIFIER_POINTER (tmv)
10088 && !strncmp (IDENTIFIER_POINTER (tmv), "NSString", 8));
10091 /* At present the behavior of this is undefined and it does nothing. */
10092 void
10093 objc_check_format_arg (tree ARG_UNUSED (format_arg),
10094 tree ARG_UNUSED (args_list))
10098 void
10099 objc_common_init_ts (void)
10101 c_common_init_ts ();
10103 MARK_TS_DECL_NON_COMMON (CLASS_METHOD_DECL);
10104 MARK_TS_DECL_NON_COMMON (INSTANCE_METHOD_DECL);
10105 MARK_TS_DECL_NON_COMMON (KEYWORD_DECL);
10106 MARK_TS_DECL_NON_COMMON (PROPERTY_DECL);
10108 MARK_TS_COMMON (CLASS_INTERFACE_TYPE);
10109 MARK_TS_COMMON (PROTOCOL_INTERFACE_TYPE);
10110 MARK_TS_COMMON (CLASS_IMPLEMENTATION_TYPE);
10112 MARK_TS_TYPED (MESSAGE_SEND_EXPR);
10113 MARK_TS_TYPED (PROPERTY_REF);
10116 size_t
10117 objc_common_tree_size (enum tree_code code)
10119 switch (code)
10121 case CLASS_METHOD_DECL:
10122 case INSTANCE_METHOD_DECL:
10123 case KEYWORD_DECL:
10124 case PROPERTY_DECL:
10125 return sizeof (struct tree_decl_non_common);
10126 default:
10127 gcc_unreachable ();
10133 #include "gt-objc-objc-act.h"