1 /* Implement classes and message passing for Objective C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 Contributed by Steve Naroff.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-pragma.h"
38 #include "c-family/c-format.h"
40 #include "langhooks.h"
49 #include "diagnostic-core.h"
52 #include "tree-iterator.h"
54 #include "langhooks-def.h"
56 /* For default_tree_printer (). */
57 #include "tree-pretty-print.h"
59 /* For enum gimplify_status */
62 #define OBJC_VOID_AT_END void_list_node
64 static unsigned int should_call_super_dealloc = 0;
66 /* When building Objective-C++, we need in_late_binary_op. */
68 bool in_late_binary_op = false;
71 /* When building Objective-C++, we are not linking against the C front-end
72 and so need to replicate the C tree-construction functions in some way. */
74 #define OBJCP_REMAP_FUNCTIONS
75 #include "objcp-decl.h"
78 /* This is the default way of generating a method name. */
79 /* This has the problem that "test_method:argument:" and
80 "test:method_argument:" will generate the same name
81 ("_i_Test__test_method_argument_" for an instance method of the
82 class "Test"), so you can't have them both in the same class!
83 Moreover, the demangling (going from
84 "_i_Test__test_method_argument" back to the original name) is
85 undefined because there are two correct ways of demangling the
87 #ifndef OBJC_GEN_METHOD_LABEL
88 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
91 sprintf ((BUF), "_%s_%s_%s_%s", \
92 ((IS_INST) ? "i" : "c"), \
94 ((CAT_NAME)? (CAT_NAME) : ""), \
96 for (temp = (BUF); *temp; temp++) \
97 if (*temp == ':') *temp = '_'; \
101 /* These need specifying. */
102 #ifndef OBJC_FORWARDING_STACK_OFFSET
103 #define OBJC_FORWARDING_STACK_OFFSET 0
106 #ifndef OBJC_FORWARDING_MIN_OFFSET
107 #define OBJC_FORWARDING_MIN_OFFSET 0
110 /* Set up for use of obstacks. */
114 /* This obstack is used to accumulate the encoding of a data type. */
115 static struct obstack util_obstack;
117 /* This points to the beginning of obstack contents, so we can free
118 the whole contents. */
121 /* The version identifies which language generation and runtime
122 the module (file) was compiled for, and is recorded in the
123 module descriptor. */
125 #define OBJC_VERSION (flag_next_runtime ? 6 : 8)
126 #define PROTOCOL_VERSION 2
128 /* (Decide if these can ever be validly changed.) */
129 #define OBJC_ENCODE_INLINE_DEFS 0
130 #define OBJC_ENCODE_DONT_INLINE_DEFS 1
132 /*** Private Interface (procedures) ***/
134 /* Used by compile_file. */
136 static void init_objc (void);
137 static void finish_objc (void);
139 /* Code generation. */
141 static tree objc_build_constructor (tree, VEC(constructor_elt,gc) *);
142 static tree build_objc_method_call (location_t, int, tree, tree, tree, tree);
143 static tree get_proto_encoding (tree);
144 static tree lookup_interface (tree);
145 static tree objc_add_static_instance (tree, tree);
147 static tree start_class (enum tree_code, tree, tree, tree);
148 static tree continue_class (tree);
149 static void finish_class (tree);
150 static void start_method_def (tree);
152 static void objc_start_function (tree, tree, tree, tree);
154 static void objc_start_function (tree, tree, tree, struct c_arg_info *);
156 static tree start_protocol (enum tree_code, tree, tree);
157 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
158 static tree objc_add_method (tree, tree, int, bool);
159 static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree);
160 static tree build_ivar_reference (tree);
161 static tree is_ivar (tree, tree);
163 static void build_objc_exception_stuff (void);
164 static void build_next_objc_exception_stuff (void);
166 /* We only need the following for ObjC; ObjC++ will use C++'s definition
167 of DERIVED_FROM_P. */
169 static bool objc_derived_from_p (tree, tree);
170 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
174 static void objc_gen_property_data (tree, tree);
175 static void objc_synthesize_getter (tree, tree, tree);
176 static void objc_synthesize_setter (tree, tree, tree);
177 static char *objc_build_property_setter_name (tree);
178 static int match_proto_with_proto (tree, tree, int);
179 static tree lookup_property (tree, tree);
180 static tree lookup_property_in_list (tree, tree);
181 static tree lookup_property_in_protocol_list (tree, tree);
182 static void build_objc_property_accessor_helpers (void);
184 static void objc_xref_basetypes (tree, tree);
186 static void build_class_template (void);
187 static void build_selector_template (void);
188 static void build_category_template (void);
189 static void build_super_template (void);
190 static tree build_protocol_initializer (tree, tree, tree, tree, tree);
191 static tree get_class_ivars (tree, bool);
192 static tree generate_protocol_list (tree);
193 static void build_protocol_reference (tree);
195 static void build_fast_enumeration_state_template (void);
198 static void objc_generate_cxx_cdtors (void);
202 static void objc_decl_method_attributes (tree*, tree, int);
203 static tree build_keyword_selector (tree);
204 static const char *synth_id_with_class_suffix (const char *, tree);
206 /* Hash tables to manage the global pool of method prototypes. */
208 hash *nst_method_hash_list = 0;
209 hash *cls_method_hash_list = 0;
211 /* Hash tables to manage the global pool of class names. */
213 hash *cls_name_hash_list = 0;
214 hash *als_name_hash_list = 0;
216 static void hash_class_name_enter (hash *, tree, tree);
217 static hash hash_class_name_lookup (hash *, tree);
219 static hash hash_lookup (hash *, tree);
220 static tree lookup_method (tree, tree);
221 static tree lookup_method_static (tree, tree, int);
223 static tree add_class (tree, tree);
224 static void add_category (tree, tree);
225 static inline tree lookup_category (tree, tree);
229 class_names, /* class, category, protocol, module names */
230 meth_var_names, /* method and variable names */
231 meth_var_types /* method and variable type descriptors */
234 static tree add_objc_string (tree, enum string_section);
235 static void build_selector_table_decl (void);
237 /* Protocol additions. */
239 static tree lookup_protocol (tree);
240 static tree lookup_and_install_protocols (tree);
244 static void encode_type_qualifiers (tree);
245 static void encode_type (tree, int, int);
246 static void encode_field_decl (tree, int, int);
249 static void really_start_method (tree, tree);
251 static void really_start_method (tree, struct c_arg_info *);
253 static int comp_proto_with_proto (tree, tree, int);
254 static tree get_arg_type_list (tree, int, int);
255 static tree objc_decay_parm_type (tree);
256 static void objc_push_parm (tree);
258 static tree objc_get_parm_info (int);
260 static struct c_arg_info *objc_get_parm_info (int);
263 /* Utilities for debugging and error diagnostics. */
265 static char *gen_type_name (tree);
266 static char *gen_type_name_0 (tree);
267 static char *gen_method_decl (tree);
268 static char *gen_declaration (tree);
270 /* Everything else. */
272 static tree create_field_decl (tree, const char *);
273 static void add_class_reference (tree);
274 static void build_protocol_template (void);
275 static tree encode_method_prototype (tree);
276 static void generate_classref_translation_entry (tree);
277 static void handle_class_ref (tree);
278 static void generate_struct_by_value_array (void)
280 static void mark_referenced_methods (void);
281 static void generate_objc_image_info (void);
282 static bool objc_type_valid_for_messaging (tree type, bool allow_classes);
284 /*** Private Interface (data) ***/
286 /* Reserved tag definitions. */
288 #define OBJECT_TYPEDEF_NAME "id"
289 #define CLASS_TYPEDEF_NAME "Class"
291 #define TAG_OBJECT "objc_object"
292 #define TAG_CLASS "objc_class"
293 #define TAG_SUPER "objc_super"
294 #define TAG_SELECTOR "objc_selector"
296 #define UTAG_CLASS "_objc_class"
297 #define UTAG_IVAR "_objc_ivar"
298 #define UTAG_IVAR_LIST "_objc_ivar_list"
299 #define UTAG_METHOD "_objc_method"
300 #define UTAG_METHOD_LIST "_objc_method_list"
301 #define UTAG_CATEGORY "_objc_category"
302 #define UTAG_MODULE "_objc_module"
303 #define UTAG_SYMTAB "_objc_symtab"
304 #define UTAG_SUPER "_objc_super"
305 #define UTAG_SELECTOR "_objc_selector"
307 #define UTAG_PROTOCOL "_objc_protocol"
308 #define UTAG_METHOD_PROTOTYPE "_objc_method_prototype"
309 #define UTAG_METHOD_PROTOTYPE_LIST "_objc__method_prototype_list"
311 /* Note that the string object global name is only needed for the
313 #define STRING_OBJECT_GLOBAL_FORMAT "_%sClassReference"
315 #define PROTOCOL_OBJECT_CLASS_NAME "Protocol"
317 #define TAG_ENUMERATION_MUTATION "objc_enumerationMutation"
318 #define TAG_FAST_ENUMERATION_STATE "__objcFastEnumerationState"
320 static const char *TAG_GETCLASS;
321 static const char *TAG_GETMETACLASS;
322 static const char *TAG_MSGSEND;
323 static const char *TAG_MSGSENDSUPER;
324 /* The NeXT Objective-C messenger may have two extra entry points, for use
325 when returning a structure. */
326 static const char *TAG_MSGSEND_STRET;
327 static const char *TAG_MSGSENDSUPER_STRET;
328 static const char *default_constant_string_class_name;
330 /* Runtime metadata flags. */
331 #define CLS_FACTORY 0x0001L
332 #define CLS_META 0x0002L
333 #define CLS_HAS_CXX_STRUCTORS 0x2000L
335 #define OBJC_MODIFIER_STATIC 0x00000001
336 #define OBJC_MODIFIER_FINAL 0x00000002
337 #define OBJC_MODIFIER_PUBLIC 0x00000004
338 #define OBJC_MODIFIER_PRIVATE 0x00000008
339 #define OBJC_MODIFIER_PROTECTED 0x00000010
340 #define OBJC_MODIFIER_NATIVE 0x00000020
341 #define OBJC_MODIFIER_SYNCHRONIZED 0x00000040
342 #define OBJC_MODIFIER_ABSTRACT 0x00000080
343 #define OBJC_MODIFIER_VOLATILE 0x00000100
344 #define OBJC_MODIFIER_TRANSIENT 0x00000200
345 #define OBJC_MODIFIER_NONE_SPECIFIED 0x80000000
347 /* NeXT-specific tags. */
349 #define TAG_MSGSEND_NONNIL "objc_msgSendNonNil"
350 #define TAG_MSGSEND_NONNIL_STRET "objc_msgSendNonNil_stret"
351 #define TAG_EXCEPTIONEXTRACT "objc_exception_extract"
352 #define TAG_EXCEPTIONTRYENTER "objc_exception_try_enter"
353 #define TAG_EXCEPTIONTRYEXIT "objc_exception_try_exit"
354 #define TAG_EXCEPTIONMATCH "objc_exception_match"
355 #define TAG_EXCEPTIONTHROW "objc_exception_throw"
356 #define TAG_SYNCENTER "objc_sync_enter"
357 #define TAG_SYNCEXIT "objc_sync_exit"
358 #define TAG_SETJMP "_setjmp"
359 #define UTAG_EXCDATA "_objc_exception_data"
361 #define TAG_ASSIGNIVAR "objc_assign_ivar"
362 #define TAG_ASSIGNGLOBAL "objc_assign_global"
363 #define TAG_ASSIGNSTRONGCAST "objc_assign_strongCast"
365 /* Branch entry points. All that matters here are the addresses;
366 functions with these names do not really exist in libobjc. */
368 #define TAG_MSGSEND_FAST "objc_msgSend_Fast"
369 #define TAG_ASSIGNIVAR_FAST "objc_assign_ivar_Fast"
371 #define TAG_CXX_CONSTRUCT ".cxx_construct"
372 #define TAG_CXX_DESTRUCT ".cxx_destruct"
374 /* GNU-specific tags. */
376 #define TAG_EXECCLASS "__objc_exec_class"
377 #define TAG_GNUINIT "__objc_gnu_init"
379 /* Flags for lookup_method_static(). */
381 /* Look for class methods. */
382 #define OBJC_LOOKUP_CLASS 1
383 /* Do not examine superclasses. */
384 #define OBJC_LOOKUP_NO_SUPER 2
385 /* Disable returning an instance method of a root class when a class
386 method can't be found. */
387 #define OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS 4
389 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
390 tree objc_global_trees[OCTI_MAX];
392 static void handle_impent (struct imp_entry *);
394 struct imp_entry *imp_list = 0;
395 int imp_count = 0; /* `@implementation' */
396 int cat_count = 0; /* `@category' */
398 objc_ivar_visibility_kind objc_ivar_visibility;
400 /* Use to generate method labels. */
401 static int method_slot = 0;
403 /* Flag to say whether methods in a protocol are optional or
405 static bool objc_method_optional_flag = false;
407 static int objc_collecting_ivars = 0;
411 static char *errbuf; /* Buffer for error diagnostics */
413 /* Data imported from tree.c. */
415 extern enum debug_info_type write_symbols;
418 static int flag_typed_selectors;
420 /* Store all constructed constant strings in a hash table so that
421 they get uniqued properly. */
423 struct GTY(()) string_descriptor {
424 /* The literal argument . */
427 /* The resulting constant string. */
431 static GTY((param_is (struct string_descriptor))) htab_t string_htab;
433 FILE *gen_declaration_file;
435 /* Tells "encode_pointer/encode_aggregate" whether we are generating
436 type descriptors for instance variables (as opposed to methods).
437 Type descriptors for instance variables contain more information
438 than methods (for static typing and embedded structures). */
440 static int generating_instance_variables = 0;
442 /* For building an objc struct. These may not be used when this file
443 is compiled as part of obj-c++. */
445 static bool objc_building_struct;
446 static struct c_struct_parse_info *objc_struct_info ATTRIBUTE_UNUSED;
448 /* Start building a struct for objc. */
451 objc_start_struct (tree name)
453 gcc_assert (!objc_building_struct);
454 objc_building_struct = true;
455 return start_struct (input_location, RECORD_TYPE, name, &objc_struct_info);
458 /* Finish building a struct for objc. */
461 objc_finish_struct (tree type, tree fieldlist)
463 gcc_assert (objc_building_struct);
464 objc_building_struct = false;
465 return finish_struct (input_location, type, fieldlist, NULL_TREE,
470 build_sized_array_type (tree base_type, int size)
472 tree index_type = build_index_type (build_int_cst (NULL_TREE, size - 1));
473 return build_array_type (base_type, index_type);
477 add_field_decl (tree type, const char *name, tree **chain)
479 tree field = create_field_decl (type, name);
483 *chain = &DECL_CHAIN (field);
488 /* Some platforms pass small structures through registers versus
489 through an invisible pointer. Determine at what size structure is
490 the transition point between the two possibilities. */
493 generate_struct_by_value_array (void)
498 int aggregate_in_mem[32];
501 /* Presumably no platform passes 32 byte structures in a register. */
502 for (i = 1; i < 32; i++)
507 /* Create an unnamed struct that has `i' character components */
508 type = objc_start_struct (NULL_TREE);
510 strcpy (buffer, "c1");
511 decls = add_field_decl (char_type_node, buffer, &chain);
513 for (j = 1; j < i; j++)
515 sprintf (buffer, "c%d", j + 1);
516 add_field_decl (char_type_node, buffer, &chain);
518 objc_finish_struct (type, decls);
520 aggregate_in_mem[i] = aggregate_value_p (type, 0);
521 if (!aggregate_in_mem[i])
525 /* We found some structures that are returned in registers instead of memory
526 so output the necessary data. */
529 for (i = 31; i >= 0; i--)
530 if (!aggregate_in_mem[i])
532 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n\n", i);
534 /* The first member of the structure is always 0 because we don't handle
535 structures with 0 members */
536 printf ("static int struct_forward_array[] = {\n 0");
538 for (j = 1; j <= i; j++)
539 printf (", %d", aggregate_in_mem[j]);
550 if (cxx_init () == false)
552 if (c_objc_common_init () == false)
556 /* If gen_declaration desired, open the output file. */
557 if (flag_gen_declaration)
559 register char * const dumpname = concat (dump_base_name, ".decl", NULL);
560 gen_declaration_file = fopen (dumpname, "w");
561 if (gen_declaration_file == 0)
562 fatal_error ("can%'t open %s: %m", dumpname);
566 if (flag_next_runtime)
568 TAG_GETCLASS = "objc_getClass";
569 TAG_GETMETACLASS = "objc_getMetaClass";
570 TAG_MSGSEND = "objc_msgSend";
571 TAG_MSGSENDSUPER = "objc_msgSendSuper";
572 TAG_MSGSEND_STRET = "objc_msgSend_stret";
573 TAG_MSGSENDSUPER_STRET = "objc_msgSendSuper_stret";
574 default_constant_string_class_name = "NSConstantString";
578 TAG_GETCLASS = "objc_get_class";
579 TAG_GETMETACLASS = "objc_get_meta_class";
580 TAG_MSGSEND = "objc_msg_lookup";
581 TAG_MSGSENDSUPER = "objc_msg_lookup_super";
582 /* GNU runtime does not provide special functions to support
583 structure-returning methods. */
584 default_constant_string_class_name = "NXConstantString";
585 flag_typed_selectors = 1;
586 /* GNU runtime does not need the compiler to change code
587 in order to do GC. */
590 warning_at (0, 0, "%<-fobjc-gc%> is ignored for %<-fgnu-runtime%>");
597 if (print_struct_values && !flag_compare_debug)
598 generate_struct_by_value_array ();
603 /* This is called automatically (at the very end of compilation) by
604 c_write_global_declarations and cp_write_global_declarations. */
606 objc_write_global_declarations (void)
608 mark_referenced_methods ();
610 /* Finalize Objective-C runtime data. */
613 if (gen_declaration_file)
614 fclose (gen_declaration_file);
617 /* Return the first occurrence of a method declaration corresponding
618 to sel_name in rproto_list. Search rproto_list recursively.
619 If is_class is 0, search for instance methods, otherwise for class
622 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
628 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
630 p = TREE_VALUE (rproto);
632 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
634 if ((fnd = lookup_method (is_class
635 ? PROTOCOL_CLS_METHODS (p)
636 : PROTOCOL_NST_METHODS (p), sel_name)))
638 else if (PROTOCOL_LIST (p))
639 fnd = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
644 ; /* An identifier...if we could not find a protocol. */
655 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
659 /* Make sure the protocol is supported by the object on the rhs. */
660 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
663 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
665 p = TREE_VALUE (rproto);
667 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
672 else if (PROTOCOL_LIST (p))
673 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
682 ; /* An identifier...if we could not find a protocol. */
689 objc_start_class_interface (tree klass, tree super_class,
690 tree protos, tree attributes)
695 error_at (input_location, "class attributes are not available in Objective-C 1.0");
697 warning_at (input_location, OPT_Wattributes,
698 "class attributes are not available in this version"
699 " of the compiler, (ignored)");
701 objc_interface_context
703 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos);
704 objc_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
708 objc_start_category_interface (tree klass, tree categ,
709 tree protos, tree attributes)
714 error_at (input_location, "category attributes are not available in Objective-C 1.0");
716 warning_at (input_location, OPT_Wattributes,
717 "category attributes are not available in this version"
718 " of the compiler, (ignored)");
720 objc_interface_context
721 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos);
723 = continue_class (objc_interface_context);
727 objc_start_protocol (tree name, tree protos, tree attributes)
732 error_at (input_location, "protocol attributes are not available in Objective-C 1.0");
734 warning_at (input_location, OPT_Wattributes,
735 "protocol attributes are not available in this version"
736 " of the compiler, (ignored)");
738 objc_interface_context
739 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos);
740 objc_method_optional_flag = false;
744 objc_continue_interface (void)
747 = continue_class (objc_interface_context);
751 objc_finish_interface (void)
753 finish_class (objc_interface_context);
754 objc_interface_context = NULL_TREE;
755 objc_method_optional_flag = false;
759 objc_start_class_implementation (tree klass, tree super_class)
761 objc_implementation_context
763 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE);
764 objc_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
768 objc_start_category_implementation (tree klass, tree categ)
770 objc_implementation_context
771 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE);
773 = continue_class (objc_implementation_context);
777 objc_continue_implementation (void)
780 = continue_class (objc_implementation_context);
784 objc_finish_implementation (void)
787 if (flag_objc_call_cxx_cdtors)
788 objc_generate_cxx_cdtors ();
791 if (objc_implementation_context)
793 finish_class (objc_implementation_context);
794 objc_ivar_chain = NULL_TREE;
795 objc_implementation_context = NULL_TREE;
798 warning (0, "%<@end%> must appear in an @implementation context");
802 objc_set_visibility (objc_ivar_visibility_kind visibility)
804 if (visibility == OBJC_IVAR_VIS_PACKAGE)
807 error ("%<@package%> is not available in Objective-C 1.0");
809 warning (0, "%<@package%> presently has the same effect as %<@public%>");
811 objc_ivar_visibility = visibility;
815 objc_set_method_opt (bool optional)
818 error_at (input_location, "@optional/@required are not available in Objective-C 1.0");
820 objc_method_optional_flag = optional;
821 if (!objc_interface_context
822 || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
824 error ("@optional/@required is allowed in @protocol context only");
825 objc_method_optional_flag = false;
829 /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
832 lookup_property_in_list (tree chain, tree property)
835 for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x))
836 if (PROPERTY_NAME (x) == property)
841 /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
842 static tree lookup_property_in_protocol_list (tree rproto_list, tree property)
845 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
847 tree p = TREE_VALUE (rproto);
848 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
850 if ((x = lookup_property_in_list (p, property)))
852 if (PROTOCOL_LIST (p))
853 return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property);
857 ; /* An identifier...if we could not find a protocol. */
863 /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
864 chain of interface hierarchy. */
866 lookup_property (tree interface_type, tree property)
868 tree inter = interface_type;
872 if ((x = lookup_property_in_list (inter, property)))
874 /* Failing that, look for the property in each category of the class. */
876 while ((category = CLASS_CATEGORY_LIST (category)))
878 if ((x = lookup_property_in_list (category, property)))
881 /* When checking a category, also check the protocols
882 attached with the category itself. */
883 if (CLASS_PROTOCOL_LIST (category)
884 && (x = lookup_property_in_protocol_list
885 (CLASS_PROTOCOL_LIST (category), property)))
889 /* Failing to find in categories, look for property in protocol list. */
890 if (CLASS_PROTOCOL_LIST (inter)
891 && (x = lookup_property_in_protocol_list
892 (CLASS_PROTOCOL_LIST (inter), property)))
895 /* Failing that, climb up the inheritance hierarchy. */
896 inter = lookup_interface (CLASS_SUPER_NAME (inter));
901 /* This routine is called by the parser when a
902 @property... declaration is found. 'decl' is the declaration of
903 the property (type/identifier), and the other arguments represent
904 property attributes that may have been specified in the Objective-C
905 declaration. 'parsed_property_readonly' is 'true' if the attribute
906 'readonly' was specified, and 'false' if not; similarly for the
907 other bool parameters. 'parsed_property_getter_ident' is NULL_TREE
908 if the attribute 'getter' was not specified, and is the identifier
909 corresponding to the specified getter if it was; similarly for
910 'parsed_property_setter_ident'. */
912 objc_add_property_declaration (location_t location, tree decl,
913 bool parsed_property_readonly, bool parsed_property_readwrite,
914 bool parsed_property_assign, bool parsed_property_retain,
915 bool parsed_property_copy, bool parsed_property_nonatomic,
916 tree parsed_property_getter_ident, tree parsed_property_setter_ident)
920 /* 'property_readonly' and 'property_assign_semantics' are the final
921 attributes of the property after all parsed attributes have been
922 considered (eg, if we parsed no 'readonly' and no 'readwrite', ie
923 parsed_property_readonly = false and parsed_property_readwrite =
924 false, then property_readonly will be false because the default
926 bool property_readonly = false;
927 objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN;
930 error_at (input_location, "%<@property%> is not available in Objective-C 1.0");
932 if (parsed_property_readonly && parsed_property_readwrite)
934 error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute");
935 /* In case of conflicting attributes (here and below), after
936 producing an error, we pick one of the attributes and keep
938 property_readonly = false;
942 if (parsed_property_readonly)
943 property_readonly = true;
945 if (parsed_property_readwrite)
946 property_readonly = false;
949 if (parsed_property_readonly && parsed_property_setter_ident)
951 /* Maybe this should be an error ? The Apple documentation says it is a warning. */
952 warning_at (location, 0, "%<readonly%> attribute conflicts with %<setter%> attribute");
953 property_readonly = false;
956 if (parsed_property_assign && parsed_property_retain)
958 error_at (location, "%<assign%> attribute conflicts with %<retain%> attribute");
959 property_assign_semantics = OBJC_PROPERTY_RETAIN;
961 else if (parsed_property_assign && parsed_property_copy)
963 error_at (location, "%<assign%> attribute conflicts with %<copy%> attribute");
964 property_assign_semantics = OBJC_PROPERTY_COPY;
966 else if (parsed_property_retain && parsed_property_copy)
968 error_at (location, "%<retain%> attribute conflicts with %<copy%> attribute");
969 property_assign_semantics = OBJC_PROPERTY_COPY;
973 if (parsed_property_assign)
974 property_assign_semantics = OBJC_PROPERTY_ASSIGN;
976 if (parsed_property_retain)
977 property_assign_semantics = OBJC_PROPERTY_RETAIN;
979 if (parsed_property_copy)
980 property_assign_semantics = OBJC_PROPERTY_COPY;
983 if (!objc_interface_context)
985 error_at (location, "property declaration not in @interface or @protocol context");
989 /* At this point we know that we are either in an interface, a
990 category, or a protocol. */
992 /* Check that the property does not have an initial value specified.
993 This should never happen as the parser doesn't allow this, but
994 it's just in case. */
995 if (DECL_INITIAL (decl))
997 error_at (location, "property can not have an initial value");
1001 /* TODO: Check that the property type is an Objective-C object or a "POD". */
1003 /* Implement -Wproperty-assign-default (which is enabled by default). */
1004 if (warn_property_assign_default
1005 /* If garbage collection is not being used, then 'assign' is
1006 valid for objects (and typically used for delegates) but it
1007 is wrong in most cases (since most objects need to be
1008 retained or copied in setters). Warn users when 'assign' is
1010 && property_assign_semantics == OBJC_PROPERTY_ASSIGN
1011 /* Read-only properties are never assigned, so the assignment
1012 semantics do not matter in that case. */
1013 && !property_readonly
1016 /* Please note that it would make sense to default to 'assign'
1017 for non-{Objective-C objects}, and to 'retain' for
1018 Objective-C objects. But that would break compatibility with
1020 if (!parsed_property_assign && !parsed_property_retain && !parsed_property_copy)
1022 /* Use 'false' so we do not warn for Class objects. */
1023 if (objc_type_valid_for_messaging (TREE_TYPE (decl), false))
1025 warning_at (location,
1027 "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>",
1030 "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it");
1035 if (property_assign_semantics == OBJC_PROPERTY_RETAIN
1036 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
1037 error_at (location, "%<retain%> attribute is only valid for Objective-C objects");
1039 if (property_assign_semantics == OBJC_PROPERTY_COPY
1040 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
1041 error_at (location, "%<copy%> attribute is only valid for Objective-C objects");
1043 /* Now determine the final property getter and setter names. They
1044 will be stored in the PROPERTY_DECL, from which they'll always be
1045 extracted and used. */
1047 /* Adjust, or fill in, setter and getter names. We overwrite the
1048 parsed_property_setter_ident and parsed_property_getter_ident
1049 with the final setter and getter identifiers that will be
1051 if (parsed_property_setter_ident)
1053 /* The setter should be terminated by ':', but the parser only
1054 gives us an identifier without ':'. So, we need to add ':'
1056 const char *parsed_setter = IDENTIFIER_POINTER (parsed_property_setter_ident);
1057 size_t length = strlen (parsed_setter);
1058 char *final_setter = (char *)alloca (length + 2);
1060 sprintf (final_setter, "%s:", parsed_setter);
1061 parsed_property_setter_ident = get_identifier (final_setter);
1065 if (!property_readonly)
1066 parsed_property_setter_ident = get_identifier (objc_build_property_setter_name
1067 (DECL_NAME (decl)));
1070 if (!parsed_property_getter_ident)
1071 parsed_property_getter_ident = DECL_NAME (decl);
1073 /* Check for duplicate property declarations. We first check the
1074 immediate context for a property with the same name. Any such
1075 declarations are an error. */
1076 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
1078 if (PROPERTY_NAME (x) == DECL_NAME (decl))
1080 location_t original_location = DECL_SOURCE_LOCATION (x);
1082 error_at (location, "redeclaration of property %qD", decl);
1084 if (original_location != UNKNOWN_LOCATION)
1085 inform (original_location, "originally specified here");
1090 /* We now need to check for existing property declarations (in the
1091 superclass, other categories or protocols) and check that the new
1092 declaration is not in conflict with existing ones. */
1094 /* Search for a previous, existing declaration of a property with
1095 the same name in superclasses, protocols etc. If one is found,
1096 it will be in the 'x' variable. */
1099 /* Note that, for simplicity, the following may search again the
1100 local context. That's Ok as nothing will be found (else we'd
1101 have thrown an error above); it's only a little inefficient, but
1102 the code is simpler. */
1103 switch (TREE_CODE (objc_interface_context))
1105 case CLASS_INTERFACE_TYPE:
1106 /* Look up the property in the current @interface (which will
1107 find nothing), then its protocols and categories and
1109 x = lookup_property (objc_interface_context, DECL_NAME (decl));
1111 case CATEGORY_INTERFACE_TYPE:
1112 /* Look up the property in the main @interface, then protocols
1113 and categories (one of them is ours, and will find nothing)
1114 and superclasses. */
1115 x = lookup_property (lookup_interface (CLASS_NAME (objc_interface_context)),
1118 case PROTOCOL_INTERFACE_TYPE:
1119 /* Looks up the property in any protocols attached to the
1120 current protocol. */
1121 if (PROTOCOL_LIST (objc_interface_context))
1123 x = lookup_property_in_protocol_list (PROTOCOL_LIST (objc_interface_context),
1133 /* An existing property was found; check that it has the same
1134 types, or it is compatible. */
1135 location_t original_location = DECL_SOURCE_LOCATION (x);
1137 if (PROPERTY_NONATOMIC (x) != parsed_property_nonatomic)
1139 error_at (location, "'nonatomic' attribute of property %qD conflicts with previous declaration", decl);
1141 if (original_location != UNKNOWN_LOCATION)
1142 inform (original_location, "originally specified here");
1146 if (PROPERTY_GETTER_NAME (x) != parsed_property_getter_ident)
1148 error_at (location, "'getter' attribute of property %qD conflicts with previous declaration", decl);
1150 if (original_location != UNKNOWN_LOCATION)
1151 inform (original_location, "originally specified here");
1155 /* We can only compare the setter names if both the old and new property have a setter. */
1156 if (!property_readonly && !PROPERTY_READONLY(x))
1158 if (PROPERTY_SETTER_NAME (x) != parsed_property_setter_ident)
1160 error_at (location, "'setter' attribute of property %qD conflicts with previous declaration", decl);
1162 if (original_location != UNKNOWN_LOCATION)
1163 inform (original_location, "originally specified here");
1168 if (PROPERTY_ASSIGN_SEMANTICS (x) != property_assign_semantics)
1170 error_at (location, "assign semantics attributes of property %qD conflict with previous declaration", decl);
1172 if (original_location != UNKNOWN_LOCATION)
1173 inform (original_location, "originally specified here");
1177 /* It's ok to have a readonly property that becomes a readwrite, but not vice versa. */
1178 if (PROPERTY_READONLY (x) == 0 && property_readonly == 1)
1180 error_at (location, "'readonly' attribute of property %qD conflicts with previous declaration", decl);
1182 if (original_location != UNKNOWN_LOCATION)
1183 inform (original_location, "originally specified here");
1187 /* We now check that the new and old property declarations have
1188 the same types (or compatible one). In the Objective-C
1189 tradition of loose type checking, we do type-checking but
1190 only generate warnings (not errors) if they do not match.
1191 For non-readonly properties, the types must match exactly;
1192 for readonly properties, it is allowed to use a "more
1193 specialized" type in the new property declaration. Eg, the
1194 superclass has a getter returning (NSArray *) and the
1195 subclass a getter returning (NSMutableArray *). The object's
1196 getter returns an (NSMutableArray *); but if you cast the
1197 object to the superclass, which is allowed, you'd still
1198 expect the getter to return an (NSArray *), which works since
1199 an (NSMutableArray *) is an (NSArray *) too. So, the set of
1200 objects belonging to the type of the new @property should be
1201 a subset of the set of objects belonging to the type of the
1202 old @property. This is what "specialization" means. And the
1203 reason it only applies to readonly properties is that for a
1204 readwrite property the setter would have the opposite
1205 requirement - ie that the superclass type is more specialized
1206 then the subclass one; hence the only way to satisfy both
1207 constraints is that the types match. */
1209 /* If the types are not the same in the C sense, we warn ... */
1210 if (!comptypes (TREE_TYPE (x), TREE_TYPE (decl))
1211 /* ... unless the property is readonly, in which case we
1212 allow a new, more specialized, declaration. */
1213 && (!property_readonly
1214 || !objc_compare_types (TREE_TYPE (x),
1215 TREE_TYPE (decl), -5, NULL_TREE)))
1217 warning_at (location, 0,
1218 "type of property %qD conflicts with previous declaration", decl);
1219 if (original_location != UNKNOWN_LOCATION)
1220 inform (original_location, "originally specified here");
1225 /* Create a PROPERTY_DECL node. */
1226 property_decl = make_node (PROPERTY_DECL);
1228 /* Copy the basic information from the original decl. */
1229 TREE_TYPE (property_decl) = TREE_TYPE (decl);
1230 DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl);
1231 TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl);
1233 /* Add property-specific information. */
1234 PROPERTY_NAME (property_decl) = DECL_NAME (decl);
1235 PROPERTY_GETTER_NAME (property_decl) = parsed_property_getter_ident;
1236 PROPERTY_SETTER_NAME (property_decl) = parsed_property_setter_ident;
1237 PROPERTY_READONLY (property_decl) = property_readonly;
1238 PROPERTY_NONATOMIC (property_decl) = parsed_property_nonatomic;
1239 PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics;
1240 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1241 PROPERTY_DYNAMIC (property_decl) = 0;
1243 /* Note that PROPERTY_GETTER_NAME is always set for all
1244 PROPERTY_DECLs, and PROPERTY_SETTER_NAME is always set for all
1245 PROPERTY_DECLs where PROPERTY_READONLY == 0. Any time we deal
1246 with a getter or setter, we should get the PROPERTY_DECL and use
1247 PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know the correct
1250 /* Add the PROPERTY_DECL to the list of properties for the class. */
1251 TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context);
1252 CLASS_PROPERTY_DECL (objc_interface_context) = property_decl;
1255 /* This is a subroutine of objc_maybe_build_component_ref. Search the
1256 list of methods in the interface (and, failing that, the local list
1257 in the implementation, and failing that, the protocol list)
1258 provided for a 'setter' or 'getter' for 'component' with default
1259 names (ie, if 'component' is "name", then search for "name" and
1260 "setName:"). If any is found, then create an artificial property
1261 that uses them. Return NULL_TREE if 'getter' or 'setter' could not
1264 maybe_make_artificial_property_decl (tree interface, tree implementation,
1265 tree protocol_list, tree component, bool is_class)
1267 tree getter_name = component;
1268 tree setter_name = get_identifier (objc_build_property_setter_name (component));
1269 tree getter = NULL_TREE;
1270 tree setter = NULL_TREE;
1272 /* First, check the @interface and all superclasses. */
1277 /* Using instance methods of the root class as accessors is most
1278 likely unwanted and can be extremely confusing (and, most
1279 importantly, other Objective-C 2.0 compilers do not do it).
1282 flags = OBJC_LOOKUP_CLASS | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS;
1284 getter = lookup_method_static (interface, getter_name, flags);
1285 setter = lookup_method_static (interface, setter_name, flags);
1288 /* Second, check the local @implementation context. */
1289 if (!getter && !setter)
1295 getter = lookup_method (CLASS_CLS_METHODS (implementation), getter_name);
1296 setter = lookup_method (CLASS_CLS_METHODS (implementation), setter_name);
1300 getter = lookup_method (CLASS_NST_METHODS (implementation), getter_name);
1301 setter = lookup_method (CLASS_NST_METHODS (implementation), setter_name);
1306 /* Try the protocol_list if we didn't find anything in the
1307 @interface and in the @implementation. */
1308 if (!getter && !setter)
1310 getter = lookup_method_in_protocol_list (protocol_list, getter_name, is_class);
1311 setter = lookup_method_in_protocol_list (protocol_list, setter_name, is_class);
1314 /* There needs to be at least a getter or setter for this to be a
1315 valid 'object.component' syntax. */
1316 if (getter || setter)
1318 /* Yes ... determine the type of the expression. */
1323 type = TREE_VALUE (TREE_TYPE (getter));
1325 type = TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter)));
1327 /* Create an artificial property declaration with the
1328 information we collected on the type and getter/setter
1330 property_decl = make_node (PROPERTY_DECL);
1332 TREE_TYPE (property_decl) = type;
1333 DECL_SOURCE_LOCATION (property_decl) = input_location;
1334 TREE_DEPRECATED (property_decl) = 0;
1335 DECL_ARTIFICIAL (property_decl) = 1;
1337 /* Add property-specific information. Note that one of
1338 PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a
1339 non-existing method; this will generate an error when the
1340 expression is later compiled. At this stage we don't know if
1341 the getter or setter will be used, so we can't generate an
1343 PROPERTY_NAME (property_decl) = component;
1344 PROPERTY_GETTER_NAME (property_decl) = getter_name;
1345 PROPERTY_SETTER_NAME (property_decl) = setter_name;
1346 PROPERTY_READONLY (property_decl) = 0;
1347 PROPERTY_NONATOMIC (property_decl) = 0;
1348 PROPERTY_ASSIGN_SEMANTICS (property_decl) = 0;
1349 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1350 PROPERTY_DYNAMIC (property_decl) = 0;
1353 PROPERTY_HAS_NO_GETTER (property_decl) = 1;
1355 /* The following is currently unused, but it's nice to have
1356 there. We may use it if we need in the future. */
1358 PROPERTY_HAS_NO_SETTER (property_decl) = 1;
1360 return property_decl;
1366 /* This hook routine is invoked by the parser when an expression such
1367 as 'xxx.yyy' is parsed. We get a chance to process these
1368 expressions in a way that is specified to Objective-C (to implement
1369 the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars).
1370 If the expression is not an Objective-C specified expression, we
1371 should return NULL_TREE; else we return the expression.
1373 At the moment this only implements dot-syntax and properties (not
1374 non-fragile ivars yet), ie 'object.property' or 'object.component'
1375 where 'component' is not a declared property, but a valid getter or
1376 setter for it could be found. */
1378 objc_maybe_build_component_ref (tree object, tree property_ident)
1383 /* If we are in Objective-C 1.0 mode, dot-syntax and properties are
1385 if (flag_objc1_only)
1388 /* Try to determine if 'object' is an Objective-C object or not. If
1390 if (object == NULL_TREE || object == error_mark_node
1391 || (rtype = TREE_TYPE (object)) == NULL_TREE)
1394 if (property_ident == NULL_TREE || property_ident == error_mark_node
1395 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1398 /* The following analysis of 'object' is similar to the one used for
1399 the 'receiver' of a method invocation. We need to determine what
1400 'object' is and find the appropriate property (either declared,
1401 or artificial) for it (in the same way as we need to find the
1402 appropriate method prototype for a method invocation). There are
1403 some simplifications here though: "object.property" is invalid if
1404 "object" has a type of "id" or "Class"; it must at least have a
1405 protocol attached to it, and "object" is never a class name as
1406 that is done by objc_build_class_component_ref. Finally, we
1407 don't know if this really is a dot-syntax expression, so we want
1408 to make a quick exit if it is not; for this reason, we try to
1409 postpone checks after determining that 'object' looks like an
1410 Objective-C object. */
1412 if (objc_is_id (rtype))
1414 /* This is the case that the 'object' is of type 'id' or
1417 /* Check if at least it is of type 'id <Protocol>' or 'Class
1418 <Protocol>'; if so, look the property up in the
1420 if (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype)))
1422 tree rprotos = TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype));
1426 /* No point looking up declared @properties if we are
1427 dealing with a class. Classes have no declared
1429 if (!IS_CLASS (rtype))
1430 x = lookup_property_in_protocol_list (rprotos, property_ident);
1434 /* Ok, no property. Maybe it was an
1435 object.component dot-syntax without a declared
1436 property (this is valid for classes too). Look
1437 for getter/setter methods and internally declare
1438 an artifical property based on them if found. */
1439 x = maybe_make_artificial_property_decl (NULL_TREE,
1447 else if (objc_method_context)
1449 /* Else, if we are inside a method it could be the case of
1450 'super' or 'self'. */
1451 tree interface_type = NULL_TREE;
1453 while (TREE_CODE (t) == COMPOUND_EXPR
1454 || TREE_CODE (t) == MODIFY_EXPR
1455 || CONVERT_EXPR_P (t)
1456 || TREE_CODE (t) == COMPONENT_REF)
1457 t = TREE_OPERAND (t, 0);
1459 if (t == UOBJC_SUPER_decl)
1461 /* TODO: Check if this is correct also for 'super' in categories. */
1462 interface_type = lookup_interface (CLASS_SUPER_NAME (implementation_template));
1464 else if (t == self_decl)
1465 interface_type = lookup_interface (CLASS_NAME (implementation_template));
1469 if (TREE_CODE (objc_method_context) != CLASS_METHOD_DECL)
1470 x = lookup_property (interface_type, property_ident);
1474 /* Try the dot-syntax without a declared property.
1475 If this is an access to 'self', it is possible
1476 that they may refer to a setter/getter that is
1477 not declared in the interface, but exists locally
1478 in the implementation. In that case, get the
1479 implementation context and use it. */
1480 tree implementation = NULL_TREE;
1483 implementation = objc_implementation_context;
1485 x = maybe_make_artificial_property_decl
1486 (interface_type, implementation, NULL_TREE,
1488 (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL));
1495 /* This is the case where we have more information on 'rtype'. */
1496 tree basetype = TYPE_MAIN_VARIANT (rtype);
1498 /* Skip the pointer - if none, it's not an Objective-C object or
1500 if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE)
1501 basetype = TREE_TYPE (basetype);
1505 /* Traverse typedefs. */
1506 while (basetype != NULL_TREE
1507 && TREE_CODE (basetype) == RECORD_TYPE
1508 && OBJC_TYPE_NAME (basetype)
1509 && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL
1510 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)))
1511 basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype));
1513 if (basetype != NULL_TREE && TYPED_OBJECT (basetype))
1515 tree interface_type = TYPE_OBJC_INTERFACE (basetype);
1516 tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype);
1519 && (TREE_CODE (interface_type) == CLASS_INTERFACE_TYPE
1520 || TREE_CODE (interface_type) == CATEGORY_INTERFACE_TYPE
1521 || TREE_CODE (interface_type) == PROTOCOL_INTERFACE_TYPE))
1523 /* Not sure 'rtype' could ever be a class here! Just
1524 for safety we keep the checks. */
1525 if (!IS_CLASS (rtype))
1527 x = lookup_property (interface_type, property_ident);
1530 x = lookup_property_in_protocol_list (protocol_list,
1536 /* Try the dot-syntax without a declared property.
1537 If we are inside a method implementation, it is
1538 possible that they may refer to a setter/getter
1539 that is not declared in the interface, but exists
1540 locally in the implementation. In that case, get
1541 the implementation context and use it. */
1542 tree implementation = NULL_TREE;
1544 if (objc_implementation_context
1545 && CLASS_NAME (objc_implementation_context)
1546 == OBJC_TYPE_NAME (interface_type))
1547 implementation = objc_implementation_context;
1549 x = maybe_make_artificial_property_decl (interface_type,
1564 /* We have an additional nasty problem here; if this
1565 PROPERTY_REF needs to become a 'getter', then the conversion
1566 from PROPERTY_REF into a getter call happens in gimplify,
1567 after the selector table has already been generated and when
1568 it is too late to add another selector to it. To work around
1569 the problem, we always create the getter call at this stage,
1570 which puts the selector in the table. Note that if the
1571 PROPERTY_REF becomes a 'setter' instead of a 'getter', then
1572 we have added a selector too many to the selector table.
1573 This is a little inefficient.
1575 Also note that method calls to 'self' and 'super' require the
1576 context (self_decl, UOBJS_SUPER_decl,
1577 objc_implementation_context etc) to be built correctly; this
1578 is yet another reason why building the call at the gimplify
1579 stage (when this context has been lost) is not very
1580 practical. If we build it at this stage, we know it will
1581 always be built correctly.
1583 If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial
1584 property decl created to deal with a dotsyntax not really
1585 referring to an existing property) then do not try to build a
1586 call to the getter as there is no getter. */
1587 if (PROPERTY_HAS_NO_GETTER (x))
1588 getter_call = NULL_TREE;
1590 getter_call = objc_finish_message_expr (object,
1591 PROPERTY_GETTER_NAME (x),
1594 if (TREE_DEPRECATED (x))
1595 warn_deprecated_use (x, NULL_TREE);
1597 expression = build3 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call);
1598 SET_EXPR_LOCATION (expression, input_location);
1599 TREE_SIDE_EFFECTS (expression) = 1;
1607 /* This hook routine is invoked by the parser when an expression such
1608 as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the
1609 Objective-C 2.0 dot-syntax applied to classes, so we need to
1610 convert it into a setter/getter call on the class. */
1612 objc_build_class_component_ref (tree class_name, tree property_ident)
1617 if (flag_objc1_only)
1618 error_at (input_location, "the dot syntax is not available in Objective-C 1.0");
1620 if (class_name == NULL_TREE || class_name == error_mark_node
1621 || TREE_CODE (class_name) != IDENTIFIER_NODE)
1622 return error_mark_node;
1624 if (property_ident == NULL_TREE || property_ident == error_mark_node
1625 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1628 object = objc_get_class_reference (class_name);
1631 /* We know that 'class_name' is an Objective-C class name as the
1632 parser won't call this function if it is not. This is only a
1633 double-check for safety. */
1634 error_at (input_location, "could not find class %qE", class_name);
1635 return error_mark_node;
1638 rtype = lookup_interface (class_name);
1641 /* Again, this should never happen, but we do check. */
1642 error_at (input_location, "could not find interface for class %qE", class_name);
1643 return error_mark_node;
1646 x = maybe_make_artificial_property_decl (rtype, NULL_TREE, NULL_TREE,
1655 if (PROPERTY_HAS_NO_GETTER (x))
1656 getter_call = NULL_TREE;
1658 getter_call = objc_finish_message_expr (object,
1659 PROPERTY_GETTER_NAME (x),
1661 if (TREE_DEPRECATED (x))
1662 warn_deprecated_use (x, NULL_TREE);
1664 expression = build3 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call);
1665 SET_EXPR_LOCATION (expression, input_location);
1666 TREE_SIDE_EFFECTS (expression) = 1;
1672 error_at (input_location, "could not find setter/getter for %qE in class %qE",
1673 property_ident, class_name);
1674 return error_mark_node;
1682 /* This is used because we don't want to expose PROPERTY_REF to the
1683 C/C++ frontends. Maybe we should! */
1685 objc_is_property_ref (tree node)
1687 if (node && TREE_CODE (node) == PROPERTY_REF)
1693 /* This hook routine is called when a MODIFY_EXPR is being built. We
1694 check what is being modified; if it is a PROPERTY_REF, we need to
1695 generate a 'setter' function call for the property. If this is not
1696 a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go
1697 on creating their MODIFY_EXPR.
1699 This is used for example if you write
1703 where 'count' is a property. The left-hand side creates a
1704 PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR
1705 to assign something to it. We intercept that here, and generate a
1706 call to the 'setter' method instead. */
1708 objc_maybe_build_modify_expr (tree lhs, tree rhs)
1710 if (lhs && TREE_CODE (lhs) == PROPERTY_REF)
1712 tree object_expr = PROPERTY_REF_OBJECT (lhs);
1713 tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs);
1715 if (PROPERTY_READONLY (property_decl))
1717 error ("readonly property can not be set");
1718 return error_mark_node;
1722 tree setter_argument = build_tree_list (NULL_TREE, rhs);
1725 /* TODO: Check that the setter return type is 'void'. */
1727 /* TODO: Decay argument in C. */
1728 setter = objc_finish_message_expr (object_expr,
1729 PROPERTY_SETTER_NAME (property_decl),
1739 objc_build_method_signature (bool is_class_method, tree rettype, tree selector,
1740 tree optparms, bool ellipsis)
1742 if (is_class_method)
1743 return build_method_decl (CLASS_METHOD_DECL, rettype, selector,
1744 optparms, ellipsis);
1746 return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector,
1747 optparms, ellipsis);
1751 objc_add_method_declaration (bool is_class_method, tree decl, tree attributes)
1753 if (!objc_interface_context)
1755 /* PS: At the moment, due to how the parser works, it should be
1756 impossible to get here. But it's good to have the check in
1757 case the parser changes.
1759 fatal_error ("method declaration not in @interface context");
1762 if (flag_objc1_only && attributes)
1763 error_at (input_location, "method attributes are not available in Objective-C 1.0");
1765 objc_decl_method_attributes (&decl, attributes, 0);
1766 objc_add_method (objc_interface_context,
1769 objc_method_optional_flag);
1772 /* Return 'true' if the method definition could be started, and
1773 'false' if not (because we are outside an @implementation context).
1776 objc_start_method_definition (bool is_class_method, tree decl, tree attributes)
1778 if (!objc_implementation_context)
1780 error ("method definition not in @implementation context");
1784 if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node)
1788 /* Indicate no valid break/continue context by setting these variables
1789 to some non-null, non-label value. We'll notice and emit the proper
1790 error message in c_finish_bc_stmt. */
1791 c_break_label = c_cont_label = size_zero_node;
1795 warning_at (input_location, 0, "method attributes can not be specified in @implementation context");
1797 objc_decl_method_attributes (&decl, attributes, 0);
1799 objc_add_method (objc_implementation_context,
1802 /* is optional */ false);
1803 start_method_def (decl);
1808 objc_add_instance_variable (tree decl)
1810 (void) add_instance_variable (objc_ivar_context,
1811 objc_ivar_visibility,
1815 /* Return true if TYPE is 'id'. */
1818 objc_is_object_id (tree type)
1820 return OBJC_TYPE_NAME (type) == objc_object_id;
1824 objc_is_class_id (tree type)
1826 return OBJC_TYPE_NAME (type) == objc_class_id;
1829 /* Construct a C struct with same name as KLASS, a base struct with tag
1830 SUPER_NAME (if any), and FIELDS indicated. */
1833 objc_build_struct (tree klass, tree fields, tree super_name)
1835 tree name = CLASS_NAME (klass);
1836 tree s = objc_start_struct (name);
1837 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
1839 VEC(tree,heap) *objc_info = NULL;
1844 /* Prepend a packed variant of the base class into the layout. This
1845 is necessary to preserve ObjC ABI compatibility. */
1846 tree base = build_decl (input_location,
1847 FIELD_DECL, NULL_TREE, super);
1848 tree field = TYPE_FIELDS (super);
1850 while (field && DECL_CHAIN (field)
1851 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
1852 field = DECL_CHAIN (field);
1854 /* For ObjC ABI purposes, the "packed" size of a base class is
1855 the sum of the offset and the size (in bits) of the last field
1858 = (field && TREE_CODE (field) == FIELD_DECL
1859 ? size_binop (PLUS_EXPR,
1860 size_binop (PLUS_EXPR,
1863 convert (bitsizetype,
1864 DECL_FIELD_OFFSET (field)),
1865 bitsize_int (BITS_PER_UNIT)),
1866 DECL_FIELD_BIT_OFFSET (field)),
1868 : bitsize_zero_node);
1869 DECL_SIZE_UNIT (base)
1870 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
1871 size_int (BITS_PER_UNIT));
1872 DECL_ARTIFICIAL (base) = 1;
1873 DECL_ALIGN (base) = 1;
1874 DECL_FIELD_CONTEXT (base) = s;
1876 DECL_FIELD_IS_BASE (base) = 1;
1879 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
1880 #endif /* are following the ObjC ABI here. */
1881 DECL_CHAIN (base) = fields;
1885 /* NB: Calling finish_struct() may cause type TYPE_LANG_SPECIFIC fields
1886 in all variants of this RECORD_TYPE to be clobbered, but it is therein
1887 that we store protocol conformance info (e.g., 'NSObject <MyProtocol>').
1888 Hence, we must squirrel away the ObjC-specific information before calling
1889 finish_struct(), and then reinstate it afterwards. */
1891 for (t = TYPE_NEXT_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
1893 if (!TYPE_HAS_OBJC_INFO (t))
1895 INIT_TYPE_OBJC_INFO (t);
1896 TYPE_OBJC_INTERFACE (t) = klass;
1898 VEC_safe_push (tree, heap, objc_info, TYPE_OBJC_INFO (t));
1901 /* Point the struct at its related Objective-C class. */
1902 INIT_TYPE_OBJC_INFO (s);
1903 TYPE_OBJC_INTERFACE (s) = klass;
1905 s = objc_finish_struct (s, fields);
1907 for (i = 0, t = TYPE_NEXT_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
1909 TYPE_OBJC_INFO (t) = VEC_index (tree, objc_info, i);
1910 /* Replace the IDENTIFIER_NODE with an actual @interface. */
1911 TYPE_OBJC_INTERFACE (t) = klass;
1913 VEC_free (tree, heap, objc_info);
1915 /* Use TYPE_BINFO structures to point at the super class, if any. */
1916 objc_xref_basetypes (s, super);
1918 /* Mark this struct as a class template. */
1919 CLASS_STATIC_TEMPLATE (klass) = s;
1924 /* Build a type differing from TYPE only in that TYPE_VOLATILE is set.
1925 Unlike tree.c:build_qualified_type(), preserve TYPE_LANG_SPECIFIC in the
1928 objc_build_volatilized_type (tree type)
1932 /* Check if we have not constructed the desired variant already. */
1933 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1935 /* The type qualifiers must (obviously) match up. */
1936 if (!TYPE_VOLATILE (t)
1937 || (TYPE_READONLY (t) != TYPE_READONLY (type))
1938 || (TYPE_RESTRICT (t) != TYPE_RESTRICT (type)))
1941 /* For pointer types, the pointees (and hence their TYPE_LANG_SPECIFIC
1942 info, if any) must match up. */
1943 if (POINTER_TYPE_P (t)
1944 && (TREE_TYPE (t) != TREE_TYPE (type)))
1947 /* Only match up the types which were previously volatilized in similar fashion and not
1948 because they were declared as such. */
1949 if (!lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (t)))
1952 /* Everything matches up! */
1956 /* Ok, we could not re-use any of the pre-existing variants. Create
1958 t = build_variant_type_copy (type);
1959 TYPE_VOLATILE (t) = 1;
1961 TYPE_ATTRIBUTES (t) = merge_attributes (TYPE_ATTRIBUTES (type),
1962 tree_cons (get_identifier ("objc_volatilized"),
1965 if (TREE_CODE (t) == ARRAY_TYPE)
1966 TREE_TYPE (t) = objc_build_volatilized_type (TREE_TYPE (t));
1968 /* Set up the canonical type information. */
1969 if (TYPE_STRUCTURAL_EQUALITY_P (type))
1970 SET_TYPE_STRUCTURAL_EQUALITY (t);
1971 else if (TYPE_CANONICAL (type) != type)
1972 TYPE_CANONICAL (t) = objc_build_volatilized_type (TYPE_CANONICAL (type));
1974 TYPE_CANONICAL (t) = t;
1979 /* Mark DECL as being 'volatile' for purposes of Darwin
1980 _setjmp()/_longjmp() exception handling. Called from
1981 objc_mark_locals_volatile(). */
1983 objc_volatilize_decl (tree decl)
1985 /* Do not mess with variables that are 'static' or (already)
1987 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
1988 && (TREE_CODE (decl) == VAR_DECL
1989 || TREE_CODE (decl) == PARM_DECL))
1991 tree t = TREE_TYPE (decl);
1993 t = objc_build_volatilized_type (t);
1995 TREE_TYPE (decl) = t;
1996 TREE_THIS_VOLATILE (decl) = 1;
1997 TREE_SIDE_EFFECTS (decl) = 1;
1998 DECL_REGISTER (decl) = 0;
2000 C_DECL_REGISTER (decl) = 0;
2005 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
2006 (including its categories and superclasses) or by object type TYP.
2007 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
2010 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
2012 bool class_type = (cls != NULL_TREE);
2018 /* Check protocols adopted by the class and its categories. */
2019 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
2021 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
2025 /* Repeat for superclasses. */
2026 cls = lookup_interface (CLASS_SUPER_NAME (cls));
2029 /* Check for any protocols attached directly to the object type. */
2030 if (TYPE_HAS_OBJC_INFO (typ))
2032 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
2039 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
2040 /* NB: Types 'id' and 'Class' cannot reasonably be described as
2041 "implementing" a given protocol, since they do not have an
2044 warning (0, "class %qs does not implement the %qE protocol",
2045 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2047 warning (0, "type %qs does not conform to the %qE protocol",
2048 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2054 /* Check if class RCLS and instance struct type RTYP conform to at least the
2055 same protocols that LCLS and LTYP conform to. */
2058 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
2061 bool have_lproto = false;
2065 /* NB: We do _not_ look at categories defined for LCLS; these may or
2066 may not get loaded in, and therefore it is unreasonable to require
2067 that RCLS/RTYP must implement any of their protocols. */
2068 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
2072 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2076 /* Repeat for superclasses. */
2077 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
2080 /* Check for any protocols attached directly to the object type. */
2081 if (TYPE_HAS_OBJC_INFO (ltyp))
2083 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
2087 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2092 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
2093 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
2094 away with simply checking for 'id' or 'Class' (!RCLS), since this
2095 routine will not get called in other cases. */
2096 return have_lproto || (rcls != NULL_TREE);
2099 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
2100 Both TYPE1 and TYPE2 must be pointers, and already determined to be
2101 compatible by objc_compare_types() below. */
2104 objc_common_type (tree type1, tree type2)
2106 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
2108 while (POINTER_TYPE_P (inner1))
2110 inner1 = TREE_TYPE (inner1);
2111 inner2 = TREE_TYPE (inner2);
2114 /* If one type is derived from another, return the base type. */
2115 if (DERIVED_FROM_P (inner1, inner2))
2117 else if (DERIVED_FROM_P (inner2, inner1))
2120 /* If both types are 'Class', return 'Class'. */
2121 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
2122 return objc_class_type;
2124 /* Otherwise, return 'id'. */
2125 return objc_object_type;
2128 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
2129 an instance of RTYP to an instance of LTYP or to compare the two
2130 (if ARGNO is equal to -3), per ObjC type system rules. Before
2131 returning 'true', this routine may issue warnings related to, e.g.,
2132 protocol conformance. When returning 'false', the routine must
2133 produce absolutely no warnings; the C or C++ front-end will do so
2134 instead, if needed. If either LTYP or RTYP is not an Objective-C
2135 type, the routine must return 'false'.
2137 The ARGNO parameter is encoded as follows:
2138 >= 1 Parameter number (CALLEE contains function being called);
2142 -3 Comparison (LTYP and RTYP may match in either direction);
2143 -4 Silent comparison (for C++ overload resolution);
2144 -5 Silent "specialization" comparison for RTYP to be a "specialization"
2145 of LTYP (a specialization means that RTYP is LTYP plus some constraints,
2146 so that each object of type RTYP is also of type LTYP). This is used
2147 when comparing property types. */
2150 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
2152 tree lcls, rcls, lproto, rproto;
2153 bool pointers_compatible;
2155 /* We must be dealing with pointer types */
2156 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
2161 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2162 rtyp = TREE_TYPE (rtyp);
2164 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2166 /* We must also handle function pointers, since ObjC is a bit more
2167 lenient than C or C++ on this. */
2168 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
2170 /* Return types must be covariant. */
2171 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
2172 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
2176 /* Argument types must be contravariant. */
2177 for (ltyp = TYPE_ARG_TYPES (ltyp), rtyp = TYPE_ARG_TYPES (rtyp);
2178 ltyp && rtyp; ltyp = TREE_CHAIN (ltyp), rtyp = TREE_CHAIN (rtyp))
2180 if (!comptypes (TREE_VALUE (rtyp), TREE_VALUE (ltyp))
2181 && !objc_compare_types (TREE_VALUE (rtyp), TREE_VALUE (ltyp),
2186 return (ltyp == rtyp);
2189 /* Past this point, we are only interested in ObjC class instances,
2190 or 'id' or 'Class'. */
2191 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
2194 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
2195 && !TYPE_HAS_OBJC_INFO (ltyp))
2198 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
2199 && !TYPE_HAS_OBJC_INFO (rtyp))
2202 /* Past this point, we are committed to returning 'true' to the caller
2203 (unless performing a silent comparison; see below). However, we can
2204 still warn about type and/or protocol mismatches. */
2206 if (TYPE_HAS_OBJC_INFO (ltyp))
2208 lcls = TYPE_OBJC_INTERFACE (ltyp);
2209 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
2212 lcls = lproto = NULL_TREE;
2214 if (TYPE_HAS_OBJC_INFO (rtyp))
2216 rcls = TYPE_OBJC_INTERFACE (rtyp);
2217 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
2220 rcls = rproto = NULL_TREE;
2222 /* If we could not find an @interface declaration, we must have
2223 only seen a @class declaration; for purposes of type comparison,
2224 treat it as a stand-alone (root) class. */
2226 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
2229 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
2232 /* If either type is an unqualified 'id', we're done. This is because
2233 an 'id' can be assigned to or from any type with no warnings. */
2236 if ((!lproto && objc_is_object_id (ltyp))
2237 || (!rproto && objc_is_object_id (rtyp)))
2242 /* For property checks, though, an 'id' is considered the most
2243 general type of object, hence if you try to specialize an
2244 'NSArray *' (ltyp) property with an 'id' (rtyp) one, we need
2246 if (!lproto && objc_is_object_id (ltyp))
2250 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
2252 /* If the underlying types are the same, and at most one of them has
2253 a protocol list, we do not need to issue any diagnostics. */
2254 if (pointers_compatible && (!lproto || !rproto))
2257 /* If exactly one of the types is 'Class', issue a diagnostic; any
2258 exceptions of this rule have already been handled. */
2259 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
2260 pointers_compatible = false;
2261 /* Otherwise, check for inheritance relations. */
2264 if (!pointers_compatible)
2266 /* Again, if any of the two is an 'id', we're satisfied,
2267 unless we're comparing properties, in which case only an
2268 'id' on the left-hand side (old property) is good
2272 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
2274 pointers_compatible = objc_is_object_id (ltyp);
2277 if (!pointers_compatible)
2278 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
2280 if (!pointers_compatible && (argno == -3 || argno == -4))
2281 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
2284 /* If the pointers match modulo protocols, check for protocol conformance
2286 if (pointers_compatible)
2288 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
2291 if (!pointers_compatible && argno == -3)
2292 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
2296 if (!pointers_compatible)
2298 /* The two pointers are not exactly compatible. Issue a warning, unless
2299 we are performing a silent comparison, in which case return 'false'
2301 /* NB: For the time being, we shall make our warnings look like their
2302 C counterparts. In the future, we may wish to make them more
2311 warning (0, "comparison of distinct Objective-C types lacks a cast");
2315 warning (0, "initialization from distinct Objective-C type");
2319 warning (0, "assignment from distinct Objective-C type");
2323 warning (0, "distinct Objective-C type in return");
2327 warning (0, "passing argument %d of %qE from distinct "
2328 "Objective-C type", argno, callee);
2336 /* This routine is similar to objc_compare_types except that function-pointers are
2337 excluded. This is because, caller assumes that common types are of (id, Object*)
2338 variety and calls objc_common_type to obtain a common type. There is no commonolty
2339 between two function-pointers in this regard. */
2342 objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
2344 if (objc_compare_types (ltyp, rtyp, argno, callee))
2346 /* exclude function-pointer types. */
2349 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2350 rtyp = TREE_TYPE (rtyp);
2352 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2353 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
2358 /* Check if LTYP and RTYP have the same type qualifiers. If either type
2359 lives in the volatilized hash table, ignore the 'volatile' bit when
2360 making the comparison. */
2363 objc_type_quals_match (tree ltyp, tree rtyp)
2365 int lquals = TYPE_QUALS (ltyp), rquals = TYPE_QUALS (rtyp);
2367 if (lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (ltyp)))
2368 lquals &= ~TYPE_QUAL_VOLATILE;
2370 if (lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (rtyp)))
2371 rquals &= ~TYPE_QUAL_VOLATILE;
2373 return (lquals == rquals);
2377 /* Determine if CHILD is derived from PARENT. The routine assumes that
2378 both parameters are RECORD_TYPEs, and is non-reflexive. */
2381 objc_derived_from_p (tree parent, tree child)
2383 parent = TYPE_MAIN_VARIANT (parent);
2385 for (child = TYPE_MAIN_VARIANT (child);
2386 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
2388 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
2389 (TYPE_BINFO (child),
2392 if (child == parent)
2401 objc_build_component_ref (tree datum, tree component)
2403 /* If COMPONENT is NULL, the caller is referring to the anonymous
2404 base class field. */
2407 tree base = TYPE_FIELDS (TREE_TYPE (datum));
2409 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
2412 /* The 'build_component_ref' routine has been removed from the C++
2413 front-end, but 'finish_class_member_access_expr' seems to be
2414 a worthy substitute. */
2416 return finish_class_member_access_expr (datum, component, false,
2417 tf_warning_or_error);
2419 return build_component_ref (input_location, datum, component);
2423 /* Recursively copy inheritance information rooted at BINFO. To do this,
2424 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
2427 objc_copy_binfo (tree binfo)
2429 tree btype = BINFO_TYPE (binfo);
2430 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
2434 BINFO_TYPE (binfo2) = btype;
2435 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
2436 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
2438 /* Recursively copy base binfos of BINFO. */
2439 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2441 tree base_binfo2 = objc_copy_binfo (base_binfo);
2443 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
2444 BINFO_BASE_APPEND (binfo2, base_binfo2);
2450 /* Record superclass information provided in BASETYPE for ObjC class REF.
2451 This is loosely based on cp/decl.c:xref_basetypes(). */
2454 objc_xref_basetypes (tree ref, tree basetype)
2456 tree binfo = make_tree_binfo (basetype ? 1 : 0);
2458 TYPE_BINFO (ref) = binfo;
2459 BINFO_OFFSET (binfo) = size_zero_node;
2460 BINFO_TYPE (binfo) = ref;
2464 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
2466 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
2467 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, 1);
2468 BINFO_BASE_APPEND (binfo, base_binfo);
2469 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
2473 /* Called from finish_decl. */
2476 objc_check_decl (tree decl)
2478 tree type = TREE_TYPE (decl);
2480 if (TREE_CODE (type) != RECORD_TYPE)
2482 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
2483 error ("statically allocated instance of Objective-C class %qE",
2488 objc_check_global_decl (tree decl)
2490 tree id = DECL_NAME (decl);
2491 if (objc_is_class_name (id) && global_bindings_p())
2492 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id));
2495 /* Return a non-volatalized version of TYPE. */
2498 objc_non_volatilized_type (tree type)
2500 if (lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (type)))
2501 type = build_qualified_type (type, (TYPE_QUALS (type) & ~TYPE_QUAL_VOLATILE));
2505 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where INTERFACE may
2506 either name an Objective-C class, or refer to the special 'id' or 'Class'
2507 types. If INTERFACE is not a valid ObjC type, just return it unchanged. */
2510 objc_get_protocol_qualified_type (tree interface, tree protocols)
2512 /* If INTERFACE is not provided, default to 'id'. */
2513 tree type = (interface ? objc_is_id (interface) : objc_object_type);
2514 bool is_ptr = (type != NULL_TREE);
2518 type = objc_is_class_name (interface);
2522 /* If looking at a typedef, retrieve the precise type it
2524 if (TREE_CODE (interface) == IDENTIFIER_NODE)
2525 interface = identifier_global_value (interface);
2527 type = ((interface && TREE_CODE (interface) == TYPE_DECL
2528 && DECL_ORIGINAL_TYPE (interface))
2529 ? DECL_ORIGINAL_TYPE (interface)
2530 : xref_tag (RECORD_TYPE, type));
2534 /* This case happens when we are given an 'interface' which
2535 is not a valid class name. For example if a typedef was
2536 used, and 'interface' really is the identifier of the
2537 typedef, but when you resolve it you don't get an
2538 Objective-C class, but something else, such as 'int'.
2539 This is an error; protocols make no sense unless you use
2540 them with Objective-C objects. */
2541 error_at (input_location, "only Objective-C object types can be qualified with a protocol");
2543 /* Try to recover. Ignore the invalid class name, and treat
2544 the object as an 'id' to silence further warnings about
2546 type = objc_object_type;
2553 type = build_variant_type_copy (type);
2555 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
2559 tree orig_pointee_type = TREE_TYPE (type);
2560 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
2562 /* Set up the canonical type information. */
2563 TYPE_CANONICAL (type)
2564 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
2566 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
2567 type = TREE_TYPE (type);
2570 /* Look up protocols and install in lang specific list. */
2571 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
2572 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols (protocols);
2574 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
2575 return the pointer to the new pointee variant. */
2577 type = TYPE_POINTER_TO (type);
2579 TYPE_OBJC_INTERFACE (type)
2580 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
2586 /* Check for circular dependencies in protocols. The arguments are
2587 PROTO, the protocol to check, and LIST, a list of protocol it
2591 check_protocol_recursively (tree proto, tree list)
2595 for (p = list; p; p = TREE_CHAIN (p))
2597 tree pp = TREE_VALUE (p);
2599 if (TREE_CODE (pp) == IDENTIFIER_NODE)
2600 pp = lookup_protocol (pp);
2603 fatal_error ("protocol %qE has circular dependency",
2604 PROTOCOL_NAME (pp));
2606 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
2610 /* Look up PROTOCOLS, and return a list of those that are found.
2611 If none are found, return NULL. */
2614 lookup_and_install_protocols (tree protocols)
2617 tree return_value = NULL_TREE;
2619 if (protocols == error_mark_node)
2622 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
2624 tree ident = TREE_VALUE (proto);
2625 tree p = lookup_protocol (ident);
2628 return_value = chainon (return_value,
2629 build_tree_list (NULL_TREE, p));
2630 else if (ident != error_mark_node)
2631 error ("cannot find protocol declaration for %qE",
2635 return return_value;
2638 /* Create a declaration for field NAME of a given TYPE. */
2641 create_field_decl (tree type, const char *name)
2643 return build_decl (input_location,
2644 FIELD_DECL, get_identifier (name), type);
2647 /* Create a global, static declaration for variable NAME of a given TYPE. The
2648 finish_var_decl() routine will need to be called on it afterwards. */
2651 start_var_decl (tree type, const char *name)
2653 tree var = build_decl (input_location,
2654 VAR_DECL, get_identifier (name), type);
2656 TREE_STATIC (var) = 1;
2657 DECL_INITIAL (var) = error_mark_node; /* A real initializer is coming... */
2658 DECL_IGNORED_P (var) = 1;
2659 DECL_ARTIFICIAL (var) = 1;
2660 DECL_CONTEXT (var) = NULL_TREE;
2662 DECL_THIS_STATIC (var) = 1; /* squash redeclaration errors */
2668 /* Finish off the variable declaration created by start_var_decl(). */
2671 finish_var_decl (tree var, tree initializer)
2673 finish_decl (var, input_location, initializer, NULL_TREE, NULL_TREE);
2676 /* Find the decl for the constant string class reference. This is only
2677 used for the NeXT runtime. */
2680 setup_string_decl (void)
2685 /* %s in format will provide room for terminating null */
2686 length = strlen (STRING_OBJECT_GLOBAL_FORMAT)
2687 + strlen (constant_string_class_name);
2688 name = XNEWVEC (char, length);
2689 sprintf (name, STRING_OBJECT_GLOBAL_FORMAT,
2690 constant_string_class_name);
2691 constant_string_global_id = get_identifier (name);
2692 string_class_decl = lookup_name (constant_string_global_id);
2694 return string_class_decl;
2697 /* Purpose: "play" parser, creating/installing representations
2698 of the declarations that are required by Objective-C.
2702 type_spec--------->sc_spec
2703 (tree_list) (tree_list)
2706 identifier_node identifier_node */
2709 synth_module_prologue (void)
2712 enum debug_info_type save_write_symbols = write_symbols;
2713 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
2715 /* Suppress outputting debug symbols, because
2716 dbxout_init hasn't been called yet. */
2717 write_symbols = NO_DEBUG;
2718 debug_hooks = &do_nothing_debug_hooks;
2721 push_lang_context (lang_name_c); /* extern "C" */
2724 /* The following are also defined in <objc/objc.h> and friends. */
2726 objc_object_id = get_identifier (TAG_OBJECT);
2727 objc_class_id = get_identifier (TAG_CLASS);
2729 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
2730 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
2732 objc_object_type = build_pointer_type (objc_object_reference);
2733 objc_class_type = build_pointer_type (objc_class_reference);
2735 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
2736 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
2738 /* Declare the 'id' and 'Class' typedefs. */
2740 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2744 TREE_NO_WARNING (type) = 1;
2745 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2749 TREE_NO_WARNING (type) = 1;
2751 /* Forward-declare '@interface Protocol'. */
2753 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
2754 objc_declare_class (tree_cons (NULL_TREE, type, NULL_TREE));
2755 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE,
2758 /* Declare type of selector-objects that represent an operation name. */
2760 if (flag_next_runtime)
2761 /* `struct objc_selector *' */
2763 = build_pointer_type (xref_tag (RECORD_TYPE,
2764 get_identifier (TAG_SELECTOR)));
2766 /* `const struct objc_selector *' */
2768 = build_pointer_type
2769 (build_qualified_type (xref_tag (RECORD_TYPE,
2770 get_identifier (TAG_SELECTOR)),
2773 /* Declare receiver type used for dispatching messages to 'super'. */
2775 /* `struct objc_super *' */
2776 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
2777 get_identifier (TAG_SUPER)));
2779 /* Declare pointers to method and ivar lists. */
2780 objc_method_list_ptr = build_pointer_type
2781 (xref_tag (RECORD_TYPE,
2782 get_identifier (UTAG_METHOD_LIST)));
2783 objc_method_proto_list_ptr
2784 = build_pointer_type (xref_tag (RECORD_TYPE,
2785 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
2786 objc_ivar_list_ptr = build_pointer_type
2787 (xref_tag (RECORD_TYPE,
2788 get_identifier (UTAG_IVAR_LIST)));
2790 /* TREE_NOTHROW is cleared for the message-sending functions,
2791 because the function that gets called can throw in Obj-C++, or
2792 could itself call something that can throw even in Obj-C. */
2794 if (flag_next_runtime)
2796 /* NB: In order to call one of the ..._stret (struct-returning)
2797 functions, the function *MUST* first be cast to a signature that
2798 corresponds to the actual ObjC method being invoked. This is
2799 what is done by the build_objc_method_call() routine below. */
2801 /* id objc_msgSend (id, SEL, ...); */
2802 /* id objc_msgSendNonNil (id, SEL, ...); */
2803 /* id objc_msgSend_stret (id, SEL, ...); */
2804 /* id objc_msgSendNonNil_stret (id, SEL, ...); */
2806 = build_varargs_function_type_list (objc_object_type,
2810 umsg_decl = add_builtin_function (TAG_MSGSEND,
2811 type, 0, NOT_BUILT_IN,
2813 umsg_nonnil_decl = add_builtin_function (TAG_MSGSEND_NONNIL,
2814 type, 0, NOT_BUILT_IN,
2816 umsg_stret_decl = add_builtin_function (TAG_MSGSEND_STRET,
2817 type, 0, NOT_BUILT_IN,
2819 umsg_nonnil_stret_decl = add_builtin_function (TAG_MSGSEND_NONNIL_STRET,
2820 type, 0, NOT_BUILT_IN,
2823 /* These can throw, because the function that gets called can throw
2824 in Obj-C++, or could itself call something that can throw even
2826 TREE_NOTHROW (umsg_decl) = 0;
2827 TREE_NOTHROW (umsg_nonnil_decl) = 0;
2828 TREE_NOTHROW (umsg_stret_decl) = 0;
2829 TREE_NOTHROW (umsg_nonnil_stret_decl) = 0;
2831 /* id objc_msgSend_Fast (id, SEL, ...)
2832 __attribute__ ((hard_coded_address (OFFS_MSGSEND_FAST))); */
2833 #ifdef OFFS_MSGSEND_FAST
2834 umsg_fast_decl = add_builtin_function (TAG_MSGSEND_FAST,
2835 type, 0, NOT_BUILT_IN,
2837 TREE_NOTHROW (umsg_fast_decl) = 0;
2838 DECL_ATTRIBUTES (umsg_fast_decl)
2839 = tree_cons (get_identifier ("hard_coded_address"),
2840 build_int_cst (NULL_TREE, OFFS_MSGSEND_FAST),
2843 /* No direct dispatch available. */
2844 umsg_fast_decl = umsg_decl;
2847 /* id objc_msgSendSuper (struct objc_super *, SEL, ...); */
2848 /* id objc_msgSendSuper_stret (struct objc_super *, SEL, ...); */
2850 = build_varargs_function_type_list (objc_object_type,
2854 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
2855 type, 0, NOT_BUILT_IN,
2857 umsg_super_stret_decl = add_builtin_function (TAG_MSGSENDSUPER_STRET,
2858 type, 0, NOT_BUILT_IN, 0,
2860 TREE_NOTHROW (umsg_super_decl) = 0;
2861 TREE_NOTHROW (umsg_super_stret_decl) = 0;
2865 /* GNU runtime messenger entry points. */
2867 /* typedef id (*IMP)(id, SEL, ...); */
2869 build_varargs_function_type_list (objc_object_type,
2873 tree IMP_type = build_pointer_type (ftype);
2875 /* IMP objc_msg_lookup (id, SEL); */
2876 type = build_function_type_list (IMP_type,
2880 umsg_decl = add_builtin_function (TAG_MSGSEND,
2881 type, 0, NOT_BUILT_IN,
2883 TREE_NOTHROW (umsg_decl) = 0;
2885 /* IMP objc_msg_lookup_super (struct objc_super *, SEL); */
2887 = build_function_type_list (IMP_type,
2891 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
2892 type, 0, NOT_BUILT_IN,
2894 TREE_NOTHROW (umsg_super_decl) = 0;
2896 /* The following GNU runtime entry point is called to initialize
2899 __objc_exec_class (void *); */
2901 = build_function_type_list (void_type_node,
2904 execclass_decl = add_builtin_function (TAG_EXECCLASS,
2905 type, 0, NOT_BUILT_IN,
2909 /* id objc_getClass (const char *); */
2911 type = build_function_type_list (objc_object_type,
2912 const_string_type_node,
2916 = add_builtin_function (TAG_GETCLASS, type, 0, NOT_BUILT_IN,
2919 /* id objc_getMetaClass (const char *); */
2921 objc_get_meta_class_decl
2922 = add_builtin_function (TAG_GETMETACLASS, type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
2924 build_class_template ();
2925 build_super_template ();
2926 build_protocol_template ();
2927 build_category_template ();
2928 build_objc_exception_stuff ();
2930 /* Declare objc_getProperty, object_setProperty and other property
2931 accessor helpers. */
2932 build_objc_property_accessor_helpers ();
2934 if (flag_next_runtime)
2935 build_next_objc_exception_stuff ();
2937 /* static SEL _OBJC_SELECTOR_TABLE[]; */
2939 if (! flag_next_runtime)
2940 build_selector_table_decl ();
2942 /* Forward declare constant_string_id and constant_string_type. */
2943 if (!constant_string_class_name)
2944 constant_string_class_name = default_constant_string_class_name;
2946 constant_string_id = get_identifier (constant_string_class_name);
2947 objc_declare_class (tree_cons (NULL_TREE, constant_string_id, NULL_TREE));
2949 /* Pre-build the following entities - for speed/convenience. */
2950 self_id = get_identifier ("self");
2951 ucmd_id = get_identifier ("_cmd");
2953 /* Declare struct _objc_fast_enumeration_state { ... }; */
2954 build_fast_enumeration_state_template ();
2956 /* void objc_enumeration_mutation (id) */
2957 type = build_function_type (void_type_node,
2958 tree_cons (NULL_TREE, objc_object_type, NULL_TREE));
2959 objc_enumeration_mutation_decl
2960 = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN,
2962 TREE_NOTHROW (objc_enumeration_mutation_decl) = 0;
2965 pop_lang_context ();
2968 write_symbols = save_write_symbols;
2969 debug_hooks = save_hooks;
2972 /* Ensure that the ivar list for NSConstantString/NXConstantString
2973 (or whatever was specified via `-fconstant-string-class')
2974 contains fields at least as large as the following three, so that
2975 the runtime can stomp on them with confidence:
2977 struct STRING_OBJECT_CLASS_NAME
2981 unsigned int length;
2985 check_string_class_template (void)
2987 tree field_decl = objc_get_class_ivars (constant_string_id);
2989 #define AT_LEAST_AS_LARGE_AS(F, T) \
2990 (F && TREE_CODE (F) == FIELD_DECL \
2991 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
2992 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
2994 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
2997 field_decl = DECL_CHAIN (field_decl);
2998 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3001 field_decl = DECL_CHAIN (field_decl);
3002 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
3004 #undef AT_LEAST_AS_LARGE_AS
3007 /* Avoid calling `check_string_class_template ()' more than once. */
3008 static GTY(()) int string_layout_checked;
3010 /* Construct an internal string layout to be used as a template for
3011 creating NSConstantString/NXConstantString instances. */
3014 objc_build_internal_const_str_type (void)
3016 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
3017 tree fields = build_decl (input_location,
3018 FIELD_DECL, NULL_TREE, ptr_type_node);
3019 tree field = build_decl (input_location,
3020 FIELD_DECL, NULL_TREE, ptr_type_node);
3022 DECL_CHAIN (field) = fields; fields = field;
3023 field = build_decl (input_location,
3024 FIELD_DECL, NULL_TREE, unsigned_type_node);
3025 DECL_CHAIN (field) = fields; fields = field;
3026 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
3028 finish_builtin_struct (type, "__builtin_ObjCString",
3034 /* Custom build_string which sets TREE_TYPE! */
3037 my_build_string (int len, const char *str)
3039 return fix_string_type (build_string (len, str));
3042 /* Build a string with contents STR and length LEN and convert it to a
3046 my_build_string_pointer (int len, const char *str)
3048 tree string = my_build_string (len, str);
3049 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
3050 return build1 (ADDR_EXPR, ptrtype, string);
3054 string_hash (const void *ptr)
3056 const_tree const str = ((const struct string_descriptor *)ptr)->literal;
3057 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3058 int i, len = TREE_STRING_LENGTH (str);
3061 for (i = 0; i < len; i++)
3062 h = ((h * 613) + p[i]);
3068 string_eq (const void *ptr1, const void *ptr2)
3070 const_tree const str1 = ((const struct string_descriptor *)ptr1)->literal;
3071 const_tree const str2 = ((const struct string_descriptor *)ptr2)->literal;
3072 int len1 = TREE_STRING_LENGTH (str1);
3074 return (len1 == TREE_STRING_LENGTH (str2)
3075 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3079 /* Given a chain of STRING_CST's, build a static instance of
3080 NXConstantString which points at the concatenation of those
3081 strings. We place the string object in the __string_objects
3082 section of the __OBJC segment. The Objective-C runtime will
3083 initialize the isa pointers of the string objects to point at the
3084 NXConstantString class object. */
3087 objc_build_string_object (tree string)
3089 tree constant_string_class;
3092 struct string_descriptor *desc, key;
3095 /* Prep the string argument. */
3096 string = fix_string_type (string);
3097 TREE_SET_CODE (string, STRING_CST);
3098 length = TREE_STRING_LENGTH (string) - 1;
3100 /* The target may have different ideas on how to construct an ObjC string
3101 literal. On Darwin (Mac OS X), for example, we may wish to obtain a
3102 constant CFString reference instead.
3103 At present, this is only supported for the NeXT runtime. */
3104 if (flag_next_runtime && targetcm.objc_construct_string_object)
3106 tree constructor = (*targetcm.objc_construct_string_object) (string);
3108 return build1 (NOP_EXPR, objc_object_type, constructor);
3111 /* Check whether the string class being used actually exists and has the
3112 correct ivar layout. */
3113 if (!string_layout_checked)
3115 string_layout_checked = -1;
3116 constant_string_class = lookup_interface (constant_string_id);
3117 internal_const_str_type = objc_build_internal_const_str_type ();
3119 if (!constant_string_class
3120 || !(constant_string_type
3121 = CLASS_STATIC_TEMPLATE (constant_string_class)))
3122 error ("cannot find interface declaration for %qE",
3123 constant_string_id);
3124 /* The NSConstantString/NXConstantString ivar layout is now known. */
3125 else if (!check_string_class_template ())
3126 error ("interface %qE does not have valid constant string layout",
3127 constant_string_id);
3128 /* For the NeXT runtime, we can generate a literal reference
3129 to the string class, don't need to run a constructor. */
3130 else if (flag_next_runtime && !setup_string_decl ())
3131 error ("cannot find reference tag for class %qE",
3132 constant_string_id);
3135 string_layout_checked = 1; /* Success! */
3136 add_class_reference (constant_string_id);
3140 if (string_layout_checked == -1)
3141 return error_mark_node;
3143 /* Perhaps we already constructed a constant string just like this one? */
3144 key.literal = string;
3145 loc = htab_find_slot (string_htab, &key, INSERT);
3146 desc = (struct string_descriptor *) *loc;
3150 tree var, constructor;
3151 VEC(constructor_elt,gc) *v = NULL;
3152 *loc = desc = ggc_alloc_string_descriptor ();
3153 desc->literal = string;
3155 /* GNU: (NXConstantString *) & ((__builtin_ObjCString) { NULL, string, length }) */
3156 /* NeXT: (NSConstantString *) & ((__builtin_ObjCString) { isa, string, length }) */
3157 fields = TYPE_FIELDS (internal_const_str_type);
3158 CONSTRUCTOR_APPEND_ELT (v, fields,
3160 ? build_unary_op (input_location,
3161 ADDR_EXPR, string_class_decl, 0)
3162 : build_int_cst (NULL_TREE, 0));
3163 fields = DECL_CHAIN (fields);
3164 CONSTRUCTOR_APPEND_ELT (v, fields,
3165 build_unary_op (input_location,
3166 ADDR_EXPR, string, 1));
3167 fields = DECL_CHAIN (fields);
3168 CONSTRUCTOR_APPEND_ELT (v, fields, build_int_cst (NULL_TREE, length));
3169 constructor = objc_build_constructor (internal_const_str_type, v);
3171 if (!flag_next_runtime)
3173 = objc_add_static_instance (constructor, constant_string_type);
3176 var = build_decl (input_location,
3177 CONST_DECL, NULL, TREE_TYPE (constructor));
3178 DECL_INITIAL (var) = constructor;
3179 TREE_STATIC (var) = 1;
3180 pushdecl_top_level (var);
3183 desc->constructor = constructor;
3186 addr = convert (build_pointer_type (constant_string_type),
3187 build_unary_op (input_location,
3188 ADDR_EXPR, desc->constructor, 1));
3193 /* Declare a static instance of CLASS_DECL initialized by CONSTRUCTOR. */
3195 static GTY(()) int num_static_inst;
3198 objc_add_static_instance (tree constructor, tree class_decl)
3203 /* Find the list of static instances for the CLASS_DECL. Create one if
3205 for (chain = &objc_static_instances;
3206 *chain && TREE_VALUE (*chain) != class_decl;
3207 chain = &TREE_CHAIN (*chain));
3210 *chain = tree_cons (NULL_TREE, class_decl, NULL_TREE);
3211 add_objc_string (OBJC_TYPE_NAME (class_decl), class_names);
3214 sprintf (buf, "_OBJC_INSTANCE_%d", num_static_inst++);
3215 decl = build_decl (input_location,
3216 VAR_DECL, get_identifier (buf), class_decl);
3217 TREE_STATIC (decl) = 1;
3218 DECL_ARTIFICIAL (decl) = 1;
3219 TREE_USED (decl) = 1;
3220 DECL_INITIAL (decl) = constructor;
3222 /* We may be writing something else just now.
3223 Postpone till end of input. */
3224 DECL_DEFER_OUTPUT (decl) = 1;
3225 pushdecl_top_level (decl);
3226 rest_of_decl_compilation (decl, 1, 0);
3228 /* Add the DECL to the head of this CLASS' list. */
3229 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE, decl, TREE_PURPOSE (*chain));
3234 /* Build a static constant CONSTRUCTOR
3235 with type TYPE and elements ELTS. */
3238 objc_build_constructor (tree type, VEC(constructor_elt,gc) *elts)
3240 tree constructor = build_constructor (type, elts);
3242 TREE_CONSTANT (constructor) = 1;
3243 TREE_STATIC (constructor) = 1;
3244 TREE_READONLY (constructor) = 1;
3247 /* Adjust for impedance mismatch. We should figure out how to build
3248 CONSTRUCTORs that consistently please both the C and C++ gods. */
3249 if (!VEC_index (constructor_elt, elts, 0)->index)
3250 TREE_TYPE (constructor) = init_list_type_node;
3256 /* Take care of defining and initializing _OBJC_SYMBOLS. */
3258 /* Predefine the following data type:
3266 void *defs[cls_def_cnt + cat_def_cnt];
3270 build_objc_symtab_template (void)
3272 tree fields, *chain = NULL;
3274 objc_symtab_template = objc_start_struct (get_identifier (UTAG_SYMTAB));
3276 /* long sel_ref_cnt; */
3277 fields = add_field_decl (long_integer_type_node, "sel_ref_cnt", &chain);
3280 add_field_decl (build_pointer_type (objc_selector_type), "refs", &chain);
3282 /* short cls_def_cnt; */
3283 add_field_decl (short_integer_type_node, "cls_def_cnt", &chain);
3285 /* short cat_def_cnt; */
3286 add_field_decl (short_integer_type_node, "cat_def_cnt", &chain);
3288 if (imp_count || cat_count || !flag_next_runtime)
3290 /* void *defs[imp_count + cat_count (+ 1)]; */
3291 /* NB: The index is one less than the size of the array. */
3292 int index = imp_count + cat_count + (flag_next_runtime ? -1: 0);
3293 tree array_type = build_sized_array_type (ptr_type_node, index + 1);
3294 add_field_decl (array_type, "defs", &chain);
3297 objc_finish_struct (objc_symtab_template, fields);
3300 /* Create the initial value for the `defs' field of _objc_symtab.
3301 This is a CONSTRUCTOR. */
3304 init_def_list (tree type)
3307 struct imp_entry *impent;
3308 VEC(constructor_elt,gc) *v = NULL;
3311 for (impent = imp_list; impent; impent = impent->next)
3313 if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
3315 expr = build_unary_op (input_location,
3316 ADDR_EXPR, impent->class_decl, 0);
3317 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3322 for (impent = imp_list; impent; impent = impent->next)
3324 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
3326 expr = build_unary_op (input_location,
3327 ADDR_EXPR, impent->class_decl, 0);
3328 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3332 if (!flag_next_runtime)
3334 /* statics = { ..., _OBJC_STATIC_INSTANCES, ... } */
3335 if (static_instances_decl)
3336 expr = build_unary_op (input_location,
3337 ADDR_EXPR, static_instances_decl, 0);
3339 expr = integer_zero_node;
3341 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3344 return objc_build_constructor (type, v);
3347 /* Construct the initial value for all of _objc_symtab. */
3350 init_objc_symtab (tree type)
3352 VEC(constructor_elt,gc) *v = NULL;
3354 /* sel_ref_cnt = { ..., 5, ... } */
3356 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3357 build_int_cst (long_integer_type_node, 0));
3359 /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
3361 if (flag_next_runtime || ! sel_ref_chain)
3362 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, convert (
3363 build_pointer_type (objc_selector_type),
3364 integer_zero_node));
3367 tree expr = build_unary_op (input_location, ADDR_EXPR,
3368 UOBJC_SELECTOR_TABLE_decl, 1);
3370 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3371 convert (build_pointer_type (objc_selector_type),
3375 /* cls_def_cnt = { ..., 5, ... } */
3377 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3378 build_int_cst (short_integer_type_node, imp_count));
3380 /* cat_def_cnt = { ..., 5, ... } */
3382 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3383 build_int_cst (short_integer_type_node, cat_count));
3385 /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
3387 if (imp_count || cat_count || !flag_next_runtime)
3390 tree field = TYPE_FIELDS (type);
3391 field = DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (field))));
3393 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init_def_list (TREE_TYPE (field)));
3396 return objc_build_constructor (type, v);
3399 /* Generate forward declarations for metadata such as
3400 'OBJC_CLASS_...'. */
3403 build_metadata_decl (const char *name, tree type)
3407 /* struct TYPE NAME_<name>; */
3408 decl = start_var_decl (type, synth_id_with_class_suffix
3410 objc_implementation_context));
3415 /* Push forward-declarations of all the categories so that
3416 init_def_list can use them in a CONSTRUCTOR. */
3419 forward_declare_categories (void)
3421 struct imp_entry *impent;
3422 tree sav = objc_implementation_context;
3424 for (impent = imp_list; impent; impent = impent->next)
3426 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
3428 /* Set an invisible arg to synth_id_with_class_suffix. */
3429 objc_implementation_context = impent->imp_context;
3430 /* extern struct objc_category _OBJC_CATEGORY_<name>; */
3431 impent->class_decl = build_metadata_decl ("_OBJC_CATEGORY",
3432 objc_category_template);
3435 objc_implementation_context = sav;
3438 /* Create the declaration of _OBJC_SYMBOLS, with type `struct _objc_symtab'
3439 and initialized appropriately. */
3442 generate_objc_symtab_decl (void)
3445 build_objc_symtab_template ();
3446 UOBJC_SYMBOLS_decl = start_var_decl (objc_symtab_template, "_OBJC_SYMBOLS");
3447 finish_var_decl (UOBJC_SYMBOLS_decl,
3448 init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl)));
3452 init_module_descriptor (tree type)
3455 VEC(constructor_elt,gc) *v = NULL;
3457 /* version = { 1, ... } */
3459 expr = build_int_cst (long_integer_type_node, OBJC_VERSION);
3460 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3462 /* size = { ..., sizeof (struct _objc_module), ... } */
3464 expr = convert (long_integer_type_node,
3465 size_in_bytes (objc_module_template));
3466 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3468 /* Don't provide any file name for security reasons. */
3469 /* name = { ..., "", ... } */
3471 expr = add_objc_string (get_identifier (""), class_names);
3472 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3474 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
3476 if (UOBJC_SYMBOLS_decl)
3477 expr = build_unary_op (input_location,
3478 ADDR_EXPR, UOBJC_SYMBOLS_decl, 0);
3480 expr = null_pointer_node;
3481 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3483 return objc_build_constructor (type, v);
3486 /* Write out the data structures to describe Objective C classes defined.
3488 struct _objc_module { ... } _OBJC_MODULE = { ... }; */
3491 build_module_descriptor (void)
3493 tree decls, *chain = NULL;
3496 push_lang_context (lang_name_c); /* extern "C" */
3499 objc_module_template = objc_start_struct (get_identifier (UTAG_MODULE));
3502 decls = add_field_decl (long_integer_type_node, "version", &chain);
3505 add_field_decl (long_integer_type_node, "size", &chain);
3508 add_field_decl (string_type_node, "name", &chain);
3510 /* struct _objc_symtab *symtab; */
3511 add_field_decl (build_pointer_type (xref_tag (RECORD_TYPE,
3512 get_identifier (UTAG_SYMTAB))),
3515 objc_finish_struct (objc_module_template, decls);
3517 /* Create an instance of "_objc_module". */
3518 UOBJC_MODULES_decl = start_var_decl (objc_module_template, "_OBJC_MODULES");
3519 /* This is the root of the metadata for defined classes and categories, it
3520 is referenced by the runtime and, therefore, needed. */
3521 DECL_PRESERVE_P (UOBJC_MODULES_decl) = 1;
3522 finish_var_decl (UOBJC_MODULES_decl,
3523 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl)));
3526 pop_lang_context ();
3530 /* The GNU runtime requires us to provide a static initializer function
3533 static void __objc_gnu_init (void) {
3534 __objc_exec_class (&L_OBJC_MODULES);
3538 build_module_initializer_routine (void)
3543 push_lang_context (lang_name_c); /* extern "C" */
3546 objc_push_parm (build_decl (input_location,
3547 PARM_DECL, NULL_TREE, void_type_node));
3549 objc_start_function (get_identifier (TAG_GNUINIT),
3550 build_function_type_list (void_type_node, NULL_TREE),
3551 NULL_TREE, NULL_TREE);
3553 objc_start_function (get_identifier (TAG_GNUINIT),
3554 build_function_type_list (void_type_node, NULL_TREE),
3555 NULL_TREE, objc_get_parm_info (0));
3557 body = c_begin_compound_stmt (true);
3558 add_stmt (build_function_call
3563 build_unary_op (input_location, ADDR_EXPR,
3564 UOBJC_MODULES_decl, 0))));
3565 add_stmt (c_end_compound_stmt (input_location, body, true));
3567 TREE_PUBLIC (current_function_decl) = 0;
3570 /* For Objective-C++, we will need to call __objc_gnu_init
3571 from objc_generate_static_init_call() below. */
3572 DECL_STATIC_CONSTRUCTOR (current_function_decl) = 1;
3575 GNU_INIT_decl = current_function_decl;
3579 pop_lang_context ();
3584 /* Return 1 if the __objc_gnu_init function has been synthesized and needs
3585 to be called by the module initializer routine. */
3588 objc_static_init_needed_p (void)
3590 return (GNU_INIT_decl != NULL_TREE);
3593 /* Generate a call to the __objc_gnu_init initializer function. */
3596 objc_generate_static_init_call (tree ctors ATTRIBUTE_UNUSED)
3598 add_stmt (build_stmt (input_location, EXPR_STMT,
3599 build_function_call (input_location,
3600 GNU_INIT_decl, NULL_TREE)));
3604 #endif /* OBJCPLUS */
3606 /* Return the DECL of the string IDENT in the SECTION. */
3609 get_objc_string_decl (tree ident, enum string_section section)
3616 chain = class_names_chain;
3618 case meth_var_names:
3619 chain = meth_var_names_chain;
3621 case meth_var_types:
3622 chain = meth_var_types_chain;
3628 for (; chain != 0; chain = TREE_CHAIN (chain))
3629 if (TREE_VALUE (chain) == ident)
3630 return (TREE_PURPOSE (chain));
3636 /* Output references to all statically allocated objects. Return the DECL
3637 for the array built. */
3640 generate_static_references (void)
3642 tree expr = NULL_TREE;
3643 tree class_name, klass, decl;
3644 tree cl_chain, in_chain, type
3645 = build_array_type (build_pointer_type (void_type_node), NULL_TREE);
3646 int num_inst, num_class;
3648 VEC(constructor_elt,gc) *decls = NULL;
3650 if (flag_next_runtime)
3653 for (cl_chain = objc_static_instances, num_class = 0;
3654 cl_chain; cl_chain = TREE_CHAIN (cl_chain), num_class++)
3656 VEC(constructor_elt,gc) *v = NULL;
3658 for (num_inst = 0, in_chain = TREE_PURPOSE (cl_chain);
3659 in_chain; num_inst++, in_chain = TREE_CHAIN (in_chain));
3661 sprintf (buf, "_OBJC_STATIC_INSTANCES_%d", num_class);
3662 decl = start_var_decl (type, buf);
3664 /* Output {class_name, ...}. */
3665 klass = TREE_VALUE (cl_chain);
3666 class_name = get_objc_string_decl (OBJC_TYPE_NAME (klass), class_names);
3667 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3668 build_unary_op (input_location,
3669 ADDR_EXPR, class_name, 1));
3671 /* Output {..., instance, ...}. */
3672 for (in_chain = TREE_PURPOSE (cl_chain);
3673 in_chain; in_chain = TREE_CHAIN (in_chain))
3675 expr = build_unary_op (input_location,
3676 ADDR_EXPR, TREE_VALUE (in_chain), 1);
3677 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3680 /* Output {..., NULL}. */
3681 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
3683 expr = objc_build_constructor (TREE_TYPE (decl), v);
3684 finish_var_decl (decl, expr);
3685 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE,
3686 build_unary_op (input_location,
3687 ADDR_EXPR, decl, 1));
3690 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE, build_int_cst (NULL_TREE, 0));
3691 expr = objc_build_constructor (type, decls);
3692 static_instances_decl = start_var_decl (type, "_OBJC_STATIC_INSTANCES");
3693 finish_var_decl (static_instances_decl, expr);
3696 static GTY(()) int selector_reference_idx;
3699 build_selector_reference_decl (void)
3704 sprintf (buf, "_OBJC_SELECTOR_REFERENCES_%d", selector_reference_idx++);
3705 decl = start_var_decl (objc_selector_type, buf);
3711 build_selector_table_decl (void)
3715 if (flag_typed_selectors)
3717 build_selector_template ();
3718 temp = build_array_type (objc_selector_template, NULL_TREE);
3721 temp = build_array_type (objc_selector_type, NULL_TREE);
3723 UOBJC_SELECTOR_TABLE_decl = start_var_decl (temp, "_OBJC_SELECTOR_TABLE");
3726 /* Just a handy wrapper for add_objc_string. */
3729 build_selector (tree ident)
3731 return convert (objc_selector_type,
3732 add_objc_string (ident, meth_var_names));
3735 /* Used only by build_*_selector_translation_table (). */
3737 diagnose_missing_method (tree meth, location_t here)
3741 for (method_chain = meth_var_names_chain;
3743 method_chain = TREE_CHAIN (method_chain))
3745 if (TREE_VALUE (method_chain) == meth)
3753 warning_at (here, 0, "creating selector for nonexistent method %qE",
3758 build_next_selector_translation_table (void)
3761 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
3764 tree decl = TREE_PURPOSE (chain);
3765 if (warn_selector && objc_implementation_context)
3769 loc = DECL_SOURCE_LOCATION (decl);
3771 loc = input_location;
3772 diagnose_missing_method (TREE_VALUE (chain), loc);
3775 expr = build_selector (TREE_VALUE (chain));
3779 /* Entries of this form are used for references to methods.
3780 The runtime re-writes these on start-up, but the compiler can't see
3781 that and optimizes it away unless we force it. */
3782 DECL_PRESERVE_P (decl) = 1;
3783 finish_var_decl (decl, expr);
3789 build_gnu_selector_translation_table (void)
3793 tree decl = NULL_TREE;*/
3794 VEC(constructor_elt,gc) *inits = NULL;
3796 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
3800 if (warn_selector && objc_implementation_context)
3801 diagnose_missing_method (TREE_VALUE (chain), input_location);
3803 expr = build_selector (TREE_VALUE (chain));
3804 /* add one for the '\0' character
3805 offset += IDENTIFIER_LENGTH (TREE_VALUE (chain)) + 1;*/
3808 if (flag_typed_selectors)
3810 VEC(constructor_elt,gc) *v = NULL;
3811 tree encoding = get_proto_encoding (TREE_PURPOSE (chain));
3812 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3813 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, encoding);
3814 expr = objc_build_constructor (objc_selector_template, v);
3817 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
3819 } /* each element in the chain */
3822 /* Cause the selector table (previously forward-declared)
3823 to be actually output. */
3826 if (flag_typed_selectors)
3828 VEC(constructor_elt,gc) *v = NULL;
3829 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
3830 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
3831 expr = objc_build_constructor (objc_selector_template, v);
3834 expr = integer_zero_node;
3836 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
3837 expr = objc_build_constructor (TREE_TYPE (UOBJC_SELECTOR_TABLE_decl),
3839 finish_var_decl (UOBJC_SELECTOR_TABLE_decl, expr);
3844 get_proto_encoding (tree proto)
3849 if (! METHOD_ENCODING (proto))
3851 encoding = encode_method_prototype (proto);
3852 METHOD_ENCODING (proto) = encoding;
3855 encoding = METHOD_ENCODING (proto);
3857 return add_objc_string (encoding, meth_var_types);
3860 return build_int_cst (NULL_TREE, 0);
3863 /* sel_ref_chain is a list whose "value" fields will be instances of
3864 identifier_node that represent the selector. LOC is the location of
3868 build_typed_selector_reference (location_t loc, tree ident, tree prototype)
3870 tree *chain = &sel_ref_chain;
3876 if (TREE_PURPOSE (*chain) == prototype && TREE_VALUE (*chain) == ident)
3877 goto return_at_index;
3880 chain = &TREE_CHAIN (*chain);
3883 *chain = tree_cons (prototype, ident, NULL_TREE);
3886 expr = build_unary_op (loc, ADDR_EXPR,
3887 build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
3888 build_int_cst (NULL_TREE, index)),
3890 return convert (objc_selector_type, expr);
3894 build_selector_reference (location_t loc, tree ident)
3896 tree *chain = &sel_ref_chain;
3902 if (TREE_VALUE (*chain) == ident)
3903 return (flag_next_runtime
3904 ? TREE_PURPOSE (*chain)
3905 : build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
3906 build_int_cst (NULL_TREE, index)));
3909 chain = &TREE_CHAIN (*chain);
3912 expr = (flag_next_runtime ? build_selector_reference_decl (): NULL_TREE);
3914 *chain = tree_cons (expr, ident, NULL_TREE);
3916 return (flag_next_runtime
3918 : build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
3919 build_int_cst (NULL_TREE, index)));
3922 static GTY(()) int class_reference_idx;
3925 build_class_reference_decl (void)
3930 sprintf (buf, "_OBJC_CLASS_REFERENCES_%d", class_reference_idx++);
3931 decl = start_var_decl (objc_class_type, buf);
3936 /* Create a class reference, but don't create a variable to reference
3940 add_class_reference (tree ident)
3944 if ((chain = cls_ref_chain))
3949 if (ident == TREE_VALUE (chain))
3953 chain = TREE_CHAIN (chain);
3957 /* Append to the end of the list */
3958 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
3961 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
3964 /* Get a class reference, creating it if necessary. Also create the
3965 reference variable. */
3968 objc_get_class_reference (tree ident)
3970 tree orig_ident = (DECL_P (ident)
3973 ? OBJC_TYPE_NAME (ident)
3975 bool local_scope = false;
3978 if (processing_template_decl)
3979 /* Must wait until template instantiation time. */
3980 return build_min_nt (CLASS_REFERENCE_EXPR, ident);
3983 if (TREE_CODE (ident) == TYPE_DECL)
3984 ident = (DECL_ORIGINAL_TYPE (ident)
3985 ? DECL_ORIGINAL_TYPE (ident)
3986 : TREE_TYPE (ident));
3990 && CP_TYPE_CONTEXT (ident) != global_namespace)
3994 if (local_scope || !(ident = objc_is_class_name (ident)))
3996 error ("%qE is not an Objective-C class name or alias",
3998 return error_mark_node;
4001 if (flag_next_runtime && !flag_zero_link)
4006 for (chain = &cls_ref_chain; *chain; chain = &TREE_CHAIN (*chain))
4007 if (TREE_VALUE (*chain) == ident)
4009 if (! TREE_PURPOSE (*chain))
4010 TREE_PURPOSE (*chain) = build_class_reference_decl ();
4012 return TREE_PURPOSE (*chain);
4015 decl = build_class_reference_decl ();
4016 *chain = tree_cons (decl, ident, NULL_TREE);
4023 add_class_reference (ident);
4025 params = build_tree_list (NULL_TREE,
4026 my_build_string_pointer
4027 (IDENTIFIER_LENGTH (ident) + 1,
4028 IDENTIFIER_POINTER (ident)));
4030 assemble_external (objc_get_class_decl);
4031 return build_function_call (input_location, objc_get_class_decl, params);
4035 /* For each string section we have a chain which maps identifier nodes
4036 to decls for the strings. */
4038 static GTY(()) int class_names_idx;
4039 static GTY(()) int meth_var_names_idx;
4040 static GTY(()) int meth_var_types_idx;
4043 add_objc_string (tree ident, enum string_section section)
4045 tree *chain, decl, type, string_expr;
4052 chain = &class_names_chain;
4053 sprintf (buf, "_OBJC_CLASS_NAME_%d", class_names_idx++);
4055 case meth_var_names:
4056 chain = &meth_var_names_chain;
4057 sprintf (buf, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx++);
4059 case meth_var_types:
4060 chain = &meth_var_types_chain;
4061 sprintf (buf, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx++);
4069 if (TREE_VALUE (*chain) == ident)
4070 return convert (string_type_node,
4071 build_unary_op (input_location,
4072 ADDR_EXPR, TREE_PURPOSE (*chain), 1));
4074 chain = &TREE_CHAIN (*chain);
4077 type = build_sized_array_type (char_type_node, IDENTIFIER_LENGTH (ident) + 1);
4078 decl = start_var_decl (type, buf);
4079 string_expr = my_build_string (IDENTIFIER_LENGTH (ident) + 1,
4080 IDENTIFIER_POINTER (ident));
4081 TREE_CONSTANT (decl) = 1;
4082 finish_var_decl (decl, string_expr);
4084 *chain = tree_cons (decl, ident, NULL_TREE);
4086 return convert (string_type_node, build_unary_op (input_location,
4087 ADDR_EXPR, decl, 1));
4091 objc_declare_alias (tree alias_ident, tree class_ident)
4093 tree underlying_class;
4096 if (current_namespace != global_namespace) {
4097 error ("Objective-C declarations may only appear in global scope");
4099 #endif /* OBJCPLUS */
4101 if (!(underlying_class = objc_is_class_name (class_ident)))
4102 warning (0, "cannot find class %qE", class_ident);
4103 else if (objc_is_class_name (alias_ident))
4104 warning (0, "class %qE already exists", alias_ident);
4107 /* Implement @compatibility_alias as a typedef. */
4109 push_lang_context (lang_name_c); /* extern "C" */
4111 lang_hooks.decls.pushdecl (build_decl
4115 xref_tag (RECORD_TYPE, underlying_class)));
4117 pop_lang_context ();
4119 hash_class_name_enter (als_name_hash_list, alias_ident,
4125 objc_declare_class (tree ident_list)
4129 if (current_namespace != global_namespace) {
4130 error ("Objective-C declarations may only appear in global scope");
4132 #endif /* OBJCPLUS */
4134 for (list = ident_list; list; list = TREE_CHAIN (list))
4136 tree ident = TREE_VALUE (list);
4138 if (! objc_is_class_name (ident))
4140 tree record = lookup_name (ident), type = record;
4144 if (TREE_CODE (record) == TYPE_DECL)
4145 type = DECL_ORIGINAL_TYPE (record) ?
4146 DECL_ORIGINAL_TYPE (record) :
4149 if (!TYPE_HAS_OBJC_INFO (type)
4150 || !TYPE_OBJC_INTERFACE (type))
4152 error ("%qE redeclared as different kind of symbol",
4154 error ("previous declaration of %q+D",
4159 record = xref_tag (RECORD_TYPE, ident);
4160 INIT_TYPE_OBJC_INFO (record);
4161 TYPE_OBJC_INTERFACE (record) = ident;
4162 hash_class_name_enter (cls_name_hash_list, ident, NULL_TREE);
4168 objc_is_class_name (tree ident)
4172 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE
4173 && identifier_global_value (ident))
4174 ident = identifier_global_value (ident);
4175 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
4176 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
4178 if (ident && TREE_CODE (ident) == RECORD_TYPE)
4179 ident = OBJC_TYPE_NAME (ident);
4181 if (ident && TREE_CODE (ident) == TYPE_DECL)
4183 tree type = TREE_TYPE (ident);
4184 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
4186 ident = DECL_NAME (ident);
4189 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
4192 if (lookup_interface (ident))
4195 target = hash_class_name_lookup (cls_name_hash_list, ident);
4199 target = hash_class_name_lookup (als_name_hash_list, ident);
4202 gcc_assert (target->list && target->list->value);
4203 return target->list->value;
4209 /* Check whether TYPE is either 'id' or 'Class'. */
4212 objc_is_id (tree type)
4214 if (type && TREE_CODE (type) == IDENTIFIER_NODE
4215 && identifier_global_value (type))
4216 type = identifier_global_value (type);
4218 if (type && TREE_CODE (type) == TYPE_DECL)
4219 type = TREE_TYPE (type);
4221 /* NB: This function may be called before the ObjC front-end has
4222 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
4223 return (objc_object_type && type
4224 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
4229 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
4230 class instance. This is needed by other parts of the compiler to
4231 handle ObjC types gracefully. */
4234 objc_is_object_ptr (tree type)
4238 type = TYPE_MAIN_VARIANT (type);
4239 if (!POINTER_TYPE_P (type))
4242 ret = objc_is_id (type);
4244 ret = objc_is_class_name (TREE_TYPE (type));
4250 objc_is_gcable_type (tree type, int or_strong_p)
4256 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
4258 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
4260 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
4262 type = TREE_TYPE (type);
4263 if (TREE_CODE (type) != RECORD_TYPE)
4265 name = TYPE_NAME (type);
4266 return (objc_is_class_name (name) != NULL_TREE);
4270 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
4272 if (expr == oldexpr)
4275 switch (TREE_CODE (expr))
4278 return objc_build_component_ref
4279 (objc_substitute_decl (TREE_OPERAND (expr, 0),
4282 DECL_NAME (TREE_OPERAND (expr, 1)));
4284 return build_array_ref (input_location,
4285 objc_substitute_decl (TREE_OPERAND (expr, 0),
4288 TREE_OPERAND (expr, 1));
4290 return build_indirect_ref (input_location,
4291 objc_substitute_decl (TREE_OPERAND (expr, 0),
4293 newexpr), RO_ARROW);
4300 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
4303 /* The LHS parameter contains the expression 'outervar->memberspec';
4304 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
4305 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
4308 = objc_substitute_decl
4309 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
4311 = (flag_objc_direct_dispatch
4312 ? objc_assign_ivar_fast_decl
4313 : objc_assign_ivar_decl);
4315 offs = convert (integer_type_node, build_unary_op (input_location,
4316 ADDR_EXPR, offs, 0));
4318 func_params = tree_cons (NULL_TREE,
4319 convert (objc_object_type, rhs),
4320 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
4321 tree_cons (NULL_TREE, offs,
4324 assemble_external (func);
4325 return build_function_call (input_location, func, func_params);
4329 objc_build_global_assignment (tree lhs, tree rhs)
4331 tree func_params = tree_cons (NULL_TREE,
4332 convert (objc_object_type, rhs),
4333 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
4334 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
4337 assemble_external (objc_assign_global_decl);
4338 return build_function_call (input_location,
4339 objc_assign_global_decl, func_params);
4343 objc_build_strong_cast_assignment (tree lhs, tree rhs)
4345 tree func_params = tree_cons (NULL_TREE,
4346 convert (objc_object_type, rhs),
4347 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
4348 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
4351 assemble_external (objc_assign_strong_cast_decl);
4352 return build_function_call (input_location,
4353 objc_assign_strong_cast_decl, func_params);
4357 objc_is_gcable_p (tree expr)
4359 return (TREE_CODE (expr) == COMPONENT_REF
4360 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
4361 : TREE_CODE (expr) == ARRAY_REF
4362 ? (objc_is_gcable_p (TREE_TYPE (expr))
4363 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
4364 : TREE_CODE (expr) == ARRAY_TYPE
4365 ? objc_is_gcable_p (TREE_TYPE (expr))
4367 ? objc_is_gcable_type (expr, 1)
4368 : (objc_is_gcable_p (TREE_TYPE (expr))
4370 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
4374 objc_is_ivar_reference_p (tree expr)
4376 return (TREE_CODE (expr) == ARRAY_REF
4377 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
4378 : TREE_CODE (expr) == COMPONENT_REF
4379 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
4384 objc_is_global_reference_p (tree expr)
4386 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
4387 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
4389 ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr))
4394 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
4396 tree result = NULL_TREE, outer;
4397 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
4399 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
4400 will have been transformed to the form '*(type *)&expr'. */
4401 if (TREE_CODE (lhs) == INDIRECT_REF)
4403 outer = TREE_OPERAND (lhs, 0);
4405 while (!strong_cast_p
4406 && (CONVERT_EXPR_P (outer)
4407 || TREE_CODE (outer) == NON_LVALUE_EXPR))
4409 tree lhstype = TREE_TYPE (outer);
4411 /* Descend down the cast chain, and record the first objc_gc
4413 if (POINTER_TYPE_P (lhstype))
4416 = lookup_attribute ("objc_gc",
4417 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
4423 outer = TREE_OPERAND (outer, 0);
4427 /* If we have a __strong cast, it trumps all else. */
4430 if (modifycode != NOP_EXPR)
4431 goto invalid_pointer_arithmetic;
4433 if (warn_assign_intercept)
4434 warning (0, "strong-cast assignment has been intercepted");
4436 result = objc_build_strong_cast_assignment (lhs, rhs);
4441 /* the lhs must be of a suitable type, regardless of its underlying
4443 if (!objc_is_gcable_p (lhs))
4449 && (TREE_CODE (outer) == COMPONENT_REF
4450 || TREE_CODE (outer) == ARRAY_REF))
4451 outer = TREE_OPERAND (outer, 0);
4453 if (TREE_CODE (outer) == INDIRECT_REF)
4455 outer = TREE_OPERAND (outer, 0);
4459 outer_gc_p = objc_is_gcable_p (outer);
4461 /* Handle ivar assignments. */
4462 if (objc_is_ivar_reference_p (lhs))
4464 /* if the struct to the left of the ivar is not an Objective-C object (__strong
4465 doesn't cut it here), the best we can do here is suggest a cast. */
4466 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
4468 /* We may still be able to use the global write barrier... */
4469 if (!indirect_p && objc_is_global_reference_p (outer))
4470 goto global_reference;
4473 if (modifycode == NOP_EXPR)
4475 if (warn_assign_intercept)
4476 warning (0, "strong-cast may possibly be needed");
4482 if (modifycode != NOP_EXPR)
4483 goto invalid_pointer_arithmetic;
4485 if (warn_assign_intercept)
4486 warning (0, "instance variable assignment has been intercepted");
4488 result = objc_build_ivar_assignment (outer, lhs, rhs);
4493 /* Likewise, intercept assignment to global/static variables if their type is
4495 if (objc_is_global_reference_p (outer))
4501 if (modifycode != NOP_EXPR)
4503 invalid_pointer_arithmetic:
4505 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
4510 if (warn_assign_intercept)
4511 warning (0, "global/static variable assignment has been intercepted");
4513 result = objc_build_global_assignment (lhs, rhs);
4516 /* In all other cases, fall back to the normal mechanism. */
4521 struct GTY(()) interface_tuple {
4526 static GTY ((param_is (struct interface_tuple))) htab_t interface_htab;
4529 hash_interface (const void *p)
4531 const struct interface_tuple *d = (const struct interface_tuple *) p;
4532 return IDENTIFIER_HASH_VALUE (d->id);
4536 eq_interface (const void *p1, const void *p2)
4538 const struct interface_tuple *d = (const struct interface_tuple *) p1;
4543 lookup_interface (tree ident)
4546 if (ident && TREE_CODE (ident) == TYPE_DECL)
4547 ident = DECL_NAME (ident);
4550 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
4554 struct interface_tuple **slot;
4559 slot = (struct interface_tuple **)
4560 htab_find_slot_with_hash (interface_htab, ident,
4561 IDENTIFIER_HASH_VALUE (ident),
4564 i = (*slot)->class_name;
4570 /* Implement @defs (<classname>) within struct bodies. */
4573 objc_get_class_ivars (tree class_name)
4575 tree interface = lookup_interface (class_name);
4578 return get_class_ivars (interface, true);
4580 error ("cannot find interface declaration for %qE",
4583 return error_mark_node;
4586 /* Called when checking the variables in a struct. If we are not
4587 doing the ivars list inside an @interface context, then returns
4588 fieldlist unchanged. Else, returns the list of class ivars.
4591 objc_get_interface_ivars (tree fieldlist)
4593 if (!objc_collecting_ivars || !objc_interface_context
4594 || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE
4595 || CLASS_SUPER_NAME (objc_interface_context) == NULL_TREE)
4598 return get_class_ivars (objc_interface_context, true);
4601 /* Used by: build_private_template, continue_class,
4602 and for @defs constructs. */
4605 get_class_ivars (tree interface, bool inherited)
4607 tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface));
4609 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
4610 by the current class (i.e., they do not include super-class ivars).
4611 However, the CLASS_IVARS list will be side-effected by a call to
4612 finish_struct(), which will fill in field offsets. */
4613 if (!CLASS_IVARS (interface))
4614 CLASS_IVARS (interface) = ivar_chain;
4619 while (CLASS_SUPER_NAME (interface))
4621 /* Prepend super-class ivars. */
4622 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4623 ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)),
4630 /* Create a temporary variable of type 'type'. If 'name' is set, uses
4631 the specified name, else use no name. Returns the declaration of
4632 the type. The 'name' is mostly useful for debugging.
4635 objc_create_temporary_var (tree type, const char *name)
4641 decl = build_decl (input_location,
4642 VAR_DECL, get_identifier (name), type);
4646 decl = build_decl (input_location,
4647 VAR_DECL, NULL_TREE, type);
4649 TREE_USED (decl) = 1;
4650 DECL_ARTIFICIAL (decl) = 1;
4651 DECL_IGNORED_P (decl) = 1;
4652 DECL_CONTEXT (decl) = current_function_decl;
4657 /* Exception handling constructs. We begin by having the parser do most
4658 of the work and passing us blocks. What we do next depends on whether
4659 we're doing "native" exception handling or legacy Darwin setjmp exceptions.
4660 We abstract all of this in a handful of appropriately named routines. */
4662 /* Stack of open try blocks. */
4664 struct objc_try_context
4666 struct objc_try_context *outer;
4668 /* Statements (or statement lists) as processed by the parser. */
4672 /* Some file position locations. */
4673 location_t try_locus;
4674 location_t end_try_locus;
4675 location_t end_catch_locus;
4676 location_t finally_locus;
4677 location_t end_finally_locus;
4679 /* A STATEMENT_LIST of CATCH_EXPRs, appropriate for sticking into op1
4680 of a TRY_CATCH_EXPR. Even when doing Darwin setjmp. */
4683 /* The CATCH_EXPR of an open @catch clause. */
4686 /* The VAR_DECL holding the Darwin equivalent of __builtin_eh_pointer. */
4692 static struct objc_try_context *cur_try_context;
4694 static GTY(()) tree objc_eh_personality_decl;
4696 /* This hook, called via lang_eh_runtime_type, generates a runtime object
4697 that represents TYPE. For Objective-C, this is just the class name. */
4698 /* ??? Isn't there a class object or some such? Is it easy to get? */
4702 objc_eh_runtime_type (tree type)
4704 return add_objc_string (OBJC_TYPE_NAME (TREE_TYPE (type)), class_names);
4708 objc_eh_personality (void)
4710 if (!flag_objc_sjlj_exceptions && !objc_eh_personality_decl)
4711 objc_eh_personality_decl = build_personality_function ("gnu_objc");
4712 return objc_eh_personality_decl;
4716 /* Build __builtin_eh_pointer, or the moral equivalent. In the case
4717 of Darwin, we'll arrange for it to be initialized (and associated
4718 with a binding) later. */
4721 objc_build_exc_ptr (void)
4723 if (flag_objc_sjlj_exceptions)
4725 tree var = cur_try_context->caught_decl;
4728 var = objc_create_temporary_var (objc_object_type, NULL);
4729 cur_try_context->caught_decl = var;
4736 t = built_in_decls[BUILT_IN_EH_POINTER];
4737 t = build_call_expr (t, 1, integer_zero_node);
4738 return fold_convert (objc_object_type, t);
4742 /* Build "objc_exception_try_exit(&_stack)". */
4745 next_sjlj_build_try_exit (void)
4748 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
4749 t = tree_cons (NULL, t, NULL);
4750 t = build_function_call (input_location,
4751 objc_exception_try_exit_decl, t);
4756 objc_exception_try_enter (&_stack);
4757 if (_setjmp(&_stack.buf))
4761 Return the COND_EXPR. Note that the THEN and ELSE fields are left
4762 empty, ready for the caller to fill them in. */
4765 next_sjlj_build_enter_and_setjmp (void)
4767 tree t, enter, sj, cond;
4769 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
4770 t = tree_cons (NULL, t, NULL);
4771 enter = build_function_call (input_location,
4772 objc_exception_try_enter_decl, t);
4774 t = objc_build_component_ref (cur_try_context->stack_decl,
4775 get_identifier ("buf"));
4776 t = build_fold_addr_expr_loc (input_location, t);
4778 /* Convert _setjmp argument to type that is expected. */
4779 if (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl)))
4780 t = convert (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl))), t);
4782 t = convert (ptr_type_node, t);
4784 t = convert (ptr_type_node, t);
4786 t = tree_cons (NULL, t, NULL);
4787 sj = build_function_call (input_location,
4788 objc_setjmp_decl, t);
4790 cond = build2 (COMPOUND_EXPR, TREE_TYPE (sj), enter, sj);
4791 cond = c_common_truthvalue_conversion (input_location, cond);
4793 return build3 (COND_EXPR, void_type_node, cond, NULL, NULL);
4798 DECL = objc_exception_extract(&_stack); */
4801 next_sjlj_build_exc_extract (tree decl)
4805 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
4806 t = tree_cons (NULL, t, NULL);
4807 t = build_function_call (input_location,
4808 objc_exception_extract_decl, t);
4809 t = convert (TREE_TYPE (decl), t);
4810 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
4816 if (objc_exception_match(obj_get_class(TYPE), _caught)
4823 objc_exception_try_exit(&_stack);
4825 from the sequence of CATCH_EXPRs in the current try context. */
4828 next_sjlj_build_catch_list (void)
4830 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
4832 tree *last = &catch_seq;
4833 bool saw_id = false;
4835 for (; !tsi_end_p (i); tsi_next (&i))
4837 tree stmt = tsi_stmt (i);
4838 tree type = CATCH_TYPES (stmt);
4839 tree body = CATCH_BODY (stmt);
4851 if (type == error_mark_node)
4852 cond = error_mark_node;
4855 args = tree_cons (NULL, cur_try_context->caught_decl, NULL);
4856 t = objc_get_class_reference (OBJC_TYPE_NAME (TREE_TYPE (type)));
4857 args = tree_cons (NULL, t, args);
4858 t = build_function_call (input_location,
4859 objc_exception_match_decl, args);
4860 cond = c_common_truthvalue_conversion (input_location, t);
4862 t = build3 (COND_EXPR, void_type_node, cond, body, NULL);
4863 SET_EXPR_LOCATION (t, EXPR_LOCATION (stmt));
4866 last = &COND_EXPR_ELSE (t);
4872 t = build2 (MODIFY_EXPR, void_type_node, cur_try_context->rethrow_decl,
4873 cur_try_context->caught_decl);
4874 SET_EXPR_LOCATION (t, cur_try_context->end_catch_locus);
4875 append_to_statement_list (t, last);
4877 t = next_sjlj_build_try_exit ();
4878 SET_EXPR_LOCATION (t, cur_try_context->end_catch_locus);
4879 append_to_statement_list (t, last);
4885 /* Build a complete @try-@catch-@finally block for legacy Darwin setjmp
4886 exception handling. We aim to build:
4889 struct _objc_exception_data _stack;
4893 objc_exception_try_enter (&_stack);
4894 if (_setjmp(&_stack.buf))
4896 id _caught = objc_exception_extract(&_stack);
4897 objc_exception_try_enter (&_stack);
4898 if (_setjmp(&_stack.buf))
4899 _rethrow = objc_exception_extract(&_stack);
4909 objc_exception_try_exit(&_stack);
4912 objc_exception_throw(_rethrow);
4916 If CATCH-LIST is empty, we can omit all of the block containing
4917 "_caught" except for the setting of _rethrow. Note the use of
4918 a real TRY_FINALLY_EXPR here, which is not involved in EH per-se,
4919 but handles goto and other exits from the block. */
4922 next_sjlj_build_try_catch_finally (void)
4924 tree rethrow_decl, stack_decl, t;
4925 tree catch_seq, try_fin, bind;
4927 /* Create the declarations involved. */
4928 t = xref_tag (RECORD_TYPE, get_identifier (UTAG_EXCDATA));
4929 stack_decl = objc_create_temporary_var (t, NULL);
4930 cur_try_context->stack_decl = stack_decl;
4932 rethrow_decl = objc_create_temporary_var (objc_object_type, NULL);
4933 cur_try_context->rethrow_decl = rethrow_decl;
4934 TREE_CHAIN (rethrow_decl) = stack_decl;
4936 /* Build the outermost variable binding level. */
4937 bind = build3 (BIND_EXPR, void_type_node, rethrow_decl, NULL, NULL);
4938 SET_EXPR_LOCATION (bind, cur_try_context->try_locus);
4939 TREE_SIDE_EFFECTS (bind) = 1;
4941 /* Initialize rethrow_decl. */
4942 t = build2 (MODIFY_EXPR, void_type_node, rethrow_decl,
4943 convert (objc_object_type, null_pointer_node));
4944 SET_EXPR_LOCATION (t, cur_try_context->try_locus);
4945 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
4947 /* Build the outermost TRY_FINALLY_EXPR. */
4948 try_fin = build2 (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
4949 SET_EXPR_LOCATION (try_fin, cur_try_context->try_locus);
4950 TREE_SIDE_EFFECTS (try_fin) = 1;
4951 append_to_statement_list (try_fin, &BIND_EXPR_BODY (bind));
4953 /* Create the complete catch sequence. */
4954 if (cur_try_context->catch_list)
4956 tree caught_decl = objc_build_exc_ptr ();
4957 catch_seq = build_stmt (input_location, BIND_EXPR, caught_decl, NULL, NULL);
4958 TREE_SIDE_EFFECTS (catch_seq) = 1;
4960 t = next_sjlj_build_exc_extract (caught_decl);
4961 append_to_statement_list (t, &BIND_EXPR_BODY (catch_seq));
4963 t = next_sjlj_build_enter_and_setjmp ();
4964 COND_EXPR_THEN (t) = next_sjlj_build_exc_extract (rethrow_decl);
4965 COND_EXPR_ELSE (t) = next_sjlj_build_catch_list ();
4966 append_to_statement_list (t, &BIND_EXPR_BODY (catch_seq));
4969 catch_seq = next_sjlj_build_exc_extract (rethrow_decl);
4970 SET_EXPR_LOCATION (catch_seq, cur_try_context->end_try_locus);
4972 /* Build the main register-and-try if statement. */
4973 t = next_sjlj_build_enter_and_setjmp ();
4974 SET_EXPR_LOCATION (t, cur_try_context->try_locus);
4975 COND_EXPR_THEN (t) = catch_seq;
4976 COND_EXPR_ELSE (t) = cur_try_context->try_body;
4977 TREE_OPERAND (try_fin, 0) = t;
4979 /* Build the complete FINALLY statement list. */
4980 t = next_sjlj_build_try_exit ();
4981 t = build_stmt (input_location, COND_EXPR,
4982 c_common_truthvalue_conversion
4983 (input_location, rethrow_decl),
4985 SET_EXPR_LOCATION (t, cur_try_context->finally_locus);
4986 append_to_statement_list (t, &TREE_OPERAND (try_fin, 1));
4988 append_to_statement_list (cur_try_context->finally_body,
4989 &TREE_OPERAND (try_fin, 1));
4991 t = tree_cons (NULL, rethrow_decl, NULL);
4992 t = build_function_call (input_location,
4993 objc_exception_throw_decl, t);
4994 t = build_stmt (input_location, COND_EXPR,
4995 c_common_truthvalue_conversion (input_location,
4998 SET_EXPR_LOCATION (t, cur_try_context->end_finally_locus);
4999 append_to_statement_list (t, &TREE_OPERAND (try_fin, 1));
5004 /* Called just after parsing the @try and its associated BODY. We now
5005 must prepare for the tricky bits -- handling the catches and finally. */
5008 objc_begin_try_stmt (location_t try_locus, tree body)
5010 struct objc_try_context *c = XCNEW (struct objc_try_context);
5011 c->outer = cur_try_context;
5013 c->try_locus = try_locus;
5014 c->end_try_locus = input_location;
5015 cur_try_context = c;
5017 /* -fobjc-exceptions is required to enable Objective-C exceptions.
5018 For example, on Darwin, ObjC exceptions require a sufficiently
5019 recent version of the runtime, so the user must ask for them
5020 explicitly. On other platforms, at the moment -fobjc-exceptions
5021 triggers -fexceptions which again is required for exceptions to
5024 if (!flag_objc_exceptions)
5026 error_at (try_locus, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
5029 if (flag_objc_sjlj_exceptions)
5030 objc_mark_locals_volatile (NULL);
5033 /* Called just after parsing "@catch (parm)". Open a binding level,
5034 enter DECL into the binding level, and initialize it. Leave the
5035 binding level open while the body of the compound statement is parsed. */
5038 objc_begin_catch_clause (tree decl)
5040 tree compound, type, t;
5042 /* Begin a new scope that the entire catch clause will live in. */
5043 compound = c_begin_compound_stmt (true);
5045 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
5046 decl = build_decl (input_location,
5047 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
5048 lang_hooks.decls.pushdecl (decl);
5050 /* Since a decl is required here by syntax, don't warn if its unused. */
5051 /* ??? As opposed to __attribute__((unused))? Anyway, this appears to
5052 be what the previous objc implementation did. */
5053 TREE_USED (decl) = 1;
5054 DECL_READ_P (decl) = 1;
5056 /* Verify that the type of the catch is valid. It must be a pointer
5057 to an Objective-C class, or "id" (which is catch-all). */
5058 type = TREE_TYPE (decl);
5060 if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type)))
5062 else if (!POINTER_TYPE_P (type) || !TYPED_OBJECT (TREE_TYPE (type)))
5064 error ("@catch parameter is not a known Objective-C class type");
5065 type = error_mark_node;
5067 else if (cur_try_context->catch_list)
5069 /* Examine previous @catch clauses and see if we've already
5070 caught the type in question. */
5071 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
5072 for (; !tsi_end_p (i); tsi_next (&i))
5074 tree stmt = tsi_stmt (i);
5075 t = CATCH_TYPES (stmt);
5076 if (t == error_mark_node)
5078 if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type)))
5080 warning (0, "exception of type %<%T%> will be caught",
5082 warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>",
5083 TREE_TYPE (t ? t : objc_object_type));
5089 /* Record the data for the catch in the try context so that we can
5090 finalize it later. */
5091 t = build_stmt (input_location, CATCH_EXPR, type, compound);
5092 cur_try_context->current_catch = t;
5094 /* Initialize the decl from the EXC_PTR_EXPR we get from the runtime. */
5095 t = objc_build_exc_ptr ();
5096 t = convert (TREE_TYPE (decl), t);
5097 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
5101 /* Called just after parsing the closing brace of a @catch clause. Close
5102 the open binding level, and record a CATCH_EXPR for it. */
5105 objc_finish_catch_clause (void)
5107 tree c = cur_try_context->current_catch;
5108 cur_try_context->current_catch = NULL;
5109 cur_try_context->end_catch_locus = input_location;
5111 CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1);
5112 append_to_statement_list (c, &cur_try_context->catch_list);
5115 /* Called after parsing a @finally clause and its associated BODY.
5116 Record the body for later placement. */
5119 objc_build_finally_clause (location_t finally_locus, tree body)
5121 cur_try_context->finally_body = body;
5122 cur_try_context->finally_locus = finally_locus;
5123 cur_try_context->end_finally_locus = input_location;
5126 /* Called to finalize a @try construct. */
5129 objc_finish_try_stmt (void)
5131 struct objc_try_context *c = cur_try_context;
5134 if (c->catch_list == NULL && c->finally_body == NULL)
5135 error ("%<@try%> without %<@catch%> or %<@finally%>");
5137 /* If we're doing Darwin setjmp exceptions, build the big nasty. */
5138 if (flag_objc_sjlj_exceptions)
5140 bool save = in_late_binary_op;
5141 in_late_binary_op = true;
5142 if (!cur_try_context->finally_body)
5144 cur_try_context->finally_locus = input_location;
5145 cur_try_context->end_finally_locus = input_location;
5147 stmt = next_sjlj_build_try_catch_finally ();
5148 in_late_binary_op = save;
5152 /* Otherwise, nest the CATCH inside a FINALLY. */
5156 stmt = build_stmt (input_location, TRY_CATCH_EXPR, stmt, c->catch_list);
5157 SET_EXPR_LOCATION (stmt, cur_try_context->try_locus);
5159 if (c->finally_body)
5161 stmt = build_stmt (input_location, TRY_FINALLY_EXPR, stmt, c->finally_body);
5162 SET_EXPR_LOCATION (stmt, cur_try_context->try_locus);
5167 cur_try_context = c->outer;
5173 objc_build_throw_stmt (location_t loc, tree throw_expr)
5177 if (!flag_objc_exceptions)
5179 error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
5182 if (throw_expr == NULL)
5184 /* If we're not inside a @catch block, there is no "current
5185 exception" to be rethrown. */
5186 if (cur_try_context == NULL
5187 || cur_try_context->current_catch == NULL)
5189 error_at (loc, "%<@throw%> (rethrow) used outside of a @catch block");
5193 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
5194 value that we get from the runtime. */
5195 throw_expr = objc_build_exc_ptr ();
5198 /* A throw is just a call to the runtime throw function with the
5199 object as a parameter. */
5200 args = tree_cons (NULL, throw_expr, NULL);
5201 return add_stmt (build_function_call (loc,
5202 objc_exception_throw_decl, args));
5206 objc_build_synchronized (location_t start_locus, tree mutex, tree body)
5210 /* First lock the mutex. */
5211 mutex = save_expr (mutex);
5212 args = tree_cons (NULL, mutex, NULL);
5213 call = build_function_call (input_location,
5214 objc_sync_enter_decl, args);
5215 SET_EXPR_LOCATION (call, start_locus);
5218 /* Build the mutex unlock. */
5219 args = tree_cons (NULL, mutex, NULL);
5220 call = build_function_call (input_location,
5221 objc_sync_exit_decl, args);
5222 SET_EXPR_LOCATION (call, input_location);
5224 /* Put the that and the body in a TRY_FINALLY. */
5225 objc_begin_try_stmt (start_locus, body);
5226 objc_build_finally_clause (input_location, call);
5227 return objc_finish_try_stmt ();
5231 /* Predefine the following data type:
5233 struct _objc_exception_data
5235 int buf[OBJC_JBLEN];
5239 /* The following yuckiness should prevent users from having to #include
5240 <setjmp.h> in their code... */
5242 /* Define to a harmless positive value so the below code doesn't die. */
5244 #define OBJC_JBLEN 18
5248 build_next_objc_exception_stuff (void)
5250 tree decls, temp_type, *chain = NULL;
5252 objc_exception_data_template
5253 = objc_start_struct (get_identifier (UTAG_EXCDATA));
5255 /* int buf[OBJC_JBLEN]; */
5257 temp_type = build_sized_array_type (integer_type_node, OBJC_JBLEN);
5258 decls = add_field_decl (temp_type, "buf", &chain);
5260 /* void *pointers[4]; */
5262 temp_type = build_sized_array_type (ptr_type_node, 4);
5263 add_field_decl (temp_type, "pointers", &chain);
5265 objc_finish_struct (objc_exception_data_template, decls);
5267 /* int _setjmp(...); */
5268 /* If the user includes <setjmp.h>, this shall be superseded by
5269 'int _setjmp(jmp_buf);' */
5270 temp_type = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5272 = add_builtin_function (TAG_SETJMP, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
5274 /* id objc_exception_extract(struct _objc_exception_data *); */
5276 = build_function_type_list (objc_object_type,
5277 build_pointer_type (objc_exception_data_template),
5279 objc_exception_extract_decl
5280 = add_builtin_function (TAG_EXCEPTIONEXTRACT, temp_type, 0, NOT_BUILT_IN, NULL,
5282 /* void objc_exception_try_enter(struct _objc_exception_data *); */
5283 /* void objc_exception_try_exit(struct _objc_exception_data *); */
5285 = build_function_type_list (void_type_node,
5286 build_pointer_type (objc_exception_data_template),
5288 objc_exception_try_enter_decl
5289 = add_builtin_function (TAG_EXCEPTIONTRYENTER, temp_type, 0, NOT_BUILT_IN, NULL,
5291 objc_exception_try_exit_decl
5292 = add_builtin_function (TAG_EXCEPTIONTRYEXIT, temp_type, 0, NOT_BUILT_IN, NULL,
5295 /* int objc_exception_match(id, id); */
5297 = build_function_type_list (integer_type_node,
5298 objc_object_type, objc_object_type, NULL_TREE);
5299 objc_exception_match_decl
5300 = add_builtin_function (TAG_EXCEPTIONMATCH, temp_type, 0, NOT_BUILT_IN, NULL,
5303 /* id objc_assign_ivar (id, id, unsigned int); */
5304 /* id objc_assign_ivar_Fast (id, id, unsigned int)
5305 __attribute__ ((hard_coded_address (OFFS_ASSIGNIVAR_FAST))); */
5307 = build_function_type_list (objc_object_type,
5312 objc_assign_ivar_decl
5313 = add_builtin_function (TAG_ASSIGNIVAR, temp_type, 0, NOT_BUILT_IN,
5315 #ifdef OFFS_ASSIGNIVAR_FAST
5316 objc_assign_ivar_fast_decl
5317 = add_builtin_function (TAG_ASSIGNIVAR_FAST, temp_type, 0,
5318 NOT_BUILT_IN, NULL, NULL_TREE);
5319 DECL_ATTRIBUTES (objc_assign_ivar_fast_decl)
5320 = tree_cons (get_identifier ("hard_coded_address"),
5321 build_int_cst (NULL_TREE, OFFS_ASSIGNIVAR_FAST),
5324 /* Default to slower ivar method. */
5325 objc_assign_ivar_fast_decl = objc_assign_ivar_decl;
5328 /* id objc_assign_global (id, id *); */
5329 /* id objc_assign_strongCast (id, id *); */
5330 temp_type = build_function_type_list (objc_object_type,
5332 build_pointer_type (objc_object_type),
5334 objc_assign_global_decl
5335 = add_builtin_function (TAG_ASSIGNGLOBAL, temp_type, 0, NOT_BUILT_IN, NULL,
5337 objc_assign_strong_cast_decl
5338 = add_builtin_function (TAG_ASSIGNSTRONGCAST, temp_type, 0, NOT_BUILT_IN, NULL,
5343 build_objc_exception_stuff (void)
5345 tree noreturn_list, nothrow_list, temp_type;
5347 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
5348 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
5350 /* void objc_exception_throw(id) __attribute__((noreturn)); */
5351 /* void objc_sync_enter(id); */
5352 /* void objc_sync_exit(id); */
5353 temp_type = build_function_type_list (void_type_node,
5356 objc_exception_throw_decl
5357 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
5359 objc_sync_enter_decl
5360 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
5361 NULL, nothrow_list);
5363 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
5364 NULL, nothrow_list);
5367 /* Construct a C struct corresponding to ObjC class CLASS, with the same
5370 struct <classname> {
5371 struct _objc_class *isa;
5376 build_private_template (tree klass)
5378 if (!CLASS_STATIC_TEMPLATE (klass))
5380 tree record = objc_build_struct (klass,
5381 get_class_ivars (klass, false),
5382 CLASS_SUPER_NAME (klass));
5384 /* Set the TREE_USED bit for this struct, so that stab generator
5385 can emit stabs for this struct type. */
5386 if (flag_debug_only_used_symbols && TYPE_STUB_DECL (record))
5387 TREE_USED (TYPE_STUB_DECL (record)) = 1;
5391 /* Begin code generation for protocols... */
5393 /* struct _objc_protocol {
5394 struct _objc_class *isa;
5395 char *protocol_name;
5396 struct _objc_protocol **protocol_list;
5397 struct _objc__method_prototype_list *instance_methods;
5398 struct _objc__method_prototype_list *class_methods;
5402 build_protocol_template (void)
5404 tree ptype, decls, *chain = NULL;
5406 objc_protocol_template = objc_start_struct (get_identifier (UTAG_PROTOCOL));
5408 /* struct _objc_class *isa; */
5409 ptype = build_pointer_type (xref_tag (RECORD_TYPE,
5410 get_identifier (UTAG_CLASS)));
5411 decls = add_field_decl (ptype, "isa", &chain);
5413 /* char *protocol_name; */
5414 add_field_decl (string_type_node, "protocol_name", &chain);
5416 /* struct _objc_protocol **protocol_list; */
5417 ptype = build_pointer_type (build_pointer_type (objc_protocol_template));
5418 add_field_decl (ptype, "protocol_list", &chain);
5420 /* struct _objc__method_prototype_list *instance_methods; */
5421 add_field_decl (objc_method_proto_list_ptr, "instance_methods", &chain);
5423 /* struct _objc__method_prototype_list *class_methods; */
5424 add_field_decl (objc_method_proto_list_ptr, "class_methods", &chain);
5426 objc_finish_struct (objc_protocol_template, decls);
5430 build_descriptor_table_initializer (tree type, tree entries)
5432 VEC(constructor_elt,gc) *inits = NULL;
5436 VEC(constructor_elt,gc) *elts = NULL;
5438 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
5439 build_selector (METHOD_SEL_NAME (entries)));
5440 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
5441 add_objc_string (METHOD_ENCODING (entries),
5444 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
5445 objc_build_constructor (type, elts));
5447 entries = DECL_CHAIN (entries);
5451 return objc_build_constructor (build_array_type (type, 0), inits);
5454 /* struct objc_method_prototype_list {
5456 struct objc_method_prototype {
5463 build_method_prototype_list_template (tree list_type, int size)
5465 tree objc_ivar_list_record;
5466 tree array_type, decls, *chain = NULL;
5468 /* Generate an unnamed struct definition. */
5470 objc_ivar_list_record = objc_start_struct (NULL_TREE);
5472 /* int method_count; */
5473 decls = add_field_decl (integer_type_node, "method_count", &chain);
5475 /* struct objc_method method_list[]; */
5476 array_type = build_sized_array_type (list_type, size);
5477 add_field_decl (array_type, "method_list", &chain);
5479 objc_finish_struct (objc_ivar_list_record, decls);
5481 return objc_ivar_list_record;
5485 build_method_prototype_template (void)
5488 tree decls, *chain = NULL;
5490 proto_record = objc_start_struct (get_identifier (UTAG_METHOD_PROTOTYPE));
5493 decls = add_field_decl (objc_selector_type, "_cmd", &chain);
5495 /* char *method_types; */
5496 add_field_decl (string_type_node, "method_types", &chain);
5498 objc_finish_struct (proto_record, decls);
5500 return proto_record;
5504 objc_method_parm_type (tree type)
5506 type = TREE_VALUE (TREE_TYPE (type));
5507 if (TREE_CODE (type) == TYPE_DECL)
5508 type = TREE_TYPE (type);
5513 objc_encoded_type_size (tree type)
5515 int sz = int_size_in_bytes (type);
5517 /* Make all integer and enum types at least as large
5519 if (sz > 0 && INTEGRAL_TYPE_P (type))
5520 sz = MAX (sz, int_size_in_bytes (integer_type_node));
5521 /* Treat arrays as pointers, since that's how they're
5523 else if (TREE_CODE (type) == ARRAY_TYPE)
5524 sz = int_size_in_bytes (ptr_type_node);
5528 /* Encode a method prototype.
5530 The format is described in gcc/doc/objc.texi, section 'Method
5534 encode_method_prototype (tree method_decl)
5541 /* ONEWAY and BYCOPY, for remote object are the only method qualifiers. */
5542 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (method_decl)));
5544 /* Encode return type. */
5545 encode_type (objc_method_parm_type (method_decl),
5546 obstack_object_size (&util_obstack),
5547 OBJC_ENCODE_INLINE_DEFS);
5550 /* The first two arguments (self and _cmd) are pointers; account for
5552 i = int_size_in_bytes (ptr_type_node);
5553 parm_offset = 2 * i;
5554 for (parms = METHOD_SEL_ARGS (method_decl); parms;
5555 parms = DECL_CHAIN (parms))
5557 tree type = objc_method_parm_type (parms);
5558 int sz = objc_encoded_type_size (type);
5560 /* If a type size is not known, bail out. */
5563 error ("type %q+D does not have a known size",
5565 /* Pretend that the encoding succeeded; the compilation will
5566 fail nevertheless. */
5567 goto finish_encoding;
5572 sprintf (buf, "%d@0:%d", parm_offset, i);
5573 obstack_grow (&util_obstack, buf, strlen (buf));
5575 /* Argument types. */
5576 parm_offset = 2 * i;
5577 for (parms = METHOD_SEL_ARGS (method_decl); parms;
5578 parms = DECL_CHAIN (parms))
5580 tree type = objc_method_parm_type (parms);
5582 /* Process argument qualifiers for user supplied arguments. */
5583 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (parms)));
5586 encode_type (type, obstack_object_size (&util_obstack),
5587 OBJC_ENCODE_INLINE_DEFS);
5589 /* Compute offset. */
5590 sprintf (buf, "%d", parm_offset);
5591 parm_offset += objc_encoded_type_size (type);
5593 obstack_grow (&util_obstack, buf, strlen (buf));
5597 obstack_1grow (&util_obstack, '\0');
5598 result = get_identifier (XOBFINISH (&util_obstack, char *));
5599 obstack_free (&util_obstack, util_firstobj);
5604 generate_descriptor_table (tree type, const char *name, int size, tree list,
5608 VEC(constructor_elt,gc) *v = NULL;
5610 decl = start_var_decl (type, synth_id_with_class_suffix (name, proto));
5612 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, size));
5613 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, list);
5615 finish_var_decl (decl, objc_build_constructor (type, v));
5621 generate_method_descriptors (tree protocol)
5623 tree initlist, chain, method_list_template;
5626 if (!objc_method_prototype_template)
5627 objc_method_prototype_template = build_method_prototype_template ();
5629 chain = PROTOCOL_CLS_METHODS (protocol);
5632 size = list_length (chain);
5634 method_list_template
5635 = build_method_prototype_list_template (objc_method_prototype_template,
5639 = build_descriptor_table_initializer (objc_method_prototype_template,
5642 UOBJC_CLASS_METHODS_decl
5643 = generate_descriptor_table (method_list_template,
5644 "_OBJC_PROTOCOL_CLASS_METHODS",
5645 size, initlist, protocol);
5648 UOBJC_CLASS_METHODS_decl = 0;
5650 chain = PROTOCOL_NST_METHODS (protocol);
5653 size = list_length (chain);
5655 method_list_template
5656 = build_method_prototype_list_template (objc_method_prototype_template,
5659 = build_descriptor_table_initializer (objc_method_prototype_template,
5662 UOBJC_INSTANCE_METHODS_decl
5663 = generate_descriptor_table (method_list_template,
5664 "_OBJC_PROTOCOL_INSTANCE_METHODS",
5665 size, initlist, protocol);
5668 UOBJC_INSTANCE_METHODS_decl = 0;
5672 generate_protocol_references (tree plist)
5676 /* Forward declare protocols referenced. */
5677 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
5679 tree proto = TREE_VALUE (lproto);
5681 if (TREE_CODE (proto) == PROTOCOL_INTERFACE_TYPE
5682 && PROTOCOL_NAME (proto))
5684 if (! PROTOCOL_FORWARD_DECL (proto))
5685 build_protocol_reference (proto);
5687 if (PROTOCOL_LIST (proto))
5688 generate_protocol_references (PROTOCOL_LIST (proto));
5693 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
5697 objc_generate_cxx_ctor_or_dtor (bool dtor)
5699 tree fn, body, compound_stmt, ivar;
5701 /* - (id) .cxx_construct { ... return self; } */
5702 /* - (void) .cxx_construct { ... } */
5704 objc_start_method_definition
5705 (false /* is_class_method */,
5706 objc_build_method_signature (false /* is_class_method */,
5707 build_tree_list (NULL_TREE,
5710 : objc_object_type),
5711 get_identifier (dtor
5713 : TAG_CXX_CONSTRUCT),
5714 make_node (TREE_LIST),
5716 body = begin_function_body ();
5717 compound_stmt = begin_compound_stmt (0);
5719 ivar = CLASS_IVARS (implementation_template);
5720 /* Destroy ivars in reverse order. */
5722 ivar = nreverse (copy_list (ivar));
5724 for (; ivar; ivar = TREE_CHAIN (ivar))
5726 if (TREE_CODE (ivar) == FIELD_DECL)
5728 tree type = TREE_TYPE (ivar);
5730 /* Call the ivar's default constructor or destructor. Do not
5731 call the destructor unless a corresponding constructor call
5732 has also been made (or is not needed). */
5733 if (MAYBE_CLASS_TYPE_P (type)
5735 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
5736 && (!TYPE_NEEDS_CONSTRUCTING (type)
5737 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5738 : (TYPE_NEEDS_CONSTRUCTING (type)
5739 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))))
5741 (build_special_member_call
5742 (build_ivar_reference (DECL_NAME (ivar)),
5743 dtor ? complete_dtor_identifier : complete_ctor_identifier,
5744 NULL, type, LOOKUP_NORMAL, tf_warning_or_error));
5748 /* The constructor returns 'self'. */
5750 finish_return_stmt (self_decl);
5752 finish_compound_stmt (compound_stmt);
5753 finish_function_body (body);
5754 fn = current_function_decl;
5756 objc_finish_method_definition (fn);
5759 /* The following routine will examine the current @interface for any
5760 non-POD C++ ivars requiring non-trivial construction and/or
5761 destruction, and then synthesize special '- .cxx_construct' and/or
5762 '- .cxx_destruct' methods which will run the appropriate
5763 construction or destruction code. Note that ivars inherited from
5764 super-classes are _not_ considered. */
5766 objc_generate_cxx_cdtors (void)
5768 bool need_ctor = false, need_dtor = false;
5771 /* Error case, due to possibly an extra @end. */
5772 if (!objc_implementation_context)
5775 /* We do not want to do this for categories, since they do not have
5778 if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE)
5781 /* First, determine if we even need a constructor and/or destructor. */
5783 for (ivar = CLASS_IVARS (implementation_template); ivar;
5784 ivar = TREE_CHAIN (ivar))
5786 if (TREE_CODE (ivar) == FIELD_DECL)
5788 tree type = TREE_TYPE (ivar);
5790 if (MAYBE_CLASS_TYPE_P (type))
5792 if (TYPE_NEEDS_CONSTRUCTING (type)
5793 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
5794 /* NB: If a default constructor is not available, we will not
5795 be able to initialize this ivar; the add_instance_variable()
5796 routine will already have warned about this. */
5799 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
5800 && (!TYPE_NEEDS_CONSTRUCTING (type)
5801 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5802 /* NB: If a default constructor is not available, we will not
5803 call the destructor either, for symmetry. */
5809 /* Generate '- .cxx_construct' if needed. */
5812 objc_generate_cxx_ctor_or_dtor (false);
5814 /* Generate '- .cxx_destruct' if needed. */
5817 objc_generate_cxx_ctor_or_dtor (true);
5819 /* The 'imp_list' variable points at an imp_entry record for the current
5820 @implementation. Record the existence of '- .cxx_construct' and/or
5821 '- .cxx_destruct' methods therein; it will be included in the
5822 metadata for the class. */
5823 if (flag_next_runtime)
5824 imp_list->has_cxx_cdtors = (need_ctor || need_dtor);
5828 /* For each protocol which was referenced either from a @protocol()
5829 expression, or because a class/category implements it (then a
5830 pointer to the protocol is stored in the struct describing the
5831 class/category), we create a statically allocated instance of the
5832 Protocol class. The code is written in such a way as to generate
5833 as few Protocol objects as possible; we generate a unique Protocol
5834 instance for each protocol, and we don't generate a Protocol
5835 instance if the protocol is never referenced (either from a
5836 @protocol() or from a class/category implementation). These
5837 statically allocated objects can be referred to via the static
5838 (that is, private to this module) symbols _OBJC_PROTOCOL_n.
5840 The statically allocated Protocol objects that we generate here
5841 need to be fixed up at runtime in order to be used: the 'isa'
5842 pointer of the objects need to be set up to point to the 'Protocol'
5843 class, as known at runtime.
5845 The NeXT runtime fixes up all protocols at program startup time,
5846 before main() is entered. It uses a low-level trick to look up all
5847 those symbols, then loops on them and fixes them up.
5849 The GNU runtime as well fixes up all protocols before user code
5850 from the module is executed; it requires pointers to those symbols
5851 to be put in the objc_symtab (which is then passed as argument to
5852 the function __objc_exec_class() which the compiler sets up to be
5853 executed automatically when the module is loaded); setup of those
5854 Protocol objects happen in two ways in the GNU runtime: all
5855 Protocol objects referred to by a class or category implementation
5856 are fixed up when the class/category is loaded; all Protocol
5857 objects referred to by a @protocol() expression are added by the
5858 compiler to the list of statically allocated instances to fixup
5859 (the same list holding the statically allocated constant string
5860 objects). Because, as explained above, the compiler generates as
5861 few Protocol objects as possible, some Protocol object might end up
5862 being referenced multiple times when compiled with the GNU runtime,
5863 and end up being fixed up multiple times at runtime initialization.
5864 But that doesn't hurt, it's just a little inefficient. */
5867 generate_protocols (void)
5871 tree initlist, protocol_name_expr, refs_decl, refs_expr;
5873 /* If a protocol was directly referenced, pull in indirect references. */
5874 for (p = protocol_chain; p; p = TREE_CHAIN (p))
5875 if (PROTOCOL_FORWARD_DECL (p) && PROTOCOL_LIST (p))
5876 generate_protocol_references (PROTOCOL_LIST (p));
5878 for (p = protocol_chain; p; p = TREE_CHAIN (p))
5880 tree nst_methods = PROTOCOL_NST_METHODS (p);
5881 tree cls_methods = PROTOCOL_CLS_METHODS (p);
5883 /* If protocol wasn't referenced, don't generate any code. */
5884 decl = PROTOCOL_FORWARD_DECL (p);
5889 /* Make sure we link in the Protocol class. */
5890 add_class_reference (get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
5894 if (! METHOD_ENCODING (nst_methods))
5896 encoding = encode_method_prototype (nst_methods);
5897 METHOD_ENCODING (nst_methods) = encoding;
5899 nst_methods = DECL_CHAIN (nst_methods);
5904 if (! METHOD_ENCODING (cls_methods))
5906 encoding = encode_method_prototype (cls_methods);
5907 METHOD_ENCODING (cls_methods) = encoding;
5910 cls_methods = DECL_CHAIN (cls_methods);
5912 generate_method_descriptors (p);
5914 if (PROTOCOL_LIST (p))
5915 refs_decl = generate_protocol_list (p);
5919 /* static struct objc_protocol _OBJC_PROTOCOL_<mumble>; */
5920 protocol_name_expr = add_objc_string (PROTOCOL_NAME (p), class_names);
5923 refs_expr = convert (build_pointer_type (build_pointer_type
5924 (objc_protocol_template)),
5925 build_unary_op (input_location,
5926 ADDR_EXPR, refs_decl, 0));
5928 refs_expr = build_int_cst (NULL_TREE, 0);
5930 /* UOBJC_INSTANCE_METHODS_decl/UOBJC_CLASS_METHODS_decl are set
5931 by generate_method_descriptors, which is called above. */
5932 initlist = build_protocol_initializer (TREE_TYPE (decl),
5933 protocol_name_expr, refs_expr,
5934 UOBJC_INSTANCE_METHODS_decl,
5935 UOBJC_CLASS_METHODS_decl);
5936 finish_var_decl (decl, initlist);
5941 build_protocol_initializer (tree type, tree protocol_name,
5942 tree protocol_list, tree instance_methods,
5946 tree cast_type = build_pointer_type
5947 (xref_tag (RECORD_TYPE,
5948 get_identifier (UTAG_CLASS)));
5949 VEC(constructor_elt,gc) *inits = NULL;
5951 /* Filling the "isa" in with one allows the runtime system to
5952 detect that the version change...should remove before final release. */
5954 expr = build_int_cst (cast_type, PROTOCOL_VERSION);
5955 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
5956 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, protocol_name);
5957 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, protocol_list);
5959 if (!instance_methods)
5960 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, 0));
5963 expr = convert (objc_method_proto_list_ptr,
5964 build_unary_op (input_location,
5965 ADDR_EXPR, instance_methods, 0));
5966 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
5970 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, 0));
5973 expr = convert (objc_method_proto_list_ptr,
5974 build_unary_op (input_location,
5975 ADDR_EXPR, class_methods, 0));
5976 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
5979 return objc_build_constructor (type, inits);
5982 /* struct _objc_category {
5983 char *category_name;
5985 struct _objc_method_list *instance_methods;
5986 struct _objc_method_list *class_methods;
5987 struct _objc_protocol_list *protocols;
5991 build_category_template (void)
5993 tree ptype, decls, *chain = NULL;
5995 objc_category_template = objc_start_struct (get_identifier (UTAG_CATEGORY));
5997 /* char *category_name; */
5998 decls = add_field_decl (string_type_node, "category_name", &chain);
6000 /* char *class_name; */
6001 add_field_decl (string_type_node, "class_name", &chain);
6003 /* struct _objc_method_list *instance_methods; */
6004 add_field_decl (objc_method_list_ptr, "instance_methods", &chain);
6006 /* struct _objc_method_list *class_methods; */
6007 add_field_decl (objc_method_list_ptr, "class_methods", &chain);
6009 /* struct _objc_protocol **protocol_list; */
6010 ptype = build_pointer_type (build_pointer_type (objc_protocol_template));
6011 add_field_decl (ptype, "protocol_list", &chain);
6013 objc_finish_struct (objc_category_template, decls);
6016 /* struct _objc_selector {
6022 build_selector_template (void)
6024 tree decls, *chain = NULL;
6026 objc_selector_template = objc_start_struct (get_identifier (UTAG_SELECTOR));
6029 decls = add_field_decl (objc_selector_type, "sel_id", &chain);
6031 /* char *sel_type; */
6032 add_field_decl (string_type_node, "sel_type", &chain);
6034 objc_finish_struct (objc_selector_template, decls);
6037 /* struct _objc_class {
6038 struct _objc_class *isa;
6039 struct _objc_class *super_class;
6044 struct _objc_ivar_list *ivars;
6045 struct _objc_method_list *methods;
6046 #ifdef __NEXT_RUNTIME__
6047 struct objc_cache *cache;
6049 struct sarray *dtable;
6050 struct _objc_class *subclass_list;
6051 struct _objc_class *sibling_class;
6053 struct _objc_protocol_list *protocols;
6054 #ifdef __NEXT_RUNTIME__
6057 void *gc_object_type;
6060 /* NB: The 'sel_id' and 'gc_object_type' fields are not being used by
6061 the NeXT/Apple runtime; still, the compiler must generate them to
6062 maintain backward binary compatibility (and to allow for future
6066 build_class_template (void)
6068 tree ptype, decls, *chain = NULL;
6070 objc_class_template = objc_start_struct (get_identifier (UTAG_CLASS));
6072 /* struct _objc_class *isa; */
6073 decls = add_field_decl (build_pointer_type (objc_class_template),
6076 /* struct _objc_class *super_class; */
6077 add_field_decl (build_pointer_type (objc_class_template),
6078 "super_class", &chain);
6081 add_field_decl (string_type_node, "name", &chain);
6084 add_field_decl (long_integer_type_node, "version", &chain);
6087 add_field_decl (long_integer_type_node, "info", &chain);
6089 /* long instance_size; */
6090 add_field_decl (long_integer_type_node, "instance_size", &chain);
6092 /* struct _objc_ivar_list *ivars; */
6093 add_field_decl (objc_ivar_list_ptr,"ivars", &chain);
6095 /* struct _objc_method_list *methods; */
6096 add_field_decl (objc_method_list_ptr, "methods", &chain);
6098 if (flag_next_runtime)
6100 /* struct objc_cache *cache; */
6101 ptype = build_pointer_type (xref_tag (RECORD_TYPE,
6102 get_identifier ("objc_cache")));
6103 add_field_decl (ptype, "cache", &chain);
6107 /* struct sarray *dtable; */
6108 ptype = build_pointer_type(xref_tag (RECORD_TYPE,
6109 get_identifier ("sarray")));
6110 add_field_decl (ptype, "dtable", &chain);
6112 /* struct objc_class *subclass_list; */
6113 ptype = build_pointer_type (objc_class_template);
6114 add_field_decl (ptype, "subclass_list", &chain);
6116 /* struct objc_class *sibling_class; */
6117 ptype = build_pointer_type (objc_class_template);
6118 add_field_decl (ptype, "sibling_class", &chain);
6121 /* struct _objc_protocol **protocol_list; */
6122 ptype = build_pointer_type (build_pointer_type
6123 (xref_tag (RECORD_TYPE,
6124 get_identifier (UTAG_PROTOCOL))));
6125 add_field_decl (ptype, "protocol_list", &chain);
6127 if (flag_next_runtime)
6130 add_field_decl (build_pointer_type (void_type_node), "sel_id", &chain);
6133 /* void *gc_object_type; */
6134 add_field_decl (build_pointer_type (void_type_node),
6135 "gc_object_type", &chain);
6137 objc_finish_struct (objc_class_template, decls);
6140 /* Generate appropriate forward declarations for an implementation. */
6143 synth_forward_declarations (void)
6147 /* static struct objc_class _OBJC_CLASS_<my_name>; */
6148 UOBJC_CLASS_decl = build_metadata_decl ("_OBJC_CLASS",
6149 objc_class_template);
6151 /* static struct objc_class _OBJC_METACLASS_<my_name>; */
6152 UOBJC_METACLASS_decl = build_metadata_decl ("_OBJC_METACLASS",
6153 objc_class_template);
6155 /* Pre-build the following entities - for speed/convenience. */
6157 an_id = get_identifier ("super_class");
6158 ucls_super_ref = objc_build_component_ref (UOBJC_CLASS_decl, an_id);
6159 uucls_super_ref = objc_build_component_ref (UOBJC_METACLASS_decl, an_id);
6163 error_with_ivar (const char *message, tree decl)
6165 error_at (DECL_SOURCE_LOCATION (decl), "%s %qs",
6166 message, identifier_to_locale (gen_declaration (decl)));
6171 check_ivars (tree inter, tree imp)
6173 tree intdecls = CLASS_RAW_IVARS (inter);
6174 tree impdecls = CLASS_RAW_IVARS (imp);
6181 if (intdecls && TREE_CODE (intdecls) == TYPE_DECL)
6182 intdecls = TREE_CHAIN (intdecls);
6184 if (intdecls == 0 && impdecls == 0)
6186 if (intdecls == 0 || impdecls == 0)
6188 error ("inconsistent instance variable specification");
6192 t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
6194 if (!comptypes (t1, t2)
6195 || !tree_int_cst_equal (DECL_INITIAL (intdecls),
6196 DECL_INITIAL (impdecls)))
6198 if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
6200 error_with_ivar ("conflicting instance variable type",
6202 error_with_ivar ("previous declaration of",
6205 else /* both the type and the name don't match */
6207 error ("inconsistent instance variable specification");
6212 else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
6214 error_with_ivar ("conflicting instance variable name",
6216 error_with_ivar ("previous declaration of",
6220 intdecls = DECL_CHAIN (intdecls);
6221 impdecls = DECL_CHAIN (impdecls);
6225 /* Set 'objc_super_template' to the data type node for 'struct _objc_super'.
6226 This needs to be done just once per compilation. */
6228 /* struct _objc_super {
6229 struct _objc_object *self;
6230 struct _objc_class *super_class;
6234 build_super_template (void)
6236 tree decls, *chain = NULL;
6238 objc_super_template = objc_start_struct (get_identifier (UTAG_SUPER));
6240 /* struct _objc_object *self; */
6241 decls = add_field_decl (objc_object_type, "self", &chain);
6243 /* struct _objc_class *super_class; */
6244 add_field_decl (build_pointer_type (objc_class_template),
6245 "super_class", &chain);
6247 objc_finish_struct (objc_super_template, decls);
6250 /* struct _objc_ivar {
6257 build_ivar_template (void)
6259 tree objc_ivar_id, objc_ivar_record;
6260 tree decls, *chain = NULL;
6262 objc_ivar_id = get_identifier (UTAG_IVAR);
6263 objc_ivar_record = objc_start_struct (objc_ivar_id);
6265 /* char *ivar_name; */
6266 decls = add_field_decl (string_type_node, "ivar_name", &chain);
6268 /* char *ivar_type; */
6269 add_field_decl (string_type_node, "ivar_type", &chain);
6271 /* int ivar_offset; */
6272 add_field_decl (integer_type_node, "ivar_offset", &chain);
6274 objc_finish_struct (objc_ivar_record, decls);
6276 return objc_ivar_record;
6281 struct objc_ivar ivar_list[ivar_count];
6285 build_ivar_list_template (tree list_type, int size)
6287 tree objc_ivar_list_record;
6288 tree array_type, decls, *chain = NULL;
6290 objc_ivar_list_record = objc_start_struct (NULL_TREE);
6292 /* int ivar_count; */
6293 decls = add_field_decl (integer_type_node, "ivar_count", &chain);
6295 /* struct objc_ivar ivar_list[]; */
6296 array_type = build_sized_array_type (list_type, size);
6297 add_field_decl (array_type, "ivar_list", &chain);
6299 objc_finish_struct (objc_ivar_list_record, decls);
6301 return objc_ivar_list_record;
6305 struct _objc__method_prototype_list *method_next;
6307 struct objc_method method_list[method_count];
6311 build_method_list_template (tree list_type, int size)
6313 tree objc_ivar_list_record;
6314 tree array_type, decls, *chain = NULL;
6316 objc_ivar_list_record = objc_start_struct (NULL_TREE);
6318 /* struct _objc__method_prototype_list *method_next; */
6319 decls = add_field_decl (objc_method_proto_list_ptr, "method_next", &chain);
6321 /* int method_count; */
6322 add_field_decl (integer_type_node, "method_count", &chain);
6324 /* struct objc_method method_list[]; */
6325 array_type = build_sized_array_type (list_type, size);
6326 add_field_decl (array_type, "method_list", &chain);
6328 objc_finish_struct (objc_ivar_list_record, decls);
6330 return objc_ivar_list_record;
6334 build_ivar_list_initializer (tree type, tree field_decl)
6336 VEC(constructor_elt,gc) *inits = NULL;
6340 VEC(constructor_elt,gc) *ivar = NULL;
6344 if (DECL_NAME (field_decl))
6345 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE,
6346 add_objc_string (DECL_NAME (field_decl),
6349 /* Unnamed bit-field ivar (yuck). */
6350 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, build_int_cst (NULL_TREE, 0));
6353 encode_field_decl (field_decl,
6354 obstack_object_size (&util_obstack),
6355 OBJC_ENCODE_DONT_INLINE_DEFS);
6357 /* Null terminate string. */
6358 obstack_1grow (&util_obstack, 0);
6359 id = add_objc_string (get_identifier (XOBFINISH (&util_obstack, char *)),
6361 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, id);
6362 obstack_free (&util_obstack, util_firstobj);
6365 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, byte_position (field_decl));
6366 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
6367 objc_build_constructor (type, ivar));
6369 field_decl = DECL_CHAIN (field_decl);
6370 while (field_decl && TREE_CODE (field_decl) != FIELD_DECL);
6374 return objc_build_constructor (build_array_type (type, 0), inits);
6378 generate_ivars_list (tree type, const char *name, int size, tree list)
6381 VEC(constructor_elt,gc) *inits = NULL;
6383 decl = start_var_decl (type, synth_id_with_class_suffix
6384 (name, objc_implementation_context));
6386 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, size));
6387 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, list);
6389 finish_var_decl (decl,
6390 objc_build_constructor (TREE_TYPE (decl), inits));
6395 /* Count only the fields occurring in T. */
6398 ivar_list_length (tree t)
6402 for (; t; t = DECL_CHAIN (t))
6403 if (TREE_CODE (t) == FIELD_DECL)
6410 generate_ivar_lists (void)
6412 tree initlist, ivar_list_template, chain;
6415 generating_instance_variables = 1;
6417 if (!objc_ivar_template)
6418 objc_ivar_template = build_ivar_template ();
6420 /* Only generate class variables for the root of the inheritance
6421 hierarchy since these will be the same for every class. */
6423 if (CLASS_SUPER_NAME (implementation_template) == NULL_TREE
6424 && (chain = TYPE_FIELDS (objc_class_template)))
6426 size = ivar_list_length (chain);
6428 ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
6429 initlist = build_ivar_list_initializer (objc_ivar_template, chain);
6431 UOBJC_CLASS_VARIABLES_decl
6432 = generate_ivars_list (ivar_list_template, "_OBJC_CLASS_VARIABLES",
6436 UOBJC_CLASS_VARIABLES_decl = 0;
6438 chain = CLASS_IVARS (implementation_template);
6441 size = ivar_list_length (chain);
6442 ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
6443 initlist = build_ivar_list_initializer (objc_ivar_template, chain);
6445 UOBJC_INSTANCE_VARIABLES_decl
6446 = generate_ivars_list (ivar_list_template, "_OBJC_INSTANCE_VARIABLES",
6450 UOBJC_INSTANCE_VARIABLES_decl = 0;
6452 generating_instance_variables = 0;
6456 build_dispatch_table_initializer (tree type, tree entries)
6458 VEC(constructor_elt,gc) *inits = NULL;
6462 VEC(constructor_elt,gc) *elems = NULL;
6465 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE,
6466 build_selector (METHOD_SEL_NAME (entries)));
6468 /* Generate the method encoding if we don't have one already. */
6469 if (! METHOD_ENCODING (entries))
6470 METHOD_ENCODING (entries) =
6471 encode_method_prototype (entries);
6473 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE,
6474 add_objc_string (METHOD_ENCODING (entries),
6477 expr = convert (ptr_type_node,
6478 build_unary_op (input_location, ADDR_EXPR,
6479 METHOD_DEFINITION (entries), 1));
6480 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE, expr);
6482 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
6483 objc_build_constructor (type, elems));
6485 entries = DECL_CHAIN (entries);
6489 return objc_build_constructor (build_array_type (type, 0), inits);
6492 /* To accomplish method prototyping without generating all kinds of
6493 inane warnings, the definition of the dispatch table entries were
6496 struct objc_method { SEL _cmd; ...; id (*_imp)(); };
6498 struct objc_method { SEL _cmd; ...; void *_imp; }; */
6501 build_method_template (void)
6504 tree decls, *chain = NULL;
6506 _SLT_record = objc_start_struct (get_identifier (UTAG_METHOD));
6509 decls = add_field_decl (objc_selector_type, "_cmd", &chain);
6511 /* char *method_types; */
6512 add_field_decl (string_type_node, "method_types", &chain);
6515 add_field_decl (build_pointer_type (void_type_node), "_imp", &chain);
6517 objc_finish_struct (_SLT_record, decls);
6524 generate_dispatch_table (tree type, const char *name, int size, tree list)
6527 VEC(constructor_elt,gc) *v = NULL;
6529 decl = start_var_decl (type, synth_id_with_class_suffix
6530 (name, objc_implementation_context));
6532 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
6533 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (integer_type_node, size));
6534 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, list);
6536 finish_var_decl (decl,
6537 objc_build_constructor (TREE_TYPE (decl), v));
6543 mark_referenced_methods (void)
6545 struct imp_entry *impent;
6548 for (impent = imp_list; impent; impent = impent->next)
6550 chain = CLASS_CLS_METHODS (impent->imp_context);
6553 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain)));
6554 chain = DECL_CHAIN (chain);
6557 chain = CLASS_NST_METHODS (impent->imp_context);
6560 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain)));
6561 chain = DECL_CHAIN (chain);
6567 generate_dispatch_tables (void)
6569 tree initlist, chain, method_list_template;
6572 if (!objc_method_template)
6573 objc_method_template = build_method_template ();
6575 chain = CLASS_CLS_METHODS (objc_implementation_context);
6578 size = list_length (chain);
6580 method_list_template
6581 = build_method_list_template (objc_method_template, size);
6583 = build_dispatch_table_initializer (objc_method_template, chain);
6585 UOBJC_CLASS_METHODS_decl
6586 = generate_dispatch_table (method_list_template,
6587 ((TREE_CODE (objc_implementation_context)
6588 == CLASS_IMPLEMENTATION_TYPE)
6589 ? "_OBJC_CLASS_METHODS"
6590 : "_OBJC_CATEGORY_CLASS_METHODS"),
6594 UOBJC_CLASS_METHODS_decl = 0;
6596 chain = CLASS_NST_METHODS (objc_implementation_context);
6599 size = list_length (chain);
6601 method_list_template
6602 = build_method_list_template (objc_method_template, size);
6604 = build_dispatch_table_initializer (objc_method_template, chain);
6606 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
6607 UOBJC_INSTANCE_METHODS_decl
6608 = generate_dispatch_table (method_list_template,
6609 "_OBJC_INSTANCE_METHODS",
6612 /* We have a category. */
6613 UOBJC_INSTANCE_METHODS_decl
6614 = generate_dispatch_table (method_list_template,
6615 "_OBJC_CATEGORY_INSTANCE_METHODS",
6619 UOBJC_INSTANCE_METHODS_decl = 0;
6623 generate_protocol_list (tree i_or_p)
6625 tree array_type, ptype, refs_decl, lproto, e, plist;
6627 const char *ref_name;
6628 VEC(constructor_elt,gc) *v = NULL;
6630 switch (TREE_CODE (i_or_p))
6632 case CLASS_INTERFACE_TYPE:
6633 case CATEGORY_INTERFACE_TYPE:
6634 plist = CLASS_PROTOCOL_LIST (i_or_p);
6636 case PROTOCOL_INTERFACE_TYPE:
6637 plist = PROTOCOL_LIST (i_or_p);
6644 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
6645 if (TREE_CODE (TREE_VALUE (lproto)) == PROTOCOL_INTERFACE_TYPE
6646 && PROTOCOL_FORWARD_DECL (TREE_VALUE (lproto)))
6649 /* Build initializer. */
6650 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6651 e = build_int_cst (build_pointer_type (objc_protocol_template), size);
6652 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, e);
6654 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
6656 tree pval = TREE_VALUE (lproto);
6658 if (TREE_CODE (pval) == PROTOCOL_INTERFACE_TYPE
6659 && PROTOCOL_FORWARD_DECL (pval))
6661 e = build_unary_op (input_location, ADDR_EXPR,
6662 PROTOCOL_FORWARD_DECL (pval), 0);
6663 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, e);
6667 /* static struct objc_protocol *refs[n]; */
6669 switch (TREE_CODE (i_or_p))
6671 case PROTOCOL_INTERFACE_TYPE:
6672 ref_name = synth_id_with_class_suffix ("_OBJC_PROTOCOL_REFS", i_or_p);
6674 case CLASS_INTERFACE_TYPE:
6675 ref_name = synth_id_with_class_suffix ("_OBJC_CLASS_PROTOCOLS", i_or_p);
6677 case CATEGORY_INTERFACE_TYPE:
6678 ref_name = synth_id_with_class_suffix ("_OBJC_CATEGORY_PROTOCOLS", i_or_p);
6684 ptype = build_pointer_type (objc_protocol_template);
6685 array_type = build_sized_array_type (ptype, size + 3);
6686 refs_decl = start_var_decl (array_type, ref_name);
6688 finish_var_decl (refs_decl,
6689 objc_build_constructor (TREE_TYPE (refs_decl), v));
6695 build_category_initializer (tree type, tree cat_name, tree class_name,
6696 tree instance_methods, tree class_methods,
6700 VEC(constructor_elt,gc) *v = NULL;
6702 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, cat_name);
6703 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, class_name);
6705 if (!instance_methods)
6706 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6709 expr = convert (objc_method_list_ptr,
6710 build_unary_op (input_location, ADDR_EXPR,
6711 instance_methods, 0));
6712 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6715 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6718 expr = convert (objc_method_list_ptr,
6719 build_unary_op (input_location, ADDR_EXPR,
6721 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6724 /* protocol_list = */
6726 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6729 expr = convert (build_pointer_type
6731 (objc_protocol_template)),
6732 build_unary_op (input_location, ADDR_EXPR,
6734 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6737 return objc_build_constructor (type, v);
6740 /* struct _objc_class {
6741 struct objc_class *isa;
6742 struct objc_class *super_class;
6747 struct objc_ivar_list *ivars;
6748 struct objc_method_list *methods;
6749 if (flag_next_runtime)
6750 struct objc_cache *cache;
6752 struct sarray *dtable;
6753 struct objc_class *subclass_list;
6754 struct objc_class *sibling_class;
6756 struct objc_protocol_list *protocols;
6757 if (flag_next_runtime)
6759 void *gc_object_type;
6763 build_shared_structure_initializer (tree type, tree isa, tree super,
6764 tree name, tree size, int status,
6765 tree dispatch_table, tree ivar_list,
6769 VEC(constructor_elt,gc) *v = NULL;
6772 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, isa);
6775 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, super);
6778 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, default_conversion (name));
6781 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6782 build_int_cst (long_integer_type_node, 0));
6785 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6786 build_int_cst (long_integer_type_node, status));
6788 /* instance_size = */
6789 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6790 convert (long_integer_type_node, size));
6792 /* objc_ivar_list = */
6794 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6797 expr = convert (objc_ivar_list_ptr,
6798 build_unary_op (input_location, ADDR_EXPR,
6800 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6803 /* objc_method_list = */
6804 if (!dispatch_table)
6805 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6808 expr = convert (objc_method_list_ptr,
6809 build_unary_op (input_location, ADDR_EXPR,
6810 dispatch_table, 0));
6811 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6814 if (flag_next_runtime)
6815 /* method_cache = */
6816 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6820 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6822 /* subclass_list = */
6823 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6825 /* sibling_class = */
6826 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6829 /* protocol_list = */
6830 if (! protocol_list)
6831 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6834 expr = convert (build_pointer_type
6836 (objc_protocol_template)),
6837 build_unary_op (input_location, ADDR_EXPR,
6839 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6842 if (flag_next_runtime)
6844 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6846 /* gc_object_type = NULL */
6847 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6849 return objc_build_constructor (type, v);
6852 /* Retrieve category interface CAT_NAME (if any) associated with CLASS. */
6855 lookup_category (tree klass, tree cat_name)
6857 tree category = CLASS_CATEGORY_LIST (klass);
6859 while (category && CLASS_SUPER_NAME (category) != cat_name)
6860 category = CLASS_CATEGORY_LIST (category);
6864 /* static struct objc_category _OBJC_CATEGORY_<name> = { ... }; */
6867 generate_category (struct imp_entry *impent)
6869 tree initlist, cat_name_expr, class_name_expr;
6870 tree protocol_decl, category;
6871 tree cat = impent->imp_context;
6873 implementation_template = impent->imp_template;
6874 UOBJC_CLASS_decl = impent->class_decl;
6875 UOBJC_METACLASS_decl = impent->meta_decl;
6877 add_class_reference (CLASS_NAME (cat));
6878 cat_name_expr = add_objc_string (CLASS_SUPER_NAME (cat), class_names);
6880 class_name_expr = add_objc_string (CLASS_NAME (cat), class_names);
6882 category = lookup_category (implementation_template,
6883 CLASS_SUPER_NAME (cat));
6885 if (category && CLASS_PROTOCOL_LIST (category))
6887 generate_protocol_references (CLASS_PROTOCOL_LIST (category));
6888 protocol_decl = generate_protocol_list (category);
6893 initlist = build_category_initializer (TREE_TYPE (UOBJC_CLASS_decl),
6894 cat_name_expr, class_name_expr,
6895 UOBJC_INSTANCE_METHODS_decl,
6896 UOBJC_CLASS_METHODS_decl,
6898 /* Finish and initialize the forward decl. */
6899 finish_var_decl (UOBJC_CLASS_decl, initlist);
6902 /* static struct objc_class _OBJC_METACLASS_Foo={ ... };
6903 static struct objc_class _OBJC_CLASS_Foo={ ... }; */
6906 generate_shared_structures (struct imp_entry *impent)
6908 tree name_expr, super_expr, root_expr;
6909 tree my_root_id, my_super_id;
6910 tree cast_type, initlist, protocol_decl;
6913 objc_implementation_context = impent->imp_context;
6914 implementation_template = impent->imp_template;
6915 UOBJC_CLASS_decl = impent->class_decl;
6916 UOBJC_METACLASS_decl = impent->meta_decl;
6917 cls_flags = impent->has_cxx_cdtors ? CLS_HAS_CXX_STRUCTORS : 0 ;
6919 my_super_id = CLASS_SUPER_NAME (implementation_template);
6922 add_class_reference (my_super_id);
6924 /* Compute "my_root_id" - this is required for code generation.
6925 the "isa" for all meta class structures points to the root of
6926 the inheritance hierarchy (e.g. "__Object")... */
6927 my_root_id = my_super_id;
6930 tree my_root_int = lookup_interface (my_root_id);
6932 if (my_root_int && CLASS_SUPER_NAME (my_root_int))
6933 my_root_id = CLASS_SUPER_NAME (my_root_int);
6940 /* No super class. */
6941 my_root_id = CLASS_NAME (implementation_template);
6943 cast_type = build_pointer_type (objc_class_template);
6944 name_expr = add_objc_string (CLASS_NAME (implementation_template),
6947 /* Install class `isa' and `super' pointers at runtime. */
6949 super_expr = add_objc_string (my_super_id, class_names);
6951 super_expr = integer_zero_node;
6953 super_expr = build_c_cast (input_location,
6954 cast_type, super_expr); /* cast! */
6956 root_expr = add_objc_string (my_root_id, class_names);
6957 root_expr = build_c_cast (input_location, cast_type, root_expr); /* cast! */
6959 if (CLASS_PROTOCOL_LIST (implementation_template))
6961 generate_protocol_references
6962 (CLASS_PROTOCOL_LIST (implementation_template));
6963 protocol_decl = generate_protocol_list (implementation_template);
6968 /* static struct objc_class _OBJC_METACLASS_Foo = { ... }; */
6971 = build_shared_structure_initializer
6972 (TREE_TYPE (UOBJC_METACLASS_decl),
6973 root_expr, super_expr, name_expr,
6974 convert (integer_type_node, TYPE_SIZE_UNIT (objc_class_template)),
6976 UOBJC_CLASS_METHODS_decl,
6977 UOBJC_CLASS_VARIABLES_decl,
6980 finish_var_decl (UOBJC_METACLASS_decl, initlist);
6982 /* static struct objc_class _OBJC_CLASS_Foo={ ... }; */
6985 = build_shared_structure_initializer
6986 (TREE_TYPE (UOBJC_CLASS_decl),
6987 build_unary_op (input_location, ADDR_EXPR, UOBJC_METACLASS_decl, 0),
6988 super_expr, name_expr,
6989 convert (integer_type_node,
6990 TYPE_SIZE_UNIT (CLASS_STATIC_TEMPLATE
6991 (implementation_template))),
6992 1 /*CLS_FACTORY*/ | cls_flags,
6993 UOBJC_INSTANCE_METHODS_decl,
6994 UOBJC_INSTANCE_VARIABLES_decl,
6997 finish_var_decl (UOBJC_CLASS_decl, initlist);
7002 synth_id_with_class_suffix (const char *preamble, tree ctxt)
7004 static char string[BUFSIZE];
7006 switch (TREE_CODE (ctxt))
7008 case CLASS_IMPLEMENTATION_TYPE:
7009 case CLASS_INTERFACE_TYPE:
7010 sprintf (string, "%s_%s", preamble,
7011 IDENTIFIER_POINTER (CLASS_NAME (ctxt)));
7013 case CATEGORY_IMPLEMENTATION_TYPE:
7014 case CATEGORY_INTERFACE_TYPE:
7016 /* We have a category. */
7017 const char *const class_name
7018 = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
7019 const char *const class_super_name
7020 = IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context));
7021 sprintf (string, "%s_%s_%s", preamble, class_name, class_super_name);
7024 case PROTOCOL_INTERFACE_TYPE:
7026 const char *protocol_name = IDENTIFIER_POINTER (PROTOCOL_NAME (ctxt));
7027 sprintf (string, "%s_%s", preamble, protocol_name);
7037 /* If type is empty or only type qualifiers are present, add default
7038 type of id (otherwise grokdeclarator will default to int). */
7040 adjust_type_for_id_default (tree type)
7043 type = make_node (TREE_LIST);
7045 if (!TREE_VALUE (type))
7046 TREE_VALUE (type) = objc_object_type;
7047 else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE
7048 && TYPED_OBJECT (TREE_VALUE (type)))
7049 error ("can not use an object as parameter to a method");
7054 /* Return a KEYWORD_DECL built using the specified key_name, arg_type,
7055 arg_name and attributes. (TODO: Rename KEYWORD_DECL to
7056 OBJC_METHOD_PARM_DECL ?)
7058 A KEYWORD_DECL is a tree representing the declaration of a
7059 parameter of an Objective-C method. It is produced when parsing a
7060 fragment of Objective-C method declaration of the form
7063 selector ':' '(' typename ')' identifier
7065 For example, take the Objective-C method
7067 -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type;
7069 the two fragments "pathForResource:(NSString *)resource" and
7070 "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The
7071 KEYWORD_DECL stores the 'key_name' (eg, identifier for
7072 "pathForResource"), the 'arg_type' (eg, tree representing a
7073 NSString *), the 'arg_name' (eg identifier for "resource") and
7074 potentially some attributes (for example, a tree representing
7075 __attribute__ ((unused)) if such an attribute was attached to a
7076 certain parameter). You can access this information using the
7077 TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name),
7078 KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes).
7080 'key_name' is an identifier node (and is optional as you can omit
7081 it in Objective-C methods).
7082 'arg_type' is a tree list (and is optional too if no parameter type
7084 'arg_name' is an identifier node and is required.
7085 'attributes' is an optional tree containing parameter attributes. */
7087 objc_build_keyword_decl (tree key_name, tree arg_type,
7088 tree arg_name, tree attributes)
7092 if (flag_objc1_only && attributes)
7093 error_at (input_location, "method argument attributes are not available in Objective-C 1.0");
7095 /* If no type is specified, default to "id". */
7096 arg_type = adjust_type_for_id_default (arg_type);
7098 keyword_decl = make_node (KEYWORD_DECL);
7100 TREE_TYPE (keyword_decl) = arg_type;
7101 KEYWORD_ARG_NAME (keyword_decl) = arg_name;
7102 KEYWORD_KEY_NAME (keyword_decl) = key_name;
7103 DECL_ATTRIBUTES (keyword_decl) = attributes;
7105 return keyword_decl;
7108 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
7110 build_keyword_selector (tree selector)
7113 tree key_chain, key_name;
7116 /* Scan the selector to see how much space we'll need. */
7117 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
7119 switch (TREE_CODE (selector))
7122 key_name = KEYWORD_KEY_NAME (key_chain);
7125 key_name = TREE_PURPOSE (key_chain);
7132 len += IDENTIFIER_LENGTH (key_name) + 1;
7134 /* Just a ':' arg. */
7138 buf = (char *) alloca (len + 1);
7139 /* Start the buffer out as an empty string. */
7142 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
7144 switch (TREE_CODE (selector))
7147 key_name = KEYWORD_KEY_NAME (key_chain);
7150 key_name = TREE_PURPOSE (key_chain);
7151 /* The keyword decl chain will later be used as a function
7152 argument chain. Unhook the selector itself so as to not
7153 confuse other parts of the compiler. */
7154 TREE_PURPOSE (key_chain) = NULL_TREE;
7161 strcat (buf, IDENTIFIER_POINTER (key_name));
7165 return get_identifier (buf);
7168 /* Used for declarations and definitions. */
7171 build_method_decl (enum tree_code code, tree ret_type, tree selector,
7172 tree add_args, bool ellipsis)
7176 /* If no type is specified, default to "id". */
7177 ret_type = adjust_type_for_id_default (ret_type);
7179 /* Note how a method_decl has a TREE_TYPE which is not the function
7180 type of the function implementing the method, but only the return
7181 type of the method. We may want to change this, and store the
7182 entire function type in there (eg, it may be used to simplify
7183 dealing with attributes below). */
7184 method_decl = make_node (code);
7185 TREE_TYPE (method_decl) = ret_type;
7187 /* If we have a keyword selector, create an identifier_node that
7188 represents the full selector name (`:' included)... */
7189 if (TREE_CODE (selector) == KEYWORD_DECL)
7191 METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
7192 METHOD_SEL_ARGS (method_decl) = selector;
7193 METHOD_ADD_ARGS (method_decl) = add_args;
7194 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis;
7198 METHOD_SEL_NAME (method_decl) = selector;
7199 METHOD_SEL_ARGS (method_decl) = NULL_TREE;
7200 METHOD_ADD_ARGS (method_decl) = NULL_TREE;
7206 #define METHOD_DEF 0
7207 #define METHOD_REF 1
7209 /* This routine processes objective-c method attributes. */
7212 objc_decl_method_attributes (tree *node, tree attributes, int flags)
7214 /* TODO: Replace the hackery below. An idea would be to store the
7215 full function type in the method declaration (for example in
7216 TREE_TYPE) and then expose ObjC method declarations to c-family
7217 and they could deal with them by simply treating them as
7220 /* Because of the dangers in the hackery below, we filter out any
7221 attribute that we do not know about. For the ones we know about,
7222 we know that they work with the hackery. For the other ones,
7223 there is no guarantee, so we have to filter them out. */
7224 tree filtered_attributes = NULL_TREE;
7229 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
7231 tree name = TREE_PURPOSE (attribute);
7233 if (is_attribute_p ("deprecated", name)
7234 || is_attribute_p ("sentinel", name)
7235 || is_attribute_p ("noreturn", name))
7237 /* An attribute that we support; add it to the filtered
7239 filtered_attributes = chainon (filtered_attributes,
7240 copy_node (attribute));
7242 else if (is_attribute_p ("format", name))
7244 /* "format" is special because before adding it to the
7245 filtered attributes we need to adjust the specified
7246 format by adding the hidden function parameters for
7247 an Objective-C method (self, _cmd). */
7248 tree new_attribute = copy_node (attribute);
7250 /* Check the arguments specified with the attribute, and
7251 modify them adding 2 for the two hidden arguments.
7252 Note how this differs from C++; according to the
7253 specs, C++ does not do it so you have to add the +1
7254 yourself. For Objective-C, instead, the compiler
7255 adds the +2 for you. */
7257 /* The attribute arguments have not been checked yet, so
7258 we need to be careful as they could be missing or
7259 invalid. If anything looks wrong, we skip the
7260 process and the compiler will complain about it later
7261 when it validates the attribute. */
7262 /* Check that we have at least three arguments. */
7263 if (TREE_VALUE (new_attribute)
7264 && TREE_CHAIN (TREE_VALUE (new_attribute))
7265 && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (new_attribute))))
7267 tree second_argument = TREE_CHAIN (TREE_VALUE (new_attribute));
7268 tree third_argument = TREE_CHAIN (second_argument);
7271 /* This is the second argument, the "string-index",
7272 which specifies the index of the format string
7274 number = TREE_VALUE (second_argument);
7276 && TREE_CODE (number) == INTEGER_CST
7277 && TREE_INT_CST_HIGH (number) == 0)
7279 TREE_VALUE (second_argument)
7280 = build_int_cst (integer_type_node,
7281 TREE_INT_CST_LOW (number) + 2);
7284 /* This is the third argument, the "first-to-check",
7285 which specifies the index of the first argument to
7286 check. This could be 0, meaning it is not available,
7287 in which case we don't need to add 2. Add 2 if not
7289 number = TREE_VALUE (third_argument);
7291 && TREE_CODE (number) == INTEGER_CST
7292 && TREE_INT_CST_HIGH (number) == 0
7293 && TREE_INT_CST_LOW (number) != 0)
7295 TREE_VALUE (third_argument)
7296 = build_int_cst (integer_type_node,
7297 TREE_INT_CST_LOW (number) + 2);
7300 filtered_attributes = chainon (filtered_attributes,
7304 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
7308 if (filtered_attributes)
7310 /* This hackery changes the TREE_TYPE of the ObjC method
7311 declaration to be a function type, so that decl_attributes
7312 will treat the ObjC method as if it was a function. Some
7313 attributes (sentinel, format) will be applied to the function
7314 type, changing it in place; so after calling decl_attributes,
7315 we extract the function type attributes and store them in
7316 METHOD_TYPE_ATTRIBUTES. Some other attributes (noreturn,
7317 deprecated) are applied directly to the method declaration
7318 (by setting TREE_DEPRECATED and TREE_THIS_VOLATILE) so there
7319 is nothing to do. */
7320 tree saved_type = TREE_TYPE (*node);
7321 TREE_TYPE (*node) = build_function_type
7322 (TREE_VALUE (saved_type), get_arg_type_list (*node, METHOD_REF, 0));
7323 decl_attributes (node, filtered_attributes, flags);
7324 METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node));
7325 TREE_TYPE (*node) = saved_type;
7330 objc_method_decl (enum tree_code opcode)
7332 return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL;
7335 /* Used by `build_objc_method_call' and `comp_proto_with_proto'. Return
7336 an argument list for method METH. CONTEXT is either METHOD_DEF or
7337 METHOD_REF, saying whether we are trying to define a method or call
7338 one. SUPERFLAG says this is for a send to super; this makes a
7339 difference for the NeXT calling sequence in which the lookup and
7340 the method call are done together. If METH is null, user-defined
7341 arguments (i.e., beyond self and _cmd) shall be represented by `...'. */
7344 get_arg_type_list (tree meth, int context, int superflag)
7348 /* Receiver type. */
7349 if (flag_next_runtime && superflag)
7350 arglist = build_tree_list (NULL_TREE, objc_super_type);
7351 else if (context == METHOD_DEF && TREE_CODE (meth) == INSTANCE_METHOD_DECL)
7352 arglist = build_tree_list (NULL_TREE, objc_instance_type);
7354 arglist = build_tree_list (NULL_TREE, objc_object_type);
7356 /* Selector type - will eventually change to `int'. */
7357 chainon (arglist, build_tree_list (NULL_TREE, objc_selector_type));
7359 /* No actual method prototype given -- assume that remaining arguments
7364 /* Build a list of argument types. */
7365 for (akey = METHOD_SEL_ARGS (meth); akey; akey = DECL_CHAIN (akey))
7367 tree arg_type = TREE_VALUE (TREE_TYPE (akey));
7369 /* Decay argument types for the underlying C function as appropriate. */
7370 arg_type = objc_decay_parm_type (arg_type);
7372 chainon (arglist, build_tree_list (NULL_TREE, arg_type));
7375 if (METHOD_ADD_ARGS (meth))
7377 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (meth));
7378 akey; akey = TREE_CHAIN (akey))
7380 tree arg_type = TREE_TYPE (TREE_VALUE (akey));
7382 arg_type = objc_decay_parm_type (arg_type);
7384 chainon (arglist, build_tree_list (NULL_TREE, arg_type));
7387 if (!METHOD_ADD_ARGS_ELLIPSIS_P (meth))
7388 goto lack_of_ellipsis;
7393 chainon (arglist, OBJC_VOID_AT_END);
7400 check_duplicates (hash hsh, int methods, int is_class)
7402 tree meth = NULL_TREE;
7410 /* We have two or more methods with the same name but
7414 /* But just how different are those types? If
7415 -Wno-strict-selector-match is specified, we shall not
7416 complain if the differences are solely among types with
7417 identical size and alignment. */
7418 if (!warn_strict_selector_match)
7420 for (loop = hsh->list; loop; loop = loop->next)
7421 if (!comp_proto_with_proto (meth, loop->value, 0))
7430 bool type = TREE_CODE (meth) == INSTANCE_METHOD_DECL;
7432 warning_at (input_location, 0,
7433 "multiple methods named %<%c%E%> found",
7434 (is_class ? '+' : '-'),
7435 METHOD_SEL_NAME (meth));
7436 inform (DECL_SOURCE_LOCATION (meth), "using %<%c%s%>",
7438 identifier_to_locale (gen_method_decl (meth)));
7442 bool type = TREE_CODE (meth) == INSTANCE_METHOD_DECL;
7444 warning_at (input_location, 0,
7445 "multiple selectors named %<%c%E%> found",
7446 (is_class ? '+' : '-'),
7447 METHOD_SEL_NAME (meth));
7448 inform (DECL_SOURCE_LOCATION (meth), "found %<%c%s%>",
7450 identifier_to_locale (gen_method_decl (meth)));
7453 for (loop = hsh->list; loop; loop = loop->next)
7455 bool type = TREE_CODE (loop->value) == INSTANCE_METHOD_DECL;
7457 inform (DECL_SOURCE_LOCATION (loop->value), "also found %<%c%s%>",
7459 identifier_to_locale (gen_method_decl (loop->value)));
7466 /* If RECEIVER is a class reference, return the identifier node for
7467 the referenced class. RECEIVER is created by objc_get_class_reference,
7468 so we check the exact form created depending on which runtimes are
7472 receiver_is_class_object (tree receiver, int self, int super)
7474 tree chain, exp, arg;
7476 /* The receiver is 'self' or 'super' in the context of a class method. */
7477 if (objc_method_context
7478 && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
7481 ? CLASS_SUPER_NAME (implementation_template)
7482 : CLASS_NAME (implementation_template));
7484 if (flag_next_runtime)
7486 /* The receiver is a variable created by
7487 build_class_reference_decl. */
7488 if (TREE_CODE (receiver) == VAR_DECL && IS_CLASS (TREE_TYPE (receiver)))
7489 /* Look up the identifier. */
7490 for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
7491 if (TREE_PURPOSE (chain) == receiver)
7492 return TREE_VALUE (chain);
7495 /* The receiver is a function call that returns an id. Check if
7496 it is a call to objc_getClass, if so, pick up the class name. */
7497 if (TREE_CODE (receiver) == CALL_EXPR
7498 && (exp = CALL_EXPR_FN (receiver))
7499 && TREE_CODE (exp) == ADDR_EXPR
7500 && (exp = TREE_OPERAND (exp, 0))
7501 && TREE_CODE (exp) == FUNCTION_DECL
7502 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
7503 prototypes for objc_get_class(). Thankfully, they seem to share the
7504 same function type. */
7505 && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl)
7506 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), TAG_GETCLASS)
7507 /* We have a call to objc_get_class/objc_getClass! */
7508 && (arg = CALL_EXPR_ARG (receiver, 0)))
7511 if (TREE_CODE (arg) == ADDR_EXPR
7512 && (arg = TREE_OPERAND (arg, 0))
7513 && TREE_CODE (arg) == STRING_CST)
7514 /* Finally, we have the class name. */
7515 return get_identifier (TREE_STRING_POINTER (arg));
7520 /* If we are currently building a message expr, this holds
7521 the identifier of the selector of the message. This is
7522 used when printing warnings about argument mismatches. */
7524 static tree current_objc_message_selector = 0;
7527 objc_message_selector (void)
7529 return current_objc_message_selector;
7532 /* Construct an expression for sending a message.
7533 MESS has the object to send to in TREE_PURPOSE
7534 and the argument list (including selector) in TREE_VALUE.
7536 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
7537 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
7540 objc_build_message_expr (tree mess)
7542 tree receiver = TREE_PURPOSE (mess);
7545 tree args = TREE_PURPOSE (TREE_VALUE (mess));
7547 tree args = TREE_VALUE (mess);
7549 tree method_params = NULL_TREE;
7551 if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK)
7552 return error_mark_node;
7554 /* Obtain the full selector name. */
7555 switch (TREE_CODE (args))
7557 case IDENTIFIER_NODE:
7558 /* A unary selector. */
7562 sel_name = build_keyword_selector (args);
7568 /* Build the parameter list to give to the method. */
7569 if (TREE_CODE (args) == TREE_LIST)
7571 method_params = chainon (args, TREE_VALUE (TREE_VALUE (mess)));
7574 tree chain = args, prev = NULL_TREE;
7576 /* We have a keyword selector--check for comma expressions. */
7579 tree element = TREE_VALUE (chain);
7581 /* We have a comma expression, must collapse... */
7582 if (TREE_CODE (element) == TREE_LIST)
7585 TREE_CHAIN (prev) = element;
7590 chain = TREE_CHAIN (chain);
7592 method_params = args;
7597 if (processing_template_decl)
7598 /* Must wait until template instantiation time. */
7599 return build_min_nt (MESSAGE_SEND_EXPR, receiver, sel_name,
7603 return objc_finish_message_expr (receiver, sel_name, method_params);
7606 /* Look up method SEL_NAME that would be suitable for receiver
7607 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
7608 nonzero), and report on any duplicates. */
7611 lookup_method_in_hash_lists (tree sel_name, int is_class)
7613 hash method_prototype = NULL;
7616 method_prototype = hash_lookup (nst_method_hash_list,
7619 if (!method_prototype)
7621 method_prototype = hash_lookup (cls_method_hash_list,
7626 return check_duplicates (method_prototype, 1, is_class);
7629 /* The 'objc_finish_message_expr' routine is called from within
7630 'objc_build_message_expr' for non-template functions. In the case of
7631 C++ template functions, it is called from 'build_expr_from_tree'
7632 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded. */
7635 objc_finish_message_expr (tree receiver, tree sel_name, tree method_params)
7637 tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype;
7638 tree selector, retval, class_tree;
7639 int self, super, have_cast;
7641 /* We have used the receiver, so mark it as read. */
7642 mark_exp_read (receiver);
7644 /* Extract the receiver of the message, as well as its type
7645 (where the latter may take the form of a cast or be inferred
7646 from the implementation context). */
7648 while (TREE_CODE (rtype) == COMPOUND_EXPR
7649 || TREE_CODE (rtype) == MODIFY_EXPR
7650 || CONVERT_EXPR_P (rtype)
7651 || TREE_CODE (rtype) == COMPONENT_REF)
7652 rtype = TREE_OPERAND (rtype, 0);
7654 self = (rtype == self_decl);
7655 super = (rtype == UOBJC_SUPER_decl);
7656 rtype = TREE_TYPE (receiver);
7658 have_cast = (TREE_CODE (receiver) == NOP_EXPR
7659 || (TREE_CODE (receiver) == COMPOUND_EXPR
7660 && !IS_SUPER (rtype)));
7662 /* If we are calling [super dealloc], reset our warning flag. */
7663 if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name)))
7664 should_call_super_dealloc = 0;
7666 /* If the receiver is a class object, retrieve the corresponding
7667 @interface, if one exists. */
7668 class_tree = receiver_is_class_object (receiver, self, super);
7670 /* Now determine the receiver type (if an explicit cast has not been
7675 rtype = lookup_interface (class_tree);
7676 /* Handle `self' and `super'. */
7679 if (!CLASS_SUPER_NAME (implementation_template))
7681 error ("no super class declared in @interface for %qE",
7682 CLASS_NAME (implementation_template));
7683 return error_mark_node;
7685 rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template));
7688 rtype = lookup_interface (CLASS_NAME (implementation_template));
7691 /* If receiver is of type `id' or `Class' (or if the @interface for a
7692 class is not visible), we shall be satisfied with the existence of
7693 any instance or class method. */
7694 if (objc_is_id (rtype))
7696 class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE);
7697 rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
7698 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
7704 /* If messaging 'id <Protos>' or 'Class <Proto>', first search
7705 in protocols themselves for the method prototype. */
7707 = lookup_method_in_protocol_list (rprotos, sel_name,
7708 class_tree != NULL_TREE);
7710 /* If messaging 'Class <Proto>' but did not find a class method
7711 prototype, search for an instance method instead, and warn
7712 about having done so. */
7713 if (!method_prototype && !rtype && class_tree != NULL_TREE)
7716 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
7718 if (method_prototype)
7719 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
7720 sel_name, sel_name);
7726 tree orig_rtype = rtype;
7728 if (TREE_CODE (rtype) == POINTER_TYPE)
7729 rtype = TREE_TYPE (rtype);
7730 /* Traverse typedef aliases */
7731 while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype)
7732 && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL
7733 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)))
7734 rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype));
7735 if (TYPED_OBJECT (rtype))
7737 rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype);
7738 rtype = TYPE_OBJC_INTERFACE (rtype);
7740 /* If we could not find an @interface declaration, we must have
7741 only seen a @class declaration; so, we cannot say anything
7742 more intelligent about which methods the receiver will
7744 if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE)
7747 /* We could not find an @interface declaration, yet Message maybe in a
7748 @class's protocol. */
7749 if (!method_prototype && rprotos)
7751 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
7753 else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE
7754 || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE)
7756 /* We have a valid ObjC class name. Look up the method name
7757 in the published @interface for the class (and its
7760 = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE);
7762 /* If the method was not found in the @interface, it may still
7763 exist locally as part of the @implementation. */
7764 if (!method_prototype && objc_implementation_context
7765 && CLASS_NAME (objc_implementation_context)
7766 == OBJC_TYPE_NAME (rtype))
7770 ? CLASS_CLS_METHODS (objc_implementation_context)
7771 : CLASS_NST_METHODS (objc_implementation_context)),
7774 /* If we haven't found a candidate method by now, try looking for
7775 it in the protocol list. */
7776 if (!method_prototype && rprotos)
7778 = lookup_method_in_protocol_list (rprotos, sel_name,
7779 class_tree != NULL_TREE);
7783 warning (0, "invalid receiver type %qs",
7784 identifier_to_locale (gen_type_name (orig_rtype)));
7785 /* After issuing the "invalid receiver" warning, perform method
7786 lookup as if we were messaging 'id'. */
7787 rtype = rprotos = NULL_TREE;
7792 /* For 'id' or 'Class' receivers, search in the global hash table
7793 as a last resort. For all receivers, warn if protocol searches
7795 if (!method_prototype)
7798 warning (0, "%<%c%E%> not found in protocol(s)",
7799 (class_tree ? '+' : '-'),
7804 = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE);
7807 if (!method_prototype)
7809 static bool warn_missing_methods = false;
7812 warning (0, "%qE may not respond to %<%c%E%>",
7813 OBJC_TYPE_NAME (rtype),
7814 (class_tree ? '+' : '-'),
7816 /* If we are messaging an 'id' or 'Class' object and made it here,
7817 then we have failed to find _any_ instance or class method,
7820 warning (0, "no %<%c%E%> method found",
7821 (class_tree ? '+' : '-'),
7824 if (!warn_missing_methods)
7826 warning_at (input_location,
7827 0, "(Messages without a matching method signature");
7828 warning_at (input_location,
7829 0, "will be assumed to return %<id%> and accept");
7830 warning_at (input_location,
7831 0, "%<...%> as arguments.)");
7832 warn_missing_methods = true;
7837 /* Warn if the method is deprecated, but not if the receiver is
7838 a generic 'id'. 'id' is used to cast an object to a generic
7839 object of an unspecified class; in that case, we'll use
7840 whatever method prototype we can find to get the method
7841 argument and return types, but it is not appropriate to
7842 produce deprecation warnings since we don't know the class
7843 that the object will be of at runtime. The @interface(s) for
7844 that class may not even be available to the compiler right
7845 now, and it is perfectly possible that the method is marked
7846 as non-deprecated in such @interface(s).
7848 In practice this makes sense since casting an object to 'id'
7849 is often used precisely to turn off warnings associated with
7850 the object being of a particular class. */
7851 if (TREE_DEPRECATED (method_prototype) && rtype != NULL_TREE)
7852 warn_deprecated_use (method_prototype, NULL_TREE);
7856 /* Save the selector name for printing error messages. */
7857 current_objc_message_selector = sel_name;
7859 /* Build the parameters list for looking up the method.
7860 These are the object itself and the selector. */
7862 if (flag_typed_selectors)
7863 selector = build_typed_selector_reference (input_location,
7864 sel_name, method_prototype);
7866 selector = build_selector_reference (input_location, sel_name);
7868 retval = build_objc_method_call (input_location, super, method_prototype,
7870 selector, method_params);
7872 current_objc_message_selector = 0;
7877 /* Build a tree expression to send OBJECT the operation SELECTOR,
7878 looking up the method on object LOOKUP_OBJECT (often same as OBJECT),
7879 assuming the method has prototype METHOD_PROTOTYPE.
7880 (That is an INSTANCE_METHOD_DECL or CLASS_METHOD_DECL.)
7881 LOC is the location of the expression to build.
7882 Use METHOD_PARAMS as list of args to pass to the method.
7883 If SUPER_FLAG is nonzero, we look up the superclass's method. */
7886 build_objc_method_call (location_t loc, int super_flag, tree method_prototype,
7887 tree lookup_object, tree selector,
7890 tree sender = (super_flag ? umsg_super_decl :
7891 (!flag_next_runtime || flag_nil_receivers
7892 ? (flag_objc_direct_dispatch
7895 : umsg_nonnil_decl));
7896 tree rcv_p = (super_flag ? objc_super_type : objc_object_type);
7897 VEC(tree, gc) *parms = NULL;
7898 unsigned nparm = (method_params ? list_length (method_params) : 0);
7900 /* If a prototype for the method to be called exists, then cast
7901 the sender's return type and arguments to match that of the method.
7902 Otherwise, leave sender as is. */
7905 ? TREE_VALUE (TREE_TYPE (method_prototype))
7906 : objc_object_type);
7908 tree method_param_types =
7909 get_arg_type_list (method_prototype, METHOD_REF, super_flag);
7910 tree ftype = build_function_type (ret_type, method_param_types);
7914 if (method_prototype && METHOD_TYPE_ATTRIBUTES (method_prototype))
7915 ftype = build_type_attribute_variant (ftype,
7916 METHOD_TYPE_ATTRIBUTES
7917 (method_prototype));
7919 sender_cast = build_pointer_type (ftype);
7921 lookup_object = build_c_cast (loc, rcv_p, lookup_object);
7923 /* Use SAVE_EXPR to avoid evaluating the receiver twice. */
7924 lookup_object = save_expr (lookup_object);
7926 /* Param list + 2 slots for object and selector. */
7927 parms = VEC_alloc (tree, gc, nparm + 2);
7929 if (flag_next_runtime)
7931 /* If we are returning a struct in memory, and the address
7932 of that memory location is passed as a hidden first
7933 argument, then change which messenger entry point this
7934 expr will call. NB: Note that sender_cast remains
7935 unchanged (it already has a struct return type). */
7936 if (!targetm.calls.struct_value_rtx (0, 0)
7937 && (TREE_CODE (ret_type) == RECORD_TYPE
7938 || TREE_CODE (ret_type) == UNION_TYPE)
7939 && targetm.calls.return_in_memory (ret_type, 0))
7940 sender = (super_flag ? umsg_super_stret_decl :
7941 flag_nil_receivers ? umsg_stret_decl : umsg_nonnil_stret_decl);
7943 method = build_fold_addr_expr_loc (input_location, sender);
7944 /* Pass the object to the method. */
7945 VEC_quick_push (tree, parms, lookup_object);
7949 /* This is the portable (GNU) way. */
7950 /* First, call the lookup function to get a pointer to the method,
7951 then cast the pointer, then call it with the method arguments. */
7952 VEC(tree, gc) *tv = VEC_alloc (tree, gc, 2);
7953 VEC_quick_push (tree, tv, lookup_object);
7954 VEC_quick_push (tree, tv, selector);
7955 method = build_function_call_vec (loc, sender, tv, NULL);
7956 VEC_free (tree, gc, tv);
7958 /* Pass the appropriate object to the method. */
7959 VEC_quick_push (tree, parms, (super_flag ? self_decl : lookup_object));
7962 /* Pass the selector to the method. */
7963 VEC_quick_push (tree, parms, selector);
7964 /* Now append the remainder of the parms. */
7966 for (; method_params; method_params = TREE_CHAIN (method_params))
7967 VEC_quick_push (tree, parms, TREE_VALUE (method_params));
7969 /* Build an obj_type_ref, with the correct cast for the method call. */
7970 t = build3 (OBJ_TYPE_REF, sender_cast, method,
7971 lookup_object, size_zero_node);
7972 t = build_function_call_vec (loc, t, parms, NULL);\
7973 VEC_free (tree, gc, parms);
7978 build_protocol_reference (tree p)
7981 const char *proto_name;
7983 /* static struct _objc_protocol _OBJC_PROTOCOL_<mumble>; */
7985 proto_name = synth_id_with_class_suffix ("_OBJC_PROTOCOL", p);
7986 decl = start_var_decl (objc_protocol_template, proto_name);
7988 PROTOCOL_FORWARD_DECL (p) = decl;
7991 /* This function is called by the parser when (and only when) a
7992 @protocol() expression is found, in order to compile it. */
7994 objc_build_protocol_expr (tree protoname)
7997 tree p = lookup_protocol (protoname);
8001 error ("cannot find protocol declaration for %qE",
8003 return error_mark_node;
8006 if (!PROTOCOL_FORWARD_DECL (p))
8007 build_protocol_reference (p);
8009 expr = build_unary_op (input_location,
8010 ADDR_EXPR, PROTOCOL_FORWARD_DECL (p), 0);
8012 /* ??? Ideally we'd build the reference with objc_protocol_type directly,
8013 if we have it, rather than converting it here. */
8014 expr = convert (objc_protocol_type, expr);
8016 /* The @protocol() expression is being compiled into a pointer to a
8017 statically allocated instance of the Protocol class. To become
8018 usable at runtime, the 'isa' pointer of the instance need to be
8019 fixed up at runtime by the runtime library, to point to the
8020 actual 'Protocol' class. */
8022 /* For the GNU runtime, put the static Protocol instance in the list
8023 of statically allocated instances, so that we make sure that its
8024 'isa' pointer is fixed up at runtime by the GNU runtime library
8025 to point to the Protocol class (at runtime, when loading the
8026 module, the GNU runtime library loops on the statically allocated
8027 instances (as found in the defs field in objc_symtab) and fixups
8028 all the 'isa' pointers of those objects). */
8029 if (! flag_next_runtime)
8031 /* This type is a struct containing the fields of a Protocol
8032 object. (Cfr. objc_protocol_type instead is the type of a pointer
8033 to such a struct). */
8034 tree protocol_struct_type = xref_tag
8035 (RECORD_TYPE, get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
8038 /* Look for the list of Protocol statically allocated instances
8039 to fixup at runtime. Create a new list to hold Protocol
8040 statically allocated instances, if the list is not found. At
8041 present there is only another list, holding NSConstantString
8042 static instances to be fixed up at runtime. */
8043 for (chain = &objc_static_instances;
8044 *chain && TREE_VALUE (*chain) != protocol_struct_type;
8045 chain = &TREE_CHAIN (*chain));
8048 *chain = tree_cons (NULL_TREE, protocol_struct_type, NULL_TREE);
8049 add_objc_string (OBJC_TYPE_NAME (protocol_struct_type),
8053 /* Add this statically allocated instance to the Protocol list. */
8054 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE,
8055 PROTOCOL_FORWARD_DECL (p),
8056 TREE_PURPOSE (*chain));
8063 /* This function is called by the parser when a @selector() expression
8064 is found, in order to compile it. It is only called by the parser
8065 and only to compile a @selector(). LOC is the location of the
8068 objc_build_selector_expr (location_t loc, tree selnamelist)
8072 /* Obtain the full selector name. */
8073 switch (TREE_CODE (selnamelist))
8075 case IDENTIFIER_NODE:
8076 /* A unary selector. */
8077 selname = selnamelist;
8080 selname = build_keyword_selector (selnamelist);
8086 /* If we are required to check @selector() expressions as they
8087 are found, check that the selector has been declared. */
8088 if (warn_undeclared_selector)
8090 /* Look the selector up in the list of all known class and
8091 instance methods (up to this line) to check that the selector
8095 /* First try with instance methods. */
8096 hsh = hash_lookup (nst_method_hash_list, selname);
8098 /* If not found, try with class methods. */
8101 hsh = hash_lookup (cls_method_hash_list, selname);
8104 /* If still not found, print out a warning. */
8107 warning (0, "undeclared selector %qE", selname);
8112 if (flag_typed_selectors)
8113 return build_typed_selector_reference (loc, selname, 0);
8115 return build_selector_reference (loc, selname);
8118 /* This is used to implement @encode(). See gcc/doc/objc.texi,
8119 section '@encode'. */
8121 objc_build_encode_expr (tree type)
8126 encode_type (type, obstack_object_size (&util_obstack),
8127 OBJC_ENCODE_INLINE_DEFS);
8128 obstack_1grow (&util_obstack, 0); /* null terminate string */
8129 string = XOBFINISH (&util_obstack, const char *);
8131 /* Synthesize a string that represents the encoded struct/union. */
8132 result = my_build_string (strlen (string) + 1, string);
8133 obstack_free (&util_obstack, util_firstobj);
8138 build_ivar_reference (tree id)
8140 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
8142 /* Historically, a class method that produced objects (factory
8143 method) would assign `self' to the instance that it
8144 allocated. This would effectively turn the class method into
8145 an instance method. Following this assignment, the instance
8146 variables could be accessed. That practice, while safe,
8147 violates the simple rule that a class method should not refer
8148 to an instance variable. It's better to catch the cases
8149 where this is done unknowingly than to support the above
8151 warning (0, "instance variable %qE accessed in class method",
8153 self_decl = convert (objc_instance_type, self_decl); /* cast */
8156 return objc_build_component_ref (build_indirect_ref (input_location,
8157 self_decl, RO_ARROW),
8161 /* Compute a hash value for a given method SEL_NAME. */
8164 hash_func (tree sel_name)
8166 const unsigned char *s
8167 = (const unsigned char *)IDENTIFIER_POINTER (sel_name);
8171 h = h * 67 + *s++ - 113;
8178 nst_method_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
8179 cls_method_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
8181 cls_name_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
8182 als_name_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
8184 /* Initialize the hash table used to hold the constant string objects. */
8185 string_htab = htab_create_ggc (31, string_hash,
8189 /* This routine adds sel_name to the hash list. sel_name is a class or alias
8190 name for the class. If alias name, then value is its underlying class.
8191 If class, the value is NULL_TREE. */
8194 hash_class_name_enter (hash *hashlist, tree sel_name, tree value)
8197 int slot = hash_func (sel_name) % SIZEHASHTABLE;
8199 obj = ggc_alloc_hashed_entry ();
8200 if (value != NULL_TREE)
8202 /* Save the underlying class for the 'alias' in the hash table */
8203 attr obj_attr = ggc_alloc_hashed_attribute ();
8204 obj_attr->value = value;
8205 obj->list = obj_attr;
8209 obj->next = hashlist[slot];
8210 obj->key = sel_name;
8212 hashlist[slot] = obj; /* append to front */
8217 Searches in the hash table looking for a match for class or alias name.
8221 hash_class_name_lookup (hash *hashlist, tree sel_name)
8225 target = hashlist[hash_func (sel_name) % SIZEHASHTABLE];
8229 if (sel_name == target->key)
8232 target = target->next;
8237 /* WARNING!!!! hash_enter is called with a method, and will peek
8238 inside to find its selector! But hash_lookup is given a selector
8239 directly, and looks for the selector that's inside the found
8240 entry's key (method) for comparison. */
8243 hash_enter (hash *hashlist, tree method)
8246 int slot = hash_func (METHOD_SEL_NAME (method)) % SIZEHASHTABLE;
8248 obj = ggc_alloc_hashed_entry ();
8250 obj->next = hashlist[slot];
8253 hashlist[slot] = obj; /* append to front */
8257 hash_lookup (hash *hashlist, tree sel_name)
8261 target = hashlist[hash_func (sel_name) % SIZEHASHTABLE];
8265 if (sel_name == METHOD_SEL_NAME (target->key))
8268 target = target->next;
8274 hash_add_attr (hash entry, tree value)
8278 obj = ggc_alloc_hashed_attribute ();
8279 obj->next = entry->list;
8282 entry->list = obj; /* append to front */
8286 lookup_method (tree mchain, tree method)
8290 if (TREE_CODE (method) == IDENTIFIER_NODE)
8293 key = METHOD_SEL_NAME (method);
8297 if (METHOD_SEL_NAME (mchain) == key)
8300 mchain = DECL_CHAIN (mchain);
8305 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance
8306 method in INTERFACE, along with any categories and protocols
8307 attached thereto. If method is not found, and the
8308 OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS, recursively examine the
8309 INTERFACE's superclass. If OBJC_LOOKUP_CLASS is set,
8310 OBJC_LOOKUP_NO_SUPER is clear, and no suitable class method could
8311 be found in INTERFACE or any of its superclasses, look for an
8312 _instance_ method of the same name in the root class as a last
8313 resort. This behaviour can be turned off by using
8314 OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS.
8316 If a suitable method cannot be found, return NULL_TREE. */
8319 lookup_method_static (tree interface, tree ident, int flags)
8321 tree meth = NULL_TREE, root_inter = NULL_TREE;
8322 tree inter = interface;
8323 int is_class = (flags & OBJC_LOOKUP_CLASS);
8324 int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER);
8325 int no_instance_methods_of_root_class = (flags & OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS);
8329 tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter);
8330 tree category = inter;
8332 /* First, look up the method in the class itself. */
8333 if ((meth = lookup_method (chain, ident)))
8336 /* Failing that, look for the method in each category of the class. */
8337 while ((category = CLASS_CATEGORY_LIST (category)))
8339 chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category);
8341 /* Check directly in each category. */
8342 if ((meth = lookup_method (chain, ident)))
8345 /* Failing that, check in each category's protocols. */
8346 if (CLASS_PROTOCOL_LIST (category))
8348 if ((meth = (lookup_method_in_protocol_list
8349 (CLASS_PROTOCOL_LIST (category), ident, is_class))))
8354 /* If not found in categories, check in protocols of the main class. */
8355 if (CLASS_PROTOCOL_LIST (inter))
8357 if ((meth = (lookup_method_in_protocol_list
8358 (CLASS_PROTOCOL_LIST (inter), ident, is_class))))
8362 /* If we were instructed not to look in superclasses, don't. */
8363 if (no_superclasses)
8366 /* Failing that, climb up the inheritance hierarchy. */
8368 inter = lookup_interface (CLASS_SUPER_NAME (inter));
8372 if (is_class && !no_instance_methods_of_root_class)
8374 /* If no class (factory) method was found, check if an _instance_
8375 method of the same name exists in the root class. This is what
8376 the Objective-C runtime will do. */
8377 return lookup_method_static (root_inter, ident, 0);
8381 /* If an instance method was not found, return 0. */
8386 /* Add the method to the hash list if it doesn't contain an identical
8390 add_method_to_hash_list (hash *hash_list, tree method)
8394 if (!(hsh = hash_lookup (hash_list, METHOD_SEL_NAME (method))))
8396 /* Install on a global chain. */
8397 hash_enter (hash_list, method);
8401 /* Check types against those; if different, add to a list. */
8403 int already_there = comp_proto_with_proto (method, hsh->key, 1);
8404 for (loop = hsh->list; !already_there && loop; loop = loop->next)
8405 already_there |= comp_proto_with_proto (method, loop->value, 1);
8407 hash_add_attr (hsh, method);
8412 objc_add_method (tree klass, tree method, int is_class, bool is_optional)
8416 /* @optional methods are added to protocol's OPTIONAL list */
8419 gcc_assert (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE);
8420 if (!(mth = lookup_method (is_class
8421 ? PROTOCOL_OPTIONAL_CLS_METHODS (klass)
8422 : PROTOCOL_OPTIONAL_NST_METHODS (klass),
8427 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass);
8428 PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method;
8432 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass);
8433 PROTOCOL_OPTIONAL_NST_METHODS (klass) = method;
8437 else if (!(mth = lookup_method (is_class
8438 ? CLASS_CLS_METHODS (klass)
8439 : CLASS_NST_METHODS (klass), method)))
8441 /* put method on list in reverse order */
8444 DECL_CHAIN (method) = CLASS_CLS_METHODS (klass);
8445 CLASS_CLS_METHODS (klass) = method;
8449 DECL_CHAIN (method) = CLASS_NST_METHODS (klass);
8450 CLASS_NST_METHODS (klass) = method;
8455 /* When processing an @interface for a class or category, give hard
8456 errors on methods with identical selectors but differing argument
8457 and/or return types. We do not do this for @implementations, because
8458 C/C++ will do it for us (i.e., there will be duplicate function
8459 definition errors). */
8460 if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE
8461 || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE)
8462 && !comp_proto_with_proto (method, mth, 1))
8463 error ("duplicate declaration of method %<%c%E%>",
8464 is_class ? '+' : '-',
8465 METHOD_SEL_NAME (mth));
8469 add_method_to_hash_list (cls_method_hash_list, method);
8472 add_method_to_hash_list (nst_method_hash_list, method);
8474 /* Instance methods in root classes (and categories thereof)
8475 may act as class methods as a last resort. We also add
8476 instance methods listed in @protocol declarations to
8477 the class hash table, on the assumption that @protocols
8478 may be adopted by root classes or categories. */
8479 if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
8480 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
8481 klass = lookup_interface (CLASS_NAME (klass));
8483 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE
8484 || !CLASS_SUPER_NAME (klass))
8485 add_method_to_hash_list (cls_method_hash_list, method);
8492 add_class (tree class_name, tree name)
8494 struct interface_tuple **slot;
8496 /* Put interfaces on list in reverse order. */
8497 TREE_CHAIN (class_name) = interface_chain;
8498 interface_chain = class_name;
8500 if (interface_htab == NULL)
8501 interface_htab = htab_create_ggc (31, hash_interface, eq_interface, NULL);
8502 slot = (struct interface_tuple **)
8503 htab_find_slot_with_hash (interface_htab, name,
8504 IDENTIFIER_HASH_VALUE (name),
8508 *slot = ggc_alloc_cleared_interface_tuple ();
8511 (*slot)->class_name = class_name;
8513 return interface_chain;
8517 add_category (tree klass, tree category)
8519 /* Put categories on list in reverse order. */
8520 tree cat = lookup_category (klass, CLASS_SUPER_NAME (category));
8524 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
8526 CLASS_SUPER_NAME (category));
8530 CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass);
8531 CLASS_CATEGORY_LIST (klass) = category;
8535 /* Called after parsing each instance variable declaration. Necessary to
8536 preserve typedefs and implement public/private...
8538 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
8541 add_instance_variable (tree klass, objc_ivar_visibility_kind visibility,
8544 tree field_type = TREE_TYPE (field_decl);
8545 const char *ivar_name = DECL_NAME (field_decl)
8546 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl)))
8550 if (TREE_CODE (field_type) == REFERENCE_TYPE)
8552 error ("illegal reference type specified for instance variable %qs",
8554 /* Return class as is without adding this ivar. */
8559 if (field_type == error_mark_node || !TYPE_SIZE (field_type)
8560 || TYPE_SIZE (field_type) == error_mark_node)
8561 /* 'type[0]' is allowed, but 'type[]' is not! */
8563 error ("instance variable %qs has unknown size", ivar_name);
8564 /* Return class as is without adding this ivar. */
8569 /* Check if the ivar being added has a non-POD C++ type. If so, we will
8570 need to either (1) warn the user about it or (2) generate suitable
8571 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
8572 methods (if '-fobjc-call-cxx-cdtors' was specified). */
8573 if (MAYBE_CLASS_TYPE_P (field_type)
8574 && (TYPE_NEEDS_CONSTRUCTING (field_type)
8575 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)
8576 || TYPE_POLYMORPHIC_P (field_type)))
8578 tree type_name = OBJC_TYPE_NAME (field_type);
8580 if (flag_objc_call_cxx_cdtors)
8582 /* Since the ObjC runtime will be calling the constructors and
8583 destructors for us, the only thing we can't handle is the lack
8584 of a default constructor. */
8585 if (TYPE_NEEDS_CONSTRUCTING (field_type)
8586 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type))
8588 warning (0, "type %qE has no default constructor to call",
8591 /* If we cannot call a constructor, we should also avoid
8592 calling the destructor, for symmetry. */
8593 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
8594 warning (0, "destructor for %qE shall not be run either",
8600 static bool warn_cxx_ivars = false;
8602 if (TYPE_POLYMORPHIC_P (field_type))
8604 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
8606 error ("type %qE has virtual member functions", type_name);
8607 error ("illegal aggregate type %qE specified "
8608 "for instance variable %qs",
8609 type_name, ivar_name);
8610 /* Return class as is without adding this ivar. */
8614 /* User-defined constructors and destructors are not known to Obj-C
8615 and hence will not be called. This may or may not be a problem. */
8616 if (TYPE_NEEDS_CONSTRUCTING (field_type))
8617 warning (0, "type %qE has a user-defined constructor", type_name);
8618 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
8619 warning (0, "type %qE has a user-defined destructor", type_name);
8621 if (!warn_cxx_ivars)
8623 warning (0, "C++ constructors and destructors will not "
8624 "be invoked for Objective-C fields");
8625 warn_cxx_ivars = true;
8631 /* Overload the public attribute, it is not used for FIELD_DECLs. */
8634 case OBJC_IVAR_VIS_PROTECTED:
8635 TREE_PUBLIC (field_decl) = 0;
8636 TREE_PRIVATE (field_decl) = 0;
8637 TREE_PROTECTED (field_decl) = 1;
8640 case OBJC_IVAR_VIS_PACKAGE:
8641 /* TODO: Implement the package variant. */
8642 case OBJC_IVAR_VIS_PUBLIC:
8643 TREE_PUBLIC (field_decl) = 1;
8644 TREE_PRIVATE (field_decl) = 0;
8645 TREE_PROTECTED (field_decl) = 0;
8648 case OBJC_IVAR_VIS_PRIVATE:
8649 TREE_PUBLIC (field_decl) = 0;
8650 TREE_PRIVATE (field_decl) = 1;
8651 TREE_PROTECTED (field_decl) = 0;
8656 CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl);
8663 is_ivar (tree decl_chain, tree ident)
8665 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
8666 if (DECL_NAME (decl_chain) == ident)
8671 /* True if the ivar is private and we are not in its implementation. */
8674 is_private (tree decl)
8676 return (TREE_PRIVATE (decl)
8677 && ! is_ivar (CLASS_IVARS (implementation_template),
8681 /* We have an instance variable reference;, check to see if it is public. */
8684 objc_is_public (tree expr, tree identifier)
8686 tree basetype, decl;
8689 if (processing_template_decl)
8693 if (TREE_TYPE (expr) == error_mark_node)
8696 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8698 if (basetype && TREE_CODE (basetype) == RECORD_TYPE)
8700 if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype))
8702 tree klass = lookup_interface (OBJC_TYPE_NAME (basetype));
8706 error ("cannot find interface declaration for %qE",
8707 OBJC_TYPE_NAME (basetype));
8711 if ((decl = is_ivar (get_class_ivars (klass, true), identifier)))
8713 if (TREE_PUBLIC (decl))
8716 /* Important difference between the Stepstone translator:
8717 all instance variables should be public within the context
8718 of the implementation. */
8719 if (objc_implementation_context
8720 && ((TREE_CODE (objc_implementation_context)
8721 == CLASS_IMPLEMENTATION_TYPE)
8722 || (TREE_CODE (objc_implementation_context)
8723 == CATEGORY_IMPLEMENTATION_TYPE)))
8725 tree curtype = TYPE_MAIN_VARIANT
8726 (CLASS_STATIC_TEMPLATE
8727 (implementation_template));
8729 if (basetype == curtype
8730 || DERIVED_FROM_P (basetype, curtype))
8732 int priv = is_private (decl);
8735 error ("instance variable %qE is declared private",
8742 /* The 2.95.2 compiler sometimes allowed C functions to access
8743 non-@public ivars. We will let this slide for now... */
8744 if (!objc_method_context)
8746 warning (0, "instance variable %qE is %s; "
8747 "this will be a hard error in the future",
8749 TREE_PRIVATE (decl) ? "@private" : "@protected");
8753 error ("instance variable %qE is declared %s",
8755 TREE_PRIVATE (decl) ? "private" : "protected");
8764 /* Make sure all methods in CHAIN (a list of method declarations from
8765 an @interface or a @protocol) are in IMPLEMENTATION (the
8766 implementation context). This is used to check for example that
8767 all methods declared in an @interface were implemented in an
8770 Some special methods (property setters/getters) are special and if
8771 they are not found in IMPLEMENTATION, we look them up in its
8775 check_methods (tree chain, tree implementation, int mtype)
8780 if (mtype == (int)'+')
8781 list = CLASS_CLS_METHODS (implementation);
8783 list = CLASS_NST_METHODS (implementation);
8787 /* If the method is associated with a dynamic property, then it
8788 is Ok not to have the method implementation, as it will be
8789 generated dynamically at runtime. To decide if the method is
8790 associated with a @dynamic property, we search the list of
8791 @synthesize and @dynamic for this implementation, and look
8792 for any @dynamic property with the same setter or getter name
8795 for (x = IMPL_PROPERTY_DECL (implementation); x; x = TREE_CHAIN (x))
8796 if (PROPERTY_DYNAMIC (x)
8797 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
8798 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
8803 chain = TREE_CHAIN (chain); /* next method... */
8807 if (!lookup_method (list, chain))
8809 /* If the method is a property setter/getter, we'll still
8810 allow it to be missing if it is implemented by
8811 'interface' or any of its superclasses. */
8812 tree property = METHOD_PROPERTY_CONTEXT (chain);
8815 /* Note that since this is a property getter/setter, it
8816 is obviously an instance method. */
8817 tree interface = NULL_TREE;
8819 /* For a category, first check the main class
8821 if (TREE_CODE (implementation) == CATEGORY_IMPLEMENTATION_TYPE)
8823 interface = lookup_interface (CLASS_NAME (implementation));
8825 /* If the method is found in the main class, it's Ok. */
8826 if (lookup_method (CLASS_NST_METHODS (interface), chain))
8828 chain = DECL_CHAIN (chain);
8832 /* Else, get the superclass. */
8833 if (CLASS_SUPER_NAME (interface))
8834 interface = lookup_interface (CLASS_SUPER_NAME (interface));
8836 interface = NULL_TREE;
8839 /* Get the superclass for classes. */
8840 if (TREE_CODE (implementation) == CLASS_IMPLEMENTATION_TYPE)
8842 if (CLASS_SUPER_NAME (implementation))
8843 interface = lookup_interface (CLASS_SUPER_NAME (implementation));
8845 interface = NULL_TREE;
8848 /* Now, interface is the superclass, if any; go check it. */
8851 if (lookup_method_static (interface, chain, 0))
8853 chain = DECL_CHAIN (chain);
8857 /* Else, fall through - warn. */
8861 switch (TREE_CODE (implementation))
8863 case CLASS_IMPLEMENTATION_TYPE:
8864 warning (0, "incomplete implementation of class %qE",
8865 CLASS_NAME (implementation));
8867 case CATEGORY_IMPLEMENTATION_TYPE:
8868 warning (0, "incomplete implementation of category %qE",
8869 CLASS_SUPER_NAME (implementation));
8877 warning (0, "method definition for %<%c%E%> not found",
8878 mtype, METHOD_SEL_NAME (chain));
8881 chain = DECL_CHAIN (chain);
8887 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
8890 conforms_to_protocol (tree klass, tree protocol)
8892 if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE)
8894 tree p = CLASS_PROTOCOL_LIST (klass);
8895 while (p && TREE_VALUE (p) != protocol)
8900 tree super = (CLASS_SUPER_NAME (klass)
8901 ? lookup_interface (CLASS_SUPER_NAME (klass))
8903 int tmp = super ? conforms_to_protocol (super, protocol) : 0;
8912 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
8913 CONTEXT. This is one of two mechanisms to check protocol integrity. */
8916 check_methods_accessible (tree chain, tree context, int mtype)
8920 tree base_context = context;
8924 /* If the method is associated with a dynamic property, then it
8925 is Ok not to have the method implementation, as it will be
8926 generated dynamically at runtime. Search for any @dynamic
8927 property with the same setter or getter name as this
8928 method. TODO: Use a hashtable lookup. */
8930 for (x = IMPL_PROPERTY_DECL (base_context); x; x = TREE_CHAIN (x))
8931 if (PROPERTY_DYNAMIC (x)
8932 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
8933 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
8938 chain = TREE_CHAIN (chain); /* next method... */
8942 context = base_context;
8946 list = CLASS_CLS_METHODS (context);
8948 list = CLASS_NST_METHODS (context);
8950 if (lookup_method (list, chain))
8953 switch (TREE_CODE (context))
8955 case CLASS_IMPLEMENTATION_TYPE:
8956 case CLASS_INTERFACE_TYPE:
8957 context = (CLASS_SUPER_NAME (context)
8958 ? lookup_interface (CLASS_SUPER_NAME (context))
8961 case CATEGORY_IMPLEMENTATION_TYPE:
8962 case CATEGORY_INTERFACE_TYPE:
8963 context = (CLASS_NAME (context)
8964 ? lookup_interface (CLASS_NAME (context))
8972 if (context == NULL_TREE)
8976 switch (TREE_CODE (objc_implementation_context))
8978 case CLASS_IMPLEMENTATION_TYPE:
8979 warning (0, "incomplete implementation of class %qE",
8980 CLASS_NAME (objc_implementation_context));
8982 case CATEGORY_IMPLEMENTATION_TYPE:
8983 warning (0, "incomplete implementation of category %qE",
8984 CLASS_SUPER_NAME (objc_implementation_context));
8991 warning (0, "method definition for %<%c%E%> not found",
8992 mtype, METHOD_SEL_NAME (chain));
8995 chain = TREE_CHAIN (chain); /* next method... */
9000 /* Check whether the current interface (accessible via
9001 'objc_implementation_context') actually implements protocol P, along
9002 with any protocols that P inherits. */
9005 check_protocol (tree p, const char *type, tree name)
9007 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
9011 /* Ensure that all protocols have bodies! */
9014 f1 = check_methods (PROTOCOL_CLS_METHODS (p),
9015 objc_implementation_context,
9017 f2 = check_methods (PROTOCOL_NST_METHODS (p),
9018 objc_implementation_context,
9023 f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
9024 objc_implementation_context,
9026 f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
9027 objc_implementation_context,
9032 warning (0, "%s %qE does not fully implement the %qE protocol",
9033 type, name, PROTOCOL_NAME (p));
9036 /* Check protocols recursively. */
9037 if (PROTOCOL_LIST (p))
9039 tree subs = PROTOCOL_LIST (p);
9041 lookup_interface (CLASS_SUPER_NAME (implementation_template));
9045 tree sub = TREE_VALUE (subs);
9047 /* If the superclass does not conform to the protocols
9048 inherited by P, then we must! */
9049 if (!super_class || !conforms_to_protocol (super_class, sub))
9050 check_protocol (sub, type, name);
9051 subs = TREE_CHAIN (subs);
9056 /* Check whether the current interface (accessible via
9057 'objc_implementation_context') actually implements the protocols listed
9061 check_protocols (tree proto_list, const char *type, tree name)
9063 for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
9065 tree p = TREE_VALUE (proto_list);
9067 check_protocol (p, type, name);
9071 /* Make sure that the class CLASS_NAME is defined
9072 CODE says which kind of thing CLASS_NAME ought to be.
9073 It can be CLASS_INTERFACE_TYPE, CLASS_IMPLEMENTATION_TYPE,
9074 CATEGORY_INTERFACE_TYPE, or CATEGORY_IMPLEMENTATION_TYPE. */
9077 start_class (enum tree_code code, tree class_name, tree super_name,
9083 if (current_namespace != global_namespace) {
9084 error ("Objective-C declarations may only appear in global scope");
9086 #endif /* OBJCPLUS */
9088 if (objc_implementation_context)
9090 warning (0, "%<@end%> missing in implementation context");
9091 finish_class (objc_implementation_context);
9092 objc_ivar_chain = NULL_TREE;
9093 objc_implementation_context = NULL_TREE;
9096 klass = make_node (code);
9097 TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS);
9099 /* Check for existence of the super class, if one was specified. Note
9100 that we must have seen an @interface, not just a @class. If we
9101 are looking at a @compatibility_alias, traverse it first. */
9102 if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE)
9105 tree super = objc_is_class_name (super_name);
9107 if (!super || !lookup_interface (super))
9109 error ("cannot find interface declaration for %qE, superclass of %qE",
9110 super ? super : super_name,
9112 super_name = NULL_TREE;
9118 CLASS_NAME (klass) = class_name;
9119 CLASS_SUPER_NAME (klass) = super_name;
9120 CLASS_CLS_METHODS (klass) = NULL_TREE;
9122 if (! objc_is_class_name (class_name)
9123 && (decl = lookup_name (class_name)))
9125 error ("%qE redeclared as different kind of symbol",
9127 error ("previous declaration of %q+D",
9133 case CLASS_IMPLEMENTATION_TYPE:
9137 for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
9138 if (TREE_VALUE (chain) == class_name)
9140 error ("reimplementation of class %qE",
9142 return error_mark_node;
9144 implemented_classes = tree_cons (NULL_TREE, class_name,
9145 implemented_classes);
9148 /* Reset for multiple classes per file. */
9151 objc_implementation_context = klass;
9153 /* Lookup the interface for this implementation. */
9155 if (!(implementation_template = lookup_interface (class_name)))
9157 warning (0, "cannot find interface declaration for %qE",
9159 add_class (implementation_template = objc_implementation_context,
9163 /* If a super class has been specified in the implementation,
9164 insure it conforms to the one specified in the interface. */
9167 && (super_name != CLASS_SUPER_NAME (implementation_template)))
9169 tree previous_name = CLASS_SUPER_NAME (implementation_template);
9170 error ("conflicting super class name %qE",
9173 error ("previous declaration of %qE", previous_name);
9175 error ("previous declaration");
9178 else if (! super_name)
9180 CLASS_SUPER_NAME (objc_implementation_context)
9181 = CLASS_SUPER_NAME (implementation_template);
9185 case CLASS_INTERFACE_TYPE:
9186 if (lookup_interface (class_name))
9188 error ("duplicate interface declaration for class %qE", class_name);
9190 warning (0, "duplicate interface declaration for class %qE", class_name);
9193 add_class (klass, class_name);
9196 CLASS_PROTOCOL_LIST (klass)
9197 = lookup_and_install_protocols (protocol_list);
9200 case CATEGORY_INTERFACE_TYPE:
9202 tree class_category_is_assoc_with;
9204 /* For a category, class_name is really the name of the class that
9205 the following set of methods will be associated with. We must
9206 find the interface so that can derive the objects template. */
9207 if (!(class_category_is_assoc_with = lookup_interface (class_name)))
9209 error ("cannot find interface declaration for %qE",
9211 exit (FATAL_EXIT_CODE);
9214 add_category (class_category_is_assoc_with, klass);
9217 CLASS_PROTOCOL_LIST (klass)
9218 = lookup_and_install_protocols (protocol_list);
9222 case CATEGORY_IMPLEMENTATION_TYPE:
9223 /* Reset for multiple classes per file. */
9226 objc_implementation_context = klass;
9228 /* For a category, class_name is really the name of the class that
9229 the following set of methods will be associated with. We must
9230 find the interface so that can derive the objects template. */
9232 if (!(implementation_template = lookup_interface (class_name)))
9234 error ("cannot find interface declaration for %qE",
9236 exit (FATAL_EXIT_CODE);
9246 continue_class (tree klass)
9248 switch (TREE_CODE (klass))
9250 case CLASS_IMPLEMENTATION_TYPE:
9251 case CATEGORY_IMPLEMENTATION_TYPE:
9253 struct imp_entry *imp_entry;
9255 /* Check consistency of the instance variables. */
9257 if (CLASS_RAW_IVARS (klass))
9258 check_ivars (implementation_template, klass);
9260 /* code generation */
9262 push_lang_context (lang_name_c);
9264 build_private_template (implementation_template);
9265 uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template);
9266 objc_instance_type = build_pointer_type (uprivate_record);
9268 imp_entry = ggc_alloc_imp_entry ();
9270 imp_entry->next = imp_list;
9271 imp_entry->imp_context = klass;
9272 imp_entry->imp_template = implementation_template;
9274 synth_forward_declarations ();
9275 imp_entry->class_decl = UOBJC_CLASS_decl;
9276 imp_entry->meta_decl = UOBJC_METACLASS_decl;
9277 imp_entry->has_cxx_cdtors = 0;
9279 /* Append to front and increment count. */
9280 imp_list = imp_entry;
9281 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
9286 pop_lang_context ();
9287 #endif /* OBJCPLUS */
9289 return get_class_ivars (implementation_template, true);
9292 case CLASS_INTERFACE_TYPE:
9295 push_lang_context (lang_name_c);
9296 #endif /* OBJCPLUS */
9297 objc_collecting_ivars = 1;
9298 build_private_template (klass);
9299 objc_collecting_ivars = 0;
9301 pop_lang_context ();
9302 #endif /* OBJCPLUS */
9307 return error_mark_node;
9311 /* This routine builds name of the setter synthesized function. */
9313 objc_build_property_setter_name (tree ident)
9315 /* TODO: Use alloca to allocate buffer of appropriate size. */
9316 static char string[BUFSIZE];
9317 sprintf (string, "set%s:", IDENTIFIER_POINTER (ident));
9318 string[3] = TOUPPER (string[3]);
9322 /* This routine prepares the declarations of the property accessor
9323 helper functions (objc_getProperty(), etc) that are used when
9324 @synthesize is used. */
9326 build_objc_property_accessor_helpers (void)
9330 /* Declare the following function:
9332 objc_getProperty (id self, SEL _cmd,
9333 ptrdiff_t offset, BOOL is_atomic); */
9334 type = build_function_type_list (objc_object_type,
9340 objc_getProperty_decl = add_builtin_function ("objc_getProperty",
9341 type, 0, NOT_BUILT_IN,
9343 TREE_NOTHROW (objc_getProperty_decl) = 0;
9345 /* Declare the following function:
9347 objc_setProperty (id self, SEL _cmd,
9348 ptrdiff_t offset, id new_value,
9349 BOOL is_atomic, BOOL should_copy); */
9350 type = build_function_type_list (void_type_node,
9358 objc_setProperty_decl = add_builtin_function ("objc_setProperty",
9359 type, 0, NOT_BUILT_IN,
9361 TREE_NOTHROW (objc_setProperty_decl) = 0;
9363 /* This is the type of all of the following functions
9364 (objc_copyStruct(), objc_getPropertyStruct() and
9365 objc_setPropertyStruct()). */
9366 type = build_function_type_list (void_type_node,
9368 const_ptr_type_node,
9374 if (flag_next_runtime)
9376 /* Declare the following function:
9378 objc_copyStruct (void *destination, const void *source,
9379 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
9380 objc_copyStruct_decl = add_builtin_function ("objc_copyStruct",
9381 type, 0, NOT_BUILT_IN,
9383 TREE_NOTHROW (objc_copyStruct_decl) = 0;
9384 objc_getPropertyStruct_decl = NULL_TREE;
9385 objc_setPropertyStruct_decl = NULL_TREE;
9389 objc_copyStruct_decl = NULL_TREE;
9391 /* Declare the following function:
9393 objc_getPropertyStruct (void *destination, const void *source,
9394 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
9395 objc_getPropertyStruct_decl = add_builtin_function ("objc_getPropertyStruct",
9396 type, 0, NOT_BUILT_IN,
9398 TREE_NOTHROW (objc_getPropertyStruct_decl) = 0;
9399 /* Declare the following function:
9401 objc_setPropertyStruct (void *destination, const void *source,
9402 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
9403 objc_setPropertyStruct_decl = add_builtin_function ("objc_setPropertyStruct",
9404 type, 0, NOT_BUILT_IN,
9406 TREE_NOTHROW (objc_setPropertyStruct_decl) = 0;
9410 /* This looks up an ivar in a class (including superclasses). */
9412 lookup_ivar (tree interface, tree instance_variable_name)
9418 for (decl_chain = CLASS_IVARS (interface); decl_chain; decl_chain = DECL_CHAIN (decl_chain))
9419 if (DECL_NAME (decl_chain) == instance_variable_name)
9422 /* Not found. Search superclass if any. */
9423 if (CLASS_SUPER_NAME (interface))
9424 interface = lookup_interface (CLASS_SUPER_NAME (interface));
9430 /* This routine synthesizes a 'getter' method. This is only called
9431 for @synthesize properties. */
9433 objc_synthesize_getter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
9435 location_t location = DECL_SOURCE_LOCATION (property);
9440 /* If user has implemented a getter with same name then do nothing. */
9441 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
9442 PROPERTY_GETTER_NAME (property)))
9445 /* Find declaration of the property getter in the interface (or
9446 superclass, or protocol). There must be one. */
9447 decl = lookup_method_static (klass, PROPERTY_GETTER_NAME (property), 0);
9449 /* If one not declared in the interface, this condition has already
9450 been reported as user error (because property was not declared in
9455 /* Adapt the 'decl'. Use the source location of the @synthesize
9456 statement for error messages. */
9457 decl = copy_node (decl);
9458 DECL_SOURCE_LOCATION (decl) = location;
9460 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE);
9461 body = c_begin_compound_stmt (true);
9463 /* Now we need to decide how we build the getter. There are three
9466 for 'copy' or 'retain' properties we need to use the
9467 objc_getProperty() accessor helper which knows about retain and
9468 copy. It supports both 'nonatomic' and 'atomic' access.
9470 for 'nonatomic, assign' properties we can access the instance
9471 variable directly. 'nonatomic' means we don't have to use locks,
9472 and 'assign' means we don't have to worry about retain or copy.
9473 If you combine the two, it means we can just access the instance
9476 for 'atomic, assign' properties we use objc_copyStruct() (for the
9477 next runtime) or objc_getPropertyStruct() (for the GNU runtime). */
9478 switch (PROPERTY_ASSIGN_SEMANTICS (property))
9480 case OBJC_PROPERTY_RETAIN:
9481 case OBJC_PROPERTY_COPY:
9483 /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */
9484 tree cmd, ivar, offset, is_atomic;
9485 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
9487 /* Find the ivar to compute the offset. */
9488 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
9489 if (!ivar || is_private (ivar))
9491 /* This should never happen. */
9493 "can not find instance variable associated with property");
9494 ret_val = error_mark_node;
9497 offset = byte_position (ivar);
9499 if (PROPERTY_NONATOMIC (property))
9500 is_atomic = boolean_false_node;
9502 is_atomic = boolean_true_node;
9504 ret_val = build_function_call
9506 /* Function prototype. */
9507 objc_getProperty_decl,
9509 tree_cons /* self */
9510 (NULL_TREE, self_decl,
9511 tree_cons /* _cmd */
9513 tree_cons /* offset */
9515 tree_cons /* is_atomic */
9516 (NULL_TREE, is_atomic, NULL_TREE)))));
9519 case OBJC_PROPERTY_ASSIGN:
9520 if (PROPERTY_NONATOMIC (property))
9522 /* We build "return self->PROPERTY_IVAR_NAME;" */
9523 ret_val = objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property));
9529 <property type> __objc_property_temp;
9530 objc_getPropertyStruct (&__objc_property_temp,
9531 &(self->PROPERTY_IVAR_NAME),
9532 sizeof (type of self->PROPERTY_IVAR_NAME),
9535 return __objc_property_temp;
9537 For the NeXT runtime, we need to use objc_copyStruct
9538 instead of objc_getPropertyStruct. */
9539 tree objc_property_temp_decl, function_decl, function_call;
9540 tree size_of, is_atomic;
9542 objc_property_temp_decl = objc_create_temporary_var (TREE_TYPE (property), "__objc_property_temp");
9543 DECL_SOURCE_LOCATION (objc_property_temp_decl) = location;
9544 objc_property_temp_decl = lang_hooks.decls.pushdecl (objc_property_temp_decl);
9546 /* sizeof (ivar type). Since the ivar and the property have
9547 the same type, there is no need to lookup the ivar. */
9548 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
9549 true /* is_sizeof */,
9550 false /* complain */);
9552 if (PROPERTY_NONATOMIC (property))
9553 is_atomic = boolean_false_node;
9555 is_atomic = boolean_true_node;
9557 if (flag_next_runtime)
9558 function_decl = objc_copyStruct_decl;
9560 function_decl = objc_getPropertyStruct_decl;
9562 function_call = build_function_call
9564 /* Function prototype. */
9567 tree_cons /* &__objc_property_temp_decl */
9568 /* Warning: note that using build_fold_addr_expr_loc()
9569 here causes invalid code to be generated. */
9570 (NULL_TREE, build_unary_op (location, ADDR_EXPR, objc_property_temp_decl, 0),
9571 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
9572 (NULL_TREE, build_fold_addr_expr_loc (location,
9574 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
9575 tree_cons /* sizeof (PROPERTY_IVAR) */
9576 (NULL_TREE, size_of,
9577 tree_cons /* is_atomic */
9578 (NULL_TREE, is_atomic,
9579 /* TODO: This is currently ignored by the GNU
9580 runtime, but what about the next one ? */
9581 tree_cons /* has_strong */
9582 (NULL_TREE, boolean_true_node, NULL_TREE))))));
9584 add_stmt (function_call);
9586 ret_val = objc_property_temp_decl;
9593 gcc_assert (ret_val);
9596 finish_return_stmt (ret_val);
9598 c_finish_return (location, ret_val, NULL_TREE);
9601 add_stmt (c_end_compound_stmt (location, body, true));
9602 fn = current_function_decl;
9606 objc_finish_method_definition (fn);
9609 /* This routine synthesizes a 'setter' method. */
9612 objc_synthesize_setter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
9614 location_t location = DECL_SOURCE_LOCATION (property);
9617 tree new_value, statement;
9619 /* If user has implemented a setter with same name then do nothing. */
9620 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
9621 PROPERTY_SETTER_NAME (property)))
9624 /* Find declaration of the property setter in the interface (or
9625 superclass, or protocol). There must be one. */
9626 decl = lookup_method_static (klass, PROPERTY_SETTER_NAME (property), 0);
9628 /* If one not declared in the interface, this condition has already
9629 been reported as user error (because property was not declared in
9634 /* Adapt the 'decl'. Use the source location of the @synthesize
9635 statement for error messages. */
9636 decl = copy_node (decl);
9637 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (property);
9639 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE);
9641 body = c_begin_compound_stmt (true);
9643 /* The 'new_value' is the only argument to the method, which is the
9644 3rd argument of the function, after self and _cmd. We use twice
9645 TREE_CHAIN to move forward two arguments. */
9646 new_value = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)));
9648 /* This would presumably happen if the user has specified a
9649 prototype for the setter that does not have an argument! */
9650 if (new_value == NULL_TREE)
9652 /* TODO: This should be caught much earlier than this. */
9653 error_at (DECL_SOURCE_LOCATION (decl), "invalid setter, it must have one argument");
9654 /* Try to recover somehow. */
9655 new_value = error_mark_node;
9658 /* Now we need to decide how we build the setter. There are three
9661 for 'copy' or 'retain' properties we need to use the
9662 objc_setProperty() accessor helper which knows about retain and
9663 copy. It supports both 'nonatomic' and 'atomic' access.
9665 for 'nonatomic, assign' properties we can access the instance
9666 variable directly. 'nonatomic' means we don't have to use locks,
9667 and 'assign' means we don't have to worry about retain or copy.
9668 If you combine the two, it means we can just access the instance
9671 for 'atomic, assign' properties we use objc_copyStruct() (for the
9672 next runtime) or objc_setPropertyStruct() (for the GNU runtime). */
9673 switch (PROPERTY_ASSIGN_SEMANTICS (property))
9675 case OBJC_PROPERTY_RETAIN:
9676 case OBJC_PROPERTY_COPY:
9678 /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */
9679 tree cmd, ivar, offset, is_atomic, should_copy;
9680 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
9682 /* Find the ivar to compute the offset. */
9683 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
9684 if (!ivar || is_private (ivar))
9687 "can not find instance variable associated with property");
9688 statement = error_mark_node;
9691 offset = byte_position (ivar);
9693 if (PROPERTY_NONATOMIC (property))
9694 is_atomic = boolean_false_node;
9696 is_atomic = boolean_true_node;
9698 if (PROPERTY_ASSIGN_SEMANTICS (property) == OBJC_PROPERTY_COPY)
9699 should_copy = boolean_true_node;
9701 should_copy = boolean_false_node;
9703 statement = build_function_call
9705 /* Function prototype. */
9706 objc_setProperty_decl,
9708 tree_cons /* self */
9709 (NULL_TREE, self_decl,
9710 tree_cons /* _cmd */
9712 tree_cons /* offset */
9714 tree_cons /* new_value */
9715 (NULL_TREE, new_value,
9716 tree_cons /* is_atomic */
9717 (NULL_TREE, is_atomic,
9718 tree_cons /* should_copy */
9719 (NULL_TREE, should_copy, NULL_TREE)))))));
9722 case OBJC_PROPERTY_ASSIGN:
9723 if (PROPERTY_NONATOMIC (property))
9725 /* We build "self->PROPERTY_IVAR_NAME = new_value;" */
9726 statement = build_modify_expr
9728 objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)),
9729 NULL_TREE, NOP_EXPR,
9730 location, new_value, NULL_TREE);
9736 objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME),
9738 sizeof (type of self->PROPERTY_IVAR_NAME),
9742 For the NeXT runtime, we need to use objc_copyStruct
9743 instead of objc_getPropertyStruct. */
9744 tree function_decl, size_of, is_atomic;
9746 /* sizeof (ivar type). Since the ivar and the property have
9747 the same type, there is no need to lookup the ivar. */
9748 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
9749 true /* is_sizeof */,
9750 false /* complain */);
9752 if (PROPERTY_NONATOMIC (property))
9753 is_atomic = boolean_false_node;
9755 is_atomic = boolean_true_node;
9757 if (flag_next_runtime)
9758 function_decl = objc_copyStruct_decl;
9760 function_decl = objc_setPropertyStruct_decl;
9762 statement = build_function_call
9764 /* Function prototype. */
9767 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
9768 (NULL_TREE, build_fold_addr_expr_loc (location,
9770 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
9771 tree_cons /* &new_value */
9772 (NULL_TREE, build_fold_addr_expr_loc (location, new_value),
9773 tree_cons /* sizeof (PROPERTY_IVAR) */
9774 (NULL_TREE, size_of,
9775 tree_cons /* is_atomic */
9776 (NULL_TREE, is_atomic,
9777 /* TODO: This is currently ignored by the GNU
9778 runtime, but what about the next one ? */
9779 tree_cons /* has_strong */
9780 (NULL_TREE, boolean_true_node, NULL_TREE))))));
9786 gcc_assert (statement);
9788 add_stmt (statement);
9789 add_stmt (c_end_compound_stmt (location, body, true));
9790 fn = current_function_decl;
9794 objc_finish_method_definition (fn);
9797 /* This function is a sub-routine of objc_add_synthesize_declaration.
9798 It is called for each property to synthesize once we have
9799 determined that the context is Ok. */
9801 objc_add_synthesize_declaration_for_property (location_t location, tree interface,
9802 tree property_name, tree ivar_name)
9804 /* Find the @property declaration. */
9808 /* Check that synthesize or dynamic has not already been used for
9809 the same property. */
9810 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
9811 if (PROPERTY_NAME (property) == property_name)
9813 location_t original_location = DECL_SOURCE_LOCATION (property);
9815 if (PROPERTY_DYNAMIC (property))
9816 error_at (location, "property %qs already specified in %<@dynamic%>",
9817 IDENTIFIER_POINTER (property_name));
9819 error_at (location, "property %qs already specified in %<@synthesize%>",
9820 IDENTIFIER_POINTER (property_name));
9822 if (original_location != UNKNOWN_LOCATION)
9823 inform (original_location, "originally specified here");
9827 /* Check that the property is declared in the interface. It could
9828 also be declared in a superclass or protocol. */
9829 property = lookup_property (interface, property_name);
9833 error_at (location, "no declaration of property %qs found in the interface",
9834 IDENTIFIER_POINTER (property_name));
9839 /* We have to copy the property, because we want to chain it to
9840 the implementation context, and we want to store the source
9841 location of the @synthesize, not of the original
9843 property = copy_node (property);
9844 DECL_SOURCE_LOCATION (property) = location;
9847 /* Determine PROPERTY_IVAR_NAME. */
9848 if (ivar_name == NULL_TREE)
9849 ivar_name = property_name;
9851 /* Check that the instance variable exists. You can only use an
9852 instance variable from the same class, not one from the
9853 superclass (this makes sense as it allows us to check that an
9854 instance variable is only used in one synthesized property). */
9856 tree ivar = is_ivar (CLASS_IVARS (interface), ivar_name);
9859 error_at (location, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar",
9860 IDENTIFIER_POINTER (property_name));
9864 /* If the instance variable has a different C type, we warn. */
9865 if (!comptypes (TREE_TYPE (property), TREE_TYPE (ivar)))
9867 location_t original_location = DECL_SOURCE_LOCATION (ivar);
9869 error_at (location, "property %qs is using instance variable %qs of incompatible type",
9870 IDENTIFIER_POINTER (property_name),
9871 IDENTIFIER_POINTER (ivar_name));
9873 if (original_location != UNKNOWN_LOCATION)
9874 inform (original_location, "originally specified here");
9878 /* Check that no other property is using the same instance
9880 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
9881 if (PROPERTY_IVAR_NAME (x) == ivar_name)
9883 location_t original_location = DECL_SOURCE_LOCATION (x);
9885 error_at (location, "property %qs is using the same instance variable as property %qs",
9886 IDENTIFIER_POINTER (property_name),
9887 IDENTIFIER_POINTER (PROPERTY_NAME (x)));
9889 if (original_location != UNKNOWN_LOCATION)
9890 inform (original_location, "originally specified here");
9892 /* We keep going on. This won't cause the compiler to fail;
9893 the failure would most likely be at runtime. */
9896 /* Note that a @synthesize (and only a @synthesize) always sets
9897 PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a
9898 @synthesize by that. */
9899 PROPERTY_IVAR_NAME (property) = ivar_name;
9901 /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the
9902 original declaration; they are always set (with the exception of
9903 PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */
9905 /* Add the property to the list of properties for current implementation. */
9906 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
9907 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
9909 /* Note how we don't actually synthesize the getter/setter here; it
9910 would be very natural, but we may miss the fact that the user has
9911 implemented his own getter/setter later on in the @implementation
9912 (in which case we shouldn't generate getter/setter). We wait
9913 until we have parsed it all before generating the code. */
9916 /* This function is called by the parser after a @synthesize
9917 expression is parsed. 'location' is the location of the
9918 @synthesize expression, and 'property_and_ivar_list' is a chained
9919 list of the property and ivar names. */
9921 objc_add_synthesize_declaration (location_t location, tree property_and_ivar_list)
9923 tree interface, chain;
9925 if (flag_objc1_only)
9926 error_at (input_location, "%<@synthesize%> is not available in Objective-C 1.0");
9928 if (property_and_ivar_list == error_mark_node)
9931 if (!objc_implementation_context)
9933 /* We can get here only in Objective-C; the Objective-C++ parser
9934 detects the problem while parsing, outputs the error
9935 "misplaced '@synthesize' Objective-C++ construct" and skips
9937 error_at (location, "%<@synthesize%> not in @implementation context");
9941 if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE)
9943 error_at (location, "%<@synthesize%> can not be used in categories");
9947 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
9950 /* I can't see how this could happen, but it is good as a safety check. */
9952 "%<@synthesize%> requires the @interface of the class to be available");
9956 /* Now, iterate over the properties and do each of them. */
9957 for (chain = property_and_ivar_list; chain; chain = TREE_CHAIN (chain))
9959 objc_add_synthesize_declaration_for_property (location, interface, TREE_VALUE (chain),
9960 TREE_PURPOSE (chain));
9964 /* This function is a sub-routine of objc_add_dynamic_declaration. It
9965 is called for each property to mark as dynamic once we have
9966 determined that the context is Ok. */
9968 objc_add_dynamic_declaration_for_property (location_t location, tree interface,
9971 /* Find the @property declaration. */
9974 /* Check that synthesize or dynamic has not already been used for
9975 the same property. */
9976 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
9977 if (PROPERTY_NAME (property) == property_name)
9979 location_t original_location = DECL_SOURCE_LOCATION (property);
9981 if (PROPERTY_DYNAMIC (property))
9982 error_at (location, "property %qs already specified in %<@dynamic%>",
9983 IDENTIFIER_POINTER (property_name));
9985 error_at (location, "property %qs already specified in %<@synthesize%>",
9986 IDENTIFIER_POINTER (property_name));
9988 if (original_location != UNKNOWN_LOCATION)
9989 inform (original_location, "originally specified here");
9993 /* Check that the property is declared in the interface. It could
9994 also be declared in a superclass or protocol. */
9995 property = lookup_property (interface, property_name);
9999 error_at (location, "no declaration of property %qs found in the interface",
10000 IDENTIFIER_POINTER (property_name));
10005 /* We have to copy the property, because we want to chain it to
10006 the implementation context, and we want to store the source
10007 location of the @synthesize, not of the original
10009 property = copy_node (property);
10010 DECL_SOURCE_LOCATION (property) = location;
10013 /* Note that a @dynamic (and only a @dynamic) always sets
10014 PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that.
10015 (actually, as explained above, PROPERTY_DECL generated by
10016 @property and associated with a @dynamic property are also marked
10017 as PROPERTY_DYNAMIC). */
10018 PROPERTY_DYNAMIC (property) = 1;
10020 /* Add the property to the list of properties for current implementation. */
10021 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
10022 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
10025 /* This function is called by the parser after a @dynamic expression
10026 is parsed. 'location' is the location of the @dynamic expression,
10027 and 'property_list' is a chained list of all the property
10030 objc_add_dynamic_declaration (location_t location, tree property_list)
10032 tree interface, chain;
10034 if (flag_objc1_only)
10035 error_at (input_location, "%<@dynamic%> is not available in Objective-C 1.0");
10037 if (property_list == error_mark_node)
10040 if (!objc_implementation_context)
10042 /* We can get here only in Objective-C; the Objective-C++ parser
10043 detects the problem while parsing, outputs the error
10044 "misplaced '@dynamic' Objective-C++ construct" and skips the
10046 error_at (location, "%<@dynamic%> not in @implementation context");
10050 /* @dynamic is allowed in categories. */
10051 switch (TREE_CODE (objc_implementation_context))
10053 case CLASS_IMPLEMENTATION_TYPE:
10054 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
10056 case CATEGORY_IMPLEMENTATION_TYPE:
10057 interface = lookup_category (implementation_template,
10058 CLASS_SUPER_NAME (objc_implementation_context));
10061 gcc_unreachable ();
10066 /* I can't see how this could happen, but it is good as a safety check. */
10067 error_at (location,
10068 "%<@dynamic%> requires the @interface of the class to be available");
10072 /* Now, iterate over the properties and do each of them. */
10073 for (chain = property_list; chain; chain = TREE_CHAIN (chain))
10075 objc_add_dynamic_declaration_for_property (location, interface, TREE_VALUE (chain));
10079 /* Main routine to generate code/data for all the property information for
10080 current implementation (class or category). CLASS is the interface where
10081 ivars are declared. CLASS_METHODS is where methods are found which
10082 could be a class or a category depending on whether we are implementing
10083 property of a class or a category. */
10086 objc_gen_property_data (tree klass, tree class_methods)
10090 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
10092 /* @dynamic property - nothing to check or synthesize. */
10093 if (PROPERTY_DYNAMIC (x))
10096 /* @synthesize property - need to synthesize the accessors. */
10097 if (PROPERTY_IVAR_NAME (x))
10099 objc_synthesize_getter (klass, class_methods, x);
10101 if (PROPERTY_READONLY (x) == 0)
10102 objc_synthesize_setter (klass, class_methods, x);
10107 gcc_unreachable ();
10111 /* This is called once we see the "@end" in an interface/implementation. */
10114 finish_class (tree klass)
10116 switch (TREE_CODE (klass))
10118 case CLASS_IMPLEMENTATION_TYPE:
10120 /* All code generation is done in finish_objc. */
10122 /* Generate what needed for property; setters, getters, etc. */
10123 objc_gen_property_data (implementation_template, implementation_template);
10125 if (implementation_template != objc_implementation_context)
10127 /* Ensure that all method listed in the interface contain bodies. */
10128 check_methods (CLASS_CLS_METHODS (implementation_template),
10129 objc_implementation_context, '+');
10130 check_methods (CLASS_NST_METHODS (implementation_template),
10131 objc_implementation_context, '-');
10133 if (CLASS_PROTOCOL_LIST (implementation_template))
10134 check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
10136 CLASS_NAME (objc_implementation_context));
10140 case CATEGORY_IMPLEMENTATION_TYPE:
10142 tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass));
10146 /* Generate what needed for property; setters, getters, etc. */
10147 objc_gen_property_data (implementation_template, category);
10149 /* Ensure all method listed in the interface contain bodies. */
10150 check_methods (CLASS_CLS_METHODS (category),
10151 objc_implementation_context, '+');
10152 check_methods (CLASS_NST_METHODS (category),
10153 objc_implementation_context, '-');
10155 if (CLASS_PROTOCOL_LIST (category))
10156 check_protocols (CLASS_PROTOCOL_LIST (category),
10158 CLASS_SUPER_NAME (objc_implementation_context));
10162 case CLASS_INTERFACE_TYPE:
10163 case CATEGORY_INTERFACE_TYPE:
10164 case PROTOCOL_INTERFACE_TYPE:
10166 /* Process properties of the class. */
10168 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
10170 /* Now we check that the appropriate getter is declared,
10171 and if not, we declare one ourselves. */
10172 tree getter_decl = lookup_method (CLASS_NST_METHODS (klass),
10173 PROPERTY_GETTER_NAME (x));
10177 /* TODO: Check that the declaration is consistent with the property. */
10182 /* Generate an instance method declaration for the
10183 getter; for example "- (id) name;". In general it
10184 will be of the form
10185 -(type)property_getter_name; */
10186 tree rettype = build_tree_list (NULL_TREE, TREE_TYPE (x));
10187 getter_decl = build_method_decl (INSTANCE_METHOD_DECL,
10188 rettype, PROPERTY_GETTER_NAME (x),
10190 objc_add_method (objc_interface_context, getter_decl, false, false);
10191 METHOD_PROPERTY_CONTEXT (getter_decl) = x;
10194 if (PROPERTY_READONLY (x) == 0)
10196 /* Now we check that the appropriate setter is declared,
10197 and if not, we declare on ourselves. */
10198 tree setter_decl = lookup_method (CLASS_NST_METHODS (klass),
10199 PROPERTY_SETTER_NAME (x));
10203 /* TODO: Check that the declaration is consistent with the property. */
10208 /* The setter name is something like 'setName:'.
10209 We need the substring 'setName' to build the
10210 method declaration due to how the declaration
10211 works. TODO: build_method_decl() will then
10212 generate back 'setName:' from 'setName'; it
10213 would be more efficient to hook into there. */
10214 const char *full_setter_name = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x));
10215 size_t length = strlen (full_setter_name);
10216 char *setter_name = (char *) alloca (length);
10217 tree ret_type, selector, arg_type, arg_name;
10219 strcpy (setter_name, full_setter_name);
10220 setter_name[length - 1] = '\0';
10221 ret_type = build_tree_list (NULL_TREE, void_type_node);
10222 arg_type = build_tree_list (NULL_TREE, TREE_TYPE (x));
10223 arg_name = get_identifier ("_value");
10224 selector = objc_build_keyword_decl (get_identifier (setter_name),
10225 arg_type, arg_name, NULL);
10226 setter_decl = build_method_decl (INSTANCE_METHOD_DECL,
10227 ret_type, selector,
10228 build_tree_list (NULL_TREE, NULL_TREE),
10230 objc_add_method (objc_interface_context, setter_decl, false, false);
10231 METHOD_PROPERTY_CONTEXT (setter_decl) = x;
10238 gcc_unreachable ();
10244 add_protocol (tree protocol)
10246 /* Put protocol on list in reverse order. */
10247 TREE_CHAIN (protocol) = protocol_chain;
10248 protocol_chain = protocol;
10249 return protocol_chain;
10253 lookup_protocol (tree ident)
10257 for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
10258 if (ident == PROTOCOL_NAME (chain))
10264 /* This function forward declares the protocols named by NAMES. If
10265 they are already declared or defined, the function has no effect. */
10268 objc_declare_protocols (tree names)
10273 if (current_namespace != global_namespace) {
10274 error ("Objective-C declarations may only appear in global scope");
10276 #endif /* OBJCPLUS */
10278 for (list = names; list; list = TREE_CHAIN (list))
10280 tree name = TREE_VALUE (list);
10282 if (lookup_protocol (name) == NULL_TREE)
10284 tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
10286 TYPE_LANG_SLOT_1 (protocol)
10287 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
10288 PROTOCOL_NAME (protocol) = name;
10289 PROTOCOL_LIST (protocol) = NULL_TREE;
10290 add_protocol (protocol);
10291 PROTOCOL_DEFINED (protocol) = 0;
10292 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
10298 start_protocol (enum tree_code code, tree name, tree list)
10303 if (current_namespace != global_namespace) {
10304 error ("Objective-C declarations may only appear in global scope");
10306 #endif /* OBJCPLUS */
10308 protocol = lookup_protocol (name);
10312 protocol = make_node (code);
10313 TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
10315 PROTOCOL_NAME (protocol) = name;
10316 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list);
10317 add_protocol (protocol);
10318 PROTOCOL_DEFINED (protocol) = 1;
10319 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
10321 check_protocol_recursively (protocol, list);
10323 else if (! PROTOCOL_DEFINED (protocol))
10325 PROTOCOL_DEFINED (protocol) = 1;
10326 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list);
10328 check_protocol_recursively (protocol, list);
10332 warning (0, "duplicate declaration for protocol %qE",
10339 /* "Encode" a data type into a string, which grows in util_obstack.
10341 The format is described in gcc/doc/objc.texi, section 'Type
10344 Most of the encode_xxx functions have a 'type' argument, which is
10345 the type to encode, and an integer 'curtype' argument, which is the
10346 index in the encoding string of the beginning of the encoding of
10347 the current type, and allows you to find what characters have
10348 already been written for the current type (they are the ones in the
10349 current encoding string starting from 'curtype').
10351 For example, if we are encoding a method which returns 'int' and
10352 takes a 'char **' argument, then when we get to the point of
10353 encoding the 'char **' argument, the encoded string already
10354 contains 'i12@0:4' (assuming a pointer size of 4 bytes). So,
10355 'curtype' will be set to 7 when starting to encode 'char **'.
10356 During the whole of the encoding of 'char **', 'curtype' will be
10357 fixed at 7, so the routine encoding the second pointer can find out
10358 that it's actually encoding a pointer to a pointer by looking
10359 backwards at what has already been encoded for the current type,
10360 and seeing there is a "^" (meaning a pointer) in there.
10364 /* Encode type qualifiers encodes one of the "PQ" Objective-C
10365 keywords, ie 'in', 'out', 'inout', 'bycopy', 'byref', 'oneway'.
10366 'const', instead, is encoded directly as part of the type.
10370 encode_type_qualifiers (tree declspecs)
10374 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10376 /* FIXME: Shouldn't we use token->keyword here ? */
10377 if (ridpointers[(int) RID_IN] == TREE_VALUE (spec))
10378 obstack_1grow (&util_obstack, 'n');
10379 else if (ridpointers[(int) RID_INOUT] == TREE_VALUE (spec))
10380 obstack_1grow (&util_obstack, 'N');
10381 else if (ridpointers[(int) RID_OUT] == TREE_VALUE (spec))
10382 obstack_1grow (&util_obstack, 'o');
10383 else if (ridpointers[(int) RID_BYCOPY] == TREE_VALUE (spec))
10384 obstack_1grow (&util_obstack, 'O');
10385 else if (ridpointers[(int) RID_BYREF] == TREE_VALUE (spec))
10386 obstack_1grow (&util_obstack, 'R');
10387 else if (ridpointers[(int) RID_ONEWAY] == TREE_VALUE (spec))
10388 obstack_1grow (&util_obstack, 'V');
10390 gcc_unreachable ();
10394 /* Determine if a pointee is marked read-only. Only used by the NeXT
10395 runtime to be compatible with gcc-3.3. */
10398 pointee_is_readonly (tree pointee)
10400 while (POINTER_TYPE_P (pointee))
10401 pointee = TREE_TYPE (pointee);
10403 return TYPE_READONLY (pointee);
10406 /* Encode a pointer type. */
10409 encode_pointer (tree type, int curtype, int format)
10411 tree pointer_to = TREE_TYPE (type);
10413 if (flag_next_runtime)
10415 /* This code is used to be compatible with gcc-3.3. */
10416 /* For historical/compatibility reasons, the read-only qualifier
10417 of the pointee gets emitted _before_ the '^'. The read-only
10418 qualifier of the pointer itself gets ignored, _unless_ we are
10419 looking at a typedef! Also, do not emit the 'r' for anything
10420 but the outermost type! */
10421 if (!generating_instance_variables
10422 && (obstack_object_size (&util_obstack) - curtype <= 1)
10423 && (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10424 ? TYPE_READONLY (type)
10425 : pointee_is_readonly (pointer_to)))
10426 obstack_1grow (&util_obstack, 'r');
10429 if (TREE_CODE (pointer_to) == RECORD_TYPE)
10431 if (OBJC_TYPE_NAME (pointer_to)
10432 && TREE_CODE (OBJC_TYPE_NAME (pointer_to)) == IDENTIFIER_NODE)
10434 const char *name = IDENTIFIER_POINTER (OBJC_TYPE_NAME (pointer_to));
10436 if (strcmp (name, TAG_OBJECT) == 0) /* '@' */
10438 obstack_1grow (&util_obstack, '@');
10441 else if (TYPE_HAS_OBJC_INFO (pointer_to)
10442 && TYPE_OBJC_INTERFACE (pointer_to))
10444 if (generating_instance_variables)
10446 obstack_1grow (&util_obstack, '@');
10447 obstack_1grow (&util_obstack, '"');
10448 obstack_grow (&util_obstack, name, strlen (name));
10449 obstack_1grow (&util_obstack, '"');
10454 obstack_1grow (&util_obstack, '@');
10458 else if (strcmp (name, TAG_CLASS) == 0) /* '#' */
10460 obstack_1grow (&util_obstack, '#');
10463 else if (strcmp (name, TAG_SELECTOR) == 0) /* ':' */
10465 obstack_1grow (&util_obstack, ':');
10470 else if (TREE_CODE (pointer_to) == INTEGER_TYPE
10471 && TYPE_MODE (pointer_to) == QImode)
10473 tree pname = TREE_CODE (OBJC_TYPE_NAME (pointer_to)) == IDENTIFIER_NODE
10474 ? OBJC_TYPE_NAME (pointer_to)
10475 : DECL_NAME (OBJC_TYPE_NAME (pointer_to));
10477 /* (BOOL *) are an exception and are encoded as ^c, while all
10478 other pointers to char are encoded as *. */
10479 if (strcmp (IDENTIFIER_POINTER (pname), "BOOL"))
10481 if (!flag_next_runtime)
10483 /* The NeXT runtime adds the 'r' before getting here. */
10485 /* It appears that "r*" means "const char *" rather than
10486 "char *const". "char *const" is encoded as "*",
10487 which is identical to "char *", so the "const" is
10488 unfortunately lost. */
10489 if (TYPE_READONLY (pointer_to))
10490 obstack_1grow (&util_obstack, 'r');
10493 obstack_1grow (&util_obstack, '*');
10498 /* We have a normal pointer type that does not get special treatment. */
10499 obstack_1grow (&util_obstack, '^');
10500 encode_type (pointer_to, curtype, format);
10504 encode_array (tree type, int curtype, int format)
10506 tree an_int_cst = TYPE_SIZE (type);
10507 tree array_of = TREE_TYPE (type);
10510 if (an_int_cst == NULL)
10512 /* We are trying to encode an incomplete array. An incomplete
10513 array is forbidden as part of an instance variable. */
10514 if (generating_instance_variables)
10516 /* TODO: Detect this error earlier. */
10517 error ("instance variable has unknown size");
10521 /* So the only case in which an incomplete array could occur is
10522 if we are encoding the arguments or return value of a method.
10523 In that case, an incomplete array argument or return value
10524 (eg, -(void)display: (char[])string) is treated like a
10525 pointer because that is how the compiler does the function
10526 call. A special, more complicated case, is when the
10527 incomplete array is the last member of a struct (eg, if we
10528 are encoding "struct { unsigned long int a;double b[];}"),
10529 which is again part of a method argument/return value. In
10530 that case, we really need to communicate to the runtime that
10531 there is an incomplete array (not a pointer!) there. So, we
10532 detect that special case and encode it as a zero-length
10535 Try to detect that we are part of a struct. We do this by
10536 searching for '=' in the type encoding for the current type.
10537 NB: This hack assumes that you can't use '=' as part of a C
10541 char *enc = obstack_base (&util_obstack) + curtype;
10542 if (memchr (enc, '=',
10543 obstack_object_size (&util_obstack) - curtype) == NULL)
10545 /* We are not inside a struct. Encode the array as a
10547 encode_pointer (type, curtype, format);
10552 /* Else, we are in a struct, and we encode it as a zero-length
10554 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT)0);
10556 else if (TREE_INT_CST_LOW (TYPE_SIZE (array_of)) == 0)
10557 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT)0);
10559 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC,
10560 TREE_INT_CST_LOW (an_int_cst)
10561 / TREE_INT_CST_LOW (TYPE_SIZE (array_of)));
10563 obstack_grow (&util_obstack, buffer, strlen (buffer));
10564 encode_type (array_of, curtype, format);
10565 obstack_1grow (&util_obstack, ']');
10569 /* Encode a vector. The vector type is a GCC extension to C. */
10571 encode_vector (tree type, int curtype, int format)
10573 tree vector_of = TREE_TYPE (type);
10576 /* Vectors are like simple fixed-size arrays. */
10578 /* Output ![xx,yy,<code>] where xx is the vector_size, yy is the
10579 alignment of the vector, and <code> is the base type. Eg, int
10580 __attribute__ ((vector_size (16))) gets encoded as ![16,32,i]
10581 assuming that the alignment is 32 bytes. We include size and
10582 alignment in bytes so that the runtime does not have to have any
10583 knowledge of the actual types.
10585 sprintf (buffer, "![" HOST_WIDE_INT_PRINT_DEC ",%d",
10586 /* We want to compute the equivalent of sizeof (<vector>).
10587 Code inspired by c_sizeof_or_alignof_type. */
10588 ((TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type))
10589 / (TYPE_PRECISION (char_type_node) / BITS_PER_UNIT))),
10590 /* We want to compute the equivalent of __alignof__
10591 (<vector>). Code inspired by
10592 c_sizeof_or_alignof_type. */
10593 TYPE_ALIGN_UNIT (type));
10594 obstack_grow (&util_obstack, buffer, strlen (buffer));
10595 encode_type (vector_of, curtype, format);
10596 obstack_1grow (&util_obstack, ']');
10601 encode_aggregate_fields (tree type, bool pointed_to, int curtype, int format)
10603 tree field = TYPE_FIELDS (type);
10605 for (; field; field = DECL_CHAIN (field))
10608 /* C++ static members, and things that are not field at all,
10609 should not appear in the encoding. */
10610 if (TREE_CODE (field) != FIELD_DECL || TREE_STATIC (field))
10614 /* Recursively encode fields of embedded base classes. */
10615 if (DECL_ARTIFICIAL (field) && !DECL_NAME (field)
10616 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
10618 encode_aggregate_fields (TREE_TYPE (field),
10619 pointed_to, curtype, format);
10623 if (generating_instance_variables && !pointed_to)
10625 tree fname = DECL_NAME (field);
10627 obstack_1grow (&util_obstack, '"');
10629 if (fname && TREE_CODE (fname) == IDENTIFIER_NODE)
10630 obstack_grow (&util_obstack,
10631 IDENTIFIER_POINTER (fname),
10632 strlen (IDENTIFIER_POINTER (fname)));
10634 obstack_1grow (&util_obstack, '"');
10637 encode_field_decl (field, curtype, format);
10642 encode_aggregate_within (tree type, int curtype, int format, int left,
10646 /* NB: aggregates that are pointed to have slightly different encoding
10647 rules in that you never encode the names of instance variables. */
10648 int ob_size = obstack_object_size (&util_obstack);
10649 bool inline_contents = false;
10650 bool pointed_to = false;
10652 if (flag_next_runtime)
10654 if (ob_size > 0 && *(obstack_next_free (&util_obstack) - 1) == '^')
10657 if ((format == OBJC_ENCODE_INLINE_DEFS || generating_instance_variables)
10658 && (!pointed_to || ob_size - curtype == 1
10659 || (ob_size - curtype == 2
10660 && *(obstack_next_free (&util_obstack) - 2) == 'r')))
10661 inline_contents = true;
10665 /* c0 and c1 are the last two characters in the encoding of the
10666 current type; if the last two characters were '^' or '^r',
10667 then we are encoding an aggregate that is "pointed to". The
10668 comment above applies: in that case we should avoid encoding
10669 the names of instance variables.
10671 char c1 = ob_size > 1 ? *(obstack_next_free (&util_obstack) - 2) : 0;
10672 char c0 = ob_size > 0 ? *(obstack_next_free (&util_obstack) - 1) : 0;
10674 if (c0 == '^' || (c1 == '^' && c0 == 'r'))
10677 if (format == OBJC_ENCODE_INLINE_DEFS || generating_instance_variables)
10680 inline_contents = true;
10683 /* Note that the check (ob_size - curtype < 2) prevents
10684 infinite recursion when encoding a structure which is
10685 a linked list (eg, struct node { struct node *next;
10686 }). Each time we follow a pointer, we add one
10687 character to ob_size, and curtype is fixed, so after
10688 at most two pointers we stop inlining contents and
10691 The other case where we don't inline is "^r", which
10692 is a pointer to a constant struct.
10694 if ((ob_size - curtype <= 2) && !(c0 == 'r'))
10695 inline_contents = true;
10700 /* Traverse struct aliases; it is important to get the
10701 original struct and its tag name (if any). */
10702 type = TYPE_MAIN_VARIANT (type);
10703 name = OBJC_TYPE_NAME (type);
10704 /* Open parenth/bracket. */
10705 obstack_1grow (&util_obstack, left);
10707 /* Encode the struct/union tag name, or '?' if a tag was
10708 not provided. Typedef aliases do not qualify. */
10710 /* For compatibility with the NeXT runtime, ObjC++ encodes template
10711 args as a composite struct tag name. */
10712 if (name && TREE_CODE (name) == IDENTIFIER_NODE
10713 /* Did this struct have a tag? */
10714 && !TYPE_WAS_ANONYMOUS (type))
10715 obstack_grow (&util_obstack,
10716 decl_as_string (type, TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME),
10717 strlen (decl_as_string (type, TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME)));
10719 if (name && TREE_CODE (name) == IDENTIFIER_NODE)
10720 obstack_grow (&util_obstack,
10721 IDENTIFIER_POINTER (name),
10722 strlen (IDENTIFIER_POINTER (name)));
10725 obstack_1grow (&util_obstack, '?');
10727 /* Encode the types (and possibly names) of the inner fields,
10729 if (inline_contents)
10731 obstack_1grow (&util_obstack, '=');
10732 encode_aggregate_fields (type, pointed_to, curtype, format);
10734 /* Close parenth/bracket. */
10735 obstack_1grow (&util_obstack, right);
10738 /* Encode a bitfield NeXT-style (i.e., without a bit offset or the underlying
10742 encode_next_bitfield (int width)
10745 sprintf (buffer, "b%d", width);
10746 obstack_grow (&util_obstack, buffer, strlen (buffer));
10750 /* Encodes 'type', ignoring type qualifiers (which you should encode
10751 beforehand if needed) with the exception of 'const', which is
10752 encoded by encode_type. See above for the explanation of
10753 'curtype'. 'format' can be OBJC_ENCODE_INLINE_DEFS or
10754 OBJC_ENCODE_DONT_INLINE_DEFS.
10757 encode_type (tree type, int curtype, int format)
10759 enum tree_code code = TREE_CODE (type);
10761 /* Ignore type qualifiers other than 'const' when encoding a
10764 if (type == error_mark_node)
10767 if (!flag_next_runtime)
10769 if (TYPE_READONLY (type))
10770 obstack_1grow (&util_obstack, 'r');
10775 case ENUMERAL_TYPE:
10776 if (flag_next_runtime)
10778 /* Kludge for backwards-compatibility with gcc-3.3: enums
10779 are always encoded as 'i' no matter what type they
10780 actually are (!). */
10781 obstack_1grow (&util_obstack, 'i');
10784 /* Else, they are encoded exactly like the integer type that is
10785 used by the compiler to store them. */
10789 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
10791 case 8: c = TYPE_UNSIGNED (type) ? 'C' : 'c'; break;
10792 case 16: c = TYPE_UNSIGNED (type) ? 'S' : 's'; break;
10795 tree int_type = type;
10796 if (flag_next_runtime)
10798 /* Another legacy kludge for compatiblity with
10799 gcc-3.3: 32-bit longs are encoded as 'l' or 'L',
10800 but not always. For typedefs, we need to use 'i'
10801 or 'I' instead if encoding a struct field, or a
10803 int_type = ((!generating_instance_variables
10804 && (obstack_object_size (&util_obstack)
10805 == (unsigned) curtype))
10806 ? TYPE_MAIN_VARIANT (type)
10809 if (int_type == long_unsigned_type_node
10810 || int_type == long_integer_type_node)
10811 c = TYPE_UNSIGNED (type) ? 'L' : 'l';
10813 c = TYPE_UNSIGNED (type) ? 'I' : 'i';
10816 case 64: c = TYPE_UNSIGNED (type) ? 'Q' : 'q'; break;
10817 case 128: c = TYPE_UNSIGNED (type) ? 'T' : 't'; break;
10818 default: gcc_unreachable ();
10820 obstack_1grow (&util_obstack, c);
10826 /* Floating point types. */
10827 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
10829 case 32: c = 'f'; break;
10830 case 64: c = 'd'; break;
10832 case 128: c = 'D'; break;
10833 default: gcc_unreachable ();
10835 obstack_1grow (&util_obstack, c);
10839 obstack_1grow (&util_obstack, 'v');
10843 obstack_1grow (&util_obstack, 'B');
10847 encode_array (type, curtype, format);
10852 case REFERENCE_TYPE:
10854 encode_pointer (type, curtype, format);
10858 encode_aggregate_within (type, curtype, format, '{', '}');
10862 encode_aggregate_within (type, curtype, format, '(', ')');
10865 case FUNCTION_TYPE: /* '?' means an unknown type. */
10866 obstack_1grow (&util_obstack, '?');
10870 /* A complex is encoded as 'j' followed by the inner type (eg,
10871 "_Complex int" is encoded as 'ji'). */
10872 obstack_1grow (&util_obstack, 'j');
10873 encode_type (TREE_TYPE (type), curtype, format);
10877 encode_vector (type, curtype, format);
10881 warning (0, "unknown type %s found during Objective-C encoding",
10882 gen_type_name (type));
10883 obstack_1grow (&util_obstack, '?');
10887 if (flag_next_runtime)
10889 /* Super-kludge. Some ObjC qualifier and type combinations need
10890 to be rearranged for compatibility with gcc-3.3. */
10891 if (code == POINTER_TYPE && obstack_object_size (&util_obstack) >= 3)
10893 char *enc = obstack_base (&util_obstack) + curtype;
10895 /* Rewrite "in const" from "nr" to "rn". */
10896 if (curtype >= 1 && !strncmp (enc - 1, "nr", 2))
10897 strncpy (enc - 1, "rn", 2);
10903 encode_gnu_bitfield (int position, tree type, int size)
10905 enum tree_code code = TREE_CODE (type);
10907 char charType = '?';
10909 /* This code is only executed for the GNU runtime, so we can ignore
10910 the NeXT runtime kludge of always encoding enums as 'i' no matter
10911 what integers they actually are. */
10912 if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
10914 if (integer_zerop (TYPE_MIN_VALUE (type)))
10915 /* Unsigned integer types. */
10917 switch (TYPE_MODE (type))
10920 charType = 'C'; break;
10922 charType = 'S'; break;
10925 if (type == long_unsigned_type_node)
10932 charType = 'Q'; break;
10934 gcc_unreachable ();
10938 /* Signed integer types. */
10940 switch (TYPE_MODE (type))
10943 charType = 'c'; break;
10945 charType = 's'; break;
10948 if (type == long_integer_type_node)
10955 charType = 'q'; break;
10957 gcc_unreachable ();
10963 /* Do not do any encoding, produce an error and keep going. */
10964 error ("trying to encode non-integer type as a bitfield");
10968 sprintf (buffer, "b%d%c%d", position, charType, size);
10969 obstack_grow (&util_obstack, buffer, strlen (buffer));
10973 encode_field_decl (tree field_decl, int curtype, int format)
10976 /* C++ static members, and things that are not fields at all,
10977 should not appear in the encoding. */
10978 if (TREE_CODE (field_decl) != FIELD_DECL || TREE_STATIC (field_decl))
10982 /* Generate the bitfield typing information, if needed. Note the difference
10983 between GNU and NeXT runtimes. */
10984 if (DECL_BIT_FIELD_TYPE (field_decl))
10986 int size = tree_low_cst (DECL_SIZE (field_decl), 1);
10988 if (flag_next_runtime)
10989 encode_next_bitfield (size);
10991 encode_gnu_bitfield (int_bit_position (field_decl),
10992 DECL_BIT_FIELD_TYPE (field_decl), size);
10995 encode_type (TREE_TYPE (field_decl), curtype, format);
10998 /* Decay array and function parameters into pointers. */
11001 objc_decay_parm_type (tree type)
11003 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE)
11004 type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE
11011 static GTY(()) tree objc_parmlist = NULL_TREE;
11013 /* Append PARM to a list of formal parameters of a method, making a necessary
11014 array-to-pointer adjustment along the way. */
11017 objc_push_parm (tree parm)
11021 if (TREE_TYPE (parm) == error_mark_node)
11023 objc_parmlist = chainon (objc_parmlist, parm);
11027 /* Decay arrays and functions into pointers. */
11028 type = objc_decay_parm_type (TREE_TYPE (parm));
11030 /* If the parameter type has been decayed, a new PARM_DECL needs to be
11032 if (type != TREE_TYPE (parm))
11033 parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type);
11035 DECL_ARG_TYPE (parm)
11036 = lang_hooks.types.type_promotes_to (TREE_TYPE (parm));
11038 /* Record constancy and volatility. */
11039 c_apply_type_quals_to_decl
11040 ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0)
11041 | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0)
11042 | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm);
11044 objc_parmlist = chainon (objc_parmlist, parm);
11047 /* Retrieve the formal parameter list constructed via preceding calls to
11048 objc_push_parm(). */
11052 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED)
11054 static struct c_arg_info *
11055 objc_get_parm_info (int have_ellipsis)
11059 tree parm_info = objc_parmlist;
11060 objc_parmlist = NULL_TREE;
11064 tree parm_info = objc_parmlist;
11065 struct c_arg_info *arg_info;
11066 /* The C front-end requires an elaborate song and dance at
11069 declare_parm_level ();
11072 tree next = DECL_CHAIN (parm_info);
11074 DECL_CHAIN (parm_info) = NULL_TREE;
11075 parm_info = pushdecl (parm_info);
11076 finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
11079 arg_info = get_parm_info (have_ellipsis);
11081 objc_parmlist = NULL_TREE;
11086 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
11087 method definitions. In the case of instance methods, we can be more
11088 specific as to the type of 'self'. */
11091 synth_self_and_ucmd_args (void)
11095 if (objc_method_context
11096 && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL)
11097 self_type = objc_instance_type;
11099 /* Really a `struct objc_class *'. However, we allow people to
11100 assign to self, which changes its type midstream. */
11101 self_type = objc_object_type;
11104 objc_push_parm (build_decl (input_location,
11105 PARM_DECL, self_id, self_type));
11108 objc_push_parm (build_decl (input_location,
11109 PARM_DECL, ucmd_id, objc_selector_type));
11112 /* Transform an Objective-C method definition into a static C function
11113 definition, synthesizing the first two arguments, "self" and "_cmd",
11117 start_method_def (tree method)
11123 struct c_arg_info *parm_info;
11125 int have_ellipsis = 0;
11127 /* If we are defining a "dealloc" method in a non-root class, we
11128 will need to check if a [super dealloc] is missing, and warn if
11130 if(CLASS_SUPER_NAME (objc_implementation_context)
11131 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method))))
11132 should_call_super_dealloc = 1;
11134 should_call_super_dealloc = 0;
11136 /* Required to implement _msgSuper. */
11137 objc_method_context = method;
11138 UOBJC_SUPER_decl = NULL_TREE;
11140 /* Generate prototype declarations for arguments..."new-style". */
11141 synth_self_and_ucmd_args ();
11143 /* Generate argument declarations if a keyword_decl. */
11144 parmlist = METHOD_SEL_ARGS (method);
11147 /* parmlist is a KEYWORD_DECL. */
11148 tree type = TREE_VALUE (TREE_TYPE (parmlist));
11151 parm = build_decl (input_location,
11152 PARM_DECL, KEYWORD_ARG_NAME (parmlist), type);
11153 decl_attributes (&parm, DECL_ATTRIBUTES (parmlist), 0);
11154 objc_push_parm (parm);
11155 parmlist = DECL_CHAIN (parmlist);
11158 if (METHOD_ADD_ARGS (method))
11162 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method));
11163 akey; akey = TREE_CHAIN (akey))
11165 objc_push_parm (TREE_VALUE (akey));
11168 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
11172 parm_info = objc_get_parm_info (have_ellipsis);
11174 really_start_method (objc_method_context, parm_info);
11177 /* Return 1 if TYPE1 is equivalent to TYPE2
11178 for purposes of method overloading. */
11181 objc_types_are_equivalent (tree type1, tree type2)
11183 if (type1 == type2)
11186 /* Strip away indirections. */
11187 while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE)
11188 && (TREE_CODE (type1) == TREE_CODE (type2)))
11189 type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2);
11190 if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
11193 type1 = (TYPE_HAS_OBJC_INFO (type1)
11194 ? TYPE_OBJC_PROTOCOL_LIST (type1)
11196 type2 = (TYPE_HAS_OBJC_INFO (type2)
11197 ? TYPE_OBJC_PROTOCOL_LIST (type2)
11200 if (list_length (type1) == list_length (type2))
11202 for (; type2; type2 = TREE_CHAIN (type2))
11203 if (!lookup_protocol_in_reflist (type1, TREE_VALUE (type2)))
11210 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
11213 objc_types_share_size_and_alignment (tree type1, tree type2)
11215 return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
11216 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2));
11219 /* Return 1 if PROTO1 is equivalent to PROTO2
11220 for purposes of method overloading. Ordinarily, the type signatures
11221 should match up exactly, unless STRICT is zero, in which case we
11222 shall allow differences in which the size and alignment of a type
11226 comp_proto_with_proto (tree proto1, tree proto2, int strict)
11228 /* The following test is needed in case there are hashing
11230 if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2))
11233 return match_proto_with_proto (proto1, proto2, strict);
11237 match_proto_with_proto (tree proto1, tree proto2, int strict)
11241 /* Compare return types. */
11242 type1 = TREE_VALUE (TREE_TYPE (proto1));
11243 type2 = TREE_VALUE (TREE_TYPE (proto2));
11245 if (!objc_types_are_equivalent (type1, type2)
11246 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
11249 /* Compare argument types. */
11250 for (type1 = get_arg_type_list (proto1, METHOD_REF, 0),
11251 type2 = get_arg_type_list (proto2, METHOD_REF, 0);
11253 type1 = TREE_CHAIN (type1), type2 = TREE_CHAIN (type2))
11255 if (!objc_types_are_equivalent (TREE_VALUE (type1), TREE_VALUE (type2))
11257 || !objc_types_share_size_and_alignment (TREE_VALUE (type1),
11258 TREE_VALUE (type2))))
11262 return (!type1 && !type2);
11265 /* Fold an OBJ_TYPE_REF expression for ObjC method dispatches, where
11266 this occurs. ObjC method dispatches are _not_ like C++ virtual
11267 member function dispatches, and we account for the difference here. */
11270 objc_fold_obj_type_ref (tree ref, tree known_type)
11272 objc_fold_obj_type_ref (tree ref ATTRIBUTE_UNUSED,
11273 tree known_type ATTRIBUTE_UNUSED)
11277 tree v = BINFO_VIRTUALS (TYPE_BINFO (known_type));
11279 /* If the receiver does not have virtual member functions, there
11280 is nothing we can (or need to) do here. */
11284 /* Let C++ handle C++ virtual functions. */
11285 return cp_fold_obj_type_ref (ref, known_type);
11287 /* For plain ObjC, we currently do not need to do anything. */
11293 objc_start_function (tree name, tree type, tree attrs,
11297 struct c_arg_info *params
11301 tree fndecl = build_decl (input_location,
11302 FUNCTION_DECL, name, type);
11305 DECL_ARGUMENTS (fndecl) = params;
11306 DECL_INITIAL (fndecl) = error_mark_node;
11307 DECL_EXTERNAL (fndecl) = 0;
11308 TREE_STATIC (fndecl) = 1;
11309 retrofit_lang_decl (fndecl);
11310 cplus_decl_attributes (&fndecl, attrs, 0);
11311 start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT);
11313 current_function_returns_value = 0; /* Assume, until we see it does. */
11314 current_function_returns_null = 0;
11315 decl_attributes (&fndecl, attrs, 0);
11316 announce_function (fndecl);
11317 DECL_INITIAL (fndecl) = error_mark_node;
11318 DECL_EXTERNAL (fndecl) = 0;
11319 TREE_STATIC (fndecl) = 1;
11320 current_function_decl = pushdecl (fndecl);
11322 declare_parm_level ();
11323 DECL_RESULT (current_function_decl)
11324 = build_decl (input_location,
11325 RESULT_DECL, NULL_TREE,
11326 TREE_TYPE (TREE_TYPE (current_function_decl)));
11327 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1;
11328 DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1;
11329 start_fname_decls ();
11330 store_parm_decls_from (params);
11333 TREE_USED (current_function_decl) = 1;
11336 /* - Generate an identifier for the function. the format is "_n_cls",
11337 where 1 <= n <= nMethods, and cls is the name the implementation we
11339 - Install the return type from the method declaration.
11340 - If we have a prototype, check for type consistency. */
11343 really_start_method (tree method,
11347 struct c_arg_info *parmlist
11351 tree ret_type, meth_type;
11353 const char *sel_name, *class_name, *cat_name;
11356 /* Synth the storage class & assemble the return type. */
11357 ret_type = TREE_VALUE (TREE_TYPE (method));
11359 sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
11360 class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
11361 cat_name = ((TREE_CODE (objc_implementation_context)
11362 == CLASS_IMPLEMENTATION_TYPE)
11364 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context)));
11367 /* Make sure this is big enough for any plausible method label. */
11368 buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
11369 + (cat_name ? strlen (cat_name) : 0));
11371 OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
11372 class_name, cat_name, sel_name, method_slot);
11374 method_id = get_identifier (buf);
11377 /* Objective-C methods cannot be overloaded, so we don't need
11378 the type encoding appended. It looks bad anyway... */
11379 push_lang_context (lang_name_c);
11383 = build_function_type (ret_type,
11384 get_arg_type_list (method, METHOD_DEF, 0));
11385 objc_start_function (method_id, meth_type, NULL_TREE, parmlist);
11387 /* Set self_decl from the first argument. */
11388 self_decl = DECL_ARGUMENTS (current_function_decl);
11390 /* Suppress unused warnings. */
11391 TREE_USED (self_decl) = 1;
11392 DECL_READ_P (self_decl) = 1;
11393 TREE_USED (DECL_CHAIN (self_decl)) = 1;
11394 DECL_READ_P (DECL_CHAIN (self_decl)) = 1;
11396 pop_lang_context ();
11399 METHOD_DEFINITION (method) = current_function_decl;
11401 /* Check consistency...start_function, pushdecl, duplicate_decls. */
11403 if (implementation_template != objc_implementation_context)
11406 = lookup_method_static (implementation_template,
11407 METHOD_SEL_NAME (method),
11408 ((TREE_CODE (method) == CLASS_METHOD_DECL)
11409 | OBJC_LOOKUP_NO_SUPER));
11413 if (!comp_proto_with_proto (method, proto, 1))
11415 bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL;
11417 warning_at (DECL_SOURCE_LOCATION (method), 0,
11418 "conflicting types for %<%c%s%>",
11419 (type ? '-' : '+'),
11420 identifier_to_locale (gen_method_decl (method)));
11421 inform (DECL_SOURCE_LOCATION (proto),
11422 "previous declaration of %<%c%s%>",
11423 (type ? '-' : '+'),
11424 identifier_to_locale (gen_method_decl (proto)));
11428 /* If the method in the @interface was deprecated, mark
11429 the implemented method as deprecated too. It should
11430 never be used for messaging (when the deprecation
11431 warnings are produced), but just in case. */
11432 if (TREE_DEPRECATED (proto))
11433 TREE_DEPRECATED (method) = 1;
11435 /* If the method in the @interface was marked as
11436 'noreturn', mark the function implementing the method
11437 as 'noreturn' too. */
11438 TREE_THIS_VOLATILE (current_function_decl) = TREE_THIS_VOLATILE (proto);
11443 /* We have a method @implementation even though we did not
11444 see a corresponding @interface declaration (which is allowed
11445 by Objective-C rules). Go ahead and place the method in
11446 the @interface anyway, so that message dispatch lookups
11448 tree interface = implementation_template;
11450 if (TREE_CODE (objc_implementation_context)
11451 == CATEGORY_IMPLEMENTATION_TYPE)
11452 interface = lookup_category
11454 CLASS_SUPER_NAME (objc_implementation_context));
11457 objc_add_method (interface, copy_node (method),
11458 TREE_CODE (method) == CLASS_METHOD_DECL,
11459 /* is_optional= */ false);
11464 static void *UOBJC_SUPER_scope = 0;
11466 /* _n_Method (id self, SEL sel, ...)
11468 struct objc_super _S;
11469 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
11473 get_super_receiver (void)
11475 if (objc_method_context)
11477 tree super_expr, super_expr_list;
11479 if (!UOBJC_SUPER_decl)
11481 UOBJC_SUPER_decl = build_decl (input_location,
11482 VAR_DECL, get_identifier (TAG_SUPER),
11483 objc_super_template);
11484 /* This prevents `unused variable' warnings when compiling with -Wall. */
11485 TREE_USED (UOBJC_SUPER_decl) = 1;
11486 DECL_READ_P (UOBJC_SUPER_decl) = 1;
11487 lang_hooks.decls.pushdecl (UOBJC_SUPER_decl);
11488 finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE,
11490 UOBJC_SUPER_scope = objc_get_current_scope ();
11493 /* Set receiver to self. */
11494 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id);
11495 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
11496 NOP_EXPR, input_location, self_decl,
11498 super_expr_list = super_expr;
11500 /* Set class to begin searching. */
11501 super_expr = objc_build_component_ref (UOBJC_SUPER_decl,
11502 get_identifier ("super_class"));
11504 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
11506 /* [_cls, __cls]Super are "pre-built" in
11507 synth_forward_declarations. */
11509 super_expr = build_modify_expr (input_location, super_expr,
11510 NULL_TREE, NOP_EXPR,
11512 ((TREE_CODE (objc_method_context)
11513 == INSTANCE_METHOD_DECL)
11515 : uucls_super_ref),
11520 /* We have a category. */
11522 tree super_name = CLASS_SUPER_NAME (implementation_template);
11525 /* Barf if super used in a category of Object. */
11528 error ("no super class declared in interface for %qE",
11529 CLASS_NAME (implementation_template));
11530 return error_mark_node;
11533 if (flag_next_runtime && !flag_zero_link)
11535 super_class = objc_get_class_reference (super_name);
11536 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
11537 /* If we are in a class method, we must retrieve the
11538 _metaclass_ for the current class, pointed at by
11539 the class's "isa" pointer. The following assumes that
11540 "isa" is the first ivar in a class (which it must be). */
11542 = build_indirect_ref
11544 build_c_cast (input_location,
11545 build_pointer_type (objc_class_type),
11546 super_class), RO_UNARY_STAR);
11550 add_class_reference (super_name);
11551 super_class = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
11552 ? objc_get_class_decl : objc_get_meta_class_decl);
11553 assemble_external (super_class);
11555 = build_function_call
11560 my_build_string_pointer
11561 (IDENTIFIER_LENGTH (super_name) + 1,
11562 IDENTIFIER_POINTER (super_name))));
11566 = build_modify_expr (input_location, super_expr, NULL_TREE,
11569 build_c_cast (input_location,
11570 TREE_TYPE (super_expr),
11575 super_expr_list = build_compound_expr (input_location,
11576 super_expr_list, super_expr);
11578 super_expr = build_unary_op (input_location,
11579 ADDR_EXPR, UOBJC_SUPER_decl, 0);
11580 super_expr_list = build_compound_expr (input_location,
11581 super_expr_list, super_expr);
11583 return super_expr_list;
11587 error ("[super ...] must appear in a method context");
11588 return error_mark_node;
11592 /* When exiting a scope, sever links to a 'super' declaration (if any)
11593 therein contained. */
11596 objc_clear_super_receiver (void)
11598 if (objc_method_context
11599 && UOBJC_SUPER_scope == objc_get_current_scope ()) {
11600 UOBJC_SUPER_decl = 0;
11601 UOBJC_SUPER_scope = 0;
11606 objc_finish_method_definition (tree fndecl)
11608 /* We cannot validly inline ObjC methods, at least not without a language
11609 extension to declare that a method need not be dynamically
11610 dispatched, so suppress all thoughts of doing so. */
11611 DECL_UNINLINABLE (fndecl) = 1;
11614 /* The C++ front-end will have called finish_function() for us. */
11615 finish_function ();
11618 METHOD_ENCODING (objc_method_context)
11619 = encode_method_prototype (objc_method_context);
11621 /* Required to implement _msgSuper. This must be done AFTER finish_function,
11622 since the optimizer may find "may be used before set" errors. */
11623 objc_method_context = NULL_TREE;
11625 if (should_call_super_dealloc)
11626 warning (0, "method possibly missing a [super dealloc] call");
11629 /* Given a tree DECL node, produce a printable description of it in the given
11630 buffer, overwriting the buffer. */
11633 gen_declaration (tree decl)
11639 gen_type_name_0 (TREE_TYPE (decl));
11641 if (DECL_NAME (decl))
11643 if (!POINTER_TYPE_P (TREE_TYPE (decl)))
11644 strcat (errbuf, " ");
11646 strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
11649 if (DECL_INITIAL (decl)
11650 && TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST)
11651 sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
11652 TREE_INT_CST_LOW (DECL_INITIAL (decl)));
11658 /* Given a tree TYPE node, produce a printable description of it in the given
11659 buffer, overwriting the buffer. */
11662 gen_type_name_0 (tree type)
11664 tree orig = type, proto;
11666 if (TYPE_P (type) && TYPE_NAME (type))
11667 type = TYPE_NAME (type);
11668 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
11670 tree inner = TREE_TYPE (type);
11672 while (TREE_CODE (inner) == ARRAY_TYPE)
11673 inner = TREE_TYPE (inner);
11675 gen_type_name_0 (inner);
11677 if (!POINTER_TYPE_P (inner))
11678 strcat (errbuf, " ");
11680 if (POINTER_TYPE_P (type))
11681 strcat (errbuf, "*");
11683 while (type != inner)
11685 strcat (errbuf, "[");
11687 if (TYPE_DOMAIN (type))
11691 sprintf (sz, HOST_WIDE_INT_PRINT_DEC,
11693 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1));
11694 strcat (errbuf, sz);
11697 strcat (errbuf, "]");
11698 type = TREE_TYPE (type);
11701 goto exit_function;
11704 if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type))
11705 type = DECL_NAME (type);
11707 strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE
11708 ? IDENTIFIER_POINTER (type)
11711 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
11712 if (objc_is_id (orig))
11713 orig = TREE_TYPE (orig);
11715 proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE;
11719 strcat (errbuf, " <");
11723 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto))));
11724 proto = TREE_CHAIN (proto);
11725 strcat (errbuf, proto ? ", " : ">");
11734 gen_type_name (tree type)
11738 return gen_type_name_0 (type);
11741 /* Given a method tree, put a printable description into the given
11742 buffer (overwriting) and return a pointer to the buffer. */
11745 gen_method_decl (tree method)
11749 strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */
11750 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method)));
11751 strcat (errbuf, ")");
11752 chain = METHOD_SEL_ARGS (method);
11756 /* We have a chain of keyword_decls. */
11759 if (KEYWORD_KEY_NAME (chain))
11760 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
11762 strcat (errbuf, ":(");
11763 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain)));
11764 strcat (errbuf, ")");
11766 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
11767 if ((chain = DECL_CHAIN (chain)))
11768 strcat (errbuf, " ");
11772 if (METHOD_ADD_ARGS (method))
11774 chain = TREE_CHAIN (METHOD_ADD_ARGS (method));
11776 /* Know we have a chain of parm_decls. */
11779 strcat (errbuf, ", ");
11780 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain)));
11781 chain = TREE_CHAIN (chain);
11784 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
11785 strcat (errbuf, ", ...");
11790 /* We have a unary selector. */
11791 strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
11799 /* Dump an @interface declaration of the supplied class CHAIN to the
11800 supplied file FP. Used to implement the -gen-decls option (which
11801 prints out an @interface declaration of all classes compiled in
11802 this run); potentially useful for debugging the compiler too. */
11804 dump_interface (FILE *fp, tree chain)
11806 /* FIXME: A heap overflow here whenever a method (or ivar)
11807 declaration is so long that it doesn't fit in the buffer. The
11808 code and all the related functions should be rewritten to avoid
11809 using fixed size buffers. */
11810 const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
11811 tree ivar_decls = CLASS_RAW_IVARS (chain);
11812 tree nst_methods = CLASS_NST_METHODS (chain);
11813 tree cls_methods = CLASS_CLS_METHODS (chain);
11815 fprintf (fp, "\n@interface %s", my_name);
11817 /* CLASS_SUPER_NAME is used to store the superclass name for
11818 classes, and the category name for categories. */
11819 if (CLASS_SUPER_NAME (chain))
11821 const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
11823 switch (TREE_CODE (chain))
11825 case CATEGORY_IMPLEMENTATION_TYPE:
11826 case CATEGORY_INTERFACE_TYPE:
11827 fprintf (fp, " (%s)\n", name);
11830 fprintf (fp, " : %s\n", name);
11835 fprintf (fp, "\n");
11837 /* FIXME - the following doesn't seem to work at the moment. */
11840 fprintf (fp, "{\n");
11843 fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls));
11844 ivar_decls = TREE_CHAIN (ivar_decls);
11846 while (ivar_decls);
11847 fprintf (fp, "}\n");
11850 while (nst_methods)
11852 fprintf (fp, "- %s;\n", gen_method_decl (nst_methods));
11853 nst_methods = TREE_CHAIN (nst_methods);
11856 while (cls_methods)
11858 fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods));
11859 cls_methods = TREE_CHAIN (cls_methods);
11862 fprintf (fp, "@end\n");
11866 /* Produce the pretty printing for an Objective-C method. This is
11867 currently unused, but could be handy while reorganizing the pretty
11868 printing to be more robust. */
11869 static const char *
11870 objc_pretty_print_method (bool is_class_method,
11871 const char *class_name,
11872 const char *category_name,
11873 const char *selector)
11877 char *result = XNEWVEC (char, strlen (class_name) + strlen (category_name)
11878 + strlen (selector) + 7);
11880 if (is_class_method)
11881 sprintf (result, "+[%s(%s) %s]", class_name, category_name, selector);
11883 sprintf (result, "-[%s(%s) %s]", class_name, category_name, selector);
11889 char *result = XNEWVEC (char, strlen (class_name)
11890 + strlen (selector) + 5);
11892 if (is_class_method)
11893 sprintf (result, "+[%s %s]", class_name, selector);
11895 sprintf (result, "-[%s %s]", class_name, selector);
11902 /* Demangle function for Objective-C. Attempt to demangle the
11903 function name associated with a method (eg, going from
11904 "_i_NSObject__class" to "-[NSObject class]"); usually for the
11905 purpose of pretty printing or error messages. Return the demangled
11906 name, or NULL if the string is not an Objective-C mangled method
11909 Because of how the mangling is done, any method that has a '_' in
11910 its original name is at risk of being demangled incorrectly. In
11911 some cases there are multiple valid ways to demangle a method name
11912 and there is no way we can decide.
11914 TODO: objc_demangle() can't always get it right; the right way to
11915 get this correct for all method names would be to store the
11916 Objective-C method name somewhere in the function decl. Then,
11917 there is no demangling to do; we'd just pull the method name out of
11918 the decl. As an additional bonus, when printing error messages we
11919 could check for such a method name, and if we find it, we know the
11920 function is actually an Objective-C method and we could print error
11921 messages saying "In method '+[NSObject class]" instead of "In
11922 function '+[NSObject class]" as we do now. */
11923 static const char *
11924 objc_demangle (const char *mangled)
11926 char *demangled, *cp;
11928 if (mangled[0] == '_' &&
11929 (mangled[1] == 'i' || mangled[1] == 'c') &&
11932 cp = demangled = XNEWVEC (char, strlen(mangled) + 2);
11933 if (mangled[1] == 'i')
11934 *cp++ = '-'; /* for instance method */
11936 *cp++ = '+'; /* for class method */
11937 *cp++ = '['; /* opening left brace */
11938 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
11939 while (*cp && *cp == '_')
11940 cp++; /* skip any initial underbars in class name */
11941 cp = strchr(cp, '_'); /* find first non-initial underbar */
11944 free(demangled); /* not mangled name */
11947 if (cp[1] == '_') /* easy case: no category name */
11949 *cp++ = ' '; /* replace two '_' with one ' ' */
11950 strcpy(cp, mangled + (cp - demangled) + 2);
11954 *cp++ = '('; /* less easy case: category name */
11955 cp = strchr(cp, '_');
11958 free(demangled); /* not mangled name */
11962 *cp++ = ' '; /* overwriting 1st char of method name... */
11963 strcpy(cp, mangled + (cp - demangled)); /* get it back */
11965 /* Now we have the method name. We need to generally replace
11966 '_' with ':' but trying to preserve '_' if it could only have
11967 been in the mangled string because it was already in the
11968 original name. In cases where it's ambiguous, we assume that
11969 any '_' originated from a ':'. */
11971 /* Initial '_'s in method name can't have been generating by
11972 converting ':'s. Skip them. */
11973 while (*cp && *cp == '_')
11976 /* If the method name does not end with '_', then it has no
11977 arguments and there was no replacement of ':'s with '_'s
11978 during mangling. Check for that case, and skip any
11979 replacement if so. This at least guarantees that methods
11980 with no arguments are always demangled correctly (unless the
11981 original name ends with '_'). */
11982 if (*(mangled + strlen (mangled) - 1) != '_')
11984 /* Skip to the end. */
11990 /* Replace remaining '_' with ':'. This may get it wrong if
11991 there were '_'s in the original name. In most cases it
11992 is impossible to disambiguate. */
11997 *cp++ = ']'; /* closing right brace */
11998 *cp++ = 0; /* string terminator */
12002 return NULL; /* not an objc mangled name */
12005 /* Try to pretty-print a decl. If the 'decl' is an Objective-C
12006 specific decl, return the printable name for it. If not, return
12009 objc_maybe_printable_name (tree decl, int v ATTRIBUTE_UNUSED)
12011 switch (TREE_CODE (decl))
12013 case FUNCTION_DECL:
12014 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)));
12017 /* The following happens when we are printing a deprecation
12018 warning for a method. The warn_deprecation() will end up
12019 trying to print the decl for INSTANCE_METHOD_DECL or
12020 CLASS_METHOD_DECL. It would be nice to be able to print
12021 "-[NSObject autorelease] is deprecated", but to do that, we'd
12022 need to store the class and method name in the method decl,
12023 which we currently don't do. For now, just return the name
12024 of the method. We don't return NULL, because that may
12025 trigger further attempts to pretty-print the decl in C/C++,
12026 but they wouldn't know how to pretty-print it. */
12027 case INSTANCE_METHOD_DECL:
12028 case CLASS_METHOD_DECL:
12029 return IDENTIFIER_POINTER (DECL_NAME (decl));
12031 /* This happens when printing a deprecation warning for a
12032 property. We may want to consider some sort of pretty
12033 printing (eg, include the class name where it was declared
12035 case PROPERTY_DECL:
12036 return IDENTIFIER_POINTER (PROPERTY_NAME (decl));
12044 /* Return a printable name for 'decl'. This first tries
12045 objc_maybe_printable_name(), and if that fails, it returns the name
12046 in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for
12047 Objective-C; in Objective-C++, setting the hook is not enough
12048 because lots of C++ Front-End code calls cxx_printable_name,
12049 dump_decl and other C++ functions directly. So instead we have
12050 modified dump_decl to call objc_maybe_printable_name directly. */
12052 objc_printable_name (tree decl, int v)
12054 const char *demangled_name = objc_maybe_printable_name (decl, v);
12056 if (demangled_name != NULL)
12057 return demangled_name;
12059 return IDENTIFIER_POINTER (DECL_NAME (decl));
12065 gcc_obstack_init (&util_obstack);
12066 util_firstobj = (char *) obstack_finish (&util_obstack);
12068 errbuf = XNEWVEC (char, 1024 * 10);
12070 synth_module_prologue ();
12076 struct imp_entry *impent;
12078 /* The internally generated initializers appear to have missing braces.
12079 Don't warn about this. */
12080 int save_warn_missing_braces = warn_missing_braces;
12081 warn_missing_braces = 0;
12083 /* A missing @end may not be detected by the parser. */
12084 if (objc_implementation_context)
12086 warning (0, "%<@end%> missing in implementation context");
12087 finish_class (objc_implementation_context);
12088 objc_ivar_chain = NULL_TREE;
12089 objc_implementation_context = NULL_TREE;
12092 /* Process the static instances here because initialization of objc_symtab
12093 depends on them. */
12094 if (objc_static_instances)
12095 generate_static_references ();
12097 /* forward declare categories */
12099 forward_declare_categories ();
12101 for (impent = imp_list; impent; impent = impent->next)
12103 objc_implementation_context = impent->imp_context;
12104 implementation_template = impent->imp_template;
12106 /* FIXME: This needs reworking to be more obvious. */
12108 UOBJC_CLASS_decl = impent->class_decl;
12109 UOBJC_METACLASS_decl = impent->meta_decl;
12111 /* Dump the @interface of each class as we compile it, if the
12112 -gen-decls option is in use. TODO: Dump the classes in the
12113 order they were found, rather than in reverse order as we
12115 if (flag_gen_declaration)
12117 dump_interface (gen_declaration_file, objc_implementation_context);
12120 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
12122 /* all of the following reference the string pool... */
12123 generate_ivar_lists ();
12124 generate_dispatch_tables ();
12125 generate_shared_structures (impent);
12129 generate_dispatch_tables ();
12130 generate_category (impent);
12133 impent->class_decl = UOBJC_CLASS_decl;
12134 impent->meta_decl = UOBJC_METACLASS_decl;
12137 /* If we are using an array of selectors, we must always
12138 finish up the array decl even if no selectors were used. */
12139 if (flag_next_runtime)
12140 build_next_selector_translation_table ();
12142 build_gnu_selector_translation_table ();
12144 if (protocol_chain)
12145 generate_protocols ();
12147 if (flag_next_runtime)
12148 generate_objc_image_info ();
12150 if (imp_list || class_names_chain
12151 || meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
12152 generate_objc_symtab_decl ();
12154 /* Arrange for ObjC data structures to be initialized at run time. */
12155 if (objc_implementation_context || class_names_chain || objc_static_instances
12156 || meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
12158 build_module_descriptor ();
12160 if (!flag_next_runtime)
12161 build_module_initializer_routine ();
12164 /* Dump the class references. This forces the appropriate classes
12165 to be linked into the executable image, preserving unix archive
12166 semantics. This can be removed when we move to a more dynamically
12167 linked environment. */
12169 for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
12171 handle_class_ref (chain);
12172 if (TREE_PURPOSE (chain))
12173 generate_classref_translation_entry (chain);
12176 for (impent = imp_list; impent; impent = impent->next)
12177 handle_impent (impent);
12184 /* Run through the selector hash tables and print a warning for any
12185 selector which has multiple methods. */
12187 for (slot = 0; slot < SIZEHASHTABLE; slot++)
12189 for (hsh = cls_method_hash_list[slot]; hsh; hsh = hsh->next)
12190 check_duplicates (hsh, 0, 1);
12191 for (hsh = nst_method_hash_list[slot]; hsh; hsh = hsh->next)
12192 check_duplicates (hsh, 0, 1);
12196 warn_missing_braces = save_warn_missing_braces;
12199 /* Subroutines of finish_objc. */
12202 generate_classref_translation_entry (tree chain)
12204 tree expr, decl, type;
12206 decl = TREE_PURPOSE (chain);
12207 type = TREE_TYPE (decl);
12209 expr = add_objc_string (TREE_VALUE (chain), class_names);
12210 expr = convert (type, expr); /* cast! */
12212 /* This is a class reference. It is re-written by the runtime,
12213 but will be optimized away unless we force it. */
12214 DECL_PRESERVE_P (decl) = 1;
12215 finish_var_decl (decl, expr);
12220 handle_class_ref (tree chain)
12222 const char *name = IDENTIFIER_POINTER (TREE_VALUE (chain));
12223 char *string = (char *) alloca (strlen (name) + 30);
12227 sprintf (string, "%sobjc_class_name_%s",
12228 (flag_next_runtime ? "." : "__"), name);
12230 #ifdef ASM_DECLARE_UNRESOLVED_REFERENCE
12231 if (flag_next_runtime)
12233 ASM_DECLARE_UNRESOLVED_REFERENCE (asm_out_file, string);
12238 /* Make a decl for this name, so we can use its address in a tree. */
12239 decl = build_decl (input_location,
12240 VAR_DECL, get_identifier (string), TREE_TYPE (integer_zero_node));
12241 DECL_EXTERNAL (decl) = 1;
12242 TREE_PUBLIC (decl) = 1;
12244 finish_var_decl (decl, 0);
12246 /* Make a decl for the address. */
12247 sprintf (string, "%sobjc_class_ref_%s",
12248 (flag_next_runtime ? "." : "__"), name);
12249 exp = build1 (ADDR_EXPR, string_type_node, decl);
12250 decl = build_decl (input_location,
12251 VAR_DECL, get_identifier (string), string_type_node);
12252 TREE_STATIC (decl) = 1;
12253 TREE_USED (decl) = 1;
12254 DECL_READ_P (decl) = 1;
12255 DECL_ARTIFICIAL (decl) = 1;
12256 DECL_INITIAL (decl) = error_mark_node;
12258 /* We must force the reference. */
12259 DECL_PRESERVE_P (decl) = 1;
12262 finish_var_decl (decl, exp);
12266 handle_impent (struct imp_entry *impent)
12270 objc_implementation_context = impent->imp_context;
12271 implementation_template = impent->imp_template;
12273 switch (TREE_CODE (impent->imp_context))
12275 case CLASS_IMPLEMENTATION_TYPE:
12277 const char *const class_name =
12278 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
12280 string = (char *) alloca (strlen (class_name) + 30);
12282 sprintf (string, "%sobjc_class_name_%s",
12283 (flag_next_runtime ? "." : "__"), class_name);
12286 case CATEGORY_IMPLEMENTATION_TYPE:
12288 const char *const class_name =
12289 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
12290 const char *const class_super_name =
12291 IDENTIFIER_POINTER (CLASS_SUPER_NAME (impent->imp_context));
12293 string = (char *) alloca (strlen (class_name)
12294 + strlen (class_super_name) + 30);
12296 /* Do the same for categories. Even though no references to
12297 these symbols are generated automatically by the compiler,
12298 it gives you a handle to pull them into an archive by
12300 sprintf (string, "*%sobjc_category_name_%s_%s",
12301 (flag_next_runtime ? "." : "__"), class_name, class_super_name);
12308 #ifdef ASM_DECLARE_CLASS_REFERENCE
12309 if (flag_next_runtime)
12311 ASM_DECLARE_CLASS_REFERENCE (asm_out_file, string);
12319 init = integer_zero_node;
12320 decl = build_decl (input_location,
12321 VAR_DECL, get_identifier (string), TREE_TYPE (init));
12322 TREE_PUBLIC (decl) = 1;
12323 TREE_READONLY (decl) = 1;
12324 TREE_USED (decl) = 1;
12325 TREE_CONSTANT (decl) = 1;
12326 DECL_CONTEXT (decl) = NULL_TREE;
12327 DECL_ARTIFICIAL (decl) = 1;
12328 TREE_STATIC (decl) = 1;
12329 DECL_INITIAL (decl) = error_mark_node; /* A real initializer is coming... */
12330 /* We must force the reference. */
12331 DECL_PRESERVE_P (decl) = 1;
12333 finish_var_decl(decl, init) ;
12337 /* The Fix-and-Continue functionality available in Mac OS X 10.3 and
12338 later requires that ObjC translation units participating in F&C be
12339 specially marked. The following routine accomplishes this. */
12341 /* static int _OBJC_IMAGE_INFO[2] = { 0, 1 }; */
12344 generate_objc_image_info (void)
12348 = ((flag_replace_objc_classes && imp_count ? 1 : 0)
12349 | (flag_objc_gc ? 2 : 0));
12350 VEC(constructor_elt,gc) *v = NULL;
12354 return; /* No need for an image_info entry. */
12356 array_type = build_sized_array_type (integer_type_node, 2);
12358 decl = start_var_decl (array_type, "_OBJC_IMAGE_INFO");
12360 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
12361 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (integer_type_node, flags));
12362 /* If we need this (determined above) it is because the runtime wants to
12363 refer to it in a manner hidden from the compiler. So we must force the
12365 DECL_PRESERVE_P (decl) = 1;
12366 finish_var_decl (decl, objc_build_constructor (TREE_TYPE (decl), v));
12369 /* Routine is called to issue diagnostic when reference to a private
12370 ivar is made and no other variable with same name is found in
12373 objc_diagnose_private_ivar (tree id)
12376 if (!objc_method_context)
12378 ivar = is_ivar (objc_ivar_chain, id);
12379 if (ivar && is_private (ivar))
12381 error ("instance variable %qs is declared private",
12382 IDENTIFIER_POINTER (id));
12388 /* Look up ID as an instance variable. OTHER contains the result of
12389 the C or C++ lookup, which we may want to use instead. */
12390 /* To use properties inside an instance method, use self.property. */
12392 objc_lookup_ivar (tree other, tree id)
12396 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
12397 if (!objc_method_context)
12400 if (!strcmp (IDENTIFIER_POINTER (id), "super"))
12401 /* We have a message to super. */
12402 return get_super_receiver ();
12404 /* In a class method, look up an instance variable only as a last
12406 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
12407 && other && other != error_mark_node)
12410 /* Look up the ivar, but do not use it if it is not accessible. */
12411 ivar = is_ivar (objc_ivar_chain, id);
12413 if (!ivar || is_private (ivar))
12416 /* In an instance method, a local variable (or parameter) may hide the
12417 instance variable. */
12418 if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
12419 && other && other != error_mark_node
12421 && CP_DECL_CONTEXT (other) != global_namespace)
12423 && !DECL_FILE_SCOPE_P (other))
12426 warning (0, "local declaration of %qE hides instance variable", id);
12431 /* At this point, we are either in an instance method with no obscuring
12432 local definitions, or in a class method with no alternate definitions
12434 return build_ivar_reference (id);
12437 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
12438 needs to be done if we are calling a function through a cast. */
12441 objc_rewrite_function_call (tree function, tree first_param)
12443 if (TREE_CODE (function) == NOP_EXPR
12444 && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR
12445 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0))
12448 function = build3 (OBJ_TYPE_REF, TREE_TYPE (function),
12449 TREE_OPERAND (function, 0),
12450 first_param, size_zero_node);
12456 /* This is called to "gimplify" a PROPERTY_REF node. It builds the
12457 corresponding 'getter' function call. Note that we assume the
12458 PROPERTY_REF to be valid since we generated it while parsing. */
12460 objc_gimplify_property_ref (tree *expr_p)
12462 tree getter = PROPERTY_REF_GETTER_CALL (*expr_p);
12465 if (getter == NULL_TREE)
12467 tree property_decl = PROPERTY_REF_PROPERTY_DECL (*expr_p);
12468 /* This can happen if DECL_ARTIFICIAL (*expr_p), but
12469 should be impossible for real properties, which always
12471 error_at (EXPR_LOCATION (*expr_p), "no %qs getter found",
12472 IDENTIFIER_POINTER (PROPERTY_NAME (property_decl)));
12473 /* Try to recover from the error to prevent an ICE. We take
12474 zero and cast it to the type of the property. */
12475 *expr_p = convert (TREE_TYPE (property_decl),
12476 integer_zero_node);
12482 /* In C++, a getter which returns an aggregate value results in a
12483 target_expr which initializes a temporary to the call
12485 if (TREE_CODE (getter) == TARGET_EXPR)
12487 gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter)));
12488 gcc_assert (TREE_CODE (TREE_OPERAND (getter, 0)) == VAR_DECL);
12489 call_exp = TREE_OPERAND (getter, 1);
12492 gcc_assert (TREE_CODE (call_exp) == CALL_EXPR);
12494 *expr_p = call_exp;
12497 /* This is called when "gimplifying" the trees. We need to gimplify
12498 the Objective-C/Objective-C++ specific trees, then hand over the
12499 process to C/C++. */
12501 objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
12503 enum tree_code code = TREE_CODE (*expr_p);
12506 /* Look for the special case of OBJC_TYPE_REF with the address
12507 of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend
12508 or one of its cousins). */
12510 if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR
12511 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0))
12514 enum gimplify_status r0, r1;
12516 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
12517 value of the OBJ_TYPE_REF, so force them to be emitted
12518 during subexpression evaluation rather than after the
12519 OBJ_TYPE_REF. This permits objc_msgSend calls in
12520 Objective C to use direct rather than indirect calls when
12521 the object expression has a postincrement. */
12522 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL,
12523 is_gimple_val, fb_rvalue);
12524 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
12525 is_gimple_val, fb_rvalue);
12527 return MIN (r0, r1);
12531 objc_gimplify_property_ref (expr_p);
12532 /* Do not return yet; let C/C++ gimplify the resulting expression. */
12539 return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p);
12541 return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
12545 /* This routine returns true if TYPE is a valid objc object type,
12546 suitable for messaging; false otherwise. If 'accept_class' is
12547 'true', then a Class object is considered valid for messaging and
12548 'true' is returned if 'type' refers to a Class. If 'accept_class'
12549 is 'false', then a Class object is not considered valid for
12550 messaging and 'false' is returned in that case. */
12553 objc_type_valid_for_messaging (tree type, bool accept_classes)
12555 if (!POINTER_TYPE_P (type))
12558 /* Remove the pointer indirection; don't remove more than one
12559 otherwise we'd consider "NSObject **" a valid type for messaging,
12561 type = TREE_TYPE (type);
12563 if (TREE_CODE (type) != RECORD_TYPE)
12566 if (objc_is_object_id (type))
12569 if (accept_classes && objc_is_class_id (type))
12572 if (TYPE_HAS_OBJC_INFO (type))
12578 /* Begin code generation for fast enumeration (foreach) ... */
12582 struct __objcFastEnumerationState
12584 unsigned long state;
12586 unsigned long *mutationsPtr;
12587 unsigned long extra[5];
12590 Confusingly enough, NSFastEnumeration is then defined by libraries
12591 to be the same structure.
12595 build_fast_enumeration_state_template (void)
12597 tree decls, *chain = NULL;
12600 objc_fast_enumeration_state_template = objc_start_struct (get_identifier
12601 (TAG_FAST_ENUMERATION_STATE));
12603 /* unsigned long state; */
12604 decls = add_field_decl (long_unsigned_type_node, "state", &chain);
12606 /* id *itemsPtr; */
12607 add_field_decl (build_pointer_type (objc_object_type),
12608 "itemsPtr", &chain);
12610 /* unsigned long *mutationsPtr; */
12611 add_field_decl (build_pointer_type (long_unsigned_type_node),
12612 "mutationsPtr", &chain);
12614 /* unsigned long extra[5]; */
12615 add_field_decl (build_sized_array_type (long_unsigned_type_node, 5),
12619 objc_finish_struct (objc_fast_enumeration_state_template, decls);
12623 'objc_finish_foreach_loop()' generates the code for an Objective-C
12624 foreach loop. The 'location' argument is the location of the 'for'
12625 that starts the loop. The 'object_expression' is the expression of
12626 the 'object' that iterates; the 'collection_expression' is the
12627 expression of the collection that we iterate over (we need to make
12628 sure we evaluate this only once); the 'for_body' is the set of
12629 statements to be executed in each iteration; 'break_label' and
12630 'continue_label' are the break and continue labels which we need to
12631 emit since the <statements> may be jumping to 'break_label' (if they
12632 contain 'break') or to 'continue_label' (if they contain
12637 for (<object expression> in <collection expression>)
12640 which is compiled into the following blurb:
12643 id __objc_foreach_collection;
12644 __objc_fast_enumeration_state __objc_foreach_enum_state;
12645 unsigned long __objc_foreach_batchsize;
12646 id __objc_foreach_items[16];
12647 __objc_foreach_collection = <collection expression>;
12648 __objc_foreach_enum_state = { 0 };
12649 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
12651 if (__objc_foreach_batchsize == 0)
12652 <object expression> = nil;
12655 unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr;
12658 unsigned long __objc_foreach_index;
12659 __objc_foreach_index = 0;
12662 if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>);
12663 <object expression> = enumState.itemsPtr[__objc_foreach_index];
12664 <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label]
12667 __objc_foreach_index++;
12668 if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object;
12669 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
12671 if (__objc_foreach_batchsize != 0) goto next_batch;
12672 <object expression> = nil;
12677 'statements' may contain a 'continue' or 'break' instruction, which
12678 the user expects to 'continue' or 'break' the entire foreach loop.
12679 We are provided the labels that 'break' and 'continue' jump to, so
12680 we place them where we want them to jump to when they pick them.
12682 Optimization TODO: we could cache the IMP of
12683 countByEnumeratingWithState:objects:count:.
12686 /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */
12687 /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */
12689 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
12690 #include "tree-pretty-print.h"
12694 objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body,
12695 tree break_label, tree continue_label)
12697 /* A tree representing the __objcFastEnumerationState struct type,
12698 or NSFastEnumerationState struct, whatever we are using. */
12699 tree objc_fast_enumeration_state_type;
12701 /* The trees representing the declarations of each of the local variables. */
12702 tree objc_foreach_collection_decl;
12703 tree objc_foreach_enum_state_decl;
12704 tree objc_foreach_items_decl;
12705 tree objc_foreach_batchsize_decl;
12706 tree objc_foreach_mutations_pointer_decl;
12707 tree objc_foreach_index_decl;
12709 /* A tree representing the selector countByEnumeratingWithState:objects:count:. */
12710 tree selector_name;
12712 /* A tree representing the local bind. */
12715 /* A tree representing the external 'if (__objc_foreach_batchsize)' */
12718 /* A tree representing the 'else' part of 'first_if' */
12721 /* A tree representing the 'next_batch' label. */
12722 tree next_batch_label_decl;
12724 /* A tree representing the binding after the 'next_batch' label. */
12725 tree next_batch_bind;
12727 /* A tree representing the 'next_object' label. */
12728 tree next_object_label_decl;
12730 /* Temporary variables. */
12734 if (flag_objc1_only)
12735 error_at (location, "fast enumeration is not available in Objective-C 1.0");
12737 if (object_expression == error_mark_node)
12740 if (collection_expression == error_mark_node)
12743 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression), true))
12745 error ("iterating variable in fast enumeration is not an object");
12749 if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression), true))
12751 error ("collection in fast enumeration is not an object");
12755 /* TODO: Check that object_expression is either a variable
12756 declaration, or an lvalue. */
12758 /* This kludge is an idea from apple. We use the
12759 __objcFastEnumerationState struct implicitly defined by the
12760 compiler, unless a NSFastEnumerationState struct has been defined
12761 (by a Foundation library such as GNUstep Base) in which case, we
12764 objc_fast_enumeration_state_type = objc_fast_enumeration_state_template;
12766 tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState"));
12768 if (objc_NSFastEnumeration_type)
12770 /* TODO: We really need to check that
12771 objc_NSFastEnumeration_type is the same as ours! */
12772 if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL)
12774 /* If it's a typedef, use the original type. */
12775 if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type))
12776 objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type);
12778 objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type);
12784 /* Done by c-parser.c. */
12787 /* Done by c-parser.c. */
12789 /* id __objc_foreach_collection */
12790 objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection");
12792 /* __objcFastEnumerationState __objc_foreach_enum_state; */
12793 objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state");
12794 TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl;
12796 /* id __objc_foreach_items[16]; */
12797 objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items");
12798 TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl;
12800 /* unsigned long __objc_foreach_batchsize; */
12801 objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize");
12802 TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl;
12804 /* Generate the local variable binding. */
12805 bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL);
12806 SET_EXPR_LOCATION (bind, location);
12807 TREE_SIDE_EFFECTS (bind) = 1;
12809 /* __objc_foreach_collection = <collection expression>; */
12810 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression);
12811 SET_EXPR_LOCATION (t, location);
12812 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
12814 /* __objc_foreach_enum_state.state = 0; */
12815 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
12816 get_identifier ("state")),
12817 build_int_cst (long_unsigned_type_node, 0));
12818 SET_EXPR_LOCATION (t, location);
12819 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
12821 /* __objc_foreach_enum_state.itemsPtr = NULL; */
12822 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
12823 get_identifier ("itemsPtr")),
12824 null_pointer_node);
12825 SET_EXPR_LOCATION (t, location);
12826 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
12828 /* __objc_foreach_enum_state.mutationsPtr = NULL; */
12829 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
12830 get_identifier ("mutationsPtr")),
12831 null_pointer_node);
12832 SET_EXPR_LOCATION (t, location);
12833 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
12835 /* __objc_foreach_enum_state.extra[0] = 0; */
12836 /* __objc_foreach_enum_state.extra[1] = 0; */
12837 /* __objc_foreach_enum_state.extra[2] = 0; */
12838 /* __objc_foreach_enum_state.extra[3] = 0; */
12839 /* __objc_foreach_enum_state.extra[4] = 0; */
12840 for (i = 0; i < 5 ; i++)
12842 t = build2 (MODIFY_EXPR, void_type_node,
12843 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
12844 get_identifier ("extra")),
12845 build_int_cst (NULL_TREE, i)),
12846 build_int_cst (long_unsigned_type_node, 0));
12847 SET_EXPR_LOCATION (t, location);
12848 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
12851 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
12852 selector_name = get_identifier ("countByEnumeratingWithState:objects:count:");
12854 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
12856 tree_cons /* &__objc_foreach_enum_state */
12857 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
12858 tree_cons /* __objc_foreach_items */
12859 (NULL_TREE, objc_foreach_items_decl,
12861 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
12863 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
12865 struct c_expr array;
12866 array.value = objc_foreach_items_decl;
12867 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
12869 tree_cons /* &__objc_foreach_enum_state */
12870 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
12871 tree_cons /* __objc_foreach_items */
12872 (NULL_TREE, default_function_array_conversion (location, array).value,
12874 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
12877 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
12878 convert (long_unsigned_type_node, t));
12879 SET_EXPR_LOCATION (t, location);
12880 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
12882 /* if (__objc_foreach_batchsize == 0) */
12883 first_if = build3 (COND_EXPR, void_type_node,
12886 (c_common_truthvalue_conversion
12888 build_binary_op (location,
12890 objc_foreach_batchsize_decl,
12891 build_int_cst (long_unsigned_type_node, 0), 1)),
12893 /* Then block (we fill it in later). */
12895 /* Else block (we fill it in later). */
12897 SET_EXPR_LOCATION (first_if, location);
12898 append_to_statement_list (first_if, &BIND_EXPR_BODY (bind));
12900 /* then <object expression> = nil; */
12901 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
12902 SET_EXPR_LOCATION (t, location);
12903 COND_EXPR_THEN (first_if) = t;
12905 /* Now we build the 'else' part of the if; once we finish building
12906 it, we attach it to first_if as the 'else' part. */
12911 /* unsigned long __objc_foreach_mutations_pointer; */
12912 objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer");
12914 /* Generate the local variable binding. */
12915 first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL);
12916 SET_EXPR_LOCATION (first_else, location);
12917 TREE_SIDE_EFFECTS (first_else) = 1;
12919 /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */
12920 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl,
12921 build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
12922 get_identifier ("mutationsPtr")),
12924 SET_EXPR_LOCATION (t, location);
12925 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
12928 next_batch_label_decl = create_artificial_label (location);
12929 t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl);
12930 SET_EXPR_LOCATION (t, location);
12931 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
12935 /* unsigned long __objc_foreach_index; */
12936 objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index");
12938 /* Generate the local variable binding. */
12939 next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL);
12940 SET_EXPR_LOCATION (next_batch_bind, location);
12941 TREE_SIDE_EFFECTS (next_batch_bind) = 1;
12942 append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else));
12944 /* __objc_foreach_index = 0; */
12945 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
12946 build_int_cst (long_unsigned_type_node, 0));
12947 SET_EXPR_LOCATION (t, location);
12948 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12951 next_object_label_decl = create_artificial_label (location);
12952 t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl);
12953 SET_EXPR_LOCATION (t, location);
12954 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12956 /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */
12957 t = build3 (COND_EXPR, void_type_node,
12960 (c_common_truthvalue_conversion
12965 objc_foreach_mutations_pointer_decl,
12966 build_indirect_ref (location,
12967 objc_build_component_ref (objc_foreach_enum_state_decl,
12968 get_identifier ("mutationsPtr")),
12969 RO_UNARY_STAR), 1)),
12972 build_function_call (input_location,
12973 objc_enumeration_mutation_decl,
12974 tree_cons (NULL, collection_expression, NULL)),
12977 SET_EXPR_LOCATION (t, location);
12978 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12980 /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */
12981 t = build2 (MODIFY_EXPR, void_type_node, object_expression,
12982 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
12983 get_identifier ("itemsPtr")),
12984 objc_foreach_index_decl));
12985 SET_EXPR_LOCATION (t, location);
12986 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12988 /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */
12989 append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind));
12991 /* continue_label: */
12992 if (continue_label)
12994 t = build1 (LABEL_EXPR, void_type_node, continue_label);
12995 SET_EXPR_LOCATION (t, location);
12996 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12999 /* __objc_foreach_index++; */
13000 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
13001 build_binary_op (location,
13003 objc_foreach_index_decl,
13004 build_int_cst (long_unsigned_type_node, 1), 1));
13005 SET_EXPR_LOCATION (t, location);
13006 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13008 /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */
13009 t = build3 (COND_EXPR, void_type_node,
13012 (c_common_truthvalue_conversion
13014 build_binary_op (location,
13016 objc_foreach_index_decl,
13017 objc_foreach_batchsize_decl, 1)),
13020 build1 (GOTO_EXPR, void_type_node, next_object_label_decl),
13023 SET_EXPR_LOCATION (t, location);
13024 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13026 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
13028 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
13030 tree_cons /* &__objc_foreach_enum_state */
13031 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
13032 tree_cons /* __objc_foreach_items */
13033 (NULL_TREE, objc_foreach_items_decl,
13035 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
13037 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
13039 struct c_expr array;
13040 array.value = objc_foreach_items_decl;
13041 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
13043 tree_cons /* &__objc_foreach_enum_state */
13044 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
13045 tree_cons /* __objc_foreach_items */
13046 (NULL_TREE, default_function_array_conversion (location, array).value,
13048 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
13051 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
13052 convert (long_unsigned_type_node, t));
13053 SET_EXPR_LOCATION (t, location);
13054 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13058 /* if (__objc_foreach_batchsize != 0) goto next_batch; */
13059 t = build3 (COND_EXPR, void_type_node,
13062 (c_common_truthvalue_conversion
13064 build_binary_op (location,
13066 objc_foreach_batchsize_decl,
13067 build_int_cst (long_unsigned_type_node, 0), 1)),
13070 build1 (GOTO_EXPR, void_type_node, next_batch_label_decl),
13073 SET_EXPR_LOCATION (t, location);
13074 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
13076 /* <object expression> = nil; */
13077 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
13078 SET_EXPR_LOCATION (t, location);
13079 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
13084 t = build1 (LABEL_EXPR, void_type_node, break_label);
13085 SET_EXPR_LOCATION (t, location);
13086 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
13090 COND_EXPR_ELSE (first_if) = first_else;
13092 /* Do the whole thing. */
13095 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
13096 /* This will print to stderr the whole blurb generated by the
13097 compiler while compiling (assuming the compiler doesn't crash
13098 before getting here).
13100 debug_generic_stmt (bind);
13104 /* Done by c-parser.c */
13107 /* Return true if we have an NxString object pointer. */
13110 objc_string_ref_type_p (tree strp)
13113 if (!strp || TREE_CODE (strp) != POINTER_TYPE)
13116 tmv = TYPE_MAIN_VARIANT (TREE_TYPE (strp));
13117 tmv = OBJC_TYPE_NAME (tmv);
13119 && TREE_CODE (tmv) == IDENTIFIER_NODE
13120 && IDENTIFIER_POINTER (tmv)
13121 && !strncmp (IDENTIFIER_POINTER (tmv), "NSString", 8));
13124 /* At present the behavior of this is undefined and it does nothing. */
13126 objc_check_format_arg (tree ARG_UNUSED (format_arg),
13127 tree ARG_UNUSED (args_list))
13131 #include "gt-objc-objc-act.h"