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 /* I am not sure it is really correct.
79 Perhaps there's a danger that it will make name conflicts
80 if method names contain underscores. -- rms. */
81 #ifndef OBJC_GEN_METHOD_LABEL
82 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
85 sprintf ((BUF), "_%s_%s_%s_%s", \
86 ((IS_INST) ? "i" : "c"), \
88 ((CAT_NAME)? (CAT_NAME) : ""), \
90 for (temp = (BUF); *temp; temp++) \
91 if (*temp == ':') *temp = '_'; \
95 /* These need specifying. */
96 #ifndef OBJC_FORWARDING_STACK_OFFSET
97 #define OBJC_FORWARDING_STACK_OFFSET 0
100 #ifndef OBJC_FORWARDING_MIN_OFFSET
101 #define OBJC_FORWARDING_MIN_OFFSET 0
104 /* Set up for use of obstacks. */
108 /* This obstack is used to accumulate the encoding of a data type. */
109 static struct obstack util_obstack;
111 /* This points to the beginning of obstack contents, so we can free
112 the whole contents. */
115 /* The version identifies which language generation and runtime
116 the module (file) was compiled for, and is recorded in the
117 module descriptor. */
119 #define OBJC_VERSION (flag_next_runtime ? 6 : 8)
120 #define PROTOCOL_VERSION 2
122 /* (Decide if these can ever be validly changed.) */
123 #define OBJC_ENCODE_INLINE_DEFS 0
124 #define OBJC_ENCODE_DONT_INLINE_DEFS 1
126 /*** Private Interface (procedures) ***/
128 /* Used by compile_file. */
130 static void init_objc (void);
131 static void finish_objc (void);
133 /* Code generation. */
135 static tree objc_build_constructor (tree, VEC(constructor_elt,gc) *);
136 static tree build_objc_method_call (location_t, int, tree, tree, tree, tree);
137 static tree get_proto_encoding (tree);
138 static tree lookup_interface (tree);
139 static tree objc_add_static_instance (tree, tree);
141 static tree start_class (enum tree_code, tree, tree, tree);
142 static tree continue_class (tree);
143 static void finish_class (tree);
144 static void start_method_def (tree);
146 static void objc_start_function (tree, tree, tree, tree);
148 static void objc_start_function (tree, tree, tree, struct c_arg_info *);
150 static tree start_protocol (enum tree_code, tree, tree);
151 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
152 static tree objc_add_method (tree, tree, int, bool);
153 static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree);
154 static tree build_ivar_reference (tree);
155 static tree is_ivar (tree, tree);
157 static void build_objc_exception_stuff (void);
158 static void build_next_objc_exception_stuff (void);
160 /* We only need the following for ObjC; ObjC++ will use C++'s definition
161 of DERIVED_FROM_P. */
163 static bool objc_derived_from_p (tree, tree);
164 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
168 static void objc_gen_one_property_datum (tree, tree, tree, bool*);
169 static void objc_gen_property_data (tree, tree);
170 static void objc_synthesize_getter (tree, tree, tree);
171 static void objc_process_getter_setter (tree, tree, bool);
172 static void objc_synthesize_setter (tree, tree, tree);
173 static char *objc_build_property_ivar_name (tree);
174 static char *objc_build_property_setter_name (tree, bool);
175 static int match_proto_with_proto (tree, tree, int);
176 static tree lookup_property (tree, tree);
177 static tree lookup_property_in_list (tree, tree);
178 static tree lookup_property_in_protocol_list (tree, tree);
179 static tree objc_setter_func_call (tree, tree, tree);
180 static tree build_property_reference (tree, tree);
181 static tree is_property (tree, tree);
182 /* Set on a CALL_EXPR if it is for call to a getter function represented by an
183 objective-c property declaration. */
184 #define CALL_EXPR_OBJC_PROPERTY_GETTER(NODE) \
185 (CALL_EXPR_CHECK(NODE)->base.deprecated_flag)
187 static void objc_xref_basetypes (tree, tree);
189 static void build_class_template (void);
190 static void build_selector_template (void);
191 static void build_category_template (void);
192 static void build_super_template (void);
193 static tree build_protocol_initializer (tree, tree, tree, tree, tree);
194 static tree get_class_ivars (tree, bool);
195 static tree generate_protocol_list (tree);
196 static void build_protocol_reference (tree);
198 static void build_fast_enumeration_state_template (void);
201 static void objc_generate_cxx_cdtors (void);
205 static void objc_decl_method_attributes (tree*, tree, int);
206 static tree build_keyword_selector (tree);
207 static const char *synth_id_with_class_suffix (const char *, tree);
209 /* Hash tables to manage the global pool of method prototypes. */
211 hash *nst_method_hash_list = 0;
212 hash *cls_method_hash_list = 0;
214 static hash hash_lookup (hash *, tree);
215 static tree lookup_method (tree, tree);
216 static tree lookup_method_static (tree, tree, int);
218 static tree add_class (tree, tree);
219 static void add_category (tree, tree);
220 static inline tree lookup_category (tree, tree);
224 class_names, /* class, category, protocol, module names */
225 meth_var_names, /* method and variable names */
226 meth_var_types /* method and variable type descriptors */
229 static tree add_objc_string (tree, enum string_section);
230 static void build_selector_table_decl (void);
232 /* Protocol additions. */
234 static tree lookup_protocol (tree);
235 static tree lookup_and_install_protocols (tree);
239 static void encode_type_qualifiers (tree);
240 static void encode_type (tree, int, int);
241 static void encode_field_decl (tree, int, int);
244 static void really_start_method (tree, tree);
246 static void really_start_method (tree, struct c_arg_info *);
248 static int comp_proto_with_proto (tree, tree, int);
249 static tree get_arg_type_list (tree, int, int);
250 static tree objc_decay_parm_type (tree);
251 static void objc_push_parm (tree);
253 static tree objc_get_parm_info (int);
255 static struct c_arg_info *objc_get_parm_info (int);
258 /* Utilities for debugging and error diagnostics. */
260 static char *gen_type_name (tree);
261 static char *gen_type_name_0 (tree);
262 static char *gen_method_decl (tree);
263 static char *gen_declaration (tree);
265 /* Everything else. */
267 static tree create_field_decl (tree, const char *);
268 static void add_class_reference (tree);
269 static void build_protocol_template (void);
270 static tree encode_method_prototype (tree);
271 static void generate_classref_translation_entry (tree);
272 static void handle_class_ref (tree);
273 static void generate_struct_by_value_array (void)
275 static void mark_referenced_methods (void);
276 static void generate_objc_image_info (void);
277 static bool objc_type_valid_for_messaging (tree typ);
279 /*** Private Interface (data) ***/
281 /* Reserved tag definitions. */
283 #define OBJECT_TYPEDEF_NAME "id"
284 #define CLASS_TYPEDEF_NAME "Class"
286 #define TAG_OBJECT "objc_object"
287 #define TAG_CLASS "objc_class"
288 #define TAG_SUPER "objc_super"
289 #define TAG_SELECTOR "objc_selector"
291 #define UTAG_CLASS "_objc_class"
292 #define UTAG_IVAR "_objc_ivar"
293 #define UTAG_IVAR_LIST "_objc_ivar_list"
294 #define UTAG_METHOD "_objc_method"
295 #define UTAG_METHOD_LIST "_objc_method_list"
296 #define UTAG_CATEGORY "_objc_category"
297 #define UTAG_MODULE "_objc_module"
298 #define UTAG_SYMTAB "_objc_symtab"
299 #define UTAG_SUPER "_objc_super"
300 #define UTAG_SELECTOR "_objc_selector"
302 #define UTAG_PROTOCOL "_objc_protocol"
303 #define UTAG_METHOD_PROTOTYPE "_objc_method_prototype"
304 #define UTAG_METHOD_PROTOTYPE_LIST "_objc__method_prototype_list"
306 /* Note that the string object global name is only needed for the
308 #define STRING_OBJECT_GLOBAL_FORMAT "_%sClassReference"
310 #define PROTOCOL_OBJECT_CLASS_NAME "Protocol"
312 #define TAG_ENUMERATION_MUTATION "objc_enumerationMutation"
313 #define TAG_FAST_ENUMERATION_STATE "__objcFastEnumerationState"
315 static const char *TAG_GETCLASS;
316 static const char *TAG_GETMETACLASS;
317 static const char *TAG_MSGSEND;
318 static const char *TAG_MSGSENDSUPER;
319 /* The NeXT Objective-C messenger may have two extra entry points, for use
320 when returning a structure. */
321 static const char *TAG_MSGSEND_STRET;
322 static const char *TAG_MSGSENDSUPER_STRET;
323 static const char *default_constant_string_class_name;
325 /* Runtime metadata flags. */
326 #define CLS_FACTORY 0x0001L
327 #define CLS_META 0x0002L
328 #define CLS_HAS_CXX_STRUCTORS 0x2000L
330 #define OBJC_MODIFIER_STATIC 0x00000001
331 #define OBJC_MODIFIER_FINAL 0x00000002
332 #define OBJC_MODIFIER_PUBLIC 0x00000004
333 #define OBJC_MODIFIER_PRIVATE 0x00000008
334 #define OBJC_MODIFIER_PROTECTED 0x00000010
335 #define OBJC_MODIFIER_NATIVE 0x00000020
336 #define OBJC_MODIFIER_SYNCHRONIZED 0x00000040
337 #define OBJC_MODIFIER_ABSTRACT 0x00000080
338 #define OBJC_MODIFIER_VOLATILE 0x00000100
339 #define OBJC_MODIFIER_TRANSIENT 0x00000200
340 #define OBJC_MODIFIER_NONE_SPECIFIED 0x80000000
342 /* NeXT-specific tags. */
344 #define TAG_MSGSEND_NONNIL "objc_msgSendNonNil"
345 #define TAG_MSGSEND_NONNIL_STRET "objc_msgSendNonNil_stret"
346 #define TAG_EXCEPTIONEXTRACT "objc_exception_extract"
347 #define TAG_EXCEPTIONTRYENTER "objc_exception_try_enter"
348 #define TAG_EXCEPTIONTRYEXIT "objc_exception_try_exit"
349 #define TAG_EXCEPTIONMATCH "objc_exception_match"
350 #define TAG_EXCEPTIONTHROW "objc_exception_throw"
351 #define TAG_SYNCENTER "objc_sync_enter"
352 #define TAG_SYNCEXIT "objc_sync_exit"
353 #define TAG_SETJMP "_setjmp"
354 #define UTAG_EXCDATA "_objc_exception_data"
356 #define TAG_ASSIGNIVAR "objc_assign_ivar"
357 #define TAG_ASSIGNGLOBAL "objc_assign_global"
358 #define TAG_ASSIGNSTRONGCAST "objc_assign_strongCast"
360 /* Branch entry points. All that matters here are the addresses;
361 functions with these names do not really exist in libobjc. */
363 #define TAG_MSGSEND_FAST "objc_msgSend_Fast"
364 #define TAG_ASSIGNIVAR_FAST "objc_assign_ivar_Fast"
366 #define TAG_CXX_CONSTRUCT ".cxx_construct"
367 #define TAG_CXX_DESTRUCT ".cxx_destruct"
369 /* GNU-specific tags. */
371 #define TAG_EXECCLASS "__objc_exec_class"
372 #define TAG_GNUINIT "__objc_gnu_init"
374 /* Flags for lookup_method_static(). */
375 #define OBJC_LOOKUP_CLASS 1 /* Look for class methods. */
376 #define OBJC_LOOKUP_NO_SUPER 2 /* Do not examine superclasses. */
378 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
379 tree objc_global_trees[OCTI_MAX];
381 static void handle_impent (struct imp_entry *);
383 struct imp_entry *imp_list = 0;
384 int imp_count = 0; /* `@implementation' */
385 int cat_count = 0; /* `@category' */
387 enum tree_code objc_inherit_code;
388 objc_ivar_visibility_kind objc_ivar_visibility;
390 /* Use to generate method labels. */
391 static int method_slot = 0;
393 /* Flag to say whether methods in a protocol are optional or
395 static bool objc_method_optional_flag = false;
397 static bool property_readonly;
398 static tree property_getter;
399 static tree property_setter;
400 static tree property_ivar;
401 static bool property_copies;
402 static bool in_objc_property_setter_name_context = false;
404 static int objc_collecting_ivars = 0;
408 static char *errbuf; /* Buffer for error diagnostics */
410 /* Data imported from tree.c. */
412 extern enum debug_info_type write_symbols;
414 /* Data imported from toplev.c. */
416 extern const char *dump_base_name;
418 static int flag_typed_selectors;
420 /* Store all constructed constant strings in a hash table so that
421 they get uniqued properly. */
423 struct GTY(()) string_descriptor {
424 /* The literal argument . */
427 /* The resulting constant string. */
431 static GTY((param_is (struct string_descriptor))) htab_t string_htab;
433 /* Store the EH-volatilized types in a hash table, for easy retrieval. */
434 struct GTY(()) volatilized_type {
438 static GTY((param_is (struct volatilized_type))) htab_t volatilized_htab;
440 FILE *gen_declaration_file;
442 /* Tells "encode_pointer/encode_aggregate" whether we are generating
443 type descriptors for instance variables (as opposed to methods).
444 Type descriptors for instance variables contain more information
445 than methods (for static typing and embedded structures). */
447 static int generating_instance_variables = 0;
449 /* For building an objc struct. These may not be used when this file
450 is compiled as part of obj-c++. */
452 static bool objc_building_struct;
453 static struct c_struct_parse_info *objc_struct_info ATTRIBUTE_UNUSED;
455 /* Start building a struct for objc. */
458 objc_start_struct (tree name)
460 gcc_assert (!objc_building_struct);
461 objc_building_struct = true;
462 return start_struct (input_location, RECORD_TYPE, name, &objc_struct_info);
465 /* Finish building a struct for objc. */
468 objc_finish_struct (tree type, tree fieldlist)
470 gcc_assert (objc_building_struct);
471 objc_building_struct = false;
472 return finish_struct (input_location, type, fieldlist, NULL_TREE,
477 build_sized_array_type (tree base_type, int size)
479 tree index_type = build_index_type (build_int_cst (NULL_TREE, size - 1));
480 return build_array_type (base_type, index_type);
484 add_field_decl (tree type, const char *name, tree **chain)
486 tree field = create_field_decl (type, name);
490 *chain = &DECL_CHAIN (field);
495 /* Some platforms pass small structures through registers versus
496 through an invisible pointer. Determine at what size structure is
497 the transition point between the two possibilities. */
500 generate_struct_by_value_array (void)
505 int aggregate_in_mem[32];
508 /* Presumably no platform passes 32 byte structures in a register. */
509 for (i = 1; i < 32; i++)
514 /* Create an unnamed struct that has `i' character components */
515 type = objc_start_struct (NULL_TREE);
517 strcpy (buffer, "c1");
518 decls = add_field_decl (char_type_node, buffer, &chain);
520 for (j = 1; j < i; j++)
522 sprintf (buffer, "c%d", j + 1);
523 add_field_decl (char_type_node, buffer, &chain);
525 objc_finish_struct (type, decls);
527 aggregate_in_mem[i] = aggregate_value_p (type, 0);
528 if (!aggregate_in_mem[i])
532 /* We found some structures that are returned in registers instead of memory
533 so output the necessary data. */
536 for (i = 31; i >= 0; i--)
537 if (!aggregate_in_mem[i])
539 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n\n", i);
541 /* The first member of the structure is always 0 because we don't handle
542 structures with 0 members */
543 printf ("static int struct_forward_array[] = {\n 0");
545 for (j = 1; j <= i; j++)
546 printf (", %d", aggregate_in_mem[j]);
553 /* FIXME: We need to intercept calls to warn_deprecated_use, since that
554 ultimately calls warning () with a "qD" formatter for decls. The 'D'
555 formatter does not handle ObjC-specific decls (in ObjC++). For now, we
556 interpose a switch to the default handler which simply prints the decl
558 Eventually, we should handle this within the objc{,p}/ code. */
561 objc_warn_deprecated_use (tree depitem, tree attr)
563 if (DECL_P (depitem))
565 static bool (*sav_printer) (pretty_printer *, text_info *, const char *,
566 int, bool, bool, bool) = NULL ;
567 if (sav_printer == NULL)
568 sav_printer = diagnostic_format_decoder (global_dc) ;
569 diagnostic_format_decoder (global_dc) = &default_tree_printer;
570 warn_deprecated_use (depitem, attr);
571 diagnostic_format_decoder (global_dc) = sav_printer;
574 warn_deprecated_use (depitem, attr);
581 if (cxx_init () == false)
583 if (c_objc_common_init () == false)
587 /* If gen_declaration desired, open the output file. */
588 if (flag_gen_declaration)
590 register char * const dumpname = concat (dump_base_name, ".decl", NULL);
591 gen_declaration_file = fopen (dumpname, "w");
592 if (gen_declaration_file == 0)
593 fatal_error ("can't open %s: %m", dumpname);
597 if (flag_next_runtime)
599 TAG_GETCLASS = "objc_getClass";
600 TAG_GETMETACLASS = "objc_getMetaClass";
601 TAG_MSGSEND = "objc_msgSend";
602 TAG_MSGSENDSUPER = "objc_msgSendSuper";
603 TAG_MSGSEND_STRET = "objc_msgSend_stret";
604 TAG_MSGSENDSUPER_STRET = "objc_msgSendSuper_stret";
605 default_constant_string_class_name = "NSConstantString";
609 TAG_GETCLASS = "objc_get_class";
610 TAG_GETMETACLASS = "objc_get_meta_class";
611 TAG_MSGSEND = "objc_msg_lookup";
612 TAG_MSGSENDSUPER = "objc_msg_lookup_super";
613 /* GNU runtime does not provide special functions to support
614 structure-returning methods. */
615 default_constant_string_class_name = "NXConstantString";
616 flag_typed_selectors = 1;
617 /* GNU runtime does not need the compiler to change code
618 in order to do GC. */
621 warning_at (0, 0, "%<-fobjc-gc%> is ignored for %<-fgnu-runtime%>");
628 if (print_struct_values && !flag_compare_debug)
629 generate_struct_by_value_array ();
635 objc_finish_file (void)
637 mark_referenced_methods ();
640 /* We need to instantiate templates _before_ we emit ObjC metadata;
641 if we do not, some metadata (such as selectors) may go missing. */
643 instantiate_pending_templates (0);
646 /* Finalize Objective-C runtime data. No need to generate tables
647 and code if only checking syntax, or if generating a PCH file. */
648 if (!flag_syntax_only && !pch_file)
651 if (gen_declaration_file)
652 fclose (gen_declaration_file);
655 /* Return the first occurrence of a method declaration corresponding
656 to sel_name in rproto_list. Search rproto_list recursively.
657 If is_class is 0, search for instance methods, otherwise for class
660 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
666 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
668 p = TREE_VALUE (rproto);
670 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
672 if ((fnd = lookup_method (is_class
673 ? PROTOCOL_CLS_METHODS (p)
674 : PROTOCOL_NST_METHODS (p), sel_name)))
676 else if (PROTOCOL_LIST (p))
677 fnd = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
682 ; /* An identifier...if we could not find a protocol. */
693 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
697 /* Make sure the protocol is supported by the object on the rhs. */
698 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
701 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
703 p = TREE_VALUE (rproto);
705 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
710 else if (PROTOCOL_LIST (p))
711 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
720 ; /* An identifier...if we could not find a protocol. */
727 objc_start_class_interface (tree klass, tree super_class,
728 tree protos, tree attributes)
731 warning_at (input_location, OPT_Wattributes,
732 "class attributes are not available in this version"
733 " of the compiler, (ignored)");
734 objc_interface_context
736 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos);
737 objc_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
741 objc_start_category_interface (tree klass, tree categ,
742 tree protos, tree attributes)
745 warning_at (input_location, OPT_Wattributes,
746 "category attributes are not available in this version"
747 " of the compiler, (ignored)");
748 objc_interface_context
749 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos);
751 = continue_class (objc_interface_context);
755 objc_start_protocol (tree name, tree protos, tree attributes)
758 warning_at (input_location, OPT_Wattributes,
759 "protocol attributes are not available in this version"
760 " of the compiler, (ignored)");
761 objc_interface_context
762 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos);
763 objc_method_optional_flag = false;
767 objc_continue_interface (void)
770 = continue_class (objc_interface_context);
774 objc_finish_interface (void)
776 finish_class (objc_interface_context);
777 objc_interface_context = NULL_TREE;
778 objc_method_optional_flag = false;
782 objc_start_class_implementation (tree klass, tree super_class)
784 objc_implementation_context
786 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE);
787 objc_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
791 objc_start_category_implementation (tree klass, tree categ)
793 objc_implementation_context
794 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE);
796 = continue_class (objc_implementation_context);
800 objc_continue_implementation (void)
803 = continue_class (objc_implementation_context);
807 objc_finish_implementation (void)
810 if (flag_objc_call_cxx_cdtors)
811 objc_generate_cxx_cdtors ();
814 if (objc_implementation_context)
816 finish_class (objc_implementation_context);
817 objc_ivar_chain = NULL_TREE;
818 objc_implementation_context = NULL_TREE;
821 warning (0, "%<@end%> must appear in an @implementation context");
825 objc_set_visibility (objc_ivar_visibility_kind visibility)
827 if (visibility == OBJC_IVAR_VIS_PACKAGE)
828 warning (0, "%<@package%> presently has the same effect as %<@public%>");
829 objc_ivar_visibility = visibility;
833 objc_set_method_opt (bool optional)
835 objc_method_optional_flag = optional;
836 if (!objc_interface_context
837 || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
839 error ("@optional/@required is allowed in @protocol context only.");
840 objc_method_optional_flag = false;
844 /* This routine gathers property attribute information from the attribute
845 portion of a property declaration. */
848 objc_set_property_attr (location_t loc, objc_property_attribute_kind attr,
851 static char string[BUFSIZE];
854 case OBJC_PATTR_INIT: /* init */
855 property_readonly = property_copies = false;
856 property_setter = property_getter = property_ivar = NULL_TREE;
858 case OBJC_PATTR_READONLY: /* readonly */
859 property_readonly = true;
861 case OBJC_PATTR_GETTER: /* getter = ident */
862 if (property_getter != NULL_TREE)
863 error_at (loc, "the %<getter%> attribute may only be specified once");
864 property_getter = ident;
866 case OBJC_PATTR_SETTER: /* setter = ident */
867 if (property_setter != NULL_TREE)
868 error_at (loc, "the %<setter%> attribute may only be specified once");
869 /* setters always have a trailing ':' in their name. In fact, this is the
870 only syntax that parser recognizes for a setter name. Must add a trailing
871 ':' here so name matches that of the declaration of user instance method
873 sprintf (string, "%s:", IDENTIFIER_POINTER (ident));
874 property_setter = get_identifier (string);;
876 case OBJC_PATTR_IVAR: /* ivar = ident */
877 if (property_ivar != NULL_TREE)
878 error_at (loc, "the %<ivar%> attribute may only be specified once");
879 else if (objc_interface_context)
881 warning_at (loc, 0, "the %<ivar%> attribute is ignored in an @interface");
882 property_ivar = NULL_TREE;
885 property_ivar = ident;
887 case OBJC_PATTR_COPIES: /* copies */
888 property_copies = true;
895 /* This routine builds a 'property_decl' tree node and adds it to the list
896 of such properties in the current class. It also checks for duplicates.
900 objc_add_property_variable (tree decl)
904 tree interface = NULL_TREE;
906 if (objc_implementation_context)
908 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
911 error ("no class property can be implemented without an interface");
914 if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE)
916 interface = lookup_category (interface,
917 CLASS_SUPER_NAME (objc_implementation_context));
920 error ("no category property can be implemented without an interface");
925 else if (!objc_interface_context)
927 fatal_error ("property declaration not in @interface or @implementation context");
931 property_decl = make_node (PROPERTY_DECL);
932 TREE_TYPE (property_decl) = TREE_TYPE (decl);
934 PROPERTY_NAME (property_decl) = DECL_NAME (decl);
935 PROPERTY_GETTER_NAME (property_decl) = property_getter;
936 PROPERTY_SETTER_NAME (property_decl) = property_setter;
937 PROPERTY_IVAR_NAME (property_decl) = property_ivar;
938 PROPERTY_READONLY (property_decl) = property_readonly
940 : boolean_false_node;
941 PROPERTY_COPIES (property_decl) = property_copies
943 : boolean_false_node;
945 if (objc_interface_context)
947 /* Doing the property in interface declaration. */
949 /* Issue error if property and an ivar name match. */
950 if (TREE_CODE (objc_interface_context) == CLASS_INTERFACE_TYPE
951 && is_ivar (CLASS_IVARS (objc_interface_context), DECL_NAME (decl)))
952 error ("property %qD may not have the same name as an ivar in the class", decl);
953 /* must check for duplicate property declarations. */
954 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
956 if (PROPERTY_NAME (x) == DECL_NAME (decl))
958 error ("duplicate property declaration %qD", decl);
962 TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context);
963 CLASS_PROPERTY_DECL (objc_interface_context) = property_decl;
967 /* Doing the property in implementation context. */
968 /* If property is not declared in the interface issue error. */
969 for (x = CLASS_PROPERTY_DECL (interface); x; x = TREE_CHAIN (x))
970 if (PROPERTY_NAME (x) == DECL_NAME (decl))
974 error ("no declaration of property %qD found in the interface", decl);
977 /* readonlys must also match. */
978 if (PROPERTY_READONLY (x) != PROPERTY_READONLY (property_decl))
980 error ("property %qD %<readonly%> attribute conflicts with its"
981 " interface version", decl);
983 /* copies must also match. */
984 if (PROPERTY_COPIES (x) != PROPERTY_COPIES (property_decl))
986 error ("property %qD %<copies%> attribute conflicts with its"
987 " interface version", decl);
989 /* Cannot have readonly and setter attribute for the same property. */
990 if (PROPERTY_READONLY (property_decl) == boolean_true_node &&
991 PROPERTY_SETTER_NAME (property_decl))
993 warning (0, "a %<readonly%> property cannot have a setter (ignored)");
994 PROPERTY_SETTER_NAME (property_decl) = NULL_TREE;
996 /* Add the property to the list of properties for current implementation. */
997 TREE_CHAIN (property_decl) = IMPL_PROPERTY_DECL (objc_implementation_context);
998 IMPL_PROPERTY_DECL (objc_implementation_context) = property_decl;
1002 /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
1006 lookup_property_in_list (tree chain, tree property)
1009 for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x))
1010 if (PROPERTY_NAME (x) == property)
1015 /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
1017 static tree lookup_property_in_protocol_list (tree rproto_list, tree property)
1020 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
1022 tree p = TREE_VALUE (rproto);
1023 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
1025 if ((x = lookup_property_in_list (p, property)))
1027 if (PROTOCOL_LIST (p))
1028 return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property);
1032 ; /* An identifier...if we could not find a protocol. */
1038 /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
1039 chain of interface hierarchy.
1042 lookup_property (tree interface_type, tree property)
1044 tree inter = interface_type;
1048 if ((x = lookup_property_in_list (inter, property)))
1050 /* Failing that, look for the property in each category of the class. */
1052 while ((category = CLASS_CATEGORY_LIST (category)))
1053 if ((x = lookup_property_in_list (category, property)))
1056 /* Failing to find in categories, look for property in protocol list. */
1057 if (CLASS_PROTOCOL_LIST (inter)
1058 && (x = lookup_property_in_protocol_list (
1059 CLASS_PROTOCOL_LIST (inter), property)))
1062 /* Failing that, climb up the inheritance hierarchy. */
1063 inter = lookup_interface (CLASS_SUPER_NAME (inter));
1068 /* This routine recognizes a dot-notation for a propery reference and generates a call to
1069 the getter function for this property. In all other cases, it returns a NULL_TREE.
1073 objc_build_getter_call (tree receiver, tree component)
1078 if (receiver == NULL_TREE
1079 || receiver == error_mark_node
1080 || (rtype = TREE_TYPE (receiver)) == NULL_TREE)
1083 if (component == NULL_TREE
1084 || component == error_mark_node
1085 || TREE_CODE (component) != IDENTIFIER_NODE)
1088 if (objc_is_id (rtype))
1090 tree rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
1091 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
1094 x = lookup_property_in_protocol_list (rprotos, component);
1098 tree basetype = TYPE_MAIN_VARIANT (rtype);
1100 if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE)
1101 basetype = TREE_TYPE (basetype);
1105 while (basetype != NULL_TREE
1106 && TREE_CODE (basetype) == RECORD_TYPE
1107 && OBJC_TYPE_NAME (basetype)
1108 && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL
1109 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)))
1110 basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype));
1112 if (basetype != NULL_TREE && TYPED_OBJECT (basetype))
1114 tree interface_type = TYPE_OBJC_INTERFACE (basetype);
1115 if (!interface_type)
1117 x = lookup_property (interface_type, component);
1123 tree call_exp, getter;
1124 /* Get the getter name. */
1125 gcc_assert (PROPERTY_NAME (x));
1126 getter = objc_finish_message_expr (receiver, PROPERTY_NAME (x),
1130 /* In C++, a getter which returns an aggregate value results in a
1131 target_expr which initializes a temporary to the call expression. */
1132 if (TREE_CODE (getter) == TARGET_EXPR)
1134 gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter)));
1135 gcc_assert (TREE_CODE (TREE_OPERAND (getter,0)) == VAR_DECL);
1136 call_exp = TREE_OPERAND (getter,1);
1139 gcc_assert (TREE_CODE (call_exp) == CALL_EXPR);
1141 CALL_EXPR_OBJC_PROPERTY_GETTER (call_exp) = 1;
1147 /* This routine builds a call to property's 'setter' function. RECEIVER is the
1148 receiving object for 'setter'. PROPERTY_IDENT is name of the property and
1149 RHS is the argument passed to the 'setter' function. */
1152 objc_setter_func_call (tree receiver, tree property_ident, tree rhs)
1154 tree setter_argument = build_tree_list (NULL_TREE, rhs);
1155 char *setter_name = objc_build_property_setter_name (property_ident, true);
1157 in_objc_property_setter_name_context = true;
1158 setter = objc_finish_message_expr (receiver, get_identifier (setter_name),
1160 in_objc_property_setter_name_context = false;
1164 /* Find the selector identifier from a reference. A somewhat tortuous way of
1165 obtaining the information to allow a setter to be written, given an
1169 get_selector_from_reference (tree selref)
1173 if (flag_next_runtime)
1175 /* Run through the selectors until we find the one we're looking for. */
1176 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
1177 if (TREE_PURPOSE (chain) == selref)
1178 return TREE_VALUE (chain);
1182 /* To find our way back to the selector for the GNU runtime is harder
1183 work, we need to decompose the representation of SELECTOR_TABLE[n]
1184 to find 'n'. This representation is in several forms. */
1185 if (TREE_CODE (selref) == POINTER_PLUS_EXPR)
1187 /* We need the element size to decode the array offset expression
1189 unsigned size = (unsigned) TREE_INT_CST_LOW
1195 (TREE_OPERAND (selref, 0), 0), 0)))));
1197 (unsigned) TREE_INT_CST_LOW (TREE_OPERAND (selref, 1))
1199 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
1201 return TREE_VALUE (chain);
1203 else if (TREE_CODE (selref) == NOP_EXPR)
1205 /* Either we have a base an index, or we have just a base (when the
1207 if (TREE_CODE (TREE_OPERAND (selref, 0)) == ADDR_EXPR
1210 (TREE_OPERAND (selref, 0), 0)) == ARRAY_REF)
1213 unsigned index = (unsigned) TREE_INT_CST_LOW
1216 (TREE_OPERAND (selref, 0), 0), 1));
1217 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
1219 return TREE_VALUE (chain);
1222 return TREE_VALUE (sel_ref_chain);
1223 } /* Else we don't know how to figure this out - which will produce a
1224 parse error - saying that the LHS is not writeable. */
1229 /* This routine converts a previously synthesized 'getter' function call for
1230 a property and converts it to a 'setter' function call for the same
1234 objc_build_setter_call (tree lhs, tree rhs)
1237 && TREE_CODE (lhs) == CALL_EXPR
1238 && CALL_EXPR_OBJC_PROPERTY_GETTER (lhs))
1241 /* Get the Object. */
1242 tree receiver = TREE_OPERAND (lhs, 3);
1243 /* Get the selector reference. */
1244 tree selector_reference = TREE_OPERAND (lhs, 4);
1245 gcc_assert (receiver && selector_reference);
1246 /* The style of the selector reference is different for GNU & NeXT. */
1247 selector = get_selector_from_reference (selector_reference);
1249 return objc_setter_func_call (receiver, selector, rhs);
1254 /* This routine checks to see if ID is a property name. If so, it
1255 returns property declaration. */
1258 is_property (tree klass, tree id)
1262 for (x = CLASS_PROPERTY_DECL (klass); x; x = TREE_CHAIN (x))
1263 if (PROPERTY_NAME (x) == id)
1268 /* This routine returns call to property's getter when a property is
1269 used stand-alone (without self. notation). */
1272 build_property_reference (tree property, tree id)
1275 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
1277 error ("property %qs accessed in class method",
1278 IDENTIFIER_POINTER (id));
1279 return error_mark_node;
1282 getter = objc_finish_message_expr (self_decl, PROPERTY_NAME (property), NULL_TREE);
1283 CALL_EXPR_OBJC_PROPERTY_GETTER (getter) = 1;
1288 objc_set_method_type (enum tree_code type)
1290 objc_inherit_code = (type == PLUS_EXPR
1292 : INSTANCE_METHOD_DECL);
1296 objc_build_method_signature (tree rettype, tree selector,
1297 tree optparms, bool ellipsis)
1299 return build_method_decl (objc_inherit_code, rettype, selector,
1300 optparms, ellipsis);
1304 objc_add_method_declaration (tree decl, tree attributes)
1306 if (!objc_interface_context)
1308 /* PS: At the moment, due to how the parser works, it should be
1309 impossible to get here. But it's good to have the check in
1310 case the parser changes.
1312 fatal_error ("method declaration not in @interface context");
1315 objc_decl_method_attributes (&decl, attributes, 0);
1316 objc_add_method (objc_interface_context,
1318 objc_inherit_code == CLASS_METHOD_DECL,
1319 objc_method_optional_flag);
1322 /* Return 'true' if the method definition could be started, and
1323 'false' if not (because we are outside an @implementation context).
1326 objc_start_method_definition (tree decl, tree attributes)
1328 if (!objc_implementation_context)
1330 error ("method definition not in @implementation context");
1334 if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node)
1338 /* Indicate no valid break/continue context by setting these variables
1339 to some non-null, non-label value. We'll notice and emit the proper
1340 error message in c_finish_bc_stmt. */
1341 c_break_label = c_cont_label = size_zero_node;
1344 objc_decl_method_attributes (&decl, attributes, 0);
1345 objc_add_method (objc_implementation_context,
1347 objc_inherit_code == CLASS_METHOD_DECL,
1348 /* is optional */ false);
1349 start_method_def (decl);
1354 objc_add_instance_variable (tree decl)
1356 (void) add_instance_variable (objc_ivar_context,
1357 objc_ivar_visibility,
1361 /* Return true if TYPE is 'id'. */
1364 objc_is_object_id (tree type)
1366 return OBJC_TYPE_NAME (type) == objc_object_id;
1370 objc_is_class_id (tree type)
1372 return OBJC_TYPE_NAME (type) == objc_class_id;
1375 /* Construct a C struct with same name as KLASS, a base struct with tag
1376 SUPER_NAME (if any), and FIELDS indicated. */
1379 objc_build_struct (tree klass, tree fields, tree super_name)
1381 tree name = CLASS_NAME (klass);
1382 tree s = objc_start_struct (name);
1383 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
1385 VEC(tree,heap) *objc_info = NULL;
1390 /* Prepend a packed variant of the base class into the layout. This
1391 is necessary to preserve ObjC ABI compatibility. */
1392 tree base = build_decl (input_location,
1393 FIELD_DECL, NULL_TREE, super);
1394 tree field = TYPE_FIELDS (super);
1396 while (field && DECL_CHAIN (field)
1397 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
1398 field = DECL_CHAIN (field);
1400 /* For ObjC ABI purposes, the "packed" size of a base class is
1401 the sum of the offset and the size (in bits) of the last field
1404 = (field && TREE_CODE (field) == FIELD_DECL
1405 ? size_binop (PLUS_EXPR,
1406 size_binop (PLUS_EXPR,
1409 convert (bitsizetype,
1410 DECL_FIELD_OFFSET (field)),
1411 bitsize_int (BITS_PER_UNIT)),
1412 DECL_FIELD_BIT_OFFSET (field)),
1414 : bitsize_zero_node);
1415 DECL_SIZE_UNIT (base)
1416 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
1417 size_int (BITS_PER_UNIT));
1418 DECL_ARTIFICIAL (base) = 1;
1419 DECL_ALIGN (base) = 1;
1420 DECL_FIELD_CONTEXT (base) = s;
1422 DECL_FIELD_IS_BASE (base) = 1;
1425 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
1426 #endif /* are following the ObjC ABI here. */
1427 DECL_CHAIN (base) = fields;
1431 /* NB: Calling finish_struct() may cause type TYPE_LANG_SPECIFIC fields
1432 in all variants of this RECORD_TYPE to be clobbered, but it is therein
1433 that we store protocol conformance info (e.g., 'NSObject <MyProtocol>').
1434 Hence, we must squirrel away the ObjC-specific information before calling
1435 finish_struct(), and then reinstate it afterwards. */
1437 for (t = TYPE_NEXT_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
1439 if (!TYPE_HAS_OBJC_INFO (t))
1441 INIT_TYPE_OBJC_INFO (t);
1442 TYPE_OBJC_INTERFACE (t) = klass;
1444 VEC_safe_push (tree, heap, objc_info, TYPE_OBJC_INFO (t));
1447 /* Point the struct at its related Objective-C class. */
1448 INIT_TYPE_OBJC_INFO (s);
1449 TYPE_OBJC_INTERFACE (s) = klass;
1451 s = objc_finish_struct (s, fields);
1453 for (i = 0, t = TYPE_NEXT_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
1455 TYPE_OBJC_INFO (t) = VEC_index (tree, objc_info, i);
1456 /* Replace the IDENTIFIER_NODE with an actual @interface. */
1457 TYPE_OBJC_INTERFACE (t) = klass;
1459 VEC_free (tree, heap, objc_info);
1461 /* Use TYPE_BINFO structures to point at the super class, if any. */
1462 objc_xref_basetypes (s, super);
1464 /* Mark this struct as a class template. */
1465 CLASS_STATIC_TEMPLATE (klass) = s;
1470 /* Build a type differing from TYPE only in that TYPE_VOLATILE is set.
1471 Unlike tree.c:build_qualified_type(), preserve TYPE_LANG_SPECIFIC in the
1474 objc_build_volatilized_type (tree type)
1478 /* Check if we have not constructed the desired variant already. */
1479 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1481 /* The type qualifiers must (obviously) match up. */
1482 if (!TYPE_VOLATILE (t)
1483 || (TYPE_READONLY (t) != TYPE_READONLY (type))
1484 || (TYPE_RESTRICT (t) != TYPE_RESTRICT (type)))
1487 /* For pointer types, the pointees (and hence their TYPE_LANG_SPECIFIC
1488 info, if any) must match up. */
1489 if (POINTER_TYPE_P (t)
1490 && (TREE_TYPE (t) != TREE_TYPE (type)))
1493 /* Everything matches up! */
1497 /* Ok, we could not re-use any of the pre-existing variants. Create
1499 t = build_variant_type_copy (type);
1500 TYPE_VOLATILE (t) = 1;
1502 /* Set up the canonical type information. */
1503 if (TYPE_STRUCTURAL_EQUALITY_P (type))
1504 SET_TYPE_STRUCTURAL_EQUALITY (t);
1505 else if (TYPE_CANONICAL (type) != type)
1506 TYPE_CANONICAL (t) = objc_build_volatilized_type (TYPE_CANONICAL (type));
1508 TYPE_CANONICAL (t) = t;
1513 /* Mark DECL as being 'volatile' for purposes of Darwin
1514 _setjmp()/_longjmp() exception handling. Called from
1515 objc_mark_locals_volatile(). */
1517 objc_volatilize_decl (tree decl)
1519 /* Do not mess with variables that are 'static' or (already)
1521 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
1522 && (TREE_CODE (decl) == VAR_DECL
1523 || TREE_CODE (decl) == PARM_DECL))
1525 tree t = TREE_TYPE (decl);
1526 struct volatilized_type key;
1529 t = objc_build_volatilized_type (t);
1531 loc = htab_find_slot (volatilized_htab, &key, INSERT);
1535 *loc = ggc_alloc_volatilized_type ();
1536 ((struct volatilized_type *) *loc)->type = t;
1539 TREE_TYPE (decl) = t;
1540 TREE_THIS_VOLATILE (decl) = 1;
1541 TREE_SIDE_EFFECTS (decl) = 1;
1542 DECL_REGISTER (decl) = 0;
1544 C_DECL_REGISTER (decl) = 0;
1549 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
1550 (including its categories and superclasses) or by object type TYP.
1551 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
1554 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
1556 bool class_type = (cls != NULL_TREE);
1562 /* Check protocols adopted by the class and its categories. */
1563 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
1565 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
1569 /* Repeat for superclasses. */
1570 cls = lookup_interface (CLASS_SUPER_NAME (cls));
1573 /* Check for any protocols attached directly to the object type. */
1574 if (TYPE_HAS_OBJC_INFO (typ))
1576 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
1583 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
1584 /* NB: Types 'id' and 'Class' cannot reasonably be described as
1585 "implementing" a given protocol, since they do not have an
1588 warning (0, "class %qs does not implement the %qE protocol",
1589 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
1591 warning (0, "type %qs does not conform to the %qE protocol",
1592 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
1598 /* Check if class RCLS and instance struct type RTYP conform to at least the
1599 same protocols that LCLS and LTYP conform to. */
1602 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
1605 bool have_lproto = false;
1609 /* NB: We do _not_ look at categories defined for LCLS; these may or
1610 may not get loaded in, and therefore it is unreasonable to require
1611 that RCLS/RTYP must implement any of their protocols. */
1612 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
1616 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
1620 /* Repeat for superclasses. */
1621 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
1624 /* Check for any protocols attached directly to the object type. */
1625 if (TYPE_HAS_OBJC_INFO (ltyp))
1627 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
1631 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
1636 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
1637 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
1638 away with simply checking for 'id' or 'Class' (!RCLS), since this
1639 routine will not get called in other cases. */
1640 return have_lproto || (rcls != NULL_TREE);
1643 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
1644 Both TYPE1 and TYPE2 must be pointers, and already determined to be
1645 compatible by objc_compare_types() below. */
1648 objc_common_type (tree type1, tree type2)
1650 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
1652 while (POINTER_TYPE_P (inner1))
1654 inner1 = TREE_TYPE (inner1);
1655 inner2 = TREE_TYPE (inner2);
1658 /* If one type is derived from another, return the base type. */
1659 if (DERIVED_FROM_P (inner1, inner2))
1661 else if (DERIVED_FROM_P (inner2, inner1))
1664 /* If both types are 'Class', return 'Class'. */
1665 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
1666 return objc_class_type;
1668 /* Otherwise, return 'id'. */
1669 return objc_object_type;
1672 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
1673 an instance of RTYP to an instance of LTYP or to compare the two
1674 (if ARGNO is equal to -3), per ObjC type system rules. Before
1675 returning 'true', this routine may issue warnings related to, e.g.,
1676 protocol conformance. When returning 'false', the routine must
1677 produce absolutely no warnings; the C or C++ front-end will do so
1678 instead, if needed. If either LTYP or RTYP is not an Objective-C type,
1679 the routine must return 'false'.
1681 The ARGNO parameter is encoded as follows:
1682 >= 1 Parameter number (CALLEE contains function being called);
1686 -3 Comparison (LTYP and RTYP may match in either direction);
1687 -4 Silent comparison (for C++ overload resolution).
1691 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
1693 tree lcls, rcls, lproto, rproto;
1694 bool pointers_compatible;
1696 /* We must be dealing with pointer types */
1697 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
1702 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
1703 rtyp = TREE_TYPE (rtyp);
1705 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
1707 /* We must also handle function pointers, since ObjC is a bit more
1708 lenient than C or C++ on this. */
1709 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
1711 /* Return types must be covariant. */
1712 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
1713 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
1717 /* Argument types must be contravariant. */
1718 for (ltyp = TYPE_ARG_TYPES (ltyp), rtyp = TYPE_ARG_TYPES (rtyp);
1719 ltyp && rtyp; ltyp = TREE_CHAIN (ltyp), rtyp = TREE_CHAIN (rtyp))
1721 if (!comptypes (TREE_VALUE (rtyp), TREE_VALUE (ltyp))
1722 && !objc_compare_types (TREE_VALUE (rtyp), TREE_VALUE (ltyp),
1727 return (ltyp == rtyp);
1730 /* Past this point, we are only interested in ObjC class instances,
1731 or 'id' or 'Class'. */
1732 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
1735 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
1736 && !TYPE_HAS_OBJC_INFO (ltyp))
1739 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
1740 && !TYPE_HAS_OBJC_INFO (rtyp))
1743 /* Past this point, we are committed to returning 'true' to the caller
1744 (unless performing a silent comparison; see below). However, we can
1745 still warn about type and/or protocol mismatches. */
1747 if (TYPE_HAS_OBJC_INFO (ltyp))
1749 lcls = TYPE_OBJC_INTERFACE (ltyp);
1750 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
1753 lcls = lproto = NULL_TREE;
1755 if (TYPE_HAS_OBJC_INFO (rtyp))
1757 rcls = TYPE_OBJC_INTERFACE (rtyp);
1758 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
1761 rcls = rproto = NULL_TREE;
1763 /* If we could not find an @interface declaration, we must have
1764 only seen a @class declaration; for purposes of type comparison,
1765 treat it as a stand-alone (root) class. */
1767 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
1770 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
1773 /* If either type is an unqualified 'id', we're done. */
1774 if ((!lproto && objc_is_object_id (ltyp))
1775 || (!rproto && objc_is_object_id (rtyp)))
1778 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
1780 /* If the underlying types are the same, and at most one of them has
1781 a protocol list, we do not need to issue any diagnostics. */
1782 if (pointers_compatible && (!lproto || !rproto))
1785 /* If exactly one of the types is 'Class', issue a diagnostic; any
1786 exceptions of this rule have already been handled. */
1787 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
1788 pointers_compatible = false;
1789 /* Otherwise, check for inheritance relations. */
1792 if (!pointers_compatible)
1794 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
1796 if (!pointers_compatible)
1797 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
1799 if (!pointers_compatible && argno <= -3)
1800 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
1803 /* If the pointers match modulo protocols, check for protocol conformance
1805 if (pointers_compatible)
1807 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
1810 if (!pointers_compatible && argno == -3)
1811 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
1815 if (!pointers_compatible)
1817 /* The two pointers are not exactly compatible. Issue a warning, unless
1818 we are performing a silent comparison, in which case return 'false'
1820 /* NB: For the time being, we shall make our warnings look like their
1821 C counterparts. In the future, we may wish to make them more
1829 warning (0, "comparison of distinct Objective-C types lacks a cast");
1833 warning (0, "initialization from distinct Objective-C type");
1837 warning (0, "assignment from distinct Objective-C type");
1841 warning (0, "distinct Objective-C type in return");
1845 warning (0, "passing argument %d of %qE from distinct "
1846 "Objective-C type", argno, callee);
1854 /* This routine is similar to objc_compare_types except that function-pointers are
1855 excluded. This is because, caller assumes that common types are of (id, Object*)
1856 variety and calls objc_common_type to obtain a common type. There is no commonolty
1857 between two function-pointers in this regard. */
1860 objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
1862 if (objc_compare_types (ltyp, rtyp, argno, callee))
1864 /* exclude function-pointer types. */
1867 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
1868 rtyp = TREE_TYPE (rtyp);
1870 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
1871 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
1876 /* Check if LTYP and RTYP have the same type qualifiers. If either type
1877 lives in the volatilized hash table, ignore the 'volatile' bit when
1878 making the comparison. */
1881 objc_type_quals_match (tree ltyp, tree rtyp)
1883 int lquals = TYPE_QUALS (ltyp), rquals = TYPE_QUALS (rtyp);
1884 struct volatilized_type key;
1888 if (htab_find_slot (volatilized_htab, &key, NO_INSERT))
1889 lquals &= ~TYPE_QUAL_VOLATILE;
1893 if (htab_find_slot (volatilized_htab, &key, NO_INSERT))
1894 rquals &= ~TYPE_QUAL_VOLATILE;
1896 return (lquals == rquals);
1900 /* Determine if CHILD is derived from PARENT. The routine assumes that
1901 both parameters are RECORD_TYPEs, and is non-reflexive. */
1904 objc_derived_from_p (tree parent, tree child)
1906 parent = TYPE_MAIN_VARIANT (parent);
1908 for (child = TYPE_MAIN_VARIANT (child);
1909 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
1911 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
1912 (TYPE_BINFO (child),
1915 if (child == parent)
1924 objc_build_component_ref (tree datum, tree component)
1926 /* If COMPONENT is NULL, the caller is referring to the anonymous
1927 base class field. */
1930 tree base = TYPE_FIELDS (TREE_TYPE (datum));
1932 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
1935 /* The 'build_component_ref' routine has been removed from the C++
1936 front-end, but 'finish_class_member_access_expr' seems to be
1937 a worthy substitute. */
1939 return finish_class_member_access_expr (datum, component, false,
1940 tf_warning_or_error);
1942 return build_component_ref (input_location, datum, component);
1946 /* Recursively copy inheritance information rooted at BINFO. To do this,
1947 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
1950 objc_copy_binfo (tree binfo)
1952 tree btype = BINFO_TYPE (binfo);
1953 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
1957 BINFO_TYPE (binfo2) = btype;
1958 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
1959 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
1961 /* Recursively copy base binfos of BINFO. */
1962 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1964 tree base_binfo2 = objc_copy_binfo (base_binfo);
1966 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
1967 BINFO_BASE_APPEND (binfo2, base_binfo2);
1973 /* Record superclass information provided in BASETYPE for ObjC class REF.
1974 This is loosely based on cp/decl.c:xref_basetypes(). */
1977 objc_xref_basetypes (tree ref, tree basetype)
1979 tree binfo = make_tree_binfo (basetype ? 1 : 0);
1981 TYPE_BINFO (ref) = binfo;
1982 BINFO_OFFSET (binfo) = size_zero_node;
1983 BINFO_TYPE (binfo) = ref;
1987 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
1989 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
1990 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, 1);
1991 BINFO_BASE_APPEND (binfo, base_binfo);
1992 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
1997 volatilized_hash (const void *ptr)
1999 const_tree const typ = ((const struct volatilized_type *)ptr)->type;
2001 return htab_hash_pointer(typ);
2005 volatilized_eq (const void *ptr1, const void *ptr2)
2007 const_tree const typ1 = ((const struct volatilized_type *)ptr1)->type;
2008 const_tree const typ2 = ((const struct volatilized_type *)ptr2)->type;
2010 return typ1 == typ2;
2013 /* Called from finish_decl. */
2016 objc_check_decl (tree decl)
2018 tree type = TREE_TYPE (decl);
2020 if (TREE_CODE (type) != RECORD_TYPE)
2022 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
2023 error ("statically allocated instance of Objective-C class %qE",
2028 objc_check_global_decl (tree decl)
2030 tree id = DECL_NAME (decl);
2031 if (objc_is_class_name (id) && global_bindings_p())
2032 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id));
2035 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where INTERFACE may
2036 either name an Objective-C class, or refer to the special 'id' or 'Class'
2037 types. If INTERFACE is not a valid ObjC type, just return it unchanged. */
2040 objc_get_protocol_qualified_type (tree interface, tree protocols)
2042 /* If INTERFACE is not provided, default to 'id'. */
2043 tree type = (interface ? objc_is_id (interface) : objc_object_type);
2044 bool is_ptr = (type != NULL_TREE);
2048 type = objc_is_class_name (interface);
2052 /* If looking at a typedef, retrieve the precise type it
2054 if (TREE_CODE (interface) == IDENTIFIER_NODE)
2055 interface = identifier_global_value (interface);
2057 type = ((interface && TREE_CODE (interface) == TYPE_DECL
2058 && DECL_ORIGINAL_TYPE (interface))
2059 ? DECL_ORIGINAL_TYPE (interface)
2060 : xref_tag (RECORD_TYPE, type));
2068 type = build_variant_type_copy (type);
2070 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
2074 tree orig_pointee_type = TREE_TYPE (type);
2075 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
2077 /* Set up the canonical type information. */
2078 TYPE_CANONICAL (type)
2079 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
2081 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
2082 type = TREE_TYPE (type);
2085 /* Look up protocols and install in lang specific list. */
2086 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
2087 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols (protocols);
2089 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
2090 return the pointer to the new pointee variant. */
2092 type = TYPE_POINTER_TO (type);
2094 TYPE_OBJC_INTERFACE (type)
2095 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
2101 /* Check for circular dependencies in protocols. The arguments are
2102 PROTO, the protocol to check, and LIST, a list of protocol it
2106 check_protocol_recursively (tree proto, tree list)
2110 for (p = list; p; p = TREE_CHAIN (p))
2112 tree pp = TREE_VALUE (p);
2114 if (TREE_CODE (pp) == IDENTIFIER_NODE)
2115 pp = lookup_protocol (pp);
2118 fatal_error ("protocol %qE has circular dependency",
2119 PROTOCOL_NAME (pp));
2121 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
2125 /* Look up PROTOCOLS, and return a list of those that are found.
2126 If none are found, return NULL. */
2129 lookup_and_install_protocols (tree protocols)
2132 tree return_value = NULL_TREE;
2134 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
2136 tree ident = TREE_VALUE (proto);
2137 tree p = lookup_protocol (ident);
2140 return_value = chainon (return_value,
2141 build_tree_list (NULL_TREE, p));
2142 else if (ident != error_mark_node)
2143 error ("cannot find protocol declaration for %qE",
2147 return return_value;
2150 /* Create a declaration for field NAME of a given TYPE. */
2153 create_field_decl (tree type, const char *name)
2155 return build_decl (input_location,
2156 FIELD_DECL, get_identifier (name), type);
2159 /* Create a global, static declaration for variable NAME of a given TYPE. The
2160 finish_var_decl() routine will need to be called on it afterwards. */
2163 start_var_decl (tree type, const char *name)
2165 tree var = build_decl (input_location,
2166 VAR_DECL, get_identifier (name), type);
2168 TREE_STATIC (var) = 1;
2169 DECL_INITIAL (var) = error_mark_node; /* A real initializer is coming... */
2170 DECL_IGNORED_P (var) = 1;
2171 DECL_ARTIFICIAL (var) = 1;
2172 DECL_CONTEXT (var) = NULL_TREE;
2174 DECL_THIS_STATIC (var) = 1; /* squash redeclaration errors */
2180 /* Finish off the variable declaration created by start_var_decl(). */
2183 finish_var_decl (tree var, tree initializer)
2185 finish_decl (var, input_location, initializer, NULL_TREE, NULL_TREE);
2188 /* Find the decl for the constant string class reference. This is only
2189 used for the NeXT runtime. */
2192 setup_string_decl (void)
2197 /* %s in format will provide room for terminating null */
2198 length = strlen (STRING_OBJECT_GLOBAL_FORMAT)
2199 + strlen (constant_string_class_name);
2200 name = XNEWVEC (char, length);
2201 sprintf (name, STRING_OBJECT_GLOBAL_FORMAT,
2202 constant_string_class_name);
2203 constant_string_global_id = get_identifier (name);
2204 string_class_decl = lookup_name (constant_string_global_id);
2206 return string_class_decl;
2209 /* Purpose: "play" parser, creating/installing representations
2210 of the declarations that are required by Objective-C.
2214 type_spec--------->sc_spec
2215 (tree_list) (tree_list)
2218 identifier_node identifier_node */
2221 synth_module_prologue (void)
2224 enum debug_info_type save_write_symbols = write_symbols;
2225 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
2227 /* Suppress outputting debug symbols, because
2228 dbxout_init hasn't been called yet. */
2229 write_symbols = NO_DEBUG;
2230 debug_hooks = &do_nothing_debug_hooks;
2233 push_lang_context (lang_name_c); /* extern "C" */
2236 /* The following are also defined in <objc/objc.h> and friends. */
2238 objc_object_id = get_identifier (TAG_OBJECT);
2239 objc_class_id = get_identifier (TAG_CLASS);
2241 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
2242 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
2244 objc_object_type = build_pointer_type (objc_object_reference);
2245 objc_class_type = build_pointer_type (objc_class_reference);
2247 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
2248 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
2250 /* Declare the 'id' and 'Class' typedefs. */
2252 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2256 TREE_NO_WARNING (type) = 1;
2257 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2261 TREE_NO_WARNING (type) = 1;
2263 /* Forward-declare '@interface Protocol'. */
2265 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
2266 objc_declare_class (tree_cons (NULL_TREE, type, NULL_TREE));
2267 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE,
2270 /* Declare type of selector-objects that represent an operation name. */
2272 if (flag_next_runtime)
2273 /* `struct objc_selector *' */
2275 = build_pointer_type (xref_tag (RECORD_TYPE,
2276 get_identifier (TAG_SELECTOR)));
2278 /* `const struct objc_selector *' */
2280 = build_pointer_type
2281 (build_qualified_type (xref_tag (RECORD_TYPE,
2282 get_identifier (TAG_SELECTOR)),
2285 /* Declare receiver type used for dispatching messages to 'super'. */
2287 /* `struct objc_super *' */
2288 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
2289 get_identifier (TAG_SUPER)));
2291 /* Declare pointers to method and ivar lists. */
2292 objc_method_list_ptr = build_pointer_type
2293 (xref_tag (RECORD_TYPE,
2294 get_identifier (UTAG_METHOD_LIST)));
2295 objc_method_proto_list_ptr
2296 = build_pointer_type (xref_tag (RECORD_TYPE,
2297 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
2298 objc_ivar_list_ptr = build_pointer_type
2299 (xref_tag (RECORD_TYPE,
2300 get_identifier (UTAG_IVAR_LIST)));
2302 /* TREE_NOTHROW is cleared for the message-sending functions,
2303 because the function that gets called can throw in Obj-C++, or
2304 could itself call something that can throw even in Obj-C. */
2306 if (flag_next_runtime)
2308 /* NB: In order to call one of the ..._stret (struct-returning)
2309 functions, the function *MUST* first be cast to a signature that
2310 corresponds to the actual ObjC method being invoked. This is
2311 what is done by the build_objc_method_call() routine below. */
2313 /* id objc_msgSend (id, SEL, ...); */
2314 /* id objc_msgSendNonNil (id, SEL, ...); */
2315 /* id objc_msgSend_stret (id, SEL, ...); */
2316 /* id objc_msgSendNonNil_stret (id, SEL, ...); */
2318 = build_varargs_function_type_list (objc_object_type,
2322 umsg_decl = add_builtin_function (TAG_MSGSEND,
2323 type, 0, NOT_BUILT_IN,
2325 umsg_nonnil_decl = add_builtin_function (TAG_MSGSEND_NONNIL,
2326 type, 0, NOT_BUILT_IN,
2328 umsg_stret_decl = add_builtin_function (TAG_MSGSEND_STRET,
2329 type, 0, NOT_BUILT_IN,
2331 umsg_nonnil_stret_decl = add_builtin_function (TAG_MSGSEND_NONNIL_STRET,
2332 type, 0, NOT_BUILT_IN,
2335 /* These can throw, because the function that gets called can throw
2336 in Obj-C++, or could itself call something that can throw even
2338 TREE_NOTHROW (umsg_decl) = 0;
2339 TREE_NOTHROW (umsg_nonnil_decl) = 0;
2340 TREE_NOTHROW (umsg_stret_decl) = 0;
2341 TREE_NOTHROW (umsg_nonnil_stret_decl) = 0;
2343 /* id objc_msgSend_Fast (id, SEL, ...)
2344 __attribute__ ((hard_coded_address (OFFS_MSGSEND_FAST))); */
2345 #ifdef OFFS_MSGSEND_FAST
2346 umsg_fast_decl = add_builtin_function (TAG_MSGSEND_FAST,
2347 type, 0, NOT_BUILT_IN,
2349 TREE_NOTHROW (umsg_fast_decl) = 0;
2350 DECL_ATTRIBUTES (umsg_fast_decl)
2351 = tree_cons (get_identifier ("hard_coded_address"),
2352 build_int_cst (NULL_TREE, OFFS_MSGSEND_FAST),
2355 /* No direct dispatch available. */
2356 umsg_fast_decl = umsg_decl;
2359 /* id objc_msgSendSuper (struct objc_super *, SEL, ...); */
2360 /* id objc_msgSendSuper_stret (struct objc_super *, SEL, ...); */
2362 = build_varargs_function_type_list (objc_object_type,
2366 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
2367 type, 0, NOT_BUILT_IN,
2369 umsg_super_stret_decl = add_builtin_function (TAG_MSGSENDSUPER_STRET,
2370 type, 0, NOT_BUILT_IN, 0,
2372 TREE_NOTHROW (umsg_super_decl) = 0;
2373 TREE_NOTHROW (umsg_super_stret_decl) = 0;
2377 /* GNU runtime messenger entry points. */
2379 /* typedef id (*IMP)(id, SEL, ...); */
2381 build_varargs_function_type_list (objc_object_type,
2385 tree IMP_type = build_pointer_type (ftype);
2387 /* IMP objc_msg_lookup (id, SEL); */
2388 type = build_function_type_list (IMP_type,
2392 umsg_decl = add_builtin_function (TAG_MSGSEND,
2393 type, 0, NOT_BUILT_IN,
2395 TREE_NOTHROW (umsg_decl) = 0;
2397 /* IMP objc_msg_lookup_super (struct objc_super *, SEL); */
2399 = build_function_type_list (IMP_type,
2403 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
2404 type, 0, NOT_BUILT_IN,
2406 TREE_NOTHROW (umsg_super_decl) = 0;
2408 /* The following GNU runtime entry point is called to initialize
2411 __objc_exec_class (void *); */
2413 = build_function_type_list (void_type_node,
2416 execclass_decl = add_builtin_function (TAG_EXECCLASS,
2417 type, 0, NOT_BUILT_IN,
2421 /* id objc_getClass (const char *); */
2423 type = build_function_type_list (objc_object_type,
2424 const_string_type_node,
2428 = add_builtin_function (TAG_GETCLASS, type, 0, NOT_BUILT_IN,
2431 /* id objc_getMetaClass (const char *); */
2433 objc_get_meta_class_decl
2434 = add_builtin_function (TAG_GETMETACLASS, type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
2436 build_class_template ();
2437 build_super_template ();
2438 build_protocol_template ();
2439 build_category_template ();
2440 build_objc_exception_stuff ();
2442 if (flag_next_runtime)
2443 build_next_objc_exception_stuff ();
2445 /* static SEL _OBJC_SELECTOR_TABLE[]; */
2447 if (! flag_next_runtime)
2448 build_selector_table_decl ();
2450 /* Forward declare constant_string_id and constant_string_type. */
2451 if (!constant_string_class_name)
2452 constant_string_class_name = default_constant_string_class_name;
2454 constant_string_id = get_identifier (constant_string_class_name);
2455 objc_declare_class (tree_cons (NULL_TREE, constant_string_id, NULL_TREE));
2457 /* Pre-build the following entities - for speed/convenience. */
2458 self_id = get_identifier ("self");
2459 ucmd_id = get_identifier ("_cmd");
2461 /* Declare struct _objc_fast_enumeration_state { ... }; */
2462 build_fast_enumeration_state_template ();
2464 /* void objc_enumeration_mutation (id) */
2465 type = build_function_type (void_type_node,
2466 tree_cons (NULL_TREE, objc_object_type, NULL_TREE));
2467 objc_enumeration_mutation_decl
2468 = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN,
2470 TREE_NOTHROW (objc_enumeration_mutation_decl) = 0;
2473 pop_lang_context ();
2476 write_symbols = save_write_symbols;
2477 debug_hooks = save_hooks;
2480 /* Ensure that the ivar list for NSConstantString/NXConstantString
2481 (or whatever was specified via `-fconstant-string-class')
2482 contains fields at least as large as the following three, so that
2483 the runtime can stomp on them with confidence:
2485 struct STRING_OBJECT_CLASS_NAME
2489 unsigned int length;
2493 check_string_class_template (void)
2495 tree field_decl = objc_get_class_ivars (constant_string_id);
2497 #define AT_LEAST_AS_LARGE_AS(F, T) \
2498 (F && TREE_CODE (F) == FIELD_DECL \
2499 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
2500 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
2502 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
2505 field_decl = DECL_CHAIN (field_decl);
2506 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
2509 field_decl = DECL_CHAIN (field_decl);
2510 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
2512 #undef AT_LEAST_AS_LARGE_AS
2515 /* Avoid calling `check_string_class_template ()' more than once. */
2516 static GTY(()) int string_layout_checked;
2518 /* Construct an internal string layout to be used as a template for
2519 creating NSConstantString/NXConstantString instances. */
2522 objc_build_internal_const_str_type (void)
2524 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
2525 tree fields = build_decl (input_location,
2526 FIELD_DECL, NULL_TREE, ptr_type_node);
2527 tree field = build_decl (input_location,
2528 FIELD_DECL, NULL_TREE, ptr_type_node);
2530 DECL_CHAIN (field) = fields; fields = field;
2531 field = build_decl (input_location,
2532 FIELD_DECL, NULL_TREE, unsigned_type_node);
2533 DECL_CHAIN (field) = fields; fields = field;
2534 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
2536 finish_builtin_struct (type, "__builtin_ObjCString",
2542 /* Custom build_string which sets TREE_TYPE! */
2545 my_build_string (int len, const char *str)
2547 return fix_string_type (build_string (len, str));
2550 /* Build a string with contents STR and length LEN and convert it to a
2554 my_build_string_pointer (int len, const char *str)
2556 tree string = my_build_string (len, str);
2557 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
2558 return build1 (ADDR_EXPR, ptrtype, string);
2562 string_hash (const void *ptr)
2564 const_tree const str = ((const struct string_descriptor *)ptr)->literal;
2565 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
2566 int i, len = TREE_STRING_LENGTH (str);
2569 for (i = 0; i < len; i++)
2570 h = ((h * 613) + p[i]);
2576 string_eq (const void *ptr1, const void *ptr2)
2578 const_tree const str1 = ((const struct string_descriptor *)ptr1)->literal;
2579 const_tree const str2 = ((const struct string_descriptor *)ptr2)->literal;
2580 int len1 = TREE_STRING_LENGTH (str1);
2582 return (len1 == TREE_STRING_LENGTH (str2)
2583 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
2587 /* Given a chain of STRING_CST's, build a static instance of
2588 NXConstantString which points at the concatenation of those
2589 strings. We place the string object in the __string_objects
2590 section of the __OBJC segment. The Objective-C runtime will
2591 initialize the isa pointers of the string objects to point at the
2592 NXConstantString class object. */
2595 objc_build_string_object (tree string)
2597 tree constructor = NULL_TREE, constant_string_class;
2600 struct string_descriptor *desc, key;
2603 /* Prep the string argument. */
2604 string = fix_string_type (string);
2605 TREE_SET_CODE (string, STRING_CST);
2606 length = TREE_STRING_LENGTH (string) - 1;
2608 /* Check whether the string class being used actually exists and has the
2609 correct ivar layout. */
2610 if (!string_layout_checked)
2612 string_layout_checked = -1;
2613 constant_string_class = lookup_interface (constant_string_id);
2614 internal_const_str_type = objc_build_internal_const_str_type ();
2616 if (!constant_string_class
2617 || !(constant_string_type
2618 = CLASS_STATIC_TEMPLATE (constant_string_class)))
2619 error ("cannot find interface declaration for %qE",
2620 constant_string_id);
2621 /* The NSConstantString/NXConstantString ivar layout is now known. */
2622 else if (!check_string_class_template ())
2623 error ("interface %qE does not have valid constant string layout",
2624 constant_string_id);
2625 /* For the NeXT runtime, we can generate a literal reference
2626 to the string class, don't need to run a constructor. */
2627 else if (flag_next_runtime && !setup_string_decl ())
2628 error ("cannot find reference tag for class %qE",
2629 constant_string_id);
2632 string_layout_checked = 1; /* Success! */
2633 add_class_reference (constant_string_id);
2637 if (string_layout_checked == -1)
2638 return error_mark_node;
2640 /* Perhaps we already constructed a constant string just like this one? */
2641 key.literal = string;
2642 loc = htab_find_slot (string_htab, &key, INSERT);
2643 desc = (struct string_descriptor *) *loc;
2648 VEC(constructor_elt,gc) *v = NULL;
2649 *loc = desc = ggc_alloc_string_descriptor ();
2650 desc->literal = string;
2652 /* GNU: (NXConstantString *) & ((__builtin_ObjCString) { NULL, string, length }) */
2653 /* NeXT: (NSConstantString *) & ((__builtin_ObjCString) { isa, string, length }) */
2654 fields = TYPE_FIELDS (internal_const_str_type);
2655 CONSTRUCTOR_APPEND_ELT (v, fields,
2657 ? build_unary_op (input_location,
2658 ADDR_EXPR, string_class_decl, 0)
2659 : build_int_cst (NULL_TREE, 0));
2660 fields = DECL_CHAIN (fields);
2661 CONSTRUCTOR_APPEND_ELT (v, fields,
2662 build_unary_op (input_location,
2663 ADDR_EXPR, string, 1));
2664 fields = DECL_CHAIN (fields);
2665 CONSTRUCTOR_APPEND_ELT (v, fields, build_int_cst (NULL_TREE, length));
2666 constructor = objc_build_constructor (internal_const_str_type, v);
2668 if (!flag_next_runtime)
2670 = objc_add_static_instance (constructor, constant_string_type);
2673 var = build_decl (input_location,
2674 CONST_DECL, NULL, TREE_TYPE (constructor));
2675 DECL_INITIAL (var) = constructor;
2676 TREE_STATIC (var) = 1;
2677 pushdecl_top_level (var);
2680 desc->constructor = constructor;
2683 addr = convert (build_pointer_type (constant_string_type),
2684 build_unary_op (input_location,
2685 ADDR_EXPR, desc->constructor, 1));
2690 /* Declare a static instance of CLASS_DECL initialized by CONSTRUCTOR. */
2692 static GTY(()) int num_static_inst;
2695 objc_add_static_instance (tree constructor, tree class_decl)
2700 /* Find the list of static instances for the CLASS_DECL. Create one if
2702 for (chain = &objc_static_instances;
2703 *chain && TREE_VALUE (*chain) != class_decl;
2704 chain = &TREE_CHAIN (*chain));
2707 *chain = tree_cons (NULL_TREE, class_decl, NULL_TREE);
2708 add_objc_string (OBJC_TYPE_NAME (class_decl), class_names);
2711 sprintf (buf, "_OBJC_INSTANCE_%d", num_static_inst++);
2712 decl = build_decl (input_location,
2713 VAR_DECL, get_identifier (buf), class_decl);
2714 TREE_STATIC (decl) = 1;
2715 DECL_ARTIFICIAL (decl) = 1;
2716 TREE_USED (decl) = 1;
2717 DECL_INITIAL (decl) = constructor;
2719 /* We may be writing something else just now.
2720 Postpone till end of input. */
2721 DECL_DEFER_OUTPUT (decl) = 1;
2722 pushdecl_top_level (decl);
2723 rest_of_decl_compilation (decl, 1, 0);
2725 /* Add the DECL to the head of this CLASS' list. */
2726 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE, decl, TREE_PURPOSE (*chain));
2731 /* Build a static constant CONSTRUCTOR
2732 with type TYPE and elements ELTS. */
2735 objc_build_constructor (tree type, VEC(constructor_elt,gc) *elts)
2737 tree constructor = build_constructor (type, elts);
2739 TREE_CONSTANT (constructor) = 1;
2740 TREE_STATIC (constructor) = 1;
2741 TREE_READONLY (constructor) = 1;
2744 /* Adjust for impedance mismatch. We should figure out how to build
2745 CONSTRUCTORs that consistently please both the C and C++ gods. */
2746 if (!VEC_index (constructor_elt, elts, 0)->index)
2747 TREE_TYPE (constructor) = init_list_type_node;
2753 /* Take care of defining and initializing _OBJC_SYMBOLS. */
2755 /* Predefine the following data type:
2763 void *defs[cls_def_cnt + cat_def_cnt];
2767 build_objc_symtab_template (void)
2769 tree fields, *chain = NULL;
2771 objc_symtab_template = objc_start_struct (get_identifier (UTAG_SYMTAB));
2773 /* long sel_ref_cnt; */
2774 fields = add_field_decl (long_integer_type_node, "sel_ref_cnt", &chain);
2777 add_field_decl (build_pointer_type (objc_selector_type), "refs", &chain);
2779 /* short cls_def_cnt; */
2780 add_field_decl (short_integer_type_node, "cls_def_cnt", &chain);
2782 /* short cat_def_cnt; */
2783 add_field_decl (short_integer_type_node, "cat_def_cnt", &chain);
2785 if (imp_count || cat_count || !flag_next_runtime)
2787 /* void *defs[imp_count + cat_count (+ 1)]; */
2788 /* NB: The index is one less than the size of the array. */
2789 int index = imp_count + cat_count + (flag_next_runtime ? -1: 0);
2790 tree array_type = build_sized_array_type (ptr_type_node, index + 1);
2791 add_field_decl (array_type, "defs", &chain);
2794 objc_finish_struct (objc_symtab_template, fields);
2797 /* Create the initial value for the `defs' field of _objc_symtab.
2798 This is a CONSTRUCTOR. */
2801 init_def_list (tree type)
2804 struct imp_entry *impent;
2805 VEC(constructor_elt,gc) *v = NULL;
2808 for (impent = imp_list; impent; impent = impent->next)
2810 if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
2812 expr = build_unary_op (input_location,
2813 ADDR_EXPR, impent->class_decl, 0);
2814 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2819 for (impent = imp_list; impent; impent = impent->next)
2821 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
2823 expr = build_unary_op (input_location,
2824 ADDR_EXPR, impent->class_decl, 0);
2825 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2829 if (!flag_next_runtime)
2831 /* statics = { ..., _OBJC_STATIC_INSTANCES, ... } */
2832 if (static_instances_decl)
2833 expr = build_unary_op (input_location,
2834 ADDR_EXPR, static_instances_decl, 0);
2836 expr = integer_zero_node;
2838 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2841 return objc_build_constructor (type, v);
2844 /* Construct the initial value for all of _objc_symtab. */
2847 init_objc_symtab (tree type)
2849 VEC(constructor_elt,gc) *v = NULL;
2851 /* sel_ref_cnt = { ..., 5, ... } */
2853 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
2854 build_int_cst (long_integer_type_node, 0));
2856 /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
2858 if (flag_next_runtime || ! sel_ref_chain)
2859 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, convert (
2860 build_pointer_type (objc_selector_type),
2861 integer_zero_node));
2864 tree expr = build_unary_op (input_location, ADDR_EXPR,
2865 UOBJC_SELECTOR_TABLE_decl, 1);
2867 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
2868 convert (build_pointer_type (objc_selector_type),
2872 /* cls_def_cnt = { ..., 5, ... } */
2874 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
2875 build_int_cst (short_integer_type_node, imp_count));
2877 /* cat_def_cnt = { ..., 5, ... } */
2879 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
2880 build_int_cst (short_integer_type_node, cat_count));
2882 /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
2884 if (imp_count || cat_count || !flag_next_runtime)
2887 tree field = TYPE_FIELDS (type);
2888 field = DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (field))));
2890 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init_def_list (TREE_TYPE (field)));
2893 return objc_build_constructor (type, v);
2896 /* Generate forward declarations for metadata such as
2897 'OBJC_CLASS_...'. */
2900 build_metadata_decl (const char *name, tree type)
2904 /* struct TYPE NAME_<name>; */
2905 decl = start_var_decl (type, synth_id_with_class_suffix
2907 objc_implementation_context));
2912 /* Push forward-declarations of all the categories so that
2913 init_def_list can use them in a CONSTRUCTOR. */
2916 forward_declare_categories (void)
2918 struct imp_entry *impent;
2919 tree sav = objc_implementation_context;
2921 for (impent = imp_list; impent; impent = impent->next)
2923 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
2925 /* Set an invisible arg to synth_id_with_class_suffix. */
2926 objc_implementation_context = impent->imp_context;
2927 /* extern struct objc_category _OBJC_CATEGORY_<name>; */
2928 impent->class_decl = build_metadata_decl ("_OBJC_CATEGORY",
2929 objc_category_template);
2932 objc_implementation_context = sav;
2935 /* Create the declaration of _OBJC_SYMBOLS, with type `struct _objc_symtab'
2936 and initialized appropriately. */
2939 generate_objc_symtab_decl (void)
2942 build_objc_symtab_template ();
2943 UOBJC_SYMBOLS_decl = start_var_decl (objc_symtab_template, "_OBJC_SYMBOLS");
2944 finish_var_decl (UOBJC_SYMBOLS_decl,
2945 init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl)));
2949 init_module_descriptor (tree type)
2952 VEC(constructor_elt,gc) *v = NULL;
2954 /* version = { 1, ... } */
2956 expr = build_int_cst (long_integer_type_node, OBJC_VERSION);
2957 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2959 /* size = { ..., sizeof (struct _objc_module), ... } */
2961 expr = convert (long_integer_type_node,
2962 size_in_bytes (objc_module_template));
2963 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2965 /* Don't provide any file name for security reasons. */
2966 /* name = { ..., "", ... } */
2968 expr = add_objc_string (get_identifier (""), class_names);
2969 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2971 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
2973 if (UOBJC_SYMBOLS_decl)
2974 expr = build_unary_op (input_location,
2975 ADDR_EXPR, UOBJC_SYMBOLS_decl, 0);
2977 expr = null_pointer_node;
2978 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2980 return objc_build_constructor (type, v);
2983 /* Write out the data structures to describe Objective C classes defined.
2985 struct _objc_module { ... } _OBJC_MODULE = { ... }; */
2988 build_module_descriptor (void)
2990 tree decls, *chain = NULL;
2993 push_lang_context (lang_name_c); /* extern "C" */
2996 objc_module_template = objc_start_struct (get_identifier (UTAG_MODULE));
2999 decls = add_field_decl (long_integer_type_node, "version", &chain);
3002 add_field_decl (long_integer_type_node, "size", &chain);
3005 add_field_decl (string_type_node, "name", &chain);
3007 /* struct _objc_symtab *symtab; */
3008 add_field_decl (build_pointer_type (xref_tag (RECORD_TYPE,
3009 get_identifier (UTAG_SYMTAB))),
3012 objc_finish_struct (objc_module_template, decls);
3014 /* Create an instance of "_objc_module". */
3015 UOBJC_MODULES_decl = start_var_decl (objc_module_template, "_OBJC_MODULES");
3016 /* This is the root of the metadata for defined classes and categories, it
3017 is referenced by the runtime and, therefore, needed. */
3018 DECL_PRESERVE_P (UOBJC_MODULES_decl) = 1;
3019 finish_var_decl (UOBJC_MODULES_decl,
3020 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl)));
3023 pop_lang_context ();
3027 /* The GNU runtime requires us to provide a static initializer function
3030 static void __objc_gnu_init (void) {
3031 __objc_exec_class (&L_OBJC_MODULES);
3035 build_module_initializer_routine (void)
3040 push_lang_context (lang_name_c); /* extern "C" */
3043 objc_push_parm (build_decl (input_location,
3044 PARM_DECL, NULL_TREE, void_type_node));
3046 objc_start_function (get_identifier (TAG_GNUINIT),
3047 build_function_type_list (void_type_node, NULL_TREE),
3048 NULL_TREE, NULL_TREE);
3050 objc_start_function (get_identifier (TAG_GNUINIT),
3051 build_function_type_list (void_type_node, NULL_TREE),
3052 NULL_TREE, objc_get_parm_info (0));
3054 body = c_begin_compound_stmt (true);
3055 add_stmt (build_function_call
3060 build_unary_op (input_location, ADDR_EXPR,
3061 UOBJC_MODULES_decl, 0))));
3062 add_stmt (c_end_compound_stmt (input_location, body, true));
3064 TREE_PUBLIC (current_function_decl) = 0;
3067 /* For Objective-C++, we will need to call __objc_gnu_init
3068 from objc_generate_static_init_call() below. */
3069 DECL_STATIC_CONSTRUCTOR (current_function_decl) = 1;
3072 GNU_INIT_decl = current_function_decl;
3076 pop_lang_context ();
3081 /* Return 1 if the __objc_gnu_init function has been synthesized and needs
3082 to be called by the module initializer routine. */
3085 objc_static_init_needed_p (void)
3087 return (GNU_INIT_decl != NULL_TREE);
3090 /* Generate a call to the __objc_gnu_init initializer function. */
3093 objc_generate_static_init_call (tree ctors ATTRIBUTE_UNUSED)
3095 add_stmt (build_stmt (input_location, EXPR_STMT,
3096 build_function_call (input_location,
3097 GNU_INIT_decl, NULL_TREE)));
3101 #endif /* OBJCPLUS */
3103 /* Return the DECL of the string IDENT in the SECTION. */
3106 get_objc_string_decl (tree ident, enum string_section section)
3110 if (section == class_names)
3111 chain = class_names_chain;
3112 else if (section == meth_var_names)
3113 chain = meth_var_names_chain;
3114 else if (section == meth_var_types)
3115 chain = meth_var_types_chain;
3119 for (; chain != 0; chain = TREE_CHAIN (chain))
3120 if (TREE_VALUE (chain) == ident)
3121 return (TREE_PURPOSE (chain));
3127 /* Output references to all statically allocated objects. Return the DECL
3128 for the array built. */
3131 generate_static_references (void)
3133 tree expr = NULL_TREE;
3134 tree class_name, klass, decl;
3135 tree cl_chain, in_chain, type
3136 = build_array_type (build_pointer_type (void_type_node), NULL_TREE);
3137 int num_inst, num_class;
3139 VEC(constructor_elt,gc) *decls = NULL;
3141 if (flag_next_runtime)
3144 for (cl_chain = objc_static_instances, num_class = 0;
3145 cl_chain; cl_chain = TREE_CHAIN (cl_chain), num_class++)
3147 VEC(constructor_elt,gc) *v = NULL;
3149 for (num_inst = 0, in_chain = TREE_PURPOSE (cl_chain);
3150 in_chain; num_inst++, in_chain = TREE_CHAIN (in_chain));
3152 sprintf (buf, "_OBJC_STATIC_INSTANCES_%d", num_class);
3153 decl = start_var_decl (type, buf);
3155 /* Output {class_name, ...}. */
3156 klass = TREE_VALUE (cl_chain);
3157 class_name = get_objc_string_decl (OBJC_TYPE_NAME (klass), class_names);
3158 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3159 build_unary_op (input_location,
3160 ADDR_EXPR, class_name, 1));
3162 /* Output {..., instance, ...}. */
3163 for (in_chain = TREE_PURPOSE (cl_chain);
3164 in_chain; in_chain = TREE_CHAIN (in_chain))
3166 expr = build_unary_op (input_location,
3167 ADDR_EXPR, TREE_VALUE (in_chain), 1);
3168 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3171 /* Output {..., NULL}. */
3172 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
3174 expr = objc_build_constructor (TREE_TYPE (decl), v);
3175 finish_var_decl (decl, expr);
3176 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE,
3177 build_unary_op (input_location,
3178 ADDR_EXPR, decl, 1));
3181 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE, build_int_cst (NULL_TREE, 0));
3182 expr = objc_build_constructor (type, decls);
3183 static_instances_decl = start_var_decl (type, "_OBJC_STATIC_INSTANCES");
3184 finish_var_decl (static_instances_decl, expr);
3187 static GTY(()) int selector_reference_idx;
3190 build_selector_reference_decl (void)
3195 sprintf (buf, "_OBJC_SELECTOR_REFERENCES_%d", selector_reference_idx++);
3196 decl = start_var_decl (objc_selector_type, buf);
3202 build_selector_table_decl (void)
3206 if (flag_typed_selectors)
3208 build_selector_template ();
3209 temp = build_array_type (objc_selector_template, NULL_TREE);
3212 temp = build_array_type (objc_selector_type, NULL_TREE);
3214 UOBJC_SELECTOR_TABLE_decl = start_var_decl (temp, "_OBJC_SELECTOR_TABLE");
3217 /* Just a handy wrapper for add_objc_string. */
3220 build_selector (tree ident)
3222 return convert (objc_selector_type,
3223 add_objc_string (ident, meth_var_names));
3226 /* Used only by build_*_selector_translation_table (). */
3228 diagnose_missing_method (tree meth, location_t here)
3232 for (method_chain = meth_var_names_chain;
3234 method_chain = TREE_CHAIN (method_chain))
3236 if (TREE_VALUE (method_chain) == meth)
3244 warning_at (here, 0, "creating selector for nonexistent method %qE",
3249 build_next_selector_translation_table (void)
3252 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
3255 tree decl = TREE_PURPOSE (chain);
3256 if (warn_selector && objc_implementation_context)
3260 loc = DECL_SOURCE_LOCATION (decl);
3262 loc = input_location;
3263 diagnose_missing_method (TREE_VALUE (chain), loc);
3266 expr = build_selector (TREE_VALUE (chain));
3270 /* Entries of this form are used for references to methods.
3271 The runtime re-writes these on start-up, but the compiler can't see
3272 that and optimizes it away unless we force it. */
3273 DECL_PRESERVE_P (decl) = 1;
3274 finish_var_decl (decl, expr);
3280 build_gnu_selector_translation_table (void)
3284 tree decl = NULL_TREE;*/
3285 VEC(constructor_elt,gc) *inits = NULL;
3287 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
3291 if (warn_selector && objc_implementation_context)
3292 diagnose_missing_method (TREE_VALUE (chain), input_location);
3294 expr = build_selector (TREE_VALUE (chain));
3295 /* add one for the '\0' character
3296 offset += IDENTIFIER_LENGTH (TREE_VALUE (chain)) + 1;*/
3299 if (flag_typed_selectors)
3301 VEC(constructor_elt,gc) *v = NULL;
3302 tree encoding = get_proto_encoding (TREE_PURPOSE (chain));
3303 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3304 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, encoding);
3305 expr = objc_build_constructor (objc_selector_template, v);
3308 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
3310 } /* each element in the chain */
3313 /* Cause the selector table (previously forward-declared)
3314 to be actually output. */
3317 if (flag_typed_selectors)
3319 VEC(constructor_elt,gc) *v = NULL;
3320 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
3321 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
3322 expr = objc_build_constructor (objc_selector_template, v);
3325 expr = integer_zero_node;
3327 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
3328 expr = objc_build_constructor (TREE_TYPE (UOBJC_SELECTOR_TABLE_decl),
3330 finish_var_decl (UOBJC_SELECTOR_TABLE_decl, expr);
3335 get_proto_encoding (tree proto)
3340 if (! METHOD_ENCODING (proto))
3342 encoding = encode_method_prototype (proto);
3343 METHOD_ENCODING (proto) = encoding;
3346 encoding = METHOD_ENCODING (proto);
3348 return add_objc_string (encoding, meth_var_types);
3351 return build_int_cst (NULL_TREE, 0);
3354 /* sel_ref_chain is a list whose "value" fields will be instances of
3355 identifier_node that represent the selector. LOC is the location of
3359 build_typed_selector_reference (location_t loc, tree ident, tree prototype)
3361 tree *chain = &sel_ref_chain;
3367 if (TREE_PURPOSE (*chain) == prototype && TREE_VALUE (*chain) == ident)
3368 goto return_at_index;
3371 chain = &TREE_CHAIN (*chain);
3374 *chain = tree_cons (prototype, ident, NULL_TREE);
3377 expr = build_unary_op (loc, ADDR_EXPR,
3378 build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
3379 build_int_cst (NULL_TREE, index)),
3381 return convert (objc_selector_type, expr);
3385 build_selector_reference (location_t loc, tree ident)
3387 tree *chain = &sel_ref_chain;
3393 if (TREE_VALUE (*chain) == ident)
3394 return (flag_next_runtime
3395 ? TREE_PURPOSE (*chain)
3396 : build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
3397 build_int_cst (NULL_TREE, index)));
3400 chain = &TREE_CHAIN (*chain);
3403 expr = (flag_next_runtime ? build_selector_reference_decl (): NULL_TREE);
3405 *chain = tree_cons (expr, ident, NULL_TREE);
3407 return (flag_next_runtime
3409 : build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
3410 build_int_cst (NULL_TREE, index)));
3413 static GTY(()) int class_reference_idx;
3416 build_class_reference_decl (void)
3421 sprintf (buf, "_OBJC_CLASS_REFERENCES_%d", class_reference_idx++);
3422 decl = start_var_decl (objc_class_type, buf);
3427 /* Create a class reference, but don't create a variable to reference
3431 add_class_reference (tree ident)
3435 if ((chain = cls_ref_chain))
3440 if (ident == TREE_VALUE (chain))
3444 chain = TREE_CHAIN (chain);
3448 /* Append to the end of the list */
3449 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
3452 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
3455 /* Get a class reference, creating it if necessary. Also create the
3456 reference variable. */
3459 objc_get_class_reference (tree ident)
3461 tree orig_ident = (DECL_P (ident)
3464 ? OBJC_TYPE_NAME (ident)
3466 bool local_scope = false;
3469 if (processing_template_decl)
3470 /* Must wait until template instantiation time. */
3471 return build_min_nt (CLASS_REFERENCE_EXPR, ident);
3474 if (TREE_CODE (ident) == TYPE_DECL)
3475 ident = (DECL_ORIGINAL_TYPE (ident)
3476 ? DECL_ORIGINAL_TYPE (ident)
3477 : TREE_TYPE (ident));
3481 && CP_TYPE_CONTEXT (ident) != global_namespace)
3485 if (local_scope || !(ident = objc_is_class_name (ident)))
3487 error ("%qE is not an Objective-C class name or alias",
3489 return error_mark_node;
3492 if (flag_next_runtime && !flag_zero_link)
3497 for (chain = &cls_ref_chain; *chain; chain = &TREE_CHAIN (*chain))
3498 if (TREE_VALUE (*chain) == ident)
3500 if (! TREE_PURPOSE (*chain))
3501 TREE_PURPOSE (*chain) = build_class_reference_decl ();
3503 return TREE_PURPOSE (*chain);
3506 decl = build_class_reference_decl ();
3507 *chain = tree_cons (decl, ident, NULL_TREE);
3514 add_class_reference (ident);
3516 params = build_tree_list (NULL_TREE,
3517 my_build_string_pointer
3518 (IDENTIFIER_LENGTH (ident) + 1,
3519 IDENTIFIER_POINTER (ident)));
3521 assemble_external (objc_get_class_decl);
3522 return build_function_call (input_location, objc_get_class_decl, params);
3526 /* For each string section we have a chain which maps identifier nodes
3527 to decls for the strings. */
3529 static GTY(()) int class_names_idx;
3530 static GTY(()) int meth_var_names_idx;
3531 static GTY(()) int meth_var_types_idx;
3534 add_objc_string (tree ident, enum string_section section)
3536 tree *chain, decl, type, string_expr;
3540 if (section == class_names)
3542 chain = &class_names_chain;
3543 sprintf (buf, "_OBJC_CLASS_NAME_%d", class_names_idx++);
3545 else if (section == meth_var_names)
3547 chain = &meth_var_names_chain;
3548 sprintf (buf, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx++);
3550 else if (section == meth_var_types)
3552 chain = &meth_var_types_chain;
3553 sprintf (buf, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx++);
3560 if (TREE_VALUE (*chain) == ident)
3561 return convert (string_type_node,
3562 build_unary_op (input_location,
3563 ADDR_EXPR, TREE_PURPOSE (*chain), 1));
3565 chain = &TREE_CHAIN (*chain);
3568 type = build_sized_array_type (char_type_node, IDENTIFIER_LENGTH (ident) + 1);
3569 decl = start_var_decl (type, buf);
3570 string_expr = my_build_string (IDENTIFIER_LENGTH (ident) + 1,
3571 IDENTIFIER_POINTER (ident));
3572 TREE_CONSTANT (decl) = 1;
3573 finish_var_decl (decl, string_expr);
3575 *chain = tree_cons (decl, ident, NULL_TREE);
3577 return convert (string_type_node, build_unary_op (input_location,
3578 ADDR_EXPR, decl, 1));
3582 objc_declare_alias (tree alias_ident, tree class_ident)
3584 tree underlying_class;
3587 if (current_namespace != global_namespace) {
3588 error ("Objective-C declarations may only appear in global scope");
3590 #endif /* OBJCPLUS */
3592 if (!(underlying_class = objc_is_class_name (class_ident)))
3593 warning (0, "cannot find class %qE", class_ident);
3594 else if (objc_is_class_name (alias_ident))
3595 warning (0, "class %qE already exists", alias_ident);
3598 /* Implement @compatibility_alias as a typedef. */
3600 push_lang_context (lang_name_c); /* extern "C" */
3602 lang_hooks.decls.pushdecl (build_decl
3606 xref_tag (RECORD_TYPE, underlying_class)));
3608 pop_lang_context ();
3610 alias_chain = tree_cons (underlying_class, alias_ident, alias_chain);
3615 objc_declare_class (tree ident_list)
3619 if (current_namespace != global_namespace) {
3620 error ("Objective-C declarations may only appear in global scope");
3622 #endif /* OBJCPLUS */
3624 for (list = ident_list; list; list = TREE_CHAIN (list))
3626 tree ident = TREE_VALUE (list);
3628 if (! objc_is_class_name (ident))
3630 tree record = lookup_name (ident), type = record;
3634 if (TREE_CODE (record) == TYPE_DECL)
3635 type = DECL_ORIGINAL_TYPE (record) ?
3636 DECL_ORIGINAL_TYPE (record) :
3639 if (!TYPE_HAS_OBJC_INFO (type)
3640 || !TYPE_OBJC_INTERFACE (type))
3642 error ("%qE redeclared as different kind of symbol",
3644 error ("previous declaration of %q+D",
3649 record = xref_tag (RECORD_TYPE, ident);
3650 INIT_TYPE_OBJC_INFO (record);
3651 TYPE_OBJC_INTERFACE (record) = ident;
3652 class_chain = tree_cons (NULL_TREE, ident, class_chain);
3658 objc_is_class_name (tree ident)
3662 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE
3663 && identifier_global_value (ident))
3664 ident = identifier_global_value (ident);
3665 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
3666 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
3668 if (ident && TREE_CODE (ident) == RECORD_TYPE)
3669 ident = OBJC_TYPE_NAME (ident);
3671 if (ident && TREE_CODE (ident) == TYPE_DECL)
3672 ident = DECL_NAME (ident);
3674 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
3677 if (lookup_interface (ident))
3680 for (chain = class_chain; chain; chain = TREE_CHAIN (chain))
3682 if (ident == TREE_VALUE (chain))
3686 for (chain = alias_chain; chain; chain = TREE_CHAIN (chain))
3688 if (ident == TREE_VALUE (chain))
3689 return TREE_PURPOSE (chain);
3695 /* Check whether TYPE is either 'id' or 'Class'. */
3698 objc_is_id (tree type)
3700 if (type && TREE_CODE (type) == IDENTIFIER_NODE
3701 && identifier_global_value (type))
3702 type = identifier_global_value (type);
3704 if (type && TREE_CODE (type) == TYPE_DECL)
3705 type = TREE_TYPE (type);
3707 /* NB: This function may be called before the ObjC front-end has
3708 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3709 return (objc_object_type && type
3710 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
3715 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3716 class instance. This is needed by other parts of the compiler to
3717 handle ObjC types gracefully. */
3720 objc_is_object_ptr (tree type)
3724 type = TYPE_MAIN_VARIANT (type);
3725 if (!POINTER_TYPE_P (type))
3728 ret = objc_is_id (type);
3730 ret = objc_is_class_name (TREE_TYPE (type));
3736 objc_is_gcable_type (tree type, int or_strong_p)
3742 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
3744 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
3746 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
3748 type = TREE_TYPE (type);
3749 if (TREE_CODE (type) != RECORD_TYPE)
3751 name = TYPE_NAME (type);
3752 return (objc_is_class_name (name) != NULL_TREE);
3756 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
3758 if (expr == oldexpr)
3761 switch (TREE_CODE (expr))
3764 return objc_build_component_ref
3765 (objc_substitute_decl (TREE_OPERAND (expr, 0),
3768 DECL_NAME (TREE_OPERAND (expr, 1)));
3770 return build_array_ref (input_location,
3771 objc_substitute_decl (TREE_OPERAND (expr, 0),
3774 TREE_OPERAND (expr, 1));
3776 return build_indirect_ref (input_location,
3777 objc_substitute_decl (TREE_OPERAND (expr, 0),
3779 newexpr), RO_ARROW);
3786 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
3789 /* The LHS parameter contains the expression 'outervar->memberspec';
3790 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3791 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3794 = objc_substitute_decl
3795 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
3797 = (flag_objc_direct_dispatch
3798 ? objc_assign_ivar_fast_decl
3799 : objc_assign_ivar_decl);
3801 offs = convert (integer_type_node, build_unary_op (input_location,
3802 ADDR_EXPR, offs, 0));
3804 func_params = tree_cons (NULL_TREE,
3805 convert (objc_object_type, rhs),
3806 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
3807 tree_cons (NULL_TREE, offs,
3810 assemble_external (func);
3811 return build_function_call (input_location, func, func_params);
3815 objc_build_global_assignment (tree lhs, tree rhs)
3817 tree func_params = tree_cons (NULL_TREE,
3818 convert (objc_object_type, rhs),
3819 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3820 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3823 assemble_external (objc_assign_global_decl);
3824 return build_function_call (input_location,
3825 objc_assign_global_decl, func_params);
3829 objc_build_strong_cast_assignment (tree lhs, tree rhs)
3831 tree func_params = tree_cons (NULL_TREE,
3832 convert (objc_object_type, rhs),
3833 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3834 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3837 assemble_external (objc_assign_strong_cast_decl);
3838 return build_function_call (input_location,
3839 objc_assign_strong_cast_decl, func_params);
3843 objc_is_gcable_p (tree expr)
3845 return (TREE_CODE (expr) == COMPONENT_REF
3846 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
3847 : TREE_CODE (expr) == ARRAY_REF
3848 ? (objc_is_gcable_p (TREE_TYPE (expr))
3849 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
3850 : TREE_CODE (expr) == ARRAY_TYPE
3851 ? objc_is_gcable_p (TREE_TYPE (expr))
3853 ? objc_is_gcable_type (expr, 1)
3854 : (objc_is_gcable_p (TREE_TYPE (expr))
3856 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
3860 objc_is_ivar_reference_p (tree expr)
3862 return (TREE_CODE (expr) == ARRAY_REF
3863 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
3864 : TREE_CODE (expr) == COMPONENT_REF
3865 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
3870 objc_is_global_reference_p (tree expr)
3872 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
3873 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
3875 ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr))
3880 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
3882 tree result = NULL_TREE, outer;
3883 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
3885 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3886 will have been transformed to the form '*(type *)&expr'. */
3887 if (TREE_CODE (lhs) == INDIRECT_REF)
3889 outer = TREE_OPERAND (lhs, 0);
3891 while (!strong_cast_p
3892 && (CONVERT_EXPR_P (outer)
3893 || TREE_CODE (outer) == NON_LVALUE_EXPR))
3895 tree lhstype = TREE_TYPE (outer);
3897 /* Descend down the cast chain, and record the first objc_gc
3899 if (POINTER_TYPE_P (lhstype))
3902 = lookup_attribute ("objc_gc",
3903 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
3909 outer = TREE_OPERAND (outer, 0);
3913 /* If we have a __strong cast, it trumps all else. */
3916 if (modifycode != NOP_EXPR)
3917 goto invalid_pointer_arithmetic;
3919 if (warn_assign_intercept)
3920 warning (0, "strong-cast assignment has been intercepted");
3922 result = objc_build_strong_cast_assignment (lhs, rhs);
3927 /* the lhs must be of a suitable type, regardless of its underlying
3929 if (!objc_is_gcable_p (lhs))
3935 && (TREE_CODE (outer) == COMPONENT_REF
3936 || TREE_CODE (outer) == ARRAY_REF))
3937 outer = TREE_OPERAND (outer, 0);
3939 if (TREE_CODE (outer) == INDIRECT_REF)
3941 outer = TREE_OPERAND (outer, 0);
3945 outer_gc_p = objc_is_gcable_p (outer);
3947 /* Handle ivar assignments. */
3948 if (objc_is_ivar_reference_p (lhs))
3950 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3951 doesn't cut it here), the best we can do here is suggest a cast. */
3952 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
3954 /* We may still be able to use the global write barrier... */
3955 if (!indirect_p && objc_is_global_reference_p (outer))
3956 goto global_reference;
3959 if (modifycode == NOP_EXPR)
3961 if (warn_assign_intercept)
3962 warning (0, "strong-cast may possibly be needed");
3968 if (modifycode != NOP_EXPR)
3969 goto invalid_pointer_arithmetic;
3971 if (warn_assign_intercept)
3972 warning (0, "instance variable assignment has been intercepted");
3974 result = objc_build_ivar_assignment (outer, lhs, rhs);
3979 /* Likewise, intercept assignment to global/static variables if their type is
3981 if (objc_is_global_reference_p (outer))
3987 if (modifycode != NOP_EXPR)
3989 invalid_pointer_arithmetic:
3991 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3996 if (warn_assign_intercept)
3997 warning (0, "global/static variable assignment has been intercepted");
3999 result = objc_build_global_assignment (lhs, rhs);
4002 /* In all other cases, fall back to the normal mechanism. */
4007 struct GTY(()) interface_tuple {
4012 static GTY ((param_is (struct interface_tuple))) htab_t interface_htab;
4015 hash_interface (const void *p)
4017 const struct interface_tuple *d = (const struct interface_tuple *) p;
4018 return IDENTIFIER_HASH_VALUE (d->id);
4022 eq_interface (const void *p1, const void *p2)
4024 const struct interface_tuple *d = (const struct interface_tuple *) p1;
4029 lookup_interface (tree ident)
4032 if (ident && TREE_CODE (ident) == TYPE_DECL)
4033 ident = DECL_NAME (ident);
4036 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
4040 struct interface_tuple **slot;
4045 slot = (struct interface_tuple **)
4046 htab_find_slot_with_hash (interface_htab, ident,
4047 IDENTIFIER_HASH_VALUE (ident),
4050 i = (*slot)->class_name;
4056 /* Implement @defs (<classname>) within struct bodies. */
4059 objc_get_class_ivars (tree class_name)
4061 tree interface = lookup_interface (class_name);
4064 return get_class_ivars (interface, true);
4066 error ("cannot find interface declaration for %qE",
4069 return error_mark_node;
4072 /* Called when checking the variables in a struct. If we are not
4073 doing the ivars list inside an @interface context, then returns
4074 fieldlist unchanged. Else, returns the list of class ivars.
4077 objc_get_interface_ivars (tree fieldlist)
4079 if (!objc_collecting_ivars || !objc_interface_context
4080 || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE
4081 || CLASS_SUPER_NAME (objc_interface_context) == NULL_TREE)
4084 return get_class_ivars (objc_interface_context, true);
4087 /* Used by: build_private_template, continue_class,
4088 and for @defs constructs. */
4091 get_class_ivars (tree interface, bool inherited)
4093 tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface));
4095 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
4096 by the current class (i.e., they do not include super-class ivars).
4097 However, the CLASS_IVARS list will be side-effected by a call to
4098 finish_struct(), which will fill in field offsets. */
4099 if (!CLASS_IVARS (interface))
4100 CLASS_IVARS (interface) = ivar_chain;
4105 while (CLASS_SUPER_NAME (interface))
4107 /* Prepend super-class ivars. */
4108 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4109 ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)),
4116 /* Create a temporary variable of type 'type'. If 'name' is set, uses
4117 the specified name, else use no name. Returns the declaration of
4118 the type. The 'name' is mostly useful for debugging.
4121 objc_create_temporary_var (tree type, const char *name)
4127 decl = build_decl (input_location,
4128 VAR_DECL, get_identifier (name), type);
4132 decl = build_decl (input_location,
4133 VAR_DECL, NULL_TREE, type);
4135 TREE_USED (decl) = 1;
4136 DECL_ARTIFICIAL (decl) = 1;
4137 DECL_IGNORED_P (decl) = 1;
4138 DECL_CONTEXT (decl) = current_function_decl;
4143 /* Exception handling constructs. We begin by having the parser do most
4144 of the work and passing us blocks. What we do next depends on whether
4145 we're doing "native" exception handling or legacy Darwin setjmp exceptions.
4146 We abstract all of this in a handful of appropriately named routines. */
4148 /* Stack of open try blocks. */
4150 struct objc_try_context
4152 struct objc_try_context *outer;
4154 /* Statements (or statement lists) as processed by the parser. */
4158 /* Some file position locations. */
4159 location_t try_locus;
4160 location_t end_try_locus;
4161 location_t end_catch_locus;
4162 location_t finally_locus;
4163 location_t end_finally_locus;
4165 /* A STATEMENT_LIST of CATCH_EXPRs, appropriate for sticking into op1
4166 of a TRY_CATCH_EXPR. Even when doing Darwin setjmp. */
4169 /* The CATCH_EXPR of an open @catch clause. */
4172 /* The VAR_DECL holding the Darwin equivalent of __builtin_eh_pointer. */
4178 static struct objc_try_context *cur_try_context;
4180 static GTY(()) tree objc_eh_personality_decl;
4182 /* This hook, called via lang_eh_runtime_type, generates a runtime object
4183 that represents TYPE. For Objective-C, this is just the class name. */
4184 /* ??? Isn't there a class object or some such? Is it easy to get? */
4188 objc_eh_runtime_type (tree type)
4190 return add_objc_string (OBJC_TYPE_NAME (TREE_TYPE (type)), class_names);
4194 objc_eh_personality (void)
4196 if (!flag_objc_sjlj_exceptions && !objc_eh_personality_decl)
4197 objc_eh_personality_decl = build_personality_function ("gnu_objc");
4198 return objc_eh_personality_decl;
4202 /* Build __builtin_eh_pointer, or the moral equivalent. In the case
4203 of Darwin, we'll arrange for it to be initialized (and associated
4204 with a binding) later. */
4207 objc_build_exc_ptr (void)
4209 if (flag_objc_sjlj_exceptions)
4211 tree var = cur_try_context->caught_decl;
4214 var = objc_create_temporary_var (objc_object_type, NULL);
4215 cur_try_context->caught_decl = var;
4222 t = built_in_decls[BUILT_IN_EH_POINTER];
4223 t = build_call_expr (t, 1, integer_zero_node);
4224 return fold_convert (objc_object_type, t);
4228 /* Build "objc_exception_try_exit(&_stack)". */
4231 next_sjlj_build_try_exit (void)
4234 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
4235 t = tree_cons (NULL, t, NULL);
4236 t = build_function_call (input_location,
4237 objc_exception_try_exit_decl, t);
4242 objc_exception_try_enter (&_stack);
4243 if (_setjmp(&_stack.buf))
4247 Return the COND_EXPR. Note that the THEN and ELSE fields are left
4248 empty, ready for the caller to fill them in. */
4251 next_sjlj_build_enter_and_setjmp (void)
4253 tree t, enter, sj, cond;
4255 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
4256 t = tree_cons (NULL, t, NULL);
4257 enter = build_function_call (input_location,
4258 objc_exception_try_enter_decl, t);
4260 t = objc_build_component_ref (cur_try_context->stack_decl,
4261 get_identifier ("buf"));
4262 t = build_fold_addr_expr_loc (input_location, t);
4264 /* Convert _setjmp argument to type that is expected. */
4265 if (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl)))
4266 t = convert (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl))), t);
4268 t = convert (ptr_type_node, t);
4270 t = convert (ptr_type_node, t);
4272 t = tree_cons (NULL, t, NULL);
4273 sj = build_function_call (input_location,
4274 objc_setjmp_decl, t);
4276 cond = build2 (COMPOUND_EXPR, TREE_TYPE (sj), enter, sj);
4277 cond = c_common_truthvalue_conversion (input_location, cond);
4279 return build3 (COND_EXPR, void_type_node, cond, NULL, NULL);
4284 DECL = objc_exception_extract(&_stack); */
4287 next_sjlj_build_exc_extract (tree decl)
4291 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
4292 t = tree_cons (NULL, t, NULL);
4293 t = build_function_call (input_location,
4294 objc_exception_extract_decl, t);
4295 t = convert (TREE_TYPE (decl), t);
4296 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
4302 if (objc_exception_match(obj_get_class(TYPE), _caught)
4309 objc_exception_try_exit(&_stack);
4311 from the sequence of CATCH_EXPRs in the current try context. */
4314 next_sjlj_build_catch_list (void)
4316 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
4318 tree *last = &catch_seq;
4319 bool saw_id = false;
4321 for (; !tsi_end_p (i); tsi_next (&i))
4323 tree stmt = tsi_stmt (i);
4324 tree type = CATCH_TYPES (stmt);
4325 tree body = CATCH_BODY (stmt);
4337 if (type == error_mark_node)
4338 cond = error_mark_node;
4341 args = tree_cons (NULL, cur_try_context->caught_decl, NULL);
4342 t = objc_get_class_reference (OBJC_TYPE_NAME (TREE_TYPE (type)));
4343 args = tree_cons (NULL, t, args);
4344 t = build_function_call (input_location,
4345 objc_exception_match_decl, args);
4346 cond = c_common_truthvalue_conversion (input_location, t);
4348 t = build3 (COND_EXPR, void_type_node, cond, body, NULL);
4349 SET_EXPR_LOCATION (t, EXPR_LOCATION (stmt));
4352 last = &COND_EXPR_ELSE (t);
4358 t = build2 (MODIFY_EXPR, void_type_node, cur_try_context->rethrow_decl,
4359 cur_try_context->caught_decl);
4360 SET_EXPR_LOCATION (t, cur_try_context->end_catch_locus);
4361 append_to_statement_list (t, last);
4363 t = next_sjlj_build_try_exit ();
4364 SET_EXPR_LOCATION (t, cur_try_context->end_catch_locus);
4365 append_to_statement_list (t, last);
4371 /* Build a complete @try-@catch-@finally block for legacy Darwin setjmp
4372 exception handling. We aim to build:
4375 struct _objc_exception_data _stack;
4379 objc_exception_try_enter (&_stack);
4380 if (_setjmp(&_stack.buf))
4382 id _caught = objc_exception_extract(&_stack);
4383 objc_exception_try_enter (&_stack);
4384 if (_setjmp(&_stack.buf))
4385 _rethrow = objc_exception_extract(&_stack);
4395 objc_exception_try_exit(&_stack);
4398 objc_exception_throw(_rethrow);
4402 If CATCH-LIST is empty, we can omit all of the block containing
4403 "_caught" except for the setting of _rethrow. Note the use of
4404 a real TRY_FINALLY_EXPR here, which is not involved in EH per-se,
4405 but handles goto and other exits from the block. */
4408 next_sjlj_build_try_catch_finally (void)
4410 tree rethrow_decl, stack_decl, t;
4411 tree catch_seq, try_fin, bind;
4413 /* Create the declarations involved. */
4414 t = xref_tag (RECORD_TYPE, get_identifier (UTAG_EXCDATA));
4415 stack_decl = objc_create_temporary_var (t, NULL);
4416 cur_try_context->stack_decl = stack_decl;
4418 rethrow_decl = objc_create_temporary_var (objc_object_type, NULL);
4419 cur_try_context->rethrow_decl = rethrow_decl;
4420 TREE_CHAIN (rethrow_decl) = stack_decl;
4422 /* Build the outermost variable binding level. */
4423 bind = build3 (BIND_EXPR, void_type_node, rethrow_decl, NULL, NULL);
4424 SET_EXPR_LOCATION (bind, cur_try_context->try_locus);
4425 TREE_SIDE_EFFECTS (bind) = 1;
4427 /* Initialize rethrow_decl. */
4428 t = build2 (MODIFY_EXPR, void_type_node, rethrow_decl,
4429 convert (objc_object_type, null_pointer_node));
4430 SET_EXPR_LOCATION (t, cur_try_context->try_locus);
4431 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
4433 /* Build the outermost TRY_FINALLY_EXPR. */
4434 try_fin = build2 (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
4435 SET_EXPR_LOCATION (try_fin, cur_try_context->try_locus);
4436 TREE_SIDE_EFFECTS (try_fin) = 1;
4437 append_to_statement_list (try_fin, &BIND_EXPR_BODY (bind));
4439 /* Create the complete catch sequence. */
4440 if (cur_try_context->catch_list)
4442 tree caught_decl = objc_build_exc_ptr ();
4443 catch_seq = build_stmt (input_location, BIND_EXPR, caught_decl, NULL, NULL);
4444 TREE_SIDE_EFFECTS (catch_seq) = 1;
4446 t = next_sjlj_build_exc_extract (caught_decl);
4447 append_to_statement_list (t, &BIND_EXPR_BODY (catch_seq));
4449 t = next_sjlj_build_enter_and_setjmp ();
4450 COND_EXPR_THEN (t) = next_sjlj_build_exc_extract (rethrow_decl);
4451 COND_EXPR_ELSE (t) = next_sjlj_build_catch_list ();
4452 append_to_statement_list (t, &BIND_EXPR_BODY (catch_seq));
4455 catch_seq = next_sjlj_build_exc_extract (rethrow_decl);
4456 SET_EXPR_LOCATION (catch_seq, cur_try_context->end_try_locus);
4458 /* Build the main register-and-try if statement. */
4459 t = next_sjlj_build_enter_and_setjmp ();
4460 SET_EXPR_LOCATION (t, cur_try_context->try_locus);
4461 COND_EXPR_THEN (t) = catch_seq;
4462 COND_EXPR_ELSE (t) = cur_try_context->try_body;
4463 TREE_OPERAND (try_fin, 0) = t;
4465 /* Build the complete FINALLY statement list. */
4466 t = next_sjlj_build_try_exit ();
4467 t = build_stmt (input_location, COND_EXPR,
4468 c_common_truthvalue_conversion
4469 (input_location, rethrow_decl),
4471 SET_EXPR_LOCATION (t, cur_try_context->finally_locus);
4472 append_to_statement_list (t, &TREE_OPERAND (try_fin, 1));
4474 append_to_statement_list (cur_try_context->finally_body,
4475 &TREE_OPERAND (try_fin, 1));
4477 t = tree_cons (NULL, rethrow_decl, NULL);
4478 t = build_function_call (input_location,
4479 objc_exception_throw_decl, t);
4480 t = build_stmt (input_location, COND_EXPR,
4481 c_common_truthvalue_conversion (input_location,
4484 SET_EXPR_LOCATION (t, cur_try_context->end_finally_locus);
4485 append_to_statement_list (t, &TREE_OPERAND (try_fin, 1));
4490 /* Called just after parsing the @try and its associated BODY. We now
4491 must prepare for the tricky bits -- handling the catches and finally. */
4494 objc_begin_try_stmt (location_t try_locus, tree body)
4496 struct objc_try_context *c = XCNEW (struct objc_try_context);
4497 c->outer = cur_try_context;
4499 c->try_locus = try_locus;
4500 c->end_try_locus = input_location;
4501 cur_try_context = c;
4503 /* -fobjc-exceptions is required to enable Objective-C exceptions.
4504 For example, on Darwin, ObjC exceptions require a sufficiently
4505 recent version of the runtime, so the user must ask for them
4506 explicitly. On other platforms, at the moment -fobjc-exceptions
4507 triggers -fexceptions which again is required for exceptions to
4510 if (!flag_objc_exceptions)
4512 error_at (try_locus, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4515 if (flag_objc_sjlj_exceptions)
4516 objc_mark_locals_volatile (NULL);
4519 /* Called just after parsing "@catch (parm)". Open a binding level,
4520 enter DECL into the binding level, and initialize it. Leave the
4521 binding level open while the body of the compound statement is parsed. */
4524 objc_begin_catch_clause (tree decl)
4526 tree compound, type, t;
4528 /* Begin a new scope that the entire catch clause will live in. */
4529 compound = c_begin_compound_stmt (true);
4531 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
4532 decl = build_decl (input_location,
4533 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
4534 lang_hooks.decls.pushdecl (decl);
4536 /* Since a decl is required here by syntax, don't warn if its unused. */
4537 /* ??? As opposed to __attribute__((unused))? Anyway, this appears to
4538 be what the previous objc implementation did. */
4539 TREE_USED (decl) = 1;
4540 DECL_READ_P (decl) = 1;
4542 /* Verify that the type of the catch is valid. It must be a pointer
4543 to an Objective-C class, or "id" (which is catch-all). */
4544 type = TREE_TYPE (decl);
4546 if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type)))
4548 else if (!POINTER_TYPE_P (type) || !TYPED_OBJECT (TREE_TYPE (type)))
4550 error ("@catch parameter is not a known Objective-C class type");
4551 type = error_mark_node;
4553 else if (cur_try_context->catch_list)
4555 /* Examine previous @catch clauses and see if we've already
4556 caught the type in question. */
4557 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
4558 for (; !tsi_end_p (i); tsi_next (&i))
4560 tree stmt = tsi_stmt (i);
4561 t = CATCH_TYPES (stmt);
4562 if (t == error_mark_node)
4564 if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type)))
4566 warning (0, "exception of type %<%T%> will be caught",
4568 warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>",
4569 TREE_TYPE (t ? t : objc_object_type));
4575 /* Record the data for the catch in the try context so that we can
4576 finalize it later. */
4577 t = build_stmt (input_location, CATCH_EXPR, type, compound);
4578 cur_try_context->current_catch = t;
4580 /* Initialize the decl from the EXC_PTR_EXPR we get from the runtime. */
4581 t = objc_build_exc_ptr ();
4582 t = convert (TREE_TYPE (decl), t);
4583 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
4587 /* Called just after parsing the closing brace of a @catch clause. Close
4588 the open binding level, and record a CATCH_EXPR for it. */
4591 objc_finish_catch_clause (void)
4593 tree c = cur_try_context->current_catch;
4594 cur_try_context->current_catch = NULL;
4595 cur_try_context->end_catch_locus = input_location;
4597 CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1);
4598 append_to_statement_list (c, &cur_try_context->catch_list);
4601 /* Called after parsing a @finally clause and its associated BODY.
4602 Record the body for later placement. */
4605 objc_build_finally_clause (location_t finally_locus, tree body)
4607 cur_try_context->finally_body = body;
4608 cur_try_context->finally_locus = finally_locus;
4609 cur_try_context->end_finally_locus = input_location;
4612 /* Called to finalize a @try construct. */
4615 objc_finish_try_stmt (void)
4617 struct objc_try_context *c = cur_try_context;
4620 if (c->catch_list == NULL && c->finally_body == NULL)
4621 error ("%<@try%> without %<@catch%> or %<@finally%>");
4623 /* If we're doing Darwin setjmp exceptions, build the big nasty. */
4624 if (flag_objc_sjlj_exceptions)
4626 bool save = in_late_binary_op;
4627 in_late_binary_op = true;
4628 if (!cur_try_context->finally_body)
4630 cur_try_context->finally_locus = input_location;
4631 cur_try_context->end_finally_locus = input_location;
4633 stmt = next_sjlj_build_try_catch_finally ();
4634 in_late_binary_op = save;
4638 /* Otherwise, nest the CATCH inside a FINALLY. */
4642 stmt = build_stmt (input_location, TRY_CATCH_EXPR, stmt, c->catch_list);
4643 SET_EXPR_LOCATION (stmt, cur_try_context->try_locus);
4645 if (c->finally_body)
4647 stmt = build_stmt (input_location, TRY_FINALLY_EXPR, stmt, c->finally_body);
4648 SET_EXPR_LOCATION (stmt, cur_try_context->try_locus);
4653 cur_try_context = c->outer;
4659 objc_build_throw_stmt (location_t loc, tree throw_expr)
4663 if (!flag_objc_exceptions)
4665 error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4668 if (throw_expr == NULL)
4670 /* If we're not inside a @catch block, there is no "current
4671 exception" to be rethrown. */
4672 if (cur_try_context == NULL
4673 || cur_try_context->current_catch == NULL)
4675 error_at (loc, "%<@throw%> (rethrow) used outside of a @catch block");
4679 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
4680 value that we get from the runtime. */
4681 throw_expr = objc_build_exc_ptr ();
4684 /* A throw is just a call to the runtime throw function with the
4685 object as a parameter. */
4686 args = tree_cons (NULL, throw_expr, NULL);
4687 return add_stmt (build_function_call (loc,
4688 objc_exception_throw_decl, args));
4692 objc_build_synchronized (location_t start_locus, tree mutex, tree body)
4696 /* First lock the mutex. */
4697 mutex = save_expr (mutex);
4698 args = tree_cons (NULL, mutex, NULL);
4699 call = build_function_call (input_location,
4700 objc_sync_enter_decl, args);
4701 SET_EXPR_LOCATION (call, start_locus);
4704 /* Build the mutex unlock. */
4705 args = tree_cons (NULL, mutex, NULL);
4706 call = build_function_call (input_location,
4707 objc_sync_exit_decl, args);
4708 SET_EXPR_LOCATION (call, input_location);
4710 /* Put the that and the body in a TRY_FINALLY. */
4711 objc_begin_try_stmt (start_locus, body);
4712 objc_build_finally_clause (input_location, call);
4713 return objc_finish_try_stmt ();
4717 /* Predefine the following data type:
4719 struct _objc_exception_data
4721 int buf[OBJC_JBLEN];
4725 /* The following yuckiness should prevent users from having to #include
4726 <setjmp.h> in their code... */
4728 /* Define to a harmless positive value so the below code doesn't die. */
4730 #define OBJC_JBLEN 18
4734 build_next_objc_exception_stuff (void)
4736 tree decls, temp_type, *chain = NULL;
4738 objc_exception_data_template
4739 = objc_start_struct (get_identifier (UTAG_EXCDATA));
4741 /* int buf[OBJC_JBLEN]; */
4743 temp_type = build_sized_array_type (integer_type_node, OBJC_JBLEN);
4744 decls = add_field_decl (temp_type, "buf", &chain);
4746 /* void *pointers[4]; */
4748 temp_type = build_sized_array_type (ptr_type_node, 4);
4749 add_field_decl (temp_type, "pointers", &chain);
4751 objc_finish_struct (objc_exception_data_template, decls);
4753 /* int _setjmp(...); */
4754 /* If the user includes <setjmp.h>, this shall be superseded by
4755 'int _setjmp(jmp_buf);' */
4756 temp_type = build_varargs_function_type_list (integer_type_node, NULL_TREE);
4758 = add_builtin_function (TAG_SETJMP, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
4760 /* id objc_exception_extract(struct _objc_exception_data *); */
4762 = build_function_type_list (objc_object_type,
4763 build_pointer_type (objc_exception_data_template),
4765 objc_exception_extract_decl
4766 = add_builtin_function (TAG_EXCEPTIONEXTRACT, temp_type, 0, NOT_BUILT_IN, NULL,
4768 /* void objc_exception_try_enter(struct _objc_exception_data *); */
4769 /* void objc_exception_try_exit(struct _objc_exception_data *); */
4771 = build_function_type_list (void_type_node,
4772 build_pointer_type (objc_exception_data_template),
4774 objc_exception_try_enter_decl
4775 = add_builtin_function (TAG_EXCEPTIONTRYENTER, temp_type, 0, NOT_BUILT_IN, NULL,
4777 objc_exception_try_exit_decl
4778 = add_builtin_function (TAG_EXCEPTIONTRYEXIT, temp_type, 0, NOT_BUILT_IN, NULL,
4781 /* int objc_exception_match(id, id); */
4783 = build_function_type_list (integer_type_node,
4784 objc_object_type, objc_object_type, NULL_TREE);
4785 objc_exception_match_decl
4786 = add_builtin_function (TAG_EXCEPTIONMATCH, temp_type, 0, NOT_BUILT_IN, NULL,
4789 /* id objc_assign_ivar (id, id, unsigned int); */
4790 /* id objc_assign_ivar_Fast (id, id, unsigned int)
4791 __attribute__ ((hard_coded_address (OFFS_ASSIGNIVAR_FAST))); */
4793 = build_function_type_list (objc_object_type,
4798 objc_assign_ivar_decl
4799 = add_builtin_function (TAG_ASSIGNIVAR, temp_type, 0, NOT_BUILT_IN,
4801 #ifdef OFFS_ASSIGNIVAR_FAST
4802 objc_assign_ivar_fast_decl
4803 = add_builtin_function (TAG_ASSIGNIVAR_FAST, temp_type, 0,
4804 NOT_BUILT_IN, NULL, NULL_TREE);
4805 DECL_ATTRIBUTES (objc_assign_ivar_fast_decl)
4806 = tree_cons (get_identifier ("hard_coded_address"),
4807 build_int_cst (NULL_TREE, OFFS_ASSIGNIVAR_FAST),
4810 /* Default to slower ivar method. */
4811 objc_assign_ivar_fast_decl = objc_assign_ivar_decl;
4814 /* id objc_assign_global (id, id *); */
4815 /* id objc_assign_strongCast (id, id *); */
4816 temp_type = build_function_type_list (objc_object_type,
4818 build_pointer_type (objc_object_type),
4820 objc_assign_global_decl
4821 = add_builtin_function (TAG_ASSIGNGLOBAL, temp_type, 0, NOT_BUILT_IN, NULL,
4823 objc_assign_strong_cast_decl
4824 = add_builtin_function (TAG_ASSIGNSTRONGCAST, temp_type, 0, NOT_BUILT_IN, NULL,
4829 build_objc_exception_stuff (void)
4831 tree noreturn_list, nothrow_list, temp_type;
4833 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
4834 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
4836 /* void objc_exception_throw(id) __attribute__((noreturn)); */
4837 /* void objc_sync_enter(id); */
4838 /* void objc_sync_exit(id); */
4839 temp_type = build_function_type_list (void_type_node,
4842 objc_exception_throw_decl
4843 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
4845 objc_sync_enter_decl
4846 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
4847 NULL, nothrow_list);
4849 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
4850 NULL, nothrow_list);
4853 /* Construct a C struct corresponding to ObjC class CLASS, with the same
4856 struct <classname> {
4857 struct _objc_class *isa;
4862 build_private_template (tree klass)
4864 if (!CLASS_STATIC_TEMPLATE (klass))
4866 tree record = objc_build_struct (klass,
4867 get_class_ivars (klass, false),
4868 CLASS_SUPER_NAME (klass));
4870 /* Set the TREE_USED bit for this struct, so that stab generator
4871 can emit stabs for this struct type. */
4872 if (flag_debug_only_used_symbols && TYPE_STUB_DECL (record))
4873 TREE_USED (TYPE_STUB_DECL (record)) = 1;
4877 /* Begin code generation for protocols... */
4879 /* struct _objc_protocol {
4880 struct _objc_class *isa;
4881 char *protocol_name;
4882 struct _objc_protocol **protocol_list;
4883 struct _objc__method_prototype_list *instance_methods;
4884 struct _objc__method_prototype_list *class_methods;
4888 build_protocol_template (void)
4890 tree ptype, decls, *chain = NULL;
4892 objc_protocol_template = objc_start_struct (get_identifier (UTAG_PROTOCOL));
4894 /* struct _objc_class *isa; */
4895 ptype = build_pointer_type (xref_tag (RECORD_TYPE,
4896 get_identifier (UTAG_CLASS)));
4897 decls = add_field_decl (ptype, "isa", &chain);
4899 /* char *protocol_name; */
4900 add_field_decl (string_type_node, "protocol_name", &chain);
4902 /* struct _objc_protocol **protocol_list; */
4903 ptype = build_pointer_type (build_pointer_type (objc_protocol_template));
4904 add_field_decl (ptype, "protocol_list", &chain);
4906 /* struct _objc__method_prototype_list *instance_methods; */
4907 add_field_decl (objc_method_proto_list_ptr, "instance_methods", &chain);
4909 /* struct _objc__method_prototype_list *class_methods; */
4910 add_field_decl (objc_method_proto_list_ptr, "class_methods", &chain);
4912 objc_finish_struct (objc_protocol_template, decls);
4916 build_descriptor_table_initializer (tree type, tree entries)
4918 VEC(constructor_elt,gc) *inits = NULL;
4922 VEC(constructor_elt,gc) *elts = NULL;
4924 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
4925 build_selector (METHOD_SEL_NAME (entries)));
4926 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
4927 add_objc_string (METHOD_ENCODING (entries),
4930 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
4931 objc_build_constructor (type, elts));
4933 entries = DECL_CHAIN (entries);
4937 return objc_build_constructor (build_array_type (type, 0), inits);
4940 /* struct objc_method_prototype_list {
4942 struct objc_method_prototype {
4949 build_method_prototype_list_template (tree list_type, int size)
4951 tree objc_ivar_list_record;
4952 tree array_type, decls, *chain = NULL;
4954 /* Generate an unnamed struct definition. */
4956 objc_ivar_list_record = objc_start_struct (NULL_TREE);
4958 /* int method_count; */
4959 decls = add_field_decl (integer_type_node, "method_count", &chain);
4961 /* struct objc_method method_list[]; */
4962 array_type = build_sized_array_type (list_type, size);
4963 add_field_decl (array_type, "method_list", &chain);
4965 objc_finish_struct (objc_ivar_list_record, decls);
4967 return objc_ivar_list_record;
4971 build_method_prototype_template (void)
4974 tree decls, *chain = NULL;
4976 proto_record = objc_start_struct (get_identifier (UTAG_METHOD_PROTOTYPE));
4979 decls = add_field_decl (objc_selector_type, "_cmd", &chain);
4981 /* char *method_types; */
4982 add_field_decl (string_type_node, "method_types", &chain);
4984 objc_finish_struct (proto_record, decls);
4986 return proto_record;
4990 objc_method_parm_type (tree type)
4992 type = TREE_VALUE (TREE_TYPE (type));
4993 if (TREE_CODE (type) == TYPE_DECL)
4994 type = TREE_TYPE (type);
4999 objc_encoded_type_size (tree type)
5001 int sz = int_size_in_bytes (type);
5003 /* Make all integer and enum types at least as large
5005 if (sz > 0 && INTEGRAL_TYPE_P (type))
5006 sz = MAX (sz, int_size_in_bytes (integer_type_node));
5007 /* Treat arrays as pointers, since that's how they're
5009 else if (TREE_CODE (type) == ARRAY_TYPE)
5010 sz = int_size_in_bytes (ptr_type_node);
5014 /* Encode a method prototype.
5016 The format is described in gcc/doc/objc.texi, section 'Method
5020 encode_method_prototype (tree method_decl)
5027 /* ONEWAY and BYCOPY, for remote object are the only method qualifiers. */
5028 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (method_decl)));
5030 /* Encode return type. */
5031 encode_type (objc_method_parm_type (method_decl),
5032 obstack_object_size (&util_obstack),
5033 OBJC_ENCODE_INLINE_DEFS);
5036 /* The first two arguments (self and _cmd) are pointers; account for
5038 i = int_size_in_bytes (ptr_type_node);
5039 parm_offset = 2 * i;
5040 for (parms = METHOD_SEL_ARGS (method_decl); parms;
5041 parms = DECL_CHAIN (parms))
5043 tree type = objc_method_parm_type (parms);
5044 int sz = objc_encoded_type_size (type);
5046 /* If a type size is not known, bail out. */
5049 error ("type %q+D does not have a known size",
5051 /* Pretend that the encoding succeeded; the compilation will
5052 fail nevertheless. */
5053 goto finish_encoding;
5058 sprintf (buf, "%d@0:%d", parm_offset, i);
5059 obstack_grow (&util_obstack, buf, strlen (buf));
5061 /* Argument types. */
5062 parm_offset = 2 * i;
5063 for (parms = METHOD_SEL_ARGS (method_decl); parms;
5064 parms = DECL_CHAIN (parms))
5066 tree type = objc_method_parm_type (parms);
5068 /* Process argument qualifiers for user supplied arguments. */
5069 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (parms)));
5072 encode_type (type, obstack_object_size (&util_obstack),
5073 OBJC_ENCODE_INLINE_DEFS);
5075 /* Compute offset. */
5076 sprintf (buf, "%d", parm_offset);
5077 parm_offset += objc_encoded_type_size (type);
5079 obstack_grow (&util_obstack, buf, strlen (buf));
5083 obstack_1grow (&util_obstack, '\0');
5084 result = get_identifier (XOBFINISH (&util_obstack, char *));
5085 obstack_free (&util_obstack, util_firstobj);
5090 generate_descriptor_table (tree type, const char *name, int size, tree list,
5094 VEC(constructor_elt,gc) *v = NULL;
5096 decl = start_var_decl (type, synth_id_with_class_suffix (name, proto));
5098 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, size));
5099 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, list);
5101 finish_var_decl (decl, objc_build_constructor (type, v));
5107 generate_method_descriptors (tree protocol)
5109 tree initlist, chain, method_list_template;
5112 if (!objc_method_prototype_template)
5113 objc_method_prototype_template = build_method_prototype_template ();
5115 chain = PROTOCOL_CLS_METHODS (protocol);
5118 size = list_length (chain);
5120 method_list_template
5121 = build_method_prototype_list_template (objc_method_prototype_template,
5125 = build_descriptor_table_initializer (objc_method_prototype_template,
5128 UOBJC_CLASS_METHODS_decl
5129 = generate_descriptor_table (method_list_template,
5130 "_OBJC_PROTOCOL_CLASS_METHODS",
5131 size, initlist, protocol);
5134 UOBJC_CLASS_METHODS_decl = 0;
5136 chain = PROTOCOL_NST_METHODS (protocol);
5139 size = list_length (chain);
5141 method_list_template
5142 = build_method_prototype_list_template (objc_method_prototype_template,
5145 = build_descriptor_table_initializer (objc_method_prototype_template,
5148 UOBJC_INSTANCE_METHODS_decl
5149 = generate_descriptor_table (method_list_template,
5150 "_OBJC_PROTOCOL_INSTANCE_METHODS",
5151 size, initlist, protocol);
5154 UOBJC_INSTANCE_METHODS_decl = 0;
5158 generate_protocol_references (tree plist)
5162 /* Forward declare protocols referenced. */
5163 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
5165 tree proto = TREE_VALUE (lproto);
5167 if (TREE_CODE (proto) == PROTOCOL_INTERFACE_TYPE
5168 && PROTOCOL_NAME (proto))
5170 if (! PROTOCOL_FORWARD_DECL (proto))
5171 build_protocol_reference (proto);
5173 if (PROTOCOL_LIST (proto))
5174 generate_protocol_references (PROTOCOL_LIST (proto));
5179 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
5183 objc_generate_cxx_ctor_or_dtor (bool dtor)
5185 tree fn, body, compound_stmt, ivar;
5187 /* - (id) .cxx_construct { ... return self; } */
5188 /* - (void) .cxx_construct { ... } */
5190 objc_set_method_type (MINUS_EXPR);
5191 objc_start_method_definition
5192 (objc_build_method_signature (build_tree_list (NULL_TREE,
5195 : objc_object_type),
5196 get_identifier (dtor
5198 : TAG_CXX_CONSTRUCT),
5199 make_node (TREE_LIST),
5201 body = begin_function_body ();
5202 compound_stmt = begin_compound_stmt (0);
5204 ivar = CLASS_IVARS (implementation_template);
5205 /* Destroy ivars in reverse order. */
5207 ivar = nreverse (copy_list (ivar));
5209 for (; ivar; ivar = TREE_CHAIN (ivar))
5211 if (TREE_CODE (ivar) == FIELD_DECL)
5213 tree type = TREE_TYPE (ivar);
5215 /* Call the ivar's default constructor or destructor. Do not
5216 call the destructor unless a corresponding constructor call
5217 has also been made (or is not needed). */
5218 if (MAYBE_CLASS_TYPE_P (type)
5220 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
5221 && (!TYPE_NEEDS_CONSTRUCTING (type)
5222 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5223 : (TYPE_NEEDS_CONSTRUCTING (type)
5224 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))))
5226 (build_special_member_call
5227 (build_ivar_reference (DECL_NAME (ivar)),
5228 dtor ? complete_dtor_identifier : complete_ctor_identifier,
5229 NULL, type, LOOKUP_NORMAL, tf_warning_or_error));
5233 /* The constructor returns 'self'. */
5235 finish_return_stmt (self_decl);
5237 finish_compound_stmt (compound_stmt);
5238 finish_function_body (body);
5239 fn = current_function_decl;
5241 objc_finish_method_definition (fn);
5244 /* The following routine will examine the current @interface for any
5245 non-POD C++ ivars requiring non-trivial construction and/or
5246 destruction, and then synthesize special '- .cxx_construct' and/or
5247 '- .cxx_destruct' methods which will run the appropriate
5248 construction or destruction code. Note that ivars inherited from
5249 super-classes are _not_ considered. */
5251 objc_generate_cxx_cdtors (void)
5253 bool need_ctor = false, need_dtor = false;
5256 /* We do not want to do this for categories, since they do not have
5259 if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE)
5262 /* First, determine if we even need a constructor and/or destructor. */
5264 for (ivar = CLASS_IVARS (implementation_template); ivar;
5265 ivar = TREE_CHAIN (ivar))
5267 if (TREE_CODE (ivar) == FIELD_DECL)
5269 tree type = TREE_TYPE (ivar);
5271 if (MAYBE_CLASS_TYPE_P (type))
5273 if (TYPE_NEEDS_CONSTRUCTING (type)
5274 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
5275 /* NB: If a default constructor is not available, we will not
5276 be able to initialize this ivar; the add_instance_variable()
5277 routine will already have warned about this. */
5280 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
5281 && (!TYPE_NEEDS_CONSTRUCTING (type)
5282 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5283 /* NB: If a default constructor is not available, we will not
5284 call the destructor either, for symmetry. */
5290 /* Generate '- .cxx_construct' if needed. */
5293 objc_generate_cxx_ctor_or_dtor (false);
5295 /* Generate '- .cxx_destruct' if needed. */
5298 objc_generate_cxx_ctor_or_dtor (true);
5300 /* The 'imp_list' variable points at an imp_entry record for the current
5301 @implementation. Record the existence of '- .cxx_construct' and/or
5302 '- .cxx_destruct' methods therein; it will be included in the
5303 metadata for the class. */
5304 if (flag_next_runtime)
5305 imp_list->has_cxx_cdtors = (need_ctor || need_dtor);
5309 /* For each protocol which was referenced either from a @protocol()
5310 expression, or because a class/category implements it (then a
5311 pointer to the protocol is stored in the struct describing the
5312 class/category), we create a statically allocated instance of the
5313 Protocol class. The code is written in such a way as to generate
5314 as few Protocol objects as possible; we generate a unique Protocol
5315 instance for each protocol, and we don't generate a Protocol
5316 instance if the protocol is never referenced (either from a
5317 @protocol() or from a class/category implementation). These
5318 statically allocated objects can be referred to via the static
5319 (that is, private to this module) symbols _OBJC_PROTOCOL_n.
5321 The statically allocated Protocol objects that we generate here
5322 need to be fixed up at runtime in order to be used: the 'isa'
5323 pointer of the objects need to be set up to point to the 'Protocol'
5324 class, as known at runtime.
5326 The NeXT runtime fixes up all protocols at program startup time,
5327 before main() is entered. It uses a low-level trick to look up all
5328 those symbols, then loops on them and fixes them up.
5330 The GNU runtime as well fixes up all protocols before user code
5331 from the module is executed; it requires pointers to those symbols
5332 to be put in the objc_symtab (which is then passed as argument to
5333 the function __objc_exec_class() which the compiler sets up to be
5334 executed automatically when the module is loaded); setup of those
5335 Protocol objects happen in two ways in the GNU runtime: all
5336 Protocol objects referred to by a class or category implementation
5337 are fixed up when the class/category is loaded; all Protocol
5338 objects referred to by a @protocol() expression are added by the
5339 compiler to the list of statically allocated instances to fixup
5340 (the same list holding the statically allocated constant string
5341 objects). Because, as explained above, the compiler generates as
5342 few Protocol objects as possible, some Protocol object might end up
5343 being referenced multiple times when compiled with the GNU runtime,
5344 and end up being fixed up multiple times at runtime initialization.
5345 But that doesn't hurt, it's just a little inefficient. */
5348 generate_protocols (void)
5352 tree initlist, protocol_name_expr, refs_decl, refs_expr;
5354 /* If a protocol was directly referenced, pull in indirect references. */
5355 for (p = protocol_chain; p; p = TREE_CHAIN (p))
5356 if (PROTOCOL_FORWARD_DECL (p) && PROTOCOL_LIST (p))
5357 generate_protocol_references (PROTOCOL_LIST (p));
5359 for (p = protocol_chain; p; p = TREE_CHAIN (p))
5361 tree nst_methods = PROTOCOL_NST_METHODS (p);
5362 tree cls_methods = PROTOCOL_CLS_METHODS (p);
5364 /* If protocol wasn't referenced, don't generate any code. */
5365 decl = PROTOCOL_FORWARD_DECL (p);
5370 /* Make sure we link in the Protocol class. */
5371 add_class_reference (get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
5375 if (! METHOD_ENCODING (nst_methods))
5377 encoding = encode_method_prototype (nst_methods);
5378 METHOD_ENCODING (nst_methods) = encoding;
5380 nst_methods = DECL_CHAIN (nst_methods);
5385 if (! METHOD_ENCODING (cls_methods))
5387 encoding = encode_method_prototype (cls_methods);
5388 METHOD_ENCODING (cls_methods) = encoding;
5391 cls_methods = DECL_CHAIN (cls_methods);
5393 generate_method_descriptors (p);
5395 if (PROTOCOL_LIST (p))
5396 refs_decl = generate_protocol_list (p);
5400 /* static struct objc_protocol _OBJC_PROTOCOL_<mumble>; */
5401 protocol_name_expr = add_objc_string (PROTOCOL_NAME (p), class_names);
5404 refs_expr = convert (build_pointer_type (build_pointer_type
5405 (objc_protocol_template)),
5406 build_unary_op (input_location,
5407 ADDR_EXPR, refs_decl, 0));
5409 refs_expr = build_int_cst (NULL_TREE, 0);
5411 /* UOBJC_INSTANCE_METHODS_decl/UOBJC_CLASS_METHODS_decl are set
5412 by generate_method_descriptors, which is called above. */
5413 initlist = build_protocol_initializer (TREE_TYPE (decl),
5414 protocol_name_expr, refs_expr,
5415 UOBJC_INSTANCE_METHODS_decl,
5416 UOBJC_CLASS_METHODS_decl);
5417 finish_var_decl (decl, initlist);
5422 build_protocol_initializer (tree type, tree protocol_name,
5423 tree protocol_list, tree instance_methods,
5427 tree cast_type = build_pointer_type
5428 (xref_tag (RECORD_TYPE,
5429 get_identifier (UTAG_CLASS)));
5430 VEC(constructor_elt,gc) *inits = NULL;
5432 /* Filling the "isa" in with one allows the runtime system to
5433 detect that the version change...should remove before final release. */
5435 expr = build_int_cst (cast_type, PROTOCOL_VERSION);
5436 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
5437 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, protocol_name);
5438 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, protocol_list);
5440 if (!instance_methods)
5441 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, 0));
5444 expr = convert (objc_method_proto_list_ptr,
5445 build_unary_op (input_location,
5446 ADDR_EXPR, instance_methods, 0));
5447 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
5451 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, 0));
5454 expr = convert (objc_method_proto_list_ptr,
5455 build_unary_op (input_location,
5456 ADDR_EXPR, class_methods, 0));
5457 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
5460 return objc_build_constructor (type, inits);
5463 /* struct _objc_category {
5464 char *category_name;
5466 struct _objc_method_list *instance_methods;
5467 struct _objc_method_list *class_methods;
5468 struct _objc_protocol_list *protocols;
5472 build_category_template (void)
5474 tree ptype, decls, *chain = NULL;
5476 objc_category_template = objc_start_struct (get_identifier (UTAG_CATEGORY));
5478 /* char *category_name; */
5479 decls = add_field_decl (string_type_node, "category_name", &chain);
5481 /* char *class_name; */
5482 add_field_decl (string_type_node, "class_name", &chain);
5484 /* struct _objc_method_list *instance_methods; */
5485 add_field_decl (objc_method_list_ptr, "instance_methods", &chain);
5487 /* struct _objc_method_list *class_methods; */
5488 add_field_decl (objc_method_list_ptr, "class_methods", &chain);
5490 /* struct _objc_protocol **protocol_list; */
5491 ptype = build_pointer_type (build_pointer_type (objc_protocol_template));
5492 add_field_decl (ptype, "protocol_list", &chain);
5494 objc_finish_struct (objc_category_template, decls);
5497 /* struct _objc_selector {
5503 build_selector_template (void)
5505 tree decls, *chain = NULL;
5507 objc_selector_template = objc_start_struct (get_identifier (UTAG_SELECTOR));
5510 decls = add_field_decl (objc_selector_type, "sel_id", &chain);
5512 /* char *sel_type; */
5513 add_field_decl (string_type_node, "sel_type", &chain);
5515 objc_finish_struct (objc_selector_template, decls);
5518 /* struct _objc_class {
5519 struct _objc_class *isa;
5520 struct _objc_class *super_class;
5525 struct _objc_ivar_list *ivars;
5526 struct _objc_method_list *methods;
5527 #ifdef __NEXT_RUNTIME__
5528 struct objc_cache *cache;
5530 struct sarray *dtable;
5531 struct _objc_class *subclass_list;
5532 struct _objc_class *sibling_class;
5534 struct _objc_protocol_list *protocols;
5535 #ifdef __NEXT_RUNTIME__
5538 void *gc_object_type;
5541 /* NB: The 'sel_id' and 'gc_object_type' fields are not being used by
5542 the NeXT/Apple runtime; still, the compiler must generate them to
5543 maintain backward binary compatibility (and to allow for future
5547 build_class_template (void)
5549 tree ptype, decls, *chain = NULL;
5551 objc_class_template = objc_start_struct (get_identifier (UTAG_CLASS));
5553 /* struct _objc_class *isa; */
5554 decls = add_field_decl (build_pointer_type (objc_class_template),
5557 /* struct _objc_class *super_class; */
5558 add_field_decl (build_pointer_type (objc_class_template),
5559 "super_class", &chain);
5562 add_field_decl (string_type_node, "name", &chain);
5565 add_field_decl (long_integer_type_node, "version", &chain);
5568 add_field_decl (long_integer_type_node, "info", &chain);
5570 /* long instance_size; */
5571 add_field_decl (long_integer_type_node, "instance_size", &chain);
5573 /* struct _objc_ivar_list *ivars; */
5574 add_field_decl (objc_ivar_list_ptr,"ivars", &chain);
5576 /* struct _objc_method_list *methods; */
5577 add_field_decl (objc_method_list_ptr, "methods", &chain);
5579 if (flag_next_runtime)
5581 /* struct objc_cache *cache; */
5582 ptype = build_pointer_type (xref_tag (RECORD_TYPE,
5583 get_identifier ("objc_cache")));
5584 add_field_decl (ptype, "cache", &chain);
5588 /* struct sarray *dtable; */
5589 ptype = build_pointer_type(xref_tag (RECORD_TYPE,
5590 get_identifier ("sarray")));
5591 add_field_decl (ptype, "dtable", &chain);
5593 /* struct objc_class *subclass_list; */
5594 ptype = build_pointer_type (objc_class_template);
5595 add_field_decl (ptype, "subclass_list", &chain);
5597 /* struct objc_class *sibling_class; */
5598 ptype = build_pointer_type (objc_class_template);
5599 add_field_decl (ptype, "sibling_class", &chain);
5602 /* struct _objc_protocol **protocol_list; */
5603 ptype = build_pointer_type (build_pointer_type
5604 (xref_tag (RECORD_TYPE,
5605 get_identifier (UTAG_PROTOCOL))));
5606 add_field_decl (ptype, "protocol_list", &chain);
5608 if (flag_next_runtime)
5611 add_field_decl (build_pointer_type (void_type_node), "sel_id", &chain);
5614 /* void *gc_object_type; */
5615 add_field_decl (build_pointer_type (void_type_node),
5616 "gc_object_type", &chain);
5618 objc_finish_struct (objc_class_template, decls);
5621 /* Generate appropriate forward declarations for an implementation. */
5624 synth_forward_declarations (void)
5628 /* static struct objc_class _OBJC_CLASS_<my_name>; */
5629 UOBJC_CLASS_decl = build_metadata_decl ("_OBJC_CLASS",
5630 objc_class_template);
5632 /* static struct objc_class _OBJC_METACLASS_<my_name>; */
5633 UOBJC_METACLASS_decl = build_metadata_decl ("_OBJC_METACLASS",
5634 objc_class_template);
5636 /* Pre-build the following entities - for speed/convenience. */
5638 an_id = get_identifier ("super_class");
5639 ucls_super_ref = objc_build_component_ref (UOBJC_CLASS_decl, an_id);
5640 uucls_super_ref = objc_build_component_ref (UOBJC_METACLASS_decl, an_id);
5644 error_with_ivar (const char *message, tree decl)
5646 error_at (DECL_SOURCE_LOCATION (decl), "%s %qs",
5647 message, identifier_to_locale (gen_declaration (decl)));
5652 check_ivars (tree inter, tree imp)
5654 tree intdecls = CLASS_RAW_IVARS (inter);
5655 tree impdecls = CLASS_RAW_IVARS (imp);
5662 if (intdecls && TREE_CODE (intdecls) == TYPE_DECL)
5663 intdecls = TREE_CHAIN (intdecls);
5665 if (intdecls == 0 && impdecls == 0)
5667 if (intdecls == 0 || impdecls == 0)
5669 error ("inconsistent instance variable specification");
5673 t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
5675 if (!comptypes (t1, t2)
5676 || !tree_int_cst_equal (DECL_INITIAL (intdecls),
5677 DECL_INITIAL (impdecls)))
5679 if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
5681 error_with_ivar ("conflicting instance variable type",
5683 error_with_ivar ("previous declaration of",
5686 else /* both the type and the name don't match */
5688 error ("inconsistent instance variable specification");
5693 else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
5695 error_with_ivar ("conflicting instance variable name",
5697 error_with_ivar ("previous declaration of",
5701 intdecls = DECL_CHAIN (intdecls);
5702 impdecls = DECL_CHAIN (impdecls);
5706 /* Set 'objc_super_template' to the data type node for 'struct _objc_super'.
5707 This needs to be done just once per compilation. */
5709 /* struct _objc_super {
5710 struct _objc_object *self;
5711 struct _objc_class *super_class;
5715 build_super_template (void)
5717 tree decls, *chain = NULL;
5719 objc_super_template = objc_start_struct (get_identifier (UTAG_SUPER));
5721 /* struct _objc_object *self; */
5722 decls = add_field_decl (objc_object_type, "self", &chain);
5724 /* struct _objc_class *super_class; */
5725 add_field_decl (build_pointer_type (objc_class_template),
5726 "super_class", &chain);
5728 objc_finish_struct (objc_super_template, decls);
5731 /* struct _objc_ivar {
5738 build_ivar_template (void)
5740 tree objc_ivar_id, objc_ivar_record;
5741 tree decls, *chain = NULL;
5743 objc_ivar_id = get_identifier (UTAG_IVAR);
5744 objc_ivar_record = objc_start_struct (objc_ivar_id);
5746 /* char *ivar_name; */
5747 decls = add_field_decl (string_type_node, "ivar_name", &chain);
5749 /* char *ivar_type; */
5750 add_field_decl (string_type_node, "ivar_type", &chain);
5752 /* int ivar_offset; */
5753 add_field_decl (integer_type_node, "ivar_offset", &chain);
5755 objc_finish_struct (objc_ivar_record, decls);
5757 return objc_ivar_record;
5762 struct objc_ivar ivar_list[ivar_count];
5766 build_ivar_list_template (tree list_type, int size)
5768 tree objc_ivar_list_record;
5769 tree array_type, decls, *chain = NULL;
5771 objc_ivar_list_record = objc_start_struct (NULL_TREE);
5773 /* int ivar_count; */
5774 decls = add_field_decl (integer_type_node, "ivar_count", &chain);
5776 /* struct objc_ivar ivar_list[]; */
5777 array_type = build_sized_array_type (list_type, size);
5778 add_field_decl (array_type, "ivar_list", &chain);
5780 objc_finish_struct (objc_ivar_list_record, decls);
5782 return objc_ivar_list_record;
5786 struct _objc__method_prototype_list *method_next;
5788 struct objc_method method_list[method_count];
5792 build_method_list_template (tree list_type, int size)
5794 tree objc_ivar_list_record;
5795 tree array_type, decls, *chain = NULL;
5797 objc_ivar_list_record = objc_start_struct (NULL_TREE);
5799 /* struct _objc__method_prototype_list *method_next; */
5800 decls = add_field_decl (objc_method_proto_list_ptr, "method_next", &chain);
5802 /* int method_count; */
5803 add_field_decl (integer_type_node, "method_count", &chain);
5805 /* struct objc_method method_list[]; */
5806 array_type = build_sized_array_type (list_type, size);
5807 add_field_decl (array_type, "method_list", &chain);
5809 objc_finish_struct (objc_ivar_list_record, decls);
5811 return objc_ivar_list_record;
5815 build_ivar_list_initializer (tree type, tree field_decl)
5817 VEC(constructor_elt,gc) *inits = NULL;
5821 VEC(constructor_elt,gc) *ivar = NULL;
5825 if (DECL_NAME (field_decl))
5826 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE,
5827 add_objc_string (DECL_NAME (field_decl),
5830 /* Unnamed bit-field ivar (yuck). */
5831 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, build_int_cst (NULL_TREE, 0));
5834 encode_field_decl (field_decl,
5835 obstack_object_size (&util_obstack),
5836 OBJC_ENCODE_DONT_INLINE_DEFS);
5838 /* Null terminate string. */
5839 obstack_1grow (&util_obstack, 0);
5840 id = add_objc_string (get_identifier (XOBFINISH (&util_obstack, char *)),
5842 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, id);
5843 obstack_free (&util_obstack, util_firstobj);
5846 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, byte_position (field_decl));
5847 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
5848 objc_build_constructor (type, ivar));
5850 field_decl = DECL_CHAIN (field_decl);
5851 while (field_decl && TREE_CODE (field_decl) != FIELD_DECL);
5855 return objc_build_constructor (build_array_type (type, 0), inits);
5859 generate_ivars_list (tree type, const char *name, int size, tree list)
5862 VEC(constructor_elt,gc) *inits = NULL;
5864 decl = start_var_decl (type, synth_id_with_class_suffix
5865 (name, objc_implementation_context));
5867 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, size));
5868 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, list);
5870 finish_var_decl (decl,
5871 objc_build_constructor (TREE_TYPE (decl), inits));
5876 /* Count only the fields occurring in T. */
5879 ivar_list_length (tree t)
5883 for (; t; t = DECL_CHAIN (t))
5884 if (TREE_CODE (t) == FIELD_DECL)
5891 generate_ivar_lists (void)
5893 tree initlist, ivar_list_template, chain;
5896 generating_instance_variables = 1;
5898 if (!objc_ivar_template)
5899 objc_ivar_template = build_ivar_template ();
5901 /* Only generate class variables for the root of the inheritance
5902 hierarchy since these will be the same for every class. */
5904 if (CLASS_SUPER_NAME (implementation_template) == NULL_TREE
5905 && (chain = TYPE_FIELDS (objc_class_template)))
5907 size = ivar_list_length (chain);
5909 ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
5910 initlist = build_ivar_list_initializer (objc_ivar_template, chain);
5912 UOBJC_CLASS_VARIABLES_decl
5913 = generate_ivars_list (ivar_list_template, "_OBJC_CLASS_VARIABLES",
5917 UOBJC_CLASS_VARIABLES_decl = 0;
5919 chain = CLASS_IVARS (implementation_template);
5922 size = ivar_list_length (chain);
5923 ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
5924 initlist = build_ivar_list_initializer (objc_ivar_template, chain);
5926 UOBJC_INSTANCE_VARIABLES_decl
5927 = generate_ivars_list (ivar_list_template, "_OBJC_INSTANCE_VARIABLES",
5931 UOBJC_INSTANCE_VARIABLES_decl = 0;
5933 generating_instance_variables = 0;
5937 build_dispatch_table_initializer (tree type, tree entries)
5939 VEC(constructor_elt,gc) *inits = NULL;
5943 VEC(constructor_elt,gc) *elems = NULL;
5946 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE,
5947 build_selector (METHOD_SEL_NAME (entries)));
5949 /* Generate the method encoding if we don't have one already. */
5950 if (! METHOD_ENCODING (entries))
5951 METHOD_ENCODING (entries) =
5952 encode_method_prototype (entries);
5954 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE,
5955 add_objc_string (METHOD_ENCODING (entries),
5958 expr = convert (ptr_type_node,
5959 build_unary_op (input_location, ADDR_EXPR,
5960 METHOD_DEFINITION (entries), 1));
5961 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE, expr);
5963 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
5964 objc_build_constructor (type, elems));
5966 entries = DECL_CHAIN (entries);
5970 return objc_build_constructor (build_array_type (type, 0), inits);
5973 /* To accomplish method prototyping without generating all kinds of
5974 inane warnings, the definition of the dispatch table entries were
5977 struct objc_method { SEL _cmd; ...; id (*_imp)(); };
5979 struct objc_method { SEL _cmd; ...; void *_imp; }; */
5982 build_method_template (void)
5985 tree decls, *chain = NULL;
5987 _SLT_record = objc_start_struct (get_identifier (UTAG_METHOD));
5990 decls = add_field_decl (objc_selector_type, "_cmd", &chain);
5992 /* char *method_types; */
5993 add_field_decl (string_type_node, "method_types", &chain);
5996 add_field_decl (build_pointer_type (void_type_node), "_imp", &chain);
5998 objc_finish_struct (_SLT_record, decls);
6005 generate_dispatch_table (tree type, const char *name, int size, tree list)
6008 VEC(constructor_elt,gc) *v = NULL;
6010 decl = start_var_decl (type, synth_id_with_class_suffix
6011 (name, objc_implementation_context));
6013 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
6014 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (integer_type_node, size));
6015 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, list);
6017 finish_var_decl (decl,
6018 objc_build_constructor (TREE_TYPE (decl), v));
6024 mark_referenced_methods (void)
6026 struct imp_entry *impent;
6029 for (impent = imp_list; impent; impent = impent->next)
6031 chain = CLASS_CLS_METHODS (impent->imp_context);
6034 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain)));
6035 chain = DECL_CHAIN (chain);
6038 chain = CLASS_NST_METHODS (impent->imp_context);
6041 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain)));
6042 chain = DECL_CHAIN (chain);
6048 generate_dispatch_tables (void)
6050 tree initlist, chain, method_list_template;
6053 if (!objc_method_template)
6054 objc_method_template = build_method_template ();
6056 chain = CLASS_CLS_METHODS (objc_implementation_context);
6059 size = list_length (chain);
6061 method_list_template
6062 = build_method_list_template (objc_method_template, size);
6064 = build_dispatch_table_initializer (objc_method_template, chain);
6066 UOBJC_CLASS_METHODS_decl
6067 = generate_dispatch_table (method_list_template,
6068 ((TREE_CODE (objc_implementation_context)
6069 == CLASS_IMPLEMENTATION_TYPE)
6070 ? "_OBJC_CLASS_METHODS"
6071 : "_OBJC_CATEGORY_CLASS_METHODS"),
6075 UOBJC_CLASS_METHODS_decl = 0;
6077 chain = CLASS_NST_METHODS (objc_implementation_context);
6080 size = list_length (chain);
6082 method_list_template
6083 = build_method_list_template (objc_method_template, size);
6085 = build_dispatch_table_initializer (objc_method_template, chain);
6087 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
6088 UOBJC_INSTANCE_METHODS_decl
6089 = generate_dispatch_table (method_list_template,
6090 "_OBJC_INSTANCE_METHODS",
6093 /* We have a category. */
6094 UOBJC_INSTANCE_METHODS_decl
6095 = generate_dispatch_table (method_list_template,
6096 "_OBJC_CATEGORY_INSTANCE_METHODS",
6100 UOBJC_INSTANCE_METHODS_decl = 0;
6104 generate_protocol_list (tree i_or_p)
6106 tree array_type, ptype, refs_decl, lproto, e, plist;
6108 const char *ref_name;
6109 VEC(constructor_elt,gc) *v = NULL;
6111 if (TREE_CODE (i_or_p) == CLASS_INTERFACE_TYPE
6112 || TREE_CODE (i_or_p) == CATEGORY_INTERFACE_TYPE)
6113 plist = CLASS_PROTOCOL_LIST (i_or_p);
6114 else if (TREE_CODE (i_or_p) == PROTOCOL_INTERFACE_TYPE)
6115 plist = PROTOCOL_LIST (i_or_p);
6120 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
6121 if (TREE_CODE (TREE_VALUE (lproto)) == PROTOCOL_INTERFACE_TYPE
6122 && PROTOCOL_FORWARD_DECL (TREE_VALUE (lproto)))
6125 /* Build initializer. */
6126 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6127 e = build_int_cst (build_pointer_type (objc_protocol_template), size);
6128 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, e);
6130 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
6132 tree pval = TREE_VALUE (lproto);
6134 if (TREE_CODE (pval) == PROTOCOL_INTERFACE_TYPE
6135 && PROTOCOL_FORWARD_DECL (pval))
6137 e = build_unary_op (input_location, ADDR_EXPR,
6138 PROTOCOL_FORWARD_DECL (pval), 0);
6139 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, e);
6143 /* static struct objc_protocol *refs[n]; */
6145 if (TREE_CODE (i_or_p) == PROTOCOL_INTERFACE_TYPE)
6146 ref_name = synth_id_with_class_suffix ("_OBJC_PROTOCOL_REFS", i_or_p);
6147 else if (TREE_CODE (i_or_p) == CLASS_INTERFACE_TYPE)
6148 ref_name = synth_id_with_class_suffix ("_OBJC_CLASS_PROTOCOLS", i_or_p);
6149 else if (TREE_CODE (i_or_p) == CATEGORY_INTERFACE_TYPE)
6150 ref_name = synth_id_with_class_suffix ("_OBJC_CATEGORY_PROTOCOLS", i_or_p);
6154 ptype = build_pointer_type (objc_protocol_template);
6155 array_type = build_sized_array_type (ptype, size + 3);
6156 refs_decl = start_var_decl (array_type, ref_name);
6158 finish_var_decl (refs_decl,
6159 objc_build_constructor (TREE_TYPE (refs_decl), v));
6165 build_category_initializer (tree type, tree cat_name, tree class_name,
6166 tree instance_methods, tree class_methods,
6170 VEC(constructor_elt,gc) *v = NULL;
6172 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, cat_name);
6173 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, class_name);
6175 if (!instance_methods)
6176 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6179 expr = convert (objc_method_list_ptr,
6180 build_unary_op (input_location, ADDR_EXPR,
6181 instance_methods, 0));
6182 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6185 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6188 expr = convert (objc_method_list_ptr,
6189 build_unary_op (input_location, ADDR_EXPR,
6191 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6194 /* protocol_list = */
6196 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6199 expr = convert (build_pointer_type
6201 (objc_protocol_template)),
6202 build_unary_op (input_location, ADDR_EXPR,
6204 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6207 return objc_build_constructor (type, v);
6210 /* struct _objc_class {
6211 struct objc_class *isa;
6212 struct objc_class *super_class;
6217 struct objc_ivar_list *ivars;
6218 struct objc_method_list *methods;
6219 if (flag_next_runtime)
6220 struct objc_cache *cache;
6222 struct sarray *dtable;
6223 struct objc_class *subclass_list;
6224 struct objc_class *sibling_class;
6226 struct objc_protocol_list *protocols;
6227 if (flag_next_runtime)
6229 void *gc_object_type;
6233 build_shared_structure_initializer (tree type, tree isa, tree super,
6234 tree name, tree size, int status,
6235 tree dispatch_table, tree ivar_list,
6239 VEC(constructor_elt,gc) *v = NULL;
6242 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, isa);
6245 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, super);
6248 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, default_conversion (name));
6251 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6252 build_int_cst (long_integer_type_node, 0));
6255 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6256 build_int_cst (long_integer_type_node, status));
6258 /* instance_size = */
6259 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6260 convert (long_integer_type_node, size));
6262 /* objc_ivar_list = */
6264 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6267 expr = convert (objc_ivar_list_ptr,
6268 build_unary_op (input_location, ADDR_EXPR,
6270 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6273 /* objc_method_list = */
6274 if (!dispatch_table)
6275 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6278 expr = convert (objc_method_list_ptr,
6279 build_unary_op (input_location, ADDR_EXPR,
6280 dispatch_table, 0));
6281 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6284 if (flag_next_runtime)
6285 /* method_cache = */
6286 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6290 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6292 /* subclass_list = */
6293 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6295 /* sibling_class = */
6296 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6299 /* protocol_list = */
6300 if (! protocol_list)
6301 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6304 expr = convert (build_pointer_type
6306 (objc_protocol_template)),
6307 build_unary_op (input_location, ADDR_EXPR,
6309 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6312 if (flag_next_runtime)
6314 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6316 /* gc_object_type = NULL */
6317 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6319 return objc_build_constructor (type, v);
6322 /* Retrieve category interface CAT_NAME (if any) associated with CLASS. */
6325 lookup_category (tree klass, tree cat_name)
6327 tree category = CLASS_CATEGORY_LIST (klass);
6329 while (category && CLASS_SUPER_NAME (category) != cat_name)
6330 category = CLASS_CATEGORY_LIST (category);
6334 /* static struct objc_category _OBJC_CATEGORY_<name> = { ... }; */
6337 generate_category (struct imp_entry *impent)
6339 tree initlist, cat_name_expr, class_name_expr;
6340 tree protocol_decl, category;
6341 tree cat = impent->imp_context;
6343 implementation_template = impent->imp_template;
6344 UOBJC_CLASS_decl = impent->class_decl;
6345 UOBJC_METACLASS_decl = impent->meta_decl;
6347 add_class_reference (CLASS_NAME (cat));
6348 cat_name_expr = add_objc_string (CLASS_SUPER_NAME (cat), class_names);
6350 class_name_expr = add_objc_string (CLASS_NAME (cat), class_names);
6352 category = lookup_category (implementation_template,
6353 CLASS_SUPER_NAME (cat));
6355 if (category && CLASS_PROTOCOL_LIST (category))
6357 generate_protocol_references (CLASS_PROTOCOL_LIST (category));
6358 protocol_decl = generate_protocol_list (category);
6363 initlist = build_category_initializer (TREE_TYPE (UOBJC_CLASS_decl),
6364 cat_name_expr, class_name_expr,
6365 UOBJC_INSTANCE_METHODS_decl,
6366 UOBJC_CLASS_METHODS_decl,
6368 /* Finish and initialize the forward decl. */
6369 finish_var_decl (UOBJC_CLASS_decl, initlist);
6372 /* static struct objc_class _OBJC_METACLASS_Foo={ ... };
6373 static struct objc_class _OBJC_CLASS_Foo={ ... }; */
6376 generate_shared_structures (struct imp_entry *impent)
6378 tree name_expr, super_expr, root_expr;
6379 tree my_root_id, my_super_id;
6380 tree cast_type, initlist, protocol_decl;
6383 objc_implementation_context = impent->imp_context;
6384 implementation_template = impent->imp_template;
6385 UOBJC_CLASS_decl = impent->class_decl;
6386 UOBJC_METACLASS_decl = impent->meta_decl;
6387 cls_flags = impent->has_cxx_cdtors ? CLS_HAS_CXX_STRUCTORS : 0 ;
6389 my_super_id = CLASS_SUPER_NAME (implementation_template);
6392 add_class_reference (my_super_id);
6394 /* Compute "my_root_id" - this is required for code generation.
6395 the "isa" for all meta class structures points to the root of
6396 the inheritance hierarchy (e.g. "__Object")... */
6397 my_root_id = my_super_id;
6400 tree my_root_int = lookup_interface (my_root_id);
6402 if (my_root_int && CLASS_SUPER_NAME (my_root_int))
6403 my_root_id = CLASS_SUPER_NAME (my_root_int);
6410 /* No super class. */
6411 my_root_id = CLASS_NAME (implementation_template);
6413 cast_type = build_pointer_type (objc_class_template);
6414 name_expr = add_objc_string (CLASS_NAME (implementation_template),
6417 /* Install class `isa' and `super' pointers at runtime. */
6419 super_expr = add_objc_string (my_super_id, class_names);
6421 super_expr = integer_zero_node;
6423 super_expr = build_c_cast (input_location,
6424 cast_type, super_expr); /* cast! */
6426 root_expr = add_objc_string (my_root_id, class_names);
6427 root_expr = build_c_cast (input_location, cast_type, root_expr); /* cast! */
6429 if (CLASS_PROTOCOL_LIST (implementation_template))
6431 generate_protocol_references
6432 (CLASS_PROTOCOL_LIST (implementation_template));
6433 protocol_decl = generate_protocol_list (implementation_template);
6438 /* static struct objc_class _OBJC_METACLASS_Foo = { ... }; */
6441 = build_shared_structure_initializer
6442 (TREE_TYPE (UOBJC_METACLASS_decl),
6443 root_expr, super_expr, name_expr,
6444 convert (integer_type_node, TYPE_SIZE_UNIT (objc_class_template)),
6446 UOBJC_CLASS_METHODS_decl,
6447 UOBJC_CLASS_VARIABLES_decl,
6450 finish_var_decl (UOBJC_METACLASS_decl, initlist);
6452 /* static struct objc_class _OBJC_CLASS_Foo={ ... }; */
6455 = build_shared_structure_initializer
6456 (TREE_TYPE (UOBJC_CLASS_decl),
6457 build_unary_op (input_location, ADDR_EXPR, UOBJC_METACLASS_decl, 0),
6458 super_expr, name_expr,
6459 convert (integer_type_node,
6460 TYPE_SIZE_UNIT (CLASS_STATIC_TEMPLATE
6461 (implementation_template))),
6462 1 /*CLS_FACTORY*/ | cls_flags,
6463 UOBJC_INSTANCE_METHODS_decl,
6464 UOBJC_INSTANCE_VARIABLES_decl,
6467 finish_var_decl (UOBJC_CLASS_decl, initlist);
6472 synth_id_with_class_suffix (const char *preamble, tree ctxt)
6474 static char string[BUFSIZE];
6476 if (TREE_CODE (ctxt) == CLASS_IMPLEMENTATION_TYPE
6477 || TREE_CODE (ctxt) == CLASS_INTERFACE_TYPE)
6479 sprintf (string, "%s_%s", preamble,
6480 IDENTIFIER_POINTER (CLASS_NAME (ctxt)));
6482 else if (TREE_CODE (ctxt) == CATEGORY_IMPLEMENTATION_TYPE
6483 || TREE_CODE (ctxt) == CATEGORY_INTERFACE_TYPE)
6485 /* We have a category. */
6486 const char *const class_name
6487 = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
6488 const char *const class_super_name
6489 = IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context));
6490 sprintf (string, "%s_%s_%s", preamble, class_name, class_super_name);
6492 else if (TREE_CODE (ctxt) == PROTOCOL_INTERFACE_TYPE)
6494 const char *protocol_name = IDENTIFIER_POINTER (PROTOCOL_NAME (ctxt));
6495 sprintf (string, "%s_%s", preamble, protocol_name);
6503 /* If type is empty or only type qualifiers are present, add default
6504 type of id (otherwise grokdeclarator will default to int). */
6507 adjust_type_for_id_default (tree type)
6510 type = make_node (TREE_LIST);
6512 if (!TREE_VALUE (type))
6513 TREE_VALUE (type) = objc_object_type;
6514 else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE
6515 && TYPED_OBJECT (TREE_VALUE (type)))
6516 error ("can not use an object as parameter to a method");
6523 selector ':' '(' typename ')' identifier
6526 Transform an Objective-C keyword argument into
6527 the C equivalent parameter declarator.
6529 In: key_name, an "identifier_node" (optional).
6530 arg_type, a "tree_list" (optional).
6531 arg_name, an "identifier_node".
6532 attributes, a optional tree containing param attributes.
6534 Note: It would be really nice to strongly type the preceding
6535 arguments in the function prototype; however, then I
6536 could not use the "accessor" macros defined in "tree.h".
6538 Out: an instance of "keyword_decl". */
6541 objc_build_keyword_decl (tree key_name, tree arg_type,
6542 tree arg_name, tree attributes)
6547 warning_at (input_location, OPT_Wattributes,
6548 "method parameter attributes are not available in this "
6549 "version of the compiler, (ignored)");
6551 /* If no type is specified, default to "id". */
6552 arg_type = adjust_type_for_id_default (arg_type);
6554 keyword_decl = make_node (KEYWORD_DECL);
6556 TREE_TYPE (keyword_decl) = arg_type;
6557 KEYWORD_ARG_NAME (keyword_decl) = arg_name;
6558 KEYWORD_KEY_NAME (keyword_decl) = key_name;
6560 return keyword_decl;
6563 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
6566 build_keyword_selector (tree selector)
6569 tree key_chain, key_name;
6572 /* Scan the selector to see how much space we'll need. */
6573 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
6575 if (TREE_CODE (selector) == KEYWORD_DECL)
6576 key_name = KEYWORD_KEY_NAME (key_chain);
6577 else if (TREE_CODE (selector) == TREE_LIST)
6578 key_name = TREE_PURPOSE (key_chain);
6583 len += IDENTIFIER_LENGTH (key_name) + 1;
6585 /* Just a ':' arg. */
6589 buf = (char *) alloca (len + 1);
6590 /* Start the buffer out as an empty string. */
6593 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
6595 if (TREE_CODE (selector) == KEYWORD_DECL)
6596 key_name = KEYWORD_KEY_NAME (key_chain);
6597 else if (TREE_CODE (selector) == TREE_LIST)
6599 key_name = TREE_PURPOSE (key_chain);
6600 /* The keyword decl chain will later be used as a function argument
6601 chain. Unhook the selector itself so as to not confuse other
6602 parts of the compiler. */
6603 TREE_PURPOSE (key_chain) = NULL_TREE;
6609 strcat (buf, IDENTIFIER_POINTER (key_name));
6613 return get_identifier (buf);
6616 /* Used for declarations and definitions. */
6619 build_method_decl (enum tree_code code, tree ret_type, tree selector,
6620 tree add_args, bool ellipsis)
6624 /* If no type is specified, default to "id". */
6625 ret_type = adjust_type_for_id_default (ret_type);
6627 method_decl = make_node (code);
6628 TREE_TYPE (method_decl) = ret_type;
6630 /* If we have a keyword selector, create an identifier_node that
6631 represents the full selector name (`:' included)... */
6632 if (TREE_CODE (selector) == KEYWORD_DECL)
6634 METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
6635 METHOD_SEL_ARGS (method_decl) = selector;
6636 METHOD_ADD_ARGS (method_decl) = add_args;
6637 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis;
6641 METHOD_SEL_NAME (method_decl) = selector;
6642 METHOD_SEL_ARGS (method_decl) = NULL_TREE;
6643 METHOD_ADD_ARGS (method_decl) = NULL_TREE;
6649 #define METHOD_DEF 0
6650 #define METHOD_REF 1
6652 /* This routine processes objective-c method attributes. */
6655 objc_decl_method_attributes (tree *node, tree attributes, int flags)
6657 tree sentinel_attr = lookup_attribute ("sentinel", attributes);
6660 /* hackery to make an obj method look like a function type. */
6661 tree rettype = TREE_TYPE (*node);
6662 TREE_TYPE (*node) = build_function_type (TREE_VALUE (rettype),
6663 get_arg_type_list (*node, METHOD_REF, 0));
6664 decl_attributes (node, attributes, flags);
6665 METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node));
6666 TREE_TYPE (*node) = rettype;
6669 decl_attributes (node, attributes, flags);
6673 objc_method_decl (enum tree_code opcode)
6675 return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL;
6678 /* Used by `build_objc_method_call' and `comp_proto_with_proto'. Return
6679 an argument list for method METH. CONTEXT is either METHOD_DEF or
6680 METHOD_REF, saying whether we are trying to define a method or call
6681 one. SUPERFLAG says this is for a send to super; this makes a
6682 difference for the NeXT calling sequence in which the lookup and
6683 the method call are done together. If METH is null, user-defined
6684 arguments (i.e., beyond self and _cmd) shall be represented by `...'. */
6687 get_arg_type_list (tree meth, int context, int superflag)
6691 /* Receiver type. */
6692 if (flag_next_runtime && superflag)
6693 arglist = build_tree_list (NULL_TREE, objc_super_type);
6694 else if (context == METHOD_DEF && TREE_CODE (meth) == INSTANCE_METHOD_DECL)
6695 arglist = build_tree_list (NULL_TREE, objc_instance_type);
6697 arglist = build_tree_list (NULL_TREE, objc_object_type);
6699 /* Selector type - will eventually change to `int'. */
6700 chainon (arglist, build_tree_list (NULL_TREE, objc_selector_type));
6702 /* No actual method prototype given -- assume that remaining arguments
6707 /* Build a list of argument types. */
6708 for (akey = METHOD_SEL_ARGS (meth); akey; akey = DECL_CHAIN (akey))
6710 tree arg_type = TREE_VALUE (TREE_TYPE (akey));
6712 /* Decay argument types for the underlying C function as appropriate. */
6713 arg_type = objc_decay_parm_type (arg_type);
6715 chainon (arglist, build_tree_list (NULL_TREE, arg_type));
6718 if (METHOD_ADD_ARGS (meth))
6720 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (meth));
6721 akey; akey = TREE_CHAIN (akey))
6723 tree arg_type = TREE_TYPE (TREE_VALUE (akey));
6725 arg_type = objc_decay_parm_type (arg_type);
6727 chainon (arglist, build_tree_list (NULL_TREE, arg_type));
6730 if (!METHOD_ADD_ARGS_ELLIPSIS_P (meth))
6731 goto lack_of_ellipsis;
6736 chainon (arglist, OBJC_VOID_AT_END);
6743 check_duplicates (hash hsh, int methods, int is_class)
6745 tree meth = NULL_TREE;
6753 /* We have two or more methods with the same name but
6757 /* But just how different are those types? If
6758 -Wno-strict-selector-match is specified, we shall not
6759 complain if the differences are solely among types with
6760 identical size and alignment. */
6761 if (!warn_strict_selector_match)
6763 for (loop = hsh->list; loop; loop = loop->next)
6764 if (!comp_proto_with_proto (meth, loop->value, 0))
6773 bool type = TREE_CODE (meth) == INSTANCE_METHOD_DECL;
6775 warning_at (input_location, 0,
6776 "multiple methods named %<%c%E%> found",
6777 (is_class ? '+' : '-'),
6778 METHOD_SEL_NAME (meth));
6779 inform (DECL_SOURCE_LOCATION (meth), "using %<%c%s%>",
6781 identifier_to_locale (gen_method_decl (meth)));
6785 bool type = TREE_CODE (meth) == INSTANCE_METHOD_DECL;
6787 warning_at (input_location, 0,
6788 "multiple selectors named %<%c%E%> found",
6789 (is_class ? '+' : '-'),
6790 METHOD_SEL_NAME (meth));
6791 inform (DECL_SOURCE_LOCATION (meth), "found %<%c%s%>",
6793 identifier_to_locale (gen_method_decl (meth)));
6796 for (loop = hsh->list; loop; loop = loop->next)
6798 bool type = TREE_CODE (loop->value) == INSTANCE_METHOD_DECL;
6800 inform (DECL_SOURCE_LOCATION (loop->value), "also found %<%c%s%>",
6802 identifier_to_locale (gen_method_decl (loop->value)));
6809 /* If RECEIVER is a class reference, return the identifier node for
6810 the referenced class. RECEIVER is created by objc_get_class_reference,
6811 so we check the exact form created depending on which runtimes are
6815 receiver_is_class_object (tree receiver, int self, int super)
6817 tree chain, exp, arg;
6819 /* The receiver is 'self' or 'super' in the context of a class method. */
6820 if (objc_method_context
6821 && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
6824 ? CLASS_SUPER_NAME (implementation_template)
6825 : CLASS_NAME (implementation_template));
6827 if (flag_next_runtime)
6829 /* The receiver is a variable created by
6830 build_class_reference_decl. */
6831 if (TREE_CODE (receiver) == VAR_DECL && IS_CLASS (TREE_TYPE (receiver)))
6832 /* Look up the identifier. */
6833 for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
6834 if (TREE_PURPOSE (chain) == receiver)
6835 return TREE_VALUE (chain);
6838 /* The receiver is a function call that returns an id. Check if
6839 it is a call to objc_getClass, if so, pick up the class name. */
6840 if (TREE_CODE (receiver) == CALL_EXPR
6841 && (exp = CALL_EXPR_FN (receiver))
6842 && TREE_CODE (exp) == ADDR_EXPR
6843 && (exp = TREE_OPERAND (exp, 0))
6844 && TREE_CODE (exp) == FUNCTION_DECL
6845 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
6846 prototypes for objc_get_class(). Thankfully, they seem to share the
6847 same function type. */
6848 && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl)
6849 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), TAG_GETCLASS)
6850 /* We have a call to objc_get_class/objc_getClass! */
6851 && (arg = CALL_EXPR_ARG (receiver, 0)))
6854 if (TREE_CODE (arg) == ADDR_EXPR
6855 && (arg = TREE_OPERAND (arg, 0))
6856 && TREE_CODE (arg) == STRING_CST)
6857 /* Finally, we have the class name. */
6858 return get_identifier (TREE_STRING_POINTER (arg));
6863 /* If we are currently building a message expr, this holds
6864 the identifier of the selector of the message. This is
6865 used when printing warnings about argument mismatches. */
6867 static tree current_objc_message_selector = 0;
6870 objc_message_selector (void)
6872 return current_objc_message_selector;
6875 /* Construct an expression for sending a message.
6876 MESS has the object to send to in TREE_PURPOSE
6877 and the argument list (including selector) in TREE_VALUE.
6879 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
6880 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
6883 objc_build_message_expr (tree mess)
6885 tree receiver = TREE_PURPOSE (mess);
6888 tree args = TREE_PURPOSE (TREE_VALUE (mess));
6890 tree args = TREE_VALUE (mess);
6892 tree method_params = NULL_TREE;
6894 if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK)
6895 return error_mark_node;
6897 /* Obtain the full selector name. */
6898 if (TREE_CODE (args) == IDENTIFIER_NODE)
6899 /* A unary selector. */
6901 else if (TREE_CODE (args) == TREE_LIST)
6902 sel_name = build_keyword_selector (args);
6906 /* Build the parameter list to give to the method. */
6907 if (TREE_CODE (args) == TREE_LIST)
6909 method_params = chainon (args, TREE_VALUE (TREE_VALUE (mess)));
6912 tree chain = args, prev = NULL_TREE;
6914 /* We have a keyword selector--check for comma expressions. */
6917 tree element = TREE_VALUE (chain);
6919 /* We have a comma expression, must collapse... */
6920 if (TREE_CODE (element) == TREE_LIST)
6923 TREE_CHAIN (prev) = element;
6928 chain = TREE_CHAIN (chain);
6930 method_params = args;
6935 if (processing_template_decl)
6936 /* Must wait until template instantiation time. */
6937 return build_min_nt (MESSAGE_SEND_EXPR, receiver, sel_name,
6941 return objc_finish_message_expr (receiver, sel_name, method_params);
6944 /* Look up method SEL_NAME that would be suitable for receiver
6945 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
6946 nonzero), and report on any duplicates. */
6949 lookup_method_in_hash_lists (tree sel_name, int is_class)
6951 hash method_prototype = NULL;
6954 method_prototype = hash_lookup (nst_method_hash_list,
6957 if (!method_prototype)
6959 method_prototype = hash_lookup (cls_method_hash_list,
6964 return check_duplicates (method_prototype, 1, is_class);
6967 /* The 'objc_finish_message_expr' routine is called from within
6968 'objc_build_message_expr' for non-template functions. In the case of
6969 C++ template functions, it is called from 'build_expr_from_tree'
6970 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded. */
6973 objc_finish_message_expr (tree receiver, tree sel_name, tree method_params)
6975 tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype;
6976 tree selector, retval, class_tree;
6977 int self, super, have_cast;
6979 /* We have used the receiver, so mark it as read. */
6980 mark_exp_read (receiver);
6982 /* Extract the receiver of the message, as well as its type
6983 (where the latter may take the form of a cast or be inferred
6984 from the implementation context). */
6986 while (TREE_CODE (rtype) == COMPOUND_EXPR
6987 || TREE_CODE (rtype) == MODIFY_EXPR
6988 || CONVERT_EXPR_P (rtype)
6989 || TREE_CODE (rtype) == COMPONENT_REF)
6990 rtype = TREE_OPERAND (rtype, 0);
6992 self = (rtype == self_decl);
6993 super = (rtype == UOBJC_SUPER_decl);
6994 rtype = TREE_TYPE (receiver);
6996 have_cast = (TREE_CODE (receiver) == NOP_EXPR
6997 || (TREE_CODE (receiver) == COMPOUND_EXPR
6998 && !IS_SUPER (rtype)));
7000 /* If we are calling [super dealloc], reset our warning flag. */
7001 if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name)))
7002 should_call_super_dealloc = 0;
7004 /* If the receiver is a class object, retrieve the corresponding
7005 @interface, if one exists. */
7006 class_tree = receiver_is_class_object (receiver, self, super);
7008 /* Now determine the receiver type (if an explicit cast has not been
7013 rtype = lookup_interface (class_tree);
7014 /* Handle `self' and `super'. */
7017 if (!CLASS_SUPER_NAME (implementation_template))
7019 error ("no super class declared in @interface for %qE",
7020 CLASS_NAME (implementation_template));
7021 return error_mark_node;
7023 rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template));
7026 rtype = lookup_interface (CLASS_NAME (implementation_template));
7029 /* If receiver is of type `id' or `Class' (or if the @interface for a
7030 class is not visible), we shall be satisfied with the existence of
7031 any instance or class method. */
7032 if (objc_is_id (rtype))
7034 class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE);
7035 rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
7036 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
7042 /* If messaging 'id <Protos>' or 'Class <Proto>', first search
7043 in protocols themselves for the method prototype. */
7045 = lookup_method_in_protocol_list (rprotos, sel_name,
7046 class_tree != NULL_TREE);
7048 /* If messaging 'Class <Proto>' but did not find a class method
7049 prototype, search for an instance method instead, and warn
7050 about having done so. */
7051 if (!method_prototype && !rtype && class_tree != NULL_TREE)
7054 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
7056 if (method_prototype)
7057 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
7058 sel_name, sel_name);
7064 tree orig_rtype = rtype;
7066 if (TREE_CODE (rtype) == POINTER_TYPE)
7067 rtype = TREE_TYPE (rtype);
7068 /* Traverse typedef aliases */
7069 while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype)
7070 && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL
7071 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)))
7072 rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype));
7073 if (TYPED_OBJECT (rtype))
7075 rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype);
7076 rtype = TYPE_OBJC_INTERFACE (rtype);
7078 /* If we could not find an @interface declaration, we must have
7079 only seen a @class declaration; so, we cannot say anything
7080 more intelligent about which methods the receiver will
7082 if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE)
7085 /* We could not find an @interface declaration, yet Message maybe in a
7086 @class's protocol. */
7087 if (!method_prototype && rprotos)
7089 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
7091 else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE
7092 || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE)
7094 /* We have a valid ObjC class name. Look up the method name
7095 in the published @interface for the class (and its
7098 = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE);
7100 /* If the method was not found in the @interface, it may still
7101 exist locally as part of the @implementation. */
7102 if (!method_prototype && objc_implementation_context
7103 && CLASS_NAME (objc_implementation_context)
7104 == OBJC_TYPE_NAME (rtype))
7108 ? CLASS_CLS_METHODS (objc_implementation_context)
7109 : CLASS_NST_METHODS (objc_implementation_context)),
7112 /* If we haven't found a candidate method by now, try looking for
7113 it in the protocol list. */
7114 if (!method_prototype && rprotos)
7116 = lookup_method_in_protocol_list (rprotos, sel_name,
7117 class_tree != NULL_TREE);
7121 warning (0, "invalid receiver type %qs",
7122 identifier_to_locale (gen_type_name (orig_rtype)));
7123 /* After issuing the "invalid receiver" warning, perform method
7124 lookup as if we were messaging 'id'. */
7125 rtype = rprotos = NULL_TREE;
7130 /* For 'id' or 'Class' receivers, search in the global hash table
7131 as a last resort. For all receivers, warn if protocol searches
7133 if (!method_prototype)
7136 warning (0, "%<%c%E%> not found in protocol(s)",
7137 (class_tree ? '+' : '-'),
7142 = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE);
7145 if (!method_prototype && in_objc_property_setter_name_context)
7146 error ("readonly property can not be set");
7147 else if (!method_prototype)
7149 static bool warn_missing_methods = false;
7152 warning (0, "%qE may not respond to %<%c%E%>",
7153 OBJC_TYPE_NAME (rtype),
7154 (class_tree ? '+' : '-'),
7156 /* If we are messaging an 'id' or 'Class' object and made it here,
7157 then we have failed to find _any_ instance or class method,
7160 warning (0, "no %<%c%E%> method found",
7161 (class_tree ? '+' : '-'),
7164 if (!warn_missing_methods)
7166 warning_at (input_location,
7167 0, "(Messages without a matching method signature");
7168 warning_at (input_location,
7169 0, "will be assumed to return %<id%> and accept");
7170 warning_at (input_location,
7171 0, "%<...%> as arguments.)");
7172 warn_missing_methods = true;
7176 /* Save the selector name for printing error messages. */
7177 current_objc_message_selector = sel_name;
7179 /* Build the parameters list for looking up the method.
7180 These are the object itself and the selector. */
7182 if (flag_typed_selectors)
7183 selector = build_typed_selector_reference (input_location,
7184 sel_name, method_prototype);
7186 selector = build_selector_reference (input_location, sel_name);
7188 retval = build_objc_method_call (input_location, super, method_prototype,
7190 selector, method_params);
7192 current_objc_message_selector = 0;
7197 /* Build a tree expression to send OBJECT the operation SELECTOR,
7198 looking up the method on object LOOKUP_OBJECT (often same as OBJECT),
7199 assuming the method has prototype METHOD_PROTOTYPE.
7200 (That is an INSTANCE_METHOD_DECL or CLASS_METHOD_DECL.)
7201 LOC is the location of the expression to build.
7202 Use METHOD_PARAMS as list of args to pass to the method.
7203 If SUPER_FLAG is nonzero, we look up the superclass's method. */
7206 build_objc_method_call (location_t loc, int super_flag, tree method_prototype,
7207 tree lookup_object, tree selector,
7210 tree sender = (super_flag ? umsg_super_decl :
7211 (!flag_next_runtime || flag_nil_receivers
7212 ? (flag_objc_direct_dispatch
7215 : umsg_nonnil_decl));
7216 tree rcv_p = (super_flag ? objc_super_type : objc_object_type);
7217 VEC(tree, gc) *parms = NULL;
7218 unsigned nparm = (method_params ? list_length (method_params) : 0);
7220 /* If a prototype for the method to be called exists, then cast
7221 the sender's return type and arguments to match that of the method.
7222 Otherwise, leave sender as is. */
7225 ? TREE_VALUE (TREE_TYPE (method_prototype))
7226 : objc_object_type);
7228 tree method_param_types =
7229 get_arg_type_list (method_prototype, METHOD_REF, super_flag);
7230 tree ftype = build_function_type (ret_type, method_param_types);
7234 if (method_prototype && METHOD_TYPE_ATTRIBUTES (method_prototype))
7235 ftype = build_type_attribute_variant (
7236 ftype, METHOD_TYPE_ATTRIBUTES (method_prototype));
7238 sender_cast = build_pointer_type (ftype);
7240 if (method_prototype && TREE_DEPRECATED (method_prototype))
7241 objc_warn_deprecated_use (method_prototype, NULL_TREE);
7243 lookup_object = build_c_cast (loc, rcv_p, lookup_object);
7245 /* Use SAVE_EXPR to avoid evaluating the receiver twice. */
7246 lookup_object = save_expr (lookup_object);
7248 /* Param list + 2 slots for object and selector. */
7249 parms = VEC_alloc (tree, gc, nparm + 2);
7251 if (flag_next_runtime)
7253 /* If we are returning a struct in memory, and the address
7254 of that memory location is passed as a hidden first
7255 argument, then change which messenger entry point this
7256 expr will call. NB: Note that sender_cast remains
7257 unchanged (it already has a struct return type). */
7258 if (!targetm.calls.struct_value_rtx (0, 0)
7259 && (TREE_CODE (ret_type) == RECORD_TYPE
7260 || TREE_CODE (ret_type) == UNION_TYPE)
7261 && targetm.calls.return_in_memory (ret_type, 0))
7262 sender = (super_flag ? umsg_super_stret_decl :
7263 flag_nil_receivers ? umsg_stret_decl : umsg_nonnil_stret_decl);
7265 method = build_fold_addr_expr_loc (input_location, sender);
7266 /* Pass the object to the method. */
7267 VEC_quick_push (tree, parms, lookup_object);
7271 /* This is the portable (GNU) way. */
7272 /* First, call the lookup function to get a pointer to the method,
7273 then cast the pointer, then call it with the method arguments. */
7274 VEC(tree, gc) *tv = VEC_alloc (tree, gc, 2);
7275 VEC_quick_push (tree, tv, lookup_object);
7276 VEC_quick_push (tree, tv, selector);
7277 method = build_function_call_vec (loc, sender, tv, NULL);
7278 VEC_free (tree, gc, tv);
7280 /* Pass the appropriate object to the method. */
7281 VEC_quick_push (tree, parms, (super_flag ? self_decl : lookup_object));
7284 /* Pass the selector to the method. */
7285 VEC_quick_push (tree, parms, selector);
7286 /* Now append the remainder of the parms. */
7288 for (; method_params; method_params = TREE_CHAIN (method_params))
7289 VEC_quick_push (tree, parms, TREE_VALUE (method_params));
7291 /* Build an obj_type_ref, with the correct cast for the method call. */
7292 t = build3 (OBJ_TYPE_REF, sender_cast, method,
7293 lookup_object, size_zero_node);
7294 t = build_function_call_vec (loc, t, parms, NULL);\
7295 VEC_free (tree, gc, parms);
7300 build_protocol_reference (tree p)
7303 const char *proto_name;
7305 /* static struct _objc_protocol _OBJC_PROTOCOL_<mumble>; */
7307 proto_name = synth_id_with_class_suffix ("_OBJC_PROTOCOL", p);
7308 decl = start_var_decl (objc_protocol_template, proto_name);
7310 PROTOCOL_FORWARD_DECL (p) = decl;
7313 /* This function is called by the parser when (and only when) a
7314 @protocol() expression is found, in order to compile it. */
7316 objc_build_protocol_expr (tree protoname)
7319 tree p = lookup_protocol (protoname);
7323 error ("cannot find protocol declaration for %qE",
7325 return error_mark_node;
7328 if (!PROTOCOL_FORWARD_DECL (p))
7329 build_protocol_reference (p);
7331 expr = build_unary_op (input_location,
7332 ADDR_EXPR, PROTOCOL_FORWARD_DECL (p), 0);
7334 /* ??? Ideally we'd build the reference with objc_protocol_type directly,
7335 if we have it, rather than converting it here. */
7336 expr = convert (objc_protocol_type, expr);
7338 /* The @protocol() expression is being compiled into a pointer to a
7339 statically allocated instance of the Protocol class. To become
7340 usable at runtime, the 'isa' pointer of the instance need to be
7341 fixed up at runtime by the runtime library, to point to the
7342 actual 'Protocol' class. */
7344 /* For the GNU runtime, put the static Protocol instance in the list
7345 of statically allocated instances, so that we make sure that its
7346 'isa' pointer is fixed up at runtime by the GNU runtime library
7347 to point to the Protocol class (at runtime, when loading the
7348 module, the GNU runtime library loops on the statically allocated
7349 instances (as found in the defs field in objc_symtab) and fixups
7350 all the 'isa' pointers of those objects). */
7351 if (! flag_next_runtime)
7353 /* This type is a struct containing the fields of a Protocol
7354 object. (Cfr. objc_protocol_type instead is the type of a pointer
7355 to such a struct). */
7356 tree protocol_struct_type = xref_tag
7357 (RECORD_TYPE, get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
7360 /* Look for the list of Protocol statically allocated instances
7361 to fixup at runtime. Create a new list to hold Protocol
7362 statically allocated instances, if the list is not found. At
7363 present there is only another list, holding NSConstantString
7364 static instances to be fixed up at runtime. */
7365 for (chain = &objc_static_instances;
7366 *chain && TREE_VALUE (*chain) != protocol_struct_type;
7367 chain = &TREE_CHAIN (*chain));
7370 *chain = tree_cons (NULL_TREE, protocol_struct_type, NULL_TREE);
7371 add_objc_string (OBJC_TYPE_NAME (protocol_struct_type),
7375 /* Add this statically allocated instance to the Protocol list. */
7376 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE,
7377 PROTOCOL_FORWARD_DECL (p),
7378 TREE_PURPOSE (*chain));
7385 /* This function is called by the parser when a @selector() expression
7386 is found, in order to compile it. It is only called by the parser
7387 and only to compile a @selector(). LOC is the location of the
7390 objc_build_selector_expr (location_t loc, tree selnamelist)
7394 /* Obtain the full selector name. */
7395 if (TREE_CODE (selnamelist) == IDENTIFIER_NODE)
7396 /* A unary selector. */
7397 selname = selnamelist;
7398 else if (TREE_CODE (selnamelist) == TREE_LIST)
7399 selname = build_keyword_selector (selnamelist);
7403 /* If we are required to check @selector() expressions as they
7404 are found, check that the selector has been declared. */
7405 if (warn_undeclared_selector)
7407 /* Look the selector up in the list of all known class and
7408 instance methods (up to this line) to check that the selector
7412 /* First try with instance methods. */
7413 hsh = hash_lookup (nst_method_hash_list, selname);
7415 /* If not found, try with class methods. */
7418 hsh = hash_lookup (cls_method_hash_list, selname);
7421 /* If still not found, print out a warning. */
7424 warning (0, "undeclared selector %qE", selname);
7429 if (flag_typed_selectors)
7430 return build_typed_selector_reference (loc, selname, 0);
7432 return build_selector_reference (loc, selname);
7435 /* This is used to implement @encode(). See gcc/doc/objc.texi,
7436 section '@encode'. */
7438 objc_build_encode_expr (tree type)
7443 encode_type (type, obstack_object_size (&util_obstack),
7444 OBJC_ENCODE_INLINE_DEFS);
7445 obstack_1grow (&util_obstack, 0); /* null terminate string */
7446 string = XOBFINISH (&util_obstack, const char *);
7448 /* Synthesize a string that represents the encoded struct/union. */
7449 result = my_build_string (strlen (string) + 1, string);
7450 obstack_free (&util_obstack, util_firstobj);
7455 build_ivar_reference (tree id)
7457 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
7459 /* Historically, a class method that produced objects (factory
7460 method) would assign `self' to the instance that it
7461 allocated. This would effectively turn the class method into
7462 an instance method. Following this assignment, the instance
7463 variables could be accessed. That practice, while safe,
7464 violates the simple rule that a class method should not refer
7465 to an instance variable. It's better to catch the cases
7466 where this is done unknowingly than to support the above
7468 warning (0, "instance variable %qE accessed in class method",
7470 self_decl = convert (objc_instance_type, self_decl); /* cast */
7473 return objc_build_component_ref (build_indirect_ref (input_location,
7474 self_decl, RO_ARROW),
7478 /* Compute a hash value for a given method SEL_NAME. */
7481 hash_func (tree sel_name)
7483 const unsigned char *s
7484 = (const unsigned char *)IDENTIFIER_POINTER (sel_name);
7488 h = h * 67 + *s++ - 113;
7495 nst_method_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
7496 cls_method_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
7498 /* Initialize the hash table used to hold the constant string objects. */
7499 string_htab = htab_create_ggc (31, string_hash,
7502 /* Initialize the hash table used to hold EH-volatilized types. */
7503 volatilized_htab = htab_create_ggc (31, volatilized_hash,
7504 volatilized_eq, NULL);
7507 /* WARNING!!!! hash_enter is called with a method, and will peek
7508 inside to find its selector! But hash_lookup is given a selector
7509 directly, and looks for the selector that's inside the found
7510 entry's key (method) for comparison. */
7513 hash_enter (hash *hashlist, tree method)
7516 int slot = hash_func (METHOD_SEL_NAME (method)) % SIZEHASHTABLE;
7518 obj = ggc_alloc_hashed_entry ();
7520 obj->next = hashlist[slot];
7523 hashlist[slot] = obj; /* append to front */
7527 hash_lookup (hash *hashlist, tree sel_name)
7531 target = hashlist[hash_func (sel_name) % SIZEHASHTABLE];
7535 if (sel_name == METHOD_SEL_NAME (target->key))
7538 target = target->next;
7544 hash_add_attr (hash entry, tree value)
7548 obj = ggc_alloc_hashed_attribute ();
7549 obj->next = entry->list;
7552 entry->list = obj; /* append to front */
7556 lookup_method (tree mchain, tree method)
7560 if (TREE_CODE (method) == IDENTIFIER_NODE)
7563 key = METHOD_SEL_NAME (method);
7567 if (METHOD_SEL_NAME (mchain) == key)
7570 mchain = DECL_CHAIN (mchain);
7575 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance method
7576 in INTERFACE, along with any categories and protocols attached thereto.
7577 If method is not found, and the OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS,
7578 recursively examine the INTERFACE's superclass. If OBJC_LOOKUP_CLASS is
7579 set, OBJC_LOOKUP_NO_SUPER is cleared, and no suitable class method could
7580 be found in INTERFACE or any of its superclasses, look for an _instance_
7581 method of the same name in the root class as a last resort.
7583 If a suitable method cannot be found, return NULL_TREE. */
7586 lookup_method_static (tree interface, tree ident, int flags)
7588 tree meth = NULL_TREE, root_inter = NULL_TREE;
7589 tree inter = interface;
7590 int is_class = (flags & OBJC_LOOKUP_CLASS);
7591 int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER);
7595 tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter);
7596 tree category = inter;
7598 /* First, look up the method in the class itself. */
7599 if ((meth = lookup_method (chain, ident)))
7602 /* Failing that, look for the method in each category of the class. */
7603 while ((category = CLASS_CATEGORY_LIST (category)))
7605 chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category);
7607 /* Check directly in each category. */
7608 if ((meth = lookup_method (chain, ident)))
7611 /* Failing that, check in each category's protocols. */
7612 if (CLASS_PROTOCOL_LIST (category))
7614 if ((meth = (lookup_method_in_protocol_list
7615 (CLASS_PROTOCOL_LIST (category), ident, is_class))))
7620 /* If not found in categories, check in protocols of the main class. */
7621 if (CLASS_PROTOCOL_LIST (inter))
7623 if ((meth = (lookup_method_in_protocol_list
7624 (CLASS_PROTOCOL_LIST (inter), ident, is_class))))
7628 /* If we were instructed not to look in superclasses, don't. */
7629 if (no_superclasses)
7632 /* Failing that, climb up the inheritance hierarchy. */
7634 inter = lookup_interface (CLASS_SUPER_NAME (inter));
7638 /* If no class (factory) method was found, check if an _instance_
7639 method of the same name exists in the root class. This is what
7640 the Objective-C runtime will do. If an instance method was not
7642 return is_class ? lookup_method_static (root_inter, ident, 0): NULL_TREE;
7645 /* Add the method to the hash list if it doesn't contain an identical
7649 add_method_to_hash_list (hash *hash_list, tree method)
7653 if (!(hsh = hash_lookup (hash_list, METHOD_SEL_NAME (method))))
7655 /* Install on a global chain. */
7656 hash_enter (hash_list, method);
7660 /* Check types against those; if different, add to a list. */
7662 int already_there = comp_proto_with_proto (method, hsh->key, 1);
7663 for (loop = hsh->list; !already_there && loop; loop = loop->next)
7664 already_there |= comp_proto_with_proto (method, loop->value, 1);
7666 hash_add_attr (hsh, method);
7671 objc_add_method (tree klass, tree method, int is_class, bool is_optional)
7675 /* @optional methods are added to protocol's OPTIONAL list */
7678 gcc_assert (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE);
7679 if (!(mth = lookup_method (is_class
7680 ? PROTOCOL_OPTIONAL_CLS_METHODS (klass)
7681 : PROTOCOL_OPTIONAL_NST_METHODS (klass),
7686 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass);
7687 PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method;
7691 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass);
7692 PROTOCOL_OPTIONAL_NST_METHODS (klass) = method;
7696 else if (!(mth = lookup_method (is_class
7697 ? CLASS_CLS_METHODS (klass)
7698 : CLASS_NST_METHODS (klass), method)))
7700 /* put method on list in reverse order */
7703 DECL_CHAIN (method) = CLASS_CLS_METHODS (klass);
7704 CLASS_CLS_METHODS (klass) = method;
7708 DECL_CHAIN (method) = CLASS_NST_METHODS (klass);
7709 CLASS_NST_METHODS (klass) = method;
7714 /* When processing an @interface for a class or category, give hard
7715 errors on methods with identical selectors but differing argument
7716 and/or return types. We do not do this for @implementations, because
7717 C/C++ will do it for us (i.e., there will be duplicate function
7718 definition errors). */
7719 if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE
7720 || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE)
7721 && !comp_proto_with_proto (method, mth, 1))
7722 error ("duplicate declaration of method %<%c%E%>",
7723 is_class ? '+' : '-',
7724 METHOD_SEL_NAME (mth));
7728 add_method_to_hash_list (cls_method_hash_list, method);
7731 add_method_to_hash_list (nst_method_hash_list, method);
7733 /* Instance methods in root classes (and categories thereof)
7734 may act as class methods as a last resort. We also add
7735 instance methods listed in @protocol declarations to
7736 the class hash table, on the assumption that @protocols
7737 may be adopted by root classes or categories. */
7738 if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
7739 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
7740 klass = lookup_interface (CLASS_NAME (klass));
7742 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE
7743 || !CLASS_SUPER_NAME (klass))
7744 add_method_to_hash_list (cls_method_hash_list, method);
7751 add_class (tree class_name, tree name)
7753 struct interface_tuple **slot;
7755 /* Put interfaces on list in reverse order. */
7756 TREE_CHAIN (class_name) = interface_chain;
7757 interface_chain = class_name;
7759 if (interface_htab == NULL)
7760 interface_htab = htab_create_ggc (31, hash_interface, eq_interface, NULL);
7761 slot = (struct interface_tuple **)
7762 htab_find_slot_with_hash (interface_htab, name,
7763 IDENTIFIER_HASH_VALUE (name),
7767 *slot = ggc_alloc_cleared_interface_tuple ();
7770 (*slot)->class_name = class_name;
7772 return interface_chain;
7776 add_category (tree klass, tree category)
7778 /* Put categories on list in reverse order. */
7779 tree cat = lookup_category (klass, CLASS_SUPER_NAME (category));
7783 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
7785 CLASS_SUPER_NAME (category));
7789 CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass);
7790 CLASS_CATEGORY_LIST (klass) = category;
7794 /* Called after parsing each instance variable declaration. Necessary to
7795 preserve typedefs and implement public/private...
7797 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
7800 add_instance_variable (tree klass, objc_ivar_visibility_kind visibility,
7803 tree field_type = TREE_TYPE (field_decl);
7804 const char *ivar_name = DECL_NAME (field_decl)
7805 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl)))
7809 if (TREE_CODE (field_type) == REFERENCE_TYPE)
7811 error ("illegal reference type specified for instance variable %qs",
7813 /* Return class as is without adding this ivar. */
7818 if (field_type == error_mark_node || !TYPE_SIZE (field_type)
7819 || TYPE_SIZE (field_type) == error_mark_node)
7820 /* 'type[0]' is allowed, but 'type[]' is not! */
7822 error ("instance variable %qs has unknown size", ivar_name);
7823 /* Return class as is without adding this ivar. */
7828 /* Check if the ivar being added has a non-POD C++ type. If so, we will
7829 need to either (1) warn the user about it or (2) generate suitable
7830 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
7831 methods (if '-fobjc-call-cxx-cdtors' was specified). */
7832 if (MAYBE_CLASS_TYPE_P (field_type)
7833 && (TYPE_NEEDS_CONSTRUCTING (field_type)
7834 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)
7835 || TYPE_POLYMORPHIC_P (field_type)))
7837 tree type_name = OBJC_TYPE_NAME (field_type);
7839 if (flag_objc_call_cxx_cdtors)
7841 /* Since the ObjC runtime will be calling the constructors and
7842 destructors for us, the only thing we can't handle is the lack
7843 of a default constructor. */
7844 if (TYPE_NEEDS_CONSTRUCTING (field_type)
7845 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type))
7847 warning (0, "type %qE has no default constructor to call",
7850 /* If we cannot call a constructor, we should also avoid
7851 calling the destructor, for symmetry. */
7852 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
7853 warning (0, "destructor for %qE shall not be run either",
7859 static bool warn_cxx_ivars = false;
7861 if (TYPE_POLYMORPHIC_P (field_type))
7863 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
7865 error ("type %qE has virtual member functions", type_name);
7866 error ("illegal aggregate type %qE specified "
7867 "for instance variable %qs",
7868 type_name, ivar_name);
7869 /* Return class as is without adding this ivar. */
7873 /* User-defined constructors and destructors are not known to Obj-C
7874 and hence will not be called. This may or may not be a problem. */
7875 if (TYPE_NEEDS_CONSTRUCTING (field_type))
7876 warning (0, "type %qE has a user-defined constructor", type_name);
7877 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
7878 warning (0, "type %qE has a user-defined destructor", type_name);
7880 if (!warn_cxx_ivars)
7882 warning (0, "C++ constructors and destructors will not "
7883 "be invoked for Objective-C fields");
7884 warn_cxx_ivars = true;
7890 /* Overload the public attribute, it is not used for FIELD_DECLs. */
7893 case OBJC_IVAR_VIS_PROTECTED:
7894 TREE_PUBLIC (field_decl) = 0;
7895 TREE_PRIVATE (field_decl) = 0;
7896 TREE_PROTECTED (field_decl) = 1;
7899 case OBJC_IVAR_VIS_PACKAGE:
7900 /* TODO: Implement the package variant. */
7901 case OBJC_IVAR_VIS_PUBLIC:
7902 TREE_PUBLIC (field_decl) = 1;
7903 TREE_PRIVATE (field_decl) = 0;
7904 TREE_PROTECTED (field_decl) = 0;
7907 case OBJC_IVAR_VIS_PRIVATE:
7908 TREE_PUBLIC (field_decl) = 0;
7909 TREE_PRIVATE (field_decl) = 1;
7910 TREE_PROTECTED (field_decl) = 0;
7915 CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl);
7921 is_ivar (tree decl_chain, tree ident)
7923 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
7924 if (DECL_NAME (decl_chain) == ident)
7929 /* True if the ivar is private and we are not in its implementation. */
7932 is_private (tree decl)
7934 return (TREE_PRIVATE (decl)
7935 && ! is_ivar (CLASS_IVARS (implementation_template),
7939 /* We have an instance variable reference;, check to see if it is public. */
7942 objc_is_public (tree expr, tree identifier)
7944 tree basetype, decl;
7947 if (processing_template_decl)
7951 if (TREE_TYPE (expr) == error_mark_node)
7954 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7956 if (basetype && TREE_CODE (basetype) == RECORD_TYPE)
7958 if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype))
7960 tree klass = lookup_interface (OBJC_TYPE_NAME (basetype));
7964 error ("cannot find interface declaration for %qE",
7965 OBJC_TYPE_NAME (basetype));
7969 if ((decl = is_ivar (get_class_ivars (klass, true), identifier)))
7971 if (TREE_PUBLIC (decl))
7974 /* Important difference between the Stepstone translator:
7975 all instance variables should be public within the context
7976 of the implementation. */
7977 if (objc_implementation_context
7978 && ((TREE_CODE (objc_implementation_context)
7979 == CLASS_IMPLEMENTATION_TYPE)
7980 || (TREE_CODE (objc_implementation_context)
7981 == CATEGORY_IMPLEMENTATION_TYPE)))
7983 tree curtype = TYPE_MAIN_VARIANT
7984 (CLASS_STATIC_TEMPLATE
7985 (implementation_template));
7987 if (basetype == curtype
7988 || DERIVED_FROM_P (basetype, curtype))
7990 int priv = is_private (decl);
7993 error ("instance variable %qE is declared private",
8000 /* The 2.95.2 compiler sometimes allowed C functions to access
8001 non-@public ivars. We will let this slide for now... */
8002 if (!objc_method_context)
8004 warning (0, "instance variable %qE is %s; "
8005 "this will be a hard error in the future",
8007 TREE_PRIVATE (decl) ? "@private" : "@protected");
8011 error ("instance variable %qE is declared %s",
8013 TREE_PRIVATE (decl) ? "private" : "protected");
8022 /* Make sure all entries in CHAIN are also in LIST. */
8025 check_methods (tree chain, tree list, int mtype)
8031 if (!lookup_method (list, chain))
8035 if (TREE_CODE (objc_implementation_context)
8036 == CLASS_IMPLEMENTATION_TYPE)
8037 warning (0, "incomplete implementation of class %qE",
8038 CLASS_NAME (objc_implementation_context));
8039 else if (TREE_CODE (objc_implementation_context)
8040 == CATEGORY_IMPLEMENTATION_TYPE)
8041 warning (0, "incomplete implementation of category %qE",
8042 CLASS_SUPER_NAME (objc_implementation_context));
8046 warning (0, "method definition for %<%c%E%> not found",
8047 mtype, METHOD_SEL_NAME (chain));
8050 chain = DECL_CHAIN (chain);
8056 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
8059 conforms_to_protocol (tree klass, tree protocol)
8061 if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE)
8063 tree p = CLASS_PROTOCOL_LIST (klass);
8064 while (p && TREE_VALUE (p) != protocol)
8069 tree super = (CLASS_SUPER_NAME (klass)
8070 ? lookup_interface (CLASS_SUPER_NAME (klass))
8072 int tmp = super ? conforms_to_protocol (super, protocol) : 0;
8081 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
8082 CONTEXT. This is one of two mechanisms to check protocol integrity. */
8085 check_methods_accessible (tree chain, tree context, int mtype)
8089 tree base_context = context;
8093 context = base_context;
8097 list = CLASS_CLS_METHODS (context);
8099 list = CLASS_NST_METHODS (context);
8101 if (lookup_method (list, chain))
8104 else if (TREE_CODE (context) == CLASS_IMPLEMENTATION_TYPE
8105 || TREE_CODE (context) == CLASS_INTERFACE_TYPE)
8106 context = (CLASS_SUPER_NAME (context)
8107 ? lookup_interface (CLASS_SUPER_NAME (context))
8110 else if (TREE_CODE (context) == CATEGORY_IMPLEMENTATION_TYPE
8111 || TREE_CODE (context) == CATEGORY_INTERFACE_TYPE)
8112 context = (CLASS_NAME (context)
8113 ? lookup_interface (CLASS_NAME (context))
8119 if (context == NULL_TREE)
8123 if (TREE_CODE (objc_implementation_context)
8124 == CLASS_IMPLEMENTATION_TYPE)
8125 warning (0, "incomplete implementation of class %qE",
8126 CLASS_NAME (objc_implementation_context));
8127 else if (TREE_CODE (objc_implementation_context)
8128 == CATEGORY_IMPLEMENTATION_TYPE)
8129 warning (0, "incomplete implementation of category %qE",
8130 CLASS_SUPER_NAME (objc_implementation_context));
8133 warning (0, "method definition for %<%c%E%> not found",
8134 mtype, METHOD_SEL_NAME (chain));
8137 chain = TREE_CHAIN (chain); /* next method... */
8142 /* Check whether the current interface (accessible via
8143 'objc_implementation_context') actually implements protocol P, along
8144 with any protocols that P inherits. */
8147 check_protocol (tree p, const char *type, tree name)
8149 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
8153 /* Ensure that all protocols have bodies! */
8156 f1 = check_methods (PROTOCOL_CLS_METHODS (p),
8157 CLASS_CLS_METHODS (objc_implementation_context),
8159 f2 = check_methods (PROTOCOL_NST_METHODS (p),
8160 CLASS_NST_METHODS (objc_implementation_context),
8165 f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
8166 objc_implementation_context,
8168 f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
8169 objc_implementation_context,
8174 warning (0, "%s %qE does not fully implement the %qE protocol",
8175 type, name, PROTOCOL_NAME (p));
8178 /* Check protocols recursively. */
8179 if (PROTOCOL_LIST (p))
8181 tree subs = PROTOCOL_LIST (p);
8183 lookup_interface (CLASS_SUPER_NAME (implementation_template));
8187 tree sub = TREE_VALUE (subs);
8189 /* If the superclass does not conform to the protocols
8190 inherited by P, then we must! */
8191 if (!super_class || !conforms_to_protocol (super_class, sub))
8192 check_protocol (sub, type, name);
8193 subs = TREE_CHAIN (subs);
8198 /* Check whether the current interface (accessible via
8199 'objc_implementation_context') actually implements the protocols listed
8203 check_protocols (tree proto_list, const char *type, tree name)
8205 for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
8207 tree p = TREE_VALUE (proto_list);
8209 check_protocol (p, type, name);
8213 /* Make sure that the class CLASS_NAME is defined
8214 CODE says which kind of thing CLASS_NAME ought to be.
8215 It can be CLASS_INTERFACE_TYPE, CLASS_IMPLEMENTATION_TYPE,
8216 CATEGORY_INTERFACE_TYPE, or CATEGORY_IMPLEMENTATION_TYPE. */
8219 start_class (enum tree_code code, tree class_name, tree super_name,
8225 if (current_namespace != global_namespace) {
8226 error ("Objective-C declarations may only appear in global scope");
8228 #endif /* OBJCPLUS */
8230 if (objc_implementation_context)
8232 warning (0, "%<@end%> missing in implementation context");
8233 finish_class (objc_implementation_context);
8234 objc_ivar_chain = NULL_TREE;
8235 objc_implementation_context = NULL_TREE;
8238 klass = make_node (code);
8239 TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS);
8241 /* Check for existence of the super class, if one was specified. Note
8242 that we must have seen an @interface, not just a @class. If we
8243 are looking at a @compatibility_alias, traverse it first. */
8244 if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE)
8247 tree super = objc_is_class_name (super_name);
8249 if (!super || !lookup_interface (super))
8251 error ("cannot find interface declaration for %qE, superclass of %qE",
8252 super ? super : super_name,
8254 super_name = NULL_TREE;
8260 CLASS_NAME (klass) = class_name;
8261 CLASS_SUPER_NAME (klass) = super_name;
8262 CLASS_CLS_METHODS (klass) = NULL_TREE;
8264 if (! objc_is_class_name (class_name)
8265 && (decl = lookup_name (class_name)))
8267 error ("%qE redeclared as different kind of symbol",
8269 error ("previous declaration of %q+D",
8273 if (code == CLASS_IMPLEMENTATION_TYPE)
8278 for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
8279 if (TREE_VALUE (chain) == class_name)
8281 error ("reimplementation of class %qE",
8283 return error_mark_node;
8285 implemented_classes = tree_cons (NULL_TREE, class_name,
8286 implemented_classes);
8289 /* Reset for multiple classes per file. */
8292 objc_implementation_context = klass;
8294 /* Lookup the interface for this implementation. */
8296 if (!(implementation_template = lookup_interface (class_name)))
8298 warning (0, "cannot find interface declaration for %qE",
8300 add_class (implementation_template = objc_implementation_context,
8304 /* If a super class has been specified in the implementation,
8305 insure it conforms to the one specified in the interface. */
8308 && (super_name != CLASS_SUPER_NAME (implementation_template)))
8310 tree previous_name = CLASS_SUPER_NAME (implementation_template);
8311 error ("conflicting super class name %qE",
8314 error ("previous declaration of %qE", previous_name);
8316 error ("previous declaration");
8319 else if (! super_name)
8321 CLASS_SUPER_NAME (objc_implementation_context)
8322 = CLASS_SUPER_NAME (implementation_template);
8326 else if (code == CLASS_INTERFACE_TYPE)
8328 if (lookup_interface (class_name))
8330 error ("duplicate interface declaration for class %qE",
8332 warning (0, "duplicate interface declaration for class %qE",
8336 add_class (klass, class_name);
8339 CLASS_PROTOCOL_LIST (klass)
8340 = lookup_and_install_protocols (protocol_list);
8343 else if (code == CATEGORY_INTERFACE_TYPE)
8345 tree class_category_is_assoc_with;
8347 /* For a category, class_name is really the name of the class that
8348 the following set of methods will be associated with. We must
8349 find the interface so that can derive the objects template. */
8351 if (!(class_category_is_assoc_with = lookup_interface (class_name)))
8353 error ("cannot find interface declaration for %qE",
8355 exit (FATAL_EXIT_CODE);
8358 add_category (class_category_is_assoc_with, klass);
8361 CLASS_PROTOCOL_LIST (klass)
8362 = lookup_and_install_protocols (protocol_list);
8365 else if (code == CATEGORY_IMPLEMENTATION_TYPE)
8367 /* Reset for multiple classes per file. */
8370 objc_implementation_context = klass;
8372 /* For a category, class_name is really the name of the class that
8373 the following set of methods will be associated with. We must
8374 find the interface so that can derive the objects template. */
8376 if (!(implementation_template = lookup_interface (class_name)))
8378 error ("cannot find interface declaration for %qE",
8380 exit (FATAL_EXIT_CODE);
8387 continue_class (tree klass)
8389 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE
8390 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
8392 struct imp_entry *imp_entry;
8394 /* Check consistency of the instance variables. */
8396 if (CLASS_RAW_IVARS (klass))
8397 check_ivars (implementation_template, klass);
8399 /* code generation */
8402 push_lang_context (lang_name_c);
8405 build_private_template (implementation_template);
8406 uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template);
8407 objc_instance_type = build_pointer_type (uprivate_record);
8409 imp_entry = ggc_alloc_imp_entry ();
8411 imp_entry->next = imp_list;
8412 imp_entry->imp_context = klass;
8413 imp_entry->imp_template = implementation_template;
8415 synth_forward_declarations ();
8416 imp_entry->class_decl = UOBJC_CLASS_decl;
8417 imp_entry->meta_decl = UOBJC_METACLASS_decl;
8418 imp_entry->has_cxx_cdtors = 0;
8420 /* Append to front and increment count. */
8421 imp_list = imp_entry;
8422 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
8428 pop_lang_context ();
8429 #endif /* OBJCPLUS */
8431 return get_class_ivars (implementation_template, true);
8434 else if (TREE_CODE (klass) == CLASS_INTERFACE_TYPE)
8437 push_lang_context (lang_name_c);
8438 #endif /* OBJCPLUS */
8440 objc_collecting_ivars = 1;
8441 build_private_template (klass);
8442 objc_collecting_ivars = 0;
8445 pop_lang_context ();
8446 #endif /* OBJCPLUS */
8452 return error_mark_node;
8455 /* This routine builds a property ivar name. */
8458 objc_build_property_ivar_name (tree property_decl)
8460 static char string[BUFSIZE];
8461 sprintf (string, "_%s", IDENTIFIER_POINTER (PROPERTY_NAME (property_decl)));
8465 /* This routine builds name of the setter synthesized function. */
8468 objc_build_property_setter_name (tree ident, bool delimit_colon)
8470 static char string[BUFSIZE];
8472 sprintf (string, "set%s:", IDENTIFIER_POINTER (ident));
8474 sprintf (string, "set%s", IDENTIFIER_POINTER (ident));
8475 string[3] = TOUPPER (string[3]);
8479 /* This routine does all the work for generating data and code per each
8480 property declared in current implementation. */
8483 objc_gen_one_property_datum (tree klass, tree property, tree class_methods, bool *ivar_added)
8487 /* If getter, check that it is already declared in user code. */
8488 if (PROPERTY_GETTER_NAME (property))
8490 mth = lookup_method (CLASS_NST_METHODS (class_methods),
8491 PROPERTY_GETTER_NAME (property));
8493 error ("property getter %qs not declared in class %qs",
8494 IDENTIFIER_POINTER (PROPERTY_GETTER_NAME (property)),
8495 IDENTIFIER_POINTER (CLASS_NAME (class_methods)));
8497 /* If setter, check that it is already declared in user code. */
8498 if (PROPERTY_SETTER_NAME (property))
8500 mth = lookup_method (CLASS_NST_METHODS (class_methods),
8501 PROPERTY_SETTER_NAME (property));
8503 error ("property setter %qs not declared in class %qs",
8504 IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (property)),
8505 IDENTIFIER_POINTER (CLASS_NAME (class_methods)));
8507 /* If ivar attribute specified, check that it is already declared. */
8508 if (PROPERTY_IVAR_NAME (property))
8510 if (!is_ivar (CLASS_IVARS (klass),
8511 PROPERTY_IVAR_NAME (property)))
8512 error ("ivar %qs in property declaration must be an existing ivar",
8513 IDENTIFIER_POINTER (PROPERTY_IVAR_NAME (property)));
8515 else if (!PROPERTY_GETTER_NAME (property)
8516 || (PROPERTY_READONLY (property) == boolean_false_node
8517 && !PROPERTY_SETTER_NAME (property)))
8519 /* Setter and/or getter must be synthesize and there was no user-specified
8520 ivar. Must create an ivar and add to to current class's ivar list. */
8521 tree record = CLASS_STATIC_TEMPLATE (klass);
8522 tree type = TREE_TYPE (property);
8523 tree field_decl, field;
8524 field_decl = create_field_decl (type,
8525 objc_build_property_ivar_name (property));
8526 DECL_CONTEXT (field_decl) = record;
8527 (void) add_instance_variable (klass,
8528 OBJC_IVAR_VIS_PUBLIC, field_decl);
8529 /* Unfortunately, CLASS_IVARS is completed when interface is completed.
8530 Must add the new ivar by hand to its list here. */
8532 CLASS_IVARS (klass) =
8533 chainon (CLASS_IVARS (klass),
8534 copy_node (field_decl));
8535 gcc_assert (record);
8536 /* Must also add this ivar to the end of list of fields for this class. */
8537 field = TYPE_FIELDS (record);
8538 if (field && field != CLASS_IVARS (klass))
8539 /* class has a hidden field, attach ivar list after the hiddent field. */
8540 TREE_CHAIN (field) = CLASS_IVARS (klass);
8542 TYPE_FIELDS (record) = CLASS_IVARS (klass);
8547 /* This routine processes an existing getter or setter attribute.
8548 It aliases internal property getter or setter to the user implemented
8553 objc_process_getter_setter (tree klass, tree property, bool getter)
8556 tree prop_getter_mth_decl;
8560 /* getter name is same as property name. */
8561 name_ident = PROPERTY_NAME (property);
8563 /* Must synthesize setter name from property name. */
8564 name_ident = get_identifier (objc_build_property_setter_name (
8565 PROPERTY_NAME (property), true));
8567 /* Find declaration of instance method for the property in its class. */
8568 prop_mth_decl = lookup_method (CLASS_NST_METHODS (klass), name_ident);
8573 prop_getter_mth_decl = lookup_method (CLASS_NST_METHODS (objc_implementation_context),
8574 getter ? PROPERTY_GETTER_NAME (property)
8575 : PROPERTY_SETTER_NAME (property));
8577 if (!prop_getter_mth_decl)
8580 if (!match_proto_with_proto (prop_getter_mth_decl, prop_mth_decl, 1))
8582 error ("User %s %qs does not match property %qs type",
8583 getter ? "getter" : "setter",
8584 IDENTIFIER_POINTER (DECL_NAME (prop_getter_mth_decl)),
8585 IDENTIFIER_POINTER (PROPERTY_NAME (property)));
8588 /* We alias internal property getter to the user implemented getter by copying relevant
8589 entries from user's implementation to the internal one. */
8590 prop_mth_decl = copy_node (prop_mth_decl);
8591 METHOD_ENCODING (prop_mth_decl) = METHOD_ENCODING (prop_getter_mth_decl);
8592 METHOD_DEFINITION (prop_mth_decl) = METHOD_DEFINITION (prop_getter_mth_decl);
8593 objc_add_method (objc_implementation_context, prop_mth_decl, 0, 0);
8596 /* This routine synthesizes a 'getter' method. */
8599 objc_synthesize_getter (tree klass, tree class_method, tree property)
8606 /* If user has implemented a getter with same name then do nothing. */
8607 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
8608 PROPERTY_NAME (property)))
8611 /* Find declaration of the property in the interface. There must be one. */
8612 decl = lookup_method (CLASS_NST_METHODS (class_method),
8613 PROPERTY_NAME (property));
8615 /* If one not declared in the interface, this condition has already been reported
8616 as user error (because property was not declared in the interface). */
8620 objc_inherit_code = INSTANCE_METHOD_DECL;
8621 /* For now no attributes. */
8622 objc_start_method_definition (copy_node (decl), NULL_TREE);
8624 body = c_begin_compound_stmt (true);
8625 /* return self->_property_name; */
8626 /* If user specified an ivar, use it in generation of the getter. */
8627 ivar_ident = PROPERTY_IVAR_NAME (property)
8628 ? PROPERTY_IVAR_NAME (property)
8629 : get_identifier (objc_build_property_ivar_name (property));
8631 /* objc_ivar_chain might not be up to date in the case that property 'ivar'
8632 is added *after* user ivar is parsed and objc_continue_implementation
8633 has already been called. */
8634 objc_ivar_chain = CLASS_IVARS (klass);
8635 ret_val = objc_lookup_ivar (NULL_TREE, ivar_ident);
8636 /* If ivar attribute is not a user declared attribute, this condition has
8637 already been repored as error. */
8638 gcc_assert (ret_val || PROPERTY_IVAR_NAME (property));
8643 finish_return_stmt (ret_val);
8645 (void)c_finish_return (input_location, ret_val, NULL);
8648 add_stmt (c_end_compound_stmt (input_location, body, true));
8649 fn = current_function_decl;
8653 objc_finish_method_definition (fn);
8656 /* This routine synthesizes a 'setter' method. */
8659 objc_synthesize_setter (tree klass, tree class_method, tree property)
8661 tree fn, decl, ivar_ident, lhs, rhs;
8663 char *setter_name = objc_build_property_setter_name (
8664 PROPERTY_NAME (property), true);
8665 tree setter_ident = get_identifier (setter_name);
8667 /* If user has implemented a setter with same name then do nothing. */
8668 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
8672 /* Find declaration of the property in the interface. There must be one. */
8673 decl = lookup_method (CLASS_NST_METHODS (class_method), setter_ident);
8675 /* If one not declared in the inerface, this condition has already been reported
8676 as user error (because property was not declared in the interface. */
8680 objc_inherit_code = INSTANCE_METHOD_DECL;
8681 /* For now, no attributes. */
8682 objc_start_method_definition (copy_node (decl), NULL_TREE);
8684 body = c_begin_compound_stmt (true);
8685 /* _property_name = _value; */
8686 /* If user specified an ivar, use it in generation of the setter. */
8687 ivar_ident = PROPERTY_IVAR_NAME (property)
8688 ? PROPERTY_IVAR_NAME (property)
8689 : get_identifier (objc_build_property_ivar_name (property));
8691 /* objc_ivar_chain might not be up to date in the case that property 'ivar'
8692 is added *after* user ivar is parsed and objc_continue_implementation
8693 has already been called. */
8694 objc_ivar_chain = CLASS_IVARS (klass);
8695 lhs = objc_lookup_ivar (NULL_TREE, ivar_ident);
8696 /* If ivar attribute is not a user declared attribute, this condition has
8697 already been repored as error. */
8698 gcc_assert (lhs || PROPERTY_IVAR_NAME (property));
8701 rhs = lookup_name (get_identifier ("_value"));
8703 /* FIXME: NULL types to get compile. */
8704 add_stmt (build_modify_expr (input_location,
8705 lhs, NULL_TREE, NOP_EXPR,
8706 input_location, rhs, NULL_TREE));
8708 add_stmt (c_end_compound_stmt (input_location, body, true));
8709 fn = current_function_decl;
8713 objc_finish_method_definition (fn);
8716 /* Main routine to generate code/data for all the property information for
8717 current implementation (class or category). CLASS is the interface where
8718 ivars are declared. CLASS_METHODS is where methods are found which
8719 could be a class or a category depending on whether we are implementing
8720 property of a class or a category. */
8723 objc_gen_property_data (tree klass, tree class_methods)
8726 bool ivar_added = false;
8727 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
8728 objc_gen_one_property_datum (klass, x, class_methods, &ivar_added);
8732 tree record = CLASS_STATIC_TEMPLATE (klass);
8733 /* Ugh, must recalculate struct layout since at least one ivar was added. */
8734 TYPE_SIZE (record) = 0;
8735 layout_type (record);
8738 /* Synthesize all getters for properties. */
8739 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
8741 /* Property has a getter attribute, no need to synthesize one. */
8742 if (PROPERTY_GETTER_NAME (x) == NULL_TREE)
8743 objc_synthesize_getter (klass, class_methods, x);
8745 objc_process_getter_setter (class_methods, x, true);
8747 if (PROPERTY_READONLY (x) == boolean_false_node)
8749 /* not a readonly property. */
8750 if (PROPERTY_SETTER_NAME (x) == NULL_TREE)
8751 objc_synthesize_setter (klass, class_methods, x);
8753 objc_process_getter_setter (class_methods, x, false);
8758 /* This is called once we see the "@end" in an interface/implementation. */
8761 finish_class (tree klass)
8763 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
8765 /* All code generation is done in finish_objc. */
8767 /* Generate what needed for property; setters, getters, etc. */
8768 objc_gen_property_data (implementation_template, implementation_template);
8770 if (implementation_template != objc_implementation_context)
8772 /* Ensure that all method listed in the interface contain bodies. */
8773 check_methods (CLASS_CLS_METHODS (implementation_template),
8774 CLASS_CLS_METHODS (objc_implementation_context), '+');
8775 check_methods (CLASS_NST_METHODS (implementation_template),
8776 CLASS_NST_METHODS (objc_implementation_context), '-');
8778 if (CLASS_PROTOCOL_LIST (implementation_template))
8779 check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
8781 CLASS_NAME (objc_implementation_context));
8785 else if (TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
8787 tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass));
8791 /* Generate what needed for property; setters, getters, etc. */
8792 objc_gen_property_data (implementation_template, category);
8794 /* Ensure all method listed in the interface contain bodies. */
8795 check_methods (CLASS_CLS_METHODS (category),
8796 CLASS_CLS_METHODS (objc_implementation_context), '+');
8797 check_methods (CLASS_NST_METHODS (category),
8798 CLASS_NST_METHODS (objc_implementation_context), '-');
8800 if (CLASS_PROTOCOL_LIST (category))
8801 check_protocols (CLASS_PROTOCOL_LIST (category),
8803 CLASS_SUPER_NAME (objc_implementation_context));
8808 /* Process properties of the class. */
8810 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
8812 tree type = TREE_TYPE (x);
8813 tree prop_name = PROPERTY_NAME (x);
8814 /* Build an instance method declaration: - (type) prop_name; */
8815 if (PROPERTY_GETTER_NAME (x) == NULL_TREE)
8817 /* No getter attribute specified. Generate an instance method for the
8819 tree rettype = build_tree_list (NULL_TREE, type);
8820 tree getter_decl = build_method_decl (INSTANCE_METHOD_DECL,
8823 objc_add_method (objc_interface_context, getter_decl, false, false);
8824 METHOD_PROPERTY_CONTEXT (getter_decl) = x;
8827 warning (0, "getter = %qs may not be specified in an interface",
8828 IDENTIFIER_POINTER (PROPERTY_GETTER_NAME (x)));
8830 /* Build an instance method declaration: - (void) setName: (type)value; */
8831 if (PROPERTY_SETTER_NAME (x) == NULL_TREE
8832 && PROPERTY_READONLY (x) == boolean_false_node)
8834 /* Declare a setter instance method in the interface. */
8835 tree key_name, arg_type, arg_name;
8836 tree setter_decl, selector;
8837 tree ret_type = build_tree_list (NULL_TREE, void_type_node);
8839 key_name = get_identifier (objc_build_property_setter_name (
8840 PROPERTY_NAME (x), false));
8841 arg_type = build_tree_list (NULL_TREE, type);
8842 arg_name = get_identifier ("_value");
8843 /* For now, no attributes. */
8844 selector = objc_build_keyword_decl (key_name, arg_type, arg_name, NULL);
8845 setter_decl = build_method_decl (INSTANCE_METHOD_DECL,
8847 build_tree_list (NULL_TREE, NULL_TREE),
8849 objc_add_method (objc_interface_context, setter_decl, false, false);
8850 METHOD_PROPERTY_CONTEXT (setter_decl) = x;
8852 else if (PROPERTY_SETTER_NAME (x))
8853 warning (0, "setter = %qs may not be specified in an interface",
8854 IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x)));
8855 if (PROPERTY_IVAR_NAME (x))
8856 warning (0, "ivar = %qs attribute may not be specified in an interface",
8857 IDENTIFIER_POINTER (PROPERTY_IVAR_NAME (x)));
8863 add_protocol (tree protocol)
8865 /* Put protocol on list in reverse order. */
8866 TREE_CHAIN (protocol) = protocol_chain;
8867 protocol_chain = protocol;
8868 return protocol_chain;
8872 lookup_protocol (tree ident)
8876 for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
8877 if (ident == PROTOCOL_NAME (chain))
8883 /* This function forward declares the protocols named by NAMES. If
8884 they are already declared or defined, the function has no effect. */
8887 objc_declare_protocols (tree names)
8892 if (current_namespace != global_namespace) {
8893 error ("Objective-C declarations may only appear in global scope");
8895 #endif /* OBJCPLUS */
8897 for (list = names; list; list = TREE_CHAIN (list))
8899 tree name = TREE_VALUE (list);
8901 if (lookup_protocol (name) == NULL_TREE)
8903 tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
8905 TYPE_LANG_SLOT_1 (protocol)
8906 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8907 PROTOCOL_NAME (protocol) = name;
8908 PROTOCOL_LIST (protocol) = NULL_TREE;
8909 add_protocol (protocol);
8910 PROTOCOL_DEFINED (protocol) = 0;
8911 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8917 start_protocol (enum tree_code code, tree name, tree list)
8922 if (current_namespace != global_namespace) {
8923 error ("Objective-C declarations may only appear in global scope");
8925 #endif /* OBJCPLUS */
8927 protocol = lookup_protocol (name);
8931 protocol = make_node (code);
8932 TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8934 PROTOCOL_NAME (protocol) = name;
8935 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list);
8936 add_protocol (protocol);
8937 PROTOCOL_DEFINED (protocol) = 1;
8938 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8940 check_protocol_recursively (protocol, list);
8942 else if (! PROTOCOL_DEFINED (protocol))
8944 PROTOCOL_DEFINED (protocol) = 1;
8945 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list);
8947 check_protocol_recursively (protocol, list);
8951 warning (0, "duplicate declaration for protocol %qE",
8958 /* "Encode" a data type into a string, which grows in util_obstack.
8960 The format is described in gcc/doc/objc.texi, section 'Type
8963 Most of the encode_xxx functions have a 'type' argument, which is
8964 the type to encode, and an integer 'curtype' argument, which is the
8965 index in the encoding string of the beginning of the encoding of
8966 the current type, and allows you to find what characters have
8967 already been written for the current type (they are the ones in the
8968 current encoding string starting from 'curtype').
8970 For example, if we are encoding a method which returns 'int' and
8971 takes a 'char **' argument, then when we get to the point of
8972 encoding the 'char **' argument, the encoded string already
8973 contains 'i12@0:4' (assuming a pointer size of 4 bytes). So,
8974 'curtype' will be set to 7 when starting to encode 'char **'.
8975 During the whole of the encoding of 'char **', 'curtype' will be
8976 fixed at 7, so the routine encoding the second pointer can find out
8977 that it's actually encoding a pointer to a pointer by looking
8978 backwards at what has already been encoded for the current type,
8979 and seeing there is a "^" (meaning a pointer) in there.
8983 /* Encode type qualifiers encodes one of the "PQ" Objective-C
8984 keywords, ie 'in', 'out', 'inout', 'bycopy', 'byref', 'oneway'.
8985 'const', instead, is encoded directly as part of the type.
8989 encode_type_qualifiers (tree declspecs)
8993 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
8995 /* FIXME: Shouldn't we use token->keyword here ? */
8996 if (ridpointers[(int) RID_IN] == TREE_VALUE (spec))
8997 obstack_1grow (&util_obstack, 'n');
8998 else if (ridpointers[(int) RID_INOUT] == TREE_VALUE (spec))
8999 obstack_1grow (&util_obstack, 'N');
9000 else if (ridpointers[(int) RID_OUT] == TREE_VALUE (spec))
9001 obstack_1grow (&util_obstack, 'o');
9002 else if (ridpointers[(int) RID_BYCOPY] == TREE_VALUE (spec))
9003 obstack_1grow (&util_obstack, 'O');
9004 else if (ridpointers[(int) RID_BYREF] == TREE_VALUE (spec))
9005 obstack_1grow (&util_obstack, 'R');
9006 else if (ridpointers[(int) RID_ONEWAY] == TREE_VALUE (spec))
9007 obstack_1grow (&util_obstack, 'V');
9011 /* Determine if a pointee is marked read-only. Only used by the NeXT
9012 runtime to be compatible with gcc-3.3. */
9015 pointee_is_readonly (tree pointee)
9017 while (POINTER_TYPE_P (pointee))
9018 pointee = TREE_TYPE (pointee);
9020 return TYPE_READONLY (pointee);
9023 /* Encode a pointer type. */
9026 encode_pointer (tree type, int curtype, int format)
9028 tree pointer_to = TREE_TYPE (type);
9030 if (flag_next_runtime)
9032 /* This code is used to be compatible with gcc-3.3. */
9033 /* For historical/compatibility reasons, the read-only qualifier
9034 of the pointee gets emitted _before_ the '^'. The read-only
9035 qualifier of the pointer itself gets ignored, _unless_ we are
9036 looking at a typedef! Also, do not emit the 'r' for anything
9037 but the outermost type! */
9038 if (!generating_instance_variables
9039 && (obstack_object_size (&util_obstack) - curtype <= 1)
9040 && (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9041 ? TYPE_READONLY (type)
9042 : pointee_is_readonly (pointer_to)))
9043 obstack_1grow (&util_obstack, 'r');
9046 if (TREE_CODE (pointer_to) == RECORD_TYPE)
9048 if (OBJC_TYPE_NAME (pointer_to)
9049 && TREE_CODE (OBJC_TYPE_NAME (pointer_to)) == IDENTIFIER_NODE)
9051 const char *name = IDENTIFIER_POINTER (OBJC_TYPE_NAME (pointer_to));
9053 if (strcmp (name, TAG_OBJECT) == 0) /* '@' */
9055 obstack_1grow (&util_obstack, '@');
9058 else if (TYPE_HAS_OBJC_INFO (pointer_to)
9059 && TYPE_OBJC_INTERFACE (pointer_to))
9061 if (generating_instance_variables)
9063 obstack_1grow (&util_obstack, '@');
9064 obstack_1grow (&util_obstack, '"');
9065 obstack_grow (&util_obstack, name, strlen (name));
9066 obstack_1grow (&util_obstack, '"');
9071 obstack_1grow (&util_obstack, '@');
9075 else if (strcmp (name, TAG_CLASS) == 0) /* '#' */
9077 obstack_1grow (&util_obstack, '#');
9080 else if (strcmp (name, TAG_SELECTOR) == 0) /* ':' */
9082 obstack_1grow (&util_obstack, ':');
9087 else if (TREE_CODE (pointer_to) == INTEGER_TYPE
9088 && TYPE_MODE (pointer_to) == QImode)
9090 tree pname = TREE_CODE (OBJC_TYPE_NAME (pointer_to)) == IDENTIFIER_NODE
9091 ? OBJC_TYPE_NAME (pointer_to)
9092 : DECL_NAME (OBJC_TYPE_NAME (pointer_to));
9094 /* (BOOL *) are an exception and are encoded as ^c, while all
9095 other pointers to char are encoded as *. */
9096 if (strcmp (IDENTIFIER_POINTER (pname), "BOOL"))
9098 if (!flag_next_runtime)
9100 /* The NeXT runtime adds the 'r' before getting here. */
9102 /* It appears that "r*" means "const char *" rather than
9103 "char *const". "char *const" is encoded as "*",
9104 which is identical to "char *", so the "const" is
9105 unfortunately lost. */
9106 if (TYPE_READONLY (pointer_to))
9107 obstack_1grow (&util_obstack, 'r');
9110 obstack_1grow (&util_obstack, '*');
9115 /* We have a normal pointer type that does not get special treatment. */
9116 obstack_1grow (&util_obstack, '^');
9117 encode_type (pointer_to, curtype, format);
9121 encode_array (tree type, int curtype, int format)
9123 tree an_int_cst = TYPE_SIZE (type);
9124 tree array_of = TREE_TYPE (type);
9127 if (an_int_cst == NULL)
9129 /* We are trying to encode an incomplete array. An incomplete
9130 array is forbidden as part of an instance variable. */
9131 if (generating_instance_variables)
9133 /* TODO: Detect this error earlier. */
9134 error ("instance variable has unknown size");
9138 /* So the only case in which an incomplete array could occur is
9139 if we are encoding the arguments or return value of a method.
9140 In that case, an incomplete array argument or return value
9141 (eg, -(void)display: (char[])string) is treated like a
9142 pointer because that is how the compiler does the function
9143 call. A special, more complicated case, is when the
9144 incomplete array is the last member of a struct (eg, if we
9145 are encoding "struct { unsigned long int a;double b[];}"),
9146 which is again part of a method argument/return value. In
9147 that case, we really need to communicate to the runtime that
9148 there is an incomplete array (not a pointer!) there. So, we
9149 detect that special case and encode it as a zero-length
9152 Try to detect that we are part of a struct. We do this by
9153 searching for '=' in the type encoding for the current type.
9154 NB: This hack assumes that you can't use '=' as part of a C
9158 char *enc = obstack_base (&util_obstack) + curtype;
9159 if (memchr (enc, '=',
9160 obstack_object_size (&util_obstack) - curtype) == NULL)
9162 /* We are not inside a struct. Encode the array as a
9164 encode_pointer (type, curtype, format);
9169 /* Else, we are in a struct, and we encode it as a zero-length
9171 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT)0);
9173 else if (TREE_INT_CST_LOW (TYPE_SIZE (array_of)) == 0)
9174 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT)0);
9176 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC,
9177 TREE_INT_CST_LOW (an_int_cst)
9178 / TREE_INT_CST_LOW (TYPE_SIZE (array_of)));
9180 obstack_grow (&util_obstack, buffer, strlen (buffer));
9181 encode_type (array_of, curtype, format);
9182 obstack_1grow (&util_obstack, ']');
9186 /* Encode a vector. The vector type is a GCC extension to C. */
9188 encode_vector (tree type, int curtype, int format)
9190 tree vector_of = TREE_TYPE (type);
9193 /* Vectors are like simple fixed-size arrays. */
9195 /* Output ![xx,yy,<code>] where xx is the vector_size, yy is the
9196 alignment of the vector, and <code> is the base type. Eg, int
9197 __attribute__ ((vector_size (16))) gets encoded as ![16,32,i]
9198 assuming that the alignment is 32 bytes. We include size and
9199 alignment in bytes so that the runtime does not have to have any
9200 knowledge of the actual types.
9202 sprintf (buffer, "![" HOST_WIDE_INT_PRINT_DEC ",%d",
9203 /* We want to compute the equivalent of sizeof (<vector>).
9204 Code inspired by c_sizeof_or_alignof_type. */
9205 ((TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type))
9206 / (TYPE_PRECISION (char_type_node) / BITS_PER_UNIT))),
9207 /* We want to compute the equivalent of __alignof__
9208 (<vector>). Code inspired by
9209 c_sizeof_or_alignof_type. */
9210 TYPE_ALIGN_UNIT (type));
9211 obstack_grow (&util_obstack, buffer, strlen (buffer));
9212 encode_type (vector_of, curtype, format);
9213 obstack_1grow (&util_obstack, ']');
9218 encode_aggregate_fields (tree type, bool pointed_to, int curtype, int format)
9220 tree field = TYPE_FIELDS (type);
9222 for (; field; field = DECL_CHAIN (field))
9225 /* C++ static members, and things that are not field at all,
9226 should not appear in the encoding. */
9227 if (TREE_CODE (field) != FIELD_DECL || TREE_STATIC (field))
9231 /* Recursively encode fields of embedded base classes. */
9232 if (DECL_ARTIFICIAL (field) && !DECL_NAME (field)
9233 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
9235 encode_aggregate_fields (TREE_TYPE (field),
9236 pointed_to, curtype, format);
9240 if (generating_instance_variables && !pointed_to)
9242 tree fname = DECL_NAME (field);
9244 obstack_1grow (&util_obstack, '"');
9246 if (fname && TREE_CODE (fname) == IDENTIFIER_NODE)
9247 obstack_grow (&util_obstack,
9248 IDENTIFIER_POINTER (fname),
9249 strlen (IDENTIFIER_POINTER (fname)));
9251 obstack_1grow (&util_obstack, '"');
9254 encode_field_decl (field, curtype, format);
9259 encode_aggregate_within (tree type, int curtype, int format, int left,
9263 /* NB: aggregates that are pointed to have slightly different encoding
9264 rules in that you never encode the names of instance variables. */
9265 int ob_size = obstack_object_size (&util_obstack);
9266 bool inline_contents = false;
9267 bool pointed_to = false;
9269 if (flag_next_runtime)
9271 if (ob_size > 0 && *(obstack_next_free (&util_obstack) - 1) == '^')
9274 if ((format == OBJC_ENCODE_INLINE_DEFS || generating_instance_variables)
9275 && (!pointed_to || ob_size - curtype == 1
9276 || (ob_size - curtype == 2
9277 && *(obstack_next_free (&util_obstack) - 2) == 'r')))
9278 inline_contents = true;
9282 /* c0 and c1 are the last two characters in the encoding of the
9283 current type; if the last two characters were '^' or '^r',
9284 then we are encoding an aggregate that is "pointed to". The
9285 comment above applies: in that case we should avoid encoding
9286 the names of instance variables.
9288 char c1 = ob_size > 1 ? *(obstack_next_free (&util_obstack) - 2) : 0;
9289 char c0 = ob_size > 0 ? *(obstack_next_free (&util_obstack) - 1) : 0;
9291 if (c0 == '^' || (c1 == '^' && c0 == 'r'))
9294 if (format == OBJC_ENCODE_INLINE_DEFS || generating_instance_variables)
9297 inline_contents = true;
9300 /* Note that the check (ob_size - curtype < 2) prevents
9301 infinite recursion when encoding a structure which is
9302 a linked list (eg, struct node { struct node *next;
9303 }). Each time we follow a pointer, we add one
9304 character to ob_size, and curtype is fixed, so after
9305 at most two pointers we stop inlining contents and
9308 The other case where we don't inline is "^r", which
9309 is a pointer to a constant struct.
9311 if ((ob_size - curtype <= 2) && !(c0 == 'r'))
9312 inline_contents = true;
9317 /* Traverse struct aliases; it is important to get the
9318 original struct and its tag name (if any). */
9319 type = TYPE_MAIN_VARIANT (type);
9320 name = OBJC_TYPE_NAME (type);
9321 /* Open parenth/bracket. */
9322 obstack_1grow (&util_obstack, left);
9324 /* Encode the struct/union tag name, or '?' if a tag was
9325 not provided. Typedef aliases do not qualify. */
9327 /* For compatibility with the NeXT runtime, ObjC++ encodes template
9328 args as a composite struct tag name. */
9329 if (name && TREE_CODE (name) == IDENTIFIER_NODE
9330 /* Did this struct have a tag? */
9331 && !TYPE_WAS_ANONYMOUS (type))
9332 obstack_grow (&util_obstack,
9333 decl_as_string (type, TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME),
9334 strlen (decl_as_string (type, TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME)));
9336 if (name && TREE_CODE (name) == IDENTIFIER_NODE)
9337 obstack_grow (&util_obstack,
9338 IDENTIFIER_POINTER (name),
9339 strlen (IDENTIFIER_POINTER (name)));
9342 obstack_1grow (&util_obstack, '?');
9344 /* Encode the types (and possibly names) of the inner fields,
9346 if (inline_contents)
9348 obstack_1grow (&util_obstack, '=');
9349 encode_aggregate_fields (type, pointed_to, curtype, format);
9351 /* Close parenth/bracket. */
9352 obstack_1grow (&util_obstack, right);
9355 /* Encode a bitfield NeXT-style (i.e., without a bit offset or the underlying
9359 encode_next_bitfield (int width)
9362 sprintf (buffer, "b%d", width);
9363 obstack_grow (&util_obstack, buffer, strlen (buffer));
9367 /* Encodes 'type', ignoring type qualifiers (which you should encode
9368 beforehand if needed) with the exception of 'const', which is
9369 encoded by encode_type. See above for the explanation of
9370 'curtype'. 'format' can be OBJC_ENCODE_INLINE_DEFS or
9371 OBJC_ENCODE_DONT_INLINE_DEFS.
9374 encode_type (tree type, int curtype, int format)
9376 enum tree_code code = TREE_CODE (type);
9378 /* Ignore type qualifiers other than 'const' when encoding a
9381 if (type == error_mark_node)
9384 if (!flag_next_runtime)
9386 if (TYPE_READONLY (type))
9387 obstack_1grow (&util_obstack, 'r');
9393 if (flag_next_runtime)
9395 /* Kludge for backwards-compatibility with gcc-3.3: enums
9396 are always encoded as 'i' no matter what type they
9397 actually are (!). */
9398 obstack_1grow (&util_obstack, 'i');
9401 /* Else, they are encoded exactly like the integer type that is
9402 used by the compiler to store them. */
9406 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
9408 case 8: c = TYPE_UNSIGNED (type) ? 'C' : 'c'; break;
9409 case 16: c = TYPE_UNSIGNED (type) ? 'S' : 's'; break;
9411 if (flag_next_runtime)
9414 /* Another legacy kludge for compatiblity with
9415 gcc-3.3: 32-bit longs are encoded as 'l' or 'L',
9416 but not always. For typedefs, we need to use 'i'
9417 or 'I' instead if encoding a struct field, or a
9419 int_type = ((!generating_instance_variables
9420 && (obstack_object_size (&util_obstack)
9421 == (unsigned) curtype))
9422 ? TYPE_MAIN_VARIANT (type)
9425 if (int_type == long_unsigned_type_node
9426 || int_type == long_integer_type_node)
9427 c = TYPE_UNSIGNED (type) ? 'L' : 'l';
9429 c = TYPE_UNSIGNED (type) ? 'I' : 'i';
9433 if (type == long_unsigned_type_node
9434 || type == long_integer_type_node)
9435 c = TYPE_UNSIGNED (type) ? 'L' : 'l';
9437 c = TYPE_UNSIGNED (type) ? 'I' : 'i';
9440 case 64: c = TYPE_UNSIGNED (type) ? 'Q' : 'q'; break;
9441 case 128: c = TYPE_UNSIGNED (type) ? 'T' : 't'; break;
9444 obstack_1grow (&util_obstack, c);
9450 /* Floating point types. */
9451 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
9453 case 32: c = 'f'; break;
9454 case 64: c = 'd'; break;
9456 case 128: c = 'D'; break;
9459 obstack_1grow (&util_obstack, c);
9463 obstack_1grow (&util_obstack, 'v');
9467 obstack_1grow (&util_obstack, 'B');
9471 encode_array (type, curtype, format);
9476 case REFERENCE_TYPE:
9478 encode_pointer (type, curtype, format);
9482 encode_aggregate_within (type, curtype, format, '{', '}');
9486 encode_aggregate_within (type, curtype, format, '(', ')');
9489 case FUNCTION_TYPE: /* '?' means an unknown type. */
9490 obstack_1grow (&util_obstack, '?');
9494 /* A complex is encoded as 'j' followed by the inner type (eg,
9495 "_Complex int" is encoded as 'ji'). */
9496 obstack_1grow (&util_obstack, 'j');
9497 encode_type (TREE_TYPE (type), curtype, format);
9501 encode_vector (type, curtype, format);
9505 warning (0, "unknown type %s found during Objective-C encoding",
9506 gen_type_name (type));
9507 obstack_1grow (&util_obstack, '?');
9511 if (flag_next_runtime)
9513 /* Super-kludge. Some ObjC qualifier and type combinations need
9514 to be rearranged for compatibility with gcc-3.3. */
9515 if (code == POINTER_TYPE && obstack_object_size (&util_obstack) >= 3)
9517 char *enc = obstack_base (&util_obstack) + curtype;
9519 /* Rewrite "in const" from "nr" to "rn". */
9520 if (curtype >= 1 && !strncmp (enc - 1, "nr", 2))
9521 strncpy (enc - 1, "rn", 2);
9527 encode_gnu_bitfield (int position, tree type, int size)
9529 enum tree_code code = TREE_CODE (type);
9531 char charType = '?';
9533 /* This code is only executed for the GNU runtime, so we can ignore
9534 the NeXT runtime kludge of always encoding enums as 'i' no matter
9535 what integers they actually are. */
9536 if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
9538 if (integer_zerop (TYPE_MIN_VALUE (type)))
9539 /* Unsigned integer types. */
9541 if (TYPE_MODE (type) == QImode)
9543 else if (TYPE_MODE (type) == HImode)
9545 else if (TYPE_MODE (type) == SImode)
9547 if (type == long_unsigned_type_node)
9552 else if (TYPE_MODE (type) == DImode)
9556 /* Signed integer types. */
9558 if (TYPE_MODE (type) == QImode)
9560 else if (TYPE_MODE (type) == HImode)
9562 else if (TYPE_MODE (type) == SImode)
9564 if (type == long_integer_type_node)
9570 else if (TYPE_MODE (type) == DImode)
9576 /* Do not do any encoding, produce an error and keep going. */
9577 error ("trying to encode non-integer type as a bitfield");
9581 sprintf (buffer, "b%d%c%d", position, charType, size);
9582 obstack_grow (&util_obstack, buffer, strlen (buffer));
9586 encode_field_decl (tree field_decl, int curtype, int format)
9589 /* C++ static members, and things that are not fields at all,
9590 should not appear in the encoding. */
9591 if (TREE_CODE (field_decl) != FIELD_DECL || TREE_STATIC (field_decl))
9595 /* Generate the bitfield typing information, if needed. Note the difference
9596 between GNU and NeXT runtimes. */
9597 if (DECL_BIT_FIELD_TYPE (field_decl))
9599 int size = tree_low_cst (DECL_SIZE (field_decl), 1);
9601 if (flag_next_runtime)
9602 encode_next_bitfield (size);
9604 encode_gnu_bitfield (int_bit_position (field_decl),
9605 DECL_BIT_FIELD_TYPE (field_decl), size);
9608 encode_type (TREE_TYPE (field_decl), curtype, format);
9611 /* Decay array and function parameters into pointers. */
9614 objc_decay_parm_type (tree type)
9616 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE)
9617 type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE
9624 static GTY(()) tree objc_parmlist = NULL_TREE;
9626 /* Append PARM to a list of formal parameters of a method, making a necessary
9627 array-to-pointer adjustment along the way. */
9630 objc_push_parm (tree parm)
9634 if (TREE_TYPE (parm) == error_mark_node)
9636 objc_parmlist = chainon (objc_parmlist, parm);
9640 /* Decay arrays and functions into pointers. */
9641 type = objc_decay_parm_type (TREE_TYPE (parm));
9643 /* If the parameter type has been decayed, a new PARM_DECL needs to be
9645 if (type != TREE_TYPE (parm))
9646 parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type);
9648 DECL_ARG_TYPE (parm)
9649 = lang_hooks.types.type_promotes_to (TREE_TYPE (parm));
9651 /* Record constancy and volatility. */
9652 c_apply_type_quals_to_decl
9653 ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0)
9654 | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0)
9655 | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm);
9657 objc_parmlist = chainon (objc_parmlist, parm);
9660 /* Retrieve the formal parameter list constructed via preceding calls to
9661 objc_push_parm(). */
9665 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED)
9667 static struct c_arg_info *
9668 objc_get_parm_info (int have_ellipsis)
9672 tree parm_info = objc_parmlist;
9673 objc_parmlist = NULL_TREE;
9677 tree parm_info = objc_parmlist;
9678 struct c_arg_info *arg_info;
9679 /* The C front-end requires an elaborate song and dance at
9682 declare_parm_level ();
9685 tree next = DECL_CHAIN (parm_info);
9687 DECL_CHAIN (parm_info) = NULL_TREE;
9688 parm_info = pushdecl (parm_info);
9689 finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
9692 arg_info = get_parm_info (have_ellipsis);
9694 objc_parmlist = NULL_TREE;
9699 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
9700 method definitions. In the case of instance methods, we can be more
9701 specific as to the type of 'self'. */
9704 synth_self_and_ucmd_args (void)
9708 if (objc_method_context
9709 && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL)
9710 self_type = objc_instance_type;
9712 /* Really a `struct objc_class *'. However, we allow people to
9713 assign to self, which changes its type midstream. */
9714 self_type = objc_object_type;
9717 objc_push_parm (build_decl (input_location,
9718 PARM_DECL, self_id, self_type));
9721 objc_push_parm (build_decl (input_location,
9722 PARM_DECL, ucmd_id, objc_selector_type));
9725 /* Transform an Objective-C method definition into a static C function
9726 definition, synthesizing the first two arguments, "self" and "_cmd",
9730 start_method_def (tree method)
9736 struct c_arg_info *parm_info;
9738 int have_ellipsis = 0;
9740 /* If we are defining a "dealloc" method in a non-root class, we
9741 will need to check if a [super dealloc] is missing, and warn if
9743 if(CLASS_SUPER_NAME (objc_implementation_context)
9744 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method))))
9745 should_call_super_dealloc = 1;
9747 should_call_super_dealloc = 0;
9749 /* Required to implement _msgSuper. */
9750 objc_method_context = method;
9751 UOBJC_SUPER_decl = NULL_TREE;
9753 /* Generate prototype declarations for arguments..."new-style". */
9754 synth_self_and_ucmd_args ();
9756 /* Generate argument declarations if a keyword_decl. */
9757 parmlist = METHOD_SEL_ARGS (method);
9760 tree type = TREE_VALUE (TREE_TYPE (parmlist)), parm;
9762 parm = build_decl (input_location,
9763 PARM_DECL, KEYWORD_ARG_NAME (parmlist), type);
9764 objc_push_parm (parm);
9765 parmlist = DECL_CHAIN (parmlist);
9768 if (METHOD_ADD_ARGS (method))
9772 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method));
9773 akey; akey = TREE_CHAIN (akey))
9775 objc_push_parm (TREE_VALUE (akey));
9778 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
9782 parm_info = objc_get_parm_info (have_ellipsis);
9784 really_start_method (objc_method_context, parm_info);
9787 /* Return 1 if TYPE1 is equivalent to TYPE2
9788 for purposes of method overloading. */
9791 objc_types_are_equivalent (tree type1, tree type2)
9796 /* Strip away indirections. */
9797 while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE)
9798 && (TREE_CODE (type1) == TREE_CODE (type2)))
9799 type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2);
9800 if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
9803 type1 = (TYPE_HAS_OBJC_INFO (type1)
9804 ? TYPE_OBJC_PROTOCOL_LIST (type1)
9806 type2 = (TYPE_HAS_OBJC_INFO (type2)
9807 ? TYPE_OBJC_PROTOCOL_LIST (type2)
9810 if (list_length (type1) == list_length (type2))
9812 for (; type2; type2 = TREE_CHAIN (type2))
9813 if (!lookup_protocol_in_reflist (type1, TREE_VALUE (type2)))
9820 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
9823 objc_types_share_size_and_alignment (tree type1, tree type2)
9825 return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
9826 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2));
9829 /* Return 1 if PROTO1 is equivalent to PROTO2
9830 for purposes of method overloading. Ordinarily, the type signatures
9831 should match up exactly, unless STRICT is zero, in which case we
9832 shall allow differences in which the size and alignment of a type
9836 comp_proto_with_proto (tree proto1, tree proto2, int strict)
9838 /* The following test is needed in case there are hashing
9840 if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2))
9843 return match_proto_with_proto (proto1, proto2, strict);
9847 match_proto_with_proto (tree proto1, tree proto2, int strict)
9851 /* Compare return types. */
9852 type1 = TREE_VALUE (TREE_TYPE (proto1));
9853 type2 = TREE_VALUE (TREE_TYPE (proto2));
9855 if (!objc_types_are_equivalent (type1, type2)
9856 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
9859 /* Compare argument types. */
9860 for (type1 = get_arg_type_list (proto1, METHOD_REF, 0),
9861 type2 = get_arg_type_list (proto2, METHOD_REF, 0);
9863 type1 = TREE_CHAIN (type1), type2 = TREE_CHAIN (type2))
9865 if (!objc_types_are_equivalent (TREE_VALUE (type1), TREE_VALUE (type2))
9867 || !objc_types_share_size_and_alignment (TREE_VALUE (type1),
9868 TREE_VALUE (type2))))
9872 return (!type1 && !type2);
9875 /* Fold an OBJ_TYPE_REF expression for ObjC method dispatches, where
9876 this occurs. ObjC method dispatches are _not_ like C++ virtual
9877 member function dispatches, and we account for the difference here. */
9880 objc_fold_obj_type_ref (tree ref, tree known_type)
9882 objc_fold_obj_type_ref (tree ref ATTRIBUTE_UNUSED,
9883 tree known_type ATTRIBUTE_UNUSED)
9887 tree v = BINFO_VIRTUALS (TYPE_BINFO (known_type));
9889 /* If the receiver does not have virtual member functions, there
9890 is nothing we can (or need to) do here. */
9894 /* Let C++ handle C++ virtual functions. */
9895 return cp_fold_obj_type_ref (ref, known_type);
9897 /* For plain ObjC, we currently do not need to do anything. */
9903 objc_start_function (tree name, tree type, tree attrs,
9907 struct c_arg_info *params
9911 tree fndecl = build_decl (input_location,
9912 FUNCTION_DECL, name, type);
9915 DECL_ARGUMENTS (fndecl) = params;
9916 DECL_INITIAL (fndecl) = error_mark_node;
9917 DECL_EXTERNAL (fndecl) = 0;
9918 TREE_STATIC (fndecl) = 1;
9919 retrofit_lang_decl (fndecl);
9920 cplus_decl_attributes (&fndecl, attrs, 0);
9921 start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT);
9923 current_function_returns_value = 0; /* Assume, until we see it does. */
9924 current_function_returns_null = 0;
9926 decl_attributes (&fndecl, attrs, 0);
9927 announce_function (fndecl);
9928 DECL_INITIAL (fndecl) = error_mark_node;
9929 DECL_EXTERNAL (fndecl) = 0;
9930 TREE_STATIC (fndecl) = 1;
9931 current_function_decl = pushdecl (fndecl);
9933 declare_parm_level ();
9934 DECL_RESULT (current_function_decl)
9935 = build_decl (input_location,
9936 RESULT_DECL, NULL_TREE,
9937 TREE_TYPE (TREE_TYPE (current_function_decl)));
9938 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1;
9939 DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1;
9940 start_fname_decls ();
9941 store_parm_decls_from (params);
9944 TREE_USED (current_function_decl) = 1;
9947 /* - Generate an identifier for the function. the format is "_n_cls",
9948 where 1 <= n <= nMethods, and cls is the name the implementation we
9950 - Install the return type from the method declaration.
9951 - If we have a prototype, check for type consistency. */
9954 really_start_method (tree method,
9958 struct c_arg_info *parmlist
9962 tree ret_type, meth_type;
9964 const char *sel_name, *class_name, *cat_name;
9967 /* Synth the storage class & assemble the return type. */
9968 ret_type = TREE_VALUE (TREE_TYPE (method));
9970 sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
9971 class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
9972 cat_name = ((TREE_CODE (objc_implementation_context)
9973 == CLASS_IMPLEMENTATION_TYPE)
9975 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context)));
9978 /* Make sure this is big enough for any plausible method label. */
9979 buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
9980 + (cat_name ? strlen (cat_name) : 0));
9982 OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
9983 class_name, cat_name, sel_name, method_slot);
9985 method_id = get_identifier (buf);
9988 /* Objective-C methods cannot be overloaded, so we don't need
9989 the type encoding appended. It looks bad anyway... */
9990 push_lang_context (lang_name_c);
9994 = build_function_type (ret_type,
9995 get_arg_type_list (method, METHOD_DEF, 0));
9996 objc_start_function (method_id, meth_type, NULL_TREE, parmlist);
9998 /* Set self_decl from the first argument. */
9999 self_decl = DECL_ARGUMENTS (current_function_decl);
10001 /* Suppress unused warnings. */
10002 TREE_USED (self_decl) = 1;
10003 DECL_READ_P (self_decl) = 1;
10004 TREE_USED (DECL_CHAIN (self_decl)) = 1;
10005 DECL_READ_P (DECL_CHAIN (self_decl)) = 1;
10007 pop_lang_context ();
10010 METHOD_DEFINITION (method) = current_function_decl;
10012 /* Check consistency...start_function, pushdecl, duplicate_decls. */
10014 if (implementation_template != objc_implementation_context)
10017 = lookup_method_static (implementation_template,
10018 METHOD_SEL_NAME (method),
10019 ((TREE_CODE (method) == CLASS_METHOD_DECL)
10020 | OBJC_LOOKUP_NO_SUPER));
10024 if (!comp_proto_with_proto (method, proto, 1))
10026 bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL;
10028 warning_at (DECL_SOURCE_LOCATION (method), 0,
10029 "conflicting types for %<%c%s%>",
10030 (type ? '-' : '+'),
10031 identifier_to_locale (gen_method_decl (method)));
10032 inform (DECL_SOURCE_LOCATION (proto),
10033 "previous declaration of %<%c%s%>",
10034 (type ? '-' : '+'),
10035 identifier_to_locale (gen_method_decl (proto)));
10040 /* We have a method @implementation even though we did not
10041 see a corresponding @interface declaration (which is allowed
10042 by Objective-C rules). Go ahead and place the method in
10043 the @interface anyway, so that message dispatch lookups
10045 tree interface = implementation_template;
10047 if (TREE_CODE (objc_implementation_context)
10048 == CATEGORY_IMPLEMENTATION_TYPE)
10049 interface = lookup_category
10051 CLASS_SUPER_NAME (objc_implementation_context));
10054 objc_add_method (interface, copy_node (method),
10055 TREE_CODE (method) == CLASS_METHOD_DECL,
10056 /* is_optional= */ false);
10061 static void *UOBJC_SUPER_scope = 0;
10063 /* _n_Method (id self, SEL sel, ...)
10065 struct objc_super _S;
10066 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
10070 get_super_receiver (void)
10072 if (objc_method_context)
10074 tree super_expr, super_expr_list;
10076 if (!UOBJC_SUPER_decl)
10078 UOBJC_SUPER_decl = build_decl (input_location,
10079 VAR_DECL, get_identifier (TAG_SUPER),
10080 objc_super_template);
10081 /* This prevents `unused variable' warnings when compiling with -Wall. */
10082 TREE_USED (UOBJC_SUPER_decl) = 1;
10083 DECL_READ_P (UOBJC_SUPER_decl) = 1;
10084 lang_hooks.decls.pushdecl (UOBJC_SUPER_decl);
10085 finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE,
10087 UOBJC_SUPER_scope = objc_get_current_scope ();
10090 /* Set receiver to self. */
10091 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id);
10092 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
10093 NOP_EXPR, input_location, self_decl,
10095 super_expr_list = super_expr;
10097 /* Set class to begin searching. */
10098 super_expr = objc_build_component_ref (UOBJC_SUPER_decl,
10099 get_identifier ("super_class"));
10101 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
10103 /* [_cls, __cls]Super are "pre-built" in
10104 synth_forward_declarations. */
10106 super_expr = build_modify_expr (input_location, super_expr,
10107 NULL_TREE, NOP_EXPR,
10109 ((TREE_CODE (objc_method_context)
10110 == INSTANCE_METHOD_DECL)
10112 : uucls_super_ref),
10117 /* We have a category. */
10119 tree super_name = CLASS_SUPER_NAME (implementation_template);
10122 /* Barf if super used in a category of Object. */
10125 error ("no super class declared in interface for %qE",
10126 CLASS_NAME (implementation_template));
10127 return error_mark_node;
10130 if (flag_next_runtime && !flag_zero_link)
10132 super_class = objc_get_class_reference (super_name);
10133 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
10134 /* If we are in a class method, we must retrieve the
10135 _metaclass_ for the current class, pointed at by
10136 the class's "isa" pointer. The following assumes that
10137 "isa" is the first ivar in a class (which it must be). */
10139 = build_indirect_ref
10141 build_c_cast (input_location,
10142 build_pointer_type (objc_class_type),
10143 super_class), RO_UNARY_STAR);
10147 add_class_reference (super_name);
10148 super_class = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
10149 ? objc_get_class_decl : objc_get_meta_class_decl);
10150 assemble_external (super_class);
10152 = build_function_call
10157 my_build_string_pointer
10158 (IDENTIFIER_LENGTH (super_name) + 1,
10159 IDENTIFIER_POINTER (super_name))));
10163 = build_modify_expr (input_location, super_expr, NULL_TREE,
10166 build_c_cast (input_location,
10167 TREE_TYPE (super_expr),
10172 super_expr_list = build_compound_expr (input_location,
10173 super_expr_list, super_expr);
10175 super_expr = build_unary_op (input_location,
10176 ADDR_EXPR, UOBJC_SUPER_decl, 0);
10177 super_expr_list = build_compound_expr (input_location,
10178 super_expr_list, super_expr);
10180 return super_expr_list;
10184 error ("[super ...] must appear in a method context");
10185 return error_mark_node;
10189 /* When exiting a scope, sever links to a 'super' declaration (if any)
10190 therein contained. */
10193 objc_clear_super_receiver (void)
10195 if (objc_method_context
10196 && UOBJC_SUPER_scope == objc_get_current_scope ()) {
10197 UOBJC_SUPER_decl = 0;
10198 UOBJC_SUPER_scope = 0;
10203 objc_finish_method_definition (tree fndecl)
10205 /* We cannot validly inline ObjC methods, at least not without a language
10206 extension to declare that a method need not be dynamically
10207 dispatched, so suppress all thoughts of doing so. */
10208 DECL_UNINLINABLE (fndecl) = 1;
10211 /* The C++ front-end will have called finish_function() for us. */
10212 finish_function ();
10215 METHOD_ENCODING (objc_method_context)
10216 = encode_method_prototype (objc_method_context);
10218 /* Required to implement _msgSuper. This must be done AFTER finish_function,
10219 since the optimizer may find "may be used before set" errors. */
10220 objc_method_context = NULL_TREE;
10222 if (should_call_super_dealloc)
10223 warning (0, "method possibly missing a [super dealloc] call");
10226 /* Given a tree DECL node, produce a printable description of it in the given
10227 buffer, overwriting the buffer. */
10230 gen_declaration (tree decl)
10236 gen_type_name_0 (TREE_TYPE (decl));
10238 if (DECL_NAME (decl))
10240 if (!POINTER_TYPE_P (TREE_TYPE (decl)))
10241 strcat (errbuf, " ");
10243 strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
10246 if (DECL_INITIAL (decl)
10247 && TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST)
10248 sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
10249 TREE_INT_CST_LOW (DECL_INITIAL (decl)));
10255 /* Given a tree TYPE node, produce a printable description of it in the given
10256 buffer, overwriting the buffer. */
10259 gen_type_name_0 (tree type)
10261 tree orig = type, proto;
10263 if (TYPE_P (type) && TYPE_NAME (type))
10264 type = TYPE_NAME (type);
10265 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
10267 tree inner = TREE_TYPE (type);
10269 while (TREE_CODE (inner) == ARRAY_TYPE)
10270 inner = TREE_TYPE (inner);
10272 gen_type_name_0 (inner);
10274 if (!POINTER_TYPE_P (inner))
10275 strcat (errbuf, " ");
10277 if (POINTER_TYPE_P (type))
10278 strcat (errbuf, "*");
10280 while (type != inner)
10282 strcat (errbuf, "[");
10284 if (TYPE_DOMAIN (type))
10288 sprintf (sz, HOST_WIDE_INT_PRINT_DEC,
10290 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1));
10291 strcat (errbuf, sz);
10294 strcat (errbuf, "]");
10295 type = TREE_TYPE (type);
10298 goto exit_function;
10301 if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type))
10302 type = DECL_NAME (type);
10304 strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE
10305 ? IDENTIFIER_POINTER (type)
10308 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
10309 if (objc_is_id (orig))
10310 orig = TREE_TYPE (orig);
10312 proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE;
10316 strcat (errbuf, " <");
10320 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto))));
10321 proto = TREE_CHAIN (proto);
10322 strcat (errbuf, proto ? ", " : ">");
10331 gen_type_name (tree type)
10335 return gen_type_name_0 (type);
10338 /* Given a method tree, put a printable description into the given
10339 buffer (overwriting) and return a pointer to the buffer. */
10342 gen_method_decl (tree method)
10346 strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */
10347 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method)));
10348 strcat (errbuf, ")");
10349 chain = METHOD_SEL_ARGS (method);
10353 /* We have a chain of keyword_decls. */
10356 if (KEYWORD_KEY_NAME (chain))
10357 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
10359 strcat (errbuf, ":(");
10360 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain)));
10361 strcat (errbuf, ")");
10363 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
10364 if ((chain = DECL_CHAIN (chain)))
10365 strcat (errbuf, " ");
10369 if (METHOD_ADD_ARGS (method))
10371 chain = TREE_CHAIN (METHOD_ADD_ARGS (method));
10373 /* Know we have a chain of parm_decls. */
10376 strcat (errbuf, ", ");
10377 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain)));
10378 chain = TREE_CHAIN (chain);
10381 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
10382 strcat (errbuf, ", ...");
10387 /* We have a unary selector. */
10388 strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
10396 /* Dump an @interface declaration of the supplied class CHAIN to the
10397 supplied file FP. Used to implement the -gen-decls option (which
10398 prints out an @interface declaration of all classes compiled in
10399 this run); potentially useful for debugging the compiler too. */
10401 dump_interface (FILE *fp, tree chain)
10403 /* FIXME: A heap overflow here whenever a method (or ivar)
10404 declaration is so long that it doesn't fit in the buffer. The
10405 code and all the related functions should be rewritten to avoid
10406 using fixed size buffers. */
10407 const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
10408 tree ivar_decls = CLASS_RAW_IVARS (chain);
10409 tree nst_methods = CLASS_NST_METHODS (chain);
10410 tree cls_methods = CLASS_CLS_METHODS (chain);
10412 fprintf (fp, "\n@interface %s", my_name);
10414 /* CLASS_SUPER_NAME is used to store the superclass name for
10415 classes, and the category name for categories. */
10416 if (CLASS_SUPER_NAME (chain))
10418 const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
10420 if (TREE_CODE (chain) == CATEGORY_IMPLEMENTATION_TYPE
10421 || TREE_CODE (chain) == CATEGORY_INTERFACE_TYPE)
10423 fprintf (fp, " (%s)\n", name);
10427 fprintf (fp, " : %s\n", name);
10431 fprintf (fp, "\n");
10433 /* FIXME - the following doesn't seem to work at the moment. */
10436 fprintf (fp, "{\n");
10439 fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls));
10440 ivar_decls = TREE_CHAIN (ivar_decls);
10442 while (ivar_decls);
10443 fprintf (fp, "}\n");
10446 while (nst_methods)
10448 fprintf (fp, "- %s;\n", gen_method_decl (nst_methods));
10449 nst_methods = TREE_CHAIN (nst_methods);
10452 while (cls_methods)
10454 fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods));
10455 cls_methods = TREE_CHAIN (cls_methods);
10458 fprintf (fp, "@end\n");
10461 /* Demangle function for Objective-C */
10462 static const char *
10463 objc_demangle (const char *mangled)
10465 char *demangled, *cp;
10467 if (mangled[0] == '_' &&
10468 (mangled[1] == 'i' || mangled[1] == 'c') &&
10471 cp = demangled = XNEWVEC (char, strlen(mangled) + 2);
10472 if (mangled[1] == 'i')
10473 *cp++ = '-'; /* for instance method */
10475 *cp++ = '+'; /* for class method */
10476 *cp++ = '['; /* opening left brace */
10477 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
10478 while (*cp && *cp == '_')
10479 cp++; /* skip any initial underbars in class name */
10480 cp = strchr(cp, '_'); /* find first non-initial underbar */
10483 free(demangled); /* not mangled name */
10486 if (cp[1] == '_') /* easy case: no category name */
10488 *cp++ = ' '; /* replace two '_' with one ' ' */
10489 strcpy(cp, mangled + (cp - demangled) + 2);
10493 *cp++ = '('; /* less easy case: category name */
10494 cp = strchr(cp, '_');
10497 free(demangled); /* not mangled name */
10501 *cp++ = ' '; /* overwriting 1st char of method name... */
10502 strcpy(cp, mangled + (cp - demangled)); /* get it back */
10504 while (*cp && *cp == '_')
10505 cp++; /* skip any initial underbars in method name */
10508 *cp = ':'; /* replace remaining '_' with ':' */
10509 *cp++ = ']'; /* closing right brace */
10510 *cp++ = 0; /* string terminator */
10514 return mangled; /* not an objc mangled name */
10518 objc_printable_name (tree decl, int kind ATTRIBUTE_UNUSED)
10520 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)));
10526 gcc_obstack_init (&util_obstack);
10527 util_firstobj = (char *) obstack_finish (&util_obstack);
10529 errbuf = XNEWVEC (char, 1024 * 10);
10531 synth_module_prologue ();
10537 struct imp_entry *impent;
10539 /* The internally generated initializers appear to have missing braces.
10540 Don't warn about this. */
10541 int save_warn_missing_braces = warn_missing_braces;
10542 warn_missing_braces = 0;
10544 /* A missing @end may not be detected by the parser. */
10545 if (objc_implementation_context)
10547 warning (0, "%<@end%> missing in implementation context");
10548 finish_class (objc_implementation_context);
10549 objc_ivar_chain = NULL_TREE;
10550 objc_implementation_context = NULL_TREE;
10553 /* Process the static instances here because initialization of objc_symtab
10554 depends on them. */
10555 if (objc_static_instances)
10556 generate_static_references ();
10558 /* forward declare categories */
10560 forward_declare_categories ();
10562 for (impent = imp_list; impent; impent = impent->next)
10564 objc_implementation_context = impent->imp_context;
10565 implementation_template = impent->imp_template;
10567 /* FIXME: This needs reworking to be more obvious. */
10569 UOBJC_CLASS_decl = impent->class_decl;
10570 UOBJC_METACLASS_decl = impent->meta_decl;
10572 /* Dump the @interface of each class as we compile it, if the
10573 -gen-decls option is in use. TODO: Dump the classes in the
10574 order they were found, rather than in reverse order as we
10576 if (flag_gen_declaration)
10578 dump_interface (gen_declaration_file, objc_implementation_context);
10581 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
10583 /* all of the following reference the string pool... */
10584 generate_ivar_lists ();
10585 generate_dispatch_tables ();
10586 generate_shared_structures (impent);
10590 generate_dispatch_tables ();
10591 generate_category (impent);
10594 impent->class_decl = UOBJC_CLASS_decl;
10595 impent->meta_decl = UOBJC_METACLASS_decl;
10598 /* If we are using an array of selectors, we must always
10599 finish up the array decl even if no selectors were used. */
10600 if (flag_next_runtime)
10601 build_next_selector_translation_table ();
10603 build_gnu_selector_translation_table ();
10605 if (protocol_chain)
10606 generate_protocols ();
10608 if (flag_next_runtime)
10609 generate_objc_image_info ();
10611 if (imp_list || class_names_chain
10612 || meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
10613 generate_objc_symtab_decl ();
10615 /* Arrange for ObjC data structures to be initialized at run time. */
10616 if (objc_implementation_context || class_names_chain || objc_static_instances
10617 || meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
10619 build_module_descriptor ();
10621 if (!flag_next_runtime)
10622 build_module_initializer_routine ();
10625 /* Dump the class references. This forces the appropriate classes
10626 to be linked into the executable image, preserving unix archive
10627 semantics. This can be removed when we move to a more dynamically
10628 linked environment. */
10630 for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
10632 handle_class_ref (chain);
10633 if (TREE_PURPOSE (chain))
10634 generate_classref_translation_entry (chain);
10637 for (impent = imp_list; impent; impent = impent->next)
10638 handle_impent (impent);
10645 /* Run through the selector hash tables and print a warning for any
10646 selector which has multiple methods. */
10648 for (slot = 0; slot < SIZEHASHTABLE; slot++)
10650 for (hsh = cls_method_hash_list[slot]; hsh; hsh = hsh->next)
10651 check_duplicates (hsh, 0, 1);
10652 for (hsh = nst_method_hash_list[slot]; hsh; hsh = hsh->next)
10653 check_duplicates (hsh, 0, 1);
10657 warn_missing_braces = save_warn_missing_braces;
10660 /* Subroutines of finish_objc. */
10663 generate_classref_translation_entry (tree chain)
10665 tree expr, decl, type;
10667 decl = TREE_PURPOSE (chain);
10668 type = TREE_TYPE (decl);
10670 expr = add_objc_string (TREE_VALUE (chain), class_names);
10671 expr = convert (type, expr); /* cast! */
10673 /* This is a class reference. It is re-written by the runtime,
10674 but will be optimized away unless we force it. */
10675 DECL_PRESERVE_P (decl) = 1;
10676 finish_var_decl (decl, expr);
10681 handle_class_ref (tree chain)
10683 const char *name = IDENTIFIER_POINTER (TREE_VALUE (chain));
10684 char *string = (char *) alloca (strlen (name) + 30);
10688 sprintf (string, "%sobjc_class_name_%s",
10689 (flag_next_runtime ? "." : "__"), name);
10691 #ifdef ASM_DECLARE_UNRESOLVED_REFERENCE
10692 if (flag_next_runtime)
10694 ASM_DECLARE_UNRESOLVED_REFERENCE (asm_out_file, string);
10699 /* Make a decl for this name, so we can use its address in a tree. */
10700 decl = build_decl (input_location,
10701 VAR_DECL, get_identifier (string), TREE_TYPE (integer_zero_node));
10702 DECL_EXTERNAL (decl) = 1;
10703 TREE_PUBLIC (decl) = 1;
10705 finish_var_decl (decl, 0);
10707 /* Make a decl for the address. */
10708 sprintf (string, "%sobjc_class_ref_%s",
10709 (flag_next_runtime ? "." : "__"), name);
10710 exp = build1 (ADDR_EXPR, string_type_node, decl);
10711 decl = build_decl (input_location,
10712 VAR_DECL, get_identifier (string), string_type_node);
10713 TREE_STATIC (decl) = 1;
10714 TREE_USED (decl) = 1;
10715 DECL_READ_P (decl) = 1;
10716 DECL_ARTIFICIAL (decl) = 1;
10717 DECL_INITIAL (decl) = error_mark_node;
10719 /* We must force the reference. */
10720 DECL_PRESERVE_P (decl) = 1;
10723 finish_var_decl (decl, exp);
10727 handle_impent (struct imp_entry *impent)
10731 objc_implementation_context = impent->imp_context;
10732 implementation_template = impent->imp_template;
10734 if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
10736 const char *const class_name =
10737 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
10739 string = (char *) alloca (strlen (class_name) + 30);
10741 sprintf (string, "%sobjc_class_name_%s",
10742 (flag_next_runtime ? "." : "__"), class_name);
10744 else if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
10746 const char *const class_name =
10747 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
10748 const char *const class_super_name =
10749 IDENTIFIER_POINTER (CLASS_SUPER_NAME (impent->imp_context));
10751 string = (char *) alloca (strlen (class_name)
10752 + strlen (class_super_name) + 30);
10754 /* Do the same for categories. Even though no references to
10755 these symbols are generated automatically by the compiler, it
10756 gives you a handle to pull them into an archive by hand. */
10757 sprintf (string, "*%sobjc_category_name_%s_%s",
10758 (flag_next_runtime ? "." : "__"), class_name, class_super_name);
10763 #ifdef ASM_DECLARE_CLASS_REFERENCE
10764 if (flag_next_runtime)
10766 ASM_DECLARE_CLASS_REFERENCE (asm_out_file, string);
10774 init = integer_zero_node;
10775 decl = build_decl (input_location,
10776 VAR_DECL, get_identifier (string), TREE_TYPE (init));
10777 TREE_PUBLIC (decl) = 1;
10778 TREE_READONLY (decl) = 1;
10779 TREE_USED (decl) = 1;
10780 TREE_CONSTANT (decl) = 1;
10781 DECL_CONTEXT (decl) = NULL_TREE;
10782 DECL_ARTIFICIAL (decl) = 1;
10783 TREE_STATIC (decl) = 1;
10784 DECL_INITIAL (decl) = error_mark_node; /* A real initializer is coming... */
10785 /* We must force the reference. */
10786 DECL_PRESERVE_P (decl) = 1;
10788 finish_var_decl(decl, init) ;
10792 /* The Fix-and-Continue functionality available in Mac OS X 10.3 and
10793 later requires that ObjC translation units participating in F&C be
10794 specially marked. The following routine accomplishes this. */
10796 /* static int _OBJC_IMAGE_INFO[2] = { 0, 1 }; */
10799 generate_objc_image_info (void)
10803 = ((flag_replace_objc_classes && imp_count ? 1 : 0)
10804 | (flag_objc_gc ? 2 : 0));
10805 VEC(constructor_elt,gc) *v = NULL;
10809 return; /* No need for an image_info entry. */
10811 array_type = build_sized_array_type (integer_type_node, 2);
10813 decl = start_var_decl (array_type, "_OBJC_IMAGE_INFO");
10815 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
10816 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (integer_type_node, flags));
10817 /* If we need this (determined above) it is because the runtime wants to
10818 refer to it in a manner hidden from the compiler. So we must force the
10820 DECL_PRESERVE_P (decl) = 1;
10821 finish_var_decl (decl, objc_build_constructor (TREE_TYPE (decl), v));
10824 /* Routine is called to issue diagnostic when reference to a private
10825 ivar is made and no other variable with same name is found in
10828 objc_diagnose_private_ivar (tree id)
10831 if (!objc_method_context)
10833 ivar = is_ivar (objc_ivar_chain, id);
10834 if (ivar && is_private (ivar))
10836 error ("instance variable %qs is declared private",
10837 IDENTIFIER_POINTER (id));
10843 /* Look up ID as an instance variable. OTHER contains the result of
10844 the C or C++ lookup, which we may want to use instead. */
10845 /* Also handle use of property as setter/getter. */
10847 objc_lookup_ivar (tree other, tree id)
10849 tree ivar, property;
10851 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
10852 if (!objc_method_context)
10855 if (!strcmp (IDENTIFIER_POINTER (id), "super"))
10856 /* We have a message to super. */
10857 return get_super_receiver ();
10859 /* In a class method, look up an instance variable only as a last
10861 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
10862 && other && other != error_mark_node)
10865 property = NULL_TREE;
10866 if (objc_implementation_context)
10867 property = is_property (objc_implementation_context, id);
10871 /* Look up the ivar, but do not use it if it is not accessible. */
10872 ivar = is_ivar (objc_ivar_chain, id);
10874 if (!ivar || is_private (ivar))
10878 /* In an instance method, a local variable (or parameter) may hide the
10879 instance variable. */
10880 if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
10881 && other && other != error_mark_node
10883 && CP_DECL_CONTEXT (other) != global_namespace)
10885 && !DECL_FILE_SCOPE_P (other))
10889 warning (0, "local declaration of %qE hides property", id);
10891 warning (0, "local declaration of %qE hides instance variable", id);
10897 return build_property_reference (property, id);
10899 /* At this point, we are either in an instance method with no obscuring
10900 local definitions, or in a class method with no alternate definitions
10902 return build_ivar_reference (id);
10905 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
10906 needs to be done if we are calling a function through a cast. */
10909 objc_rewrite_function_call (tree function, tree first_param)
10911 if (TREE_CODE (function) == NOP_EXPR
10912 && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR
10913 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0))
10916 function = build3 (OBJ_TYPE_REF, TREE_TYPE (function),
10917 TREE_OPERAND (function, 0),
10918 first_param, size_zero_node);
10924 /* Look for the special case of OBJC_TYPE_REF with the address of
10925 a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend or one
10926 of its cousins). */
10929 objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
10931 enum gimplify_status r0, r1;
10932 if (TREE_CODE (*expr_p) == OBJ_TYPE_REF
10933 && TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR
10934 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0))
10937 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
10938 value of the OBJ_TYPE_REF, so force them to be emitted
10939 during subexpression evaluation rather than after the
10940 OBJ_TYPE_REF. This permits objc_msgSend calls in Objective
10941 C to use direct rather than indirect calls when the
10942 object expression has a postincrement. */
10943 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL,
10944 is_gimple_val, fb_rvalue);
10945 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
10946 is_gimple_val, fb_rvalue);
10948 return MIN (r0, r1);
10952 return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p);
10954 return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
10958 /* This routine returns true if TYP is a valid objc object type,
10959 suitable for messaging; false otherwise.
10963 objc_type_valid_for_messaging (tree typ)
10965 if (!POINTER_TYPE_P (typ))
10969 typ = TREE_TYPE (typ); /* Remove indirections. */
10970 while (POINTER_TYPE_P (typ));
10972 if (TREE_CODE (typ) != RECORD_TYPE)
10975 return objc_is_object_id (typ) || TYPE_HAS_OBJC_INFO (typ);
10978 /* Begin code generation for fast enumeration (foreach) ... */
10982 struct __objcFastEnumerationState
10984 unsigned long state;
10986 unsigned long *mutationsPtr;
10987 unsigned long extra[5];
10990 Confusingly enough, NSFastEnumeration is then defined by libraries
10991 to be the same structure.
10995 build_fast_enumeration_state_template (void)
10997 tree decls, *chain = NULL;
11000 objc_fast_enumeration_state_template = objc_start_struct (get_identifier
11001 (TAG_FAST_ENUMERATION_STATE));
11003 /* unsigned long state; */
11004 decls = add_field_decl (long_unsigned_type_node, "state", &chain);
11006 /* id *itemsPtr; */
11007 add_field_decl (build_pointer_type (objc_object_type),
11008 "itemsPtr", &chain);
11010 /* unsigned long *mutationsPtr; */
11011 add_field_decl (build_pointer_type (long_unsigned_type_node),
11012 "mutationsPtr", &chain);
11014 /* unsigned long extra[5]; */
11015 add_field_decl (build_sized_array_type (long_unsigned_type_node, 5),
11019 objc_finish_struct (objc_fast_enumeration_state_template, decls);
11023 'objc_finish_foreach_loop()' generates the code for an Objective-C
11024 foreach loop. The 'location' argument is the location of the 'for'
11025 that starts the loop. The 'object_expression' is the expression of
11026 the 'object' that iterates; the 'collection_expression' is the
11027 expression of the collection that we iterate over (we need to make
11028 sure we evaluate this only once); the 'for_body' is the set of
11029 statements to be executed in each iteration; 'break_label' and
11030 'continue_label' are the break and continue labels which we need to
11031 emit since the <statements> may be jumping to 'break_label' (if they
11032 contain 'break') or to 'continue_label' (if they contain
11037 for (<object expression> in <collection expression>)
11040 which is compiled into the following blurb:
11043 id __objc_foreach_collection;
11044 __objc_fast_enumeration_state __objc_foreach_enum_state;
11045 unsigned long __objc_foreach_batchsize;
11046 id __objc_foreach_items[16];
11047 __objc_foreach_collection = <collection expression>;
11048 __objc_foreach_enum_state = { 0 };
11049 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
11051 if (__objc_foreach_batchsize == 0)
11052 <object expression> = nil;
11055 unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr;
11058 unsigned long __objc_foreach_index;
11059 __objc_foreach_index = 0;
11062 if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>);
11063 <object expression> = enumState.itemsPtr[__objc_foreach_index];
11064 <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label]
11067 __objc_foreach_index++;
11068 if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object;
11069 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
11071 if (__objc_foreach_batchsize != 0) goto next_batch;
11072 <object expression> = nil;
11077 'statements' may contain a 'continue' or 'break' instruction, which
11078 the user expects to 'continue' or 'break' the entire foreach loop.
11079 We are provided the labels that 'break' and 'continue' jump to, so
11080 we place them where we want them to jump to when they pick them.
11082 Optimization TODO: we could cache the IMP of
11083 countByEnumeratingWithState:objects:count:.
11086 /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */
11087 /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */
11089 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
11090 #include "tree-pretty-print.h"
11094 objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body,
11095 tree break_label, tree continue_label)
11097 /* A tree representing the __objcFastEnumerationState struct type,
11098 or NSFastEnumerationState struct, whatever we are using. */
11099 tree objc_fast_enumeration_state_type;
11101 /* The trees representing the declarations of each of the local variables. */
11102 tree objc_foreach_collection_decl;
11103 tree objc_foreach_enum_state_decl;
11104 tree objc_foreach_items_decl;
11105 tree objc_foreach_batchsize_decl;
11106 tree objc_foreach_mutations_pointer_decl;
11107 tree objc_foreach_index_decl;
11109 /* A tree representing the selector countByEnumeratingWithState:objects:count:. */
11110 tree selector_name;
11112 /* A tree representing the local bind. */
11115 /* A tree representing the external 'if (__objc_foreach_batchsize)' */
11118 /* A tree representing the 'else' part of 'first_if' */
11121 /* A tree representing the 'next_batch' label. */
11122 tree next_batch_label_decl;
11124 /* A tree representing the binding after the 'next_batch' label. */
11125 tree next_batch_bind;
11127 /* A tree representing the 'next_object' label. */
11128 tree next_object_label_decl;
11130 /* Temporary variables. */
11134 if (object_expression == error_mark_node)
11137 if (collection_expression == error_mark_node)
11140 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression)))
11142 error ("iterating variable in fast enumeration is not an object");
11146 if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression)))
11148 error ("collection in fast enumeration is not an object");
11152 /* TODO: Check that object_expression is either a variable
11153 declaration, or an lvalue. */
11155 /* This kludge is an idea from apple. We use the
11156 __objcFastEnumerationState struct implicitly defined by the
11157 compiler, unless a NSFastEnumerationState struct has been defined
11158 (by a Foundation library such as GNUstep Base) in which case, we
11161 objc_fast_enumeration_state_type = objc_fast_enumeration_state_template;
11163 tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState"));
11165 if (objc_NSFastEnumeration_type)
11167 /* TODO: We really need to check that
11168 objc_NSFastEnumeration_type is the same as ours! */
11169 if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL)
11171 /* If it's a typedef, use the original type. */
11172 if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type))
11173 objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type);
11175 objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type);
11181 /* Done by c-parser.c. */
11184 /* Done by c-parser.c. */
11186 /* id __objc_foreach_collection */
11187 objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection");
11189 /* __objcFastEnumerationState __objc_foreach_enum_state; */
11190 objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state");
11191 TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl;
11193 /* id __objc_foreach_items[16]; */
11194 objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items");
11195 TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl;
11197 /* unsigned long __objc_foreach_batchsize; */
11198 objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize");
11199 TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl;
11201 /* Generate the local variable binding. */
11202 bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL);
11203 SET_EXPR_LOCATION (bind, location);
11204 TREE_SIDE_EFFECTS (bind) = 1;
11206 /* __objc_foreach_collection = <collection expression>; */
11207 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression);
11208 SET_EXPR_LOCATION (t, location);
11209 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
11211 /* __objc_foreach_enum_state.state = 0; */
11212 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
11213 get_identifier ("state")),
11214 build_int_cst (long_unsigned_type_node, 0));
11215 SET_EXPR_LOCATION (t, location);
11216 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
11218 /* __objc_foreach_enum_state.itemsPtr = NULL; */
11219 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
11220 get_identifier ("itemsPtr")),
11221 null_pointer_node);
11222 SET_EXPR_LOCATION (t, location);
11223 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
11225 /* __objc_foreach_enum_state.mutationsPtr = NULL; */
11226 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
11227 get_identifier ("mutationsPtr")),
11228 null_pointer_node);
11229 SET_EXPR_LOCATION (t, location);
11230 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
11232 /* __objc_foreach_enum_state.extra[0] = 0; */
11233 /* __objc_foreach_enum_state.extra[1] = 0; */
11234 /* __objc_foreach_enum_state.extra[2] = 0; */
11235 /* __objc_foreach_enum_state.extra[3] = 0; */
11236 /* __objc_foreach_enum_state.extra[4] = 0; */
11237 for (i = 0; i < 5 ; i++)
11239 t = build2 (MODIFY_EXPR, void_type_node,
11240 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
11241 get_identifier ("extra")),
11242 build_int_cst (NULL_TREE, i)),
11243 build_int_cst (long_unsigned_type_node, 0));
11244 SET_EXPR_LOCATION (t, location);
11245 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
11248 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
11249 selector_name = get_identifier ("countByEnumeratingWithState:objects:count:");
11251 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
11253 tree_cons /* &__objc_foreach_enum_state */
11254 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
11255 tree_cons /* __objc_foreach_items */
11256 (NULL_TREE, objc_foreach_items_decl,
11258 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
11260 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
11262 struct c_expr array;
11263 array.value = objc_foreach_items_decl;
11264 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
11266 tree_cons /* &__objc_foreach_enum_state */
11267 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
11268 tree_cons /* __objc_foreach_items */
11269 (NULL_TREE, default_function_array_conversion (location, array).value,
11271 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
11274 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
11275 convert (long_unsigned_type_node, t));
11276 SET_EXPR_LOCATION (t, location);
11277 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
11279 /* if (__objc_foreach_batchsize == 0) */
11280 first_if = build3 (COND_EXPR, void_type_node,
11283 (c_common_truthvalue_conversion
11285 build_binary_op (location,
11287 objc_foreach_batchsize_decl,
11288 build_int_cst (long_unsigned_type_node, 0), 1)),
11290 /* Then block (we fill it in later). */
11292 /* Else block (we fill it in later). */
11294 SET_EXPR_LOCATION (first_if, location);
11295 append_to_statement_list (first_if, &BIND_EXPR_BODY (bind));
11297 /* then <object expression> = nil; */
11298 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
11299 SET_EXPR_LOCATION (t, location);
11300 COND_EXPR_THEN (first_if) = t;
11302 /* Now we build the 'else' part of the if; once we finish building
11303 it, we attach it to first_if as the 'else' part. */
11308 /* unsigned long __objc_foreach_mutations_pointer; */
11309 objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer");
11311 /* Generate the local variable binding. */
11312 first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL);
11313 SET_EXPR_LOCATION (first_else, location);
11314 TREE_SIDE_EFFECTS (first_else) = 1;
11316 /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */
11317 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl,
11318 build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
11319 get_identifier ("mutationsPtr")),
11321 SET_EXPR_LOCATION (t, location);
11322 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
11325 next_batch_label_decl = create_artificial_label (location);
11326 t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl);
11327 SET_EXPR_LOCATION (t, location);
11328 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
11332 /* unsigned long __objc_foreach_index; */
11333 objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index");
11335 /* Generate the local variable binding. */
11336 next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL);
11337 SET_EXPR_LOCATION (next_batch_bind, location);
11338 TREE_SIDE_EFFECTS (next_batch_bind) = 1;
11339 append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else));
11341 /* __objc_foreach_index = 0; */
11342 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
11343 build_int_cst (long_unsigned_type_node, 0));
11344 SET_EXPR_LOCATION (t, location);
11345 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
11348 next_object_label_decl = create_artificial_label (location);
11349 t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl);
11350 SET_EXPR_LOCATION (t, location);
11351 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
11353 /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */
11354 t = build3 (COND_EXPR, void_type_node,
11357 (c_common_truthvalue_conversion
11362 objc_foreach_mutations_pointer_decl,
11363 build_indirect_ref (location,
11364 objc_build_component_ref (objc_foreach_enum_state_decl,
11365 get_identifier ("mutationsPtr")),
11366 RO_UNARY_STAR), 1)),
11369 build_function_call (input_location,
11370 objc_enumeration_mutation_decl,
11371 tree_cons (NULL, collection_expression, NULL)),
11374 SET_EXPR_LOCATION (t, location);
11375 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
11377 /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */
11378 t = build2 (MODIFY_EXPR, void_type_node, object_expression,
11379 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
11380 get_identifier ("itemsPtr")),
11381 objc_foreach_index_decl));
11382 SET_EXPR_LOCATION (t, location);
11383 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
11385 /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */
11386 append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind));
11388 /* continue_label: */
11389 if (continue_label)
11391 t = build1 (LABEL_EXPR, void_type_node, continue_label);
11392 SET_EXPR_LOCATION (t, location);
11393 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
11396 /* __objc_foreach_index++; */
11397 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
11398 build_binary_op (location,
11400 objc_foreach_index_decl,
11401 build_int_cst (long_unsigned_type_node, 1), 1));
11402 SET_EXPR_LOCATION (t, location);
11403 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
11405 /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */
11406 t = build3 (COND_EXPR, void_type_node,
11409 (c_common_truthvalue_conversion
11411 build_binary_op (location,
11413 objc_foreach_index_decl,
11414 objc_foreach_batchsize_decl, 1)),
11417 build1 (GOTO_EXPR, void_type_node, next_object_label_decl),
11420 SET_EXPR_LOCATION (t, location);
11421 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
11423 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
11425 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
11427 tree_cons /* &__objc_foreach_enum_state */
11428 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
11429 tree_cons /* __objc_foreach_items */
11430 (NULL_TREE, objc_foreach_items_decl,
11432 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
11434 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
11436 struct c_expr array;
11437 array.value = objc_foreach_items_decl;
11438 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
11440 tree_cons /* &__objc_foreach_enum_state */
11441 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
11442 tree_cons /* __objc_foreach_items */
11443 (NULL_TREE, default_function_array_conversion (location, array).value,
11445 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
11448 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
11449 convert (long_unsigned_type_node, t));
11450 SET_EXPR_LOCATION (t, location);
11451 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
11455 /* if (__objc_foreach_batchsize != 0) goto next_batch; */
11456 t = build3 (COND_EXPR, void_type_node,
11459 (c_common_truthvalue_conversion
11461 build_binary_op (location,
11463 objc_foreach_batchsize_decl,
11464 build_int_cst (long_unsigned_type_node, 0), 1)),
11467 build1 (GOTO_EXPR, void_type_node, next_batch_label_decl),
11470 SET_EXPR_LOCATION (t, location);
11471 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
11473 /* <object expression> = nil; */
11474 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
11475 SET_EXPR_LOCATION (t, location);
11476 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
11481 t = build1 (LABEL_EXPR, void_type_node, break_label);
11482 SET_EXPR_LOCATION (t, location);
11483 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
11487 COND_EXPR_ELSE (first_if) = first_else;
11489 /* Do the whole thing. */
11492 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
11493 /* This will print to stderr the whole blurb generated by the
11494 compiler while compiling (assuming the compiler doesn't crash
11495 before getting here).
11497 debug_generic_stmt (bind);
11501 /* Done by c-parser.c */
11504 #include "gt-objc-objc-act.h"