1 /* Implement classes and message passing for Objective C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005 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 2, 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 COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
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"
60 #include "langhooks.h"
71 #include "diagnostic.h"
73 #include "tree-iterator.h"
76 #include "langhooks-def.h"
78 #define OBJC_VOID_AT_END void_list_node
80 static unsigned int should_call_super_dealloc = 0;
82 /* When building Objective-C++, we are not linking against the C front-end
83 and so need to replicate the C tree-construction functions in some way. */
85 #define OBJCP_REMAP_FUNCTIONS
86 #include "objcp-decl.h"
89 /* This is the default way of generating a method name. */
90 /* I am not sure it is really correct.
91 Perhaps there's a danger that it will make name conflicts
92 if method names contain underscores. -- rms. */
93 #ifndef OBJC_GEN_METHOD_LABEL
94 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
97 sprintf ((BUF), "_%s_%s_%s_%s", \
98 ((IS_INST) ? "i" : "c"), \
100 ((CAT_NAME)? (CAT_NAME) : ""), \
102 for (temp = (BUF); *temp; temp++) \
103 if (*temp == ':') *temp = '_'; \
107 /* These need specifying. */
108 #ifndef OBJC_FORWARDING_STACK_OFFSET
109 #define OBJC_FORWARDING_STACK_OFFSET 0
112 #ifndef OBJC_FORWARDING_MIN_OFFSET
113 #define OBJC_FORWARDING_MIN_OFFSET 0
116 /* Set up for use of obstacks. */
120 /* This obstack is used to accumulate the encoding of a data type. */
121 static struct obstack util_obstack;
123 /* This points to the beginning of obstack contents, so we can free
124 the whole contents. */
127 /* The version identifies which language generation and runtime
128 the module (file) was compiled for, and is recorded in the
129 module descriptor. */
131 #define OBJC_VERSION (flag_next_runtime ? 6 : 8)
132 #define PROTOCOL_VERSION 2
134 /* (Decide if these can ever be validly changed.) */
135 #define OBJC_ENCODE_INLINE_DEFS 0
136 #define OBJC_ENCODE_DONT_INLINE_DEFS 1
138 /*** Private Interface (procedures) ***/
140 /* Used by compile_file. */
142 static void init_objc (void);
143 static void finish_objc (void);
145 /* Code generation. */
147 static tree objc_build_constructor (tree, tree);
148 static tree build_objc_method_call (int, tree, tree, tree, tree);
149 static tree get_proto_encoding (tree);
150 static tree lookup_interface (tree);
151 static tree objc_add_static_instance (tree, tree);
153 static tree start_class (enum tree_code, tree, tree, tree);
154 static tree continue_class (tree);
155 static void finish_class (tree);
156 static void start_method_def (tree);
158 static void objc_start_function (tree, tree, tree, tree);
160 static void objc_start_function (tree, tree, tree, struct c_arg_info *);
162 static tree start_protocol (enum tree_code, tree, tree);
163 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
164 static tree objc_add_method (tree, tree, int);
165 static tree add_instance_variable (tree, int, tree);
166 static tree build_ivar_reference (tree);
167 static tree is_ivar (tree, tree);
169 static void build_objc_exception_stuff (void);
170 static void build_next_objc_exception_stuff (void);
172 /* We only need the following for ObjC; ObjC++ will use C++'s definition
173 of DERIVED_FROM_P. */
175 static bool objc_derived_from_p (tree, tree);
176 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
178 static void objc_xref_basetypes (tree, tree);
180 static void build_class_template (void);
181 static void build_selector_template (void);
182 static void build_category_template (void);
183 static void build_super_template (void);
184 static tree build_protocol_initializer (tree, tree, tree, tree, tree);
185 static tree get_class_ivars (tree, bool);
186 static tree generate_protocol_list (tree);
187 static void build_protocol_reference (tree);
188 static tree objc_build_volatilized_type (tree);
191 static void objc_generate_cxx_cdtors (void);
194 static const char *synth_id_with_class_suffix (const char *, tree);
196 /* Hash tables to manage the global pool of method prototypes. */
198 hash *nst_method_hash_list = 0;
199 hash *cls_method_hash_list = 0;
201 static hash hash_lookup (hash *, tree);
202 static tree lookup_method (tree, tree);
203 static tree lookup_method_static (tree, tree, int);
207 class_names, /* class, category, protocol, module names */
208 meth_var_names, /* method and variable names */
209 meth_var_types /* method and variable type descriptors */
212 static tree add_objc_string (tree, enum string_section);
213 static tree build_objc_string_decl (enum string_section);
214 static void build_selector_table_decl (void);
216 /* Protocol additions. */
218 static tree lookup_protocol (tree);
219 static tree lookup_and_install_protocols (tree);
223 static void encode_type_qualifiers (tree);
224 static void encode_type (tree, int, int);
225 static void encode_field_decl (tree, int, int);
228 static void really_start_method (tree, tree);
230 static void really_start_method (tree, struct c_arg_info *);
232 static int comp_proto_with_proto (tree, tree, int);
233 static void objc_push_parm (tree);
235 static tree objc_get_parm_info (int);
237 static struct c_arg_info *objc_get_parm_info (int);
240 /* Utilities for debugging and error diagnostics. */
242 static void warn_with_method (const char *, int, tree);
243 static char *gen_type_name (tree);
244 static char *gen_type_name_0 (tree);
245 static char *gen_method_decl (tree);
246 static char *gen_declaration (tree);
248 /* Everything else. */
250 static tree create_field_decl (tree, const char *);
251 static void add_class_reference (tree);
252 static void build_protocol_template (void);
253 static tree encode_method_prototype (tree);
254 static void generate_classref_translation_entry (tree);
255 static void handle_class_ref (tree);
256 static void generate_struct_by_value_array (void)
258 static void mark_referenced_methods (void);
259 static void generate_objc_image_info (void);
261 /*** Private Interface (data) ***/
263 /* Reserved tag definitions. */
265 #define OBJECT_TYPEDEF_NAME "id"
266 #define CLASS_TYPEDEF_NAME "Class"
268 #define TAG_OBJECT "objc_object"
269 #define TAG_CLASS "objc_class"
270 #define TAG_SUPER "objc_super"
271 #define TAG_SELECTOR "objc_selector"
273 #define UTAG_CLASS "_objc_class"
274 #define UTAG_IVAR "_objc_ivar"
275 #define UTAG_IVAR_LIST "_objc_ivar_list"
276 #define UTAG_METHOD "_objc_method"
277 #define UTAG_METHOD_LIST "_objc_method_list"
278 #define UTAG_CATEGORY "_objc_category"
279 #define UTAG_MODULE "_objc_module"
280 #define UTAG_SYMTAB "_objc_symtab"
281 #define UTAG_SUPER "_objc_super"
282 #define UTAG_SELECTOR "_objc_selector"
284 #define UTAG_PROTOCOL "_objc_protocol"
285 #define UTAG_METHOD_PROTOTYPE "_objc_method_prototype"
286 #define UTAG_METHOD_PROTOTYPE_LIST "_objc__method_prototype_list"
288 /* Note that the string object global name is only needed for the
290 #define STRING_OBJECT_GLOBAL_FORMAT "_%sClassReference"
292 #define PROTOCOL_OBJECT_CLASS_NAME "Protocol"
294 static const char *TAG_GETCLASS;
295 static const char *TAG_GETMETACLASS;
296 static const char *TAG_MSGSEND;
297 static const char *TAG_MSGSENDSUPER;
298 /* The NeXT Objective-C messenger may have two extra entry points, for use
299 when returning a structure. */
300 static const char *TAG_MSGSEND_STRET;
301 static const char *TAG_MSGSENDSUPER_STRET;
302 static const char *default_constant_string_class_name;
304 /* Runtime metadata flags. */
305 #define CLS_FACTORY 0x0001L
306 #define CLS_META 0x0002L
307 #define CLS_HAS_CXX_STRUCTORS 0x2000L
309 #define OBJC_MODIFIER_STATIC 0x00000001
310 #define OBJC_MODIFIER_FINAL 0x00000002
311 #define OBJC_MODIFIER_PUBLIC 0x00000004
312 #define OBJC_MODIFIER_PRIVATE 0x00000008
313 #define OBJC_MODIFIER_PROTECTED 0x00000010
314 #define OBJC_MODIFIER_NATIVE 0x00000020
315 #define OBJC_MODIFIER_SYNCHRONIZED 0x00000040
316 #define OBJC_MODIFIER_ABSTRACT 0x00000080
317 #define OBJC_MODIFIER_VOLATILE 0x00000100
318 #define OBJC_MODIFIER_TRANSIENT 0x00000200
319 #define OBJC_MODIFIER_NONE_SPECIFIED 0x80000000
321 /* NeXT-specific tags. */
323 #define TAG_MSGSEND_NONNIL "objc_msgSendNonNil"
324 #define TAG_MSGSEND_NONNIL_STRET "objc_msgSendNonNil_stret"
325 #define TAG_EXCEPTIONEXTRACT "objc_exception_extract"
326 #define TAG_EXCEPTIONTRYENTER "objc_exception_try_enter"
327 #define TAG_EXCEPTIONTRYEXIT "objc_exception_try_exit"
328 #define TAG_EXCEPTIONMATCH "objc_exception_match"
329 #define TAG_EXCEPTIONTHROW "objc_exception_throw"
330 #define TAG_SYNCENTER "objc_sync_enter"
331 #define TAG_SYNCEXIT "objc_sync_exit"
332 #define TAG_SETJMP "_setjmp"
333 #define UTAG_EXCDATA "_objc_exception_data"
335 #define TAG_ASSIGNIVAR "objc_assign_ivar"
336 #define TAG_ASSIGNGLOBAL "objc_assign_global"
337 #define TAG_ASSIGNSTRONGCAST "objc_assign_strongCast"
339 /* Branch entry points. All that matters here are the addresses;
340 functions with these names do not really exist in libobjc. */
342 #define TAG_MSGSEND_FAST "objc_msgSend_Fast"
343 #define TAG_ASSIGNIVAR_FAST "objc_assign_ivar_Fast"
345 #define TAG_CXX_CONSTRUCT ".cxx_construct"
346 #define TAG_CXX_DESTRUCT ".cxx_destruct"
348 /* GNU-specific tags. */
350 #define TAG_EXECCLASS "__objc_exec_class"
351 #define TAG_GNUINIT "__objc_gnu_init"
353 /* Flags for lookup_method_static(). */
354 #define OBJC_LOOKUP_CLASS 1 /* Look for class methods. */
355 #define OBJC_LOOKUP_NO_SUPER 2 /* Do not examine superclasses. */
357 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
358 tree objc_global_trees[OCTI_MAX];
360 static void handle_impent (struct imp_entry *);
362 struct imp_entry *imp_list = 0;
363 int imp_count = 0; /* `@implementation' */
364 int cat_count = 0; /* `@category' */
366 enum tree_code objc_inherit_code;
367 int objc_public_flag;
369 /* Use to generate method labels. */
370 static int method_slot = 0;
374 static char *errbuf; /* Buffer for error diagnostics */
376 /* Data imported from tree.c. */
378 extern enum debug_info_type write_symbols;
380 /* Data imported from toplev.c. */
382 extern const char *dump_base_name;
384 static int flag_typed_selectors;
386 /* Store all constructed constant strings in a hash table so that
387 they get uniqued properly. */
389 struct string_descriptor GTY(())
391 /* The literal argument . */
394 /* The resulting constant string. */
398 static GTY((param_is (struct string_descriptor))) htab_t string_htab;
400 /* Store the EH-volatilized types in a hash table, for easy retrieval. */
401 struct volatilized_type GTY(())
406 static GTY((param_is (struct volatilized_type))) htab_t volatilized_htab;
408 FILE *gen_declaration_file;
410 /* Tells "encode_pointer/encode_aggregate" whether we are generating
411 type descriptors for instance variables (as opposed to methods).
412 Type descriptors for instance variables contain more information
413 than methods (for static typing and embedded structures). */
415 static int generating_instance_variables = 0;
417 /* Some platforms pass small structures through registers versus
418 through an invisible pointer. Determine at what size structure is
419 the transition point between the two possibilities. */
422 generate_struct_by_value_array (void)
425 tree field_decl, field_decl_chain;
427 int aggregate_in_mem[32];
430 /* Presumably no platform passes 32 byte structures in a register. */
431 for (i = 1; i < 32; i++)
435 /* Create an unnamed struct that has `i' character components */
436 type = start_struct (RECORD_TYPE, NULL_TREE);
438 strcpy (buffer, "c1");
439 field_decl = create_field_decl (char_type_node,
441 field_decl_chain = field_decl;
443 for (j = 1; j < i; j++)
445 sprintf (buffer, "c%d", j + 1);
446 field_decl = create_field_decl (char_type_node,
448 chainon (field_decl_chain, field_decl);
450 finish_struct (type, field_decl_chain, NULL_TREE);
452 aggregate_in_mem[i] = aggregate_value_p (type, 0);
453 if (!aggregate_in_mem[i])
457 /* We found some structures that are returned in registers instead of memory
458 so output the necessary data. */
461 for (i = 31; i >= 0; i--)
462 if (!aggregate_in_mem[i])
464 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n\n", i);
466 /* The first member of the structure is always 0 because we don't handle
467 structures with 0 members */
468 printf ("static int struct_forward_array[] = {\n 0");
470 for (j = 1; j <= i; j++)
471 printf (", %d", aggregate_in_mem[j]);
482 if (cxx_init () == false)
484 if (c_objc_common_init () == false)
488 #ifndef USE_MAPPED_LOCATION
489 /* Force the line number back to 0; check_newline will have
490 raised it to 1, which will make the builtin functions appear
491 not to be built in. */
495 /* If gen_declaration desired, open the output file. */
496 if (flag_gen_declaration)
498 register char * const dumpname = concat (dump_base_name, ".decl", NULL);
499 gen_declaration_file = fopen (dumpname, "w");
500 if (gen_declaration_file == 0)
501 fatal_error ("can't open %s: %m", dumpname);
505 if (flag_next_runtime)
507 TAG_GETCLASS = "objc_getClass";
508 TAG_GETMETACLASS = "objc_getMetaClass";
509 TAG_MSGSEND = "objc_msgSend";
510 TAG_MSGSENDSUPER = "objc_msgSendSuper";
511 TAG_MSGSEND_STRET = "objc_msgSend_stret";
512 TAG_MSGSENDSUPER_STRET = "objc_msgSendSuper_stret";
513 default_constant_string_class_name = "NSConstantString";
517 TAG_GETCLASS = "objc_get_class";
518 TAG_GETMETACLASS = "objc_get_meta_class";
519 TAG_MSGSEND = "objc_msg_lookup";
520 TAG_MSGSENDSUPER = "objc_msg_lookup_super";
521 /* GNU runtime does not provide special functions to support
522 structure-returning methods. */
523 default_constant_string_class_name = "NXConstantString";
524 flag_typed_selectors = 1;
529 if (print_struct_values)
530 generate_struct_by_value_array ();
536 objc_finish_file (void)
538 mark_referenced_methods ();
541 /* We need to instantiate templates _before_ we emit ObjC metadata;
542 if we do not, some metadata (such as selectors) may go missing. */
544 instantiate_pending_templates (0);
547 /* Finalize Objective-C runtime data. No need to generate tables
548 and code if only checking syntax, or if generating a PCH file. */
549 if (!flag_syntax_only && !pch_file)
552 if (gen_declaration_file)
553 fclose (gen_declaration_file);
560 /* Return the first occurrence of a method declaration corresponding
561 to sel_name in rproto_list. Search rproto_list recursively.
562 If is_class is 0, search for instance methods, otherwise for class
565 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
571 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
573 p = TREE_VALUE (rproto);
575 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
577 if ((fnd = lookup_method (is_class
578 ? PROTOCOL_CLS_METHODS (p)
579 : PROTOCOL_NST_METHODS (p), sel_name)))
581 else if (PROTOCOL_LIST (p))
582 fnd = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
587 ; /* An identifier...if we could not find a protocol. */
598 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
602 /* Make sure the protocol is supported by the object on the rhs. */
603 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
606 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
608 p = TREE_VALUE (rproto);
610 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
615 else if (PROTOCOL_LIST (p))
616 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
625 ; /* An identifier...if we could not find a protocol. */
632 objc_start_class_interface (tree class, tree super_class, tree protos)
634 objc_interface_context
636 = start_class (CLASS_INTERFACE_TYPE, class, super_class, protos);
637 objc_public_flag = 0;
641 objc_start_category_interface (tree class, tree categ, tree protos)
643 objc_interface_context
644 = start_class (CATEGORY_INTERFACE_TYPE, class, categ, protos);
646 = continue_class (objc_interface_context);
650 objc_start_protocol (tree name, tree protos)
652 objc_interface_context
653 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos);
657 objc_continue_interface (void)
660 = continue_class (objc_interface_context);
664 objc_finish_interface (void)
666 finish_class (objc_interface_context);
667 objc_interface_context = NULL_TREE;
671 objc_start_class_implementation (tree class, tree super_class)
673 objc_implementation_context
675 = start_class (CLASS_IMPLEMENTATION_TYPE, class, super_class, NULL_TREE);
676 objc_public_flag = 0;
680 objc_start_category_implementation (tree class, tree categ)
682 objc_implementation_context
683 = start_class (CATEGORY_IMPLEMENTATION_TYPE, class, categ, NULL_TREE);
685 = continue_class (objc_implementation_context);
689 objc_continue_implementation (void)
692 = continue_class (objc_implementation_context);
696 objc_finish_implementation (void)
699 if (flag_objc_call_cxx_cdtors)
700 objc_generate_cxx_cdtors ();
703 if (objc_implementation_context)
705 finish_class (objc_implementation_context);
706 objc_ivar_chain = NULL_TREE;
707 objc_implementation_context = NULL_TREE;
710 warning (0, "%<@end%> must appear in an @implementation context");
714 objc_set_visibility (int visibility)
716 objc_public_flag = visibility;
720 objc_set_method_type (enum tree_code type)
722 objc_inherit_code = (type == PLUS_EXPR
724 : INSTANCE_METHOD_DECL);
728 objc_build_method_signature (tree rettype, tree selector,
729 tree optparms, bool ellipsis)
731 return build_method_decl (objc_inherit_code, rettype, selector,
736 objc_add_method_declaration (tree decl)
738 if (!objc_interface_context)
739 fatal_error ("method declaration not in @interface context");
741 objc_add_method (objc_interface_context,
743 objc_inherit_code == CLASS_METHOD_DECL);
747 objc_start_method_definition (tree decl)
749 if (!objc_implementation_context)
750 fatal_error ("method definition not in @implementation context");
752 objc_add_method (objc_implementation_context,
754 objc_inherit_code == CLASS_METHOD_DECL);
755 start_method_def (decl);
759 objc_add_instance_variable (tree decl)
761 (void) add_instance_variable (objc_ivar_context,
766 /* Return 1 if IDENT is an ObjC/ObjC++ reserved keyword in the context of
770 objc_is_reserved_word (tree ident)
772 unsigned char code = C_RID_CODE (ident);
774 return (OBJC_IS_AT_KEYWORD (code)
776 || code == RID_CLASS || code == RID_PUBLIC
777 || code == RID_PROTECTED || code == RID_PRIVATE
778 || code == RID_TRY || code == RID_THROW || code == RID_CATCH
783 /* Return true if TYPE is 'id'. */
786 objc_is_object_id (tree type)
788 return OBJC_TYPE_NAME (type) == objc_object_id;
792 objc_is_class_id (tree type)
794 return OBJC_TYPE_NAME (type) == objc_class_id;
797 /* Construct a C struct with same name as CLASS, a base struct with tag
798 SUPER_NAME (if any), and FIELDS indicated. */
801 objc_build_struct (tree class, tree fields, tree super_name)
803 tree name = CLASS_NAME (class);
804 tree s = start_struct (RECORD_TYPE, name);
805 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
806 tree t, objc_info = NULL_TREE;
810 /* Prepend a packed variant of the base class into the layout. This
811 is necessary to preserve ObjC ABI compatibility. */
812 tree base = build_decl (FIELD_DECL, NULL_TREE, super);
813 tree field = TYPE_FIELDS (super);
815 while (field && TREE_CHAIN (field)
816 && TREE_CODE (TREE_CHAIN (field)) == FIELD_DECL)
817 field = TREE_CHAIN (field);
819 /* For ObjC ABI purposes, the "packed" size of a base class is the
820 the sum of the offset and the size (in bits) of the last field
823 = (field && TREE_CODE (field) == FIELD_DECL
824 ? size_binop (PLUS_EXPR,
825 size_binop (PLUS_EXPR,
828 convert (bitsizetype,
829 DECL_FIELD_OFFSET (field)),
830 bitsize_int (BITS_PER_UNIT)),
831 DECL_FIELD_BIT_OFFSET (field)),
833 : bitsize_zero_node);
834 DECL_SIZE_UNIT (base)
835 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
836 size_int (BITS_PER_UNIT));
837 DECL_ARTIFICIAL (base) = 1;
838 DECL_ALIGN (base) = 1;
839 DECL_FIELD_CONTEXT (base) = s;
841 DECL_FIELD_IS_BASE (base) = 1;
844 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
845 #endif /* are following the ObjC ABI here. */
846 TREE_CHAIN (base) = fields;
850 /* NB: Calling finish_struct() may cause type TYPE_LANG_SPECIFIC fields
851 in all variants of this RECORD_TYPE to be clobbered, but it is therein
852 that we store protocol conformance info (e.g., 'NSObject <MyProtocol>').
853 Hence, we must squirrel away the ObjC-specific information before calling
854 finish_struct(), and then reinstate it afterwards. */
856 for (t = TYPE_NEXT_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
858 = chainon (objc_info,
859 build_tree_list (NULL_TREE, TYPE_OBJC_INFO (t)));
861 /* Point the struct at its related Objective-C class. */
862 INIT_TYPE_OBJC_INFO (s);
863 TYPE_OBJC_INTERFACE (s) = class;
865 s = finish_struct (s, fields, NULL_TREE);
867 for (t = TYPE_NEXT_VARIANT (s); t;
868 t = TYPE_NEXT_VARIANT (t), objc_info = TREE_CHAIN (objc_info))
870 TYPE_OBJC_INFO (t) = TREE_VALUE (objc_info);
871 /* Replace the IDENTIFIER_NODE with an actual @interface. */
872 TYPE_OBJC_INTERFACE (t) = class;
875 /* Use TYPE_BINFO structures to point at the super class, if any. */
876 objc_xref_basetypes (s, super);
878 /* Mark this struct as a class template. */
879 CLASS_STATIC_TEMPLATE (class) = s;
884 /* Build a type differing from TYPE only in that TYPE_VOLATILE is set.
885 Unlike tree.c:build_qualified_type(), preserve TYPE_LANG_SPECIFIC in the
888 objc_build_volatilized_type (tree type)
892 /* Check if we have not constructed the desired variant already. */
893 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
895 /* The type qualifiers must (obviously) match up. */
896 if (!TYPE_VOLATILE (t)
897 || (TYPE_READONLY (t) != TYPE_READONLY (type))
898 || (TYPE_RESTRICT (t) != TYPE_RESTRICT (type)))
901 /* For pointer types, the pointees (and hence their TYPE_LANG_SPECIFIC
902 info, if any) must match up. */
903 if (POINTER_TYPE_P (t)
904 && (TREE_TYPE (t) != TREE_TYPE (type)))
907 /* Everything matches up! */
911 /* Ok, we could not re-use any of the pre-existing variants. Create
913 t = build_variant_type_copy (type);
914 TYPE_VOLATILE (t) = 1;
919 /* Mark DECL as being 'volatile' for purposes of Darwin
920 _setjmp()/_longjmp() exception handling. Called from
921 objc_mark_locals_volatile(). */
923 objc_volatilize_decl (tree decl)
925 /* Do not mess with variables that are 'static' or (already)
927 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
928 && (TREE_CODE (decl) == VAR_DECL
929 || TREE_CODE (decl) == PARM_DECL))
931 tree t = TREE_TYPE (decl);
932 struct volatilized_type key;
935 t = objc_build_volatilized_type (t);
937 loc = htab_find_slot (volatilized_htab, &key, INSERT);
941 *loc = ggc_alloc (sizeof (key));
942 ((struct volatilized_type *) *loc)->type = t;
945 TREE_TYPE (decl) = t;
946 TREE_THIS_VOLATILE (decl) = 1;
947 TREE_SIDE_EFFECTS (decl) = 1;
948 DECL_REGISTER (decl) = 0;
950 C_DECL_REGISTER (decl) = 0;
955 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
956 (including its categoreis and superclasses) or by object type TYP.
957 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
960 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
962 bool class_type = (cls != NULL_TREE);
968 /* Check protocols adopted by the class and its categories. */
969 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
971 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
975 /* Repeat for superclasses. */
976 cls = lookup_interface (CLASS_SUPER_NAME (cls));
979 /* Check for any protocols attached directly to the object type. */
980 if (TYPE_HAS_OBJC_INFO (typ))
982 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
988 strcpy (errbuf, class_type ? "class \'" : "type \'");
989 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
990 strcat (errbuf, "\' does not ");
991 /* NB: Types 'id' and 'Class' cannot reasonably be described as
992 "implementing" a given protocol, since they do not have an
994 strcat (errbuf, class_type ? "implement" : "conform to");
995 strcat (errbuf, " the \'");
996 strcat (errbuf, IDENTIFIER_POINTER (PROTOCOL_NAME (proto)));
997 strcat (errbuf, "\' protocol");
1004 /* Check if class RCLS and instance struct type RTYP conform to at least the
1005 same protocols that LCLS and LTYP conform to. */
1008 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
1011 bool have_lproto = false;
1015 /* NB: We do _not_ look at categories defined for LCLS; these may or
1016 may not get loaded in, and therefore it is unreasonable to require
1017 that RCLS/RTYP must implement any of their protocols. */
1018 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
1022 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
1026 /* Repeat for superclasses. */
1027 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
1030 /* Check for any protocols attached directly to the object type. */
1031 if (TYPE_HAS_OBJC_INFO (ltyp))
1033 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
1037 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
1042 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
1043 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
1044 away with simply checking for 'id' or 'Class' (!RCLS), since this
1045 routine will not get called in other cases. */
1046 return have_lproto || (rcls != NULL_TREE);
1049 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
1050 an instance of RTYP to an instance of LTYP or to compare the two
1051 (if ARGNO is equal to -3), per ObjC type system rules. Before
1052 returning 'true', this routine may issue warnings related to, e.g.,
1053 protocol conformance. When returning 'false', the routine must
1054 produce absolutely no warnings; the C or C++ front-end will do so
1055 instead, if needed. If either LTYP or RTYP is not an Objective-C type,
1056 the routine must return 'false'.
1058 The ARGNO parameter is encoded as follows:
1059 >= 1 Parameter number (CALLEE contains function being called);
1063 -3 Comparison (LTYP and RTYP may match in either direction). */
1066 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
1068 tree lcls, rcls, lproto, rproto;
1069 bool pointers_compatible;
1071 /* We must be dealing with pointer types */
1072 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
1077 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
1078 rtyp = TREE_TYPE (rtyp);
1080 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
1082 /* Past this point, we are only interested in ObjC class instances,
1083 or 'id' or 'Class'. */
1084 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
1087 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
1088 && !TYPE_HAS_OBJC_INFO (ltyp))
1091 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
1092 && !TYPE_HAS_OBJC_INFO (rtyp))
1095 /* Past this point, we are committed to returning 'true' to the caller.
1096 However, we can still warn about type and/or protocol mismatches. */
1098 if (TYPE_HAS_OBJC_INFO (ltyp))
1100 lcls = TYPE_OBJC_INTERFACE (ltyp);
1101 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
1104 lcls = lproto = NULL_TREE;
1106 if (TYPE_HAS_OBJC_INFO (rtyp))
1108 rcls = TYPE_OBJC_INTERFACE (rtyp);
1109 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
1112 rcls = rproto = NULL_TREE;
1114 /* If we could not find an @interface declaration, we must have
1115 only seen a @class declaration; for purposes of type comparison,
1116 treat it as a stand-alone (root) class. */
1118 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
1121 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
1124 /* If either type is an unqualified 'id', we're done. */
1125 if ((!lproto && objc_is_object_id (ltyp))
1126 || (!rproto && objc_is_object_id (rtyp)))
1129 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
1131 /* If the underlying types are the same, and at most one of them has
1132 a protocol list, we do not need to issue any diagnostics. */
1133 if (pointers_compatible && (!lproto || !rproto))
1136 /* If exactly one of the types is 'Class', issue a diagnostic; any
1137 exceptions of this rule have already been handled. */
1138 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
1139 pointers_compatible = false;
1140 /* Otherwise, check for inheritance relations. */
1143 if (!pointers_compatible)
1145 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
1147 if (!pointers_compatible)
1148 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
1150 if (!pointers_compatible && argno == -3)
1151 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
1154 /* If the pointers match modulo protocols, check for protocol conformance
1156 if (pointers_compatible)
1158 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
1161 if (!pointers_compatible && argno == -3)
1162 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
1166 if (!pointers_compatible)
1168 /* NB: For the time being, we shall make our warnings look like their
1169 C counterparts. In the future, we may wish to make them more
1174 warning (0, "comparison of distinct Objective-C types lacks a cast");
1178 warning (0, "initialization from distinct Objective-C type");
1182 warning (0, "assignment from distinct Objective-C type");
1186 warning (0, "distinct Objective-C type in return");
1190 warning (0, "passing argument %d of %qE from distinct "
1191 "Objective-C type", argno, callee);
1199 /* Check if LTYP and RTYP have the same type qualifiers. If either type
1200 lives in the volatilized hash table, ignore the 'volatile' bit when
1201 making the comparison. */
1204 objc_type_quals_match (tree ltyp, tree rtyp)
1206 int lquals = TYPE_QUALS (ltyp), rquals = TYPE_QUALS (rtyp);
1207 struct volatilized_type key;
1211 if (htab_find_slot (volatilized_htab, &key, NO_INSERT))
1212 lquals &= ~TYPE_QUAL_VOLATILE;
1216 if (htab_find_slot (volatilized_htab, &key, NO_INSERT))
1217 rquals &= ~TYPE_QUAL_VOLATILE;
1219 return (lquals == rquals);
1223 /* Determine if CHILD is derived from PARENT. The routine assumes that
1224 both parameters are RECORD_TYPEs, and is non-reflexive. */
1227 objc_derived_from_p (tree parent, tree child)
1229 parent = TYPE_MAIN_VARIANT (parent);
1231 for (child = TYPE_MAIN_VARIANT (child);
1232 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
1234 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
1235 (TYPE_BINFO (child),
1238 if (child == parent)
1247 objc_build_component_ref (tree datum, tree component)
1249 /* If COMPONENT is NULL, the caller is referring to the anonymous
1250 base class field. */
1253 tree base = TYPE_FIELDS (TREE_TYPE (datum));
1255 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
1258 /* The 'build_component_ref' routine has been removed from the C++
1259 front-end, but 'finish_class_member_access_expr' seems to be
1260 a worthy substitute. */
1262 return finish_class_member_access_expr (datum, component, false);
1264 return build_component_ref (datum, component);
1268 /* Recursively copy inheritance information rooted at BINFO. To do this,
1269 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
1272 objc_copy_binfo (tree binfo)
1274 tree btype = BINFO_TYPE (binfo);
1275 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
1279 BINFO_TYPE (binfo2) = btype;
1280 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
1281 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
1283 /* Recursively copy base binfos of BINFO. */
1284 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1286 tree base_binfo2 = objc_copy_binfo (base_binfo);
1288 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
1289 BINFO_BASE_APPEND (binfo2, base_binfo2);
1295 /* Record superclass information provided in BASETYPE for ObjC class REF.
1296 This is loosely based on cp/decl.c:xref_basetypes(). */
1299 objc_xref_basetypes (tree ref, tree basetype)
1301 tree binfo = make_tree_binfo (basetype ? 1 : 0);
1303 TYPE_BINFO (ref) = binfo;
1304 BINFO_OFFSET (binfo) = size_zero_node;
1305 BINFO_TYPE (binfo) = ref;
1309 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
1311 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
1312 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, 1);
1313 BINFO_BASE_APPEND (binfo, base_binfo);
1314 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
1319 volatilized_hash (const void *ptr)
1321 tree typ = ((struct volatilized_type *)ptr)->type;
1323 return htab_hash_pointer(typ);
1327 volatilized_eq (const void *ptr1, const void *ptr2)
1329 tree typ1 = ((struct volatilized_type *)ptr1)->type;
1330 tree typ2 = ((struct volatilized_type *)ptr2)->type;
1332 return typ1 == typ2;
1335 /* Called from finish_decl. */
1338 objc_check_decl (tree decl)
1340 tree type = TREE_TYPE (decl);
1342 if (TREE_CODE (type) != RECORD_TYPE)
1344 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
1345 error ("statically allocated instance of Objective-C class %qs",
1346 IDENTIFIER_POINTER (type));
1349 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where INTERFACE may
1350 either name an Objective-C class, or refer to the special 'id' or 'Class'
1351 types. If INTERFACE is not a valid ObjC type, just return it unchanged. */
1354 objc_get_protocol_qualified_type (tree interface, tree protocols)
1356 /* If INTERFACE is not provided, default to 'id'. */
1357 tree type = (interface ? objc_is_id (interface) : objc_object_type);
1358 bool is_ptr = (type != NULL_TREE);
1362 type = objc_is_class_name (interface);
1365 type = xref_tag (RECORD_TYPE, type);
1372 type = build_variant_type_copy (type);
1374 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
1378 TREE_TYPE (type) = build_variant_type_copy (TREE_TYPE (type));
1379 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
1380 type = TREE_TYPE (type);
1383 /* Look up protocols and install in lang specific list. */
1384 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
1385 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols (protocols);
1387 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
1388 return the pointer to the new pointee variant. */
1390 type = TYPE_POINTER_TO (type);
1392 TYPE_OBJC_INTERFACE (type)
1393 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
1399 /* Check for circular dependencies in protocols. The arguments are
1400 PROTO, the protocol to check, and LIST, a list of protocol it
1404 check_protocol_recursively (tree proto, tree list)
1408 for (p = list; p; p = TREE_CHAIN (p))
1410 tree pp = TREE_VALUE (p);
1412 if (TREE_CODE (pp) == IDENTIFIER_NODE)
1413 pp = lookup_protocol (pp);
1416 fatal_error ("protocol %qs has circular dependency",
1417 IDENTIFIER_POINTER (PROTOCOL_NAME (pp)));
1419 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
1423 /* Look up PROTOCOLS, and return a list of those that are found.
1424 If none are found, return NULL. */
1427 lookup_and_install_protocols (tree protocols)
1430 tree return_value = NULL_TREE;
1432 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
1434 tree ident = TREE_VALUE (proto);
1435 tree p = lookup_protocol (ident);
1438 error ("cannot find protocol declaration for %qs",
1439 IDENTIFIER_POINTER (ident));
1441 return_value = chainon (return_value,
1442 build_tree_list (NULL_TREE, p));
1445 return return_value;
1448 /* Create a declaration for field NAME of a given TYPE. */
1451 create_field_decl (tree type, const char *name)
1453 return build_decl (FIELD_DECL, get_identifier (name), type);
1456 /* Create a global, static declaration for variable NAME of a given TYPE. The
1457 finish_var_decl() routine will need to be called on it afterwards. */
1460 start_var_decl (tree type, const char *name)
1462 tree var = build_decl (VAR_DECL, get_identifier (name), type);
1464 TREE_STATIC (var) = 1;
1465 DECL_INITIAL (var) = error_mark_node; /* A real initializer is coming... */
1466 DECL_IGNORED_P (var) = 1;
1467 DECL_ARTIFICIAL (var) = 1;
1468 DECL_CONTEXT (var) = NULL_TREE;
1470 DECL_THIS_STATIC (var) = 1; /* squash redeclaration errors */
1476 /* Finish off the variable declaration created by start_var_decl(). */
1479 finish_var_decl (tree var, tree initializer)
1481 finish_decl (var, initializer, NULL_TREE);
1482 /* Ensure that the variable actually gets output. */
1483 mark_decl_referenced (var);
1484 /* Mark the decl to avoid "defined but not used" warning. */
1485 TREE_USED (var) = 1;
1488 /* Find the decl for the constant string class reference. This is only
1489 used for the NeXT runtime. */
1492 setup_string_decl (void)
1497 /* %s in format will provide room for terminating null */
1498 length = strlen (STRING_OBJECT_GLOBAL_FORMAT)
1499 + strlen (constant_string_class_name);
1500 name = xmalloc (length);
1501 sprintf (name, STRING_OBJECT_GLOBAL_FORMAT,
1502 constant_string_class_name);
1503 constant_string_global_id = get_identifier (name);
1504 string_class_decl = lookup_name (constant_string_global_id);
1506 return string_class_decl;
1509 /* Purpose: "play" parser, creating/installing representations
1510 of the declarations that are required by Objective-C.
1514 type_spec--------->sc_spec
1515 (tree_list) (tree_list)
1518 identifier_node identifier_node */
1521 synth_module_prologue (void)
1524 enum debug_info_type save_write_symbols = write_symbols;
1525 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
1527 /* Suppress outputting debug symbols, because
1528 dbxout_init hasn'r been called yet. */
1529 write_symbols = NO_DEBUG;
1530 debug_hooks = &do_nothing_debug_hooks;
1533 push_lang_context (lang_name_c); /* extern "C" */
1536 /* The following are also defined in <objc/objc.h> and friends. */
1538 objc_object_id = get_identifier (TAG_OBJECT);
1539 objc_class_id = get_identifier (TAG_CLASS);
1541 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
1542 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
1544 objc_object_type = build_pointer_type (objc_object_reference);
1545 objc_class_type = build_pointer_type (objc_class_reference);
1547 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
1548 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
1550 /* Declare the 'id' and 'Class' typedefs. */
1552 type = lang_hooks.decls.pushdecl (build_decl (TYPE_DECL,
1555 DECL_IN_SYSTEM_HEADER (type) = 1;
1556 type = lang_hooks.decls.pushdecl (build_decl (TYPE_DECL,
1559 DECL_IN_SYSTEM_HEADER (type) = 1;
1561 /* Forward-declare '@interface Protocol'. */
1563 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
1564 objc_declare_class (tree_cons (NULL_TREE, type, NULL_TREE));
1565 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE,
1568 /* Declare type of selector-objects that represent an operation name. */
1570 if (flag_next_runtime)
1571 /* `struct objc_selector *' */
1573 = build_pointer_type (xref_tag (RECORD_TYPE,
1574 get_identifier (TAG_SELECTOR)));
1576 /* `const struct objc_selector *' */
1578 = build_pointer_type
1579 (build_qualified_type (xref_tag (RECORD_TYPE,
1580 get_identifier (TAG_SELECTOR)),
1583 /* Declare receiver type used for dispatching messages to 'super'. */
1585 /* `struct objc_super *' */
1586 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
1587 get_identifier (TAG_SUPER)));
1589 /* Declare pointers to method and ivar lists. */
1590 objc_method_list_ptr = build_pointer_type
1591 (xref_tag (RECORD_TYPE,
1592 get_identifier (UTAG_METHOD_LIST)));
1593 objc_method_proto_list_ptr
1594 = build_pointer_type (xref_tag (RECORD_TYPE,
1595 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
1596 objc_ivar_list_ptr = build_pointer_type
1597 (xref_tag (RECORD_TYPE,
1598 get_identifier (UTAG_IVAR_LIST)));
1600 /* TREE_NOTHROW is cleared for the message-sending functions,
1601 because the function that gets called can throw in Obj-C++, or
1602 could itself call something that can throw even in Obj-C. */
1604 if (flag_next_runtime)
1606 /* NB: In order to call one of the ..._stret (struct-returning)
1607 functions, the function *MUST* first be cast to a signature that
1608 corresponds to the actual ObjC method being invoked. This is
1609 what is done by the build_objc_method_call() routine below. */
1611 /* id objc_msgSend (id, SEL, ...); */
1612 /* id objc_msgSendNonNil (id, SEL, ...); */
1613 /* id objc_msgSend_stret (id, SEL, ...); */
1614 /* id objc_msgSendNonNil_stret (id, SEL, ...); */
1616 = build_function_type (objc_object_type,
1617 tree_cons (NULL_TREE, objc_object_type,
1618 tree_cons (NULL_TREE, objc_selector_type,
1620 umsg_decl = builtin_function (TAG_MSGSEND,
1621 type, 0, NOT_BUILT_IN,
1623 umsg_nonnil_decl = builtin_function (TAG_MSGSEND_NONNIL,
1624 type, 0, NOT_BUILT_IN,
1626 umsg_stret_decl = builtin_function (TAG_MSGSEND_STRET,
1627 type, 0, NOT_BUILT_IN,
1629 umsg_nonnil_stret_decl = builtin_function (TAG_MSGSEND_NONNIL_STRET,
1630 type, 0, NOT_BUILT_IN,
1633 /* These can throw, because the function that gets called can throw
1634 in Obj-C++, or could itself call something that can throw even
1636 TREE_NOTHROW (umsg_decl) = 0;
1637 TREE_NOTHROW (umsg_nonnil_decl) = 0;
1638 TREE_NOTHROW (umsg_stret_decl) = 0;
1639 TREE_NOTHROW (umsg_nonnil_stret_decl) = 0;
1641 /* id objc_msgSend_Fast (id, SEL, ...)
1642 __attribute__ ((hard_coded_address (OFFS_MSGSEND_FAST))); */
1643 #ifdef OFFS_MSGSEND_FAST
1644 umsg_fast_decl = builtin_function (TAG_MSGSEND_FAST,
1645 type, 0, NOT_BUILT_IN,
1647 TREE_NOTHROW (umsg_fast_decl) = 0;
1648 DECL_ATTRIBUTES (umsg_fast_decl)
1649 = tree_cons (get_identifier ("hard_coded_address"),
1650 build_int_cst (NULL_TREE, OFFS_MSGSEND_FAST),
1653 /* No direct dispatch availible. */
1654 umsg_fast_decl = umsg_decl;
1657 /* id objc_msgSendSuper (struct objc_super *, SEL, ...); */
1658 /* id objc_msgSendSuper_stret (struct objc_super *, SEL, ...); */
1660 = build_function_type (objc_object_type,
1661 tree_cons (NULL_TREE, objc_super_type,
1662 tree_cons (NULL_TREE, objc_selector_type,
1664 umsg_super_decl = builtin_function (TAG_MSGSENDSUPER,
1665 type, 0, NOT_BUILT_IN,
1667 umsg_super_stret_decl = builtin_function (TAG_MSGSENDSUPER_STRET,
1668 type, 0, NOT_BUILT_IN, 0,
1670 TREE_NOTHROW (umsg_super_decl) = 0;
1671 TREE_NOTHROW (umsg_super_stret_decl) = 0;
1675 /* GNU runtime messenger entry points. */
1677 /* typedef id (*IMP)(id, SEL, ...); */
1679 = build_pointer_type
1680 (build_function_type (objc_object_type,
1681 tree_cons (NULL_TREE, objc_object_type,
1682 tree_cons (NULL_TREE, objc_selector_type,
1685 /* IMP objc_msg_lookup (id, SEL); */
1687 = build_function_type (IMP_type,
1688 tree_cons (NULL_TREE, objc_object_type,
1689 tree_cons (NULL_TREE, objc_selector_type,
1690 OBJC_VOID_AT_END)));
1691 umsg_decl = builtin_function (TAG_MSGSEND,
1692 type, 0, NOT_BUILT_IN,
1694 TREE_NOTHROW (umsg_decl) = 0;
1696 /* IMP objc_msg_lookup_super (struct objc_super *, SEL); */
1698 = build_function_type (IMP_type,
1699 tree_cons (NULL_TREE, objc_super_type,
1700 tree_cons (NULL_TREE, objc_selector_type,
1701 OBJC_VOID_AT_END)));
1702 umsg_super_decl = builtin_function (TAG_MSGSENDSUPER,
1703 type, 0, NOT_BUILT_IN,
1705 TREE_NOTHROW (umsg_super_decl) = 0;
1707 /* The following GNU runtime entry point is called to initialize
1710 __objc_exec_class (void *); */
1712 = build_function_type (void_type_node,
1713 tree_cons (NULL_TREE, ptr_type_node,
1715 execclass_decl = builtin_function (TAG_EXECCLASS,
1716 type, 0, NOT_BUILT_IN,
1720 /* id objc_getClass (const char *); */
1722 type = build_function_type (objc_object_type,
1723 tree_cons (NULL_TREE,
1724 const_string_type_node,
1728 = builtin_function (TAG_GETCLASS, type, 0, NOT_BUILT_IN,
1731 /* id objc_getMetaClass (const char *); */
1733 objc_get_meta_class_decl
1734 = builtin_function (TAG_GETMETACLASS, type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
1736 build_class_template ();
1737 build_super_template ();
1738 build_protocol_template ();
1739 build_category_template ();
1740 build_objc_exception_stuff ();
1742 if (flag_next_runtime)
1743 build_next_objc_exception_stuff ();
1745 /* static SEL _OBJC_SELECTOR_TABLE[]; */
1747 if (! flag_next_runtime)
1748 build_selector_table_decl ();
1750 /* Forward declare constant_string_id and constant_string_type. */
1751 if (!constant_string_class_name)
1752 constant_string_class_name = default_constant_string_class_name;
1754 constant_string_id = get_identifier (constant_string_class_name);
1755 objc_declare_class (tree_cons (NULL_TREE, constant_string_id, NULL_TREE));
1757 /* Pre-build the following entities - for speed/convenience. */
1758 self_id = get_identifier ("self");
1759 ucmd_id = get_identifier ("_cmd");
1762 pop_lang_context ();
1765 write_symbols = save_write_symbols;
1766 debug_hooks = save_hooks;
1769 /* Ensure that the ivar list for NSConstantString/NXConstantString
1770 (or whatever was specified via `-fconstant-string-class')
1771 contains fields at least as large as the following three, so that
1772 the runtime can stomp on them with confidence:
1774 struct STRING_OBJECT_CLASS_NAME
1778 unsigned int length;
1782 check_string_class_template (void)
1784 tree field_decl = objc_get_class_ivars (constant_string_id);
1786 #define AT_LEAST_AS_LARGE_AS(F, T) \
1787 (F && TREE_CODE (F) == FIELD_DECL \
1788 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
1789 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
1791 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
1794 field_decl = TREE_CHAIN (field_decl);
1795 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
1798 field_decl = TREE_CHAIN (field_decl);
1799 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
1801 #undef AT_LEAST_AS_LARGE_AS
1804 /* Avoid calling `check_string_class_template ()' more than once. */
1805 static GTY(()) int string_layout_checked;
1807 /* Construct an internal string layout to be used as a template for
1808 creating NSConstantString/NXConstantString instances. */
1811 objc_build_internal_const_str_type (void)
1813 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
1814 tree fields = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1815 tree field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1817 TREE_CHAIN (field) = fields; fields = field;
1818 field = build_decl (FIELD_DECL, NULL_TREE, unsigned_type_node);
1819 TREE_CHAIN (field) = fields; fields = field;
1820 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
1822 finish_builtin_struct (type, "__builtin_ObjCString",
1828 /* Custom build_string which sets TREE_TYPE! */
1831 my_build_string (int len, const char *str)
1833 return fix_string_type (build_string (len, str));
1836 /* Build a string with contents STR and length LEN and convert it to a
1840 my_build_string_pointer (int len, const char *str)
1842 tree string = my_build_string (len, str);
1843 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
1844 return build1 (ADDR_EXPR, ptrtype, string);
1848 string_hash (const void *ptr)
1850 tree str = ((struct string_descriptor *)ptr)->literal;
1851 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
1852 int i, len = TREE_STRING_LENGTH (str);
1855 for (i = 0; i < len; i++)
1856 h = ((h * 613) + p[i]);
1862 string_eq (const void *ptr1, const void *ptr2)
1864 tree str1 = ((struct string_descriptor *)ptr1)->literal;
1865 tree str2 = ((struct string_descriptor *)ptr2)->literal;
1866 int len1 = TREE_STRING_LENGTH (str1);
1868 return (len1 == TREE_STRING_LENGTH (str2)
1869 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
1873 /* Given a chain of STRING_CST's, build a static instance of
1874 NXConstantString which points at the concatenation of those
1875 strings. We place the string object in the __string_objects
1876 section of the __OBJC segment. The Objective-C runtime will
1877 initialize the isa pointers of the string objects to point at the
1878 NXConstantString class object. */
1881 objc_build_string_object (tree string)
1883 tree initlist, constructor, constant_string_class;
1886 struct string_descriptor *desc, key;
1889 /* Prep the string argument. */
1890 string = fix_string_type (string);
1891 TREE_SET_CODE (string, STRING_CST);
1892 length = TREE_STRING_LENGTH (string) - 1;
1894 /* Check whether the string class being used actually exists and has the
1895 correct ivar layout. */
1896 if (!string_layout_checked)
1898 string_layout_checked = -1;
1899 constant_string_class = lookup_interface (constant_string_id);
1900 internal_const_str_type = objc_build_internal_const_str_type ();
1902 if (!constant_string_class
1903 || !(constant_string_type
1904 = CLASS_STATIC_TEMPLATE (constant_string_class)))
1905 error ("cannot find interface declaration for %qs",
1906 IDENTIFIER_POINTER (constant_string_id));
1907 /* The NSConstantString/NXConstantString ivar layout is now known. */
1908 else if (!check_string_class_template ())
1909 error ("interface %qs does not have valid constant string layout",
1910 IDENTIFIER_POINTER (constant_string_id));
1911 /* For the NeXT runtime, we can generate a literal reference
1912 to the string class, don't need to run a constructor. */
1913 else if (flag_next_runtime && !setup_string_decl ())
1914 error ("cannot find reference tag for class %qs",
1915 IDENTIFIER_POINTER (constant_string_id));
1918 string_layout_checked = 1; /* Success! */
1919 add_class_reference (constant_string_id);
1923 if (string_layout_checked == -1)
1924 return error_mark_node;
1926 /* Perhaps we already constructed a constant string just like this one? */
1927 key.literal = string;
1928 loc = htab_find_slot (string_htab, &key, INSERT);
1934 *loc = desc = ggc_alloc (sizeof (*desc));
1935 desc->literal = string;
1937 /* GNU: (NXConstantString *) & ((__builtin_ObjCString) { NULL, string, length }) */
1938 /* NeXT: (NSConstantString *) & ((__builtin_ObjCString) { isa, string, length }) */
1939 fields = TYPE_FIELDS (internal_const_str_type);
1941 = build_tree_list (fields,
1943 ? build_unary_op (ADDR_EXPR, string_class_decl, 0)
1944 : build_int_cst (NULL_TREE, 0));
1945 fields = TREE_CHAIN (fields);
1946 initlist = tree_cons (fields, build_unary_op (ADDR_EXPR, string, 1),
1948 fields = TREE_CHAIN (fields);
1949 initlist = tree_cons (fields, build_int_cst (NULL_TREE, length),
1951 constructor = objc_build_constructor (internal_const_str_type,
1952 nreverse (initlist));
1953 TREE_INVARIANT (constructor) = true;
1955 if (!flag_next_runtime)
1957 = objc_add_static_instance (constructor, constant_string_type);
1960 var = build_decl (CONST_DECL, NULL, TREE_TYPE (constructor));
1961 DECL_INITIAL (var) = constructor;
1962 TREE_STATIC (var) = 1;
1963 pushdecl_top_level (var);
1966 desc->constructor = constructor;
1969 addr = convert (build_pointer_type (constant_string_type),
1970 build_unary_op (ADDR_EXPR, desc->constructor, 1));
1975 /* Declare a static instance of CLASS_DECL initialized by CONSTRUCTOR. */
1977 static GTY(()) int num_static_inst;
1980 objc_add_static_instance (tree constructor, tree class_decl)
1985 /* Find the list of static instances for the CLASS_DECL. Create one if
1987 for (chain = &objc_static_instances;
1988 *chain && TREE_VALUE (*chain) != class_decl;
1989 chain = &TREE_CHAIN (*chain));
1992 *chain = tree_cons (NULL_TREE, class_decl, NULL_TREE);
1993 add_objc_string (OBJC_TYPE_NAME (class_decl), class_names);
1996 sprintf (buf, "_OBJC_INSTANCE_%d", num_static_inst++);
1997 decl = build_decl (VAR_DECL, get_identifier (buf), class_decl);
1998 DECL_COMMON (decl) = 1;
1999 TREE_STATIC (decl) = 1;
2000 DECL_ARTIFICIAL (decl) = 1;
2001 DECL_INITIAL (decl) = constructor;
2003 /* We may be writing something else just now.
2004 Postpone till end of input. */
2005 DECL_DEFER_OUTPUT (decl) = 1;
2006 pushdecl_top_level (decl);
2007 rest_of_decl_compilation (decl, 1, 0);
2009 /* Add the DECL to the head of this CLASS' list. */
2010 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE, decl, TREE_PURPOSE (*chain));
2015 /* Build a static constant CONSTRUCTOR
2016 with type TYPE and elements ELTS. */
2019 objc_build_constructor (tree type, tree elts)
2021 tree constructor = build_constructor_from_list (type, elts);
2023 TREE_CONSTANT (constructor) = 1;
2024 TREE_STATIC (constructor) = 1;
2025 TREE_READONLY (constructor) = 1;
2028 /* Adjust for impedance mismatch. We should figure out how to build
2029 CONSTRUCTORs that consistently please both the C and C++ gods. */
2030 if (!TREE_PURPOSE (elts))
2031 TREE_TYPE (constructor) = NULL_TREE;
2032 TREE_HAS_CONSTRUCTOR (constructor) = 1;
2038 /* Take care of defining and initializing _OBJC_SYMBOLS. */
2040 /* Predefine the following data type:
2048 void *defs[cls_def_cnt + cat_def_cnt];
2052 build_objc_symtab_template (void)
2054 tree field_decl, field_decl_chain;
2056 objc_symtab_template
2057 = start_struct (RECORD_TYPE, get_identifier (UTAG_SYMTAB));
2059 /* long sel_ref_cnt; */
2060 field_decl = create_field_decl (long_integer_type_node, "sel_ref_cnt");
2061 field_decl_chain = field_decl;
2064 field_decl = create_field_decl (build_pointer_type (objc_selector_type),
2066 chainon (field_decl_chain, field_decl);
2068 /* short cls_def_cnt; */
2069 field_decl = create_field_decl (short_integer_type_node, "cls_def_cnt");
2070 chainon (field_decl_chain, field_decl);
2072 /* short cat_def_cnt; */
2073 field_decl = create_field_decl (short_integer_type_node,
2075 chainon (field_decl_chain, field_decl);
2077 if (imp_count || cat_count || !flag_next_runtime)
2079 /* void *defs[imp_count + cat_count (+ 1)]; */
2080 /* NB: The index is one less than the size of the array. */
2081 int index = imp_count + cat_count
2082 + (flag_next_runtime? -1: 0);
2083 field_decl = create_field_decl
2086 build_index_type (build_int_cst (NULL_TREE, index))),
2088 chainon (field_decl_chain, field_decl);
2091 finish_struct (objc_symtab_template, field_decl_chain, NULL_TREE);
2094 /* Create the initial value for the `defs' field of _objc_symtab.
2095 This is a CONSTRUCTOR. */
2098 init_def_list (tree type)
2100 tree expr, initlist = NULL_TREE;
2101 struct imp_entry *impent;
2104 for (impent = imp_list; impent; impent = impent->next)
2106 if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
2108 expr = build_unary_op (ADDR_EXPR, impent->class_decl, 0);
2109 initlist = tree_cons (NULL_TREE, expr, initlist);
2114 for (impent = imp_list; impent; impent = impent->next)
2116 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
2118 expr = build_unary_op (ADDR_EXPR, impent->class_decl, 0);
2119 initlist = tree_cons (NULL_TREE, expr, initlist);
2123 if (!flag_next_runtime)
2125 /* statics = { ..., _OBJC_STATIC_INSTANCES, ... } */
2128 if (static_instances_decl)
2129 expr = build_unary_op (ADDR_EXPR, static_instances_decl, 0);
2131 expr = build_int_cst (NULL_TREE, 0);
2133 initlist = tree_cons (NULL_TREE, expr, initlist);
2136 return objc_build_constructor (type, nreverse (initlist));
2139 /* Construct the initial value for all of _objc_symtab. */
2142 init_objc_symtab (tree type)
2146 /* sel_ref_cnt = { ..., 5, ... } */
2148 initlist = build_tree_list (NULL_TREE,
2149 build_int_cst (long_integer_type_node, 0));
2151 /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
2153 if (flag_next_runtime || ! sel_ref_chain)
2154 initlist = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, 0), initlist);
2157 = tree_cons (NULL_TREE,
2158 convert (build_pointer_type (objc_selector_type),
2159 build_unary_op (ADDR_EXPR,
2160 UOBJC_SELECTOR_TABLE_decl, 1)),
2163 /* cls_def_cnt = { ..., 5, ... } */
2165 initlist = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, imp_count), initlist);
2167 /* cat_def_cnt = { ..., 5, ... } */
2169 initlist = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, cat_count), initlist);
2171 /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
2173 if (imp_count || cat_count || !flag_next_runtime)
2176 tree field = TYPE_FIELDS (type);
2177 field = TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (field))));
2179 initlist = tree_cons (NULL_TREE, init_def_list (TREE_TYPE (field)),
2183 return objc_build_constructor (type, nreverse (initlist));
2186 /* Generate forward declarations for metadata such as
2187 'OBJC_CLASS_...'. */
2190 build_metadata_decl (const char *name, tree type)
2194 /* struct TYPE NAME_<name>; */
2195 decl = start_var_decl (type, synth_id_with_class_suffix
2197 objc_implementation_context));
2202 /* Push forward-declarations of all the categories so that
2203 init_def_list can use them in a CONSTRUCTOR. */
2206 forward_declare_categories (void)
2208 struct imp_entry *impent;
2209 tree sav = objc_implementation_context;
2211 for (impent = imp_list; impent; impent = impent->next)
2213 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
2215 /* Set an invisible arg to synth_id_with_class_suffix. */
2216 objc_implementation_context = impent->imp_context;
2217 /* extern struct objc_category _OBJC_CATEGORY_<name>; */
2218 impent->class_decl = build_metadata_decl ("_OBJC_CATEGORY",
2219 objc_category_template);
2222 objc_implementation_context = sav;
2225 /* Create the declaration of _OBJC_SYMBOLS, with type `struct _objc_symtab'
2226 and initialized appropriately. */
2229 generate_objc_symtab_decl (void)
2231 /* forward declare categories */
2233 forward_declare_categories ();
2235 build_objc_symtab_template ();
2236 UOBJC_SYMBOLS_decl = start_var_decl (objc_symtab_template, "_OBJC_SYMBOLS");
2237 finish_var_decl (UOBJC_SYMBOLS_decl,
2238 init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl)));
2242 init_module_descriptor (tree type)
2244 tree initlist, expr;
2246 /* version = { 1, ... } */
2248 expr = build_int_cst (long_integer_type_node, OBJC_VERSION);
2249 initlist = build_tree_list (NULL_TREE, expr);
2251 /* size = { ..., sizeof (struct _objc_module), ... } */
2253 expr = convert (long_integer_type_node,
2254 size_in_bytes (objc_module_template));
2255 initlist = tree_cons (NULL_TREE, expr, initlist);
2257 /* name = { ..., "foo.m", ... } */
2259 expr = add_objc_string (get_identifier (input_filename), class_names);
2260 initlist = tree_cons (NULL_TREE, expr, initlist);
2262 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
2264 if (UOBJC_SYMBOLS_decl)
2265 expr = build_unary_op (ADDR_EXPR, UOBJC_SYMBOLS_decl, 0);
2267 expr = build_int_cst (NULL_TREE, 0);
2268 initlist = tree_cons (NULL_TREE, expr, initlist);
2270 return objc_build_constructor (type, nreverse (initlist));
2273 /* Write out the data structures to describe Objective C classes defined.
2275 struct _objc_module { ... } _OBJC_MODULE = { ... }; */
2278 build_module_descriptor (void)
2280 tree field_decl, field_decl_chain;
2283 push_lang_context (lang_name_c); /* extern "C" */
2286 objc_module_template
2287 = start_struct (RECORD_TYPE, get_identifier (UTAG_MODULE));
2290 field_decl = create_field_decl (long_integer_type_node, "version");
2291 field_decl_chain = field_decl;
2294 field_decl = create_field_decl (long_integer_type_node, "size");
2295 chainon (field_decl_chain, field_decl);
2298 field_decl = create_field_decl (string_type_node, "name");
2299 chainon (field_decl_chain, field_decl);
2301 /* struct _objc_symtab *symtab; */
2303 = create_field_decl (build_pointer_type
2304 (xref_tag (RECORD_TYPE,
2305 get_identifier (UTAG_SYMTAB))),
2307 chainon (field_decl_chain, field_decl);
2309 finish_struct (objc_module_template, field_decl_chain, NULL_TREE);
2311 /* Create an instance of "_objc_module". */
2312 UOBJC_MODULES_decl = start_var_decl (objc_module_template, "_OBJC_MODULES");
2313 finish_var_decl (UOBJC_MODULES_decl,
2314 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl)));
2317 pop_lang_context ();
2321 /* The GNU runtime requires us to provide a static initializer function
2324 static void __objc_gnu_init (void) {
2325 __objc_exec_class (&L_OBJC_MODULES);
2329 build_module_initializer_routine (void)
2334 push_lang_context (lang_name_c); /* extern "C" */
2337 objc_push_parm (build_decl (PARM_DECL, NULL_TREE, void_type_node));
2338 objc_start_function (get_identifier (TAG_GNUINIT),
2339 build_function_type (void_type_node,
2341 NULL_TREE, objc_get_parm_info (0));
2343 body = c_begin_compound_stmt (true);
2344 add_stmt (build_function_call
2348 build_unary_op (ADDR_EXPR,
2349 UOBJC_MODULES_decl, 0))));
2350 add_stmt (c_end_compound_stmt (body, true));
2352 TREE_PUBLIC (current_function_decl) = 0;
2355 /* For Objective-C++, we will need to call __objc_gnu_init
2356 from objc_generate_static_init_call() below. */
2357 DECL_STATIC_CONSTRUCTOR (current_function_decl) = 1;
2360 GNU_INIT_decl = current_function_decl;
2364 pop_lang_context ();
2369 /* Return 1 if the __objc_gnu_init function has been synthesized and needs
2370 to be called by the module initializer routine. */
2373 objc_static_init_needed_p (void)
2375 return (GNU_INIT_decl != NULL_TREE);
2378 /* Generate a call to the __objc_gnu_init initializer function. */
2381 objc_generate_static_init_call (tree ctors ATTRIBUTE_UNUSED)
2383 add_stmt (build_stmt (EXPR_STMT,
2384 build_function_call (GNU_INIT_decl, NULL_TREE)));
2388 #endif /* OBJCPLUS */
2390 /* Return the DECL of the string IDENT in the SECTION. */
2393 get_objc_string_decl (tree ident, enum string_section section)
2397 if (section == class_names)
2398 chain = class_names_chain;
2399 else if (section == meth_var_names)
2400 chain = meth_var_names_chain;
2401 else if (section == meth_var_types)
2402 chain = meth_var_types_chain;
2406 for (; chain != 0; chain = TREE_CHAIN (chain))
2407 if (TREE_VALUE (chain) == ident)
2408 return (TREE_PURPOSE (chain));
2414 /* Output references to all statically allocated objects. Return the DECL
2415 for the array built. */
2418 generate_static_references (void)
2420 tree decls = NULL_TREE, expr = NULL_TREE;
2421 tree class_name, class, decl, initlist;
2422 tree cl_chain, in_chain, type
2423 = build_array_type (build_pointer_type (void_type_node), NULL_TREE);
2424 int num_inst, num_class;
2427 if (flag_next_runtime)
2430 for (cl_chain = objc_static_instances, num_class = 0;
2431 cl_chain; cl_chain = TREE_CHAIN (cl_chain), num_class++)
2433 for (num_inst = 0, in_chain = TREE_PURPOSE (cl_chain);
2434 in_chain; num_inst++, in_chain = TREE_CHAIN (in_chain));
2436 sprintf (buf, "_OBJC_STATIC_INSTANCES_%d", num_class);
2437 decl = start_var_decl (type, buf);
2439 /* Output {class_name, ...}. */
2440 class = TREE_VALUE (cl_chain);
2441 class_name = get_objc_string_decl (OBJC_TYPE_NAME (class), class_names);
2442 initlist = build_tree_list (NULL_TREE,
2443 build_unary_op (ADDR_EXPR, class_name, 1));
2445 /* Output {..., instance, ...}. */
2446 for (in_chain = TREE_PURPOSE (cl_chain);
2447 in_chain; in_chain = TREE_CHAIN (in_chain))
2449 expr = build_unary_op (ADDR_EXPR, TREE_VALUE (in_chain), 1);
2450 initlist = tree_cons (NULL_TREE, expr, initlist);
2453 /* Output {..., NULL}. */
2454 initlist = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, 0), initlist);
2456 expr = objc_build_constructor (TREE_TYPE (decl), nreverse (initlist));
2457 finish_var_decl (decl, expr);
2459 = tree_cons (NULL_TREE, build_unary_op (ADDR_EXPR, decl, 1), decls);
2462 decls = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, 0), decls);
2463 expr = objc_build_constructor (type, nreverse (decls));
2464 static_instances_decl = start_var_decl (type, "_OBJC_STATIC_INSTANCES");
2465 finish_var_decl (static_instances_decl, expr);
2468 static GTY(()) int selector_reference_idx;
2471 build_selector_reference_decl (void)
2476 sprintf (buf, "_OBJC_SELECTOR_REFERENCES_%d", selector_reference_idx++);
2477 decl = start_var_decl (objc_selector_type, buf);
2483 build_selector_table_decl (void)
2487 if (flag_typed_selectors)
2489 build_selector_template ();
2490 temp = build_array_type (objc_selector_template, NULL_TREE);
2493 temp = build_array_type (objc_selector_type, NULL_TREE);
2495 UOBJC_SELECTOR_TABLE_decl = start_var_decl (temp, "_OBJC_SELECTOR_TABLE");
2498 /* Just a handy wrapper for add_objc_string. */
2501 build_selector (tree ident)
2503 return convert (objc_selector_type,
2504 add_objc_string (ident, meth_var_names));
2508 build_selector_translation_table (void)
2510 tree chain, initlist = NULL_TREE;
2512 tree decl = NULL_TREE;
2514 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
2518 if (warn_selector && objc_implementation_context)
2522 for (method_chain = meth_var_names_chain;
2524 method_chain = TREE_CHAIN (method_chain))
2526 if (TREE_VALUE (method_chain) == TREE_VALUE (chain))
2535 if (flag_next_runtime && TREE_PURPOSE (chain))
2536 loc = &DECL_SOURCE_LOCATION (TREE_PURPOSE (chain));
2538 loc = &input_location;
2539 warning (0, "%Hcreating selector for nonexistent method %qE",
2540 loc, TREE_VALUE (chain));
2544 expr = build_selector (TREE_VALUE (chain));
2545 /* add one for the '\0' character */
2546 offset += IDENTIFIER_LENGTH (TREE_VALUE (chain)) + 1;
2548 if (flag_next_runtime)
2550 decl = TREE_PURPOSE (chain);
2551 finish_var_decl (decl, expr);
2555 if (flag_typed_selectors)
2557 tree eltlist = NULL_TREE;
2558 tree encoding = get_proto_encoding (TREE_PURPOSE (chain));
2559 eltlist = tree_cons (NULL_TREE, expr, NULL_TREE);
2560 eltlist = tree_cons (NULL_TREE, encoding, eltlist);
2561 expr = objc_build_constructor (objc_selector_template,
2562 nreverse (eltlist));
2565 initlist = tree_cons (NULL_TREE, expr, initlist);
2569 if (! flag_next_runtime)
2571 /* Cause the selector table (previously forward-declared)
2572 to be actually output. */
2573 initlist = tree_cons (NULL_TREE,
2574 flag_typed_selectors
2575 ? objc_build_constructor
2576 (objc_selector_template,
2577 tree_cons (NULL_TREE,
2578 build_int_cst (NULL_TREE, 0),
2579 tree_cons (NULL_TREE,
2580 build_int_cst (NULL_TREE, 0),
2582 : build_int_cst (NULL_TREE, 0), initlist);
2583 initlist = objc_build_constructor (TREE_TYPE (UOBJC_SELECTOR_TABLE_decl),
2584 nreverse (initlist));
2585 finish_var_decl (UOBJC_SELECTOR_TABLE_decl, initlist);
2590 get_proto_encoding (tree proto)
2595 if (! METHOD_ENCODING (proto))
2597 encoding = encode_method_prototype (proto);
2598 METHOD_ENCODING (proto) = encoding;
2601 encoding = METHOD_ENCODING (proto);
2603 return add_objc_string (encoding, meth_var_types);
2606 return build_int_cst (NULL_TREE, 0);
2609 /* sel_ref_chain is a list whose "value" fields will be instances of
2610 identifier_node that represent the selector. */
2613 build_typed_selector_reference (tree ident, tree prototype)
2615 tree *chain = &sel_ref_chain;
2621 if (TREE_PURPOSE (*chain) == prototype && TREE_VALUE (*chain) == ident)
2622 goto return_at_index;
2625 chain = &TREE_CHAIN (*chain);
2628 *chain = tree_cons (prototype, ident, NULL_TREE);
2631 expr = build_unary_op (ADDR_EXPR,
2632 build_array_ref (UOBJC_SELECTOR_TABLE_decl,
2633 build_int_cst (NULL_TREE, index)),
2635 return convert (objc_selector_type, expr);
2639 build_selector_reference (tree ident)
2641 tree *chain = &sel_ref_chain;
2647 if (TREE_VALUE (*chain) == ident)
2648 return (flag_next_runtime
2649 ? TREE_PURPOSE (*chain)
2650 : build_array_ref (UOBJC_SELECTOR_TABLE_decl,
2651 build_int_cst (NULL_TREE, index)));
2654 chain = &TREE_CHAIN (*chain);
2657 expr = (flag_next_runtime ? build_selector_reference_decl (): NULL_TREE);
2659 *chain = tree_cons (expr, ident, NULL_TREE);
2661 return (flag_next_runtime
2663 : build_array_ref (UOBJC_SELECTOR_TABLE_decl,
2664 build_int_cst (NULL_TREE, index)));
2667 static GTY(()) int class_reference_idx;
2670 build_class_reference_decl (void)
2675 sprintf (buf, "_OBJC_CLASS_REFERENCES_%d", class_reference_idx++);
2676 decl = start_var_decl (objc_class_type, buf);
2681 /* Create a class reference, but don't create a variable to reference
2685 add_class_reference (tree ident)
2689 if ((chain = cls_ref_chain))
2694 if (ident == TREE_VALUE (chain))
2698 chain = TREE_CHAIN (chain);
2702 /* Append to the end of the list */
2703 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
2706 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
2709 /* Get a class reference, creating it if necessary. Also create the
2710 reference variable. */
2713 objc_get_class_reference (tree ident)
2715 tree orig_ident = (DECL_P (ident)
2718 ? OBJC_TYPE_NAME (ident)
2720 bool local_scope = false;
2723 if (processing_template_decl)
2724 /* Must wait until template instantiation time. */
2725 return build_min_nt (CLASS_REFERENCE_EXPR, ident);
2728 if (TREE_CODE (ident) == TYPE_DECL)
2729 ident = (DECL_ORIGINAL_TYPE (ident)
2730 ? DECL_ORIGINAL_TYPE (ident)
2731 : TREE_TYPE (ident));
2734 if (TYPE_P (ident) && TYPE_CONTEXT (ident)
2735 && TYPE_CONTEXT (ident) != global_namespace)
2739 if (local_scope || !(ident = objc_is_class_name (ident)))
2741 error ("%qs is not an Objective-C class name or alias",
2742 IDENTIFIER_POINTER (orig_ident));
2743 return error_mark_node;
2746 if (flag_next_runtime && !flag_zero_link)
2751 for (chain = &cls_ref_chain; *chain; chain = &TREE_CHAIN (*chain))
2752 if (TREE_VALUE (*chain) == ident)
2754 if (! TREE_PURPOSE (*chain))
2755 TREE_PURPOSE (*chain) = build_class_reference_decl ();
2757 return TREE_PURPOSE (*chain);
2760 decl = build_class_reference_decl ();
2761 *chain = tree_cons (decl, ident, NULL_TREE);
2768 add_class_reference (ident);
2770 params = build_tree_list (NULL_TREE,
2771 my_build_string_pointer
2772 (IDENTIFIER_LENGTH (ident) + 1,
2773 IDENTIFIER_POINTER (ident)));
2775 assemble_external (objc_get_class_decl);
2776 return build_function_call (objc_get_class_decl, params);
2780 /* For each string section we have a chain which maps identifier nodes
2781 to decls for the strings. */
2784 add_objc_string (tree ident, enum string_section section)
2786 tree *chain, decl, type, string_expr;
2788 if (section == class_names)
2789 chain = &class_names_chain;
2790 else if (section == meth_var_names)
2791 chain = &meth_var_names_chain;
2792 else if (section == meth_var_types)
2793 chain = &meth_var_types_chain;
2799 if (TREE_VALUE (*chain) == ident)
2800 return convert (string_type_node,
2801 build_unary_op (ADDR_EXPR, TREE_PURPOSE (*chain), 1));
2803 chain = &TREE_CHAIN (*chain);
2806 decl = build_objc_string_decl (section);
2808 type = build_array_type
2811 (build_int_cst (NULL_TREE,
2812 IDENTIFIER_LENGTH (ident))));
2813 decl = start_var_decl (type, IDENTIFIER_POINTER (DECL_NAME (decl)));
2814 string_expr = my_build_string (IDENTIFIER_LENGTH (ident) + 1,
2815 IDENTIFIER_POINTER (ident));
2816 finish_var_decl (decl, string_expr);
2818 *chain = tree_cons (decl, ident, NULL_TREE);
2820 return convert (string_type_node, build_unary_op (ADDR_EXPR, decl, 1));
2823 static GTY(()) int class_names_idx;
2824 static GTY(()) int meth_var_names_idx;
2825 static GTY(()) int meth_var_types_idx;
2828 build_objc_string_decl (enum string_section section)
2833 if (section == class_names)
2834 sprintf (buf, "_OBJC_CLASS_NAME_%d", class_names_idx++);
2835 else if (section == meth_var_names)
2836 sprintf (buf, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx++);
2837 else if (section == meth_var_types)
2838 sprintf (buf, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx++);
2840 ident = get_identifier (buf);
2842 decl = build_decl (VAR_DECL, ident, build_array_type (char_type_node, 0));
2843 DECL_EXTERNAL (decl) = 1;
2844 TREE_PUBLIC (decl) = 0;
2845 TREE_USED (decl) = 1;
2846 TREE_CONSTANT (decl) = 1;
2847 DECL_CONTEXT (decl) = 0;
2848 DECL_ARTIFICIAL (decl) = 1;
2850 DECL_THIS_STATIC (decl) = 1; /* squash redeclaration errors */
2853 make_decl_rtl (decl);
2854 pushdecl_top_level (decl);
2861 objc_declare_alias (tree alias_ident, tree class_ident)
2863 tree underlying_class;
2866 if (current_namespace != global_namespace) {
2867 error ("Objective-C declarations may only appear in global scope");
2869 #endif /* OBJCPLUS */
2871 if (!(underlying_class = objc_is_class_name (class_ident)))
2872 warning (0, "cannot find class %qs", IDENTIFIER_POINTER (class_ident));
2873 else if (objc_is_class_name (alias_ident))
2874 warning (0, "class %qs already exists", IDENTIFIER_POINTER (alias_ident));
2877 /* Implement @compatibility_alias as a typedef. */
2879 push_lang_context (lang_name_c); /* extern "C" */
2881 lang_hooks.decls.pushdecl (build_decl
2884 xref_tag (RECORD_TYPE, underlying_class)));
2886 pop_lang_context ();
2888 alias_chain = tree_cons (underlying_class, alias_ident, alias_chain);
2893 objc_declare_class (tree ident_list)
2897 if (current_namespace != global_namespace) {
2898 error ("Objective-C declarations may only appear in global scope");
2900 #endif /* OBJCPLUS */
2902 for (list = ident_list; list; list = TREE_CHAIN (list))
2904 tree ident = TREE_VALUE (list);
2906 if (! objc_is_class_name (ident))
2908 tree record = lookup_name (ident), type = record;
2912 if (TREE_CODE (record) == TYPE_DECL)
2913 type = DECL_ORIGINAL_TYPE (record);
2915 if (!TYPE_HAS_OBJC_INFO (type)
2916 || !TYPE_OBJC_INTERFACE (type))
2918 error ("%qs redeclared as different kind of symbol",
2919 IDENTIFIER_POINTER (ident));
2920 error ("previous declaration of %q+D",
2925 record = xref_tag (RECORD_TYPE, ident);
2926 INIT_TYPE_OBJC_INFO (record);
2927 TYPE_OBJC_INTERFACE (record) = ident;
2928 class_chain = tree_cons (NULL_TREE, ident, class_chain);
2934 objc_is_class_name (tree ident)
2938 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE
2939 && identifier_global_value (ident))
2940 ident = identifier_global_value (ident);
2941 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
2942 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
2944 if (ident && TREE_CODE (ident) == RECORD_TYPE)
2945 ident = OBJC_TYPE_NAME (ident);
2947 if (ident && TREE_CODE (ident) == TYPE_DECL)
2948 ident = DECL_NAME (ident);
2950 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
2953 if (lookup_interface (ident))
2956 for (chain = class_chain; chain; chain = TREE_CHAIN (chain))
2958 if (ident == TREE_VALUE (chain))
2962 for (chain = alias_chain; chain; chain = TREE_CHAIN (chain))
2964 if (ident == TREE_VALUE (chain))
2965 return TREE_PURPOSE (chain);
2971 /* Check whether TYPE is either 'id' or 'Class'. */
2974 objc_is_id (tree type)
2976 if (type && TREE_CODE (type) == IDENTIFIER_NODE
2977 && identifier_global_value (type))
2978 type = identifier_global_value (type);
2980 if (type && TREE_CODE (type) == TYPE_DECL)
2981 type = TREE_TYPE (type);
2983 /* NB: This function may be called before the ObjC front-end has
2984 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
2985 return (objc_object_type && type
2986 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
2991 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
2992 class instance. This is needed by other parts of the compiler to
2993 handle ObjC types gracefully. */
2996 objc_is_object_ptr (tree type)
3000 type = TYPE_MAIN_VARIANT (type);
3001 if (!POINTER_TYPE_P (type))
3004 ret = objc_is_id (type);
3006 ret = objc_is_class_name (TREE_TYPE (type));
3012 objc_is_gcable_type (tree type, int or_strong_p)
3018 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
3020 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
3022 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
3024 type = TREE_TYPE (type);
3025 if (TREE_CODE (type) != RECORD_TYPE)
3027 name = TYPE_NAME (type);
3028 return (objc_is_class_name (name) != NULL_TREE);
3032 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
3034 if (expr == oldexpr)
3037 switch (TREE_CODE (expr))
3040 return objc_build_component_ref
3041 (objc_substitute_decl (TREE_OPERAND (expr, 0),
3044 DECL_NAME (TREE_OPERAND (expr, 1)));
3046 return build_array_ref (objc_substitute_decl (TREE_OPERAND (expr, 0),
3049 TREE_OPERAND (expr, 1));
3051 return build_indirect_ref (objc_substitute_decl (TREE_OPERAND (expr, 0),
3060 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
3063 /* The LHS parameter contains the expression 'outervar->memberspec';
3064 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3065 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3068 = objc_substitute_decl
3069 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
3071 = (flag_objc_direct_dispatch
3072 ? objc_assign_ivar_fast_decl
3073 : objc_assign_ivar_decl);
3075 offs = convert (integer_type_node, build_unary_op (ADDR_EXPR, offs, 0));
3077 func_params = tree_cons (NULL_TREE,
3078 convert (objc_object_type, rhs),
3079 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
3080 tree_cons (NULL_TREE, offs,
3083 assemble_external (func);
3084 return build_function_call (func, func_params);
3088 objc_build_global_assignment (tree lhs, tree rhs)
3090 tree func_params = tree_cons (NULL_TREE,
3091 convert (objc_object_type, rhs),
3092 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3093 build_unary_op (ADDR_EXPR, lhs, 0)),
3096 assemble_external (objc_assign_global_decl);
3097 return build_function_call (objc_assign_global_decl, func_params);
3101 objc_build_strong_cast_assignment (tree lhs, tree rhs)
3103 tree func_params = tree_cons (NULL_TREE,
3104 convert (objc_object_type, rhs),
3105 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3106 build_unary_op (ADDR_EXPR, lhs, 0)),
3109 assemble_external (objc_assign_strong_cast_decl);
3110 return build_function_call (objc_assign_strong_cast_decl, func_params);
3114 objc_is_gcable_p (tree expr)
3116 return (TREE_CODE (expr) == COMPONENT_REF
3117 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
3118 : TREE_CODE (expr) == ARRAY_REF
3119 ? (objc_is_gcable_p (TREE_TYPE (expr))
3120 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
3121 : TREE_CODE (expr) == ARRAY_TYPE
3122 ? objc_is_gcable_p (TREE_TYPE (expr))
3124 ? objc_is_gcable_type (expr, 1)
3125 : (objc_is_gcable_p (TREE_TYPE (expr))
3127 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
3131 objc_is_ivar_reference_p (tree expr)
3133 return (TREE_CODE (expr) == ARRAY_REF
3134 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
3135 : TREE_CODE (expr) == COMPONENT_REF
3136 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
3141 objc_is_global_reference_p (tree expr)
3143 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
3144 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
3146 ? (!DECL_CONTEXT (expr) || TREE_STATIC (expr))
3151 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
3153 tree result = NULL_TREE, outer;
3154 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
3156 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3157 will have been transformed to the form '*(type *)&expr'. */
3158 if (TREE_CODE (lhs) == INDIRECT_REF)
3160 outer = TREE_OPERAND (lhs, 0);
3162 while (!strong_cast_p
3163 && (TREE_CODE (outer) == CONVERT_EXPR
3164 || TREE_CODE (outer) == NOP_EXPR
3165 || TREE_CODE (outer) == NON_LVALUE_EXPR))
3167 tree lhstype = TREE_TYPE (outer);
3169 /* Descend down the cast chain, and record the first objc_gc
3171 if (POINTER_TYPE_P (lhstype))
3174 = lookup_attribute ("objc_gc",
3175 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
3181 outer = TREE_OPERAND (outer, 0);
3185 /* If we have a __strong cast, it trumps all else. */
3188 if (modifycode != NOP_EXPR)
3189 goto invalid_pointer_arithmetic;
3191 if (warn_assign_intercept)
3192 warning (0, "strong-cast assignment has been intercepted");
3194 result = objc_build_strong_cast_assignment (lhs, rhs);
3199 /* the lhs must be of a suitable type, regardless of its underlying
3201 if (!objc_is_gcable_p (lhs))
3207 && (TREE_CODE (outer) == COMPONENT_REF
3208 || TREE_CODE (outer) == ARRAY_REF))
3209 outer = TREE_OPERAND (outer, 0);
3211 if (TREE_CODE (outer) == INDIRECT_REF)
3213 outer = TREE_OPERAND (outer, 0);
3217 outer_gc_p = objc_is_gcable_p (outer);
3219 /* Handle ivar assignments. */
3220 if (objc_is_ivar_reference_p (lhs))
3222 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3223 doesn't cut it here), the best we can do here is suggest a cast. */
3224 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
3226 /* We may still be able to use the global write barrier... */
3227 if (!indirect_p && objc_is_global_reference_p (outer))
3228 goto global_reference;
3231 if (modifycode == NOP_EXPR)
3233 if (warn_assign_intercept)
3234 warning (0, "strong-cast may possibly be needed");
3240 if (modifycode != NOP_EXPR)
3241 goto invalid_pointer_arithmetic;
3243 if (warn_assign_intercept)
3244 warning (0, "instance variable assignment has been intercepted");
3246 result = objc_build_ivar_assignment (outer, lhs, rhs);
3251 /* Likewise, intercept assignment to global/static variables if their type is
3253 if (objc_is_global_reference_p (outer))
3259 if (modifycode != NOP_EXPR)
3261 invalid_pointer_arithmetic:
3263 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3268 if (warn_assign_intercept)
3269 warning (0, "global/static variable assignment has been intercepted");
3271 result = objc_build_global_assignment (lhs, rhs);
3274 /* In all other cases, fall back to the normal mechanism. */
3279 struct interface_tuple GTY(())
3285 static GTY ((param_is (struct interface_tuple))) htab_t interface_htab;
3288 hash_interface (const void *p)
3290 const struct interface_tuple *d = p;
3291 return htab_hash_pointer (d->id);
3295 eq_interface (const void *p1, const void *p2)
3297 const struct interface_tuple *d = p1;
3302 lookup_interface (tree ident)
3305 if (ident && TREE_CODE (ident) == TYPE_DECL)
3306 ident = DECL_NAME (ident);
3309 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
3313 struct interface_tuple **slot;
3318 slot = (struct interface_tuple **)
3319 htab_find_slot_with_hash (interface_htab, ident,
3320 htab_hash_pointer (ident),
3323 i = (*slot)->class_name;
3329 /* Implement @defs (<classname>) within struct bodies. */
3332 objc_get_class_ivars (tree class_name)
3334 tree interface = lookup_interface (class_name);
3337 return get_class_ivars (interface, true);
3339 error ("cannot find interface declaration for %qs",
3340 IDENTIFIER_POINTER (class_name));
3342 return error_mark_node;
3345 /* Used by: build_private_template, continue_class,
3346 and for @defs constructs. */
3349 get_class_ivars (tree interface, bool inherited)
3351 tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface));
3353 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
3354 by the current class (i.e., they do not include super-class ivars).
3355 However, the CLASS_IVARS list will be side-effected by a call to
3356 finish_struct(), which will fill in field offsets. */
3357 if (!CLASS_IVARS (interface))
3358 CLASS_IVARS (interface) = ivar_chain;
3363 while (CLASS_SUPER_NAME (interface))
3365 /* Prepend super-class ivars. */
3366 interface = lookup_interface (CLASS_SUPER_NAME (interface));
3367 ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)),
3375 objc_create_temporary_var (tree type)
3379 decl = build_decl (VAR_DECL, NULL_TREE, type);
3380 TREE_USED (decl) = 1;
3381 DECL_ARTIFICIAL (decl) = 1;
3382 DECL_IGNORED_P (decl) = 1;
3383 DECL_CONTEXT (decl) = current_function_decl;
3388 /* Exception handling constructs. We begin by having the parser do most
3389 of the work and passing us blocks. What we do next depends on whether
3390 we're doing "native" exception handling or legacy Darwin setjmp exceptions.
3391 We abstract all of this in a handful of appropriately named routines. */
3393 /* Stack of open try blocks. */
3395 struct objc_try_context
3397 struct objc_try_context *outer;
3399 /* Statements (or statement lists) as processed by the parser. */
3403 /* Some file position locations. */
3404 location_t try_locus;
3405 location_t end_try_locus;
3406 location_t end_catch_locus;
3407 location_t finally_locus;
3408 location_t end_finally_locus;
3410 /* A STATEMENT_LIST of CATCH_EXPRs, appropriate for sticking into op1
3411 of a TRY_CATCH_EXPR. Even when doing Darwin setjmp. */
3414 /* The CATCH_EXPR of an open @catch clause. */
3417 /* The VAR_DECL holding the Darwin equivalent of EXC_PTR_EXPR. */
3423 static struct objc_try_context *cur_try_context;
3425 /* This hook, called via lang_eh_runtime_type, generates a runtime object
3426 that represents TYPE. For Objective-C, this is just the class name. */
3427 /* ??? Isn't there a class object or some such? Is it easy to get? */
3431 objc_eh_runtime_type (tree type)
3433 return add_objc_string (OBJC_TYPE_NAME (TREE_TYPE (type)), class_names);
3437 /* Initialize exception handling. */
3440 objc_init_exceptions (void)
3442 static bool done = false;
3447 if (flag_objc_sjlj_exceptions)
3449 /* On Darwin, ObjC exceptions require a sufficiently recent
3450 version of the runtime, so the user must ask for them explicitly. */
3451 if (!flag_objc_exceptions)
3452 warning (0, "use %<-fobjc-exceptions%> to enable Objective-C "
3453 "exception syntax");
3458 c_eh_initialized_p = true;
3459 eh_personality_libfunc
3460 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3461 ? "__gnu_objc_personality_sj0"
3462 : "__gnu_objc_personality_v0");
3463 default_init_unwind_resume_libfunc ();
3464 using_eh_for_cleanups ();
3465 lang_eh_runtime_type = objc_eh_runtime_type;
3470 /* Build an EXC_PTR_EXPR, or the moral equivalent. In the case of Darwin,
3471 we'll arrange for it to be initialized (and associated with a binding)
3475 objc_build_exc_ptr (void)
3477 if (flag_objc_sjlj_exceptions)
3479 tree var = cur_try_context->caught_decl;
3482 var = objc_create_temporary_var (objc_object_type);
3483 cur_try_context->caught_decl = var;
3488 return build (EXC_PTR_EXPR, objc_object_type);
3491 /* Build "objc_exception_try_exit(&_stack)". */
3494 next_sjlj_build_try_exit (void)
3497 t = build_fold_addr_expr (cur_try_context->stack_decl);
3498 t = tree_cons (NULL, t, NULL);
3499 t = build_function_call (objc_exception_try_exit_decl, t);
3504 objc_exception_try_enter (&_stack);
3505 if (_setjmp(&_stack.buf))
3509 Return the COND_EXPR. Note that the THEN and ELSE fields are left
3510 empty, ready for the caller to fill them in. */
3513 next_sjlj_build_enter_and_setjmp (void)
3515 tree t, enter, sj, cond;
3517 t = build_fold_addr_expr (cur_try_context->stack_decl);
3518 t = tree_cons (NULL, t, NULL);
3519 enter = build_function_call (objc_exception_try_enter_decl, t);
3521 t = objc_build_component_ref (cur_try_context->stack_decl,
3522 get_identifier ("buf"));
3523 t = build_fold_addr_expr (t);
3525 /* Convert _setjmp argument to type that is expected. */
3526 if (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl)))
3527 t = convert (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl))), t);
3529 t = convert (ptr_type_node, t);
3531 t = convert (ptr_type_node, t);
3533 t = tree_cons (NULL, t, NULL);
3534 sj = build_function_call (objc_setjmp_decl, t);
3536 cond = build (COMPOUND_EXPR, TREE_TYPE (sj), enter, sj);
3537 cond = c_common_truthvalue_conversion (cond);
3539 return build (COND_EXPR, void_type_node, cond, NULL, NULL);
3543 DECL = objc_exception_extract(&_stack);
3547 next_sjlj_build_exc_extract (tree decl)
3551 t = build_fold_addr_expr (cur_try_context->stack_decl);
3552 t = tree_cons (NULL, t, NULL);
3553 t = build_function_call (objc_exception_extract_decl, t);
3554 t = convert (TREE_TYPE (decl), t);
3555 t = build (MODIFY_EXPR, void_type_node, decl, t);
3561 if (objc_exception_match(obj_get_class(TYPE), _caught)
3568 objc_exception_try_exit(&_stack);
3570 from the sequence of CATCH_EXPRs in the current try context. */
3573 next_sjlj_build_catch_list (void)
3575 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
3577 tree *last = &catch_seq;
3578 bool saw_id = false;
3580 for (; !tsi_end_p (i); tsi_next (&i))
3582 tree stmt = tsi_stmt (i);
3583 tree type = CATCH_TYPES (stmt);
3584 tree body = CATCH_BODY (stmt);
3596 if (type == error_mark_node)
3597 cond = error_mark_node;
3600 args = tree_cons (NULL, cur_try_context->caught_decl, NULL);
3601 t = objc_get_class_reference (OBJC_TYPE_NAME (TREE_TYPE (type)));
3602 args = tree_cons (NULL, t, args);