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 Free Software Foundation, Inc.
4 Contributed by Steve Naroff.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* Purpose: This module implements the Objective-C 4.0 language.
25 compatibility issues (with the Stepstone translator):
27 - does not recognize the following 3.3 constructs.
28 @requires, @classes, @messages, = (...)
29 - methods with variable arguments must conform to ANSI standard.
30 - tagged structure definitions that appear in BOTH the interface
31 and implementation are not allowed.
32 - public/private: all instance variables are public within the
33 context of the implementation...I consider this to be a bug in
35 - statically allocated objects are not supported. the user will
36 receive an error if this service is requested.
38 code generation `options':
44 #include "coretypes.h"
61 #include "langhooks.h"
72 #include "diagnostic.h"
75 #include "tree-iterator.h"
78 #include "langhooks-def.h"
80 #define OBJC_VOID_AT_END void_list_node
82 static unsigned int should_call_super_dealloc = 0;
84 /* When building Objective-C++, we need in_late_binary_op. */
86 bool in_late_binary_op = false;
89 /* When building Objective-C++, we are not linking against the C front-end
90 and so need to replicate the C tree-construction functions in some way. */
92 #define OBJCP_REMAP_FUNCTIONS
93 #include "objcp-decl.h"
96 /* This is the default way of generating a method name. */
97 /* I am not sure it is really correct.
98 Perhaps there's a danger that it will make name conflicts
99 if method names contain underscores. -- rms. */
100 #ifndef OBJC_GEN_METHOD_LABEL
101 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
104 sprintf ((BUF), "_%s_%s_%s_%s", \
105 ((IS_INST) ? "i" : "c"), \
107 ((CAT_NAME)? (CAT_NAME) : ""), \
109 for (temp = (BUF); *temp; temp++) \
110 if (*temp == ':') *temp = '_'; \
114 /* These need specifying. */
115 #ifndef OBJC_FORWARDING_STACK_OFFSET
116 #define OBJC_FORWARDING_STACK_OFFSET 0
119 #ifndef OBJC_FORWARDING_MIN_OFFSET
120 #define OBJC_FORWARDING_MIN_OFFSET 0
123 /* Set up for use of obstacks. */
127 /* This obstack is used to accumulate the encoding of a data type. */
128 static struct obstack util_obstack;
130 /* This points to the beginning of obstack contents, so we can free
131 the whole contents. */
134 /* The version identifies which language generation and runtime
135 the module (file) was compiled for, and is recorded in the
136 module descriptor. */
138 #define OBJC_VERSION (flag_next_runtime ? 6 : 8)
139 #define PROTOCOL_VERSION 2
141 /* (Decide if these can ever be validly changed.) */
142 #define OBJC_ENCODE_INLINE_DEFS 0
143 #define OBJC_ENCODE_DONT_INLINE_DEFS 1
145 /*** Private Interface (procedures) ***/
147 /* Used by compile_file. */
149 static void init_objc (void);
150 static void finish_objc (void);
152 /* Code generation. */
154 static tree objc_build_constructor (tree, tree);
155 static tree build_objc_method_call (location_t, int, tree, tree, tree, tree);
156 static tree get_proto_encoding (tree);
157 static tree lookup_interface (tree);
158 static tree objc_add_static_instance (tree, tree);
160 static tree start_class (enum tree_code, tree, tree, tree);
161 static tree continue_class (tree);
162 static void finish_class (tree);
163 static void start_method_def (tree);
165 static void objc_start_function (tree, tree, tree, tree);
167 static void objc_start_function (tree, tree, tree, struct c_arg_info *);
169 static tree start_protocol (enum tree_code, tree, tree);
170 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
171 static tree objc_add_method (tree, tree, int);
172 static tree add_instance_variable (tree, int, tree);
173 static tree build_ivar_reference (tree);
174 static tree is_ivar (tree, tree);
176 static void build_objc_exception_stuff (void);
177 static void build_next_objc_exception_stuff (void);
179 /* We only need the following for ObjC; ObjC++ will use C++'s definition
180 of DERIVED_FROM_P. */
182 static bool objc_derived_from_p (tree, tree);
183 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
185 static void objc_xref_basetypes (tree, tree);
187 static void build_class_template (void);
188 static void build_selector_template (void);
189 static void build_category_template (void);
190 static void build_super_template (void);
191 static tree build_protocol_initializer (tree, tree, tree, tree, tree);
192 static tree get_class_ivars (tree, bool);
193 static tree generate_protocol_list (tree);
194 static void build_protocol_reference (tree);
197 static void objc_generate_cxx_cdtors (void);
200 static const char *synth_id_with_class_suffix (const char *, tree);
202 /* Hash tables to manage the global pool of method prototypes. */
204 hash *nst_method_hash_list = 0;
205 hash *cls_method_hash_list = 0;
207 static hash hash_lookup (hash *, tree);
208 static tree lookup_method (tree, tree);
209 static tree lookup_method_static (tree, tree, int);
213 class_names, /* class, category, protocol, module names */
214 meth_var_names, /* method and variable names */
215 meth_var_types /* method and variable type descriptors */
218 static tree add_objc_string (tree, enum string_section);
219 static tree build_objc_string_decl (enum string_section);
220 static void build_selector_table_decl (void);
222 /* Protocol additions. */
224 static tree lookup_protocol (tree);
225 static tree lookup_and_install_protocols (tree);
229 static void encode_type_qualifiers (tree);
230 static void encode_type (tree, int, int);
231 static void encode_field_decl (tree, int, int);
234 static void really_start_method (tree, tree);
236 static void really_start_method (tree, struct c_arg_info *);
238 static int comp_proto_with_proto (tree, tree, int);
239 static void objc_push_parm (tree);
241 static tree objc_get_parm_info (int);
243 static struct c_arg_info *objc_get_parm_info (int);
246 /* Utilities for debugging and error diagnostics. */
248 static char *gen_type_name (tree);
249 static char *gen_type_name_0 (tree);
250 static char *gen_method_decl (tree);
251 static char *gen_declaration (tree);
253 /* Everything else. */
255 static tree create_field_decl (tree, const char *);
256 static void add_class_reference (tree);
257 static void build_protocol_template (void);
258 static tree encode_method_prototype (tree);
259 static void generate_classref_translation_entry (tree);
260 static void handle_class_ref (tree);
261 static void generate_struct_by_value_array (void)
263 static void mark_referenced_methods (void);
264 static void generate_objc_image_info (void);
266 /*** Private Interface (data) ***/
268 /* Reserved tag definitions. */
270 #define OBJECT_TYPEDEF_NAME "id"
271 #define CLASS_TYPEDEF_NAME "Class"
273 #define TAG_OBJECT "objc_object"
274 #define TAG_CLASS "objc_class"
275 #define TAG_SUPER "objc_super"
276 #define TAG_SELECTOR "objc_selector"
278 #define UTAG_CLASS "_objc_class"
279 #define UTAG_IVAR "_objc_ivar"
280 #define UTAG_IVAR_LIST "_objc_ivar_list"
281 #define UTAG_METHOD "_objc_method"
282 #define UTAG_METHOD_LIST "_objc_method_list"
283 #define UTAG_CATEGORY "_objc_category"
284 #define UTAG_MODULE "_objc_module"
285 #define UTAG_SYMTAB "_objc_symtab"
286 #define UTAG_SUPER "_objc_super"
287 #define UTAG_SELECTOR "_objc_selector"
289 #define UTAG_PROTOCOL "_objc_protocol"
290 #define UTAG_METHOD_PROTOTYPE "_objc_method_prototype"
291 #define UTAG_METHOD_PROTOTYPE_LIST "_objc__method_prototype_list"
293 /* Note that the string object global name is only needed for the
295 #define STRING_OBJECT_GLOBAL_FORMAT "_%sClassReference"
297 #define PROTOCOL_OBJECT_CLASS_NAME "Protocol"
299 static const char *TAG_GETCLASS;
300 static const char *TAG_GETMETACLASS;
301 static const char *TAG_MSGSEND;
302 static const char *TAG_MSGSENDSUPER;
303 /* The NeXT Objective-C messenger may have two extra entry points, for use
304 when returning a structure. */
305 static const char *TAG_MSGSEND_STRET;
306 static const char *TAG_MSGSENDSUPER_STRET;
307 static const char *default_constant_string_class_name;
309 /* Runtime metadata flags. */
310 #define CLS_FACTORY 0x0001L
311 #define CLS_META 0x0002L
312 #define CLS_HAS_CXX_STRUCTORS 0x2000L
314 #define OBJC_MODIFIER_STATIC 0x00000001
315 #define OBJC_MODIFIER_FINAL 0x00000002
316 #define OBJC_MODIFIER_PUBLIC 0x00000004
317 #define OBJC_MODIFIER_PRIVATE 0x00000008
318 #define OBJC_MODIFIER_PROTECTED 0x00000010
319 #define OBJC_MODIFIER_NATIVE 0x00000020
320 #define OBJC_MODIFIER_SYNCHRONIZED 0x00000040
321 #define OBJC_MODIFIER_ABSTRACT 0x00000080
322 #define OBJC_MODIFIER_VOLATILE 0x00000100
323 #define OBJC_MODIFIER_TRANSIENT 0x00000200
324 #define OBJC_MODIFIER_NONE_SPECIFIED 0x80000000
326 /* NeXT-specific tags. */
328 #define TAG_MSGSEND_NONNIL "objc_msgSendNonNil"
329 #define TAG_MSGSEND_NONNIL_STRET "objc_msgSendNonNil_stret"
330 #define TAG_EXCEPTIONEXTRACT "objc_exception_extract"
331 #define TAG_EXCEPTIONTRYENTER "objc_exception_try_enter"
332 #define TAG_EXCEPTIONTRYEXIT "objc_exception_try_exit"
333 #define TAG_EXCEPTIONMATCH "objc_exception_match"
334 #define TAG_EXCEPTIONTHROW "objc_exception_throw"
335 #define TAG_SYNCENTER "objc_sync_enter"
336 #define TAG_SYNCEXIT "objc_sync_exit"
337 #define TAG_SETJMP "_setjmp"
338 #define UTAG_EXCDATA "_objc_exception_data"
340 #define TAG_ASSIGNIVAR "objc_assign_ivar"
341 #define TAG_ASSIGNGLOBAL "objc_assign_global"
342 #define TAG_ASSIGNSTRONGCAST "objc_assign_strongCast"
344 /* Branch entry points. All that matters here are the addresses;
345 functions with these names do not really exist in libobjc. */
347 #define TAG_MSGSEND_FAST "objc_msgSend_Fast"
348 #define TAG_ASSIGNIVAR_FAST "objc_assign_ivar_Fast"
350 #define TAG_CXX_CONSTRUCT ".cxx_construct"
351 #define TAG_CXX_DESTRUCT ".cxx_destruct"
353 /* GNU-specific tags. */
355 #define TAG_EXECCLASS "__objc_exec_class"
356 #define TAG_GNUINIT "__objc_gnu_init"
358 /* Flags for lookup_method_static(). */
359 #define OBJC_LOOKUP_CLASS 1 /* Look for class methods. */
360 #define OBJC_LOOKUP_NO_SUPER 2 /* Do not examine superclasses. */
362 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
363 tree objc_global_trees[OCTI_MAX];
365 static void handle_impent (struct imp_entry *);
367 struct imp_entry *imp_list = 0;
368 int imp_count = 0; /* `@implementation' */
369 int cat_count = 0; /* `@category' */
371 enum tree_code objc_inherit_code;
372 int objc_public_flag;
374 /* Use to generate method labels. */
375 static int method_slot = 0;
379 static char *errbuf; /* Buffer for error diagnostics */
381 /* Data imported from tree.c. */
383 extern enum debug_info_type write_symbols;
385 /* Data imported from toplev.c. */
387 extern const char *dump_base_name;
389 static int flag_typed_selectors;
391 /* Store all constructed constant strings in a hash table so that
392 they get uniqued properly. */
394 struct GTY(()) string_descriptor {
395 /* The literal argument . */
398 /* The resulting constant string. */
402 static GTY((param_is (struct string_descriptor))) htab_t string_htab;
404 /* Store the EH-volatilized types in a hash table, for easy retrieval. */
405 struct GTY(()) volatilized_type {
409 static GTY((param_is (struct volatilized_type))) htab_t volatilized_htab;
411 FILE *gen_declaration_file;
413 /* Tells "encode_pointer/encode_aggregate" whether we are generating
414 type descriptors for instance variables (as opposed to methods).
415 Type descriptors for instance variables contain more information
416 than methods (for static typing and embedded structures). */
418 static int generating_instance_variables = 0;
420 /* For building an objc struct. These may not be used when this file
421 is compiled as part of obj-c++. */
423 static bool objc_building_struct;
424 static struct c_struct_parse_info *objc_struct_info ATTRIBUTE_UNUSED;
426 /* Start building a struct for objc. */
429 objc_start_struct (tree name)
431 gcc_assert (!objc_building_struct);
432 objc_building_struct = true;
433 return start_struct (input_location, RECORD_TYPE, name, &objc_struct_info);
436 /* Finish building a struct for objc. */
439 objc_finish_struct (tree type, tree fieldlist)
441 gcc_assert (objc_building_struct);
442 objc_building_struct = false;
443 return finish_struct (input_location, type, fieldlist, NULL_TREE,
447 /* Some platforms pass small structures through registers versus
448 through an invisible pointer. Determine at what size structure is
449 the transition point between the two possibilities. */
452 generate_struct_by_value_array (void)
455 tree field_decl, field_decl_chain;
457 int aggregate_in_mem[32];
460 /* Presumably no platform passes 32 byte structures in a register. */
461 for (i = 1; i < 32; i++)
465 /* Create an unnamed struct that has `i' character components */
466 type = objc_start_struct (NULL_TREE);
468 strcpy (buffer, "c1");
469 field_decl = create_field_decl (char_type_node,
471 field_decl_chain = field_decl;
473 for (j = 1; j < i; j++)
475 sprintf (buffer, "c%d", j + 1);
476 field_decl = create_field_decl (char_type_node,
478 chainon (field_decl_chain, field_decl);
480 objc_finish_struct (type, field_decl_chain);
482 aggregate_in_mem[i] = aggregate_value_p (type, 0);
483 if (!aggregate_in_mem[i])
487 /* We found some structures that are returned in registers instead of memory
488 so output the necessary data. */
491 for (i = 31; i >= 0; i--)
492 if (!aggregate_in_mem[i])
494 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n\n", i);
496 /* The first member of the structure is always 0 because we don't handle
497 structures with 0 members */
498 printf ("static int struct_forward_array[] = {\n 0");
500 for (j = 1; j <= i; j++)
501 printf (", %d", aggregate_in_mem[j]);
512 if (cxx_init () == false)
514 if (c_objc_common_init () == false)
518 /* If gen_declaration desired, open the output file. */
519 if (flag_gen_declaration)
521 register char * const dumpname = concat (dump_base_name, ".decl", NULL);
522 gen_declaration_file = fopen (dumpname, "w");
523 if (gen_declaration_file == 0)
524 fatal_error ("can't open %s: %m", dumpname);
528 if (flag_next_runtime)
530 TAG_GETCLASS = "objc_getClass";
531 TAG_GETMETACLASS = "objc_getMetaClass";
532 TAG_MSGSEND = "objc_msgSend";
533 TAG_MSGSENDSUPER = "objc_msgSendSuper";
534 TAG_MSGSEND_STRET = "objc_msgSend_stret";
535 TAG_MSGSENDSUPER_STRET = "objc_msgSendSuper_stret";
536 default_constant_string_class_name = "NSConstantString";
540 TAG_GETCLASS = "objc_get_class";
541 TAG_GETMETACLASS = "objc_get_meta_class";
542 TAG_MSGSEND = "objc_msg_lookup";
543 TAG_MSGSENDSUPER = "objc_msg_lookup_super";
544 /* GNU runtime does not provide special functions to support
545 structure-returning methods. */
546 default_constant_string_class_name = "NXConstantString";
547 flag_typed_selectors = 1;
552 if (print_struct_values && !flag_compare_debug)
553 generate_struct_by_value_array ();
559 objc_finish_file (void)
561 mark_referenced_methods ();
564 /* We need to instantiate templates _before_ we emit ObjC metadata;
565 if we do not, some metadata (such as selectors) may go missing. */
567 instantiate_pending_templates (0);
570 /* Finalize Objective-C runtime data. No need to generate tables
571 and code if only checking syntax, or if generating a PCH file. */
572 if (!flag_syntax_only && !pch_file)
575 if (gen_declaration_file)
576 fclose (gen_declaration_file);
579 /* Return the first occurrence of a method declaration corresponding
580 to sel_name in rproto_list. Search rproto_list recursively.
581 If is_class is 0, search for instance methods, otherwise for class
584 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
590 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
592 p = TREE_VALUE (rproto);
594 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
596 if ((fnd = lookup_method (is_class
597 ? PROTOCOL_CLS_METHODS (p)
598 : PROTOCOL_NST_METHODS (p), sel_name)))
600 else if (PROTOCOL_LIST (p))
601 fnd = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
606 ; /* An identifier...if we could not find a protocol. */
617 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
621 /* Make sure the protocol is supported by the object on the rhs. */
622 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
625 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
627 p = TREE_VALUE (rproto);
629 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
634 else if (PROTOCOL_LIST (p))
635 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
644 ; /* An identifier...if we could not find a protocol. */
651 objc_start_class_interface (tree klass, tree super_class, tree protos)
653 objc_interface_context
655 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos);
656 objc_public_flag = 0;
660 objc_start_category_interface (tree klass, tree categ, tree protos)
662 objc_interface_context
663 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos);
665 = continue_class (objc_interface_context);
669 objc_start_protocol (tree name, tree protos)
671 objc_interface_context
672 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos);
676 objc_continue_interface (void)
679 = continue_class (objc_interface_context);
683 objc_finish_interface (void)
685 finish_class (objc_interface_context);
686 objc_interface_context = NULL_TREE;
690 objc_start_class_implementation (tree klass, tree super_class)
692 objc_implementation_context
694 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE);
695 objc_public_flag = 0;
699 objc_start_category_implementation (tree klass, tree categ)
701 objc_implementation_context
702 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE);
704 = continue_class (objc_implementation_context);
708 objc_continue_implementation (void)
711 = continue_class (objc_implementation_context);
715 objc_finish_implementation (void)
718 if (flag_objc_call_cxx_cdtors)
719 objc_generate_cxx_cdtors ();
722 if (objc_implementation_context)
724 finish_class (objc_implementation_context);
725 objc_ivar_chain = NULL_TREE;
726 objc_implementation_context = NULL_TREE;
729 warning (0, "%<@end%> must appear in an @implementation context");
733 objc_set_visibility (int visibility)
735 objc_public_flag = visibility;
739 objc_set_method_type (enum tree_code type)
741 objc_inherit_code = (type == PLUS_EXPR
743 : INSTANCE_METHOD_DECL);
747 objc_build_method_signature (tree rettype, tree selector,
748 tree optparms, bool ellipsis)
750 return build_method_decl (objc_inherit_code, rettype, selector,
755 objc_add_method_declaration (tree decl)
757 if (!objc_interface_context)
758 fatal_error ("method declaration not in @interface context");
760 objc_add_method (objc_interface_context,
762 objc_inherit_code == CLASS_METHOD_DECL);
766 objc_start_method_definition (tree decl)
768 if (!objc_implementation_context)
769 fatal_error ("method definition not in @implementation context");
771 objc_add_method (objc_implementation_context,
773 objc_inherit_code == CLASS_METHOD_DECL);
774 start_method_def (decl);
778 objc_add_instance_variable (tree decl)
780 (void) add_instance_variable (objc_ivar_context,
785 /* Return 1 if IDENT is an ObjC/ObjC++ reserved keyword in the context of
789 objc_is_reserved_word (tree ident)
791 unsigned char code = C_RID_CODE (ident);
793 return (OBJC_IS_AT_KEYWORD (code)
794 || code == RID_CLASS || code == RID_PUBLIC
795 || code == RID_PROTECTED || code == RID_PRIVATE
796 || code == RID_TRY || code == RID_THROW || code == RID_CATCH);
799 /* Return true if TYPE is 'id'. */
802 objc_is_object_id (tree type)
804 return OBJC_TYPE_NAME (type) == objc_object_id;
808 objc_is_class_id (tree type)
810 return OBJC_TYPE_NAME (type) == objc_class_id;
813 /* Construct a C struct with same name as KLASS, a base struct with tag
814 SUPER_NAME (if any), and FIELDS indicated. */
817 objc_build_struct (tree klass, tree fields, tree super_name)
819 tree name = CLASS_NAME (klass);
820 tree s = objc_start_struct (name);
821 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
822 tree t, objc_info = NULL_TREE;
826 /* Prepend a packed variant of the base class into the layout. This
827 is necessary to preserve ObjC ABI compatibility. */
828 tree base = build_decl (input_location,
829 FIELD_DECL, NULL_TREE, super);
830 tree field = TYPE_FIELDS (super);
832 while (field && TREE_CHAIN (field)
833 && TREE_CODE (TREE_CHAIN (field)) == FIELD_DECL)
834 field = TREE_CHAIN (field);
836 /* For ObjC ABI purposes, the "packed" size of a base class is
837 the sum of the offset and the size (in bits) of the last field
840 = (field && TREE_CODE (field) == FIELD_DECL
841 ? size_binop (PLUS_EXPR,
842 size_binop (PLUS_EXPR,
845 convert (bitsizetype,
846 DECL_FIELD_OFFSET (field)),
847 bitsize_int (BITS_PER_UNIT)),
848 DECL_FIELD_BIT_OFFSET (field)),
850 : bitsize_zero_node);
851 DECL_SIZE_UNIT (base)
852 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
853 size_int (BITS_PER_UNIT));
854 DECL_ARTIFICIAL (base) = 1;
855 DECL_ALIGN (base) = 1;
856 DECL_FIELD_CONTEXT (base) = s;
858 DECL_FIELD_IS_BASE (base) = 1;
861 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
862 #endif /* are following the ObjC ABI here. */
863 TREE_CHAIN (base) = fields;
867 /* NB: Calling finish_struct() may cause type TYPE_LANG_SPECIFIC fields
868 in all variants of this RECORD_TYPE to be clobbered, but it is therein
869 that we store protocol conformance info (e.g., 'NSObject <MyProtocol>').
870 Hence, we must squirrel away the ObjC-specific information before calling
871 finish_struct(), and then reinstate it afterwards. */
873 for (t = TYPE_NEXT_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
875 if (!TYPE_HAS_OBJC_INFO (t))
877 INIT_TYPE_OBJC_INFO (t);
878 TYPE_OBJC_INTERFACE (t) = klass;
881 = chainon (objc_info,
882 build_tree_list (NULL_TREE, TYPE_OBJC_INFO (t)));
885 /* Point the struct at its related Objective-C class. */
886 INIT_TYPE_OBJC_INFO (s);
887 TYPE_OBJC_INTERFACE (s) = klass;
889 s = objc_finish_struct (s, fields);
891 for (t = TYPE_NEXT_VARIANT (s); t;
892 t = TYPE_NEXT_VARIANT (t), objc_info = TREE_CHAIN (objc_info))
894 TYPE_OBJC_INFO (t) = TREE_VALUE (objc_info);
895 /* Replace the IDENTIFIER_NODE with an actual @interface. */
896 TYPE_OBJC_INTERFACE (t) = klass;
899 /* Use TYPE_BINFO structures to point at the super class, if any. */
900 objc_xref_basetypes (s, super);
902 /* Mark this struct as a class template. */
903 CLASS_STATIC_TEMPLATE (klass) = s;
908 /* Build a type differing from TYPE only in that TYPE_VOLATILE is set.
909 Unlike tree.c:build_qualified_type(), preserve TYPE_LANG_SPECIFIC in the
912 objc_build_volatilized_type (tree type)
916 /* Check if we have not constructed the desired variant already. */
917 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
919 /* The type qualifiers must (obviously) match up. */
920 if (!TYPE_VOLATILE (t)
921 || (TYPE_READONLY (t) != TYPE_READONLY (type))
922 || (TYPE_RESTRICT (t) != TYPE_RESTRICT (type)))
925 /* For pointer types, the pointees (and hence their TYPE_LANG_SPECIFIC
926 info, if any) must match up. */
927 if (POINTER_TYPE_P (t)
928 && (TREE_TYPE (t) != TREE_TYPE (type)))
931 /* Everything matches up! */
935 /* Ok, we could not re-use any of the pre-existing variants. Create
937 t = build_variant_type_copy (type);
938 TYPE_VOLATILE (t) = 1;
940 /* Set up the canonical type information. */
941 if (TYPE_STRUCTURAL_EQUALITY_P (type))
942 SET_TYPE_STRUCTURAL_EQUALITY (t);
943 else if (TYPE_CANONICAL (type) != type)
944 TYPE_CANONICAL (t) = objc_build_volatilized_type (TYPE_CANONICAL (type));
946 TYPE_CANONICAL (t) = t;
951 /* Mark DECL as being 'volatile' for purposes of Darwin
952 _setjmp()/_longjmp() exception handling. Called from
953 objc_mark_locals_volatile(). */
955 objc_volatilize_decl (tree decl)
957 /* Do not mess with variables that are 'static' or (already)
959 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
960 && (TREE_CODE (decl) == VAR_DECL
961 || TREE_CODE (decl) == PARM_DECL))
963 tree t = TREE_TYPE (decl);
964 struct volatilized_type key;
967 t = objc_build_volatilized_type (t);
969 loc = htab_find_slot (volatilized_htab, &key, INSERT);
973 *loc = ggc_alloc (sizeof (key));
974 ((struct volatilized_type *) *loc)->type = t;
977 TREE_TYPE (decl) = t;
978 TREE_THIS_VOLATILE (decl) = 1;
979 TREE_SIDE_EFFECTS (decl) = 1;
980 DECL_REGISTER (decl) = 0;
982 C_DECL_REGISTER (decl) = 0;
987 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
988 (including its categories and superclasses) or by object type TYP.
989 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
992 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
994 bool class_type = (cls != NULL_TREE);
1000 /* Check protocols adopted by the class and its categories. */
1001 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
1003 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
1007 /* Repeat for superclasses. */
1008 cls = lookup_interface (CLASS_SUPER_NAME (cls));
1011 /* Check for any protocols attached directly to the object type. */
1012 if (TYPE_HAS_OBJC_INFO (typ))
1014 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
1021 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
1022 /* NB: Types 'id' and 'Class' cannot reasonably be described as
1023 "implementing" a given protocol, since they do not have an
1026 warning (0, "class %qs does not implement the %qE protocol",
1027 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
1029 warning (0, "type %qs does not conform to the %qE protocol",
1030 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
1036 /* Check if class RCLS and instance struct type RTYP conform to at least the
1037 same protocols that LCLS and LTYP conform to. */
1040 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
1043 bool have_lproto = false;
1047 /* NB: We do _not_ look at categories defined for LCLS; these may or
1048 may not get loaded in, and therefore it is unreasonable to require
1049 that RCLS/RTYP must implement any of their protocols. */
1050 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
1054 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
1058 /* Repeat for superclasses. */
1059 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
1062 /* Check for any protocols attached directly to the object type. */
1063 if (TYPE_HAS_OBJC_INFO (ltyp))
1065 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
1069 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
1074 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
1075 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
1076 away with simply checking for 'id' or 'Class' (!RCLS), since this
1077 routine will not get called in other cases. */
1078 return have_lproto || (rcls != NULL_TREE);
1081 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
1082 an instance of RTYP to an instance of LTYP or to compare the two
1083 (if ARGNO is equal to -3), per ObjC type system rules. Before
1084 returning 'true', this routine may issue warnings related to, e.g.,
1085 protocol conformance. When returning 'false', the routine must
1086 produce absolutely no warnings; the C or C++ front-end will do so
1087 instead, if needed. If either LTYP or RTYP is not an Objective-C type,
1088 the routine must return 'false'.
1090 The ARGNO parameter is encoded as follows:
1091 >= 1 Parameter number (CALLEE contains function being called);
1095 -3 Comparison (LTYP and RTYP may match in either direction). */
1098 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
1100 tree lcls, rcls, lproto, rproto;
1101 bool pointers_compatible;
1103 /* We must be dealing with pointer types */
1104 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
1109 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
1110 rtyp = TREE_TYPE (rtyp);
1112 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
1114 /* Past this point, we are only interested in ObjC class instances,
1115 or 'id' or 'Class'. */
1116 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
1119 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
1120 && !TYPE_HAS_OBJC_INFO (ltyp))
1123 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
1124 && !TYPE_HAS_OBJC_INFO (rtyp))
1127 /* Past this point, we are committed to returning 'true' to the caller.
1128 However, we can still warn about type and/or protocol mismatches. */
1130 if (TYPE_HAS_OBJC_INFO (ltyp))
1132 lcls = TYPE_OBJC_INTERFACE (ltyp);
1133 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
1136 lcls = lproto = NULL_TREE;
1138 if (TYPE_HAS_OBJC_INFO (rtyp))
1140 rcls = TYPE_OBJC_INTERFACE (rtyp);
1141 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
1144 rcls = rproto = NULL_TREE;
1146 /* If we could not find an @interface declaration, we must have
1147 only seen a @class declaration; for purposes of type comparison,
1148 treat it as a stand-alone (root) class. */
1150 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
1153 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
1156 /* If either type is an unqualified 'id', we're done. */
1157 if ((!lproto && objc_is_object_id (ltyp))
1158 || (!rproto && objc_is_object_id (rtyp)))
1161 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
1163 /* If the underlying types are the same, and at most one of them has
1164 a protocol list, we do not need to issue any diagnostics. */
1165 if (pointers_compatible && (!lproto || !rproto))
1168 /* If exactly one of the types is 'Class', issue a diagnostic; any
1169 exceptions of this rule have already been handled. */
1170 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
1171 pointers_compatible = false;
1172 /* Otherwise, check for inheritance relations. */
1175 if (!pointers_compatible)
1177 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
1179 if (!pointers_compatible)
1180 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
1182 if (!pointers_compatible && argno == -3)
1183 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
1186 /* If the pointers match modulo protocols, check for protocol conformance
1188 if (pointers_compatible)
1190 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
1193 if (!pointers_compatible && argno == -3)
1194 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
1198 if (!pointers_compatible)
1200 /* NB: For the time being, we shall make our warnings look like their
1201 C counterparts. In the future, we may wish to make them more
1206 warning (0, "comparison of distinct Objective-C types lacks a cast");
1210 warning (0, "initialization from distinct Objective-C type");
1214 warning (0, "assignment from distinct Objective-C type");
1218 warning (0, "distinct Objective-C type in return");
1222 warning (0, "passing argument %d of %qE from distinct "
1223 "Objective-C type", argno, callee);
1231 /* Check if LTYP and RTYP have the same type qualifiers. If either type
1232 lives in the volatilized hash table, ignore the 'volatile' bit when
1233 making the comparison. */
1236 objc_type_quals_match (tree ltyp, tree rtyp)
1238 int lquals = TYPE_QUALS (ltyp), rquals = TYPE_QUALS (rtyp);
1239 struct volatilized_type key;
1243 if (htab_find_slot (volatilized_htab, &key, NO_INSERT))
1244 lquals &= ~TYPE_QUAL_VOLATILE;
1248 if (htab_find_slot (volatilized_htab, &key, NO_INSERT))
1249 rquals &= ~TYPE_QUAL_VOLATILE;
1251 return (lquals == rquals);
1255 /* Determine if CHILD is derived from PARENT. The routine assumes that
1256 both parameters are RECORD_TYPEs, and is non-reflexive. */
1259 objc_derived_from_p (tree parent, tree child)
1261 parent = TYPE_MAIN_VARIANT (parent);
1263 for (child = TYPE_MAIN_VARIANT (child);
1264 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
1266 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
1267 (TYPE_BINFO (child),
1270 if (child == parent)
1279 objc_build_component_ref (tree datum, tree component)
1281 /* If COMPONENT is NULL, the caller is referring to the anonymous
1282 base class field. */
1285 tree base = TYPE_FIELDS (TREE_TYPE (datum));
1287 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
1290 /* The 'build_component_ref' routine has been removed from the C++
1291 front-end, but 'finish_class_member_access_expr' seems to be
1292 a worthy substitute. */
1294 return finish_class_member_access_expr (datum, component, false,
1295 tf_warning_or_error);
1297 return build_component_ref (input_location, datum, component);
1301 /* Recursively copy inheritance information rooted at BINFO. To do this,
1302 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
1305 objc_copy_binfo (tree binfo)
1307 tree btype = BINFO_TYPE (binfo);
1308 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
1312 BINFO_TYPE (binfo2) = btype;
1313 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
1314 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
1316 /* Recursively copy base binfos of BINFO. */
1317 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1319 tree base_binfo2 = objc_copy_binfo (base_binfo);
1321 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
1322 BINFO_BASE_APPEND (binfo2, base_binfo2);
1328 /* Record superclass information provided in BASETYPE for ObjC class REF.
1329 This is loosely based on cp/decl.c:xref_basetypes(). */
1332 objc_xref_basetypes (tree ref, tree basetype)
1334 tree binfo = make_tree_binfo (basetype ? 1 : 0);
1336 TYPE_BINFO (ref) = binfo;
1337 BINFO_OFFSET (binfo) = size_zero_node;
1338 BINFO_TYPE (binfo) = ref;
1342 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
1344 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
1345 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, 1);
1346 BINFO_BASE_APPEND (binfo, base_binfo);
1347 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
1352 volatilized_hash (const void *ptr)
1354 const_tree const typ = ((const struct volatilized_type *)ptr)->type;
1356 return htab_hash_pointer(typ);
1360 volatilized_eq (const void *ptr1, const void *ptr2)
1362 const_tree const typ1 = ((const struct volatilized_type *)ptr1)->type;
1363 const_tree const typ2 = ((const struct volatilized_type *)ptr2)->type;
1365 return typ1 == typ2;
1368 /* Called from finish_decl. */
1371 objc_check_decl (tree decl)
1373 tree type = TREE_TYPE (decl);
1375 if (TREE_CODE (type) != RECORD_TYPE)
1377 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
1378 error ("statically allocated instance of Objective-C class %qE",
1382 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where INTERFACE may
1383 either name an Objective-C class, or refer to the special 'id' or 'Class'
1384 types. If INTERFACE is not a valid ObjC type, just return it unchanged. */
1387 objc_get_protocol_qualified_type (tree interface, tree protocols)
1389 /* If INTERFACE is not provided, default to 'id'. */
1390 tree type = (interface ? objc_is_id (interface) : objc_object_type);
1391 bool is_ptr = (type != NULL_TREE);
1395 type = objc_is_class_name (interface);
1398 type = xref_tag (RECORD_TYPE, type);
1405 type = build_variant_type_copy (type);
1407 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
1411 tree orig_pointee_type = TREE_TYPE (type);
1412 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
1414 /* Set up the canonical type information. */
1415 TYPE_CANONICAL (type)
1416 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
1418 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
1419 type = TREE_TYPE (type);
1422 /* Look up protocols and install in lang specific list. */
1423 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
1424 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols (protocols);
1426 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
1427 return the pointer to the new pointee variant. */
1429 type = TYPE_POINTER_TO (type);
1431 TYPE_OBJC_INTERFACE (type)
1432 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
1438 /* Check for circular dependencies in protocols. The arguments are
1439 PROTO, the protocol to check, and LIST, a list of protocol it
1443 check_protocol_recursively (tree proto, tree list)
1447 for (p = list; p; p = TREE_CHAIN (p))
1449 tree pp = TREE_VALUE (p);
1451 if (TREE_CODE (pp) == IDENTIFIER_NODE)
1452 pp = lookup_protocol (pp);
1455 fatal_error ("protocol %qE has circular dependency",
1456 PROTOCOL_NAME (pp));
1458 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
1462 /* Look up PROTOCOLS, and return a list of those that are found.
1463 If none are found, return NULL. */
1466 lookup_and_install_protocols (tree protocols)
1469 tree return_value = NULL_TREE;
1471 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
1473 tree ident = TREE_VALUE (proto);
1474 tree p = lookup_protocol (ident);
1477 return_value = chainon (return_value,
1478 build_tree_list (NULL_TREE, p));
1479 else if (ident != error_mark_node)
1480 error ("cannot find protocol declaration for %qE",
1484 return return_value;
1487 /* Create a declaration for field NAME of a given TYPE. */
1490 create_field_decl (tree type, const char *name)
1492 return build_decl (input_location,
1493 FIELD_DECL, get_identifier (name), type);
1496 /* Create a global, static declaration for variable NAME of a given TYPE. The
1497 finish_var_decl() routine will need to be called on it afterwards. */
1500 start_var_decl (tree type, const char *name)
1502 tree var = build_decl (input_location,
1503 VAR_DECL, get_identifier (name), type);
1505 TREE_STATIC (var) = 1;
1506 DECL_INITIAL (var) = error_mark_node; /* A real initializer is coming... */
1507 DECL_IGNORED_P (var) = 1;
1508 DECL_ARTIFICIAL (var) = 1;
1509 DECL_CONTEXT (var) = NULL_TREE;
1511 DECL_THIS_STATIC (var) = 1; /* squash redeclaration errors */
1517 /* Finish off the variable declaration created by start_var_decl(). */
1520 finish_var_decl (tree var, tree initializer)
1522 finish_decl (var, input_location, initializer, NULL_TREE, NULL_TREE);
1523 /* Ensure that the variable actually gets output. */
1524 mark_decl_referenced (var);
1525 /* Mark the decl to avoid "defined but not used" warning. */
1526 TREE_USED (var) = 1;
1529 /* Find the decl for the constant string class reference. This is only
1530 used for the NeXT runtime. */
1533 setup_string_decl (void)
1538 /* %s in format will provide room for terminating null */
1539 length = strlen (STRING_OBJECT_GLOBAL_FORMAT)
1540 + strlen (constant_string_class_name);
1541 name = XNEWVEC (char, length);
1542 sprintf (name, STRING_OBJECT_GLOBAL_FORMAT,
1543 constant_string_class_name);
1544 constant_string_global_id = get_identifier (name);
1545 string_class_decl = lookup_name (constant_string_global_id);
1547 return string_class_decl;
1550 /* Purpose: "play" parser, creating/installing representations
1551 of the declarations that are required by Objective-C.
1555 type_spec--------->sc_spec
1556 (tree_list) (tree_list)
1559 identifier_node identifier_node */
1562 synth_module_prologue (void)
1565 enum debug_info_type save_write_symbols = write_symbols;
1566 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
1568 /* Suppress outputting debug symbols, because
1569 dbxout_init hasn't been called yet. */
1570 write_symbols = NO_DEBUG;
1571 debug_hooks = &do_nothing_debug_hooks;
1574 push_lang_context (lang_name_c); /* extern "C" */
1577 /* The following are also defined in <objc/objc.h> and friends. */
1579 objc_object_id = get_identifier (TAG_OBJECT);
1580 objc_class_id = get_identifier (TAG_CLASS);
1582 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
1583 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
1585 objc_object_type = build_pointer_type (objc_object_reference);
1586 objc_class_type = build_pointer_type (objc_class_reference);
1588 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
1589 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
1591 /* Declare the 'id' and 'Class' typedefs. */
1593 type = lang_hooks.decls.pushdecl (build_decl (input_location,
1597 TREE_NO_WARNING (type) = 1;
1598 type = lang_hooks.decls.pushdecl (build_decl (input_location,
1602 TREE_NO_WARNING (type) = 1;
1604 /* Forward-declare '@interface Protocol'. */
1606 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
1607 objc_declare_class (tree_cons (NULL_TREE, type, NULL_TREE));
1608 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE,
1611 /* Declare type of selector-objects that represent an operation name. */
1613 if (flag_next_runtime)
1614 /* `struct objc_selector *' */
1616 = build_pointer_type (xref_tag (RECORD_TYPE,
1617 get_identifier (TAG_SELECTOR)));
1619 /* `const struct objc_selector *' */
1621 = build_pointer_type
1622 (build_qualified_type (xref_tag (RECORD_TYPE,
1623 get_identifier (TAG_SELECTOR)),
1626 /* Declare receiver type used for dispatching messages to 'super'. */
1628 /* `struct objc_super *' */
1629 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
1630 get_identifier (TAG_SUPER)));
1632 /* Declare pointers to method and ivar lists. */
1633 objc_method_list_ptr = build_pointer_type
1634 (xref_tag (RECORD_TYPE,
1635 get_identifier (UTAG_METHOD_LIST)));
1636 objc_method_proto_list_ptr
1637 = build_pointer_type (xref_tag (RECORD_TYPE,
1638 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
1639 objc_ivar_list_ptr = build_pointer_type
1640 (xref_tag (RECORD_TYPE,
1641 get_identifier (UTAG_IVAR_LIST)));
1643 /* TREE_NOTHROW is cleared for the message-sending functions,
1644 because the function that gets called can throw in Obj-C++, or
1645 could itself call something that can throw even in Obj-C. */
1647 if (flag_next_runtime)
1649 /* NB: In order to call one of the ..._stret (struct-returning)
1650 functions, the function *MUST* first be cast to a signature that
1651 corresponds to the actual ObjC method being invoked. This is
1652 what is done by the build_objc_method_call() routine below. */
1654 /* id objc_msgSend (id, SEL, ...); */
1655 /* id objc_msgSendNonNil (id, SEL, ...); */
1656 /* id objc_msgSend_stret (id, SEL, ...); */
1657 /* id objc_msgSendNonNil_stret (id, SEL, ...); */
1659 = build_function_type (objc_object_type,
1660 tree_cons (NULL_TREE, objc_object_type,
1661 tree_cons (NULL_TREE, objc_selector_type,
1663 umsg_decl = add_builtin_function (TAG_MSGSEND,
1664 type, 0, NOT_BUILT_IN,
1666 umsg_nonnil_decl = add_builtin_function (TAG_MSGSEND_NONNIL,
1667 type, 0, NOT_BUILT_IN,
1669 umsg_stret_decl = add_builtin_function (TAG_MSGSEND_STRET,
1670 type, 0, NOT_BUILT_IN,
1672 umsg_nonnil_stret_decl = add_builtin_function (TAG_MSGSEND_NONNIL_STRET,
1673 type, 0, NOT_BUILT_IN,
1676 /* These can throw, because the function that gets called can throw
1677 in Obj-C++, or could itself call something that can throw even
1679 TREE_NOTHROW (umsg_decl) = 0;
1680 TREE_NOTHROW (umsg_nonnil_decl) = 0;
1681 TREE_NOTHROW (umsg_stret_decl) = 0;
1682 TREE_NOTHROW (umsg_nonnil_stret_decl) = 0;
1684 /* id objc_msgSend_Fast (id, SEL, ...)
1685 __attribute__ ((hard_coded_address (OFFS_MSGSEND_FAST))); */
1686 #ifdef OFFS_MSGSEND_FAST
1687 umsg_fast_decl = add_builtin_function (TAG_MSGSEND_FAST,
1688 type, 0, NOT_BUILT_IN,
1690 TREE_NOTHROW (umsg_fast_decl) = 0;
1691 DECL_ATTRIBUTES (umsg_fast_decl)
1692 = tree_cons (get_identifier ("hard_coded_address"),
1693 build_int_cst (NULL_TREE, OFFS_MSGSEND_FAST),
1696 /* No direct dispatch available. */
1697 umsg_fast_decl = umsg_decl;
1700 /* id objc_msgSendSuper (struct objc_super *, SEL, ...); */
1701 /* id objc_msgSendSuper_stret (struct objc_super *, SEL, ...); */
1703 = build_function_type (objc_object_type,
1704 tree_cons (NULL_TREE, objc_super_type,
1705 tree_cons (NULL_TREE, objc_selector_type,
1707 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
1708 type, 0, NOT_BUILT_IN,
1710 umsg_super_stret_decl = add_builtin_function (TAG_MSGSENDSUPER_STRET,
1711 type, 0, NOT_BUILT_IN, 0,
1713 TREE_NOTHROW (umsg_super_decl) = 0;
1714 TREE_NOTHROW (umsg_super_stret_decl) = 0;
1718 /* GNU runtime messenger entry points. */
1720 /* typedef id (*IMP)(id, SEL, ...); */
1722 = build_pointer_type
1723 (build_function_type (objc_object_type,
1724 tree_cons (NULL_TREE, objc_object_type,
1725 tree_cons (NULL_TREE, objc_selector_type,
1728 /* IMP objc_msg_lookup (id, SEL); */
1730 = build_function_type (IMP_type,
1731 tree_cons (NULL_TREE, objc_object_type,
1732 tree_cons (NULL_TREE, objc_selector_type,
1733 OBJC_VOID_AT_END)));
1734 umsg_decl = add_builtin_function (TAG_MSGSEND,
1735 type, 0, NOT_BUILT_IN,
1737 TREE_NOTHROW (umsg_decl) = 0;
1739 /* IMP objc_msg_lookup_super (struct objc_super *, SEL); */
1741 = build_function_type (IMP_type,
1742 tree_cons (NULL_TREE, objc_super_type,
1743 tree_cons (NULL_TREE, objc_selector_type,
1744 OBJC_VOID_AT_END)));
1745 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
1746 type, 0, NOT_BUILT_IN,
1748 TREE_NOTHROW (umsg_super_decl) = 0;
1750 /* The following GNU runtime entry point is called to initialize
1753 __objc_exec_class (void *); */
1755 = build_function_type (void_type_node,
1756 tree_cons (NULL_TREE, ptr_type_node,
1758 execclass_decl = add_builtin_function (TAG_EXECCLASS,
1759 type, 0, NOT_BUILT_IN,
1763 /* id objc_getClass (const char *); */
1765 type = build_function_type (objc_object_type,
1766 tree_cons (NULL_TREE,
1767 const_string_type_node,
1771 = add_builtin_function (TAG_GETCLASS, type, 0, NOT_BUILT_IN,
1774 /* id objc_getMetaClass (const char *); */
1776 objc_get_meta_class_decl
1777 = add_builtin_function (TAG_GETMETACLASS, type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
1779 build_class_template ();
1780 build_super_template ();
1781 build_protocol_template ();
1782 build_category_template ();
1783 build_objc_exception_stuff ();
1785 if (flag_next_runtime)
1786 build_next_objc_exception_stuff ();
1788 /* static SEL _OBJC_SELECTOR_TABLE[]; */
1790 if (! flag_next_runtime)
1791 build_selector_table_decl ();
1793 /* Forward declare constant_string_id and constant_string_type. */
1794 if (!constant_string_class_name)
1795 constant_string_class_name = default_constant_string_class_name;
1797 constant_string_id = get_identifier (constant_string_class_name);
1798 objc_declare_class (tree_cons (NULL_TREE, constant_string_id, NULL_TREE));
1800 /* Pre-build the following entities - for speed/convenience. */
1801 self_id = get_identifier ("self");
1802 ucmd_id = get_identifier ("_cmd");
1805 pop_lang_context ();
1808 write_symbols = save_write_symbols;
1809 debug_hooks = save_hooks;
1812 /* Ensure that the ivar list for NSConstantString/NXConstantString
1813 (or whatever was specified via `-fconstant-string-class')
1814 contains fields at least as large as the following three, so that
1815 the runtime can stomp on them with confidence:
1817 struct STRING_OBJECT_CLASS_NAME
1821 unsigned int length;
1825 check_string_class_template (void)
1827 tree field_decl = objc_get_class_ivars (constant_string_id);
1829 #define AT_LEAST_AS_LARGE_AS(F, T) \
1830 (F && TREE_CODE (F) == FIELD_DECL \
1831 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
1832 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
1834 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
1837 field_decl = TREE_CHAIN (field_decl);
1838 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
1841 field_decl = TREE_CHAIN (field_decl);
1842 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
1844 #undef AT_LEAST_AS_LARGE_AS
1847 /* Avoid calling `check_string_class_template ()' more than once. */
1848 static GTY(()) int string_layout_checked;
1850 /* Construct an internal string layout to be used as a template for
1851 creating NSConstantString/NXConstantString instances. */
1854 objc_build_internal_const_str_type (void)
1856 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
1857 tree fields = build_decl (input_location,
1858 FIELD_DECL, NULL_TREE, ptr_type_node);
1859 tree field = build_decl (input_location,
1860 FIELD_DECL, NULL_TREE, ptr_type_node);
1862 TREE_CHAIN (field) = fields; fields = field;
1863 field = build_decl (input_location,
1864 FIELD_DECL, NULL_TREE, unsigned_type_node);
1865 TREE_CHAIN (field) = fields; fields = field;
1866 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
1868 finish_builtin_struct (type, "__builtin_ObjCString",
1874 /* Custom build_string which sets TREE_TYPE! */
1877 my_build_string (int len, const char *str)
1879 return fix_string_type (build_string (len, str));
1882 /* Build a string with contents STR and length LEN and convert it to a
1886 my_build_string_pointer (int len, const char *str)
1888 tree string = my_build_string (len, str);
1889 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
1890 return build1 (ADDR_EXPR, ptrtype, string);
1894 string_hash (const void *ptr)
1896 const_tree const str = ((const struct string_descriptor *)ptr)->literal;
1897 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
1898 int i, len = TREE_STRING_LENGTH (str);
1901 for (i = 0; i < len; i++)
1902 h = ((h * 613) + p[i]);
1908 string_eq (const void *ptr1, const void *ptr2)
1910 const_tree const str1 = ((const struct string_descriptor *)ptr1)->literal;
1911 const_tree const str2 = ((const struct string_descriptor *)ptr2)->literal;
1912 int len1 = TREE_STRING_LENGTH (str1);
1914 return (len1 == TREE_STRING_LENGTH (str2)
1915 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
1919 /* Given a chain of STRING_CST's, build a static instance of
1920 NXConstantString which points at the concatenation of those
1921 strings. We place the string object in the __string_objects
1922 section of the __OBJC segment. The Objective-C runtime will
1923 initialize the isa pointers of the string objects to point at the
1924 NXConstantString class object. */
1927 objc_build_string_object (tree string)
1929 tree initlist, constructor, constant_string_class;
1932 struct string_descriptor *desc, key;
1935 /* Prep the string argument. */
1936 string = fix_string_type (string);
1937 TREE_SET_CODE (string, STRING_CST);
1938 length = TREE_STRING_LENGTH (string) - 1;
1940 /* Check whether the string class being used actually exists and has the
1941 correct ivar layout. */
1942 if (!string_layout_checked)
1944 string_layout_checked = -1;
1945 constant_string_class = lookup_interface (constant_string_id);
1946 internal_const_str_type = objc_build_internal_const_str_type ();
1948 if (!constant_string_class
1949 || !(constant_string_type
1950 = CLASS_STATIC_TEMPLATE (constant_string_class)))
1951 error ("cannot find interface declaration for %qE",
1952 constant_string_id);
1953 /* The NSConstantString/NXConstantString ivar layout is now known. */
1954 else if (!check_string_class_template ())
1955 error ("interface %qE does not have valid constant string layout",
1956 constant_string_id);
1957 /* For the NeXT runtime, we can generate a literal reference
1958 to the string class, don't need to run a constructor. */
1959 else if (flag_next_runtime && !setup_string_decl ())
1960 error ("cannot find reference tag for class %qE",
1961 constant_string_id);
1964 string_layout_checked = 1; /* Success! */
1965 add_class_reference (constant_string_id);
1969 if (string_layout_checked == -1)
1970 return error_mark_node;
1972 /* Perhaps we already constructed a constant string just like this one? */
1973 key.literal = string;
1974 loc = htab_find_slot (string_htab, &key, INSERT);
1975 desc = (struct string_descriptor *) *loc;
1980 *loc = desc = GGC_NEW (struct string_descriptor);
1981 desc->literal = string;
1983 /* GNU: (NXConstantString *) & ((__builtin_ObjCString) { NULL, string, length }) */
1984 /* NeXT: (NSConstantString *) & ((__builtin_ObjCString) { isa, string, length }) */
1985 fields = TYPE_FIELDS (internal_const_str_type);
1987 = build_tree_list (fields,
1989 ? build_unary_op (input_location,
1990 ADDR_EXPR, string_class_decl, 0)
1991 : build_int_cst (NULL_TREE, 0));
1992 fields = TREE_CHAIN (fields);
1993 initlist = tree_cons (fields, build_unary_op (input_location,
1994 ADDR_EXPR, string, 1),
1996 fields = TREE_CHAIN (fields);
1997 initlist = tree_cons (fields, build_int_cst (NULL_TREE, length),
1999 constructor = objc_build_constructor (internal_const_str_type,
2000 nreverse (initlist));
2002 if (!flag_next_runtime)
2004 = objc_add_static_instance (constructor, constant_string_type);
2007 var = build_decl (input_location,
2008 CONST_DECL, NULL, TREE_TYPE (constructor));
2009 DECL_INITIAL (var) = constructor;
2010 TREE_STATIC (var) = 1;
2011 pushdecl_top_level (var);
2014 desc->constructor = constructor;
2017 addr = convert (build_pointer_type (constant_string_type),
2018 build_unary_op (input_location,
2019 ADDR_EXPR, desc->constructor, 1));
2024 /* Declare a static instance of CLASS_DECL initialized by CONSTRUCTOR. */
2026 static GTY(()) int num_static_inst;
2029 objc_add_static_instance (tree constructor, tree class_decl)
2034 /* Find the list of static instances for the CLASS_DECL. Create one if
2036 for (chain = &objc_static_instances;
2037 *chain && TREE_VALUE (*chain) != class_decl;
2038 chain = &TREE_CHAIN (*chain));
2041 *chain = tree_cons (NULL_TREE, class_decl, NULL_TREE);
2042 add_objc_string (OBJC_TYPE_NAME (class_decl), class_names);
2045 sprintf (buf, "_OBJC_INSTANCE_%d", num_static_inst++);
2046 decl = build_decl (input_location,
2047 VAR_DECL, get_identifier (buf), class_decl);
2048 TREE_STATIC (decl) = 1;
2049 DECL_ARTIFICIAL (decl) = 1;
2050 TREE_USED (decl) = 1;
2051 DECL_INITIAL (decl) = constructor;
2053 /* We may be writing something else just now.
2054 Postpone till end of input. */
2055 DECL_DEFER_OUTPUT (decl) = 1;
2056 pushdecl_top_level (decl);
2057 rest_of_decl_compilation (decl, 1, 0);
2059 /* Add the DECL to the head of this CLASS' list. */
2060 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE, decl, TREE_PURPOSE (*chain));
2065 /* Build a static constant CONSTRUCTOR
2066 with type TYPE and elements ELTS. */
2069 objc_build_constructor (tree type, tree elts)
2071 tree constructor = build_constructor_from_list (type, elts);
2073 TREE_CONSTANT (constructor) = 1;
2074 TREE_STATIC (constructor) = 1;
2075 TREE_READONLY (constructor) = 1;
2078 /* Adjust for impedance mismatch. We should figure out how to build
2079 CONSTRUCTORs that consistently please both the C and C++ gods. */
2080 if (!TREE_PURPOSE (elts))
2081 TREE_TYPE (constructor) = init_list_type_node;
2087 /* Take care of defining and initializing _OBJC_SYMBOLS. */
2089 /* Predefine the following data type:
2097 void *defs[cls_def_cnt + cat_def_cnt];
2101 build_objc_symtab_template (void)
2103 tree field_decl, field_decl_chain;
2105 objc_symtab_template = objc_start_struct (get_identifier (UTAG_SYMTAB));
2107 /* long sel_ref_cnt; */
2108 field_decl = create_field_decl (long_integer_type_node, "sel_ref_cnt");
2109 field_decl_chain = field_decl;
2112 field_decl = create_field_decl (build_pointer_type (objc_selector_type),
2114 chainon (field_decl_chain, field_decl);
2116 /* short cls_def_cnt; */
2117 field_decl = create_field_decl (short_integer_type_node, "cls_def_cnt");
2118 chainon (field_decl_chain, field_decl);
2120 /* short cat_def_cnt; */
2121 field_decl = create_field_decl (short_integer_type_node,
2123 chainon (field_decl_chain, field_decl);
2125 if (imp_count || cat_count || !flag_next_runtime)
2127 /* void *defs[imp_count + cat_count (+ 1)]; */
2128 /* NB: The index is one less than the size of the array. */
2129 int index = imp_count + cat_count
2130 + (flag_next_runtime? -1: 0);
2131 field_decl = create_field_decl
2134 build_index_type (build_int_cst (NULL_TREE, index))),
2136 chainon (field_decl_chain, field_decl);
2139 objc_finish_struct (objc_symtab_template, field_decl_chain);
2142 /* Create the initial value for the `defs' field of _objc_symtab.
2143 This is a CONSTRUCTOR. */
2146 init_def_list (tree type)
2148 tree expr, initlist = NULL_TREE;
2149 struct imp_entry *impent;
2152 for (impent = imp_list; impent; impent = impent->next)
2154 if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
2156 expr = build_unary_op (input_location,
2157 ADDR_EXPR, impent->class_decl, 0);
2158 initlist = tree_cons (NULL_TREE, expr, initlist);
2163 for (impent = imp_list; impent; impent = impent->next)
2165 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
2167 expr = build_unary_op (input_location,
2168 ADDR_EXPR, impent->class_decl, 0);
2169 initlist = tree_cons (NULL_TREE, expr, initlist);
2173 if (!flag_next_runtime)
2175 /* statics = { ..., _OBJC_STATIC_INSTANCES, ... } */
2178 if (static_instances_decl)
2179 expr = build_unary_op (input_location,
2180 ADDR_EXPR, static_instances_decl, 0);
2182 expr = build_int_cst (NULL_TREE, 0);
2184 initlist = tree_cons (NULL_TREE, expr, initlist);
2187 return objc_build_constructor (type, nreverse (initlist));
2190 /* Construct the initial value for all of _objc_symtab. */
2193 init_objc_symtab (tree type)
2197 /* sel_ref_cnt = { ..., 5, ... } */
2199 initlist = build_tree_list (NULL_TREE,
2200 build_int_cst (long_integer_type_node, 0));
2202 /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
2204 if (flag_next_runtime || ! sel_ref_chain)
2205 initlist = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, 0), initlist);
2208 = tree_cons (NULL_TREE,
2209 convert (build_pointer_type (objc_selector_type),
2210 build_unary_op (input_location, ADDR_EXPR,
2211 UOBJC_SELECTOR_TABLE_decl, 1)),
2214 /* cls_def_cnt = { ..., 5, ... } */
2216 initlist = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, imp_count), initlist);
2218 /* cat_def_cnt = { ..., 5, ... } */
2220 initlist = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, cat_count), initlist);
2222 /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
2224 if (imp_count || cat_count || !flag_next_runtime)
2227 tree field = TYPE_FIELDS (type);
2228 field = TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (field))));
2230 initlist = tree_cons (NULL_TREE, init_def_list (TREE_TYPE (field)),
2234 return objc_build_constructor (type, nreverse (initlist));
2237 /* Generate forward declarations for metadata such as
2238 'OBJC_CLASS_...'. */
2241 build_metadata_decl (const char *name, tree type)
2245 /* struct TYPE NAME_<name>; */
2246 decl = start_var_decl (type, synth_id_with_class_suffix
2248 objc_implementation_context));
2253 /* Push forward-declarations of all the categories so that
2254 init_def_list can use them in a CONSTRUCTOR. */
2257 forward_declare_categories (void)
2259 struct imp_entry *impent;
2260 tree sav = objc_implementation_context;
2262 for (impent = imp_list; impent; impent = impent->next)
2264 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
2266 /* Set an invisible arg to synth_id_with_class_suffix. */
2267 objc_implementation_context = impent->imp_context;
2268 /* extern struct objc_category _OBJC_CATEGORY_<name>; */
2269 impent->class_decl = build_metadata_decl ("_OBJC_CATEGORY",
2270 objc_category_template);
2273 objc_implementation_context = sav;
2276 /* Create the declaration of _OBJC_SYMBOLS, with type `struct _objc_symtab'
2277 and initialized appropriately. */
2280 generate_objc_symtab_decl (void)
2282 /* forward declare categories */
2284 forward_declare_categories ();
2286 build_objc_symtab_template ();
2287 UOBJC_SYMBOLS_decl = start_var_decl (objc_symtab_template, "_OBJC_SYMBOLS");
2288 finish_var_decl (UOBJC_SYMBOLS_decl,
2289 init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl)));
2293 init_module_descriptor (tree type)
2295 tree initlist, expr;
2297 /* version = { 1, ... } */
2299 expr = build_int_cst (long_integer_type_node, OBJC_VERSION);
2300 initlist = build_tree_list (NULL_TREE, expr);
2302 /* size = { ..., sizeof (struct _objc_module), ... } */
2304 expr = convert (long_integer_type_node,
2305 size_in_bytes (objc_module_template));
2306 initlist = tree_cons (NULL_TREE, expr, initlist);
2308 /* Don't provide any file name for security reasons. */
2309 /* name = { ..., "", ... } */
2311 expr = add_objc_string (get_identifier (""), class_names);
2312 initlist = tree_cons (NULL_TREE, expr, initlist);
2314 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
2316 if (UOBJC_SYMBOLS_decl)
2317 expr = build_unary_op (input_location,
2318 ADDR_EXPR, UOBJC_SYMBOLS_decl, 0);
2320 expr = build_int_cst (NULL_TREE, 0);
2321 initlist = tree_cons (NULL_TREE, expr, initlist);
2323 return objc_build_constructor (type, nreverse (initlist));
2326 /* Write out the data structures to describe Objective C classes defined.
2328 struct _objc_module { ... } _OBJC_MODULE = { ... }; */
2331 build_module_descriptor (void)
2333 tree field_decl, field_decl_chain;
2336 push_lang_context (lang_name_c); /* extern "C" */
2339 objc_module_template = objc_start_struct (get_identifier (UTAG_MODULE));
2342 field_decl = create_field_decl (long_integer_type_node, "version");
2343 field_decl_chain = field_decl;
2346 field_decl = create_field_decl (long_integer_type_node, "size");
2347 chainon (field_decl_chain, field_decl);
2350 field_decl = create_field_decl (string_type_node, "name");
2351 chainon (field_decl_chain, field_decl);
2353 /* struct _objc_symtab *symtab; */
2355 = create_field_decl (build_pointer_type
2356 (xref_tag (RECORD_TYPE,
2357 get_identifier (UTAG_SYMTAB))),
2359 chainon (field_decl_chain, field_decl);
2361 objc_finish_struct (objc_module_template, field_decl_chain);
2363 /* Create an instance of "_objc_module". */
2364 UOBJC_MODULES_decl = start_var_decl (objc_module_template, "_OBJC_MODULES");
2365 finish_var_decl (UOBJC_MODULES_decl,
2366 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl)));
2369 pop_lang_context ();
2373 /* The GNU runtime requires us to provide a static initializer function
2376 static void __objc_gnu_init (void) {
2377 __objc_exec_class (&L_OBJC_MODULES);
2381 build_module_initializer_routine (void)
2386 push_lang_context (lang_name_c); /* extern "C" */
2389 objc_push_parm (build_decl (input_location,
2390 PARM_DECL, NULL_TREE, void_type_node));
2391 objc_start_function (get_identifier (TAG_GNUINIT),
2392 build_function_type (void_type_node,
2394 NULL_TREE, objc_get_parm_info (0));
2396 body = c_begin_compound_stmt (true);
2397 add_stmt (build_function_call
2402 build_unary_op (input_location, ADDR_EXPR,
2403 UOBJC_MODULES_decl, 0))));
2404 add_stmt (c_end_compound_stmt (input_location, body, true));
2406 TREE_PUBLIC (current_function_decl) = 0;
2409 /* For Objective-C++, we will need to call __objc_gnu_init
2410 from objc_generate_static_init_call() below. */
2411 DECL_STATIC_CONSTRUCTOR (current_function_decl) = 1;
2414 GNU_INIT_decl = current_function_decl;
2418 pop_lang_context ();
2423 /* Return 1 if the __objc_gnu_init function has been synthesized and needs
2424 to be called by the module initializer routine. */
2427 objc_static_init_needed_p (void)
2429 return (GNU_INIT_decl != NULL_TREE);
2432 /* Generate a call to the __objc_gnu_init initializer function. */
2435 objc_generate_static_init_call (tree ctors ATTRIBUTE_UNUSED)
2437 add_stmt (build_stmt (input_location, EXPR_STMT,
2438 build_function_call (input_location,
2439 GNU_INIT_decl, NULL_TREE)));
2443 #endif /* OBJCPLUS */
2445 /* Return the DECL of the string IDENT in the SECTION. */
2448 get_objc_string_decl (tree ident, enum string_section section)
2452 if (section == class_names)
2453 chain = class_names_chain;
2454 else if (section == meth_var_names)
2455 chain = meth_var_names_chain;
2456 else if (section == meth_var_types)
2457 chain = meth_var_types_chain;
2461 for (; chain != 0; chain = TREE_CHAIN (chain))
2462 if (TREE_VALUE (chain) == ident)
2463 return (TREE_PURPOSE (chain));
2469 /* Output references to all statically allocated objects. Return the DECL
2470 for the array built. */
2473 generate_static_references (void)
2475 tree decls = NULL_TREE, expr = NULL_TREE;
2476 tree class_name, klass, decl, initlist;
2477 tree cl_chain, in_chain, type
2478 = build_array_type (build_pointer_type (void_type_node), NULL_TREE);
2479 int num_inst, num_class;
2482 if (flag_next_runtime)
2485 for (cl_chain = objc_static_instances, num_class = 0;
2486 cl_chain; cl_chain = TREE_CHAIN (cl_chain), num_class++)
2488 for (num_inst = 0, in_chain = TREE_PURPOSE (cl_chain);
2489 in_chain; num_inst++, in_chain = TREE_CHAIN (in_chain));
2491 sprintf (buf, "_OBJC_STATIC_INSTANCES_%d", num_class);
2492 decl = start_var_decl (type, buf);
2494 /* Output {class_name, ...}. */
2495 klass = TREE_VALUE (cl_chain);
2496 class_name = get_objc_string_decl (OBJC_TYPE_NAME (klass), class_names);
2497 initlist = build_tree_list (NULL_TREE,
2498 build_unary_op (input_location,
2499 ADDR_EXPR, class_name, 1));
2501 /* Output {..., instance, ...}. */
2502 for (in_chain = TREE_PURPOSE (cl_chain);
2503 in_chain; in_chain = TREE_CHAIN (in_chain))
2505 expr = build_unary_op (input_location,
2506 ADDR_EXPR, TREE_VALUE (in_chain), 1);
2507 initlist = tree_cons (NULL_TREE, expr, initlist);
2510 /* Output {..., NULL}. */
2511 initlist = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, 0), initlist);
2513 expr = objc_build_constructor (TREE_TYPE (decl), nreverse (initlist));
2514 finish_var_decl (decl, expr);
2516 = tree_cons (NULL_TREE, build_unary_op (input_location,
2517 ADDR_EXPR, decl, 1), decls);
2520 decls = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, 0), decls);
2521 expr = objc_build_constructor (type, nreverse (decls));
2522 static_instances_decl = start_var_decl (type, "_OBJC_STATIC_INSTANCES");
2523 finish_var_decl (static_instances_decl, expr);
2526 static GTY(()) int selector_reference_idx;
2529 build_selector_reference_decl (void)
2534 sprintf (buf, "_OBJC_SELECTOR_REFERENCES_%d", selector_reference_idx++);
2535 decl = start_var_decl (objc_selector_type, buf);
2541 build_selector_table_decl (void)
2545 if (flag_typed_selectors)
2547 build_selector_template ();
2548 temp = build_array_type (objc_selector_template, NULL_TREE);
2551 temp = build_array_type (objc_selector_type, NULL_TREE);
2553 UOBJC_SELECTOR_TABLE_decl = start_var_decl (temp, "_OBJC_SELECTOR_TABLE");
2556 /* Just a handy wrapper for add_objc_string. */
2559 build_selector (tree ident)
2561 return convert (objc_selector_type,
2562 add_objc_string (ident, meth_var_names));
2566 build_selector_translation_table (void)
2568 tree chain, initlist = NULL_TREE;
2570 tree decl = NULL_TREE;
2572 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
2576 if (warn_selector && objc_implementation_context)
2580 for (method_chain = meth_var_names_chain;
2582 method_chain = TREE_CHAIN (method_chain))
2584 if (TREE_VALUE (method_chain) == TREE_VALUE (chain))
2593 if (flag_next_runtime && TREE_PURPOSE (chain))
2594 loc = DECL_SOURCE_LOCATION (TREE_PURPOSE (chain));
2596 loc = input_location;
2597 warning_at (loc, 0, "creating selector for nonexistent method %qE",
2598 TREE_VALUE (chain));
2602 expr = build_selector (TREE_VALUE (chain));
2603 /* add one for the '\0' character */
2604 offset += IDENTIFIER_LENGTH (TREE_VALUE (chain)) + 1;
2606 if (flag_next_runtime)
2608 decl = TREE_PURPOSE (chain);
2609 finish_var_decl (decl, expr);
2613 if (flag_typed_selectors)
2615 tree eltlist = NULL_TREE;
2616 tree encoding = get_proto_encoding (TREE_PURPOSE (chain));
2617 eltlist = tree_cons (NULL_TREE, expr, NULL_TREE);
2618 eltlist = tree_cons (NULL_TREE, encoding, eltlist);
2619 expr = objc_build_constructor (objc_selector_template,
2620 nreverse (eltlist));
2623 initlist = tree_cons (NULL_TREE, expr, initlist);
2627 if (! flag_next_runtime)
2629 /* Cause the selector table (previously forward-declared)
2630 to be actually output. */
2631 initlist = tree_cons (NULL_TREE,
2632 flag_typed_selectors
2633 ? objc_build_constructor
2634 (objc_selector_template,
2635 tree_cons (NULL_TREE,
2636 build_int_cst (NULL_TREE, 0),
2637 tree_cons (NULL_TREE,
2638 build_int_cst (NULL_TREE, 0),
2640 : build_int_cst (NULL_TREE, 0), initlist);
2641 initlist = objc_build_constructor (TREE_TYPE (UOBJC_SELECTOR_TABLE_decl),
2642 nreverse (initlist));
2643 finish_var_decl (UOBJC_SELECTOR_TABLE_decl, initlist);
2648 get_proto_encoding (tree proto)
2653 if (! METHOD_ENCODING (proto))
2655 encoding = encode_method_prototype (proto);
2656 METHOD_ENCODING (proto) = encoding;
2659 encoding = METHOD_ENCODING (proto);
2661 return add_objc_string (encoding, meth_var_types);
2664 return build_int_cst (NULL_TREE, 0);
2667 /* sel_ref_chain is a list whose "value" fields will be instances of
2668 identifier_node that represent the selector. LOC is the location of
2672 build_typed_selector_reference (location_t loc, tree ident, tree prototype)
2674 tree *chain = &sel_ref_chain;
2680 if (TREE_PURPOSE (*chain) == prototype && TREE_VALUE (*chain) == ident)
2681 goto return_at_index;
2684 chain = &TREE_CHAIN (*chain);
2687 *chain = tree_cons (prototype, ident, NULL_TREE);
2690 expr = build_unary_op (loc, ADDR_EXPR,
2691 build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
2692 build_int_cst (NULL_TREE, index)),
2694 return convert (objc_selector_type, expr);
2698 build_selector_reference (location_t loc, tree ident)
2700 tree *chain = &sel_ref_chain;
2706 if (TREE_VALUE (*chain) == ident)
2707 return (flag_next_runtime
2708 ? TREE_PURPOSE (*chain)
2709 : build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
2710 build_int_cst (NULL_TREE, index)));
2713 chain = &TREE_CHAIN (*chain);
2716 expr = (flag_next_runtime ? build_selector_reference_decl (): NULL_TREE);
2718 *chain = tree_cons (expr, ident, NULL_TREE);
2720 return (flag_next_runtime
2722 : build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
2723 build_int_cst (NULL_TREE, index)));
2726 static GTY(()) int class_reference_idx;
2729 build_class_reference_decl (void)
2734 sprintf (buf, "_OBJC_CLASS_REFERENCES_%d", class_reference_idx++);
2735 decl = start_var_decl (objc_class_type, buf);
2740 /* Create a class reference, but don't create a variable to reference
2744 add_class_reference (tree ident)
2748 if ((chain = cls_ref_chain))
2753 if (ident == TREE_VALUE (chain))
2757 chain = TREE_CHAIN (chain);
2761 /* Append to the end of the list */
2762 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
2765 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
2768 /* Get a class reference, creating it if necessary. Also create the
2769 reference variable. */
2772 objc_get_class_reference (tree ident)
2774 tree orig_ident = (DECL_P (ident)
2777 ? OBJC_TYPE_NAME (ident)
2779 bool local_scope = false;
2782 if (processing_template_decl)
2783 /* Must wait until template instantiation time. */
2784 return build_min_nt (CLASS_REFERENCE_EXPR, ident);
2787 if (TREE_CODE (ident) == TYPE_DECL)
2788 ident = (DECL_ORIGINAL_TYPE (ident)
2789 ? DECL_ORIGINAL_TYPE (ident)
2790 : TREE_TYPE (ident));
2793 if (TYPE_P (ident) && TYPE_CONTEXT (ident)
2794 && TYPE_CONTEXT (ident) != global_namespace)
2798 if (local_scope || !(ident = objc_is_class_name (ident)))
2800 error ("%qE is not an Objective-C class name or alias",
2802 return error_mark_node;
2805 if (flag_next_runtime && !flag_zero_link)
2810 for (chain = &cls_ref_chain; *chain; chain = &TREE_CHAIN (*chain))
2811 if (TREE_VALUE (*chain) == ident)
2813 if (! TREE_PURPOSE (*chain))
2814 TREE_PURPOSE (*chain) = build_class_reference_decl ();
2816 return TREE_PURPOSE (*chain);
2819 decl = build_class_reference_decl ();
2820 *chain = tree_cons (decl, ident, NULL_TREE);
2827 add_class_reference (ident);
2829 params = build_tree_list (NULL_TREE,
2830 my_build_string_pointer
2831 (IDENTIFIER_LENGTH (ident) + 1,
2832 IDENTIFIER_POINTER (ident)));
2834 assemble_external (objc_get_class_decl);
2835 return build_function_call (input_location, objc_get_class_decl, params);
2839 /* For each string section we have a chain which maps identifier nodes
2840 to decls for the strings. */
2843 add_objc_string (tree ident, enum string_section section)
2845 tree *chain, decl, type, string_expr;
2847 if (section == class_names)
2848 chain = &class_names_chain;
2849 else if (section == meth_var_names)
2850 chain = &meth_var_names_chain;
2851 else if (section == meth_var_types)
2852 chain = &meth_var_types_chain;
2858 if (TREE_VALUE (*chain) == ident)
2859 return convert (string_type_node,
2860 build_unary_op (input_location,
2861 ADDR_EXPR, TREE_PURPOSE (*chain), 1));
2863 chain = &TREE_CHAIN (*chain);
2866 decl = build_objc_string_decl (section);
2868 type = build_array_type
2871 (build_int_cst (NULL_TREE,
2872 IDENTIFIER_LENGTH (ident))));
2873 decl = start_var_decl (type, IDENTIFIER_POINTER (DECL_NAME (decl)));
2874 string_expr = my_build_string (IDENTIFIER_LENGTH (ident) + 1,
2875 IDENTIFIER_POINTER (ident));
2876 finish_var_decl (decl, string_expr);
2878 *chain = tree_cons (decl, ident, NULL_TREE);
2880 return convert (string_type_node, build_unary_op (input_location,
2881 ADDR_EXPR, decl, 1));
2884 static GTY(()) int class_names_idx;
2885 static GTY(()) int meth_var_names_idx;
2886 static GTY(()) int meth_var_types_idx;
2889 build_objc_string_decl (enum string_section section)
2894 if (section == class_names)
2895 sprintf (buf, "_OBJC_CLASS_NAME_%d", class_names_idx++);
2896 else if (section == meth_var_names)
2897 sprintf (buf, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx++);
2898 else if (section == meth_var_types)
2899 sprintf (buf, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx++);
2901 ident = get_identifier (buf);
2903 decl = build_decl (input_location,
2904 VAR_DECL, ident, build_array_type (char_type_node, 0));
2905 DECL_EXTERNAL (decl) = 1;
2906 TREE_PUBLIC (decl) = 0;
2907 TREE_USED (decl) = 1;
2908 TREE_CONSTANT (decl) = 1;
2909 DECL_CONTEXT (decl) = 0;
2910 DECL_ARTIFICIAL (decl) = 1;
2912 DECL_THIS_STATIC (decl) = 1; /* squash redeclaration errors */
2915 make_decl_rtl (decl);
2916 pushdecl_top_level (decl);
2923 objc_declare_alias (tree alias_ident, tree class_ident)
2925 tree underlying_class;
2928 if (current_namespace != global_namespace) {
2929 error ("Objective-C declarations may only appear in global scope");
2931 #endif /* OBJCPLUS */
2933 if (!(underlying_class = objc_is_class_name (class_ident)))
2934 warning (0, "cannot find class %qE", class_ident);
2935 else if (objc_is_class_name (alias_ident))
2936 warning (0, "class %qE already exists", alias_ident);
2939 /* Implement @compatibility_alias as a typedef. */
2941 push_lang_context (lang_name_c); /* extern "C" */
2943 lang_hooks.decls.pushdecl (build_decl
2947 xref_tag (RECORD_TYPE, underlying_class)));
2949 pop_lang_context ();
2951 alias_chain = tree_cons (underlying_class, alias_ident, alias_chain);
2956 objc_declare_class (tree ident_list)
2960 if (current_namespace != global_namespace) {
2961 error ("Objective-C declarations may only appear in global scope");
2963 #endif /* OBJCPLUS */
2965 for (list = ident_list; list; list = TREE_CHAIN (list))
2967 tree ident = TREE_VALUE (list);
2969 if (! objc_is_class_name (ident))
2971 tree record = lookup_name (ident), type = record;
2975 if (TREE_CODE (record) == TYPE_DECL)
2976 type = DECL_ORIGINAL_TYPE (record);
2978 if (!TYPE_HAS_OBJC_INFO (type)
2979 || !TYPE_OBJC_INTERFACE (type))
2981 error ("%qE redeclared as different kind of symbol",
2983 error ("previous declaration of %q+D",
2988 record = xref_tag (RECORD_TYPE, ident);
2989 INIT_TYPE_OBJC_INFO (record);
2990 TYPE_OBJC_INTERFACE (record) = ident;
2991 class_chain = tree_cons (NULL_TREE, ident, class_chain);
2997 objc_is_class_name (tree ident)
3001 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE
3002 && identifier_global_value (ident))
3003 ident = identifier_global_value (ident);
3004 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
3005 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
3007 if (ident && TREE_CODE (ident) == RECORD_TYPE)
3008 ident = OBJC_TYPE_NAME (ident);
3010 if (ident && TREE_CODE (ident) == TYPE_DECL)
3011 ident = DECL_NAME (ident);
3013 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
3016 if (lookup_interface (ident))
3019 for (chain = class_chain; chain; chain = TREE_CHAIN (chain))
3021 if (ident == TREE_VALUE (chain))
3025 for (chain = alias_chain; chain; chain = TREE_CHAIN (chain))
3027 if (ident == TREE_VALUE (chain))
3028 return TREE_PURPOSE (chain);
3034 /* Check whether TYPE is either 'id' or 'Class'. */
3037 objc_is_id (tree type)
3039 if (type && TREE_CODE (type) == IDENTIFIER_NODE
3040 && identifier_global_value (type))
3041 type = identifier_global_value (type);
3043 if (type && TREE_CODE (type) == TYPE_DECL)
3044 type = TREE_TYPE (type);
3046 /* NB: This function may be called before the ObjC front-end has
3047 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3048 return (objc_object_type && type
3049 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
3054 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3055 class instance. This is needed by other parts of the compiler to
3056 handle ObjC types gracefully. */
3059 objc_is_object_ptr (tree type)
3063 type = TYPE_MAIN_VARIANT (type);
3064 if (!POINTER_TYPE_P (type))
3067 ret = objc_is_id (type);
3069 ret = objc_is_class_name (TREE_TYPE (type));
3075 objc_is_gcable_type (tree type, int or_strong_p)
3081 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
3083 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
3085 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
3087 type = TREE_TYPE (type);
3088 if (TREE_CODE (type) != RECORD_TYPE)
3090 name = TYPE_NAME (type);
3091 return (objc_is_class_name (name) != NULL_TREE);
3095 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
3097 if (expr == oldexpr)
3100 switch (TREE_CODE (expr))
3103 return objc_build_component_ref
3104 (objc_substitute_decl (TREE_OPERAND (expr, 0),
3107 DECL_NAME (TREE_OPERAND (expr, 1)));
3109 return build_array_ref (input_location,
3110 objc_substitute_decl (TREE_OPERAND (expr, 0),
3113 TREE_OPERAND (expr, 1));
3115 return build_indirect_ref (input_location,
3116 objc_substitute_decl (TREE_OPERAND (expr, 0),
3118 newexpr), RO_ARROW);
3125 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
3128 /* The LHS parameter contains the expression 'outervar->memberspec';
3129 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3130 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3133 = objc_substitute_decl
3134 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
3136 = (flag_objc_direct_dispatch
3137 ? objc_assign_ivar_fast_decl
3138 : objc_assign_ivar_decl);
3140 offs = convert (integer_type_node, build_unary_op (input_location,
3141 ADDR_EXPR, offs, 0));
3143 func_params = tree_cons (NULL_TREE,
3144 convert (objc_object_type, rhs),
3145 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
3146 tree_cons (NULL_TREE, offs,
3149 assemble_external (func);
3150 return build_function_call (input_location, func, func_params);
3154 objc_build_global_assignment (tree lhs, tree rhs)
3156 tree func_params = tree_cons (NULL_TREE,
3157 convert (objc_object_type, rhs),
3158 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3159 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3162 assemble_external (objc_assign_global_decl);
3163 return build_function_call (input_location,
3164 objc_assign_global_decl, func_params);
3168 objc_build_strong_cast_assignment (tree lhs, tree rhs)
3170 tree func_params = tree_cons (NULL_TREE,
3171 convert (objc_object_type, rhs),
3172 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3173 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3176 assemble_external (objc_assign_strong_cast_decl);
3177 return build_function_call (input_location,
3178 objc_assign_strong_cast_decl, func_params);
3182 objc_is_gcable_p (tree expr)
3184 return (TREE_CODE (expr) == COMPONENT_REF
3185 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
3186 : TREE_CODE (expr) == ARRAY_REF
3187 ? (objc_is_gcable_p (TREE_TYPE (expr))
3188 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
3189 : TREE_CODE (expr) == ARRAY_TYPE
3190 ? objc_is_gcable_p (TREE_TYPE (expr))
3192 ? objc_is_gcable_type (expr, 1)
3193 : (objc_is_gcable_p (TREE_TYPE (expr))
3195 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
3199 objc_is_ivar_reference_p (tree expr)
3201 return (TREE_CODE (expr) == ARRAY_REF
3202 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
3203 : TREE_CODE (expr) == COMPONENT_REF
3204 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
3209 objc_is_global_reference_p (tree expr)
3211 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
3212 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
3214 ? (!DECL_CONTEXT (expr) || TREE_STATIC (expr))
3219 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
3221 tree result = NULL_TREE, outer;
3222 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
3224 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3225 will have been transformed to the form '*(type *)&expr'. */
3226 if (TREE_CODE (lhs) == INDIRECT_REF)
3228 outer = TREE_OPERAND (lhs, 0);
3230 while (!strong_cast_p
3231 && (CONVERT_EXPR_P (outer)
3232 || TREE_CODE (outer) == NON_LVALUE_EXPR))
3234 tree lhstype = TREE_TYPE (outer);
3236 /* Descend down the cast chain, and record the first objc_gc
3238 if (POINTER_TYPE_P (lhstype))
3241 = lookup_attribute ("objc_gc",
3242 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
3248 outer = TREE_OPERAND (outer, 0);
3252 /* If we have a __strong cast, it trumps all else. */
3255 if (modifycode != NOP_EXPR)
3256 goto invalid_pointer_arithmetic;
3258 if (warn_assign_intercept)
3259 warning (0, "strong-cast assignment has been intercepted");
3261 result = objc_build_strong_cast_assignment (lhs, rhs);
3266 /* the lhs must be of a suitable type, regardless of its underlying
3268 if (!objc_is_gcable_p (lhs))
3274 && (TREE_CODE (outer) == COMPONENT_REF
3275 || TREE_CODE (outer) == ARRAY_REF))
3276 outer = TREE_OPERAND (outer, 0);
3278 if (TREE_CODE (outer) == INDIRECT_REF)
3280 outer = TREE_OPERAND (outer, 0);
3284 outer_gc_p = objc_is_gcable_p (outer);
3286 /* Handle ivar assignments. */
3287 if (objc_is_ivar_reference_p (lhs))
3289 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3290 doesn't cut it here), the best we can do here is suggest a cast. */
3291 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
3293 /* We may still be able to use the global write barrier... */
3294 if (!indirect_p && objc_is_global_reference_p (outer))
3295 goto global_reference;
3298 if (modifycode == NOP_EXPR)
3300 if (warn_assign_intercept)
3301 warning (0, "strong-cast may possibly be needed");
3307 if (modifycode != NOP_EXPR)
3308 goto invalid_pointer_arithmetic;
3310 if (warn_assign_intercept)
3311 warning (0, "instance variable assignment has been intercepted");
3313 result = objc_build_ivar_assignment (outer, lhs, rhs);
3318 /* Likewise, intercept assignment to global/static variables if their type is
3320 if (objc_is_global_reference_p (outer))
3326 if (modifycode != NOP_EXPR)
3328 invalid_pointer_arithmetic:
3330 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3335 if (warn_assign_intercept)
3336 warning (0, "global/static variable assignment has been intercepted");
3338 result = objc_build_global_assignment (lhs, rhs);
3341 /* In all other cases, fall back to the normal mechanism. */
3346 struct GTY(()) interface_tuple {
3351 static GTY ((param_is (struct interface_tuple))) htab_t interface_htab;
3354 hash_interface (const void *p)
3356 const struct interface_tuple *d = (const struct interface_tuple *) p;
3357 return IDENTIFIER_HASH_VALUE (d->id);
3361 eq_interface (const void *p1, const void *p2)
3363 const struct interface_tuple *d = (const struct interface_tuple *) p1;
3368 lookup_interface (tree ident)
3371 if (ident && TREE_CODE (ident) == TYPE_DECL)
3372 ident = DECL_NAME (ident);
3375 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
3379 struct interface_tuple **slot;
3384 slot = (struct interface_tuple **)
3385 htab_find_slot_with_hash (interface_htab, ident,
3386 IDENTIFIER_HASH_VALUE (ident),
3389 i = (*slot)->class_name;
3395 /* Implement @defs (<classname>) within struct bodies. */
3398 objc_get_class_ivars (tree class_name)
3400 tree interface = lookup_interface (class_name);
3403 return get_class_ivars (interface, true);
3405 error ("cannot find interface declaration for %qE",
3408 return error_mark_node;
3411 /* Used by: build_private_template, continue_class,
3412 and for @defs constructs. */
3415 get_class_ivars (tree interface, bool inherited)
3417 tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface));
3419 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
3420 by the current class (i.e., they do not include super-class ivars).
3421 However, the CLASS_IVARS list will be side-effected by a call to
3422 finish_struct(), which will fill in field offsets. */
3423 if (!CLASS_IVARS (interface))
3424 CLASS_IVARS (interface) = ivar_chain;
3429 while (CLASS_SUPER_NAME (interface))
3431 /* Prepend super-class ivars. */
3432 interface = lookup_interface (CLASS_SUPER_NAME (interface));
3433 ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)),
3441 objc_create_temporary_var (tree type)
3445 decl = build_decl (input_location,
3446 VAR_DECL, NULL_TREE, type);
3447 TREE_USED (decl) = 1;
3448 DECL_ARTIFICIAL (decl) = 1;
3449 DECL_IGNORED_P (decl) = 1;
3450 DECL_CONTEXT (decl) = current_function_decl;
3455 /* Exception handling constructs. We begin by having the parser do most
3456 of the work and passing us blocks. What we do next depends on whether
3457 we're doing "native" exception handling or legacy Darwin setjmp exceptions.
3458 We abstract all of this in a handful of appropriately named routines. */
3460 /* Stack of open try blocks. */
3462 struct objc_try_context
3464 struct objc_try_context *outer;
3466 /* Statements (or statement lists) as processed by the parser. */
3470 /* Some file position locations. */
3471 location_t try_locus;
3472 location_t end_try_locus;
3473 location_t end_catch_locus;
3474 location_t finally_locus;
3475 location_t end_finally_locus;
3477 /* A STATEMENT_LIST of CATCH_EXPRs, appropriate for sticking into op1
3478 of a TRY_CATCH_EXPR. Even when doing Darwin setjmp. */
3481 /* The CATCH_EXPR of an open @catch clause. */
3484 /* The VAR_DECL holding the Darwin equivalent of __builtin_eh_pointer. */
3490 static struct objc_try_context *cur_try_context;
3492 static GTY(()) tree objc_eh_personality_decl;
3494 /* This hook, called via lang_eh_runtime_type, generates a runtime object
3495 that represents TYPE. For Objective-C, this is just the class name. */
3496 /* ??? Isn't there a class object or some such? Is it easy to get? */
3500 objc_eh_runtime_type (tree type)
3502 return add_objc_string (OBJC_TYPE_NAME (TREE_TYPE (type)), class_names);
3506 objc_eh_personality (void)
3508 if (!flag_objc_sjlj_exceptions
3509 && !objc_eh_personality_decl)
3510 objc_eh_personality_decl
3511 = build_personality_function (USING_SJLJ_EXCEPTIONS
3512 ? "__gnu_objc_personality_sj0"
3513 : "__gnu_objc_personality_v0");
3515 return objc_eh_personality_decl;
3519 /* Build __builtin_eh_pointer, or the moral equivalent. In the case
3520 of Darwin, we'll arrange for it to be initialized (and associated
3521 with a binding) later. */
3524 objc_build_exc_ptr (void)
3526 if (flag_objc_sjlj_exceptions)
3528 tree var = cur_try_context->caught_decl;
3531 var = objc_create_temporary_var (objc_object_type);
3532 cur_try_context->caught_decl = var;
3539 t = built_in_decls[BUILT_IN_EH_POINTER];
3540 t = build_call_expr (t, 1, integer_zero_node);
3541 return fold_convert (objc_object_type, t);
3545 /* Build "objc_exception_try_exit(&_stack)". */
3548 next_sjlj_build_try_exit (void)
3551 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
3552 t = tree_cons (NULL, t, NULL);
3553 t = build_function_call (input_location,
3554 objc_exception_try_exit_decl, t);
3559 objc_exception_try_enter (&_stack);
3560 if (_setjmp(&_stack.buf))
3564 Return the COND_EXPR. Note that the THEN and ELSE fields are left
3565 empty, ready for the caller to fill them in. */
3568 next_sjlj_build_enter_and_setjmp (void)
3570 tree t, enter, sj, cond;
3572 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
3573 t = tree_cons (NULL, t, NULL);
3574 enter = build_function_call (input_location,
3575 objc_exception_try_enter_decl, t);
3577 t = objc_build_component_ref (cur_try_context->stack_decl,
3578 get_identifier ("buf"));
3579 t = build_fold_addr_expr_loc (input_location, t);
3581 /* Convert _setjmp argument to type that is expected. */
3582 if (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl)))
3583 t = convert (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl))), t);
3585 t = convert (ptr_type_node, t);
3587 t = convert (ptr_type_node, t);
3589 t = tree_cons (NULL, t, NULL);
3590 sj = build_function_call (input_location,
3591 objc_setjmp_decl, t);
3593 cond = build2 (COMPOUND_EXPR, TREE_TYPE (sj), enter, sj);
3594 cond = c_common_truthvalue_conversion (input_location, cond);
3596 return build3 (COND_EXPR, void_type_node, cond, NULL, NULL);
3601 DECL = objc_exception_extract(&_stack); */
3604 next_sjlj_build_exc_extract (tree decl)
3608 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
3609 t = tree_cons (NULL, t, NULL);
3610 t = build_function_call (input_location,
3611 objc_exception_extract_decl, t);
3612 t = convert (TREE_TYPE (decl), t);
3613 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
3619 if (objc_exception_match(obj_get_class(TYPE), _caught)
3626 objc_exception_try_exit(&_stack);
3628 from the sequence of CATCH_EXPRs in the current try context. */
3631 next_sjlj_build_catch_list (void)
3633 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
3635 tree *last = &catch_seq;
3636 bool saw_id = false;
3638 for (; !tsi_end_p (i); tsi_next (&i))
3640 tree stmt = tsi_stmt (i);
3641 tree type = CATCH_TYPES (stmt);
3642 tree body = CATCH_BODY (stmt);
3654 if (type == error_mark_node)
3655 cond = error_mark_node;
3658 args = tree_cons (NULL, cur_try_context->caught_decl, NULL);
3659 t = objc_get_class_reference (OBJC_TYPE_NAME (TREE_TYPE (type)));
3660 args = tree_cons (NULL, t, args);
3661 t = build_function_call (input_location,
3662 objc_exception_match_decl, args);
3663 cond = c_common_truthvalue_conversion (input_location, t);
3665 t = build3 (COND_EXPR, void_type_node, cond, body, NULL);
3666 SET_EXPR_LOCATION (t, EXPR_LOCATION (stmt));
3669 last = &COND_EXPR_ELSE (t);
3675 t = build2 (MODIFY_EXPR, void_type_node, cur_try_context->rethrow_decl,
3676 cur_try_context->caught_decl);
3677 SET_EXPR_LOCATION (t, cur_try_context->end_catch_locus);
3678 append_to_statement_list (t, last);
3680 t = next_sjlj_build_try_exit ();
3681 SET_EXPR_LOCATION (t, cur_try_context->end_catch_locus);
3682 append_to_statement_list (t, last);
3688 /* Build a complete @try-@catch-@finally block for legacy Darwin setjmp
3689 exception handling. We aim to build:
3692 struct _objc_exception_data _stack;
3696 objc_exception_try_enter (&_stack);
3697 if (_setjmp(&_stack.buf))
3699 id _caught = objc_exception_extract(&_stack);
3700 objc_exception_try_enter (&_stack);
3701 if (_setjmp(&_stack.buf))
3702 _rethrow = objc_exception_extract(&_stack);
3712 objc_exception_try_exit(&_stack);
3715 objc_exception_throw(_rethrow);
3719 If CATCH-LIST is empty, we can omit all of the block containing
3720 "_caught" except for the setting of _rethrow. Note the use of
3721 a real TRY_FINALLY_EXPR here, which is not involved in EH per-se,
3722 but handles goto and other exits from the block. */
3725 next_sjlj_build_try_catch_finally (void)
3727 tree rethrow_decl, stack_decl, t;
3728 tree catch_seq, try_fin, bind;
3730 /* Create the declarations involved. */
3731 t = xref_tag (RECORD_TYPE, get_identifier (UTAG_EXCDATA));
3732 stack_decl = objc_create_temporary_var (t);
3733 cur_try_context->stack_decl = stack_decl;
3735 rethrow_decl = objc_create_temporary_var (objc_object_type);
3736 cur_try_context->rethrow_decl = rethrow_decl;
3737 TREE_CHAIN (rethrow_decl) = stack_decl;
3739 /* Build the outermost variable binding level. */
3740 bind = build3 (BIND_EXPR, void_type_node, rethrow_decl, NULL, NULL);
3741 SET_EXPR_LOCATION (bind, cur_try_context->try_locus);
3742 TREE_SIDE_EFFECTS (bind) = 1;
3744 /* Initialize rethrow_decl. */
3745 t = build2 (MODIFY_EXPR, void_type_node, rethrow_decl,
3746 convert (objc_object_type, null_pointer_node));
3747 SET_EXPR_LOCATION (t, cur_try_context->try_locus);
3748 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
3750 /* Build the outermost TRY_FINALLY_EXPR. */
3751 try_fin = build2 (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
3752 SET_EXPR_LOCATION (try_fin, cur_try_context->try_locus);
3753 TREE_SIDE_EFFECTS (try_fin) = 1;
3754 append_to_statement_list (try_fin, &BIND_EXPR_BODY (bind));
3756 /* Create the complete catch sequence. */
3757 if (cur_try_context->catch_list)
3759 tree caught_decl = objc_build_exc_ptr ();
3760 catch_seq = build_stmt (input_location, BIND_EXPR, caught_decl, NULL, NULL);
3761 TREE_SIDE_EFFECTS (catch_seq) = 1;
3763 t = next_sjlj_build_exc_extract (caught_decl);
3764 append_to_statement_list (t, &BIND_EXPR_BODY (catch_seq));
3766 t = next_sjlj_build_enter_and_setjmp ();
3767 COND_EXPR_THEN (t) = next_sjlj_build_exc_extract (rethrow_decl);
3768 COND_EXPR_ELSE (t) = next_sjlj_build_catch_list ();
3769 append_to_statement_list (t, &BIND_EXPR_BODY (catch_seq));
3772 catch_seq = next_sjlj_build_exc_extract (rethrow_decl);
3773 SET_EXPR_LOCATION (catch_seq, cur_try_context->end_try_locus);
3775 /* Build the main register-and-try if statement. */
3776 t = next_sjlj_build_enter_and_setjmp ();
3777 SET_EXPR_LOCATION (t, cur_try_context->try_locus);
3778 COND_EXPR_THEN (t) = catch_seq;
3779 COND_EXPR_ELSE (t) = cur_try_context->try_body;
3780 TREE_OPERAND (try_fin, 0) = t;
3782 /* Build the complete FINALLY statement list. */
3783 t = next_sjlj_build_try_exit ();
3784 t = build_stmt (input_location, COND_EXPR,
3785 c_common_truthvalue_conversion
3786 (input_location, rethrow_decl),
3788 SET_EXPR_LOCATION (t, cur_try_context->finally_locus);
3789 append_to_statement_list (t, &TREE_OPERAND (try_fin, 1));
3791 append_to_statement_list (cur_try_context->finally_body,
3792 &TREE_OPERAND (try_fin, 1));
3794 t = tree_cons (NULL, rethrow_decl, NULL);
3795 t = build_function_call (input_location,
3796 objc_exception_throw_decl, t);
3797 t = build_stmt (input_location, COND_EXPR,
3798 c_common_truthvalue_conversion (input_location,
3801 SET_EXPR_LOCATION (t, cur_try_context->end_finally_locus);
3802 append_to_statement_list (t, &TREE_OPERAND (try_fin, 1));
3807 /* Called just after parsing the @try and its associated BODY. We now
3808 must prepare for the tricky bits -- handling the catches and finally. */
3811 objc_begin_try_stmt (location_t try_locus, tree body)
3813 struct objc_try_context *c = XCNEW (struct objc_try_context);
3814 c->outer = cur_try_context;
3816 c->try_locus = try_locus;
3817 c->end_try_locus = input_location;
3818 cur_try_context = c;
3820 if (flag_objc_sjlj_exceptions)
3822 /* On Darwin, ObjC exceptions require a sufficiently recent
3823 version of the runtime, so the user must ask for them explicitly. */
3824 if (!flag_objc_exceptions)
3825 warning (0, "use %<-fobjc-exceptions%> to enable Objective-C "
3826 "exception syntax");
3829 if (flag_objc_sjlj_exceptions)
3830 objc_mark_locals_volatile (NULL);
3833 /* Called just after parsing "@catch (parm)". Open a binding level,
3834 enter DECL into the binding level, and initialize it. Leave the
3835 binding level open while the body of the compound statement is parsed. */
3838 objc_begin_catch_clause (tree decl)
3840 tree compound, type, t;
3842 /* Begin a new scope that the entire catch clause will live in. */
3843 compound = c_begin_compound_stmt (true);
3845 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
3846 decl = build_decl (input_location,
3847 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
3848 lang_hooks.decls.pushdecl (decl);
3850 /* Since a decl is required here by syntax, don't warn if its unused. */
3851 /* ??? As opposed to __attribute__((unused))? Anyway, this appears to
3852 be what the previous objc implementation did. */
3853 TREE_USED (decl) = 1;
3855 /* Verify that the type of the catch is valid. It must be a pointer
3856 to an Objective-C class, or "id" (which is catch-all). */
3857 type = TREE_TYPE (decl);
3859 if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type)))
3861 else if (!POINTER_TYPE_P (type) || !TYPED_OBJECT (TREE_TYPE (type)))
3863 error ("@catch parameter is not a known Objective-C class type");
3864 type = error_mark_node;
3866 else if (cur_try_context->catch_list)
3868 /* Examine previous @catch clauses and see if we've already
3869 caught the type in question. */
3870 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
3871 for (; !tsi_end_p (i); tsi_next (&i))
3873 tree stmt = tsi_stmt (i);
3874 t = CATCH_TYPES (stmt);
3875 if (t == error_mark_node)
3877 if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type)))
3879 warning (0, "exception of type %<%T%> will be caught",
3881 warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>",
3882 TREE_TYPE (t ? t : objc_object_type));
3888 /* Record the data for the catch in the try context so that we can
3889 finalize it later. */
3890 t = build_stmt (input_location, CATCH_EXPR, type, compound);
3891 cur_try_context->current_catch = t;
3893 /* Initialize the decl from the EXC_PTR_EXPR we get from the runtime. */
3894 t = objc_build_exc_ptr ();
3895 t = convert (TREE_TYPE (decl), t);
3896 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
3900 /* Called just after parsing the closing brace of a @catch clause. Close
3901 the open binding level, and record a CATCH_EXPR for it. */
3904 objc_finish_catch_clause (void)
3906 tree c = cur_try_context->current_catch;
3907 cur_try_context->current_catch = NULL;
3908 cur_try_context->end_catch_locus = input_location;
3910 CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1);
3911 append_to_statement_list (c, &cur_try_context->catch_list);
3914 /* Called after parsing a @finally clause and its associated BODY.
3915 Record the body for later placement. */
3918 objc_build_finally_clause (location_t finally_locus, tree body)
3920 cur_try_context->finally_body = body;
3921 cur_try_context->finally_locus = finally_locus;
3922 cur_try_context->end_finally_locus = input_location;
3925 /* Called to finalize a @try construct. */
3928 objc_finish_try_stmt (void)
3930 struct objc_try_context *c = cur_try_context;
3933 if (c->catch_list == NULL && c->finally_body == NULL)
3934 error ("%<@try%> without %<@catch%> or %<@finally%>");
3936 /* If we're doing Darwin setjmp exceptions, build the big nasty. */
3937 if (flag_objc_sjlj_exceptions)
3939 bool save = in_late_binary_op;
3940 in_late_binary_op = true;
3941 if (!cur_try_context->finally_body)
3943 cur_try_context->finally_locus = input_location;
3944 cur_try_context->end_finally_locus = input_location;
3946 stmt = next_sjlj_build_try_catch_finally ();
3947 in_late_binary_op = save;
3951 /* Otherwise, nest the CATCH inside a FINALLY. */
3955 stmt = build_stmt (input_location, TRY_CATCH_EXPR, stmt, c->catch_list);
3956 SET_EXPR_LOCATION (stmt, cur_try_context->try_locus);
3958 if (c->finally_body)
3960 stmt = build_stmt (input_location, TRY_FINALLY_EXPR, stmt, c->finally_body);
3961 SET_EXPR_LOCATION (stmt, cur_try_context->try_locus);
3966 cur_try_context = c->outer;
3972 objc_build_throw_stmt (location_t loc, tree throw_expr)
3976 if (flag_objc_sjlj_exceptions)
3978 /* On Darwin, ObjC exceptions require a sufficiently recent
3979 version of the runtime, so the user must ask for them explicitly. */
3980 if (!flag_objc_exceptions)
3981 warning (0, "use %<-fobjc-exceptions%> to enable Objective-C "
3982 "exception syntax");
3985 if (throw_expr == NULL)
3987 /* If we're not inside a @catch block, there is no "current
3988 exception" to be rethrown. */
3989 if (cur_try_context == NULL
3990 || cur_try_context->current_catch == NULL)
3992 error_at (loc, "%<@throw%> (rethrow) used outside of a @catch block");
3996 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
3997 value that we get from the runtime. */
3998 throw_expr = objc_build_exc_ptr ();
4001 /* A throw is just a call to the runtime throw function with the
4002 object as a parameter. */
4003 args = tree_cons (NULL, throw_expr, NULL);
4004 return add_stmt (build_function_call (loc,
4005 objc_exception_throw_decl, args));
4009 objc_build_synchronized (location_t start_locus, tree mutex, tree body)
4013 /* First lock the mutex. */
4014 mutex = save_expr (mutex);
4015 args = tree_cons (NULL, mutex, NULL);
4016 call = build_function_call (input_location,
4017 objc_sync_enter_decl, args);
4018 SET_EXPR_LOCATION (call, start_locus);
4021 /* Build the mutex unlock. */
4022 args = tree_cons (NULL, mutex, NULL);
4023 call = build_function_call (input_location,
4024 objc_sync_exit_decl, args);
4025 SET_EXPR_LOCATION (call, input_location);
4027 /* Put the that and the body in a TRY_FINALLY. */
4028 objc_begin_try_stmt (start_locus, body);
4029 objc_build_finally_clause (input_location, call);
4030 return objc_finish_try_stmt ();
4034 /* Predefine the following data type:
4036 struct _objc_exception_data
4038 int buf[OBJC_JBLEN];
4042 /* The following yuckiness should prevent users from having to #include
4043 <setjmp.h> in their code... */
4045 /* Define to a harmless positive value so the below code doesn't die. */
4047 #define OBJC_JBLEN 18
4051 build_next_objc_exception_stuff (void)
4053 tree field_decl, field_decl_chain, index, temp_type;
4055 objc_exception_data_template
4056 = objc_start_struct (get_identifier (UTAG_EXCDATA));
4058 /* int buf[OBJC_JBLEN]; */
4060 index = build_index_type (build_int_cst (NULL_TREE, OBJC_JBLEN - 1));
4061 field_decl = create_field_decl (build_array_type (integer_type_node, index),
4063 field_decl_chain = field_decl;
4065 /* void *pointers[4]; */
4067 index = build_index_type (build_int_cst (NULL_TREE, 4 - 1));
4068 field_decl = create_field_decl (build_array_type (ptr_type_node, index),
4070 chainon (field_decl_chain, field_decl);
4072 objc_finish_struct (objc_exception_data_template, field_decl_chain);
4074 /* int _setjmp(...); */
4075 /* If the user includes <setjmp.h>, this shall be superseded by
4076 'int _setjmp(jmp_buf);' */
4077 temp_type = build_function_type (integer_type_node, NULL_TREE);
4079 = add_builtin_function (TAG_SETJMP, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
4081 /* id objc_exception_extract(struct _objc_exception_data *); */
4083 = build_function_type (objc_object_type,
4084 tree_cons (NULL_TREE,
4085 build_pointer_type (objc_exception_data_template),
4087 objc_exception_extract_decl
4088 = add_builtin_function (TAG_EXCEPTIONEXTRACT, temp_type, 0, NOT_BUILT_IN, NULL,
4090 /* void objc_exception_try_enter(struct _objc_exception_data *); */
4091 /* void objc_exception_try_exit(struct _objc_exception_data *); */
4093 = build_function_type (void_type_node,
4094 tree_cons (NULL_TREE,
4095 build_pointer_type (objc_exception_data_template),
4097 objc_exception_try_enter_decl
4098 = add_builtin_function (TAG_EXCEPTIONTRYENTER, temp_type, 0, NOT_BUILT_IN, NULL,
4100 objc_exception_try_exit_decl
4101 = add_builtin_function (TAG_EXCEPTIONTRYEXIT, temp_type, 0, NOT_BUILT_IN, NULL,
4104 /* int objc_exception_match(id, id); */
4106 = build_function_type (integer_type_node,
4107 tree_cons (NULL_TREE, objc_object_type,
4108 tree_cons (NULL_TREE, objc_object_type,
4109 OBJC_VOID_AT_END)));
4110 objc_exception_match_decl
4111 = add_builtin_function (TAG_EXCEPTIONMATCH, temp_type, 0, NOT_BUILT_IN, NULL,
4114 /* id objc_assign_ivar (id, id, unsigned int); */
4115 /* id objc_assign_ivar_Fast (id, id, unsigned int)
4116 __attribute__ ((hard_coded_address (OFFS_ASSIGNIVAR_FAST))); */
4118 = build_function_type (objc_object_type,
4120 (NULL_TREE, objc_object_type,
4121 tree_cons (NULL_TREE, objc_object_type,
4122 tree_cons (NULL_TREE,
4124 OBJC_VOID_AT_END))));
4125 objc_assign_ivar_decl
4126 = add_builtin_function (TAG_ASSIGNIVAR, temp_type, 0, NOT_BUILT_IN,
4128 #ifdef OFFS_ASSIGNIVAR_FAST
4129 objc_assign_ivar_fast_decl
4130 = add_builtin_function (TAG_ASSIGNIVAR_FAST, temp_type, 0,
4131 NOT_BUILT_IN, NULL, NULL_TREE);
4132 DECL_ATTRIBUTES (objc_assign_ivar_fast_decl)
4133 = tree_cons (get_identifier ("hard_coded_address"),
4134 build_int_cst (NULL_TREE, OFFS_ASSIGNIVAR_FAST),
4137 /* Default to slower ivar method. */
4138 objc_assign_ivar_fast_decl = objc_assign_ivar_decl;
4141 /* id objc_assign_global (id, id *); */
4142 /* id objc_assign_strongCast (id, id *); */
4143 temp_type = build_function_type (objc_object_type,
4144 tree_cons (NULL_TREE, objc_object_type,
4145 tree_cons (NULL_TREE, build_pointer_type (objc_object_type),
4146 OBJC_VOID_AT_END)));
4147 objc_assign_global_decl
4148 = add_builtin_function (TAG_ASSIGNGLOBAL, temp_type, 0, NOT_BUILT_IN, NULL,
4150 objc_assign_strong_cast_decl
4151 = add_builtin_function (TAG_ASSIGNSTRONGCAST, temp_type, 0, NOT_BUILT_IN, NULL,
4156 build_objc_exception_stuff (void)
4158 tree noreturn_list, nothrow_list, temp_type;
4160 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
4161 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
4163 /* void objc_exception_throw(id) __attribute__((noreturn)); */
4164 /* void objc_sync_enter(id); */
4165 /* void objc_sync_exit(id); */
4166 temp_type = build_function_type (void_type_node,
4167 tree_cons (NULL_TREE, objc_object_type,
4169 objc_exception_throw_decl
4170 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
4172 objc_sync_enter_decl
4173 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
4174 NULL, nothrow_list);
4176 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
4177 NULL, nothrow_list);
4180 /* Construct a C struct corresponding to ObjC class CLASS, with the same
4183 struct <classname> {
4184 struct _objc_class *isa;
4189 build_private_template (tree klass)
4191 if (!CLASS_STATIC_TEMPLATE (klass))
4193 tree record = objc_build_struct (klass,
4194 get_class_ivars (klass, false),
4195 CLASS_SUPER_NAME (klass));