typeck.c (cp_build_function_call_vec): When mark_used fails unconditionally return...
[official-gcc.git] / gcc / objc / objc-act.c
blob5cf7205c23b87b4020fa8d2e0e6927824cd36af8
1 /* Implement classes and message passing for Objective C.
2 Copyright (C) 1992-2019 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-objc.h"
38 #include "langhooks.h"
39 #include "objc-act.h"
40 #include "objc-map.h"
41 #include "function.h"
42 #include "toplev.h"
43 #include "debug.h"
44 #include "c-family/c-target.h"
45 #include "intl.h"
46 #include "cgraph.h"
47 #include "tree-iterator.h"
48 /* Different initialization, code gen and meta data generation for each
49 runtime. */
50 #include "objc-runtime-hooks.h"
51 /* Routines used mainly by the runtimes. */
52 #include "objc-runtime-shared-support.h"
53 /* For default_tree_printer (). */
55 /* For enum gimplify_status */
56 #include "gimple-expr.h"
57 #include "gimplify.h"
59 /* For encode_method_prototype(). */
60 #include "objc-encoding.h"
62 static unsigned int should_call_super_dealloc = 0;
64 /* When building Objective-C++, we are not linking against the C front-end
65 and so need to replicate the C tree-construction functions in some way. */
66 #ifdef OBJCPLUS
67 #define OBJCP_REMAP_FUNCTIONS
68 #include "objcp-decl.h"
69 #endif /* OBJCPLUS */
71 /* This is the default way of generating a method name. */
72 /* This has the problem that "test_method:argument:" and
73 "test:method_argument:" will generate the same name
74 ("_i_Test__test_method_argument_" for an instance method of the
75 class "Test"), so you can't have them both in the same class!
76 Moreover, the demangling (going from
77 "_i_Test__test_method_argument" back to the original name) is
78 undefined because there are two correct ways of demangling the
79 name. */
80 #ifndef OBJC_GEN_METHOD_LABEL
81 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
82 do { \
83 char *temp; \
84 sprintf ((BUF), "_%s_%s_%s_%s", \
85 ((IS_INST) ? "i" : "c"), \
86 (CLASS_NAME), \
87 ((CAT_NAME)? (CAT_NAME) : ""), \
88 (SEL_NAME)); \
89 for (temp = (BUF); *temp; temp++) \
90 if (*temp == ':') *temp = '_'; \
91 } while (0)
92 #endif
94 /* These need specifying. */
95 #ifndef OBJC_FORWARDING_STACK_OFFSET
96 #define OBJC_FORWARDING_STACK_OFFSET 0
97 #endif
99 #ifndef OBJC_FORWARDING_MIN_OFFSET
100 #define OBJC_FORWARDING_MIN_OFFSET 0
101 #endif
103 /*** Private Interface (procedures) ***/
105 /* Init stuff. */
106 static void synth_module_prologue (void);
108 /* Code generation. */
110 static tree start_class (enum tree_code, tree, tree, tree, tree);
111 static tree continue_class (tree);
112 static void finish_class (tree);
113 static void start_method_def (tree, tree);
115 static tree start_protocol (enum tree_code, tree, tree, tree);
116 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
117 static tree objc_add_method (tree, tree, int, bool);
118 static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree);
119 static tree build_ivar_reference (tree);
120 static tree is_ivar (tree, tree);
122 /* We only need the following for ObjC; ObjC++ will use C++'s definition
123 of DERIVED_FROM_P. */
124 #ifndef OBJCPLUS
125 static bool objc_derived_from_p (tree, tree);
126 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
127 #endif
129 /* Property. */
130 static void objc_gen_property_data (tree, tree);
131 static void objc_synthesize_getter (tree, tree, tree);
132 static void objc_synthesize_setter (tree, tree, tree);
133 static tree lookup_property (tree, tree);
134 static tree lookup_property_in_list (tree, tree);
135 static tree lookup_property_in_protocol_list (tree, tree);
136 static void build_common_objc_property_accessor_helpers (void);
138 static void objc_xref_basetypes (tree, tree);
140 static tree get_class_ivars (tree, bool);
142 static void build_fast_enumeration_state_template (void);
144 #ifdef OBJCPLUS
145 static void objc_generate_cxx_cdtors (void);
146 #endif
148 /* objc attribute */
149 static void objc_decl_method_attributes (tree*, tree, int);
150 static tree build_keyword_selector (tree);
152 static void hash_init (void);
154 /* Hash tables to manage the global pool of method prototypes. Each
155 of these maps map a method name (selector) identifier to either a
156 single tree (for methods with a single method prototype) or a
157 TREE_VEC (for methods with multiple method prototypes). */
158 static GTY(()) objc_map_t instance_method_map = 0;
159 static GTY(()) objc_map_t class_method_map = 0;
161 /* Hash tables to manage the global pool of class names. */
163 static GTY(()) objc_map_t class_name_map = 0;
164 static GTY(()) objc_map_t alias_name_map = 0;
166 static tree lookup_method (tree, tree);
167 static tree lookup_method_static (tree, tree, int);
169 static void interface_hash_init (void);
170 static tree add_interface (tree, tree);
171 static void add_category (tree, tree);
172 static inline tree lookup_category (tree, tree);
174 /* Protocols. */
176 static tree lookup_protocol (tree, bool, bool);
177 static tree lookup_and_install_protocols (tree, bool);
179 #ifdef OBJCPLUS
180 static void really_start_method (tree, tree);
181 #else
182 static void really_start_method (tree, struct c_arg_info *);
183 #endif
184 static int comp_proto_with_proto (tree, tree, int);
185 static tree objc_decay_parm_type (tree);
187 /* Utilities for debugging and error diagnostics. */
189 static char *gen_type_name (tree);
190 static char *gen_type_name_0 (tree);
191 static char *gen_method_decl (tree);
192 static char *gen_declaration (tree);
194 /* Everything else. */
196 static void generate_struct_by_value_array (void) ATTRIBUTE_NORETURN;
198 static void mark_referenced_methods (void);
199 static bool objc_type_valid_for_messaging (tree type, bool allow_classes);
200 static tree check_duplicates (tree, int, int);
202 /*** Private Interface (data) ***/
203 /* Flags for lookup_method_static(). */
205 /* Look for class methods. */
206 #define OBJC_LOOKUP_CLASS 1
207 /* Do not examine superclasses. */
208 #define OBJC_LOOKUP_NO_SUPER 2
209 /* Disable returning an instance method of a root class when a class
210 method can't be found. */
211 #define OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS 4
213 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
214 tree objc_global_trees[OCTI_MAX];
216 struct imp_entry *imp_list = 0;
217 int imp_count = 0; /* `@implementation' */
218 int cat_count = 0; /* `@category' */
220 objc_ivar_visibility_kind objc_ivar_visibility, objc_default_ivar_visibility;
222 /* Use to generate method labels. */
223 static int method_slot = 0;
225 /* Flag to say whether methods in a protocol are optional or
226 required. */
227 static bool objc_method_optional_flag = false;
229 static int objc_collecting_ivars = 0;
231 /* Flag that is set to 'true' while we are processing a class
232 extension. Since a class extension just "reopens" the main
233 @interface, this can be used to determine if we are in the main
234 @interface, or in a class extension. */
235 static bool objc_in_class_extension = false;
237 static char *errbuf; /* Buffer for error diagnostics */
239 /* An array of all the local variables in the current function that
240 need to be marked as volatile. */
241 vec<tree, va_gc> *local_variables_to_volatilize = NULL;
243 /* Store all constructed constant strings in a hash table so that
244 they get uniqued properly. */
246 struct GTY((for_user)) string_descriptor {
247 /* The literal argument . */
248 tree literal;
250 /* The resulting constant string. */
251 tree constructor;
254 struct objc_string_hasher : ggc_ptr_hash<string_descriptor>
256 static hashval_t hash (string_descriptor *);
257 static bool equal (string_descriptor *, string_descriptor *);
260 static GTY(()) hash_table<objc_string_hasher> *string_htab;
262 FILE *gen_declaration_file;
264 /* Hooks for stuff that differs between runtimes. */
265 objc_runtime_hooks runtime;
267 /* Create a temporary variable of type 'type'. If 'name' is set, uses
268 the specified name, else use no name. Returns the declaration of
269 the type. The 'name' is mostly useful for debugging.
271 tree
272 objc_create_temporary_var (tree type, const char *name)
274 tree decl;
276 if (name != NULL)
278 decl = build_decl (input_location,
279 VAR_DECL, get_identifier (name), type);
281 else
283 decl = build_decl (input_location,
284 VAR_DECL, NULL_TREE, type);
286 TREE_USED (decl) = 1;
287 DECL_ARTIFICIAL (decl) = 1;
288 DECL_IGNORED_P (decl) = 1;
289 DECL_CONTEXT (decl) = current_function_decl;
291 return decl;
294 /* Some platforms pass small structures through registers versus
295 through an invisible pointer. Determine at what size structure is
296 the transition point between the two possibilities. */
298 static void
299 generate_struct_by_value_array (void)
301 tree type;
302 tree decls;
303 int i, j;
304 int aggregate_in_mem[32];
305 int found = 0;
307 /* Presumably no platform passes 32 byte structures in a register. */
308 /* ??? As an example, m64/ppc/Darwin can pass up to 8*long+13*double
309 in registers. */
310 for (i = 1; i < 32; i++)
312 char buffer[5];
313 tree *chain = NULL;
315 /* Create an unnamed struct that has `i' character components */
316 type = objc_start_struct (NULL_TREE);
318 strcpy (buffer, "c1");
319 decls = add_field_decl (char_type_node, buffer, &chain);
321 for (j = 1; j < i; j++)
323 sprintf (buffer, "c%d", j + 1);
324 add_field_decl (char_type_node, buffer, &chain);
326 objc_finish_struct (type, decls);
328 aggregate_in_mem[i] = aggregate_value_p (type, 0);
329 if (!aggregate_in_mem[i])
330 found = 1;
333 /* We found some structures that are returned in registers instead of memory
334 so output the necessary data. */
335 if (found)
337 for (i = 31; i >= 0; i--)
338 if (!aggregate_in_mem[i])
339 break;
340 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n", i);
343 exit (0);
346 bool
347 objc_init (void)
349 bool ok;
350 #ifdef OBJCPLUS
351 if (cxx_init () == false)
352 #else
353 if (c_objc_common_init () == false)
354 #endif
355 return false;
357 /* print_struct_values is triggered by -print-runtime-info (used
358 when building libobjc, with an empty file as input). It does not
359 require any ObjC setup, and it never returns.
361 -fcompare-debug is used to check the compiler output; we are
362 executed twice, once with flag_compare_debug set, and once with
363 it not set. If the flag is used together with
364 -print-runtime-info, we want to print the runtime info only once,
365 else it would be output in duplicate. So we check
366 flag_compare_debug to output it in only one of the invocations.
368 As a side effect, this also that means -fcompare-debug
369 -print-runtime-info will run the compiler twice, and compare the
370 generated assembler file; the first time the compiler exits
371 immediately (producing no file), and the second time it compiles
372 an empty file. This checks, as a side effect, that compiling an
373 empty file produces no assembler output. */
374 if (print_struct_values && !flag_compare_debug)
375 generate_struct_by_value_array ();
377 /* Set up stuff used by FE parser and all runtimes. */
378 errbuf = XNEWVEC (char, 1024 * 10);
379 interface_hash_init ();
380 hash_init ();
381 objc_encoding_init ();
382 /* ... and then check flags and set-up for the selected runtime ... */
383 if (flag_next_runtime && flag_objc_abi >= 2)
384 ok = objc_next_runtime_abi_02_init (&runtime);
385 else if (flag_next_runtime)
386 ok = objc_next_runtime_abi_01_init (&runtime);
387 else
388 ok = objc_gnu_runtime_abi_01_init (&runtime);
390 /* If that part of the setup failed - bail out immediately. */
391 if (!ok)
392 return false;
394 /* Determine the default visibility for instance variables. */
395 switch (default_ivar_visibility)
397 case IVAR_VISIBILITY_PRIVATE:
398 objc_default_ivar_visibility = OBJC_IVAR_VIS_PRIVATE;
399 break;
400 case IVAR_VISIBILITY_PUBLIC:
401 objc_default_ivar_visibility = OBJC_IVAR_VIS_PUBLIC;
402 break;
403 case IVAR_VISIBILITY_PACKAGE:
404 objc_default_ivar_visibility = OBJC_IVAR_VIS_PACKAGE;
405 break;
406 default:
407 objc_default_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
410 /* Generate general types and push runtime-specific decls to file scope. */
411 synth_module_prologue ();
413 return true;
416 /* This is called at the end of parsing by the C/C++ parsers. */
417 void
418 objc_write_global_declarations (void)
420 mark_referenced_methods ();
422 /* A missing @end might not be detected by the parser. */
423 if (objc_implementation_context)
425 warning (0, "%<@end%> missing in implementation context");
426 finish_class (objc_implementation_context);
427 objc_ivar_chain = NULL_TREE;
428 objc_implementation_context = NULL_TREE;
431 if (warn_selector)
433 objc_map_iterator_t i;
435 objc_map_iterator_initialize (class_method_map, &i);
436 while (objc_map_iterator_move_to_next (class_method_map, &i))
437 check_duplicates (objc_map_iterator_current_value (class_method_map, i), 0, 1);
439 objc_map_iterator_initialize (instance_method_map, &i);
440 while (objc_map_iterator_move_to_next (instance_method_map, &i))
441 check_duplicates (objc_map_iterator_current_value (instance_method_map, i), 0, 0);
444 /* TODO: consider an early exit here if either errorcount or sorrycount
445 is non-zero. Not only is it wasting time to generate the metadata,
446 it needlessly imposes need to re-check for things that are already
447 determined to be errors. */
449 /* Finalize Objective-C runtime data. No need to generate tables
450 and code if only checking syntax, or if generating a PCH file. */
451 if (!flag_syntax_only && !pch_file)
453 location_t saved_location;
455 /* If gen_declaration desired, open the output file. */
456 if (flag_gen_declaration)
458 char * const dumpname = concat (dump_base_name, ".decl", NULL);
459 gen_declaration_file = fopen (dumpname, "w");
460 if (gen_declaration_file == 0)
461 fatal_error (input_location, "can%'t open %s: %m", dumpname);
462 free (dumpname);
465 /* Set the input location to BUILTINS_LOCATION. This is good
466 for error messages, in case any is generated while producing
467 the metadata, but it also silences warnings that would be
468 produced when compiling with -Wpadded in case when padding is
469 automatically added to the built-in runtime data structure
470 declarations. We know about this padding, and it is fine; we
471 don't want users to see any warnings about it if they use
472 -Wpadded. */
473 saved_location = input_location;
474 input_location = BUILTINS_LOCATION;
476 /* Compute and emit the meta-data tables for this runtime. */
477 (*runtime.generate_metadata) ();
479 /* Restore the original location, just in case it mattered. */
480 input_location = saved_location;
482 /* ... and then close any declaration file we opened. */
483 if (gen_declaration_file)
484 fclose (gen_declaration_file);
488 /* Return the first occurrence of a method declaration corresponding
489 to sel_name in rproto_list. Search rproto_list recursively.
490 If is_class is 0, search for instance methods, otherwise for class
491 methods. */
492 static tree
493 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
494 int is_class)
496 tree rproto, p, m;
498 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
500 p = TREE_VALUE (rproto);
501 m = NULL_TREE;
503 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
505 /* First, search the @required protocol methods. */
506 if (is_class)
507 m = lookup_method (PROTOCOL_CLS_METHODS (p), sel_name);
508 else
509 m = lookup_method (PROTOCOL_NST_METHODS (p), sel_name);
511 if (m)
512 return m;
514 /* If still not found, search the @optional protocol methods. */
515 if (is_class)
516 m = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (p), sel_name);
517 else
518 m = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (p), sel_name);
520 if (m)
521 return m;
523 /* If still not found, search the attached protocols. */
524 if (PROTOCOL_LIST (p))
525 m = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
526 sel_name, is_class);
527 if (m)
528 return m;
530 else
532 ; /* An identifier...if we could not find a protocol. */
536 return 0;
539 static tree
540 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
542 tree rproto, p;
544 /* Make sure the protocol is supported by the object on the rhs. */
545 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
547 tree fnd = 0;
548 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
550 p = TREE_VALUE (rproto);
552 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
554 if (lproto == p)
555 fnd = lproto;
557 else if (PROTOCOL_LIST (p))
558 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
561 if (fnd)
562 return fnd;
565 else
567 ; /* An identifier...if we could not find a protocol. */
570 return 0;
573 void
574 objc_start_class_interface (tree klass, tree super_class,
575 tree protos, tree attributes)
577 if (flag_objc1_only && attributes)
578 error_at (input_location, "class attributes are not available in Objective-C 1.0");
580 objc_interface_context
581 = objc_ivar_context
582 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos, attributes);
583 objc_ivar_visibility = objc_default_ivar_visibility;
586 void
587 objc_start_category_interface (tree klass, tree categ,
588 tree protos, tree attributes)
590 if (attributes)
592 if (flag_objc1_only)
593 error_at (input_location, "category attributes are not available in Objective-C 1.0");
594 else
595 warning_at (input_location, OPT_Wattributes,
596 "category attributes are not available in this version"
597 " of the compiler, (ignored)");
599 if (categ == NULL_TREE)
601 if (flag_objc1_only)
602 error_at (input_location, "class extensions are not available in Objective-C 1.0");
603 else
605 /* Iterate over all the classes and categories implemented
606 up to now in this compilation unit. */
607 struct imp_entry *t;
609 for (t = imp_list; t; t = t->next)
611 /* If we find a class @implementation with the same name
612 as the one we are extending, produce an error. */
613 if (TREE_CODE (t->imp_context) == CLASS_IMPLEMENTATION_TYPE
614 && IDENTIFIER_POINTER (CLASS_NAME (t->imp_context)) == IDENTIFIER_POINTER (klass))
615 error_at (input_location,
616 "class extension for class %qE declared after its %<@implementation%>",
617 klass);
621 objc_interface_context
622 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos, NULL_TREE);
623 objc_ivar_chain
624 = continue_class (objc_interface_context);
627 void
628 objc_start_protocol (tree name, tree protos, tree attributes)
630 if (flag_objc1_only && attributes)
631 error_at (input_location, "protocol attributes are not available in Objective-C 1.0");
633 objc_interface_context
634 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos, attributes);
635 objc_method_optional_flag = false;
638 void
639 objc_continue_interface (void)
641 objc_ivar_chain
642 = continue_class (objc_interface_context);
645 void
646 objc_finish_interface (void)
648 finish_class (objc_interface_context);
649 objc_interface_context = NULL_TREE;
650 objc_method_optional_flag = false;
651 objc_in_class_extension = false;
654 void
655 objc_start_class_implementation (tree klass, tree super_class)
657 objc_implementation_context
658 = objc_ivar_context
659 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE,
660 NULL_TREE);
661 objc_ivar_visibility = objc_default_ivar_visibility;
664 void
665 objc_start_category_implementation (tree klass, tree categ)
667 objc_implementation_context
668 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE,
669 NULL_TREE);
670 objc_ivar_chain
671 = continue_class (objc_implementation_context);
674 void
675 objc_continue_implementation (void)
677 objc_ivar_chain
678 = continue_class (objc_implementation_context);
681 void
682 objc_finish_implementation (void)
684 #ifdef OBJCPLUS
685 if (flag_objc_call_cxx_cdtors)
686 objc_generate_cxx_cdtors ();
687 #endif
689 if (objc_implementation_context)
691 finish_class (objc_implementation_context);
692 objc_ivar_chain = NULL_TREE;
693 objc_implementation_context = NULL_TREE;
695 else
696 warning (0, "%<@end%> must appear in an @implementation context");
699 void
700 objc_set_visibility (objc_ivar_visibility_kind visibility)
702 if (visibility == OBJC_IVAR_VIS_PACKAGE)
704 if (flag_objc1_only)
705 error ("%<@package%> is not available in Objective-C 1.0");
706 else
707 warning (0, "%<@package%> presently has the same effect as %<@public%>");
709 objc_ivar_visibility = visibility;
712 void
713 objc_set_method_opt (bool optional)
715 if (flag_objc1_only)
717 if (optional)
718 error_at (input_location, "%<@optional%> is not available in Objective-C 1.0");
719 else
720 error_at (input_location, "%<@required%> is not available in Objective-C 1.0");
723 objc_method_optional_flag = optional;
724 if (!objc_interface_context
725 || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
727 if (optional)
728 error ("%<@optional%> is allowed in @protocol context only");
729 else
730 error ("%<@required%> is allowed in @protocol context only");
731 objc_method_optional_flag = false;
735 /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
736 PROTOCOL. */
737 static tree
738 lookup_property_in_list (tree chain, tree property)
740 tree x;
741 for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x))
742 if (PROPERTY_NAME (x) == property)
743 return x;
744 return NULL_TREE;
747 /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
748 static tree lookup_property_in_protocol_list (tree rproto_list, tree property)
750 tree rproto, x;
751 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
753 tree p = TREE_VALUE (rproto);
754 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
756 if ((x = lookup_property_in_list (p, property)))
757 return x;
758 if (PROTOCOL_LIST (p))
759 return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property);
761 else
763 ; /* An identifier...if we could not find a protocol. */
766 return NULL_TREE;
769 /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
770 chain of interface hierarchy. */
771 static tree
772 lookup_property (tree interface_type, tree property)
774 tree inter = interface_type;
775 while (inter)
777 tree x, category;
778 if ((x = lookup_property_in_list (inter, property)))
779 return x;
780 /* Failing that, look for the property in each category of the class. */
781 category = inter;
782 while ((category = CLASS_CATEGORY_LIST (category)))
784 if ((x = lookup_property_in_list (category, property)))
785 return x;
787 /* When checking a category, also check the protocols
788 attached with the category itself. */
789 if (CLASS_PROTOCOL_LIST (category)
790 && (x = lookup_property_in_protocol_list
791 (CLASS_PROTOCOL_LIST (category), property)))
792 return x;
795 /* Failing to find in categories, look for property in protocol list. */
796 if (CLASS_PROTOCOL_LIST (inter)
797 && (x = lookup_property_in_protocol_list
798 (CLASS_PROTOCOL_LIST (inter), property)))
799 return x;
801 /* Failing that, climb up the inheritance hierarchy. */
802 inter = lookup_interface (CLASS_SUPER_NAME (inter));
804 return inter;
807 /* This routine is called by the parser when a
808 @property... declaration is found. 'decl' is the declaration of
809 the property (type/identifier), and the other arguments represent
810 property attributes that may have been specified in the Objective-C
811 declaration. 'parsed_property_readonly' is 'true' if the attribute
812 'readonly' was specified, and 'false' if not; similarly for the
813 other bool parameters. 'parsed_property_getter_ident' is NULL_TREE
814 if the attribute 'getter' was not specified, and is the identifier
815 corresponding to the specified getter if it was; similarly for
816 'parsed_property_setter_ident'. */
817 void
818 objc_add_property_declaration (location_t location, tree decl,
819 bool parsed_property_readonly, bool parsed_property_readwrite,
820 bool parsed_property_assign, bool parsed_property_retain,
821 bool parsed_property_copy, bool parsed_property_nonatomic,
822 tree parsed_property_getter_ident, tree parsed_property_setter_ident)
824 tree property_decl;
825 tree x;
826 /* 'property_readonly' and 'property_assign_semantics' are the final
827 attributes of the property after all parsed attributes have been
828 considered (eg, if we parsed no 'readonly' and no 'readwrite', ie
829 parsed_property_readonly = false and parsed_property_readwrite =
830 false, then property_readonly will be false because the default
831 is readwrite). */
832 bool property_readonly = false;
833 objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN;
834 bool property_extension_in_class_extension = false;
836 if (flag_objc1_only)
837 error_at (input_location, "%<@property%> is not available in Objective-C 1.0");
839 if (parsed_property_readonly && parsed_property_readwrite)
841 error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute");
842 /* In case of conflicting attributes (here and below), after
843 producing an error, we pick one of the attributes and keep
844 going. */
845 property_readonly = false;
847 else
849 if (parsed_property_readonly)
850 property_readonly = true;
852 if (parsed_property_readwrite)
853 property_readonly = false;
856 if (parsed_property_readonly && parsed_property_setter_ident)
858 error_at (location, "%<readonly%> attribute conflicts with %<setter%> attribute");
859 property_readonly = false;
862 if (parsed_property_assign && parsed_property_retain)
864 error_at (location, "%<assign%> attribute conflicts with %<retain%> attribute");
865 property_assign_semantics = OBJC_PROPERTY_RETAIN;
867 else if (parsed_property_assign && parsed_property_copy)
869 error_at (location, "%<assign%> attribute conflicts with %<copy%> attribute");
870 property_assign_semantics = OBJC_PROPERTY_COPY;
872 else if (parsed_property_retain && parsed_property_copy)
874 error_at (location, "%<retain%> attribute conflicts with %<copy%> attribute");
875 property_assign_semantics = OBJC_PROPERTY_COPY;
877 else
879 if (parsed_property_assign)
880 property_assign_semantics = OBJC_PROPERTY_ASSIGN;
882 if (parsed_property_retain)
883 property_assign_semantics = OBJC_PROPERTY_RETAIN;
885 if (parsed_property_copy)
886 property_assign_semantics = OBJC_PROPERTY_COPY;
889 if (!objc_interface_context)
891 error_at (location, "property declaration not in @interface or @protocol context");
892 return;
895 /* At this point we know that we are either in an interface, a
896 category, or a protocol. */
898 /* We expect a FIELD_DECL from the parser. Make sure we didn't get
899 something else, as that would confuse the checks below. */
900 if (TREE_CODE (decl) != FIELD_DECL)
902 error_at (location, "invalid property declaration");
903 return;
906 /* Do some spot-checks for the most obvious invalid types. */
908 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
910 error_at (location, "property cannot be an array");
911 return;
914 /* The C++/ObjC++ parser seems to reject the ':' for a bitfield when
915 parsing, while the C/ObjC parser accepts it and gives us a
916 FIELD_DECL with a DECL_INITIAL set. So we use the DECL_INITIAL
917 to check for a bitfield when doing ObjC. */
918 #ifndef OBJCPLUS
919 if (DECL_INITIAL (decl))
921 /* A @property is not an actual variable, but it is a way to
922 describe a pair of accessor methods, so its type (which is
923 the type of the return value of the getter and the first
924 argument of the setter) can't be a bitfield (as return values
925 and arguments of functions cannot be bitfields). The
926 underlying instance variable could be a bitfield, but that is
927 a different matter. */
928 error_at (location, "property cannot be a bit-field");
929 return;
931 #endif
933 /* TODO: Check that the property type is an Objective-C object or a
934 "POD". */
936 /* Implement -Wproperty-assign-default (which is enabled by default). */
937 if (warn_property_assign_default
938 /* If garbage collection is not being used, then 'assign' is
939 valid for objects (and typically used for delegates) but it
940 is wrong in most cases (since most objects need to be
941 retained or copied in setters). Warn users when 'assign' is
942 used implicitly. */
943 && property_assign_semantics == OBJC_PROPERTY_ASSIGN
944 /* Read-only properties are never assigned, so the assignment
945 semantics do not matter in that case. */
946 && !property_readonly
947 && !flag_objc_gc)
949 /* Please note that it would make sense to default to 'assign'
950 for non-{Objective-C objects}, and to 'retain' for
951 Objective-C objects. But that would break compatibility with
952 other compilers. */
953 if (!parsed_property_assign && !parsed_property_retain && !parsed_property_copy)
955 /* Use 'false' so we do not warn for Class objects. */
956 if (objc_type_valid_for_messaging (TREE_TYPE (decl), false))
958 warning_at (location,
960 "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>",
961 decl);
962 inform (location,
963 "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it");
968 if (property_assign_semantics == OBJC_PROPERTY_RETAIN
969 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
970 error_at (location, "%<retain%> attribute is only valid for Objective-C objects");
972 if (property_assign_semantics == OBJC_PROPERTY_COPY
973 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
974 error_at (location, "%<copy%> attribute is only valid for Objective-C objects");
976 /* Now determine the final property getter and setter names. They
977 will be stored in the PROPERTY_DECL, from which they'll always be
978 extracted and used. */
980 /* Adjust, or fill in, setter and getter names. We overwrite the
981 parsed_property_setter_ident and parsed_property_getter_ident
982 with the final setter and getter identifiers that will be
983 used. */
984 if (parsed_property_setter_ident)
986 /* The setter should be terminated by ':', but the parser only
987 gives us an identifier without ':'. So, we need to add ':'
988 at the end. */
989 const char *parsed_setter = IDENTIFIER_POINTER (parsed_property_setter_ident);
990 size_t length = strlen (parsed_setter);
991 char *final_setter = (char *)alloca (length + 2);
993 sprintf (final_setter, "%s:", parsed_setter);
994 parsed_property_setter_ident = get_identifier (final_setter);
996 else
998 if (!property_readonly)
999 parsed_property_setter_ident = get_identifier (objc_build_property_setter_name
1000 (DECL_NAME (decl)));
1003 if (!parsed_property_getter_ident)
1004 parsed_property_getter_ident = DECL_NAME (decl);
1006 /* Check for duplicate property declarations. We first check the
1007 immediate context for a property with the same name. Any such
1008 declarations are an error, unless this is a class extension and
1009 we are extending a property from readonly to readwrite. */
1010 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
1012 if (PROPERTY_NAME (x) == DECL_NAME (decl))
1014 if (objc_in_class_extension
1015 && property_readonly == 0
1016 && PROPERTY_READONLY (x) == 1)
1018 /* This is a class extension, and we are extending an
1019 existing readonly property to a readwrite one.
1020 That's fine. :-) */
1021 property_extension_in_class_extension = true;
1022 break;
1024 else
1026 location_t original_location = DECL_SOURCE_LOCATION (x);
1028 error_at (location, "redeclaration of property %qD", decl);
1030 if (original_location != UNKNOWN_LOCATION)
1031 inform (original_location, "originally specified here");
1032 return;
1037 /* If x is not NULL_TREE, we must be in a class extension and we're
1038 extending a readonly property. In that case, no point in
1039 searching for another declaration. */
1040 if (x == NULL_TREE)
1042 /* We now need to check for existing property declarations (in
1043 the superclass, other categories or protocols) and check that
1044 the new declaration is not in conflict with existing
1045 ones. */
1047 /* Search for a previous, existing declaration of a property
1048 with the same name in superclasses, protocols etc. If one is
1049 found, it will be in the 'x' variable. */
1051 /* Note that, for simplicity, the following may search again the
1052 local context. That's Ok as nothing will be found (else we'd
1053 have thrown an error above); it's only a little inefficient,
1054 but the code is simpler. */
1055 switch (TREE_CODE (objc_interface_context))
1057 case CLASS_INTERFACE_TYPE:
1058 /* Look up the property in the current @interface (which
1059 will find nothing), then its protocols and categories and
1060 superclasses. */
1061 x = lookup_property (objc_interface_context, DECL_NAME (decl));
1062 break;
1063 case CATEGORY_INTERFACE_TYPE:
1064 /* Look up the property in the main @interface, then
1065 protocols and categories (one of them is ours, and will
1066 find nothing) and superclasses. */
1067 x = lookup_property (lookup_interface (CLASS_NAME (objc_interface_context)),
1068 DECL_NAME (decl));
1069 break;
1070 case PROTOCOL_INTERFACE_TYPE:
1071 /* Looks up the property in any protocols attached to the
1072 current protocol. */
1073 if (PROTOCOL_LIST (objc_interface_context))
1075 x = lookup_property_in_protocol_list (PROTOCOL_LIST (objc_interface_context),
1076 DECL_NAME (decl));
1078 break;
1079 default:
1080 gcc_unreachable ();
1084 if (x != NULL_TREE)
1086 /* An existing property was found; check that it has the same
1087 types, or it is compatible. */
1088 location_t original_location = DECL_SOURCE_LOCATION (x);
1090 if (PROPERTY_NONATOMIC (x) != parsed_property_nonatomic)
1092 warning_at (location, 0,
1093 "%<nonatomic%> attribute of property %qD conflicts with "
1094 "previous declaration", decl);
1096 if (original_location != UNKNOWN_LOCATION)
1097 inform (original_location, "originally specified here");
1098 return;
1101 if (PROPERTY_GETTER_NAME (x) != parsed_property_getter_ident)
1103 warning_at (location, 0,
1104 "%<getter%> attribute of property %qD conflicts with "
1105 "previous declaration", decl);
1107 if (original_location != UNKNOWN_LOCATION)
1108 inform (original_location, "originally specified here");
1109 return;
1112 /* We can only compare the setter names if both the old and new property have a setter. */
1113 if (!property_readonly && !PROPERTY_READONLY(x))
1115 if (PROPERTY_SETTER_NAME (x) != parsed_property_setter_ident)
1117 warning_at (location, 0,
1118 "%<setter%> attribute of property %qD conflicts with "
1119 "previous declaration", decl);
1121 if (original_location != UNKNOWN_LOCATION)
1122 inform (original_location, "originally specified here");
1123 return;
1127 if (PROPERTY_ASSIGN_SEMANTICS (x) != property_assign_semantics)
1129 warning_at (location, 0,
1130 "assign semantics attributes of property %qD conflict with previous declaration", decl);
1132 if (original_location != UNKNOWN_LOCATION)
1133 inform (original_location, "originally specified here");
1134 return;
1137 /* It's ok to have a readonly property that becomes a readwrite, but not vice versa. */
1138 if (PROPERTY_READONLY (x) == 0 && property_readonly == 1)
1140 warning_at (location, 0,
1141 "%<readonly%> attribute of property %qD conflicts with "
1142 "previous declaration", decl);
1144 if (original_location != UNKNOWN_LOCATION)
1145 inform (original_location, "originally specified here");
1146 return;
1149 /* We now check that the new and old property declarations have
1150 the same types (or compatible one). In the Objective-C
1151 tradition of loose type checking, we do type-checking but
1152 only generate warnings (not errors) if they do not match.
1153 For non-readonly properties, the types must match exactly;
1154 for readonly properties, it is allowed to use a "more
1155 specialized" type in the new property declaration. Eg, the
1156 superclass has a getter returning (NSArray *) and the
1157 subclass a getter returning (NSMutableArray *). The object's
1158 getter returns an (NSMutableArray *); but if you cast the
1159 object to the superclass, which is allowed, you'd still
1160 expect the getter to return an (NSArray *), which works since
1161 an (NSMutableArray *) is an (NSArray *) too. So, the set of
1162 objects belonging to the type of the new @property should be
1163 a subset of the set of objects belonging to the type of the
1164 old @property. This is what "specialization" means. And the
1165 reason it only applies to readonly properties is that for a
1166 readwrite property the setter would have the opposite
1167 requirement - ie that the superclass type is more specialized
1168 then the subclass one; hence the only way to satisfy both
1169 constraints is that the types match. */
1171 /* If the types are not the same in the C sense, we warn ... */
1172 if (!comptypes (TREE_TYPE (x), TREE_TYPE (decl))
1173 /* ... unless the property is readonly, in which case we
1174 allow a new, more specialized, declaration. */
1175 && (!property_readonly
1176 || !objc_compare_types (TREE_TYPE (x),
1177 TREE_TYPE (decl), -5, NULL_TREE)))
1179 warning_at (location, 0,
1180 "type of property %qD conflicts with previous declaration", decl);
1181 if (original_location != UNKNOWN_LOCATION)
1182 inform (original_location, "originally specified here");
1183 return;
1186 /* If we are in a class extension and we're extending a readonly
1187 property in the main @interface, we'll just update the
1188 existing property with the readwrite flag and potentially the
1189 new setter name. */
1190 if (property_extension_in_class_extension)
1192 PROPERTY_READONLY (x) = 0;
1193 PROPERTY_SETTER_NAME (x) = parsed_property_setter_ident;
1194 return;
1198 /* Create a PROPERTY_DECL node. */
1199 property_decl = make_node (PROPERTY_DECL);
1201 /* Copy the basic information from the original decl. */
1202 TREE_TYPE (property_decl) = TREE_TYPE (decl);
1203 DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl);
1204 TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl);
1206 /* Add property-specific information. */
1207 PROPERTY_NAME (property_decl) = DECL_NAME (decl);
1208 PROPERTY_GETTER_NAME (property_decl) = parsed_property_getter_ident;
1209 PROPERTY_SETTER_NAME (property_decl) = parsed_property_setter_ident;
1210 PROPERTY_READONLY (property_decl) = property_readonly;
1211 PROPERTY_NONATOMIC (property_decl) = parsed_property_nonatomic;
1212 PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics;
1213 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1214 PROPERTY_DYNAMIC (property_decl) = 0;
1216 /* Remember the fact that the property was found in the @optional
1217 section in a @protocol, or not. */
1218 if (objc_method_optional_flag)
1219 PROPERTY_OPTIONAL (property_decl) = 1;
1220 else
1221 PROPERTY_OPTIONAL (property_decl) = 0;
1223 /* Note that PROPERTY_GETTER_NAME is always set for all
1224 PROPERTY_DECLs, and PROPERTY_SETTER_NAME is always set for all
1225 PROPERTY_DECLs where PROPERTY_READONLY == 0. Any time we deal
1226 with a getter or setter, we should get the PROPERTY_DECL and use
1227 PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know the correct
1228 names. */
1230 /* Add the PROPERTY_DECL to the list of properties for the class. */
1231 TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context);
1232 CLASS_PROPERTY_DECL (objc_interface_context) = property_decl;
1235 /* This is a subroutine of objc_maybe_build_component_ref. Search the
1236 list of methods in the interface (and, failing that, the local list
1237 in the implementation, and failing that, the protocol list)
1238 provided for a 'setter' or 'getter' for 'component' with default
1239 names (ie, if 'component' is "name", then search for "name" and
1240 "setName:"). It is also possible to specify a different
1241 'getter_name' (this is used for @optional readonly properties). If
1242 any is found, then create an artificial property that uses them.
1243 Return NULL_TREE if 'getter' or 'setter' could not be found. */
1244 static tree
1245 maybe_make_artificial_property_decl (tree interface, tree implementation,
1246 tree protocol_list, tree component, bool is_class,
1247 tree getter_name)
1249 tree setter_name = get_identifier (objc_build_property_setter_name (component));
1250 tree getter = NULL_TREE;
1251 tree setter = NULL_TREE;
1253 if (getter_name == NULL_TREE)
1254 getter_name = component;
1256 /* First, check the @interface and all superclasses. */
1257 if (interface)
1259 int flags = 0;
1261 /* Using instance methods of the root class as accessors is most
1262 likely unwanted and can be extremely confusing (and, most
1263 importantly, other Objective-C 2.0 compilers do not do it).
1264 Turn it off. */
1265 if (is_class)
1266 flags = OBJC_LOOKUP_CLASS | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS;
1268 getter = lookup_method_static (interface, getter_name, flags);
1269 setter = lookup_method_static (interface, setter_name, flags);
1272 /* Second, check the local @implementation context. */
1273 if (!getter && !setter)
1275 if (implementation)
1277 if (is_class)
1279 getter = lookup_method (CLASS_CLS_METHODS (implementation), getter_name);
1280 setter = lookup_method (CLASS_CLS_METHODS (implementation), setter_name);
1282 else
1284 getter = lookup_method (CLASS_NST_METHODS (implementation), getter_name);
1285 setter = lookup_method (CLASS_NST_METHODS (implementation), setter_name);
1290 /* Try the protocol_list if we didn't find anything in the
1291 @interface and in the @implementation. */
1292 if (!getter && !setter)
1294 getter = lookup_method_in_protocol_list (protocol_list, getter_name, is_class);
1295 setter = lookup_method_in_protocol_list (protocol_list, setter_name, is_class);
1298 /* There needs to be at least a getter or setter for this to be a
1299 valid 'object.component' syntax. */
1300 if (getter || setter)
1302 /* Yes ... determine the type of the expression. */
1303 tree property_decl;
1304 tree type;
1306 if (getter)
1307 type = TREE_VALUE (TREE_TYPE (getter));
1308 else
1309 type = TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter)));
1311 /* Create an artificial property declaration with the
1312 information we collected on the type and getter/setter
1313 names. */
1314 property_decl = make_node (PROPERTY_DECL);
1316 TREE_TYPE (property_decl) = type;
1317 DECL_SOURCE_LOCATION (property_decl) = input_location;
1318 TREE_DEPRECATED (property_decl) = 0;
1319 DECL_ARTIFICIAL (property_decl) = 1;
1321 /* Add property-specific information. Note that one of
1322 PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a
1323 non-existing method; this will generate an error when the
1324 expression is later compiled. At this stage we don't know if
1325 the getter or setter will be used, so we can't generate an
1326 error. */
1327 PROPERTY_NAME (property_decl) = component;
1328 PROPERTY_GETTER_NAME (property_decl) = getter_name;
1329 PROPERTY_SETTER_NAME (property_decl) = setter_name;
1330 PROPERTY_READONLY (property_decl) = 0;
1331 PROPERTY_NONATOMIC (property_decl) = 0;
1332 PROPERTY_ASSIGN_SEMANTICS (property_decl) = 0;
1333 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1334 PROPERTY_DYNAMIC (property_decl) = 0;
1335 PROPERTY_OPTIONAL (property_decl) = 0;
1337 if (!getter)
1338 PROPERTY_HAS_NO_GETTER (property_decl) = 1;
1340 /* The following is currently unused, but it's nice to have
1341 there. We may use it if we need in the future. */
1342 if (!setter)
1343 PROPERTY_HAS_NO_SETTER (property_decl) = 1;
1345 return property_decl;
1348 return NULL_TREE;
1351 /* This hook routine is invoked by the parser when an expression such
1352 as 'xxx.yyy' is parsed. We get a chance to process these
1353 expressions in a way that is specified to Objective-C (to implement
1354 the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars).
1355 If the expression is not an Objective-C specified expression, we
1356 should return NULL_TREE; else we return the expression.
1358 At the moment this only implements dot-syntax and properties (not
1359 non-fragile ivars yet), ie 'object.property' or 'object.component'
1360 where 'component' is not a declared property, but a valid getter or
1361 setter for it could be found. */
1362 tree
1363 objc_maybe_build_component_ref (tree object, tree property_ident)
1365 tree x = NULL_TREE;
1366 tree rtype;
1368 /* If we are in Objective-C 1.0 mode, dot-syntax and properties are
1369 not available. */
1370 if (flag_objc1_only)
1371 return NULL_TREE;
1373 /* Try to determine if 'object' is an Objective-C object or not. If
1374 not, return. */
1375 if (object == NULL_TREE || object == error_mark_node
1376 || (rtype = TREE_TYPE (object)) == NULL_TREE)
1377 return NULL_TREE;
1379 if (property_ident == NULL_TREE || property_ident == error_mark_node
1380 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1381 return NULL_TREE;
1383 /* The following analysis of 'object' is similar to the one used for
1384 the 'receiver' of a method invocation. We need to determine what
1385 'object' is and find the appropriate property (either declared,
1386 or artificial) for it (in the same way as we need to find the
1387 appropriate method prototype for a method invocation). There are
1388 some simplifications here though: "object.property" is invalid if
1389 "object" has a type of "id" or "Class"; it must at least have a
1390 protocol attached to it, and "object" is never a class name as
1391 that is done by objc_build_class_component_ref. Finally, we
1392 don't know if this really is a dot-syntax expression, so we want
1393 to make a quick exit if it is not; for this reason, we try to
1394 postpone checks after determining that 'object' looks like an
1395 Objective-C object. */
1397 if (objc_is_id (rtype))
1399 /* This is the case that the 'object' is of type 'id' or
1400 'Class'. */
1402 /* Check if at least it is of type 'id <Protocol>' or 'Class
1403 <Protocol>'; if so, look the property up in the
1404 protocols. */
1405 if (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype)))
1407 tree rprotos = TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype));
1409 if (rprotos)
1411 /* No point looking up declared @properties if we are
1412 dealing with a class. Classes have no declared
1413 properties. */
1414 if (!IS_CLASS (rtype))
1415 x = lookup_property_in_protocol_list (rprotos, property_ident);
1417 if (x == NULL_TREE)
1419 /* Ok, no property. Maybe it was an
1420 object.component dot-syntax without a declared
1421 property (this is valid for classes too). Look
1422 for getter/setter methods and internally declare
1423 an artificial property based on them if found. */
1424 x = maybe_make_artificial_property_decl (NULL_TREE,
1425 NULL_TREE,
1426 rprotos,
1427 property_ident,
1428 IS_CLASS (rtype),
1429 NULL_TREE);
1431 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1433 /* This is a special, complicated case. If the
1434 property is optional, and is read-only, then the
1435 property is always used for reading, but an
1436 eventual existing non-property setter can be used
1437 for writing. We create an artificial property
1438 decl copying the getter from the optional
1439 property, and looking up the setter in the
1440 interface. */
1441 x = maybe_make_artificial_property_decl (NULL_TREE,
1442 NULL_TREE,
1443 rprotos,
1444 property_ident,
1445 false,
1446 PROPERTY_GETTER_NAME (x));
1450 else if (objc_method_context)
1452 /* Else, if we are inside a method it could be the case of
1453 'super' or 'self'. */
1454 tree interface_type = NULL_TREE;
1455 tree t = object;
1456 while (TREE_CODE (t) == COMPOUND_EXPR
1457 || TREE_CODE (t) == MODIFY_EXPR
1458 || CONVERT_EXPR_P (t)
1459 || TREE_CODE (t) == COMPONENT_REF)
1460 t = TREE_OPERAND (t, 0);
1462 STRIP_ANY_LOCATION_WRAPPER (t);
1464 if (t == UOBJC_SUPER_decl)
1465 interface_type = lookup_interface (CLASS_SUPER_NAME (implementation_template));
1466 else if (t == self_decl)
1467 interface_type = lookup_interface (CLASS_NAME (implementation_template));
1469 if (interface_type)
1471 if (TREE_CODE (objc_method_context) != CLASS_METHOD_DECL)
1472 x = lookup_property (interface_type, property_ident);
1474 if (x == NULL_TREE)
1476 /* Try the dot-syntax without a declared property.
1477 If this is an access to 'self', it is possible
1478 that they may refer to a setter/getter that is
1479 not declared in the interface, but exists locally
1480 in the implementation. In that case, get the
1481 implementation context and use it. */
1482 tree implementation = NULL_TREE;
1484 if (t == self_decl)
1485 implementation = objc_implementation_context;
1487 x = maybe_make_artificial_property_decl
1488 (interface_type, implementation, NULL_TREE,
1489 property_ident,
1490 (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL),
1491 NULL_TREE);
1493 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1495 tree implementation = NULL_TREE;
1497 if (t == self_decl)
1498 implementation = objc_implementation_context;
1500 x = maybe_make_artificial_property_decl (interface_type,
1501 implementation,
1502 NULL_TREE,
1503 property_ident,
1504 false,
1505 PROPERTY_GETTER_NAME (x));
1510 else
1512 /* This is the case where we have more information on 'rtype'. */
1513 tree basetype = TYPE_MAIN_VARIANT (rtype);
1515 /* Skip the pointer - if none, it's not an Objective-C object or
1516 class. */
1517 if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE)
1518 basetype = TREE_TYPE (basetype);
1519 else
1520 return NULL_TREE;
1522 /* Traverse typedefs. */
1523 while (basetype != NULL_TREE
1524 && TREE_CODE (basetype) == RECORD_TYPE
1525 && OBJC_TYPE_NAME (basetype)
1526 && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL
1527 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)))
1528 basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype));
1530 if (basetype != NULL_TREE && TYPED_OBJECT (basetype))
1532 tree interface_type = TYPE_OBJC_INTERFACE (basetype);
1533 tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype);
1535 if (interface_type
1536 && (TREE_CODE (interface_type) == CLASS_INTERFACE_TYPE
1537 || TREE_CODE (interface_type) == CATEGORY_INTERFACE_TYPE
1538 || TREE_CODE (interface_type) == PROTOCOL_INTERFACE_TYPE))
1540 /* Not sure 'rtype' could ever be a class here! Just
1541 for safety we keep the checks. */
1542 if (!IS_CLASS (rtype))
1544 x = lookup_property (interface_type, property_ident);
1546 if (x == NULL_TREE)
1547 x = lookup_property_in_protocol_list (protocol_list,
1548 property_ident);
1551 if (x == NULL_TREE)
1553 /* Try the dot-syntax without a declared property.
1554 If we are inside a method implementation, it is
1555 possible that they may refer to a setter/getter
1556 that is not declared in the interface, but exists
1557 locally in the implementation. In that case, get
1558 the implementation context and use it. */
1559 tree implementation = NULL_TREE;
1561 if (objc_implementation_context
1562 && CLASS_NAME (objc_implementation_context)
1563 == OBJC_TYPE_NAME (interface_type))
1564 implementation = objc_implementation_context;
1566 x = maybe_make_artificial_property_decl (interface_type,
1567 implementation,
1568 protocol_list,
1569 property_ident,
1570 IS_CLASS (rtype),
1571 NULL_TREE);
1573 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1575 tree implementation = NULL_TREE;
1577 if (objc_implementation_context
1578 && CLASS_NAME (objc_implementation_context)
1579 == OBJC_TYPE_NAME (interface_type))
1580 implementation = objc_implementation_context;
1582 x = maybe_make_artificial_property_decl (interface_type,
1583 implementation,
1584 protocol_list,
1585 property_ident,
1586 false,
1587 PROPERTY_GETTER_NAME (x));
1593 if (x)
1595 tree expression;
1596 tree getter_call;
1597 tree deprecated_method_prototype = NULL_TREE;
1599 /* We have an additional nasty problem here; if this
1600 PROPERTY_REF needs to become a 'getter', then the conversion
1601 from PROPERTY_REF into a getter call happens in gimplify,
1602 after the selector table has already been generated and when
1603 it is too late to add another selector to it. To work around
1604 the problem, we always create the getter call at this stage,
1605 which puts the selector in the table. Note that if the
1606 PROPERTY_REF becomes a 'setter' instead of a 'getter', then
1607 we have added a selector too many to the selector table.
1608 This is a little inefficient.
1610 Also note that method calls to 'self' and 'super' require the
1611 context (self_decl, UOBJS_SUPER_decl,
1612 objc_implementation_context etc) to be built correctly; this
1613 is yet another reason why building the call at the gimplify
1614 stage (when this context has been lost) is not very
1615 practical. If we build it at this stage, we know it will
1616 always be built correctly.
1618 If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial
1619 property decl created to deal with a dotsyntax not really
1620 referring to an existing property) then do not try to build a
1621 call to the getter as there is no getter. */
1622 if (PROPERTY_HAS_NO_GETTER (x))
1623 getter_call = NULL_TREE;
1624 else
1625 getter_call = objc_finish_message_expr
1626 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1627 /* Disable the immediate deprecation warning if the getter
1628 is deprecated, but record the fact that the getter is
1629 deprecated by setting PROPERTY_REF_DEPRECATED_GETTER to
1630 the method prototype. */
1631 &deprecated_method_prototype);
1633 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1634 deprecated_method_prototype);
1635 SET_EXPR_LOCATION (expression, input_location);
1636 TREE_SIDE_EFFECTS (expression) = 1;
1638 return expression;
1641 return NULL_TREE;
1644 /* This hook routine is invoked by the parser when an expression such
1645 as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the
1646 Objective-C 2.0 dot-syntax applied to classes, so we need to
1647 convert it into a setter/getter call on the class. */
1648 tree
1649 objc_build_class_component_ref (tree class_name, tree property_ident)
1651 tree x = NULL_TREE;
1652 tree object, rtype;
1654 if (flag_objc1_only)
1655 error_at (input_location, "the dot syntax is not available in Objective-C 1.0");
1657 if (class_name == NULL_TREE || class_name == error_mark_node
1658 || TREE_CODE (class_name) != IDENTIFIER_NODE)
1659 return error_mark_node;
1661 if (property_ident == NULL_TREE || property_ident == error_mark_node
1662 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1663 return NULL_TREE;
1665 object = objc_get_class_reference (class_name);
1666 if (!object)
1668 /* We know that 'class_name' is an Objective-C class name as the
1669 parser won't call this function if it is not. This is only a
1670 double-check for safety. */
1671 error_at (input_location, "could not find class %qE", class_name);
1672 return error_mark_node;
1675 rtype = lookup_interface (class_name);
1676 if (!rtype)
1678 /* Again, this should never happen, but we do check. */
1679 error_at (input_location, "could not find interface for class %qE", class_name);
1680 return error_mark_node;
1682 else
1684 if (TREE_DEPRECATED (rtype))
1685 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", class_name);
1688 x = maybe_make_artificial_property_decl (rtype, NULL_TREE, NULL_TREE,
1689 property_ident,
1690 true, NULL_TREE);
1692 if (x)
1694 tree expression;
1695 tree getter_call;
1696 tree deprecated_method_prototype = NULL_TREE;
1698 if (PROPERTY_HAS_NO_GETTER (x))
1699 getter_call = NULL_TREE;
1700 else
1701 getter_call = objc_finish_message_expr
1702 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1703 &deprecated_method_prototype);
1705 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1706 deprecated_method_prototype);
1707 SET_EXPR_LOCATION (expression, input_location);
1708 TREE_SIDE_EFFECTS (expression) = 1;
1710 return expression;
1712 else
1714 error_at (input_location, "could not find setter/getter for %qE in class %qE",
1715 property_ident, class_name);
1716 return error_mark_node;
1719 return NULL_TREE;
1724 /* This is used because we don't want to expose PROPERTY_REF to the
1725 C/C++ frontends. Maybe we should! */
1726 bool
1727 objc_is_property_ref (tree node)
1729 if (node && TREE_CODE (node) == PROPERTY_REF)
1730 return true;
1731 else
1732 return false;
1735 /* This function builds a setter call for a PROPERTY_REF (real, for a
1736 declared property, or artificial, for a dot-syntax accessor which
1737 is not corresponding to a property). 'lhs' must be a PROPERTY_REF
1738 (the caller must check this beforehand). 'rhs' is the value to
1739 assign to the property. A plain setter call is returned, or
1740 error_mark_node if the property is readonly. */
1742 static tree
1743 objc_build_setter_call (tree lhs, tree rhs)
1745 tree object_expr = PROPERTY_REF_OBJECT (lhs);
1746 tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs);
1748 if (PROPERTY_READONLY (property_decl))
1750 error ("readonly property cannot be set");
1751 return error_mark_node;
1753 else
1755 tree setter_argument = build_tree_list (NULL_TREE, rhs);
1756 tree setter;
1758 /* TODO: Check that the setter return type is 'void'. */
1760 /* TODO: Decay arguments in C. */
1761 setter = objc_finish_message_expr (object_expr,
1762 PROPERTY_SETTER_NAME (property_decl),
1763 setter_argument, NULL);
1764 return setter;
1767 /* Unreachable, but the compiler may not realize. */
1768 return error_mark_node;
1771 /* This hook routine is called when a MODIFY_EXPR is being built. We
1772 check what is being modified; if it is a PROPERTY_REF, we need to
1773 generate a 'setter' function call for the property. If this is not
1774 a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go
1775 on creating their MODIFY_EXPR.
1777 This is used for example if you write
1779 object.count = 1;
1781 where 'count' is a property. The left-hand side creates a
1782 PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR
1783 to assign something to it. We intercept that here, and generate a
1784 call to the 'setter' method instead. */
1785 tree
1786 objc_maybe_build_modify_expr (tree lhs, tree rhs)
1788 if (lhs && TREE_CODE (lhs) == PROPERTY_REF)
1790 /* Building a simple call to the setter method would work for cases such as
1792 object.count = 1;
1794 but wouldn't work for cases such as
1796 count = object2.count = 1;
1798 to get these to work with very little effort, we build a
1799 compound statement which does the setter call (to set the
1800 property to 'rhs'), but which can also be evaluated returning
1801 the 'rhs'. If the 'rhs' has no side effects, we can simply
1802 evaluate it twice, building
1804 ([object setProperty: rhs]; rhs)
1806 If it has side effects, we put it in a temporary variable first,
1807 so we create the following:
1809 (temp = rhs; [object setProperty: temp]; temp)
1811 setter_argument is rhs in the first case, and temp in the second
1812 case.
1814 tree setter_argument;
1816 /* s1, s2 and s3 are the tree statements that we need in the
1817 compound expression. */
1818 tree s1, s2, s3, compound_expr;
1820 if (TREE_SIDE_EFFECTS (rhs))
1822 tree bind;
1824 /* Declare __objc_property_temp in a local bind. */
1825 setter_argument = objc_create_temporary_var (TREE_TYPE (rhs), "__objc_property_temp");
1826 DECL_SOURCE_LOCATION (setter_argument) = input_location;
1827 bind = build3 (BIND_EXPR, void_type_node, setter_argument, NULL, NULL);
1828 SET_EXPR_LOCATION (bind, input_location);
1829 TREE_SIDE_EFFECTS (bind) = 1;
1830 add_stmt (bind);
1832 /* s1: x = rhs */
1833 s1 = build_modify_expr (input_location, setter_argument, NULL_TREE,
1834 NOP_EXPR,
1835 input_location, rhs, NULL_TREE);
1836 SET_EXPR_LOCATION (s1, input_location);
1838 else
1840 /* No s1. */
1841 setter_argument = rhs;
1842 s1 = NULL_TREE;
1845 /* Now build the compound statement. */
1847 /* s2: [object setProperty: x] */
1848 s2 = objc_build_setter_call (lhs, setter_argument);
1850 /* This happens if building the setter failed because the
1851 property is readonly. */
1852 if (s2 == error_mark_node)
1853 return error_mark_node;
1855 SET_EXPR_LOCATION (s2, input_location);
1857 /* s3: x */
1858 s3 = convert (TREE_TYPE (lhs), setter_argument);
1860 /* Now build the compound statement (s1, s2, s3) or (s2, s3) as
1861 appropriate. */
1862 if (s1)
1863 compound_expr = build_compound_expr (input_location, build_compound_expr (input_location, s1, s2), s3);
1864 else
1865 compound_expr = build_compound_expr (input_location, s2, s3);
1867 /* Without this, with -Wall you get a 'valued computed is not
1868 used' every time there is a "object.property = x" where the
1869 value of the resulting MODIFY_EXPR is not used. That is
1870 correct (maybe a more sophisticated implementation could
1871 avoid generating the compound expression if not needed), but
1872 we need to turn it off. */
1873 TREE_NO_WARNING (compound_expr) = 1;
1874 return compound_expr;
1876 else
1877 return NULL_TREE;
1880 /* This hook is called by the frontend when one of the four unary
1881 expressions PREINCREMENT_EXPR, POSTINCREMENT_EXPR,
1882 PREDECREMENT_EXPR and POSTDECREMENT_EXPR is being built with an
1883 argument which is a PROPERTY_REF. For example, this happens if you have
1885 object.count++;
1887 where 'count' is a property. We need to use the 'getter' and
1888 'setter' for the property in an appropriate way to build the
1889 appropriate expression. 'code' is the code for the expression (one
1890 of the four mentioned above); 'argument' is the PROPERTY_REF, and
1891 'increment' is how much we need to add or subtract. */
1892 tree
1893 objc_build_incr_expr_for_property_ref (location_t location,
1894 enum tree_code code,
1895 tree argument, tree increment)
1897 /* Here are the expressions that we want to build:
1899 For PREINCREMENT_EXPR / PREDECREMENT_EXPR:
1900 (temp = [object property] +/- increment, [object setProperty: temp], temp)
1902 For POSTINCREMENT_EXPR / POSTECREMENT_EXPR:
1903 (temp = [object property], [object setProperty: temp +/- increment], temp) */
1905 tree temp_variable_decl, bind;
1906 /* s1, s2 and s3 are the tree statements that we need in the
1907 compound expression. */
1908 tree s1, s2, s3, compound_expr;
1910 /* Safety check. */
1911 if (!argument || TREE_CODE (argument) != PROPERTY_REF)
1912 return error_mark_node;
1914 /* Declare __objc_property_temp in a local bind. */
1915 temp_variable_decl = objc_create_temporary_var (TREE_TYPE (argument), "__objc_property_temp");
1916 DECL_SOURCE_LOCATION (temp_variable_decl) = location;
1917 bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL);
1918 SET_EXPR_LOCATION (bind, location);
1919 TREE_SIDE_EFFECTS (bind) = 1;
1920 add_stmt (bind);
1922 /* Now build the compound statement. */
1924 /* Note that the 'getter' is generated at gimplify time; at this
1925 time, we can simply put the property_ref (ie, argument) wherever
1926 we want the getter ultimately to be. */
1928 /* s1: __objc_property_temp = [object property] <+/- increment> */
1929 switch (code)
1931 case PREINCREMENT_EXPR:
1932 /* __objc_property_temp = [object property] + increment */
1933 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1934 NOP_EXPR,
1935 location, build2 (PLUS_EXPR, TREE_TYPE (argument),
1936 argument, increment), NULL_TREE);
1937 break;
1938 case PREDECREMENT_EXPR:
1939 /* __objc_property_temp = [object property] - increment */
1940 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1941 NOP_EXPR,
1942 location, build2 (MINUS_EXPR, TREE_TYPE (argument),
1943 argument, increment), NULL_TREE);
1944 break;
1945 case POSTINCREMENT_EXPR:
1946 case POSTDECREMENT_EXPR:
1947 /* __objc_property_temp = [object property] */
1948 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1949 NOP_EXPR,
1950 location, argument, NULL_TREE);
1951 break;
1952 default:
1953 gcc_unreachable ();
1956 /* s2: [object setProperty: __objc_property_temp <+/- increment>] */
1957 switch (code)
1959 case PREINCREMENT_EXPR:
1960 case PREDECREMENT_EXPR:
1961 /* [object setProperty: __objc_property_temp] */
1962 s2 = objc_build_setter_call (argument, temp_variable_decl);
1963 break;
1964 case POSTINCREMENT_EXPR:
1965 /* [object setProperty: __objc_property_temp + increment] */
1966 s2 = objc_build_setter_call (argument,
1967 build2 (PLUS_EXPR, TREE_TYPE (argument),
1968 temp_variable_decl, increment));
1969 break;
1970 case POSTDECREMENT_EXPR:
1971 /* [object setProperty: __objc_property_temp - increment] */
1972 s2 = objc_build_setter_call (argument,
1973 build2 (MINUS_EXPR, TREE_TYPE (argument),
1974 temp_variable_decl, increment));
1975 break;
1976 default:
1977 gcc_unreachable ();
1980 /* This happens if building the setter failed because the property
1981 is readonly. */
1982 if (s2 == error_mark_node)
1983 return error_mark_node;
1985 SET_EXPR_LOCATION (s2, location);
1987 /* s3: __objc_property_temp */
1988 s3 = convert (TREE_TYPE (argument), temp_variable_decl);
1990 /* Now build the compound statement (s1, s2, s3) */
1991 compound_expr = build_compound_expr (location, build_compound_expr (location, s1, s2), s3);
1993 /* Prevent C++ from warning with -Wall that "right operand of comma
1994 operator has no effect". */
1995 TREE_NO_WARNING (compound_expr) = 1;
1996 return compound_expr;
1999 tree
2000 objc_build_method_signature (bool is_class_method, tree rettype, tree selector,
2001 tree optparms, bool ellipsis)
2003 if (is_class_method)
2004 return build_method_decl (CLASS_METHOD_DECL, rettype, selector,
2005 optparms, ellipsis);
2006 else
2007 return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector,
2008 optparms, ellipsis);
2011 void
2012 objc_add_method_declaration (bool is_class_method, tree decl, tree attributes)
2014 if (!objc_interface_context)
2016 /* PS: At the moment, due to how the parser works, it should be
2017 impossible to get here. But it's good to have the check in
2018 case the parser changes.
2020 fatal_error (input_location,
2021 "method declaration not in @interface context");
2024 if (flag_objc1_only && attributes)
2025 error_at (input_location, "method attributes are not available in Objective-C 1.0");
2027 objc_decl_method_attributes (&decl, attributes, 0);
2028 objc_add_method (objc_interface_context,
2029 decl,
2030 is_class_method,
2031 objc_method_optional_flag);
2034 /* Return 'true' if the method definition could be started, and
2035 'false' if not (because we are outside an @implementation context).
2036 EXPR is NULL or an expression that needs to be evaluated for the
2037 side effects of array size expressions in the parameters.
2039 bool
2040 objc_start_method_definition (bool is_class_method, tree decl, tree attributes,
2041 tree expr)
2043 if (!objc_implementation_context)
2045 error ("method definition not in @implementation context");
2046 return false;
2049 if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node)
2050 return false;
2052 #ifndef OBJCPLUS
2053 /* Indicate no valid break/continue context by setting these variables
2054 to some non-null, non-label value. We'll notice and emit the proper
2055 error message in c_finish_bc_stmt. */
2056 c_break_label = c_cont_label = size_zero_node;
2057 #endif
2059 if (attributes)
2060 warning_at (input_location, 0, "method attributes cannot be specified in @implementation context");
2061 else
2062 objc_decl_method_attributes (&decl, attributes, 0);
2064 objc_add_method (objc_implementation_context,
2065 decl,
2066 is_class_method,
2067 /* is optional */ false);
2068 start_method_def (decl, expr);
2069 return true;
2072 void
2073 objc_add_instance_variable (tree decl)
2075 (void) add_instance_variable (objc_ivar_context,
2076 objc_ivar_visibility,
2077 decl);
2080 /* Construct a C struct with same name as KLASS, a base struct with tag
2081 SUPER_NAME (if any), and FIELDS indicated. */
2083 static tree
2084 objc_build_struct (tree klass, tree fields, tree super_name)
2086 tree name = CLASS_NAME (klass);
2087 tree s = objc_start_struct (name);
2088 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
2089 tree t;
2090 vec<tree> objc_info = vNULL;
2091 int i;
2093 if (super)
2095 /* Prepend a packed variant of the base class into the layout. This
2096 is necessary to preserve ObjC ABI compatibility. */
2097 tree base = build_decl (input_location,
2098 FIELD_DECL, NULL_TREE, super);
2099 tree field = TYPE_FIELDS (super);
2101 while (field && DECL_CHAIN (field)
2102 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
2103 field = DECL_CHAIN (field);
2105 /* For ObjC ABI purposes, the "packed" size of a base class is
2106 the sum of the offset and the size (in bits) of the last field
2107 in the class. */
2108 DECL_SIZE (base)
2109 = (field && TREE_CODE (field) == FIELD_DECL
2110 ? size_binop (PLUS_EXPR,
2111 size_binop (PLUS_EXPR,
2112 size_binop
2113 (MULT_EXPR,
2114 convert (bitsizetype,
2115 DECL_FIELD_OFFSET (field)),
2116 bitsize_int (BITS_PER_UNIT)),
2117 DECL_FIELD_BIT_OFFSET (field)),
2118 DECL_SIZE (field))
2119 : bitsize_zero_node);
2120 DECL_SIZE_UNIT (base)
2121 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
2122 size_int (BITS_PER_UNIT));
2123 DECL_ARTIFICIAL (base) = 1;
2124 SET_DECL_ALIGN (base, 1);
2125 DECL_FIELD_CONTEXT (base) = s;
2126 #ifdef OBJCPLUS
2127 DECL_FIELD_IS_BASE (base) = 1;
2129 if (fields)
2130 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
2131 #endif /* are following the ObjC ABI here. */
2132 DECL_CHAIN (base) = fields;
2133 fields = base;
2136 /* NB: Calling finish_struct() may cause type TYPE_OBJC_INFO
2137 information in all variants of this RECORD_TYPE to be destroyed
2138 (this is because the C frontend manipulates TYPE_LANG_SPECIFIC
2139 for something else and then will change all variants to use the
2140 same resulting TYPE_LANG_SPECIFIC, ignoring the fact that we use
2141 it for ObjC protocols and that such propagation will make all
2142 variants use the same objc_info), but it is therein that we store
2143 protocol conformance info (e.g., 'NSObject <MyProtocol>').
2144 Hence, we must save the ObjC-specific information before calling
2145 finish_struct(), and then reinstate it afterwards. */
2147 for (t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
2149 INIT_TYPE_OBJC_INFO (t);
2150 objc_info.safe_push (TYPE_OBJC_INFO (t));
2153 s = objc_finish_struct (s, fields);
2155 for (i = 0, t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
2157 /* We now want to restore the different TYPE_OBJC_INFO, but we
2158 have the additional problem that the C frontend doesn't just
2159 copy TYPE_LANG_SPECIFIC from one variant to the other; it
2160 actually makes all of them the *same* TYPE_LANG_SPECIFIC. As
2161 we need a different TYPE_OBJC_INFO for each (and
2162 TYPE_OBJC_INFO is a field in TYPE_LANG_SPECIFIC), we need to
2163 make a copy of each TYPE_LANG_SPECIFIC before we modify
2164 TYPE_OBJC_INFO. */
2165 if (TYPE_LANG_SPECIFIC (t))
2167 /* Create a copy of TYPE_LANG_SPECIFIC. */
2168 struct lang_type *old_lang_type = TYPE_LANG_SPECIFIC (t);
2169 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2170 memcpy (TYPE_LANG_SPECIFIC (t), old_lang_type,
2171 SIZEOF_OBJC_TYPE_LANG_SPECIFIC);
2173 else
2175 /* Just create a new one. */
2176 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2178 /* Replace TYPE_OBJC_INFO with the saved one. This restores any
2179 protocol information that may have been associated with the
2180 type. */
2181 TYPE_OBJC_INFO (t) = objc_info[i];
2182 /* Replace the IDENTIFIER_NODE with an actual @interface now
2183 that we have it. */
2184 TYPE_OBJC_INTERFACE (t) = klass;
2186 objc_info.release ();
2188 /* Use TYPE_BINFO structures to point at the super class, if any. */
2189 objc_xref_basetypes (s, super);
2191 /* Mark this struct as a class template. */
2192 CLASS_STATIC_TEMPLATE (klass) = s;
2194 return s;
2197 /* Mark DECL as being 'volatile' for purposes of Darwin
2198 _setjmp()/_longjmp() exception handling. Called from
2199 objc_mark_locals_volatile(). */
2200 void
2201 objc_volatilize_decl (tree decl)
2203 /* Do not mess with variables that are 'static' or (already)
2204 'volatile'. */
2205 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
2206 && (TREE_CODE (decl) == VAR_DECL
2207 || TREE_CODE (decl) == PARM_DECL))
2209 if (local_variables_to_volatilize == NULL)
2210 vec_alloc (local_variables_to_volatilize, 8);
2212 vec_safe_push (local_variables_to_volatilize, decl);
2216 /* Called when parsing of a function completes; if any local variables
2217 in the function were marked as variables to volatilize, change them
2218 to volatile. We do this at the end of the function when the
2219 warnings about discarding 'volatile' have already been produced.
2220 We are making the variables as volatile just to force the compiler
2221 to preserve them between setjmp/longjmp, but we don't want warnings
2222 for them as they aren't really volatile. */
2223 void
2224 objc_finish_function (void)
2226 /* If there are any local variables to volatilize, volatilize them. */
2227 if (local_variables_to_volatilize)
2229 int i;
2230 tree decl;
2231 FOR_EACH_VEC_ELT (*local_variables_to_volatilize, i, decl)
2233 tree t = TREE_TYPE (decl);
2235 t = build_qualified_type (t, TYPE_QUALS (t) | TYPE_QUAL_VOLATILE);
2236 TREE_TYPE (decl) = t;
2237 TREE_THIS_VOLATILE (decl) = 1;
2238 TREE_SIDE_EFFECTS (decl) = 1;
2239 DECL_REGISTER (decl) = 0;
2240 #ifndef OBJCPLUS
2241 C_DECL_REGISTER (decl) = 0;
2242 #endif
2245 /* Now we delete the vector. This sets it to NULL as well. */
2246 vec_free (local_variables_to_volatilize);
2250 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
2251 (including its categories and superclasses) or by object type TYP.
2252 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
2254 static bool
2255 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
2257 bool class_type = (cls != NULL_TREE);
2259 while (cls)
2261 tree c;
2263 /* Check protocols adopted by the class and its categories. */
2264 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
2266 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
2267 return true;
2270 /* Repeat for superclasses. */
2271 cls = lookup_interface (CLASS_SUPER_NAME (cls));
2274 /* Check for any protocols attached directly to the object type. */
2275 if (TYPE_HAS_OBJC_INFO (typ))
2277 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
2278 return true;
2281 if (warn)
2283 *errbuf = 0;
2284 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
2285 /* NB: Types 'id' and 'Class' cannot reasonably be described as
2286 "implementing" a given protocol, since they do not have an
2287 implementation. */
2288 if (class_type)
2289 warning (0, "class %qs does not implement the %qE protocol",
2290 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2291 else
2292 warning (0, "type %qs does not conform to the %qE protocol",
2293 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2296 return false;
2299 /* Check if class RCLS and instance struct type RTYP conform to at least the
2300 same protocols that LCLS and LTYP conform to. */
2302 static bool
2303 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
2305 tree p;
2306 bool have_lproto = false;
2308 while (lcls)
2310 /* NB: We do _not_ look at categories defined for LCLS; these may or
2311 may not get loaded in, and therefore it is unreasonable to require
2312 that RCLS/RTYP must implement any of their protocols. */
2313 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
2315 have_lproto = true;
2317 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2318 return warn;
2321 /* Repeat for superclasses. */
2322 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
2325 /* Check for any protocols attached directly to the object type. */
2326 if (TYPE_HAS_OBJC_INFO (ltyp))
2328 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
2330 have_lproto = true;
2332 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2333 return warn;
2337 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
2338 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
2339 away with simply checking for 'id' or 'Class' (!RCLS), since this
2340 routine will not get called in other cases. */
2341 return have_lproto || (rcls != NULL_TREE);
2344 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
2345 Both TYPE1 and TYPE2 must be pointers, and already determined to be
2346 compatible by objc_compare_types() below. */
2348 tree
2349 objc_common_type (tree type1, tree type2)
2351 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
2353 while (POINTER_TYPE_P (inner1))
2355 inner1 = TREE_TYPE (inner1);
2356 inner2 = TREE_TYPE (inner2);
2359 /* If one type is derived from another, return the base type. */
2360 if (DERIVED_FROM_P (inner1, inner2))
2361 return type1;
2362 else if (DERIVED_FROM_P (inner2, inner1))
2363 return type2;
2365 /* If both types are 'Class', return 'Class'. */
2366 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
2367 return objc_class_type;
2369 /* Otherwise, return 'id'. */
2370 return objc_object_type;
2373 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
2374 an instance of RTYP to an instance of LTYP or to compare the two
2375 (if ARGNO is equal to -3), per ObjC type system rules. Before
2376 returning 'true', this routine may issue warnings related to, e.g.,
2377 protocol conformance. When returning 'false', the routine must
2378 produce absolutely no warnings; the C or C++ front-end will do so
2379 instead, if needed. If either LTYP or RTYP is not an Objective-C
2380 type, the routine must return 'false'.
2382 The ARGNO parameter is encoded as follows:
2383 >= 1 Parameter number (CALLEE contains function being called);
2384 0 Return value;
2385 -1 Assignment;
2386 -2 Initialization;
2387 -3 Comparison (LTYP and RTYP may match in either direction);
2388 -4 Silent comparison (for C++ overload resolution);
2389 -5 Silent "specialization" comparison for RTYP to be a "specialization"
2390 of LTYP (a specialization means that RTYP is LTYP plus some constraints,
2391 so that each object of type RTYP is also of type LTYP). This is used
2392 when comparing property types. */
2394 bool
2395 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
2397 tree lcls, rcls, lproto, rproto;
2398 bool pointers_compatible;
2400 /* We must be dealing with pointer types */
2401 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
2402 return false;
2406 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2407 rtyp = TREE_TYPE (rtyp);
2409 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2411 /* We must also handle function pointers, since ObjC is a bit more
2412 lenient than C or C++ on this. */
2413 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
2415 function_args_iterator liter, riter;
2417 /* Return types must be covariant. */
2418 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
2419 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
2420 argno, callee))
2421 return false;
2423 /* Argument types must be contravariant. */
2424 function_args_iter_init (&liter, ltyp);
2425 function_args_iter_init (&riter, rtyp);
2427 while (1)
2429 ltyp = function_args_iter_cond (&liter);
2430 rtyp = function_args_iter_cond (&riter);
2432 /* If we've exhaused both lists simulateously, we're done. */
2433 if (ltyp == NULL_TREE && rtyp == NULL_TREE)
2434 break;
2436 /* If one list is shorter than the other, they fail to match. */
2437 if (ltyp == NULL_TREE || rtyp == NULL_TREE)
2438 return false;
2440 if (!comptypes (rtyp, ltyp)
2441 && !objc_compare_types (rtyp, ltyp, argno, callee))
2442 return false;
2444 function_args_iter_next (&liter);
2445 function_args_iter_next (&riter);
2448 return true;
2451 /* Past this point, we are only interested in ObjC class instances,
2452 or 'id' or 'Class'. */
2453 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
2454 return false;
2456 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
2457 && !TYPE_HAS_OBJC_INFO (ltyp))
2458 return false;
2460 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
2461 && !TYPE_HAS_OBJC_INFO (rtyp))
2462 return false;
2464 /* Past this point, we are committed to returning 'true' to the caller
2465 (unless performing a silent comparison; see below). However, we can
2466 still warn about type and/or protocol mismatches. */
2468 if (TYPE_HAS_OBJC_INFO (ltyp))
2470 lcls = TYPE_OBJC_INTERFACE (ltyp);
2471 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
2473 else
2474 lcls = lproto = NULL_TREE;
2476 if (TYPE_HAS_OBJC_INFO (rtyp))
2478 rcls = TYPE_OBJC_INTERFACE (rtyp);
2479 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
2481 else
2482 rcls = rproto = NULL_TREE;
2484 /* If we could not find an @interface declaration, we must have
2485 only seen a @class declaration; for purposes of type comparison,
2486 treat it as a stand-alone (root) class. */
2488 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
2489 lcls = NULL_TREE;
2491 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
2492 rcls = NULL_TREE;
2494 /* If either type is an unqualified 'id', we're done. This is because
2495 an 'id' can be assigned to or from any type with no warnings. */
2496 if (argno != -5)
2498 if ((!lproto && objc_is_object_id (ltyp))
2499 || (!rproto && objc_is_object_id (rtyp)))
2500 return true;
2502 else
2504 /* For property checks, though, an 'id' is considered the most
2505 general type of object, hence if you try to specialize an
2506 'NSArray *' (ltyp) property with an 'id' (rtyp) one, we need
2507 to warn. */
2508 if (!lproto && objc_is_object_id (ltyp))
2509 return true;
2512 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
2514 /* If the underlying types are the same, and at most one of them has
2515 a protocol list, we do not need to issue any diagnostics. */
2516 if (pointers_compatible && (!lproto || !rproto))
2517 return true;
2519 /* If exactly one of the types is 'Class', issue a diagnostic; any
2520 exceptions of this rule have already been handled. */
2521 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
2522 pointers_compatible = false;
2523 /* Otherwise, check for inheritance relations. */
2524 else
2526 if (!pointers_compatible)
2528 /* Again, if any of the two is an 'id', we're satisfied,
2529 unless we're comparing properties, in which case only an
2530 'id' on the left-hand side (old property) is good
2531 enough. */
2532 if (argno != -5)
2533 pointers_compatible
2534 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
2535 else
2536 pointers_compatible = objc_is_object_id (ltyp);
2539 if (!pointers_compatible)
2540 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
2542 if (!pointers_compatible && (argno == -3 || argno == -4))
2543 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
2546 /* If the pointers match modulo protocols, check for protocol conformance
2547 mismatches. */
2548 if (pointers_compatible)
2550 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
2551 argno != -3);
2553 if (!pointers_compatible && argno == -3)
2554 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
2555 argno != -3);
2558 if (!pointers_compatible)
2560 /* The two pointers are not exactly compatible. Issue a warning, unless
2561 we are performing a silent comparison, in which case return 'false'
2562 instead. */
2563 /* NB: For the time being, we shall make our warnings look like their
2564 C counterparts. In the future, we may wish to make them more
2565 ObjC-specific. */
2566 switch (argno)
2568 case -5:
2569 case -4:
2570 return false;
2572 case -3:
2573 warning (0, "comparison of distinct Objective-C types lacks a cast");
2574 break;
2576 case -2:
2577 warning (0, "initialization from distinct Objective-C type");
2578 break;
2580 case -1:
2581 warning (0, "assignment from distinct Objective-C type");
2582 break;
2584 case 0:
2585 warning (0, "distinct Objective-C type in return");
2586 break;
2588 default:
2589 warning (0, "passing argument %d of %qE from distinct "
2590 "Objective-C type", argno, callee);
2591 break;
2595 return true;
2598 /* This routine is similar to objc_compare_types except that function-pointers are
2599 excluded. This is because, caller assumes that common types are of (id, Object*)
2600 variety and calls objc_common_type to obtain a common type. There is no commonolty
2601 between two function-pointers in this regard. */
2603 bool
2604 objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
2606 if (objc_compare_types (ltyp, rtyp, argno, callee))
2608 /* exclude function-pointer types. */
2611 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2612 rtyp = TREE_TYPE (rtyp);
2614 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2615 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
2617 return false;
2620 #ifndef OBJCPLUS
2621 /* Determine if CHILD is derived from PARENT. The routine assumes that
2622 both parameters are RECORD_TYPEs, and is non-reflexive. */
2624 static bool
2625 objc_derived_from_p (tree parent, tree child)
2627 parent = TYPE_MAIN_VARIANT (parent);
2629 for (child = TYPE_MAIN_VARIANT (child);
2630 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
2632 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
2633 (TYPE_BINFO (child),
2634 0)));
2636 if (child == parent)
2637 return true;
2640 return false;
2642 #endif
2644 tree
2645 objc_build_component_ref (tree datum, tree component)
2647 /* If COMPONENT is NULL, the caller is referring to the anonymous
2648 base class field. */
2649 if (!component)
2651 tree base = TYPE_FIELDS (TREE_TYPE (datum));
2653 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
2656 /* The 'build_component_ref' routine has been removed from the C++
2657 front-end, but 'finish_class_member_access_expr' seems to be
2658 a worthy substitute. */
2659 #ifdef OBJCPLUS
2660 return finish_class_member_access_expr (datum, component, false,
2661 tf_warning_or_error);
2662 #else
2663 return build_component_ref (input_location, datum, component,
2664 UNKNOWN_LOCATION);
2665 #endif
2668 /* Recursively copy inheritance information rooted at BINFO. To do this,
2669 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
2671 static tree
2672 objc_copy_binfo (tree binfo)
2674 tree btype = BINFO_TYPE (binfo);
2675 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
2676 tree base_binfo;
2677 int ix;
2679 BINFO_TYPE (binfo2) = btype;
2680 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
2681 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
2683 /* Recursively copy base binfos of BINFO. */
2684 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2686 tree base_binfo2 = objc_copy_binfo (base_binfo);
2688 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
2689 BINFO_BASE_APPEND (binfo2, base_binfo2);
2692 return binfo2;
2695 /* Record superclass information provided in BASETYPE for ObjC class REF.
2696 This is loosely based on cp/decl.c:xref_basetypes(). */
2698 static void
2699 objc_xref_basetypes (tree ref, tree basetype)
2701 tree variant;
2702 tree binfo = make_tree_binfo (basetype ? 1 : 0);
2703 TYPE_BINFO (ref) = binfo;
2704 BINFO_OFFSET (binfo) = size_zero_node;
2705 BINFO_TYPE (binfo) = ref;
2707 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
2708 for (variant = ref; variant; variant = TYPE_NEXT_VARIANT (variant))
2709 TYPE_BINFO (variant) = binfo;
2711 if (basetype)
2713 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
2715 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
2716 vec_alloc (BINFO_BASE_ACCESSES (binfo), 1);
2717 BINFO_BASE_APPEND (binfo, base_binfo);
2718 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
2722 /* Called from finish_decl. */
2724 void
2725 objc_check_decl (tree decl)
2727 tree type = TREE_TYPE (decl);
2729 if (TREE_CODE (type) != RECORD_TYPE)
2730 return;
2731 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
2732 error ("statically allocated instance of Objective-C class %qE",
2733 type);
2736 void
2737 objc_check_global_decl (tree decl)
2739 tree id = DECL_NAME (decl);
2740 if (objc_is_class_name (id) && global_bindings_p())
2741 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id));
2744 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where
2745 INTERFACE may either name an Objective-C class, or refer to the
2746 special 'id' or 'Class' types. If INTERFACE is not a valid ObjC
2747 type, just return it unchanged. This function is often called when
2748 PROTOCOLS is NULL_TREE, in which case we simply look up the
2749 appropriate INTERFACE. */
2751 tree
2752 objc_get_protocol_qualified_type (tree interface, tree protocols)
2754 /* If INTERFACE is not provided, default to 'id'. */
2755 tree type = (interface ? objc_is_id (interface) : objc_object_type);
2756 bool is_ptr = (type != NULL_TREE);
2758 if (!is_ptr)
2760 type = objc_is_class_name (interface);
2762 if (type)
2764 /* If looking at a typedef, retrieve the precise type it
2765 describes. */
2766 if (TREE_CODE (interface) == IDENTIFIER_NODE)
2767 interface = identifier_global_value (interface);
2769 type = ((interface && TREE_CODE (interface) == TYPE_DECL
2770 && DECL_ORIGINAL_TYPE (interface))
2771 ? DECL_ORIGINAL_TYPE (interface)
2772 : xref_tag (RECORD_TYPE, type));
2774 else
2776 /* This case happens when we are given an 'interface' which
2777 is not a valid class name. For example if a typedef was
2778 used, and 'interface' really is the identifier of the
2779 typedef, but when you resolve it you don't get an
2780 Objective-C class, but something else, such as 'int'.
2781 This is an error; protocols make no sense unless you use
2782 them with Objective-C objects. */
2783 error_at (input_location, "only Objective-C object types can be qualified with a protocol");
2785 /* Try to recover. Ignore the invalid class name, and treat
2786 the object as an 'id' to silence further warnings about
2787 the class. */
2788 type = objc_object_type;
2789 is_ptr = true;
2793 if (protocols)
2795 type = build_variant_type_copy (type);
2797 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
2798 to the pointee. */
2799 if (is_ptr)
2801 tree orig_pointee_type = TREE_TYPE (type);
2802 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
2804 /* Set up the canonical type information. */
2805 TYPE_CANONICAL (type)
2806 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
2808 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
2809 type = TREE_TYPE (type);
2812 /* Look up protocols and install in lang specific list. */
2813 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
2814 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols
2815 (protocols, /* definition_required */ false);
2817 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
2818 return the pointer to the new pointee variant. */
2819 if (is_ptr)
2820 type = TYPE_POINTER_TO (type);
2821 else
2822 TYPE_OBJC_INTERFACE (type)
2823 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
2826 return type;
2829 /* Check for circular dependencies in protocols. The arguments are
2830 PROTO, the protocol to check, and LIST, a list of protocol it
2831 conforms to. */
2833 static void
2834 check_protocol_recursively (tree proto, tree list)
2836 tree p;
2838 for (p = list; p; p = TREE_CHAIN (p))
2840 tree pp = TREE_VALUE (p);
2842 if (TREE_CODE (pp) == IDENTIFIER_NODE)
2843 pp = lookup_protocol (pp, /* warn if deprecated */ false,
2844 /* definition_required */ false);
2846 if (pp == proto)
2847 fatal_error (input_location, "protocol %qE has circular dependency",
2848 PROTOCOL_NAME (pp));
2849 if (pp)
2850 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
2854 /* Look up PROTOCOLS, and return a list of those that are found. If
2855 none are found, return NULL. Note that this function will emit a
2856 warning if a protocol is found and is deprecated. If
2857 'definition_required', then warn if the protocol is found but is
2858 not defined (ie, if we only saw a forward-declaration of the
2859 protocol (as in "@protocol NSObject;") not a real definition with
2860 the list of methods). */
2861 static tree
2862 lookup_and_install_protocols (tree protocols, bool definition_required)
2864 tree proto;
2865 tree return_value = NULL_TREE;
2867 if (protocols == error_mark_node)
2868 return NULL;
2870 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
2872 tree ident = TREE_VALUE (proto);
2873 tree p = lookup_protocol (ident, /* warn_if_deprecated */ true,
2874 definition_required);
2876 if (p)
2877 return_value = chainon (return_value,
2878 build_tree_list (NULL_TREE, p));
2879 else if (ident != error_mark_node)
2880 error ("cannot find protocol declaration for %qE",
2881 ident);
2884 return return_value;
2887 static void
2888 build_common_objc_exception_stuff (void)
2890 tree noreturn_list, nothrow_list, temp_type;
2892 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
2893 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
2895 /* void objc_exception_throw(id) __attribute__((noreturn)); */
2896 /* void objc_sync_enter(id); */
2897 /* void objc_sync_exit(id); */
2898 temp_type = build_function_type_list (void_type_node,
2899 objc_object_type,
2900 NULL_TREE);
2901 objc_exception_throw_decl
2902 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
2903 noreturn_list);
2904 /* Make sure that objc_exception_throw (id) claims that it may throw an
2905 exception. */
2906 TREE_NOTHROW (objc_exception_throw_decl) = 0;
2908 objc_sync_enter_decl
2909 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
2910 NULL, nothrow_list);
2912 objc_sync_exit_decl
2913 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
2914 NULL, nothrow_list);
2917 /* Purpose: "play" parser, creating/installing representations
2918 of the declarations that are required by Objective-C.
2920 Model:
2922 type_spec--------->sc_spec
2923 (tree_list) (tree_list)
2926 identifier_node identifier_node */
2928 static void
2929 synth_module_prologue (void)
2931 tree type;
2932 enum debug_info_type save_write_symbols = write_symbols;
2933 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
2935 /* Suppress outputting debug symbols, because
2936 dbxout_init hasn't been called yet. */
2937 write_symbols = NO_DEBUG;
2938 debug_hooks = &do_nothing_debug_hooks;
2940 #ifdef OBJCPLUS
2941 push_lang_context (lang_name_c); /* extern "C" */
2942 #endif
2944 /* The following are also defined in <objc/objc.h> and friends. */
2946 objc_object_id = get_identifier (TAG_OBJECT);
2947 objc_class_id = get_identifier (TAG_CLASS);
2949 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
2950 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
2952 objc_object_type = build_pointer_type (objc_object_reference);
2953 objc_class_type = build_pointer_type (objc_class_reference);
2955 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
2956 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
2958 /* Declare the 'id' and 'Class' typedefs. */
2959 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2960 TYPE_DECL,
2961 objc_object_name,
2962 objc_object_type));
2963 TREE_NO_WARNING (type) = 1;
2965 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2966 TYPE_DECL,
2967 objc_class_name,
2968 objc_class_type));
2969 TREE_NO_WARNING (type) = 1;
2971 /* Forward-declare '@interface Protocol'. */
2972 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
2973 objc_declare_class (type);
2974 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE, type));
2976 /* Declare receiver type used for dispatching messages to 'super'. */
2977 /* `struct objc_super *' */
2978 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
2979 get_identifier (TAG_SUPER)));
2981 /* Declare pointers to method and ivar lists. */
2982 objc_method_list_ptr = build_pointer_type
2983 (xref_tag (RECORD_TYPE,
2984 get_identifier (UTAG_METHOD_LIST)));
2985 objc_method_proto_list_ptr
2986 = build_pointer_type (xref_tag (RECORD_TYPE,
2987 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
2988 objc_ivar_list_ptr = build_pointer_type
2989 (xref_tag (RECORD_TYPE,
2990 get_identifier (UTAG_IVAR_LIST)));
2992 build_common_objc_exception_stuff ();
2994 /* Set-up runtime-specific templates, message and exception stuff. */
2995 (*runtime.initialize) ();
2997 /* Declare objc_getProperty, object_setProperty and other property
2998 accessor helpers. */
2999 build_common_objc_property_accessor_helpers ();
3001 /* Forward declare constant_string_id and constant_string_type. */
3002 if (!constant_string_class_name)
3003 constant_string_class_name = runtime.default_constant_string_class_name;
3004 constant_string_id = get_identifier (constant_string_class_name);
3005 objc_declare_class (constant_string_id);
3007 /* Pre-build the following entities - for speed/convenience. */
3008 self_id = get_identifier ("self");
3009 ucmd_id = get_identifier ("_cmd");
3011 /* Declare struct _objc_fast_enumeration_state { ... }; */
3012 build_fast_enumeration_state_template ();
3014 /* void objc_enumeration_mutation (id) */
3015 type = build_function_type_list (void_type_node,
3016 objc_object_type, NULL_TREE);
3017 objc_enumeration_mutation_decl
3018 = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN,
3019 NULL, NULL_TREE);
3020 TREE_NOTHROW (objc_enumeration_mutation_decl) = 0;
3022 #ifdef OBJCPLUS
3023 pop_lang_context ();
3024 #endif
3026 write_symbols = save_write_symbols;
3027 debug_hooks = save_hooks;
3030 /* --- const strings --- */
3032 /* Ensure that the ivar list for NSConstantString/NXConstantString
3033 (or whatever was specified via `-fconstant-string-class')
3034 contains fields at least as large as the following three, so that
3035 the runtime can stomp on them with confidence:
3037 struct STRING_OBJECT_CLASS_NAME
3039 Object isa;
3040 char *cString;
3041 unsigned int length;
3042 }; */
3044 static int
3045 check_string_class_template (void)
3047 tree field_decl = objc_get_class_ivars (constant_string_id);
3049 #define AT_LEAST_AS_LARGE_AS(F, T) \
3050 (F && TREE_CODE (F) == FIELD_DECL \
3051 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
3052 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
3054 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3055 return 0;
3057 field_decl = DECL_CHAIN (field_decl);
3058 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3059 return 0;
3061 field_decl = DECL_CHAIN (field_decl);
3062 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
3064 #undef AT_LEAST_AS_LARGE_AS
3067 /* Avoid calling `check_string_class_template ()' more than once. */
3068 static GTY(()) int string_layout_checked;
3070 /* Construct an internal string layout to be used as a template for
3071 creating NSConstantString/NXConstantString instances. */
3073 static tree
3074 objc_build_internal_const_str_type (void)
3076 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
3077 tree fields = build_decl (input_location,
3078 FIELD_DECL, NULL_TREE, ptr_type_node);
3079 tree field = build_decl (input_location,
3080 FIELD_DECL, NULL_TREE, ptr_type_node);
3082 DECL_CHAIN (field) = fields; fields = field;
3083 field = build_decl (input_location,
3084 FIELD_DECL, NULL_TREE, unsigned_type_node);
3085 DECL_CHAIN (field) = fields; fields = field;
3086 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
3087 reverse order! */
3088 finish_builtin_struct (type, "__builtin_ObjCString",
3089 fields, NULL_TREE);
3091 return type;
3094 /* Custom build_string which sets TREE_TYPE! */
3096 tree
3097 my_build_string (int len, const char *str)
3099 return fix_string_type (build_string (len, str));
3102 /* Build a string with contents STR and length LEN and convert it to a
3103 pointer. */
3105 tree
3106 my_build_string_pointer (int len, const char *str)
3108 tree string = my_build_string (len, str);
3109 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
3110 return build1 (ADDR_EXPR, ptrtype, string);
3113 hashval_t
3114 objc_string_hasher::hash (string_descriptor *ptr)
3116 const_tree const str = ptr->literal;
3117 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3118 int i, len = TREE_STRING_LENGTH (str);
3119 hashval_t h = len;
3121 for (i = 0; i < len; i++)
3122 h = ((h * 613) + p[i]);
3124 return h;
3127 bool
3128 objc_string_hasher::equal (string_descriptor *ptr1, string_descriptor *ptr2)
3130 const_tree const str1 = ptr1->literal;
3131 const_tree const str2 = ptr2->literal;
3132 int len1 = TREE_STRING_LENGTH (str1);
3134 return (len1 == TREE_STRING_LENGTH (str2)
3135 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3136 len1));
3139 /* Given a chain of STRING_CST's, build a static instance of
3140 NXConstantString which points at the concatenation of those
3141 strings. We place the string object in the __string_objects
3142 section of the __OBJC segment. The Objective-C runtime will
3143 initialize the isa pointers of the string objects to point at the
3144 NXConstantString class object. */
3146 tree
3147 objc_build_string_object (tree string)
3149 tree constant_string_class;
3150 int length;
3151 tree addr;
3152 struct string_descriptor *desc, key;
3154 /* We should be passed a STRING_CST. */
3155 gcc_checking_assert (TREE_CODE (string) == STRING_CST);
3156 length = TREE_STRING_LENGTH (string) - 1;
3158 /* The target may have different ideas on how to construct an ObjC string
3159 literal. On Darwin (Mac OS X), for example, we may wish to obtain a
3160 constant CFString reference instead.
3161 At present, this is only supported for the NeXT runtime. */
3162 if (flag_next_runtime
3163 && targetcm.objc_construct_string_object)
3165 tree constructor = (*targetcm.objc_construct_string_object) (string);
3166 if (constructor)
3167 return build1 (NOP_EXPR, objc_object_type, constructor);
3170 /* Check whether the string class being used actually exists and has the
3171 correct ivar layout. */
3172 if (!string_layout_checked)
3174 string_layout_checked = -1;
3175 constant_string_class = lookup_interface (constant_string_id);
3176 internal_const_str_type = objc_build_internal_const_str_type ();
3178 if (!constant_string_class
3179 || !(constant_string_type
3180 = CLASS_STATIC_TEMPLATE (constant_string_class)))
3181 error ("cannot find interface declaration for %qE",
3182 constant_string_id);
3183 /* The NSConstantString/NXConstantString ivar layout is now known. */
3184 else if (!check_string_class_template ())
3185 error ("interface %qE does not have valid constant string layout",
3186 constant_string_id);
3187 /* If the runtime can generate a literal reference to the string class,
3188 don't need to run a constructor. */
3189 else if (!(*runtime.setup_const_string_class_decl)())
3190 error ("cannot find reference tag for class %qE", constant_string_id);
3191 else
3193 string_layout_checked = 1; /* Success! */
3194 add_class_reference (constant_string_id);
3198 if (string_layout_checked == -1)
3199 return error_mark_node;
3201 /* Perhaps we already constructed a constant string just like this one? */
3202 key.literal = string;
3203 string_descriptor **loc = string_htab->find_slot (&key, INSERT);
3204 desc = *loc;
3206 if (!desc)
3208 *loc = desc = ggc_alloc<string_descriptor> ();
3209 desc->literal = string;
3210 desc->constructor =
3211 (*runtime.build_const_string_constructor) (input_location, string, length);
3214 addr = convert (build_pointer_type (constant_string_type),
3215 build_unary_op (input_location,
3216 ADDR_EXPR, desc->constructor, 1));
3218 return addr;
3221 /* Build a static constant CONSTRUCTOR
3222 with type TYPE and elements ELTS. */
3224 tree
3225 objc_build_constructor (tree type, vec<constructor_elt, va_gc> *elts)
3227 tree constructor = build_constructor (type, elts);
3229 TREE_CONSTANT (constructor) = 1;
3230 TREE_STATIC (constructor) = 1;
3231 TREE_READONLY (constructor) = 1;
3233 #ifdef OBJCPLUS
3234 /* Adjust for impedance mismatch. We should figure out how to build
3235 CONSTRUCTORs that consistently please both the C and C++ gods. */
3236 if (!(*elts)[0].index)
3237 TREE_TYPE (constructor) = init_list_type_node;
3238 #endif
3240 return constructor;
3243 /* Return the DECL of the string IDENT in the SECTION. */
3245 tree
3246 get_objc_string_decl (tree ident, enum string_section section)
3248 tree chain;
3250 switch (section)
3252 case class_names:
3253 chain = class_names_chain;
3254 break;
3255 case meth_var_names:
3256 chain = meth_var_names_chain;
3257 break;
3258 case meth_var_types:
3259 chain = meth_var_types_chain;
3260 break;
3261 case prop_names_attr:
3262 chain = prop_names_attr_chain;
3263 break;
3264 default:
3265 gcc_unreachable ();
3268 for (; chain != 0; chain = TREE_CHAIN (chain))
3269 if (TREE_VALUE (chain) == ident)
3270 return (TREE_PURPOSE (chain));
3272 /* We didn't find the entry. */
3273 return NULL_TREE;
3276 /* Create a class reference, but don't create a variable to reference
3277 it. */
3279 void
3280 add_class_reference (tree ident)
3282 tree chain;
3284 if ((chain = cls_ref_chain))
3286 tree tail;
3289 if (ident == TREE_VALUE (chain))
3290 return;
3292 tail = chain;
3293 chain = TREE_CHAIN (chain);
3295 while (chain);
3297 /* Append to the end of the list */
3298 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
3300 else
3301 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
3304 /* Get a class reference, creating it if necessary. Also create the
3305 reference variable. */
3306 tree
3307 objc_get_class_reference (tree ident)
3309 tree orig_ident = (DECL_P (ident)
3310 ? DECL_NAME (ident)
3311 : TYPE_P (ident)
3312 ? OBJC_TYPE_NAME (ident)
3313 : ident);
3314 bool local_scope = false;
3316 #ifdef OBJCPLUS
3317 if (processing_template_decl)
3318 /* Must wait until template instantiation time. */
3319 return build_min_nt_loc (UNKNOWN_LOCATION, CLASS_REFERENCE_EXPR, ident);
3320 #endif
3322 if (TREE_CODE (ident) == TYPE_DECL)
3323 ident = (DECL_ORIGINAL_TYPE (ident)
3324 ? DECL_ORIGINAL_TYPE (ident)
3325 : TREE_TYPE (ident));
3327 #ifdef OBJCPLUS
3328 if (TYPE_P (ident)
3329 && CP_TYPE_CONTEXT (ident) != global_namespace)
3330 local_scope = true;
3331 #endif
3333 if (local_scope || !(ident = objc_is_class_name (ident)))
3335 error ("%qE is not an Objective-C class name or alias",
3336 orig_ident);
3337 return error_mark_node;
3340 return (*runtime.get_class_reference) (ident);
3343 void
3344 objc_declare_alias (tree alias_ident, tree class_ident)
3346 tree underlying_class;
3348 #ifdef OBJCPLUS
3349 if (current_namespace != global_namespace) {
3350 error ("Objective-C declarations may only appear in global scope");
3352 #endif /* OBJCPLUS */
3354 if (!(underlying_class = objc_is_class_name (class_ident)))
3355 warning (0, "cannot find class %qE", class_ident);
3356 else if (objc_is_class_name (alias_ident))
3357 warning (0, "class %qE already exists", alias_ident);
3358 else
3360 /* Implement @compatibility_alias as a typedef. */
3361 #ifdef OBJCPLUS
3362 push_lang_context (lang_name_c); /* extern "C" */
3363 #endif
3364 lang_hooks.decls.pushdecl (build_decl
3365 (input_location,
3366 TYPE_DECL,
3367 alias_ident,
3368 xref_tag (RECORD_TYPE, underlying_class)));
3369 #ifdef OBJCPLUS
3370 pop_lang_context ();
3371 #endif
3372 objc_map_put (alias_name_map, alias_ident, underlying_class);
3376 void
3377 objc_declare_class (tree identifier)
3379 #ifdef OBJCPLUS
3380 if (current_namespace != global_namespace) {
3381 error ("Objective-C declarations may only appear in global scope");
3383 #endif /* OBJCPLUS */
3385 if (! objc_is_class_name (identifier))
3387 tree record = lookup_name (identifier), type = record;
3389 if (record)
3391 if (TREE_CODE (record) == TYPE_DECL)
3392 type = DECL_ORIGINAL_TYPE (record)
3393 ? DECL_ORIGINAL_TYPE (record)
3394 : TREE_TYPE (record);
3396 if (!TYPE_HAS_OBJC_INFO (type)
3397 || !TYPE_OBJC_INTERFACE (type))
3399 error ("%qE redeclared as different kind of symbol",
3400 identifier);
3401 error ("previous declaration of %q+D",
3402 record);
3406 record = xref_tag (RECORD_TYPE, identifier);
3407 INIT_TYPE_OBJC_INFO (record);
3408 /* In the case of a @class declaration, we store the ident in
3409 the TYPE_OBJC_INTERFACE. If later an @interface is found,
3410 we'll replace the ident with the interface. */
3411 TYPE_OBJC_INTERFACE (record) = identifier;
3412 objc_map_put (class_name_map, identifier, NULL_TREE);
3416 tree
3417 objc_is_class_name (tree ident)
3419 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE)
3421 tree t = identifier_global_value (ident);
3422 if (t)
3423 ident = t;
3426 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
3427 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
3429 if (ident && TREE_CODE (ident) == RECORD_TYPE)
3430 ident = OBJC_TYPE_NAME (ident);
3431 #ifdef OBJCPLUS
3432 if (ident && TREE_CODE (ident) == TYPE_DECL)
3434 tree type = TREE_TYPE (ident);
3435 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
3436 return NULL_TREE;
3437 ident = DECL_NAME (ident);
3439 #endif
3440 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
3441 return NULL_TREE;
3443 if (lookup_interface (ident))
3444 return ident;
3447 tree target;
3449 target = objc_map_get (class_name_map, ident);
3450 if (target != OBJC_MAP_NOT_FOUND)
3451 return ident;
3453 target = objc_map_get (alias_name_map, ident);
3454 if (target != OBJC_MAP_NOT_FOUND)
3455 return target;
3458 return 0;
3461 /* Check whether TYPE is either 'id' or 'Class'. */
3463 tree
3464 objc_is_id (tree type)
3466 if (type && TREE_CODE (type) == IDENTIFIER_NODE)
3468 tree t = identifier_global_value (type);
3469 if (t)
3470 type = t;
3473 if (type && TREE_CODE (type) == TYPE_DECL)
3474 type = TREE_TYPE (type);
3476 /* NB: This function may be called before the ObjC front-end has
3477 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3478 return (objc_object_type && type
3479 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
3480 ? type
3481 : NULL_TREE);
3484 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3485 class instance. This is needed by other parts of the compiler to
3486 handle ObjC types gracefully. */
3488 tree
3489 objc_is_object_ptr (tree type)
3491 tree ret;
3493 type = TYPE_MAIN_VARIANT (type);
3494 if (!POINTER_TYPE_P (type))
3495 return 0;
3497 ret = objc_is_id (type);
3498 if (!ret)
3499 ret = objc_is_class_name (TREE_TYPE (type));
3501 return ret;
3504 static int
3505 objc_is_gcable_type (tree type, int or_strong_p)
3507 tree name;
3509 if (!TYPE_P (type))
3510 return 0;
3511 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
3512 return 1;
3513 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
3514 return 1;
3515 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
3516 return 0;
3517 type = TREE_TYPE (type);
3518 if (TREE_CODE (type) != RECORD_TYPE)
3519 return 0;
3520 name = TYPE_NAME (type);
3521 return (objc_is_class_name (name) != NULL_TREE);
3524 static tree
3525 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
3527 if (expr == oldexpr)
3528 return newexpr;
3530 switch (TREE_CODE (expr))
3532 case COMPONENT_REF:
3533 return objc_build_component_ref
3534 (objc_substitute_decl (TREE_OPERAND (expr, 0),
3535 oldexpr,
3536 newexpr),
3537 DECL_NAME (TREE_OPERAND (expr, 1)));
3538 case ARRAY_REF:
3539 return build_array_ref (input_location,
3540 objc_substitute_decl (TREE_OPERAND (expr, 0),
3541 oldexpr,
3542 newexpr),
3543 TREE_OPERAND (expr, 1));
3544 case INDIRECT_REF:
3545 return build_indirect_ref (input_location,
3546 objc_substitute_decl (TREE_OPERAND (expr, 0),
3547 oldexpr,
3548 newexpr), RO_ARROW);
3549 default:
3550 return expr;
3554 static tree
3555 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
3557 tree func_params;
3558 /* The LHS parameter contains the expression 'outervar->memberspec';
3559 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3560 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3562 tree offs
3563 = objc_substitute_decl
3564 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
3565 tree func
3566 = (flag_objc_direct_dispatch
3567 ? objc_assign_ivar_fast_decl
3568 : objc_assign_ivar_decl);
3570 offs = convert (integer_type_node, build_unary_op (input_location,
3571 ADDR_EXPR, offs, 0));
3572 offs = fold (offs);
3573 func_params = tree_cons (NULL_TREE,
3574 convert (objc_object_type, rhs),
3575 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
3576 tree_cons (NULL_TREE, offs,
3577 NULL_TREE)));
3579 return build_function_call (input_location, func, func_params);
3582 static tree
3583 objc_build_global_assignment (tree lhs, tree rhs)
3585 tree func_params = tree_cons (NULL_TREE,
3586 convert (objc_object_type, rhs),
3587 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3588 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3589 NULL_TREE));
3591 return build_function_call (input_location,
3592 objc_assign_global_decl, func_params);
3595 static tree
3596 objc_build_strong_cast_assignment (tree lhs, tree rhs)
3598 tree func_params = tree_cons (NULL_TREE,
3599 convert (objc_object_type, rhs),
3600 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3601 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3602 NULL_TREE));
3604 return build_function_call (input_location,
3605 objc_assign_strong_cast_decl, func_params);
3608 static int
3609 objc_is_gcable_p (tree expr)
3611 return (TREE_CODE (expr) == COMPONENT_REF
3612 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
3613 : TREE_CODE (expr) == ARRAY_REF
3614 ? (objc_is_gcable_p (TREE_TYPE (expr))
3615 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
3616 : TREE_CODE (expr) == ARRAY_TYPE
3617 ? objc_is_gcable_p (TREE_TYPE (expr))
3618 : TYPE_P (expr)
3619 ? objc_is_gcable_type (expr, 1)
3620 : (objc_is_gcable_p (TREE_TYPE (expr))
3621 || (DECL_P (expr)
3622 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
3625 static int
3626 objc_is_ivar_reference_p (tree expr)
3628 return (TREE_CODE (expr) == ARRAY_REF
3629 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
3630 : TREE_CODE (expr) == COMPONENT_REF
3631 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
3632 : 0);
3635 static int
3636 objc_is_global_reference_p (tree expr)
3638 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
3639 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
3640 : DECL_P (expr)
3641 ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr))
3642 : 0);
3645 tree
3646 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
3648 tree result = NULL_TREE, outer;
3649 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
3651 /* This function is currently only used with the next runtime with
3652 garbage collection enabled (-fobjc-gc). */
3653 gcc_assert (flag_next_runtime);
3655 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3656 will have been transformed to the form '*(type *)&expr'. */
3657 if (TREE_CODE (lhs) == INDIRECT_REF)
3659 outer = TREE_OPERAND (lhs, 0);
3661 while (!strong_cast_p
3662 && (CONVERT_EXPR_P (outer)
3663 || TREE_CODE (outer) == NON_LVALUE_EXPR))
3665 tree lhstype = TREE_TYPE (outer);
3667 /* Descend down the cast chain, and record the first objc_gc
3668 attribute found. */
3669 if (POINTER_TYPE_P (lhstype))
3671 tree attr
3672 = lookup_attribute ("objc_gc",
3673 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
3675 if (attr)
3676 strong_cast_p = 1;
3679 outer = TREE_OPERAND (outer, 0);
3683 /* If we have a __strong cast, it trumps all else. */
3684 if (strong_cast_p)
3686 if (modifycode != NOP_EXPR)
3687 goto invalid_pointer_arithmetic;
3689 if (warn_assign_intercept)
3690 warning (0, "strong-cast assignment has been intercepted");
3692 result = objc_build_strong_cast_assignment (lhs, rhs);
3694 goto exit_point;
3697 /* the lhs must be of a suitable type, regardless of its underlying
3698 structure. */
3699 if (!objc_is_gcable_p (lhs))
3700 goto exit_point;
3702 outer = lhs;
3704 while (outer
3705 && (TREE_CODE (outer) == COMPONENT_REF
3706 || TREE_CODE (outer) == ARRAY_REF))
3707 outer = TREE_OPERAND (outer, 0);
3709 if (TREE_CODE (outer) == INDIRECT_REF)
3711 outer = TREE_OPERAND (outer, 0);
3712 indirect_p = 1;
3715 outer_gc_p = objc_is_gcable_p (outer);
3717 /* Handle ivar assignments. */
3718 if (objc_is_ivar_reference_p (lhs))
3720 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3721 doesn't cut it here), the best we can do here is suggest a cast. */
3722 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
3724 /* We may still be able to use the global write barrier... */
3725 if (!indirect_p && objc_is_global_reference_p (outer))
3726 goto global_reference;
3728 suggest_cast:
3729 if (modifycode == NOP_EXPR)
3731 if (warn_assign_intercept)
3732 warning (0, "strong-cast may possibly be needed");
3735 goto exit_point;
3738 if (modifycode != NOP_EXPR)
3739 goto invalid_pointer_arithmetic;
3741 if (warn_assign_intercept)
3742 warning (0, "instance variable assignment has been intercepted");
3744 result = objc_build_ivar_assignment (outer, lhs, rhs);
3746 goto exit_point;
3749 /* Likewise, intercept assignment to global/static variables if their type is
3750 GC-marked. */
3751 if (objc_is_global_reference_p (outer))
3753 if (indirect_p)
3754 goto suggest_cast;
3756 global_reference:
3757 if (modifycode != NOP_EXPR)
3759 invalid_pointer_arithmetic:
3760 if (outer_gc_p)
3761 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3763 goto exit_point;
3766 if (warn_assign_intercept)
3767 warning (0, "global/static variable assignment has been intercepted");
3769 result = objc_build_global_assignment (lhs, rhs);
3772 /* In all other cases, fall back to the normal mechanism. */
3773 exit_point:
3774 return result;
3777 /* Implementation of the table mapping a class name (as an identifier)
3778 to a class node. The two public functions for it are
3779 lookup_interface() and add_interface(). add_interface() is only
3780 used in this file, so we can make it static. */
3782 static GTY(()) objc_map_t interface_map;
3784 static void
3785 interface_hash_init (void)
3787 interface_map = objc_map_alloc_ggc (200);
3790 static tree
3791 add_interface (tree class_name, tree name)
3793 /* Put interfaces on list in reverse order. */
3794 TREE_CHAIN (class_name) = interface_chain;
3795 interface_chain = class_name;
3797 /* Add it to the map. */
3798 objc_map_put (interface_map, name, class_name);
3800 return interface_chain;
3803 tree
3804 lookup_interface (tree ident)
3806 #ifdef OBJCPLUS
3807 if (ident && TREE_CODE (ident) == TYPE_DECL)
3808 ident = DECL_NAME (ident);
3809 #endif
3811 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
3812 return NULL_TREE;
3815 tree interface = objc_map_get (interface_map, ident);
3817 if (interface == OBJC_MAP_NOT_FOUND)
3818 return NULL_TREE;
3819 else
3820 return interface;
3826 /* Implement @defs (<classname>) within struct bodies. */
3828 tree
3829 objc_get_class_ivars (tree class_name)
3831 tree interface = lookup_interface (class_name);
3833 if (interface)
3834 return get_class_ivars (interface, true);
3836 error ("cannot find interface declaration for %qE",
3837 class_name);
3839 return error_mark_node;
3843 /* Functions used by the hashtable for field duplicates in
3844 objc_detect_field_duplicates(). Ideally, we'd use a standard
3845 key-value dictionary hashtable , and store as keys the field names,
3846 and as values the actual declarations (used to print nice error
3847 messages with the locations). But, the hashtable we are using only
3848 allows us to store keys in the hashtable, without values (it looks
3849 more like a set). So, we store the DECLs, but define equality as
3850 DECLs having the same name, and hash as the hash of the name. */
3852 struct decl_name_hash : nofree_ptr_hash <tree_node>
3854 static inline hashval_t hash (const tree_node *);
3855 static inline bool equal (const tree_node *, const tree_node *);
3858 inline hashval_t
3859 decl_name_hash::hash (const tree_node *q)
3861 return (hashval_t) ((intptr_t)(DECL_NAME (q)) >> 3);
3864 inline bool
3865 decl_name_hash::equal (const tree_node *a, const tree_node *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 cannot 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 #ifdef OBJCPLUS
4612 || !tree_int_cst_equal (DECL_BIT_FIELD_REPRESENTATIVE (intdecls),
4613 DECL_BIT_FIELD_REPRESENTATIVE (impdecls))
4614 #else
4615 || !tree_int_cst_equal (DECL_INITIAL (intdecls),
4616 DECL_INITIAL (impdecls))
4617 #endif
4620 if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
4622 error_with_ivar ("conflicting instance variable type",
4623 impdecls);
4624 error_with_ivar ("previous declaration of",
4625 intdecls);
4627 else /* both the type and the name don't match */
4629 error ("inconsistent instance variable specification");
4630 break;
4634 else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
4636 error_with_ivar ("conflicting instance variable name",
4637 impdecls);
4638 error_with_ivar ("previous declaration of",
4639 intdecls);
4642 intdecls = DECL_CHAIN (intdecls);
4643 impdecls = DECL_CHAIN (impdecls);
4648 static void
4649 mark_referenced_methods (void)
4651 struct imp_entry *impent;
4652 tree chain;
4654 for (impent = imp_list; impent; impent = impent->next)
4656 chain = CLASS_CLS_METHODS (impent->imp_context);
4657 while (chain)
4659 cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output ();
4660 chain = DECL_CHAIN (chain);
4663 chain = CLASS_NST_METHODS (impent->imp_context);
4664 while (chain)
4666 cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output ();
4667 chain = DECL_CHAIN (chain);
4672 /* If type is empty or only type qualifiers are present, add default
4673 type of id (otherwise grokdeclarator will default to int). */
4674 static inline tree
4675 adjust_type_for_id_default (tree type)
4677 if (!type)
4678 type = make_node (TREE_LIST);
4680 if (!TREE_VALUE (type))
4681 TREE_VALUE (type) = objc_object_type;
4682 else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE
4683 && TYPED_OBJECT (TREE_VALUE (type)))
4684 error ("cannot use an object as parameter to a method");
4686 return type;
4689 /* Return a KEYWORD_DECL built using the specified key_name, arg_type,
4690 arg_name and attributes. (TODO: Rename KEYWORD_DECL to
4691 OBJC_METHOD_PARM_DECL ?)
4693 A KEYWORD_DECL is a tree representing the declaration of a
4694 parameter of an Objective-C method. It is produced when parsing a
4695 fragment of Objective-C method declaration of the form
4697 keyworddecl:
4698 selector ':' '(' typename ')' identifier
4700 For example, take the Objective-C method
4702 -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type;
4704 the two fragments "pathForResource:(NSString *)resource" and
4705 "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The
4706 KEYWORD_DECL stores the 'key_name' (eg, identifier for
4707 "pathForResource"), the 'arg_type' (eg, tree representing a
4708 NSString *), the 'arg_name' (eg identifier for "resource") and
4709 potentially some attributes (for example, a tree representing
4710 __attribute__ ((unused)) if such an attribute was attached to a
4711 certain parameter). You can access this information using the
4712 TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name),
4713 KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes).
4715 'key_name' is an identifier node (and is optional as you can omit
4716 it in Objective-C methods).
4717 'arg_type' is a tree list (and is optional too if no parameter type
4718 was specified).
4719 'arg_name' is an identifier node and is required.
4720 'attributes' is an optional tree containing parameter attributes. */
4721 tree
4722 objc_build_keyword_decl (tree key_name, tree arg_type,
4723 tree arg_name, tree attributes)
4725 tree keyword_decl;
4727 if (flag_objc1_only && attributes)
4728 error_at (input_location, "method argument attributes are not available in Objective-C 1.0");
4730 /* If no type is specified, default to "id". */
4731 arg_type = adjust_type_for_id_default (arg_type);
4733 keyword_decl = make_node (KEYWORD_DECL);
4735 TREE_TYPE (keyword_decl) = arg_type;
4736 KEYWORD_ARG_NAME (keyword_decl) = arg_name;
4737 KEYWORD_KEY_NAME (keyword_decl) = key_name;
4738 DECL_ATTRIBUTES (keyword_decl) = attributes;
4740 return keyword_decl;
4743 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
4744 static tree
4745 build_keyword_selector (tree selector)
4747 int len = 0;
4748 tree key_chain, key_name;
4749 char *buf;
4751 /* Scan the selector to see how much space we'll need. */
4752 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
4754 switch (TREE_CODE (selector))
4756 case KEYWORD_DECL:
4757 key_name = KEYWORD_KEY_NAME (key_chain);
4758 break;
4759 case TREE_LIST:
4760 key_name = TREE_PURPOSE (key_chain);
4761 break;
4762 default:
4763 gcc_unreachable ();
4766 if (key_name)
4767 len += IDENTIFIER_LENGTH (key_name) + 1;
4768 else
4769 /* Just a ':' arg. */
4770 len++;
4773 buf = (char *) alloca (len + 1);
4774 /* Start the buffer out as an empty string. */
4775 buf[0] = '\0';
4777 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
4779 switch (TREE_CODE (selector))
4781 case KEYWORD_DECL:
4782 key_name = KEYWORD_KEY_NAME (key_chain);
4783 break;
4784 case TREE_LIST:
4785 key_name = TREE_PURPOSE (key_chain);
4786 /* The keyword decl chain will later be used as a function
4787 argument chain. Unhook the selector itself so as to not
4788 confuse other parts of the compiler. */
4789 TREE_PURPOSE (key_chain) = NULL_TREE;
4790 break;
4791 default:
4792 gcc_unreachable ();
4795 if (key_name)
4796 strcat (buf, IDENTIFIER_POINTER (key_name));
4797 strcat (buf, ":");
4800 return get_identifier_with_length (buf, len);
4803 /* Used for declarations and definitions. */
4805 static tree
4806 build_method_decl (enum tree_code code, tree ret_type, tree selector,
4807 tree add_args, bool ellipsis)
4809 tree method_decl;
4811 /* If no type is specified, default to "id". */
4812 ret_type = adjust_type_for_id_default (ret_type);
4814 /* Note how a method_decl has a TREE_TYPE which is not the function
4815 type of the function implementing the method, but only the return
4816 type of the method. We may want to change this, and store the
4817 entire function type in there (eg, it may be used to simplify
4818 dealing with attributes below). */
4819 method_decl = make_node (code);
4820 TREE_TYPE (method_decl) = ret_type;
4822 /* If we have a keyword selector, create an identifier_node that
4823 represents the full selector name (`:' included)... */
4824 if (TREE_CODE (selector) == KEYWORD_DECL)
4826 METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
4827 METHOD_SEL_ARGS (method_decl) = selector;
4828 METHOD_ADD_ARGS (method_decl) = add_args;
4829 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis;
4831 else
4833 METHOD_SEL_NAME (method_decl) = selector;
4834 METHOD_SEL_ARGS (method_decl) = NULL_TREE;
4835 METHOD_ADD_ARGS (method_decl) = NULL_TREE;
4838 return method_decl;
4841 /* This routine processes objective-c method attributes. */
4843 static void
4844 objc_decl_method_attributes (tree *node, tree attributes, int flags)
4846 /* TODO: Replace the hackery below. An idea would be to store the
4847 full function type in the method declaration (for example in
4848 TREE_TYPE) and then expose ObjC method declarations to c-family
4849 and they could deal with them by simply treating them as
4850 functions. */
4852 /* Because of the dangers in the hackery below, we filter out any
4853 attribute that we do not know about. For the ones we know about,
4854 we know that they work with the hackery. For the other ones,
4855 there is no guarantee, so we have to filter them out. */
4856 tree filtered_attributes = NULL_TREE;
4858 if (attributes)
4860 tree attribute;
4861 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
4863 tree name = TREE_PURPOSE (attribute);
4865 if (is_attribute_p ("deprecated", name)
4866 || is_attribute_p ("sentinel", name)
4867 || is_attribute_p ("noreturn", name))
4869 /* An attribute that we support; add it to the filtered
4870 attributes. */
4871 filtered_attributes = chainon (filtered_attributes,
4872 copy_node (attribute));
4874 else if (is_attribute_p ("format", name))
4876 /* "format" is special because before adding it to the
4877 filtered attributes we need to adjust the specified
4878 format by adding the hidden function parameters for
4879 an Objective-C method (self, _cmd). */
4880 tree new_attribute = copy_node (attribute);
4882 /* Check the arguments specified with the attribute, and
4883 modify them adding 2 for the two hidden arguments.
4884 Note how this differs from C++; according to the
4885 specs, C++ does not do it so you have to add the +1
4886 yourself. For Objective-C, instead, the compiler
4887 adds the +2 for you. */
4889 /* The attribute arguments have not been checked yet, so
4890 we need to be careful as they could be missing or
4891 invalid. If anything looks wrong, we skip the
4892 process and the compiler will complain about it later
4893 when it validates the attribute. */
4894 /* Check that we have at least three arguments. */
4895 if (TREE_VALUE (new_attribute)
4896 && TREE_CHAIN (TREE_VALUE (new_attribute))
4897 && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (new_attribute))))
4899 tree second_argument = TREE_CHAIN (TREE_VALUE (new_attribute));
4900 tree third_argument = TREE_CHAIN (second_argument);
4901 tree number;
4903 /* This is the second argument, the "string-index",
4904 which specifies the index of the format string
4905 argument. Add 2. */
4906 number = TREE_VALUE (second_argument);
4907 if (number
4908 && TREE_CODE (number) == INTEGER_CST
4909 && wi::to_wide (number) != 0)
4910 TREE_VALUE (second_argument)
4911 = wide_int_to_tree (TREE_TYPE (number),
4912 wi::to_wide (number) + 2);
4914 /* This is the third argument, the "first-to-check",
4915 which specifies the index of the first argument to
4916 check. This could be 0, meaning it is not available,
4917 in which case we don't need to add 2. Add 2 if not
4918 0. */
4919 number = TREE_VALUE (third_argument);
4920 if (number
4921 && TREE_CODE (number) == INTEGER_CST
4922 && wi::to_wide (number) != 0)
4923 TREE_VALUE (third_argument)
4924 = wide_int_to_tree (TREE_TYPE (number),
4925 wi::to_wide (number) + 2);
4927 filtered_attributes = chainon (filtered_attributes,
4928 new_attribute);
4930 else if (is_attribute_p ("nonnull", name))
4932 /* We need to fixup all the argument indexes by adding 2
4933 for the two hidden arguments of an Objective-C method
4934 invocation, similat to what we do above for the
4935 "format" attribute. */
4936 /* FIXME: This works great in terms of implementing the
4937 functionality, but the warnings that are produced by
4938 nonnull do mention the argument index (while the
4939 format ones don't). For example, you could get
4940 "warning: null argument where non-null required
4941 (argument 3)". Now in that message, "argument 3"
4942 includes the 2 hidden arguments; it would be much
4943 more friendly to call it "argument 1", as that would
4944 be consistent with __attribute__ ((nonnnull (1))).
4945 To do this, we'd need to have the C family code that
4946 checks the arguments know about adding/removing 2 to
4947 the argument index ... or alternatively we could
4948 maybe store the "printable" argument index in
4949 addition to the actual argument index ? Some
4950 refactoring is needed to do this elegantly. */
4951 tree new_attribute = copy_node (attribute);
4952 tree argument = TREE_VALUE (attribute);
4953 while (argument != NULL_TREE)
4955 /* Get the value of the argument and add 2. */
4956 tree number = TREE_VALUE (argument);
4957 if (number && TREE_CODE (number) == INTEGER_CST
4958 && wi::to_wide (number) != 0)
4959 TREE_VALUE (argument)
4960 = wide_int_to_tree (TREE_TYPE (number),
4961 wi::to_wide (number) + 2);
4962 argument = TREE_CHAIN (argument);
4965 filtered_attributes = chainon (filtered_attributes,
4966 new_attribute);
4968 else
4969 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
4973 if (filtered_attributes)
4975 /* This hackery changes the TREE_TYPE of the ObjC method
4976 declaration to be a function type, so that decl_attributes
4977 will treat the ObjC method as if it was a function. Some
4978 attributes (sentinel, format) will be applied to the function
4979 type, changing it in place; so after calling decl_attributes,
4980 we extract the function type attributes and store them in
4981 METHOD_TYPE_ATTRIBUTES. Some other attributes (noreturn,
4982 deprecated) are applied directly to the method declaration
4983 (by setting TREE_DEPRECATED and TREE_THIS_VOLATILE) so there
4984 is nothing to do. */
4985 tree saved_type = TREE_TYPE (*node);
4986 TREE_TYPE (*node)
4987 = build_function_type_for_method (TREE_VALUE (saved_type), *node,
4988 METHOD_REF, 0);
4989 decl_attributes (node, filtered_attributes, flags);
4990 METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node));
4991 TREE_TYPE (*node) = saved_type;
4995 bool
4996 objc_method_decl (enum tree_code opcode)
4998 return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL;
5001 /* Return a function type for METHOD with RETURN_TYPE. CONTEXT is
5002 either METHOD_DEF or METHOD_REF, indicating whether we are defining a
5003 method or calling one. SUPER_FLAG indicates whether this is a send
5004 to super; this makes a difference for the NeXT calling sequence in
5005 which the lookup and the method call are done together. If METHOD is
5006 NULL, user-defined arguments (i.e., beyond self and _cmd) shall be
5007 represented as varargs. */
5009 tree
5010 build_function_type_for_method (tree return_type, tree method,
5011 int context, bool super_flag)
5013 vec<tree, va_gc> *argtypes = make_tree_vector ();
5014 tree t, ftype;
5015 bool is_varargs = false;
5017 (*runtime.get_arg_type_list_base) (&argtypes, method, context, super_flag);
5019 /* No actual method prototype given; remaining args passed as varargs. */
5020 if (method == NULL_TREE)
5022 is_varargs = true;
5023 goto build_ftype;
5026 for (t = METHOD_SEL_ARGS (method); t; t = DECL_CHAIN (t))
5028 tree arg_type = TREE_VALUE (TREE_TYPE (t));
5030 /* Decay argument types for the underlying C function as
5031 appropriate. */
5032 arg_type = objc_decay_parm_type (arg_type);
5034 vec_safe_push (argtypes, arg_type);
5037 if (METHOD_ADD_ARGS (method))
5039 for (t = TREE_CHAIN (METHOD_ADD_ARGS (method));
5040 t; t = TREE_CHAIN (t))
5042 tree arg_type = TREE_TYPE (TREE_VALUE (t));
5044 arg_type = objc_decay_parm_type (arg_type);
5046 vec_safe_push (argtypes, arg_type);
5049 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
5050 is_varargs = true;
5053 build_ftype:
5054 if (is_varargs)
5055 ftype = build_varargs_function_type_vec (return_type, argtypes);
5056 else
5057 ftype = build_function_type_vec (return_type, argtypes);
5059 release_tree_vector (argtypes);
5060 return ftype;
5063 /* The 'method' argument is a tree; this tree could either be a single
5064 method, which is returned, or could be a TREE_VEC containing a list
5065 of methods. In that case, the first one is returned, and warnings
5066 are issued as appropriate. */
5067 static tree
5068 check_duplicates (tree method, int methods, int is_class)
5070 tree first_method;
5071 size_t i;
5073 if (method == NULL_TREE)
5074 return NULL_TREE;
5076 if (TREE_CODE (method) != TREE_VEC)
5077 return method;
5079 /* We have two or more methods with the same name but different
5080 types. */
5081 first_method = TREE_VEC_ELT (method, 0);
5083 /* But just how different are those types? If
5084 -Wno-strict-selector-match is specified, we shall not complain if
5085 the differences are solely among types with identical size and
5086 alignment. */
5087 if (!warn_strict_selector_match)
5089 for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++)
5090 if (!comp_proto_with_proto (first_method, TREE_VEC_ELT (method, i), 0))
5091 goto issue_warning;
5093 return first_method;
5096 issue_warning:
5097 if (methods)
5099 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5101 warning_at (input_location, 0,
5102 "multiple methods named %<%c%E%> found",
5103 (is_class ? '+' : '-'),
5104 METHOD_SEL_NAME (first_method));
5105 inform (DECL_SOURCE_LOCATION (first_method), "using %<%c%s%>",
5106 (type ? '-' : '+'),
5107 identifier_to_locale (gen_method_decl (first_method)));
5109 else
5111 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5113 warning_at (input_location, 0,
5114 "multiple selectors named %<%c%E%> found",
5115 (is_class ? '+' : '-'),
5116 METHOD_SEL_NAME (first_method));
5117 inform (DECL_SOURCE_LOCATION (first_method), "found %<%c%s%>",
5118 (type ? '-' : '+'),
5119 identifier_to_locale (gen_method_decl (first_method)));
5122 for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++)
5124 bool type = TREE_CODE (TREE_VEC_ELT (method, i)) == INSTANCE_METHOD_DECL;
5126 inform (DECL_SOURCE_LOCATION (TREE_VEC_ELT (method, i)), "also found %<%c%s%>",
5127 (type ? '-' : '+'),
5128 identifier_to_locale (gen_method_decl (TREE_VEC_ELT (method, i))));
5131 return first_method;
5134 /* If RECEIVER is a class reference, return the identifier node for
5135 the referenced class. RECEIVER is created by objc_get_class_reference,
5136 so we check the exact form created depending on which runtimes are
5137 used. */
5139 static tree
5140 receiver_is_class_object (tree receiver, int self, int super)
5142 tree exp, arg;
5144 /* The receiver is 'self' or 'super' in the context of a class method. */
5145 if (objc_method_context
5146 && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
5147 && (self || super))
5148 return (super
5149 ? CLASS_SUPER_NAME (implementation_template)
5150 : CLASS_NAME (implementation_template));
5152 /* The runtime might encapsulate things its own way. */
5153 exp = (*runtime.receiver_is_class_object) (receiver);
5154 if (exp)
5155 return exp;
5157 /* The receiver is a function call that returns an id. Check if
5158 it is a call to objc_getClass, if so, pick up the class name.
5160 This is required by the GNU runtime, which compiles
5162 [NSObject alloc]
5164 into
5166 [objc_get_class ("NSObject") alloc];
5168 and then, to check that the receiver responds to the +alloc
5169 method, needs to be able to determine that the objc_get_class()
5170 call returns the NSObject class and not just a generic Class
5171 pointer.
5173 But, traditionally this is enabled for all runtimes, not just the
5174 GNU one, which means that the compiler is smarter than you'd
5175 expect when dealing with objc_getClass(). For example, with the
5176 Apple runtime, in the code
5178 [objc_getClass ("NSObject") alloc];
5180 the compiler will recognize the objc_getClass() call as special
5181 (due to the code below) and so will know that +alloc is called on
5182 the 'NSObject' class, and can perform the corresponding checks.
5184 Programmers can disable this behavior by casting the results of
5185 objc_getClass() to 'Class' (this may seem weird because
5186 objc_getClass() is already declared to return 'Class', but the
5187 compiler treats it as a special function). This may be useful if
5188 the class is never declared, and the compiler would complain
5189 about a missing @interface for it. Then, you can do
5191 [(Class)objc_getClass ("MyClassNeverDeclared") alloc];
5193 to silence the warnings. */
5194 if (TREE_CODE (receiver) == CALL_EXPR
5195 && (exp = CALL_EXPR_FN (receiver))
5196 && TREE_CODE (exp) == ADDR_EXPR
5197 && (exp = TREE_OPERAND (exp, 0))
5198 && TREE_CODE (exp) == FUNCTION_DECL
5199 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
5200 prototypes for objc_get_class(). Thankfully, they seem to share the
5201 same function type. */
5202 && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl)
5203 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), runtime.tag_getclass)
5204 /* We have a call to objc_get_class/objc_getClass! */
5205 && (arg = CALL_EXPR_ARG (receiver, 0)))
5207 STRIP_NOPS (arg);
5208 if (TREE_CODE (arg) == ADDR_EXPR
5209 && (arg = TREE_OPERAND (arg, 0))
5210 && TREE_CODE (arg) == STRING_CST)
5211 /* Finally, we have the class name. */
5212 return get_identifier (TREE_STRING_POINTER (arg));
5214 return 0;
5217 /* If we are currently building a message expr, this holds
5218 the identifier of the selector of the message. This is
5219 used when printing warnings about argument mismatches. */
5221 static tree current_objc_message_selector = 0;
5223 tree
5224 objc_message_selector (void)
5226 return current_objc_message_selector;
5229 /* Construct an expression for sending a message.
5230 MESS has the object to send to in TREE_PURPOSE
5231 and the argument list (including selector) in TREE_VALUE.
5233 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
5234 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
5236 tree
5237 objc_build_message_expr (tree receiver, tree message_args)
5239 tree sel_name;
5240 #ifdef OBJCPLUS
5241 tree args = TREE_PURPOSE (message_args);
5242 #else
5243 tree args = message_args;
5244 #endif
5245 tree method_params = NULL_TREE;
5247 if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK)
5248 return error_mark_node;
5250 /* Obtain the full selector name. */
5251 switch (TREE_CODE (args))
5253 case IDENTIFIER_NODE:
5254 /* A unary selector. */
5255 sel_name = args;
5256 break;
5257 case TREE_LIST:
5258 sel_name = build_keyword_selector (args);
5259 break;
5260 default:
5261 gcc_unreachable ();
5264 /* Build the parameter list to give to the method. */
5265 if (TREE_CODE (args) == TREE_LIST)
5266 #ifdef OBJCPLUS
5267 method_params = chainon (args, TREE_VALUE (message_args));
5268 #else
5270 tree chain = args, prev = NULL_TREE;
5272 /* We have a keyword selector--check for comma expressions. */
5273 while (chain)
5275 tree element = TREE_VALUE (chain);
5277 /* We have a comma expression, must collapse... */
5278 if (TREE_CODE (element) == TREE_LIST)
5280 if (prev)
5281 TREE_CHAIN (prev) = element;
5282 else
5283 args = element;
5285 prev = chain;
5286 chain = TREE_CHAIN (chain);
5288 method_params = args;
5290 #endif
5292 #ifdef OBJCPLUS
5293 if (processing_template_decl)
5294 /* Must wait until template instantiation time. */
5295 return build_min_nt_loc (UNKNOWN_LOCATION, MESSAGE_SEND_EXPR, receiver,
5296 sel_name, method_params);
5297 #endif
5299 return objc_finish_message_expr (receiver, sel_name, method_params, NULL);
5302 /* Look up method SEL_NAME that would be suitable for receiver
5303 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
5304 nonzero), and report on any duplicates. */
5306 static tree
5307 lookup_method_in_hash_lists (tree sel_name, int is_class)
5309 tree method_prototype = OBJC_MAP_NOT_FOUND;
5311 if (!is_class)
5312 method_prototype = objc_map_get (instance_method_map, sel_name);
5314 if (method_prototype == OBJC_MAP_NOT_FOUND)
5316 method_prototype = objc_map_get (class_method_map, sel_name);
5317 is_class = 1;
5319 if (method_prototype == OBJC_MAP_NOT_FOUND)
5320 return NULL_TREE;
5323 return check_duplicates (method_prototype, 1, is_class);
5326 /* The 'objc_finish_message_expr' routine is called from within
5327 'objc_build_message_expr' for non-template functions. In the case of
5328 C++ template functions, it is called from 'build_expr_from_tree'
5329 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded.
5331 If the DEPRECATED_METHOD_PROTOTYPE argument is NULL, then we warn
5332 if the method being used is deprecated. If it is not NULL, instead
5333 of deprecating, we set *DEPRECATED_METHOD_PROTOTYPE to the method
5334 prototype that was used and is deprecated. This is useful for
5335 getter calls that are always generated when compiling dot-syntax
5336 expressions, even if they may not be used. In that case, we don't
5337 want the warning immediately; we produce it (if needed) at gimplify
5338 stage when we are sure that the deprecated getter is being
5339 used. */
5340 tree
5341 objc_finish_message_expr (tree receiver, tree sel_name, tree method_params,
5342 tree *deprecated_method_prototype)
5344 tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype;
5345 tree retval, class_tree;
5346 int self, super, have_cast;
5348 STRIP_ANY_LOCATION_WRAPPER (receiver);
5350 /* We have used the receiver, so mark it as read. */
5351 mark_exp_read (receiver);
5353 /* Extract the receiver of the message, as well as its type
5354 (where the latter may take the form of a cast or be inferred
5355 from the implementation context). */
5356 rtype = receiver;
5357 while (TREE_CODE (rtype) == COMPOUND_EXPR
5358 || TREE_CODE (rtype) == MODIFY_EXPR
5359 || CONVERT_EXPR_P (rtype)
5360 || TREE_CODE (rtype) == COMPONENT_REF)
5361 rtype = TREE_OPERAND (rtype, 0);
5363 /* self is 1 if this is a message to self, 0 otherwise */
5364 self = (rtype == self_decl);
5366 /* super is 1 if this is a message to super, 0 otherwise. */
5367 super = (rtype == UOBJC_SUPER_decl);
5369 /* rtype is the type of the receiver. */
5370 rtype = TREE_TYPE (receiver);
5372 /* have_cast is 1 if the receiver is casted. */
5373 have_cast = (TREE_CODE (receiver) == NOP_EXPR
5374 || (TREE_CODE (receiver) == COMPOUND_EXPR
5375 && !IS_SUPER (rtype)));
5377 /* If we are calling [super dealloc], reset our warning flag. */
5378 if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name)))
5379 should_call_super_dealloc = 0;
5381 /* If the receiver is a class object, retrieve the corresponding
5382 @interface, if one exists. class_tree is the class name
5383 identifier, or NULL_TREE if this is not a class method or the
5384 class name could not be determined (as in the case "Class c; [c
5385 method];"). */
5386 class_tree = receiver_is_class_object (receiver, self, super);
5388 /* Now determine the receiver type (if an explicit cast has not been
5389 provided). */
5390 if (!have_cast)
5392 if (class_tree)
5394 /* We are here when we have no cast, and we have a class
5395 name. So, this is a plain method to a class object, as
5396 in [NSObject alloc]. Find the interface corresponding to
5397 the class name. */
5398 rtype = lookup_interface (class_tree);
5400 if (rtype == NULL_TREE)
5402 /* If 'rtype' is NULL_TREE at this point it means that
5403 we have seen no @interface corresponding to that
5404 class name, only a @class declaration (alternatively,
5405 this was a call such as [objc_getClass("SomeClass")
5406 alloc], where we've never seen the @interface of
5407 SomeClass). So, we have a class name (class_tree)
5408 but no actual details of the class methods. We won't
5409 be able to check that the class responds to the
5410 method, and we will have to guess the method
5411 prototype. Emit a warning, then keep going (this
5412 will use any method with a matching name, as if the
5413 receiver was of type 'Class'). */
5414 warning (0, "@interface of class %qE not found", class_tree);
5417 /* Handle `self' and `super'. */
5418 else if (super)
5420 if (!CLASS_SUPER_NAME (implementation_template))
5422 error ("no super class declared in @interface for %qE",
5423 CLASS_NAME (implementation_template));
5424 return error_mark_node;
5426 rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template));
5428 else if (self)
5429 rtype = lookup_interface (CLASS_NAME (implementation_template));
5432 if (objc_is_id (rtype))
5434 /* The receiver is of type 'id' or 'Class' (with or without some
5435 protocols attached to it). */
5437 /* We set class_tree to the identifier for 'Class' if this is a
5438 class method, and to NULL_TREE if not. */
5439 class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE);
5441 /* 'rprotos' is the list of protocols that the receiver
5442 supports. */
5443 rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
5444 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
5445 : NULL_TREE);
5447 /* We have no information on the type, and we set it to
5448 NULL_TREE. */
5449 rtype = NULL_TREE;
5451 /* If there are any protocols, check that the method we are
5452 calling appears in the protocol list. If there are no
5453 protocols, this is a message to 'id' or 'Class' and we accept
5454 any method that exists. */
5455 if (rprotos)
5457 /* If messaging 'id <Protos>' or 'Class <Proto>', first
5458 search in protocols themselves for the method
5459 prototype. */
5460 method_prototype
5461 = lookup_method_in_protocol_list (rprotos, sel_name,
5462 class_tree != NULL_TREE);
5464 /* If messaging 'Class <Proto>' but did not find a class
5465 method prototype, search for an instance method instead,
5466 and warn about having done so. */
5467 if (!method_prototype && !rtype && class_tree != NULL_TREE)
5469 method_prototype
5470 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5472 if (method_prototype)
5473 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
5474 sel_name, sel_name);
5478 else if (rtype)
5480 /* We have a receiver type which is more specific than 'id' or
5481 'Class'. */
5482 tree orig_rtype = rtype;
5484 if (TREE_CODE (rtype) == POINTER_TYPE)
5485 rtype = TREE_TYPE (rtype);
5486 /* Traverse typedef aliases */
5487 while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype)
5488 && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL
5489 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)))
5490 rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype));
5491 if (TYPED_OBJECT (rtype))
5493 rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype);
5494 rtype = TYPE_OBJC_INTERFACE (rtype);
5496 if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE)
5498 /* If we could not find an @interface declaration, we must
5499 have only seen a @class declaration; so, we cannot say
5500 anything more intelligent about which methods the
5501 receiver will understand. Note that this only happens
5502 for instance methods; for class methods to a class where
5503 we have only seen a @class declaration,
5504 lookup_interface() above would have set rtype to
5505 NULL_TREE. */
5506 if (rprotos)
5508 /* We could not find an @interface declaration, yet, if
5509 there are protocols attached to the type, we can
5510 still look up the method in the protocols. Ie, we
5511 are in the following case:
5513 @class MyClass;
5514 MyClass<MyProtocol> *x;
5515 [x method];
5517 If 'MyProtocol' has the method 'method', we can check
5518 and retrieve the method prototype. */
5519 method_prototype
5520 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5522 /* At this point, if we have found the method_prototype,
5523 we are quite happy. The details of the class are
5524 irrelevant. If we haven't found it, a warning will
5525 have been produced that the method could not be found
5526 in the protocol, and we won't produce further
5527 warnings (please note that this means that "@class
5528 MyClass; MyClass <MyProtocol> *x;" is exactly
5529 equivalent to "id <MyProtocol> x", which isn't too
5530 satisfactory but it's not easy to see how to do
5531 better). */
5533 else
5535 if (rtype)
5537 /* We could not find an @interface declaration, and
5538 there are no protocols attached to the receiver,
5539 so we can't complete the check that the receiver
5540 responds to the method, and we can't retrieve the
5541 method prototype. But, because the receiver has
5542 a well-specified class, the programmer did want
5543 this check to be performed. Emit a warning, then
5544 keep going as if it was an 'id'. To remove the
5545 warning, either include an @interface for the
5546 class, or cast the receiver to 'id'. Note that
5547 rtype is an IDENTIFIER_NODE at this point. */
5548 warning (0, "@interface of class %qE not found", rtype);
5552 rtype = NULL_TREE;
5554 else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE
5555 || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE)
5557 /* We have a valid ObjC class name with an associated
5558 @interface. Look up the method name in the published
5559 @interface for the class (and its superclasses). */
5560 method_prototype
5561 = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE);
5563 /* If the method was not found in the @interface, it may still
5564 exist locally as part of the @implementation. */
5565 if (!method_prototype && objc_implementation_context
5566 && CLASS_NAME (objc_implementation_context)
5567 == OBJC_TYPE_NAME (rtype))
5568 method_prototype
5569 = lookup_method
5570 ((class_tree
5571 ? CLASS_CLS_METHODS (objc_implementation_context)
5572 : CLASS_NST_METHODS (objc_implementation_context)),
5573 sel_name);
5575 /* If we haven't found a candidate method by now, try looking for
5576 it in the protocol list. */
5577 if (!method_prototype && rprotos)
5578 method_prototype
5579 = lookup_method_in_protocol_list (rprotos, sel_name,
5580 class_tree != NULL_TREE);
5582 else
5584 /* We have a type, but it's not an Objective-C type (!). */
5585 warning (0, "invalid receiver type %qs",
5586 identifier_to_locale (gen_type_name (orig_rtype)));
5587 /* After issuing the "invalid receiver" warning, perform method
5588 lookup as if we were messaging 'id'. */
5589 rtype = rprotos = NULL_TREE;
5592 /* Note that rtype could also be NULL_TREE. This happens if we are
5593 messaging a class by name, but the class was only
5594 forward-declared using @class. */
5596 /* For 'id' or 'Class' receivers, search in the global hash table as
5597 a last resort. For all receivers, warn if protocol searches have
5598 failed. */
5599 if (!method_prototype)
5601 if (rprotos)
5602 warning (0, "%<%c%E%> not found in protocol(s)",
5603 (class_tree ? '+' : '-'),
5604 sel_name);
5606 if (!rtype)
5607 method_prototype
5608 = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE);
5611 if (!method_prototype)
5613 static bool warn_missing_methods = false;
5615 if (rtype)
5616 warning (0, "%qE may not respond to %<%c%E%>",
5617 OBJC_TYPE_NAME (rtype),
5618 (class_tree ? '+' : '-'),
5619 sel_name);
5620 /* If we are messaging an 'id' or 'Class' object and made it here,
5621 then we have failed to find _any_ instance or class method,
5622 respectively. */
5623 else
5624 warning (0, "no %<%c%E%> method found",
5625 (class_tree ? '+' : '-'),
5626 sel_name);
5628 if (!warn_missing_methods)
5630 warning_at (input_location,
5631 0, "(Messages without a matching method signature");
5632 warning_at (input_location,
5633 0, "will be assumed to return %<id%> and accept");
5634 warning_at (input_location,
5635 0, "%<...%> as arguments.)");
5636 warn_missing_methods = true;
5639 else
5641 /* Warn if the method is deprecated, but not if the receiver is
5642 a generic 'id'. 'id' is used to cast an object to a generic
5643 object of an unspecified class; in that case, we'll use
5644 whatever method prototype we can find to get the method
5645 argument and return types, but it is not appropriate to
5646 produce deprecation warnings since we don't know the class
5647 that the object will be of at runtime. The @interface(s) for
5648 that class may not even be available to the compiler right
5649 now, and it is perfectly possible that the method is marked
5650 as non-deprecated in such @interface(s).
5652 In practice this makes sense since casting an object to 'id'
5653 is often used precisely to turn off warnings associated with
5654 the object being of a particular class. */
5655 if (TREE_DEPRECATED (method_prototype) && rtype != NULL_TREE)
5657 if (deprecated_method_prototype)
5658 *deprecated_method_prototype = method_prototype;
5659 else
5660 warn_deprecated_use (method_prototype, NULL_TREE);
5664 /* Save the selector name for printing error messages. */
5665 current_objc_message_selector = sel_name;
5667 /* Build the method call.
5668 TODO: Get the location from somewhere that will work for delayed
5669 expansion. */
5671 retval = (*runtime.build_objc_method_call) (input_location, method_prototype,
5672 receiver, rtype, sel_name,
5673 method_params, super);
5675 current_objc_message_selector = 0;
5677 return retval;
5681 /* This routine creates a static variable used to implement @protocol(MyProtocol)
5682 expression. This variable will be initialized to global protocol_t meta-data
5683 pointer. */
5685 /* This function is called by the parser when (and only when) a
5686 @protocol() expression is found, in order to compile it. */
5687 tree
5688 objc_build_protocol_expr (tree protoname)
5690 tree p = lookup_protocol (protoname, /* warn if deprecated */ true,
5691 /* definition_required */ false);
5693 if (!p)
5695 error ("cannot find protocol declaration for %qE", protoname);
5696 return error_mark_node;
5699 return (*runtime.get_protocol_reference) (input_location, p);
5702 /* This function is called by the parser when a @selector() expression
5703 is found, in order to compile it. It is only called by the parser
5704 and only to compile a @selector(). LOC is the location of the
5705 @selector. */
5706 tree
5707 objc_build_selector_expr (location_t loc, tree selnamelist)
5709 tree selname;
5711 /* Obtain the full selector name. */
5712 switch (TREE_CODE (selnamelist))
5714 case IDENTIFIER_NODE:
5715 /* A unary selector. */
5716 selname = selnamelist;
5717 break;
5718 case TREE_LIST:
5719 selname = build_keyword_selector (selnamelist);
5720 break;
5721 default:
5722 gcc_unreachable ();
5725 /* If we are required to check @selector() expressions as they
5726 are found, check that the selector has been declared. */
5727 if (warn_undeclared_selector)
5729 /* Look the selector up in the list of all known class and
5730 instance methods (up to this line) to check that the selector
5731 exists. */
5732 tree method;
5734 /* First try with instance methods. */
5735 method = objc_map_get (instance_method_map, selname);
5737 /* If not found, try with class methods. */
5738 if (method == OBJC_MAP_NOT_FOUND)
5740 method = objc_map_get (class_method_map, selname);
5742 /* If still not found, print out a warning. */
5743 if (method == OBJC_MAP_NOT_FOUND)
5744 warning (0, "undeclared selector %qE", selname);
5748 /* The runtimes do this differently, most particularly, GNU has typed
5749 selectors, whilst NeXT does not. */
5750 return (*runtime.build_selector_reference) (loc, selname, NULL_TREE);
5753 static tree
5754 build_ivar_reference (tree id)
5756 tree base;
5757 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
5759 /* Historically, a class method that produced objects (factory
5760 method) would assign `self' to the instance that it
5761 allocated. This would effectively turn the class method into
5762 an instance method. Following this assignment, the instance
5763 variables could be accessed. That practice, while safe,
5764 violates the simple rule that a class method should not refer
5765 to an instance variable. It's better to catch the cases
5766 where this is done unknowingly than to support the above
5767 paradigm. */
5768 warning (0, "instance variable %qE accessed in class method",
5769 id);
5770 self_decl = convert (objc_instance_type, self_decl); /* cast */
5773 base = build_indirect_ref (input_location, self_decl, RO_ARROW);
5774 return (*runtime.build_ivar_reference) (input_location, base, id);
5777 static void
5778 hash_init (void)
5780 instance_method_map = objc_map_alloc_ggc (1000);
5781 class_method_map = objc_map_alloc_ggc (1000);
5783 class_name_map = objc_map_alloc_ggc (200);
5784 alias_name_map = objc_map_alloc_ggc (200);
5786 /* Initialize the hash table used to hold the constant string objects. */
5787 string_htab = hash_table<objc_string_hasher>::create_ggc (31);
5790 /* Use the following to add a method to class_method_map or
5791 instance_method_map. It will add the method, keyed by the
5792 METHOD_SEL_NAME. If the method already exists, but with one or
5793 more different prototypes, it will store a TREE_VEC in the map,
5794 with the method prototypes in the vector. */
5795 static void
5796 insert_method_into_method_map (bool class_method, tree method)
5798 tree method_name = METHOD_SEL_NAME (method);
5799 tree existing_entry;
5800 objc_map_t map;
5802 if (class_method)
5803 map = class_method_map;
5804 else
5805 map = instance_method_map;
5807 /* Check if the method already exists in the map. */
5808 existing_entry = objc_map_get (map, method_name);
5810 /* If not, we simply add it to the map. */
5811 if (existing_entry == OBJC_MAP_NOT_FOUND)
5812 objc_map_put (map, method_name, method);
5813 else
5815 tree new_entry;
5817 /* If an entry already exists, it's more complicated. We'll
5818 have to check whether the method prototype is the same or
5819 not. */
5820 if (TREE_CODE (existing_entry) != TREE_VEC)
5822 /* If the method prototypes are the same, there is nothing
5823 to do. */
5824 if (comp_proto_with_proto (method, existing_entry, 1))
5825 return;
5827 /* If not, create a vector to store both the method already
5828 in the map, and the new one that we are adding. */
5829 new_entry = make_tree_vec (2);
5831 TREE_VEC_ELT (new_entry, 0) = existing_entry;
5832 TREE_VEC_ELT (new_entry, 1) = method;
5834 else
5836 /* An entry already exists, and it's already a vector. This
5837 means that at least 2 different method prototypes were
5838 already found, and we're considering registering yet
5839 another one. */
5840 size_t i;
5842 /* Check all the existing prototypes. If any matches the
5843 one we need to add, there is nothing to do because it's
5844 already there. */
5845 for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++)
5846 if (comp_proto_with_proto (method, TREE_VEC_ELT (existing_entry, i), 1))
5847 return;
5849 /* Else, create a new, bigger vector and add the new method
5850 at the end of it. This is inefficient but extremely
5851 rare; in any sane program most methods have a single
5852 prototype, and very few, if any, will have more than
5853 2! */
5854 new_entry = make_tree_vec (TREE_VEC_LENGTH (existing_entry) + 1);
5856 /* Copy the methods from the existing vector. */
5857 for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++)
5858 TREE_VEC_ELT (new_entry, i) = TREE_VEC_ELT (existing_entry, i);
5860 /* Add the new method at the end. */
5861 TREE_VEC_ELT (new_entry, i) = method;
5864 /* Store the new vector in the map. */
5865 objc_map_put (map, method_name, new_entry);
5870 static tree
5871 lookup_method (tree mchain, tree method)
5873 tree key;
5875 if (TREE_CODE (method) == IDENTIFIER_NODE)
5876 key = method;
5877 else
5878 key = METHOD_SEL_NAME (method);
5880 while (mchain)
5882 if (METHOD_SEL_NAME (mchain) == key)
5883 return mchain;
5885 mchain = DECL_CHAIN (mchain);
5887 return NULL_TREE;
5890 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance
5891 method in INTERFACE, along with any categories and protocols
5892 attached thereto. If method is not found, and the
5893 OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS, recursively examine the
5894 INTERFACE's superclass. If OBJC_LOOKUP_CLASS is set,
5895 OBJC_LOOKUP_NO_SUPER is clear, and no suitable class method could
5896 be found in INTERFACE or any of its superclasses, look for an
5897 _instance_ method of the same name in the root class as a last
5898 resort. This behavior can be turned off by using
5899 OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS.
5901 If a suitable method cannot be found, return NULL_TREE. */
5903 static tree
5904 lookup_method_static (tree interface, tree ident, int flags)
5906 tree meth = NULL_TREE, root_inter = NULL_TREE;
5907 tree inter = interface;
5908 int is_class = (flags & OBJC_LOOKUP_CLASS);
5909 int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER);
5910 int no_instance_methods_of_root_class = (flags & OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS);
5912 while (inter)
5914 tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter);
5915 tree category = inter;
5917 /* First, look up the method in the class itself. */
5918 if ((meth = lookup_method (chain, ident)))
5919 return meth;
5921 /* Failing that, look for the method in each category of the class. */
5922 while ((category = CLASS_CATEGORY_LIST (category)))
5924 chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category);
5926 /* Check directly in each category. */
5927 if ((meth = lookup_method (chain, ident)))
5928 return meth;
5930 /* Failing that, check in each category's protocols. */
5931 if (CLASS_PROTOCOL_LIST (category))
5933 if ((meth = (lookup_method_in_protocol_list
5934 (CLASS_PROTOCOL_LIST (category), ident, is_class))))
5935 return meth;
5939 /* If not found in categories, check in protocols of the main class. */
5940 if (CLASS_PROTOCOL_LIST (inter))
5942 if ((meth = (lookup_method_in_protocol_list
5943 (CLASS_PROTOCOL_LIST (inter), ident, is_class))))
5944 return meth;
5947 /* If we were instructed not to look in superclasses, don't. */
5948 if (no_superclasses)
5949 return NULL_TREE;
5951 /* Failing that, climb up the inheritance hierarchy. */
5952 root_inter = inter;
5953 inter = lookup_interface (CLASS_SUPER_NAME (inter));
5955 while (inter);
5957 if (is_class && !no_instance_methods_of_root_class)
5959 /* If no class (factory) method was found, check if an _instance_
5960 method of the same name exists in the root class. This is what
5961 the Objective-C runtime will do. */
5962 return lookup_method_static (root_inter, ident, 0);
5964 else
5966 /* If an instance method was not found, return 0. */
5967 return NULL_TREE;
5971 static tree
5972 objc_add_method (tree klass, tree method, int is_class, bool is_optional)
5974 tree existing_method = NULL_TREE;
5976 /* The first thing we do is look up the method in the list of
5977 methods already defined in the interface (or implementation). */
5978 if (is_class)
5979 existing_method = lookup_method (CLASS_CLS_METHODS (klass), method);
5980 else
5981 existing_method = lookup_method (CLASS_NST_METHODS (klass), method);
5983 /* In the case of protocols, we have a second list of methods to
5984 consider, the list of optional ones. */
5985 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
5987 /* @required methods are added to the protocol's normal list.
5988 @optional methods are added to the protocol's OPTIONAL lists.
5989 Note that adding the methods to the optional lists disables
5990 checking that the methods are implemented by classes
5991 implementing the protocol, since these checks only use the
5992 CLASS_CLS_METHODS and CLASS_NST_METHODS. */
5994 /* First of all, if the method to add is @optional, and we found
5995 it already existing as @required, emit an error. */
5996 if (is_optional && existing_method)
5998 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
5999 (is_class ? '+' : '-'),
6000 METHOD_SEL_NAME (existing_method));
6001 inform (DECL_SOURCE_LOCATION (existing_method),
6002 "previous declaration of %<%c%E%> as %<@required%>",
6003 (is_class ? '+' : '-'),
6004 METHOD_SEL_NAME (existing_method));
6007 /* Now check the list of @optional methods if we didn't find the
6008 method in the @required list. */
6009 if (!existing_method)
6011 if (is_class)
6012 existing_method = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (klass), method);
6013 else
6014 existing_method = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (klass), method);
6016 if (!is_optional && existing_method)
6018 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
6019 (is_class ? '+' : '-'),
6020 METHOD_SEL_NAME (existing_method));
6021 inform (DECL_SOURCE_LOCATION (existing_method),
6022 "previous declaration of %<%c%E%> as %<@optional%>",
6023 (is_class ? '+' : '-'),
6024 METHOD_SEL_NAME (existing_method));
6029 /* If the method didn't exist already, add it. */
6030 if (!existing_method)
6032 if (is_optional)
6034 if (is_class)
6036 /* Put the method on the list in reverse order. */
6037 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass);
6038 PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method;
6040 else
6042 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass);
6043 PROTOCOL_OPTIONAL_NST_METHODS (klass) = method;
6046 else
6048 if (is_class)
6050 DECL_CHAIN (method) = CLASS_CLS_METHODS (klass);
6051 CLASS_CLS_METHODS (klass) = method;
6053 else
6055 DECL_CHAIN (method) = CLASS_NST_METHODS (klass);
6056 CLASS_NST_METHODS (klass) = method;
6060 else
6062 /* The method was already defined. Check that the types match
6063 for an @interface for a class or category, or for a
6064 @protocol. Give hard errors on methods with identical
6065 selectors but differing argument and/or return types. We do
6066 not do this for @implementations, because C/C++ will do it
6067 for us (i.e., there will be duplicate function definition
6068 errors). */
6069 if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE
6070 || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6071 /* Starting with GCC 4.6, we emit the same error for
6072 protocols too. The situation is identical to
6073 @interfaces as there is no possible meaningful reason
6074 for defining the same method with different signatures
6075 in the very same @protocol. If that was allowed,
6076 whenever the protocol is used (both at compile and run
6077 time) there wouldn't be any meaningful way to decide
6078 which of the two method signatures should be used. */
6079 || TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
6080 && !comp_proto_with_proto (method, existing_method, 1))
6082 error ("duplicate declaration of method %<%c%E%> with conflicting types",
6083 (is_class ? '+' : '-'),
6084 METHOD_SEL_NAME (existing_method));
6085 inform (DECL_SOURCE_LOCATION (existing_method),
6086 "previous declaration of %<%c%E%>",
6087 (is_class ? '+' : '-'),
6088 METHOD_SEL_NAME (existing_method));
6092 if (is_class)
6093 insert_method_into_method_map (true, method);
6094 else
6096 insert_method_into_method_map (false, method);
6098 /* Instance methods in root classes (and categories thereof)
6099 may act as class methods as a last resort. We also add
6100 instance methods listed in @protocol declarations to
6101 the class hash table, on the assumption that @protocols
6102 may be adopted by root classes or categories. */
6103 if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6104 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
6105 klass = lookup_interface (CLASS_NAME (klass));
6107 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE
6108 || !CLASS_SUPER_NAME (klass))
6109 insert_method_into_method_map (true, method);
6112 return method;
6115 static void
6116 add_category (tree klass, tree category)
6118 /* Put categories on list in reverse order. */
6119 tree cat = lookup_category (klass, CLASS_SUPER_NAME (category));
6121 if (cat)
6123 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
6124 CLASS_NAME (klass),
6125 CLASS_SUPER_NAME (category));
6127 else
6129 CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass);
6130 CLASS_CATEGORY_LIST (klass) = category;
6134 #ifndef OBJCPLUS
6135 /* A flexible array member is a C99 extension where you can use
6136 "type[]" at the end of a struct to mean a variable-length array.
6138 In Objective-C, instance variables are fundamentally members of a
6139 struct, but the struct can always be extended by subclassing; hence
6140 we need to detect and forbid all instance variables declared using
6141 flexible array members.
6143 No check for this is needed in Objective-C++, since C++ does not
6144 have flexible array members. */
6146 /* Determine whether TYPE is a structure with a flexible array member,
6147 a union containing such a structure (possibly recursively) or an
6148 array of such structures or unions. These are all invalid as
6149 instance variable. */
6150 static bool
6151 flexible_array_type_p (tree type)
6153 tree x;
6154 switch (TREE_CODE (type))
6156 case RECORD_TYPE:
6157 x = TYPE_FIELDS (type);
6158 if (x == NULL_TREE)
6159 return false;
6160 while (DECL_CHAIN (x) != NULL_TREE)
6161 x = DECL_CHAIN (x);
6162 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6163 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6164 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6165 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6166 return true;
6167 return false;
6168 case UNION_TYPE:
6169 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
6171 if (flexible_array_type_p (TREE_TYPE (x)))
6172 return true;
6174 return false;
6175 /* Note that we also check for arrays of something that uses a flexible array member. */
6176 case ARRAY_TYPE:
6177 if (flexible_array_type_p (TREE_TYPE (type)))
6178 return true;
6179 return false;
6180 default:
6181 return false;
6184 #endif
6186 /* Produce a printable version of an ivar name. This is only used
6187 inside add_instance_variable. */
6188 static const char *
6189 printable_ivar_name (tree field_decl)
6191 if (DECL_NAME (field_decl))
6192 return identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl)));
6193 else
6194 return _("<unnamed>");
6197 /* Called after parsing each instance variable declaration. Necessary to
6198 preserve typedefs and implement public/private...
6200 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
6202 static tree
6203 add_instance_variable (tree klass, objc_ivar_visibility_kind visibility,
6204 tree field_decl)
6206 tree field_type = TREE_TYPE (field_decl);
6208 #ifdef OBJCPLUS
6209 if (TREE_CODE (field_type) == REFERENCE_TYPE)
6211 error ("illegal reference type specified for instance variable %qs",
6212 printable_ivar_name (field_decl));
6213 /* Return class as is without adding this ivar. */
6214 return klass;
6216 #endif
6218 if (field_type == error_mark_node || !TYPE_SIZE (field_type)
6219 || TYPE_SIZE (field_type) == error_mark_node)
6220 /* 'type[0]' is allowed, but 'type[]' is not! */
6222 error ("instance variable %qs has unknown size",
6223 printable_ivar_name (field_decl));
6224 /* Return class as is without adding this ivar. */
6225 return klass;
6228 #ifndef OBJCPLUS
6229 /* Also, in C reject a struct with a flexible array member. Ie,
6231 struct A { int x; int[] y; };
6233 @interface X
6235 struct A instance_variable;
6237 @end
6239 is not valid because if the class is subclassed, we wouldn't be able
6240 to calculate the offset of the next instance variable. */
6241 if (flexible_array_type_p (field_type))
6243 error ("instance variable %qs uses flexible array member",
6244 printable_ivar_name (field_decl));
6245 /* Return class as is without adding this ivar. */
6246 return klass;
6248 #endif
6250 #ifdef OBJCPLUS
6251 /* Check if the ivar being added has a non-POD C++ type. If so, we will
6252 need to either (1) warn the user about it or (2) generate suitable
6253 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
6254 methods (if '-fobjc-call-cxx-cdtors' was specified). */
6255 if (MAYBE_CLASS_TYPE_P (field_type)
6256 && (TYPE_NEEDS_CONSTRUCTING (field_type)
6257 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)
6258 || TYPE_POLYMORPHIC_P (field_type)))
6260 tree type_name = OBJC_TYPE_NAME (field_type);
6262 if (flag_objc_call_cxx_cdtors)
6264 /* Since the ObjC runtime will be calling the constructors and
6265 destructors for us, the only thing we can't handle is the lack
6266 of a default constructor. */
6267 if (TYPE_NEEDS_CONSTRUCTING (field_type)
6268 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type))
6270 warning (0, "type %qE has no default constructor to call",
6271 type_name);
6273 /* If we cannot call a constructor, we should also avoid
6274 calling the destructor, for symmetry. */
6275 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6276 warning (0, "destructor for %qE shall not be run either",
6277 type_name);
6280 else
6282 static bool warn_cxx_ivars = false;
6284 if (TYPE_POLYMORPHIC_P (field_type))
6286 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
6287 initialize them. */
6288 error ("type %qE has virtual member functions", type_name);
6289 error ("illegal aggregate type %qE specified "
6290 "for instance variable %qs",
6291 type_name, printable_ivar_name (field_decl));
6292 /* Return class as is without adding this ivar. */
6293 return klass;
6296 /* User-defined constructors and destructors are not known to Obj-C
6297 and hence will not be called. This may or may not be a problem. */
6298 if (TYPE_NEEDS_CONSTRUCTING (field_type))
6299 warning (0, "type %qE has a user-defined constructor", type_name);
6300 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6301 warning (0, "type %qE has a user-defined destructor", type_name);
6303 if (!warn_cxx_ivars)
6305 warning (0, "C++ constructors and destructors will not "
6306 "be invoked for Objective-C fields");
6307 warn_cxx_ivars = true;
6311 #endif
6313 /* Overload the public attribute, it is not used for FIELD_DECLs. */
6314 switch (visibility)
6316 case OBJC_IVAR_VIS_PROTECTED:
6317 TREE_PUBLIC (field_decl) = 0;
6318 TREE_PRIVATE (field_decl) = 0;
6319 TREE_PROTECTED (field_decl) = 1;
6320 break;
6322 case OBJC_IVAR_VIS_PACKAGE:
6323 /* TODO: Implement the package variant. */
6324 case OBJC_IVAR_VIS_PUBLIC:
6325 TREE_PUBLIC (field_decl) = 1;
6326 TREE_PRIVATE (field_decl) = 0;
6327 TREE_PROTECTED (field_decl) = 0;
6328 break;
6330 case OBJC_IVAR_VIS_PRIVATE:
6331 TREE_PUBLIC (field_decl) = 0;
6332 TREE_PRIVATE (field_decl) = 1;
6333 TREE_PROTECTED (field_decl) = 0;
6334 break;
6338 CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl);
6340 return klass;
6343 /* True if the ivar is private and we are not in its implementation. */
6345 static int
6346 is_private (tree decl)
6348 return (TREE_PRIVATE (decl)
6349 && ! is_ivar (CLASS_IVARS (implementation_template),
6350 DECL_NAME (decl)));
6353 /* Searches all the instance variables of 'klass' and of its
6354 superclasses for an instance variable whose name (identifier) is
6355 'ivar_name_ident'. Return the declaration (DECL) of the instance
6356 variable, if found, or NULL_TREE, if not found. */
6357 static inline tree
6358 ivar_of_class (tree klass, tree ivar_name_ident)
6360 /* First, look up the ivar in CLASS_RAW_IVARS. */
6361 tree decl_chain = CLASS_RAW_IVARS (klass);
6363 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6364 if (DECL_NAME (decl_chain) == ivar_name_ident)
6365 return decl_chain;
6367 /* If not found, search up the class hierarchy. */
6368 while (CLASS_SUPER_NAME (klass))
6370 klass = lookup_interface (CLASS_SUPER_NAME (klass));
6372 decl_chain = CLASS_RAW_IVARS (klass);
6374 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6375 if (DECL_NAME (decl_chain) == ivar_name_ident)
6376 return decl_chain;
6379 return NULL_TREE;
6382 /* We have an instance variable reference;, check to see if it is public. */
6385 objc_is_public (tree expr, tree identifier)
6387 tree basetype, decl;
6389 #ifdef OBJCPLUS
6390 if (processing_template_decl)
6391 return 1;
6392 #endif
6394 if (TREE_TYPE (expr) == error_mark_node)
6395 return 1;
6397 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
6399 if (basetype && TREE_CODE (basetype) == RECORD_TYPE)
6401 if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype))
6403 tree klass = lookup_interface (OBJC_TYPE_NAME (basetype));
6405 if (!klass)
6407 error ("cannot find interface declaration for %qE",
6408 OBJC_TYPE_NAME (basetype));
6409 return 0;
6412 if ((decl = ivar_of_class (klass, identifier)))
6414 if (TREE_PUBLIC (decl))
6415 return 1;
6417 /* Important difference between the Stepstone translator:
6418 all instance variables should be public within the context
6419 of the implementation. */
6420 if (objc_implementation_context
6421 && ((TREE_CODE (objc_implementation_context)
6422 == CLASS_IMPLEMENTATION_TYPE)
6423 || (TREE_CODE (objc_implementation_context)
6424 == CATEGORY_IMPLEMENTATION_TYPE)))
6426 tree curtype = TYPE_MAIN_VARIANT
6427 (CLASS_STATIC_TEMPLATE
6428 (implementation_template));
6430 if (basetype == curtype
6431 || DERIVED_FROM_P (basetype, curtype))
6433 int priv = is_private (decl);
6435 if (priv)
6436 error ("instance variable %qE is declared private",
6437 DECL_NAME (decl));
6439 return !priv;
6443 /* The 2.95.2 compiler sometimes allowed C functions to access
6444 non-@public ivars. We will let this slide for now... */
6445 if (!objc_method_context)
6447 warning (0, "instance variable %qE is %s; "
6448 "this will be a hard error in the future",
6449 identifier,
6450 TREE_PRIVATE (decl) ? "@private" : "@protected");
6451 return 1;
6454 error ("instance variable %qE is declared %s",
6455 identifier,
6456 TREE_PRIVATE (decl) ? "private" : "protected");
6457 return 0;
6462 return 1;
6465 /* Make sure all methods in CHAIN (a list of method declarations from
6466 an @interface or a @protocol) are in IMPLEMENTATION (the
6467 implementation context). This is used to check for example that
6468 all methods declared in an @interface were implemented in an
6469 @implementation.
6471 Some special methods (property setters/getters) are special and if
6472 they are not found in IMPLEMENTATION, we look them up in its
6473 superclasses. */
6475 static int
6476 check_methods (tree chain, tree implementation, int mtype)
6478 int first = 1;
6479 tree list;
6481 if (mtype == (int)'+')
6482 list = CLASS_CLS_METHODS (implementation);
6483 else
6484 list = CLASS_NST_METHODS (implementation);
6486 while (chain)
6488 /* If the method is associated with a dynamic property, then it
6489 is Ok not to have the method implementation, as it will be
6490 generated dynamically at runtime. To decide if the method is
6491 associated with a @dynamic property, we search the list of
6492 @synthesize and @dynamic for this implementation, and look
6493 for any @dynamic property with the same setter or getter name
6494 as this method. */
6495 tree x;
6496 for (x = IMPL_PROPERTY_DECL (implementation); x; x = TREE_CHAIN (x))
6497 if (PROPERTY_DYNAMIC (x)
6498 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6499 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6500 break;
6502 if (x != NULL_TREE)
6504 chain = TREE_CHAIN (chain); /* next method... */
6505 continue;
6508 if (!lookup_method (list, chain))
6510 /* If the method is a property setter/getter, we'll still
6511 allow it to be missing if it is implemented by
6512 'interface' or any of its superclasses. */
6513 tree property = METHOD_PROPERTY_CONTEXT (chain);
6514 if (property)
6516 /* Note that since this is a property getter/setter, it
6517 is obviously an instance method. */
6518 tree interface = NULL_TREE;
6520 /* For a category, first check the main class
6521 @interface. */
6522 if (TREE_CODE (implementation) == CATEGORY_IMPLEMENTATION_TYPE)
6524 interface = lookup_interface (CLASS_NAME (implementation));
6526 /* If the method is found in the main class, it's Ok. */
6527 if (lookup_method (CLASS_NST_METHODS (interface), chain))
6529 chain = DECL_CHAIN (chain);
6530 continue;
6533 /* Else, get the superclass. */
6534 if (CLASS_SUPER_NAME (interface))
6535 interface = lookup_interface (CLASS_SUPER_NAME (interface));
6536 else
6537 interface = NULL_TREE;
6540 /* Get the superclass for classes. */
6541 if (TREE_CODE (implementation) == CLASS_IMPLEMENTATION_TYPE)
6543 if (CLASS_SUPER_NAME (implementation))
6544 interface = lookup_interface (CLASS_SUPER_NAME (implementation));
6545 else
6546 interface = NULL_TREE;
6549 /* Now, interface is the superclass, if any; go check it. */
6550 if (interface)
6552 if (lookup_method_static (interface, chain, 0))
6554 chain = DECL_CHAIN (chain);
6555 continue;
6558 /* Else, fall through - warn. */
6560 if (first)
6562 switch (TREE_CODE (implementation))
6564 case CLASS_IMPLEMENTATION_TYPE:
6565 warning (0, "incomplete implementation of class %qE",
6566 CLASS_NAME (implementation));
6567 break;
6568 case CATEGORY_IMPLEMENTATION_TYPE:
6569 warning (0, "incomplete implementation of category %qE",
6570 CLASS_SUPER_NAME (implementation));
6571 break;
6572 default:
6573 gcc_unreachable ();
6575 first = 0;
6578 warning (0, "method definition for %<%c%E%> not found",
6579 mtype, METHOD_SEL_NAME (chain));
6582 chain = DECL_CHAIN (chain);
6585 return first;
6588 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
6590 static int
6591 conforms_to_protocol (tree klass, tree protocol)
6593 if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE)
6595 tree p = CLASS_PROTOCOL_LIST (klass);
6596 while (p && TREE_VALUE (p) != protocol)
6597 p = TREE_CHAIN (p);
6599 if (!p)
6601 tree super = (CLASS_SUPER_NAME (klass)
6602 ? lookup_interface (CLASS_SUPER_NAME (klass))
6603 : NULL_TREE);
6604 int tmp = super ? conforms_to_protocol (super, protocol) : 0;
6605 if (!tmp)
6606 return 0;
6610 return 1;
6613 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
6614 CONTEXT. This is one of two mechanisms to check protocol integrity. */
6616 static int
6617 check_methods_accessible (tree chain, tree context, int mtype)
6619 int first = 1;
6620 tree list;
6621 tree base_context = context;
6623 while (chain)
6625 /* If the method is associated with a dynamic property, then it
6626 is Ok not to have the method implementation, as it will be
6627 generated dynamically at runtime. Search for any @dynamic
6628 property with the same setter or getter name as this
6629 method. TODO: Use a hashtable lookup. */
6630 tree x;
6631 for (x = IMPL_PROPERTY_DECL (base_context); x; x = TREE_CHAIN (x))
6632 if (PROPERTY_DYNAMIC (x)
6633 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6634 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6635 break;
6637 if (x != NULL_TREE)
6639 chain = TREE_CHAIN (chain); /* next method... */
6640 continue;
6643 context = base_context;
6644 while (context)
6646 if (mtype == '+')
6647 list = CLASS_CLS_METHODS (context);
6648 else
6649 list = CLASS_NST_METHODS (context);
6651 if (lookup_method (list, chain))
6652 break;
6654 switch (TREE_CODE (context))
6656 case CLASS_IMPLEMENTATION_TYPE:
6657 case CLASS_INTERFACE_TYPE:
6658 context = (CLASS_SUPER_NAME (context)
6659 ? lookup_interface (CLASS_SUPER_NAME (context))
6660 : NULL_TREE);
6661 break;
6662 case CATEGORY_IMPLEMENTATION_TYPE:
6663 case CATEGORY_INTERFACE_TYPE:
6664 context = (CLASS_NAME (context)
6665 ? lookup_interface (CLASS_NAME (context))
6666 : NULL_TREE);
6667 break;
6668 default:
6669 gcc_unreachable ();
6673 if (context == NULL_TREE)
6675 if (first)
6677 switch (TREE_CODE (objc_implementation_context))
6679 case CLASS_IMPLEMENTATION_TYPE:
6680 warning (0, "incomplete implementation of class %qE",
6681 CLASS_NAME (objc_implementation_context));
6682 break;
6683 case CATEGORY_IMPLEMENTATION_TYPE:
6684 warning (0, "incomplete implementation of category %qE",
6685 CLASS_SUPER_NAME (objc_implementation_context));
6686 break;
6687 default:
6688 gcc_unreachable ();
6690 first = 0;
6692 warning (0, "method definition for %<%c%E%> not found",
6693 mtype, METHOD_SEL_NAME (chain));
6696 chain = TREE_CHAIN (chain); /* next method... */
6698 return first;
6701 /* Check whether the current interface (accessible via
6702 'objc_implementation_context') actually implements protocol P, along
6703 with any protocols that P inherits. */
6705 static void
6706 check_protocol (tree p, const char *type, tree name)
6708 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
6710 int f1, f2;
6712 /* Ensure that all protocols have bodies! */
6713 if (warn_protocol)
6715 f1 = check_methods (PROTOCOL_CLS_METHODS (p),
6716 objc_implementation_context,
6717 '+');
6718 f2 = check_methods (PROTOCOL_NST_METHODS (p),
6719 objc_implementation_context,
6720 '-');
6722 else
6724 f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
6725 objc_implementation_context,
6726 '+');
6727 f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
6728 objc_implementation_context,
6729 '-');
6732 if (!f1 || !f2)
6733 warning (0, "%s %qE does not fully implement the %qE protocol",
6734 type, name, PROTOCOL_NAME (p));
6737 /* Check protocols recursively. */
6738 if (PROTOCOL_LIST (p))
6740 tree subs = PROTOCOL_LIST (p);
6741 tree super_class =
6742 lookup_interface (CLASS_SUPER_NAME (implementation_template));
6744 while (subs)
6746 tree sub = TREE_VALUE (subs);
6748 /* If the superclass does not conform to the protocols
6749 inherited by P, then we must! */
6750 if (!super_class || !conforms_to_protocol (super_class, sub))
6751 check_protocol (sub, type, name);
6752 subs = TREE_CHAIN (subs);
6757 /* Check whether the current interface (accessible via
6758 'objc_implementation_context') actually implements the protocols listed
6759 in PROTO_LIST. */
6761 static void
6762 check_protocols (tree proto_list, const char *type, tree name)
6764 for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
6766 tree p = TREE_VALUE (proto_list);
6768 check_protocol (p, type, name);
6772 /* Make sure that the class CLASS_NAME is defined CODE says which kind
6773 of thing CLASS_NAME ought to be. It can be CLASS_INTERFACE_TYPE,
6774 CLASS_IMPLEMENTATION_TYPE, CATEGORY_INTERFACE_TYPE, or
6775 CATEGORY_IMPLEMENTATION_TYPE. For a CATEGORY_INTERFACE_TYPE,
6776 SUPER_NAME is the name of the category. For a class extension,
6777 CODE is CATEGORY_INTERFACE_TYPE and SUPER_NAME is NULL_TREE. */
6778 static tree
6779 start_class (enum tree_code code, tree class_name, tree super_name,
6780 tree protocol_list, tree attributes)
6782 tree klass = NULL_TREE;
6783 tree decl;
6785 #ifdef OBJCPLUS
6786 if (current_namespace != global_namespace)
6788 error ("Objective-C declarations may only appear in global scope");
6790 #endif /* OBJCPLUS */
6792 if (objc_implementation_context)
6794 warning (0, "%<@end%> missing in implementation context");
6795 finish_class (objc_implementation_context);
6796 objc_ivar_chain = NULL_TREE;
6797 objc_implementation_context = NULL_TREE;
6800 /* If this is a class extension, we'll be "reopening" the existing
6801 CLASS_INTERFACE_TYPE, so in that case there is no need to create
6802 a new node. */
6803 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6805 klass = make_node (code);
6806 TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS);
6809 /* Check for existence of the super class, if one was specified. Note
6810 that we must have seen an @interface, not just a @class. If we
6811 are looking at a @compatibility_alias, traverse it first. */
6812 if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE)
6813 && super_name)
6815 tree super = objc_is_class_name (super_name);
6816 tree super_interface = NULL_TREE;
6818 if (super)
6819 super_interface = lookup_interface (super);
6821 if (!super_interface)
6823 error ("cannot find interface declaration for %qE, superclass of %qE",
6824 super ? super : super_name,
6825 class_name);
6826 super_name = NULL_TREE;
6828 else
6830 if (TREE_DEPRECATED (super_interface))
6831 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
6832 super);
6833 super_name = super;
6837 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6839 CLASS_NAME (klass) = class_name;
6840 CLASS_SUPER_NAME (klass) = super_name;
6841 CLASS_CLS_METHODS (klass) = NULL_TREE;
6844 if (! objc_is_class_name (class_name)
6845 && (decl = lookup_name (class_name)))
6847 error ("%qE redeclared as different kind of symbol",
6848 class_name);
6849 error ("previous declaration of %q+D",
6850 decl);
6853 switch (code)
6855 case CLASS_IMPLEMENTATION_TYPE:
6857 tree chain;
6859 for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
6860 if (TREE_VALUE (chain) == class_name)
6862 error ("reimplementation of class %qE",
6863 class_name);
6864 /* TODO: error message saying where it was previously
6865 implemented. */
6866 break;
6868 if (chain == NULL_TREE)
6869 implemented_classes = tree_cons (NULL_TREE, class_name,
6870 implemented_classes);
6873 /* Reset for multiple classes per file. */
6874 method_slot = 0;
6876 objc_implementation_context = klass;
6878 /* Lookup the interface for this implementation. */
6880 if (!(implementation_template = lookup_interface (class_name)))
6882 warning (0, "cannot find interface declaration for %qE",
6883 class_name);
6884 add_interface (implementation_template = objc_implementation_context,
6885 class_name);
6888 /* If a super class has been specified in the implementation,
6889 insure it conforms to the one specified in the interface. */
6891 if (super_name
6892 && (super_name != CLASS_SUPER_NAME (implementation_template)))
6894 tree previous_name = CLASS_SUPER_NAME (implementation_template);
6895 error ("conflicting super class name %qE",
6896 super_name);
6897 if (previous_name)
6898 error ("previous declaration of %qE", previous_name);
6899 else
6900 error ("previous declaration");
6903 else if (! super_name)
6905 CLASS_SUPER_NAME (objc_implementation_context)
6906 = CLASS_SUPER_NAME (implementation_template);
6908 break;
6910 case CLASS_INTERFACE_TYPE:
6911 if (lookup_interface (class_name))
6912 #ifdef OBJCPLUS
6913 error ("duplicate interface declaration for class %qE", class_name);
6914 #else
6915 warning (0, "duplicate interface declaration for class %qE", class_name);
6916 #endif
6917 else
6918 add_interface (klass, class_name);
6920 if (protocol_list)
6921 CLASS_PROTOCOL_LIST (klass)
6922 = lookup_and_install_protocols (protocol_list, /* definition_required */ true);
6924 if (attributes)
6926 tree attribute;
6927 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
6929 tree name = TREE_PURPOSE (attribute);
6931 /* TODO: Document what the objc_exception attribute is/does. */
6932 /* We handle the 'deprecated' and (undocumented) 'objc_exception'
6933 attributes. */
6934 if (is_attribute_p ("deprecated", name))
6935 TREE_DEPRECATED (klass) = 1;
6936 else if (is_attribute_p ("objc_exception", name))
6937 CLASS_HAS_EXCEPTION_ATTR (klass) = 1;
6938 else
6939 /* Warn about and ignore all others for now, but store them. */
6940 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
6942 TYPE_ATTRIBUTES (klass) = attributes;
6944 break;
6946 case CATEGORY_INTERFACE_TYPE:
6948 tree class_category_is_assoc_with;
6950 /* For a category, class_name is really the name of the class that
6951 the following set of methods will be associated with. We must
6952 find the interface so that can derive the objects template. */
6953 if (!(class_category_is_assoc_with = lookup_interface (class_name)))
6955 error ("cannot find interface declaration for %qE",
6956 class_name);
6957 exit (FATAL_EXIT_CODE);
6959 else
6961 if (TREE_DEPRECATED (class_category_is_assoc_with))
6962 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
6963 class_name);
6965 if (super_name == NULL_TREE)
6967 /* This is a class extension. Get the original
6968 interface, and continue working on it. */
6969 objc_in_class_extension = true;
6970 klass = class_category_is_assoc_with;
6972 if (protocol_list)
6974 /* Append protocols to the original protocol
6975 list. */
6976 CLASS_PROTOCOL_LIST (klass)
6977 = chainon (CLASS_PROTOCOL_LIST (klass),
6978 lookup_and_install_protocols
6979 (protocol_list,
6980 /* definition_required */ true));
6983 else
6985 add_category (class_category_is_assoc_with, klass);
6987 if (protocol_list)
6988 CLASS_PROTOCOL_LIST (klass)
6989 = lookup_and_install_protocols
6990 (protocol_list, /* definition_required */ true);
6994 break;
6996 case CATEGORY_IMPLEMENTATION_TYPE:
6997 /* Reset for multiple classes per file. */
6998 method_slot = 0;
7000 objc_implementation_context = klass;
7002 /* For a category, class_name is really the name of the class that
7003 the following set of methods will be associated with. We must
7004 find the interface so that can derive the objects template. */
7006 if (!(implementation_template = lookup_interface (class_name)))
7008 error ("cannot find interface declaration for %qE",
7009 class_name);
7010 exit (FATAL_EXIT_CODE);
7012 break;
7013 default:
7014 gcc_unreachable ();
7016 return klass;
7019 static tree
7020 continue_class (tree klass)
7022 switch (TREE_CODE (klass))
7024 case CLASS_IMPLEMENTATION_TYPE:
7025 case CATEGORY_IMPLEMENTATION_TYPE:
7027 struct imp_entry *imp_entry;
7029 /* Check consistency of the instance variables. */
7031 if (CLASS_RAW_IVARS (klass))
7032 check_ivars (implementation_template, klass);
7034 /* code generation */
7035 #ifdef OBJCPLUS
7036 push_lang_context (lang_name_c);
7037 #endif
7038 build_private_template (implementation_template);
7039 uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template);
7040 objc_instance_type = build_pointer_type (uprivate_record);
7042 imp_entry = ggc_alloc<struct imp_entry> ();
7044 imp_entry->next = imp_list;
7045 imp_entry->imp_context = klass;
7046 imp_entry->imp_template = implementation_template;
7047 ucls_super_ref = uucls_super_ref = NULL;
7048 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7050 imp_entry->class_decl = (*runtime.class_decl) (klass);
7051 imp_entry->meta_decl = (*runtime.metaclass_decl) (klass);
7053 else
7055 imp_entry->class_decl = (*runtime.category_decl) (klass);
7056 imp_entry->meta_decl = NULL;
7058 imp_entry->has_cxx_cdtors = 0;
7060 /* Append to front and increment count. */
7061 imp_list = imp_entry;
7062 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7063 imp_count++;
7064 else
7065 cat_count++;
7066 #ifdef OBJCPLUS
7067 pop_lang_context ();
7068 #endif /* OBJCPLUS */
7070 return get_class_ivars (implementation_template, true);
7072 case CLASS_INTERFACE_TYPE:
7074 if (objc_in_class_extension)
7075 return NULL_TREE;
7076 #ifdef OBJCPLUS
7077 push_lang_context (lang_name_c);
7078 #endif /* OBJCPLUS */
7079 objc_collecting_ivars = 1;
7080 build_private_template (klass);
7081 objc_collecting_ivars = 0;
7082 #ifdef OBJCPLUS
7083 pop_lang_context ();
7084 #endif /* OBJCPLUS */
7085 return NULL_TREE;
7087 default:
7088 return error_mark_node;
7092 /* This routine builds name of the setter synthesized function. */
7093 char *
7094 objc_build_property_setter_name (tree ident)
7096 /* TODO: Use alloca to allocate buffer of appropriate size. */
7097 static char string[BUFSIZE];
7098 sprintf (string, "set%s:", IDENTIFIER_POINTER (ident));
7099 string[3] = TOUPPER (string[3]);
7100 return string;
7103 /* This routine prepares the declarations of the property accessor
7104 helper functions (objc_getProperty(), etc) that are used when
7105 @synthesize is used.
7107 runtime-specific routines are built in the respective runtime
7108 initialize functions. */
7109 static void
7110 build_common_objc_property_accessor_helpers (void)
7112 tree type;
7114 /* Declare the following function:
7116 objc_getProperty (id self, SEL _cmd,
7117 ptrdiff_t offset, BOOL is_atomic); */
7118 type = build_function_type_list (objc_object_type,
7119 objc_object_type,
7120 objc_selector_type,
7121 ptrdiff_type_node,
7122 boolean_type_node,
7123 NULL_TREE);
7124 objc_getProperty_decl = add_builtin_function ("objc_getProperty",
7125 type, 0, NOT_BUILT_IN,
7126 NULL, NULL_TREE);
7127 TREE_NOTHROW (objc_getProperty_decl) = 0;
7129 /* Declare the following function:
7130 void
7131 objc_setProperty (id self, SEL _cmd,
7132 ptrdiff_t offset, id new_value,
7133 BOOL is_atomic, BOOL should_copy); */
7134 type = build_function_type_list (void_type_node,
7135 objc_object_type,
7136 objc_selector_type,
7137 ptrdiff_type_node,
7138 objc_object_type,
7139 boolean_type_node,
7140 boolean_type_node,
7141 NULL_TREE);
7142 objc_setProperty_decl = add_builtin_function ("objc_setProperty",
7143 type, 0, NOT_BUILT_IN,
7144 NULL, NULL_TREE);
7145 TREE_NOTHROW (objc_setProperty_decl) = 0;
7148 /* This looks up an ivar in a class (including superclasses). */
7149 static tree
7150 lookup_ivar (tree interface, tree instance_variable_name)
7152 while (interface)
7154 tree decl_chain;
7156 for (decl_chain = CLASS_IVARS (interface); decl_chain; decl_chain = DECL_CHAIN (decl_chain))
7157 if (DECL_NAME (decl_chain) == instance_variable_name)
7158 return decl_chain;
7160 /* Not found. Search superclass if any. */
7161 if (CLASS_SUPER_NAME (interface))
7162 interface = lookup_interface (CLASS_SUPER_NAME (interface));
7165 return NULL_TREE;
7168 /* This routine synthesizes a 'getter' method. This is only called
7169 for @synthesize properties. */
7170 static void
7171 objc_synthesize_getter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
7173 location_t location = DECL_SOURCE_LOCATION (property);
7174 tree fn, decl;
7175 tree body;
7176 tree ret_val;
7178 /* If user has implemented a getter with same name then do nothing. */
7179 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7180 PROPERTY_GETTER_NAME (property)))
7181 return;
7183 /* Find declaration of the property getter in the interface (or
7184 superclass, or protocol). There must be one. */
7185 decl = lookup_method_static (klass, PROPERTY_GETTER_NAME (property), 0);
7187 /* If one not declared in the interface, this condition has already
7188 been reported as user error (because property was not declared in
7189 the interface). */
7190 if (!decl)
7191 return;
7193 /* Adapt the 'decl'. Use the source location of the @synthesize
7194 statement for error messages. */
7195 decl = copy_node (decl);
7196 DECL_SOURCE_LOCATION (decl) = location;
7198 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7199 NULL_TREE);
7200 body = c_begin_compound_stmt (true);
7202 /* Now we need to decide how we build the getter. There are three
7203 cases:
7205 for 'copy' or 'retain' properties we need to use the
7206 objc_getProperty() accessor helper which knows about retain and
7207 copy. It supports both 'nonatomic' and 'atomic' access.
7209 for 'nonatomic, assign' properties we can access the instance
7210 variable directly. 'nonatomic' means we don't have to use locks,
7211 and 'assign' means we don't have to worry about retain or copy.
7212 If you combine the two, it means we can just access the instance
7213 variable directly.
7215 for 'atomic, assign' properties we use objc_copyStruct() (for the
7216 next runtime) or objc_getPropertyStruct() (for the GNU runtime). */
7217 switch (PROPERTY_ASSIGN_SEMANTICS (property))
7219 case OBJC_PROPERTY_RETAIN:
7220 case OBJC_PROPERTY_COPY:
7222 /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */
7223 tree cmd, ivar, offset, is_atomic;
7224 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
7226 /* Find the ivar to compute the offset. */
7227 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7228 if (!ivar || is_private (ivar))
7230 /* This should never happen. */
7231 error_at (location,
7232 "cannot find instance variable associated with property");
7233 ret_val = error_mark_node;
7234 break;
7236 offset = byte_position (ivar);
7238 if (PROPERTY_NONATOMIC (property))
7239 is_atomic = boolean_false_node;
7240 else
7241 is_atomic = boolean_true_node;
7243 ret_val = build_function_call
7244 (location,
7245 /* Function prototype. */
7246 objc_getProperty_decl,
7247 /* Parameters. */
7248 tree_cons /* self */
7249 (NULL_TREE, self_decl,
7250 tree_cons /* _cmd */
7251 (NULL_TREE, cmd,
7252 tree_cons /* offset */
7253 (NULL_TREE, offset,
7254 tree_cons /* is_atomic */
7255 (NULL_TREE, is_atomic, NULL_TREE)))));
7257 break;
7258 case OBJC_PROPERTY_ASSIGN:
7259 if (PROPERTY_NONATOMIC (property))
7261 /* We build "return self->PROPERTY_IVAR_NAME;" */
7262 ret_val = objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property));
7263 break;
7265 else
7267 /* We build
7268 <property type> __objc_property_temp;
7269 objc_getPropertyStruct (&__objc_property_temp,
7270 &(self->PROPERTY_IVAR_NAME),
7271 sizeof (type of self->PROPERTY_IVAR_NAME),
7272 is_atomic,
7273 false)
7274 return __objc_property_temp;
7276 For the NeXT runtime, we need to use objc_copyStruct
7277 instead of objc_getPropertyStruct. */
7278 tree objc_property_temp_decl, function_decl, function_call;
7279 tree size_of, is_atomic;
7281 objc_property_temp_decl = objc_create_temporary_var (TREE_TYPE (property), "__objc_property_temp");
7282 DECL_SOURCE_LOCATION (objc_property_temp_decl) = location;
7283 objc_property_temp_decl = lang_hooks.decls.pushdecl (objc_property_temp_decl);
7285 /* sizeof (ivar type). Since the ivar and the property have
7286 the same type, there is no need to lookup the ivar. */
7287 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7288 true /* is_sizeof */,
7289 false /* min_alignof */,
7290 false /* complain */);
7292 if (PROPERTY_NONATOMIC (property))
7293 is_atomic = boolean_false_node;
7294 else
7295 is_atomic = boolean_true_node;
7297 if (objc_copyStruct_decl)
7298 function_decl = objc_copyStruct_decl;
7299 else
7300 function_decl = objc_getPropertyStruct_decl;
7302 function_call = build_function_call
7303 (location,
7304 /* Function prototype. */
7305 function_decl,
7306 /* Parameters. */
7307 tree_cons /* &__objc_property_temp_decl */
7308 /* Warning: note that using build_fold_addr_expr_loc()
7309 here causes invalid code to be generated. */
7310 (NULL_TREE, build_unary_op (location, ADDR_EXPR, objc_property_temp_decl, 0),
7311 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
7312 (NULL_TREE, build_fold_addr_expr_loc (location,
7313 objc_lookup_ivar
7314 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7315 tree_cons /* sizeof (PROPERTY_IVAR) */
7316 (NULL_TREE, size_of,
7317 tree_cons /* is_atomic */
7318 (NULL_TREE, is_atomic,
7319 /* TODO: This is currently ignored by the GNU
7320 runtime, but what about the next one ? */
7321 tree_cons /* has_strong */
7322 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7324 add_stmt (function_call);
7326 ret_val = objc_property_temp_decl;
7328 break;
7329 default:
7330 gcc_unreachable ();
7333 gcc_assert (ret_val);
7335 #ifdef OBJCPLUS
7336 finish_return_stmt (ret_val);
7337 #else
7338 c_finish_return (location, ret_val, NULL_TREE);
7339 #endif
7341 add_stmt (c_end_compound_stmt (location, body, true));
7342 fn = current_function_decl;
7343 #ifdef OBJCPLUS
7344 finish_function ();
7345 #endif
7346 objc_finish_method_definition (fn);
7349 /* This routine synthesizes a 'setter' method. */
7351 static void
7352 objc_synthesize_setter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
7354 location_t location = DECL_SOURCE_LOCATION (property);
7355 tree fn, decl;
7356 tree body;
7357 tree new_value, statement;
7359 /* If user has implemented a setter with same name then do nothing. */
7360 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7361 PROPERTY_SETTER_NAME (property)))
7362 return;
7364 /* Find declaration of the property setter in the interface (or
7365 superclass, or protocol). There must be one. */
7366 decl = lookup_method_static (klass, PROPERTY_SETTER_NAME (property), 0);
7368 /* If one not declared in the interface, this condition has already
7369 been reported as user error (because property was not declared in
7370 the interface). */
7371 if (!decl)
7372 return;
7374 /* Adapt the 'decl'. Use the source location of the @synthesize
7375 statement for error messages. */
7376 decl = copy_node (decl);
7377 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (property);
7379 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7380 NULL_TREE);
7382 body = c_begin_compound_stmt (true);
7384 /* The 'new_value' is the only argument to the method, which is the
7385 3rd argument of the function, after self and _cmd. We use twice
7386 TREE_CHAIN to move forward two arguments. */
7387 new_value = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)));
7389 /* This would presumably happen if the user has specified a
7390 prototype for the setter that does not have an argument! */
7391 if (new_value == NULL_TREE)
7393 /* TODO: This should be caught much earlier than this. */
7394 error_at (DECL_SOURCE_LOCATION (decl), "invalid setter, it must have one argument");
7395 /* Try to recover somehow. */
7396 new_value = error_mark_node;
7399 /* Now we need to decide how we build the setter. There are three
7400 cases:
7402 for 'copy' or 'retain' properties we need to use the
7403 objc_setProperty() accessor helper which knows about retain and
7404 copy. It supports both 'nonatomic' and 'atomic' access.
7406 for 'nonatomic, assign' properties we can access the instance
7407 variable directly. 'nonatomic' means we don't have to use locks,
7408 and 'assign' means we don't have to worry about retain or copy.
7409 If you combine the two, it means we can just access the instance
7410 variable directly.
7412 for 'atomic, assign' properties we use objc_copyStruct() (for the
7413 next runtime) or objc_setPropertyStruct() (for the GNU runtime). */
7414 switch (PROPERTY_ASSIGN_SEMANTICS (property))
7416 case OBJC_PROPERTY_RETAIN:
7417 case OBJC_PROPERTY_COPY:
7419 /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */
7420 tree cmd, ivar, offset, is_atomic, should_copy;
7421 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
7423 /* Find the ivar to compute the offset. */
7424 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7425 if (!ivar || is_private (ivar))
7427 error_at (location,
7428 "cannot find instance variable associated with property");
7429 statement = error_mark_node;
7430 break;
7432 offset = byte_position (ivar);
7434 if (PROPERTY_NONATOMIC (property))
7435 is_atomic = boolean_false_node;
7436 else
7437 is_atomic = boolean_true_node;
7439 if (PROPERTY_ASSIGN_SEMANTICS (property) == OBJC_PROPERTY_COPY)
7440 should_copy = boolean_true_node;
7441 else
7442 should_copy = boolean_false_node;
7444 statement = build_function_call
7445 (location,
7446 /* Function prototype. */
7447 objc_setProperty_decl,
7448 /* Parameters. */
7449 tree_cons /* self */
7450 (NULL_TREE, self_decl,
7451 tree_cons /* _cmd */
7452 (NULL_TREE, cmd,
7453 tree_cons /* offset */
7454 (NULL_TREE, offset,
7455 tree_cons /* new_value */
7456 (NULL_TREE, new_value,
7457 tree_cons /* is_atomic */
7458 (NULL_TREE, is_atomic,
7459 tree_cons /* should_copy */
7460 (NULL_TREE, should_copy, NULL_TREE)))))));
7462 break;
7463 case OBJC_PROPERTY_ASSIGN:
7464 if (PROPERTY_NONATOMIC (property))
7466 /* We build "self->PROPERTY_IVAR_NAME = new_value;" */
7467 statement = build_modify_expr
7468 (location,
7469 objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)),
7470 NULL_TREE, NOP_EXPR,
7471 location, new_value, NULL_TREE);
7472 break;
7474 else
7476 /* We build
7477 objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME),
7478 &new_value,
7479 sizeof (type of self->PROPERTY_IVAR_NAME),
7480 is_atomic,
7481 false)
7483 For the NeXT runtime, we need to use objc_copyStruct
7484 instead of objc_getPropertyStruct. */
7485 tree function_decl, size_of, is_atomic;
7487 /* sizeof (ivar type). Since the ivar and the property have
7488 the same type, there is no need to lookup the ivar. */
7489 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7490 true /* is_sizeof */,
7491 false /* min_alignof */,
7492 false /* complain */);
7494 if (PROPERTY_NONATOMIC (property))
7495 is_atomic = boolean_false_node;
7496 else
7497 is_atomic = boolean_true_node;
7499 if (objc_copyStruct_decl)
7500 function_decl = objc_copyStruct_decl;
7501 else
7502 function_decl = objc_setPropertyStruct_decl;
7504 statement = build_function_call
7505 (location,
7506 /* Function prototype. */
7507 function_decl,
7508 /* Parameters. */
7509 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
7510 (NULL_TREE, build_fold_addr_expr_loc (location,
7511 objc_lookup_ivar
7512 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7513 tree_cons /* &new_value */
7514 (NULL_TREE, build_fold_addr_expr_loc (location, new_value),
7515 tree_cons /* sizeof (PROPERTY_IVAR) */
7516 (NULL_TREE, size_of,
7517 tree_cons /* is_atomic */
7518 (NULL_TREE, is_atomic,
7519 /* TODO: This is currently ignored by the GNU
7520 runtime, but what about the next one ? */
7521 tree_cons /* has_strong */
7522 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7524 break;
7525 default:
7526 gcc_unreachable ();
7528 gcc_assert (statement);
7530 add_stmt (statement);
7531 add_stmt (c_end_compound_stmt (location, body, true));
7532 fn = current_function_decl;
7533 #ifdef OBJCPLUS
7534 finish_function ();
7535 #endif
7536 objc_finish_method_definition (fn);
7539 /* This function is a sub-routine of objc_add_synthesize_declaration.
7540 It is called for each property to synthesize once we have
7541 determined that the context is Ok. */
7542 static void
7543 objc_add_synthesize_declaration_for_property (location_t location, tree interface,
7544 tree property_name, tree ivar_name)
7546 /* Find the @property declaration. */
7547 tree property;
7548 tree x;
7550 /* Check that synthesize or dynamic has not already been used for
7551 the same property. */
7552 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7553 if (PROPERTY_NAME (property) == property_name)
7555 location_t original_location = DECL_SOURCE_LOCATION (property);
7557 if (PROPERTY_DYNAMIC (property))
7558 error_at (location, "property %qs already specified in %<@dynamic%>",
7559 IDENTIFIER_POINTER (property_name));
7560 else
7561 error_at (location, "property %qs already specified in %<@synthesize%>",
7562 IDENTIFIER_POINTER (property_name));
7564 if (original_location != UNKNOWN_LOCATION)
7565 inform (original_location, "originally specified here");
7566 return;
7569 /* Check that the property is declared in the interface. It could
7570 also be declared in a superclass or protocol. */
7571 property = lookup_property (interface, property_name);
7573 if (!property)
7575 error_at (location, "no declaration of property %qs found in the interface",
7576 IDENTIFIER_POINTER (property_name));
7577 return;
7579 else
7581 /* We have to copy the property, because we want to chain it to
7582 the implementation context, and we want to store the source
7583 location of the @synthesize, not of the original
7584 @property. */
7585 property = copy_node (property);
7586 DECL_SOURCE_LOCATION (property) = location;
7589 /* Determine PROPERTY_IVAR_NAME. */
7590 if (ivar_name == NULL_TREE)
7591 ivar_name = property_name;
7593 /* Check that the instance variable exists. You can only use an
7594 instance variable from the same class, not one from the
7595 superclass (this makes sense as it allows us to check that an
7596 instance variable is only used in one synthesized property). */
7598 tree ivar = is_ivar (CLASS_IVARS (interface), ivar_name);
7599 tree type_of_ivar;
7600 if (!ivar)
7602 error_at (location, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar",
7603 IDENTIFIER_POINTER (property_name));
7604 return;
7607 if (DECL_BIT_FIELD_TYPE (ivar))
7608 type_of_ivar = DECL_BIT_FIELD_TYPE (ivar);
7609 else
7610 type_of_ivar = TREE_TYPE (ivar);
7612 /* If the instance variable has a different C type, we throw an error ... */
7613 if (!comptypes (TREE_TYPE (property), type_of_ivar)
7614 /* ... unless the property is readonly, in which case we allow
7615 the instance variable to be more specialized (this means we
7616 can generate the getter all right and it works). */
7617 && (!PROPERTY_READONLY (property)
7618 || !objc_compare_types (TREE_TYPE (property),
7619 type_of_ivar, -5, NULL_TREE)))
7621 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7623 error_at (location, "property %qs is using instance variable %qs of incompatible type",
7624 IDENTIFIER_POINTER (property_name),
7625 IDENTIFIER_POINTER (ivar_name));
7627 if (original_location != UNKNOWN_LOCATION)
7628 inform (original_location, "originally specified here");
7631 /* If the instance variable is a bitfield, the property must be
7632 'assign', 'nonatomic' because the runtime getter/setter helper
7633 do not work with bitfield instance variables. */
7634 if (DECL_BIT_FIELD_TYPE (ivar))
7636 /* If there is an error, we return and not generate any
7637 getter/setter because trying to set up the runtime
7638 getter/setter helper calls with bitfields is at high risk
7639 of ICE. */
7641 if (PROPERTY_ASSIGN_SEMANTICS (property) != OBJC_PROPERTY_ASSIGN)
7643 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7645 error_at (location, "%<assign%> property %qs is using bit-field "
7646 "instance variable %qs",
7647 IDENTIFIER_POINTER (property_name),
7648 IDENTIFIER_POINTER (ivar_name));
7650 if (original_location != UNKNOWN_LOCATION)
7651 inform (original_location, "originally specified here");
7652 return;
7655 if (!PROPERTY_NONATOMIC (property))
7657 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7659 error_at (location, "%<atomic%> property %qs is using bit-field "
7660 "instance variable %qs",
7661 IDENTIFIER_POINTER (property_name),
7662 IDENTIFIER_POINTER (ivar_name));
7664 if (original_location != UNKNOWN_LOCATION)
7665 inform (original_location, "originally specified here");
7666 return;
7671 /* Check that no other property is using the same instance
7672 variable. */
7673 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7674 if (PROPERTY_IVAR_NAME (x) == ivar_name)
7676 location_t original_location = DECL_SOURCE_LOCATION (x);
7678 error_at (location, "property %qs is using the same instance variable as property %qs",
7679 IDENTIFIER_POINTER (property_name),
7680 IDENTIFIER_POINTER (PROPERTY_NAME (x)));
7682 if (original_location != UNKNOWN_LOCATION)
7683 inform (original_location, "originally specified here");
7685 /* We keep going on. This won't cause the compiler to fail;
7686 the failure would most likely be at runtime. */
7689 /* Note that a @synthesize (and only a @synthesize) always sets
7690 PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a
7691 @synthesize by that. */
7692 PROPERTY_IVAR_NAME (property) = ivar_name;
7694 /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the
7695 original declaration; they are always set (with the exception of
7696 PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */
7698 /* Add the property to the list of properties for current implementation. */
7699 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7700 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7702 /* Note how we don't actually synthesize the getter/setter here; it
7703 would be very natural, but we may miss the fact that the user has
7704 implemented his own getter/setter later on in the @implementation
7705 (in which case we shouldn't generate getter/setter). We wait
7706 until we have parsed it all before generating the code. */
7709 /* This function is called by the parser after a @synthesize
7710 expression is parsed. 'location' is the location of the
7711 @synthesize expression, and 'property_and_ivar_list' is a chained
7712 list of the property and ivar names. */
7713 void
7714 objc_add_synthesize_declaration (location_t location, tree property_and_ivar_list)
7716 tree interface, chain;
7718 if (flag_objc1_only)
7719 error_at (input_location, "%<@synthesize%> is not available in Objective-C 1.0");
7721 if (property_and_ivar_list == error_mark_node)
7722 return;
7724 if (!objc_implementation_context)
7726 /* We can get here only in Objective-C; the Objective-C++ parser
7727 detects the problem while parsing, outputs the error
7728 "misplaced '@synthesize' Objective-C++ construct" and skips
7729 the declaration. */
7730 error_at (location, "%<@synthesize%> not in @implementation context");
7731 return;
7734 if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE)
7736 error_at (location, "%<@synthesize%> cannot be used in categories");
7737 return;
7740 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7741 if (!interface)
7743 /* I can't see how this could happen, but it is good as a safety check. */
7744 error_at (location,
7745 "%<@synthesize%> requires the @interface of the class to be available");
7746 return;
7749 /* Now, iterate over the properties and do each of them. */
7750 for (chain = property_and_ivar_list; chain; chain = TREE_CHAIN (chain))
7752 objc_add_synthesize_declaration_for_property (location, interface, TREE_VALUE (chain),
7753 TREE_PURPOSE (chain));
7757 /* This function is a sub-routine of objc_add_dynamic_declaration. It
7758 is called for each property to mark as dynamic once we have
7759 determined that the context is Ok. */
7760 static void
7761 objc_add_dynamic_declaration_for_property (location_t location, tree interface,
7762 tree property_name)
7764 /* Find the @property declaration. */
7765 tree property;
7767 /* Check that synthesize or dynamic has not already been used for
7768 the same property. */
7769 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7770 if (PROPERTY_NAME (property) == property_name)
7772 location_t original_location = DECL_SOURCE_LOCATION (property);
7774 if (PROPERTY_DYNAMIC (property))
7775 error_at (location, "property %qs already specified in %<@dynamic%>",
7776 IDENTIFIER_POINTER (property_name));
7777 else
7778 error_at (location, "property %qs already specified in %<@synthesize%>",
7779 IDENTIFIER_POINTER (property_name));
7781 if (original_location != UNKNOWN_LOCATION)
7782 inform (original_location, "originally specified here");
7783 return;
7786 /* Check that the property is declared in the interface. It could
7787 also be declared in a superclass or protocol. */
7788 property = lookup_property (interface, property_name);
7790 if (!property)
7792 error_at (location, "no declaration of property %qs found in the interface",
7793 IDENTIFIER_POINTER (property_name));
7794 return;
7796 else
7798 /* We have to copy the property, because we want to chain it to
7799 the implementation context, and we want to store the source
7800 location of the @synthesize, not of the original
7801 @property. */
7802 property = copy_node (property);
7803 DECL_SOURCE_LOCATION (property) = location;
7806 /* Note that a @dynamic (and only a @dynamic) always sets
7807 PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that.
7808 (actually, as explained above, PROPERTY_DECL generated by
7809 @property and associated with a @dynamic property are also marked
7810 as PROPERTY_DYNAMIC). */
7811 PROPERTY_DYNAMIC (property) = 1;
7813 /* Add the property to the list of properties for current implementation. */
7814 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7815 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7818 /* This function is called by the parser after a @dynamic expression
7819 is parsed. 'location' is the location of the @dynamic expression,
7820 and 'property_list' is a chained list of all the property
7821 names. */
7822 void
7823 objc_add_dynamic_declaration (location_t location, tree property_list)
7825 tree interface, chain;
7827 if (flag_objc1_only)
7828 error_at (input_location, "%<@dynamic%> is not available in Objective-C 1.0");
7830 if (property_list == error_mark_node)
7831 return;
7833 if (!objc_implementation_context)
7835 /* We can get here only in Objective-C; the Objective-C++ parser
7836 detects the problem while parsing, outputs the error
7837 "misplaced '@dynamic' Objective-C++ construct" and skips the
7838 declaration. */
7839 error_at (location, "%<@dynamic%> not in @implementation context");
7840 return;
7843 /* @dynamic is allowed in categories. */
7844 switch (TREE_CODE (objc_implementation_context))
7846 case CLASS_IMPLEMENTATION_TYPE:
7847 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7848 break;
7849 case CATEGORY_IMPLEMENTATION_TYPE:
7850 interface = lookup_category (implementation_template,
7851 CLASS_SUPER_NAME (objc_implementation_context));
7852 break;
7853 default:
7854 gcc_unreachable ();
7857 if (!interface)
7859 /* I can't see how this could happen, but it is good as a safety check. */
7860 error_at (location,
7861 "%<@dynamic%> requires the @interface of the class to be available");
7862 return;
7865 /* Now, iterate over the properties and do each of them. */
7866 for (chain = property_list; chain; chain = TREE_CHAIN (chain))
7868 objc_add_dynamic_declaration_for_property (location, interface, TREE_VALUE (chain));
7872 /* Main routine to generate code/data for all the property information for
7873 current implementation (class or category). CLASS is the interface where
7874 ivars are declared. CLASS_METHODS is where methods are found which
7875 could be a class or a category depending on whether we are implementing
7876 property of a class or a category. */
7878 static void
7879 objc_gen_property_data (tree klass, tree class_methods)
7881 tree x;
7883 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7885 /* @dynamic property - nothing to check or synthesize. */
7886 if (PROPERTY_DYNAMIC (x))
7887 continue;
7889 /* @synthesize property - need to synthesize the accessors. */
7890 if (PROPERTY_IVAR_NAME (x))
7892 objc_synthesize_getter (klass, class_methods, x);
7894 if (PROPERTY_READONLY (x) == 0)
7895 objc_synthesize_setter (klass, class_methods, x);
7897 continue;
7900 gcc_unreachable ();
7904 /* This is called once we see the "@end" in an interface/implementation. */
7906 static void
7907 finish_class (tree klass)
7909 switch (TREE_CODE (klass))
7911 case CLASS_IMPLEMENTATION_TYPE:
7913 /* All metadata generation is done in runtime.generate_metadata(). */
7915 /* Generate what needed for property; setters, getters, etc. */
7916 objc_gen_property_data (implementation_template, implementation_template);
7918 if (implementation_template != objc_implementation_context)
7920 /* Ensure that all method listed in the interface contain bodies. */
7921 check_methods (CLASS_CLS_METHODS (implementation_template),
7922 objc_implementation_context, '+');
7923 check_methods (CLASS_NST_METHODS (implementation_template),
7924 objc_implementation_context, '-');
7926 if (CLASS_PROTOCOL_LIST (implementation_template))
7927 check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
7928 "class",
7929 CLASS_NAME (objc_implementation_context));
7931 break;
7933 case CATEGORY_IMPLEMENTATION_TYPE:
7935 tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass));
7937 if (category)
7939 /* Generate what needed for property; setters, getters, etc. */
7940 objc_gen_property_data (implementation_template, category);
7942 /* Ensure all method listed in the interface contain bodies. */
7943 check_methods (CLASS_CLS_METHODS (category),
7944 objc_implementation_context, '+');
7945 check_methods (CLASS_NST_METHODS (category),
7946 objc_implementation_context, '-');
7948 if (CLASS_PROTOCOL_LIST (category))
7949 check_protocols (CLASS_PROTOCOL_LIST (category),
7950 "category",
7951 CLASS_SUPER_NAME (objc_implementation_context));
7953 break;
7955 case CLASS_INTERFACE_TYPE:
7956 case CATEGORY_INTERFACE_TYPE:
7957 case PROTOCOL_INTERFACE_TYPE:
7959 /* Process properties of the class. */
7960 tree x;
7961 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
7963 /* Now we check that the appropriate getter is declared,
7964 and if not, we declare one ourselves. */
7965 tree getter_decl = lookup_method (CLASS_NST_METHODS (klass),
7966 PROPERTY_GETTER_NAME (x));
7968 if (getter_decl)
7970 /* TODO: Check that the declaration is consistent with the property. */
7973 else
7975 /* Generate an instance method declaration for the
7976 getter; for example "- (id) name;". In general it
7977 will be of the form
7978 -(type)property_getter_name; */
7979 tree rettype = build_tree_list (NULL_TREE, TREE_TYPE (x));
7980 getter_decl = build_method_decl (INSTANCE_METHOD_DECL,
7981 rettype, PROPERTY_GETTER_NAME (x),
7982 NULL_TREE, false);
7983 if (PROPERTY_OPTIONAL (x))
7984 objc_add_method (objc_interface_context, getter_decl, false, true);
7985 else
7986 objc_add_method (objc_interface_context, getter_decl, false, false);
7987 TREE_DEPRECATED (getter_decl) = TREE_DEPRECATED (x);
7988 METHOD_PROPERTY_CONTEXT (getter_decl) = x;
7991 if (PROPERTY_READONLY (x) == 0)
7993 /* Now we check that the appropriate setter is declared,
7994 and if not, we declare on ourselves. */
7995 tree setter_decl = lookup_method (CLASS_NST_METHODS (klass),
7996 PROPERTY_SETTER_NAME (x));
7998 if (setter_decl)
8000 /* TODO: Check that the declaration is consistent with the property. */
8003 else
8005 /* The setter name is something like 'setName:'.
8006 We need the substring 'setName' to build the
8007 method declaration due to how the declaration
8008 works. TODO: build_method_decl() will then
8009 generate back 'setName:' from 'setName'; it
8010 would be more efficient to hook into there. */
8011 const char *full_setter_name = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x));
8012 size_t length = strlen (full_setter_name);
8013 char *setter_name = (char *) alloca (length);
8014 tree ret_type, selector, arg_type, arg_name;
8016 memcpy (setter_name, full_setter_name, length - 1);
8017 setter_name[length - 1] = '\0';
8018 ret_type = build_tree_list (NULL_TREE, void_type_node);
8019 arg_type = build_tree_list (NULL_TREE, TREE_TYPE (x));
8020 arg_name = get_identifier ("_value");
8021 selector = objc_build_keyword_decl (get_identifier (setter_name),
8022 arg_type, arg_name, NULL);
8023 setter_decl = build_method_decl (INSTANCE_METHOD_DECL,
8024 ret_type, selector,
8025 build_tree_list (NULL_TREE, NULL_TREE),
8026 false);
8027 if (PROPERTY_OPTIONAL (x))
8028 objc_add_method (objc_interface_context, setter_decl, false, true);
8029 else
8030 objc_add_method (objc_interface_context, setter_decl, false, false);
8031 TREE_DEPRECATED (setter_decl) = TREE_DEPRECATED (x);
8032 METHOD_PROPERTY_CONTEXT (setter_decl) = x;
8036 break;
8038 default:
8039 gcc_unreachable ();
8040 break;
8044 static tree
8045 add_protocol (tree protocol)
8047 /* Put protocol on list in reverse order. */
8048 TREE_CHAIN (protocol) = protocol_chain;
8049 protocol_chain = protocol;
8050 return protocol_chain;
8053 /* Check that a protocol is defined, and, recursively, that all
8054 protocols that this protocol conforms to are defined too. */
8055 static void
8056 check_that_protocol_is_defined (tree protocol)
8058 if (!PROTOCOL_DEFINED (protocol))
8059 warning (0, "definition of protocol %qE not found",
8060 PROTOCOL_NAME (protocol));
8062 /* If the protocol itself conforms to other protocols, check them
8063 too, recursively. */
8064 if (PROTOCOL_LIST (protocol))
8066 tree p;
8068 for (p = PROTOCOL_LIST (protocol); p; p = TREE_CHAIN (p))
8069 check_that_protocol_is_defined (TREE_VALUE (p));
8073 /* Looks up a protocol. If 'warn_if_deprecated' is true, a warning is
8074 emitted if the protocol is deprecated. If 'definition_required' is
8075 true, a warning is emitted if a full @protocol definition has not
8076 been seen. */
8077 static tree
8078 lookup_protocol (tree ident, bool warn_if_deprecated, bool definition_required)
8080 tree chain;
8082 for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
8083 if (ident == PROTOCOL_NAME (chain))
8085 if (warn_if_deprecated && TREE_DEPRECATED (chain))
8087 /* It would be nice to use warn_deprecated_use() here, but
8088 we are using TREE_CHAIN (which is supposed to be the
8089 TYPE_STUB_DECL for a TYPE) for something different. */
8090 warning (OPT_Wdeprecated_declarations, "protocol %qE is deprecated",
8091 PROTOCOL_NAME (chain));
8094 if (definition_required)
8095 check_that_protocol_is_defined (chain);
8097 return chain;
8100 return NULL_TREE;
8103 /* This function forward declares the protocols named by NAMES. If
8104 they are already declared or defined, the function has no effect. */
8106 void
8107 objc_declare_protocol (tree name, tree attributes)
8109 bool deprecated = false;
8111 #ifdef OBJCPLUS
8112 if (current_namespace != global_namespace) {
8113 error ("Objective-C declarations may only appear in global scope");
8115 #endif /* OBJCPLUS */
8117 /* Determine if 'deprecated', the only attribute we recognize for
8118 protocols, was used. Ignore all other attributes. */
8119 if (attributes)
8121 tree attribute;
8122 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8124 tree name = TREE_PURPOSE (attribute);
8126 if (is_attribute_p ("deprecated", name))
8127 deprecated = true;
8128 else
8129 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8133 if (lookup_protocol (name, /* warn if deprecated */ false,
8134 /* definition_required */ false) == NULL_TREE)
8136 tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
8138 TYPE_LANG_SLOT_1 (protocol)
8139 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8140 PROTOCOL_NAME (protocol) = name;
8141 PROTOCOL_LIST (protocol) = NULL_TREE;
8142 add_protocol (protocol);
8143 PROTOCOL_DEFINED (protocol) = 0;
8144 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8146 if (attributes)
8148 /* TODO: Do we need to store the attributes here ? */
8149 TYPE_ATTRIBUTES (protocol) = attributes;
8150 if (deprecated)
8151 TREE_DEPRECATED (protocol) = 1;
8156 static tree
8157 start_protocol (enum tree_code code, tree name, tree list, tree attributes)
8159 tree protocol;
8160 bool deprecated = false;
8162 #ifdef OBJCPLUS
8163 if (current_namespace != global_namespace) {
8164 error ("Objective-C declarations may only appear in global scope");
8166 #endif /* OBJCPLUS */
8168 /* Determine if 'deprecated', the only attribute we recognize for
8169 protocols, was used. Ignore all other attributes. */
8170 if (attributes)
8172 tree attribute;
8173 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8175 tree name = TREE_PURPOSE (attribute);
8177 if (is_attribute_p ("deprecated", name))
8178 deprecated = true;
8179 else
8180 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8184 protocol = lookup_protocol (name, /* warn_if_deprecated */ false,
8185 /* definition_required */ false);
8187 if (!protocol)
8189 protocol = make_node (code);
8190 TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8192 PROTOCOL_NAME (protocol) = name;
8193 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8194 add_protocol (protocol);
8195 PROTOCOL_DEFINED (protocol) = 1;
8196 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8198 check_protocol_recursively (protocol, list);
8200 else if (! PROTOCOL_DEFINED (protocol))
8202 PROTOCOL_DEFINED (protocol) = 1;
8203 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8205 check_protocol_recursively (protocol, list);
8207 else
8209 warning (0, "duplicate declaration for protocol %qE",
8210 name);
8213 if (attributes)
8215 TYPE_ATTRIBUTES (protocol) = attributes;
8216 if (deprecated)
8217 TREE_DEPRECATED (protocol) = 1;
8220 return protocol;
8223 /* Decay array and function parameters into pointers. */
8225 static tree
8226 objc_decay_parm_type (tree type)
8228 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE)
8229 type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE
8230 ? TREE_TYPE (type)
8231 : type);
8233 return type;
8236 static GTY(()) tree objc_parmlist = NULL_TREE;
8238 /* Append PARM to a list of formal parameters of a method, making a necessary
8239 array-to-pointer adjustment along the way. */
8241 void
8242 objc_push_parm (tree parm)
8244 tree type;
8246 if (TREE_TYPE (parm) == error_mark_node)
8248 objc_parmlist = chainon (objc_parmlist, parm);
8249 return;
8252 /* Decay arrays and functions into pointers. */
8253 type = objc_decay_parm_type (TREE_TYPE (parm));
8255 /* If the parameter type has been decayed, a new PARM_DECL needs to be
8256 built as well. */
8257 if (type != TREE_TYPE (parm))
8258 parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type);
8260 DECL_ARG_TYPE (parm)
8261 = lang_hooks.types.type_promotes_to (TREE_TYPE (parm));
8263 /* Record constancy and volatility. */
8264 c_apply_type_quals_to_decl
8265 ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0)
8266 | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0)
8267 | (TYPE_ATOMIC (TREE_TYPE (parm)) ? TYPE_QUAL_ATOMIC : 0)
8268 | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm);
8270 objc_parmlist = chainon (objc_parmlist, parm);
8273 /* Retrieve the formal parameter list constructed via preceding calls to
8274 objc_push_parm(). */
8276 #ifdef OBJCPLUS
8277 tree
8278 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED,
8279 tree expr ATTRIBUTE_UNUSED)
8281 tree parm_info = objc_parmlist;
8282 objc_parmlist = NULL_TREE;
8284 return parm_info;
8286 #else
8287 struct c_arg_info *
8288 objc_get_parm_info (int have_ellipsis, tree expr)
8290 tree parm_info = objc_parmlist;
8291 struct c_arg_info *arg_info;
8292 /* The C front-end requires an elaborate song and dance at
8293 this point. */
8294 push_scope ();
8295 declare_parm_level ();
8296 while (parm_info)
8298 tree next = DECL_CHAIN (parm_info);
8300 DECL_CHAIN (parm_info) = NULL_TREE;
8301 parm_info = pushdecl (parm_info);
8302 finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
8303 parm_info = next;
8305 arg_info = get_parm_info (have_ellipsis, expr);
8306 pop_scope ();
8307 objc_parmlist = NULL_TREE;
8308 return arg_info;
8310 #endif
8312 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
8313 method definitions. In the case of instance methods, we can be more
8314 specific as to the type of 'self'. */
8316 static void
8317 synth_self_and_ucmd_args (void)
8319 tree self_type;
8321 if (objc_method_context
8322 && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL)
8323 self_type = objc_instance_type;
8324 else
8325 /* Really a `struct objc_class *'. However, we allow people to
8326 assign to self, which changes its type midstream. */
8327 self_type = objc_object_type;
8329 /* id self; */
8330 objc_push_parm (build_decl (input_location,
8331 PARM_DECL, self_id, self_type));
8333 /* SEL _cmd; */
8334 objc_push_parm (build_decl (input_location,
8335 PARM_DECL, ucmd_id, objc_selector_type));
8338 /* Transform an Objective-C method definition into a static C function
8339 definition, synthesizing the first two arguments, "self" and "_cmd",
8340 in the process. EXPR is NULL or an expression that needs to be
8341 evaluated for the side effects of array size expressions in the
8342 parameters. */
8344 static void
8345 start_method_def (tree method, tree expr)
8347 tree parmlist;
8348 #ifdef OBJCPLUS
8349 tree parm_info;
8350 #else
8351 struct c_arg_info *parm_info;
8352 #endif
8353 int have_ellipsis = 0;
8355 /* If we are defining a "dealloc" method in a non-root class, we
8356 will need to check if a [super dealloc] is missing, and warn if
8357 it is. */
8358 if(CLASS_SUPER_NAME (objc_implementation_context)
8359 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method))))
8360 should_call_super_dealloc = 1;
8361 else
8362 should_call_super_dealloc = 0;
8364 /* Required to implement _msgSuper. */
8365 objc_method_context = method;
8366 UOBJC_SUPER_decl = NULL_TREE;
8368 /* Generate prototype declarations for arguments..."new-style". */
8369 synth_self_and_ucmd_args ();
8371 /* Generate argument declarations if a keyword_decl. */
8372 parmlist = METHOD_SEL_ARGS (method);
8373 while (parmlist)
8375 /* parmlist is a KEYWORD_DECL. */
8376 tree type = TREE_VALUE (TREE_TYPE (parmlist));
8377 tree parm;
8379 parm = build_decl (input_location,
8380 PARM_DECL, KEYWORD_ARG_NAME (parmlist), type);
8381 decl_attributes (&parm, DECL_ATTRIBUTES (parmlist), 0);
8382 objc_push_parm (parm);
8383 parmlist = DECL_CHAIN (parmlist);
8386 if (METHOD_ADD_ARGS (method))
8388 tree akey;
8390 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method));
8391 akey; akey = TREE_CHAIN (akey))
8393 objc_push_parm (TREE_VALUE (akey));
8396 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
8397 have_ellipsis = 1;
8400 parm_info = objc_get_parm_info (have_ellipsis, expr);
8402 really_start_method (objc_method_context, parm_info);
8405 /* Return 1 if TYPE1 is equivalent to TYPE2 for purposes of method
8406 overloading. */
8407 static int
8408 objc_types_are_equivalent (tree type1, tree type2)
8410 if (type1 == type2)
8411 return 1;
8413 /* Strip away indirections. */
8414 while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE)
8415 && (TREE_CODE (type1) == TREE_CODE (type2)))
8416 type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2);
8417 if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
8418 return 0;
8420 /* Compare the protocol lists. */
8421 type1 = (TYPE_HAS_OBJC_INFO (type1)
8422 ? TYPE_OBJC_PROTOCOL_LIST (type1)
8423 : NULL_TREE);
8424 type2 = (TYPE_HAS_OBJC_INFO (type2)
8425 ? TYPE_OBJC_PROTOCOL_LIST (type2)
8426 : NULL_TREE);
8428 /* If there are no protocols (most common case), the types are
8429 identical. */
8430 if (type1 == NULL_TREE && type2 == NULL_TREE)
8431 return 1;
8433 /* If one has protocols, and the other one hasn't, they are not
8434 identical. */
8435 if ((type1 == NULL_TREE && type2 != NULL_TREE)
8436 || (type1 != NULL_TREE && type2 == NULL_TREE))
8437 return 0;
8438 else
8440 /* Else, both have protocols, and we need to do the full
8441 comparison. It is possible that either type1 or type2
8442 contain some duplicate protocols in the list, so we can't
8443 even just compare list_length as a first check. */
8444 tree t;
8446 for (t = type2; t; t = TREE_CHAIN (t))
8447 if (!lookup_protocol_in_reflist (type1, TREE_VALUE (t)))
8448 return 0;
8450 for (t = type1; t; t = TREE_CHAIN (t))
8451 if (!lookup_protocol_in_reflist (type2, TREE_VALUE (t)))
8452 return 0;
8454 return 1;
8458 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
8460 static int
8461 objc_types_share_size_and_alignment (tree type1, tree type2)
8463 return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
8464 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2));
8467 /* Return 1 if PROTO1 is equivalent to PROTO2
8468 for purposes of method overloading. Ordinarily, the type signatures
8469 should match up exactly, unless STRICT is zero, in which case we
8470 shall allow differences in which the size and alignment of a type
8471 is the same. */
8473 static int
8474 comp_proto_with_proto (tree proto1, tree proto2, int strict)
8476 tree type1, type2;
8478 /* The following test is needed in case there are hashing
8479 collisions. */
8480 if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2))
8481 return 0;
8483 /* Compare return types. */
8484 type1 = TREE_VALUE (TREE_TYPE (proto1));
8485 type2 = TREE_VALUE (TREE_TYPE (proto2));
8487 if (!objc_types_are_equivalent (type1, type2)
8488 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8489 return 0;
8491 /* Compare argument types. */
8493 /* The first argument (objc_object_type) is always the same, no need
8494 to compare. */
8496 /* The second argument (objc_selector_type) is always the same, no
8497 need to compare. */
8499 /* Compare the other arguments. */
8501 tree arg1, arg2;
8503 /* Compare METHOD_SEL_ARGS. */
8504 for (arg1 = METHOD_SEL_ARGS (proto1), arg2 = METHOD_SEL_ARGS (proto2);
8505 arg1 && arg2;
8506 arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2))
8508 type1 = TREE_VALUE (TREE_TYPE (arg1));
8509 type2 = TREE_VALUE (TREE_TYPE (arg2));
8511 /* FIXME: Do we need to decay argument types to compare them ? */
8512 type1 = objc_decay_parm_type (type1);
8513 type2 = objc_decay_parm_type (type2);
8515 if (!objc_types_are_equivalent (type1, type2)
8516 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8517 return 0;
8520 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8521 both NULL. */
8522 if (arg1 != arg2)
8523 return 0;
8525 /* Compare METHOD_ADD_ARGS. */
8526 if ((METHOD_ADD_ARGS (proto1) && !METHOD_ADD_ARGS (proto2))
8527 || (METHOD_ADD_ARGS (proto2) && !METHOD_ADD_ARGS (proto1)))
8528 return 0;
8530 if (METHOD_ADD_ARGS (proto1))
8532 for (arg1 = TREE_CHAIN (METHOD_ADD_ARGS (proto1)), arg2 = TREE_CHAIN (METHOD_ADD_ARGS (proto2));
8533 arg1 && arg2;
8534 arg1 = TREE_CHAIN (arg1), arg2 = TREE_CHAIN (arg2))
8536 type1 = TREE_TYPE (TREE_VALUE (arg1));
8537 type2 = TREE_TYPE (TREE_VALUE (arg2));
8539 /* FIXME: Do we need to decay argument types to compare them ? */
8540 type1 = objc_decay_parm_type (type1);
8541 type2 = objc_decay_parm_type (type2);
8543 if (!objc_types_are_equivalent (type1, type2)
8544 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8545 return 0;
8549 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8550 both NULL. */
8551 if (arg1 != arg2)
8552 return 0;
8554 /* Compare METHOD_ADD_ARGS_ELLIPSIS_P. */
8555 if (METHOD_ADD_ARGS_ELLIPSIS_P (proto1) != METHOD_ADD_ARGS_ELLIPSIS_P (proto2))
8556 return 0;
8559 /* Success. */
8560 return 1;
8563 /* This routine returns true if TYPE is a valid objc object type,
8564 suitable for messaging; false otherwise. If 'accept_class' is
8565 'true', then a Class object is considered valid for messaging and
8566 'true' is returned if 'type' refers to a Class. If 'accept_class'
8567 is 'false', then a Class object is not considered valid for
8568 messaging and 'false' is returned in that case. */
8570 static bool
8571 objc_type_valid_for_messaging (tree type, bool accept_classes)
8573 if (!POINTER_TYPE_P (type))
8574 return false;
8576 /* Remove the pointer indirection; don't remove more than one
8577 otherwise we'd consider "NSObject **" a valid type for messaging,
8578 which it isn't. */
8579 type = TREE_TYPE (type);
8581 if (TREE_CODE (type) != RECORD_TYPE)
8582 return false;
8584 if (objc_is_object_id (type))
8585 return true;
8587 if (objc_is_class_id (type))
8588 return accept_classes;
8590 if (TYPE_HAS_OBJC_INFO (type))
8591 return true;
8593 return false;
8596 void
8597 objc_start_function (tree name, tree type, tree attrs,
8598 #ifdef OBJCPLUS
8599 tree params
8600 #else
8601 struct c_arg_info *params
8602 #endif
8605 tree fndecl = build_decl (input_location,
8606 FUNCTION_DECL, name, type);
8608 #ifdef OBJCPLUS
8609 DECL_ARGUMENTS (fndecl) = params;
8610 DECL_INITIAL (fndecl) = error_mark_node;
8611 DECL_EXTERNAL (fndecl) = 0;
8612 TREE_STATIC (fndecl) = 1;
8613 retrofit_lang_decl (fndecl);
8614 cplus_decl_attributes (&fndecl, attrs, 0);
8615 start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT);
8616 #else
8617 current_function_returns_value = 0; /* Assume, until we see it does. */
8618 current_function_returns_null = 0;
8619 decl_attributes (&fndecl, attrs, 0);
8620 announce_function (fndecl);
8621 DECL_INITIAL (fndecl) = error_mark_node;
8622 DECL_EXTERNAL (fndecl) = 0;
8623 TREE_STATIC (fndecl) = 1;
8624 current_function_decl = pushdecl (fndecl);
8625 push_scope ();
8626 declare_parm_level ();
8627 DECL_RESULT (current_function_decl)
8628 = build_decl (input_location,
8629 RESULT_DECL, NULL_TREE,
8630 TREE_TYPE (TREE_TYPE (current_function_decl)));
8631 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1;
8632 DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1;
8633 start_fname_decls ();
8634 store_parm_decls_from (params);
8635 #endif
8637 TREE_USED (current_function_decl) = 1;
8640 /* - Generate an identifier for the function. the format is "_n_cls",
8641 where 1 <= n <= nMethods, and cls is the name the implementation we
8642 are processing.
8643 - Install the return type from the method declaration.
8644 - If we have a prototype, check for type consistency. */
8646 static void
8647 really_start_method (tree method,
8648 #ifdef OBJCPLUS
8649 tree parmlist
8650 #else
8651 struct c_arg_info *parmlist
8652 #endif
8655 tree ret_type, meth_type;
8656 tree method_id;
8657 const char *sel_name, *class_name, *cat_name;
8658 char *buf;
8660 /* Synth the storage class & assemble the return type. */
8661 ret_type = TREE_VALUE (TREE_TYPE (method));
8663 sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
8664 class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
8665 cat_name = ((TREE_CODE (objc_implementation_context)
8666 == CLASS_IMPLEMENTATION_TYPE)
8667 ? NULL
8668 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context)));
8669 method_slot++;
8671 /* Make sure this is big enough for any plausible method label. */
8672 buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
8673 + (cat_name ? strlen (cat_name) : 0));
8675 OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
8676 class_name, cat_name, sel_name, method_slot);
8678 method_id = get_identifier (buf);
8680 #ifdef OBJCPLUS
8681 /* Objective-C methods cannot be overloaded, so we don't need
8682 the type encoding appended. It looks bad anyway... */
8683 push_lang_context (lang_name_c);
8684 #endif
8686 meth_type = build_function_type_for_method (ret_type, method, METHOD_DEF, 0);
8687 objc_start_function (method_id, meth_type, NULL_TREE, parmlist);
8689 /* Set self_decl from the first argument. */
8690 self_decl = DECL_ARGUMENTS (current_function_decl);
8692 /* Suppress unused warnings. */
8693 TREE_USED (self_decl) = 1;
8694 DECL_READ_P (self_decl) = 1;
8695 TREE_USED (DECL_CHAIN (self_decl)) = 1;
8696 DECL_READ_P (DECL_CHAIN (self_decl)) = 1;
8697 #ifdef OBJCPLUS
8698 pop_lang_context ();
8699 #endif
8701 METHOD_DEFINITION (method) = current_function_decl;
8703 /* Check consistency...start_function, pushdecl, duplicate_decls. */
8705 if (implementation_template != objc_implementation_context)
8707 tree proto
8708 = lookup_method_static (implementation_template,
8709 METHOD_SEL_NAME (method),
8710 ((TREE_CODE (method) == CLASS_METHOD_DECL)
8711 | OBJC_LOOKUP_NO_SUPER));
8713 if (proto)
8715 if (!comp_proto_with_proto (method, proto, 1))
8717 bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL;
8719 warning_at (DECL_SOURCE_LOCATION (method), 0,
8720 "conflicting types for %<%c%s%>",
8721 (type ? '-' : '+'),
8722 identifier_to_locale (gen_method_decl (method)));
8723 inform (DECL_SOURCE_LOCATION (proto),
8724 "previous declaration of %<%c%s%>",
8725 (type ? '-' : '+'),
8726 identifier_to_locale (gen_method_decl (proto)));
8728 else
8730 /* If the method in the @interface was deprecated, mark
8731 the implemented method as deprecated too. It should
8732 never be used for messaging (when the deprecation
8733 warnings are produced), but just in case. */
8734 if (TREE_DEPRECATED (proto))
8735 TREE_DEPRECATED (method) = 1;
8737 /* If the method in the @interface was marked as
8738 'noreturn', mark the function implementing the method
8739 as 'noreturn' too. */
8740 TREE_THIS_VOLATILE (current_function_decl) = TREE_THIS_VOLATILE (proto);
8743 else
8745 /* We have a method @implementation even though we did not
8746 see a corresponding @interface declaration (which is allowed
8747 by Objective-C rules). Go ahead and place the method in
8748 the @interface anyway, so that message dispatch lookups
8749 will see it. */
8750 tree interface = implementation_template;
8752 if (TREE_CODE (objc_implementation_context)
8753 == CATEGORY_IMPLEMENTATION_TYPE)
8754 interface = lookup_category
8755 (interface,
8756 CLASS_SUPER_NAME (objc_implementation_context));
8758 if (interface)
8759 objc_add_method (interface, copy_node (method),
8760 TREE_CODE (method) == CLASS_METHOD_DECL,
8761 /* is_optional= */ false);
8766 static void *UOBJC_SUPER_scope = 0;
8768 /* _n_Method (id self, SEL sel, ...)
8770 struct objc_super _S;
8771 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
8772 } */
8774 static tree
8775 get_super_receiver (void)
8777 if (objc_method_context)
8779 tree super_expr, super_expr_list, class_expr;
8780 bool inst_meth;
8781 if (!UOBJC_SUPER_decl)
8783 UOBJC_SUPER_decl = build_decl (input_location,
8784 VAR_DECL, get_identifier (TAG_SUPER),
8785 objc_super_template);
8786 /* This prevents `unused variable' warnings when compiling with -Wall. */
8787 TREE_USED (UOBJC_SUPER_decl) = 1;
8788 DECL_READ_P (UOBJC_SUPER_decl) = 1;
8789 lang_hooks.decls.pushdecl (UOBJC_SUPER_decl);
8790 finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE,
8791 NULL_TREE);
8792 UOBJC_SUPER_scope = objc_get_current_scope ();
8795 /* Set receiver to self. */
8796 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id);
8797 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8798 NOP_EXPR, input_location, self_decl,
8799 NULL_TREE);
8800 super_expr_list = super_expr;
8802 /* Set class to begin searching. */
8803 /* Get the ident for the superclass class field & build a ref to it.
8804 ??? maybe we should just name the field the same for all runtimes. */
8805 super_expr = (*runtime.super_superclassfield_ident) ();
8806 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, super_expr);
8808 gcc_assert (imp_list->imp_context == objc_implementation_context
8809 && imp_list->imp_template == implementation_template);
8810 inst_meth = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL);
8812 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
8813 class_expr = (*runtime.get_class_super_ref) (input_location,
8814 imp_list, inst_meth);
8815 else
8816 /* We have a category. */
8818 tree super_name = CLASS_SUPER_NAME (imp_list->imp_template);
8819 tree super_class;
8821 /* Barf if super used in a category of a root object. */
8822 if (!super_name)
8824 error ("no super class declared in interface for %qE",
8825 CLASS_NAME (imp_list->imp_template));
8826 return error_mark_node;
8829 super_class = (*runtime.get_category_super_ref) (input_location,
8830 imp_list, inst_meth);
8831 class_expr = build_c_cast (input_location,
8832 TREE_TYPE (super_expr), super_class);
8835 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8836 NOP_EXPR,
8837 input_location, class_expr, NULL_TREE);
8839 super_expr_list = build_compound_expr (input_location,
8840 super_expr_list, super_expr);
8842 super_expr = build_unary_op (input_location,
8843 ADDR_EXPR, UOBJC_SUPER_decl, 0);
8844 super_expr_list = build_compound_expr (input_location,
8845 super_expr_list, super_expr);
8847 return super_expr_list;
8849 else
8851 error ("[super ...] must appear in a method context");
8852 return error_mark_node;
8856 /* When exiting a scope, sever links to a 'super' declaration (if any)
8857 therein contained. */
8859 void
8860 objc_clear_super_receiver (void)
8862 if (objc_method_context
8863 && UOBJC_SUPER_scope == objc_get_current_scope ())
8865 UOBJC_SUPER_decl = 0;
8866 UOBJC_SUPER_scope = 0;
8870 void
8871 objc_finish_method_definition (tree fndecl)
8873 /* We cannot validly inline ObjC methods, at least not without a language
8874 extension to declare that a method need not be dynamically
8875 dispatched, so suppress all thoughts of doing so. */
8876 DECL_UNINLINABLE (fndecl) = 1;
8878 #ifndef OBJCPLUS
8879 /* The C++ front-end will have called finish_function() for us. */
8880 finish_function ();
8881 #endif
8883 METHOD_ENCODING (objc_method_context)
8884 = encode_method_prototype (objc_method_context);
8886 /* Required to implement _msgSuper. This must be done AFTER finish_function,
8887 since the optimizer may find "may be used before set" errors. */
8888 objc_method_context = NULL_TREE;
8890 if (should_call_super_dealloc)
8891 warning (0, "method possibly missing a [super dealloc] call");
8894 /* Given a tree DECL node, produce a printable description of it in the given
8895 buffer, overwriting the buffer. */
8897 static char *
8898 gen_declaration (tree decl)
8900 errbuf[0] = '\0';
8902 if (DECL_P (decl))
8904 gen_type_name_0 (TREE_TYPE (decl));
8906 if (DECL_NAME (decl))
8908 if (!POINTER_TYPE_P (TREE_TYPE (decl)))
8909 strcat (errbuf, " ");
8911 strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
8914 #ifdef OBJCPLUS
8915 tree w = DECL_BIT_FIELD_REPRESENTATIVE (decl);
8916 #else
8917 tree w = DECL_INITIAL (decl);
8918 #endif
8919 if (w)
8921 STRIP_ANY_LOCATION_WRAPPER (w);
8922 if (TREE_CODE (w) == INTEGER_CST)
8923 sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
8924 TREE_INT_CST_LOW (w));
8928 return errbuf;
8931 /* Given a tree TYPE node, produce a printable description of it in the given
8932 buffer, overwriting the buffer. */
8934 static char *
8935 gen_type_name_0 (tree type)
8937 tree orig = type, proto;
8939 if (TYPE_P (type) && TYPE_NAME (type))
8940 type = TYPE_NAME (type);
8941 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
8943 tree inner = TREE_TYPE (type);
8945 while (TREE_CODE (inner) == ARRAY_TYPE)
8946 inner = TREE_TYPE (inner);
8948 gen_type_name_0 (inner);
8950 if (!POINTER_TYPE_P (inner))
8951 strcat (errbuf, " ");
8953 if (POINTER_TYPE_P (type))
8954 strcat (errbuf, "*");
8955 else
8956 while (type != inner)
8958 strcat (errbuf, "[");
8960 if (TYPE_DOMAIN (type))
8962 char sz[20];
8964 sprintf (sz, HOST_WIDE_INT_PRINT_DEC,
8965 (TREE_INT_CST_LOW
8966 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1));
8967 strcat (errbuf, sz);
8970 strcat (errbuf, "]");
8971 type = TREE_TYPE (type);
8974 goto exit_function;
8977 if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type))
8978 type = DECL_NAME (type);
8980 strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE
8981 ? IDENTIFIER_POINTER (type)
8982 : "");
8984 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
8985 if (objc_is_id (orig))
8986 orig = TREE_TYPE (orig);
8988 proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE;
8990 if (proto)
8992 strcat (errbuf, " <");
8994 while (proto) {
8995 strcat (errbuf,
8996 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto))));
8997 proto = TREE_CHAIN (proto);
8998 strcat (errbuf, proto ? ", " : ">");
9002 exit_function:
9003 return errbuf;
9006 static char *
9007 gen_type_name (tree type)
9009 errbuf[0] = '\0';
9011 return gen_type_name_0 (type);
9014 /* Given a method tree, put a printable description into the given
9015 buffer (overwriting) and return a pointer to the buffer. */
9017 static char *
9018 gen_method_decl (tree method)
9020 tree chain;
9022 strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */
9023 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method)));
9024 strcat (errbuf, ")");
9025 chain = METHOD_SEL_ARGS (method);
9027 if (chain)
9029 /* We have a chain of keyword_decls. */
9032 if (KEYWORD_KEY_NAME (chain))
9033 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
9035 strcat (errbuf, ":(");
9036 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain)));
9037 strcat (errbuf, ")");
9039 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
9040 if ((chain = DECL_CHAIN (chain)))
9041 strcat (errbuf, " ");
9043 while (chain);
9045 if (METHOD_ADD_ARGS (method))
9047 chain = TREE_CHAIN (METHOD_ADD_ARGS (method));
9049 /* Know we have a chain of parm_decls. */
9050 while (chain)
9052 strcat (errbuf, ", ");
9053 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain)));
9054 chain = TREE_CHAIN (chain);
9057 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
9058 strcat (errbuf, ", ...");
9062 else
9063 /* We have a unary selector. */
9064 strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
9066 return errbuf;
9069 /* Debug info. */
9072 /* Dump an @interface declaration of the supplied class CHAIN to the
9073 supplied file FP. Used to implement the -gen-decls option (which
9074 prints out an @interface declaration of all classes compiled in
9075 this run); potentially useful for debugging the compiler too. */
9076 void
9077 dump_interface (FILE *fp, tree chain)
9079 /* FIXME: A heap overflow here whenever a method (or ivar)
9080 declaration is so long that it doesn't fit in the buffer. The
9081 code and all the related functions should be rewritten to avoid
9082 using fixed size buffers. */
9083 const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
9084 tree ivar_decls = CLASS_RAW_IVARS (chain);
9085 tree nst_methods = CLASS_NST_METHODS (chain);
9086 tree cls_methods = CLASS_CLS_METHODS (chain);
9088 fprintf (fp, "\n@interface %s", my_name);
9090 /* CLASS_SUPER_NAME is used to store the superclass name for
9091 classes, and the category name for categories. */
9092 if (CLASS_SUPER_NAME (chain))
9094 const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
9096 switch (TREE_CODE (chain))
9098 case CATEGORY_IMPLEMENTATION_TYPE:
9099 case CATEGORY_INTERFACE_TYPE:
9100 fprintf (fp, " (%s)\n", name);
9101 break;
9102 default:
9103 fprintf (fp, " : %s\n", name);
9104 break;
9107 else
9108 fprintf (fp, "\n");
9110 /* FIXME - the following doesn't seem to work at the moment. */
9111 if (ivar_decls)
9113 fprintf (fp, "{\n");
9116 fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls));
9117 ivar_decls = TREE_CHAIN (ivar_decls);
9119 while (ivar_decls);
9120 fprintf (fp, "}\n");
9123 while (nst_methods)
9125 fprintf (fp, "- %s;\n", gen_method_decl (nst_methods));
9126 nst_methods = TREE_CHAIN (nst_methods);
9129 while (cls_methods)
9131 fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods));
9132 cls_methods = TREE_CHAIN (cls_methods);
9135 fprintf (fp, "@end\n");
9138 #if 0
9139 /* Produce the pretty printing for an Objective-C method. This is
9140 currently unused, but could be handy while reorganizing the pretty
9141 printing to be more robust. */
9142 static const char *
9143 objc_pretty_print_method (bool is_class_method,
9144 const char *class_name,
9145 const char *category_name,
9146 const char *selector)
9148 if (category_name)
9150 char *result = XNEWVEC (char, strlen (class_name) + strlen (category_name)
9151 + strlen (selector) + 7);
9153 if (is_class_method)
9154 sprintf (result, "+[%s(%s) %s]", class_name, category_name, selector);
9155 else
9156 sprintf (result, "-[%s(%s) %s]", class_name, category_name, selector);
9158 return result;
9160 else
9162 char *result = XNEWVEC (char, strlen (class_name)
9163 + strlen (selector) + 5);
9165 if (is_class_method)
9166 sprintf (result, "+[%s %s]", class_name, selector);
9167 else
9168 sprintf (result, "-[%s %s]", class_name, selector);
9170 return result;
9173 #endif
9175 /* Demangle function for Objective-C. Attempt to demangle the
9176 function name associated with a method (eg, going from
9177 "_i_NSObject__class" to "-[NSObject class]"); usually for the
9178 purpose of pretty printing or error messages. Return the demangled
9179 name, or NULL if the string is not an Objective-C mangled method
9180 name.
9182 Because of how the mangling is done, any method that has a '_' in
9183 its original name is at risk of being demangled incorrectly. In
9184 some cases there are multiple valid ways to demangle a method name
9185 and there is no way we can decide.
9187 TODO: objc_demangle() can't always get it right; the right way to
9188 get this correct for all method names would be to store the
9189 Objective-C method name somewhere in the function decl. Then,
9190 there is no demangling to do; we'd just pull the method name out of
9191 the decl. As an additional bonus, when printing error messages we
9192 could check for such a method name, and if we find it, we know the
9193 function is actually an Objective-C method and we could print error
9194 messages saying "In method '+[NSObject class]" instead of "In
9195 function '+[NSObject class]" as we do now. */
9196 static const char *
9197 objc_demangle (const char *mangled)
9199 char *demangled, *cp;
9201 /* First of all, if the name is too short it can't be an Objective-C
9202 mangled method name. */
9203 if (mangled[0] == '\0' || mangled[1] == '\0' || mangled[2] == '\0')
9204 return NULL;
9206 /* If the name looks like an already demangled one, return it
9207 unchanged. This should only happen on Darwin, where method names
9208 are mangled differently into a pretty-print form (such as
9209 '+[NSObject class]', see darwin.h). In that case, demangling is
9210 a no-op, but we need to return the demangled name if it was an
9211 ObjC one, and return NULL if not. We should be safe as no C/C++
9212 function can start with "-[" or "+[". */
9213 if ((mangled[0] == '-' || mangled[0] == '+')
9214 && (mangled[1] == '['))
9215 return mangled;
9217 if (mangled[0] == '_' &&
9218 (mangled[1] == 'i' || mangled[1] == 'c') &&
9219 mangled[2] == '_')
9221 cp = demangled = XNEWVEC (char, strlen(mangled) + 2);
9222 if (mangled[1] == 'i')
9223 *cp++ = '-'; /* for instance method */
9224 else
9225 *cp++ = '+'; /* for class method */
9226 *cp++ = '['; /* opening left brace */
9227 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
9228 while (*cp && *cp == '_')
9229 cp++; /* skip any initial underbars in class name */
9230 cp = strchr(cp, '_'); /* find first non-initial underbar */
9231 if (cp == NULL)
9233 free(demangled); /* not mangled name */
9234 return NULL;
9236 if (cp[1] == '_') /* easy case: no category name */
9238 *cp++ = ' '; /* replace two '_' with one ' ' */
9239 strcpy(cp, mangled + (cp - demangled) + 2);
9241 else
9243 *cp++ = '('; /* less easy case: category name */
9244 cp = strchr(cp, '_');
9245 if (cp == 0)
9247 free(demangled); /* not mangled name */
9248 return NULL;
9250 *cp++ = ')';
9251 *cp++ = ' '; /* overwriting 1st char of method name... */
9252 strcpy(cp, mangled + (cp - demangled)); /* get it back */
9254 /* Now we have the method name. We need to generally replace
9255 '_' with ':' but trying to preserve '_' if it could only have
9256 been in the mangled string because it was already in the
9257 original name. In cases where it's ambiguous, we assume that
9258 any '_' originated from a ':'. */
9260 /* Initial '_'s in method name can't have been generating by
9261 converting ':'s. Skip them. */
9262 while (*cp && *cp == '_')
9263 cp++;
9265 /* If the method name does not end with '_', then it has no
9266 arguments and there was no replacement of ':'s with '_'s
9267 during mangling. Check for that case, and skip any
9268 replacement if so. This at least guarantees that methods
9269 with no arguments are always demangled correctly (unless the
9270 original name ends with '_'). */
9271 if (*(mangled + strlen (mangled) - 1) != '_')
9273 /* Skip to the end. */
9274 for (; *cp; cp++)
9277 else
9279 /* Replace remaining '_' with ':'. This may get it wrong if
9280 there were '_'s in the original name. In most cases it
9281 is impossible to disambiguate. */
9282 for (; *cp; cp++)
9283 if (*cp == '_')
9284 *cp = ':';
9286 *cp++ = ']'; /* closing right brace */
9287 *cp++ = 0; /* string terminator */
9288 return demangled;
9290 else
9291 return NULL; /* not an objc mangled name */
9294 /* Try to pretty-print a decl. If the 'decl' is an Objective-C
9295 specific decl, return the printable name for it. If not, return
9296 NULL. */
9297 const char *
9298 objc_maybe_printable_name (tree decl, int v ATTRIBUTE_UNUSED)
9300 switch (TREE_CODE (decl))
9302 case FUNCTION_DECL:
9303 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)));
9305 /* The following happens when we are printing a deprecation
9306 warning for a method. The warn_deprecation() will end up
9307 trying to print the decl for INSTANCE_METHOD_DECL or
9308 CLASS_METHOD_DECL. It would be nice to be able to print
9309 "-[NSObject autorelease] is deprecated", but to do that, we'd
9310 need to store the class and method name in the method decl,
9311 which we currently don't do. For now, just return the name
9312 of the method. We don't return NULL, because that may
9313 trigger further attempts to pretty-print the decl in C/C++,
9314 but they wouldn't know how to pretty-print it. */
9315 case INSTANCE_METHOD_DECL:
9316 case CLASS_METHOD_DECL:
9317 return IDENTIFIER_POINTER (DECL_NAME (decl));
9318 /* This happens when printing a deprecation warning for a
9319 property. We may want to consider some sort of pretty
9320 printing (eg, include the class name where it was declared
9321 ?). */
9322 case PROPERTY_DECL:
9323 return IDENTIFIER_POINTER (PROPERTY_NAME (decl));
9324 default:
9325 return NULL;
9329 /* Return a printable name for 'decl'. This first tries
9330 objc_maybe_printable_name(), and if that fails, it returns the name
9331 in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for
9332 Objective-C; in Objective-C++, setting the hook is not enough
9333 because lots of C++ Front-End code calls cxx_printable_name,
9334 dump_decl and other C++ functions directly. So instead we have
9335 modified dump_decl to call objc_maybe_printable_name directly. */
9336 const char *
9337 objc_printable_name (tree decl, int v)
9339 const char *demangled_name = objc_maybe_printable_name (decl, v);
9341 if (demangled_name != NULL)
9342 return demangled_name;
9343 else
9344 return IDENTIFIER_POINTER (DECL_NAME (decl));
9347 /* Routine is called to issue diagnostic when reference to a private
9348 ivar is made and no other variable with same name is found in
9349 current scope. */
9350 bool
9351 objc_diagnose_private_ivar (tree id)
9353 tree ivar;
9354 if (!objc_method_context)
9355 return false;
9356 ivar = is_ivar (objc_ivar_chain, id);
9357 if (ivar && is_private (ivar))
9359 error ("instance variable %qs is declared private",
9360 IDENTIFIER_POINTER (id));
9361 return true;
9363 return false;
9366 /* Look up ID as an instance variable. OTHER contains the result of
9367 the C or C++ lookup, which we may want to use instead. */
9368 /* To use properties inside an instance method, use self.property. */
9369 tree
9370 objc_lookup_ivar (tree other, tree id)
9372 tree ivar;
9374 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
9375 if (!objc_method_context)
9376 return other;
9378 if (!strcmp (IDENTIFIER_POINTER (id), "super"))
9379 /* We have a message to super. */
9380 return get_super_receiver ();
9382 /* In a class method, look up an instance variable only as a last
9383 resort. */
9384 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
9385 && other && other != error_mark_node)
9386 return other;
9388 /* Don't look up the ivar if the user has explicitly advised against
9389 it with -fno-local-ivars. */
9391 if (!flag_local_ivars)
9392 return other;
9394 /* Look up the ivar, but do not use it if it is not accessible. */
9395 ivar = is_ivar (objc_ivar_chain, id);
9397 if (!ivar || is_private (ivar))
9398 return other;
9400 /* In an instance method, a local variable (or parameter) may hide the
9401 instance variable. */
9402 if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
9403 && other && other != error_mark_node
9404 #ifdef OBJCPLUS
9405 && CP_DECL_CONTEXT (other) != global_namespace)
9406 #else
9407 && !DECL_FILE_SCOPE_P (other))
9408 #endif
9410 if (warn_shadow_ivar == 1 || (warn_shadow && warn_shadow_ivar != 0)) {
9411 warning (warn_shadow_ivar ? OPT_Wshadow_ivar : OPT_Wshadow,
9412 "local declaration of %qE hides instance variable", id);
9415 return other;
9418 /* At this point, we are either in an instance method with no obscuring
9419 local definitions, or in a class method with no alternate definitions
9420 at all. */
9421 return build_ivar_reference (id);
9424 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
9425 needs to be done if we are calling a function through a cast. */
9427 tree
9428 objc_rewrite_function_call (tree function, tree first_param)
9430 if (TREE_CODE (function) == NOP_EXPR
9431 && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR
9432 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0))
9433 == FUNCTION_DECL)
9435 function = build3 (OBJ_TYPE_REF, TREE_TYPE (function),
9436 TREE_OPERAND (function, 0),
9437 first_param, size_zero_node);
9440 return function;
9443 /* This is called to "gimplify" a PROPERTY_REF node. It builds the
9444 corresponding 'getter' function call. Note that we assume the
9445 PROPERTY_REF to be valid since we generated it while parsing. */
9446 static void
9447 objc_gimplify_property_ref (tree *expr_p)
9449 tree getter = PROPERTY_REF_GETTER_CALL (*expr_p);
9450 tree call_exp;
9452 if (getter == NULL_TREE)
9454 tree property_decl = PROPERTY_REF_PROPERTY_DECL (*expr_p);
9455 /* This can happen if DECL_ARTIFICIAL (*expr_p), but
9456 should be impossible for real properties, which always
9457 have a getter. */
9458 error_at (EXPR_LOCATION (*expr_p), "no %qs getter found",
9459 IDENTIFIER_POINTER (PROPERTY_NAME (property_decl)));
9460 /* Try to recover from the error to prevent an ICE. We take
9461 zero and cast it to the type of the property. */
9462 *expr_p = convert (TREE_TYPE (property_decl),
9463 integer_zero_node);
9464 return;
9467 if (PROPERTY_REF_DEPRECATED_GETTER (*expr_p))
9469 /* PROPERTY_REF_DEPRECATED_GETTER contains the method prototype
9470 that is deprecated. */
9471 warn_deprecated_use (PROPERTY_REF_DEPRECATED_GETTER (*expr_p),
9472 NULL_TREE);
9475 call_exp = getter;
9476 #ifdef OBJCPLUS
9477 /* In C++, a getter which returns an aggregate value results in a
9478 target_expr which initializes a temporary to the call
9479 expression. */
9480 if (TREE_CODE (getter) == TARGET_EXPR)
9482 gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter)));
9483 gcc_assert (TREE_CODE (TREE_OPERAND (getter, 0)) == VAR_DECL);
9484 call_exp = TREE_OPERAND (getter, 1);
9486 #endif
9487 gcc_assert (TREE_CODE (call_exp) == CALL_EXPR);
9489 *expr_p = call_exp;
9492 /* This is called when "gimplifying" the trees. We need to gimplify
9493 the Objective-C/Objective-C++ specific trees, then hand over the
9494 process to C/C++. */
9496 objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9498 enum tree_code code = TREE_CODE (*expr_p);
9499 switch (code)
9501 /* Look for the special case of OBJC_TYPE_REF with the address
9502 of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend
9503 or one of its cousins). */
9504 case OBJ_TYPE_REF:
9505 if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR
9506 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0))
9507 == FUNCTION_DECL)
9509 enum gimplify_status r0, r1;
9511 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
9512 value of the OBJ_TYPE_REF, so force them to be emitted
9513 during subexpression evaluation rather than after the
9514 OBJ_TYPE_REF. This permits objc_msgSend calls in
9515 Objective C to use direct rather than indirect calls when
9516 the object expression has a postincrement. */
9517 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL,
9518 is_gimple_val, fb_rvalue);
9519 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
9520 is_gimple_val, fb_rvalue);
9522 return MIN (r0, r1);
9524 break;
9525 case PROPERTY_REF:
9526 objc_gimplify_property_ref (expr_p);
9527 /* Do not return yet; let C/C++ gimplify the resulting expression. */
9528 break;
9529 default:
9530 break;
9533 #ifdef OBJCPLUS
9534 return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p);
9535 #else
9536 return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
9537 #endif
9540 /* --- FAST ENUMERATION --- */
9541 /* Begin code generation for fast enumeration (foreach) ... */
9543 /* Defines
9545 struct __objcFastEnumerationState
9547 unsigned long state;
9548 id *itemsPtr;
9549 unsigned long *mutationsPtr;
9550 unsigned long extra[5];
9553 Confusingly enough, NSFastEnumeration is then defined by libraries
9554 to be the same structure.
9557 static void
9558 build_fast_enumeration_state_template (void)
9560 tree decls, *chain = NULL;
9562 /* { */
9563 objc_fast_enumeration_state_template = objc_start_struct (get_identifier
9564 (TAG_FAST_ENUMERATION_STATE));
9566 /* unsigned long state; */
9567 decls = add_field_decl (long_unsigned_type_node, "state", &chain);
9569 /* id *itemsPtr; */
9570 add_field_decl (build_pointer_type (objc_object_type),
9571 "itemsPtr", &chain);
9573 /* unsigned long *mutationsPtr; */
9574 add_field_decl (build_pointer_type (long_unsigned_type_node),
9575 "mutationsPtr", &chain);
9577 /* unsigned long extra[5]; */
9578 add_field_decl (build_sized_array_type (long_unsigned_type_node, 5),
9579 "extra", &chain);
9581 /* } */
9582 objc_finish_struct (objc_fast_enumeration_state_template, decls);
9586 'objc_finish_foreach_loop()' generates the code for an Objective-C
9587 foreach loop. The 'location' argument is the location of the 'for'
9588 that starts the loop. The 'object_expression' is the expression of
9589 the 'object' that iterates; the 'collection_expression' is the
9590 expression of the collection that we iterate over (we need to make
9591 sure we evaluate this only once); the 'for_body' is the set of
9592 statements to be executed in each iteration; 'break_label' and
9593 'continue_label' are the break and continue labels which we need to
9594 emit since the <statements> may be jumping to 'break_label' (if they
9595 contain 'break') or to 'continue_label' (if they contain
9596 'continue').
9598 The syntax is
9600 for (<object expression> in <collection expression>)
9601 <statements>
9603 which is compiled into the following blurb:
9606 id __objc_foreach_collection;
9607 __objc_fast_enumeration_state __objc_foreach_enum_state;
9608 unsigned long __objc_foreach_batchsize;
9609 id __objc_foreach_items[16];
9610 __objc_foreach_collection = <collection expression>;
9611 __objc_foreach_enum_state = { 0 };
9612 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9614 if (__objc_foreach_batchsize == 0)
9615 <object expression> = nil;
9616 else
9618 unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr;
9619 next_batch:
9621 unsigned long __objc_foreach_index;
9622 __objc_foreach_index = 0;
9624 next_object:
9625 if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>);
9626 <object expression> = enumState.itemsPtr[__objc_foreach_index];
9627 <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label]
9629 continue_label:
9630 __objc_foreach_index++;
9631 if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object;
9632 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9634 if (__objc_foreach_batchsize != 0) goto next_batch;
9635 <object expression> = nil;
9636 break_label:
9640 'statements' may contain a 'continue' or 'break' instruction, which
9641 the user expects to 'continue' or 'break' the entire foreach loop.
9642 We are provided the labels that 'break' and 'continue' jump to, so
9643 we place them where we want them to jump to when they pick them.
9645 Optimization TODO: we could cache the IMP of
9646 countByEnumeratingWithState:objects:count:.
9649 /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */
9650 /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */
9652 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
9653 #include "tree-pretty-print.h"
9654 #endif
9656 void
9657 objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body,
9658 tree break_label, tree continue_label)
9660 /* A tree representing the __objcFastEnumerationState struct type,
9661 or NSFastEnumerationState struct, whatever we are using. */
9662 tree objc_fast_enumeration_state_type;
9664 /* The trees representing the declarations of each of the local variables. */
9665 tree objc_foreach_collection_decl;
9666 tree objc_foreach_enum_state_decl;
9667 tree objc_foreach_items_decl;
9668 tree objc_foreach_batchsize_decl;
9669 tree objc_foreach_mutations_pointer_decl;
9670 tree objc_foreach_index_decl;
9672 /* A tree representing the selector countByEnumeratingWithState:objects:count:. */
9673 tree selector_name;
9675 /* A tree representing the local bind. */
9676 tree bind;
9678 /* A tree representing the external 'if (__objc_foreach_batchsize)' */
9679 tree first_if;
9681 /* A tree representing the 'else' part of 'first_if' */
9682 tree first_else;
9684 /* A tree representing the 'next_batch' label. */
9685 tree next_batch_label_decl;
9687 /* A tree representing the binding after the 'next_batch' label. */
9688 tree next_batch_bind;
9690 /* A tree representing the 'next_object' label. */
9691 tree next_object_label_decl;
9693 /* Temporary variables. */
9694 tree t;
9695 int i;
9697 if (flag_objc1_only)
9698 error_at (location, "fast enumeration is not available in Objective-C 1.0");
9700 if (object_expression == error_mark_node)
9701 return;
9703 if (collection_expression == error_mark_node)
9704 return;
9706 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression), true))
9708 error_at (location, "iterating variable in fast enumeration is not an object");
9709 return;
9712 if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression), true))
9714 error_at (location, "collection in fast enumeration is not an object");
9715 return;
9718 /* TODO: Check that object_expression is either a variable
9719 declaration, or an lvalue. */
9721 /* This kludge is an idea from apple. We use the
9722 __objcFastEnumerationState struct implicitly defined by the
9723 compiler, unless a NSFastEnumerationState struct has been defined
9724 (by a Foundation library such as GNUstep Base) in which case, we
9725 use that one.
9727 objc_fast_enumeration_state_type = objc_fast_enumeration_state_template;
9729 tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState"));
9731 if (objc_NSFastEnumeration_type)
9733 /* TODO: We really need to check that
9734 objc_NSFastEnumeration_type is the same as ours! */
9735 if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL)
9737 /* If it's a typedef, use the original type. */
9738 if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type))
9739 objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type);
9740 else
9741 objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type);
9746 /* { */
9747 /* Done by c-parser.c. */
9749 /* type object; */
9750 /* Done by c-parser.c. */
9752 /* Disable warnings that 'object' is unused. For example the code
9754 for (id object in collection)
9755 i++;
9757 which can be used to count how many objects there are in the
9758 collection is fine and should generate no warnings even if
9759 'object' is technically unused. */
9760 TREE_USED (object_expression) = 1;
9761 if (DECL_P (object_expression))
9762 DECL_READ_P (object_expression) = 1;
9764 /* id __objc_foreach_collection */
9765 objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection");
9767 /* __objcFastEnumerationState __objc_foreach_enum_state; */
9768 objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state");
9769 TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl;
9771 /* id __objc_foreach_items[16]; */
9772 objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items");
9773 TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl;
9775 /* unsigned long __objc_foreach_batchsize; */
9776 objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize");
9777 TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl;
9779 /* Generate the local variable binding. */
9780 bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL);
9781 SET_EXPR_LOCATION (bind, location);
9782 TREE_SIDE_EFFECTS (bind) = 1;
9784 /* __objc_foreach_collection = <collection expression>; */
9785 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression);
9786 SET_EXPR_LOCATION (t, location);
9787 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9788 /* We have used 'collection_expression'. */
9789 mark_exp_read (collection_expression);
9791 /* __objc_foreach_enum_state.state = 0; */
9792 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9793 get_identifier ("state")),
9794 build_int_cst (long_unsigned_type_node, 0));
9795 SET_EXPR_LOCATION (t, location);
9796 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9798 /* __objc_foreach_enum_state.itemsPtr = NULL; */
9799 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9800 get_identifier ("itemsPtr")),
9801 null_pointer_node);
9802 SET_EXPR_LOCATION (t, location);
9803 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9805 /* __objc_foreach_enum_state.mutationsPtr = NULL; */
9806 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9807 get_identifier ("mutationsPtr")),
9808 null_pointer_node);
9809 SET_EXPR_LOCATION (t, location);
9810 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9812 /* __objc_foreach_enum_state.extra[0] = 0; */
9813 /* __objc_foreach_enum_state.extra[1] = 0; */
9814 /* __objc_foreach_enum_state.extra[2] = 0; */
9815 /* __objc_foreach_enum_state.extra[3] = 0; */
9816 /* __objc_foreach_enum_state.extra[4] = 0; */
9817 for (i = 0; i < 5 ; i++)
9819 t = build2 (MODIFY_EXPR, void_type_node,
9820 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9821 get_identifier ("extra")),
9822 build_int_cst (NULL_TREE, i)),
9823 build_int_cst (long_unsigned_type_node, 0));
9824 SET_EXPR_LOCATION (t, location);
9825 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9828 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
9829 selector_name = get_identifier ("countByEnumeratingWithState:objects:count:");
9830 #ifdef OBJCPLUS
9831 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9832 /* Parameters. */
9833 tree_cons /* &__objc_foreach_enum_state */
9834 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9835 tree_cons /* __objc_foreach_items */
9836 (NULL_TREE, objc_foreach_items_decl,
9837 tree_cons /* 16 */
9838 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9839 #else
9840 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
9842 struct c_expr array;
9843 array.value = objc_foreach_items_decl;
9844 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9845 /* Parameters. */
9846 tree_cons /* &__objc_foreach_enum_state */
9847 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9848 tree_cons /* __objc_foreach_items */
9849 (NULL_TREE, default_function_array_conversion (location, array).value,
9850 tree_cons /* 16 */
9851 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9853 #endif
9854 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
9855 convert (long_unsigned_type_node, t));
9856 SET_EXPR_LOCATION (t, location);
9857 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9859 /* if (__objc_foreach_batchsize == 0) */
9860 first_if = build3 (COND_EXPR, void_type_node,
9861 /* Condition. */
9862 c_fully_fold
9863 (c_common_truthvalue_conversion
9864 (location,
9865 build_binary_op (location,
9866 EQ_EXPR,
9867 objc_foreach_batchsize_decl,
9868 build_int_cst (long_unsigned_type_node, 0), 1)),
9869 false, NULL),
9870 /* Then block (we fill it in later). */
9871 NULL_TREE,
9872 /* Else block (we fill it in later). */
9873 NULL_TREE);
9874 SET_EXPR_LOCATION (first_if, location);
9875 append_to_statement_list (first_if, &BIND_EXPR_BODY (bind));
9877 /* then <object expression> = nil; */
9878 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
9879 SET_EXPR_LOCATION (t, location);
9880 COND_EXPR_THEN (first_if) = t;
9882 /* Now we build the 'else' part of the if; once we finish building
9883 it, we attach it to first_if as the 'else' part. */
9885 /* else */
9886 /* { */
9888 /* unsigned long __objc_foreach_mutations_pointer; */
9889 objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer");
9891 /* Generate the local variable binding. */
9892 first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL);
9893 SET_EXPR_LOCATION (first_else, location);
9894 TREE_SIDE_EFFECTS (first_else) = 1;
9896 /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */
9897 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl,
9898 build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9899 get_identifier ("mutationsPtr")),
9900 RO_UNARY_STAR));
9901 SET_EXPR_LOCATION (t, location);
9902 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
9904 /* next_batch: */
9905 next_batch_label_decl = create_artificial_label (location);
9906 t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl);
9907 SET_EXPR_LOCATION (t, location);
9908 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
9910 /* { */
9912 /* unsigned long __objc_foreach_index; */
9913 objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index");
9915 /* Generate the local variable binding. */
9916 next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL);
9917 SET_EXPR_LOCATION (next_batch_bind, location);
9918 TREE_SIDE_EFFECTS (next_batch_bind) = 1;
9919 append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else));
9921 /* __objc_foreach_index = 0; */
9922 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
9923 build_int_cst (long_unsigned_type_node, 0));
9924 SET_EXPR_LOCATION (t, location);
9925 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9927 /* next_object: */
9928 next_object_label_decl = create_artificial_label (location);
9929 t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl);
9930 SET_EXPR_LOCATION (t, location);
9931 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9933 /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */
9934 t = build3 (COND_EXPR, void_type_node,
9935 /* Condition. */
9936 c_fully_fold
9937 (c_common_truthvalue_conversion
9938 (location,
9939 build_binary_op
9940 (location,
9941 NE_EXPR,
9942 objc_foreach_mutations_pointer_decl,
9943 build_indirect_ref (location,
9944 objc_build_component_ref (objc_foreach_enum_state_decl,
9945 get_identifier ("mutationsPtr")),
9946 RO_UNARY_STAR), 1)),
9947 false, NULL),
9948 /* Then block. */
9949 build_function_call (input_location,
9950 objc_enumeration_mutation_decl,
9951 tree_cons (NULL, collection_expression, NULL)),
9952 /* Else block. */
9953 NULL_TREE);
9954 SET_EXPR_LOCATION (t, location);
9955 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9957 /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */
9958 t = build2 (MODIFY_EXPR, void_type_node, object_expression,
9959 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9960 get_identifier ("itemsPtr")),
9961 objc_foreach_index_decl));
9962 SET_EXPR_LOCATION (t, location);
9963 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9965 /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */
9966 append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind));
9968 /* continue_label: */
9969 if (continue_label)
9971 t = build1 (LABEL_EXPR, void_type_node, continue_label);
9972 SET_EXPR_LOCATION (t, location);
9973 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9976 /* __objc_foreach_index++; */
9977 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
9978 build_binary_op (location,
9979 PLUS_EXPR,
9980 objc_foreach_index_decl,
9981 build_int_cst (long_unsigned_type_node, 1), 1));
9982 SET_EXPR_LOCATION (t, location);
9983 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9985 /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */
9986 t = build3 (COND_EXPR, void_type_node,
9987 /* Condition. */
9988 c_fully_fold
9989 (c_common_truthvalue_conversion
9990 (location,
9991 build_binary_op (location,
9992 LT_EXPR,
9993 objc_foreach_index_decl,
9994 objc_foreach_batchsize_decl, 1)),
9995 false, NULL),
9996 /* Then block. */
9997 build1 (GOTO_EXPR, void_type_node, next_object_label_decl),
9998 /* Else block. */
9999 NULL_TREE);
10000 SET_EXPR_LOCATION (t, location);
10001 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10003 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
10004 #ifdef OBJCPLUS
10005 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
10006 /* Parameters. */
10007 tree_cons /* &__objc_foreach_enum_state */
10008 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
10009 tree_cons /* __objc_foreach_items */
10010 (NULL_TREE, objc_foreach_items_decl,
10011 tree_cons /* 16 */
10012 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
10013 #else
10014 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
10016 struct c_expr array;
10017 array.value = objc_foreach_items_decl;
10018 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
10019 /* Parameters. */
10020 tree_cons /* &__objc_foreach_enum_state */
10021 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
10022 tree_cons /* __objc_foreach_items */
10023 (NULL_TREE, default_function_array_conversion (location, array).value,
10024 tree_cons /* 16 */
10025 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
10027 #endif
10028 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
10029 convert (long_unsigned_type_node, t));
10030 SET_EXPR_LOCATION (t, location);
10031 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10033 /* } */
10035 /* if (__objc_foreach_batchsize != 0) goto next_batch; */
10036 t = build3 (COND_EXPR, void_type_node,
10037 /* Condition. */
10038 c_fully_fold
10039 (c_common_truthvalue_conversion
10040 (location,
10041 build_binary_op (location,
10042 NE_EXPR,
10043 objc_foreach_batchsize_decl,
10044 build_int_cst (long_unsigned_type_node, 0), 1)),
10045 false, NULL),
10046 /* Then block. */
10047 build1 (GOTO_EXPR, void_type_node, next_batch_label_decl),
10048 /* Else block. */
10049 NULL_TREE);
10050 SET_EXPR_LOCATION (t, location);
10051 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10053 /* <object expression> = nil; */
10054 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
10055 SET_EXPR_LOCATION (t, location);
10056 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10058 /* break_label: */
10059 if (break_label)
10061 t = build1 (LABEL_EXPR, void_type_node, break_label);
10062 SET_EXPR_LOCATION (t, location);
10063 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10066 /* } */
10067 COND_EXPR_ELSE (first_if) = first_else;
10069 /* Do the whole thing. */
10070 add_stmt (bind);
10072 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
10073 /* This will print to stderr the whole blurb generated by the
10074 compiler while compiling (assuming the compiler doesn't crash
10075 before getting here).
10077 debug_generic_stmt (bind);
10078 #endif
10080 /* } */
10081 /* Done by c-parser.c */
10084 /* --- SUPPORT FOR FORMAT ARG CHECKING --- */
10085 /* Return true if we have an NxString object pointer. */
10087 bool
10088 objc_string_ref_type_p (tree strp)
10090 tree tmv;
10091 if (!strp || TREE_CODE (strp) != POINTER_TYPE)
10092 return false;
10094 tmv = TYPE_MAIN_VARIANT (TREE_TYPE (strp));
10095 tmv = OBJC_TYPE_NAME (tmv);
10096 return (tmv
10097 && TREE_CODE (tmv) == IDENTIFIER_NODE
10098 && IDENTIFIER_POINTER (tmv)
10099 && !strncmp (IDENTIFIER_POINTER (tmv), "NSString", 8));
10102 /* At present the behavior of this is undefined and it does nothing. */
10103 void
10104 objc_check_format_arg (tree ARG_UNUSED (format_arg),
10105 tree ARG_UNUSED (args_list))
10109 void
10110 objc_common_init_ts (void)
10112 c_common_init_ts ();
10114 MARK_TS_DECL_NON_COMMON (CLASS_METHOD_DECL);
10115 MARK_TS_DECL_NON_COMMON (INSTANCE_METHOD_DECL);
10116 MARK_TS_DECL_NON_COMMON (KEYWORD_DECL);
10117 MARK_TS_DECL_NON_COMMON (PROPERTY_DECL);
10119 MARK_TS_COMMON (CLASS_INTERFACE_TYPE);
10120 MARK_TS_COMMON (PROTOCOL_INTERFACE_TYPE);
10121 MARK_TS_COMMON (CLASS_IMPLEMENTATION_TYPE);
10123 MARK_TS_TYPED (MESSAGE_SEND_EXPR);
10124 MARK_TS_TYPED (PROPERTY_REF);
10127 size_t
10128 objc_common_tree_size (enum tree_code code)
10130 switch (code)
10132 case CLASS_METHOD_DECL:
10133 case INSTANCE_METHOD_DECL:
10134 case KEYWORD_DECL:
10135 case PROPERTY_DECL: return sizeof (tree_decl_non_common);
10136 case CLASS_INTERFACE_TYPE:
10137 case CLASS_IMPLEMENTATION_TYPE:
10138 case CATEGORY_INTERFACE_TYPE:
10139 case CATEGORY_IMPLEMENTATION_TYPE:
10140 case PROTOCOL_INTERFACE_TYPE: return sizeof (tree_type_non_common);
10141 default:
10142 gcc_unreachable ();
10147 #include "gt-objc-objc-act.h"