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"
39 #include "langhooks.h"
48 #include "diagnostic-core.h"
51 #include "tree-iterator.h"
53 #include "langhooks-def.h"
55 /* For default_tree_printer (). */
56 #include "tree-pretty-print.h"
58 /* For enum gimplify_status */
61 #define OBJC_VOID_AT_END void_list_node
63 static unsigned int should_call_super_dealloc = 0;
65 /* When building Objective-C++, we need in_late_binary_op. */
67 bool in_late_binary_op = false;
70 /* When building Objective-C++, we are not linking against the C front-end
71 and so need to replicate the C tree-construction functions in some way. */
73 #define OBJCP_REMAP_FUNCTIONS
74 #include "objcp-decl.h"
77 /* This is the default way of generating a method name. */
78 /* This has the problem that "test_method:argument:" and
79 "test:method_argument:" will generate the same name
80 ("_i_Test__test_method_argument_" for an instance method of the
81 class "Test"), so you can't have them both in the same class!
82 Moreover, the demangling (going from
83 "_i_Test__test_method_argument" back to the original name) is
84 undefined because there are two correct ways of demangling the
86 #ifndef OBJC_GEN_METHOD_LABEL
87 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
90 sprintf ((BUF), "_%s_%s_%s_%s", \
91 ((IS_INST) ? "i" : "c"), \
93 ((CAT_NAME)? (CAT_NAME) : ""), \
95 for (temp = (BUF); *temp; temp++) \
96 if (*temp == ':') *temp = '_'; \
100 /* These need specifying. */
101 #ifndef OBJC_FORWARDING_STACK_OFFSET
102 #define OBJC_FORWARDING_STACK_OFFSET 0
105 #ifndef OBJC_FORWARDING_MIN_OFFSET
106 #define OBJC_FORWARDING_MIN_OFFSET 0
109 /* Set up for use of obstacks. */
113 /* This obstack is used to accumulate the encoding of a data type. */
114 static struct obstack util_obstack;
116 /* This points to the beginning of obstack contents, so we can free
117 the whole contents. */
120 /* The version identifies which language generation and runtime
121 the module (file) was compiled for, and is recorded in the
122 module descriptor. */
124 #define OBJC_VERSION (flag_next_runtime ? 6 : 8)
125 #define PROTOCOL_VERSION 2
127 /* (Decide if these can ever be validly changed.) */
128 #define OBJC_ENCODE_INLINE_DEFS 0
129 #define OBJC_ENCODE_DONT_INLINE_DEFS 1
131 /*** Private Interface (procedures) ***/
133 /* Used by compile_file. */
135 static void init_objc (void);
136 static void finish_objc (void);
138 /* Code generation. */
140 static tree objc_build_constructor (tree, VEC(constructor_elt,gc) *);
141 static tree build_objc_method_call (location_t, int, tree, tree, tree, tree);
142 static tree get_proto_encoding (tree);
143 static tree lookup_interface (tree);
144 static tree objc_add_static_instance (tree, tree);
146 static tree start_class (enum tree_code, tree, tree, tree);
147 static tree continue_class (tree);
148 static void finish_class (tree);
149 static void start_method_def (tree);
151 static void objc_start_function (tree, tree, tree, tree);
153 static void objc_start_function (tree, tree, tree, struct c_arg_info *);
155 static tree start_protocol (enum tree_code, tree, tree);
156 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
157 static tree objc_add_method (tree, tree, int, bool);
158 static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree);
159 static tree build_ivar_reference (tree);
160 static tree is_ivar (tree, tree);
162 static void build_objc_exception_stuff (void);
163 static void build_next_objc_exception_stuff (void);
165 /* We only need the following for ObjC; ObjC++ will use C++'s definition
166 of DERIVED_FROM_P. */
168 static bool objc_derived_from_p (tree, tree);
169 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
173 static void objc_gen_property_data (tree, tree);
174 static void objc_synthesize_getter (tree, tree, tree);
175 static void objc_synthesize_setter (tree, tree, tree);
176 static char *objc_build_property_setter_name (tree);
177 static int match_proto_with_proto (tree, tree, int);
178 static tree lookup_property (tree, tree);
179 static tree lookup_property_in_list (tree, tree);
180 static tree lookup_property_in_protocol_list (tree, tree);
181 static void build_objc_property_accessor_helpers (void);
183 static void objc_xref_basetypes (tree, tree);
185 static void build_class_template (void);
186 static void build_selector_template (void);
187 static void build_category_template (void);
188 static void build_super_template (void);
189 static tree build_protocol_initializer (tree, tree, tree, tree, tree);
190 static tree get_class_ivars (tree, bool);
191 static tree generate_protocol_list (tree);
192 static void build_protocol_reference (tree);
194 static void build_fast_enumeration_state_template (void);
197 static void objc_generate_cxx_cdtors (void);
201 static void objc_decl_method_attributes (tree*, tree, int);
202 static tree build_keyword_selector (tree);
203 static const char *synth_id_with_class_suffix (const char *, tree);
205 /* Hash tables to manage the global pool of method prototypes. */
207 hash *nst_method_hash_list = 0;
208 hash *cls_method_hash_list = 0;
210 /* Hash tables to manage the global pool of class names. */
212 hash *cls_name_hash_list = 0;
213 hash *als_name_hash_list = 0;
215 static void hash_class_name_enter (hash *, tree, tree);
216 static hash hash_class_name_lookup (hash *, tree);
218 static hash hash_lookup (hash *, tree);
219 static tree lookup_method (tree, tree);
220 static tree lookup_method_static (tree, tree, int);
222 static tree add_class (tree, tree);
223 static void add_category (tree, tree);
224 static inline tree lookup_category (tree, tree);
228 class_names, /* class, category, protocol, module names */
229 meth_var_names, /* method and variable names */
230 meth_var_types /* method and variable type descriptors */
233 static tree add_objc_string (tree, enum string_section);
234 static void build_selector_table_decl (void);
236 /* Protocol additions. */
238 static tree lookup_protocol (tree);
239 static tree lookup_and_install_protocols (tree);
243 static void encode_type_qualifiers (tree);
244 static void encode_type (tree, int, int);
245 static void encode_field_decl (tree, int, int);
248 static void really_start_method (tree, tree);
250 static void really_start_method (tree, struct c_arg_info *);
252 static int comp_proto_with_proto (tree, tree, int);
253 static tree get_arg_type_list (tree, int, int);
254 static tree objc_decay_parm_type (tree);
255 static void objc_push_parm (tree);
257 static tree objc_get_parm_info (int);
259 static struct c_arg_info *objc_get_parm_info (int);
262 /* Utilities for debugging and error diagnostics. */
264 static char *gen_type_name (tree);
265 static char *gen_type_name_0 (tree);
266 static char *gen_method_decl (tree);
267 static char *gen_declaration (tree);
269 /* Everything else. */
271 static tree create_field_decl (tree, const char *);
272 static void add_class_reference (tree);
273 static void build_protocol_template (void);
274 static tree encode_method_prototype (tree);
275 static void generate_classref_translation_entry (tree);
276 static void handle_class_ref (tree);
277 static void generate_struct_by_value_array (void)
279 static void mark_referenced_methods (void);
280 static void generate_objc_image_info (void);
281 static bool objc_type_valid_for_messaging (tree typ);
283 /*** Private Interface (data) ***/
285 /* Reserved tag definitions. */
287 #define OBJECT_TYPEDEF_NAME "id"
288 #define CLASS_TYPEDEF_NAME "Class"
290 #define TAG_OBJECT "objc_object"
291 #define TAG_CLASS "objc_class"
292 #define TAG_SUPER "objc_super"
293 #define TAG_SELECTOR "objc_selector"
295 #define UTAG_CLASS "_objc_class"
296 #define UTAG_IVAR "_objc_ivar"
297 #define UTAG_IVAR_LIST "_objc_ivar_list"
298 #define UTAG_METHOD "_objc_method"
299 #define UTAG_METHOD_LIST "_objc_method_list"
300 #define UTAG_CATEGORY "_objc_category"
301 #define UTAG_MODULE "_objc_module"
302 #define UTAG_SYMTAB "_objc_symtab"
303 #define UTAG_SUPER "_objc_super"
304 #define UTAG_SELECTOR "_objc_selector"
306 #define UTAG_PROTOCOL "_objc_protocol"
307 #define UTAG_METHOD_PROTOTYPE "_objc_method_prototype"
308 #define UTAG_METHOD_PROTOTYPE_LIST "_objc__method_prototype_list"
310 /* Note that the string object global name is only needed for the
312 #define STRING_OBJECT_GLOBAL_FORMAT "_%sClassReference"
314 #define PROTOCOL_OBJECT_CLASS_NAME "Protocol"
316 #define TAG_ENUMERATION_MUTATION "objc_enumerationMutation"
317 #define TAG_FAST_ENUMERATION_STATE "__objcFastEnumerationState"
319 static const char *TAG_GETCLASS;
320 static const char *TAG_GETMETACLASS;
321 static const char *TAG_MSGSEND;
322 static const char *TAG_MSGSENDSUPER;
323 /* The NeXT Objective-C messenger may have two extra entry points, for use
324 when returning a structure. */
325 static const char *TAG_MSGSEND_STRET;
326 static const char *TAG_MSGSENDSUPER_STRET;
327 static const char *default_constant_string_class_name;
329 /* Runtime metadata flags. */
330 #define CLS_FACTORY 0x0001L
331 #define CLS_META 0x0002L
332 #define CLS_HAS_CXX_STRUCTORS 0x2000L
334 #define OBJC_MODIFIER_STATIC 0x00000001
335 #define OBJC_MODIFIER_FINAL 0x00000002
336 #define OBJC_MODIFIER_PUBLIC 0x00000004
337 #define OBJC_MODIFIER_PRIVATE 0x00000008
338 #define OBJC_MODIFIER_PROTECTED 0x00000010
339 #define OBJC_MODIFIER_NATIVE 0x00000020
340 #define OBJC_MODIFIER_SYNCHRONIZED 0x00000040
341 #define OBJC_MODIFIER_ABSTRACT 0x00000080
342 #define OBJC_MODIFIER_VOLATILE 0x00000100
343 #define OBJC_MODIFIER_TRANSIENT 0x00000200
344 #define OBJC_MODIFIER_NONE_SPECIFIED 0x80000000
346 /* NeXT-specific tags. */
348 #define TAG_MSGSEND_NONNIL "objc_msgSendNonNil"
349 #define TAG_MSGSEND_NONNIL_STRET "objc_msgSendNonNil_stret"
350 #define TAG_EXCEPTIONEXTRACT "objc_exception_extract"
351 #define TAG_EXCEPTIONTRYENTER "objc_exception_try_enter"
352 #define TAG_EXCEPTIONTRYEXIT "objc_exception_try_exit"
353 #define TAG_EXCEPTIONMATCH "objc_exception_match"
354 #define TAG_EXCEPTIONTHROW "objc_exception_throw"
355 #define TAG_SYNCENTER "objc_sync_enter"
356 #define TAG_SYNCEXIT "objc_sync_exit"
357 #define TAG_SETJMP "_setjmp"
358 #define UTAG_EXCDATA "_objc_exception_data"
360 #define TAG_ASSIGNIVAR "objc_assign_ivar"
361 #define TAG_ASSIGNGLOBAL "objc_assign_global"
362 #define TAG_ASSIGNSTRONGCAST "objc_assign_strongCast"
364 /* Branch entry points. All that matters here are the addresses;
365 functions with these names do not really exist in libobjc. */
367 #define TAG_MSGSEND_FAST "objc_msgSend_Fast"
368 #define TAG_ASSIGNIVAR_FAST "objc_assign_ivar_Fast"
370 #define TAG_CXX_CONSTRUCT ".cxx_construct"
371 #define TAG_CXX_DESTRUCT ".cxx_destruct"
373 /* GNU-specific tags. */
375 #define TAG_EXECCLASS "__objc_exec_class"
376 #define TAG_GNUINIT "__objc_gnu_init"
378 /* Flags for lookup_method_static(). */
379 #define OBJC_LOOKUP_CLASS 1 /* Look for class methods. */
380 #define OBJC_LOOKUP_NO_SUPER 2 /* Do not examine superclasses. */
382 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
383 tree objc_global_trees[OCTI_MAX];
385 static void handle_impent (struct imp_entry *);
387 struct imp_entry *imp_list = 0;
388 int imp_count = 0; /* `@implementation' */
389 int cat_count = 0; /* `@category' */
391 objc_ivar_visibility_kind objc_ivar_visibility;
393 /* Use to generate method labels. */
394 static int method_slot = 0;
396 /* Flag to say whether methods in a protocol are optional or
398 static bool objc_method_optional_flag = false;
400 static int objc_collecting_ivars = 0;
404 static char *errbuf; /* Buffer for error diagnostics */
406 /* Data imported from tree.c. */
408 extern enum debug_info_type write_symbols;
410 /* Data imported from toplev.c. */
412 extern const char *dump_base_name;
414 static int flag_typed_selectors;
416 /* Store all constructed constant strings in a hash table so that
417 they get uniqued properly. */
419 struct GTY(()) string_descriptor {
420 /* The literal argument . */
423 /* The resulting constant string. */
427 static GTY((param_is (struct string_descriptor))) htab_t string_htab;
429 FILE *gen_declaration_file;
431 /* Tells "encode_pointer/encode_aggregate" whether we are generating
432 type descriptors for instance variables (as opposed to methods).
433 Type descriptors for instance variables contain more information
434 than methods (for static typing and embedded structures). */
436 static int generating_instance_variables = 0;
438 /* For building an objc struct. These may not be used when this file
439 is compiled as part of obj-c++. */
441 static bool objc_building_struct;
442 static struct c_struct_parse_info *objc_struct_info ATTRIBUTE_UNUSED;
444 /* Start building a struct for objc. */
447 objc_start_struct (tree name)
449 gcc_assert (!objc_building_struct);
450 objc_building_struct = true;
451 return start_struct (input_location, RECORD_TYPE, name, &objc_struct_info);
454 /* Finish building a struct for objc. */
457 objc_finish_struct (tree type, tree fieldlist)
459 gcc_assert (objc_building_struct);
460 objc_building_struct = false;
461 return finish_struct (input_location, type, fieldlist, NULL_TREE,
466 build_sized_array_type (tree base_type, int size)
468 tree index_type = build_index_type (build_int_cst (NULL_TREE, size - 1));
469 return build_array_type (base_type, index_type);
473 add_field_decl (tree type, const char *name, tree **chain)
475 tree field = create_field_decl (type, name);
479 *chain = &DECL_CHAIN (field);
484 /* Some platforms pass small structures through registers versus
485 through an invisible pointer. Determine at what size structure is
486 the transition point between the two possibilities. */
489 generate_struct_by_value_array (void)
494 int aggregate_in_mem[32];
497 /* Presumably no platform passes 32 byte structures in a register. */
498 for (i = 1; i < 32; i++)
503 /* Create an unnamed struct that has `i' character components */
504 type = objc_start_struct (NULL_TREE);
506 strcpy (buffer, "c1");
507 decls = add_field_decl (char_type_node, buffer, &chain);
509 for (j = 1; j < i; j++)
511 sprintf (buffer, "c%d", j + 1);
512 add_field_decl (char_type_node, buffer, &chain);
514 objc_finish_struct (type, decls);
516 aggregate_in_mem[i] = aggregate_value_p (type, 0);
517 if (!aggregate_in_mem[i])
521 /* We found some structures that are returned in registers instead of memory
522 so output the necessary data. */
525 for (i = 31; i >= 0; i--)
526 if (!aggregate_in_mem[i])
528 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n\n", i);
530 /* The first member of the structure is always 0 because we don't handle
531 structures with 0 members */
532 printf ("static int struct_forward_array[] = {\n 0");
534 for (j = 1; j <= i; j++)
535 printf (", %d", aggregate_in_mem[j]);
546 if (cxx_init () == false)
548 if (c_objc_common_init () == false)
552 /* If gen_declaration desired, open the output file. */
553 if (flag_gen_declaration)
555 register char * const dumpname = concat (dump_base_name, ".decl", NULL);
556 gen_declaration_file = fopen (dumpname, "w");
557 if (gen_declaration_file == 0)
558 fatal_error ("can't open %s: %m", dumpname);
562 if (flag_next_runtime)
564 TAG_GETCLASS = "objc_getClass";
565 TAG_GETMETACLASS = "objc_getMetaClass";
566 TAG_MSGSEND = "objc_msgSend";
567 TAG_MSGSENDSUPER = "objc_msgSendSuper";
568 TAG_MSGSEND_STRET = "objc_msgSend_stret";
569 TAG_MSGSENDSUPER_STRET = "objc_msgSendSuper_stret";
570 default_constant_string_class_name = "NSConstantString";
574 TAG_GETCLASS = "objc_get_class";
575 TAG_GETMETACLASS = "objc_get_meta_class";
576 TAG_MSGSEND = "objc_msg_lookup";
577 TAG_MSGSENDSUPER = "objc_msg_lookup_super";
578 /* GNU runtime does not provide special functions to support
579 structure-returning methods. */
580 default_constant_string_class_name = "NXConstantString";
581 flag_typed_selectors = 1;
582 /* GNU runtime does not need the compiler to change code
583 in order to do GC. */
586 warning_at (0, 0, "%<-fobjc-gc%> is ignored for %<-fgnu-runtime%>");
593 if (print_struct_values && !flag_compare_debug)
594 generate_struct_by_value_array ();
599 /* This is called automatically (at the very end of compilation) by
600 c_write_global_declarations and cp_write_global_declarations. */
602 objc_write_global_declarations (void)
604 mark_referenced_methods ();
606 /* Finalize Objective-C runtime data. */
609 if (gen_declaration_file)
610 fclose (gen_declaration_file);
613 /* Return the first occurrence of a method declaration corresponding
614 to sel_name in rproto_list. Search rproto_list recursively.
615 If is_class is 0, search for instance methods, otherwise for class
618 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
624 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
626 p = TREE_VALUE (rproto);
628 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
630 if ((fnd = lookup_method (is_class
631 ? PROTOCOL_CLS_METHODS (p)
632 : PROTOCOL_NST_METHODS (p), sel_name)))
634 else if (PROTOCOL_LIST (p))
635 fnd = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
640 ; /* An identifier...if we could not find a protocol. */
651 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
655 /* Make sure the protocol is supported by the object on the rhs. */
656 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
659 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
661 p = TREE_VALUE (rproto);
663 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
668 else if (PROTOCOL_LIST (p))
669 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
678 ; /* An identifier...if we could not find a protocol. */
685 objc_start_class_interface (tree klass, tree super_class,
686 tree protos, tree attributes)
689 warning_at (input_location, OPT_Wattributes,
690 "class attributes are not available in this version"
691 " of the compiler, (ignored)");
692 objc_interface_context
694 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos);
695 objc_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
699 objc_start_category_interface (tree klass, tree categ,
700 tree protos, tree attributes)
703 warning_at (input_location, OPT_Wattributes,
704 "category attributes are not available in this version"
705 " of the compiler, (ignored)");
706 objc_interface_context
707 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos);
709 = continue_class (objc_interface_context);
713 objc_start_protocol (tree name, tree protos, tree attributes)
716 warning_at (input_location, OPT_Wattributes,
717 "protocol attributes are not available in this version"
718 " of the compiler, (ignored)");
719 objc_interface_context
720 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos);
721 objc_method_optional_flag = false;
725 objc_continue_interface (void)
728 = continue_class (objc_interface_context);
732 objc_finish_interface (void)
734 finish_class (objc_interface_context);
735 objc_interface_context = NULL_TREE;
736 objc_method_optional_flag = false;
740 objc_start_class_implementation (tree klass, tree super_class)
742 objc_implementation_context
744 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE);
745 objc_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
749 objc_start_category_implementation (tree klass, tree categ)
751 objc_implementation_context
752 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE);
754 = continue_class (objc_implementation_context);
758 objc_continue_implementation (void)
761 = continue_class (objc_implementation_context);
765 objc_finish_implementation (void)
768 if (flag_objc_call_cxx_cdtors)
769 objc_generate_cxx_cdtors ();
772 if (objc_implementation_context)
774 finish_class (objc_implementation_context);
775 objc_ivar_chain = NULL_TREE;
776 objc_implementation_context = NULL_TREE;
779 warning (0, "%<@end%> must appear in an @implementation context");
783 objc_set_visibility (objc_ivar_visibility_kind visibility)
785 if (visibility == OBJC_IVAR_VIS_PACKAGE)
786 warning (0, "%<@package%> presently has the same effect as %<@public%>");
787 objc_ivar_visibility = visibility;
791 objc_set_method_opt (bool optional)
793 objc_method_optional_flag = optional;
794 if (!objc_interface_context
795 || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
797 error ("@optional/@required is allowed in @protocol context only.");
798 objc_method_optional_flag = false;
802 /* This routine is called by the parser when a
803 @property... declaration is found. 'decl' is the declaration of
804 the property (type/identifier), and the other arguments represent
805 property attributes that may have been specified in the Objective-C
806 declaration. 'parsed_property_readonly' is 'true' if the attribute
807 'readonly' was specified, and 'false' if not; similarly for the
808 other bool parameters. 'parsed_property_getter_ident' is NULL_TREE
809 if the attribute 'getter' was not specified, and is the identifier
810 corresponding to the specified getter if it was; similarly for
811 'parsed_property_setter_ident'. */
813 objc_add_property_declaration (location_t location, tree decl,
814 bool parsed_property_readonly, bool parsed_property_readwrite,
815 bool parsed_property_assign, bool parsed_property_retain,
816 bool parsed_property_copy, bool parsed_property_nonatomic,
817 tree parsed_property_getter_ident, tree parsed_property_setter_ident)
821 /* 'property_readonly' and 'property_assign_semantics' are the final
822 attributes of the property after all parsed attributes have been
823 considered (eg, if we parsed no 'readonly' and no 'readwrite', ie
824 parsed_property_readonly = false and parsed_property_readwrite =
825 false, then property_readonly will be false because the default
827 bool property_readonly = false;
828 objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN;
830 if (parsed_property_readonly && parsed_property_readwrite)
832 error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute");
833 /* In case of conflicting attributes (here and below), after
834 producing an error, we pick one of the attributes and keep
836 property_readonly = false;
840 if (parsed_property_readonly)
841 property_readonly = true;
843 if (parsed_property_readwrite)
844 property_readonly = false;
847 if (parsed_property_readonly && parsed_property_setter_ident)
849 /* Maybe this should be an error ? The Apple documentation says it is a warning. */
850 warning_at (location, 0, "%<readonly%> attribute conflicts with %<setter%> attribute");
851 property_readonly = false;
854 if (parsed_property_assign && parsed_property_retain)
856 error_at (location, "%<assign%> attribute conflicts with %<retain%> attribute");
857 property_assign_semantics = OBJC_PROPERTY_RETAIN;
859 else if (parsed_property_assign && parsed_property_copy)
861 error_at (location, "%<assign%> attribute conflicts with %<copy%> attribute");
862 property_assign_semantics = OBJC_PROPERTY_COPY;
864 else if (parsed_property_retain && parsed_property_copy)
866 error_at (location, "%<retain%> attribute conflicts with %<copy%> attribute");
867 property_assign_semantics = OBJC_PROPERTY_COPY;
871 if (parsed_property_assign)
872 property_assign_semantics = OBJC_PROPERTY_ASSIGN;
874 if (parsed_property_retain)
875 property_assign_semantics = OBJC_PROPERTY_RETAIN;
877 if (parsed_property_copy)
878 property_assign_semantics = OBJC_PROPERTY_COPY;
881 if (!objc_interface_context)
883 error_at (location, "property declaration not in @interface or @protocol context");
887 /* At this point we know that we are either in an interface, a
888 category, or a protocol. */
890 if (parsed_property_setter_ident)
892 /* The setter should be terminated by ':', but the parser only
893 gives us an identifier without ':'. So, we need to add ':'
895 const char *parsed_setter = IDENTIFIER_POINTER (parsed_property_setter_ident);
896 size_t length = strlen (parsed_setter);
897 char *final_setter = (char *)alloca (length + 2);
899 sprintf (final_setter, "%s:", parsed_setter);
900 parsed_property_setter_ident = get_identifier (final_setter);
903 /* Check that the property does not have an initial value specified.
904 This should never happen as the parser doesn't allow this, but
905 it's just in case. */
906 if (DECL_INITIAL (decl))
908 error_at (location, "property can not have an initial value");
912 /* TODO: Check that the property type is an Objective-C object or a "POD". */
914 if (property_assign_semantics == OBJC_PROPERTY_ASSIGN)
916 /* If garbage collection is not being used, then 'assign' is
917 valid for objects (and typically used for delegates) but it
918 is wrong in most cases (since most objects need to be
919 retained or copied in setters). Warn users when 'assign' is
921 /* Please note that it would make sense to default to 'assign'
922 for non-{Objective-C objects}, and to 'retain' for
923 Objective-C objects. But that would break compatibility with
927 if (!parsed_property_assign && !parsed_property_retain && !parsed_property_copy)
929 if (objc_type_valid_for_messaging (TREE_TYPE (decl)))
931 warning_at (location,
933 "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>",
936 "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it");
942 if (property_assign_semantics == OBJC_PROPERTY_RETAIN
943 && !objc_type_valid_for_messaging (TREE_TYPE (decl)))
944 error_at (location, "%<retain%> attribute is only valid for Objective-C objects");
946 if (property_assign_semantics == OBJC_PROPERTY_COPY
947 && !objc_type_valid_for_messaging (TREE_TYPE (decl)))
948 error_at (location, "%<copy%> attribute is only valid for Objective-C objects");
950 /* Check for duplicate property declarations. We first check the
951 immediate context for a property with the same name. */
952 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
954 if (PROPERTY_NAME (x) == DECL_NAME (decl))
956 location_t original_location = DECL_SOURCE_LOCATION (x);
958 error_at (location, "redeclaration of property %qD", decl);
960 if (original_location != UNKNOWN_LOCATION)
961 inform (original_location, "originally declared here");
966 /* TODO: Shall we check here for other property declaractions (in
967 the superclass, other categories or protocols) with the same name
968 and conflicting types ? */
970 /* Create a PROPERTY_DECL node. */
971 property_decl = make_node (PROPERTY_DECL);
973 /* Copy the basic information from the original decl. */
974 TREE_TYPE (property_decl) = TREE_TYPE (decl);
975 DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl);
976 TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl);
978 /* Add property-specific information. */
979 PROPERTY_NAME (property_decl) = DECL_NAME (decl);
980 PROPERTY_GETTER_NAME (property_decl) = parsed_property_getter_ident;
981 PROPERTY_SETTER_NAME (property_decl) = parsed_property_setter_ident;
982 PROPERTY_READONLY (property_decl) = property_readonly;
983 PROPERTY_NONATOMIC (property_decl) = parsed_property_nonatomic;
984 PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics;
985 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
986 PROPERTY_DYNAMIC (property_decl) = 0;
988 /* Add the PROPERTY_DECL to the list of properties for the class. */
989 TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context);
990 CLASS_PROPERTY_DECL (objc_interface_context) = property_decl;
993 /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
996 lookup_property_in_list (tree chain, tree property)
999 for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x))
1000 if (PROPERTY_NAME (x) == property)
1005 /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
1006 static tree lookup_property_in_protocol_list (tree rproto_list, tree property)
1009 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
1011 tree p = TREE_VALUE (rproto);
1012 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
1014 if ((x = lookup_property_in_list (p, property)))
1016 if (PROTOCOL_LIST (p))
1017 return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property);
1021 ; /* An identifier...if we could not find a protocol. */
1027 /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
1028 chain of interface hierarchy. */
1030 lookup_property (tree interface_type, tree property)
1032 tree inter = interface_type;
1036 if ((x = lookup_property_in_list (inter, property)))
1038 /* Failing that, look for the property in each category of the class. */
1040 while ((category = CLASS_CATEGORY_LIST (category)))
1041 if ((x = lookup_property_in_list (category, property)))
1044 /* Failing to find in categories, look for property in protocol list. */
1045 if (CLASS_PROTOCOL_LIST (inter)
1046 && (x = lookup_property_in_protocol_list (
1047 CLASS_PROTOCOL_LIST (inter), property)))
1050 /* Failing that, climb up the inheritance hierarchy. */
1051 inter = lookup_interface (CLASS_SUPER_NAME (inter));
1057 /* This hook routine is invoked by the parser when an expression such
1058 as 'xxx.yyy' is parsed. We get a chance to process these
1059 expressions in a way that is specified to Objective-C (to implement
1060 properties, or non-fragile ivars). If the expression is not an
1061 Objective-C specified expression, we should return NULL_TREE; else
1062 we return the expression.
1064 At the moment this only implements properties (not non-fragile
1065 ivars yet), ie 'object.property'. */
1067 objc_maybe_build_component_ref (tree object, tree property_ident)
1072 /* Try to determine quickly if 'object' is an Objective-C object or
1073 not. If not, return. */
1074 if (object == NULL_TREE || object == error_mark_node
1075 || (rtype = TREE_TYPE (object)) == NULL_TREE)
1078 if (property_ident == NULL_TREE || property_ident == error_mark_node
1079 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1082 /* TODO: Implement super.property. */
1084 /* TODO: Carefully review the following code. */
1085 if (objc_is_id (rtype))
1087 tree rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
1088 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
1091 x = lookup_property_in_protocol_list (rprotos, property_ident);
1095 tree basetype = TYPE_MAIN_VARIANT (rtype);
1097 if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE)
1098 basetype = TREE_TYPE (basetype);
1102 while (basetype != NULL_TREE
1103 && TREE_CODE (basetype) == RECORD_TYPE
1104 && OBJC_TYPE_NAME (basetype)
1105 && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL
1106 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)))
1107 basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype));
1109 if (basetype != NULL_TREE && TYPED_OBJECT (basetype))
1111 tree interface_type = TYPE_OBJC_INTERFACE (basetype);
1112 tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype);
1114 x = lookup_property (interface_type, property_ident);
1117 x = lookup_property_in_protocol_list (protocol_list, property_ident);
1125 expression = build2 (PROPERTY_REF, TREE_TYPE(x), object, x);
1126 SET_EXPR_LOCATION (expression, input_location);
1127 TREE_SIDE_EFFECTS (expression) = 1;
1129 /* We have an additional nasty problem here; if this
1130 PROPERTY_REF needs to become a 'getter', then the conversion
1131 from PROPERTY_REF into a getter call happens in gimplify,
1132 after the selector table has already been generated and it is
1133 too late to add another selector to it. To work around the
1134 problem, we always put the selector in the table at this
1135 stage, as if we were building the method call here. And the
1136 easiest way to do this is precisely to build the method call,
1137 then discard it. Note that if the PROPERTY_REF becomes a
1138 'setter' instead of a 'getter', then we have added a selector
1139 too many to the selector table. This is a little
1142 TODO: This can be made more efficient; in particular we don't
1143 need to build the whole message call, we could just work on
1145 objc_finish_message_expr (object,
1146 PROPERTY_GETTER_NAME (x),
1155 /* This is used because we don't want to expose PROPERTY_REF to the
1156 C/C++ frontends. Maybe we should! */
1158 objc_is_property_ref (tree node)
1160 if (node && TREE_CODE (node) == PROPERTY_REF)
1166 /* This hook routine is called when a MODIFY_EXPR is being built. We
1167 check what is being modified; if it is a PROPERTY_REF, we need to
1168 generate a 'setter' function call for the property. If this is not
1169 a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go
1170 on creating their MODIFY_EXPR.
1172 This is used for example if you write
1176 where 'count' is a property. The left-hand side creates a
1177 PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR
1178 to assign something to it. We intercept that here, and generate a
1179 call to the 'setter' method instead. */
1181 objc_maybe_build_modify_expr (tree lhs, tree rhs)
1183 if (lhs && TREE_CODE (lhs) == PROPERTY_REF)
1185 tree object_expr = PROPERTY_REF_OBJECT (lhs);
1186 tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs);
1188 if (PROPERTY_READONLY (property_decl))
1190 error ("readonly property can not be set");
1191 return error_mark_node;
1195 tree setter_argument = build_tree_list (NULL_TREE, rhs);
1197 /* TODO: Decay argument in C. */
1198 setter = objc_finish_message_expr (object_expr,
1199 PROPERTY_SETTER_NAME (property_decl),
1209 objc_build_method_signature (bool is_class_method, tree rettype, tree selector,
1210 tree optparms, bool ellipsis)
1212 if (is_class_method)
1213 return build_method_decl (CLASS_METHOD_DECL, rettype, selector,
1214 optparms, ellipsis);
1216 return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector,
1217 optparms, ellipsis);
1221 objc_add_method_declaration (bool is_class_method, tree decl, tree attributes)
1223 if (!objc_interface_context)
1225 /* PS: At the moment, due to how the parser works, it should be
1226 impossible to get here. But it's good to have the check in
1227 case the parser changes.
1229 fatal_error ("method declaration not in @interface context");
1232 objc_decl_method_attributes (&decl, attributes, 0);
1233 objc_add_method (objc_interface_context,
1236 objc_method_optional_flag);
1239 /* Return 'true' if the method definition could be started, and
1240 'false' if not (because we are outside an @implementation context).
1243 objc_start_method_definition (bool is_class_method, tree decl, tree attributes)
1245 if (!objc_implementation_context)
1247 error ("method definition not in @implementation context");
1251 if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node)
1255 /* Indicate no valid break/continue context by setting these variables
1256 to some non-null, non-label value. We'll notice and emit the proper
1257 error message in c_finish_bc_stmt. */
1258 c_break_label = c_cont_label = size_zero_node;
1261 objc_decl_method_attributes (&decl, attributes, 0);
1262 objc_add_method (objc_implementation_context,
1265 /* is optional */ false);
1266 start_method_def (decl);
1271 objc_add_instance_variable (tree decl)
1273 (void) add_instance_variable (objc_ivar_context,
1274 objc_ivar_visibility,
1278 /* Return true if TYPE is 'id'. */
1281 objc_is_object_id (tree type)
1283 return OBJC_TYPE_NAME (type) == objc_object_id;
1287 objc_is_class_id (tree type)
1289 return OBJC_TYPE_NAME (type) == objc_class_id;
1292 /* Construct a C struct with same name as KLASS, a base struct with tag
1293 SUPER_NAME (if any), and FIELDS indicated. */
1296 objc_build_struct (tree klass, tree fields, tree super_name)
1298 tree name = CLASS_NAME (klass);
1299 tree s = objc_start_struct (name);
1300 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
1302 VEC(tree,heap) *objc_info = NULL;
1307 /* Prepend a packed variant of the base class into the layout. This
1308 is necessary to preserve ObjC ABI compatibility. */
1309 tree base = build_decl (input_location,
1310 FIELD_DECL, NULL_TREE, super);
1311 tree field = TYPE_FIELDS (super);
1313 while (field && DECL_CHAIN (field)
1314 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
1315 field = DECL_CHAIN (field);
1317 /* For ObjC ABI purposes, the "packed" size of a base class is
1318 the sum of the offset and the size (in bits) of the last field
1321 = (field && TREE_CODE (field) == FIELD_DECL
1322 ? size_binop (PLUS_EXPR,
1323 size_binop (PLUS_EXPR,
1326 convert (bitsizetype,
1327 DECL_FIELD_OFFSET (field)),
1328 bitsize_int (BITS_PER_UNIT)),
1329 DECL_FIELD_BIT_OFFSET (field)),
1331 : bitsize_zero_node);
1332 DECL_SIZE_UNIT (base)
1333 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
1334 size_int (BITS_PER_UNIT));
1335 DECL_ARTIFICIAL (base) = 1;
1336 DECL_ALIGN (base) = 1;
1337 DECL_FIELD_CONTEXT (base) = s;
1339 DECL_FIELD_IS_BASE (base) = 1;
1342 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
1343 #endif /* are following the ObjC ABI here. */
1344 DECL_CHAIN (base) = fields;
1348 /* NB: Calling finish_struct() may cause type TYPE_LANG_SPECIFIC fields
1349 in all variants of this RECORD_TYPE to be clobbered, but it is therein
1350 that we store protocol conformance info (e.g., 'NSObject <MyProtocol>').
1351 Hence, we must squirrel away the ObjC-specific information before calling
1352 finish_struct(), and then reinstate it afterwards. */
1354 for (t = TYPE_NEXT_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
1356 if (!TYPE_HAS_OBJC_INFO (t))
1358 INIT_TYPE_OBJC_INFO (t);
1359 TYPE_OBJC_INTERFACE (t) = klass;
1361 VEC_safe_push (tree, heap, objc_info, TYPE_OBJC_INFO (t));
1364 /* Point the struct at its related Objective-C class. */
1365 INIT_TYPE_OBJC_INFO (s);
1366 TYPE_OBJC_INTERFACE (s) = klass;
1368 s = objc_finish_struct (s, fields);
1370 for (i = 0, t = TYPE_NEXT_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
1372 TYPE_OBJC_INFO (t) = VEC_index (tree, objc_info, i);
1373 /* Replace the IDENTIFIER_NODE with an actual @interface. */
1374 TYPE_OBJC_INTERFACE (t) = klass;
1376 VEC_free (tree, heap, objc_info);
1378 /* Use TYPE_BINFO structures to point at the super class, if any. */
1379 objc_xref_basetypes (s, super);
1381 /* Mark this struct as a class template. */
1382 CLASS_STATIC_TEMPLATE (klass) = s;
1387 /* Build a type differing from TYPE only in that TYPE_VOLATILE is set.
1388 Unlike tree.c:build_qualified_type(), preserve TYPE_LANG_SPECIFIC in the
1391 objc_build_volatilized_type (tree type)
1395 /* Check if we have not constructed the desired variant already. */
1396 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1398 /* The type qualifiers must (obviously) match up. */
1399 if (!TYPE_VOLATILE (t)
1400 || (TYPE_READONLY (t) != TYPE_READONLY (type))
1401 || (TYPE_RESTRICT (t) != TYPE_RESTRICT (type)))
1404 /* For pointer types, the pointees (and hence their TYPE_LANG_SPECIFIC
1405 info, if any) must match up. */
1406 if (POINTER_TYPE_P (t)
1407 && (TREE_TYPE (t) != TREE_TYPE (type)))
1410 /* Only match up the types which were previously volatilized in similar fashion and not
1411 because they were declared as such. */
1412 if (!lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (t)))
1415 /* Everything matches up! */
1419 /* Ok, we could not re-use any of the pre-existing variants. Create
1421 t = build_variant_type_copy (type);
1422 TYPE_VOLATILE (t) = 1;
1424 TYPE_ATTRIBUTES (t) = merge_attributes (TYPE_ATTRIBUTES (type),
1425 tree_cons (get_identifier ("objc_volatilized"),
1428 if (TREE_CODE (t) == ARRAY_TYPE)
1429 TREE_TYPE (t) = objc_build_volatilized_type (TREE_TYPE (t));
1431 /* Set up the canonical type information. */
1432 if (TYPE_STRUCTURAL_EQUALITY_P (type))
1433 SET_TYPE_STRUCTURAL_EQUALITY (t);
1434 else if (TYPE_CANONICAL (type) != type)
1435 TYPE_CANONICAL (t) = objc_build_volatilized_type (TYPE_CANONICAL (type));
1437 TYPE_CANONICAL (t) = t;
1442 /* Mark DECL as being 'volatile' for purposes of Darwin
1443 _setjmp()/_longjmp() exception handling. Called from
1444 objc_mark_locals_volatile(). */
1446 objc_volatilize_decl (tree decl)
1448 /* Do not mess with variables that are 'static' or (already)
1450 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
1451 && (TREE_CODE (decl) == VAR_DECL
1452 || TREE_CODE (decl) == PARM_DECL))
1454 tree t = TREE_TYPE (decl);
1456 t = objc_build_volatilized_type (t);
1458 TREE_TYPE (decl) = t;
1459 TREE_THIS_VOLATILE (decl) = 1;
1460 TREE_SIDE_EFFECTS (decl) = 1;
1461 DECL_REGISTER (decl) = 0;
1463 C_DECL_REGISTER (decl) = 0;
1468 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
1469 (including its categories and superclasses) or by object type TYP.
1470 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
1473 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
1475 bool class_type = (cls != NULL_TREE);
1481 /* Check protocols adopted by the class and its categories. */
1482 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
1484 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
1488 /* Repeat for superclasses. */
1489 cls = lookup_interface (CLASS_SUPER_NAME (cls));
1492 /* Check for any protocols attached directly to the object type. */
1493 if (TYPE_HAS_OBJC_INFO (typ))
1495 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
1502 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
1503 /* NB: Types 'id' and 'Class' cannot reasonably be described as
1504 "implementing" a given protocol, since they do not have an
1507 warning (0, "class %qs does not implement the %qE protocol",
1508 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
1510 warning (0, "type %qs does not conform to the %qE protocol",
1511 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
1517 /* Check if class RCLS and instance struct type RTYP conform to at least the
1518 same protocols that LCLS and LTYP conform to. */
1521 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
1524 bool have_lproto = false;
1528 /* NB: We do _not_ look at categories defined for LCLS; these may or
1529 may not get loaded in, and therefore it is unreasonable to require
1530 that RCLS/RTYP must implement any of their protocols. */
1531 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
1535 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
1539 /* Repeat for superclasses. */
1540 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
1543 /* Check for any protocols attached directly to the object type. */
1544 if (TYPE_HAS_OBJC_INFO (ltyp))
1546 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
1550 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
1555 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
1556 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
1557 away with simply checking for 'id' or 'Class' (!RCLS), since this
1558 routine will not get called in other cases. */
1559 return have_lproto || (rcls != NULL_TREE);
1562 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
1563 Both TYPE1 and TYPE2 must be pointers, and already determined to be
1564 compatible by objc_compare_types() below. */
1567 objc_common_type (tree type1, tree type2)
1569 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
1571 while (POINTER_TYPE_P (inner1))
1573 inner1 = TREE_TYPE (inner1);
1574 inner2 = TREE_TYPE (inner2);
1577 /* If one type is derived from another, return the base type. */
1578 if (DERIVED_FROM_P (inner1, inner2))
1580 else if (DERIVED_FROM_P (inner2, inner1))
1583 /* If both types are 'Class', return 'Class'. */
1584 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
1585 return objc_class_type;
1587 /* Otherwise, return 'id'. */
1588 return objc_object_type;
1591 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
1592 an instance of RTYP to an instance of LTYP or to compare the two
1593 (if ARGNO is equal to -3), per ObjC type system rules. Before
1594 returning 'true', this routine may issue warnings related to, e.g.,
1595 protocol conformance. When returning 'false', the routine must
1596 produce absolutely no warnings; the C or C++ front-end will do so
1597 instead, if needed. If either LTYP or RTYP is not an Objective-C type,
1598 the routine must return 'false'.
1600 The ARGNO parameter is encoded as follows:
1601 >= 1 Parameter number (CALLEE contains function being called);
1605 -3 Comparison (LTYP and RTYP may match in either direction);
1606 -4 Silent comparison (for C++ overload resolution).
1610 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
1612 tree lcls, rcls, lproto, rproto;
1613 bool pointers_compatible;
1615 /* We must be dealing with pointer types */
1616 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
1621 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
1622 rtyp = TREE_TYPE (rtyp);
1624 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
1626 /* We must also handle function pointers, since ObjC is a bit more
1627 lenient than C or C++ on this. */
1628 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
1630 /* Return types must be covariant. */
1631 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
1632 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
1636 /* Argument types must be contravariant. */
1637 for (ltyp = TYPE_ARG_TYPES (ltyp), rtyp = TYPE_ARG_TYPES (rtyp);
1638 ltyp && rtyp; ltyp = TREE_CHAIN (ltyp), rtyp = TREE_CHAIN (rtyp))
1640 if (!comptypes (TREE_VALUE (rtyp), TREE_VALUE (ltyp))
1641 && !objc_compare_types (TREE_VALUE (rtyp), TREE_VALUE (ltyp),
1646 return (ltyp == rtyp);
1649 /* Past this point, we are only interested in ObjC class instances,
1650 or 'id' or 'Class'. */
1651 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
1654 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
1655 && !TYPE_HAS_OBJC_INFO (ltyp))
1658 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
1659 && !TYPE_HAS_OBJC_INFO (rtyp))
1662 /* Past this point, we are committed to returning 'true' to the caller
1663 (unless performing a silent comparison; see below). However, we can
1664 still warn about type and/or protocol mismatches. */
1666 if (TYPE_HAS_OBJC_INFO (ltyp))
1668 lcls = TYPE_OBJC_INTERFACE (ltyp);
1669 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
1672 lcls = lproto = NULL_TREE;
1674 if (TYPE_HAS_OBJC_INFO (rtyp))
1676 rcls = TYPE_OBJC_INTERFACE (rtyp);
1677 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
1680 rcls = rproto = NULL_TREE;
1682 /* If we could not find an @interface declaration, we must have
1683 only seen a @class declaration; for purposes of type comparison,
1684 treat it as a stand-alone (root) class. */
1686 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
1689 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
1692 /* If either type is an unqualified 'id', we're done. */
1693 if ((!lproto && objc_is_object_id (ltyp))
1694 || (!rproto && objc_is_object_id (rtyp)))
1697 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
1699 /* If the underlying types are the same, and at most one of them has
1700 a protocol list, we do not need to issue any diagnostics. */
1701 if (pointers_compatible && (!lproto || !rproto))
1704 /* If exactly one of the types is 'Class', issue a diagnostic; any
1705 exceptions of this rule have already been handled. */
1706 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
1707 pointers_compatible = false;
1708 /* Otherwise, check for inheritance relations. */
1711 if (!pointers_compatible)
1713 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
1715 if (!pointers_compatible)
1716 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
1718 if (!pointers_compatible && argno <= -3)
1719 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
1722 /* If the pointers match modulo protocols, check for protocol conformance
1724 if (pointers_compatible)
1726 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
1729 if (!pointers_compatible && argno == -3)
1730 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
1734 if (!pointers_compatible)
1736 /* The two pointers are not exactly compatible. Issue a warning, unless
1737 we are performing a silent comparison, in which case return 'false'
1739 /* NB: For the time being, we shall make our warnings look like their
1740 C counterparts. In the future, we may wish to make them more
1748 warning (0, "comparison of distinct Objective-C types lacks a cast");
1752 warning (0, "initialization from distinct Objective-C type");
1756 warning (0, "assignment from distinct Objective-C type");
1760 warning (0, "distinct Objective-C type in return");
1764 warning (0, "passing argument %d of %qE from distinct "
1765 "Objective-C type", argno, callee);
1773 /* This routine is similar to objc_compare_types except that function-pointers are
1774 excluded. This is because, caller assumes that common types are of (id, Object*)
1775 variety and calls objc_common_type to obtain a common type. There is no commonolty
1776 between two function-pointers in this regard. */
1779 objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
1781 if (objc_compare_types (ltyp, rtyp, argno, callee))
1783 /* exclude function-pointer types. */
1786 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
1787 rtyp = TREE_TYPE (rtyp);
1789 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
1790 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
1795 /* Check if LTYP and RTYP have the same type qualifiers. If either type
1796 lives in the volatilized hash table, ignore the 'volatile' bit when
1797 making the comparison. */
1800 objc_type_quals_match (tree ltyp, tree rtyp)
1802 int lquals = TYPE_QUALS (ltyp), rquals = TYPE_QUALS (rtyp);
1804 if (lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (ltyp)))
1805 lquals &= ~TYPE_QUAL_VOLATILE;
1807 if (lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (rtyp)))
1808 rquals &= ~TYPE_QUAL_VOLATILE;
1810 return (lquals == rquals);
1814 /* Determine if CHILD is derived from PARENT. The routine assumes that
1815 both parameters are RECORD_TYPEs, and is non-reflexive. */
1818 objc_derived_from_p (tree parent, tree child)
1820 parent = TYPE_MAIN_VARIANT (parent);
1822 for (child = TYPE_MAIN_VARIANT (child);
1823 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
1825 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
1826 (TYPE_BINFO (child),
1829 if (child == parent)
1838 objc_build_component_ref (tree datum, tree component)
1840 /* If COMPONENT is NULL, the caller is referring to the anonymous
1841 base class field. */
1844 tree base = TYPE_FIELDS (TREE_TYPE (datum));
1846 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
1849 /* The 'build_component_ref' routine has been removed from the C++
1850 front-end, but 'finish_class_member_access_expr' seems to be
1851 a worthy substitute. */
1853 return finish_class_member_access_expr (datum, component, false,
1854 tf_warning_or_error);
1856 return build_component_ref (input_location, datum, component);
1860 /* Recursively copy inheritance information rooted at BINFO. To do this,
1861 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
1864 objc_copy_binfo (tree binfo)
1866 tree btype = BINFO_TYPE (binfo);
1867 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
1871 BINFO_TYPE (binfo2) = btype;
1872 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
1873 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
1875 /* Recursively copy base binfos of BINFO. */
1876 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1878 tree base_binfo2 = objc_copy_binfo (base_binfo);
1880 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
1881 BINFO_BASE_APPEND (binfo2, base_binfo2);
1887 /* Record superclass information provided in BASETYPE for ObjC class REF.
1888 This is loosely based on cp/decl.c:xref_basetypes(). */
1891 objc_xref_basetypes (tree ref, tree basetype)
1893 tree binfo = make_tree_binfo (basetype ? 1 : 0);
1895 TYPE_BINFO (ref) = binfo;
1896 BINFO_OFFSET (binfo) = size_zero_node;
1897 BINFO_TYPE (binfo) = ref;
1901 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
1903 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
1904 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, 1);
1905 BINFO_BASE_APPEND (binfo, base_binfo);
1906 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
1910 /* Called from finish_decl. */
1913 objc_check_decl (tree decl)
1915 tree type = TREE_TYPE (decl);
1917 if (TREE_CODE (type) != RECORD_TYPE)
1919 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
1920 error ("statically allocated instance of Objective-C class %qE",
1925 objc_check_global_decl (tree decl)
1927 tree id = DECL_NAME (decl);
1928 if (objc_is_class_name (id) && global_bindings_p())
1929 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id));
1932 /* Return a non-volatalized version of TYPE. */
1935 objc_non_volatilized_type (tree type)
1937 if (lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (type)))
1938 type = build_qualified_type (type, (TYPE_QUALS (type) & ~TYPE_QUAL_VOLATILE));
1942 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where INTERFACE may
1943 either name an Objective-C class, or refer to the special 'id' or 'Class'
1944 types. If INTERFACE is not a valid ObjC type, just return it unchanged. */
1947 objc_get_protocol_qualified_type (tree interface, tree protocols)
1949 /* If INTERFACE is not provided, default to 'id'. */
1950 tree type = (interface ? objc_is_id (interface) : objc_object_type);
1951 bool is_ptr = (type != NULL_TREE);
1955 type = objc_is_class_name (interface);
1959 /* If looking at a typedef, retrieve the precise type it
1961 if (TREE_CODE (interface) == IDENTIFIER_NODE)
1962 interface = identifier_global_value (interface);
1964 type = ((interface && TREE_CODE (interface) == TYPE_DECL
1965 && DECL_ORIGINAL_TYPE (interface))
1966 ? DECL_ORIGINAL_TYPE (interface)
1967 : xref_tag (RECORD_TYPE, type));
1975 type = build_variant_type_copy (type);
1977 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
1981 tree orig_pointee_type = TREE_TYPE (type);
1982 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
1984 /* Set up the canonical type information. */
1985 TYPE_CANONICAL (type)
1986 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
1988 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
1989 type = TREE_TYPE (type);
1992 /* Look up protocols and install in lang specific list. */
1993 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
1994 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols (protocols);
1996 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
1997 return the pointer to the new pointee variant. */
1999 type = TYPE_POINTER_TO (type);
2001 TYPE_OBJC_INTERFACE (type)
2002 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
2008 /* Check for circular dependencies in protocols. The arguments are
2009 PROTO, the protocol to check, and LIST, a list of protocol it
2013 check_protocol_recursively (tree proto, tree list)
2017 for (p = list; p; p = TREE_CHAIN (p))
2019 tree pp = TREE_VALUE (p);
2021 if (TREE_CODE (pp) == IDENTIFIER_NODE)
2022 pp = lookup_protocol (pp);
2025 fatal_error ("protocol %qE has circular dependency",
2026 PROTOCOL_NAME (pp));
2028 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
2032 /* Look up PROTOCOLS, and return a list of those that are found.
2033 If none are found, return NULL. */
2036 lookup_and_install_protocols (tree protocols)
2039 tree return_value = NULL_TREE;
2041 if (protocols == error_mark_node)
2044 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
2046 tree ident = TREE_VALUE (proto);
2047 tree p = lookup_protocol (ident);
2050 return_value = chainon (return_value,
2051 build_tree_list (NULL_TREE, p));
2052 else if (ident != error_mark_node)
2053 error ("cannot find protocol declaration for %qE",
2057 return return_value;
2060 /* Create a declaration for field NAME of a given TYPE. */
2063 create_field_decl (tree type, const char *name)
2065 return build_decl (input_location,
2066 FIELD_DECL, get_identifier (name), type);
2069 /* Create a global, static declaration for variable NAME of a given TYPE. The
2070 finish_var_decl() routine will need to be called on it afterwards. */
2073 start_var_decl (tree type, const char *name)
2075 tree var = build_decl (input_location,
2076 VAR_DECL, get_identifier (name), type);
2078 TREE_STATIC (var) = 1;
2079 DECL_INITIAL (var) = error_mark_node; /* A real initializer is coming... */
2080 DECL_IGNORED_P (var) = 1;
2081 DECL_ARTIFICIAL (var) = 1;
2082 DECL_CONTEXT (var) = NULL_TREE;
2084 DECL_THIS_STATIC (var) = 1; /* squash redeclaration errors */
2090 /* Finish off the variable declaration created by start_var_decl(). */
2093 finish_var_decl (tree var, tree initializer)
2095 finish_decl (var, input_location, initializer, NULL_TREE, NULL_TREE);
2098 /* Find the decl for the constant string class reference. This is only
2099 used for the NeXT runtime. */
2102 setup_string_decl (void)
2107 /* %s in format will provide room for terminating null */
2108 length = strlen (STRING_OBJECT_GLOBAL_FORMAT)
2109 + strlen (constant_string_class_name);
2110 name = XNEWVEC (char, length);
2111 sprintf (name, STRING_OBJECT_GLOBAL_FORMAT,
2112 constant_string_class_name);
2113 constant_string_global_id = get_identifier (name);
2114 string_class_decl = lookup_name (constant_string_global_id);
2116 return string_class_decl;
2119 /* Purpose: "play" parser, creating/installing representations
2120 of the declarations that are required by Objective-C.
2124 type_spec--------->sc_spec
2125 (tree_list) (tree_list)
2128 identifier_node identifier_node */
2131 synth_module_prologue (void)
2134 enum debug_info_type save_write_symbols = write_symbols;
2135 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
2137 /* Suppress outputting debug symbols, because
2138 dbxout_init hasn't been called yet. */
2139 write_symbols = NO_DEBUG;
2140 debug_hooks = &do_nothing_debug_hooks;
2143 push_lang_context (lang_name_c); /* extern "C" */
2146 /* The following are also defined in <objc/objc.h> and friends. */
2148 objc_object_id = get_identifier (TAG_OBJECT);
2149 objc_class_id = get_identifier (TAG_CLASS);
2151 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
2152 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
2154 objc_object_type = build_pointer_type (objc_object_reference);
2155 objc_class_type = build_pointer_type (objc_class_reference);
2157 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
2158 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
2160 /* Declare the 'id' and 'Class' typedefs. */
2162 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2166 TREE_NO_WARNING (type) = 1;
2167 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2171 TREE_NO_WARNING (type) = 1;
2173 /* Forward-declare '@interface Protocol'. */
2175 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
2176 objc_declare_class (tree_cons (NULL_TREE, type, NULL_TREE));
2177 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE,
2180 /* Declare type of selector-objects that represent an operation name. */
2182 if (flag_next_runtime)
2183 /* `struct objc_selector *' */
2185 = build_pointer_type (xref_tag (RECORD_TYPE,
2186 get_identifier (TAG_SELECTOR)));
2188 /* `const struct objc_selector *' */
2190 = build_pointer_type
2191 (build_qualified_type (xref_tag (RECORD_TYPE,
2192 get_identifier (TAG_SELECTOR)),
2195 /* Declare receiver type used for dispatching messages to 'super'. */
2197 /* `struct objc_super *' */
2198 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
2199 get_identifier (TAG_SUPER)));
2201 /* Declare pointers to method and ivar lists. */
2202 objc_method_list_ptr = build_pointer_type
2203 (xref_tag (RECORD_TYPE,
2204 get_identifier (UTAG_METHOD_LIST)));
2205 objc_method_proto_list_ptr
2206 = build_pointer_type (xref_tag (RECORD_TYPE,
2207 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
2208 objc_ivar_list_ptr = build_pointer_type
2209 (xref_tag (RECORD_TYPE,
2210 get_identifier (UTAG_IVAR_LIST)));
2212 /* TREE_NOTHROW is cleared for the message-sending functions,
2213 because the function that gets called can throw in Obj-C++, or
2214 could itself call something that can throw even in Obj-C. */
2216 if (flag_next_runtime)
2218 /* NB: In order to call one of the ..._stret (struct-returning)
2219 functions, the function *MUST* first be cast to a signature that
2220 corresponds to the actual ObjC method being invoked. This is
2221 what is done by the build_objc_method_call() routine below. */
2223 /* id objc_msgSend (id, SEL, ...); */
2224 /* id objc_msgSendNonNil (id, SEL, ...); */
2225 /* id objc_msgSend_stret (id, SEL, ...); */
2226 /* id objc_msgSendNonNil_stret (id, SEL, ...); */
2228 = build_varargs_function_type_list (objc_object_type,
2232 umsg_decl = add_builtin_function (TAG_MSGSEND,
2233 type, 0, NOT_BUILT_IN,
2235 umsg_nonnil_decl = add_builtin_function (TAG_MSGSEND_NONNIL,
2236 type, 0, NOT_BUILT_IN,
2238 umsg_stret_decl = add_builtin_function (TAG_MSGSEND_STRET,
2239 type, 0, NOT_BUILT_IN,
2241 umsg_nonnil_stret_decl = add_builtin_function (TAG_MSGSEND_NONNIL_STRET,
2242 type, 0, NOT_BUILT_IN,
2245 /* These can throw, because the function that gets called can throw
2246 in Obj-C++, or could itself call something that can throw even
2248 TREE_NOTHROW (umsg_decl) = 0;
2249 TREE_NOTHROW (umsg_nonnil_decl) = 0;
2250 TREE_NOTHROW (umsg_stret_decl) = 0;
2251 TREE_NOTHROW (umsg_nonnil_stret_decl) = 0;
2253 /* id objc_msgSend_Fast (id, SEL, ...)
2254 __attribute__ ((hard_coded_address (OFFS_MSGSEND_FAST))); */
2255 #ifdef OFFS_MSGSEND_FAST
2256 umsg_fast_decl = add_builtin_function (TAG_MSGSEND_FAST,
2257 type, 0, NOT_BUILT_IN,
2259 TREE_NOTHROW (umsg_fast_decl) = 0;
2260 DECL_ATTRIBUTES (umsg_fast_decl)
2261 = tree_cons (get_identifier ("hard_coded_address"),
2262 build_int_cst (NULL_TREE, OFFS_MSGSEND_FAST),
2265 /* No direct dispatch available. */
2266 umsg_fast_decl = umsg_decl;
2269 /* id objc_msgSendSuper (struct objc_super *, SEL, ...); */
2270 /* id objc_msgSendSuper_stret (struct objc_super *, SEL, ...); */
2272 = build_varargs_function_type_list (objc_object_type,
2276 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
2277 type, 0, NOT_BUILT_IN,
2279 umsg_super_stret_decl = add_builtin_function (TAG_MSGSENDSUPER_STRET,
2280 type, 0, NOT_BUILT_IN, 0,
2282 TREE_NOTHROW (umsg_super_decl) = 0;
2283 TREE_NOTHROW (umsg_super_stret_decl) = 0;
2287 /* GNU runtime messenger entry points. */
2289 /* typedef id (*IMP)(id, SEL, ...); */
2291 build_varargs_function_type_list (objc_object_type,
2295 tree IMP_type = build_pointer_type (ftype);
2297 /* IMP objc_msg_lookup (id, SEL); */
2298 type = build_function_type_list (IMP_type,
2302 umsg_decl = add_builtin_function (TAG_MSGSEND,
2303 type, 0, NOT_BUILT_IN,
2305 TREE_NOTHROW (umsg_decl) = 0;
2307 /* IMP objc_msg_lookup_super (struct objc_super *, SEL); */
2309 = build_function_type_list (IMP_type,
2313 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
2314 type, 0, NOT_BUILT_IN,
2316 TREE_NOTHROW (umsg_super_decl) = 0;
2318 /* The following GNU runtime entry point is called to initialize
2321 __objc_exec_class (void *); */
2323 = build_function_type_list (void_type_node,
2326 execclass_decl = add_builtin_function (TAG_EXECCLASS,
2327 type, 0, NOT_BUILT_IN,
2331 /* id objc_getClass (const char *); */
2333 type = build_function_type_list (objc_object_type,
2334 const_string_type_node,
2338 = add_builtin_function (TAG_GETCLASS, type, 0, NOT_BUILT_IN,
2341 /* id objc_getMetaClass (const char *); */
2343 objc_get_meta_class_decl
2344 = add_builtin_function (TAG_GETMETACLASS, type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
2346 build_class_template ();
2347 build_super_template ();
2348 build_protocol_template ();
2349 build_category_template ();
2350 build_objc_exception_stuff ();
2352 /* Declare objc_getProperty, object_setProperty and other property
2353 accessor helpers. */
2354 build_objc_property_accessor_helpers ();
2356 if (flag_next_runtime)
2357 build_next_objc_exception_stuff ();
2359 /* static SEL _OBJC_SELECTOR_TABLE[]; */
2361 if (! flag_next_runtime)
2362 build_selector_table_decl ();
2364 /* Forward declare constant_string_id and constant_string_type. */
2365 if (!constant_string_class_name)
2366 constant_string_class_name = default_constant_string_class_name;
2368 constant_string_id = get_identifier (constant_string_class_name);
2369 objc_declare_class (tree_cons (NULL_TREE, constant_string_id, NULL_TREE));
2371 /* Pre-build the following entities - for speed/convenience. */
2372 self_id = get_identifier ("self");
2373 ucmd_id = get_identifier ("_cmd");
2375 /* Declare struct _objc_fast_enumeration_state { ... }; */
2376 build_fast_enumeration_state_template ();
2378 /* void objc_enumeration_mutation (id) */
2379 type = build_function_type (void_type_node,
2380 tree_cons (NULL_TREE, objc_object_type, NULL_TREE));
2381 objc_enumeration_mutation_decl
2382 = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN,
2384 TREE_NOTHROW (objc_enumeration_mutation_decl) = 0;
2387 pop_lang_context ();
2390 write_symbols = save_write_symbols;
2391 debug_hooks = save_hooks;
2394 /* Ensure that the ivar list for NSConstantString/NXConstantString
2395 (or whatever was specified via `-fconstant-string-class')
2396 contains fields at least as large as the following three, so that
2397 the runtime can stomp on them with confidence:
2399 struct STRING_OBJECT_CLASS_NAME
2403 unsigned int length;
2407 check_string_class_template (void)
2409 tree field_decl = objc_get_class_ivars (constant_string_id);
2411 #define AT_LEAST_AS_LARGE_AS(F, T) \
2412 (F && TREE_CODE (F) == FIELD_DECL \
2413 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
2414 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
2416 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
2419 field_decl = DECL_CHAIN (field_decl);
2420 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
2423 field_decl = DECL_CHAIN (field_decl);
2424 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
2426 #undef AT_LEAST_AS_LARGE_AS
2429 /* Avoid calling `check_string_class_template ()' more than once. */
2430 static GTY(()) int string_layout_checked;
2432 /* Construct an internal string layout to be used as a template for
2433 creating NSConstantString/NXConstantString instances. */
2436 objc_build_internal_const_str_type (void)
2438 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
2439 tree fields = build_decl (input_location,
2440 FIELD_DECL, NULL_TREE, ptr_type_node);
2441 tree field = build_decl (input_location,
2442 FIELD_DECL, NULL_TREE, ptr_type_node);
2444 DECL_CHAIN (field) = fields; fields = field;
2445 field = build_decl (input_location,
2446 FIELD_DECL, NULL_TREE, unsigned_type_node);
2447 DECL_CHAIN (field) = fields; fields = field;
2448 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
2450 finish_builtin_struct (type, "__builtin_ObjCString",
2456 /* Custom build_string which sets TREE_TYPE! */
2459 my_build_string (int len, const char *str)
2461 return fix_string_type (build_string (len, str));
2464 /* Build a string with contents STR and length LEN and convert it to a
2468 my_build_string_pointer (int len, const char *str)
2470 tree string = my_build_string (len, str);
2471 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
2472 return build1 (ADDR_EXPR, ptrtype, string);
2476 string_hash (const void *ptr)
2478 const_tree const str = ((const struct string_descriptor *)ptr)->literal;
2479 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
2480 int i, len = TREE_STRING_LENGTH (str);
2483 for (i = 0; i < len; i++)
2484 h = ((h * 613) + p[i]);
2490 string_eq (const void *ptr1, const void *ptr2)
2492 const_tree const str1 = ((const struct string_descriptor *)ptr1)->literal;
2493 const_tree const str2 = ((const struct string_descriptor *)ptr2)->literal;
2494 int len1 = TREE_STRING_LENGTH (str1);
2496 return (len1 == TREE_STRING_LENGTH (str2)
2497 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
2501 /* Given a chain of STRING_CST's, build a static instance of
2502 NXConstantString which points at the concatenation of those
2503 strings. We place the string object in the __string_objects
2504 section of the __OBJC segment. The Objective-C runtime will
2505 initialize the isa pointers of the string objects to point at the
2506 NXConstantString class object. */
2509 objc_build_string_object (tree string)
2511 tree constant_string_class;
2514 struct string_descriptor *desc, key;
2517 /* Prep the string argument. */
2518 string = fix_string_type (string);
2519 TREE_SET_CODE (string, STRING_CST);
2520 length = TREE_STRING_LENGTH (string) - 1;
2522 /* The target may have different ideas on how to construct an ObjC string
2523 literal. On Darwin (Mac OS X), for example, we may wish to obtain a
2524 constant CFString reference instead.
2525 At present, this is only supported for the NeXT runtime. */
2526 if (flag_next_runtime && targetcm.objc_construct_string)
2528 tree constructor = (*targetcm.objc_construct_string) (string);
2530 return build1 (NOP_EXPR, objc_object_type, constructor);
2533 /* Check whether the string class being used actually exists and has the
2534 correct ivar layout. */
2535 if (!string_layout_checked)
2537 string_layout_checked = -1;
2538 constant_string_class = lookup_interface (constant_string_id);
2539 internal_const_str_type = objc_build_internal_const_str_type ();
2541 if (!constant_string_class
2542 || !(constant_string_type
2543 = CLASS_STATIC_TEMPLATE (constant_string_class)))
2544 error ("cannot find interface declaration for %qE",
2545 constant_string_id);
2546 /* The NSConstantString/NXConstantString ivar layout is now known. */
2547 else if (!check_string_class_template ())
2548 error ("interface %qE does not have valid constant string layout",
2549 constant_string_id);
2550 /* For the NeXT runtime, we can generate a literal reference
2551 to the string class, don't need to run a constructor. */
2552 else if (flag_next_runtime && !setup_string_decl ())
2553 error ("cannot find reference tag for class %qE",
2554 constant_string_id);
2557 string_layout_checked = 1; /* Success! */
2558 add_class_reference (constant_string_id);
2562 if (string_layout_checked == -1)
2563 return error_mark_node;
2565 /* Perhaps we already constructed a constant string just like this one? */
2566 key.literal = string;
2567 loc = htab_find_slot (string_htab, &key, INSERT);
2568 desc = (struct string_descriptor *) *loc;
2572 tree var, constructor;
2573 VEC(constructor_elt,gc) *v = NULL;
2574 *loc = desc = ggc_alloc_string_descriptor ();
2575 desc->literal = string;
2577 /* GNU: (NXConstantString *) & ((__builtin_ObjCString) { NULL, string, length }) */
2578 /* NeXT: (NSConstantString *) & ((__builtin_ObjCString) { isa, string, length }) */
2579 fields = TYPE_FIELDS (internal_const_str_type);
2580 CONSTRUCTOR_APPEND_ELT (v, fields,
2582 ? build_unary_op (input_location,
2583 ADDR_EXPR, string_class_decl, 0)
2584 : build_int_cst (NULL_TREE, 0));
2585 fields = DECL_CHAIN (fields);
2586 CONSTRUCTOR_APPEND_ELT (v, fields,
2587 build_unary_op (input_location,
2588 ADDR_EXPR, string, 1));
2589 fields = DECL_CHAIN (fields);
2590 CONSTRUCTOR_APPEND_ELT (v, fields, build_int_cst (NULL_TREE, length));
2591 constructor = objc_build_constructor (internal_const_str_type, v);
2593 if (!flag_next_runtime)
2595 = objc_add_static_instance (constructor, constant_string_type);
2598 var = build_decl (input_location,
2599 CONST_DECL, NULL, TREE_TYPE (constructor));
2600 DECL_INITIAL (var) = constructor;
2601 TREE_STATIC (var) = 1;
2602 pushdecl_top_level (var);
2605 desc->constructor = constructor;
2608 addr = convert (build_pointer_type (constant_string_type),
2609 build_unary_op (input_location,
2610 ADDR_EXPR, desc->constructor, 1));
2615 /* Declare a static instance of CLASS_DECL initialized by CONSTRUCTOR. */
2617 static GTY(()) int num_static_inst;
2620 objc_add_static_instance (tree constructor, tree class_decl)
2625 /* Find the list of static instances for the CLASS_DECL. Create one if
2627 for (chain = &objc_static_instances;
2628 *chain && TREE_VALUE (*chain) != class_decl;
2629 chain = &TREE_CHAIN (*chain));
2632 *chain = tree_cons (NULL_TREE, class_decl, NULL_TREE);
2633 add_objc_string (OBJC_TYPE_NAME (class_decl), class_names);
2636 sprintf (buf, "_OBJC_INSTANCE_%d", num_static_inst++);
2637 decl = build_decl (input_location,
2638 VAR_DECL, get_identifier (buf), class_decl);
2639 TREE_STATIC (decl) = 1;
2640 DECL_ARTIFICIAL (decl) = 1;
2641 TREE_USED (decl) = 1;
2642 DECL_INITIAL (decl) = constructor;
2644 /* We may be writing something else just now.
2645 Postpone till end of input. */
2646 DECL_DEFER_OUTPUT (decl) = 1;
2647 pushdecl_top_level (decl);
2648 rest_of_decl_compilation (decl, 1, 0);
2650 /* Add the DECL to the head of this CLASS' list. */
2651 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE, decl, TREE_PURPOSE (*chain));
2656 /* Build a static constant CONSTRUCTOR
2657 with type TYPE and elements ELTS. */
2660 objc_build_constructor (tree type, VEC(constructor_elt,gc) *elts)
2662 tree constructor = build_constructor (type, elts);
2664 TREE_CONSTANT (constructor) = 1;
2665 TREE_STATIC (constructor) = 1;
2666 TREE_READONLY (constructor) = 1;
2669 /* Adjust for impedance mismatch. We should figure out how to build
2670 CONSTRUCTORs that consistently please both the C and C++ gods. */
2671 if (!VEC_index (constructor_elt, elts, 0)->index)
2672 TREE_TYPE (constructor) = init_list_type_node;
2678 /* Take care of defining and initializing _OBJC_SYMBOLS. */
2680 /* Predefine the following data type:
2688 void *defs[cls_def_cnt + cat_def_cnt];
2692 build_objc_symtab_template (void)
2694 tree fields, *chain = NULL;
2696 objc_symtab_template = objc_start_struct (get_identifier (UTAG_SYMTAB));
2698 /* long sel_ref_cnt; */
2699 fields = add_field_decl (long_integer_type_node, "sel_ref_cnt", &chain);
2702 add_field_decl (build_pointer_type (objc_selector_type), "refs", &chain);
2704 /* short cls_def_cnt; */
2705 add_field_decl (short_integer_type_node, "cls_def_cnt", &chain);
2707 /* short cat_def_cnt; */
2708 add_field_decl (short_integer_type_node, "cat_def_cnt", &chain);
2710 if (imp_count || cat_count || !flag_next_runtime)
2712 /* void *defs[imp_count + cat_count (+ 1)]; */
2713 /* NB: The index is one less than the size of the array. */
2714 int index = imp_count + cat_count + (flag_next_runtime ? -1: 0);
2715 tree array_type = build_sized_array_type (ptr_type_node, index + 1);
2716 add_field_decl (array_type, "defs", &chain);
2719 objc_finish_struct (objc_symtab_template, fields);
2722 /* Create the initial value for the `defs' field of _objc_symtab.
2723 This is a CONSTRUCTOR. */
2726 init_def_list (tree type)
2729 struct imp_entry *impent;
2730 VEC(constructor_elt,gc) *v = NULL;
2733 for (impent = imp_list; impent; impent = impent->next)
2735 if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
2737 expr = build_unary_op (input_location,
2738 ADDR_EXPR, impent->class_decl, 0);
2739 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2744 for (impent = imp_list; impent; impent = impent->next)
2746 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
2748 expr = build_unary_op (input_location,
2749 ADDR_EXPR, impent->class_decl, 0);
2750 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2754 if (!flag_next_runtime)
2756 /* statics = { ..., _OBJC_STATIC_INSTANCES, ... } */
2757 if (static_instances_decl)
2758 expr = build_unary_op (input_location,
2759 ADDR_EXPR, static_instances_decl, 0);
2761 expr = integer_zero_node;
2763 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2766 return objc_build_constructor (type, v);
2769 /* Construct the initial value for all of _objc_symtab. */
2772 init_objc_symtab (tree type)
2774 VEC(constructor_elt,gc) *v = NULL;
2776 /* sel_ref_cnt = { ..., 5, ... } */
2778 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
2779 build_int_cst (long_integer_type_node, 0));
2781 /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
2783 if (flag_next_runtime || ! sel_ref_chain)
2784 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, convert (
2785 build_pointer_type (objc_selector_type),
2786 integer_zero_node));
2789 tree expr = build_unary_op (input_location, ADDR_EXPR,
2790 UOBJC_SELECTOR_TABLE_decl, 1);
2792 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
2793 convert (build_pointer_type (objc_selector_type),
2797 /* cls_def_cnt = { ..., 5, ... } */
2799 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
2800 build_int_cst (short_integer_type_node, imp_count));
2802 /* cat_def_cnt = { ..., 5, ... } */
2804 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
2805 build_int_cst (short_integer_type_node, cat_count));
2807 /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
2809 if (imp_count || cat_count || !flag_next_runtime)
2812 tree field = TYPE_FIELDS (type);
2813 field = DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (field))));
2815 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init_def_list (TREE_TYPE (field)));
2818 return objc_build_constructor (type, v);
2821 /* Generate forward declarations for metadata such as
2822 'OBJC_CLASS_...'. */
2825 build_metadata_decl (const char *name, tree type)
2829 /* struct TYPE NAME_<name>; */
2830 decl = start_var_decl (type, synth_id_with_class_suffix
2832 objc_implementation_context));
2837 /* Push forward-declarations of all the categories so that
2838 init_def_list can use them in a CONSTRUCTOR. */
2841 forward_declare_categories (void)
2843 struct imp_entry *impent;
2844 tree sav = objc_implementation_context;
2846 for (impent = imp_list; impent; impent = impent->next)
2848 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
2850 /* Set an invisible arg to synth_id_with_class_suffix. */
2851 objc_implementation_context = impent->imp_context;
2852 /* extern struct objc_category _OBJC_CATEGORY_<name>; */
2853 impent->class_decl = build_metadata_decl ("_OBJC_CATEGORY",
2854 objc_category_template);
2857 objc_implementation_context = sav;
2860 /* Create the declaration of _OBJC_SYMBOLS, with type `struct _objc_symtab'
2861 and initialized appropriately. */
2864 generate_objc_symtab_decl (void)
2867 build_objc_symtab_template ();
2868 UOBJC_SYMBOLS_decl = start_var_decl (objc_symtab_template, "_OBJC_SYMBOLS");
2869 finish_var_decl (UOBJC_SYMBOLS_decl,
2870 init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl)));
2874 init_module_descriptor (tree type)
2877 VEC(constructor_elt,gc) *v = NULL;
2879 /* version = { 1, ... } */
2881 expr = build_int_cst (long_integer_type_node, OBJC_VERSION);
2882 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2884 /* size = { ..., sizeof (struct _objc_module), ... } */
2886 expr = convert (long_integer_type_node,
2887 size_in_bytes (objc_module_template));
2888 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2890 /* Don't provide any file name for security reasons. */
2891 /* name = { ..., "", ... } */
2893 expr = add_objc_string (get_identifier (""), class_names);
2894 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2896 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
2898 if (UOBJC_SYMBOLS_decl)
2899 expr = build_unary_op (input_location,
2900 ADDR_EXPR, UOBJC_SYMBOLS_decl, 0);
2902 expr = null_pointer_node;
2903 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2905 return objc_build_constructor (type, v);
2908 /* Write out the data structures to describe Objective C classes defined.
2910 struct _objc_module { ... } _OBJC_MODULE = { ... }; */
2913 build_module_descriptor (void)
2915 tree decls, *chain = NULL;
2918 push_lang_context (lang_name_c); /* extern "C" */
2921 objc_module_template = objc_start_struct (get_identifier (UTAG_MODULE));
2924 decls = add_field_decl (long_integer_type_node, "version", &chain);
2927 add_field_decl (long_integer_type_node, "size", &chain);
2930 add_field_decl (string_type_node, "name", &chain);
2932 /* struct _objc_symtab *symtab; */
2933 add_field_decl (build_pointer_type (xref_tag (RECORD_TYPE,
2934 get_identifier (UTAG_SYMTAB))),
2937 objc_finish_struct (objc_module_template, decls);
2939 /* Create an instance of "_objc_module". */
2940 UOBJC_MODULES_decl = start_var_decl (objc_module_template, "_OBJC_MODULES");
2941 /* This is the root of the metadata for defined classes and categories, it
2942 is referenced by the runtime and, therefore, needed. */
2943 DECL_PRESERVE_P (UOBJC_MODULES_decl) = 1;
2944 finish_var_decl (UOBJC_MODULES_decl,
2945 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl)));
2948 pop_lang_context ();
2952 /* The GNU runtime requires us to provide a static initializer function
2955 static void __objc_gnu_init (void) {
2956 __objc_exec_class (&L_OBJC_MODULES);
2960 build_module_initializer_routine (void)
2965 push_lang_context (lang_name_c); /* extern "C" */
2968 objc_push_parm (build_decl (input_location,
2969 PARM_DECL, NULL_TREE, void_type_node));
2971 objc_start_function (get_identifier (TAG_GNUINIT),
2972 build_function_type_list (void_type_node, NULL_TREE),
2973 NULL_TREE, NULL_TREE);
2975 objc_start_function (get_identifier (TAG_GNUINIT),
2976 build_function_type_list (void_type_node, NULL_TREE),
2977 NULL_TREE, objc_get_parm_info (0));
2979 body = c_begin_compound_stmt (true);
2980 add_stmt (build_function_call
2985 build_unary_op (input_location, ADDR_EXPR,
2986 UOBJC_MODULES_decl, 0))));
2987 add_stmt (c_end_compound_stmt (input_location, body, true));
2989 TREE_PUBLIC (current_function_decl) = 0;
2992 /* For Objective-C++, we will need to call __objc_gnu_init
2993 from objc_generate_static_init_call() below. */
2994 DECL_STATIC_CONSTRUCTOR (current_function_decl) = 1;
2997 GNU_INIT_decl = current_function_decl;
3001 pop_lang_context ();
3006 /* Return 1 if the __objc_gnu_init function has been synthesized and needs
3007 to be called by the module initializer routine. */
3010 objc_static_init_needed_p (void)
3012 return (GNU_INIT_decl != NULL_TREE);
3015 /* Generate a call to the __objc_gnu_init initializer function. */
3018 objc_generate_static_init_call (tree ctors ATTRIBUTE_UNUSED)
3020 add_stmt (build_stmt (input_location, EXPR_STMT,
3021 build_function_call (input_location,
3022 GNU_INIT_decl, NULL_TREE)));
3026 #endif /* OBJCPLUS */
3028 /* Return the DECL of the string IDENT in the SECTION. */
3031 get_objc_string_decl (tree ident, enum string_section section)
3038 chain = class_names_chain;
3040 case meth_var_names:
3041 chain = meth_var_names_chain;
3043 case meth_var_types:
3044 chain = meth_var_types_chain;
3050 for (; chain != 0; chain = TREE_CHAIN (chain))
3051 if (TREE_VALUE (chain) == ident)
3052 return (TREE_PURPOSE (chain));
3058 /* Output references to all statically allocated objects. Return the DECL
3059 for the array built. */
3062 generate_static_references (void)
3064 tree expr = NULL_TREE;
3065 tree class_name, klass, decl;
3066 tree cl_chain, in_chain, type
3067 = build_array_type (build_pointer_type (void_type_node), NULL_TREE);
3068 int num_inst, num_class;
3070 VEC(constructor_elt,gc) *decls = NULL;
3072 if (flag_next_runtime)
3075 for (cl_chain = objc_static_instances, num_class = 0;
3076 cl_chain; cl_chain = TREE_CHAIN (cl_chain), num_class++)
3078 VEC(constructor_elt,gc) *v = NULL;
3080 for (num_inst = 0, in_chain = TREE_PURPOSE (cl_chain);
3081 in_chain; num_inst++, in_chain = TREE_CHAIN (in_chain));
3083 sprintf (buf, "_OBJC_STATIC_INSTANCES_%d", num_class);
3084 decl = start_var_decl (type, buf);
3086 /* Output {class_name, ...}. */
3087 klass = TREE_VALUE (cl_chain);
3088 class_name = get_objc_string_decl (OBJC_TYPE_NAME (klass), class_names);
3089 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3090 build_unary_op (input_location,
3091 ADDR_EXPR, class_name, 1));
3093 /* Output {..., instance, ...}. */
3094 for (in_chain = TREE_PURPOSE (cl_chain);
3095 in_chain; in_chain = TREE_CHAIN (in_chain))
3097 expr = build_unary_op (input_location,
3098 ADDR_EXPR, TREE_VALUE (in_chain), 1);
3099 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3102 /* Output {..., NULL}. */
3103 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
3105 expr = objc_build_constructor (TREE_TYPE (decl), v);
3106 finish_var_decl (decl, expr);
3107 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE,
3108 build_unary_op (input_location,
3109 ADDR_EXPR, decl, 1));
3112 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE, build_int_cst (NULL_TREE, 0));
3113 expr = objc_build_constructor (type, decls);
3114 static_instances_decl = start_var_decl (type, "_OBJC_STATIC_INSTANCES");
3115 finish_var_decl (static_instances_decl, expr);
3118 static GTY(()) int selector_reference_idx;
3121 build_selector_reference_decl (void)
3126 sprintf (buf, "_OBJC_SELECTOR_REFERENCES_%d", selector_reference_idx++);
3127 decl = start_var_decl (objc_selector_type, buf);
3133 build_selector_table_decl (void)
3137 if (flag_typed_selectors)
3139 build_selector_template ();
3140 temp = build_array_type (objc_selector_template, NULL_TREE);
3143 temp = build_array_type (objc_selector_type, NULL_TREE);
3145 UOBJC_SELECTOR_TABLE_decl = start_var_decl (temp, "_OBJC_SELECTOR_TABLE");
3148 /* Just a handy wrapper for add_objc_string. */
3151 build_selector (tree ident)
3153 return convert (objc_selector_type,
3154 add_objc_string (ident, meth_var_names));
3157 /* Used only by build_*_selector_translation_table (). */
3159 diagnose_missing_method (tree meth, location_t here)
3163 for (method_chain = meth_var_names_chain;
3165 method_chain = TREE_CHAIN (method_chain))
3167 if (TREE_VALUE (method_chain) == meth)
3175 warning_at (here, 0, "creating selector for nonexistent method %qE",
3180 build_next_selector_translation_table (void)
3183 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
3186 tree decl = TREE_PURPOSE (chain);
3187 if (warn_selector && objc_implementation_context)
3191 loc = DECL_SOURCE_LOCATION (decl);
3193 loc = input_location;
3194 diagnose_missing_method (TREE_VALUE (chain), loc);
3197 expr = build_selector (TREE_VALUE (chain));
3201 /* Entries of this form are used for references to methods.
3202 The runtime re-writes these on start-up, but the compiler can't see
3203 that and optimizes it away unless we force it. */
3204 DECL_PRESERVE_P (decl) = 1;
3205 finish_var_decl (decl, expr);
3211 build_gnu_selector_translation_table (void)
3215 tree decl = NULL_TREE;*/
3216 VEC(constructor_elt,gc) *inits = NULL;
3218 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
3222 if (warn_selector && objc_implementation_context)
3223 diagnose_missing_method (TREE_VALUE (chain), input_location);
3225 expr = build_selector (TREE_VALUE (chain));
3226 /* add one for the '\0' character
3227 offset += IDENTIFIER_LENGTH (TREE_VALUE (chain)) + 1;*/
3230 if (flag_typed_selectors)
3232 VEC(constructor_elt,gc) *v = NULL;
3233 tree encoding = get_proto_encoding (TREE_PURPOSE (chain));
3234 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3235 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, encoding);
3236 expr = objc_build_constructor (objc_selector_template, v);
3239 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
3241 } /* each element in the chain */
3244 /* Cause the selector table (previously forward-declared)
3245 to be actually output. */
3248 if (flag_typed_selectors)
3250 VEC(constructor_elt,gc) *v = NULL;
3251 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
3252 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
3253 expr = objc_build_constructor (objc_selector_template, v);
3256 expr = integer_zero_node;
3258 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
3259 expr = objc_build_constructor (TREE_TYPE (UOBJC_SELECTOR_TABLE_decl),
3261 finish_var_decl (UOBJC_SELECTOR_TABLE_decl, expr);
3266 get_proto_encoding (tree proto)
3271 if (! METHOD_ENCODING (proto))
3273 encoding = encode_method_prototype (proto);
3274 METHOD_ENCODING (proto) = encoding;
3277 encoding = METHOD_ENCODING (proto);
3279 return add_objc_string (encoding, meth_var_types);
3282 return build_int_cst (NULL_TREE, 0);
3285 /* sel_ref_chain is a list whose "value" fields will be instances of
3286 identifier_node that represent the selector. LOC is the location of
3290 build_typed_selector_reference (location_t loc, tree ident, tree prototype)
3292 tree *chain = &sel_ref_chain;
3298 if (TREE_PURPOSE (*chain) == prototype && TREE_VALUE (*chain) == ident)
3299 goto return_at_index;
3302 chain = &TREE_CHAIN (*chain);
3305 *chain = tree_cons (prototype, ident, NULL_TREE);
3308 expr = build_unary_op (loc, ADDR_EXPR,
3309 build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
3310 build_int_cst (NULL_TREE, index)),
3312 return convert (objc_selector_type, expr);
3316 build_selector_reference (location_t loc, tree ident)
3318 tree *chain = &sel_ref_chain;
3324 if (TREE_VALUE (*chain) == ident)
3325 return (flag_next_runtime
3326 ? TREE_PURPOSE (*chain)
3327 : build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
3328 build_int_cst (NULL_TREE, index)));
3331 chain = &TREE_CHAIN (*chain);
3334 expr = (flag_next_runtime ? build_selector_reference_decl (): NULL_TREE);
3336 *chain = tree_cons (expr, ident, NULL_TREE);
3338 return (flag_next_runtime
3340 : build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
3341 build_int_cst (NULL_TREE, index)));
3344 static GTY(()) int class_reference_idx;
3347 build_class_reference_decl (void)
3352 sprintf (buf, "_OBJC_CLASS_REFERENCES_%d", class_reference_idx++);
3353 decl = start_var_decl (objc_class_type, buf);
3358 /* Create a class reference, but don't create a variable to reference
3362 add_class_reference (tree ident)
3366 if ((chain = cls_ref_chain))
3371 if (ident == TREE_VALUE (chain))
3375 chain = TREE_CHAIN (chain);
3379 /* Append to the end of the list */
3380 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
3383 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
3386 /* Get a class reference, creating it if necessary. Also create the
3387 reference variable. */
3390 objc_get_class_reference (tree ident)
3392 tree orig_ident = (DECL_P (ident)
3395 ? OBJC_TYPE_NAME (ident)
3397 bool local_scope = false;
3400 if (processing_template_decl)
3401 /* Must wait until template instantiation time. */
3402 return build_min_nt (CLASS_REFERENCE_EXPR, ident);
3405 if (TREE_CODE (ident) == TYPE_DECL)
3406 ident = (DECL_ORIGINAL_TYPE (ident)
3407 ? DECL_ORIGINAL_TYPE (ident)
3408 : TREE_TYPE (ident));
3412 && CP_TYPE_CONTEXT (ident) != global_namespace)
3416 if (local_scope || !(ident = objc_is_class_name (ident)))
3418 error ("%qE is not an Objective-C class name or alias",
3420 return error_mark_node;
3423 if (flag_next_runtime && !flag_zero_link)
3428 for (chain = &cls_ref_chain; *chain; chain = &TREE_CHAIN (*chain))
3429 if (TREE_VALUE (*chain) == ident)
3431 if (! TREE_PURPOSE (*chain))
3432 TREE_PURPOSE (*chain) = build_class_reference_decl ();
3434 return TREE_PURPOSE (*chain);
3437 decl = build_class_reference_decl ();
3438 *chain = tree_cons (decl, ident, NULL_TREE);
3445 add_class_reference (ident);
3447 params = build_tree_list (NULL_TREE,
3448 my_build_string_pointer
3449 (IDENTIFIER_LENGTH (ident) + 1,
3450 IDENTIFIER_POINTER (ident)));
3452 assemble_external (objc_get_class_decl);
3453 return build_function_call (input_location, objc_get_class_decl, params);
3457 /* For each string section we have a chain which maps identifier nodes
3458 to decls for the strings. */
3460 static GTY(()) int class_names_idx;
3461 static GTY(()) int meth_var_names_idx;
3462 static GTY(()) int meth_var_types_idx;
3465 add_objc_string (tree ident, enum string_section section)
3467 tree *chain, decl, type, string_expr;
3474 chain = &class_names_chain;
3475 sprintf (buf, "_OBJC_CLASS_NAME_%d", class_names_idx++);
3477 case meth_var_names:
3478 chain = &meth_var_names_chain;
3479 sprintf (buf, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx++);
3481 case meth_var_types:
3482 chain = &meth_var_types_chain;
3483 sprintf (buf, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx++);
3491 if (TREE_VALUE (*chain) == ident)
3492 return convert (string_type_node,
3493 build_unary_op (input_location,
3494 ADDR_EXPR, TREE_PURPOSE (*chain), 1));
3496 chain = &TREE_CHAIN (*chain);
3499 type = build_sized_array_type (char_type_node, IDENTIFIER_LENGTH (ident) + 1);
3500 decl = start_var_decl (type, buf);
3501 string_expr = my_build_string (IDENTIFIER_LENGTH (ident) + 1,
3502 IDENTIFIER_POINTER (ident));
3503 TREE_CONSTANT (decl) = 1;
3504 finish_var_decl (decl, string_expr);
3506 *chain = tree_cons (decl, ident, NULL_TREE);
3508 return convert (string_type_node, build_unary_op (input_location,
3509 ADDR_EXPR, decl, 1));
3513 objc_declare_alias (tree alias_ident, tree class_ident)
3515 tree underlying_class;
3518 if (current_namespace != global_namespace) {
3519 error ("Objective-C declarations may only appear in global scope");
3521 #endif /* OBJCPLUS */
3523 if (!(underlying_class = objc_is_class_name (class_ident)))
3524 warning (0, "cannot find class %qE", class_ident);
3525 else if (objc_is_class_name (alias_ident))
3526 warning (0, "class %qE already exists", alias_ident);
3529 /* Implement @compatibility_alias as a typedef. */
3531 push_lang_context (lang_name_c); /* extern "C" */
3533 lang_hooks.decls.pushdecl (build_decl
3537 xref_tag (RECORD_TYPE, underlying_class)));
3539 pop_lang_context ();
3541 hash_class_name_enter (als_name_hash_list, alias_ident,
3547 objc_declare_class (tree ident_list)
3551 if (current_namespace != global_namespace) {
3552 error ("Objective-C declarations may only appear in global scope");
3554 #endif /* OBJCPLUS */
3556 for (list = ident_list; list; list = TREE_CHAIN (list))
3558 tree ident = TREE_VALUE (list);
3560 if (! objc_is_class_name (ident))
3562 tree record = lookup_name (ident), type = record;
3566 if (TREE_CODE (record) == TYPE_DECL)
3567 type = DECL_ORIGINAL_TYPE (record) ?
3568 DECL_ORIGINAL_TYPE (record) :
3571 if (!TYPE_HAS_OBJC_INFO (type)
3572 || !TYPE_OBJC_INTERFACE (type))
3574 error ("%qE redeclared as different kind of symbol",
3576 error ("previous declaration of %q+D",
3581 record = xref_tag (RECORD_TYPE, ident);
3582 INIT_TYPE_OBJC_INFO (record);
3583 TYPE_OBJC_INTERFACE (record) = ident;
3584 hash_class_name_enter (cls_name_hash_list, ident, NULL_TREE);
3590 objc_is_class_name (tree ident)
3594 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE
3595 && identifier_global_value (ident))
3596 ident = identifier_global_value (ident);
3597 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
3598 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
3600 if (ident && TREE_CODE (ident) == RECORD_TYPE)
3601 ident = OBJC_TYPE_NAME (ident);
3603 if (ident && TREE_CODE (ident) == TYPE_DECL)
3605 tree type = TREE_TYPE (ident);
3606 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
3608 ident = DECL_NAME (ident);
3611 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
3614 if (lookup_interface (ident))
3617 target = hash_class_name_lookup (cls_name_hash_list, ident);
3621 target = hash_class_name_lookup (als_name_hash_list, ident);
3624 gcc_assert (target->list && target->list->value);
3625 return target->list->value;
3631 /* Check whether TYPE is either 'id' or 'Class'. */
3634 objc_is_id (tree type)
3636 if (type && TREE_CODE (type) == IDENTIFIER_NODE
3637 && identifier_global_value (type))
3638 type = identifier_global_value (type);
3640 if (type && TREE_CODE (type) == TYPE_DECL)
3641 type = TREE_TYPE (type);
3643 /* NB: This function may be called before the ObjC front-end has
3644 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3645 return (objc_object_type && type
3646 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
3651 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3652 class instance. This is needed by other parts of the compiler to
3653 handle ObjC types gracefully. */
3656 objc_is_object_ptr (tree type)
3660 type = TYPE_MAIN_VARIANT (type);
3661 if (!POINTER_TYPE_P (type))
3664 ret = objc_is_id (type);
3666 ret = objc_is_class_name (TREE_TYPE (type));
3672 objc_is_gcable_type (tree type, int or_strong_p)
3678 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
3680 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
3682 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
3684 type = TREE_TYPE (type);
3685 if (TREE_CODE (type) != RECORD_TYPE)
3687 name = TYPE_NAME (type);
3688 return (objc_is_class_name (name) != NULL_TREE);
3692 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
3694 if (expr == oldexpr)
3697 switch (TREE_CODE (expr))
3700 return objc_build_component_ref
3701 (objc_substitute_decl (TREE_OPERAND (expr, 0),
3704 DECL_NAME (TREE_OPERAND (expr, 1)));
3706 return build_array_ref (input_location,
3707 objc_substitute_decl (TREE_OPERAND (expr, 0),
3710 TREE_OPERAND (expr, 1));
3712 return build_indirect_ref (input_location,
3713 objc_substitute_decl (TREE_OPERAND (expr, 0),
3715 newexpr), RO_ARROW);
3722 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
3725 /* The LHS parameter contains the expression 'outervar->memberspec';
3726 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3727 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3730 = objc_substitute_decl
3731 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
3733 = (flag_objc_direct_dispatch
3734 ? objc_assign_ivar_fast_decl
3735 : objc_assign_ivar_decl);
3737 offs = convert (integer_type_node, build_unary_op (input_location,
3738 ADDR_EXPR, offs, 0));
3740 func_params = tree_cons (NULL_TREE,
3741 convert (objc_object_type, rhs),
3742 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
3743 tree_cons (NULL_TREE, offs,
3746 assemble_external (func);
3747 return build_function_call (input_location, func, func_params);
3751 objc_build_global_assignment (tree lhs, tree rhs)
3753 tree func_params = tree_cons (NULL_TREE,
3754 convert (objc_object_type, rhs),
3755 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3756 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3759 assemble_external (objc_assign_global_decl);
3760 return build_function_call (input_location,
3761 objc_assign_global_decl, func_params);
3765 objc_build_strong_cast_assignment (tree lhs, tree rhs)
3767 tree func_params = tree_cons (NULL_TREE,
3768 convert (objc_object_type, rhs),
3769 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3770 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3773 assemble_external (objc_assign_strong_cast_decl);
3774 return build_function_call (input_location,
3775 objc_assign_strong_cast_decl, func_params);
3779 objc_is_gcable_p (tree expr)
3781 return (TREE_CODE (expr) == COMPONENT_REF
3782 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
3783 : TREE_CODE (expr) == ARRAY_REF
3784 ? (objc_is_gcable_p (TREE_TYPE (expr))
3785 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
3786 : TREE_CODE (expr) == ARRAY_TYPE
3787 ? objc_is_gcable_p (TREE_TYPE (expr))
3789 ? objc_is_gcable_type (expr, 1)
3790 : (objc_is_gcable_p (TREE_TYPE (expr))
3792 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
3796 objc_is_ivar_reference_p (tree expr)
3798 return (TREE_CODE (expr) == ARRAY_REF
3799 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
3800 : TREE_CODE (expr) == COMPONENT_REF
3801 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
3806 objc_is_global_reference_p (tree expr)
3808 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
3809 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
3811 ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr))
3816 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
3818 tree result = NULL_TREE, outer;
3819 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
3821 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3822 will have been transformed to the form '*(type *)&expr'. */
3823 if (TREE_CODE (lhs) == INDIRECT_REF)
3825 outer = TREE_OPERAND (lhs, 0);
3827 while (!strong_cast_p
3828 && (CONVERT_EXPR_P (outer)
3829 || TREE_CODE (outer) == NON_LVALUE_EXPR))
3831 tree lhstype = TREE_TYPE (outer);
3833 /* Descend down the cast chain, and record the first objc_gc
3835 if (POINTER_TYPE_P (lhstype))
3838 = lookup_attribute ("objc_gc",
3839 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
3845 outer = TREE_OPERAND (outer, 0);
3849 /* If we have a __strong cast, it trumps all else. */
3852 if (modifycode != NOP_EXPR)
3853 goto invalid_pointer_arithmetic;
3855 if (warn_assign_intercept)
3856 warning (0, "strong-cast assignment has been intercepted");
3858 result = objc_build_strong_cast_assignment (lhs, rhs);
3863 /* the lhs must be of a suitable type, regardless of its underlying
3865 if (!objc_is_gcable_p (lhs))
3871 && (TREE_CODE (outer) == COMPONENT_REF
3872 || TREE_CODE (outer) == ARRAY_REF))
3873 outer = TREE_OPERAND (outer, 0);
3875 if (TREE_CODE (outer) == INDIRECT_REF)
3877 outer = TREE_OPERAND (outer, 0);
3881 outer_gc_p = objc_is_gcable_p (outer);
3883 /* Handle ivar assignments. */
3884 if (objc_is_ivar_reference_p (lhs))
3886 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3887 doesn't cut it here), the best we can do here is suggest a cast. */
3888 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
3890 /* We may still be able to use the global write barrier... */
3891 if (!indirect_p && objc_is_global_reference_p (outer))
3892 goto global_reference;
3895 if (modifycode == NOP_EXPR)
3897 if (warn_assign_intercept)
3898 warning (0, "strong-cast may possibly be needed");
3904 if (modifycode != NOP_EXPR)
3905 goto invalid_pointer_arithmetic;
3907 if (warn_assign_intercept)
3908 warning (0, "instance variable assignment has been intercepted");
3910 result = objc_build_ivar_assignment (outer, lhs, rhs);
3915 /* Likewise, intercept assignment to global/static variables if their type is
3917 if (objc_is_global_reference_p (outer))
3923 if (modifycode != NOP_EXPR)
3925 invalid_pointer_arithmetic:
3927 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3932 if (warn_assign_intercept)
3933 warning (0, "global/static variable assignment has been intercepted");
3935 result = objc_build_global_assignment (lhs, rhs);
3938 /* In all other cases, fall back to the normal mechanism. */
3943 struct GTY(()) interface_tuple {
3948 static GTY ((param_is (struct interface_tuple))) htab_t interface_htab;
3951 hash_interface (const void *p)
3953 const struct interface_tuple *d = (const struct interface_tuple *) p;
3954 return IDENTIFIER_HASH_VALUE (d->id);
3958 eq_interface (const void *p1, const void *p2)
3960 const struct interface_tuple *d = (const struct interface_tuple *) p1;
3965 lookup_interface (tree ident)
3968 if (ident && TREE_CODE (ident) == TYPE_DECL)
3969 ident = DECL_NAME (ident);
3972 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
3976 struct interface_tuple **slot;
3981 slot = (struct interface_tuple **)
3982 htab_find_slot_with_hash (interface_htab, ident,
3983 IDENTIFIER_HASH_VALUE (ident),
3986 i = (*slot)->class_name;
3992 /* Implement @defs (<classname>) within struct bodies. */
3995 objc_get_class_ivars (tree class_name)
3997 tree interface = lookup_interface (class_name);
4000 return get_class_ivars (interface, true);
4002 error ("cannot find interface declaration for %qE",
4005 return error_mark_node;
4008 /* Called when checking the variables in a struct. If we are not
4009 doing the ivars list inside an @interface context, then returns
4010 fieldlist unchanged. Else, returns the list of class ivars.
4013 objc_get_interface_ivars (tree fieldlist)
4015 if (!objc_collecting_ivars || !objc_interface_context
4016 || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE
4017 || CLASS_SUPER_NAME (objc_interface_context) == NULL_TREE)
4020 return get_class_ivars (objc_interface_context, true);
4023 /* Used by: build_private_template, continue_class,
4024 and for @defs constructs. */
4027 get_class_ivars (tree interface, bool inherited)
4029 tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface));
4031 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
4032 by the current class (i.e., they do not include super-class ivars).
4033 However, the CLASS_IVARS list will be side-effected by a call to
4034 finish_struct(), which will fill in field offsets. */
4035 if (!CLASS_IVARS (interface))
4036 CLASS_IVARS (interface) = ivar_chain;
4041 while (CLASS_SUPER_NAME (interface))
4043 /* Prepend super-class ivars. */
4044 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4045 ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)),
4052 /* Create a temporary variable of type 'type'. If 'name' is set, uses
4053 the specified name, else use no name. Returns the declaration of
4054 the type. The 'name' is mostly useful for debugging.
4057 objc_create_temporary_var (tree type, const char *name)
4063 decl = build_decl (input_location,
4064 VAR_DECL, get_identifier (name), type);
4068 decl = build_decl (input_location,
4069 VAR_DECL, NULL_TREE, type);
4071 TREE_USED (decl) = 1;
4072 DECL_ARTIFICIAL (decl) = 1;
4073 DECL_IGNORED_P (decl) = 1;
4074 DECL_CONTEXT (decl) = current_function_decl;
4079 /* Exception handling constructs. We begin by having the parser do most
4080 of the work and passing us blocks. What we do next depends on whether
4081 we're doing "native" exception handling or legacy Darwin setjmp exceptions.
4082 We abstract all of this in a handful of appropriately named routines. */
4084 /* Stack of open try blocks. */
4086 struct objc_try_context
4088 struct objc_try_context *outer;
4090 /* Statements (or statement lists) as processed by the parser. */
4094 /* Some file position locations. */
4095 location_t try_locus;
4096 location_t end_try_locus;
4097 location_t end_catch_locus;
4098 location_t finally_locus;
4099 location_t end_finally_locus;
4101 /* A STATEMENT_LIST of CATCH_EXPRs, appropriate for sticking into op1
4102 of a TRY_CATCH_EXPR. Even when doing Darwin setjmp. */
4105 /* The CATCH_EXPR of an open @catch clause. */
4108 /* The VAR_DECL holding the Darwin equivalent of __builtin_eh_pointer. */
4114 static struct objc_try_context *cur_try_context;
4116 static GTY(()) tree objc_eh_personality_decl;
4118 /* This hook, called via lang_eh_runtime_type, generates a runtime object
4119 that represents TYPE. For Objective-C, this is just the class name. */
4120 /* ??? Isn't there a class object or some such? Is it easy to get? */
4124 objc_eh_runtime_type (tree type)
4126 return add_objc_string (OBJC_TYPE_NAME (TREE_TYPE (type)), class_names);
4130 objc_eh_personality (void)
4132 if (!flag_objc_sjlj_exceptions && !objc_eh_personality_decl)
4133 objc_eh_personality_decl = build_personality_function ("gnu_objc");
4134 return objc_eh_personality_decl;
4138 /* Build __builtin_eh_pointer, or the moral equivalent. In the case
4139 of Darwin, we'll arrange for it to be initialized (and associated
4140 with a binding) later. */
4143 objc_build_exc_ptr (void)
4145 if (flag_objc_sjlj_exceptions)
4147 tree var = cur_try_context->caught_decl;
4150 var = objc_create_temporary_var (objc_object_type, NULL);
4151 cur_try_context->caught_decl = var;
4158 t = built_in_decls[BUILT_IN_EH_POINTER];
4159 t = build_call_expr (t, 1, integer_zero_node);
4160 return fold_convert (objc_object_type, t);
4164 /* Build "objc_exception_try_exit(&_stack)". */
4167 next_sjlj_build_try_exit (void)
4170 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
4171 t = tree_cons (NULL, t, NULL);
4172 t = build_function_call (input_location,
4173 objc_exception_try_exit_decl, t);
4178 objc_exception_try_enter (&_stack);
4179 if (_setjmp(&_stack.buf))
4183 Return the COND_EXPR. Note that the THEN and ELSE fields are left
4184 empty, ready for the caller to fill them in. */
4187 next_sjlj_build_enter_and_setjmp (void)
4189 tree t, enter, sj, cond;
4191 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
4192 t = tree_cons (NULL, t, NULL);
4193 enter = build_function_call (input_location,
4194 objc_exception_try_enter_decl, t);
4196 t = objc_build_component_ref (cur_try_context->stack_decl,
4197 get_identifier ("buf"));
4198 t = build_fold_addr_expr_loc (input_location, t);
4200 /* Convert _setjmp argument to type that is expected. */
4201 if (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl)))
4202 t = convert (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl))), t);
4204 t = convert (ptr_type_node, t);
4206 t = convert (ptr_type_node, t);
4208 t = tree_cons (NULL, t, NULL);
4209 sj = build_function_call (input_location,
4210 objc_setjmp_decl, t);
4212 cond = build2 (COMPOUND_EXPR, TREE_TYPE (sj), enter, sj);
4213 cond = c_common_truthvalue_conversion (input_location, cond);
4215 return build3 (COND_EXPR, void_type_node, cond, NULL, NULL);
4220 DECL = objc_exception_extract(&_stack); */
4223 next_sjlj_build_exc_extract (tree decl)
4227 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
4228 t = tree_cons (NULL, t, NULL);
4229 t = build_function_call (input_location,
4230 objc_exception_extract_decl, t);
4231 t = convert (TREE_TYPE (decl), t);
4232 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
4238 if (objc_exception_match(obj_get_class(TYPE), _caught)
4245 objc_exception_try_exit(&_stack);
4247 from the sequence of CATCH_EXPRs in the current try context. */
4250 next_sjlj_build_catch_list (void)
4252 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
4254 tree *last = &catch_seq;
4255 bool saw_id = false;
4257 for (; !tsi_end_p (i); tsi_next (&i))
4259 tree stmt = tsi_stmt (i);
4260 tree type = CATCH_TYPES (stmt);
4261 tree body = CATCH_BODY (stmt);
4273 if (type == error_mark_node)
4274 cond = error_mark_node;
4277 args = tree_cons (NULL, cur_try_context->caught_decl, NULL);
4278 t = objc_get_class_reference (OBJC_TYPE_NAME (TREE_TYPE (type)));
4279 args = tree_cons (NULL, t, args);
4280 t = build_function_call (input_location,
4281 objc_exception_match_decl, args);
4282 cond = c_common_truthvalue_conversion (input_location, t);
4284 t = build3 (COND_EXPR, void_type_node, cond, body, NULL);
4285 SET_EXPR_LOCATION (t, EXPR_LOCATION (stmt));
4288 last = &COND_EXPR_ELSE (t);
4294 t = build2 (MODIFY_EXPR, void_type_node, cur_try_context->rethrow_decl,
4295 cur_try_context->caught_decl);
4296 SET_EXPR_LOCATION (t, cur_try_context->end_catch_locus);
4297 append_to_statement_list (t, last);
4299 t = next_sjlj_build_try_exit ();
4300 SET_EXPR_LOCATION (t, cur_try_context->end_catch_locus);
4301 append_to_statement_list (t, last);
4307 /* Build a complete @try-@catch-@finally block for legacy Darwin setjmp
4308 exception handling. We aim to build:
4311 struct _objc_exception_data _stack;
4315 objc_exception_try_enter (&_stack);
4316 if (_setjmp(&_stack.buf))
4318 id _caught = objc_exception_extract(&_stack);
4319 objc_exception_try_enter (&_stack);
4320 if (_setjmp(&_stack.buf))
4321 _rethrow = objc_exception_extract(&_stack);
4331 objc_exception_try_exit(&_stack);
4334 objc_exception_throw(_rethrow);
4338 If CATCH-LIST is empty, we can omit all of the block containing
4339 "_caught" except for the setting of _rethrow. Note the use of
4340 a real TRY_FINALLY_EXPR here, which is not involved in EH per-se,
4341 but handles goto and other exits from the block. */
4344 next_sjlj_build_try_catch_finally (void)
4346 tree rethrow_decl, stack_decl, t;
4347 tree catch_seq, try_fin, bind;
4349 /* Create the declarations involved. */
4350 t = xref_tag (RECORD_TYPE, get_identifier (UTAG_EXCDATA));
4351 stack_decl = objc_create_temporary_var (t, NULL);
4352 cur_try_context->stack_decl = stack_decl;
4354 rethrow_decl = objc_create_temporary_var (objc_object_type, NULL);
4355 cur_try_context->rethrow_decl = rethrow_decl;
4356 TREE_CHAIN (rethrow_decl) = stack_decl;
4358 /* Build the outermost variable binding level. */
4359 bind = build3 (BIND_EXPR, void_type_node, rethrow_decl, NULL, NULL);
4360 SET_EXPR_LOCATION (bind, cur_try_context->try_locus);
4361 TREE_SIDE_EFFECTS (bind) = 1;
4363 /* Initialize rethrow_decl. */
4364 t = build2 (MODIFY_EXPR, void_type_node, rethrow_decl,
4365 convert (objc_object_type, null_pointer_node));
4366 SET_EXPR_LOCATION (t, cur_try_context->try_locus);
4367 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
4369 /* Build the outermost TRY_FINALLY_EXPR. */
4370 try_fin = build2 (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
4371 SET_EXPR_LOCATION (try_fin, cur_try_context->try_locus);
4372 TREE_SIDE_EFFECTS (try_fin) = 1;
4373 append_to_statement_list (try_fin, &BIND_EXPR_BODY (bind));
4375 /* Create the complete catch sequence. */
4376 if (cur_try_context->catch_list)
4378 tree caught_decl = objc_build_exc_ptr ();
4379 catch_seq = build_stmt (input_location, BIND_EXPR, caught_decl, NULL, NULL);
4380 TREE_SIDE_EFFECTS (catch_seq) = 1;
4382 t = next_sjlj_build_exc_extract (caught_decl);
4383 append_to_statement_list (t, &BIND_EXPR_BODY (catch_seq));
4385 t = next_sjlj_build_enter_and_setjmp ();
4386 COND_EXPR_THEN (t) = next_sjlj_build_exc_extract (rethrow_decl);
4387 COND_EXPR_ELSE (t) = next_sjlj_build_catch_list ();
4388 append_to_statement_list (t, &BIND_EXPR_BODY (catch_seq));
4391 catch_seq = next_sjlj_build_exc_extract (rethrow_decl);
4392 SET_EXPR_LOCATION (catch_seq, cur_try_context->end_try_locus);
4394 /* Build the main register-and-try if statement. */
4395 t = next_sjlj_build_enter_and_setjmp ();
4396 SET_EXPR_LOCATION (t, cur_try_context->try_locus);
4397 COND_EXPR_THEN (t) = catch_seq;
4398 COND_EXPR_ELSE (t) = cur_try_context->try_body;
4399 TREE_OPERAND (try_fin, 0) = t;
4401 /* Build the complete FINALLY statement list. */
4402 t = next_sjlj_build_try_exit ();
4403 t = build_stmt (input_location, COND_EXPR,
4404 c_common_truthvalue_conversion
4405 (input_location, rethrow_decl),
4407 SET_EXPR_LOCATION (t, cur_try_context->finally_locus);
4408 append_to_statement_list (t, &TREE_OPERAND (try_fin, 1));
4410 append_to_statement_list (cur_try_context->finally_body,
4411 &TREE_OPERAND (try_fin, 1));
4413 t = tree_cons (NULL, rethrow_decl, NULL);
4414 t = build_function_call (input_location,
4415 objc_exception_throw_decl, t);
4416 t = build_stmt (input_location, COND_EXPR,
4417 c_common_truthvalue_conversion (input_location,
4420 SET_EXPR_LOCATION (t, cur_try_context->end_finally_locus);
4421 append_to_statement_list (t, &TREE_OPERAND (try_fin, 1));
4426 /* Called just after parsing the @try and its associated BODY. We now
4427 must prepare for the tricky bits -- handling the catches and finally. */
4430 objc_begin_try_stmt (location_t try_locus, tree body)
4432 struct objc_try_context *c = XCNEW (struct objc_try_context);
4433 c->outer = cur_try_context;
4435 c->try_locus = try_locus;
4436 c->end_try_locus = input_location;
4437 cur_try_context = c;
4439 /* -fobjc-exceptions is required to enable Objective-C exceptions.
4440 For example, on Darwin, ObjC exceptions require a sufficiently
4441 recent version of the runtime, so the user must ask for them
4442 explicitly. On other platforms, at the moment -fobjc-exceptions
4443 triggers -fexceptions which again is required for exceptions to
4446 if (!flag_objc_exceptions)
4448 error_at (try_locus, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4451 if (flag_objc_sjlj_exceptions)
4452 objc_mark_locals_volatile (NULL);
4455 /* Called just after parsing "@catch (parm)". Open a binding level,
4456 enter DECL into the binding level, and initialize it. Leave the
4457 binding level open while the body of the compound statement is parsed. */
4460 objc_begin_catch_clause (tree decl)
4462 tree compound, type, t;
4464 /* Begin a new scope that the entire catch clause will live in. */
4465 compound = c_begin_compound_stmt (true);
4467 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
4468 decl = build_decl (input_location,
4469 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
4470 lang_hooks.decls.pushdecl (decl);
4472 /* Since a decl is required here by syntax, don't warn if its unused. */
4473 /* ??? As opposed to __attribute__((unused))? Anyway, this appears to
4474 be what the previous objc implementation did. */
4475 TREE_USED (decl) = 1;
4476 DECL_READ_P (decl) = 1;
4478 /* Verify that the type of the catch is valid. It must be a pointer
4479 to an Objective-C class, or "id" (which is catch-all). */
4480 type = TREE_TYPE (decl);
4482 if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type)))
4484 else if (!POINTER_TYPE_P (type) || !TYPED_OBJECT (TREE_TYPE (type)))
4486 error ("@catch parameter is not a known Objective-C class type");
4487 type = error_mark_node;
4489 else if (cur_try_context->catch_list)
4491 /* Examine previous @catch clauses and see if we've already
4492 caught the type in question. */
4493 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
4494 for (; !tsi_end_p (i); tsi_next (&i))
4496 tree stmt = tsi_stmt (i);
4497 t = CATCH_TYPES (stmt);
4498 if (t == error_mark_node)
4500 if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type)))
4502 warning (0, "exception of type %<%T%> will be caught",
4504 warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>",
4505 TREE_TYPE (t ? t : objc_object_type));
4511 /* Record the data for the catch in the try context so that we can
4512 finalize it later. */
4513 t = build_stmt (input_location, CATCH_EXPR, type, compound);
4514 cur_try_context->current_catch = t;
4516 /* Initialize the decl from the EXC_PTR_EXPR we get from the runtime. */
4517 t = objc_build_exc_ptr ();
4518 t = convert (TREE_TYPE (decl), t);
4519 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
4523 /* Called just after parsing the closing brace of a @catch clause. Close
4524 the open binding level, and record a CATCH_EXPR for it. */
4527 objc_finish_catch_clause (void)
4529 tree c = cur_try_context->current_catch;
4530 cur_try_context->current_catch = NULL;
4531 cur_try_context->end_catch_locus = input_location;
4533 CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1);
4534 append_to_statement_list (c, &cur_try_context->catch_list);
4537 /* Called after parsing a @finally clause and its associated BODY.
4538 Record the body for later placement. */
4541 objc_build_finally_clause (location_t finally_locus, tree body)
4543 cur_try_context->finally_body = body;
4544 cur_try_context->finally_locus = finally_locus;
4545 cur_try_context->end_finally_locus = input_location;
4548 /* Called to finalize a @try construct. */
4551 objc_finish_try_stmt (void)
4553 struct objc_try_context *c = cur_try_context;
4556 if (c->catch_list == NULL && c->finally_body == NULL)
4557 error ("%<@try%> without %<@catch%> or %<@finally%>");
4559 /* If we're doing Darwin setjmp exceptions, build the big nasty. */
4560 if (flag_objc_sjlj_exceptions)
4562 bool save = in_late_binary_op;
4563 in_late_binary_op = true;
4564 if (!cur_try_context->finally_body)
4566 cur_try_context->finally_locus = input_location;
4567 cur_try_context->end_finally_locus = input_location;
4569 stmt = next_sjlj_build_try_catch_finally ();
4570 in_late_binary_op = save;
4574 /* Otherwise, nest the CATCH inside a FINALLY. */
4578 stmt = build_stmt (input_location, TRY_CATCH_EXPR, stmt, c->catch_list);
4579 SET_EXPR_LOCATION (stmt, cur_try_context->try_locus);
4581 if (c->finally_body)
4583 stmt = build_stmt (input_location, TRY_FINALLY_EXPR, stmt, c->finally_body);
4584 SET_EXPR_LOCATION (stmt, cur_try_context->try_locus);
4589 cur_try_context = c->outer;
4595 objc_build_throw_stmt (location_t loc, tree throw_expr)
4599 if (!flag_objc_exceptions)
4601 error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4604 if (throw_expr == NULL)
4606 /* If we're not inside a @catch block, there is no "current
4607 exception" to be rethrown. */
4608 if (cur_try_context == NULL
4609 || cur_try_context->current_catch == NULL)
4611 error_at (loc, "%<@throw%> (rethrow) used outside of a @catch block");
4615 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
4616 value that we get from the runtime. */
4617 throw_expr = objc_build_exc_ptr ();
4620 /* A throw is just a call to the runtime throw function with the
4621 object as a parameter. */
4622 args = tree_cons (NULL, throw_expr, NULL);
4623 return add_stmt (build_function_call (loc,
4624 objc_exception_throw_decl, args));
4628 objc_build_synchronized (location_t start_locus, tree mutex, tree body)
4632 /* First lock the mutex. */
4633 mutex = save_expr (mutex);
4634 args = tree_cons (NULL, mutex, NULL);
4635 call = build_function_call (input_location,
4636 objc_sync_enter_decl, args);
4637 SET_EXPR_LOCATION (call, start_locus);
4640 /* Build the mutex unlock. */
4641 args = tree_cons (NULL, mutex, NULL);
4642 call = build_function_call (input_location,
4643 objc_sync_exit_decl, args);
4644 SET_EXPR_LOCATION (call, input_location);
4646 /* Put the that and the body in a TRY_FINALLY. */
4647 objc_begin_try_stmt (start_locus, body);
4648 objc_build_finally_clause (input_location, call);
4649 return objc_finish_try_stmt ();
4653 /* Predefine the following data type:
4655 struct _objc_exception_data
4657 int buf[OBJC_JBLEN];
4661 /* The following yuckiness should prevent users from having to #include
4662 <setjmp.h> in their code... */
4664 /* Define to a harmless positive value so the below code doesn't die. */
4666 #define OBJC_JBLEN 18
4670 build_next_objc_exception_stuff (void)
4672 tree decls, temp_type, *chain = NULL;
4674 objc_exception_data_template
4675 = objc_start_struct (get_identifier (UTAG_EXCDATA));
4677 /* int buf[OBJC_JBLEN]; */
4679 temp_type = build_sized_array_type (integer_type_node, OBJC_JBLEN);
4680 decls = add_field_decl (temp_type, "buf", &chain);
4682 /* void *pointers[4]; */
4684 temp_type = build_sized_array_type (ptr_type_node, 4);
4685 add_field_decl (temp_type, "pointers", &chain);
4687 objc_finish_struct (objc_exception_data_template, decls);
4689 /* int _setjmp(...); */
4690 /* If the user includes <setjmp.h>, this shall be superseded by
4691 'int _setjmp(jmp_buf);' */
4692 temp_type = build_varargs_function_type_list (integer_type_node, NULL_TREE);
4694 = add_builtin_function (TAG_SETJMP, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
4696 /* id objc_exception_extract(struct _objc_exception_data *); */
4698 = build_function_type_list (objc_object_type,
4699 build_pointer_type (objc_exception_data_template),
4701 objc_exception_extract_decl
4702 = add_builtin_function (TAG_EXCEPTIONEXTRACT, temp_type, 0, NOT_BUILT_IN, NULL,
4704 /* void objc_exception_try_enter(struct _objc_exception_data *); */
4705 /* void objc_exception_try_exit(struct _objc_exception_data *); */
4707 = build_function_type_list (void_type_node,
4708 build_pointer_type (objc_exception_data_template),
4710 objc_exception_try_enter_decl
4711 = add_builtin_function (TAG_EXCEPTIONTRYENTER, temp_type, 0, NOT_BUILT_IN, NULL,
4713 objc_exception_try_exit_decl
4714 = add_builtin_function (TAG_EXCEPTIONTRYEXIT, temp_type, 0, NOT_BUILT_IN, NULL,
4717 /* int objc_exception_match(id, id); */
4719 = build_function_type_list (integer_type_node,
4720 objc_object_type, objc_object_type, NULL_TREE);
4721 objc_exception_match_decl
4722 = add_builtin_function (TAG_EXCEPTIONMATCH, temp_type, 0, NOT_BUILT_IN, NULL,
4725 /* id objc_assign_ivar (id, id, unsigned int); */
4726 /* id objc_assign_ivar_Fast (id, id, unsigned int)
4727 __attribute__ ((hard_coded_address (OFFS_ASSIGNIVAR_FAST))); */
4729 = build_function_type_list (objc_object_type,
4734 objc_assign_ivar_decl
4735 = add_builtin_function (TAG_ASSIGNIVAR, temp_type, 0, NOT_BUILT_IN,
4737 #ifdef OFFS_ASSIGNIVAR_FAST
4738 objc_assign_ivar_fast_decl
4739 = add_builtin_function (TAG_ASSIGNIVAR_FAST, temp_type, 0,
4740 NOT_BUILT_IN, NULL, NULL_TREE);
4741 DECL_ATTRIBUTES (objc_assign_ivar_fast_decl)
4742 = tree_cons (get_identifier ("hard_coded_address"),
4743 build_int_cst (NULL_TREE, OFFS_ASSIGNIVAR_FAST),
4746 /* Default to slower ivar method. */
4747 objc_assign_ivar_fast_decl = objc_assign_ivar_decl;
4750 /* id objc_assign_global (id, id *); */
4751 /* id objc_assign_strongCast (id, id *); */
4752 temp_type = build_function_type_list (objc_object_type,
4754 build_pointer_type (objc_object_type),
4756 objc_assign_global_decl
4757 = add_builtin_function (TAG_ASSIGNGLOBAL, temp_type, 0, NOT_BUILT_IN, NULL,
4759 objc_assign_strong_cast_decl
4760 = add_builtin_function (TAG_ASSIGNSTRONGCAST, temp_type, 0, NOT_BUILT_IN, NULL,
4765 build_objc_exception_stuff (void)
4767 tree noreturn_list, nothrow_list, temp_type;
4769 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
4770 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
4772 /* void objc_exception_throw(id) __attribute__((noreturn)); */
4773 /* void objc_sync_enter(id); */
4774 /* void objc_sync_exit(id); */
4775 temp_type = build_function_type_list (void_type_node,
4778 objc_exception_throw_decl
4779 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
4781 objc_sync_enter_decl
4782 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
4783 NULL, nothrow_list);
4785 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
4786 NULL, nothrow_list);
4789 /* Construct a C struct corresponding to ObjC class CLASS, with the same
4792 struct <classname> {
4793 struct _objc_class *isa;
4798 build_private_template (tree klass)
4800 if (!CLASS_STATIC_TEMPLATE (klass))
4802 tree record = objc_build_struct (klass,
4803 get_class_ivars (klass, false),
4804 CLASS_SUPER_NAME (klass));
4806 /* Set the TREE_USED bit for this struct, so that stab generator
4807 can emit stabs for this struct type. */
4808 if (flag_debug_only_used_symbols && TYPE_STUB_DECL (record))
4809 TREE_USED (TYPE_STUB_DECL (record)) = 1;
4813 /* Begin code generation for protocols... */
4815 /* struct _objc_protocol {
4816 struct _objc_class *isa;
4817 char *protocol_name;
4818 struct _objc_protocol **protocol_list;
4819 struct _objc__method_prototype_list *instance_methods;
4820 struct _objc__method_prototype_list *class_methods;
4824 build_protocol_template (void)
4826 tree ptype, decls, *chain = NULL;
4828 objc_protocol_template = objc_start_struct (get_identifier (UTAG_PROTOCOL));
4830 /* struct _objc_class *isa; */
4831 ptype = build_pointer_type (xref_tag (RECORD_TYPE,
4832 get_identifier (UTAG_CLASS)));
4833 decls = add_field_decl (ptype, "isa", &chain);
4835 /* char *protocol_name; */
4836 add_field_decl (string_type_node, "protocol_name", &chain);
4838 /* struct _objc_protocol **protocol_list; */
4839 ptype = build_pointer_type (build_pointer_type (objc_protocol_template));
4840 add_field_decl (ptype, "protocol_list", &chain);
4842 /* struct _objc__method_prototype_list *instance_methods; */
4843 add_field_decl (objc_method_proto_list_ptr, "instance_methods", &chain);
4845 /* struct _objc__method_prototype_list *class_methods; */
4846 add_field_decl (objc_method_proto_list_ptr, "class_methods", &chain);
4848 objc_finish_struct (objc_protocol_template, decls);
4852 build_descriptor_table_initializer (tree type, tree entries)
4854 VEC(constructor_elt,gc) *inits = NULL;
4858 VEC(constructor_elt,gc) *elts = NULL;
4860 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
4861 build_selector (METHOD_SEL_NAME (entries)));
4862 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
4863 add_objc_string (METHOD_ENCODING (entries),
4866 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
4867 objc_build_constructor (type, elts));
4869 entries = DECL_CHAIN (entries);
4873 return objc_build_constructor (build_array_type (type, 0), inits);
4876 /* struct objc_method_prototype_list {
4878 struct objc_method_prototype {
4885 build_method_prototype_list_template (tree list_type, int size)
4887 tree objc_ivar_list_record;
4888 tree array_type, decls, *chain = NULL;
4890 /* Generate an unnamed struct definition. */
4892 objc_ivar_list_record = objc_start_struct (NULL_TREE);
4894 /* int method_count; */
4895 decls = add_field_decl (integer_type_node, "method_count", &chain);
4897 /* struct objc_method method_list[]; */
4898 array_type = build_sized_array_type (list_type, size);
4899 add_field_decl (array_type, "method_list", &chain);
4901 objc_finish_struct (objc_ivar_list_record, decls);
4903 return objc_ivar_list_record;
4907 build_method_prototype_template (void)
4910 tree decls, *chain = NULL;
4912 proto_record = objc_start_struct (get_identifier (UTAG_METHOD_PROTOTYPE));
4915 decls = add_field_decl (objc_selector_type, "_cmd", &chain);
4917 /* char *method_types; */
4918 add_field_decl (string_type_node, "method_types", &chain);
4920 objc_finish_struct (proto_record, decls);
4922 return proto_record;
4926 objc_method_parm_type (tree type)
4928 type = TREE_VALUE (TREE_TYPE (type));
4929 if (TREE_CODE (type) == TYPE_DECL)
4930 type = TREE_TYPE (type);
4935 objc_encoded_type_size (tree type)
4937 int sz = int_size_in_bytes (type);
4939 /* Make all integer and enum types at least as large
4941 if (sz > 0 && INTEGRAL_TYPE_P (type))
4942 sz = MAX (sz, int_size_in_bytes (integer_type_node));
4943 /* Treat arrays as pointers, since that's how they're
4945 else if (TREE_CODE (type) == ARRAY_TYPE)
4946 sz = int_size_in_bytes (ptr_type_node);
4950 /* Encode a method prototype.
4952 The format is described in gcc/doc/objc.texi, section 'Method
4956 encode_method_prototype (tree method_decl)
4963 /* ONEWAY and BYCOPY, for remote object are the only method qualifiers. */
4964 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (method_decl)));
4966 /* Encode return type. */
4967 encode_type (objc_method_parm_type (method_decl),
4968 obstack_object_size (&util_obstack),
4969 OBJC_ENCODE_INLINE_DEFS);
4972 /* The first two arguments (self and _cmd) are pointers; account for
4974 i = int_size_in_bytes (ptr_type_node);
4975 parm_offset = 2 * i;
4976 for (parms = METHOD_SEL_ARGS (method_decl); parms;
4977 parms = DECL_CHAIN (parms))
4979 tree type = objc_method_parm_type (parms);
4980 int sz = objc_encoded_type_size (type);
4982 /* If a type size is not known, bail out. */
4985 error ("type %q+D does not have a known size",
4987 /* Pretend that the encoding succeeded; the compilation will
4988 fail nevertheless. */
4989 goto finish_encoding;
4994 sprintf (buf, "%d@0:%d", parm_offset, i);
4995 obstack_grow (&util_obstack, buf, strlen (buf));
4997 /* Argument types. */
4998 parm_offset = 2 * i;
4999 for (parms = METHOD_SEL_ARGS (method_decl); parms;
5000 parms = DECL_CHAIN (parms))
5002 tree type = objc_method_parm_type (parms);
5004 /* Process argument qualifiers for user supplied arguments. */
5005 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (parms)));
5008 encode_type (type, obstack_object_size (&util_obstack),
5009 OBJC_ENCODE_INLINE_DEFS);
5011 /* Compute offset. */
5012 sprintf (buf, "%d", parm_offset);
5013 parm_offset += objc_encoded_type_size (type);
5015 obstack_grow (&util_obstack, buf, strlen (buf));
5019 obstack_1grow (&util_obstack, '\0');
5020 result = get_identifier (XOBFINISH (&util_obstack, char *));
5021 obstack_free (&util_obstack, util_firstobj);
5026 generate_descriptor_table (tree type, const char *name, int size, tree list,
5030 VEC(constructor_elt,gc) *v = NULL;
5032 decl = start_var_decl (type, synth_id_with_class_suffix (name, proto));
5034 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, size));
5035 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, list);
5037 finish_var_decl (decl, objc_build_constructor (type, v));
5043 generate_method_descriptors (tree protocol)
5045 tree initlist, chain, method_list_template;
5048 if (!objc_method_prototype_template)
5049 objc_method_prototype_template = build_method_prototype_template ();
5051 chain = PROTOCOL_CLS_METHODS (protocol);
5054 size = list_length (chain);
5056 method_list_template
5057 = build_method_prototype_list_template (objc_method_prototype_template,
5061 = build_descriptor_table_initializer (objc_method_prototype_template,
5064 UOBJC_CLASS_METHODS_decl
5065 = generate_descriptor_table (method_list_template,
5066 "_OBJC_PROTOCOL_CLASS_METHODS",
5067 size, initlist, protocol);
5070 UOBJC_CLASS_METHODS_decl = 0;
5072 chain = PROTOCOL_NST_METHODS (protocol);
5075 size = list_length (chain);
5077 method_list_template
5078 = build_method_prototype_list_template (objc_method_prototype_template,
5081 = build_descriptor_table_initializer (objc_method_prototype_template,
5084 UOBJC_INSTANCE_METHODS_decl
5085 = generate_descriptor_table (method_list_template,
5086 "_OBJC_PROTOCOL_INSTANCE_METHODS",
5087 size, initlist, protocol);
5090 UOBJC_INSTANCE_METHODS_decl = 0;
5094 generate_protocol_references (tree plist)
5098 /* Forward declare protocols referenced. */
5099 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
5101 tree proto = TREE_VALUE (lproto);
5103 if (TREE_CODE (proto) == PROTOCOL_INTERFACE_TYPE
5104 && PROTOCOL_NAME (proto))
5106 if (! PROTOCOL_FORWARD_DECL (proto))
5107 build_protocol_reference (proto);
5109 if (PROTOCOL_LIST (proto))
5110 generate_protocol_references (PROTOCOL_LIST (proto));
5115 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
5119 objc_generate_cxx_ctor_or_dtor (bool dtor)
5121 tree fn, body, compound_stmt, ivar;
5123 /* - (id) .cxx_construct { ... return self; } */
5124 /* - (void) .cxx_construct { ... } */
5126 objc_start_method_definition
5127 (false /* is_class_method */,
5128 objc_build_method_signature (false /* is_class_method */,
5129 build_tree_list (NULL_TREE,
5132 : objc_object_type),
5133 get_identifier (dtor
5135 : TAG_CXX_CONSTRUCT),
5136 make_node (TREE_LIST),
5138 body = begin_function_body ();
5139 compound_stmt = begin_compound_stmt (0);
5141 ivar = CLASS_IVARS (implementation_template);
5142 /* Destroy ivars in reverse order. */
5144 ivar = nreverse (copy_list (ivar));
5146 for (; ivar; ivar = TREE_CHAIN (ivar))
5148 if (TREE_CODE (ivar) == FIELD_DECL)
5150 tree type = TREE_TYPE (ivar);
5152 /* Call the ivar's default constructor or destructor. Do not
5153 call the destructor unless a corresponding constructor call
5154 has also been made (or is not needed). */
5155 if (MAYBE_CLASS_TYPE_P (type)
5157 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
5158 && (!TYPE_NEEDS_CONSTRUCTING (type)
5159 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5160 : (TYPE_NEEDS_CONSTRUCTING (type)
5161 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))))
5163 (build_special_member_call
5164 (build_ivar_reference (DECL_NAME (ivar)),
5165 dtor ? complete_dtor_identifier : complete_ctor_identifier,
5166 NULL, type, LOOKUP_NORMAL, tf_warning_or_error));
5170 /* The constructor returns 'self'. */
5172 finish_return_stmt (self_decl);
5174 finish_compound_stmt (compound_stmt);
5175 finish_function_body (body);
5176 fn = current_function_decl;
5178 objc_finish_method_definition (fn);
5181 /* The following routine will examine the current @interface for any
5182 non-POD C++ ivars requiring non-trivial construction and/or
5183 destruction, and then synthesize special '- .cxx_construct' and/or
5184 '- .cxx_destruct' methods which will run the appropriate
5185 construction or destruction code. Note that ivars inherited from
5186 super-classes are _not_ considered. */
5188 objc_generate_cxx_cdtors (void)
5190 bool need_ctor = false, need_dtor = false;
5193 /* Error case, due to possibly an extra @end. */
5194 if (!objc_implementation_context)
5197 /* We do not want to do this for categories, since they do not have
5200 if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE)
5203 /* First, determine if we even need a constructor and/or destructor. */
5205 for (ivar = CLASS_IVARS (implementation_template); ivar;
5206 ivar = TREE_CHAIN (ivar))
5208 if (TREE_CODE (ivar) == FIELD_DECL)
5210 tree type = TREE_TYPE (ivar);
5212 if (MAYBE_CLASS_TYPE_P (type))
5214 if (TYPE_NEEDS_CONSTRUCTING (type)
5215 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
5216 /* NB: If a default constructor is not available, we will not
5217 be able to initialize this ivar; the add_instance_variable()
5218 routine will already have warned about this. */
5221 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
5222 && (!TYPE_NEEDS_CONSTRUCTING (type)
5223 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5224 /* NB: If a default constructor is not available, we will not
5225 call the destructor either, for symmetry. */
5231 /* Generate '- .cxx_construct' if needed. */
5234 objc_generate_cxx_ctor_or_dtor (false);
5236 /* Generate '- .cxx_destruct' if needed. */
5239 objc_generate_cxx_ctor_or_dtor (true);
5241 /* The 'imp_list' variable points at an imp_entry record for the current
5242 @implementation. Record the existence of '- .cxx_construct' and/or
5243 '- .cxx_destruct' methods therein; it will be included in the
5244 metadata for the class. */
5245 if (flag_next_runtime)
5246 imp_list->has_cxx_cdtors = (need_ctor || need_dtor);
5250 /* For each protocol which was referenced either from a @protocol()
5251 expression, or because a class/category implements it (then a
5252 pointer to the protocol is stored in the struct describing the
5253 class/category), we create a statically allocated instance of the
5254 Protocol class. The code is written in such a way as to generate
5255 as few Protocol objects as possible; we generate a unique Protocol
5256 instance for each protocol, and we don't generate a Protocol
5257 instance if the protocol is never referenced (either from a
5258 @protocol() or from a class/category implementation). These
5259 statically allocated objects can be referred to via the static
5260 (that is, private to this module) symbols _OBJC_PROTOCOL_n.
5262 The statically allocated Protocol objects that we generate here
5263 need to be fixed up at runtime in order to be used: the 'isa'
5264 pointer of the objects need to be set up to point to the 'Protocol'
5265 class, as known at runtime.
5267 The NeXT runtime fixes up all protocols at program startup time,
5268 before main() is entered. It uses a low-level trick to look up all
5269 those symbols, then loops on them and fixes them up.
5271 The GNU runtime as well fixes up all protocols before user code
5272 from the module is executed; it requires pointers to those symbols
5273 to be put in the objc_symtab (which is then passed as argument to
5274 the function __objc_exec_class() which the compiler sets up to be
5275 executed automatically when the module is loaded); setup of those
5276 Protocol objects happen in two ways in the GNU runtime: all
5277 Protocol objects referred to by a class or category implementation
5278 are fixed up when the class/category is loaded; all Protocol
5279 objects referred to by a @protocol() expression are added by the
5280 compiler to the list of statically allocated instances to fixup
5281 (the same list holding the statically allocated constant string
5282 objects). Because, as explained above, the compiler generates as
5283 few Protocol objects as possible, some Protocol object might end up
5284 being referenced multiple times when compiled with the GNU runtime,
5285 and end up being fixed up multiple times at runtime initialization.
5286 But that doesn't hurt, it's just a little inefficient. */
5289 generate_protocols (void)
5293 tree initlist, protocol_name_expr, refs_decl, refs_expr;
5295 /* If a protocol was directly referenced, pull in indirect references. */
5296 for (p = protocol_chain; p; p = TREE_CHAIN (p))
5297 if (PROTOCOL_FORWARD_DECL (p) && PROTOCOL_LIST (p))
5298 generate_protocol_references (PROTOCOL_LIST (p));
5300 for (p = protocol_chain; p; p = TREE_CHAIN (p))
5302 tree nst_methods = PROTOCOL_NST_METHODS (p);
5303 tree cls_methods = PROTOCOL_CLS_METHODS (p);
5305 /* If protocol wasn't referenced, don't generate any code. */
5306 decl = PROTOCOL_FORWARD_DECL (p);
5311 /* Make sure we link in the Protocol class. */
5312 add_class_reference (get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
5316 if (! METHOD_ENCODING (nst_methods))
5318 encoding = encode_method_prototype (nst_methods);
5319 METHOD_ENCODING (nst_methods) = encoding;
5321 nst_methods = DECL_CHAIN (nst_methods);
5326 if (! METHOD_ENCODING (cls_methods))
5328 encoding = encode_method_prototype (cls_methods);
5329 METHOD_ENCODING (cls_methods) = encoding;
5332 cls_methods = DECL_CHAIN (cls_methods);
5334 generate_method_descriptors (p);
5336 if (PROTOCOL_LIST (p))
5337 refs_decl = generate_protocol_list (p);
5341 /* static struct objc_protocol _OBJC_PROTOCOL_<mumble>; */
5342 protocol_name_expr = add_objc_string (PROTOCOL_NAME (p), class_names);
5345 refs_expr = convert (build_pointer_type (build_pointer_type
5346 (objc_protocol_template)),
5347 build_unary_op (input_location,
5348 ADDR_EXPR, refs_decl, 0));
5350 refs_expr = build_int_cst (NULL_TREE, 0);
5352 /* UOBJC_INSTANCE_METHODS_decl/UOBJC_CLASS_METHODS_decl are set
5353 by generate_method_descriptors, which is called above. */
5354 initlist = build_protocol_initializer (TREE_TYPE (decl),
5355 protocol_name_expr, refs_expr,
5356 UOBJC_INSTANCE_METHODS_decl,
5357 UOBJC_CLASS_METHODS_decl);
5358 finish_var_decl (decl, initlist);
5363 build_protocol_initializer (tree type, tree protocol_name,
5364 tree protocol_list, tree instance_methods,
5368 tree cast_type = build_pointer_type
5369 (xref_tag (RECORD_TYPE,
5370 get_identifier (UTAG_CLASS)));
5371 VEC(constructor_elt,gc) *inits = NULL;
5373 /* Filling the "isa" in with one allows the runtime system to
5374 detect that the version change...should remove before final release. */
5376 expr = build_int_cst (cast_type, PROTOCOL_VERSION);
5377 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
5378 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, protocol_name);
5379 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, protocol_list);
5381 if (!instance_methods)
5382 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, 0));
5385 expr = convert (objc_method_proto_list_ptr,
5386 build_unary_op (input_location,
5387 ADDR_EXPR, instance_methods, 0));
5388 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
5392 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, 0));
5395 expr = convert (objc_method_proto_list_ptr,
5396 build_unary_op (input_location,
5397 ADDR_EXPR, class_methods, 0));
5398 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
5401 return objc_build_constructor (type, inits);
5404 /* struct _objc_category {
5405 char *category_name;
5407 struct _objc_method_list *instance_methods;
5408 struct _objc_method_list *class_methods;
5409 struct _objc_protocol_list *protocols;
5413 build_category_template (void)
5415 tree ptype, decls, *chain = NULL;
5417 objc_category_template = objc_start_struct (get_identifier (UTAG_CATEGORY));
5419 /* char *category_name; */
5420 decls = add_field_decl (string_type_node, "category_name", &chain);
5422 /* char *class_name; */
5423 add_field_decl (string_type_node, "class_name", &chain);
5425 /* struct _objc_method_list *instance_methods; */
5426 add_field_decl (objc_method_list_ptr, "instance_methods", &chain);
5428 /* struct _objc_method_list *class_methods; */
5429 add_field_decl (objc_method_list_ptr, "class_methods", &chain);
5431 /* struct _objc_protocol **protocol_list; */
5432 ptype = build_pointer_type (build_pointer_type (objc_protocol_template));
5433 add_field_decl (ptype, "protocol_list", &chain);
5435 objc_finish_struct (objc_category_template, decls);
5438 /* struct _objc_selector {
5444 build_selector_template (void)
5446 tree decls, *chain = NULL;
5448 objc_selector_template = objc_start_struct (get_identifier (UTAG_SELECTOR));
5451 decls = add_field_decl (objc_selector_type, "sel_id", &chain);
5453 /* char *sel_type; */
5454 add_field_decl (string_type_node, "sel_type", &chain);
5456 objc_finish_struct (objc_selector_template, decls);
5459 /* struct _objc_class {
5460 struct _objc_class *isa;
5461 struct _objc_class *super_class;
5466 struct _objc_ivar_list *ivars;
5467 struct _objc_method_list *methods;
5468 #ifdef __NEXT_RUNTIME__
5469 struct objc_cache *cache;
5471 struct sarray *dtable;
5472 struct _objc_class *subclass_list;
5473 struct _objc_class *sibling_class;
5475 struct _objc_protocol_list *protocols;
5476 #ifdef __NEXT_RUNTIME__
5479 void *gc_object_type;
5482 /* NB: The 'sel_id' and 'gc_object_type' fields are not being used by
5483 the NeXT/Apple runtime; still, the compiler must generate them to
5484 maintain backward binary compatibility (and to allow for future
5488 build_class_template (void)
5490 tree ptype, decls, *chain = NULL;
5492 objc_class_template = objc_start_struct (get_identifier (UTAG_CLASS));
5494 /* struct _objc_class *isa; */
5495 decls = add_field_decl (build_pointer_type (objc_class_template),
5498 /* struct _objc_class *super_class; */
5499 add_field_decl (build_pointer_type (objc_class_template),
5500 "super_class", &chain);
5503 add_field_decl (string_type_node, "name", &chain);
5506 add_field_decl (long_integer_type_node, "version", &chain);
5509 add_field_decl (long_integer_type_node, "info", &chain);
5511 /* long instance_size; */
5512 add_field_decl (long_integer_type_node, "instance_size", &chain);
5514 /* struct _objc_ivar_list *ivars; */
5515 add_field_decl (objc_ivar_list_ptr,"ivars", &chain);
5517 /* struct _objc_method_list *methods; */
5518 add_field_decl (objc_method_list_ptr, "methods", &chain);
5520 if (flag_next_runtime)
5522 /* struct objc_cache *cache; */
5523 ptype = build_pointer_type (xref_tag (RECORD_TYPE,
5524 get_identifier ("objc_cache")));
5525 add_field_decl (ptype, "cache", &chain);
5529 /* struct sarray *dtable; */
5530 ptype = build_pointer_type(xref_tag (RECORD_TYPE,
5531 get_identifier ("sarray")));
5532 add_field_decl (ptype, "dtable", &chain);
5534 /* struct objc_class *subclass_list; */
5535 ptype = build_pointer_type (objc_class_template);
5536 add_field_decl (ptype, "subclass_list", &chain);
5538 /* struct objc_class *sibling_class; */
5539 ptype = build_pointer_type (objc_class_template);
5540 add_field_decl (ptype, "sibling_class", &chain);
5543 /* struct _objc_protocol **protocol_list; */
5544 ptype = build_pointer_type (build_pointer_type
5545 (xref_tag (RECORD_TYPE,
5546 get_identifier (UTAG_PROTOCOL))));
5547 add_field_decl (ptype, "protocol_list", &chain);
5549 if (flag_next_runtime)
5552 add_field_decl (build_pointer_type (void_type_node), "sel_id", &chain);
5555 /* void *gc_object_type; */
5556 add_field_decl (build_pointer_type (void_type_node),
5557 "gc_object_type", &chain);
5559 objc_finish_struct (objc_class_template, decls);
5562 /* Generate appropriate forward declarations for an implementation. */
5565 synth_forward_declarations (void)
5569 /* static struct objc_class _OBJC_CLASS_<my_name>; */
5570 UOBJC_CLASS_decl = build_metadata_decl ("_OBJC_CLASS",
5571 objc_class_template);
5573 /* static struct objc_class _OBJC_METACLASS_<my_name>; */
5574 UOBJC_METACLASS_decl = build_metadata_decl ("_OBJC_METACLASS",
5575 objc_class_template);
5577 /* Pre-build the following entities - for speed/convenience. */
5579 an_id = get_identifier ("super_class");
5580 ucls_super_ref = objc_build_component_ref (UOBJC_CLASS_decl, an_id);
5581 uucls_super_ref = objc_build_component_ref (UOBJC_METACLASS_decl, an_id);
5585 error_with_ivar (const char *message, tree decl)
5587 error_at (DECL_SOURCE_LOCATION (decl), "%s %qs",
5588 message, identifier_to_locale (gen_declaration (decl)));
5593 check_ivars (tree inter, tree imp)
5595 tree intdecls = CLASS_RAW_IVARS (inter);
5596 tree impdecls = CLASS_RAW_IVARS (imp);
5603 if (intdecls && TREE_CODE (intdecls) == TYPE_DECL)
5604 intdecls = TREE_CHAIN (intdecls);
5606 if (intdecls == 0 && impdecls == 0)
5608 if (intdecls == 0 || impdecls == 0)
5610 error ("inconsistent instance variable specification");
5614 t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
5616 if (!comptypes (t1, t2)
5617 || !tree_int_cst_equal (DECL_INITIAL (intdecls),
5618 DECL_INITIAL (impdecls)))
5620 if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
5622 error_with_ivar ("conflicting instance variable type",
5624 error_with_ivar ("previous declaration of",
5627 else /* both the type and the name don't match */
5629 error ("inconsistent instance variable specification");
5634 else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
5636 error_with_ivar ("conflicting instance variable name",
5638 error_with_ivar ("previous declaration of",
5642 intdecls = DECL_CHAIN (intdecls);
5643 impdecls = DECL_CHAIN (impdecls);
5647 /* Set 'objc_super_template' to the data type node for 'struct _objc_super'.
5648 This needs to be done just once per compilation. */
5650 /* struct _objc_super {
5651 struct _objc_object *self;
5652 struct _objc_class *super_class;
5656 build_super_template (void)
5658 tree decls, *chain = NULL;
5660 objc_super_template = objc_start_struct (get_identifier (UTAG_SUPER));
5662 /* struct _objc_object *self; */
5663 decls = add_field_decl (objc_object_type, "self", &chain);
5665 /* struct _objc_class *super_class; */
5666 add_field_decl (build_pointer_type (objc_class_template),
5667 "super_class", &chain);
5669 objc_finish_struct (objc_super_template, decls);
5672 /* struct _objc_ivar {
5679 build_ivar_template (void)
5681 tree objc_ivar_id, objc_ivar_record;
5682 tree decls, *chain = NULL;
5684 objc_ivar_id = get_identifier (UTAG_IVAR);
5685 objc_ivar_record = objc_start_struct (objc_ivar_id);
5687 /* char *ivar_name; */
5688 decls = add_field_decl (string_type_node, "ivar_name", &chain);
5690 /* char *ivar_type; */
5691 add_field_decl (string_type_node, "ivar_type", &chain);
5693 /* int ivar_offset; */
5694 add_field_decl (integer_type_node, "ivar_offset", &chain);
5696 objc_finish_struct (objc_ivar_record, decls);
5698 return objc_ivar_record;
5703 struct objc_ivar ivar_list[ivar_count];
5707 build_ivar_list_template (tree list_type, int size)
5709 tree objc_ivar_list_record;
5710 tree array_type, decls, *chain = NULL;
5712 objc_ivar_list_record = objc_start_struct (NULL_TREE);
5714 /* int ivar_count; */
5715 decls = add_field_decl (integer_type_node, "ivar_count", &chain);
5717 /* struct objc_ivar ivar_list[]; */
5718 array_type = build_sized_array_type (list_type, size);
5719 add_field_decl (array_type, "ivar_list", &chain);
5721 objc_finish_struct (objc_ivar_list_record, decls);
5723 return objc_ivar_list_record;
5727 struct _objc__method_prototype_list *method_next;
5729 struct objc_method method_list[method_count];
5733 build_method_list_template (tree list_type, int size)
5735 tree objc_ivar_list_record;
5736 tree array_type, decls, *chain = NULL;
5738 objc_ivar_list_record = objc_start_struct (NULL_TREE);
5740 /* struct _objc__method_prototype_list *method_next; */
5741 decls = add_field_decl (objc_method_proto_list_ptr, "method_next", &chain);
5743 /* int method_count; */
5744 add_field_decl (integer_type_node, "method_count", &chain);
5746 /* struct objc_method method_list[]; */
5747 array_type = build_sized_array_type (list_type, size);
5748 add_field_decl (array_type, "method_list", &chain);
5750 objc_finish_struct (objc_ivar_list_record, decls);
5752 return objc_ivar_list_record;
5756 build_ivar_list_initializer (tree type, tree field_decl)
5758 VEC(constructor_elt,gc) *inits = NULL;
5762 VEC(constructor_elt,gc) *ivar = NULL;
5766 if (DECL_NAME (field_decl))
5767 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE,
5768 add_objc_string (DECL_NAME (field_decl),
5771 /* Unnamed bit-field ivar (yuck). */
5772 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, build_int_cst (NULL_TREE, 0));
5775 encode_field_decl (field_decl,
5776 obstack_object_size (&util_obstack),
5777 OBJC_ENCODE_DONT_INLINE_DEFS);
5779 /* Null terminate string. */
5780 obstack_1grow (&util_obstack, 0);
5781 id = add_objc_string (get_identifier (XOBFINISH (&util_obstack, char *)),
5783 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, id);
5784 obstack_free (&util_obstack, util_firstobj);
5787 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, byte_position (field_decl));
5788 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
5789 objc_build_constructor (type, ivar));
5791 field_decl = DECL_CHAIN (field_decl);
5792 while (field_decl && TREE_CODE (field_decl) != FIELD_DECL);
5796 return objc_build_constructor (build_array_type (type, 0), inits);
5800 generate_ivars_list (tree type, const char *name, int size, tree list)
5803 VEC(constructor_elt,gc) *inits = NULL;
5805 decl = start_var_decl (type, synth_id_with_class_suffix
5806 (name, objc_implementation_context));
5808 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, size));
5809 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, list);
5811 finish_var_decl (decl,
5812 objc_build_constructor (TREE_TYPE (decl), inits));
5817 /* Count only the fields occurring in T. */
5820 ivar_list_length (tree t)
5824 for (; t; t = DECL_CHAIN (t))
5825 if (TREE_CODE (t) == FIELD_DECL)
5832 generate_ivar_lists (void)
5834 tree initlist, ivar_list_template, chain;
5837 generating_instance_variables = 1;
5839 if (!objc_ivar_template)
5840 objc_ivar_template = build_ivar_template ();
5842 /* Only generate class variables for the root of the inheritance
5843 hierarchy since these will be the same for every class. */
5845 if (CLASS_SUPER_NAME (implementation_template) == NULL_TREE
5846 && (chain = TYPE_FIELDS (objc_class_template)))
5848 size = ivar_list_length (chain);
5850 ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
5851 initlist = build_ivar_list_initializer (objc_ivar_template, chain);
5853 UOBJC_CLASS_VARIABLES_decl
5854 = generate_ivars_list (ivar_list_template, "_OBJC_CLASS_VARIABLES",
5858 UOBJC_CLASS_VARIABLES_decl = 0;
5860 chain = CLASS_IVARS (implementation_template);
5863 size = ivar_list_length (chain);
5864 ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
5865 initlist = build_ivar_list_initializer (objc_ivar_template, chain);
5867 UOBJC_INSTANCE_VARIABLES_decl
5868 = generate_ivars_list (ivar_list_template, "_OBJC_INSTANCE_VARIABLES",
5872 UOBJC_INSTANCE_VARIABLES_decl = 0;
5874 generating_instance_variables = 0;
5878 build_dispatch_table_initializer (tree type, tree entries)
5880 VEC(constructor_elt,gc) *inits = NULL;
5884 VEC(constructor_elt,gc) *elems = NULL;
5887 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE,
5888 build_selector (METHOD_SEL_NAME (entries)));
5890 /* Generate the method encoding if we don't have one already. */
5891 if (! METHOD_ENCODING (entries))
5892 METHOD_ENCODING (entries) =
5893 encode_method_prototype (entries);
5895 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE,
5896 add_objc_string (METHOD_ENCODING (entries),
5899 expr = convert (ptr_type_node,
5900 build_unary_op (input_location, ADDR_EXPR,
5901 METHOD_DEFINITION (entries), 1));
5902 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE, expr);
5904 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
5905 objc_build_constructor (type, elems));
5907 entries = DECL_CHAIN (entries);
5911 return objc_build_constructor (build_array_type (type, 0), inits);
5914 /* To accomplish method prototyping without generating all kinds of
5915 inane warnings, the definition of the dispatch table entries were
5918 struct objc_method { SEL _cmd; ...; id (*_imp)(); };
5920 struct objc_method { SEL _cmd; ...; void *_imp; }; */
5923 build_method_template (void)
5926 tree decls, *chain = NULL;
5928 _SLT_record = objc_start_struct (get_identifier (UTAG_METHOD));
5931 decls = add_field_decl (objc_selector_type, "_cmd", &chain);
5933 /* char *method_types; */
5934 add_field_decl (string_type_node, "method_types", &chain);
5937 add_field_decl (build_pointer_type (void_type_node), "_imp", &chain);
5939 objc_finish_struct (_SLT_record, decls);
5946 generate_dispatch_table (tree type, const char *name, int size, tree list)
5949 VEC(constructor_elt,gc) *v = NULL;
5951 decl = start_var_decl (type, synth_id_with_class_suffix
5952 (name, objc_implementation_context));
5954 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
5955 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (integer_type_node, size));
5956 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, list);
5958 finish_var_decl (decl,
5959 objc_build_constructor (TREE_TYPE (decl), v));
5965 mark_referenced_methods (void)
5967 struct imp_entry *impent;
5970 for (impent = imp_list; impent; impent = impent->next)
5972 chain = CLASS_CLS_METHODS (impent->imp_context);
5975 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain)));
5976 chain = DECL_CHAIN (chain);
5979 chain = CLASS_NST_METHODS (impent->imp_context);
5982 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain)));
5983 chain = DECL_CHAIN (chain);
5989 generate_dispatch_tables (void)
5991 tree initlist, chain, method_list_template;
5994 if (!objc_method_template)
5995 objc_method_template = build_method_template ();
5997 chain = CLASS_CLS_METHODS (objc_implementation_context);
6000 size = list_length (chain);
6002 method_list_template
6003 = build_method_list_template (objc_method_template, size);
6005 = build_dispatch_table_initializer (objc_method_template, chain);
6007 UOBJC_CLASS_METHODS_decl
6008 = generate_dispatch_table (method_list_template,
6009 ((TREE_CODE (objc_implementation_context)
6010 == CLASS_IMPLEMENTATION_TYPE)
6011 ? "_OBJC_CLASS_METHODS"
6012 : "_OBJC_CATEGORY_CLASS_METHODS"),
6016 UOBJC_CLASS_METHODS_decl = 0;
6018 chain = CLASS_NST_METHODS (objc_implementation_context);
6021 size = list_length (chain);
6023 method_list_template
6024 = build_method_list_template (objc_method_template, size);
6026 = build_dispatch_table_initializer (objc_method_template, chain);
6028 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
6029 UOBJC_INSTANCE_METHODS_decl
6030 = generate_dispatch_table (method_list_template,
6031 "_OBJC_INSTANCE_METHODS",
6034 /* We have a category. */
6035 UOBJC_INSTANCE_METHODS_decl
6036 = generate_dispatch_table (method_list_template,
6037 "_OBJC_CATEGORY_INSTANCE_METHODS",
6041 UOBJC_INSTANCE_METHODS_decl = 0;
6045 generate_protocol_list (tree i_or_p)
6047 tree array_type, ptype, refs_decl, lproto, e, plist;
6049 const char *ref_name;
6050 VEC(constructor_elt,gc) *v = NULL;
6052 switch (TREE_CODE (i_or_p))
6054 case CLASS_INTERFACE_TYPE:
6055 case CATEGORY_INTERFACE_TYPE:
6056 plist = CLASS_PROTOCOL_LIST (i_or_p);
6058 case PROTOCOL_INTERFACE_TYPE:
6059 plist = PROTOCOL_LIST (i_or_p);
6066 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
6067 if (TREE_CODE (TREE_VALUE (lproto)) == PROTOCOL_INTERFACE_TYPE
6068 && PROTOCOL_FORWARD_DECL (TREE_VALUE (lproto)))
6071 /* Build initializer. */
6072 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6073 e = build_int_cst (build_pointer_type (objc_protocol_template), size);
6074 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, e);
6076 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
6078 tree pval = TREE_VALUE (lproto);
6080 if (TREE_CODE (pval) == PROTOCOL_INTERFACE_TYPE
6081 && PROTOCOL_FORWARD_DECL (pval))
6083 e = build_unary_op (input_location, ADDR_EXPR,
6084 PROTOCOL_FORWARD_DECL (pval), 0);
6085 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, e);
6089 /* static struct objc_protocol *refs[n]; */
6091 switch (TREE_CODE (i_or_p))
6093 case PROTOCOL_INTERFACE_TYPE:
6094 ref_name = synth_id_with_class_suffix ("_OBJC_PROTOCOL_REFS", i_or_p);
6096 case CLASS_INTERFACE_TYPE:
6097 ref_name = synth_id_with_class_suffix ("_OBJC_CLASS_PROTOCOLS", i_or_p);
6099 case CATEGORY_INTERFACE_TYPE:
6100 ref_name = synth_id_with_class_suffix ("_OBJC_CATEGORY_PROTOCOLS", i_or_p);
6106 ptype = build_pointer_type (objc_protocol_template);
6107 array_type = build_sized_array_type (ptype, size + 3);
6108 refs_decl = start_var_decl (array_type, ref_name);
6110 finish_var_decl (refs_decl,
6111 objc_build_constructor (TREE_TYPE (refs_decl), v));
6117 build_category_initializer (tree type, tree cat_name, tree class_name,
6118 tree instance_methods, tree class_methods,
6122 VEC(constructor_elt,gc) *v = NULL;
6124 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, cat_name);
6125 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, class_name);
6127 if (!instance_methods)
6128 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6131 expr = convert (objc_method_list_ptr,
6132 build_unary_op (input_location, ADDR_EXPR,
6133 instance_methods, 0));
6134 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6137 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6140 expr = convert (objc_method_list_ptr,
6141 build_unary_op (input_location, ADDR_EXPR,
6143 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6146 /* protocol_list = */
6148 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6151 expr = convert (build_pointer_type
6153 (objc_protocol_template)),
6154 build_unary_op (input_location, ADDR_EXPR,
6156 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6159 return objc_build_constructor (type, v);
6162 /* struct _objc_class {
6163 struct objc_class *isa;
6164 struct objc_class *super_class;
6169 struct objc_ivar_list *ivars;
6170 struct objc_method_list *methods;
6171 if (flag_next_runtime)
6172 struct objc_cache *cache;
6174 struct sarray *dtable;
6175 struct objc_class *subclass_list;
6176 struct objc_class *sibling_class;
6178 struct objc_protocol_list *protocols;
6179 if (flag_next_runtime)
6181 void *gc_object_type;
6185 build_shared_structure_initializer (tree type, tree isa, tree super,
6186 tree name, tree size, int status,
6187 tree dispatch_table, tree ivar_list,
6191 VEC(constructor_elt,gc) *v = NULL;
6194 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, isa);
6197 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, super);
6200 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, default_conversion (name));
6203 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6204 build_int_cst (long_integer_type_node, 0));
6207 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6208 build_int_cst (long_integer_type_node, status));
6210 /* instance_size = */
6211 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6212 convert (long_integer_type_node, size));
6214 /* objc_ivar_list = */
6216 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6219 expr = convert (objc_ivar_list_ptr,
6220 build_unary_op (input_location, ADDR_EXPR,
6222 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6225 /* objc_method_list = */
6226 if (!dispatch_table)
6227 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6230 expr = convert (objc_method_list_ptr,
6231 build_unary_op (input_location, ADDR_EXPR,
6232 dispatch_table, 0));
6233 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6236 if (flag_next_runtime)
6237 /* method_cache = */
6238 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6242 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6244 /* subclass_list = */
6245 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6247 /* sibling_class = */
6248 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6251 /* protocol_list = */
6252 if (! protocol_list)
6253 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6256 expr = convert (build_pointer_type
6258 (objc_protocol_template)),
6259 build_unary_op (input_location, ADDR_EXPR,
6261 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6264 if (flag_next_runtime)
6266 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6268 /* gc_object_type = NULL */
6269 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6271 return objc_build_constructor (type, v);
6274 /* Retrieve category interface CAT_NAME (if any) associated with CLASS. */
6277 lookup_category (tree klass, tree cat_name)
6279 tree category = CLASS_CATEGORY_LIST (klass);
6281 while (category && CLASS_SUPER_NAME (category) != cat_name)
6282 category = CLASS_CATEGORY_LIST (category);
6286 /* static struct objc_category _OBJC_CATEGORY_<name> = { ... }; */
6289 generate_category (struct imp_entry *impent)
6291 tree initlist, cat_name_expr, class_name_expr;
6292 tree protocol_decl, category;
6293 tree cat = impent->imp_context;
6295 implementation_template = impent->imp_template;
6296 UOBJC_CLASS_decl = impent->class_decl;
6297 UOBJC_METACLASS_decl = impent->meta_decl;
6299 add_class_reference (CLASS_NAME (cat));
6300 cat_name_expr = add_objc_string (CLASS_SUPER_NAME (cat), class_names);
6302 class_name_expr = add_objc_string (CLASS_NAME (cat), class_names);
6304 category = lookup_category (implementation_template,
6305 CLASS_SUPER_NAME (cat));
6307 if (category && CLASS_PROTOCOL_LIST (category))
6309 generate_protocol_references (CLASS_PROTOCOL_LIST (category));
6310 protocol_decl = generate_protocol_list (category);
6315 initlist = build_category_initializer (TREE_TYPE (UOBJC_CLASS_decl),
6316 cat_name_expr, class_name_expr,
6317 UOBJC_INSTANCE_METHODS_decl,
6318 UOBJC_CLASS_METHODS_decl,
6320 /* Finish and initialize the forward decl. */
6321 finish_var_decl (UOBJC_CLASS_decl, initlist);
6324 /* static struct objc_class _OBJC_METACLASS_Foo={ ... };
6325 static struct objc_class _OBJC_CLASS_Foo={ ... }; */
6328 generate_shared_structures (struct imp_entry *impent)
6330 tree name_expr, super_expr, root_expr;
6331 tree my_root_id, my_super_id;
6332 tree cast_type, initlist, protocol_decl;
6335 objc_implementation_context = impent->imp_context;
6336 implementation_template = impent->imp_template;
6337 UOBJC_CLASS_decl = impent->class_decl;
6338 UOBJC_METACLASS_decl = impent->meta_decl;
6339 cls_flags = impent->has_cxx_cdtors ? CLS_HAS_CXX_STRUCTORS : 0 ;
6341 my_super_id = CLASS_SUPER_NAME (implementation_template);
6344 add_class_reference (my_super_id);
6346 /* Compute "my_root_id" - this is required for code generation.
6347 the "isa" for all meta class structures points to the root of
6348 the inheritance hierarchy (e.g. "__Object")... */
6349 my_root_id = my_super_id;
6352 tree my_root_int = lookup_interface (my_root_id);
6354 if (my_root_int && CLASS_SUPER_NAME (my_root_int))
6355 my_root_id = CLASS_SUPER_NAME (my_root_int);
6362 /* No super class. */
6363 my_root_id = CLASS_NAME (implementation_template);
6365 cast_type = build_pointer_type (objc_class_template);
6366 name_expr = add_objc_string (CLASS_NAME (implementation_template),
6369 /* Install class `isa' and `super' pointers at runtime. */
6371 super_expr = add_objc_string (my_super_id, class_names);
6373 super_expr = integer_zero_node;
6375 super_expr = build_c_cast (input_location,
6376 cast_type, super_expr); /* cast! */
6378 root_expr = add_objc_string (my_root_id, class_names);
6379 root_expr = build_c_cast (input_location, cast_type, root_expr); /* cast! */
6381 if (CLASS_PROTOCOL_LIST (implementation_template))
6383 generate_protocol_references
6384 (CLASS_PROTOCOL_LIST (implementation_template));
6385 protocol_decl = generate_protocol_list (implementation_template);
6390 /* static struct objc_class _OBJC_METACLASS_Foo = { ... }; */
6393 = build_shared_structure_initializer
6394 (TREE_TYPE (UOBJC_METACLASS_decl),
6395 root_expr, super_expr, name_expr,
6396 convert (integer_type_node, TYPE_SIZE_UNIT (objc_class_template)),
6398 UOBJC_CLASS_METHODS_decl,
6399 UOBJC_CLASS_VARIABLES_decl,
6402 finish_var_decl (UOBJC_METACLASS_decl, initlist);
6404 /* static struct objc_class _OBJC_CLASS_Foo={ ... }; */
6407 = build_shared_structure_initializer
6408 (TREE_TYPE (UOBJC_CLASS_decl),
6409 build_unary_op (input_location, ADDR_EXPR, UOBJC_METACLASS_decl, 0),
6410 super_expr, name_expr,
6411 convert (integer_type_node,
6412 TYPE_SIZE_UNIT (CLASS_STATIC_TEMPLATE
6413 (implementation_template))),
6414 1 /*CLS_FACTORY*/ | cls_flags,
6415 UOBJC_INSTANCE_METHODS_decl,
6416 UOBJC_INSTANCE_VARIABLES_decl,
6419 finish_var_decl (UOBJC_CLASS_decl, initlist);
6424 synth_id_with_class_suffix (const char *preamble, tree ctxt)
6426 static char string[BUFSIZE];
6428 switch (TREE_CODE (ctxt))
6430 case CLASS_IMPLEMENTATION_TYPE:
6431 case CLASS_INTERFACE_TYPE:
6432 sprintf (string, "%s_%s", preamble,
6433 IDENTIFIER_POINTER (CLASS_NAME (ctxt)));
6435 case CATEGORY_IMPLEMENTATION_TYPE:
6436 case CATEGORY_INTERFACE_TYPE:
6438 /* We have a category. */
6439 const char *const class_name
6440 = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
6441 const char *const class_super_name
6442 = IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context));
6443 sprintf (string, "%s_%s_%s", preamble, class_name, class_super_name);
6446 case PROTOCOL_INTERFACE_TYPE:
6448 const char *protocol_name = IDENTIFIER_POINTER (PROTOCOL_NAME (ctxt));
6449 sprintf (string, "%s_%s", preamble, protocol_name);
6459 /* If type is empty or only type qualifiers are present, add default
6460 type of id (otherwise grokdeclarator will default to int). */
6462 adjust_type_for_id_default (tree type)
6465 type = make_node (TREE_LIST);
6467 if (!TREE_VALUE (type))
6468 TREE_VALUE (type) = objc_object_type;
6469 else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE
6470 && TYPED_OBJECT (TREE_VALUE (type)))
6471 error ("can not use an object as parameter to a method");
6476 /* Return a KEYWORD_DECL built using the specified key_name, arg_type,
6477 arg_name and attributes. (TODO: Rename KEYWORD_DECL to
6478 OBJC_METHOD_PARM_DECL ?)
6480 A KEYWORD_DECL is a tree representing the declaration of a
6481 parameter of an Objective-C method. It is produced when parsing a
6482 fragment of Objective-C method declaration of the form
6485 selector ':' '(' typename ')' identifier
6487 For example, take the Objective-C method
6489 -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type;
6491 the two fragments "pathForResource:(NSString *)resource" and
6492 "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The
6493 KEYWORD_DECL stores the 'key_name' (eg, identifier for
6494 "pathForResource"), the 'arg_type' (eg, tree representing a
6495 NSString *), the 'arg_name' (eg identifier for "resource") and
6496 potentially some attributes (for example, a tree representing
6497 __attribute__ ((unused)) if such an attribute was attached to a
6498 certain parameter). You can access this information using the
6499 TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name),
6500 KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes).
6502 'key_name' is an identifier node (and is optional as you can omit
6503 it in Objective-C methods).
6504 'arg_type' is a tree list (and is optional too if no parameter type
6506 'arg_name' is an identifier node and is required.
6507 'attributes' is an optional tree containing parameter attributes. */
6509 objc_build_keyword_decl (tree key_name, tree arg_type,
6510 tree arg_name, tree attributes)
6514 /* If no type is specified, default to "id". */
6515 arg_type = adjust_type_for_id_default (arg_type);
6517 keyword_decl = make_node (KEYWORD_DECL);
6519 TREE_TYPE (keyword_decl) = arg_type;
6520 KEYWORD_ARG_NAME (keyword_decl) = arg_name;
6521 KEYWORD_KEY_NAME (keyword_decl) = key_name;
6522 DECL_ATTRIBUTES (keyword_decl) = attributes;
6524 return keyword_decl;
6527 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
6529 build_keyword_selector (tree selector)
6532 tree key_chain, key_name;
6535 /* Scan the selector to see how much space we'll need. */
6536 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
6538 switch (TREE_CODE (selector))
6541 key_name = KEYWORD_KEY_NAME (key_chain);
6544 key_name = TREE_PURPOSE (key_chain);
6551 len += IDENTIFIER_LENGTH (key_name) + 1;
6553 /* Just a ':' arg. */
6557 buf = (char *) alloca (len + 1);
6558 /* Start the buffer out as an empty string. */
6561 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
6563 switch (TREE_CODE (selector))
6566 key_name = KEYWORD_KEY_NAME (key_chain);
6569 key_name = TREE_PURPOSE (key_chain);
6570 /* The keyword decl chain will later be used as a function
6571 argument chain. Unhook the selector itself so as to not
6572 confuse other parts of the compiler. */
6573 TREE_PURPOSE (key_chain) = NULL_TREE;
6580 strcat (buf, IDENTIFIER_POINTER (key_name));
6584 return get_identifier (buf);
6587 /* Used for declarations and definitions. */
6590 build_method_decl (enum tree_code code, tree ret_type, tree selector,
6591 tree add_args, bool ellipsis)
6595 /* If no type is specified, default to "id". */
6596 ret_type = adjust_type_for_id_default (ret_type);
6598 method_decl = make_node (code);
6599 TREE_TYPE (method_decl) = ret_type;
6601 /* If we have a keyword selector, create an identifier_node that
6602 represents the full selector name (`:' included)... */
6603 if (TREE_CODE (selector) == KEYWORD_DECL)
6605 METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
6606 METHOD_SEL_ARGS (method_decl) = selector;
6607 METHOD_ADD_ARGS (method_decl) = add_args;
6608 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis;
6612 METHOD_SEL_NAME (method_decl) = selector;
6613 METHOD_SEL_ARGS (method_decl) = NULL_TREE;
6614 METHOD_ADD_ARGS (method_decl) = NULL_TREE;
6620 #define METHOD_DEF 0
6621 #define METHOD_REF 1
6623 /* This routine processes objective-c method attributes. */
6626 objc_decl_method_attributes (tree *node, tree attributes, int flags)
6628 tree sentinel_attr = lookup_attribute ("sentinel", attributes);
6631 /* hackery to make an obj method look like a function type. */
6632 tree rettype = TREE_TYPE (*node);
6633 TREE_TYPE (*node) = build_function_type (TREE_VALUE (rettype),
6634 get_arg_type_list (*node, METHOD_REF, 0));
6635 decl_attributes (node, attributes, flags);
6636 METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node));
6637 TREE_TYPE (*node) = rettype;
6640 decl_attributes (node, attributes, flags);
6644 objc_method_decl (enum tree_code opcode)
6646 return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL;
6649 /* Used by `build_objc_method_call' and `comp_proto_with_proto'. Return
6650 an argument list for method METH. CONTEXT is either METHOD_DEF or
6651 METHOD_REF, saying whether we are trying to define a method or call
6652 one. SUPERFLAG says this is for a send to super; this makes a
6653 difference for the NeXT calling sequence in which the lookup and
6654 the method call are done together. If METH is null, user-defined
6655 arguments (i.e., beyond self and _cmd) shall be represented by `...'. */
6658 get_arg_type_list (tree meth, int context, int superflag)
6662 /* Receiver type. */
6663 if (flag_next_runtime && superflag)
6664 arglist = build_tree_list (NULL_TREE, objc_super_type);
6665 else if (context == METHOD_DEF && TREE_CODE (meth) == INSTANCE_METHOD_DECL)
6666 arglist = build_tree_list (NULL_TREE, objc_instance_type);
6668 arglist = build_tree_list (NULL_TREE, objc_object_type);
6670 /* Selector type - will eventually change to `int'. */
6671 chainon (arglist, build_tree_list (NULL_TREE, objc_selector_type));
6673 /* No actual method prototype given -- assume that remaining arguments
6678 /* Build a list of argument types. */
6679 for (akey = METHOD_SEL_ARGS (meth); akey; akey = DECL_CHAIN (akey))
6681 tree arg_type = TREE_VALUE (TREE_TYPE (akey));
6683 /* Decay argument types for the underlying C function as appropriate. */
6684 arg_type = objc_decay_parm_type (arg_type);
6686 chainon (arglist, build_tree_list (NULL_TREE, arg_type));
6689 if (METHOD_ADD_ARGS (meth))
6691 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (meth));
6692 akey; akey = TREE_CHAIN (akey))
6694 tree arg_type = TREE_TYPE (TREE_VALUE (akey));
6696 arg_type = objc_decay_parm_type (arg_type);
6698 chainon (arglist, build_tree_list (NULL_TREE, arg_type));
6701 if (!METHOD_ADD_ARGS_ELLIPSIS_P (meth))
6702 goto lack_of_ellipsis;
6707 chainon (arglist, OBJC_VOID_AT_END);
6714 check_duplicates (hash hsh, int methods, int is_class)
6716 tree meth = NULL_TREE;
6724 /* We have two or more methods with the same name but
6728 /* But just how different are those types? If
6729 -Wno-strict-selector-match is specified, we shall not
6730 complain if the differences are solely among types with
6731 identical size and alignment. */
6732 if (!warn_strict_selector_match)
6734 for (loop = hsh->list; loop; loop = loop->next)
6735 if (!comp_proto_with_proto (meth, loop->value, 0))
6744 bool type = TREE_CODE (meth) == INSTANCE_METHOD_DECL;
6746 warning_at (input_location, 0,
6747 "multiple methods named %<%c%E%> found",
6748 (is_class ? '+' : '-'),
6749 METHOD_SEL_NAME (meth));
6750 inform (DECL_SOURCE_LOCATION (meth), "using %<%c%s%>",
6752 identifier_to_locale (gen_method_decl (meth)));
6756 bool type = TREE_CODE (meth) == INSTANCE_METHOD_DECL;
6758 warning_at (input_location, 0,
6759 "multiple selectors named %<%c%E%> found",
6760 (is_class ? '+' : '-'),
6761 METHOD_SEL_NAME (meth));
6762 inform (DECL_SOURCE_LOCATION (meth), "found %<%c%s%>",
6764 identifier_to_locale (gen_method_decl (meth)));
6767 for (loop = hsh->list; loop; loop = loop->next)
6769 bool type = TREE_CODE (loop->value) == INSTANCE_METHOD_DECL;
6771 inform (DECL_SOURCE_LOCATION (loop->value), "also found %<%c%s%>",
6773 identifier_to_locale (gen_method_decl (loop->value)));
6780 /* If RECEIVER is a class reference, return the identifier node for
6781 the referenced class. RECEIVER is created by objc_get_class_reference,
6782 so we check the exact form created depending on which runtimes are
6786 receiver_is_class_object (tree receiver, int self, int super)
6788 tree chain, exp, arg;
6790 /* The receiver is 'self' or 'super' in the context of a class method. */
6791 if (objc_method_context
6792 && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
6795 ? CLASS_SUPER_NAME (implementation_template)
6796 : CLASS_NAME (implementation_template));
6798 if (flag_next_runtime)
6800 /* The receiver is a variable created by
6801 build_class_reference_decl. */
6802 if (TREE_CODE (receiver) == VAR_DECL && IS_CLASS (TREE_TYPE (receiver)))
6803 /* Look up the identifier. */
6804 for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
6805 if (TREE_PURPOSE (chain) == receiver)
6806 return TREE_VALUE (chain);
6809 /* The receiver is a function call that returns an id. Check if
6810 it is a call to objc_getClass, if so, pick up the class name. */
6811 if (TREE_CODE (receiver) == CALL_EXPR
6812 && (exp = CALL_EXPR_FN (receiver))
6813 && TREE_CODE (exp) == ADDR_EXPR
6814 && (exp = TREE_OPERAND (exp, 0))
6815 && TREE_CODE (exp) == FUNCTION_DECL
6816 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
6817 prototypes for objc_get_class(). Thankfully, they seem to share the
6818 same function type. */
6819 && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl)
6820 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), TAG_GETCLASS)
6821 /* We have a call to objc_get_class/objc_getClass! */
6822 && (arg = CALL_EXPR_ARG (receiver, 0)))
6825 if (TREE_CODE (arg) == ADDR_EXPR
6826 && (arg = TREE_OPERAND (arg, 0))
6827 && TREE_CODE (arg) == STRING_CST)
6828 /* Finally, we have the class name. */
6829 return get_identifier (TREE_STRING_POINTER (arg));
6834 /* If we are currently building a message expr, this holds
6835 the identifier of the selector of the message. This is
6836 used when printing warnings about argument mismatches. */
6838 static tree current_objc_message_selector = 0;
6841 objc_message_selector (void)
6843 return current_objc_message_selector;
6846 /* Construct an expression for sending a message.
6847 MESS has the object to send to in TREE_PURPOSE
6848 and the argument list (including selector) in TREE_VALUE.
6850 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
6851 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
6854 objc_build_message_expr (tree mess)
6856 tree receiver = TREE_PURPOSE (mess);
6859 tree args = TREE_PURPOSE (TREE_VALUE (mess));
6861 tree args = TREE_VALUE (mess);
6863 tree method_params = NULL_TREE;
6865 if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK)
6866 return error_mark_node;
6868 /* Obtain the full selector name. */
6869 switch (TREE_CODE (args))
6871 case IDENTIFIER_NODE:
6872 /* A unary selector. */
6876 sel_name = build_keyword_selector (args);
6882 /* Build the parameter list to give to the method. */
6883 if (TREE_CODE (args) == TREE_LIST)
6885 method_params = chainon (args, TREE_VALUE (TREE_VALUE (mess)));
6888 tree chain = args, prev = NULL_TREE;
6890 /* We have a keyword selector--check for comma expressions. */
6893 tree element = TREE_VALUE (chain);
6895 /* We have a comma expression, must collapse... */
6896 if (TREE_CODE (element) == TREE_LIST)
6899 TREE_CHAIN (prev) = element;
6904 chain = TREE_CHAIN (chain);
6906 method_params = args;
6911 if (processing_template_decl)
6912 /* Must wait until template instantiation time. */
6913 return build_min_nt (MESSAGE_SEND_EXPR, receiver, sel_name,
6917 return objc_finish_message_expr (receiver, sel_name, method_params);
6920 /* Look up method SEL_NAME that would be suitable for receiver
6921 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
6922 nonzero), and report on any duplicates. */
6925 lookup_method_in_hash_lists (tree sel_name, int is_class)
6927 hash method_prototype = NULL;
6930 method_prototype = hash_lookup (nst_method_hash_list,
6933 if (!method_prototype)
6935 method_prototype = hash_lookup (cls_method_hash_list,
6940 return check_duplicates (method_prototype, 1, is_class);
6943 /* The 'objc_finish_message_expr' routine is called from within
6944 'objc_build_message_expr' for non-template functions. In the case of
6945 C++ template functions, it is called from 'build_expr_from_tree'
6946 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded. */
6949 objc_finish_message_expr (tree receiver, tree sel_name, tree method_params)
6951 tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype;
6952 tree selector, retval, class_tree;
6953 int self, super, have_cast;
6955 /* We have used the receiver, so mark it as read. */
6956 mark_exp_read (receiver);
6958 /* Extract the receiver of the message, as well as its type
6959 (where the latter may take the form of a cast or be inferred
6960 from the implementation context). */
6962 while (TREE_CODE (rtype) == COMPOUND_EXPR
6963 || TREE_CODE (rtype) == MODIFY_EXPR
6964 || CONVERT_EXPR_P (rtype)
6965 || TREE_CODE (rtype) == COMPONENT_REF)
6966 rtype = TREE_OPERAND (rtype, 0);
6968 self = (rtype == self_decl);
6969 super = (rtype == UOBJC_SUPER_decl);
6970 rtype = TREE_TYPE (receiver);
6972 have_cast = (TREE_CODE (receiver) == NOP_EXPR
6973 || (TREE_CODE (receiver) == COMPOUND_EXPR
6974 && !IS_SUPER (rtype)));
6976 /* If we are calling [super dealloc], reset our warning flag. */
6977 if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name)))
6978 should_call_super_dealloc = 0;
6980 /* If the receiver is a class object, retrieve the corresponding
6981 @interface, if one exists. */
6982 class_tree = receiver_is_class_object (receiver, self, super);
6984 /* Now determine the receiver type (if an explicit cast has not been
6989 rtype = lookup_interface (class_tree);
6990 /* Handle `self' and `super'. */
6993 if (!CLASS_SUPER_NAME (implementation_template))
6995 error ("no super class declared in @interface for %qE",
6996 CLASS_NAME (implementation_template));
6997 return error_mark_node;
6999 rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template));
7002 rtype = lookup_interface (CLASS_NAME (implementation_template));
7005 /* If receiver is of type `id' or `Class' (or if the @interface for a
7006 class is not visible), we shall be satisfied with the existence of
7007 any instance or class method. */
7008 if (objc_is_id (rtype))
7010 class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE);
7011 rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
7012 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
7018 /* If messaging 'id <Protos>' or 'Class <Proto>', first search
7019 in protocols themselves for the method prototype. */
7021 = lookup_method_in_protocol_list (rprotos, sel_name,
7022 class_tree != NULL_TREE);
7024 /* If messaging 'Class <Proto>' but did not find a class method
7025 prototype, search for an instance method instead, and warn
7026 about having done so. */
7027 if (!method_prototype && !rtype && class_tree != NULL_TREE)
7030 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
7032 if (method_prototype)
7033 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
7034 sel_name, sel_name);
7040 tree orig_rtype = rtype;
7042 if (TREE_CODE (rtype) == POINTER_TYPE)
7043 rtype = TREE_TYPE (rtype);
7044 /* Traverse typedef aliases */
7045 while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype)
7046 && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL
7047 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)))
7048 rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype));
7049 if (TYPED_OBJECT (rtype))
7051 rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype);
7052 rtype = TYPE_OBJC_INTERFACE (rtype);
7054 /* If we could not find an @interface declaration, we must have
7055 only seen a @class declaration; so, we cannot say anything
7056 more intelligent about which methods the receiver will
7058 if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE)
7061 /* We could not find an @interface declaration, yet Message maybe in a
7062 @class's protocol. */
7063 if (!method_prototype && rprotos)
7065 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
7067 else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE
7068 || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE)
7070 /* We have a valid ObjC class name. Look up the method name
7071 in the published @interface for the class (and its
7074 = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE);
7076 /* If the method was not found in the @interface, it may still
7077 exist locally as part of the @implementation. */
7078 if (!method_prototype && objc_implementation_context
7079 && CLASS_NAME (objc_implementation_context)
7080 == OBJC_TYPE_NAME (rtype))
7084 ? CLASS_CLS_METHODS (objc_implementation_context)
7085 : CLASS_NST_METHODS (objc_implementation_context)),
7088 /* If we haven't found a candidate method by now, try looking for
7089 it in the protocol list. */
7090 if (!method_prototype && rprotos)
7092 = lookup_method_in_protocol_list (rprotos, sel_name,
7093 class_tree != NULL_TREE);
7097 warning (0, "invalid receiver type %qs",
7098 identifier_to_locale (gen_type_name (orig_rtype)));
7099 /* After issuing the "invalid receiver" warning, perform method
7100 lookup as if we were messaging 'id'. */
7101 rtype = rprotos = NULL_TREE;
7106 /* For 'id' or 'Class' receivers, search in the global hash table
7107 as a last resort. For all receivers, warn if protocol searches
7109 if (!method_prototype)
7112 warning (0, "%<%c%E%> not found in protocol(s)",
7113 (class_tree ? '+' : '-'),
7118 = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE);
7121 if (!method_prototype)
7123 static bool warn_missing_methods = false;
7126 warning (0, "%qE may not respond to %<%c%E%>",
7127 OBJC_TYPE_NAME (rtype),
7128 (class_tree ? '+' : '-'),
7130 /* If we are messaging an 'id' or 'Class' object and made it here,
7131 then we have failed to find _any_ instance or class method,
7134 warning (0, "no %<%c%E%> method found",
7135 (class_tree ? '+' : '-'),
7138 if (!warn_missing_methods)
7140 warning_at (input_location,
7141 0, "(Messages without a matching method signature");
7142 warning_at (input_location,
7143 0, "will be assumed to return %<id%> and accept");
7144 warning_at (input_location,
7145 0, "%<...%> as arguments.)");
7146 warn_missing_methods = true;
7150 /* Save the selector name for printing error messages. */
7151 current_objc_message_selector = sel_name;
7153 /* Build the parameters list for looking up the method.
7154 These are the object itself and the selector. */
7156 if (flag_typed_selectors)
7157 selector = build_typed_selector_reference (input_location,
7158 sel_name, method_prototype);
7160 selector = build_selector_reference (input_location, sel_name);
7162 retval = build_objc_method_call (input_location, super, method_prototype,
7164 selector, method_params);
7166 current_objc_message_selector = 0;
7171 /* Build a tree expression to send OBJECT the operation SELECTOR,
7172 looking up the method on object LOOKUP_OBJECT (often same as OBJECT),
7173 assuming the method has prototype METHOD_PROTOTYPE.
7174 (That is an INSTANCE_METHOD_DECL or CLASS_METHOD_DECL.)
7175 LOC is the location of the expression to build.
7176 Use METHOD_PARAMS as list of args to pass to the method.
7177 If SUPER_FLAG is nonzero, we look up the superclass's method. */
7180 build_objc_method_call (location_t loc, int super_flag, tree method_prototype,
7181 tree lookup_object, tree selector,
7184 tree sender = (super_flag ? umsg_super_decl :
7185 (!flag_next_runtime || flag_nil_receivers
7186 ? (flag_objc_direct_dispatch
7189 : umsg_nonnil_decl));
7190 tree rcv_p = (super_flag ? objc_super_type : objc_object_type);
7191 VEC(tree, gc) *parms = NULL;
7192 unsigned nparm = (method_params ? list_length (method_params) : 0);
7194 /* If a prototype for the method to be called exists, then cast
7195 the sender's return type and arguments to match that of the method.
7196 Otherwise, leave sender as is. */
7199 ? TREE_VALUE (TREE_TYPE (method_prototype))
7200 : objc_object_type);
7202 tree method_param_types =
7203 get_arg_type_list (method_prototype, METHOD_REF, super_flag);
7204 tree ftype = build_function_type (ret_type, method_param_types);
7208 if (method_prototype && METHOD_TYPE_ATTRIBUTES (method_prototype))
7209 ftype = build_type_attribute_variant (
7210 ftype, METHOD_TYPE_ATTRIBUTES (method_prototype));
7212 sender_cast = build_pointer_type (ftype);
7214 if (method_prototype && TREE_DEPRECATED (method_prototype))
7215 warn_deprecated_use (method_prototype, NULL_TREE);
7217 lookup_object = build_c_cast (loc, rcv_p, lookup_object);
7219 /* Use SAVE_EXPR to avoid evaluating the receiver twice. */
7220 lookup_object = save_expr (lookup_object);
7222 /* Param list + 2 slots for object and selector. */
7223 parms = VEC_alloc (tree, gc, nparm + 2);
7225 if (flag_next_runtime)
7227 /* If we are returning a struct in memory, and the address
7228 of that memory location is passed as a hidden first
7229 argument, then change which messenger entry point this
7230 expr will call. NB: Note that sender_cast remains
7231 unchanged (it already has a struct return type). */
7232 if (!targetm.calls.struct_value_rtx (0, 0)
7233 && (TREE_CODE (ret_type) == RECORD_TYPE
7234 || TREE_CODE (ret_type) == UNION_TYPE)
7235 && targetm.calls.return_in_memory (ret_type, 0))
7236 sender = (super_flag ? umsg_super_stret_decl :
7237 flag_nil_receivers ? umsg_stret_decl : umsg_nonnil_stret_decl);
7239 method = build_fold_addr_expr_loc (input_location, sender);
7240 /* Pass the object to the method. */
7241 VEC_quick_push (tree, parms, lookup_object);
7245 /* This is the portable (GNU) way. */
7246 /* First, call the lookup function to get a pointer to the method,
7247 then cast the pointer, then call it with the method arguments. */
7248 VEC(tree, gc) *tv = VEC_alloc (tree, gc, 2);
7249 VEC_quick_push (tree, tv, lookup_object);
7250 VEC_quick_push (tree, tv, selector);
7251 method = build_function_call_vec (loc, sender, tv, NULL);
7252 VEC_free (tree, gc, tv);
7254 /* Pass the appropriate object to the method. */
7255 VEC_quick_push (tree, parms, (super_flag ? self_decl : lookup_object));
7258 /* Pass the selector to the method. */
7259 VEC_quick_push (tree, parms, selector);
7260 /* Now append the remainder of the parms. */
7262 for (; method_params; method_params = TREE_CHAIN (method_params))
7263 VEC_quick_push (tree, parms, TREE_VALUE (method_params));
7265 /* Build an obj_type_ref, with the correct cast for the method call. */
7266 t = build3 (OBJ_TYPE_REF, sender_cast, method,
7267 lookup_object, size_zero_node);
7268 t = build_function_call_vec (loc, t, parms, NULL);\
7269 VEC_free (tree, gc, parms);
7274 build_protocol_reference (tree p)
7277 const char *proto_name;
7279 /* static struct _objc_protocol _OBJC_PROTOCOL_<mumble>; */
7281 proto_name = synth_id_with_class_suffix ("_OBJC_PROTOCOL", p);
7282 decl = start_var_decl (objc_protocol_template, proto_name);
7284 PROTOCOL_FORWARD_DECL (p) = decl;
7287 /* This function is called by the parser when (and only when) a
7288 @protocol() expression is found, in order to compile it. */
7290 objc_build_protocol_expr (tree protoname)
7293 tree p = lookup_protocol (protoname);
7297 error ("cannot find protocol declaration for %qE",
7299 return error_mark_node;
7302 if (!PROTOCOL_FORWARD_DECL (p))
7303 build_protocol_reference (p);
7305 expr = build_unary_op (input_location,
7306 ADDR_EXPR, PROTOCOL_FORWARD_DECL (p), 0);
7308 /* ??? Ideally we'd build the reference with objc_protocol_type directly,
7309 if we have it, rather than converting it here. */
7310 expr = convert (objc_protocol_type, expr);
7312 /* The @protocol() expression is being compiled into a pointer to a
7313 statically allocated instance of the Protocol class. To become
7314 usable at runtime, the 'isa' pointer of the instance need to be
7315 fixed up at runtime by the runtime library, to point to the
7316 actual 'Protocol' class. */
7318 /* For the GNU runtime, put the static Protocol instance in the list
7319 of statically allocated instances, so that we make sure that its
7320 'isa' pointer is fixed up at runtime by the GNU runtime library
7321 to point to the Protocol class (at runtime, when loading the
7322 module, the GNU runtime library loops on the statically allocated
7323 instances (as found in the defs field in objc_symtab) and fixups
7324 all the 'isa' pointers of those objects). */
7325 if (! flag_next_runtime)
7327 /* This type is a struct containing the fields of a Protocol
7328 object. (Cfr. objc_protocol_type instead is the type of a pointer
7329 to such a struct). */
7330 tree protocol_struct_type = xref_tag
7331 (RECORD_TYPE, get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
7334 /* Look for the list of Protocol statically allocated instances
7335 to fixup at runtime. Create a new list to hold Protocol
7336 statically allocated instances, if the list is not found. At
7337 present there is only another list, holding NSConstantString
7338 static instances to be fixed up at runtime. */
7339 for (chain = &objc_static_instances;
7340 *chain && TREE_VALUE (*chain) != protocol_struct_type;
7341 chain = &TREE_CHAIN (*chain));
7344 *chain = tree_cons (NULL_TREE, protocol_struct_type, NULL_TREE);
7345 add_objc_string (OBJC_TYPE_NAME (protocol_struct_type),
7349 /* Add this statically allocated instance to the Protocol list. */
7350 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE,
7351 PROTOCOL_FORWARD_DECL (p),
7352 TREE_PURPOSE (*chain));
7359 /* This function is called by the parser when a @selector() expression
7360 is found, in order to compile it. It is only called by the parser
7361 and only to compile a @selector(). LOC is the location of the
7364 objc_build_selector_expr (location_t loc, tree selnamelist)
7368 /* Obtain the full selector name. */
7369 switch (TREE_CODE (selnamelist))
7371 case IDENTIFIER_NODE:
7372 /* A unary selector. */
7373 selname = selnamelist;
7376 selname = build_keyword_selector (selnamelist);
7382 /* If we are required to check @selector() expressions as they
7383 are found, check that the selector has been declared. */
7384 if (warn_undeclared_selector)
7386 /* Look the selector up in the list of all known class and
7387 instance methods (up to this line) to check that the selector
7391 /* First try with instance methods. */
7392 hsh = hash_lookup (nst_method_hash_list, selname);
7394 /* If not found, try with class methods. */
7397 hsh = hash_lookup (cls_method_hash_list, selname);
7400 /* If still not found, print out a warning. */
7403 warning (0, "undeclared selector %qE", selname);
7408 if (flag_typed_selectors)
7409 return build_typed_selector_reference (loc, selname, 0);
7411 return build_selector_reference (loc, selname);
7414 /* This is used to implement @encode(). See gcc/doc/objc.texi,
7415 section '@encode'. */
7417 objc_build_encode_expr (tree type)
7422 encode_type (type, obstack_object_size (&util_obstack),
7423 OBJC_ENCODE_INLINE_DEFS);
7424 obstack_1grow (&util_obstack, 0); /* null terminate string */
7425 string = XOBFINISH (&util_obstack, const char *);
7427 /* Synthesize a string that represents the encoded struct/union. */
7428 result = my_build_string (strlen (string) + 1, string);
7429 obstack_free (&util_obstack, util_firstobj);
7434 build_ivar_reference (tree id)
7436 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
7438 /* Historically, a class method that produced objects (factory
7439 method) would assign `self' to the instance that it
7440 allocated. This would effectively turn the class method into
7441 an instance method. Following this assignment, the instance
7442 variables could be accessed. That practice, while safe,
7443 violates the simple rule that a class method should not refer
7444 to an instance variable. It's better to catch the cases
7445 where this is done unknowingly than to support the above
7447 warning (0, "instance variable %qE accessed in class method",
7449 self_decl = convert (objc_instance_type, self_decl); /* cast */
7452 return objc_build_component_ref (build_indirect_ref (input_location,
7453 self_decl, RO_ARROW),
7457 /* Compute a hash value for a given method SEL_NAME. */
7460 hash_func (tree sel_name)
7462 const unsigned char *s
7463 = (const unsigned char *)IDENTIFIER_POINTER (sel_name);
7467 h = h * 67 + *s++ - 113;
7474 nst_method_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
7475 cls_method_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
7477 cls_name_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
7478 als_name_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
7480 /* Initialize the hash table used to hold the constant string objects. */
7481 string_htab = htab_create_ggc (31, string_hash,
7485 /* This routine adds sel_name to the hash list. sel_name is a class or alias
7486 name for the class. If alias name, then value is its underlying class.
7487 If class, the value is NULL_TREE. */
7490 hash_class_name_enter (hash *hashlist, tree sel_name, tree value)
7493 int slot = hash_func (sel_name) % SIZEHASHTABLE;
7495 obj = ggc_alloc_hashed_entry ();
7496 if (value != NULL_TREE)
7498 /* Save the underlying class for the 'alias' in the hash table */
7499 attr obj_attr = ggc_alloc_hashed_attribute ();
7500 obj_attr->value = value;
7501 obj->list = obj_attr;
7505 obj->next = hashlist[slot];
7506 obj->key = sel_name;
7508 hashlist[slot] = obj; /* append to front */
7513 Searches in the hash table looking for a match for class or alias name.
7517 hash_class_name_lookup (hash *hashlist, tree sel_name)
7521 target = hashlist[hash_func (sel_name) % SIZEHASHTABLE];
7525 if (sel_name == target->key)
7528 target = target->next;
7533 /* WARNING!!!! hash_enter is called with a method, and will peek
7534 inside to find its selector! But hash_lookup is given a selector
7535 directly, and looks for the selector that's inside the found
7536 entry's key (method) for comparison. */
7539 hash_enter (hash *hashlist, tree method)
7542 int slot = hash_func (METHOD_SEL_NAME (method)) % SIZEHASHTABLE;
7544 obj = ggc_alloc_hashed_entry ();
7546 obj->next = hashlist[slot];
7549 hashlist[slot] = obj; /* append to front */
7553 hash_lookup (hash *hashlist, tree sel_name)
7557 target = hashlist[hash_func (sel_name) % SIZEHASHTABLE];
7561 if (sel_name == METHOD_SEL_NAME (target->key))
7564 target = target->next;
7570 hash_add_attr (hash entry, tree value)
7574 obj = ggc_alloc_hashed_attribute ();
7575 obj->next = entry->list;
7578 entry->list = obj; /* append to front */
7582 lookup_method (tree mchain, tree method)
7586 if (TREE_CODE (method) == IDENTIFIER_NODE)
7589 key = METHOD_SEL_NAME (method);
7593 if (METHOD_SEL_NAME (mchain) == key)
7596 mchain = DECL_CHAIN (mchain);
7601 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance method
7602 in INTERFACE, along with any categories and protocols attached thereto.
7603 If method is not found, and the OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS,
7604 recursively examine the INTERFACE's superclass. If OBJC_LOOKUP_CLASS is
7605 set, OBJC_LOOKUP_NO_SUPER is cleared, and no suitable class method could
7606 be found in INTERFACE or any of its superclasses, look for an _instance_
7607 method of the same name in the root class as a last resort.
7609 If a suitable method cannot be found, return NULL_TREE. */
7612 lookup_method_static (tree interface, tree ident, int flags)
7614 tree meth = NULL_TREE, root_inter = NULL_TREE;
7615 tree inter = interface;
7616 int is_class = (flags & OBJC_LOOKUP_CLASS);
7617 int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER);
7621 tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter);
7622 tree category = inter;
7624 /* First, look up the method in the class itself. */
7625 if ((meth = lookup_method (chain, ident)))
7628 /* Failing that, look for the method in each category of the class. */
7629 while ((category = CLASS_CATEGORY_LIST (category)))
7631 chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category);
7633 /* Check directly in each category. */
7634 if ((meth = lookup_method (chain, ident)))
7637 /* Failing that, check in each category's protocols. */
7638 if (CLASS_PROTOCOL_LIST (category))
7640 if ((meth = (lookup_method_in_protocol_list
7641 (CLASS_PROTOCOL_LIST (category), ident, is_class))))
7646 /* If not found in categories, check in protocols of the main class. */
7647 if (CLASS_PROTOCOL_LIST (inter))
7649 if ((meth = (lookup_method_in_protocol_list
7650 (CLASS_PROTOCOL_LIST (inter), ident, is_class))))
7654 /* If we were instructed not to look in superclasses, don't. */
7655 if (no_superclasses)
7658 /* Failing that, climb up the inheritance hierarchy. */
7660 inter = lookup_interface (CLASS_SUPER_NAME (inter));
7664 /* If no class (factory) method was found, check if an _instance_
7665 method of the same name exists in the root class. This is what
7666 the Objective-C runtime will do. If an instance method was not
7668 return is_class ? lookup_method_static (root_inter, ident, 0): NULL_TREE;
7671 /* Add the method to the hash list if it doesn't contain an identical
7675 add_method_to_hash_list (hash *hash_list, tree method)
7679 if (!(hsh = hash_lookup (hash_list, METHOD_SEL_NAME (method))))
7681 /* Install on a global chain. */
7682 hash_enter (hash_list, method);
7686 /* Check types against those; if different, add to a list. */
7688 int already_there = comp_proto_with_proto (method, hsh->key, 1);
7689 for (loop = hsh->list; !already_there && loop; loop = loop->next)
7690 already_there |= comp_proto_with_proto (method, loop->value, 1);
7692 hash_add_attr (hsh, method);
7697 objc_add_method (tree klass, tree method, int is_class, bool is_optional)
7701 /* @optional methods are added to protocol's OPTIONAL list */
7704 gcc_assert (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE);
7705 if (!(mth = lookup_method (is_class
7706 ? PROTOCOL_OPTIONAL_CLS_METHODS (klass)
7707 : PROTOCOL_OPTIONAL_NST_METHODS (klass),
7712 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass);
7713 PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method;
7717 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass);
7718 PROTOCOL_OPTIONAL_NST_METHODS (klass) = method;
7722 else if (!(mth = lookup_method (is_class
7723 ? CLASS_CLS_METHODS (klass)
7724 : CLASS_NST_METHODS (klass), method)))
7726 /* put method on list in reverse order */
7729 DECL_CHAIN (method) = CLASS_CLS_METHODS (klass);
7730 CLASS_CLS_METHODS (klass) = method;
7734 DECL_CHAIN (method) = CLASS_NST_METHODS (klass);
7735 CLASS_NST_METHODS (klass) = method;
7740 /* When processing an @interface for a class or category, give hard
7741 errors on methods with identical selectors but differing argument
7742 and/or return types. We do not do this for @implementations, because
7743 C/C++ will do it for us (i.e., there will be duplicate function
7744 definition errors). */
7745 if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE
7746 || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE)
7747 && !comp_proto_with_proto (method, mth, 1))
7748 error ("duplicate declaration of method %<%c%E%>",
7749 is_class ? '+' : '-',
7750 METHOD_SEL_NAME (mth));
7754 add_method_to_hash_list (cls_method_hash_list, method);
7757 add_method_to_hash_list (nst_method_hash_list, method);
7759 /* Instance methods in root classes (and categories thereof)
7760 may act as class methods as a last resort. We also add
7761 instance methods listed in @protocol declarations to
7762 the class hash table, on the assumption that @protocols
7763 may be adopted by root classes or categories. */
7764 if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
7765 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
7766 klass = lookup_interface (CLASS_NAME (klass));
7768 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE
7769 || !CLASS_SUPER_NAME (klass))
7770 add_method_to_hash_list (cls_method_hash_list, method);
7777 add_class (tree class_name, tree name)
7779 struct interface_tuple **slot;
7781 /* Put interfaces on list in reverse order. */
7782 TREE_CHAIN (class_name) = interface_chain;
7783 interface_chain = class_name;
7785 if (interface_htab == NULL)
7786 interface_htab = htab_create_ggc (31, hash_interface, eq_interface, NULL);
7787 slot = (struct interface_tuple **)
7788 htab_find_slot_with_hash (interface_htab, name,
7789 IDENTIFIER_HASH_VALUE (name),
7793 *slot = ggc_alloc_cleared_interface_tuple ();
7796 (*slot)->class_name = class_name;
7798 return interface_chain;
7802 add_category (tree klass, tree category)
7804 /* Put categories on list in reverse order. */
7805 tree cat = lookup_category (klass, CLASS_SUPER_NAME (category));
7809 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
7811 CLASS_SUPER_NAME (category));
7815 CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass);
7816 CLASS_CATEGORY_LIST (klass) = category;
7820 /* Called after parsing each instance variable declaration. Necessary to
7821 preserve typedefs and implement public/private...
7823 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
7826 add_instance_variable (tree klass, objc_ivar_visibility_kind visibility,
7829 tree field_type = TREE_TYPE (field_decl);
7830 const char *ivar_name = DECL_NAME (field_decl)
7831 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl)))
7835 if (TREE_CODE (field_type) == REFERENCE_TYPE)
7837 error ("illegal reference type specified for instance variable %qs",
7839 /* Return class as is without adding this ivar. */
7844 if (field_type == error_mark_node || !TYPE_SIZE (field_type)
7845 || TYPE_SIZE (field_type) == error_mark_node)
7846 /* 'type[0]' is allowed, but 'type[]' is not! */
7848 error ("instance variable %qs has unknown size", ivar_name);
7849 /* Return class as is without adding this ivar. */
7854 /* Check if the ivar being added has a non-POD C++ type. If so, we will
7855 need to either (1) warn the user about it or (2) generate suitable
7856 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
7857 methods (if '-fobjc-call-cxx-cdtors' was specified). */
7858 if (MAYBE_CLASS_TYPE_P (field_type)
7859 && (TYPE_NEEDS_CONSTRUCTING (field_type)
7860 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)
7861 || TYPE_POLYMORPHIC_P (field_type)))
7863 tree type_name = OBJC_TYPE_NAME (field_type);
7865 if (flag_objc_call_cxx_cdtors)
7867 /* Since the ObjC runtime will be calling the constructors and
7868 destructors for us, the only thing we can't handle is the lack
7869 of a default constructor. */
7870 if (TYPE_NEEDS_CONSTRUCTING (field_type)
7871 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type))
7873 warning (0, "type %qE has no default constructor to call",
7876 /* If we cannot call a constructor, we should also avoid
7877 calling the destructor, for symmetry. */
7878 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
7879 warning (0, "destructor for %qE shall not be run either",
7885 static bool warn_cxx_ivars = false;
7887 if (TYPE_POLYMORPHIC_P (field_type))
7889 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
7891 error ("type %qE has virtual member functions", type_name);
7892 error ("illegal aggregate type %qE specified "
7893 "for instance variable %qs",
7894 type_name, ivar_name);
7895 /* Return class as is without adding this ivar. */
7899 /* User-defined constructors and destructors are not known to Obj-C
7900 and hence will not be called. This may or may not be a problem. */
7901 if (TYPE_NEEDS_CONSTRUCTING (field_type))
7902 warning (0, "type %qE has a user-defined constructor", type_name);
7903 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
7904 warning (0, "type %qE has a user-defined destructor", type_name);
7906 if (!warn_cxx_ivars)
7908 warning (0, "C++ constructors and destructors will not "
7909 "be invoked for Objective-C fields");
7910 warn_cxx_ivars = true;
7916 /* Overload the public attribute, it is not used for FIELD_DECLs. */
7919 case OBJC_IVAR_VIS_PROTECTED:
7920 TREE_PUBLIC (field_decl) = 0;
7921 TREE_PRIVATE (field_decl) = 0;
7922 TREE_PROTECTED (field_decl) = 1;
7925 case OBJC_IVAR_VIS_PACKAGE:
7926 /* TODO: Implement the package variant. */
7927 case OBJC_IVAR_VIS_PUBLIC:
7928 TREE_PUBLIC (field_decl) = 1;
7929 TREE_PRIVATE (field_decl) = 0;
7930 TREE_PROTECTED (field_decl) = 0;
7933 case OBJC_IVAR_VIS_PRIVATE:
7934 TREE_PUBLIC (field_decl) = 0;
7935 TREE_PRIVATE (field_decl) = 1;
7936 TREE_PROTECTED (field_decl) = 0;
7941 CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl);
7948 is_ivar (tree decl_chain, tree ident)
7950 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
7951 if (DECL_NAME (decl_chain) == ident)
7956 /* True if the ivar is private and we are not in its implementation. */
7959 is_private (tree decl)
7961 return (TREE_PRIVATE (decl)
7962 && ! is_ivar (CLASS_IVARS (implementation_template),
7966 /* We have an instance variable reference;, check to see if it is public. */
7969 objc_is_public (tree expr, tree identifier)
7971 tree basetype, decl;
7974 if (processing_template_decl)
7978 if (TREE_TYPE (expr) == error_mark_node)
7981 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7983 if (basetype && TREE_CODE (basetype) == RECORD_TYPE)
7985 if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype))
7987 tree klass = lookup_interface (OBJC_TYPE_NAME (basetype));
7991 error ("cannot find interface declaration for %qE",
7992 OBJC_TYPE_NAME (basetype));
7996 if ((decl = is_ivar (get_class_ivars (klass, true), identifier)))
7998 if (TREE_PUBLIC (decl))
8001 /* Important difference between the Stepstone translator:
8002 all instance variables should be public within the context
8003 of the implementation. */
8004 if (objc_implementation_context
8005 && ((TREE_CODE (objc_implementation_context)
8006 == CLASS_IMPLEMENTATION_TYPE)
8007 || (TREE_CODE (objc_implementation_context)
8008 == CATEGORY_IMPLEMENTATION_TYPE)))
8010 tree curtype = TYPE_MAIN_VARIANT
8011 (CLASS_STATIC_TEMPLATE
8012 (implementation_template));
8014 if (basetype == curtype
8015 || DERIVED_FROM_P (basetype, curtype))
8017 int priv = is_private (decl);
8020 error ("instance variable %qE is declared private",
8027 /* The 2.95.2 compiler sometimes allowed C functions to access
8028 non-@public ivars. We will let this slide for now... */
8029 if (!objc_method_context)
8031 warning (0, "instance variable %qE is %s; "
8032 "this will be a hard error in the future",
8034 TREE_PRIVATE (decl) ? "@private" : "@protected");
8038 error ("instance variable %qE is declared %s",
8040 TREE_PRIVATE (decl) ? "private" : "protected");
8049 /* Make sure all entries in CHAIN are also in LIST. */
8052 check_methods (tree chain, tree list, int mtype)
8058 /* If the method is associated with a dynamic property, then it
8059 is Ok not to have the method implementation, as it will be
8060 generated dynamically at runtime. */
8061 tree property = METHOD_PROPERTY_CONTEXT (chain);
8062 if (property != NULL_TREE && PROPERTY_DYNAMIC (property))
8064 chain = TREE_CHAIN (chain); /* next method... */
8068 if (!lookup_method (list, chain))
8072 switch (TREE_CODE (objc_implementation_context))
8074 case CLASS_IMPLEMENTATION_TYPE:
8075 warning (0, "incomplete implementation of class %qE",
8076 CLASS_NAME (objc_implementation_context));
8078 case CATEGORY_IMPLEMENTATION_TYPE:
8079 warning (0, "incomplete implementation of category %qE",
8080 CLASS_SUPER_NAME (objc_implementation_context));
8088 warning (0, "method definition for %<%c%E%> not found",
8089 mtype, METHOD_SEL_NAME (chain));
8092 chain = DECL_CHAIN (chain);
8098 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
8101 conforms_to_protocol (tree klass, tree protocol)
8103 if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE)
8105 tree p = CLASS_PROTOCOL_LIST (klass);
8106 while (p && TREE_VALUE (p) != protocol)
8111 tree super = (CLASS_SUPER_NAME (klass)
8112 ? lookup_interface (CLASS_SUPER_NAME (klass))
8114 int tmp = super ? conforms_to_protocol (super, protocol) : 0;
8123 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
8124 CONTEXT. This is one of two mechanisms to check protocol integrity. */
8127 check_methods_accessible (tree chain, tree context, int mtype)
8131 tree base_context = context;
8135 /* If the method is associated with a dynamic property, then it
8136 is Ok not to have the method implementation, as it will be
8137 generated dynamically at runtime. */
8138 tree property = METHOD_PROPERTY_CONTEXT (chain);
8139 if (property != NULL_TREE && PROPERTY_DYNAMIC (property))
8141 chain = TREE_CHAIN (chain); /* next method... */
8145 context = base_context;
8149 list = CLASS_CLS_METHODS (context);
8151 list = CLASS_NST_METHODS (context);
8153 if (lookup_method (list, chain))
8156 switch (TREE_CODE (context))
8158 case CLASS_IMPLEMENTATION_TYPE:
8159 case CLASS_INTERFACE_TYPE:
8160 context = (CLASS_SUPER_NAME (context)
8161 ? lookup_interface (CLASS_SUPER_NAME (context))
8164 case CATEGORY_IMPLEMENTATION_TYPE:
8165 case CATEGORY_INTERFACE_TYPE:
8166 context = (CLASS_NAME (context)
8167 ? lookup_interface (CLASS_NAME (context))
8175 if (context == NULL_TREE)
8179 switch (TREE_CODE (objc_implementation_context))
8181 case CLASS_IMPLEMENTATION_TYPE:
8182 warning (0, "incomplete implementation of class %qE",
8183 CLASS_NAME (objc_implementation_context));
8185 case CATEGORY_IMPLEMENTATION_TYPE:
8186 warning (0, "incomplete implementation of category %qE",
8187 CLASS_SUPER_NAME (objc_implementation_context));
8194 warning (0, "method definition for %<%c%E%> not found",
8195 mtype, METHOD_SEL_NAME (chain));
8198 chain = TREE_CHAIN (chain); /* next method... */
8203 /* Check whether the current interface (accessible via
8204 'objc_implementation_context') actually implements protocol P, along
8205 with any protocols that P inherits. */
8208 check_protocol (tree p, const char *type, tree name)
8210 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
8214 /* Ensure that all protocols have bodies! */
8217 f1 = check_methods (PROTOCOL_CLS_METHODS (p),
8218 CLASS_CLS_METHODS (objc_implementation_context),
8220 f2 = check_methods (PROTOCOL_NST_METHODS (p),
8221 CLASS_NST_METHODS (objc_implementation_context),
8226 f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
8227 objc_implementation_context,
8229 f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
8230 objc_implementation_context,
8235 warning (0, "%s %qE does not fully implement the %qE protocol",
8236 type, name, PROTOCOL_NAME (p));
8239 /* Check protocols recursively. */
8240 if (PROTOCOL_LIST (p))
8242 tree subs = PROTOCOL_LIST (p);
8244 lookup_interface (CLASS_SUPER_NAME (implementation_template));
8248 tree sub = TREE_VALUE (subs);
8250 /* If the superclass does not conform to the protocols
8251 inherited by P, then we must! */
8252 if (!super_class || !conforms_to_protocol (super_class, sub))
8253 check_protocol (sub, type, name);
8254 subs = TREE_CHAIN (subs);
8259 /* Check whether the current interface (accessible via
8260 'objc_implementation_context') actually implements the protocols listed
8264 check_protocols (tree proto_list, const char *type, tree name)
8266 for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
8268 tree p = TREE_VALUE (proto_list);
8270 check_protocol (p, type, name);
8274 /* Make sure that the class CLASS_NAME is defined
8275 CODE says which kind of thing CLASS_NAME ought to be.
8276 It can be CLASS_INTERFACE_TYPE, CLASS_IMPLEMENTATION_TYPE,
8277 CATEGORY_INTERFACE_TYPE, or CATEGORY_IMPLEMENTATION_TYPE. */
8280 start_class (enum tree_code code, tree class_name, tree super_name,
8286 if (current_namespace != global_namespace) {
8287 error ("Objective-C declarations may only appear in global scope");
8289 #endif /* OBJCPLUS */
8291 if (objc_implementation_context)
8293 warning (0, "%<@end%> missing in implementation context");
8294 finish_class (objc_implementation_context);
8295 objc_ivar_chain = NULL_TREE;
8296 objc_implementation_context = NULL_TREE;
8299 klass = make_node (code);
8300 TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS);
8302 /* Check for existence of the super class, if one was specified. Note
8303 that we must have seen an @interface, not just a @class. If we
8304 are looking at a @compatibility_alias, traverse it first. */
8305 if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE)
8308 tree super = objc_is_class_name (super_name);
8310 if (!super || !lookup_interface (super))
8312 error ("cannot find interface declaration for %qE, superclass of %qE",
8313 super ? super : super_name,
8315 super_name = NULL_TREE;
8321 CLASS_NAME (klass) = class_name;
8322 CLASS_SUPER_NAME (klass) = super_name;
8323 CLASS_CLS_METHODS (klass) = NULL_TREE;
8325 if (! objc_is_class_name (class_name)
8326 && (decl = lookup_name (class_name)))
8328 error ("%qE redeclared as different kind of symbol",
8330 error ("previous declaration of %q+D",
8336 case CLASS_IMPLEMENTATION_TYPE:
8340 for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
8341 if (TREE_VALUE (chain) == class_name)
8343 error ("reimplementation of class %qE",
8345 return error_mark_node;
8347 implemented_classes = tree_cons (NULL_TREE, class_name,
8348 implemented_classes);
8351 /* Reset for multiple classes per file. */
8354 objc_implementation_context = klass;
8356 /* Lookup the interface for this implementation. */
8358 if (!(implementation_template = lookup_interface (class_name)))
8360 warning (0, "cannot find interface declaration for %qE",
8362 add_class (implementation_template = objc_implementation_context,
8366 /* If a super class has been specified in the implementation,
8367 insure it conforms to the one specified in the interface. */
8370 && (super_name != CLASS_SUPER_NAME (implementation_template)))
8372 tree previous_name = CLASS_SUPER_NAME (implementation_template);
8373 error ("conflicting super class name %qE",
8376 error ("previous declaration of %qE", previous_name);
8378 error ("previous declaration");
8381 else if (! super_name)
8383 CLASS_SUPER_NAME (objc_implementation_context)
8384 = CLASS_SUPER_NAME (implementation_template);
8388 case CLASS_INTERFACE_TYPE:
8389 if (lookup_interface (class_name))
8391 error ("duplicate interface declaration for class %qE", class_name);
8393 warning (0, "duplicate interface declaration for class %qE", class_name);
8396 add_class (klass, class_name);
8399 CLASS_PROTOCOL_LIST (klass)
8400 = lookup_and_install_protocols (protocol_list);
8403 case CATEGORY_INTERFACE_TYPE:
8405 tree class_category_is_assoc_with;
8407 /* For a category, class_name is really the name of the class that
8408 the following set of methods will be associated with. We must
8409 find the interface so that can derive the objects template. */
8410 if (!(class_category_is_assoc_with = lookup_interface (class_name)))
8412 error ("cannot find interface declaration for %qE",
8414 exit (FATAL_EXIT_CODE);
8417 add_category (class_category_is_assoc_with, klass);
8420 CLASS_PROTOCOL_LIST (klass)
8421 = lookup_and_install_protocols (protocol_list);
8425 case CATEGORY_IMPLEMENTATION_TYPE:
8426 /* Reset for multiple classes per file. */
8429 objc_implementation_context = klass;
8431 /* For a category, class_name is really the name of the class that
8432 the following set of methods will be associated with. We must
8433 find the interface so that can derive the objects template. */
8435 if (!(implementation_template = lookup_interface (class_name)))
8437 error ("cannot find interface declaration for %qE",
8439 exit (FATAL_EXIT_CODE);
8449 continue_class (tree klass)
8451 switch (TREE_CODE (klass))
8453 case CLASS_IMPLEMENTATION_TYPE:
8454 case CATEGORY_IMPLEMENTATION_TYPE:
8456 struct imp_entry *imp_entry;
8458 /* Check consistency of the instance variables. */
8460 if (CLASS_RAW_IVARS (klass))
8461 check_ivars (implementation_template, klass);
8463 /* code generation */
8465 push_lang_context (lang_name_c);
8467 build_private_template (implementation_template);
8468 uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template);
8469 objc_instance_type = build_pointer_type (uprivate_record);
8471 imp_entry = ggc_alloc_imp_entry ();
8473 imp_entry->next = imp_list;
8474 imp_entry->imp_context = klass;
8475 imp_entry->imp_template = implementation_template;
8477 synth_forward_declarations ();
8478 imp_entry->class_decl = UOBJC_CLASS_decl;
8479 imp_entry->meta_decl = UOBJC_METACLASS_decl;
8480 imp_entry->has_cxx_cdtors = 0;
8482 /* Append to front and increment count. */
8483 imp_list = imp_entry;
8484 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
8489 pop_lang_context ();
8490 #endif /* OBJCPLUS */
8492 return get_class_ivars (implementation_template, true);
8495 case CLASS_INTERFACE_TYPE:
8498 push_lang_context (lang_name_c);
8499 #endif /* OBJCPLUS */
8500 objc_collecting_ivars = 1;
8501 build_private_template (klass);
8502 objc_collecting_ivars = 0;
8504 pop_lang_context ();
8505 #endif /* OBJCPLUS */
8510 return error_mark_node;
8514 /* This routine builds name of the setter synthesized function. */
8516 objc_build_property_setter_name (tree ident)
8518 /* TODO: Use alloca to allocate buffer of appropriate size. */
8519 static char string[BUFSIZE];
8520 sprintf (string, "set%s:", IDENTIFIER_POINTER (ident));
8521 string[3] = TOUPPER (string[3]);
8525 /* This routine prepares the declarations of the property accessor
8526 helper functions (objc_getProperty(), etc) that are used when
8527 @synthesize is used. */
8529 build_objc_property_accessor_helpers (void)
8533 /* Declare the following function:
8535 objc_getProperty (id self, SEL _cmd,
8536 ptrdiff_t offset, BOOL is_atomic); */
8537 type = build_function_type_list (objc_object_type,
8543 objc_getProperty_decl = add_builtin_function ("objc_getProperty",
8544 type, 0, NOT_BUILT_IN,
8546 TREE_NOTHROW (objc_getProperty_decl) = 0;
8548 /* Declare the following function:
8550 objc_setProperty (id self, SEL _cmd,
8551 ptrdiff_t offset, id new_value,
8552 BOOL is_atomic, BOOL should_copy); */
8553 type = build_function_type_list (void_type_node,
8561 objc_setProperty_decl = add_builtin_function ("objc_setProperty",
8562 type, 0, NOT_BUILT_IN,
8564 TREE_NOTHROW (objc_setProperty_decl) = 0;
8566 /* This is the type of all of the following functions
8567 (objc_copyStruct(), objc_getPropertyStruct() and
8568 objc_setPropertyStruct()). */
8569 type = build_function_type_list (void_type_node,
8571 const_ptr_type_node,
8577 if (flag_next_runtime)
8579 /* Declare the following function:
8581 objc_copyStruct (void *destination, const void *source,
8582 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
8583 objc_copyStruct_decl = add_builtin_function ("objc_copyStruct",
8584 type, 0, NOT_BUILT_IN,
8586 TREE_NOTHROW (objc_copyStruct_decl) = 0;
8587 objc_getPropertyStruct_decl = NULL_TREE;
8588 objc_setPropertyStruct_decl = NULL_TREE;
8592 objc_copyStruct_decl = NULL_TREE;
8594 /* Declare the following function:
8596 objc_getPropertyStruct (void *destination, const void *source,
8597 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
8598 objc_getPropertyStruct_decl = add_builtin_function ("objc_getPropertyStruct",
8599 type, 0, NOT_BUILT_IN,
8601 TREE_NOTHROW (objc_getPropertyStruct_decl) = 0;
8602 /* Declare the following function:
8604 objc_setPropertyStruct (void *destination, const void *source,
8605 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
8606 objc_setPropertyStruct_decl = add_builtin_function ("objc_setPropertyStruct",
8607 type, 0, NOT_BUILT_IN,
8609 TREE_NOTHROW (objc_setPropertyStruct_decl) = 0;
8613 /* This looks up an ivar in a class (including superclasses). */
8615 lookup_ivar (tree interface, tree instance_variable_name)
8621 for (decl_chain = CLASS_IVARS (interface); decl_chain; decl_chain = DECL_CHAIN (decl_chain))
8622 if (DECL_NAME (decl_chain) == instance_variable_name)
8625 /* Not found. Search superclass if any. */
8626 if (CLASS_SUPER_NAME (interface))
8627 interface = lookup_interface (CLASS_SUPER_NAME (interface));
8633 /* This routine synthesizes a 'getter' method. This is only called
8634 for @synthesize properties. */
8636 objc_synthesize_getter (tree klass, tree class_method, tree property)
8638 location_t location = DECL_SOURCE_LOCATION (property);
8643 /* If user has implemented a getter with same name then do nothing. */
8644 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
8645 PROPERTY_GETTER_NAME (property)))
8648 /* Find declaration of the property getter in the interface. There
8649 must be one. TODO: Search superclasses as well. */
8650 decl = lookup_method (CLASS_NST_METHODS (class_method), PROPERTY_GETTER_NAME (property));
8652 /* If one not declared in the interface, this condition has already
8653 been reported as user error (because property was not declared in
8658 /* Adapt the 'decl'. Use the source location of the @synthesize
8659 statement for error messages. */
8660 decl = copy_node (decl);
8661 DECL_SOURCE_LOCATION (decl) = location;
8663 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE);
8664 body = c_begin_compound_stmt (true);
8666 /* Now we need to decide how we build the getter. There are three
8669 for 'copy' or 'retain' properties we need to use the
8670 objc_getProperty() accessor helper which knows about retain and
8671 copy. It supports both 'nonatomic' and 'atomic' access.
8673 for 'nonatomic, assign' properties we can access the instance
8674 variable directly. 'nonatomic' means we don't have to use locks,
8675 and 'assign' means we don't have to worry about retain or copy.
8676 If you combine the two, it means we can just access the instance
8679 for 'atomic, assign' properties we use objc_copyStruct() (for the
8680 next runtime) or objc_getPropertyStruct() (for the GNU runtime). */
8681 switch (PROPERTY_ASSIGN_SEMANTICS (property))
8683 case OBJC_PROPERTY_RETAIN:
8684 case OBJC_PROPERTY_COPY:
8686 /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */
8687 tree cmd, ivar, offset, is_atomic;
8688 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
8690 /* Find the ivar to compute the offset. */
8691 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
8692 if (!ivar || is_private (ivar))
8694 /* This should never happen. */
8696 "can not find instance variable associated with property");
8697 ret_val = error_mark_node;
8700 offset = byte_position (ivar);
8702 if (PROPERTY_NONATOMIC (property))
8703 is_atomic = boolean_false_node;
8705 is_atomic = boolean_true_node;
8707 ret_val = build_function_call
8709 /* Function prototype. */
8710 objc_getProperty_decl,
8712 tree_cons /* self */
8713 (NULL_TREE, self_decl,
8714 tree_cons /* _cmd */
8716 tree_cons /* offset */
8718 tree_cons /* is_atomic */
8719 (NULL_TREE, is_atomic, NULL_TREE)))));
8722 case OBJC_PROPERTY_ASSIGN:
8723 if (PROPERTY_NONATOMIC (property))
8725 /* We build "return self->PROPERTY_IVAR_NAME;" */
8726 ret_val = objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property));
8732 <property type> __objc_property_temp;
8733 objc_getPropertyStruct (&__objc_property_temp,
8734 &(self->PROPERTY_IVAR_NAME),
8735 sizeof (type of self->PROPERTY_IVAR_NAME),
8738 return __objc_property_temp;
8740 For the NeXT runtime, we need to use objc_copyStruct
8741 instead of objc_getPropertyStruct. */
8742 tree objc_property_temp_decl, function_decl, function_call;
8743 tree size_of, is_atomic;
8745 objc_property_temp_decl = objc_create_temporary_var (TREE_TYPE (property), "__objc_property_temp");
8746 DECL_SOURCE_LOCATION (objc_property_temp_decl) = location;
8747 objc_property_temp_decl = lang_hooks.decls.pushdecl (objc_property_temp_decl);
8749 /* sizeof (ivar type). Since the ivar and the property have
8750 the same type, there is no need to lookup the ivar. */
8751 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
8752 true /* is_sizeof */,
8753 false /* complain */);
8755 if (PROPERTY_NONATOMIC (property))
8756 is_atomic = boolean_false_node;
8758 is_atomic = boolean_true_node;
8760 if (flag_next_runtime)
8761 function_decl = objc_copyStruct_decl;
8763 function_decl = objc_getPropertyStruct_decl;
8765 function_call = build_function_call
8767 /* Function prototype. */
8770 tree_cons /* &__objc_property_temp_decl */
8771 /* Warning: note that using build_fold_addr_expr_loc()
8772 here causes invalid code to be generated. */
8773 (NULL_TREE, build_unary_op (location, ADDR_EXPR, objc_property_temp_decl, 0),
8774 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
8775 (NULL_TREE, build_fold_addr_expr_loc (location,
8777 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
8778 tree_cons /* sizeof (PROPERTY_IVAR) */
8779 (NULL_TREE, size_of,
8780 tree_cons /* is_atomic */
8781 (NULL_TREE, is_atomic,
8782 /* TODO: This is currently ignored by the GNU
8783 runtime, but what about the next one ? */
8784 tree_cons /* has_strong */
8785 (NULL_TREE, boolean_true_node, NULL_TREE))))));
8787 add_stmt (function_call);
8789 ret_val = objc_property_temp_decl;
8796 gcc_assert (ret_val);
8799 finish_return_stmt (ret_val);
8801 c_finish_return (location, ret_val, NULL_TREE);
8804 add_stmt (c_end_compound_stmt (location, body, true));
8805 fn = current_function_decl;
8809 objc_finish_method_definition (fn);
8812 /* This routine synthesizes a 'setter' method. */
8815 objc_synthesize_setter (tree klass ATTRIBUTE_UNUSED, tree class_method, tree property)
8817 location_t location = DECL_SOURCE_LOCATION (property);
8820 tree new_value, statement;
8822 /* If user has implemented a setter with same name then do nothing. */
8823 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
8824 PROPERTY_SETTER_NAME (property)))
8827 /* Find declaration of the property setter in the interface. There
8828 must be one. TODO: Search superclasses as well. */
8829 decl = lookup_method (CLASS_NST_METHODS (class_method), PROPERTY_SETTER_NAME (property));
8831 /* If one not declared in the interface, this condition has already
8832 been reported as user error (because property was not declared in
8837 /* Adapt the 'decl'. Use the source location of the @synthesize
8838 statement for error messages. */
8839 decl = copy_node (decl);
8840 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (property);
8842 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE);
8844 body = c_begin_compound_stmt (true);
8846 /* The 'new_value' is the only argument to the method, which is the
8847 3rd argument of the function, after self and _cmd. We use twice
8848 TREE_CHAIN to move forward two arguments. */
8849 new_value = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)));
8851 /* This would presumably happen if the user has specified a
8852 prototype for the setter that does not have an argument! */
8853 if (new_value == NULL_TREE)
8855 /* TODO: This should be caught much earlier than this. */
8856 error_at (DECL_SOURCE_LOCATION (decl), "invalid setter, it must have one argument");
8857 /* Try to recover somehow. */
8858 new_value = error_mark_node;
8861 /* Now we need to decide how we build the setter. There are three
8864 for 'copy' or 'retain' properties we need to use the
8865 objc_setProperty() accessor helper which knows about retain and
8866 copy. It supports both 'nonatomic' and 'atomic' access.
8868 for 'nonatomic, assign' properties we can access the instance
8869 variable directly. 'nonatomic' means we don't have to use locks,
8870 and 'assign' means we don't have to worry about retain or copy.
8871 If you combine the two, it means we can just access the instance
8874 for 'atomic, assign' properties we use objc_copyStruct() (for the
8875 next runtime) or objc_setPropertyStruct() (for the GNU runtime). */
8876 switch (PROPERTY_ASSIGN_SEMANTICS (property))
8878 case OBJC_PROPERTY_RETAIN:
8879 case OBJC_PROPERTY_COPY:
8881 /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */
8882 tree cmd, ivar, offset, is_atomic, should_copy;
8883 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
8885 /* Find the ivar to compute the offset. */
8886 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
8887 if (!ivar || is_private (ivar))
8890 "can not find instance variable associated with property");
8891 statement = error_mark_node;
8894 offset = byte_position (ivar);
8896 if (PROPERTY_NONATOMIC (property))
8897 is_atomic = boolean_false_node;
8899 is_atomic = boolean_true_node;
8901 if (PROPERTY_ASSIGN_SEMANTICS (property) == OBJC_PROPERTY_COPY)
8902 should_copy = boolean_true_node;
8904 should_copy = boolean_false_node;
8906 statement = build_function_call
8908 /* Function prototype. */
8909 objc_setProperty_decl,
8911 tree_cons /* self */
8912 (NULL_TREE, self_decl,
8913 tree_cons /* _cmd */
8915 tree_cons /* offset */
8917 tree_cons /* new_value */
8918 (NULL_TREE, new_value,
8919 tree_cons /* is_atomic */
8920 (NULL_TREE, is_atomic,
8921 tree_cons /* should_copy */
8922 (NULL_TREE, should_copy, NULL_TREE)))))));
8925 case OBJC_PROPERTY_ASSIGN:
8926 if (PROPERTY_NONATOMIC (property))
8928 /* We build "self->PROPERTY_IVAR_NAME = new_value;" */
8929 statement = build_modify_expr
8931 objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)),
8932 NULL_TREE, NOP_EXPR,
8933 location, new_value, NULL_TREE);
8939 objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME),
8941 sizeof (type of self->PROPERTY_IVAR_NAME),
8945 For the NeXT runtime, we need to use objc_copyStruct
8946 instead of objc_getPropertyStruct. */
8947 tree function_decl, size_of, is_atomic;
8949 /* sizeof (ivar type). Since the ivar and the property have
8950 the same type, there is no need to lookup the ivar. */
8951 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
8952 true /* is_sizeof */,
8953 false /* complain */);
8955 if (PROPERTY_NONATOMIC (property))
8956 is_atomic = boolean_false_node;
8958 is_atomic = boolean_true_node;
8960 if (flag_next_runtime)
8961 function_decl = objc_copyStruct_decl;
8963 function_decl = objc_setPropertyStruct_decl;
8965 statement = build_function_call
8967 /* Function prototype. */
8970 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
8971 (NULL_TREE, build_fold_addr_expr_loc (location,
8973 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
8974 tree_cons /* &new_value */
8975 (NULL_TREE, build_fold_addr_expr_loc (location, new_value),
8976 tree_cons /* sizeof (PROPERTY_IVAR) */
8977 (NULL_TREE, size_of,
8978 tree_cons /* is_atomic */
8979 (NULL_TREE, is_atomic,
8980 /* TODO: This is currently ignored by the GNU
8981 runtime, but what about the next one ? */
8982 tree_cons /* has_strong */
8983 (NULL_TREE, boolean_true_node, NULL_TREE))))));
8989 gcc_assert (statement);
8991 add_stmt (statement);
8992 add_stmt (c_end_compound_stmt (location, body, true));
8993 fn = current_function_decl;
8997 objc_finish_method_definition (fn);
9000 /* This function is a sub-routine of objc_add_synthesize_declaration.
9001 It is called for each property to synthesize once we have
9002 determined that the context is Ok. */
9004 objc_add_synthesize_declaration_for_property (location_t location, tree interface,
9005 tree property_name, tree ivar_name)
9007 /* Find the @property declaration. */
9010 /* Check that synthesize or dynamic has not already been used for
9011 the same property. */
9012 for (property = CLASS_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
9013 if (PROPERTY_NAME (property) == property_name)
9015 location_t original_location = DECL_SOURCE_LOCATION (property);
9017 if (PROPERTY_DYNAMIC (property))
9018 error_at (location, "property %qs already specified in %<@dynamic%>",
9019 IDENTIFIER_POINTER (property_name));
9021 error_at (location, "property %qs already specified in %<@synthesize%>",
9022 IDENTIFIER_POINTER (property_name));
9024 if (original_location != UNKNOWN_LOCATION)
9025 inform (original_location, "originally specified here");
9029 /* Check that the property is declared in the interface. */
9030 /* TODO: This only check the immediate class; we need to check the
9031 superclass (and categories ?) as well. */
9032 for (property = CLASS_PROPERTY_DECL (interface); property; property = TREE_CHAIN (property))
9033 if (PROPERTY_NAME (property) == property_name)
9038 error_at (location, "no declaration of property %qs found in the interface",
9039 IDENTIFIER_POINTER (property_name));
9044 /* We have to copy the property, because we want to chain it to
9045 the implementation context, and we want to store the source
9046 location of the @synthesize, not of the original
9048 property = copy_node (property);
9049 DECL_SOURCE_LOCATION (property) = location;
9052 /* Determine PROPERTY_IVAR_NAME. */
9053 if (ivar_name == NULL_TREE)
9054 ivar_name = property_name;
9056 /* Check that the instance variable exists. You can only use an
9057 instance variable from the same class, not one from the
9059 if (!is_ivar (CLASS_IVARS (interface), ivar_name))
9060 error_at (location, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar",
9061 IDENTIFIER_POINTER (property_name));
9063 /* TODO: Check that the types of the instance variable and of the
9066 /* TODO: Check that no other property is using the same instance
9069 /* Note that a @synthesize (and only a @synthesize) always sets
9070 PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a
9071 @synthesize by that. */
9072 PROPERTY_IVAR_NAME (property) = ivar_name;
9074 /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the
9075 original declaration; they are always set (with the exception of
9076 PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */
9078 /* Add the property to the list of properties for current implementation. */
9079 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
9080 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
9082 /* Note how we don't actually synthesize the getter/setter here; it
9083 would be very natural, but we may miss the fact that the user has
9084 implemented his own getter/setter later on in the @implementation
9085 (in which case we shouldn't generate getter/setter). We wait
9086 until we have parsed it all before generating the code. */
9089 /* This function is called by the parser after a @synthesize
9090 expression is parsed. 'location' is the location of the
9091 @synthesize expression, and 'property_and_ivar_list' is a chained
9092 list of the property and ivar names. */
9094 objc_add_synthesize_declaration (location_t location, tree property_and_ivar_list)
9096 tree interface, chain;
9098 if (property_and_ivar_list == error_mark_node)
9101 if (!objc_implementation_context)
9103 /* We can get here only in Objective-C; the Objective-C++ parser
9104 detects the problem while parsing, outputs the error
9105 "misplaced '@synthesize' Objective-C++ construct" and skips
9107 error_at (location, "%<@synthesize%> not in @implementation context");
9111 if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE)
9113 /* TODO: Maybe we should allow @synthesize in categories ? */
9114 error_at (location, "%<@synthesize%> can not be used in categories");
9118 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
9121 /* I can't see how this could happen, but it is good as a safety check. */
9123 "%<@synthesize%> requires the @interface of the class to be available");
9127 /* Now, iterate over the properties and do each of them. */
9128 for (chain = property_and_ivar_list; chain; chain = TREE_CHAIN (chain))
9130 objc_add_synthesize_declaration_for_property (location, interface, TREE_VALUE (chain),
9131 TREE_PURPOSE (chain));
9135 /* This function is a sub-routine of objc_add_dynamic_declaration. It
9136 is called for each property to mark as dynamic once we have
9137 determined that the context is Ok. */
9139 objc_add_dynamic_declaration_for_property (location_t location, tree interface,
9142 /* Find the @property declaration. */
9145 /* Check that synthesize or dynamic has not already been used for
9146 the same property. */
9147 for (property = CLASS_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
9148 if (PROPERTY_NAME (property) == property_name)
9150 location_t original_location = DECL_SOURCE_LOCATION (property);
9152 if (PROPERTY_DYNAMIC (property))
9153 error_at (location, "property %qs already specified in %<@dynamic%>",
9154 IDENTIFIER_POINTER (property_name));
9156 error_at (location, "property %qs already specified in %<@synthesize%>",
9157 IDENTIFIER_POINTER (property_name));
9159 if (original_location != UNKNOWN_LOCATION)
9160 inform (original_location, "originally specified here");
9164 /* Check that the property is declared in the interface. */
9165 /* TODO: This only check the immediate class; we need to check the
9166 superclass (and categories ?) as well. */
9167 for (property = CLASS_PROPERTY_DECL (interface); property; property = TREE_CHAIN (property))
9168 if (PROPERTY_NAME (property) == property_name)
9173 error_at (location, "no declaration of property %qs found in the interface",
9174 IDENTIFIER_POINTER (property_name));
9179 /* Mark the original PROPERTY_DECL as dynamic. The reason is
9180 that the setter and getter methods in the interface have a
9181 METHOD_PROPERTY_CONTEXT that points to the original
9182 PROPERTY_DECL; when we check that these methods have been
9183 implemented, we need to easily find that they are associated
9184 with a dynamic property. TODO: Clean this up; maybe the
9185 @property PROPERTY_DECL should contain a reference to the
9186 @dynamic PROPERTY_DECL ? */
9187 PROPERTY_DYNAMIC (property) = 1;
9189 /* We have to copy the property, because we want to chain it to
9190 the implementation context, and we want to store the source
9191 location of the @synthesize, not of the original
9193 property = copy_node (property);
9194 DECL_SOURCE_LOCATION (property) = location;
9197 /* Note that a @dynamic (and only a @dynamic) always sets
9198 PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that.
9199 (actually, as explained above, PROPERTY_DECL generated by
9200 @property and associated with a @dynamic property are also marked
9201 as PROPERTY_DYNAMIC). */
9202 PROPERTY_DYNAMIC (property) = 1;
9204 /* Add the property to the list of properties for current implementation. */
9205 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
9206 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
9209 /* This function is called by the parser after a @dynamic expression
9210 is parsed. 'location' is the location of the @dynamic expression,
9211 and 'property_list' is a chained list of all the property
9214 objc_add_dynamic_declaration (location_t location, tree property_list)
9216 tree interface, chain;
9218 if (property_list == error_mark_node)
9221 if (!objc_implementation_context)
9223 /* We can get here only in Objective-C; the Objective-C++ parser
9224 detects the problem while parsing, outputs the error
9225 "misplaced '@dynamic' Objective-C++ construct" and skips the
9227 error_at (location, "%<@dynamic%> not in @implementation context");
9231 if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE)
9233 /* TODO: Maybe we should allow @dynamic in categories ? */
9234 error_at (location, "%<@dynamic%> can not be used in categories");
9238 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
9241 /* I can't see how this could happen, but it is good as a safety check. */
9243 "%<@dynamic%> requires the @interface of the class to be available");
9247 /* Now, iterate over the properties and do each of them. */
9248 for (chain = property_list; chain; chain = TREE_CHAIN (chain))
9250 objc_add_dynamic_declaration_for_property (location, interface, TREE_VALUE (chain));
9254 /* Main routine to generate code/data for all the property information for
9255 current implementation (class or category). CLASS is the interface where
9256 ivars are declared. CLASS_METHODS is where methods are found which
9257 could be a class or a category depending on whether we are implementing
9258 property of a class or a category. */
9261 objc_gen_property_data (tree klass, tree class_methods)
9265 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
9267 /* @dynamic property - nothing to check or synthesize. */
9268 if (PROPERTY_DYNAMIC (x))
9271 /* @synthesize property - need to synthesize the accessors. */
9272 if (PROPERTY_IVAR_NAME (x))
9274 objc_synthesize_getter (klass, class_methods, x);
9276 if (PROPERTY_READONLY (x) == 0)
9277 objc_synthesize_setter (klass, class_methods, x);
9286 /* This is called once we see the "@end" in an interface/implementation. */
9289 finish_class (tree klass)
9291 switch (TREE_CODE (klass))
9293 case CLASS_IMPLEMENTATION_TYPE:
9295 /* All code generation is done in finish_objc. */
9297 /* Generate what needed for property; setters, getters, etc. */
9298 objc_gen_property_data (implementation_template, implementation_template);
9300 if (implementation_template != objc_implementation_context)
9302 /* Ensure that all method listed in the interface contain bodies. */
9303 check_methods (CLASS_CLS_METHODS (implementation_template),
9304 CLASS_CLS_METHODS (objc_implementation_context), '+');
9305 check_methods (CLASS_NST_METHODS (implementation_template),
9306 CLASS_NST_METHODS (objc_implementation_context), '-');
9308 if (CLASS_PROTOCOL_LIST (implementation_template))
9309 check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
9311 CLASS_NAME (objc_implementation_context));
9315 case CATEGORY_IMPLEMENTATION_TYPE:
9317 tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass));
9321 /* Generate what needed for property; setters, getters, etc. */
9322 objc_gen_property_data (implementation_template, category);
9324 /* Ensure all method listed in the interface contain bodies. */
9325 check_methods (CLASS_CLS_METHODS (category),
9326 CLASS_CLS_METHODS (objc_implementation_context), '+');
9327 check_methods (CLASS_NST_METHODS (category),
9328 CLASS_NST_METHODS (objc_implementation_context), '-');
9330 if (CLASS_PROTOCOL_LIST (category))
9331 check_protocols (CLASS_PROTOCOL_LIST (category),
9333 CLASS_SUPER_NAME (objc_implementation_context));
9337 case CLASS_INTERFACE_TYPE:
9338 case CATEGORY_INTERFACE_TYPE:
9339 case PROTOCOL_INTERFACE_TYPE:
9341 /* Process properties of the class. */
9343 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
9345 /* Store the getter name that we used into the property.
9346 It is used to generate the right getter calls;
9347 moreover, when a @synthesize is processed, it copies
9348 everything from the property, including the
9349 PROPERTY_GETTER_NAME. We want to be sure that
9350 @synthesize will get exactly the right
9351 PROPERTY_GETTER_NAME. */
9352 if (PROPERTY_GETTER_NAME (x) == NULL_TREE)
9353 PROPERTY_GETTER_NAME (x) = PROPERTY_NAME (x);
9355 /* Now we check that the appropriate getter is declared,
9356 and if not, we declare one ourselves. */
9358 tree getter_decl = lookup_method (CLASS_NST_METHODS (klass),
9359 PROPERTY_GETTER_NAME (x));
9363 /* TODO: Check that the declaration is consistent with the property. */
9368 /* Generate an instance method declaration for the
9369 getter; for example "- (id) name;". In general
9370 it will be of the form
9371 -(type)property_getter_name; */
9372 tree rettype = build_tree_list (NULL_TREE, TREE_TYPE (x));
9373 getter_decl = build_method_decl (INSTANCE_METHOD_DECL,
9374 rettype, PROPERTY_GETTER_NAME (x),
9376 objc_add_method (objc_interface_context, getter_decl, false, false);
9377 METHOD_PROPERTY_CONTEXT (getter_decl) = x;
9381 if (PROPERTY_READONLY (x) == 0)
9383 /* Store the setter name that we used into the
9384 property. It is used when generating setter calls;
9385 moreover, when a @synthesize is processed, it
9386 copies everything from the property, including the
9387 PROPERTY_SETTER_NAME. We want to be sure that
9388 @synthesize will get exactly the right
9389 PROPERTY_SETTER_NAME. */
9390 if (PROPERTY_SETTER_NAME (x) == NULL_TREE)
9391 PROPERTY_SETTER_NAME (x) = get_identifier (objc_build_property_setter_name
9392 (PROPERTY_NAME (x)));
9394 /* Now we check that the appropriate setter is declared,
9395 and if not, we declare on ourselves. */
9397 tree setter_decl = lookup_method (CLASS_NST_METHODS (klass),
9398 PROPERTY_SETTER_NAME (x));
9402 /* TODO: Check that the declaration is consistent with the property. */
9407 /* The setter name is something like 'setName:'.
9408 We need the substring 'setName' to build the
9409 method declaration due to how the declaration
9410 works. TODO: build_method_decl() will then
9411 generate back 'setName:' from 'setName'; it
9412 would be more efficient to hook into
9414 const char *full_setter_name = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x));
9415 size_t length = strlen (full_setter_name);
9416 char *setter_name = (char *) alloca (length);
9417 tree ret_type, selector, arg_type, arg_name;
9419 strcpy (setter_name, full_setter_name);
9420 setter_name[length - 1] = '\0';
9421 ret_type = build_tree_list (NULL_TREE, void_type_node);
9422 arg_type = build_tree_list (NULL_TREE, TREE_TYPE (x));
9423 arg_name = get_identifier ("_value");
9424 selector = objc_build_keyword_decl (get_identifier (setter_name),
9425 arg_type, arg_name, NULL);
9426 setter_decl = build_method_decl (INSTANCE_METHOD_DECL,
9428 build_tree_list (NULL_TREE, NULL_TREE),
9430 objc_add_method (objc_interface_context, setter_decl, false, false);
9431 METHOD_PROPERTY_CONTEXT (setter_decl) = x;
9435 /* Note how at this point (once an @interface or @protocol
9436 have been processed), PROPERTY_GETTER_NAME is always
9437 set for all PROPERTY_DECLs, and PROPERTY_SETTER_NAME is
9438 always set for all PROPERTY_DECLs where
9439 PROPERTY_READONLY == 0. Any time we deal with a getter
9440 or setter, we should get the PROPERTY_DECL and use
9441 PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know
9442 the correct names. */
9454 add_protocol (tree protocol)
9456 /* Put protocol on list in reverse order. */
9457 TREE_CHAIN (protocol) = protocol_chain;
9458 protocol_chain = protocol;
9459 return protocol_chain;
9463 lookup_protocol (tree ident)
9467 for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
9468 if (ident == PROTOCOL_NAME (chain))
9474 /* This function forward declares the protocols named by NAMES. If
9475 they are already declared or defined, the function has no effect. */
9478 objc_declare_protocols (tree names)
9483 if (current_namespace != global_namespace) {
9484 error ("Objective-C declarations may only appear in global scope");
9486 #endif /* OBJCPLUS */
9488 for (list = names; list; list = TREE_CHAIN (list))
9490 tree name = TREE_VALUE (list);
9492 if (lookup_protocol (name) == NULL_TREE)
9494 tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
9496 TYPE_LANG_SLOT_1 (protocol)
9497 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
9498 PROTOCOL_NAME (protocol) = name;
9499 PROTOCOL_LIST (protocol) = NULL_TREE;
9500 add_protocol (protocol);
9501 PROTOCOL_DEFINED (protocol) = 0;
9502 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
9508 start_protocol (enum tree_code code, tree name, tree list)
9513 if (current_namespace != global_namespace) {
9514 error ("Objective-C declarations may only appear in global scope");
9516 #endif /* OBJCPLUS */
9518 protocol = lookup_protocol (name);
9522 protocol = make_node (code);
9523 TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
9525 PROTOCOL_NAME (protocol) = name;
9526 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list);
9527 add_protocol (protocol);
9528 PROTOCOL_DEFINED (protocol) = 1;
9529 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
9531 check_protocol_recursively (protocol, list);
9533 else if (! PROTOCOL_DEFINED (protocol))
9535 PROTOCOL_DEFINED (protocol) = 1;
9536 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list);
9538 check_protocol_recursively (protocol, list);
9542 warning (0, "duplicate declaration for protocol %qE",
9549 /* "Encode" a data type into a string, which grows in util_obstack.
9551 The format is described in gcc/doc/objc.texi, section 'Type
9554 Most of the encode_xxx functions have a 'type' argument, which is
9555 the type to encode, and an integer 'curtype' argument, which is the
9556 index in the encoding string of the beginning of the encoding of
9557 the current type, and allows you to find what characters have
9558 already been written for the current type (they are the ones in the
9559 current encoding string starting from 'curtype').
9561 For example, if we are encoding a method which returns 'int' and
9562 takes a 'char **' argument, then when we get to the point of
9563 encoding the 'char **' argument, the encoded string already
9564 contains 'i12@0:4' (assuming a pointer size of 4 bytes). So,
9565 'curtype' will be set to 7 when starting to encode 'char **'.
9566 During the whole of the encoding of 'char **', 'curtype' will be
9567 fixed at 7, so the routine encoding the second pointer can find out
9568 that it's actually encoding a pointer to a pointer by looking
9569 backwards at what has already been encoded for the current type,
9570 and seeing there is a "^" (meaning a pointer) in there.
9574 /* Encode type qualifiers encodes one of the "PQ" Objective-C
9575 keywords, ie 'in', 'out', 'inout', 'bycopy', 'byref', 'oneway'.
9576 'const', instead, is encoded directly as part of the type.
9580 encode_type_qualifiers (tree declspecs)
9584 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
9586 /* FIXME: Shouldn't we use token->keyword here ? */
9587 if (ridpointers[(int) RID_IN] == TREE_VALUE (spec))
9588 obstack_1grow (&util_obstack, 'n');
9589 else if (ridpointers[(int) RID_INOUT] == TREE_VALUE (spec))
9590 obstack_1grow (&util_obstack, 'N');
9591 else if (ridpointers[(int) RID_OUT] == TREE_VALUE (spec))
9592 obstack_1grow (&util_obstack, 'o');
9593 else if (ridpointers[(int) RID_BYCOPY] == TREE_VALUE (spec))
9594 obstack_1grow (&util_obstack, 'O');
9595 else if (ridpointers[(int) RID_BYREF] == TREE_VALUE (spec))
9596 obstack_1grow (&util_obstack, 'R');
9597 else if (ridpointers[(int) RID_ONEWAY] == TREE_VALUE (spec))
9598 obstack_1grow (&util_obstack, 'V');
9604 /* Determine if a pointee is marked read-only. Only used by the NeXT
9605 runtime to be compatible with gcc-3.3. */
9608 pointee_is_readonly (tree pointee)
9610 while (POINTER_TYPE_P (pointee))
9611 pointee = TREE_TYPE (pointee);
9613 return TYPE_READONLY (pointee);
9616 /* Encode a pointer type. */
9619 encode_pointer (tree type, int curtype, int format)
9621 tree pointer_to = TREE_TYPE (type);
9623 if (flag_next_runtime)
9625 /* This code is used to be compatible with gcc-3.3. */
9626 /* For historical/compatibility reasons, the read-only qualifier
9627 of the pointee gets emitted _before_ the '^'. The read-only
9628 qualifier of the pointer itself gets ignored, _unless_ we are
9629 looking at a typedef! Also, do not emit the 'r' for anything
9630 but the outermost type! */
9631 if (!generating_instance_variables
9632 && (obstack_object_size (&util_obstack) - curtype <= 1)
9633 && (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9634 ? TYPE_READONLY (type)
9635 : pointee_is_readonly (pointer_to)))
9636 obstack_1grow (&util_obstack, 'r');
9639 if (TREE_CODE (pointer_to) == RECORD_TYPE)
9641 if (OBJC_TYPE_NAME (pointer_to)
9642 && TREE_CODE (OBJC_TYPE_NAME (pointer_to)) == IDENTIFIER_NODE)
9644 const char *name = IDENTIFIER_POINTER (OBJC_TYPE_NAME (pointer_to));
9646 if (strcmp (name, TAG_OBJECT) == 0) /* '@' */
9648 obstack_1grow (&util_obstack, '@');
9651 else if (TYPE_HAS_OBJC_INFO (pointer_to)
9652 && TYPE_OBJC_INTERFACE (pointer_to))
9654 if (generating_instance_variables)
9656 obstack_1grow (&util_obstack, '@');
9657 obstack_1grow (&util_obstack, '"');
9658 obstack_grow (&util_obstack, name, strlen (name));
9659 obstack_1grow (&util_obstack, '"');
9664 obstack_1grow (&util_obstack, '@');
9668 else if (strcmp (name, TAG_CLASS) == 0) /* '#' */
9670 obstack_1grow (&util_obstack, '#');
9673 else if (strcmp (name, TAG_SELECTOR) == 0) /* ':' */
9675 obstack_1grow (&util_obstack, ':');
9680 else if (TREE_CODE (pointer_to) == INTEGER_TYPE
9681 && TYPE_MODE (pointer_to) == QImode)
9683 tree pname = TREE_CODE (OBJC_TYPE_NAME (pointer_to)) == IDENTIFIER_NODE
9684 ? OBJC_TYPE_NAME (pointer_to)
9685 : DECL_NAME (OBJC_TYPE_NAME (pointer_to));
9687 /* (BOOL *) are an exception and are encoded as ^c, while all
9688 other pointers to char are encoded as *. */
9689 if (strcmp (IDENTIFIER_POINTER (pname), "BOOL"))
9691 if (!flag_next_runtime)
9693 /* The NeXT runtime adds the 'r' before getting here. */
9695 /* It appears that "r*" means "const char *" rather than
9696 "char *const". "char *const" is encoded as "*",
9697 which is identical to "char *", so the "const" is
9698 unfortunately lost. */
9699 if (TYPE_READONLY (pointer_to))
9700 obstack_1grow (&util_obstack, 'r');
9703 obstack_1grow (&util_obstack, '*');
9708 /* We have a normal pointer type that does not get special treatment. */
9709 obstack_1grow (&util_obstack, '^');
9710 encode_type (pointer_to, curtype, format);
9714 encode_array (tree type, int curtype, int format)
9716 tree an_int_cst = TYPE_SIZE (type);
9717 tree array_of = TREE_TYPE (type);
9720 if (an_int_cst == NULL)
9722 /* We are trying to encode an incomplete array. An incomplete
9723 array is forbidden as part of an instance variable. */
9724 if (generating_instance_variables)
9726 /* TODO: Detect this error earlier. */
9727 error ("instance variable has unknown size");
9731 /* So the only case in which an incomplete array could occur is
9732 if we are encoding the arguments or return value of a method.
9733 In that case, an incomplete array argument or return value
9734 (eg, -(void)display: (char[])string) is treated like a
9735 pointer because that is how the compiler does the function
9736 call. A special, more complicated case, is when the
9737 incomplete array is the last member of a struct (eg, if we
9738 are encoding "struct { unsigned long int a;double b[];}"),
9739 which is again part of a method argument/return value. In
9740 that case, we really need to communicate to the runtime that
9741 there is an incomplete array (not a pointer!) there. So, we
9742 detect that special case and encode it as a zero-length
9745 Try to detect that we are part of a struct. We do this by
9746 searching for '=' in the type encoding for the current type.
9747 NB: This hack assumes that you can't use '=' as part of a C
9751 char *enc = obstack_base (&util_obstack) + curtype;
9752 if (memchr (enc, '=',
9753 obstack_object_size (&util_obstack) - curtype) == NULL)
9755 /* We are not inside a struct. Encode the array as a
9757 encode_pointer (type, curtype, format);
9762 /* Else, we are in a struct, and we encode it as a zero-length
9764 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT)0);
9766 else if (TREE_INT_CST_LOW (TYPE_SIZE (array_of)) == 0)
9767 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT)0);
9769 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC,
9770 TREE_INT_CST_LOW (an_int_cst)
9771 / TREE_INT_CST_LOW (TYPE_SIZE (array_of)));
9773 obstack_grow (&util_obstack, buffer, strlen (buffer));
9774 encode_type (array_of, curtype, format);
9775 obstack_1grow (&util_obstack, ']');
9779 /* Encode a vector. The vector type is a GCC extension to C. */
9781 encode_vector (tree type, int curtype, int format)
9783 tree vector_of = TREE_TYPE (type);
9786 /* Vectors are like simple fixed-size arrays. */
9788 /* Output ![xx,yy,<code>] where xx is the vector_size, yy is the
9789 alignment of the vector, and <code> is the base type. Eg, int
9790 __attribute__ ((vector_size (16))) gets encoded as ![16,32,i]
9791 assuming that the alignment is 32 bytes. We include size and
9792 alignment in bytes so that the runtime does not have to have any
9793 knowledge of the actual types.
9795 sprintf (buffer, "![" HOST_WIDE_INT_PRINT_DEC ",%d",
9796 /* We want to compute the equivalent of sizeof (<vector>).
9797 Code inspired by c_sizeof_or_alignof_type. */
9798 ((TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type))
9799 / (TYPE_PRECISION (char_type_node) / BITS_PER_UNIT))),
9800 /* We want to compute the equivalent of __alignof__
9801 (<vector>). Code inspired by
9802 c_sizeof_or_alignof_type. */
9803 TYPE_ALIGN_UNIT (type));
9804 obstack_grow (&util_obstack, buffer, strlen (buffer));
9805 encode_type (vector_of, curtype, format);
9806 obstack_1grow (&util_obstack, ']');
9811 encode_aggregate_fields (tree type, bool pointed_to, int curtype, int format)
9813 tree field = TYPE_FIELDS (type);
9815 for (; field; field = DECL_CHAIN (field))
9818 /* C++ static members, and things that are not field at all,
9819 should not appear in the encoding. */
9820 if (TREE_CODE (field) != FIELD_DECL || TREE_STATIC (field))
9824 /* Recursively encode fields of embedded base classes. */
9825 if (DECL_ARTIFICIAL (field) && !DECL_NAME (field)
9826 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
9828 encode_aggregate_fields (TREE_TYPE (field),
9829 pointed_to, curtype, format);
9833 if (generating_instance_variables && !pointed_to)
9835 tree fname = DECL_NAME (field);
9837 obstack_1grow (&util_obstack, '"');
9839 if (fname && TREE_CODE (fname) == IDENTIFIER_NODE)
9840 obstack_grow (&util_obstack,
9841 IDENTIFIER_POINTER (fname),
9842 strlen (IDENTIFIER_POINTER (fname)));
9844 obstack_1grow (&util_obstack, '"');
9847 encode_field_decl (field, curtype, format);
9852 encode_aggregate_within (tree type, int curtype, int format, int left,
9856 /* NB: aggregates that are pointed to have slightly different encoding
9857 rules in that you never encode the names of instance variables. */
9858 int ob_size = obstack_object_size (&util_obstack);
9859 bool inline_contents = false;
9860 bool pointed_to = false;
9862 if (flag_next_runtime)
9864 if (ob_size > 0 && *(obstack_next_free (&util_obstack) - 1) == '^')
9867 if ((format == OBJC_ENCODE_INLINE_DEFS || generating_instance_variables)
9868 && (!pointed_to || ob_size - curtype == 1
9869 || (ob_size - curtype == 2
9870 && *(obstack_next_free (&util_obstack) - 2) == 'r')))
9871 inline_contents = true;
9875 /* c0 and c1 are the last two characters in the encoding of the
9876 current type; if the last two characters were '^' or '^r',
9877 then we are encoding an aggregate that is "pointed to". The
9878 comment above applies: in that case we should avoid encoding
9879 the names of instance variables.
9881 char c1 = ob_size > 1 ? *(obstack_next_free (&util_obstack) - 2) : 0;
9882 char c0 = ob_size > 0 ? *(obstack_next_free (&util_obstack) - 1) : 0;
9884 if (c0 == '^' || (c1 == '^' && c0 == 'r'))
9887 if (format == OBJC_ENCODE_INLINE_DEFS || generating_instance_variables)
9890 inline_contents = true;
9893 /* Note that the check (ob_size - curtype < 2) prevents
9894 infinite recursion when encoding a structure which is
9895 a linked list (eg, struct node { struct node *next;
9896 }). Each time we follow a pointer, we add one
9897 character to ob_size, and curtype is fixed, so after
9898 at most two pointers we stop inlining contents and
9901 The other case where we don't inline is "^r", which
9902 is a pointer to a constant struct.
9904 if ((ob_size - curtype <= 2) && !(c0 == 'r'))
9905 inline_contents = true;
9910 /* Traverse struct aliases; it is important to get the
9911 original struct and its tag name (if any). */
9912 type = TYPE_MAIN_VARIANT (type);
9913 name = OBJC_TYPE_NAME (type);
9914 /* Open parenth/bracket. */
9915 obstack_1grow (&util_obstack, left);
9917 /* Encode the struct/union tag name, or '?' if a tag was
9918 not provided. Typedef aliases do not qualify. */
9920 /* For compatibility with the NeXT runtime, ObjC++ encodes template
9921 args as a composite struct tag name. */
9922 if (name && TREE_CODE (name) == IDENTIFIER_NODE
9923 /* Did this struct have a tag? */
9924 && !TYPE_WAS_ANONYMOUS (type))
9925 obstack_grow (&util_obstack,
9926 decl_as_string (type, TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME),
9927 strlen (decl_as_string (type, TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME)));
9929 if (name && TREE_CODE (name) == IDENTIFIER_NODE)
9930 obstack_grow (&util_obstack,
9931 IDENTIFIER_POINTER (name),
9932 strlen (IDENTIFIER_POINTER (name)));
9935 obstack_1grow (&util_obstack, '?');
9937 /* Encode the types (and possibly names) of the inner fields,
9939 if (inline_contents)
9941 obstack_1grow (&util_obstack, '=');
9942 encode_aggregate_fields (type, pointed_to, curtype, format);
9944 /* Close parenth/bracket. */
9945 obstack_1grow (&util_obstack, right);
9948 /* Encode a bitfield NeXT-style (i.e., without a bit offset or the underlying
9952 encode_next_bitfield (int width)
9955 sprintf (buffer, "b%d", width);
9956 obstack_grow (&util_obstack, buffer, strlen (buffer));
9960 /* Encodes 'type', ignoring type qualifiers (which you should encode
9961 beforehand if needed) with the exception of 'const', which is
9962 encoded by encode_type. See above for the explanation of
9963 'curtype'. 'format' can be OBJC_ENCODE_INLINE_DEFS or
9964 OBJC_ENCODE_DONT_INLINE_DEFS.
9967 encode_type (tree type, int curtype, int format)
9969 enum tree_code code = TREE_CODE (type);
9971 /* Ignore type qualifiers other than 'const' when encoding a
9974 if (type == error_mark_node)
9977 if (!flag_next_runtime)
9979 if (TYPE_READONLY (type))
9980 obstack_1grow (&util_obstack, 'r');
9986 if (flag_next_runtime)
9988 /* Kludge for backwards-compatibility with gcc-3.3: enums
9989 are always encoded as 'i' no matter what type they
9990 actually are (!). */
9991 obstack_1grow (&util_obstack, 'i');
9994 /* Else, they are encoded exactly like the integer type that is
9995 used by the compiler to store them. */
9999 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
10001 case 8: c = TYPE_UNSIGNED (type) ? 'C' : 'c'; break;
10002 case 16: c = TYPE_UNSIGNED (type) ? 'S' : 's'; break;
10005 tree int_type = type;
10006 if (flag_next_runtime)
10008 /* Another legacy kludge for compatiblity with
10009 gcc-3.3: 32-bit longs are encoded as 'l' or 'L',
10010 but not always. For typedefs, we need to use 'i'
10011 or 'I' instead if encoding a struct field, or a
10013 int_type = ((!generating_instance_variables
10014 && (obstack_object_size (&util_obstack)
10015 == (unsigned) curtype))
10016 ? TYPE_MAIN_VARIANT (type)
10019 if (int_type == long_unsigned_type_node
10020 || int_type == long_integer_type_node)
10021 c = TYPE_UNSIGNED (type) ? 'L' : 'l';
10023 c = TYPE_UNSIGNED (type) ? 'I' : 'i';
10026 case 64: c = TYPE_UNSIGNED (type) ? 'Q' : 'q'; break;
10027 case 128: c = TYPE_UNSIGNED (type) ? 'T' : 't'; break;
10028 default: gcc_unreachable ();
10030 obstack_1grow (&util_obstack, c);
10036 /* Floating point types. */
10037 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
10039 case 32: c = 'f'; break;
10040 case 64: c = 'd'; break;
10042 case 128: c = 'D'; break;
10043 default: gcc_unreachable ();
10045 obstack_1grow (&util_obstack, c);
10049 obstack_1grow (&util_obstack, 'v');
10053 obstack_1grow (&util_obstack, 'B');
10057 encode_array (type, curtype, format);
10062 case REFERENCE_TYPE:
10064 encode_pointer (type, curtype, format);
10068 encode_aggregate_within (type, curtype, format, '{', '}');
10072 encode_aggregate_within (type, curtype, format, '(', ')');
10075 case FUNCTION_TYPE: /* '?' means an unknown type. */
10076 obstack_1grow (&util_obstack, '?');
10080 /* A complex is encoded as 'j' followed by the inner type (eg,
10081 "_Complex int" is encoded as 'ji'). */
10082 obstack_1grow (&util_obstack, 'j');
10083 encode_type (TREE_TYPE (type), curtype, format);
10087 encode_vector (type, curtype, format);
10091 warning (0, "unknown type %s found during Objective-C encoding",
10092 gen_type_name (type));
10093 obstack_1grow (&util_obstack, '?');
10097 if (flag_next_runtime)
10099 /* Super-kludge. Some ObjC qualifier and type combinations need
10100 to be rearranged for compatibility with gcc-3.3. */
10101 if (code == POINTER_TYPE && obstack_object_size (&util_obstack) >= 3)
10103 char *enc = obstack_base (&util_obstack) + curtype;
10105 /* Rewrite "in const" from "nr" to "rn". */
10106 if (curtype >= 1 && !strncmp (enc - 1, "nr", 2))
10107 strncpy (enc - 1, "rn", 2);
10113 encode_gnu_bitfield (int position, tree type, int size)
10115 enum tree_code code = TREE_CODE (type);
10117 char charType = '?';
10119 /* This code is only executed for the GNU runtime, so we can ignore
10120 the NeXT runtime kludge of always encoding enums as 'i' no matter
10121 what integers they actually are. */
10122 if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
10124 if (integer_zerop (TYPE_MIN_VALUE (type)))
10125 /* Unsigned integer types. */
10127 switch (TYPE_MODE (type))
10130 charType = 'C'; break;
10132 charType = 'S'; break;
10135 if (type == long_unsigned_type_node)
10142 charType = 'Q'; break;
10144 gcc_unreachable ();
10148 /* Signed integer types. */
10150 switch (TYPE_MODE (type))
10153 charType = 'c'; break;
10155 charType = 's'; break;
10158 if (type == long_integer_type_node)
10165 charType = 'q'; break;
10167 gcc_unreachable ();
10173 /* Do not do any encoding, produce an error and keep going. */
10174 error ("trying to encode non-integer type as a bitfield");
10178 sprintf (buffer, "b%d%c%d", position, charType, size);
10179 obstack_grow (&util_obstack, buffer, strlen (buffer));
10183 encode_field_decl (tree field_decl, int curtype, int format)
10186 /* C++ static members, and things that are not fields at all,
10187 should not appear in the encoding. */
10188 if (TREE_CODE (field_decl) != FIELD_DECL || TREE_STATIC (field_decl))
10192 /* Generate the bitfield typing information, if needed. Note the difference
10193 between GNU and NeXT runtimes. */
10194 if (DECL_BIT_FIELD_TYPE (field_decl))
10196 int size = tree_low_cst (DECL_SIZE (field_decl), 1);
10198 if (flag_next_runtime)
10199 encode_next_bitfield (size);
10201 encode_gnu_bitfield (int_bit_position (field_decl),
10202 DECL_BIT_FIELD_TYPE (field_decl), size);
10205 encode_type (TREE_TYPE (field_decl), curtype, format);
10208 /* Decay array and function parameters into pointers. */
10211 objc_decay_parm_type (tree type)
10213 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE)
10214 type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE
10221 static GTY(()) tree objc_parmlist = NULL_TREE;
10223 /* Append PARM to a list of formal parameters of a method, making a necessary
10224 array-to-pointer adjustment along the way. */
10227 objc_push_parm (tree parm)
10231 if (TREE_TYPE (parm) == error_mark_node)
10233 objc_parmlist = chainon (objc_parmlist, parm);
10237 /* Decay arrays and functions into pointers. */
10238 type = objc_decay_parm_type (TREE_TYPE (parm));
10240 /* If the parameter type has been decayed, a new PARM_DECL needs to be
10242 if (type != TREE_TYPE (parm))
10243 parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type);
10245 DECL_ARG_TYPE (parm)
10246 = lang_hooks.types.type_promotes_to (TREE_TYPE (parm));
10248 /* Record constancy and volatility. */
10249 c_apply_type_quals_to_decl
10250 ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0)
10251 | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0)
10252 | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm);
10254 objc_parmlist = chainon (objc_parmlist, parm);
10257 /* Retrieve the formal parameter list constructed via preceding calls to
10258 objc_push_parm(). */
10262 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED)
10264 static struct c_arg_info *
10265 objc_get_parm_info (int have_ellipsis)
10269 tree parm_info = objc_parmlist;
10270 objc_parmlist = NULL_TREE;
10274 tree parm_info = objc_parmlist;
10275 struct c_arg_info *arg_info;
10276 /* The C front-end requires an elaborate song and dance at
10279 declare_parm_level ();
10282 tree next = DECL_CHAIN (parm_info);
10284 DECL_CHAIN (parm_info) = NULL_TREE;
10285 parm_info = pushdecl (parm_info);
10286 finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
10289 arg_info = get_parm_info (have_ellipsis);
10291 objc_parmlist = NULL_TREE;
10296 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
10297 method definitions. In the case of instance methods, we can be more
10298 specific as to the type of 'self'. */
10301 synth_self_and_ucmd_args (void)
10305 if (objc_method_context
10306 && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL)
10307 self_type = objc_instance_type;
10309 /* Really a `struct objc_class *'. However, we allow people to
10310 assign to self, which changes its type midstream. */
10311 self_type = objc_object_type;
10314 objc_push_parm (build_decl (input_location,
10315 PARM_DECL, self_id, self_type));
10318 objc_push_parm (build_decl (input_location,
10319 PARM_DECL, ucmd_id, objc_selector_type));
10322 /* Transform an Objective-C method definition into a static C function
10323 definition, synthesizing the first two arguments, "self" and "_cmd",
10327 start_method_def (tree method)
10333 struct c_arg_info *parm_info;
10335 int have_ellipsis = 0;
10337 /* If we are defining a "dealloc" method in a non-root class, we
10338 will need to check if a [super dealloc] is missing, and warn if
10340 if(CLASS_SUPER_NAME (objc_implementation_context)
10341 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method))))
10342 should_call_super_dealloc = 1;
10344 should_call_super_dealloc = 0;
10346 /* Required to implement _msgSuper. */
10347 objc_method_context = method;
10348 UOBJC_SUPER_decl = NULL_TREE;
10350 /* Generate prototype declarations for arguments..."new-style". */
10351 synth_self_and_ucmd_args ();
10353 /* Generate argument declarations if a keyword_decl. */
10354 parmlist = METHOD_SEL_ARGS (method);
10357 /* parmlist is a KEYWORD_DECL. */
10358 tree type = TREE_VALUE (TREE_TYPE (parmlist));
10361 parm = build_decl (input_location,
10362 PARM_DECL, KEYWORD_ARG_NAME (parmlist), type);
10363 decl_attributes (&parm, DECL_ATTRIBUTES (parmlist), 0);
10364 objc_push_parm (parm);
10365 parmlist = DECL_CHAIN (parmlist);
10368 if (METHOD_ADD_ARGS (method))
10372 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method));
10373 akey; akey = TREE_CHAIN (akey))
10375 objc_push_parm (TREE_VALUE (akey));
10378 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
10382 parm_info = objc_get_parm_info (have_ellipsis);
10384 really_start_method (objc_method_context, parm_info);
10387 /* Return 1 if TYPE1 is equivalent to TYPE2
10388 for purposes of method overloading. */
10391 objc_types_are_equivalent (tree type1, tree type2)
10393 if (type1 == type2)
10396 /* Strip away indirections. */
10397 while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE)
10398 && (TREE_CODE (type1) == TREE_CODE (type2)))
10399 type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2);
10400 if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
10403 type1 = (TYPE_HAS_OBJC_INFO (type1)
10404 ? TYPE_OBJC_PROTOCOL_LIST (type1)
10406 type2 = (TYPE_HAS_OBJC_INFO (type2)
10407 ? TYPE_OBJC_PROTOCOL_LIST (type2)
10410 if (list_length (type1) == list_length (type2))
10412 for (; type2; type2 = TREE_CHAIN (type2))
10413 if (!lookup_protocol_in_reflist (type1, TREE_VALUE (type2)))
10420 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
10423 objc_types_share_size_and_alignment (tree type1, tree type2)
10425 return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
10426 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2));
10429 /* Return 1 if PROTO1 is equivalent to PROTO2
10430 for purposes of method overloading. Ordinarily, the type signatures
10431 should match up exactly, unless STRICT is zero, in which case we
10432 shall allow differences in which the size and alignment of a type
10436 comp_proto_with_proto (tree proto1, tree proto2, int strict)
10438 /* The following test is needed in case there are hashing
10440 if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2))
10443 return match_proto_with_proto (proto1, proto2, strict);
10447 match_proto_with_proto (tree proto1, tree proto2, int strict)
10451 /* Compare return types. */
10452 type1 = TREE_VALUE (TREE_TYPE (proto1));
10453 type2 = TREE_VALUE (TREE_TYPE (proto2));
10455 if (!objc_types_are_equivalent (type1, type2)
10456 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
10459 /* Compare argument types. */
10460 for (type1 = get_arg_type_list (proto1, METHOD_REF, 0),
10461 type2 = get_arg_type_list (proto2, METHOD_REF, 0);
10463 type1 = TREE_CHAIN (type1), type2 = TREE_CHAIN (type2))
10465 if (!objc_types_are_equivalent (TREE_VALUE (type1), TREE_VALUE (type2))
10467 || !objc_types_share_size_and_alignment (TREE_VALUE (type1),
10468 TREE_VALUE (type2))))
10472 return (!type1 && !type2);
10475 /* Fold an OBJ_TYPE_REF expression for ObjC method dispatches, where
10476 this occurs. ObjC method dispatches are _not_ like C++ virtual
10477 member function dispatches, and we account for the difference here. */
10480 objc_fold_obj_type_ref (tree ref, tree known_type)
10482 objc_fold_obj_type_ref (tree ref ATTRIBUTE_UNUSED,
10483 tree known_type ATTRIBUTE_UNUSED)
10487 tree v = BINFO_VIRTUALS (TYPE_BINFO (known_type));
10489 /* If the receiver does not have virtual member functions, there
10490 is nothing we can (or need to) do here. */
10494 /* Let C++ handle C++ virtual functions. */
10495 return cp_fold_obj_type_ref (ref, known_type);
10497 /* For plain ObjC, we currently do not need to do anything. */
10503 objc_start_function (tree name, tree type, tree attrs,
10507 struct c_arg_info *params
10511 tree fndecl = build_decl (input_location,
10512 FUNCTION_DECL, name, type);
10515 DECL_ARGUMENTS (fndecl) = params;
10516 DECL_INITIAL (fndecl) = error_mark_node;
10517 DECL_EXTERNAL (fndecl) = 0;
10518 TREE_STATIC (fndecl) = 1;
10519 retrofit_lang_decl (fndecl);
10520 cplus_decl_attributes (&fndecl, attrs, 0);
10521 start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT);
10523 current_function_returns_value = 0; /* Assume, until we see it does. */
10524 current_function_returns_null = 0;
10525 decl_attributes (&fndecl, attrs, 0);
10526 announce_function (fndecl);
10527 DECL_INITIAL (fndecl) = error_mark_node;
10528 DECL_EXTERNAL (fndecl) = 0;
10529 TREE_STATIC (fndecl) = 1;
10530 current_function_decl = pushdecl (fndecl);
10532 declare_parm_level ();
10533 DECL_RESULT (current_function_decl)
10534 = build_decl (input_location,
10535 RESULT_DECL, NULL_TREE,
10536 TREE_TYPE (TREE_TYPE (current_function_decl)));
10537 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1;
10538 DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1;
10539 start_fname_decls ();
10540 store_parm_decls_from (params);
10543 TREE_USED (current_function_decl) = 1;
10546 /* - Generate an identifier for the function. the format is "_n_cls",
10547 where 1 <= n <= nMethods, and cls is the name the implementation we
10549 - Install the return type from the method declaration.
10550 - If we have a prototype, check for type consistency. */
10553 really_start_method (tree method,
10557 struct c_arg_info *parmlist
10561 tree ret_type, meth_type;
10563 const char *sel_name, *class_name, *cat_name;
10566 /* Synth the storage class & assemble the return type. */
10567 ret_type = TREE_VALUE (TREE_TYPE (method));
10569 sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
10570 class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
10571 cat_name = ((TREE_CODE (objc_implementation_context)
10572 == CLASS_IMPLEMENTATION_TYPE)
10574 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context)));
10577 /* Make sure this is big enough for any plausible method label. */
10578 buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
10579 + (cat_name ? strlen (cat_name) : 0));
10581 OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
10582 class_name, cat_name, sel_name, method_slot);
10584 method_id = get_identifier (buf);
10587 /* Objective-C methods cannot be overloaded, so we don't need
10588 the type encoding appended. It looks bad anyway... */
10589 push_lang_context (lang_name_c);
10593 = build_function_type (ret_type,
10594 get_arg_type_list (method, METHOD_DEF, 0));
10595 objc_start_function (method_id, meth_type, NULL_TREE, parmlist);
10597 /* Set self_decl from the first argument. */
10598 self_decl = DECL_ARGUMENTS (current_function_decl);
10600 /* Suppress unused warnings. */
10601 TREE_USED (self_decl) = 1;
10602 DECL_READ_P (self_decl) = 1;
10603 TREE_USED (DECL_CHAIN (self_decl)) = 1;
10604 DECL_READ_P (DECL_CHAIN (self_decl)) = 1;
10606 pop_lang_context ();
10609 METHOD_DEFINITION (method) = current_function_decl;
10611 /* Check consistency...start_function, pushdecl, duplicate_decls. */
10613 if (implementation_template != objc_implementation_context)
10616 = lookup_method_static (implementation_template,
10617 METHOD_SEL_NAME (method),
10618 ((TREE_CODE (method) == CLASS_METHOD_DECL)
10619 | OBJC_LOOKUP_NO_SUPER));
10623 if (!comp_proto_with_proto (method, proto, 1))
10625 bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL;
10627 warning_at (DECL_SOURCE_LOCATION (method), 0,
10628 "conflicting types for %<%c%s%>",
10629 (type ? '-' : '+'),
10630 identifier_to_locale (gen_method_decl (method)));
10631 inform (DECL_SOURCE_LOCATION (proto),
10632 "previous declaration of %<%c%s%>",
10633 (type ? '-' : '+'),
10634 identifier_to_locale (gen_method_decl (proto)));
10639 /* We have a method @implementation even though we did not
10640 see a corresponding @interface declaration (which is allowed
10641 by Objective-C rules). Go ahead and place the method in
10642 the @interface anyway, so that message dispatch lookups
10644 tree interface = implementation_template;
10646 if (TREE_CODE (objc_implementation_context)
10647 == CATEGORY_IMPLEMENTATION_TYPE)
10648 interface = lookup_category
10650 CLASS_SUPER_NAME (objc_implementation_context));
10653 objc_add_method (interface, copy_node (method),
10654 TREE_CODE (method) == CLASS_METHOD_DECL,
10655 /* is_optional= */ false);
10660 static void *UOBJC_SUPER_scope = 0;
10662 /* _n_Method (id self, SEL sel, ...)
10664 struct objc_super _S;
10665 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
10669 get_super_receiver (void)
10671 if (objc_method_context)
10673 tree super_expr, super_expr_list;
10675 if (!UOBJC_SUPER_decl)
10677 UOBJC_SUPER_decl = build_decl (input_location,
10678 VAR_DECL, get_identifier (TAG_SUPER),
10679 objc_super_template);
10680 /* This prevents `unused variable' warnings when compiling with -Wall. */
10681 TREE_USED (UOBJC_SUPER_decl) = 1;
10682 DECL_READ_P (UOBJC_SUPER_decl) = 1;
10683 lang_hooks.decls.pushdecl (UOBJC_SUPER_decl);
10684 finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE,
10686 UOBJC_SUPER_scope = objc_get_current_scope ();
10689 /* Set receiver to self. */
10690 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id);
10691 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
10692 NOP_EXPR, input_location, self_decl,
10694 super_expr_list = super_expr;
10696 /* Set class to begin searching. */
10697 super_expr = objc_build_component_ref (UOBJC_SUPER_decl,
10698 get_identifier ("super_class"));
10700 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
10702 /* [_cls, __cls]Super are "pre-built" in
10703 synth_forward_declarations. */
10705 super_expr = build_modify_expr (input_location, super_expr,
10706 NULL_TREE, NOP_EXPR,
10708 ((TREE_CODE (objc_method_context)
10709 == INSTANCE_METHOD_DECL)
10711 : uucls_super_ref),
10716 /* We have a category. */
10718 tree super_name = CLASS_SUPER_NAME (implementation_template);
10721 /* Barf if super used in a category of Object. */
10724 error ("no super class declared in interface for %qE",
10725 CLASS_NAME (implementation_template));
10726 return error_mark_node;
10729 if (flag_next_runtime && !flag_zero_link)
10731 super_class = objc_get_class_reference (super_name);
10732 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
10733 /* If we are in a class method, we must retrieve the
10734 _metaclass_ for the current class, pointed at by
10735 the class's "isa" pointer. The following assumes that
10736 "isa" is the first ivar in a class (which it must be). */
10738 = build_indirect_ref
10740 build_c_cast (input_location,
10741 build_pointer_type (objc_class_type),
10742 super_class), RO_UNARY_STAR);
10746 add_class_reference (super_name);
10747 super_class = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
10748 ? objc_get_class_decl : objc_get_meta_class_decl);
10749 assemble_external (super_class);
10751 = build_function_call
10756 my_build_string_pointer
10757 (IDENTIFIER_LENGTH (super_name) + 1,
10758 IDENTIFIER_POINTER (super_name))));
10762 = build_modify_expr (input_location, super_expr, NULL_TREE,
10765 build_c_cast (input_location,
10766 TREE_TYPE (super_expr),
10771 super_expr_list = build_compound_expr (input_location,
10772 super_expr_list, super_expr);
10774 super_expr = build_unary_op (input_location,
10775 ADDR_EXPR, UOBJC_SUPER_decl, 0);
10776 super_expr_list = build_compound_expr (input_location,
10777 super_expr_list, super_expr);
10779 return super_expr_list;
10783 error ("[super ...] must appear in a method context");
10784 return error_mark_node;
10788 /* When exiting a scope, sever links to a 'super' declaration (if any)
10789 therein contained. */
10792 objc_clear_super_receiver (void)
10794 if (objc_method_context
10795 && UOBJC_SUPER_scope == objc_get_current_scope ()) {
10796 UOBJC_SUPER_decl = 0;
10797 UOBJC_SUPER_scope = 0;
10802 objc_finish_method_definition (tree fndecl)
10804 /* We cannot validly inline ObjC methods, at least not without a language
10805 extension to declare that a method need not be dynamically
10806 dispatched, so suppress all thoughts of doing so. */
10807 DECL_UNINLINABLE (fndecl) = 1;
10810 /* The C++ front-end will have called finish_function() for us. */
10811 finish_function ();
10814 METHOD_ENCODING (objc_method_context)
10815 = encode_method_prototype (objc_method_context);
10817 /* Required to implement _msgSuper. This must be done AFTER finish_function,
10818 since the optimizer may find "may be used before set" errors. */
10819 objc_method_context = NULL_TREE;
10821 if (should_call_super_dealloc)
10822 warning (0, "method possibly missing a [super dealloc] call");
10825 /* Given a tree DECL node, produce a printable description of it in the given
10826 buffer, overwriting the buffer. */
10829 gen_declaration (tree decl)
10835 gen_type_name_0 (TREE_TYPE (decl));
10837 if (DECL_NAME (decl))
10839 if (!POINTER_TYPE_P (TREE_TYPE (decl)))
10840 strcat (errbuf, " ");
10842 strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
10845 if (DECL_INITIAL (decl)
10846 && TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST)
10847 sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
10848 TREE_INT_CST_LOW (DECL_INITIAL (decl)));
10854 /* Given a tree TYPE node, produce a printable description of it in the given
10855 buffer, overwriting the buffer. */
10858 gen_type_name_0 (tree type)
10860 tree orig = type, proto;
10862 if (TYPE_P (type) && TYPE_NAME (type))
10863 type = TYPE_NAME (type);
10864 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
10866 tree inner = TREE_TYPE (type);
10868 while (TREE_CODE (inner) == ARRAY_TYPE)
10869 inner = TREE_TYPE (inner);
10871 gen_type_name_0 (inner);
10873 if (!POINTER_TYPE_P (inner))
10874 strcat (errbuf, " ");
10876 if (POINTER_TYPE_P (type))
10877 strcat (errbuf, "*");
10879 while (type != inner)
10881 strcat (errbuf, "[");
10883 if (TYPE_DOMAIN (type))
10887 sprintf (sz, HOST_WIDE_INT_PRINT_DEC,
10889 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1));
10890 strcat (errbuf, sz);
10893 strcat (errbuf, "]");
10894 type = TREE_TYPE (type);
10897 goto exit_function;
10900 if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type))
10901 type = DECL_NAME (type);
10903 strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE
10904 ? IDENTIFIER_POINTER (type)
10907 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
10908 if (objc_is_id (orig))
10909 orig = TREE_TYPE (orig);
10911 proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE;
10915 strcat (errbuf, " <");
10919 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto))));
10920 proto = TREE_CHAIN (proto);
10921 strcat (errbuf, proto ? ", " : ">");
10930 gen_type_name (tree type)
10934 return gen_type_name_0 (type);
10937 /* Given a method tree, put a printable description into the given
10938 buffer (overwriting) and return a pointer to the buffer. */
10941 gen_method_decl (tree method)
10945 strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */
10946 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method)));
10947 strcat (errbuf, ")");
10948 chain = METHOD_SEL_ARGS (method);
10952 /* We have a chain of keyword_decls. */
10955 if (KEYWORD_KEY_NAME (chain))
10956 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
10958 strcat (errbuf, ":(");
10959 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain)));
10960 strcat (errbuf, ")");
10962 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
10963 if ((chain = DECL_CHAIN (chain)))
10964 strcat (errbuf, " ");
10968 if (METHOD_ADD_ARGS (method))
10970 chain = TREE_CHAIN (METHOD_ADD_ARGS (method));
10972 /* Know we have a chain of parm_decls. */
10975 strcat (errbuf, ", ");
10976 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain)));
10977 chain = TREE_CHAIN (chain);
10980 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
10981 strcat (errbuf, ", ...");
10986 /* We have a unary selector. */
10987 strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
10995 /* Dump an @interface declaration of the supplied class CHAIN to the
10996 supplied file FP. Used to implement the -gen-decls option (which
10997 prints out an @interface declaration of all classes compiled in
10998 this run); potentially useful for debugging the compiler too. */
11000 dump_interface (FILE *fp, tree chain)
11002 /* FIXME: A heap overflow here whenever a method (or ivar)
11003 declaration is so long that it doesn't fit in the buffer. The
11004 code and all the related functions should be rewritten to avoid
11005 using fixed size buffers. */
11006 const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
11007 tree ivar_decls = CLASS_RAW_IVARS (chain);
11008 tree nst_methods = CLASS_NST_METHODS (chain);
11009 tree cls_methods = CLASS_CLS_METHODS (chain);
11011 fprintf (fp, "\n@interface %s", my_name);
11013 /* CLASS_SUPER_NAME is used to store the superclass name for
11014 classes, and the category name for categories. */
11015 if (CLASS_SUPER_NAME (chain))
11017 const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
11019 switch (TREE_CODE (chain))
11021 case CATEGORY_IMPLEMENTATION_TYPE:
11022 case CATEGORY_INTERFACE_TYPE:
11023 fprintf (fp, " (%s)\n", name);
11026 fprintf (fp, " : %s\n", name);
11031 fprintf (fp, "\n");
11033 /* FIXME - the following doesn't seem to work at the moment. */
11036 fprintf (fp, "{\n");
11039 fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls));
11040 ivar_decls = TREE_CHAIN (ivar_decls);
11042 while (ivar_decls);
11043 fprintf (fp, "}\n");
11046 while (nst_methods)
11048 fprintf (fp, "- %s;\n", gen_method_decl (nst_methods));
11049 nst_methods = TREE_CHAIN (nst_methods);
11052 while (cls_methods)
11054 fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods));
11055 cls_methods = TREE_CHAIN (cls_methods);
11058 fprintf (fp, "@end\n");
11062 /* Produce the pretty printing for an Objective-C method. This is
11063 currently unused, but could be handy while reorganizing the pretty
11064 printing to be more robust. */
11065 static const char *
11066 objc_pretty_print_method (bool is_class_method,
11067 const char *class_name,
11068 const char *category_name,
11069 const char *selector)
11073 char *result = XNEWVEC (char, strlen (class_name) + strlen (category_name)
11074 + strlen (selector) + 7);
11076 if (is_class_method)
11077 sprintf (result, "+[%s(%s) %s]", class_name, category_name, selector);
11079 sprintf (result, "-[%s(%s) %s]", class_name, category_name, selector);
11085 char *result = XNEWVEC (char, strlen (class_name)
11086 + strlen (selector) + 5);
11088 if (is_class_method)
11089 sprintf (result, "+[%s %s]", class_name, selector);
11091 sprintf (result, "-[%s %s]", class_name, selector);
11098 /* Demangle function for Objective-C. Attempt to demangle the
11099 function name associated with a method (eg, going from
11100 "_i_NSObject__class" to "-[NSObject class]"); usually for the
11101 purpose of pretty printing or error messages. Return the demangled
11102 name, or NULL if the string is not an Objective-C mangled method
11105 Because of how the mangling is done, any method that has a '_' in
11106 its original name is at risk of being demangled incorrectly. In
11107 some cases there are multiple valid ways to demangle a method name
11108 and there is no way we can decide.
11110 TODO: objc_demangle() can't always get it right; the right way to
11111 get this correct for all method names would be to store the
11112 Objective-C method name somewhere in the function decl. Then,
11113 there is no demangling to do; we'd just pull the method name out of
11114 the decl. As an additional bonus, when printing error messages we
11115 could check for such a method name, and if we find it, we know the
11116 function is actually an Objective-C method and we could print error
11117 messages saying "In method '+[NSObject class]" instead of "In
11118 function '+[NSObject class]" as we do now. */
11119 static const char *
11120 objc_demangle (const char *mangled)
11122 char *demangled, *cp;
11124 if (mangled[0] == '_' &&
11125 (mangled[1] == 'i' || mangled[1] == 'c') &&
11128 cp = demangled = XNEWVEC (char, strlen(mangled) + 2);
11129 if (mangled[1] == 'i')
11130 *cp++ = '-'; /* for instance method */
11132 *cp++ = '+'; /* for class method */
11133 *cp++ = '['; /* opening left brace */
11134 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
11135 while (*cp && *cp == '_')
11136 cp++; /* skip any initial underbars in class name */
11137 cp = strchr(cp, '_'); /* find first non-initial underbar */
11140 free(demangled); /* not mangled name */
11143 if (cp[1] == '_') /* easy case: no category name */
11145 *cp++ = ' '; /* replace two '_' with one ' ' */
11146 strcpy(cp, mangled + (cp - demangled) + 2);
11150 *cp++ = '('; /* less easy case: category name */
11151 cp = strchr(cp, '_');
11154 free(demangled); /* not mangled name */
11158 *cp++ = ' '; /* overwriting 1st char of method name... */
11159 strcpy(cp, mangled + (cp - demangled)); /* get it back */
11161 /* Now we have the method name. We need to generally replace
11162 '_' with ':' but trying to preserve '_' if it could only have
11163 been in the mangled string because it was already in the
11164 original name. In cases where it's ambiguous, we assume that
11165 any '_' originated from a ':'. */
11167 /* Initial '_'s in method name can't have been generating by
11168 converting ':'s. Skip them. */
11169 while (*cp && *cp == '_')
11172 /* If the method name does not end with '_', then it has no
11173 arguments and there was no replacement of ':'s with '_'s
11174 during mangling. Check for that case, and skip any
11175 replacement if so. This at least guarantees that methods
11176 with no arguments are always demangled correctly (unless the
11177 original name ends with '_'). */
11178 if (*(mangled + strlen (mangled) - 1) != '_')
11180 /* Skip to the end. */
11186 /* Replace remaining '_' with ':'. This may get it wrong if
11187 there were '_'s in the original name. In most cases it
11188 is impossible to disambiguate. */
11193 *cp++ = ']'; /* closing right brace */
11194 *cp++ = 0; /* string terminator */
11198 return NULL; /* not an objc mangled name */
11201 /* Try to pretty-print a decl. If the 'decl' is an Objective-C
11202 specific decl, return the printable name for it. If not, return
11205 objc_maybe_printable_name (tree decl, int v ATTRIBUTE_UNUSED)
11207 switch (TREE_CODE (decl))
11209 case FUNCTION_DECL:
11210 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)));
11213 /* The following happens when we are printing a deprecation
11214 warning for a method. The warn_deprecation() will end up
11215 trying to print the decl for INSTANCE_METHOD_DECL or
11216 CLASS_METHOD_DECL. It would be nice to be able to print
11217 "-[NSObject autorelease] is deprecated", but to do that, we'd
11218 need to store the class and method name in the method decl,
11219 which we currently don't do. For now, just return the name
11220 of the method. We don't return NULL, because that may
11221 trigger further attempts to pretty-print the decl in C/C++,
11222 but they wouldn't know how to pretty-print it. */
11223 case INSTANCE_METHOD_DECL:
11224 case CLASS_METHOD_DECL:
11225 return IDENTIFIER_POINTER (DECL_NAME (decl));
11233 /* Return a printable name for 'decl'. This first tries
11234 objc_maybe_printable_name(), and if that fails, it returns the name
11235 in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for
11236 Objective-C; in Objective-C++, setting the hook is not enough
11237 because lots of C++ Front-End code calls cxx_printable_name,
11238 dump_decl and other C++ functions directly. So instead we have
11239 modified dump_decl to call objc_maybe_printable_name directly. */
11241 objc_printable_name (tree decl, int v)
11243 const char *demangled_name = objc_maybe_printable_name (decl, v);
11245 if (demangled_name != NULL)
11246 return demangled_name;
11248 return IDENTIFIER_POINTER (DECL_NAME (decl));
11254 gcc_obstack_init (&util_obstack);
11255 util_firstobj = (char *) obstack_finish (&util_obstack);
11257 errbuf = XNEWVEC (char, 1024 * 10);
11259 synth_module_prologue ();
11265 struct imp_entry *impent;
11267 /* The internally generated initializers appear to have missing braces.
11268 Don't warn about this. */
11269 int save_warn_missing_braces = warn_missing_braces;
11270 warn_missing_braces = 0;
11272 /* A missing @end may not be detected by the parser. */
11273 if (objc_implementation_context)
11275 warning (0, "%<@end%> missing in implementation context");
11276 finish_class (objc_implementation_context);
11277 objc_ivar_chain = NULL_TREE;
11278 objc_implementation_context = NULL_TREE;
11281 /* Process the static instances here because initialization of objc_symtab
11282 depends on them. */
11283 if (objc_static_instances)
11284 generate_static_references ();
11286 /* forward declare categories */
11288 forward_declare_categories ();
11290 for (impent = imp_list; impent; impent = impent->next)
11292 objc_implementation_context = impent->imp_context;
11293 implementation_template = impent->imp_template;
11295 /* FIXME: This needs reworking to be more obvious. */
11297 UOBJC_CLASS_decl = impent->class_decl;
11298 UOBJC_METACLASS_decl = impent->meta_decl;
11300 /* Dump the @interface of each class as we compile it, if the
11301 -gen-decls option is in use. TODO: Dump the classes in the
11302 order they were found, rather than in reverse order as we
11304 if (flag_gen_declaration)
11306 dump_interface (gen_declaration_file, objc_implementation_context);
11309 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
11311 /* all of the following reference the string pool... */
11312 generate_ivar_lists ();
11313 generate_dispatch_tables ();
11314 generate_shared_structures (impent);
11318 generate_dispatch_tables ();
11319 generate_category (impent);
11322 impent->class_decl = UOBJC_CLASS_decl;
11323 impent->meta_decl = UOBJC_METACLASS_decl;
11326 /* If we are using an array of selectors, we must always
11327 finish up the array decl even if no selectors were used. */
11328 if (flag_next_runtime)
11329 build_next_selector_translation_table ();
11331 build_gnu_selector_translation_table ();
11333 if (protocol_chain)
11334 generate_protocols ();
11336 if (flag_next_runtime)
11337 generate_objc_image_info ();
11339 if (imp_list || class_names_chain
11340 || meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
11341 generate_objc_symtab_decl ();
11343 /* Arrange for ObjC data structures to be initialized at run time. */
11344 if (objc_implementation_context || class_names_chain || objc_static_instances
11345 || meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
11347 build_module_descriptor ();
11349 if (!flag_next_runtime)
11350 build_module_initializer_routine ();
11353 /* Dump the class references. This forces the appropriate classes
11354 to be linked into the executable image, preserving unix archive
11355 semantics. This can be removed when we move to a more dynamically
11356 linked environment. */
11358 for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
11360 handle_class_ref (chain);
11361 if (TREE_PURPOSE (chain))
11362 generate_classref_translation_entry (chain);
11365 for (impent = imp_list; impent; impent = impent->next)
11366 handle_impent (impent);
11373 /* Run through the selector hash tables and print a warning for any
11374 selector which has multiple methods. */
11376 for (slot = 0; slot < SIZEHASHTABLE; slot++)
11378 for (hsh = cls_method_hash_list[slot]; hsh; hsh = hsh->next)
11379 check_duplicates (hsh, 0, 1);
11380 for (hsh = nst_method_hash_list[slot]; hsh; hsh = hsh->next)
11381 check_duplicates (hsh, 0, 1);
11385 warn_missing_braces = save_warn_missing_braces;
11388 /* Subroutines of finish_objc. */
11391 generate_classref_translation_entry (tree chain)
11393 tree expr, decl, type;
11395 decl = TREE_PURPOSE (chain);
11396 type = TREE_TYPE (decl);
11398 expr = add_objc_string (TREE_VALUE (chain), class_names);
11399 expr = convert (type, expr); /* cast! */
11401 /* This is a class reference. It is re-written by the runtime,
11402 but will be optimized away unless we force it. */
11403 DECL_PRESERVE_P (decl) = 1;
11404 finish_var_decl (decl, expr);
11409 handle_class_ref (tree chain)
11411 const char *name = IDENTIFIER_POINTER (TREE_VALUE (chain));
11412 char *string = (char *) alloca (strlen (name) + 30);
11416 sprintf (string, "%sobjc_class_name_%s",
11417 (flag_next_runtime ? "." : "__"), name);
11419 #ifdef ASM_DECLARE_UNRESOLVED_REFERENCE
11420 if (flag_next_runtime)
11422 ASM_DECLARE_UNRESOLVED_REFERENCE (asm_out_file, string);
11427 /* Make a decl for this name, so we can use its address in a tree. */
11428 decl = build_decl (input_location,
11429 VAR_DECL, get_identifier (string), TREE_TYPE (integer_zero_node));
11430 DECL_EXTERNAL (decl) = 1;
11431 TREE_PUBLIC (decl) = 1;
11433 finish_var_decl (decl, 0);
11435 /* Make a decl for the address. */
11436 sprintf (string, "%sobjc_class_ref_%s",
11437 (flag_next_runtime ? "." : "__"), name);
11438 exp = build1 (ADDR_EXPR, string_type_node, decl);
11439 decl = build_decl (input_location,
11440 VAR_DECL, get_identifier (string), string_type_node);
11441 TREE_STATIC (decl) = 1;
11442 TREE_USED (decl) = 1;
11443 DECL_READ_P (decl) = 1;
11444 DECL_ARTIFICIAL (decl) = 1;
11445 DECL_INITIAL (decl) = error_mark_node;
11447 /* We must force the reference. */
11448 DECL_PRESERVE_P (decl) = 1;
11451 finish_var_decl (decl, exp);
11455 handle_impent (struct imp_entry *impent)
11459 objc_implementation_context = impent->imp_context;
11460 implementation_template = impent->imp_template;
11462 switch (TREE_CODE (impent->imp_context))
11464 case CLASS_IMPLEMENTATION_TYPE:
11466 const char *const class_name =
11467 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
11469 string = (char *) alloca (strlen (class_name) + 30);
11471 sprintf (string, "%sobjc_class_name_%s",
11472 (flag_next_runtime ? "." : "__"), class_name);
11475 case CATEGORY_IMPLEMENTATION_TYPE:
11477 const char *const class_name =
11478 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
11479 const char *const class_super_name =
11480 IDENTIFIER_POINTER (CLASS_SUPER_NAME (impent->imp_context));
11482 string = (char *) alloca (strlen (class_name)
11483 + strlen (class_super_name) + 30);
11485 /* Do the same for categories. Even though no references to
11486 these symbols are generated automatically by the compiler,
11487 it gives you a handle to pull them into an archive by
11489 sprintf (string, "*%sobjc_category_name_%s_%s",
11490 (flag_next_runtime ? "." : "__"), class_name, class_super_name);
11497 #ifdef ASM_DECLARE_CLASS_REFERENCE
11498 if (flag_next_runtime)
11500 ASM_DECLARE_CLASS_REFERENCE (asm_out_file, string);
11508 init = integer_zero_node;
11509 decl = build_decl (input_location,
11510 VAR_DECL, get_identifier (string), TREE_TYPE (init));
11511 TREE_PUBLIC (decl) = 1;
11512 TREE_READONLY (decl) = 1;
11513 TREE_USED (decl) = 1;
11514 TREE_CONSTANT (decl) = 1;
11515 DECL_CONTEXT (decl) = NULL_TREE;
11516 DECL_ARTIFICIAL (decl) = 1;
11517 TREE_STATIC (decl) = 1;
11518 DECL_INITIAL (decl) = error_mark_node; /* A real initializer is coming... */
11519 /* We must force the reference. */
11520 DECL_PRESERVE_P (decl) = 1;
11522 finish_var_decl(decl, init) ;
11526 /* The Fix-and-Continue functionality available in Mac OS X 10.3 and
11527 later requires that ObjC translation units participating in F&C be
11528 specially marked. The following routine accomplishes this. */
11530 /* static int _OBJC_IMAGE_INFO[2] = { 0, 1 }; */
11533 generate_objc_image_info (void)
11537 = ((flag_replace_objc_classes && imp_count ? 1 : 0)
11538 | (flag_objc_gc ? 2 : 0));
11539 VEC(constructor_elt,gc) *v = NULL;
11543 return; /* No need for an image_info entry. */
11545 array_type = build_sized_array_type (integer_type_node, 2);
11547 decl = start_var_decl (array_type, "_OBJC_IMAGE_INFO");
11549 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
11550 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (integer_type_node, flags));
11551 /* If we need this (determined above) it is because the runtime wants to
11552 refer to it in a manner hidden from the compiler. So we must force the
11554 DECL_PRESERVE_P (decl) = 1;
11555 finish_var_decl (decl, objc_build_constructor (TREE_TYPE (decl), v));
11558 /* Routine is called to issue diagnostic when reference to a private
11559 ivar is made and no other variable with same name is found in
11562 objc_diagnose_private_ivar (tree id)
11565 if (!objc_method_context)
11567 ivar = is_ivar (objc_ivar_chain, id);
11568 if (ivar && is_private (ivar))
11570 error ("instance variable %qs is declared private",
11571 IDENTIFIER_POINTER (id));
11577 /* Look up ID as an instance variable. OTHER contains the result of
11578 the C or C++ lookup, which we may want to use instead. */
11579 /* To use properties inside an instance method, use self.property. */
11581 objc_lookup_ivar (tree other, tree id)
11585 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
11586 if (!objc_method_context)
11589 if (!strcmp (IDENTIFIER_POINTER (id), "super"))
11590 /* We have a message to super. */
11591 return get_super_receiver ();
11593 /* In a class method, look up an instance variable only as a last
11595 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
11596 && other && other != error_mark_node)
11599 /* Look up the ivar, but do not use it if it is not accessible. */
11600 ivar = is_ivar (objc_ivar_chain, id);
11602 if (!ivar || is_private (ivar))
11605 /* In an instance method, a local variable (or parameter) may hide the
11606 instance variable. */
11607 if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
11608 && other && other != error_mark_node
11610 && CP_DECL_CONTEXT (other) != global_namespace)
11612 && !DECL_FILE_SCOPE_P (other))
11615 warning (0, "local declaration of %qE hides instance variable", id);
11620 /* At this point, we are either in an instance method with no obscuring
11621 local definitions, or in a class method with no alternate definitions
11623 return build_ivar_reference (id);
11626 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
11627 needs to be done if we are calling a function through a cast. */
11630 objc_rewrite_function_call (tree function, tree first_param)
11632 if (TREE_CODE (function) == NOP_EXPR
11633 && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR
11634 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0))
11637 function = build3 (OBJ_TYPE_REF, TREE_TYPE (function),
11638 TREE_OPERAND (function, 0),
11639 first_param, size_zero_node);
11645 /* This is called to "gimplify" a PROPERTY_REF node. It builds the
11646 corresponding 'getter' function call. Note that we assume the
11647 PROPERTY_REF to be valid since we generated it while parsing. */
11649 objc_gimplify_property_ref (tree *expr_p)
11651 tree object_expression = PROPERTY_REF_OBJECT (*expr_p);
11652 tree property_decl = PROPERTY_REF_PROPERTY_DECL (*expr_p);
11653 tree call_exp, getter;
11655 /* TODO: Implement super.property. */
11657 getter = objc_finish_message_expr (object_expression,
11658 PROPERTY_GETTER_NAME (property_decl),
11662 /* In C++, a getter which returns an aggregate value results in a
11663 target_expr which initializes a temporary to the call
11665 if (TREE_CODE (getter) == TARGET_EXPR)
11667 gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter)));
11668 gcc_assert (TREE_CODE (TREE_OPERAND (getter, 0)) == VAR_DECL);
11669 call_exp = TREE_OPERAND (getter, 1);
11672 gcc_assert (TREE_CODE (call_exp) == CALL_EXPR);
11674 *expr_p = call_exp;
11677 /* This is called when "gimplifying" the trees. We need to gimplify
11678 the Objective-C/Objective-C++ specific trees, then hand over the
11679 process to C/C++. */
11681 objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
11683 enum tree_code code = TREE_CODE (*expr_p);
11686 /* Look for the special case of OBJC_TYPE_REF with the address
11687 of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend
11688 or one of its cousins). */
11690 if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR
11691 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0))
11694 enum gimplify_status r0, r1;
11696 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
11697 value of the OBJ_TYPE_REF, so force them to be emitted
11698 during subexpression evaluation rather than after the
11699 OBJ_TYPE_REF. This permits objc_msgSend calls in
11700 Objective C to use direct rather than indirect calls when
11701 the object expression has a postincrement. */
11702 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL,
11703 is_gimple_val, fb_rvalue);
11704 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
11705 is_gimple_val, fb_rvalue);
11707 return MIN (r0, r1);
11711 objc_gimplify_property_ref (expr_p);
11712 /* Do not return yet; let C/C++ gimplify the resulting expression. */
11719 return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p);
11721 return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
11725 /* This routine returns true if TYP is a valid objc object type,
11726 suitable for messaging; false otherwise.
11730 objc_type_valid_for_messaging (tree typ)
11732 if (!POINTER_TYPE_P (typ))
11736 typ = TREE_TYPE (typ); /* Remove indirections. */
11737 while (POINTER_TYPE_P (typ));
11739 if (TREE_CODE (typ) != RECORD_TYPE)
11742 return objc_is_object_id (typ) || TYPE_HAS_OBJC_INFO (typ);
11745 /* Begin code generation for fast enumeration (foreach) ... */
11749 struct __objcFastEnumerationState
11751 unsigned long state;
11753 unsigned long *mutationsPtr;
11754 unsigned long extra[5];
11757 Confusingly enough, NSFastEnumeration is then defined by libraries
11758 to be the same structure.
11762 build_fast_enumeration_state_template (void)
11764 tree decls, *chain = NULL;
11767 objc_fast_enumeration_state_template = objc_start_struct (get_identifier
11768 (TAG_FAST_ENUMERATION_STATE));
11770 /* unsigned long state; */
11771 decls = add_field_decl (long_unsigned_type_node, "state", &chain);
11773 /* id *itemsPtr; */
11774 add_field_decl (build_pointer_type (objc_object_type),
11775 "itemsPtr", &chain);
11777 /* unsigned long *mutationsPtr; */
11778 add_field_decl (build_pointer_type (long_unsigned_type_node),
11779 "mutationsPtr", &chain);
11781 /* unsigned long extra[5]; */
11782 add_field_decl (build_sized_array_type (long_unsigned_type_node, 5),
11786 objc_finish_struct (objc_fast_enumeration_state_template, decls);
11790 'objc_finish_foreach_loop()' generates the code for an Objective-C
11791 foreach loop. The 'location' argument is the location of the 'for'
11792 that starts the loop. The 'object_expression' is the expression of
11793 the 'object' that iterates; the 'collection_expression' is the
11794 expression of the collection that we iterate over (we need to make
11795 sure we evaluate this only once); the 'for_body' is the set of
11796 statements to be executed in each iteration; 'break_label' and
11797 'continue_label' are the break and continue labels which we need to
11798 emit since the <statements> may be jumping to 'break_label' (if they
11799 contain 'break') or to 'continue_label' (if they contain
11804 for (<object expression> in <collection expression>)
11807 which is compiled into the following blurb:
11810 id __objc_foreach_collection;
11811 __objc_fast_enumeration_state __objc_foreach_enum_state;
11812 unsigned long __objc_foreach_batchsize;
11813 id __objc_foreach_items[16];
11814 __objc_foreach_collection = <collection expression>;
11815 __objc_foreach_enum_state = { 0 };
11816 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
11818 if (__objc_foreach_batchsize == 0)
11819 <object expression> = nil;
11822 unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr;
11825 unsigned long __objc_foreach_index;
11826 __objc_foreach_index = 0;
11829 if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>);
11830 <object expression> = enumState.itemsPtr[__objc_foreach_index];
11831 <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label]
11834 __objc_foreach_index++;
11835 if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object;
11836 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
11838 if (__objc_foreach_batchsize != 0) goto next_batch;
11839 <object expression> = nil;
11844 'statements' may contain a 'continue' or 'break' instruction, which
11845 the user expects to 'continue' or 'break' the entire foreach loop.
11846 We are provided the labels that 'break' and 'continue' jump to, so
11847 we place them where we want them to jump to when they pick them.
11849 Optimization TODO: we could cache the IMP of
11850 countByEnumeratingWithState:objects:count:.
11853 /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */
11854 /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */
11856 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
11857 #include "tree-pretty-print.h"
11861 objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body,
11862 tree break_label, tree continue_label)
11864 /* A tree representing the __objcFastEnumerationState struct type,
11865 or NSFastEnumerationState struct, whatever we are using. */
11866 tree objc_fast_enumeration_state_type;
11868 /* The trees representing the declarations of each of the local variables. */
11869 tree objc_foreach_collection_decl;
11870 tree objc_foreach_enum_state_decl;
11871 tree objc_foreach_items_decl;
11872 tree objc_foreach_batchsize_decl;
11873 tree objc_foreach_mutations_pointer_decl;
11874 tree objc_foreach_index_decl;
11876 /* A tree representing the selector countByEnumeratingWithState:objects:count:. */
11877 tree selector_name;
11879 /* A tree representing the local bind. */
11882 /* A tree representing the external 'if (__objc_foreach_batchsize)' */
11885 /* A tree representing the 'else' part of 'first_if' */
11888 /* A tree representing the 'next_batch' label. */
11889 tree next_batch_label_decl;
11891 /* A tree representing the binding after the 'next_batch' label. */
11892 tree next_batch_bind;
11894 /* A tree representing the 'next_object' label. */
11895 tree next_object_label_decl;
11897 /* Temporary variables. */
11901 if (object_expression == error_mark_node)
11904 if (collection_expression == error_mark_node)
11907 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression)))
11909 error ("iterating variable in fast enumeration is not an object");
11913 if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression)))
11915 error ("collection in fast enumeration is not an object");
11919 /* TODO: Check that object_expression is either a variable
11920 declaration, or an lvalue. */
11922 /* This kludge is an idea from apple. We use the
11923 __objcFastEnumerationState struct implicitly defined by the
11924 compiler, unless a NSFastEnumerationState struct has been defined
11925 (by a Foundation library such as GNUstep Base) in which case, we
11928 objc_fast_enumeration_state_type = objc_fast_enumeration_state_template;
11930 tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState"));
11932 if (objc_NSFastEnumeration_type)
11934 /* TODO: We really need to check that
11935 objc_NSFastEnumeration_type is the same as ours! */
11936 if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL)
11938 /* If it's a typedef, use the original type. */
11939 if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type))
11940 objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type);
11942 objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type);
11948 /* Done by c-parser.c. */
11951 /* Done by c-parser.c. */
11953 /* id __objc_foreach_collection */
11954 objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection");
11956 /* __objcFastEnumerationState __objc_foreach_enum_state; */
11957 objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state");
11958 TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl;
11960 /* id __objc_foreach_items[16]; */
11961 objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items");
11962 TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl;
11964 /* unsigned long __objc_foreach_batchsize; */
11965 objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize");
11966 TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl;
11968 /* Generate the local variable binding. */
11969 bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL);
11970 SET_EXPR_LOCATION (bind, location);
11971 TREE_SIDE_EFFECTS (bind) = 1;
11973 /* __objc_foreach_collection = <collection expression>; */
11974 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression);
11975 SET_EXPR_LOCATION (t, location);
11976 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
11978 /* __objc_foreach_enum_state.state = 0; */
11979 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
11980 get_identifier ("state")),
11981 build_int_cst (long_unsigned_type_node, 0));
11982 SET_EXPR_LOCATION (t, location);
11983 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
11985 /* __objc_foreach_enum_state.itemsPtr = NULL; */
11986 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
11987 get_identifier ("itemsPtr")),
11988 null_pointer_node);
11989 SET_EXPR_LOCATION (t, location);
11990 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
11992 /* __objc_foreach_enum_state.mutationsPtr = NULL; */
11993 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
11994 get_identifier ("mutationsPtr")),
11995 null_pointer_node);
11996 SET_EXPR_LOCATION (t, location);
11997 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
11999 /* __objc_foreach_enum_state.extra[0] = 0; */
12000 /* __objc_foreach_enum_state.extra[1] = 0; */
12001 /* __objc_foreach_enum_state.extra[2] = 0; */
12002 /* __objc_foreach_enum_state.extra[3] = 0; */
12003 /* __objc_foreach_enum_state.extra[4] = 0; */
12004 for (i = 0; i < 5 ; i++)
12006 t = build2 (MODIFY_EXPR, void_type_node,
12007 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
12008 get_identifier ("extra")),
12009 build_int_cst (NULL_TREE, i)),
12010 build_int_cst (long_unsigned_type_node, 0));
12011 SET_EXPR_LOCATION (t, location);
12012 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
12015 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
12016 selector_name = get_identifier ("countByEnumeratingWithState:objects:count:");
12018 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
12020 tree_cons /* &__objc_foreach_enum_state */
12021 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
12022 tree_cons /* __objc_foreach_items */
12023 (NULL_TREE, objc_foreach_items_decl,
12025 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
12027 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
12029 struct c_expr array;
12030 array.value = objc_foreach_items_decl;
12031 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
12033 tree_cons /* &__objc_foreach_enum_state */
12034 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
12035 tree_cons /* __objc_foreach_items */
12036 (NULL_TREE, default_function_array_conversion (location, array).value,
12038 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
12041 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
12042 convert (long_unsigned_type_node, t));
12043 SET_EXPR_LOCATION (t, location);
12044 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
12046 /* if (__objc_foreach_batchsize == 0) */
12047 first_if = build3 (COND_EXPR, void_type_node,
12050 (c_common_truthvalue_conversion
12052 build_binary_op (location,
12054 objc_foreach_batchsize_decl,
12055 build_int_cst (long_unsigned_type_node, 0), 1)),
12057 /* Then block (we fill it in later). */
12059 /* Else block (we fill it in later). */
12061 SET_EXPR_LOCATION (first_if, location);
12062 append_to_statement_list (first_if, &BIND_EXPR_BODY (bind));
12064 /* then <object expression> = nil; */
12065 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
12066 SET_EXPR_LOCATION (t, location);
12067 COND_EXPR_THEN (first_if) = t;
12069 /* Now we build the 'else' part of the if; once we finish building
12070 it, we attach it to first_if as the 'else' part. */
12075 /* unsigned long __objc_foreach_mutations_pointer; */
12076 objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer");
12078 /* Generate the local variable binding. */
12079 first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL);
12080 SET_EXPR_LOCATION (first_else, location);
12081 TREE_SIDE_EFFECTS (first_else) = 1;
12083 /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */
12084 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl,
12085 build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
12086 get_identifier ("mutationsPtr")),
12088 SET_EXPR_LOCATION (t, location);
12089 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
12092 next_batch_label_decl = create_artificial_label (location);
12093 t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl);
12094 SET_EXPR_LOCATION (t, location);
12095 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
12099 /* unsigned long __objc_foreach_index; */
12100 objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index");
12102 /* Generate the local variable binding. */
12103 next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL);
12104 SET_EXPR_LOCATION (next_batch_bind, location);
12105 TREE_SIDE_EFFECTS (next_batch_bind) = 1;
12106 append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else));
12108 /* __objc_foreach_index = 0; */
12109 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
12110 build_int_cst (long_unsigned_type_node, 0));
12111 SET_EXPR_LOCATION (t, location);
12112 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12115 next_object_label_decl = create_artificial_label (location);
12116 t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl);
12117 SET_EXPR_LOCATION (t, location);
12118 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12120 /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */
12121 t = build3 (COND_EXPR, void_type_node,
12124 (c_common_truthvalue_conversion
12129 objc_foreach_mutations_pointer_decl,
12130 build_indirect_ref (location,
12131 objc_build_component_ref (objc_foreach_enum_state_decl,
12132 get_identifier ("mutationsPtr")),
12133 RO_UNARY_STAR), 1)),
12136 build_function_call (input_location,
12137 objc_enumeration_mutation_decl,
12138 tree_cons (NULL, collection_expression, NULL)),
12141 SET_EXPR_LOCATION (t, location);
12142 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12144 /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */
12145 t = build2 (MODIFY_EXPR, void_type_node, object_expression,
12146 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
12147 get_identifier ("itemsPtr")),
12148 objc_foreach_index_decl));
12149 SET_EXPR_LOCATION (t, location);
12150 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12152 /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */
12153 append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind));
12155 /* continue_label: */
12156 if (continue_label)
12158 t = build1 (LABEL_EXPR, void_type_node, continue_label);
12159 SET_EXPR_LOCATION (t, location);
12160 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12163 /* __objc_foreach_index++; */
12164 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
12165 build_binary_op (location,
12167 objc_foreach_index_decl,
12168 build_int_cst (long_unsigned_type_node, 1), 1));
12169 SET_EXPR_LOCATION (t, location);
12170 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12172 /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */
12173 t = build3 (COND_EXPR, void_type_node,
12176 (c_common_truthvalue_conversion
12178 build_binary_op (location,
12180 objc_foreach_index_decl,
12181 objc_foreach_batchsize_decl, 1)),
12184 build1 (GOTO_EXPR, void_type_node, next_object_label_decl),
12187 SET_EXPR_LOCATION (t, location);
12188 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12190 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
12192 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
12194 tree_cons /* &__objc_foreach_enum_state */
12195 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
12196 tree_cons /* __objc_foreach_items */
12197 (NULL_TREE, objc_foreach_items_decl,
12199 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
12201 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
12203 struct c_expr array;
12204 array.value = objc_foreach_items_decl;
12205 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
12207 tree_cons /* &__objc_foreach_enum_state */
12208 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
12209 tree_cons /* __objc_foreach_items */
12210 (NULL_TREE, default_function_array_conversion (location, array).value,
12212 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
12215 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
12216 convert (long_unsigned_type_node, t));
12217 SET_EXPR_LOCATION (t, location);
12218 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12222 /* if (__objc_foreach_batchsize != 0) goto next_batch; */
12223 t = build3 (COND_EXPR, void_type_node,
12226 (c_common_truthvalue_conversion
12228 build_binary_op (location,
12230 objc_foreach_batchsize_decl,
12231 build_int_cst (long_unsigned_type_node, 0), 1)),
12234 build1 (GOTO_EXPR, void_type_node, next_batch_label_decl),
12237 SET_EXPR_LOCATION (t, location);
12238 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
12240 /* <object expression> = nil; */
12241 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
12242 SET_EXPR_LOCATION (t, location);
12243 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
12248 t = build1 (LABEL_EXPR, void_type_node, break_label);
12249 SET_EXPR_LOCATION (t, location);
12250 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
12254 COND_EXPR_ELSE (first_if) = first_else;
12256 /* Do the whole thing. */
12259 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
12260 /* This will print to stderr the whole blurb generated by the
12261 compiler while compiling (assuming the compiler doesn't crash
12262 before getting here).
12264 debug_generic_stmt (bind);
12268 /* Done by c-parser.c */
12271 #include "gt-objc-objc-act.h"