1 /* Implement classes and message passing for Objective C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 Contributed by Steve Naroff.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-pragma.h"
38 #include "c-family/c-format.h"
40 #include "langhooks.h"
49 #include "diagnostic-core.h"
52 #include "tree-iterator.h"
54 #include "langhooks-def.h"
56 /* For default_tree_printer (). */
57 #include "tree-pretty-print.h"
59 /* For enum gimplify_status */
62 #define OBJC_VOID_AT_END void_list_node
64 static unsigned int should_call_super_dealloc = 0;
66 /* When building Objective-C++, we need in_late_binary_op. */
68 bool in_late_binary_op = false;
71 /* When building Objective-C++, we are not linking against the C front-end
72 and so need to replicate the C tree-construction functions in some way. */
74 #define OBJCP_REMAP_FUNCTIONS
75 #include "objcp-decl.h"
78 /* This is the default way of generating a method name. */
79 /* This has the problem that "test_method:argument:" and
80 "test:method_argument:" will generate the same name
81 ("_i_Test__test_method_argument_" for an instance method of the
82 class "Test"), so you can't have them both in the same class!
83 Moreover, the demangling (going from
84 "_i_Test__test_method_argument" back to the original name) is
85 undefined because there are two correct ways of demangling the
87 #ifndef OBJC_GEN_METHOD_LABEL
88 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
91 sprintf ((BUF), "_%s_%s_%s_%s", \
92 ((IS_INST) ? "i" : "c"), \
94 ((CAT_NAME)? (CAT_NAME) : ""), \
96 for (temp = (BUF); *temp; temp++) \
97 if (*temp == ':') *temp = '_'; \
101 /* These need specifying. */
102 #ifndef OBJC_FORWARDING_STACK_OFFSET
103 #define OBJC_FORWARDING_STACK_OFFSET 0
106 #ifndef OBJC_FORWARDING_MIN_OFFSET
107 #define OBJC_FORWARDING_MIN_OFFSET 0
110 /* Set up for use of obstacks. */
114 /* This obstack is used to accumulate the encoding of a data type. */
115 static struct obstack util_obstack;
117 /* This points to the beginning of obstack contents, so we can free
118 the whole contents. */
121 /* The version identifies which language generation and runtime
122 the module (file) was compiled for, and is recorded in the
123 module descriptor. */
125 #define OBJC_VERSION (flag_next_runtime ? 6 : 8)
126 #define PROTOCOL_VERSION 2
128 /* (Decide if these can ever be validly changed.) */
129 #define OBJC_ENCODE_INLINE_DEFS 0
130 #define OBJC_ENCODE_DONT_INLINE_DEFS 1
132 /*** Private Interface (procedures) ***/
134 /* Used by compile_file. */
136 static void init_objc (void);
137 static void finish_objc (void);
139 /* Code generation. */
141 static tree objc_build_constructor (tree, VEC(constructor_elt,gc) *);
142 static tree build_objc_method_call (location_t, int, tree, tree, tree, tree);
143 static tree get_proto_encoding (tree);
144 static tree lookup_interface (tree);
145 static tree objc_add_static_instance (tree, tree);
147 static tree start_class (enum tree_code, tree, tree, tree);
148 static tree continue_class (tree);
149 static void finish_class (tree);
150 static void start_method_def (tree);
152 static void objc_start_function (tree, tree, tree, tree);
154 static void objc_start_function (tree, tree, tree, struct c_arg_info *);
156 static tree start_protocol (enum tree_code, tree, tree);
157 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
158 static tree objc_add_method (tree, tree, int, bool);
159 static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree);
160 static tree build_ivar_reference (tree);
161 static tree is_ivar (tree, tree);
163 static void build_objc_exception_stuff (void);
164 static void build_next_objc_exception_stuff (void);
166 /* We only need the following for ObjC; ObjC++ will use C++'s definition
167 of DERIVED_FROM_P. */
169 static bool objc_derived_from_p (tree, tree);
170 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
174 static void objc_gen_property_data (tree, tree);
175 static void objc_synthesize_getter (tree, tree, tree);
176 static void objc_synthesize_setter (tree, tree, tree);
177 static char *objc_build_property_setter_name (tree);
178 static int match_proto_with_proto (tree, tree, int);
179 static tree lookup_property (tree, tree);
180 static tree lookup_property_in_list (tree, tree);
181 static tree lookup_property_in_protocol_list (tree, tree);
182 static void build_objc_property_accessor_helpers (void);
184 static void objc_xref_basetypes (tree, tree);
186 static void build_class_template (void);
187 static void build_selector_template (void);
188 static void build_category_template (void);
189 static void build_super_template (void);
190 static tree build_protocol_initializer (tree, tree, tree, tree, tree);
191 static tree get_class_ivars (tree, bool);
192 static tree generate_protocol_list (tree);
193 static void build_protocol_reference (tree);
195 static void build_fast_enumeration_state_template (void);
198 static void objc_generate_cxx_cdtors (void);
202 static void objc_decl_method_attributes (tree*, tree, int);
203 static tree build_keyword_selector (tree);
204 static const char *synth_id_with_class_suffix (const char *, tree);
206 /* Hash tables to manage the global pool of method prototypes. */
208 hash *nst_method_hash_list = 0;
209 hash *cls_method_hash_list = 0;
211 /* Hash tables to manage the global pool of class names. */
213 hash *cls_name_hash_list = 0;
214 hash *als_name_hash_list = 0;
216 static void hash_class_name_enter (hash *, tree, tree);
217 static hash hash_class_name_lookup (hash *, tree);
219 static hash hash_lookup (hash *, tree);
220 static tree lookup_method (tree, tree);
221 static tree lookup_method_static (tree, tree, int);
223 static tree add_class (tree, tree);
224 static void add_category (tree, tree);
225 static inline tree lookup_category (tree, tree);
229 class_names, /* class, category, protocol, module names */
230 meth_var_names, /* method and variable names */
231 meth_var_types /* method and variable type descriptors */
234 static tree add_objc_string (tree, enum string_section);
235 static void build_selector_table_decl (void);
237 /* Protocol additions. */
239 static tree lookup_protocol (tree);
240 static tree lookup_and_install_protocols (tree);
244 static void encode_type_qualifiers (tree);
245 static void encode_type (tree, int, int);
246 static void encode_field_decl (tree, int, int);
249 static void really_start_method (tree, tree);
251 static void really_start_method (tree, struct c_arg_info *);
253 static int comp_proto_with_proto (tree, tree, int);
254 static tree get_arg_type_list (tree, int, int);
255 static tree objc_decay_parm_type (tree);
256 static void objc_push_parm (tree);
258 static tree objc_get_parm_info (int);
260 static struct c_arg_info *objc_get_parm_info (int);
263 /* Utilities for debugging and error diagnostics. */
265 static char *gen_type_name (tree);
266 static char *gen_type_name_0 (tree);
267 static char *gen_method_decl (tree);
268 static char *gen_declaration (tree);
270 /* Everything else. */
272 static tree create_field_decl (tree, const char *);
273 static void add_class_reference (tree);
274 static void build_protocol_template (void);
275 static tree encode_method_prototype (tree);
276 static void generate_classref_translation_entry (tree);
277 static void handle_class_ref (tree);
278 static void generate_struct_by_value_array (void)
280 static void mark_referenced_methods (void);
281 static void generate_objc_image_info (void);
282 static bool objc_type_valid_for_messaging (tree type, bool allow_classes);
284 /*** Private Interface (data) ***/
286 /* Reserved tag definitions. */
288 #define OBJECT_TYPEDEF_NAME "id"
289 #define CLASS_TYPEDEF_NAME "Class"
291 #define TAG_OBJECT "objc_object"
292 #define TAG_CLASS "objc_class"
293 #define TAG_SUPER "objc_super"
294 #define TAG_SELECTOR "objc_selector"
296 #define UTAG_CLASS "_objc_class"
297 #define UTAG_IVAR "_objc_ivar"
298 #define UTAG_IVAR_LIST "_objc_ivar_list"
299 #define UTAG_METHOD "_objc_method"
300 #define UTAG_METHOD_LIST "_objc_method_list"
301 #define UTAG_CATEGORY "_objc_category"
302 #define UTAG_MODULE "_objc_module"
303 #define UTAG_SYMTAB "_objc_symtab"
304 #define UTAG_SUPER "_objc_super"
305 #define UTAG_SELECTOR "_objc_selector"
307 #define UTAG_PROTOCOL "_objc_protocol"
308 #define UTAG_METHOD_PROTOTYPE "_objc_method_prototype"
309 #define UTAG_METHOD_PROTOTYPE_LIST "_objc__method_prototype_list"
311 /* Note that the string object global name is only needed for the
313 #define STRING_OBJECT_GLOBAL_FORMAT "_%sClassReference"
315 #define PROTOCOL_OBJECT_CLASS_NAME "Protocol"
317 #define TAG_ENUMERATION_MUTATION "objc_enumerationMutation"
318 #define TAG_FAST_ENUMERATION_STATE "__objcFastEnumerationState"
320 static const char *TAG_GETCLASS;
321 static const char *TAG_GETMETACLASS;
322 static const char *TAG_MSGSEND;
323 static const char *TAG_MSGSENDSUPER;
324 /* The NeXT Objective-C messenger may have two extra entry points, for use
325 when returning a structure. */
326 static const char *TAG_MSGSEND_STRET;
327 static const char *TAG_MSGSENDSUPER_STRET;
328 static const char *default_constant_string_class_name;
330 /* Runtime metadata flags. */
331 #define CLS_FACTORY 0x0001L
332 #define CLS_META 0x0002L
333 #define CLS_HAS_CXX_STRUCTORS 0x2000L
335 #define OBJC_MODIFIER_STATIC 0x00000001
336 #define OBJC_MODIFIER_FINAL 0x00000002
337 #define OBJC_MODIFIER_PUBLIC 0x00000004
338 #define OBJC_MODIFIER_PRIVATE 0x00000008
339 #define OBJC_MODIFIER_PROTECTED 0x00000010
340 #define OBJC_MODIFIER_NATIVE 0x00000020
341 #define OBJC_MODIFIER_SYNCHRONIZED 0x00000040
342 #define OBJC_MODIFIER_ABSTRACT 0x00000080
343 #define OBJC_MODIFIER_VOLATILE 0x00000100
344 #define OBJC_MODIFIER_TRANSIENT 0x00000200
345 #define OBJC_MODIFIER_NONE_SPECIFIED 0x80000000
347 /* NeXT-specific tags. */
349 #define TAG_MSGSEND_NONNIL "objc_msgSendNonNil"
350 #define TAG_MSGSEND_NONNIL_STRET "objc_msgSendNonNil_stret"
351 #define TAG_EXCEPTIONEXTRACT "objc_exception_extract"
352 #define TAG_EXCEPTIONTRYENTER "objc_exception_try_enter"
353 #define TAG_EXCEPTIONTRYEXIT "objc_exception_try_exit"
354 #define TAG_EXCEPTIONMATCH "objc_exception_match"
355 #define TAG_EXCEPTIONTHROW "objc_exception_throw"
356 #define TAG_SYNCENTER "objc_sync_enter"
357 #define TAG_SYNCEXIT "objc_sync_exit"
358 #define TAG_SETJMP "_setjmp"
359 #define UTAG_EXCDATA "_objc_exception_data"
361 #define TAG_ASSIGNIVAR "objc_assign_ivar"
362 #define TAG_ASSIGNGLOBAL "objc_assign_global"
363 #define TAG_ASSIGNSTRONGCAST "objc_assign_strongCast"
365 /* Branch entry points. All that matters here are the addresses;
366 functions with these names do not really exist in libobjc. */
368 #define TAG_MSGSEND_FAST "objc_msgSend_Fast"
369 #define TAG_ASSIGNIVAR_FAST "objc_assign_ivar_Fast"
371 #define TAG_CXX_CONSTRUCT ".cxx_construct"
372 #define TAG_CXX_DESTRUCT ".cxx_destruct"
374 /* GNU-specific tags. */
376 #define TAG_EXECCLASS "__objc_exec_class"
377 #define TAG_GNUINIT "__objc_gnu_init"
379 /* Flags for lookup_method_static(). */
380 #define OBJC_LOOKUP_CLASS 1 /* Look for class methods. */
381 #define OBJC_LOOKUP_NO_SUPER 2 /* Do not examine superclasses. */
383 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
384 tree objc_global_trees[OCTI_MAX];
386 static void handle_impent (struct imp_entry *);
388 struct imp_entry *imp_list = 0;
389 int imp_count = 0; /* `@implementation' */
390 int cat_count = 0; /* `@category' */
392 objc_ivar_visibility_kind objc_ivar_visibility;
394 /* Use to generate method labels. */
395 static int method_slot = 0;
397 /* Flag to say whether methods in a protocol are optional or
399 static bool objc_method_optional_flag = false;
401 static int objc_collecting_ivars = 0;
405 static char *errbuf; /* Buffer for error diagnostics */
407 /* Data imported from tree.c. */
409 extern enum debug_info_type write_symbols;
411 /* Data imported from toplev.c. */
413 extern const char *dump_base_name;
415 static int flag_typed_selectors;
417 /* Store all constructed constant strings in a hash table so that
418 they get uniqued properly. */
420 struct GTY(()) string_descriptor {
421 /* The literal argument . */
424 /* The resulting constant string. */
428 static GTY((param_is (struct string_descriptor))) htab_t string_htab;
430 FILE *gen_declaration_file;
432 /* Tells "encode_pointer/encode_aggregate" whether we are generating
433 type descriptors for instance variables (as opposed to methods).
434 Type descriptors for instance variables contain more information
435 than methods (for static typing and embedded structures). */
437 static int generating_instance_variables = 0;
439 /* For building an objc struct. These may not be used when this file
440 is compiled as part of obj-c++. */
442 static bool objc_building_struct;
443 static struct c_struct_parse_info *objc_struct_info ATTRIBUTE_UNUSED;
445 /* Start building a struct for objc. */
448 objc_start_struct (tree name)
450 gcc_assert (!objc_building_struct);
451 objc_building_struct = true;
452 return start_struct (input_location, RECORD_TYPE, name, &objc_struct_info);
455 /* Finish building a struct for objc. */
458 objc_finish_struct (tree type, tree fieldlist)
460 gcc_assert (objc_building_struct);
461 objc_building_struct = false;
462 return finish_struct (input_location, type, fieldlist, NULL_TREE,
467 build_sized_array_type (tree base_type, int size)
469 tree index_type = build_index_type (build_int_cst (NULL_TREE, size - 1));
470 return build_array_type (base_type, index_type);
474 add_field_decl (tree type, const char *name, tree **chain)
476 tree field = create_field_decl (type, name);
480 *chain = &DECL_CHAIN (field);
485 /* Some platforms pass small structures through registers versus
486 through an invisible pointer. Determine at what size structure is
487 the transition point between the two possibilities. */
490 generate_struct_by_value_array (void)
495 int aggregate_in_mem[32];
498 /* Presumably no platform passes 32 byte structures in a register. */
499 for (i = 1; i < 32; i++)
504 /* Create an unnamed struct that has `i' character components */
505 type = objc_start_struct (NULL_TREE);
507 strcpy (buffer, "c1");
508 decls = add_field_decl (char_type_node, buffer, &chain);
510 for (j = 1; j < i; j++)
512 sprintf (buffer, "c%d", j + 1);
513 add_field_decl (char_type_node, buffer, &chain);
515 objc_finish_struct (type, decls);
517 aggregate_in_mem[i] = aggregate_value_p (type, 0);
518 if (!aggregate_in_mem[i])
522 /* We found some structures that are returned in registers instead of memory
523 so output the necessary data. */
526 for (i = 31; i >= 0; i--)
527 if (!aggregate_in_mem[i])
529 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n\n", i);
531 /* The first member of the structure is always 0 because we don't handle
532 structures with 0 members */
533 printf ("static int struct_forward_array[] = {\n 0");
535 for (j = 1; j <= i; j++)
536 printf (", %d", aggregate_in_mem[j]);
547 if (cxx_init () == false)
549 if (c_objc_common_init () == false)
553 /* If gen_declaration desired, open the output file. */
554 if (flag_gen_declaration)
556 register char * const dumpname = concat (dump_base_name, ".decl", NULL);
557 gen_declaration_file = fopen (dumpname, "w");
558 if (gen_declaration_file == 0)
559 fatal_error ("can't open %s: %m", dumpname);
563 if (flag_next_runtime)
565 TAG_GETCLASS = "objc_getClass";
566 TAG_GETMETACLASS = "objc_getMetaClass";
567 TAG_MSGSEND = "objc_msgSend";
568 TAG_MSGSENDSUPER = "objc_msgSendSuper";
569 TAG_MSGSEND_STRET = "objc_msgSend_stret";
570 TAG_MSGSENDSUPER_STRET = "objc_msgSendSuper_stret";
571 default_constant_string_class_name = "NSConstantString";
575 TAG_GETCLASS = "objc_get_class";
576 TAG_GETMETACLASS = "objc_get_meta_class";
577 TAG_MSGSEND = "objc_msg_lookup";
578 TAG_MSGSENDSUPER = "objc_msg_lookup_super";
579 /* GNU runtime does not provide special functions to support
580 structure-returning methods. */
581 default_constant_string_class_name = "NXConstantString";
582 flag_typed_selectors = 1;
583 /* GNU runtime does not need the compiler to change code
584 in order to do GC. */
587 warning_at (0, 0, "%<-fobjc-gc%> is ignored for %<-fgnu-runtime%>");
594 if (print_struct_values && !flag_compare_debug)
595 generate_struct_by_value_array ();
600 /* This is called automatically (at the very end of compilation) by
601 c_write_global_declarations and cp_write_global_declarations. */
603 objc_write_global_declarations (void)
605 mark_referenced_methods ();
607 /* Finalize Objective-C runtime data. */
610 if (gen_declaration_file)
611 fclose (gen_declaration_file);
614 /* Return the first occurrence of a method declaration corresponding
615 to sel_name in rproto_list. Search rproto_list recursively.
616 If is_class is 0, search for instance methods, otherwise for class
619 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
625 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
627 p = TREE_VALUE (rproto);
629 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
631 if ((fnd = lookup_method (is_class
632 ? PROTOCOL_CLS_METHODS (p)
633 : PROTOCOL_NST_METHODS (p), sel_name)))
635 else if (PROTOCOL_LIST (p))
636 fnd = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
641 ; /* An identifier...if we could not find a protocol. */
652 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
656 /* Make sure the protocol is supported by the object on the rhs. */
657 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
660 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
662 p = TREE_VALUE (rproto);
664 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
669 else if (PROTOCOL_LIST (p))
670 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
679 ; /* An identifier...if we could not find a protocol. */
686 objc_start_class_interface (tree klass, tree super_class,
687 tree protos, tree attributes)
692 error_at (input_location, "class attributes are not available in Objective-C 1.0");
694 warning_at (input_location, OPT_Wattributes,
695 "class attributes are not available in this version"
696 " of the compiler, (ignored)");
698 objc_interface_context
700 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos);
701 objc_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
705 objc_start_category_interface (tree klass, tree categ,
706 tree protos, tree attributes)
711 error_at (input_location, "category attributes are not available in Objective-C 1.0");
713 warning_at (input_location, OPT_Wattributes,
714 "category attributes are not available in this version"
715 " of the compiler, (ignored)");
717 objc_interface_context
718 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos);
720 = continue_class (objc_interface_context);
724 objc_start_protocol (tree name, tree protos, tree attributes)
729 error_at (input_location, "protocol attributes are not available in Objective-C 1.0");
731 warning_at (input_location, OPT_Wattributes,
732 "protocol attributes are not available in this version"
733 " of the compiler, (ignored)");
735 objc_interface_context
736 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos);
737 objc_method_optional_flag = false;
741 objc_continue_interface (void)
744 = continue_class (objc_interface_context);
748 objc_finish_interface (void)
750 finish_class (objc_interface_context);
751 objc_interface_context = NULL_TREE;
752 objc_method_optional_flag = false;
756 objc_start_class_implementation (tree klass, tree super_class)
758 objc_implementation_context
760 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE);
761 objc_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
765 objc_start_category_implementation (tree klass, tree categ)
767 objc_implementation_context
768 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE);
770 = continue_class (objc_implementation_context);
774 objc_continue_implementation (void)
777 = continue_class (objc_implementation_context);
781 objc_finish_implementation (void)
784 if (flag_objc_call_cxx_cdtors)
785 objc_generate_cxx_cdtors ();
788 if (objc_implementation_context)
790 finish_class (objc_implementation_context);
791 objc_ivar_chain = NULL_TREE;
792 objc_implementation_context = NULL_TREE;
795 warning (0, "%<@end%> must appear in an @implementation context");
799 objc_set_visibility (objc_ivar_visibility_kind visibility)
801 if (visibility == OBJC_IVAR_VIS_PACKAGE)
804 error ("%<@package%> is not available in Objective-C 1.0");
806 warning (0, "%<@package%> presently has the same effect as %<@public%>");
808 objc_ivar_visibility = visibility;
812 objc_set_method_opt (bool optional)
815 error_at (input_location, "@optional/@required are not available in Objective-C 1.0");
817 objc_method_optional_flag = optional;
818 if (!objc_interface_context
819 || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
821 error ("@optional/@required is allowed in @protocol context only.");
822 objc_method_optional_flag = false;
826 /* This routine is called by the parser when a
827 @property... declaration is found. 'decl' is the declaration of
828 the property (type/identifier), and the other arguments represent
829 property attributes that may have been specified in the Objective-C
830 declaration. 'parsed_property_readonly' is 'true' if the attribute
831 'readonly' was specified, and 'false' if not; similarly for the
832 other bool parameters. 'parsed_property_getter_ident' is NULL_TREE
833 if the attribute 'getter' was not specified, and is the identifier
834 corresponding to the specified getter if it was; similarly for
835 'parsed_property_setter_ident'. */
837 objc_add_property_declaration (location_t location, tree decl,
838 bool parsed_property_readonly, bool parsed_property_readwrite,
839 bool parsed_property_assign, bool parsed_property_retain,
840 bool parsed_property_copy, bool parsed_property_nonatomic,
841 tree parsed_property_getter_ident, tree parsed_property_setter_ident)
845 /* 'property_readonly' and 'property_assign_semantics' are the final
846 attributes of the property after all parsed attributes have been
847 considered (eg, if we parsed no 'readonly' and no 'readwrite', ie
848 parsed_property_readonly = false and parsed_property_readwrite =
849 false, then property_readonly will be false because the default
851 bool property_readonly = false;
852 objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN;
855 error_at (input_location, "%<@property%> is not available in Objective-C 1.0");
857 if (parsed_property_readonly && parsed_property_readwrite)
859 error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute");
860 /* In case of conflicting attributes (here and below), after
861 producing an error, we pick one of the attributes and keep
863 property_readonly = false;
867 if (parsed_property_readonly)
868 property_readonly = true;
870 if (parsed_property_readwrite)
871 property_readonly = false;
874 if (parsed_property_readonly && parsed_property_setter_ident)
876 /* Maybe this should be an error ? The Apple documentation says it is a warning. */
877 warning_at (location, 0, "%<readonly%> attribute conflicts with %<setter%> attribute");
878 property_readonly = false;
881 if (parsed_property_assign && parsed_property_retain)
883 error_at (location, "%<assign%> attribute conflicts with %<retain%> attribute");
884 property_assign_semantics = OBJC_PROPERTY_RETAIN;
886 else if (parsed_property_assign && parsed_property_copy)
888 error_at (location, "%<assign%> attribute conflicts with %<copy%> attribute");
889 property_assign_semantics = OBJC_PROPERTY_COPY;
891 else if (parsed_property_retain && parsed_property_copy)
893 error_at (location, "%<retain%> attribute conflicts with %<copy%> attribute");
894 property_assign_semantics = OBJC_PROPERTY_COPY;
898 if (parsed_property_assign)
899 property_assign_semantics = OBJC_PROPERTY_ASSIGN;
901 if (parsed_property_retain)
902 property_assign_semantics = OBJC_PROPERTY_RETAIN;
904 if (parsed_property_copy)
905 property_assign_semantics = OBJC_PROPERTY_COPY;
908 if (!objc_interface_context)
910 error_at (location, "property declaration not in @interface or @protocol context");
914 /* At this point we know that we are either in an interface, a
915 category, or a protocol. */
917 if (parsed_property_setter_ident)
919 /* The setter should be terminated by ':', but the parser only
920 gives us an identifier without ':'. So, we need to add ':'
922 const char *parsed_setter = IDENTIFIER_POINTER (parsed_property_setter_ident);
923 size_t length = strlen (parsed_setter);
924 char *final_setter = (char *)alloca (length + 2);
926 sprintf (final_setter, "%s:", parsed_setter);
927 parsed_property_setter_ident = get_identifier (final_setter);
930 /* Check that the property does not have an initial value specified.
931 This should never happen as the parser doesn't allow this, but
932 it's just in case. */
933 if (DECL_INITIAL (decl))
935 error_at (location, "property can not have an initial value");
939 /* TODO: Check that the property type is an Objective-C object or a "POD". */
941 /* Implement -Wproperty-assign-default (which is enabled by default). */
942 if (warn_property_assign_default
943 /* If garbage collection is not being used, then 'assign' is
944 valid for objects (and typically used for delegates) but it
945 is wrong in most cases (since most objects need to be
946 retained or copied in setters). Warn users when 'assign' is
948 && property_assign_semantics == OBJC_PROPERTY_ASSIGN
949 /* Read-only properties are never assigned, so the assignment
950 semantics do not matter in that case. */
951 && !property_readonly
954 /* Please note that it would make sense to default to 'assign'
955 for non-{Objective-C objects}, and to 'retain' for
956 Objective-C objects. But that would break compatibility with
958 if (!parsed_property_assign && !parsed_property_retain && !parsed_property_copy)
960 /* Use 'false' so we do not warn for Class objects. */
961 if (objc_type_valid_for_messaging (TREE_TYPE (decl), false))
963 warning_at (location,
965 "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>",
968 "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it");
973 if (property_assign_semantics == OBJC_PROPERTY_RETAIN
974 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
975 error_at (location, "%<retain%> attribute is only valid for Objective-C objects");
977 if (property_assign_semantics == OBJC_PROPERTY_COPY
978 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
979 error_at (location, "%<copy%> attribute is only valid for Objective-C objects");
981 /* Check for duplicate property declarations. We first check the
982 immediate context for a property with the same name. */
983 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
985 if (PROPERTY_NAME (x) == DECL_NAME (decl))
987 location_t original_location = DECL_SOURCE_LOCATION (x);
989 error_at (location, "redeclaration of property %qD", decl);
991 if (original_location != UNKNOWN_LOCATION)
992 inform (original_location, "originally declared here");
997 /* TODO: Shall we check here for other property declaractions (in
998 the superclass, other categories or protocols) with the same name
999 and conflicting types ? */
1001 /* Create a PROPERTY_DECL node. */
1002 property_decl = make_node (PROPERTY_DECL);
1004 /* Copy the basic information from the original decl. */
1005 TREE_TYPE (property_decl) = TREE_TYPE (decl);
1006 DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl);
1007 TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl);
1009 /* Add property-specific information. */
1010 PROPERTY_NAME (property_decl) = DECL_NAME (decl);
1011 PROPERTY_GETTER_NAME (property_decl) = parsed_property_getter_ident;
1012 PROPERTY_SETTER_NAME (property_decl) = parsed_property_setter_ident;
1013 PROPERTY_READONLY (property_decl) = property_readonly;
1014 PROPERTY_NONATOMIC (property_decl) = parsed_property_nonatomic;
1015 PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics;
1016 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1017 PROPERTY_DYNAMIC (property_decl) = 0;
1019 /* Add the PROPERTY_DECL to the list of properties for the class. */
1020 TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context);
1021 CLASS_PROPERTY_DECL (objc_interface_context) = property_decl;
1024 /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
1027 lookup_property_in_list (tree chain, tree property)
1030 for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x))
1031 if (PROPERTY_NAME (x) == property)
1036 /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
1037 static tree lookup_property_in_protocol_list (tree rproto_list, tree property)
1040 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
1042 tree p = TREE_VALUE (rproto);
1043 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
1045 if ((x = lookup_property_in_list (p, property)))
1047 if (PROTOCOL_LIST (p))
1048 return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property);
1052 ; /* An identifier...if we could not find a protocol. */
1058 /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
1059 chain of interface hierarchy. */
1061 lookup_property (tree interface_type, tree property)
1063 tree inter = interface_type;
1067 if ((x = lookup_property_in_list (inter, property)))
1069 /* Failing that, look for the property in each category of the class. */
1071 while ((category = CLASS_CATEGORY_LIST (category)))
1072 if ((x = lookup_property_in_list (category, property)))
1075 /* Failing to find in categories, look for property in protocol list. */
1076 if (CLASS_PROTOCOL_LIST (inter)
1077 && (x = lookup_property_in_protocol_list (
1078 CLASS_PROTOCOL_LIST (inter), property)))
1081 /* Failing that, climb up the inheritance hierarchy. */
1082 inter = lookup_interface (CLASS_SUPER_NAME (inter));
1087 /* This is a subroutine of objc_maybe_build_component_ref. Search the
1088 list of methods in the interface (and, failing that, protocol list)
1089 provided for a 'setter' or 'getter' for 'component' with default
1090 names (ie, if 'component' is "name", then search for "name" and
1091 "setName:"). If any is found, then create an artificial property
1092 that uses them. Return NULL_TREE if 'getter' or 'setter' could not
1095 maybe_make_artificial_property_decl (tree interface, tree protocol_list, tree component, bool is_class)
1097 tree getter_name = component;
1098 tree setter_name = get_identifier (objc_build_property_setter_name (component));
1099 tree getter = NULL_TREE;
1100 tree setter = NULL_TREE;
1107 flags = OBJC_LOOKUP_CLASS;
1109 getter = lookup_method_static (interface, getter_name, flags);
1110 setter = lookup_method_static (interface, setter_name, flags);
1113 /* Try the protocol_list if we didn't find anything in the interface. */
1114 if (!getter && !setter)
1116 getter = lookup_method_in_protocol_list (protocol_list, getter_name, is_class);
1117 setter = lookup_method_in_protocol_list (protocol_list, setter_name, is_class);
1120 /* There needs to be at least a getter or setter for this to be a
1121 valid 'object.component' syntax. */
1122 if (getter || setter)
1124 /* Yes ... determine the type of the expression. */
1129 type = TREE_VALUE (TREE_TYPE (getter));
1131 type = TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter)));
1133 /* Create an artificial property declaration with the
1134 information we collected on the type and getter/setter
1136 property_decl = make_node (PROPERTY_DECL);
1138 TREE_TYPE (property_decl) = type;
1139 DECL_SOURCE_LOCATION (property_decl) = input_location;
1140 TREE_DEPRECATED (property_decl) = 0;
1141 DECL_ARTIFICIAL (property_decl) = 1;
1143 /* Add property-specific information. Note that one of
1144 PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a
1145 non-existing method; this will generate an error when the
1146 expression is later compiled. At this stage we don't know if
1147 the getter or setter will be used, so we can't generate an
1149 PROPERTY_NAME (property_decl) = component;
1150 PROPERTY_GETTER_NAME (property_decl) = getter_name;
1151 PROPERTY_SETTER_NAME (property_decl) = setter_name;
1152 PROPERTY_READONLY (property_decl) = 0;
1153 PROPERTY_NONATOMIC (property_decl) = 0;
1154 PROPERTY_ASSIGN_SEMANTICS (property_decl) = 0;
1155 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1156 PROPERTY_DYNAMIC (property_decl) = 0;
1159 PROPERTY_HAS_NO_GETTER (property_decl) = 1;
1161 /* The following is currently unused, but it's nice to have
1162 there. We may use it if we need in the future. */
1164 PROPERTY_HAS_NO_SETTER (property_decl) = 1;
1166 return property_decl;
1172 /* This hook routine is invoked by the parser when an expression such
1173 as 'xxx.yyy' is parsed. We get a chance to process these
1174 expressions in a way that is specified to Objective-C (to implement
1175 the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars).
1176 If the expression is not an Objective-C specified expression, we
1177 should return NULL_TREE; else we return the expression.
1179 At the moment this only implements dot-syntax and properties (not
1180 non-fragile ivars yet), ie 'object.property' or 'object.component'
1181 where 'component' is not a declared property, but a valid getter or
1182 setter for it could be found. */
1184 objc_maybe_build_component_ref (tree object, tree property_ident)
1189 /* If we are in Objective-C 1.0 mode, properties are not
1191 if (flag_objc1_only)
1194 /* Try to determine quickly if 'object' is an Objective-C object or
1195 not. If not, return. */
1196 if (object == NULL_TREE || object == error_mark_node
1197 || (rtype = TREE_TYPE (object)) == NULL_TREE)
1200 if (property_ident == NULL_TREE || property_ident == error_mark_node
1201 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1204 /* TODO: Implement super.property. */
1206 /* TODO: Carefully review the following code. */
1207 if (objc_is_id (rtype))
1209 tree rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
1210 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
1213 x = lookup_property_in_protocol_list (rprotos, property_ident);
1217 /* Ok, no property. Maybe it was an object.component
1218 dot-syntax without a declared property. Look for
1219 getter/setter methods and internally declare an artifical
1220 property based on them if found. */
1221 x = maybe_make_artificial_property_decl (NULL_TREE, rprotos,
1228 tree basetype = TYPE_MAIN_VARIANT (rtype);
1230 if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE)
1231 basetype = TREE_TYPE (basetype);
1235 while (basetype != NULL_TREE
1236 && TREE_CODE (basetype) == RECORD_TYPE
1237 && OBJC_TYPE_NAME (basetype)
1238 && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL
1239 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)))
1240 basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype));
1242 if (basetype != NULL_TREE && TYPED_OBJECT (basetype))
1244 tree interface_type = TYPE_OBJC_INTERFACE (basetype);
1245 tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype);
1247 x = lookup_property (interface_type, property_ident);
1250 x = lookup_property_in_protocol_list (protocol_list, property_ident);
1254 /* Ok, no property. Try the dot-syntax without a
1255 declared property. */
1256 x = maybe_make_artificial_property_decl (interface_type, protocol_list,
1257 property_ident, false);
1266 if (TREE_DEPRECATED (x))
1267 warn_deprecated_use (x, NULL_TREE);
1269 expression = build2 (PROPERTY_REF, TREE_TYPE(x), object, x);
1270 SET_EXPR_LOCATION (expression, input_location);
1271 TREE_SIDE_EFFECTS (expression) = 1;
1273 /* We have an additional nasty problem here; if this
1274 PROPERTY_REF needs to become a 'getter', then the conversion
1275 from PROPERTY_REF into a getter call happens in gimplify,
1276 after the selector table has already been generated and it is
1277 too late to add another selector to it. To work around the
1278 problem, we always put the selector in the table at this
1279 stage, as if we were building the method call here. And the
1280 easiest way to do this is precisely to build the method call,
1281 then discard it. Note that if the PROPERTY_REF becomes a
1282 'setter' instead of a 'getter', then we have added a selector
1283 too many to the selector table. This is a little
1286 TODO: This can be made more efficient; in particular we don't
1287 need to build the whole message call, we could just work on
1290 If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial
1291 property decl created to deal with a dotsyntax not really
1292 referring to an existing property) then do not try to build a
1293 call to the getter as there is no getter. */
1294 if (!PROPERTY_HAS_NO_GETTER (x))
1295 objc_finish_message_expr (object,
1296 PROPERTY_GETTER_NAME (x),
1305 /* This hook routine is invoked by the parser when an expression such
1306 as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the
1307 Objective-C 2.0 dot-syntax applied to classes, so we need to
1308 convert it into a setter/getter call on the class. */
1310 objc_build_class_component_ref (tree class_name, tree property_ident)
1315 if (flag_objc1_only)
1316 error_at (input_location, "the dot syntax is not available in Objective-C 1.0");
1318 if (class_name == NULL_TREE || class_name == error_mark_node
1319 || TREE_CODE (class_name) != IDENTIFIER_NODE)
1320 return error_mark_node;
1322 if (property_ident == NULL_TREE || property_ident == error_mark_node
1323 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1326 object = objc_get_class_reference (class_name);
1329 /* We know that 'class_name' is an Objective-C class name as the
1330 parser won't call this function if it is not. This is only a
1331 double-check for safety. */
1332 error_at (input_location, "could not find class %qE", class_name);
1333 return error_mark_node;
1336 rtype = lookup_interface (class_name);
1339 /* Again, this should never happen, but we do check. */
1340 error_at (input_location, "could not find interface for class %qE", class_name);
1341 return error_mark_node;
1344 x = maybe_make_artificial_property_decl (rtype, NULL_TREE,
1352 if (TREE_DEPRECATED (x))
1353 warn_deprecated_use (x, NULL_TREE);
1355 expression = build2 (PROPERTY_REF, TREE_TYPE(x), object, x);
1356 SET_EXPR_LOCATION (expression, input_location);
1357 TREE_SIDE_EFFECTS (expression) = 1;
1358 /* See above for why we do this. */
1359 if (!PROPERTY_HAS_NO_GETTER (x))
1360 objc_finish_message_expr (object,
1361 PROPERTY_GETTER_NAME (x),
1368 error_at (input_location, "could not find setter/getter for %qE in class %qE",
1369 property_ident, class_name);
1370 return error_mark_node;
1378 /* This is used because we don't want to expose PROPERTY_REF to the
1379 C/C++ frontends. Maybe we should! */
1381 objc_is_property_ref (tree node)
1383 if (node && TREE_CODE (node) == PROPERTY_REF)
1389 /* This hook routine is called when a MODIFY_EXPR is being built. We
1390 check what is being modified; if it is a PROPERTY_REF, we need to
1391 generate a 'setter' function call for the property. If this is not
1392 a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go
1393 on creating their MODIFY_EXPR.
1395 This is used for example if you write
1399 where 'count' is a property. The left-hand side creates a
1400 PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR
1401 to assign something to it. We intercept that here, and generate a
1402 call to the 'setter' method instead. */
1404 objc_maybe_build_modify_expr (tree lhs, tree rhs)
1406 if (lhs && TREE_CODE (lhs) == PROPERTY_REF)
1408 tree object_expr = PROPERTY_REF_OBJECT (lhs);
1409 tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs);
1411 if (PROPERTY_READONLY (property_decl))
1413 error ("readonly property can not be set");
1414 return error_mark_node;
1418 tree setter_argument = build_tree_list (NULL_TREE, rhs);
1421 /* TODO: Check that the setter return type is 'void'. */
1423 /* TODO: Decay argument in C. */
1424 setter = objc_finish_message_expr (object_expr,
1425 PROPERTY_SETTER_NAME (property_decl),
1435 objc_build_method_signature (bool is_class_method, tree rettype, tree selector,
1436 tree optparms, bool ellipsis)
1438 if (is_class_method)
1439 return build_method_decl (CLASS_METHOD_DECL, rettype, selector,
1440 optparms, ellipsis);
1442 return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector,
1443 optparms, ellipsis);
1447 objc_add_method_declaration (bool is_class_method, tree decl, tree attributes)
1449 if (!objc_interface_context)
1451 /* PS: At the moment, due to how the parser works, it should be
1452 impossible to get here. But it's good to have the check in
1453 case the parser changes.
1455 fatal_error ("method declaration not in @interface context");
1458 if (flag_objc1_only && attributes)
1459 error_at (input_location, "method attributes are not available in Objective-C 1.0");
1461 objc_decl_method_attributes (&decl, attributes, 0);
1462 objc_add_method (objc_interface_context,
1465 objc_method_optional_flag);
1468 /* Return 'true' if the method definition could be started, and
1469 'false' if not (because we are outside an @implementation context).
1472 objc_start_method_definition (bool is_class_method, tree decl, tree attributes)
1474 if (!objc_implementation_context)
1476 error ("method definition not in @implementation context");
1480 if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node)
1484 /* Indicate no valid break/continue context by setting these variables
1485 to some non-null, non-label value. We'll notice and emit the proper
1486 error message in c_finish_bc_stmt. */
1487 c_break_label = c_cont_label = size_zero_node;
1491 warning_at (input_location, 0, "method attributes can not be specified in @implementation context");
1493 objc_decl_method_attributes (&decl, attributes, 0);
1495 objc_add_method (objc_implementation_context,
1498 /* is optional */ false);
1499 start_method_def (decl);
1504 objc_add_instance_variable (tree decl)
1506 (void) add_instance_variable (objc_ivar_context,
1507 objc_ivar_visibility,
1511 /* Return true if TYPE is 'id'. */
1514 objc_is_object_id (tree type)
1516 return OBJC_TYPE_NAME (type) == objc_object_id;
1520 objc_is_class_id (tree type)
1522 return OBJC_TYPE_NAME (type) == objc_class_id;
1525 /* Construct a C struct with same name as KLASS, a base struct with tag
1526 SUPER_NAME (if any), and FIELDS indicated. */
1529 objc_build_struct (tree klass, tree fields, tree super_name)
1531 tree name = CLASS_NAME (klass);
1532 tree s = objc_start_struct (name);
1533 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
1535 VEC(tree,heap) *objc_info = NULL;
1540 /* Prepend a packed variant of the base class into the layout. This
1541 is necessary to preserve ObjC ABI compatibility. */
1542 tree base = build_decl (input_location,
1543 FIELD_DECL, NULL_TREE, super);
1544 tree field = TYPE_FIELDS (super);
1546 while (field && DECL_CHAIN (field)
1547 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
1548 field = DECL_CHAIN (field);
1550 /* For ObjC ABI purposes, the "packed" size of a base class is
1551 the sum of the offset and the size (in bits) of the last field
1554 = (field && TREE_CODE (field) == FIELD_DECL
1555 ? size_binop (PLUS_EXPR,
1556 size_binop (PLUS_EXPR,
1559 convert (bitsizetype,
1560 DECL_FIELD_OFFSET (field)),
1561 bitsize_int (BITS_PER_UNIT)),
1562 DECL_FIELD_BIT_OFFSET (field)),
1564 : bitsize_zero_node);
1565 DECL_SIZE_UNIT (base)
1566 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
1567 size_int (BITS_PER_UNIT));
1568 DECL_ARTIFICIAL (base) = 1;
1569 DECL_ALIGN (base) = 1;
1570 DECL_FIELD_CONTEXT (base) = s;
1572 DECL_FIELD_IS_BASE (base) = 1;
1575 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
1576 #endif /* are following the ObjC ABI here. */
1577 DECL_CHAIN (base) = fields;
1581 /* NB: Calling finish_struct() may cause type TYPE_LANG_SPECIFIC fields
1582 in all variants of this RECORD_TYPE to be clobbered, but it is therein
1583 that we store protocol conformance info (e.g., 'NSObject <MyProtocol>').
1584 Hence, we must squirrel away the ObjC-specific information before calling
1585 finish_struct(), and then reinstate it afterwards. */
1587 for (t = TYPE_NEXT_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
1589 if (!TYPE_HAS_OBJC_INFO (t))
1591 INIT_TYPE_OBJC_INFO (t);
1592 TYPE_OBJC_INTERFACE (t) = klass;
1594 VEC_safe_push (tree, heap, objc_info, TYPE_OBJC_INFO (t));
1597 /* Point the struct at its related Objective-C class. */
1598 INIT_TYPE_OBJC_INFO (s);
1599 TYPE_OBJC_INTERFACE (s) = klass;
1601 s = objc_finish_struct (s, fields);
1603 for (i = 0, t = TYPE_NEXT_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
1605 TYPE_OBJC_INFO (t) = VEC_index (tree, objc_info, i);
1606 /* Replace the IDENTIFIER_NODE with an actual @interface. */
1607 TYPE_OBJC_INTERFACE (t) = klass;
1609 VEC_free (tree, heap, objc_info);
1611 /* Use TYPE_BINFO structures to point at the super class, if any. */
1612 objc_xref_basetypes (s, super);
1614 /* Mark this struct as a class template. */
1615 CLASS_STATIC_TEMPLATE (klass) = s;
1620 /* Build a type differing from TYPE only in that TYPE_VOLATILE is set.
1621 Unlike tree.c:build_qualified_type(), preserve TYPE_LANG_SPECIFIC in the
1624 objc_build_volatilized_type (tree type)
1628 /* Check if we have not constructed the desired variant already. */
1629 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1631 /* The type qualifiers must (obviously) match up. */
1632 if (!TYPE_VOLATILE (t)
1633 || (TYPE_READONLY (t) != TYPE_READONLY (type))
1634 || (TYPE_RESTRICT (t) != TYPE_RESTRICT (type)))
1637 /* For pointer types, the pointees (and hence their TYPE_LANG_SPECIFIC
1638 info, if any) must match up. */
1639 if (POINTER_TYPE_P (t)
1640 && (TREE_TYPE (t) != TREE_TYPE (type)))
1643 /* Only match up the types which were previously volatilized in similar fashion and not
1644 because they were declared as such. */
1645 if (!lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (t)))
1648 /* Everything matches up! */
1652 /* Ok, we could not re-use any of the pre-existing variants. Create
1654 t = build_variant_type_copy (type);
1655 TYPE_VOLATILE (t) = 1;
1657 TYPE_ATTRIBUTES (t) = merge_attributes (TYPE_ATTRIBUTES (type),
1658 tree_cons (get_identifier ("objc_volatilized"),
1661 if (TREE_CODE (t) == ARRAY_TYPE)
1662 TREE_TYPE (t) = objc_build_volatilized_type (TREE_TYPE (t));
1664 /* Set up the canonical type information. */
1665 if (TYPE_STRUCTURAL_EQUALITY_P (type))
1666 SET_TYPE_STRUCTURAL_EQUALITY (t);
1667 else if (TYPE_CANONICAL (type) != type)
1668 TYPE_CANONICAL (t) = objc_build_volatilized_type (TYPE_CANONICAL (type));
1670 TYPE_CANONICAL (t) = t;
1675 /* Mark DECL as being 'volatile' for purposes of Darwin
1676 _setjmp()/_longjmp() exception handling. Called from
1677 objc_mark_locals_volatile(). */
1679 objc_volatilize_decl (tree decl)
1681 /* Do not mess with variables that are 'static' or (already)
1683 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
1684 && (TREE_CODE (decl) == VAR_DECL
1685 || TREE_CODE (decl) == PARM_DECL))
1687 tree t = TREE_TYPE (decl);
1689 t = objc_build_volatilized_type (t);
1691 TREE_TYPE (decl) = t;
1692 TREE_THIS_VOLATILE (decl) = 1;
1693 TREE_SIDE_EFFECTS (decl) = 1;
1694 DECL_REGISTER (decl) = 0;
1696 C_DECL_REGISTER (decl) = 0;
1701 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
1702 (including its categories and superclasses) or by object type TYP.
1703 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
1706 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
1708 bool class_type = (cls != NULL_TREE);
1714 /* Check protocols adopted by the class and its categories. */
1715 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
1717 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
1721 /* Repeat for superclasses. */
1722 cls = lookup_interface (CLASS_SUPER_NAME (cls));
1725 /* Check for any protocols attached directly to the object type. */
1726 if (TYPE_HAS_OBJC_INFO (typ))
1728 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
1735 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
1736 /* NB: Types 'id' and 'Class' cannot reasonably be described as
1737 "implementing" a given protocol, since they do not have an
1740 warning (0, "class %qs does not implement the %qE protocol",
1741 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
1743 warning (0, "type %qs does not conform to the %qE protocol",
1744 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
1750 /* Check if class RCLS and instance struct type RTYP conform to at least the
1751 same protocols that LCLS and LTYP conform to. */
1754 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
1757 bool have_lproto = false;
1761 /* NB: We do _not_ look at categories defined for LCLS; these may or
1762 may not get loaded in, and therefore it is unreasonable to require
1763 that RCLS/RTYP must implement any of their protocols. */
1764 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
1768 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
1772 /* Repeat for superclasses. */
1773 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
1776 /* Check for any protocols attached directly to the object type. */
1777 if (TYPE_HAS_OBJC_INFO (ltyp))
1779 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
1783 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
1788 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
1789 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
1790 away with simply checking for 'id' or 'Class' (!RCLS), since this
1791 routine will not get called in other cases. */
1792 return have_lproto || (rcls != NULL_TREE);
1795 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
1796 Both TYPE1 and TYPE2 must be pointers, and already determined to be
1797 compatible by objc_compare_types() below. */
1800 objc_common_type (tree type1, tree type2)
1802 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
1804 while (POINTER_TYPE_P (inner1))
1806 inner1 = TREE_TYPE (inner1);
1807 inner2 = TREE_TYPE (inner2);
1810 /* If one type is derived from another, return the base type. */
1811 if (DERIVED_FROM_P (inner1, inner2))
1813 else if (DERIVED_FROM_P (inner2, inner1))
1816 /* If both types are 'Class', return 'Class'. */
1817 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
1818 return objc_class_type;
1820 /* Otherwise, return 'id'. */
1821 return objc_object_type;
1824 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
1825 an instance of RTYP to an instance of LTYP or to compare the two
1826 (if ARGNO is equal to -3), per ObjC type system rules. Before
1827 returning 'true', this routine may issue warnings related to, e.g.,
1828 protocol conformance. When returning 'false', the routine must
1829 produce absolutely no warnings; the C or C++ front-end will do so
1830 instead, if needed. If either LTYP or RTYP is not an Objective-C type,
1831 the routine must return 'false'.
1833 The ARGNO parameter is encoded as follows:
1834 >= 1 Parameter number (CALLEE contains function being called);
1838 -3 Comparison (LTYP and RTYP may match in either direction);
1839 -4 Silent comparison (for C++ overload resolution).
1843 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
1845 tree lcls, rcls, lproto, rproto;
1846 bool pointers_compatible;
1848 /* We must be dealing with pointer types */
1849 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
1854 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
1855 rtyp = TREE_TYPE (rtyp);
1857 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
1859 /* We must also handle function pointers, since ObjC is a bit more
1860 lenient than C or C++ on this. */
1861 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
1863 /* Return types must be covariant. */
1864 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
1865 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
1869 /* Argument types must be contravariant. */
1870 for (ltyp = TYPE_ARG_TYPES (ltyp), rtyp = TYPE_ARG_TYPES (rtyp);
1871 ltyp && rtyp; ltyp = TREE_CHAIN (ltyp), rtyp = TREE_CHAIN (rtyp))
1873 if (!comptypes (TREE_VALUE (rtyp), TREE_VALUE (ltyp))
1874 && !objc_compare_types (TREE_VALUE (rtyp), TREE_VALUE (ltyp),
1879 return (ltyp == rtyp);
1882 /* Past this point, we are only interested in ObjC class instances,
1883 or 'id' or 'Class'. */
1884 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
1887 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
1888 && !TYPE_HAS_OBJC_INFO (ltyp))
1891 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
1892 && !TYPE_HAS_OBJC_INFO (rtyp))
1895 /* Past this point, we are committed to returning 'true' to the caller
1896 (unless performing a silent comparison; see below). However, we can
1897 still warn about type and/or protocol mismatches. */
1899 if (TYPE_HAS_OBJC_INFO (ltyp))
1901 lcls = TYPE_OBJC_INTERFACE (ltyp);
1902 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
1905 lcls = lproto = NULL_TREE;
1907 if (TYPE_HAS_OBJC_INFO (rtyp))
1909 rcls = TYPE_OBJC_INTERFACE (rtyp);
1910 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
1913 rcls = rproto = NULL_TREE;
1915 /* If we could not find an @interface declaration, we must have
1916 only seen a @class declaration; for purposes of type comparison,
1917 treat it as a stand-alone (root) class. */
1919 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
1922 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
1925 /* If either type is an unqualified 'id', we're done. */
1926 if ((!lproto && objc_is_object_id (ltyp))
1927 || (!rproto && objc_is_object_id (rtyp)))
1930 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
1932 /* If the underlying types are the same, and at most one of them has
1933 a protocol list, we do not need to issue any diagnostics. */
1934 if (pointers_compatible && (!lproto || !rproto))
1937 /* If exactly one of the types is 'Class', issue a diagnostic; any
1938 exceptions of this rule have already been handled. */
1939 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
1940 pointers_compatible = false;
1941 /* Otherwise, check for inheritance relations. */
1944 if (!pointers_compatible)
1946 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
1948 if (!pointers_compatible)
1949 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
1951 if (!pointers_compatible && argno <= -3)
1952 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
1955 /* If the pointers match modulo protocols, check for protocol conformance
1957 if (pointers_compatible)
1959 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
1962 if (!pointers_compatible && argno == -3)
1963 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
1967 if (!pointers_compatible)
1969 /* The two pointers are not exactly compatible. Issue a warning, unless
1970 we are performing a silent comparison, in which case return 'false'
1972 /* NB: For the time being, we shall make our warnings look like their
1973 C counterparts. In the future, we may wish to make them more
1981 warning (0, "comparison of distinct Objective-C types lacks a cast");
1985 warning (0, "initialization from distinct Objective-C type");
1989 warning (0, "assignment from distinct Objective-C type");
1993 warning (0, "distinct Objective-C type in return");
1997 warning (0, "passing argument %d of %qE from distinct "
1998 "Objective-C type", argno, callee);
2006 /* This routine is similar to objc_compare_types except that function-pointers are
2007 excluded. This is because, caller assumes that common types are of (id, Object*)
2008 variety and calls objc_common_type to obtain a common type. There is no commonolty
2009 between two function-pointers in this regard. */
2012 objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
2014 if (objc_compare_types (ltyp, rtyp, argno, callee))
2016 /* exclude function-pointer types. */
2019 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2020 rtyp = TREE_TYPE (rtyp);
2022 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2023 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
2028 /* Check if LTYP and RTYP have the same type qualifiers. If either type
2029 lives in the volatilized hash table, ignore the 'volatile' bit when
2030 making the comparison. */
2033 objc_type_quals_match (tree ltyp, tree rtyp)
2035 int lquals = TYPE_QUALS (ltyp), rquals = TYPE_QUALS (rtyp);
2037 if (lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (ltyp)))
2038 lquals &= ~TYPE_QUAL_VOLATILE;
2040 if (lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (rtyp)))
2041 rquals &= ~TYPE_QUAL_VOLATILE;
2043 return (lquals == rquals);
2047 /* Determine if CHILD is derived from PARENT. The routine assumes that
2048 both parameters are RECORD_TYPEs, and is non-reflexive. */
2051 objc_derived_from_p (tree parent, tree child)
2053 parent = TYPE_MAIN_VARIANT (parent);
2055 for (child = TYPE_MAIN_VARIANT (child);
2056 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
2058 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
2059 (TYPE_BINFO (child),
2062 if (child == parent)
2071 objc_build_component_ref (tree datum, tree component)
2073 /* If COMPONENT is NULL, the caller is referring to the anonymous
2074 base class field. */
2077 tree base = TYPE_FIELDS (TREE_TYPE (datum));
2079 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
2082 /* The 'build_component_ref' routine has been removed from the C++
2083 front-end, but 'finish_class_member_access_expr' seems to be
2084 a worthy substitute. */
2086 return finish_class_member_access_expr (datum, component, false,
2087 tf_warning_or_error);
2089 return build_component_ref (input_location, datum, component);
2093 /* Recursively copy inheritance information rooted at BINFO. To do this,
2094 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
2097 objc_copy_binfo (tree binfo)
2099 tree btype = BINFO_TYPE (binfo);
2100 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
2104 BINFO_TYPE (binfo2) = btype;
2105 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
2106 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
2108 /* Recursively copy base binfos of BINFO. */
2109 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2111 tree base_binfo2 = objc_copy_binfo (base_binfo);
2113 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
2114 BINFO_BASE_APPEND (binfo2, base_binfo2);
2120 /* Record superclass information provided in BASETYPE for ObjC class REF.
2121 This is loosely based on cp/decl.c:xref_basetypes(). */
2124 objc_xref_basetypes (tree ref, tree basetype)
2126 tree binfo = make_tree_binfo (basetype ? 1 : 0);
2128 TYPE_BINFO (ref) = binfo;
2129 BINFO_OFFSET (binfo) = size_zero_node;
2130 BINFO_TYPE (binfo) = ref;
2134 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
2136 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
2137 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, 1);
2138 BINFO_BASE_APPEND (binfo, base_binfo);
2139 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
2143 /* Called from finish_decl. */
2146 objc_check_decl (tree decl)
2148 tree type = TREE_TYPE (decl);
2150 if (TREE_CODE (type) != RECORD_TYPE)
2152 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
2153 error ("statically allocated instance of Objective-C class %qE",
2158 objc_check_global_decl (tree decl)
2160 tree id = DECL_NAME (decl);
2161 if (objc_is_class_name (id) && global_bindings_p())
2162 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id));
2165 /* Return a non-volatalized version of TYPE. */
2168 objc_non_volatilized_type (tree type)
2170 if (lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (type)))
2171 type = build_qualified_type (type, (TYPE_QUALS (type) & ~TYPE_QUAL_VOLATILE));
2175 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where INTERFACE may
2176 either name an Objective-C class, or refer to the special 'id' or 'Class'
2177 types. If INTERFACE is not a valid ObjC type, just return it unchanged. */
2180 objc_get_protocol_qualified_type (tree interface, tree protocols)
2182 /* If INTERFACE is not provided, default to 'id'. */
2183 tree type = (interface ? objc_is_id (interface) : objc_object_type);
2184 bool is_ptr = (type != NULL_TREE);
2188 type = objc_is_class_name (interface);
2192 /* If looking at a typedef, retrieve the precise type it
2194 if (TREE_CODE (interface) == IDENTIFIER_NODE)
2195 interface = identifier_global_value (interface);
2197 type = ((interface && TREE_CODE (interface) == TYPE_DECL
2198 && DECL_ORIGINAL_TYPE (interface))
2199 ? DECL_ORIGINAL_TYPE (interface)
2200 : xref_tag (RECORD_TYPE, type));
2208 type = build_variant_type_copy (type);
2210 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
2214 tree orig_pointee_type = TREE_TYPE (type);
2215 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
2217 /* Set up the canonical type information. */
2218 TYPE_CANONICAL (type)
2219 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
2221 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
2222 type = TREE_TYPE (type);
2225 /* Look up protocols and install in lang specific list. */
2226 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
2227 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols (protocols);
2229 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
2230 return the pointer to the new pointee variant. */
2232 type = TYPE_POINTER_TO (type);
2234 TYPE_OBJC_INTERFACE (type)
2235 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
2241 /* Check for circular dependencies in protocols. The arguments are
2242 PROTO, the protocol to check, and LIST, a list of protocol it
2246 check_protocol_recursively (tree proto, tree list)
2250 for (p = list; p; p = TREE_CHAIN (p))
2252 tree pp = TREE_VALUE (p);
2254 if (TREE_CODE (pp) == IDENTIFIER_NODE)
2255 pp = lookup_protocol (pp);
2258 fatal_error ("protocol %qE has circular dependency",
2259 PROTOCOL_NAME (pp));
2261 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
2265 /* Look up PROTOCOLS, and return a list of those that are found.
2266 If none are found, return NULL. */
2269 lookup_and_install_protocols (tree protocols)
2272 tree return_value = NULL_TREE;
2274 if (protocols == error_mark_node)
2277 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
2279 tree ident = TREE_VALUE (proto);
2280 tree p = lookup_protocol (ident);
2283 return_value = chainon (return_value,
2284 build_tree_list (NULL_TREE, p));
2285 else if (ident != error_mark_node)
2286 error ("cannot find protocol declaration for %qE",
2290 return return_value;
2293 /* Create a declaration for field NAME of a given TYPE. */
2296 create_field_decl (tree type, const char *name)
2298 return build_decl (input_location,
2299 FIELD_DECL, get_identifier (name), type);
2302 /* Create a global, static declaration for variable NAME of a given TYPE. The
2303 finish_var_decl() routine will need to be called on it afterwards. */
2306 start_var_decl (tree type, const char *name)
2308 tree var = build_decl (input_location,
2309 VAR_DECL, get_identifier (name), type);
2311 TREE_STATIC (var) = 1;
2312 DECL_INITIAL (var) = error_mark_node; /* A real initializer is coming... */
2313 DECL_IGNORED_P (var) = 1;
2314 DECL_ARTIFICIAL (var) = 1;
2315 DECL_CONTEXT (var) = NULL_TREE;
2317 DECL_THIS_STATIC (var) = 1; /* squash redeclaration errors */
2323 /* Finish off the variable declaration created by start_var_decl(). */
2326 finish_var_decl (tree var, tree initializer)
2328 finish_decl (var, input_location, initializer, NULL_TREE, NULL_TREE);
2331 /* Find the decl for the constant string class reference. This is only
2332 used for the NeXT runtime. */
2335 setup_string_decl (void)
2340 /* %s in format will provide room for terminating null */
2341 length = strlen (STRING_OBJECT_GLOBAL_FORMAT)
2342 + strlen (constant_string_class_name);
2343 name = XNEWVEC (char, length);
2344 sprintf (name, STRING_OBJECT_GLOBAL_FORMAT,
2345 constant_string_class_name);
2346 constant_string_global_id = get_identifier (name);
2347 string_class_decl = lookup_name (constant_string_global_id);
2349 return string_class_decl;
2352 /* Purpose: "play" parser, creating/installing representations
2353 of the declarations that are required by Objective-C.
2357 type_spec--------->sc_spec
2358 (tree_list) (tree_list)
2361 identifier_node identifier_node */
2364 synth_module_prologue (void)
2367 enum debug_info_type save_write_symbols = write_symbols;
2368 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
2370 /* Suppress outputting debug symbols, because
2371 dbxout_init hasn't been called yet. */
2372 write_symbols = NO_DEBUG;
2373 debug_hooks = &do_nothing_debug_hooks;
2376 push_lang_context (lang_name_c); /* extern "C" */
2379 /* The following are also defined in <objc/objc.h> and friends. */
2381 objc_object_id = get_identifier (TAG_OBJECT);
2382 objc_class_id = get_identifier (TAG_CLASS);
2384 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
2385 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
2387 objc_object_type = build_pointer_type (objc_object_reference);
2388 objc_class_type = build_pointer_type (objc_class_reference);
2390 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
2391 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
2393 /* Declare the 'id' and 'Class' typedefs. */
2395 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2399 TREE_NO_WARNING (type) = 1;
2400 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2404 TREE_NO_WARNING (type) = 1;
2406 /* Forward-declare '@interface Protocol'. */
2408 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
2409 objc_declare_class (tree_cons (NULL_TREE, type, NULL_TREE));
2410 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE,
2413 /* Declare type of selector-objects that represent an operation name. */
2415 if (flag_next_runtime)
2416 /* `struct objc_selector *' */
2418 = build_pointer_type (xref_tag (RECORD_TYPE,
2419 get_identifier (TAG_SELECTOR)));
2421 /* `const struct objc_selector *' */
2423 = build_pointer_type
2424 (build_qualified_type (xref_tag (RECORD_TYPE,
2425 get_identifier (TAG_SELECTOR)),
2428 /* Declare receiver type used for dispatching messages to 'super'. */
2430 /* `struct objc_super *' */
2431 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
2432 get_identifier (TAG_SUPER)));
2434 /* Declare pointers to method and ivar lists. */
2435 objc_method_list_ptr = build_pointer_type
2436 (xref_tag (RECORD_TYPE,
2437 get_identifier (UTAG_METHOD_LIST)));
2438 objc_method_proto_list_ptr
2439 = build_pointer_type (xref_tag (RECORD_TYPE,
2440 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
2441 objc_ivar_list_ptr = build_pointer_type
2442 (xref_tag (RECORD_TYPE,
2443 get_identifier (UTAG_IVAR_LIST)));
2445 /* TREE_NOTHROW is cleared for the message-sending functions,
2446 because the function that gets called can throw in Obj-C++, or
2447 could itself call something that can throw even in Obj-C. */
2449 if (flag_next_runtime)
2451 /* NB: In order to call one of the ..._stret (struct-returning)
2452 functions, the function *MUST* first be cast to a signature that
2453 corresponds to the actual ObjC method being invoked. This is
2454 what is done by the build_objc_method_call() routine below. */
2456 /* id objc_msgSend (id, SEL, ...); */
2457 /* id objc_msgSendNonNil (id, SEL, ...); */
2458 /* id objc_msgSend_stret (id, SEL, ...); */
2459 /* id objc_msgSendNonNil_stret (id, SEL, ...); */
2461 = build_varargs_function_type_list (objc_object_type,
2465 umsg_decl = add_builtin_function (TAG_MSGSEND,
2466 type, 0, NOT_BUILT_IN,
2468 umsg_nonnil_decl = add_builtin_function (TAG_MSGSEND_NONNIL,
2469 type, 0, NOT_BUILT_IN,
2471 umsg_stret_decl = add_builtin_function (TAG_MSGSEND_STRET,
2472 type, 0, NOT_BUILT_IN,
2474 umsg_nonnil_stret_decl = add_builtin_function (TAG_MSGSEND_NONNIL_STRET,
2475 type, 0, NOT_BUILT_IN,
2478 /* These can throw, because the function that gets called can throw
2479 in Obj-C++, or could itself call something that can throw even
2481 TREE_NOTHROW (umsg_decl) = 0;
2482 TREE_NOTHROW (umsg_nonnil_decl) = 0;
2483 TREE_NOTHROW (umsg_stret_decl) = 0;
2484 TREE_NOTHROW (umsg_nonnil_stret_decl) = 0;
2486 /* id objc_msgSend_Fast (id, SEL, ...)
2487 __attribute__ ((hard_coded_address (OFFS_MSGSEND_FAST))); */
2488 #ifdef OFFS_MSGSEND_FAST
2489 umsg_fast_decl = add_builtin_function (TAG_MSGSEND_FAST,
2490 type, 0, NOT_BUILT_IN,
2492 TREE_NOTHROW (umsg_fast_decl) = 0;
2493 DECL_ATTRIBUTES (umsg_fast_decl)
2494 = tree_cons (get_identifier ("hard_coded_address"),
2495 build_int_cst (NULL_TREE, OFFS_MSGSEND_FAST),
2498 /* No direct dispatch available. */
2499 umsg_fast_decl = umsg_decl;
2502 /* id objc_msgSendSuper (struct objc_super *, SEL, ...); */
2503 /* id objc_msgSendSuper_stret (struct objc_super *, SEL, ...); */
2505 = build_varargs_function_type_list (objc_object_type,
2509 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
2510 type, 0, NOT_BUILT_IN,
2512 umsg_super_stret_decl = add_builtin_function (TAG_MSGSENDSUPER_STRET,
2513 type, 0, NOT_BUILT_IN, 0,
2515 TREE_NOTHROW (umsg_super_decl) = 0;
2516 TREE_NOTHROW (umsg_super_stret_decl) = 0;
2520 /* GNU runtime messenger entry points. */
2522 /* typedef id (*IMP)(id, SEL, ...); */
2524 build_varargs_function_type_list (objc_object_type,
2528 tree IMP_type = build_pointer_type (ftype);
2530 /* IMP objc_msg_lookup (id, SEL); */
2531 type = build_function_type_list (IMP_type,
2535 umsg_decl = add_builtin_function (TAG_MSGSEND,
2536 type, 0, NOT_BUILT_IN,
2538 TREE_NOTHROW (umsg_decl) = 0;
2540 /* IMP objc_msg_lookup_super (struct objc_super *, SEL); */
2542 = build_function_type_list (IMP_type,
2546 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
2547 type, 0, NOT_BUILT_IN,
2549 TREE_NOTHROW (umsg_super_decl) = 0;
2551 /* The following GNU runtime entry point is called to initialize
2554 __objc_exec_class (void *); */
2556 = build_function_type_list (void_type_node,
2559 execclass_decl = add_builtin_function (TAG_EXECCLASS,
2560 type, 0, NOT_BUILT_IN,
2564 /* id objc_getClass (const char *); */
2566 type = build_function_type_list (objc_object_type,
2567 const_string_type_node,
2571 = add_builtin_function (TAG_GETCLASS, type, 0, NOT_BUILT_IN,
2574 /* id objc_getMetaClass (const char *); */
2576 objc_get_meta_class_decl
2577 = add_builtin_function (TAG_GETMETACLASS, type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
2579 build_class_template ();
2580 build_super_template ();
2581 build_protocol_template ();
2582 build_category_template ();
2583 build_objc_exception_stuff ();
2585 /* Declare objc_getProperty, object_setProperty and other property
2586 accessor helpers. */
2587 build_objc_property_accessor_helpers ();
2589 if (flag_next_runtime)
2590 build_next_objc_exception_stuff ();
2592 /* static SEL _OBJC_SELECTOR_TABLE[]; */
2594 if (! flag_next_runtime)
2595 build_selector_table_decl ();
2597 /* Forward declare constant_string_id and constant_string_type. */
2598 if (!constant_string_class_name)
2599 constant_string_class_name = default_constant_string_class_name;
2601 constant_string_id = get_identifier (constant_string_class_name);
2602 objc_declare_class (tree_cons (NULL_TREE, constant_string_id, NULL_TREE));
2604 /* Pre-build the following entities - for speed/convenience. */
2605 self_id = get_identifier ("self");
2606 ucmd_id = get_identifier ("_cmd");
2608 /* Declare struct _objc_fast_enumeration_state { ... }; */
2609 build_fast_enumeration_state_template ();
2611 /* void objc_enumeration_mutation (id) */
2612 type = build_function_type (void_type_node,
2613 tree_cons (NULL_TREE, objc_object_type, NULL_TREE));
2614 objc_enumeration_mutation_decl
2615 = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN,
2617 TREE_NOTHROW (objc_enumeration_mutation_decl) = 0;
2620 pop_lang_context ();
2623 write_symbols = save_write_symbols;
2624 debug_hooks = save_hooks;
2627 /* Ensure that the ivar list for NSConstantString/NXConstantString
2628 (or whatever was specified via `-fconstant-string-class')
2629 contains fields at least as large as the following three, so that
2630 the runtime can stomp on them with confidence:
2632 struct STRING_OBJECT_CLASS_NAME
2636 unsigned int length;
2640 check_string_class_template (void)
2642 tree field_decl = objc_get_class_ivars (constant_string_id);
2644 #define AT_LEAST_AS_LARGE_AS(F, T) \
2645 (F && TREE_CODE (F) == FIELD_DECL \
2646 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
2647 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
2649 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
2652 field_decl = DECL_CHAIN (field_decl);
2653 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
2656 field_decl = DECL_CHAIN (field_decl);
2657 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
2659 #undef AT_LEAST_AS_LARGE_AS
2662 /* Avoid calling `check_string_class_template ()' more than once. */
2663 static GTY(()) int string_layout_checked;
2665 /* Construct an internal string layout to be used as a template for
2666 creating NSConstantString/NXConstantString instances. */
2669 objc_build_internal_const_str_type (void)
2671 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
2672 tree fields = build_decl (input_location,
2673 FIELD_DECL, NULL_TREE, ptr_type_node);
2674 tree field = build_decl (input_location,
2675 FIELD_DECL, NULL_TREE, ptr_type_node);
2677 DECL_CHAIN (field) = fields; fields = field;
2678 field = build_decl (input_location,
2679 FIELD_DECL, NULL_TREE, unsigned_type_node);
2680 DECL_CHAIN (field) = fields; fields = field;
2681 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
2683 finish_builtin_struct (type, "__builtin_ObjCString",
2689 /* Custom build_string which sets TREE_TYPE! */
2692 my_build_string (int len, const char *str)
2694 return fix_string_type (build_string (len, str));
2697 /* Build a string with contents STR and length LEN and convert it to a
2701 my_build_string_pointer (int len, const char *str)
2703 tree string = my_build_string (len, str);
2704 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
2705 return build1 (ADDR_EXPR, ptrtype, string);
2709 string_hash (const void *ptr)
2711 const_tree const str = ((const struct string_descriptor *)ptr)->literal;
2712 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
2713 int i, len = TREE_STRING_LENGTH (str);
2716 for (i = 0; i < len; i++)
2717 h = ((h * 613) + p[i]);
2723 string_eq (const void *ptr1, const void *ptr2)
2725 const_tree const str1 = ((const struct string_descriptor *)ptr1)->literal;
2726 const_tree const str2 = ((const struct string_descriptor *)ptr2)->literal;
2727 int len1 = TREE_STRING_LENGTH (str1);
2729 return (len1 == TREE_STRING_LENGTH (str2)
2730 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
2734 /* Given a chain of STRING_CST's, build a static instance of
2735 NXConstantString which points at the concatenation of those
2736 strings. We place the string object in the __string_objects
2737 section of the __OBJC segment. The Objective-C runtime will
2738 initialize the isa pointers of the string objects to point at the
2739 NXConstantString class object. */
2742 objc_build_string_object (tree string)
2744 tree constant_string_class;
2747 struct string_descriptor *desc, key;
2750 /* Prep the string argument. */
2751 string = fix_string_type (string);
2752 TREE_SET_CODE (string, STRING_CST);
2753 length = TREE_STRING_LENGTH (string) - 1;
2755 /* The target may have different ideas on how to construct an ObjC string
2756 literal. On Darwin (Mac OS X), for example, we may wish to obtain a
2757 constant CFString reference instead.
2758 At present, this is only supported for the NeXT runtime. */
2759 if (flag_next_runtime && targetcm.objc_construct_string_object)
2761 tree constructor = (*targetcm.objc_construct_string_object) (string);
2763 return build1 (NOP_EXPR, objc_object_type, constructor);
2766 /* Check whether the string class being used actually exists and has the
2767 correct ivar layout. */
2768 if (!string_layout_checked)
2770 string_layout_checked = -1;
2771 constant_string_class = lookup_interface (constant_string_id);
2772 internal_const_str_type = objc_build_internal_const_str_type ();
2774 if (!constant_string_class
2775 || !(constant_string_type
2776 = CLASS_STATIC_TEMPLATE (constant_string_class)))
2777 error ("cannot find interface declaration for %qE",
2778 constant_string_id);
2779 /* The NSConstantString/NXConstantString ivar layout is now known. */
2780 else if (!check_string_class_template ())
2781 error ("interface %qE does not have valid constant string layout",
2782 constant_string_id);
2783 /* For the NeXT runtime, we can generate a literal reference
2784 to the string class, don't need to run a constructor. */
2785 else if (flag_next_runtime && !setup_string_decl ())
2786 error ("cannot find reference tag for class %qE",
2787 constant_string_id);
2790 string_layout_checked = 1; /* Success! */
2791 add_class_reference (constant_string_id);
2795 if (string_layout_checked == -1)
2796 return error_mark_node;
2798 /* Perhaps we already constructed a constant string just like this one? */
2799 key.literal = string;
2800 loc = htab_find_slot (string_htab, &key, INSERT);
2801 desc = (struct string_descriptor *) *loc;
2805 tree var, constructor;
2806 VEC(constructor_elt,gc) *v = NULL;
2807 *loc = desc = ggc_alloc_string_descriptor ();
2808 desc->literal = string;
2810 /* GNU: (NXConstantString *) & ((__builtin_ObjCString) { NULL, string, length }) */
2811 /* NeXT: (NSConstantString *) & ((__builtin_ObjCString) { isa, string, length }) */
2812 fields = TYPE_FIELDS (internal_const_str_type);
2813 CONSTRUCTOR_APPEND_ELT (v, fields,
2815 ? build_unary_op (input_location,
2816 ADDR_EXPR, string_class_decl, 0)
2817 : build_int_cst (NULL_TREE, 0));
2818 fields = DECL_CHAIN (fields);
2819 CONSTRUCTOR_APPEND_ELT (v, fields,
2820 build_unary_op (input_location,
2821 ADDR_EXPR, string, 1));
2822 fields = DECL_CHAIN (fields);
2823 CONSTRUCTOR_APPEND_ELT (v, fields, build_int_cst (NULL_TREE, length));
2824 constructor = objc_build_constructor (internal_const_str_type, v);
2826 if (!flag_next_runtime)
2828 = objc_add_static_instance (constructor, constant_string_type);
2831 var = build_decl (input_location,
2832 CONST_DECL, NULL, TREE_TYPE (constructor));
2833 DECL_INITIAL (var) = constructor;
2834 TREE_STATIC (var) = 1;
2835 pushdecl_top_level (var);
2838 desc->constructor = constructor;
2841 addr = convert (build_pointer_type (constant_string_type),
2842 build_unary_op (input_location,
2843 ADDR_EXPR, desc->constructor, 1));
2848 /* Declare a static instance of CLASS_DECL initialized by CONSTRUCTOR. */
2850 static GTY(()) int num_static_inst;
2853 objc_add_static_instance (tree constructor, tree class_decl)
2858 /* Find the list of static instances for the CLASS_DECL. Create one if
2860 for (chain = &objc_static_instances;
2861 *chain && TREE_VALUE (*chain) != class_decl;
2862 chain = &TREE_CHAIN (*chain));
2865 *chain = tree_cons (NULL_TREE, class_decl, NULL_TREE);
2866 add_objc_string (OBJC_TYPE_NAME (class_decl), class_names);
2869 sprintf (buf, "_OBJC_INSTANCE_%d", num_static_inst++);
2870 decl = build_decl (input_location,
2871 VAR_DECL, get_identifier (buf), class_decl);
2872 TREE_STATIC (decl) = 1;
2873 DECL_ARTIFICIAL (decl) = 1;
2874 TREE_USED (decl) = 1;
2875 DECL_INITIAL (decl) = constructor;
2877 /* We may be writing something else just now.
2878 Postpone till end of input. */
2879 DECL_DEFER_OUTPUT (decl) = 1;
2880 pushdecl_top_level (decl);
2881 rest_of_decl_compilation (decl, 1, 0);
2883 /* Add the DECL to the head of this CLASS' list. */
2884 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE, decl, TREE_PURPOSE (*chain));
2889 /* Build a static constant CONSTRUCTOR
2890 with type TYPE and elements ELTS. */
2893 objc_build_constructor (tree type, VEC(constructor_elt,gc) *elts)
2895 tree constructor = build_constructor (type, elts);
2897 TREE_CONSTANT (constructor) = 1;
2898 TREE_STATIC (constructor) = 1;
2899 TREE_READONLY (constructor) = 1;
2902 /* Adjust for impedance mismatch. We should figure out how to build
2903 CONSTRUCTORs that consistently please both the C and C++ gods. */
2904 if (!VEC_index (constructor_elt, elts, 0)->index)
2905 TREE_TYPE (constructor) = init_list_type_node;
2911 /* Take care of defining and initializing _OBJC_SYMBOLS. */
2913 /* Predefine the following data type:
2921 void *defs[cls_def_cnt + cat_def_cnt];
2925 build_objc_symtab_template (void)
2927 tree fields, *chain = NULL;
2929 objc_symtab_template = objc_start_struct (get_identifier (UTAG_SYMTAB));
2931 /* long sel_ref_cnt; */
2932 fields = add_field_decl (long_integer_type_node, "sel_ref_cnt", &chain);
2935 add_field_decl (build_pointer_type (objc_selector_type), "refs", &chain);
2937 /* short cls_def_cnt; */
2938 add_field_decl (short_integer_type_node, "cls_def_cnt", &chain);
2940 /* short cat_def_cnt; */
2941 add_field_decl (short_integer_type_node, "cat_def_cnt", &chain);
2943 if (imp_count || cat_count || !flag_next_runtime)
2945 /* void *defs[imp_count + cat_count (+ 1)]; */
2946 /* NB: The index is one less than the size of the array. */
2947 int index = imp_count + cat_count + (flag_next_runtime ? -1: 0);
2948 tree array_type = build_sized_array_type (ptr_type_node, index + 1);
2949 add_field_decl (array_type, "defs", &chain);
2952 objc_finish_struct (objc_symtab_template, fields);
2955 /* Create the initial value for the `defs' field of _objc_symtab.
2956 This is a CONSTRUCTOR. */
2959 init_def_list (tree type)
2962 struct imp_entry *impent;
2963 VEC(constructor_elt,gc) *v = NULL;
2966 for (impent = imp_list; impent; impent = impent->next)
2968 if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
2970 expr = build_unary_op (input_location,
2971 ADDR_EXPR, impent->class_decl, 0);
2972 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2977 for (impent = imp_list; impent; impent = impent->next)
2979 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
2981 expr = build_unary_op (input_location,
2982 ADDR_EXPR, impent->class_decl, 0);
2983 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2987 if (!flag_next_runtime)
2989 /* statics = { ..., _OBJC_STATIC_INSTANCES, ... } */
2990 if (static_instances_decl)
2991 expr = build_unary_op (input_location,
2992 ADDR_EXPR, static_instances_decl, 0);
2994 expr = integer_zero_node;
2996 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2999 return objc_build_constructor (type, v);
3002 /* Construct the initial value for all of _objc_symtab. */
3005 init_objc_symtab (tree type)
3007 VEC(constructor_elt,gc) *v = NULL;
3009 /* sel_ref_cnt = { ..., 5, ... } */
3011 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3012 build_int_cst (long_integer_type_node, 0));
3014 /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
3016 if (flag_next_runtime || ! sel_ref_chain)
3017 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, convert (
3018 build_pointer_type (objc_selector_type),
3019 integer_zero_node));
3022 tree expr = build_unary_op (input_location, ADDR_EXPR,
3023 UOBJC_SELECTOR_TABLE_decl, 1);
3025 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3026 convert (build_pointer_type (objc_selector_type),
3030 /* cls_def_cnt = { ..., 5, ... } */
3032 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3033 build_int_cst (short_integer_type_node, imp_count));
3035 /* cat_def_cnt = { ..., 5, ... } */
3037 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3038 build_int_cst (short_integer_type_node, cat_count));
3040 /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
3042 if (imp_count || cat_count || !flag_next_runtime)
3045 tree field = TYPE_FIELDS (type);
3046 field = DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (field))));
3048 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init_def_list (TREE_TYPE (field)));
3051 return objc_build_constructor (type, v);
3054 /* Generate forward declarations for metadata such as
3055 'OBJC_CLASS_...'. */
3058 build_metadata_decl (const char *name, tree type)
3062 /* struct TYPE NAME_<name>; */
3063 decl = start_var_decl (type, synth_id_with_class_suffix
3065 objc_implementation_context));
3070 /* Push forward-declarations of all the categories so that
3071 init_def_list can use them in a CONSTRUCTOR. */
3074 forward_declare_categories (void)
3076 struct imp_entry *impent;
3077 tree sav = objc_implementation_context;
3079 for (impent = imp_list; impent; impent = impent->next)
3081 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
3083 /* Set an invisible arg to synth_id_with_class_suffix. */
3084 objc_implementation_context = impent->imp_context;
3085 /* extern struct objc_category _OBJC_CATEGORY_<name>; */
3086 impent->class_decl = build_metadata_decl ("_OBJC_CATEGORY",
3087 objc_category_template);
3090 objc_implementation_context = sav;
3093 /* Create the declaration of _OBJC_SYMBOLS, with type `struct _objc_symtab'
3094 and initialized appropriately. */
3097 generate_objc_symtab_decl (void)
3100 build_objc_symtab_template ();
3101 UOBJC_SYMBOLS_decl = start_var_decl (objc_symtab_template, "_OBJC_SYMBOLS");
3102 finish_var_decl (UOBJC_SYMBOLS_decl,
3103 init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl)));
3107 init_module_descriptor (tree type)
3110 VEC(constructor_elt,gc) *v = NULL;
3112 /* version = { 1, ... } */
3114 expr = build_int_cst (long_integer_type_node, OBJC_VERSION);
3115 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3117 /* size = { ..., sizeof (struct _objc_module), ... } */
3119 expr = convert (long_integer_type_node,
3120 size_in_bytes (objc_module_template));
3121 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3123 /* Don't provide any file name for security reasons. */
3124 /* name = { ..., "", ... } */
3126 expr = add_objc_string (get_identifier (""), class_names);
3127 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3129 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
3131 if (UOBJC_SYMBOLS_decl)
3132 expr = build_unary_op (input_location,
3133 ADDR_EXPR, UOBJC_SYMBOLS_decl, 0);
3135 expr = null_pointer_node;
3136 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3138 return objc_build_constructor (type, v);
3141 /* Write out the data structures to describe Objective C classes defined.
3143 struct _objc_module { ... } _OBJC_MODULE = { ... }; */
3146 build_module_descriptor (void)
3148 tree decls, *chain = NULL;
3151 push_lang_context (lang_name_c); /* extern "C" */
3154 objc_module_template = objc_start_struct (get_identifier (UTAG_MODULE));
3157 decls = add_field_decl (long_integer_type_node, "version", &chain);
3160 add_field_decl (long_integer_type_node, "size", &chain);
3163 add_field_decl (string_type_node, "name", &chain);
3165 /* struct _objc_symtab *symtab; */
3166 add_field_decl (build_pointer_type (xref_tag (RECORD_TYPE,
3167 get_identifier (UTAG_SYMTAB))),
3170 objc_finish_struct (objc_module_template, decls);
3172 /* Create an instance of "_objc_module". */
3173 UOBJC_MODULES_decl = start_var_decl (objc_module_template, "_OBJC_MODULES");
3174 /* This is the root of the metadata for defined classes and categories, it
3175 is referenced by the runtime and, therefore, needed. */
3176 DECL_PRESERVE_P (UOBJC_MODULES_decl) = 1;
3177 finish_var_decl (UOBJC_MODULES_decl,
3178 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl)));
3181 pop_lang_context ();
3185 /* The GNU runtime requires us to provide a static initializer function
3188 static void __objc_gnu_init (void) {
3189 __objc_exec_class (&L_OBJC_MODULES);
3193 build_module_initializer_routine (void)
3198 push_lang_context (lang_name_c); /* extern "C" */
3201 objc_push_parm (build_decl (input_location,
3202 PARM_DECL, NULL_TREE, void_type_node));
3204 objc_start_function (get_identifier (TAG_GNUINIT),
3205 build_function_type_list (void_type_node, NULL_TREE),
3206 NULL_TREE, NULL_TREE);
3208 objc_start_function (get_identifier (TAG_GNUINIT),
3209 build_function_type_list (void_type_node, NULL_TREE),
3210 NULL_TREE, objc_get_parm_info (0));
3212 body = c_begin_compound_stmt (true);
3213 add_stmt (build_function_call
3218 build_unary_op (input_location, ADDR_EXPR,
3219 UOBJC_MODULES_decl, 0))));
3220 add_stmt (c_end_compound_stmt (input_location, body, true));
3222 TREE_PUBLIC (current_function_decl) = 0;
3225 /* For Objective-C++, we will need to call __objc_gnu_init
3226 from objc_generate_static_init_call() below. */
3227 DECL_STATIC_CONSTRUCTOR (current_function_decl) = 1;
3230 GNU_INIT_decl = current_function_decl;
3234 pop_lang_context ();
3239 /* Return 1 if the __objc_gnu_init function has been synthesized and needs
3240 to be called by the module initializer routine. */
3243 objc_static_init_needed_p (void)
3245 return (GNU_INIT_decl != NULL_TREE);
3248 /* Generate a call to the __objc_gnu_init initializer function. */
3251 objc_generate_static_init_call (tree ctors ATTRIBUTE_UNUSED)
3253 add_stmt (build_stmt (input_location, EXPR_STMT,
3254 build_function_call (input_location,
3255 GNU_INIT_decl, NULL_TREE)));
3259 #endif /* OBJCPLUS */
3261 /* Return the DECL of the string IDENT in the SECTION. */
3264 get_objc_string_decl (tree ident, enum string_section section)
3271 chain = class_names_chain;
3273 case meth_var_names:
3274 chain = meth_var_names_chain;
3276 case meth_var_types:
3277 chain = meth_var_types_chain;
3283 for (; chain != 0; chain = TREE_CHAIN (chain))
3284 if (TREE_VALUE (chain) == ident)
3285 return (TREE_PURPOSE (chain));
3291 /* Output references to all statically allocated objects. Return the DECL
3292 for the array built. */
3295 generate_static_references (void)
3297 tree expr = NULL_TREE;
3298 tree class_name, klass, decl;
3299 tree cl_chain, in_chain, type
3300 = build_array_type (build_pointer_type (void_type_node), NULL_TREE);
3301 int num_inst, num_class;
3303 VEC(constructor_elt,gc) *decls = NULL;
3305 if (flag_next_runtime)
3308 for (cl_chain = objc_static_instances, num_class = 0;
3309 cl_chain; cl_chain = TREE_CHAIN (cl_chain), num_class++)
3311 VEC(constructor_elt,gc) *v = NULL;
3313 for (num_inst = 0, in_chain = TREE_PURPOSE (cl_chain);
3314 in_chain; num_inst++, in_chain = TREE_CHAIN (in_chain));
3316 sprintf (buf, "_OBJC_STATIC_INSTANCES_%d", num_class);
3317 decl = start_var_decl (type, buf);
3319 /* Output {class_name, ...}. */
3320 klass = TREE_VALUE (cl_chain);
3321 class_name = get_objc_string_decl (OBJC_TYPE_NAME (klass), class_names);
3322 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3323 build_unary_op (input_location,
3324 ADDR_EXPR, class_name, 1));
3326 /* Output {..., instance, ...}. */
3327 for (in_chain = TREE_PURPOSE (cl_chain);
3328 in_chain; in_chain = TREE_CHAIN (in_chain))
3330 expr = build_unary_op (input_location,
3331 ADDR_EXPR, TREE_VALUE (in_chain), 1);
3332 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3335 /* Output {..., NULL}. */
3336 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
3338 expr = objc_build_constructor (TREE_TYPE (decl), v);
3339 finish_var_decl (decl, expr);
3340 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE,
3341 build_unary_op (input_location,
3342 ADDR_EXPR, decl, 1));
3345 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE, build_int_cst (NULL_TREE, 0));
3346 expr = objc_build_constructor (type, decls);
3347 static_instances_decl = start_var_decl (type, "_OBJC_STATIC_INSTANCES");
3348 finish_var_decl (static_instances_decl, expr);
3351 static GTY(()) int selector_reference_idx;
3354 build_selector_reference_decl (void)
3359 sprintf (buf, "_OBJC_SELECTOR_REFERENCES_%d", selector_reference_idx++);
3360 decl = start_var_decl (objc_selector_type, buf);
3366 build_selector_table_decl (void)
3370 if (flag_typed_selectors)
3372 build_selector_template ();
3373 temp = build_array_type (objc_selector_template, NULL_TREE);
3376 temp = build_array_type (objc_selector_type, NULL_TREE);
3378 UOBJC_SELECTOR_TABLE_decl = start_var_decl (temp, "_OBJC_SELECTOR_TABLE");
3381 /* Just a handy wrapper for add_objc_string. */
3384 build_selector (tree ident)
3386 return convert (objc_selector_type,
3387 add_objc_string (ident, meth_var_names));
3390 /* Used only by build_*_selector_translation_table (). */
3392 diagnose_missing_method (tree meth, location_t here)
3396 for (method_chain = meth_var_names_chain;
3398 method_chain = TREE_CHAIN (method_chain))
3400 if (TREE_VALUE (method_chain) == meth)
3408 warning_at (here, 0, "creating selector for nonexistent method %qE",
3413 build_next_selector_translation_table (void)
3416 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
3419 tree decl = TREE_PURPOSE (chain);
3420 if (warn_selector && objc_implementation_context)
3424 loc = DECL_SOURCE_LOCATION (decl);
3426 loc = input_location;
3427 diagnose_missing_method (TREE_VALUE (chain), loc);
3430 expr = build_selector (TREE_VALUE (chain));
3434 /* Entries of this form are used for references to methods.
3435 The runtime re-writes these on start-up, but the compiler can't see
3436 that and optimizes it away unless we force it. */
3437 DECL_PRESERVE_P (decl) = 1;
3438 finish_var_decl (decl, expr);
3444 build_gnu_selector_translation_table (void)
3448 tree decl = NULL_TREE;*/
3449 VEC(constructor_elt,gc) *inits = NULL;
3451 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
3455 if (warn_selector && objc_implementation_context)
3456 diagnose_missing_method (TREE_VALUE (chain), input_location);
3458 expr = build_selector (TREE_VALUE (chain));
3459 /* add one for the '\0' character
3460 offset += IDENTIFIER_LENGTH (TREE_VALUE (chain)) + 1;*/
3463 if (flag_typed_selectors)
3465 VEC(constructor_elt,gc) *v = NULL;
3466 tree encoding = get_proto_encoding (TREE_PURPOSE (chain));
3467 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3468 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, encoding);
3469 expr = objc_build_constructor (objc_selector_template, v);
3472 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
3474 } /* each element in the chain */
3477 /* Cause the selector table (previously forward-declared)
3478 to be actually output. */
3481 if (flag_typed_selectors)
3483 VEC(constructor_elt,gc) *v = NULL;
3484 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
3485 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
3486 expr = objc_build_constructor (objc_selector_template, v);
3489 expr = integer_zero_node;
3491 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
3492 expr = objc_build_constructor (TREE_TYPE (UOBJC_SELECTOR_TABLE_decl),
3494 finish_var_decl (UOBJC_SELECTOR_TABLE_decl, expr);
3499 get_proto_encoding (tree proto)
3504 if (! METHOD_ENCODING (proto))
3506 encoding = encode_method_prototype (proto);
3507 METHOD_ENCODING (proto) = encoding;
3510 encoding = METHOD_ENCODING (proto);
3512 return add_objc_string (encoding, meth_var_types);
3515 return build_int_cst (NULL_TREE, 0);
3518 /* sel_ref_chain is a list whose "value" fields will be instances of
3519 identifier_node that represent the selector. LOC is the location of
3523 build_typed_selector_reference (location_t loc, tree ident, tree prototype)
3525 tree *chain = &sel_ref_chain;
3531 if (TREE_PURPOSE (*chain) == prototype && TREE_VALUE (*chain) == ident)
3532 goto return_at_index;
3535 chain = &TREE_CHAIN (*chain);
3538 *chain = tree_cons (prototype, ident, NULL_TREE);
3541 expr = build_unary_op (loc, ADDR_EXPR,
3542 build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
3543 build_int_cst (NULL_TREE, index)),
3545 return convert (objc_selector_type, expr);
3549 build_selector_reference (location_t loc, tree ident)
3551 tree *chain = &sel_ref_chain;
3557 if (TREE_VALUE (*chain) == ident)
3558 return (flag_next_runtime
3559 ? TREE_PURPOSE (*chain)
3560 : build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
3561 build_int_cst (NULL_TREE, index)));
3564 chain = &TREE_CHAIN (*chain);
3567 expr = (flag_next_runtime ? build_selector_reference_decl (): NULL_TREE);
3569 *chain = tree_cons (expr, ident, NULL_TREE);
3571 return (flag_next_runtime
3573 : build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
3574 build_int_cst (NULL_TREE, index)));
3577 static GTY(()) int class_reference_idx;
3580 build_class_reference_decl (void)
3585 sprintf (buf, "_OBJC_CLASS_REFERENCES_%d", class_reference_idx++);
3586 decl = start_var_decl (objc_class_type, buf);
3591 /* Create a class reference, but don't create a variable to reference
3595 add_class_reference (tree ident)
3599 if ((chain = cls_ref_chain))
3604 if (ident == TREE_VALUE (chain))
3608 chain = TREE_CHAIN (chain);
3612 /* Append to the end of the list */
3613 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
3616 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
3619 /* Get a class reference, creating it if necessary. Also create the
3620 reference variable. */
3623 objc_get_class_reference (tree ident)
3625 tree orig_ident = (DECL_P (ident)
3628 ? OBJC_TYPE_NAME (ident)
3630 bool local_scope = false;
3633 if (processing_template_decl)
3634 /* Must wait until template instantiation time. */
3635 return build_min_nt (CLASS_REFERENCE_EXPR, ident);
3638 if (TREE_CODE (ident) == TYPE_DECL)
3639 ident = (DECL_ORIGINAL_TYPE (ident)
3640 ? DECL_ORIGINAL_TYPE (ident)
3641 : TREE_TYPE (ident));
3645 && CP_TYPE_CONTEXT (ident) != global_namespace)
3649 if (local_scope || !(ident = objc_is_class_name (ident)))
3651 error ("%qE is not an Objective-C class name or alias",
3653 return error_mark_node;
3656 if (flag_next_runtime && !flag_zero_link)
3661 for (chain = &cls_ref_chain; *chain; chain = &TREE_CHAIN (*chain))
3662 if (TREE_VALUE (*chain) == ident)
3664 if (! TREE_PURPOSE (*chain))
3665 TREE_PURPOSE (*chain) = build_class_reference_decl ();
3667 return TREE_PURPOSE (*chain);
3670 decl = build_class_reference_decl ();
3671 *chain = tree_cons (decl, ident, NULL_TREE);
3678 add_class_reference (ident);
3680 params = build_tree_list (NULL_TREE,
3681 my_build_string_pointer
3682 (IDENTIFIER_LENGTH (ident) + 1,
3683 IDENTIFIER_POINTER (ident)));
3685 assemble_external (objc_get_class_decl);
3686 return build_function_call (input_location, objc_get_class_decl, params);
3690 /* For each string section we have a chain which maps identifier nodes
3691 to decls for the strings. */
3693 static GTY(()) int class_names_idx;
3694 static GTY(()) int meth_var_names_idx;
3695 static GTY(()) int meth_var_types_idx;
3698 add_objc_string (tree ident, enum string_section section)
3700 tree *chain, decl, type, string_expr;
3707 chain = &class_names_chain;
3708 sprintf (buf, "_OBJC_CLASS_NAME_%d", class_names_idx++);
3710 case meth_var_names:
3711 chain = &meth_var_names_chain;
3712 sprintf (buf, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx++);
3714 case meth_var_types:
3715 chain = &meth_var_types_chain;
3716 sprintf (buf, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx++);
3724 if (TREE_VALUE (*chain) == ident)
3725 return convert (string_type_node,
3726 build_unary_op (input_location,
3727 ADDR_EXPR, TREE_PURPOSE (*chain), 1));
3729 chain = &TREE_CHAIN (*chain);
3732 type = build_sized_array_type (char_type_node, IDENTIFIER_LENGTH (ident) + 1);
3733 decl = start_var_decl (type, buf);
3734 string_expr = my_build_string (IDENTIFIER_LENGTH (ident) + 1,
3735 IDENTIFIER_POINTER (ident));
3736 TREE_CONSTANT (decl) = 1;
3737 finish_var_decl (decl, string_expr);
3739 *chain = tree_cons (decl, ident, NULL_TREE);
3741 return convert (string_type_node, build_unary_op (input_location,
3742 ADDR_EXPR, decl, 1));
3746 objc_declare_alias (tree alias_ident, tree class_ident)
3748 tree underlying_class;
3751 if (current_namespace != global_namespace) {
3752 error ("Objective-C declarations may only appear in global scope");
3754 #endif /* OBJCPLUS */
3756 if (!(underlying_class = objc_is_class_name (class_ident)))
3757 warning (0, "cannot find class %qE", class_ident);
3758 else if (objc_is_class_name (alias_ident))
3759 warning (0, "class %qE already exists", alias_ident);
3762 /* Implement @compatibility_alias as a typedef. */
3764 push_lang_context (lang_name_c); /* extern "C" */
3766 lang_hooks.decls.pushdecl (build_decl
3770 xref_tag (RECORD_TYPE, underlying_class)));
3772 pop_lang_context ();
3774 hash_class_name_enter (als_name_hash_list, alias_ident,
3780 objc_declare_class (tree ident_list)
3784 if (current_namespace != global_namespace) {
3785 error ("Objective-C declarations may only appear in global scope");
3787 #endif /* OBJCPLUS */
3789 for (list = ident_list; list; list = TREE_CHAIN (list))
3791 tree ident = TREE_VALUE (list);
3793 if (! objc_is_class_name (ident))
3795 tree record = lookup_name (ident), type = record;
3799 if (TREE_CODE (record) == TYPE_DECL)
3800 type = DECL_ORIGINAL_TYPE (record) ?
3801 DECL_ORIGINAL_TYPE (record) :
3804 if (!TYPE_HAS_OBJC_INFO (type)
3805 || !TYPE_OBJC_INTERFACE (type))
3807 error ("%qE redeclared as different kind of symbol",
3809 error ("previous declaration of %q+D",
3814 record = xref_tag (RECORD_TYPE, ident);
3815 INIT_TYPE_OBJC_INFO (record);
3816 TYPE_OBJC_INTERFACE (record) = ident;
3817 hash_class_name_enter (cls_name_hash_list, ident, NULL_TREE);
3823 objc_is_class_name (tree ident)
3827 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE
3828 && identifier_global_value (ident))
3829 ident = identifier_global_value (ident);
3830 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
3831 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
3833 if (ident && TREE_CODE (ident) == RECORD_TYPE)
3834 ident = OBJC_TYPE_NAME (ident);
3836 if (ident && TREE_CODE (ident) == TYPE_DECL)
3838 tree type = TREE_TYPE (ident);
3839 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
3841 ident = DECL_NAME (ident);
3844 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
3847 if (lookup_interface (ident))
3850 target = hash_class_name_lookup (cls_name_hash_list, ident);
3854 target = hash_class_name_lookup (als_name_hash_list, ident);
3857 gcc_assert (target->list && target->list->value);
3858 return target->list->value;
3864 /* Check whether TYPE is either 'id' or 'Class'. */
3867 objc_is_id (tree type)
3869 if (type && TREE_CODE (type) == IDENTIFIER_NODE
3870 && identifier_global_value (type))
3871 type = identifier_global_value (type);
3873 if (type && TREE_CODE (type) == TYPE_DECL)
3874 type = TREE_TYPE (type);
3876 /* NB: This function may be called before the ObjC front-end has
3877 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3878 return (objc_object_type && type
3879 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
3884 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3885 class instance. This is needed by other parts of the compiler to
3886 handle ObjC types gracefully. */
3889 objc_is_object_ptr (tree type)
3893 type = TYPE_MAIN_VARIANT (type);
3894 if (!POINTER_TYPE_P (type))
3897 ret = objc_is_id (type);
3899 ret = objc_is_class_name (TREE_TYPE (type));
3905 objc_is_gcable_type (tree type, int or_strong_p)
3911 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
3913 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
3915 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
3917 type = TREE_TYPE (type);
3918 if (TREE_CODE (type) != RECORD_TYPE)
3920 name = TYPE_NAME (type);
3921 return (objc_is_class_name (name) != NULL_TREE);
3925 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
3927 if (expr == oldexpr)
3930 switch (TREE_CODE (expr))
3933 return objc_build_component_ref
3934 (objc_substitute_decl (TREE_OPERAND (expr, 0),
3937 DECL_NAME (TREE_OPERAND (expr, 1)));
3939 return build_array_ref (input_location,
3940 objc_substitute_decl (TREE_OPERAND (expr, 0),
3943 TREE_OPERAND (expr, 1));
3945 return build_indirect_ref (input_location,
3946 objc_substitute_decl (TREE_OPERAND (expr, 0),
3948 newexpr), RO_ARROW);
3955 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
3958 /* The LHS parameter contains the expression 'outervar->memberspec';
3959 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3960 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3963 = objc_substitute_decl
3964 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
3966 = (flag_objc_direct_dispatch
3967 ? objc_assign_ivar_fast_decl
3968 : objc_assign_ivar_decl);
3970 offs = convert (integer_type_node, build_unary_op (input_location,
3971 ADDR_EXPR, offs, 0));
3973 func_params = tree_cons (NULL_TREE,
3974 convert (objc_object_type, rhs),
3975 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
3976 tree_cons (NULL_TREE, offs,
3979 assemble_external (func);
3980 return build_function_call (input_location, func, func_params);
3984 objc_build_global_assignment (tree lhs, tree rhs)
3986 tree func_params = tree_cons (NULL_TREE,
3987 convert (objc_object_type, rhs),
3988 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3989 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3992 assemble_external (objc_assign_global_decl);
3993 return build_function_call (input_location,
3994 objc_assign_global_decl, func_params);
3998 objc_build_strong_cast_assignment (tree lhs, tree rhs)
4000 tree func_params = tree_cons (NULL_TREE,
4001 convert (objc_object_type, rhs),
4002 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
4003 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
4006 assemble_external (objc_assign_strong_cast_decl);
4007 return build_function_call (input_location,
4008 objc_assign_strong_cast_decl, func_params);
4012 objc_is_gcable_p (tree expr)
4014 return (TREE_CODE (expr) == COMPONENT_REF
4015 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
4016 : TREE_CODE (expr) == ARRAY_REF
4017 ? (objc_is_gcable_p (TREE_TYPE (expr))
4018 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
4019 : TREE_CODE (expr) == ARRAY_TYPE
4020 ? objc_is_gcable_p (TREE_TYPE (expr))
4022 ? objc_is_gcable_type (expr, 1)
4023 : (objc_is_gcable_p (TREE_TYPE (expr))
4025 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
4029 objc_is_ivar_reference_p (tree expr)
4031 return (TREE_CODE (expr) == ARRAY_REF
4032 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
4033 : TREE_CODE (expr) == COMPONENT_REF
4034 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
4039 objc_is_global_reference_p (tree expr)
4041 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
4042 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
4044 ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr))
4049 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
4051 tree result = NULL_TREE, outer;
4052 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
4054 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
4055 will have been transformed to the form '*(type *)&expr'. */
4056 if (TREE_CODE (lhs) == INDIRECT_REF)
4058 outer = TREE_OPERAND (lhs, 0);
4060 while (!strong_cast_p
4061 && (CONVERT_EXPR_P (outer)
4062 || TREE_CODE (outer) == NON_LVALUE_EXPR))
4064 tree lhstype = TREE_TYPE (outer);
4066 /* Descend down the cast chain, and record the first objc_gc
4068 if (POINTER_TYPE_P (lhstype))
4071 = lookup_attribute ("objc_gc",
4072 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
4078 outer = TREE_OPERAND (outer, 0);
4082 /* If we have a __strong cast, it trumps all else. */
4085 if (modifycode != NOP_EXPR)
4086 goto invalid_pointer_arithmetic;
4088 if (warn_assign_intercept)
4089 warning (0, "strong-cast assignment has been intercepted");
4091 result = objc_build_strong_cast_assignment (lhs, rhs);
4096 /* the lhs must be of a suitable type, regardless of its underlying
4098 if (!objc_is_gcable_p (lhs))
4104 && (TREE_CODE (outer) == COMPONENT_REF
4105 || TREE_CODE (outer) == ARRAY_REF))
4106 outer = TREE_OPERAND (outer, 0);
4108 if (TREE_CODE (outer) == INDIRECT_REF)
4110 outer = TREE_OPERAND (outer, 0);
4114 outer_gc_p = objc_is_gcable_p (outer);
4116 /* Handle ivar assignments. */
4117 if (objc_is_ivar_reference_p (lhs))
4119 /* if the struct to the left of the ivar is not an Objective-C object (__strong
4120 doesn't cut it here), the best we can do here is suggest a cast. */
4121 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
4123 /* We may still be able to use the global write barrier... */
4124 if (!indirect_p && objc_is_global_reference_p (outer))
4125 goto global_reference;
4128 if (modifycode == NOP_EXPR)
4130 if (warn_assign_intercept)
4131 warning (0, "strong-cast may possibly be needed");
4137 if (modifycode != NOP_EXPR)
4138 goto invalid_pointer_arithmetic;
4140 if (warn_assign_intercept)
4141 warning (0, "instance variable assignment has been intercepted");
4143 result = objc_build_ivar_assignment (outer, lhs, rhs);
4148 /* Likewise, intercept assignment to global/static variables if their type is
4150 if (objc_is_global_reference_p (outer))
4156 if (modifycode != NOP_EXPR)
4158 invalid_pointer_arithmetic:
4160 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
4165 if (warn_assign_intercept)
4166 warning (0, "global/static variable assignment has been intercepted");
4168 result = objc_build_global_assignment (lhs, rhs);
4171 /* In all other cases, fall back to the normal mechanism. */
4176 struct GTY(()) interface_tuple {
4181 static GTY ((param_is (struct interface_tuple))) htab_t interface_htab;
4184 hash_interface (const void *p)
4186 const struct interface_tuple *d = (const struct interface_tuple *) p;
4187 return IDENTIFIER_HASH_VALUE (d->id);
4191 eq_interface (const void *p1, const void *p2)
4193 const struct interface_tuple *d = (const struct interface_tuple *) p1;
4198 lookup_interface (tree ident)
4201 if (ident && TREE_CODE (ident) == TYPE_DECL)
4202 ident = DECL_NAME (ident);
4205 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
4209 struct interface_tuple **slot;
4214 slot = (struct interface_tuple **)
4215 htab_find_slot_with_hash (interface_htab, ident,
4216 IDENTIFIER_HASH_VALUE (ident),
4219 i = (*slot)->class_name;
4225 /* Implement @defs (<classname>) within struct bodies. */
4228 objc_get_class_ivars (tree class_name)
4230 tree interface = lookup_interface (class_name);
4233 return get_class_ivars (interface, true);
4235 error ("cannot find interface declaration for %qE",
4238 return error_mark_node;
4241 /* Called when checking the variables in a struct. If we are not
4242 doing the ivars list inside an @interface context, then returns
4243 fieldlist unchanged. Else, returns the list of class ivars.
4246 objc_get_interface_ivars (tree fieldlist)
4248 if (!objc_collecting_ivars || !objc_interface_context
4249 || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE
4250 || CLASS_SUPER_NAME (objc_interface_context) == NULL_TREE)
4253 return get_class_ivars (objc_interface_context, true);
4256 /* Used by: build_private_template, continue_class,
4257 and for @defs constructs. */
4260 get_class_ivars (tree interface, bool inherited)
4262 tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface));
4264 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
4265 by the current class (i.e., they do not include super-class ivars).
4266 However, the CLASS_IVARS list will be side-effected by a call to
4267 finish_struct(), which will fill in field offsets. */
4268 if (!CLASS_IVARS (interface))
4269 CLASS_IVARS (interface) = ivar_chain;
4274 while (CLASS_SUPER_NAME (interface))
4276 /* Prepend super-class ivars. */
4277 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4278 ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)),
4285 /* Create a temporary variable of type 'type'. If 'name' is set, uses
4286 the specified name, else use no name. Returns the declaration of
4287 the type. The 'name' is mostly useful for debugging.
4290 objc_create_temporary_var (tree type, const char *name)
4296 decl = build_decl (input_location,
4297 VAR_DECL, get_identifier (name), type);
4301 decl = build_decl (input_location,
4302 VAR_DECL, NULL_TREE, type);
4304 TREE_USED (decl) = 1;
4305 DECL_ARTIFICIAL (decl) = 1;
4306 DECL_IGNORED_P (decl) = 1;
4307 DECL_CONTEXT (decl) = current_function_decl;
4312 /* Exception handling constructs. We begin by having the parser do most
4313 of the work and passing us blocks. What we do next depends on whether
4314 we're doing "native" exception handling or legacy Darwin setjmp exceptions.
4315 We abstract all of this in a handful of appropriately named routines. */
4317 /* Stack of open try blocks. */
4319 struct objc_try_context
4321 struct objc_try_context *outer;
4323 /* Statements (or statement lists) as processed by the parser. */
4327 /* Some file position locations. */
4328 location_t try_locus;
4329 location_t end_try_locus;
4330 location_t end_catch_locus;
4331 location_t finally_locus;
4332 location_t end_finally_locus;
4334 /* A STATEMENT_LIST of CATCH_EXPRs, appropriate for sticking into op1
4335 of a TRY_CATCH_EXPR. Even when doing Darwin setjmp. */
4338 /* The CATCH_EXPR of an open @catch clause. */
4341 /* The VAR_DECL holding the Darwin equivalent of __builtin_eh_pointer. */
4347 static struct objc_try_context *cur_try_context;
4349 static GTY(()) tree objc_eh_personality_decl;
4351 /* This hook, called via lang_eh_runtime_type, generates a runtime object
4352 that represents TYPE. For Objective-C, this is just the class name. */
4353 /* ??? Isn't there a class object or some such? Is it easy to get? */
4357 objc_eh_runtime_type (tree type)
4359 return add_objc_string (OBJC_TYPE_NAME (TREE_TYPE (type)), class_names);
4363 objc_eh_personality (void)
4365 if (!flag_objc_sjlj_exceptions && !objc_eh_personality_decl)
4366 objc_eh_personality_decl = build_personality_function ("gnu_objc");
4367 return objc_eh_personality_decl;
4371 /* Build __builtin_eh_pointer, or the moral equivalent. In the case
4372 of Darwin, we'll arrange for it to be initialized (and associated
4373 with a binding) later. */
4376 objc_build_exc_ptr (void)
4378 if (flag_objc_sjlj_exceptions)
4380 tree var = cur_try_context->caught_decl;
4383 var = objc_create_temporary_var (objc_object_type, NULL);
4384 cur_try_context->caught_decl = var;
4391 t = built_in_decls[BUILT_IN_EH_POINTER];
4392 t = build_call_expr (t, 1, integer_zero_node);
4393 return fold_convert (objc_object_type, t);
4397 /* Build "objc_exception_try_exit(&_stack)". */
4400 next_sjlj_build_try_exit (void)
4403 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
4404 t = tree_cons (NULL, t, NULL);
4405 t = build_function_call (input_location,
4406 objc_exception_try_exit_decl, t);
4411 objc_exception_try_enter (&_stack);
4412 if (_setjmp(&_stack.buf))
4416 Return the COND_EXPR. Note that the THEN and ELSE fields are left
4417 empty, ready for the caller to fill them in. */
4420 next_sjlj_build_enter_and_setjmp (void)
4422 tree t, enter, sj, cond;
4424 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
4425 t = tree_cons (NULL, t, NULL);
4426 enter = build_function_call (input_location,
4427 objc_exception_try_enter_decl, t);
4429 t = objc_build_component_ref (cur_try_context->stack_decl,
4430 get_identifier ("buf"));
4431 t = build_fold_addr_expr_loc (input_location, t);
4433 /* Convert _setjmp argument to type that is expected. */
4434 if (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl)))
4435 t = convert (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl))), t);
4437 t = convert (ptr_type_node, t);
4439 t = convert (ptr_type_node, t);
4441 t = tree_cons (NULL, t, NULL);
4442 sj = build_function_call (input_location,
4443 objc_setjmp_decl, t);
4445 cond = build2 (COMPOUND_EXPR, TREE_TYPE (sj), enter, sj);
4446 cond = c_common_truthvalue_conversion (input_location, cond);
4448 return build3 (COND_EXPR, void_type_node, cond, NULL, NULL);
4453 DECL = objc_exception_extract(&_stack); */
4456 next_sjlj_build_exc_extract (tree decl)
4460 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
4461 t = tree_cons (NULL, t, NULL);
4462 t = build_function_call (input_location,
4463 objc_exception_extract_decl, t);
4464 t = convert (TREE_TYPE (decl), t);
4465 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
4471 if (objc_exception_match(obj_get_class(TYPE), _caught)
4478 objc_exception_try_exit(&_stack);
4480 from the sequence of CATCH_EXPRs in the current try context. */
4483 next_sjlj_build_catch_list (void)
4485 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
4487 tree *last = &catch_seq;
4488 bool saw_id = false;
4490 for (; !tsi_end_p (i); tsi_next (&i))
4492 tree stmt = tsi_stmt (i);
4493 tree type = CATCH_TYPES (stmt);
4494 tree body = CATCH_BODY (stmt);
4506 if (type == error_mark_node)
4507 cond = error_mark_node;
4510 args = tree_cons (NULL, cur_try_context->caught_decl, NULL);
4511 t = objc_get_class_reference (OBJC_TYPE_NAME (TREE_TYPE (type)));
4512 args = tree_cons (NULL, t, args);
4513 t = build_function_call (input_location,
4514 objc_exception_match_decl, args);
4515 cond = c_common_truthvalue_conversion (input_location, t);
4517 t = build3 (COND_EXPR, void_type_node, cond, body, NULL);
4518 SET_EXPR_LOCATION (t, EXPR_LOCATION (stmt));
4521 last = &COND_EXPR_ELSE (t);
4527 t = build2 (MODIFY_EXPR, void_type_node, cur_try_context->rethrow_decl,
4528 cur_try_context->caught_decl);
4529 SET_EXPR_LOCATION (t, cur_try_context->end_catch_locus);
4530 append_to_statement_list (t, last);
4532 t = next_sjlj_build_try_exit ();
4533 SET_EXPR_LOCATION (t, cur_try_context->end_catch_locus);
4534 append_to_statement_list (t, last);
4540 /* Build a complete @try-@catch-@finally block for legacy Darwin setjmp
4541 exception handling. We aim to build:
4544 struct _objc_exception_data _stack;
4548 objc_exception_try_enter (&_stack);
4549 if (_setjmp(&_stack.buf))
4551 id _caught = objc_exception_extract(&_stack);
4552 objc_exception_try_enter (&_stack);
4553 if (_setjmp(&_stack.buf))
4554 _rethrow = objc_exception_extract(&_stack);
4564 objc_exception_try_exit(&_stack);
4567 objc_exception_throw(_rethrow);
4571 If CATCH-LIST is empty, we can omit all of the block containing
4572 "_caught" except for the setting of _rethrow. Note the use of
4573 a real TRY_FINALLY_EXPR here, which is not involved in EH per-se,
4574 but handles goto and other exits from the block. */
4577 next_sjlj_build_try_catch_finally (void)
4579 tree rethrow_decl, stack_decl, t;
4580 tree catch_seq, try_fin, bind;
4582 /* Create the declarations involved. */
4583 t = xref_tag (RECORD_TYPE, get_identifier (UTAG_EXCDATA));
4584 stack_decl = objc_create_temporary_var (t, NULL);
4585 cur_try_context->stack_decl = stack_decl;
4587 rethrow_decl = objc_create_temporary_var (objc_object_type, NULL);
4588 cur_try_context->rethrow_decl = rethrow_decl;
4589 TREE_CHAIN (rethrow_decl) = stack_decl;
4591 /* Build the outermost variable binding level. */
4592 bind = build3 (BIND_EXPR, void_type_node, rethrow_decl, NULL, NULL);
4593 SET_EXPR_LOCATION (bind, cur_try_context->try_locus);
4594 TREE_SIDE_EFFECTS (bind) = 1;
4596 /* Initialize rethrow_decl. */
4597 t = build2 (MODIFY_EXPR, void_type_node, rethrow_decl,
4598 convert (objc_object_type, null_pointer_node));
4599 SET_EXPR_LOCATION (t, cur_try_context->try_locus);
4600 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
4602 /* Build the outermost TRY_FINALLY_EXPR. */
4603 try_fin = build2 (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
4604 SET_EXPR_LOCATION (try_fin, cur_try_context->try_locus);
4605 TREE_SIDE_EFFECTS (try_fin) = 1;
4606 append_to_statement_list (try_fin, &BIND_EXPR_BODY (bind));
4608 /* Create the complete catch sequence. */
4609 if (cur_try_context->catch_list)
4611 tree caught_decl = objc_build_exc_ptr ();
4612 catch_seq = build_stmt (input_location, BIND_EXPR, caught_decl, NULL, NULL);
4613 TREE_SIDE_EFFECTS (catch_seq) = 1;
4615 t = next_sjlj_build_exc_extract (caught_decl);
4616 append_to_statement_list (t, &BIND_EXPR_BODY (catch_seq));
4618 t = next_sjlj_build_enter_and_setjmp ();
4619 COND_EXPR_THEN (t) = next_sjlj_build_exc_extract (rethrow_decl);
4620 COND_EXPR_ELSE (t) = next_sjlj_build_catch_list ();
4621 append_to_statement_list (t, &BIND_EXPR_BODY (catch_seq));
4624 catch_seq = next_sjlj_build_exc_extract (rethrow_decl);
4625 SET_EXPR_LOCATION (catch_seq, cur_try_context->end_try_locus);
4627 /* Build the main register-and-try if statement. */
4628 t = next_sjlj_build_enter_and_setjmp ();
4629 SET_EXPR_LOCATION (t, cur_try_context->try_locus);
4630 COND_EXPR_THEN (t) = catch_seq;
4631 COND_EXPR_ELSE (t) = cur_try_context->try_body;
4632 TREE_OPERAND (try_fin, 0) = t;
4634 /* Build the complete FINALLY statement list. */
4635 t = next_sjlj_build_try_exit ();
4636 t = build_stmt (input_location, COND_EXPR,
4637 c_common_truthvalue_conversion
4638 (input_location, rethrow_decl),
4640 SET_EXPR_LOCATION (t, cur_try_context->finally_locus);
4641 append_to_statement_list (t, &TREE_OPERAND (try_fin, 1));
4643 append_to_statement_list (cur_try_context->finally_body,
4644 &TREE_OPERAND (try_fin, 1));
4646 t = tree_cons (NULL, rethrow_decl, NULL);
4647 t = build_function_call (input_location,
4648 objc_exception_throw_decl, t);
4649 t = build_stmt (input_location, COND_EXPR,
4650 c_common_truthvalue_conversion (input_location,
4653 SET_EXPR_LOCATION (t, cur_try_context->end_finally_locus);
4654 append_to_statement_list (t, &TREE_OPERAND (try_fin, 1));
4659 /* Called just after parsing the @try and its associated BODY. We now
4660 must prepare for the tricky bits -- handling the catches and finally. */
4663 objc_begin_try_stmt (location_t try_locus, tree body)
4665 struct objc_try_context *c = XCNEW (struct objc_try_context);
4666 c->outer = cur_try_context;
4668 c->try_locus = try_locus;
4669 c->end_try_locus = input_location;
4670 cur_try_context = c;
4672 /* -fobjc-exceptions is required to enable Objective-C exceptions.
4673 For example, on Darwin, ObjC exceptions require a sufficiently
4674 recent version of the runtime, so the user must ask for them
4675 explicitly. On other platforms, at the moment -fobjc-exceptions
4676 triggers -fexceptions which again is required for exceptions to
4679 if (!flag_objc_exceptions)
4681 error_at (try_locus, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4684 if (flag_objc_sjlj_exceptions)
4685 objc_mark_locals_volatile (NULL);
4688 /* Called just after parsing "@catch (parm)". Open a binding level,
4689 enter DECL into the binding level, and initialize it. Leave the
4690 binding level open while the body of the compound statement is parsed. */
4693 objc_begin_catch_clause (tree decl)
4695 tree compound, type, t;
4697 /* Begin a new scope that the entire catch clause will live in. */
4698 compound = c_begin_compound_stmt (true);
4700 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
4701 decl = build_decl (input_location,
4702 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
4703 lang_hooks.decls.pushdecl (decl);
4705 /* Since a decl is required here by syntax, don't warn if its unused. */
4706 /* ??? As opposed to __attribute__((unused))? Anyway, this appears to
4707 be what the previous objc implementation did. */
4708 TREE_USED (decl) = 1;
4709 DECL_READ_P (decl) = 1;
4711 /* Verify that the type of the catch is valid. It must be a pointer
4712 to an Objective-C class, or "id" (which is catch-all). */
4713 type = TREE_TYPE (decl);
4715 if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type)))
4717 else if (!POINTER_TYPE_P (type) || !TYPED_OBJECT (TREE_TYPE (type)))
4719 error ("@catch parameter is not a known Objective-C class type");
4720 type = error_mark_node;
4722 else if (cur_try_context->catch_list)
4724 /* Examine previous @catch clauses and see if we've already
4725 caught the type in question. */
4726 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
4727 for (; !tsi_end_p (i); tsi_next (&i))
4729 tree stmt = tsi_stmt (i);
4730 t = CATCH_TYPES (stmt);
4731 if (t == error_mark_node)
4733 if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type)))
4735 warning (0, "exception of type %<%T%> will be caught",
4737 warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>",
4738 TREE_TYPE (t ? t : objc_object_type));
4744 /* Record the data for the catch in the try context so that we can
4745 finalize it later. */
4746 t = build_stmt (input_location, CATCH_EXPR, type, compound);
4747 cur_try_context->current_catch = t;
4749 /* Initialize the decl from the EXC_PTR_EXPR we get from the runtime. */
4750 t = objc_build_exc_ptr ();
4751 t = convert (TREE_TYPE (decl), t);
4752 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
4756 /* Called just after parsing the closing brace of a @catch clause. Close
4757 the open binding level, and record a CATCH_EXPR for it. */
4760 objc_finish_catch_clause (void)
4762 tree c = cur_try_context->current_catch;
4763 cur_try_context->current_catch = NULL;
4764 cur_try_context->end_catch_locus = input_location;
4766 CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1);
4767 append_to_statement_list (c, &cur_try_context->catch_list);
4770 /* Called after parsing a @finally clause and its associated BODY.
4771 Record the body for later placement. */
4774 objc_build_finally_clause (location_t finally_locus, tree body)
4776 cur_try_context->finally_body = body;
4777 cur_try_context->finally_locus = finally_locus;
4778 cur_try_context->end_finally_locus = input_location;
4781 /* Called to finalize a @try construct. */
4784 objc_finish_try_stmt (void)
4786 struct objc_try_context *c = cur_try_context;
4789 if (c->catch_list == NULL && c->finally_body == NULL)
4790 error ("%<@try%> without %<@catch%> or %<@finally%>");
4792 /* If we're doing Darwin setjmp exceptions, build the big nasty. */
4793 if (flag_objc_sjlj_exceptions)
4795 bool save = in_late_binary_op;
4796 in_late_binary_op = true;
4797 if (!cur_try_context->finally_body)
4799 cur_try_context->finally_locus = input_location;
4800 cur_try_context->end_finally_locus = input_location;
4802 stmt = next_sjlj_build_try_catch_finally ();
4803 in_late_binary_op = save;
4807 /* Otherwise, nest the CATCH inside a FINALLY. */
4811 stmt = build_stmt (input_location, TRY_CATCH_EXPR, stmt, c->catch_list);
4812 SET_EXPR_LOCATION (stmt, cur_try_context->try_locus);
4814 if (c->finally_body)
4816 stmt = build_stmt (input_location, TRY_FINALLY_EXPR, stmt, c->finally_body);
4817 SET_EXPR_LOCATION (stmt, cur_try_context->try_locus);
4822 cur_try_context = c->outer;
4828 objc_build_throw_stmt (location_t loc, tree throw_expr)
4832 if (!flag_objc_exceptions)
4834 error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4837 if (throw_expr == NULL)
4839 /* If we're not inside a @catch block, there is no "current
4840 exception" to be rethrown. */
4841 if (cur_try_context == NULL
4842 || cur_try_context->current_catch == NULL)
4844 error_at (loc, "%<@throw%> (rethrow) used outside of a @catch block");
4848 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
4849 value that we get from the runtime. */
4850 throw_expr = objc_build_exc_ptr ();
4853 /* A throw is just a call to the runtime throw function with the
4854 object as a parameter. */
4855 args = tree_cons (NULL, throw_expr, NULL);
4856 return add_stmt (build_function_call (loc,
4857 objc_exception_throw_decl, args));
4861 objc_build_synchronized (location_t start_locus, tree mutex, tree body)
4865 /* First lock the mutex. */
4866 mutex = save_expr (mutex);
4867 args = tree_cons (NULL, mutex, NULL);
4868 call = build_function_call (input_location,
4869 objc_sync_enter_decl, args);
4870 SET_EXPR_LOCATION (call, start_locus);
4873 /* Build the mutex unlock. */
4874 args = tree_cons (NULL, mutex, NULL);
4875 call = build_function_call (input_location,
4876 objc_sync_exit_decl, args);
4877 SET_EXPR_LOCATION (call, input_location);
4879 /* Put the that and the body in a TRY_FINALLY. */
4880 objc_begin_try_stmt (start_locus, body);
4881 objc_build_finally_clause (input_location, call);
4882 return objc_finish_try_stmt ();
4886 /* Predefine the following data type:
4888 struct _objc_exception_data
4890 int buf[OBJC_JBLEN];
4894 /* The following yuckiness should prevent users from having to #include
4895 <setjmp.h> in their code... */
4897 /* Define to a harmless positive value so the below code doesn't die. */
4899 #define OBJC_JBLEN 18
4903 build_next_objc_exception_stuff (void)
4905 tree decls, temp_type, *chain = NULL;
4907 objc_exception_data_template
4908 = objc_start_struct (get_identifier (UTAG_EXCDATA));
4910 /* int buf[OBJC_JBLEN]; */
4912 temp_type = build_sized_array_type (integer_type_node, OBJC_JBLEN);
4913 decls = add_field_decl (temp_type, "buf", &chain);
4915 /* void *pointers[4]; */
4917 temp_type = build_sized_array_type (ptr_type_node, 4);
4918 add_field_decl (temp_type, "pointers", &chain);
4920 objc_finish_struct (objc_exception_data_template, decls);
4922 /* int _setjmp(...); */
4923 /* If the user includes <setjmp.h>, this shall be superseded by
4924 'int _setjmp(jmp_buf);' */
4925 temp_type = build_varargs_function_type_list (integer_type_node, NULL_TREE);
4927 = add_builtin_function (TAG_SETJMP, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
4929 /* id objc_exception_extract(struct _objc_exception_data *); */
4931 = build_function_type_list (objc_object_type,
4932 build_pointer_type (objc_exception_data_template),
4934 objc_exception_extract_decl
4935 = add_builtin_function (TAG_EXCEPTIONEXTRACT, temp_type, 0, NOT_BUILT_IN, NULL,
4937 /* void objc_exception_try_enter(struct _objc_exception_data *); */
4938 /* void objc_exception_try_exit(struct _objc_exception_data *); */
4940 = build_function_type_list (void_type_node,
4941 build_pointer_type (objc_exception_data_template),
4943 objc_exception_try_enter_decl
4944 = add_builtin_function (TAG_EXCEPTIONTRYENTER, temp_type, 0, NOT_BUILT_IN, NULL,
4946 objc_exception_try_exit_decl
4947 = add_builtin_function (TAG_EXCEPTIONTRYEXIT, temp_type, 0, NOT_BUILT_IN, NULL,
4950 /* int objc_exception_match(id, id); */
4952 = build_function_type_list (integer_type_node,
4953 objc_object_type, objc_object_type, NULL_TREE);
4954 objc_exception_match_decl
4955 = add_builtin_function (TAG_EXCEPTIONMATCH, temp_type, 0, NOT_BUILT_IN, NULL,
4958 /* id objc_assign_ivar (id, id, unsigned int); */
4959 /* id objc_assign_ivar_Fast (id, id, unsigned int)
4960 __attribute__ ((hard_coded_address (OFFS_ASSIGNIVAR_FAST))); */
4962 = build_function_type_list (objc_object_type,
4967 objc_assign_ivar_decl
4968 = add_builtin_function (TAG_ASSIGNIVAR, temp_type, 0, NOT_BUILT_IN,
4970 #ifdef OFFS_ASSIGNIVAR_FAST
4971 objc_assign_ivar_fast_decl
4972 = add_builtin_function (TAG_ASSIGNIVAR_FAST, temp_type, 0,
4973 NOT_BUILT_IN, NULL, NULL_TREE);
4974 DECL_ATTRIBUTES (objc_assign_ivar_fast_decl)
4975 = tree_cons (get_identifier ("hard_coded_address"),
4976 build_int_cst (NULL_TREE, OFFS_ASSIGNIVAR_FAST),
4979 /* Default to slower ivar method. */
4980 objc_assign_ivar_fast_decl = objc_assign_ivar_decl;
4983 /* id objc_assign_global (id, id *); */
4984 /* id objc_assign_strongCast (id, id *); */
4985 temp_type = build_function_type_list (objc_object_type,
4987 build_pointer_type (objc_object_type),
4989 objc_assign_global_decl
4990 = add_builtin_function (TAG_ASSIGNGLOBAL, temp_type, 0, NOT_BUILT_IN, NULL,
4992 objc_assign_strong_cast_decl
4993 = add_builtin_function (TAG_ASSIGNSTRONGCAST, temp_type, 0, NOT_BUILT_IN, NULL,
4998 build_objc_exception_stuff (void)
5000 tree noreturn_list, nothrow_list, temp_type;
5002 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
5003 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
5005 /* void objc_exception_throw(id) __attribute__((noreturn)); */
5006 /* void objc_sync_enter(id); */
5007 /* void objc_sync_exit(id); */
5008 temp_type = build_function_type_list (void_type_node,
5011 objc_exception_throw_decl
5012 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
5014 objc_sync_enter_decl
5015 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
5016 NULL, nothrow_list);
5018 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
5019 NULL, nothrow_list);
5022 /* Construct a C struct corresponding to ObjC class CLASS, with the same
5025 struct <classname> {
5026 struct _objc_class *isa;
5031 build_private_template (tree klass)
5033 if (!CLASS_STATIC_TEMPLATE (klass))
5035 tree record = objc_build_struct (klass,
5036 get_class_ivars (klass, false),
5037 CLASS_SUPER_NAME (klass));
5039 /* Set the TREE_USED bit for this struct, so that stab generator
5040 can emit stabs for this struct type. */
5041 if (flag_debug_only_used_symbols && TYPE_STUB_DECL (record))
5042 TREE_USED (TYPE_STUB_DECL (record)) = 1;
5046 /* Begin code generation for protocols... */
5048 /* struct _objc_protocol {
5049 struct _objc_class *isa;
5050 char *protocol_name;
5051 struct _objc_protocol **protocol_list;
5052 struct _objc__method_prototype_list *instance_methods;
5053 struct _objc__method_prototype_list *class_methods;
5057 build_protocol_template (void)
5059 tree ptype, decls, *chain = NULL;
5061 objc_protocol_template = objc_start_struct (get_identifier (UTAG_PROTOCOL));
5063 /* struct _objc_class *isa; */
5064 ptype = build_pointer_type (xref_tag (RECORD_TYPE,
5065 get_identifier (UTAG_CLASS)));
5066 decls = add_field_decl (ptype, "isa", &chain);
5068 /* char *protocol_name; */
5069 add_field_decl (string_type_node, "protocol_name", &chain);
5071 /* struct _objc_protocol **protocol_list; */
5072 ptype = build_pointer_type (build_pointer_type (objc_protocol_template));
5073 add_field_decl (ptype, "protocol_list", &chain);
5075 /* struct _objc__method_prototype_list *instance_methods; */
5076 add_field_decl (objc_method_proto_list_ptr, "instance_methods", &chain);
5078 /* struct _objc__method_prototype_list *class_methods; */
5079 add_field_decl (objc_method_proto_list_ptr, "class_methods", &chain);
5081 objc_finish_struct (objc_protocol_template, decls);
5085 build_descriptor_table_initializer (tree type, tree entries)
5087 VEC(constructor_elt,gc) *inits = NULL;
5091 VEC(constructor_elt,gc) *elts = NULL;
5093 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
5094 build_selector (METHOD_SEL_NAME (entries)));
5095 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
5096 add_objc_string (METHOD_ENCODING (entries),
5099 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
5100 objc_build_constructor (type, elts));
5102 entries = DECL_CHAIN (entries);
5106 return objc_build_constructor (build_array_type (type, 0), inits);
5109 /* struct objc_method_prototype_list {
5111 struct objc_method_prototype {
5118 build_method_prototype_list_template (tree list_type, int size)
5120 tree objc_ivar_list_record;
5121 tree array_type, decls, *chain = NULL;
5123 /* Generate an unnamed struct definition. */
5125 objc_ivar_list_record = objc_start_struct (NULL_TREE);
5127 /* int method_count; */
5128 decls = add_field_decl (integer_type_node, "method_count", &chain);
5130 /* struct objc_method method_list[]; */
5131 array_type = build_sized_array_type (list_type, size);
5132 add_field_decl (array_type, "method_list", &chain);
5134 objc_finish_struct (objc_ivar_list_record, decls);
5136 return objc_ivar_list_record;
5140 build_method_prototype_template (void)
5143 tree decls, *chain = NULL;
5145 proto_record = objc_start_struct (get_identifier (UTAG_METHOD_PROTOTYPE));
5148 decls = add_field_decl (objc_selector_type, "_cmd", &chain);
5150 /* char *method_types; */
5151 add_field_decl (string_type_node, "method_types", &chain);
5153 objc_finish_struct (proto_record, decls);
5155 return proto_record;
5159 objc_method_parm_type (tree type)
5161 type = TREE_VALUE (TREE_TYPE (type));
5162 if (TREE_CODE (type) == TYPE_DECL)
5163 type = TREE_TYPE (type);
5168 objc_encoded_type_size (tree type)
5170 int sz = int_size_in_bytes (type);
5172 /* Make all integer and enum types at least as large
5174 if (sz > 0 && INTEGRAL_TYPE_P (type))
5175 sz = MAX (sz, int_size_in_bytes (integer_type_node));
5176 /* Treat arrays as pointers, since that's how they're
5178 else if (TREE_CODE (type) == ARRAY_TYPE)
5179 sz = int_size_in_bytes (ptr_type_node);
5183 /* Encode a method prototype.
5185 The format is described in gcc/doc/objc.texi, section 'Method
5189 encode_method_prototype (tree method_decl)
5196 /* ONEWAY and BYCOPY, for remote object are the only method qualifiers. */
5197 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (method_decl)));
5199 /* Encode return type. */
5200 encode_type (objc_method_parm_type (method_decl),
5201 obstack_object_size (&util_obstack),
5202 OBJC_ENCODE_INLINE_DEFS);
5205 /* The first two arguments (self and _cmd) are pointers; account for
5207 i = int_size_in_bytes (ptr_type_node);
5208 parm_offset = 2 * i;
5209 for (parms = METHOD_SEL_ARGS (method_decl); parms;
5210 parms = DECL_CHAIN (parms))
5212 tree type = objc_method_parm_type (parms);
5213 int sz = objc_encoded_type_size (type);
5215 /* If a type size is not known, bail out. */
5218 error ("type %q+D does not have a known size",
5220 /* Pretend that the encoding succeeded; the compilation will
5221 fail nevertheless. */
5222 goto finish_encoding;
5227 sprintf (buf, "%d@0:%d", parm_offset, i);
5228 obstack_grow (&util_obstack, buf, strlen (buf));
5230 /* Argument types. */
5231 parm_offset = 2 * i;
5232 for (parms = METHOD_SEL_ARGS (method_decl); parms;
5233 parms = DECL_CHAIN (parms))
5235 tree type = objc_method_parm_type (parms);
5237 /* Process argument qualifiers for user supplied arguments. */
5238 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (parms)));
5241 encode_type (type, obstack_object_size (&util_obstack),
5242 OBJC_ENCODE_INLINE_DEFS);
5244 /* Compute offset. */
5245 sprintf (buf, "%d", parm_offset);
5246 parm_offset += objc_encoded_type_size (type);
5248 obstack_grow (&util_obstack, buf, strlen (buf));
5252 obstack_1grow (&util_obstack, '\0');
5253 result = get_identifier (XOBFINISH (&util_obstack, char *));
5254 obstack_free (&util_obstack, util_firstobj);
5259 generate_descriptor_table (tree type, const char *name, int size, tree list,
5263 VEC(constructor_elt,gc) *v = NULL;
5265 decl = start_var_decl (type, synth_id_with_class_suffix (name, proto));
5267 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, size));
5268 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, list);
5270 finish_var_decl (decl, objc_build_constructor (type, v));
5276 generate_method_descriptors (tree protocol)
5278 tree initlist, chain, method_list_template;
5281 if (!objc_method_prototype_template)
5282 objc_method_prototype_template = build_method_prototype_template ();
5284 chain = PROTOCOL_CLS_METHODS (protocol);
5287 size = list_length (chain);
5289 method_list_template
5290 = build_method_prototype_list_template (objc_method_prototype_template,
5294 = build_descriptor_table_initializer (objc_method_prototype_template,
5297 UOBJC_CLASS_METHODS_decl
5298 = generate_descriptor_table (method_list_template,
5299 "_OBJC_PROTOCOL_CLASS_METHODS",
5300 size, initlist, protocol);
5303 UOBJC_CLASS_METHODS_decl = 0;
5305 chain = PROTOCOL_NST_METHODS (protocol);
5308 size = list_length (chain);
5310 method_list_template
5311 = build_method_prototype_list_template (objc_method_prototype_template,
5314 = build_descriptor_table_initializer (objc_method_prototype_template,
5317 UOBJC_INSTANCE_METHODS_decl
5318 = generate_descriptor_table (method_list_template,
5319 "_OBJC_PROTOCOL_INSTANCE_METHODS",
5320 size, initlist, protocol);
5323 UOBJC_INSTANCE_METHODS_decl = 0;
5327 generate_protocol_references (tree plist)
5331 /* Forward declare protocols referenced. */
5332 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
5334 tree proto = TREE_VALUE (lproto);
5336 if (TREE_CODE (proto) == PROTOCOL_INTERFACE_TYPE
5337 && PROTOCOL_NAME (proto))
5339 if (! PROTOCOL_FORWARD_DECL (proto))
5340 build_protocol_reference (proto);
5342 if (PROTOCOL_LIST (proto))
5343 generate_protocol_references (PROTOCOL_LIST (proto));
5348 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
5352 objc_generate_cxx_ctor_or_dtor (bool dtor)
5354 tree fn, body, compound_stmt, ivar;
5356 /* - (id) .cxx_construct { ... return self; } */
5357 /* - (void) .cxx_construct { ... } */
5359 objc_start_method_definition
5360 (false /* is_class_method */,
5361 objc_build_method_signature (false /* is_class_method */,
5362 build_tree_list (NULL_TREE,
5365 : objc_object_type),
5366 get_identifier (dtor
5368 : TAG_CXX_CONSTRUCT),
5369 make_node (TREE_LIST),
5371 body = begin_function_body ();
5372 compound_stmt = begin_compound_stmt (0);
5374 ivar = CLASS_IVARS (implementation_template);
5375 /* Destroy ivars in reverse order. */
5377 ivar = nreverse (copy_list (ivar));
5379 for (; ivar; ivar = TREE_CHAIN (ivar))
5381 if (TREE_CODE (ivar) == FIELD_DECL)
5383 tree type = TREE_TYPE (ivar);
5385 /* Call the ivar's default constructor or destructor. Do not
5386 call the destructor unless a corresponding constructor call
5387 has also been made (or is not needed). */
5388 if (MAYBE_CLASS_TYPE_P (type)
5390 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
5391 && (!TYPE_NEEDS_CONSTRUCTING (type)
5392 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5393 : (TYPE_NEEDS_CONSTRUCTING (type)
5394 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))))
5396 (build_special_member_call
5397 (build_ivar_reference (DECL_NAME (ivar)),
5398 dtor ? complete_dtor_identifier : complete_ctor_identifier,
5399 NULL, type, LOOKUP_NORMAL, tf_warning_or_error));
5403 /* The constructor returns 'self'. */
5405 finish_return_stmt (self_decl);
5407 finish_compound_stmt (compound_stmt);
5408 finish_function_body (body);
5409 fn = current_function_decl;
5411 objc_finish_method_definition (fn);
5414 /* The following routine will examine the current @interface for any
5415 non-POD C++ ivars requiring non-trivial construction and/or
5416 destruction, and then synthesize special '- .cxx_construct' and/or
5417 '- .cxx_destruct' methods which will run the appropriate
5418 construction or destruction code. Note that ivars inherited from
5419 super-classes are _not_ considered. */
5421 objc_generate_cxx_cdtors (void)
5423 bool need_ctor = false, need_dtor = false;
5426 /* Error case, due to possibly an extra @end. */
5427 if (!objc_implementation_context)
5430 /* We do not want to do this for categories, since they do not have
5433 if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE)
5436 /* First, determine if we even need a constructor and/or destructor. */
5438 for (ivar = CLASS_IVARS (implementation_template); ivar;
5439 ivar = TREE_CHAIN (ivar))
5441 if (TREE_CODE (ivar) == FIELD_DECL)
5443 tree type = TREE_TYPE (ivar);
5445 if (MAYBE_CLASS_TYPE_P (type))
5447 if (TYPE_NEEDS_CONSTRUCTING (type)
5448 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
5449 /* NB: If a default constructor is not available, we will not
5450 be able to initialize this ivar; the add_instance_variable()
5451 routine will already have warned about this. */
5454 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
5455 && (!TYPE_NEEDS_CONSTRUCTING (type)
5456 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5457 /* NB: If a default constructor is not available, we will not
5458 call the destructor either, for symmetry. */
5464 /* Generate '- .cxx_construct' if needed. */
5467 objc_generate_cxx_ctor_or_dtor (false);
5469 /* Generate '- .cxx_destruct' if needed. */
5472 objc_generate_cxx_ctor_or_dtor (true);
5474 /* The 'imp_list' variable points at an imp_entry record for the current
5475 @implementation. Record the existence of '- .cxx_construct' and/or
5476 '- .cxx_destruct' methods therein; it will be included in the
5477 metadata for the class. */
5478 if (flag_next_runtime)
5479 imp_list->has_cxx_cdtors = (need_ctor || need_dtor);
5483 /* For each protocol which was referenced either from a @protocol()
5484 expression, or because a class/category implements it (then a
5485 pointer to the protocol is stored in the struct describing the
5486 class/category), we create a statically allocated instance of the
5487 Protocol class. The code is written in such a way as to generate
5488 as few Protocol objects as possible; we generate a unique Protocol
5489 instance for each protocol, and we don't generate a Protocol
5490 instance if the protocol is never referenced (either from a
5491 @protocol() or from a class/category implementation). These
5492 statically allocated objects can be referred to via the static
5493 (that is, private to this module) symbols _OBJC_PROTOCOL_n.
5495 The statically allocated Protocol objects that we generate here
5496 need to be fixed up at runtime in order to be used: the 'isa'
5497 pointer of the objects need to be set up to point to the 'Protocol'
5498 class, as known at runtime.
5500 The NeXT runtime fixes up all protocols at program startup time,
5501 before main() is entered. It uses a low-level trick to look up all
5502 those symbols, then loops on them and fixes them up.
5504 The GNU runtime as well fixes up all protocols before user code
5505 from the module is executed; it requires pointers to those symbols
5506 to be put in the objc_symtab (which is then passed as argument to
5507 the function __objc_exec_class() which the compiler sets up to be
5508 executed automatically when the module is loaded); setup of those
5509 Protocol objects happen in two ways in the GNU runtime: all
5510 Protocol objects referred to by a class or category implementation
5511 are fixed up when the class/category is loaded; all Protocol
5512 objects referred to by a @protocol() expression are added by the
5513 compiler to the list of statically allocated instances to fixup
5514 (the same list holding the statically allocated constant string
5515 objects). Because, as explained above, the compiler generates as
5516 few Protocol objects as possible, some Protocol object might end up
5517 being referenced multiple times when compiled with the GNU runtime,
5518 and end up being fixed up multiple times at runtime initialization.
5519 But that doesn't hurt, it's just a little inefficient. */
5522 generate_protocols (void)
5526 tree initlist, protocol_name_expr, refs_decl, refs_expr;
5528 /* If a protocol was directly referenced, pull in indirect references. */
5529 for (p = protocol_chain; p; p = TREE_CHAIN (p))
5530 if (PROTOCOL_FORWARD_DECL (p) && PROTOCOL_LIST (p))
5531 generate_protocol_references (PROTOCOL_LIST (p));
5533 for (p = protocol_chain; p; p = TREE_CHAIN (p))
5535 tree nst_methods = PROTOCOL_NST_METHODS (p);
5536 tree cls_methods = PROTOCOL_CLS_METHODS (p);
5538 /* If protocol wasn't referenced, don't generate any code. */
5539 decl = PROTOCOL_FORWARD_DECL (p);
5544 /* Make sure we link in the Protocol class. */
5545 add_class_reference (get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
5549 if (! METHOD_ENCODING (nst_methods))
5551 encoding = encode_method_prototype (nst_methods);
5552 METHOD_ENCODING (nst_methods) = encoding;
5554 nst_methods = DECL_CHAIN (nst_methods);
5559 if (! METHOD_ENCODING (cls_methods))
5561 encoding = encode_method_prototype (cls_methods);
5562 METHOD_ENCODING (cls_methods) = encoding;
5565 cls_methods = DECL_CHAIN (cls_methods);
5567 generate_method_descriptors (p);
5569 if (PROTOCOL_LIST (p))
5570 refs_decl = generate_protocol_list (p);
5574 /* static struct objc_protocol _OBJC_PROTOCOL_<mumble>; */
5575 protocol_name_expr = add_objc_string (PROTOCOL_NAME (p), class_names);
5578 refs_expr = convert (build_pointer_type (build_pointer_type
5579 (objc_protocol_template)),
5580 build_unary_op (input_location,
5581 ADDR_EXPR, refs_decl, 0));
5583 refs_expr = build_int_cst (NULL_TREE, 0);
5585 /* UOBJC_INSTANCE_METHODS_decl/UOBJC_CLASS_METHODS_decl are set
5586 by generate_method_descriptors, which is called above. */
5587 initlist = build_protocol_initializer (TREE_TYPE (decl),
5588 protocol_name_expr, refs_expr,
5589 UOBJC_INSTANCE_METHODS_decl,
5590 UOBJC_CLASS_METHODS_decl);
5591 finish_var_decl (decl, initlist);
5596 build_protocol_initializer (tree type, tree protocol_name,
5597 tree protocol_list, tree instance_methods,
5601 tree cast_type = build_pointer_type
5602 (xref_tag (RECORD_TYPE,
5603 get_identifier (UTAG_CLASS)));
5604 VEC(constructor_elt,gc) *inits = NULL;
5606 /* Filling the "isa" in with one allows the runtime system to
5607 detect that the version change...should remove before final release. */
5609 expr = build_int_cst (cast_type, PROTOCOL_VERSION);
5610 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
5611 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, protocol_name);
5612 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, protocol_list);
5614 if (!instance_methods)
5615 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, 0));
5618 expr = convert (objc_method_proto_list_ptr,
5619 build_unary_op (input_location,
5620 ADDR_EXPR, instance_methods, 0));
5621 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
5625 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, 0));
5628 expr = convert (objc_method_proto_list_ptr,
5629 build_unary_op (input_location,
5630 ADDR_EXPR, class_methods, 0));
5631 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
5634 return objc_build_constructor (type, inits);
5637 /* struct _objc_category {
5638 char *category_name;
5640 struct _objc_method_list *instance_methods;
5641 struct _objc_method_list *class_methods;
5642 struct _objc_protocol_list *protocols;
5646 build_category_template (void)
5648 tree ptype, decls, *chain = NULL;
5650 objc_category_template = objc_start_struct (get_identifier (UTAG_CATEGORY));
5652 /* char *category_name; */
5653 decls = add_field_decl (string_type_node, "category_name", &chain);
5655 /* char *class_name; */
5656 add_field_decl (string_type_node, "class_name", &chain);
5658 /* struct _objc_method_list *instance_methods; */
5659 add_field_decl (objc_method_list_ptr, "instance_methods", &chain);
5661 /* struct _objc_method_list *class_methods; */
5662 add_field_decl (objc_method_list_ptr, "class_methods", &chain);
5664 /* struct _objc_protocol **protocol_list; */
5665 ptype = build_pointer_type (build_pointer_type (objc_protocol_template));
5666 add_field_decl (ptype, "protocol_list", &chain);
5668 objc_finish_struct (objc_category_template, decls);
5671 /* struct _objc_selector {
5677 build_selector_template (void)
5679 tree decls, *chain = NULL;
5681 objc_selector_template = objc_start_struct (get_identifier (UTAG_SELECTOR));
5684 decls = add_field_decl (objc_selector_type, "sel_id", &chain);
5686 /* char *sel_type; */
5687 add_field_decl (string_type_node, "sel_type", &chain);
5689 objc_finish_struct (objc_selector_template, decls);
5692 /* struct _objc_class {
5693 struct _objc_class *isa;
5694 struct _objc_class *super_class;
5699 struct _objc_ivar_list *ivars;
5700 struct _objc_method_list *methods;
5701 #ifdef __NEXT_RUNTIME__
5702 struct objc_cache *cache;
5704 struct sarray *dtable;
5705 struct _objc_class *subclass_list;
5706 struct _objc_class *sibling_class;
5708 struct _objc_protocol_list *protocols;
5709 #ifdef __NEXT_RUNTIME__
5712 void *gc_object_type;
5715 /* NB: The 'sel_id' and 'gc_object_type' fields are not being used by
5716 the NeXT/Apple runtime; still, the compiler must generate them to
5717 maintain backward binary compatibility (and to allow for future
5721 build_class_template (void)
5723 tree ptype, decls, *chain = NULL;
5725 objc_class_template = objc_start_struct (get_identifier (UTAG_CLASS));
5727 /* struct _objc_class *isa; */
5728 decls = add_field_decl (build_pointer_type (objc_class_template),
5731 /* struct _objc_class *super_class; */
5732 add_field_decl (build_pointer_type (objc_class_template),
5733 "super_class", &chain);
5736 add_field_decl (string_type_node, "name", &chain);
5739 add_field_decl (long_integer_type_node, "version", &chain);
5742 add_field_decl (long_integer_type_node, "info", &chain);
5744 /* long instance_size; */
5745 add_field_decl (long_integer_type_node, "instance_size", &chain);
5747 /* struct _objc_ivar_list *ivars; */
5748 add_field_decl (objc_ivar_list_ptr,"ivars", &chain);
5750 /* struct _objc_method_list *methods; */
5751 add_field_decl (objc_method_list_ptr, "methods", &chain);
5753 if (flag_next_runtime)
5755 /* struct objc_cache *cache; */
5756 ptype = build_pointer_type (xref_tag (RECORD_TYPE,
5757 get_identifier ("objc_cache")));
5758 add_field_decl (ptype, "cache", &chain);
5762 /* struct sarray *dtable; */
5763 ptype = build_pointer_type(xref_tag (RECORD_TYPE,
5764 get_identifier ("sarray")));
5765 add_field_decl (ptype, "dtable", &chain);
5767 /* struct objc_class *subclass_list; */
5768 ptype = build_pointer_type (objc_class_template);
5769 add_field_decl (ptype, "subclass_list", &chain);
5771 /* struct objc_class *sibling_class; */
5772 ptype = build_pointer_type (objc_class_template);
5773 add_field_decl (ptype, "sibling_class", &chain);
5776 /* struct _objc_protocol **protocol_list; */
5777 ptype = build_pointer_type (build_pointer_type
5778 (xref_tag (RECORD_TYPE,
5779 get_identifier (UTAG_PROTOCOL))));
5780 add_field_decl (ptype, "protocol_list", &chain);
5782 if (flag_next_runtime)
5785 add_field_decl (build_pointer_type (void_type_node), "sel_id", &chain);
5788 /* void *gc_object_type; */
5789 add_field_decl (build_pointer_type (void_type_node),
5790 "gc_object_type", &chain);
5792 objc_finish_struct (objc_class_template, decls);
5795 /* Generate appropriate forward declarations for an implementation. */
5798 synth_forward_declarations (void)
5802 /* static struct objc_class _OBJC_CLASS_<my_name>; */
5803 UOBJC_CLASS_decl = build_metadata_decl ("_OBJC_CLASS",
5804 objc_class_template);
5806 /* static struct objc_class _OBJC_METACLASS_<my_name>; */
5807 UOBJC_METACLASS_decl = build_metadata_decl ("_OBJC_METACLASS",
5808 objc_class_template);
5810 /* Pre-build the following entities - for speed/convenience. */
5812 an_id = get_identifier ("super_class");
5813 ucls_super_ref = objc_build_component_ref (UOBJC_CLASS_decl, an_id);
5814 uucls_super_ref = objc_build_component_ref (UOBJC_METACLASS_decl, an_id);
5818 error_with_ivar (const char *message, tree decl)
5820 error_at (DECL_SOURCE_LOCATION (decl), "%s %qs",
5821 message, identifier_to_locale (gen_declaration (decl)));
5826 check_ivars (tree inter, tree imp)
5828 tree intdecls = CLASS_RAW_IVARS (inter);
5829 tree impdecls = CLASS_RAW_IVARS (imp);
5836 if (intdecls && TREE_CODE (intdecls) == TYPE_DECL)
5837 intdecls = TREE_CHAIN (intdecls);
5839 if (intdecls == 0 && impdecls == 0)
5841 if (intdecls == 0 || impdecls == 0)
5843 error ("inconsistent instance variable specification");
5847 t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
5849 if (!comptypes (t1, t2)
5850 || !tree_int_cst_equal (DECL_INITIAL (intdecls),
5851 DECL_INITIAL (impdecls)))
5853 if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
5855 error_with_ivar ("conflicting instance variable type",
5857 error_with_ivar ("previous declaration of",
5860 else /* both the type and the name don't match */
5862 error ("inconsistent instance variable specification");
5867 else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
5869 error_with_ivar ("conflicting instance variable name",
5871 error_with_ivar ("previous declaration of",
5875 intdecls = DECL_CHAIN (intdecls);
5876 impdecls = DECL_CHAIN (impdecls);
5880 /* Set 'objc_super_template' to the data type node for 'struct _objc_super'.
5881 This needs to be done just once per compilation. */
5883 /* struct _objc_super {
5884 struct _objc_object *self;
5885 struct _objc_class *super_class;
5889 build_super_template (void)
5891 tree decls, *chain = NULL;
5893 objc_super_template = objc_start_struct (get_identifier (UTAG_SUPER));
5895 /* struct _objc_object *self; */
5896 decls = add_field_decl (objc_object_type, "self", &chain);
5898 /* struct _objc_class *super_class; */
5899 add_field_decl (build_pointer_type (objc_class_template),
5900 "super_class", &chain);
5902 objc_finish_struct (objc_super_template, decls);
5905 /* struct _objc_ivar {
5912 build_ivar_template (void)
5914 tree objc_ivar_id, objc_ivar_record;
5915 tree decls, *chain = NULL;
5917 objc_ivar_id = get_identifier (UTAG_IVAR);
5918 objc_ivar_record = objc_start_struct (objc_ivar_id);
5920 /* char *ivar_name; */
5921 decls = add_field_decl (string_type_node, "ivar_name", &chain);
5923 /* char *ivar_type; */
5924 add_field_decl (string_type_node, "ivar_type", &chain);
5926 /* int ivar_offset; */
5927 add_field_decl (integer_type_node, "ivar_offset", &chain);
5929 objc_finish_struct (objc_ivar_record, decls);
5931 return objc_ivar_record;
5936 struct objc_ivar ivar_list[ivar_count];
5940 build_ivar_list_template (tree list_type, int size)
5942 tree objc_ivar_list_record;
5943 tree array_type, decls, *chain = NULL;
5945 objc_ivar_list_record = objc_start_struct (NULL_TREE);
5947 /* int ivar_count; */
5948 decls = add_field_decl (integer_type_node, "ivar_count", &chain);
5950 /* struct objc_ivar ivar_list[]; */
5951 array_type = build_sized_array_type (list_type, size);
5952 add_field_decl (array_type, "ivar_list", &chain);
5954 objc_finish_struct (objc_ivar_list_record, decls);
5956 return objc_ivar_list_record;
5960 struct _objc__method_prototype_list *method_next;
5962 struct objc_method method_list[method_count];
5966 build_method_list_template (tree list_type, int size)
5968 tree objc_ivar_list_record;
5969 tree array_type, decls, *chain = NULL;
5971 objc_ivar_list_record = objc_start_struct (NULL_TREE);
5973 /* struct _objc__method_prototype_list *method_next; */
5974 decls = add_field_decl (objc_method_proto_list_ptr, "method_next", &chain);
5976 /* int method_count; */
5977 add_field_decl (integer_type_node, "method_count", &chain);
5979 /* struct objc_method method_list[]; */
5980 array_type = build_sized_array_type (list_type, size);
5981 add_field_decl (array_type, "method_list", &chain);
5983 objc_finish_struct (objc_ivar_list_record, decls);
5985 return objc_ivar_list_record;
5989 build_ivar_list_initializer (tree type, tree field_decl)
5991 VEC(constructor_elt,gc) *inits = NULL;
5995 VEC(constructor_elt,gc) *ivar = NULL;
5999 if (DECL_NAME (field_decl))
6000 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE,
6001 add_objc_string (DECL_NAME (field_decl),
6004 /* Unnamed bit-field ivar (yuck). */
6005 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, build_int_cst (NULL_TREE, 0));
6008 encode_field_decl (field_decl,
6009 obstack_object_size (&util_obstack),
6010 OBJC_ENCODE_DONT_INLINE_DEFS);
6012 /* Null terminate string. */
6013 obstack_1grow (&util_obstack, 0);
6014 id = add_objc_string (get_identifier (XOBFINISH (&util_obstack, char *)),
6016 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, id);
6017 obstack_free (&util_obstack, util_firstobj);
6020 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, byte_position (field_decl));
6021 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
6022 objc_build_constructor (type, ivar));
6024 field_decl = DECL_CHAIN (field_decl);
6025 while (field_decl && TREE_CODE (field_decl) != FIELD_DECL);
6029 return objc_build_constructor (build_array_type (type, 0), inits);
6033 generate_ivars_list (tree type, const char *name, int size, tree list)
6036 VEC(constructor_elt,gc) *inits = NULL;
6038 decl = start_var_decl (type, synth_id_with_class_suffix
6039 (name, objc_implementation_context));
6041 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, size));
6042 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, list);
6044 finish_var_decl (decl,
6045 objc_build_constructor (TREE_TYPE (decl), inits));
6050 /* Count only the fields occurring in T. */
6053 ivar_list_length (tree t)
6057 for (; t; t = DECL_CHAIN (t))
6058 if (TREE_CODE (t) == FIELD_DECL)
6065 generate_ivar_lists (void)
6067 tree initlist, ivar_list_template, chain;
6070 generating_instance_variables = 1;
6072 if (!objc_ivar_template)
6073 objc_ivar_template = build_ivar_template ();
6075 /* Only generate class variables for the root of the inheritance
6076 hierarchy since these will be the same for every class. */
6078 if (CLASS_SUPER_NAME (implementation_template) == NULL_TREE
6079 && (chain = TYPE_FIELDS (objc_class_template)))
6081 size = ivar_list_length (chain);
6083 ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
6084 initlist = build_ivar_list_initializer (objc_ivar_template, chain);
6086 UOBJC_CLASS_VARIABLES_decl
6087 = generate_ivars_list (ivar_list_template, "_OBJC_CLASS_VARIABLES",
6091 UOBJC_CLASS_VARIABLES_decl = 0;
6093 chain = CLASS_IVARS (implementation_template);
6096 size = ivar_list_length (chain);
6097 ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
6098 initlist = build_ivar_list_initializer (objc_ivar_template, chain);
6100 UOBJC_INSTANCE_VARIABLES_decl
6101 = generate_ivars_list (ivar_list_template, "_OBJC_INSTANCE_VARIABLES",
6105 UOBJC_INSTANCE_VARIABLES_decl = 0;
6107 generating_instance_variables = 0;
6111 build_dispatch_table_initializer (tree type, tree entries)
6113 VEC(constructor_elt,gc) *inits = NULL;
6117 VEC(constructor_elt,gc) *elems = NULL;
6120 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE,
6121 build_selector (METHOD_SEL_NAME (entries)));
6123 /* Generate the method encoding if we don't have one already. */
6124 if (! METHOD_ENCODING (entries))
6125 METHOD_ENCODING (entries) =
6126 encode_method_prototype (entries);
6128 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE,
6129 add_objc_string (METHOD_ENCODING (entries),
6132 expr = convert (ptr_type_node,
6133 build_unary_op (input_location, ADDR_EXPR,
6134 METHOD_DEFINITION (entries), 1));
6135 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE, expr);
6137 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
6138 objc_build_constructor (type, elems));
6140 entries = DECL_CHAIN (entries);
6144 return objc_build_constructor (build_array_type (type, 0), inits);
6147 /* To accomplish method prototyping without generating all kinds of
6148 inane warnings, the definition of the dispatch table entries were
6151 struct objc_method { SEL _cmd; ...; id (*_imp)(); };
6153 struct objc_method { SEL _cmd; ...; void *_imp; }; */
6156 build_method_template (void)
6159 tree decls, *chain = NULL;
6161 _SLT_record = objc_start_struct (get_identifier (UTAG_METHOD));
6164 decls = add_field_decl (objc_selector_type, "_cmd", &chain);
6166 /* char *method_types; */
6167 add_field_decl (string_type_node, "method_types", &chain);
6170 add_field_decl (build_pointer_type (void_type_node), "_imp", &chain);
6172 objc_finish_struct (_SLT_record, decls);
6179 generate_dispatch_table (tree type, const char *name, int size, tree list)
6182 VEC(constructor_elt,gc) *v = NULL;
6184 decl = start_var_decl (type, synth_id_with_class_suffix
6185 (name, objc_implementation_context));
6187 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
6188 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (integer_type_node, size));
6189 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, list);
6191 finish_var_decl (decl,
6192 objc_build_constructor (TREE_TYPE (decl), v));
6198 mark_referenced_methods (void)
6200 struct imp_entry *impent;
6203 for (impent = imp_list; impent; impent = impent->next)
6205 chain = CLASS_CLS_METHODS (impent->imp_context);
6208 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain)));
6209 chain = DECL_CHAIN (chain);
6212 chain = CLASS_NST_METHODS (impent->imp_context);
6215 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain)));
6216 chain = DECL_CHAIN (chain);
6222 generate_dispatch_tables (void)
6224 tree initlist, chain, method_list_template;
6227 if (!objc_method_template)
6228 objc_method_template = build_method_template ();
6230 chain = CLASS_CLS_METHODS (objc_implementation_context);
6233 size = list_length (chain);
6235 method_list_template
6236 = build_method_list_template (objc_method_template, size);
6238 = build_dispatch_table_initializer (objc_method_template, chain);
6240 UOBJC_CLASS_METHODS_decl
6241 = generate_dispatch_table (method_list_template,
6242 ((TREE_CODE (objc_implementation_context)
6243 == CLASS_IMPLEMENTATION_TYPE)
6244 ? "_OBJC_CLASS_METHODS"
6245 : "_OBJC_CATEGORY_CLASS_METHODS"),
6249 UOBJC_CLASS_METHODS_decl = 0;
6251 chain = CLASS_NST_METHODS (objc_implementation_context);
6254 size = list_length (chain);
6256 method_list_template
6257 = build_method_list_template (objc_method_template, size);
6259 = build_dispatch_table_initializer (objc_method_template, chain);
6261 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
6262 UOBJC_INSTANCE_METHODS_decl
6263 = generate_dispatch_table (method_list_template,
6264 "_OBJC_INSTANCE_METHODS",
6267 /* We have a category. */
6268 UOBJC_INSTANCE_METHODS_decl
6269 = generate_dispatch_table (method_list_template,
6270 "_OBJC_CATEGORY_INSTANCE_METHODS",
6274 UOBJC_INSTANCE_METHODS_decl = 0;
6278 generate_protocol_list (tree i_or_p)
6280 tree array_type, ptype, refs_decl, lproto, e, plist;
6282 const char *ref_name;
6283 VEC(constructor_elt,gc) *v = NULL;
6285 switch (TREE_CODE (i_or_p))
6287 case CLASS_INTERFACE_TYPE:
6288 case CATEGORY_INTERFACE_TYPE:
6289 plist = CLASS_PROTOCOL_LIST (i_or_p);
6291 case PROTOCOL_INTERFACE_TYPE:
6292 plist = PROTOCOL_LIST (i_or_p);
6299 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
6300 if (TREE_CODE (TREE_VALUE (lproto)) == PROTOCOL_INTERFACE_TYPE
6301 && PROTOCOL_FORWARD_DECL (TREE_VALUE (lproto)))
6304 /* Build initializer. */
6305 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6306 e = build_int_cst (build_pointer_type (objc_protocol_template), size);
6307 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, e);
6309 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
6311 tree pval = TREE_VALUE (lproto);
6313 if (TREE_CODE (pval) == PROTOCOL_INTERFACE_TYPE
6314 && PROTOCOL_FORWARD_DECL (pval))
6316 e = build_unary_op (input_location, ADDR_EXPR,
6317 PROTOCOL_FORWARD_DECL (pval), 0);
6318 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, e);
6322 /* static struct objc_protocol *refs[n]; */
6324 switch (TREE_CODE (i_or_p))
6326 case PROTOCOL_INTERFACE_TYPE:
6327 ref_name = synth_id_with_class_suffix ("_OBJC_PROTOCOL_REFS", i_or_p);
6329 case CLASS_INTERFACE_TYPE:
6330 ref_name = synth_id_with_class_suffix ("_OBJC_CLASS_PROTOCOLS", i_or_p);
6332 case CATEGORY_INTERFACE_TYPE:
6333 ref_name = synth_id_with_class_suffix ("_OBJC_CATEGORY_PROTOCOLS", i_or_p);
6339 ptype = build_pointer_type (objc_protocol_template);
6340 array_type = build_sized_array_type (ptype, size + 3);
6341 refs_decl = start_var_decl (array_type, ref_name);
6343 finish_var_decl (refs_decl,
6344 objc_build_constructor (TREE_TYPE (refs_decl), v));
6350 build_category_initializer (tree type, tree cat_name, tree class_name,
6351 tree instance_methods, tree class_methods,
6355 VEC(constructor_elt,gc) *v = NULL;
6357 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, cat_name);
6358 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, class_name);
6360 if (!instance_methods)
6361 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6364 expr = convert (objc_method_list_ptr,
6365 build_unary_op (input_location, ADDR_EXPR,
6366 instance_methods, 0));
6367 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6370 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6373 expr = convert (objc_method_list_ptr,
6374 build_unary_op (input_location, ADDR_EXPR,
6376 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6379 /* protocol_list = */
6381 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6384 expr = convert (build_pointer_type
6386 (objc_protocol_template)),
6387 build_unary_op (input_location, ADDR_EXPR,
6389 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6392 return objc_build_constructor (type, v);
6395 /* struct _objc_class {
6396 struct objc_class *isa;
6397 struct objc_class *super_class;
6402 struct objc_ivar_list *ivars;
6403 struct objc_method_list *methods;
6404 if (flag_next_runtime)
6405 struct objc_cache *cache;
6407 struct sarray *dtable;
6408 struct objc_class *subclass_list;
6409 struct objc_class *sibling_class;
6411 struct objc_protocol_list *protocols;
6412 if (flag_next_runtime)
6414 void *gc_object_type;
6418 build_shared_structure_initializer (tree type, tree isa, tree super,
6419 tree name, tree size, int status,
6420 tree dispatch_table, tree ivar_list,
6424 VEC(constructor_elt,gc) *v = NULL;
6427 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, isa);
6430 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, super);
6433 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, default_conversion (name));
6436 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6437 build_int_cst (long_integer_type_node, 0));
6440 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6441 build_int_cst (long_integer_type_node, status));
6443 /* instance_size = */
6444 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6445 convert (long_integer_type_node, size));
6447 /* objc_ivar_list = */
6449 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6452 expr = convert (objc_ivar_list_ptr,
6453 build_unary_op (input_location, ADDR_EXPR,
6455 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6458 /* objc_method_list = */
6459 if (!dispatch_table)
6460 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6463 expr = convert (objc_method_list_ptr,
6464 build_unary_op (input_location, ADDR_EXPR,
6465 dispatch_table, 0));
6466 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6469 if (flag_next_runtime)
6470 /* method_cache = */
6471 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6475 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6477 /* subclass_list = */
6478 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6480 /* sibling_class = */
6481 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6484 /* protocol_list = */
6485 if (! protocol_list)
6486 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6489 expr = convert (build_pointer_type
6491 (objc_protocol_template)),
6492 build_unary_op (input_location, ADDR_EXPR,
6494 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6497 if (flag_next_runtime)
6499 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6501 /* gc_object_type = NULL */
6502 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6504 return objc_build_constructor (type, v);
6507 /* Retrieve category interface CAT_NAME (if any) associated with CLASS. */
6510 lookup_category (tree klass, tree cat_name)
6512 tree category = CLASS_CATEGORY_LIST (klass);
6514 while (category && CLASS_SUPER_NAME (category) != cat_name)
6515 category = CLASS_CATEGORY_LIST (category);
6519 /* static struct objc_category _OBJC_CATEGORY_<name> = { ... }; */
6522 generate_category (struct imp_entry *impent)
6524 tree initlist, cat_name_expr, class_name_expr;
6525 tree protocol_decl, category;
6526 tree cat = impent->imp_context;
6528 implementation_template = impent->imp_template;
6529 UOBJC_CLASS_decl = impent->class_decl;
6530 UOBJC_METACLASS_decl = impent->meta_decl;
6532 add_class_reference (CLASS_NAME (cat));
6533 cat_name_expr = add_objc_string (CLASS_SUPER_NAME (cat), class_names);
6535 class_name_expr = add_objc_string (CLASS_NAME (cat), class_names);
6537 category = lookup_category (implementation_template,
6538 CLASS_SUPER_NAME (cat));
6540 if (category && CLASS_PROTOCOL_LIST (category))
6542 generate_protocol_references (CLASS_PROTOCOL_LIST (category));
6543 protocol_decl = generate_protocol_list (category);
6548 initlist = build_category_initializer (TREE_TYPE (UOBJC_CLASS_decl),
6549 cat_name_expr, class_name_expr,
6550 UOBJC_INSTANCE_METHODS_decl,
6551 UOBJC_CLASS_METHODS_decl,
6553 /* Finish and initialize the forward decl. */
6554 finish_var_decl (UOBJC_CLASS_decl, initlist);
6557 /* static struct objc_class _OBJC_METACLASS_Foo={ ... };
6558 static struct objc_class _OBJC_CLASS_Foo={ ... }; */
6561 generate_shared_structures (struct imp_entry *impent)
6563 tree name_expr, super_expr, root_expr;
6564 tree my_root_id, my_super_id;
6565 tree cast_type, initlist, protocol_decl;
6568 objc_implementation_context = impent->imp_context;
6569 implementation_template = impent->imp_template;
6570 UOBJC_CLASS_decl = impent->class_decl;
6571 UOBJC_METACLASS_decl = impent->meta_decl;
6572 cls_flags = impent->has_cxx_cdtors ? CLS_HAS_CXX_STRUCTORS : 0 ;
6574 my_super_id = CLASS_SUPER_NAME (implementation_template);
6577 add_class_reference (my_super_id);
6579 /* Compute "my_root_id" - this is required for code generation.
6580 the "isa" for all meta class structures points to the root of
6581 the inheritance hierarchy (e.g. "__Object")... */
6582 my_root_id = my_super_id;
6585 tree my_root_int = lookup_interface (my_root_id);
6587 if (my_root_int && CLASS_SUPER_NAME (my_root_int))
6588 my_root_id = CLASS_SUPER_NAME (my_root_int);
6595 /* No super class. */
6596 my_root_id = CLASS_NAME (implementation_template);
6598 cast_type = build_pointer_type (objc_class_template);
6599 name_expr = add_objc_string (CLASS_NAME (implementation_template),
6602 /* Install class `isa' and `super' pointers at runtime. */
6604 super_expr = add_objc_string (my_super_id, class_names);
6606 super_expr = integer_zero_node;
6608 super_expr = build_c_cast (input_location,
6609 cast_type, super_expr); /* cast! */
6611 root_expr = add_objc_string (my_root_id, class_names);
6612 root_expr = build_c_cast (input_location, cast_type, root_expr); /* cast! */
6614 if (CLASS_PROTOCOL_LIST (implementation_template))
6616 generate_protocol_references
6617 (CLASS_PROTOCOL_LIST (implementation_template));
6618 protocol_decl = generate_protocol_list (implementation_template);
6623 /* static struct objc_class _OBJC_METACLASS_Foo = { ... }; */
6626 = build_shared_structure_initializer
6627 (TREE_TYPE (UOBJC_METACLASS_decl),
6628 root_expr, super_expr, name_expr,
6629 convert (integer_type_node, TYPE_SIZE_UNIT (objc_class_template)),
6631 UOBJC_CLASS_METHODS_decl,
6632 UOBJC_CLASS_VARIABLES_decl,
6635 finish_var_decl (UOBJC_METACLASS_decl, initlist);
6637 /* static struct objc_class _OBJC_CLASS_Foo={ ... }; */
6640 = build_shared_structure_initializer
6641 (TREE_TYPE (UOBJC_CLASS_decl),
6642 build_unary_op (input_location, ADDR_EXPR, UOBJC_METACLASS_decl, 0),
6643 super_expr, name_expr,
6644 convert (integer_type_node,
6645 TYPE_SIZE_UNIT (CLASS_STATIC_TEMPLATE
6646 (implementation_template))),
6647 1 /*CLS_FACTORY*/ | cls_flags,
6648 UOBJC_INSTANCE_METHODS_decl,
6649 UOBJC_INSTANCE_VARIABLES_decl,
6652 finish_var_decl (UOBJC_CLASS_decl, initlist);
6657 synth_id_with_class_suffix (const char *preamble, tree ctxt)
6659 static char string[BUFSIZE];
6661 switch (TREE_CODE (ctxt))
6663 case CLASS_IMPLEMENTATION_TYPE:
6664 case CLASS_INTERFACE_TYPE:
6665 sprintf (string, "%s_%s", preamble,
6666 IDENTIFIER_POINTER (CLASS_NAME (ctxt)));
6668 case CATEGORY_IMPLEMENTATION_TYPE:
6669 case CATEGORY_INTERFACE_TYPE:
6671 /* We have a category. */
6672 const char *const class_name
6673 = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
6674 const char *const class_super_name
6675 = IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context));
6676 sprintf (string, "%s_%s_%s", preamble, class_name, class_super_name);
6679 case PROTOCOL_INTERFACE_TYPE:
6681 const char *protocol_name = IDENTIFIER_POINTER (PROTOCOL_NAME (ctxt));
6682 sprintf (string, "%s_%s", preamble, protocol_name);
6692 /* If type is empty or only type qualifiers are present, add default
6693 type of id (otherwise grokdeclarator will default to int). */
6695 adjust_type_for_id_default (tree type)
6698 type = make_node (TREE_LIST);
6700 if (!TREE_VALUE (type))
6701 TREE_VALUE (type) = objc_object_type;
6702 else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE
6703 && TYPED_OBJECT (TREE_VALUE (type)))
6704 error ("can not use an object as parameter to a method");
6709 /* Return a KEYWORD_DECL built using the specified key_name, arg_type,
6710 arg_name and attributes. (TODO: Rename KEYWORD_DECL to
6711 OBJC_METHOD_PARM_DECL ?)
6713 A KEYWORD_DECL is a tree representing the declaration of a
6714 parameter of an Objective-C method. It is produced when parsing a
6715 fragment of Objective-C method declaration of the form
6718 selector ':' '(' typename ')' identifier
6720 For example, take the Objective-C method
6722 -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type;
6724 the two fragments "pathForResource:(NSString *)resource" and
6725 "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The
6726 KEYWORD_DECL stores the 'key_name' (eg, identifier for
6727 "pathForResource"), the 'arg_type' (eg, tree representing a
6728 NSString *), the 'arg_name' (eg identifier for "resource") and
6729 potentially some attributes (for example, a tree representing
6730 __attribute__ ((unused)) if such an attribute was attached to a
6731 certain parameter). You can access this information using the
6732 TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name),
6733 KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes).
6735 'key_name' is an identifier node (and is optional as you can omit
6736 it in Objective-C methods).
6737 'arg_type' is a tree list (and is optional too if no parameter type
6739 'arg_name' is an identifier node and is required.
6740 'attributes' is an optional tree containing parameter attributes. */
6742 objc_build_keyword_decl (tree key_name, tree arg_type,
6743 tree arg_name, tree attributes)
6747 if (flag_objc1_only && attributes)
6748 error_at (input_location, "method argument attributes are not available in Objective-C 1.0");
6750 /* If no type is specified, default to "id". */
6751 arg_type = adjust_type_for_id_default (arg_type);
6753 keyword_decl = make_node (KEYWORD_DECL);
6755 TREE_TYPE (keyword_decl) = arg_type;
6756 KEYWORD_ARG_NAME (keyword_decl) = arg_name;
6757 KEYWORD_KEY_NAME (keyword_decl) = key_name;
6758 DECL_ATTRIBUTES (keyword_decl) = attributes;
6760 return keyword_decl;
6763 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
6765 build_keyword_selector (tree selector)
6768 tree key_chain, key_name;
6771 /* Scan the selector to see how much space we'll need. */
6772 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
6774 switch (TREE_CODE (selector))
6777 key_name = KEYWORD_KEY_NAME (key_chain);
6780 key_name = TREE_PURPOSE (key_chain);
6787 len += IDENTIFIER_LENGTH (key_name) + 1;
6789 /* Just a ':' arg. */
6793 buf = (char *) alloca (len + 1);
6794 /* Start the buffer out as an empty string. */
6797 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
6799 switch (TREE_CODE (selector))
6802 key_name = KEYWORD_KEY_NAME (key_chain);
6805 key_name = TREE_PURPOSE (key_chain);
6806 /* The keyword decl chain will later be used as a function
6807 argument chain. Unhook the selector itself so as to not
6808 confuse other parts of the compiler. */
6809 TREE_PURPOSE (key_chain) = NULL_TREE;
6816 strcat (buf, IDENTIFIER_POINTER (key_name));
6820 return get_identifier (buf);
6823 /* Used for declarations and definitions. */
6826 build_method_decl (enum tree_code code, tree ret_type, tree selector,
6827 tree add_args, bool ellipsis)
6831 /* If no type is specified, default to "id". */
6832 ret_type = adjust_type_for_id_default (ret_type);
6834 /* Note how a method_decl has a TREE_TYPE which is not the function
6835 type of the function implementing the method, but only the return
6836 type of the method. We may want to change this, and store the
6837 entire function type in there (eg, it may be used to simplify
6838 dealing with attributes below). */
6839 method_decl = make_node (code);
6840 TREE_TYPE (method_decl) = ret_type;
6842 /* If we have a keyword selector, create an identifier_node that
6843 represents the full selector name (`:' included)... */
6844 if (TREE_CODE (selector) == KEYWORD_DECL)
6846 METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
6847 METHOD_SEL_ARGS (method_decl) = selector;
6848 METHOD_ADD_ARGS (method_decl) = add_args;
6849 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis;
6853 METHOD_SEL_NAME (method_decl) = selector;
6854 METHOD_SEL_ARGS (method_decl) = NULL_TREE;
6855 METHOD_ADD_ARGS (method_decl) = NULL_TREE;
6861 #define METHOD_DEF 0
6862 #define METHOD_REF 1
6864 /* This routine processes objective-c method attributes. */
6867 objc_decl_method_attributes (tree *node, tree attributes, int flags)
6869 /* TODO: Replace the hackery below. An idea would be to store the
6870 full function type in the method declaration (for example in
6871 TREE_TYPE) and then expose ObjC method declarations to c-family
6872 and they could deal with them by simply treating them as
6875 /* Because of the dangers in the hackery below, we filter out any
6876 attribute that we do not know about. For the ones we know about,
6877 we know that they work with the hackery. For the other ones,
6878 there is no guarantee, so we have to filter them out. */
6879 tree filtered_attributes = NULL_TREE;
6884 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
6886 tree name = TREE_PURPOSE (attribute);
6888 if (is_attribute_p ("deprecated", name)
6889 || is_attribute_p ("sentinel", name)
6890 || is_attribute_p ("noreturn", name))
6892 /* An attribute that we support; add it to the filtered
6894 filtered_attributes = chainon (filtered_attributes,
6895 copy_node (attribute));
6897 else if (is_attribute_p ("format", name))
6899 /* "format" is special because before adding it to the
6900 filtered attributes we need to adjust the specified
6901 format by adding the hidden function parameters for
6902 an Objective-C method (self, _cmd). */
6903 tree new_attribute = copy_node (attribute);
6905 /* Check the arguments specified with the attribute, and
6906 modify them adding 2 for the two hidden arguments.
6907 Note how this differs from C++; according to the
6908 specs, C++ does not do it so you have to add the +1
6909 yourself. For Objective-C, instead, the compiler
6910 adds the +2 for you. */
6912 /* The attribute arguments have not been checked yet, so
6913 we need to be careful as they could be missing or
6914 invalid. If anything looks wrong, we skip the
6915 process and the compiler will complain about it later
6916 when it validates the attribute. */
6917 /* Check that we have at least three arguments. */
6918 if (TREE_VALUE (new_attribute)
6919 && TREE_CHAIN (TREE_VALUE (new_attribute))
6920 && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (new_attribute))))
6922 tree second_argument = TREE_CHAIN (TREE_VALUE (new_attribute));
6923 tree third_argument = TREE_CHAIN (second_argument);
6926 /* This is the second argument, the "string-index",
6927 which specifies the index of the format string
6929 number = TREE_VALUE (second_argument);
6931 && TREE_CODE (number) == INTEGER_CST
6932 && TREE_INT_CST_HIGH (number) == 0)
6934 TREE_VALUE (second_argument)
6935 = build_int_cst (integer_type_node,
6936 TREE_INT_CST_LOW (number) + 2);
6939 /* This is the third argument, the "first-to-check",
6940 which specifies the index of the first argument to
6941 check. This could be 0, meaning it is not available,
6942 in which case we don't need to add 2. Add 2 if not
6944 number = TREE_VALUE (third_argument);
6946 && TREE_CODE (number) == INTEGER_CST
6947 && TREE_INT_CST_HIGH (number) == 0
6948 && TREE_INT_CST_LOW (number) != 0)
6950 TREE_VALUE (third_argument)
6951 = build_int_cst (integer_type_node,
6952 TREE_INT_CST_LOW (number) + 2);
6955 filtered_attributes = chainon (filtered_attributes,
6959 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
6963 if (filtered_attributes)
6965 /* This hackery changes the TREE_TYPE of the ObjC method
6966 declaration to be a function type, so that decl_attributes
6967 will treat the ObjC method as if it was a function. Some
6968 attributes (sentinel, format) will be applied to the function
6969 type, changing it in place; so after calling decl_attributes,
6970 we extract the function type attributes and store them in
6971 METHOD_TYPE_ATTRIBUTES. Some other attributes (noreturn,
6972 deprecated) are applied directly to the method declaration
6973 (by setting TREE_DEPRECATED and TREE_THIS_VOLATILE) so there
6974 is nothing to do. */
6975 tree saved_type = TREE_TYPE (*node);
6976 TREE_TYPE (*node) = build_function_type
6977 (TREE_VALUE (saved_type), get_arg_type_list (*node, METHOD_REF, 0));
6978 decl_attributes (node, filtered_attributes, flags);
6979 METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node));
6980 TREE_TYPE (*node) = saved_type;
6985 objc_method_decl (enum tree_code opcode)
6987 return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL;
6990 /* Used by `build_objc_method_call' and `comp_proto_with_proto'. Return
6991 an argument list for method METH. CONTEXT is either METHOD_DEF or
6992 METHOD_REF, saying whether we are trying to define a method or call
6993 one. SUPERFLAG says this is for a send to super; this makes a
6994 difference for the NeXT calling sequence in which the lookup and
6995 the method call are done together. If METH is null, user-defined
6996 arguments (i.e., beyond self and _cmd) shall be represented by `...'. */
6999 get_arg_type_list (tree meth, int context, int superflag)
7003 /* Receiver type. */
7004 if (flag_next_runtime && superflag)
7005 arglist = build_tree_list (NULL_TREE, objc_super_type);
7006 else if (context == METHOD_DEF && TREE_CODE (meth) == INSTANCE_METHOD_DECL)
7007 arglist = build_tree_list (NULL_TREE, objc_instance_type);
7009 arglist = build_tree_list (NULL_TREE, objc_object_type);
7011 /* Selector type - will eventually change to `int'. */
7012 chainon (arglist, build_tree_list (NULL_TREE, objc_selector_type));
7014 /* No actual method prototype given -- assume that remaining arguments
7019 /* Build a list of argument types. */
7020 for (akey = METHOD_SEL_ARGS (meth); akey; akey = DECL_CHAIN (akey))
7022 tree arg_type = TREE_VALUE (TREE_TYPE (akey));
7024 /* Decay argument types for the underlying C function as appropriate. */
7025 arg_type = objc_decay_parm_type (arg_type);
7027 chainon (arglist, build_tree_list (NULL_TREE, arg_type));
7030 if (METHOD_ADD_ARGS (meth))
7032 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (meth));
7033 akey; akey = TREE_CHAIN (akey))
7035 tree arg_type = TREE_TYPE (TREE_VALUE (akey));
7037 arg_type = objc_decay_parm_type (arg_type);
7039 chainon (arglist, build_tree_list (NULL_TREE, arg_type));
7042 if (!METHOD_ADD_ARGS_ELLIPSIS_P (meth))
7043 goto lack_of_ellipsis;
7048 chainon (arglist, OBJC_VOID_AT_END);
7055 check_duplicates (hash hsh, int methods, int is_class)
7057 tree meth = NULL_TREE;
7065 /* We have two or more methods with the same name but
7069 /* But just how different are those types? If
7070 -Wno-strict-selector-match is specified, we shall not
7071 complain if the differences are solely among types with
7072 identical size and alignment. */
7073 if (!warn_strict_selector_match)
7075 for (loop = hsh->list; loop; loop = loop->next)
7076 if (!comp_proto_with_proto (meth, loop->value, 0))
7085 bool type = TREE_CODE (meth) == INSTANCE_METHOD_DECL;
7087 warning_at (input_location, 0,
7088 "multiple methods named %<%c%E%> found",
7089 (is_class ? '+' : '-'),
7090 METHOD_SEL_NAME (meth));
7091 inform (DECL_SOURCE_LOCATION (meth), "using %<%c%s%>",
7093 identifier_to_locale (gen_method_decl (meth)));
7097 bool type = TREE_CODE (meth) == INSTANCE_METHOD_DECL;
7099 warning_at (input_location, 0,
7100 "multiple selectors named %<%c%E%> found",
7101 (is_class ? '+' : '-'),
7102 METHOD_SEL_NAME (meth));
7103 inform (DECL_SOURCE_LOCATION (meth), "found %<%c%s%>",
7105 identifier_to_locale (gen_method_decl (meth)));
7108 for (loop = hsh->list; loop; loop = loop->next)
7110 bool type = TREE_CODE (loop->value) == INSTANCE_METHOD_DECL;
7112 inform (DECL_SOURCE_LOCATION (loop->value), "also found %<%c%s%>",
7114 identifier_to_locale (gen_method_decl (loop->value)));
7121 /* If RECEIVER is a class reference, return the identifier node for
7122 the referenced class. RECEIVER is created by objc_get_class_reference,
7123 so we check the exact form created depending on which runtimes are
7127 receiver_is_class_object (tree receiver, int self, int super)
7129 tree chain, exp, arg;
7131 /* The receiver is 'self' or 'super' in the context of a class method. */
7132 if (objc_method_context
7133 && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
7136 ? CLASS_SUPER_NAME (implementation_template)
7137 : CLASS_NAME (implementation_template));
7139 if (flag_next_runtime)
7141 /* The receiver is a variable created by
7142 build_class_reference_decl. */
7143 if (TREE_CODE (receiver) == VAR_DECL && IS_CLASS (TREE_TYPE (receiver)))
7144 /* Look up the identifier. */
7145 for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
7146 if (TREE_PURPOSE (chain) == receiver)
7147 return TREE_VALUE (chain);
7150 /* The receiver is a function call that returns an id. Check if
7151 it is a call to objc_getClass, if so, pick up the class name. */
7152 if (TREE_CODE (receiver) == CALL_EXPR
7153 && (exp = CALL_EXPR_FN (receiver))
7154 && TREE_CODE (exp) == ADDR_EXPR
7155 && (exp = TREE_OPERAND (exp, 0))
7156 && TREE_CODE (exp) == FUNCTION_DECL
7157 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
7158 prototypes for objc_get_class(). Thankfully, they seem to share the
7159 same function type. */
7160 && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl)
7161 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), TAG_GETCLASS)
7162 /* We have a call to objc_get_class/objc_getClass! */
7163 && (arg = CALL_EXPR_ARG (receiver, 0)))
7166 if (TREE_CODE (arg) == ADDR_EXPR
7167 && (arg = TREE_OPERAND (arg, 0))
7168 && TREE_CODE (arg) == STRING_CST)
7169 /* Finally, we have the class name. */
7170 return get_identifier (TREE_STRING_POINTER (arg));
7175 /* If we are currently building a message expr, this holds
7176 the identifier of the selector of the message. This is
7177 used when printing warnings about argument mismatches. */
7179 static tree current_objc_message_selector = 0;
7182 objc_message_selector (void)
7184 return current_objc_message_selector;
7187 /* Construct an expression for sending a message.
7188 MESS has the object to send to in TREE_PURPOSE
7189 and the argument list (including selector) in TREE_VALUE.
7191 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
7192 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
7195 objc_build_message_expr (tree mess)
7197 tree receiver = TREE_PURPOSE (mess);
7200 tree args = TREE_PURPOSE (TREE_VALUE (mess));
7202 tree args = TREE_VALUE (mess);
7204 tree method_params = NULL_TREE;
7206 if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK)
7207 return error_mark_node;
7209 /* Obtain the full selector name. */
7210 switch (TREE_CODE (args))
7212 case IDENTIFIER_NODE:
7213 /* A unary selector. */
7217 sel_name = build_keyword_selector (args);
7223 /* Build the parameter list to give to the method. */
7224 if (TREE_CODE (args) == TREE_LIST)
7226 method_params = chainon (args, TREE_VALUE (TREE_VALUE (mess)));
7229 tree chain = args, prev = NULL_TREE;
7231 /* We have a keyword selector--check for comma expressions. */
7234 tree element = TREE_VALUE (chain);
7236 /* We have a comma expression, must collapse... */
7237 if (TREE_CODE (element) == TREE_LIST)
7240 TREE_CHAIN (prev) = element;
7245 chain = TREE_CHAIN (chain);
7247 method_params = args;
7252 if (processing_template_decl)
7253 /* Must wait until template instantiation time. */
7254 return build_min_nt (MESSAGE_SEND_EXPR, receiver, sel_name,
7258 return objc_finish_message_expr (receiver, sel_name, method_params);
7261 /* Look up method SEL_NAME that would be suitable for receiver
7262 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
7263 nonzero), and report on any duplicates. */
7266 lookup_method_in_hash_lists (tree sel_name, int is_class)
7268 hash method_prototype = NULL;
7271 method_prototype = hash_lookup (nst_method_hash_list,
7274 if (!method_prototype)
7276 method_prototype = hash_lookup (cls_method_hash_list,
7281 return check_duplicates (method_prototype, 1, is_class);
7284 /* The 'objc_finish_message_expr' routine is called from within
7285 'objc_build_message_expr' for non-template functions. In the case of
7286 C++ template functions, it is called from 'build_expr_from_tree'
7287 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded. */
7290 objc_finish_message_expr (tree receiver, tree sel_name, tree method_params)
7292 tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype;
7293 tree selector, retval, class_tree;
7294 int self, super, have_cast;
7296 /* We have used the receiver, so mark it as read. */
7297 mark_exp_read (receiver);
7299 /* Extract the receiver of the message, as well as its type
7300 (where the latter may take the form of a cast or be inferred
7301 from the implementation context). */
7303 while (TREE_CODE (rtype) == COMPOUND_EXPR
7304 || TREE_CODE (rtype) == MODIFY_EXPR
7305 || CONVERT_EXPR_P (rtype)
7306 || TREE_CODE (rtype) == COMPONENT_REF)
7307 rtype = TREE_OPERAND (rtype, 0);
7309 self = (rtype == self_decl);
7310 super = (rtype == UOBJC_SUPER_decl);
7311 rtype = TREE_TYPE (receiver);
7313 have_cast = (TREE_CODE (receiver) == NOP_EXPR
7314 || (TREE_CODE (receiver) == COMPOUND_EXPR
7315 && !IS_SUPER (rtype)));
7317 /* If we are calling [super dealloc], reset our warning flag. */
7318 if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name)))
7319 should_call_super_dealloc = 0;
7321 /* If the receiver is a class object, retrieve the corresponding
7322 @interface, if one exists. */
7323 class_tree = receiver_is_class_object (receiver, self, super);
7325 /* Now determine the receiver type (if an explicit cast has not been
7330 rtype = lookup_interface (class_tree);
7331 /* Handle `self' and `super'. */
7334 if (!CLASS_SUPER_NAME (implementation_template))
7336 error ("no super class declared in @interface for %qE",
7337 CLASS_NAME (implementation_template));
7338 return error_mark_node;
7340 rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template));
7343 rtype = lookup_interface (CLASS_NAME (implementation_template));
7346 /* If receiver is of type `id' or `Class' (or if the @interface for a
7347 class is not visible), we shall be satisfied with the existence of
7348 any instance or class method. */
7349 if (objc_is_id (rtype))
7351 class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE);
7352 rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
7353 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
7359 /* If messaging 'id <Protos>' or 'Class <Proto>', first search
7360 in protocols themselves for the method prototype. */
7362 = lookup_method_in_protocol_list (rprotos, sel_name,
7363 class_tree != NULL_TREE);
7365 /* If messaging 'Class <Proto>' but did not find a class method
7366 prototype, search for an instance method instead, and warn
7367 about having done so. */
7368 if (!method_prototype && !rtype && class_tree != NULL_TREE)
7371 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
7373 if (method_prototype)
7374 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
7375 sel_name, sel_name);
7381 tree orig_rtype = rtype;
7383 if (TREE_CODE (rtype) == POINTER_TYPE)
7384 rtype = TREE_TYPE (rtype);
7385 /* Traverse typedef aliases */
7386 while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype)
7387 && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL
7388 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)))
7389 rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype));
7390 if (TYPED_OBJECT (rtype))
7392 rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype);
7393 rtype = TYPE_OBJC_INTERFACE (rtype);
7395 /* If we could not find an @interface declaration, we must have
7396 only seen a @class declaration; so, we cannot say anything
7397 more intelligent about which methods the receiver will
7399 if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE)
7402 /* We could not find an @interface declaration, yet Message maybe in a
7403 @class's protocol. */
7404 if (!method_prototype && rprotos)
7406 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
7408 else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE
7409 || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE)
7411 /* We have a valid ObjC class name. Look up the method name
7412 in the published @interface for the class (and its
7415 = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE);
7417 /* If the method was not found in the @interface, it may still
7418 exist locally as part of the @implementation. */
7419 if (!method_prototype && objc_implementation_context
7420 && CLASS_NAME (objc_implementation_context)
7421 == OBJC_TYPE_NAME (rtype))
7425 ? CLASS_CLS_METHODS (objc_implementation_context)
7426 : CLASS_NST_METHODS (objc_implementation_context)),
7429 /* If we haven't found a candidate method by now, try looking for
7430 it in the protocol list. */
7431 if (!method_prototype && rprotos)
7433 = lookup_method_in_protocol_list (rprotos, sel_name,
7434 class_tree != NULL_TREE);
7438 warning (0, "invalid receiver type %qs",
7439 identifier_to_locale (gen_type_name (orig_rtype)));
7440 /* After issuing the "invalid receiver" warning, perform method
7441 lookup as if we were messaging 'id'. */
7442 rtype = rprotos = NULL_TREE;
7447 /* For 'id' or 'Class' receivers, search in the global hash table
7448 as a last resort. For all receivers, warn if protocol searches
7450 if (!method_prototype)
7453 warning (0, "%<%c%E%> not found in protocol(s)",
7454 (class_tree ? '+' : '-'),
7459 = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE);
7462 if (!method_prototype)
7464 static bool warn_missing_methods = false;
7467 warning (0, "%qE may not respond to %<%c%E%>",
7468 OBJC_TYPE_NAME (rtype),
7469 (class_tree ? '+' : '-'),
7471 /* If we are messaging an 'id' or 'Class' object and made it here,
7472 then we have failed to find _any_ instance or class method,
7475 warning (0, "no %<%c%E%> method found",
7476 (class_tree ? '+' : '-'),
7479 if (!warn_missing_methods)
7481 warning_at (input_location,
7482 0, "(Messages without a matching method signature");
7483 warning_at (input_location,
7484 0, "will be assumed to return %<id%> and accept");
7485 warning_at (input_location,
7486 0, "%<...%> as arguments.)");
7487 warn_missing_methods = true;
7492 /* Warn if the method is deprecated, but not if the receiver is
7493 a generic 'id'. 'id' is used to cast an object to a generic
7494 object of an unspecified class; in that case, we'll use
7495 whatever method prototype we can find to get the method
7496 argument and return types, but it is not appropriate to
7497 produce deprecation warnings since we don't know the class
7498 that the object will be of at runtime. The @interface(s) for
7499 that class may not even be available to the compiler right
7500 now, and it is perfectly possible that the method is marked
7501 as non-deprecated in such @interface(s).
7503 In practice this makes sense since casting an object to 'id'
7504 is often used precisely to turn off warnings associated with
7505 the object being of a particular class. */
7506 if (TREE_DEPRECATED (method_prototype) && rtype != NULL_TREE)
7507 warn_deprecated_use (method_prototype, NULL_TREE);
7511 /* Save the selector name for printing error messages. */
7512 current_objc_message_selector = sel_name;
7514 /* Build the parameters list for looking up the method.
7515 These are the object itself and the selector. */
7517 if (flag_typed_selectors)
7518 selector = build_typed_selector_reference (input_location,
7519 sel_name, method_prototype);
7521 selector = build_selector_reference (input_location, sel_name);
7523 retval = build_objc_method_call (input_location, super, method_prototype,
7525 selector, method_params);
7527 current_objc_message_selector = 0;
7532 /* Build a tree expression to send OBJECT the operation SELECTOR,
7533 looking up the method on object LOOKUP_OBJECT (often same as OBJECT),
7534 assuming the method has prototype METHOD_PROTOTYPE.
7535 (That is an INSTANCE_METHOD_DECL or CLASS_METHOD_DECL.)
7536 LOC is the location of the expression to build.
7537 Use METHOD_PARAMS as list of args to pass to the method.
7538 If SUPER_FLAG is nonzero, we look up the superclass's method. */
7541 build_objc_method_call (location_t loc, int super_flag, tree method_prototype,
7542 tree lookup_object, tree selector,
7545 tree sender = (super_flag ? umsg_super_decl :
7546 (!flag_next_runtime || flag_nil_receivers
7547 ? (flag_objc_direct_dispatch
7550 : umsg_nonnil_decl));
7551 tree rcv_p = (super_flag ? objc_super_type : objc_object_type);
7552 VEC(tree, gc) *parms = NULL;
7553 unsigned nparm = (method_params ? list_length (method_params) : 0);
7555 /* If a prototype for the method to be called exists, then cast
7556 the sender's return type and arguments to match that of the method.
7557 Otherwise, leave sender as is. */
7560 ? TREE_VALUE (TREE_TYPE (method_prototype))
7561 : objc_object_type);
7563 tree method_param_types =
7564 get_arg_type_list (method_prototype, METHOD_REF, super_flag);
7565 tree ftype = build_function_type (ret_type, method_param_types);
7569 if (method_prototype && METHOD_TYPE_ATTRIBUTES (method_prototype))
7570 ftype = build_type_attribute_variant (ftype,
7571 METHOD_TYPE_ATTRIBUTES
7572 (method_prototype));
7574 sender_cast = build_pointer_type (ftype);
7576 lookup_object = build_c_cast (loc, rcv_p, lookup_object);
7578 /* Use SAVE_EXPR to avoid evaluating the receiver twice. */
7579 lookup_object = save_expr (lookup_object);
7581 /* Param list + 2 slots for object and selector. */
7582 parms = VEC_alloc (tree, gc, nparm + 2);
7584 if (flag_next_runtime)
7586 /* If we are returning a struct in memory, and the address
7587 of that memory location is passed as a hidden first
7588 argument, then change which messenger entry point this
7589 expr will call. NB: Note that sender_cast remains
7590 unchanged (it already has a struct return type). */
7591 if (!targetm.calls.struct_value_rtx (0, 0)
7592 && (TREE_CODE (ret_type) == RECORD_TYPE
7593 || TREE_CODE (ret_type) == UNION_TYPE)
7594 && targetm.calls.return_in_memory (ret_type, 0))
7595 sender = (super_flag ? umsg_super_stret_decl :
7596 flag_nil_receivers ? umsg_stret_decl : umsg_nonnil_stret_decl);
7598 method = build_fold_addr_expr_loc (input_location, sender);
7599 /* Pass the object to the method. */
7600 VEC_quick_push (tree, parms, lookup_object);
7604 /* This is the portable (GNU) way. */
7605 /* First, call the lookup function to get a pointer to the method,
7606 then cast the pointer, then call it with the method arguments. */
7607 VEC(tree, gc) *tv = VEC_alloc (tree, gc, 2);
7608 VEC_quick_push (tree, tv, lookup_object);
7609 VEC_quick_push (tree, tv, selector);
7610 method = build_function_call_vec (loc, sender, tv, NULL);
7611 VEC_free (tree, gc, tv);
7613 /* Pass the appropriate object to the method. */
7614 VEC_quick_push (tree, parms, (super_flag ? self_decl : lookup_object));
7617 /* Pass the selector to the method. */
7618 VEC_quick_push (tree, parms, selector);
7619 /* Now append the remainder of the parms. */
7621 for (; method_params; method_params = TREE_CHAIN (method_params))
7622 VEC_quick_push (tree, parms, TREE_VALUE (method_params));
7624 /* Build an obj_type_ref, with the correct cast for the method call. */
7625 t = build3 (OBJ_TYPE_REF, sender_cast, method,
7626 lookup_object, size_zero_node);
7627 t = build_function_call_vec (loc, t, parms, NULL);\
7628 VEC_free (tree, gc, parms);
7633 build_protocol_reference (tree p)
7636 const char *proto_name;
7638 /* static struct _objc_protocol _OBJC_PROTOCOL_<mumble>; */
7640 proto_name = synth_id_with_class_suffix ("_OBJC_PROTOCOL", p);
7641 decl = start_var_decl (objc_protocol_template, proto_name);
7643 PROTOCOL_FORWARD_DECL (p) = decl;
7646 /* This function is called by the parser when (and only when) a
7647 @protocol() expression is found, in order to compile it. */
7649 objc_build_protocol_expr (tree protoname)
7652 tree p = lookup_protocol (protoname);
7656 error ("cannot find protocol declaration for %qE",
7658 return error_mark_node;
7661 if (!PROTOCOL_FORWARD_DECL (p))
7662 build_protocol_reference (p);
7664 expr = build_unary_op (input_location,
7665 ADDR_EXPR, PROTOCOL_FORWARD_DECL (p), 0);
7667 /* ??? Ideally we'd build the reference with objc_protocol_type directly,
7668 if we have it, rather than converting it here. */
7669 expr = convert (objc_protocol_type, expr);
7671 /* The @protocol() expression is being compiled into a pointer to a
7672 statically allocated instance of the Protocol class. To become
7673 usable at runtime, the 'isa' pointer of the instance need to be
7674 fixed up at runtime by the runtime library, to point to the
7675 actual 'Protocol' class. */
7677 /* For the GNU runtime, put the static Protocol instance in the list
7678 of statically allocated instances, so that we make sure that its
7679 'isa' pointer is fixed up at runtime by the GNU runtime library
7680 to point to the Protocol class (at runtime, when loading the
7681 module, the GNU runtime library loops on the statically allocated
7682 instances (as found in the defs field in objc_symtab) and fixups
7683 all the 'isa' pointers of those objects). */
7684 if (! flag_next_runtime)
7686 /* This type is a struct containing the fields of a Protocol
7687 object. (Cfr. objc_protocol_type instead is the type of a pointer
7688 to such a struct). */
7689 tree protocol_struct_type = xref_tag
7690 (RECORD_TYPE, get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
7693 /* Look for the list of Protocol statically allocated instances
7694 to fixup at runtime. Create a new list to hold Protocol
7695 statically allocated instances, if the list is not found. At
7696 present there is only another list, holding NSConstantString
7697 static instances to be fixed up at runtime. */
7698 for (chain = &objc_static_instances;
7699 *chain && TREE_VALUE (*chain) != protocol_struct_type;
7700 chain = &TREE_CHAIN (*chain));
7703 *chain = tree_cons (NULL_TREE, protocol_struct_type, NULL_TREE);
7704 add_objc_string (OBJC_TYPE_NAME (protocol_struct_type),
7708 /* Add this statically allocated instance to the Protocol list. */
7709 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE,
7710 PROTOCOL_FORWARD_DECL (p),
7711 TREE_PURPOSE (*chain));
7718 /* This function is called by the parser when a @selector() expression
7719 is found, in order to compile it. It is only called by the parser
7720 and only to compile a @selector(). LOC is the location of the
7723 objc_build_selector_expr (location_t loc, tree selnamelist)
7727 /* Obtain the full selector name. */
7728 switch (TREE_CODE (selnamelist))
7730 case IDENTIFIER_NODE:
7731 /* A unary selector. */
7732 selname = selnamelist;
7735 selname = build_keyword_selector (selnamelist);
7741 /* If we are required to check @selector() expressions as they
7742 are found, check that the selector has been declared. */
7743 if (warn_undeclared_selector)
7745 /* Look the selector up in the list of all known class and
7746 instance methods (up to this line) to check that the selector
7750 /* First try with instance methods. */
7751 hsh = hash_lookup (nst_method_hash_list, selname);
7753 /* If not found, try with class methods. */
7756 hsh = hash_lookup (cls_method_hash_list, selname);
7759 /* If still not found, print out a warning. */
7762 warning (0, "undeclared selector %qE", selname);
7767 if (flag_typed_selectors)
7768 return build_typed_selector_reference (loc, selname, 0);
7770 return build_selector_reference (loc, selname);
7773 /* This is used to implement @encode(). See gcc/doc/objc.texi,
7774 section '@encode'. */
7776 objc_build_encode_expr (tree type)
7781 encode_type (type, obstack_object_size (&util_obstack),
7782 OBJC_ENCODE_INLINE_DEFS);
7783 obstack_1grow (&util_obstack, 0); /* null terminate string */
7784 string = XOBFINISH (&util_obstack, const char *);
7786 /* Synthesize a string that represents the encoded struct/union. */
7787 result = my_build_string (strlen (string) + 1, string);
7788 obstack_free (&util_obstack, util_firstobj);
7793 build_ivar_reference (tree id)
7795 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
7797 /* Historically, a class method that produced objects (factory
7798 method) would assign `self' to the instance that it
7799 allocated. This would effectively turn the class method into
7800 an instance method. Following this assignment, the instance
7801 variables could be accessed. That practice, while safe,
7802 violates the simple rule that a class method should not refer
7803 to an instance variable. It's better to catch the cases
7804 where this is done unknowingly than to support the above
7806 warning (0, "instance variable %qE accessed in class method",
7808 self_decl = convert (objc_instance_type, self_decl); /* cast */
7811 return objc_build_component_ref (build_indirect_ref (input_location,
7812 self_decl, RO_ARROW),
7816 /* Compute a hash value for a given method SEL_NAME. */
7819 hash_func (tree sel_name)
7821 const unsigned char *s
7822 = (const unsigned char *)IDENTIFIER_POINTER (sel_name);
7826 h = h * 67 + *s++ - 113;
7833 nst_method_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
7834 cls_method_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
7836 cls_name_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
7837 als_name_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
7839 /* Initialize the hash table used to hold the constant string objects. */
7840 string_htab = htab_create_ggc (31, string_hash,
7844 /* This routine adds sel_name to the hash list. sel_name is a class or alias
7845 name for the class. If alias name, then value is its underlying class.
7846 If class, the value is NULL_TREE. */
7849 hash_class_name_enter (hash *hashlist, tree sel_name, tree value)
7852 int slot = hash_func (sel_name) % SIZEHASHTABLE;
7854 obj = ggc_alloc_hashed_entry ();
7855 if (value != NULL_TREE)
7857 /* Save the underlying class for the 'alias' in the hash table */
7858 attr obj_attr = ggc_alloc_hashed_attribute ();
7859 obj_attr->value = value;
7860 obj->list = obj_attr;
7864 obj->next = hashlist[slot];
7865 obj->key = sel_name;
7867 hashlist[slot] = obj; /* append to front */
7872 Searches in the hash table looking for a match for class or alias name.
7876 hash_class_name_lookup (hash *hashlist, tree sel_name)
7880 target = hashlist[hash_func (sel_name) % SIZEHASHTABLE];
7884 if (sel_name == target->key)
7887 target = target->next;
7892 /* WARNING!!!! hash_enter is called with a method, and will peek
7893 inside to find its selector! But hash_lookup is given a selector
7894 directly, and looks for the selector that's inside the found
7895 entry's key (method) for comparison. */
7898 hash_enter (hash *hashlist, tree method)
7901 int slot = hash_func (METHOD_SEL_NAME (method)) % SIZEHASHTABLE;
7903 obj = ggc_alloc_hashed_entry ();
7905 obj->next = hashlist[slot];
7908 hashlist[slot] = obj; /* append to front */
7912 hash_lookup (hash *hashlist, tree sel_name)
7916 target = hashlist[hash_func (sel_name) % SIZEHASHTABLE];
7920 if (sel_name == METHOD_SEL_NAME (target->key))
7923 target = target->next;
7929 hash_add_attr (hash entry, tree value)
7933 obj = ggc_alloc_hashed_attribute ();
7934 obj->next = entry->list;
7937 entry->list = obj; /* append to front */
7941 lookup_method (tree mchain, tree method)
7945 if (TREE_CODE (method) == IDENTIFIER_NODE)
7948 key = METHOD_SEL_NAME (method);
7952 if (METHOD_SEL_NAME (mchain) == key)
7955 mchain = DECL_CHAIN (mchain);
7960 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance method
7961 in INTERFACE, along with any categories and protocols attached thereto.
7962 If method is not found, and the OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS,
7963 recursively examine the INTERFACE's superclass. If OBJC_LOOKUP_CLASS is
7964 set, OBJC_LOOKUP_NO_SUPER is cleared, and no suitable class method could
7965 be found in INTERFACE or any of its superclasses, look for an _instance_
7966 method of the same name in the root class as a last resort.
7968 If a suitable method cannot be found, return NULL_TREE. */
7971 lookup_method_static (tree interface, tree ident, int flags)
7973 tree meth = NULL_TREE, root_inter = NULL_TREE;
7974 tree inter = interface;
7975 int is_class = (flags & OBJC_LOOKUP_CLASS);
7976 int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER);
7980 tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter);
7981 tree category = inter;
7983 /* First, look up the method in the class itself. */
7984 if ((meth = lookup_method (chain, ident)))
7987 /* Failing that, look for the method in each category of the class. */
7988 while ((category = CLASS_CATEGORY_LIST (category)))
7990 chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category);
7992 /* Check directly in each category. */
7993 if ((meth = lookup_method (chain, ident)))
7996 /* Failing that, check in each category's protocols. */
7997 if (CLASS_PROTOCOL_LIST (category))
7999 if ((meth = (lookup_method_in_protocol_list
8000 (CLASS_PROTOCOL_LIST (category), ident, is_class))))
8005 /* If not found in categories, check in protocols of the main class. */
8006 if (CLASS_PROTOCOL_LIST (inter))
8008 if ((meth = (lookup_method_in_protocol_list
8009 (CLASS_PROTOCOL_LIST (inter), ident, is_class))))
8013 /* If we were instructed not to look in superclasses, don't. */
8014 if (no_superclasses)
8017 /* Failing that, climb up the inheritance hierarchy. */
8019 inter = lookup_interface (CLASS_SUPER_NAME (inter));
8023 /* If no class (factory) method was found, check if an _instance_
8024 method of the same name exists in the root class. This is what
8025 the Objective-C runtime will do. If an instance method was not
8027 return is_class ? lookup_method_static (root_inter, ident, 0): NULL_TREE;
8030 /* Add the method to the hash list if it doesn't contain an identical
8034 add_method_to_hash_list (hash *hash_list, tree method)
8038 if (!(hsh = hash_lookup (hash_list, METHOD_SEL_NAME (method))))
8040 /* Install on a global chain. */
8041 hash_enter (hash_list, method);
8045 /* Check types against those; if different, add to a list. */
8047 int already_there = comp_proto_with_proto (method, hsh->key, 1);
8048 for (loop = hsh->list; !already_there && loop; loop = loop->next)
8049 already_there |= comp_proto_with_proto (method, loop->value, 1);
8051 hash_add_attr (hsh, method);
8056 objc_add_method (tree klass, tree method, int is_class, bool is_optional)
8060 /* @optional methods are added to protocol's OPTIONAL list */
8063 gcc_assert (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE);
8064 if (!(mth = lookup_method (is_class
8065 ? PROTOCOL_OPTIONAL_CLS_METHODS (klass)
8066 : PROTOCOL_OPTIONAL_NST_METHODS (klass),
8071 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass);
8072 PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method;
8076 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass);
8077 PROTOCOL_OPTIONAL_NST_METHODS (klass) = method;
8081 else if (!(mth = lookup_method (is_class
8082 ? CLASS_CLS_METHODS (klass)
8083 : CLASS_NST_METHODS (klass), method)))
8085 /* put method on list in reverse order */
8088 DECL_CHAIN (method) = CLASS_CLS_METHODS (klass);
8089 CLASS_CLS_METHODS (klass) = method;
8093 DECL_CHAIN (method) = CLASS_NST_METHODS (klass);
8094 CLASS_NST_METHODS (klass) = method;
8099 /* When processing an @interface for a class or category, give hard
8100 errors on methods with identical selectors but differing argument
8101 and/or return types. We do not do this for @implementations, because
8102 C/C++ will do it for us (i.e., there will be duplicate function
8103 definition errors). */
8104 if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE
8105 || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE)
8106 && !comp_proto_with_proto (method, mth, 1))
8107 error ("duplicate declaration of method %<%c%E%>",
8108 is_class ? '+' : '-',
8109 METHOD_SEL_NAME (mth));
8113 add_method_to_hash_list (cls_method_hash_list, method);
8116 add_method_to_hash_list (nst_method_hash_list, method);
8118 /* Instance methods in root classes (and categories thereof)
8119 may act as class methods as a last resort. We also add
8120 instance methods listed in @protocol declarations to
8121 the class hash table, on the assumption that @protocols
8122 may be adopted by root classes or categories. */
8123 if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
8124 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
8125 klass = lookup_interface (CLASS_NAME (klass));
8127 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE
8128 || !CLASS_SUPER_NAME (klass))
8129 add_method_to_hash_list (cls_method_hash_list, method);
8136 add_class (tree class_name, tree name)
8138 struct interface_tuple **slot;
8140 /* Put interfaces on list in reverse order. */
8141 TREE_CHAIN (class_name) = interface_chain;
8142 interface_chain = class_name;
8144 if (interface_htab == NULL)
8145 interface_htab = htab_create_ggc (31, hash_interface, eq_interface, NULL);
8146 slot = (struct interface_tuple **)
8147 htab_find_slot_with_hash (interface_htab, name,
8148 IDENTIFIER_HASH_VALUE (name),
8152 *slot = ggc_alloc_cleared_interface_tuple ();
8155 (*slot)->class_name = class_name;
8157 return interface_chain;
8161 add_category (tree klass, tree category)
8163 /* Put categories on list in reverse order. */
8164 tree cat = lookup_category (klass, CLASS_SUPER_NAME (category));
8168 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
8170 CLASS_SUPER_NAME (category));
8174 CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass);
8175 CLASS_CATEGORY_LIST (klass) = category;
8179 /* Called after parsing each instance variable declaration. Necessary to
8180 preserve typedefs and implement public/private...
8182 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
8185 add_instance_variable (tree klass, objc_ivar_visibility_kind visibility,
8188 tree field_type = TREE_TYPE (field_decl);
8189 const char *ivar_name = DECL_NAME (field_decl)
8190 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl)))
8194 if (TREE_CODE (field_type) == REFERENCE_TYPE)
8196 error ("illegal reference type specified for instance variable %qs",
8198 /* Return class as is without adding this ivar. */
8203 if (field_type == error_mark_node || !TYPE_SIZE (field_type)
8204 || TYPE_SIZE (field_type) == error_mark_node)
8205 /* 'type[0]' is allowed, but 'type[]' is not! */
8207 error ("instance variable %qs has unknown size", ivar_name);
8208 /* Return class as is without adding this ivar. */
8213 /* Check if the ivar being added has a non-POD C++ type. If so, we will
8214 need to either (1) warn the user about it or (2) generate suitable
8215 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
8216 methods (if '-fobjc-call-cxx-cdtors' was specified). */
8217 if (MAYBE_CLASS_TYPE_P (field_type)
8218 && (TYPE_NEEDS_CONSTRUCTING (field_type)
8219 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)
8220 || TYPE_POLYMORPHIC_P (field_type)))
8222 tree type_name = OBJC_TYPE_NAME (field_type);
8224 if (flag_objc_call_cxx_cdtors)
8226 /* Since the ObjC runtime will be calling the constructors and
8227 destructors for us, the only thing we can't handle is the lack
8228 of a default constructor. */
8229 if (TYPE_NEEDS_CONSTRUCTING (field_type)
8230 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type))
8232 warning (0, "type %qE has no default constructor to call",
8235 /* If we cannot call a constructor, we should also avoid
8236 calling the destructor, for symmetry. */
8237 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
8238 warning (0, "destructor for %qE shall not be run either",
8244 static bool warn_cxx_ivars = false;
8246 if (TYPE_POLYMORPHIC_P (field_type))
8248 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
8250 error ("type %qE has virtual member functions", type_name);
8251 error ("illegal aggregate type %qE specified "
8252 "for instance variable %qs",
8253 type_name, ivar_name);
8254 /* Return class as is without adding this ivar. */
8258 /* User-defined constructors and destructors are not known to Obj-C
8259 and hence will not be called. This may or may not be a problem. */
8260 if (TYPE_NEEDS_CONSTRUCTING (field_type))
8261 warning (0, "type %qE has a user-defined constructor", type_name);
8262 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
8263 warning (0, "type %qE has a user-defined destructor", type_name);
8265 if (!warn_cxx_ivars)
8267 warning (0, "C++ constructors and destructors will not "
8268 "be invoked for Objective-C fields");
8269 warn_cxx_ivars = true;
8275 /* Overload the public attribute, it is not used for FIELD_DECLs. */
8278 case OBJC_IVAR_VIS_PROTECTED:
8279 TREE_PUBLIC (field_decl) = 0;
8280 TREE_PRIVATE (field_decl) = 0;
8281 TREE_PROTECTED (field_decl) = 1;
8284 case OBJC_IVAR_VIS_PACKAGE:
8285 /* TODO: Implement the package variant. */
8286 case OBJC_IVAR_VIS_PUBLIC:
8287 TREE_PUBLIC (field_decl) = 1;
8288 TREE_PRIVATE (field_decl) = 0;
8289 TREE_PROTECTED (field_decl) = 0;
8292 case OBJC_IVAR_VIS_PRIVATE:
8293 TREE_PUBLIC (field_decl) = 0;
8294 TREE_PRIVATE (field_decl) = 1;
8295 TREE_PROTECTED (field_decl) = 0;
8300 CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl);
8307 is_ivar (tree decl_chain, tree ident)
8309 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
8310 if (DECL_NAME (decl_chain) == ident)
8315 /* True if the ivar is private and we are not in its implementation. */
8318 is_private (tree decl)
8320 return (TREE_PRIVATE (decl)
8321 && ! is_ivar (CLASS_IVARS (implementation_template),
8325 /* We have an instance variable reference;, check to see if it is public. */
8328 objc_is_public (tree expr, tree identifier)
8330 tree basetype, decl;
8333 if (processing_template_decl)
8337 if (TREE_TYPE (expr) == error_mark_node)
8340 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8342 if (basetype && TREE_CODE (basetype) == RECORD_TYPE)
8344 if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype))
8346 tree klass = lookup_interface (OBJC_TYPE_NAME (basetype));
8350 error ("cannot find interface declaration for %qE",
8351 OBJC_TYPE_NAME (basetype));
8355 if ((decl = is_ivar (get_class_ivars (klass, true), identifier)))
8357 if (TREE_PUBLIC (decl))
8360 /* Important difference between the Stepstone translator:
8361 all instance variables should be public within the context
8362 of the implementation. */
8363 if (objc_implementation_context
8364 && ((TREE_CODE (objc_implementation_context)
8365 == CLASS_IMPLEMENTATION_TYPE)
8366 || (TREE_CODE (objc_implementation_context)
8367 == CATEGORY_IMPLEMENTATION_TYPE)))
8369 tree curtype = TYPE_MAIN_VARIANT
8370 (CLASS_STATIC_TEMPLATE
8371 (implementation_template));
8373 if (basetype == curtype
8374 || DERIVED_FROM_P (basetype, curtype))
8376 int priv = is_private (decl);
8379 error ("instance variable %qE is declared private",
8386 /* The 2.95.2 compiler sometimes allowed C functions to access
8387 non-@public ivars. We will let this slide for now... */
8388 if (!objc_method_context)
8390 warning (0, "instance variable %qE is %s; "
8391 "this will be a hard error in the future",
8393 TREE_PRIVATE (decl) ? "@private" : "@protected");
8397 error ("instance variable %qE is declared %s",
8399 TREE_PRIVATE (decl) ? "private" : "protected");
8408 /* Make sure all entries in CHAIN are also in LIST. */
8411 check_methods (tree chain, tree list, int mtype)
8417 /* If the method is associated with a dynamic property, then it
8418 is Ok not to have the method implementation, as it will be
8419 generated dynamically at runtime. */
8420 tree property = METHOD_PROPERTY_CONTEXT (chain);
8421 if (property != NULL_TREE && PROPERTY_DYNAMIC (property))
8423 chain = TREE_CHAIN (chain); /* next method... */
8427 if (!lookup_method (list, chain))
8431 switch (TREE_CODE (objc_implementation_context))
8433 case CLASS_IMPLEMENTATION_TYPE:
8434 warning (0, "incomplete implementation of class %qE",
8435 CLASS_NAME (objc_implementation_context));
8437 case CATEGORY_IMPLEMENTATION_TYPE:
8438 warning (0, "incomplete implementation of category %qE",
8439 CLASS_SUPER_NAME (objc_implementation_context));
8447 warning (0, "method definition for %<%c%E%> not found",
8448 mtype, METHOD_SEL_NAME (chain));
8451 chain = DECL_CHAIN (chain);
8457 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
8460 conforms_to_protocol (tree klass, tree protocol)
8462 if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE)
8464 tree p = CLASS_PROTOCOL_LIST (klass);
8465 while (p && TREE_VALUE (p) != protocol)
8470 tree super = (CLASS_SUPER_NAME (klass)
8471 ? lookup_interface (CLASS_SUPER_NAME (klass))
8473 int tmp = super ? conforms_to_protocol (super, protocol) : 0;
8482 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
8483 CONTEXT. This is one of two mechanisms to check protocol integrity. */
8486 check_methods_accessible (tree chain, tree context, int mtype)
8490 tree base_context = context;
8494 /* If the method is associated with a dynamic property, then it
8495 is Ok not to have the method implementation, as it will be
8496 generated dynamically at runtime. */
8497 tree property = METHOD_PROPERTY_CONTEXT (chain);
8498 if (property != NULL_TREE && PROPERTY_DYNAMIC (property))
8500 chain = TREE_CHAIN (chain); /* next method... */
8504 context = base_context;
8508 list = CLASS_CLS_METHODS (context);
8510 list = CLASS_NST_METHODS (context);
8512 if (lookup_method (list, chain))
8515 switch (TREE_CODE (context))
8517 case CLASS_IMPLEMENTATION_TYPE:
8518 case CLASS_INTERFACE_TYPE:
8519 context = (CLASS_SUPER_NAME (context)
8520 ? lookup_interface (CLASS_SUPER_NAME (context))
8523 case CATEGORY_IMPLEMENTATION_TYPE:
8524 case CATEGORY_INTERFACE_TYPE:
8525 context = (CLASS_NAME (context)
8526 ? lookup_interface (CLASS_NAME (context))
8534 if (context == NULL_TREE)
8538 switch (TREE_CODE (objc_implementation_context))
8540 case CLASS_IMPLEMENTATION_TYPE:
8541 warning (0, "incomplete implementation of class %qE",
8542 CLASS_NAME (objc_implementation_context));
8544 case CATEGORY_IMPLEMENTATION_TYPE:
8545 warning (0, "incomplete implementation of category %qE",
8546 CLASS_SUPER_NAME (objc_implementation_context));
8553 warning (0, "method definition for %<%c%E%> not found",
8554 mtype, METHOD_SEL_NAME (chain));
8557 chain = TREE_CHAIN (chain); /* next method... */
8562 /* Check whether the current interface (accessible via
8563 'objc_implementation_context') actually implements protocol P, along
8564 with any protocols that P inherits. */
8567 check_protocol (tree p, const char *type, tree name)
8569 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
8573 /* Ensure that all protocols have bodies! */
8576 f1 = check_methods (PROTOCOL_CLS_METHODS (p),
8577 CLASS_CLS_METHODS (objc_implementation_context),
8579 f2 = check_methods (PROTOCOL_NST_METHODS (p),
8580 CLASS_NST_METHODS (objc_implementation_context),
8585 f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
8586 objc_implementation_context,
8588 f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
8589 objc_implementation_context,
8594 warning (0, "%s %qE does not fully implement the %qE protocol",
8595 type, name, PROTOCOL_NAME (p));
8598 /* Check protocols recursively. */
8599 if (PROTOCOL_LIST (p))
8601 tree subs = PROTOCOL_LIST (p);
8603 lookup_interface (CLASS_SUPER_NAME (implementation_template));
8607 tree sub = TREE_VALUE (subs);
8609 /* If the superclass does not conform to the protocols
8610 inherited by P, then we must! */
8611 if (!super_class || !conforms_to_protocol (super_class, sub))
8612 check_protocol (sub, type, name);
8613 subs = TREE_CHAIN (subs);
8618 /* Check whether the current interface (accessible via
8619 'objc_implementation_context') actually implements the protocols listed
8623 check_protocols (tree proto_list, const char *type, tree name)
8625 for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
8627 tree p = TREE_VALUE (proto_list);
8629 check_protocol (p, type, name);
8633 /* Make sure that the class CLASS_NAME is defined
8634 CODE says which kind of thing CLASS_NAME ought to be.
8635 It can be CLASS_INTERFACE_TYPE, CLASS_IMPLEMENTATION_TYPE,
8636 CATEGORY_INTERFACE_TYPE, or CATEGORY_IMPLEMENTATION_TYPE. */
8639 start_class (enum tree_code code, tree class_name, tree super_name,
8645 if (current_namespace != global_namespace) {
8646 error ("Objective-C declarations may only appear in global scope");
8648 #endif /* OBJCPLUS */
8650 if (objc_implementation_context)
8652 warning (0, "%<@end%> missing in implementation context");
8653 finish_class (objc_implementation_context);
8654 objc_ivar_chain = NULL_TREE;
8655 objc_implementation_context = NULL_TREE;
8658 klass = make_node (code);
8659 TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS);
8661 /* Check for existence of the super class, if one was specified. Note
8662 that we must have seen an @interface, not just a @class. If we
8663 are looking at a @compatibility_alias, traverse it first. */
8664 if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE)
8667 tree super = objc_is_class_name (super_name);
8669 if (!super || !lookup_interface (super))
8671 error ("cannot find interface declaration for %qE, superclass of %qE",
8672 super ? super : super_name,
8674 super_name = NULL_TREE;
8680 CLASS_NAME (klass) = class_name;
8681 CLASS_SUPER_NAME (klass) = super_name;
8682 CLASS_CLS_METHODS (klass) = NULL_TREE;
8684 if (! objc_is_class_name (class_name)
8685 && (decl = lookup_name (class_name)))
8687 error ("%qE redeclared as different kind of symbol",
8689 error ("previous declaration of %q+D",
8695 case CLASS_IMPLEMENTATION_TYPE:
8699 for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
8700 if (TREE_VALUE (chain) == class_name)
8702 error ("reimplementation of class %qE",
8704 return error_mark_node;
8706 implemented_classes = tree_cons (NULL_TREE, class_name,
8707 implemented_classes);
8710 /* Reset for multiple classes per file. */
8713 objc_implementation_context = klass;
8715 /* Lookup the interface for this implementation. */
8717 if (!(implementation_template = lookup_interface (class_name)))
8719 warning (0, "cannot find interface declaration for %qE",
8721 add_class (implementation_template = objc_implementation_context,
8725 /* If a super class has been specified in the implementation,
8726 insure it conforms to the one specified in the interface. */
8729 && (super_name != CLASS_SUPER_NAME (implementation_template)))
8731 tree previous_name = CLASS_SUPER_NAME (implementation_template);
8732 error ("conflicting super class name %qE",
8735 error ("previous declaration of %qE", previous_name);
8737 error ("previous declaration");
8740 else if (! super_name)
8742 CLASS_SUPER_NAME (objc_implementation_context)
8743 = CLASS_SUPER_NAME (implementation_template);
8747 case CLASS_INTERFACE_TYPE:
8748 if (lookup_interface (class_name))
8750 error ("duplicate interface declaration for class %qE", class_name);
8752 warning (0, "duplicate interface declaration for class %qE", class_name);
8755 add_class (klass, class_name);
8758 CLASS_PROTOCOL_LIST (klass)
8759 = lookup_and_install_protocols (protocol_list);
8762 case CATEGORY_INTERFACE_TYPE:
8764 tree class_category_is_assoc_with;
8766 /* For a category, class_name is really the name of the class that
8767 the following set of methods will be associated with. We must
8768 find the interface so that can derive the objects template. */
8769 if (!(class_category_is_assoc_with = lookup_interface (class_name)))
8771 error ("cannot find interface declaration for %qE",
8773 exit (FATAL_EXIT_CODE);
8776 add_category (class_category_is_assoc_with, klass);
8779 CLASS_PROTOCOL_LIST (klass)
8780 = lookup_and_install_protocols (protocol_list);
8784 case CATEGORY_IMPLEMENTATION_TYPE:
8785 /* Reset for multiple classes per file. */
8788 objc_implementation_context = klass;
8790 /* For a category, class_name is really the name of the class that
8791 the following set of methods will be associated with. We must
8792 find the interface so that can derive the objects template. */
8794 if (!(implementation_template = lookup_interface (class_name)))
8796 error ("cannot find interface declaration for %qE",
8798 exit (FATAL_EXIT_CODE);
8808 continue_class (tree klass)
8810 switch (TREE_CODE (klass))
8812 case CLASS_IMPLEMENTATION_TYPE:
8813 case CATEGORY_IMPLEMENTATION_TYPE:
8815 struct imp_entry *imp_entry;
8817 /* Check consistency of the instance variables. */
8819 if (CLASS_RAW_IVARS (klass))
8820 check_ivars (implementation_template, klass);
8822 /* code generation */
8824 push_lang_context (lang_name_c);
8826 build_private_template (implementation_template);
8827 uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template);
8828 objc_instance_type = build_pointer_type (uprivate_record);
8830 imp_entry = ggc_alloc_imp_entry ();
8832 imp_entry->next = imp_list;
8833 imp_entry->imp_context = klass;
8834 imp_entry->imp_template = implementation_template;
8836 synth_forward_declarations ();
8837 imp_entry->class_decl = UOBJC_CLASS_decl;
8838 imp_entry->meta_decl = UOBJC_METACLASS_decl;
8839 imp_entry->has_cxx_cdtors = 0;
8841 /* Append to front and increment count. */
8842 imp_list = imp_entry;
8843 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
8848 pop_lang_context ();
8849 #endif /* OBJCPLUS */
8851 return get_class_ivars (implementation_template, true);
8854 case CLASS_INTERFACE_TYPE:
8857 push_lang_context (lang_name_c);
8858 #endif /* OBJCPLUS */
8859 objc_collecting_ivars = 1;
8860 build_private_template (klass);
8861 objc_collecting_ivars = 0;
8863 pop_lang_context ();
8864 #endif /* OBJCPLUS */
8869 return error_mark_node;
8873 /* This routine builds name of the setter synthesized function. */
8875 objc_build_property_setter_name (tree ident)
8877 /* TODO: Use alloca to allocate buffer of appropriate size. */
8878 static char string[BUFSIZE];
8879 sprintf (string, "set%s:", IDENTIFIER_POINTER (ident));
8880 string[3] = TOUPPER (string[3]);
8884 /* This routine prepares the declarations of the property accessor
8885 helper functions (objc_getProperty(), etc) that are used when
8886 @synthesize is used. */
8888 build_objc_property_accessor_helpers (void)
8892 /* Declare the following function:
8894 objc_getProperty (id self, SEL _cmd,
8895 ptrdiff_t offset, BOOL is_atomic); */
8896 type = build_function_type_list (objc_object_type,
8902 objc_getProperty_decl = add_builtin_function ("objc_getProperty",
8903 type, 0, NOT_BUILT_IN,
8905 TREE_NOTHROW (objc_getProperty_decl) = 0;
8907 /* Declare the following function:
8909 objc_setProperty (id self, SEL _cmd,
8910 ptrdiff_t offset, id new_value,
8911 BOOL is_atomic, BOOL should_copy); */
8912 type = build_function_type_list (void_type_node,
8920 objc_setProperty_decl = add_builtin_function ("objc_setProperty",
8921 type, 0, NOT_BUILT_IN,
8923 TREE_NOTHROW (objc_setProperty_decl) = 0;
8925 /* This is the type of all of the following functions
8926 (objc_copyStruct(), objc_getPropertyStruct() and
8927 objc_setPropertyStruct()). */
8928 type = build_function_type_list (void_type_node,
8930 const_ptr_type_node,
8936 if (flag_next_runtime)
8938 /* Declare the following function:
8940 objc_copyStruct (void *destination, const void *source,
8941 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
8942 objc_copyStruct_decl = add_builtin_function ("objc_copyStruct",
8943 type, 0, NOT_BUILT_IN,
8945 TREE_NOTHROW (objc_copyStruct_decl) = 0;
8946 objc_getPropertyStruct_decl = NULL_TREE;
8947 objc_setPropertyStruct_decl = NULL_TREE;
8951 objc_copyStruct_decl = NULL_TREE;
8953 /* Declare the following function:
8955 objc_getPropertyStruct (void *destination, const void *source,
8956 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
8957 objc_getPropertyStruct_decl = add_builtin_function ("objc_getPropertyStruct",
8958 type, 0, NOT_BUILT_IN,
8960 TREE_NOTHROW (objc_getPropertyStruct_decl) = 0;
8961 /* Declare the following function:
8963 objc_setPropertyStruct (void *destination, const void *source,
8964 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
8965 objc_setPropertyStruct_decl = add_builtin_function ("objc_setPropertyStruct",
8966 type, 0, NOT_BUILT_IN,
8968 TREE_NOTHROW (objc_setPropertyStruct_decl) = 0;
8972 /* This looks up an ivar in a class (including superclasses). */
8974 lookup_ivar (tree interface, tree instance_variable_name)
8980 for (decl_chain = CLASS_IVARS (interface); decl_chain; decl_chain = DECL_CHAIN (decl_chain))
8981 if (DECL_NAME (decl_chain) == instance_variable_name)
8984 /* Not found. Search superclass if any. */
8985 if (CLASS_SUPER_NAME (interface))
8986 interface = lookup_interface (CLASS_SUPER_NAME (interface));
8992 /* This routine synthesizes a 'getter' method. This is only called
8993 for @synthesize properties. */
8995 objc_synthesize_getter (tree klass, tree class_method, tree property)
8997 location_t location = DECL_SOURCE_LOCATION (property);
9002 /* If user has implemented a getter with same name then do nothing. */
9003 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
9004 PROPERTY_GETTER_NAME (property)))
9007 /* Find declaration of the property getter in the interface. There
9008 must be one. TODO: Search superclasses as well. */
9009 decl = lookup_method (CLASS_NST_METHODS (class_method), PROPERTY_GETTER_NAME (property));
9011 /* If one not declared in the interface, this condition has already
9012 been reported as user error (because property was not declared in
9017 /* Adapt the 'decl'. Use the source location of the @synthesize
9018 statement for error messages. */
9019 decl = copy_node (decl);
9020 DECL_SOURCE_LOCATION (decl) = location;
9022 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE);
9023 body = c_begin_compound_stmt (true);
9025 /* Now we need to decide how we build the getter. There are three
9028 for 'copy' or 'retain' properties we need to use the
9029 objc_getProperty() accessor helper which knows about retain and
9030 copy. It supports both 'nonatomic' and 'atomic' access.
9032 for 'nonatomic, assign' properties we can access the instance
9033 variable directly. 'nonatomic' means we don't have to use locks,
9034 and 'assign' means we don't have to worry about retain or copy.
9035 If you combine the two, it means we can just access the instance
9038 for 'atomic, assign' properties we use objc_copyStruct() (for the
9039 next runtime) or objc_getPropertyStruct() (for the GNU runtime). */
9040 switch (PROPERTY_ASSIGN_SEMANTICS (property))
9042 case OBJC_PROPERTY_RETAIN:
9043 case OBJC_PROPERTY_COPY:
9045 /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */
9046 tree cmd, ivar, offset, is_atomic;
9047 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
9049 /* Find the ivar to compute the offset. */
9050 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
9051 if (!ivar || is_private (ivar))
9053 /* This should never happen. */
9055 "can not find instance variable associated with property");
9056 ret_val = error_mark_node;
9059 offset = byte_position (ivar);
9061 if (PROPERTY_NONATOMIC (property))
9062 is_atomic = boolean_false_node;
9064 is_atomic = boolean_true_node;
9066 ret_val = build_function_call
9068 /* Function prototype. */
9069 objc_getProperty_decl,
9071 tree_cons /* self */
9072 (NULL_TREE, self_decl,
9073 tree_cons /* _cmd */
9075 tree_cons /* offset */
9077 tree_cons /* is_atomic */
9078 (NULL_TREE, is_atomic, NULL_TREE)))));
9081 case OBJC_PROPERTY_ASSIGN:
9082 if (PROPERTY_NONATOMIC (property))
9084 /* We build "return self->PROPERTY_IVAR_NAME;" */
9085 ret_val = objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property));
9091 <property type> __objc_property_temp;
9092 objc_getPropertyStruct (&__objc_property_temp,
9093 &(self->PROPERTY_IVAR_NAME),
9094 sizeof (type of self->PROPERTY_IVAR_NAME),
9097 return __objc_property_temp;
9099 For the NeXT runtime, we need to use objc_copyStruct
9100 instead of objc_getPropertyStruct. */
9101 tree objc_property_temp_decl, function_decl, function_call;
9102 tree size_of, is_atomic;
9104 objc_property_temp_decl = objc_create_temporary_var (TREE_TYPE (property), "__objc_property_temp");
9105 DECL_SOURCE_LOCATION (objc_property_temp_decl) = location;
9106 objc_property_temp_decl = lang_hooks.decls.pushdecl (objc_property_temp_decl);
9108 /* sizeof (ivar type). Since the ivar and the property have
9109 the same type, there is no need to lookup the ivar. */
9110 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
9111 true /* is_sizeof */,
9112 false /* complain */);
9114 if (PROPERTY_NONATOMIC (property))
9115 is_atomic = boolean_false_node;
9117 is_atomic = boolean_true_node;
9119 if (flag_next_runtime)
9120 function_decl = objc_copyStruct_decl;
9122 function_decl = objc_getPropertyStruct_decl;
9124 function_call = build_function_call
9126 /* Function prototype. */
9129 tree_cons /* &__objc_property_temp_decl */
9130 /* Warning: note that using build_fold_addr_expr_loc()
9131 here causes invalid code to be generated. */
9132 (NULL_TREE, build_unary_op (location, ADDR_EXPR, objc_property_temp_decl, 0),
9133 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
9134 (NULL_TREE, build_fold_addr_expr_loc (location,
9136 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
9137 tree_cons /* sizeof (PROPERTY_IVAR) */
9138 (NULL_TREE, size_of,
9139 tree_cons /* is_atomic */
9140 (NULL_TREE, is_atomic,
9141 /* TODO: This is currently ignored by the GNU
9142 runtime, but what about the next one ? */
9143 tree_cons /* has_strong */
9144 (NULL_TREE, boolean_true_node, NULL_TREE))))));
9146 add_stmt (function_call);
9148 ret_val = objc_property_temp_decl;
9155 gcc_assert (ret_val);
9158 finish_return_stmt (ret_val);
9160 c_finish_return (location, ret_val, NULL_TREE);
9163 add_stmt (c_end_compound_stmt (location, body, true));
9164 fn = current_function_decl;
9168 objc_finish_method_definition (fn);
9171 /* This routine synthesizes a 'setter' method. */
9174 objc_synthesize_setter (tree klass ATTRIBUTE_UNUSED, tree class_method, tree property)
9176 location_t location = DECL_SOURCE_LOCATION (property);
9179 tree new_value, statement;
9181 /* If user has implemented a setter with same name then do nothing. */
9182 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
9183 PROPERTY_SETTER_NAME (property)))
9186 /* Find declaration of the property setter in the interface. There
9187 must be one. TODO: Search superclasses as well. */
9188 decl = lookup_method (CLASS_NST_METHODS (class_method), PROPERTY_SETTER_NAME (property));
9190 /* If one not declared in the interface, this condition has already
9191 been reported as user error (because property was not declared in
9196 /* Adapt the 'decl'. Use the source location of the @synthesize
9197 statement for error messages. */
9198 decl = copy_node (decl);
9199 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (property);
9201 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE);
9203 body = c_begin_compound_stmt (true);
9205 /* The 'new_value' is the only argument to the method, which is the
9206 3rd argument of the function, after self and _cmd. We use twice
9207 TREE_CHAIN to move forward two arguments. */
9208 new_value = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)));
9210 /* This would presumably happen if the user has specified a
9211 prototype for the setter that does not have an argument! */
9212 if (new_value == NULL_TREE)
9214 /* TODO: This should be caught much earlier than this. */
9215 error_at (DECL_SOURCE_LOCATION (decl), "invalid setter, it must have one argument");
9216 /* Try to recover somehow. */
9217 new_value = error_mark_node;
9220 /* Now we need to decide how we build the setter. There are three
9223 for 'copy' or 'retain' properties we need to use the
9224 objc_setProperty() accessor helper which knows about retain and
9225 copy. It supports both 'nonatomic' and 'atomic' access.
9227 for 'nonatomic, assign' properties we can access the instance
9228 variable directly. 'nonatomic' means we don't have to use locks,
9229 and 'assign' means we don't have to worry about retain or copy.
9230 If you combine the two, it means we can just access the instance
9233 for 'atomic, assign' properties we use objc_copyStruct() (for the
9234 next runtime) or objc_setPropertyStruct() (for the GNU runtime). */
9235 switch (PROPERTY_ASSIGN_SEMANTICS (property))
9237 case OBJC_PROPERTY_RETAIN:
9238 case OBJC_PROPERTY_COPY:
9240 /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */
9241 tree cmd, ivar, offset, is_atomic, should_copy;
9242 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
9244 /* Find the ivar to compute the offset. */
9245 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
9246 if (!ivar || is_private (ivar))
9249 "can not find instance variable associated with property");
9250 statement = error_mark_node;
9253 offset = byte_position (ivar);
9255 if (PROPERTY_NONATOMIC (property))
9256 is_atomic = boolean_false_node;
9258 is_atomic = boolean_true_node;
9260 if (PROPERTY_ASSIGN_SEMANTICS (property) == OBJC_PROPERTY_COPY)
9261 should_copy = boolean_true_node;
9263 should_copy = boolean_false_node;
9265 statement = build_function_call
9267 /* Function prototype. */
9268 objc_setProperty_decl,
9270 tree_cons /* self */
9271 (NULL_TREE, self_decl,
9272 tree_cons /* _cmd */
9274 tree_cons /* offset */
9276 tree_cons /* new_value */
9277 (NULL_TREE, new_value,
9278 tree_cons /* is_atomic */
9279 (NULL_TREE, is_atomic,
9280 tree_cons /* should_copy */
9281 (NULL_TREE, should_copy, NULL_TREE)))))));
9284 case OBJC_PROPERTY_ASSIGN:
9285 if (PROPERTY_NONATOMIC (property))
9287 /* We build "self->PROPERTY_IVAR_NAME = new_value;" */
9288 statement = build_modify_expr
9290 objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)),
9291 NULL_TREE, NOP_EXPR,
9292 location, new_value, NULL_TREE);
9298 objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME),
9300 sizeof (type of self->PROPERTY_IVAR_NAME),
9304 For the NeXT runtime, we need to use objc_copyStruct
9305 instead of objc_getPropertyStruct. */
9306 tree function_decl, size_of, is_atomic;
9308 /* sizeof (ivar type). Since the ivar and the property have
9309 the same type, there is no need to lookup the ivar. */
9310 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
9311 true /* is_sizeof */,
9312 false /* complain */);
9314 if (PROPERTY_NONATOMIC (property))
9315 is_atomic = boolean_false_node;
9317 is_atomic = boolean_true_node;
9319 if (flag_next_runtime)
9320 function_decl = objc_copyStruct_decl;
9322 function_decl = objc_setPropertyStruct_decl;
9324 statement = build_function_call
9326 /* Function prototype. */
9329 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
9330 (NULL_TREE, build_fold_addr_expr_loc (location,
9332 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
9333 tree_cons /* &new_value */
9334 (NULL_TREE, build_fold_addr_expr_loc (location, new_value),
9335 tree_cons /* sizeof (PROPERTY_IVAR) */
9336 (NULL_TREE, size_of,
9337 tree_cons /* is_atomic */
9338 (NULL_TREE, is_atomic,
9339 /* TODO: This is currently ignored by the GNU
9340 runtime, but what about the next one ? */
9341 tree_cons /* has_strong */
9342 (NULL_TREE, boolean_true_node, NULL_TREE))))));
9348 gcc_assert (statement);
9350 add_stmt (statement);
9351 add_stmt (c_end_compound_stmt (location, body, true));
9352 fn = current_function_decl;
9356 objc_finish_method_definition (fn);
9359 /* This function is a sub-routine of objc_add_synthesize_declaration.
9360 It is called for each property to synthesize once we have
9361 determined that the context is Ok. */
9363 objc_add_synthesize_declaration_for_property (location_t location, tree interface,
9364 tree property_name, tree ivar_name)
9366 /* Find the @property declaration. */
9369 /* Check that synthesize or dynamic has not already been used for
9370 the same property. */
9371 for (property = CLASS_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
9372 if (PROPERTY_NAME (property) == property_name)
9374 location_t original_location = DECL_SOURCE_LOCATION (property);
9376 if (PROPERTY_DYNAMIC (property))
9377 error_at (location, "property %qs already specified in %<@dynamic%>",
9378 IDENTIFIER_POINTER (property_name));
9380 error_at (location, "property %qs already specified in %<@synthesize%>",
9381 IDENTIFIER_POINTER (property_name));
9383 if (original_location != UNKNOWN_LOCATION)
9384 inform (original_location, "originally specified here");
9388 /* Check that the property is declared in the interface. */
9389 /* TODO: This only check the immediate class; we need to check the
9390 superclass (and categories ?) as well. */
9391 for (property = CLASS_PROPERTY_DECL (interface); property; property = TREE_CHAIN (property))
9392 if (PROPERTY_NAME (property) == property_name)
9397 error_at (location, "no declaration of property %qs found in the interface",
9398 IDENTIFIER_POINTER (property_name));
9403 /* We have to copy the property, because we want to chain it to
9404 the implementation context, and we want to store the source
9405 location of the @synthesize, not of the original
9407 property = copy_node (property);
9408 DECL_SOURCE_LOCATION (property) = location;
9411 /* Determine PROPERTY_IVAR_NAME. */
9412 if (ivar_name == NULL_TREE)
9413 ivar_name = property_name;
9415 /* Check that the instance variable exists. You can only use an
9416 instance variable from the same class, not one from the
9418 if (!is_ivar (CLASS_IVARS (interface), ivar_name))
9419 error_at (location, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar",
9420 IDENTIFIER_POINTER (property_name));
9422 /* TODO: Check that the types of the instance variable and of the
9425 /* TODO: Check that no other property is using the same instance
9428 /* Note that a @synthesize (and only a @synthesize) always sets
9429 PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a
9430 @synthesize by that. */
9431 PROPERTY_IVAR_NAME (property) = ivar_name;
9433 /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the
9434 original declaration; they are always set (with the exception of
9435 PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */
9437 /* Add the property to the list of properties for current implementation. */
9438 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
9439 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
9441 /* Note how we don't actually synthesize the getter/setter here; it
9442 would be very natural, but we may miss the fact that the user has
9443 implemented his own getter/setter later on in the @implementation
9444 (in which case we shouldn't generate getter/setter). We wait
9445 until we have parsed it all before generating the code. */
9448 /* This function is called by the parser after a @synthesize
9449 expression is parsed. 'location' is the location of the
9450 @synthesize expression, and 'property_and_ivar_list' is a chained
9451 list of the property and ivar names. */
9453 objc_add_synthesize_declaration (location_t location, tree property_and_ivar_list)
9455 tree interface, chain;
9457 if (flag_objc1_only)
9458 error_at (input_location, "%<@synthesize%> is not available in Objective-C 1.0");
9460 if (property_and_ivar_list == error_mark_node)
9463 if (!objc_implementation_context)
9465 /* We can get here only in Objective-C; the Objective-C++ parser
9466 detects the problem while parsing, outputs the error
9467 "misplaced '@synthesize' Objective-C++ construct" and skips
9469 error_at (location, "%<@synthesize%> not in @implementation context");
9473 if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE)
9475 error_at (location, "%<@synthesize%> can not be used in categories");
9479 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
9482 /* I can't see how this could happen, but it is good as a safety check. */
9484 "%<@synthesize%> requires the @interface of the class to be available");
9488 /* Now, iterate over the properties and do each of them. */
9489 for (chain = property_and_ivar_list; chain; chain = TREE_CHAIN (chain))
9491 objc_add_synthesize_declaration_for_property (location, interface, TREE_VALUE (chain),
9492 TREE_PURPOSE (chain));
9496 /* This function is a sub-routine of objc_add_dynamic_declaration. It
9497 is called for each property to mark as dynamic once we have
9498 determined that the context is Ok. */
9500 objc_add_dynamic_declaration_for_property (location_t location, tree interface,
9503 /* Find the @property declaration. */
9506 /* Check that synthesize or dynamic has not already been used for
9507 the same property. */
9508 for (property = CLASS_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
9509 if (PROPERTY_NAME (property) == property_name)
9511 location_t original_location = DECL_SOURCE_LOCATION (property);
9513 if (PROPERTY_DYNAMIC (property))
9514 error_at (location, "property %qs already specified in %<@dynamic%>",
9515 IDENTIFIER_POINTER (property_name));
9517 error_at (location, "property %qs already specified in %<@synthesize%>",
9518 IDENTIFIER_POINTER (property_name));
9520 if (original_location != UNKNOWN_LOCATION)
9521 inform (original_location, "originally specified here");
9525 /* Check that the property is declared in the corresponding
9527 for (property = CLASS_PROPERTY_DECL (interface); property; property = TREE_CHAIN (property))
9528 if (PROPERTY_NAME (property) == property_name)
9533 error_at (location, "no declaration of property %qs found in the interface",
9534 IDENTIFIER_POINTER (property_name));
9539 /* Mark the original PROPERTY_DECL as dynamic. The reason is
9540 that the setter and getter methods in the interface have a
9541 METHOD_PROPERTY_CONTEXT that points to the original
9542 PROPERTY_DECL; when we check that these methods have been
9543 implemented, we need to easily find that they are associated
9544 with a dynamic property. TODO: Clean this up; maybe the
9545 @property PROPERTY_DECL should contain a reference to the
9546 @dynamic PROPERTY_DECL ? */
9547 PROPERTY_DYNAMIC (property) = 1;
9549 /* We have to copy the property, because we want to chain it to
9550 the implementation context, and we want to store the source
9551 location of the @synthesize, not of the original
9553 property = copy_node (property);
9554 DECL_SOURCE_LOCATION (property) = location;
9557 /* Note that a @dynamic (and only a @dynamic) always sets
9558 PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that.
9559 (actually, as explained above, PROPERTY_DECL generated by
9560 @property and associated with a @dynamic property are also marked
9561 as PROPERTY_DYNAMIC). */
9562 PROPERTY_DYNAMIC (property) = 1;
9564 /* Add the property to the list of properties for current implementation. */
9565 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
9566 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
9569 /* This function is called by the parser after a @dynamic expression
9570 is parsed. 'location' is the location of the @dynamic expression,
9571 and 'property_list' is a chained list of all the property
9574 objc_add_dynamic_declaration (location_t location, tree property_list)
9576 tree interface, chain;
9578 if (flag_objc1_only)
9579 error_at (input_location, "%<@dynamic%> is not available in Objective-C 1.0");
9581 if (property_list == error_mark_node)
9584 if (!objc_implementation_context)
9586 /* We can get here only in Objective-C; the Objective-C++ parser
9587 detects the problem while parsing, outputs the error
9588 "misplaced '@dynamic' Objective-C++ construct" and skips the
9590 error_at (location, "%<@dynamic%> not in @implementation context");
9594 /* @dynamic is allowed in categories. */
9595 switch (TREE_CODE (objc_implementation_context))
9597 case CLASS_IMPLEMENTATION_TYPE:
9598 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
9600 case CATEGORY_IMPLEMENTATION_TYPE:
9601 interface = lookup_category (implementation_template,
9602 CLASS_SUPER_NAME (objc_implementation_context));
9610 /* I can't see how this could happen, but it is good as a safety check. */
9612 "%<@dynamic%> requires the @interface of the class to be available");
9616 /* Now, iterate over the properties and do each of them. */
9617 for (chain = property_list; chain; chain = TREE_CHAIN (chain))
9619 objc_add_dynamic_declaration_for_property (location, interface, TREE_VALUE (chain));
9623 /* Main routine to generate code/data for all the property information for
9624 current implementation (class or category). CLASS is the interface where
9625 ivars are declared. CLASS_METHODS is where methods are found which
9626 could be a class or a category depending on whether we are implementing
9627 property of a class or a category. */
9630 objc_gen_property_data (tree klass, tree class_methods)
9634 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
9636 /* @dynamic property - nothing to check or synthesize. */
9637 if (PROPERTY_DYNAMIC (x))
9640 /* @synthesize property - need to synthesize the accessors. */
9641 if (PROPERTY_IVAR_NAME (x))
9643 objc_synthesize_getter (klass, class_methods, x);
9645 if (PROPERTY_READONLY (x) == 0)
9646 objc_synthesize_setter (klass, class_methods, x);
9655 /* This is called once we see the "@end" in an interface/implementation. */
9658 finish_class (tree klass)
9660 switch (TREE_CODE (klass))
9662 case CLASS_IMPLEMENTATION_TYPE:
9664 /* All code generation is done in finish_objc. */
9666 /* Generate what needed for property; setters, getters, etc. */
9667 objc_gen_property_data (implementation_template, implementation_template);
9669 if (implementation_template != objc_implementation_context)
9671 /* Ensure that all method listed in the interface contain bodies. */
9672 check_methods (CLASS_CLS_METHODS (implementation_template),
9673 CLASS_CLS_METHODS (objc_implementation_context), '+');
9674 check_methods (CLASS_NST_METHODS (implementation_template),
9675 CLASS_NST_METHODS (objc_implementation_context), '-');
9677 if (CLASS_PROTOCOL_LIST (implementation_template))
9678 check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
9680 CLASS_NAME (objc_implementation_context));
9684 case CATEGORY_IMPLEMENTATION_TYPE:
9686 tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass));
9690 /* Generate what needed for property; setters, getters, etc. */
9691 objc_gen_property_data (implementation_template, category);
9693 /* Ensure all method listed in the interface contain bodies. */
9694 check_methods (CLASS_CLS_METHODS (category),
9695 CLASS_CLS_METHODS (objc_implementation_context), '+');
9696 check_methods (CLASS_NST_METHODS (category),
9697 CLASS_NST_METHODS (objc_implementation_context), '-');
9699 if (CLASS_PROTOCOL_LIST (category))
9700 check_protocols (CLASS_PROTOCOL_LIST (category),
9702 CLASS_SUPER_NAME (objc_implementation_context));
9706 case CLASS_INTERFACE_TYPE:
9707 case CATEGORY_INTERFACE_TYPE:
9708 case PROTOCOL_INTERFACE_TYPE:
9710 /* Process properties of the class. */
9712 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
9714 /* Store the getter name that we used into the property.
9715 It is used to generate the right getter calls;
9716 moreover, when a @synthesize is processed, it copies
9717 everything from the property, including the
9718 PROPERTY_GETTER_NAME. We want to be sure that
9719 @synthesize will get exactly the right
9720 PROPERTY_GETTER_NAME. */
9721 if (PROPERTY_GETTER_NAME (x) == NULL_TREE)
9722 PROPERTY_GETTER_NAME (x) = PROPERTY_NAME (x);
9724 /* Now we check that the appropriate getter is declared,
9725 and if not, we declare one ourselves. */
9727 tree getter_decl = lookup_method (CLASS_NST_METHODS (klass),
9728 PROPERTY_GETTER_NAME (x));
9732 /* TODO: Check that the declaration is consistent with the property. */
9737 /* Generate an instance method declaration for the
9738 getter; for example "- (id) name;". In general
9739 it will be of the form
9740 -(type)property_getter_name; */
9741 tree rettype = build_tree_list (NULL_TREE, TREE_TYPE (x));
9742 getter_decl = build_method_decl (INSTANCE_METHOD_DECL,
9743 rettype, PROPERTY_GETTER_NAME (x),
9745 objc_add_method (objc_interface_context, getter_decl, false, false);
9746 METHOD_PROPERTY_CONTEXT (getter_decl) = x;
9750 if (PROPERTY_READONLY (x) == 0)
9752 /* Store the setter name that we used into the
9753 property. It is used when generating setter calls;
9754 moreover, when a @synthesize is processed, it
9755 copies everything from the property, including the
9756 PROPERTY_SETTER_NAME. We want to be sure that
9757 @synthesize will get exactly the right
9758 PROPERTY_SETTER_NAME. */
9759 if (PROPERTY_SETTER_NAME (x) == NULL_TREE)
9760 PROPERTY_SETTER_NAME (x) = get_identifier (objc_build_property_setter_name
9761 (PROPERTY_NAME (x)));
9763 /* Now we check that the appropriate setter is declared,
9764 and if not, we declare on ourselves. */
9766 tree setter_decl = lookup_method (CLASS_NST_METHODS (klass),
9767 PROPERTY_SETTER_NAME (x));
9771 /* TODO: Check that the declaration is consistent with the property. */
9776 /* The setter name is something like 'setName:'.
9777 We need the substring 'setName' to build the
9778 method declaration due to how the declaration
9779 works. TODO: build_method_decl() will then
9780 generate back 'setName:' from 'setName'; it
9781 would be more efficient to hook into
9783 const char *full_setter_name = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x));
9784 size_t length = strlen (full_setter_name);
9785 char *setter_name = (char *) alloca (length);
9786 tree ret_type, selector, arg_type, arg_name;
9788 strcpy (setter_name, full_setter_name);
9789 setter_name[length - 1] = '\0';
9790 ret_type = build_tree_list (NULL_TREE, void_type_node);
9791 arg_type = build_tree_list (NULL_TREE, TREE_TYPE (x));
9792 arg_name = get_identifier ("_value");
9793 selector = objc_build_keyword_decl (get_identifier (setter_name),
9794 arg_type, arg_name, NULL);
9795 setter_decl = build_method_decl (INSTANCE_METHOD_DECL,
9797 build_tree_list (NULL_TREE, NULL_TREE),
9799 objc_add_method (objc_interface_context, setter_decl, false, false);
9800 METHOD_PROPERTY_CONTEXT (setter_decl) = x;
9804 /* Note how at this point (once an @interface or @protocol
9805 have been processed), PROPERTY_GETTER_NAME is always
9806 set for all PROPERTY_DECLs, and PROPERTY_SETTER_NAME is
9807 always set for all PROPERTY_DECLs where
9808 PROPERTY_READONLY == 0. Any time we deal with a getter
9809 or setter, we should get the PROPERTY_DECL and use
9810 PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know
9811 the correct names. */
9823 add_protocol (tree protocol)
9825 /* Put protocol on list in reverse order. */
9826 TREE_CHAIN (protocol) = protocol_chain;
9827 protocol_chain = protocol;
9828 return protocol_chain;
9832 lookup_protocol (tree ident)
9836 for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
9837 if (ident == PROTOCOL_NAME (chain))
9843 /* This function forward declares the protocols named by NAMES. If
9844 they are already declared or defined, the function has no effect. */
9847 objc_declare_protocols (tree names)
9852 if (current_namespace != global_namespace) {
9853 error ("Objective-C declarations may only appear in global scope");
9855 #endif /* OBJCPLUS */
9857 for (list = names; list; list = TREE_CHAIN (list))
9859 tree name = TREE_VALUE (list);
9861 if (lookup_protocol (name) == NULL_TREE)
9863 tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
9865 TYPE_LANG_SLOT_1 (protocol)
9866 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
9867 PROTOCOL_NAME (protocol) = name;
9868 PROTOCOL_LIST (protocol) = NULL_TREE;
9869 add_protocol (protocol);
9870 PROTOCOL_DEFINED (protocol) = 0;
9871 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
9877 start_protocol (enum tree_code code, tree name, tree list)
9882 if (current_namespace != global_namespace) {
9883 error ("Objective-C declarations may only appear in global scope");
9885 #endif /* OBJCPLUS */
9887 protocol = lookup_protocol (name);
9891 protocol = make_node (code);
9892 TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
9894 PROTOCOL_NAME (protocol) = name;
9895 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list);
9896 add_protocol (protocol);
9897 PROTOCOL_DEFINED (protocol) = 1;
9898 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
9900 check_protocol_recursively (protocol, list);
9902 else if (! PROTOCOL_DEFINED (protocol))
9904 PROTOCOL_DEFINED (protocol) = 1;
9905 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list);
9907 check_protocol_recursively (protocol, list);
9911 warning (0, "duplicate declaration for protocol %qE",
9918 /* "Encode" a data type into a string, which grows in util_obstack.
9920 The format is described in gcc/doc/objc.texi, section 'Type
9923 Most of the encode_xxx functions have a 'type' argument, which is
9924 the type to encode, and an integer 'curtype' argument, which is the
9925 index in the encoding string of the beginning of the encoding of
9926 the current type, and allows you to find what characters have
9927 already been written for the current type (they are the ones in the
9928 current encoding string starting from 'curtype').
9930 For example, if we are encoding a method which returns 'int' and
9931 takes a 'char **' argument, then when we get to the point of
9932 encoding the 'char **' argument, the encoded string already
9933 contains 'i12@0:4' (assuming a pointer size of 4 bytes). So,
9934 'curtype' will be set to 7 when starting to encode 'char **'.
9935 During the whole of the encoding of 'char **', 'curtype' will be
9936 fixed at 7, so the routine encoding the second pointer can find out
9937 that it's actually encoding a pointer to a pointer by looking
9938 backwards at what has already been encoded for the current type,
9939 and seeing there is a "^" (meaning a pointer) in there.
9943 /* Encode type qualifiers encodes one of the "PQ" Objective-C
9944 keywords, ie 'in', 'out', 'inout', 'bycopy', 'byref', 'oneway'.
9945 'const', instead, is encoded directly as part of the type.
9949 encode_type_qualifiers (tree declspecs)
9953 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
9955 /* FIXME: Shouldn't we use token->keyword here ? */
9956 if (ridpointers[(int) RID_IN] == TREE_VALUE (spec))
9957 obstack_1grow (&util_obstack, 'n');
9958 else if (ridpointers[(int) RID_INOUT] == TREE_VALUE (spec))
9959 obstack_1grow (&util_obstack, 'N');
9960 else if (ridpointers[(int) RID_OUT] == TREE_VALUE (spec))
9961 obstack_1grow (&util_obstack, 'o');
9962 else if (ridpointers[(int) RID_BYCOPY] == TREE_VALUE (spec))
9963 obstack_1grow (&util_obstack, 'O');
9964 else if (ridpointers[(int) RID_BYREF] == TREE_VALUE (spec))
9965 obstack_1grow (&util_obstack, 'R');
9966 else if (ridpointers[(int) RID_ONEWAY] == TREE_VALUE (spec))
9967 obstack_1grow (&util_obstack, 'V');
9973 /* Determine if a pointee is marked read-only. Only used by the NeXT
9974 runtime to be compatible with gcc-3.3. */
9977 pointee_is_readonly (tree pointee)
9979 while (POINTER_TYPE_P (pointee))
9980 pointee = TREE_TYPE (pointee);
9982 return TYPE_READONLY (pointee);
9985 /* Encode a pointer type. */
9988 encode_pointer (tree type, int curtype, int format)
9990 tree pointer_to = TREE_TYPE (type);
9992 if (flag_next_runtime)
9994 /* This code is used to be compatible with gcc-3.3. */
9995 /* For historical/compatibility reasons, the read-only qualifier
9996 of the pointee gets emitted _before_ the '^'. The read-only
9997 qualifier of the pointer itself gets ignored, _unless_ we are
9998 looking at a typedef! Also, do not emit the 'r' for anything
9999 but the outermost type! */
10000 if (!generating_instance_variables
10001 && (obstack_object_size (&util_obstack) - curtype <= 1)
10002 && (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10003 ? TYPE_READONLY (type)
10004 : pointee_is_readonly (pointer_to)))
10005 obstack_1grow (&util_obstack, 'r');
10008 if (TREE_CODE (pointer_to) == RECORD_TYPE)
10010 if (OBJC_TYPE_NAME (pointer_to)
10011 && TREE_CODE (OBJC_TYPE_NAME (pointer_to)) == IDENTIFIER_NODE)
10013 const char *name = IDENTIFIER_POINTER (OBJC_TYPE_NAME (pointer_to));
10015 if (strcmp (name, TAG_OBJECT) == 0) /* '@' */
10017 obstack_1grow (&util_obstack, '@');
10020 else if (TYPE_HAS_OBJC_INFO (pointer_to)
10021 && TYPE_OBJC_INTERFACE (pointer_to))
10023 if (generating_instance_variables)
10025 obstack_1grow (&util_obstack, '@');
10026 obstack_1grow (&util_obstack, '"');
10027 obstack_grow (&util_obstack, name, strlen (name));
10028 obstack_1grow (&util_obstack, '"');
10033 obstack_1grow (&util_obstack, '@');
10037 else if (strcmp (name, TAG_CLASS) == 0) /* '#' */
10039 obstack_1grow (&util_obstack, '#');
10042 else if (strcmp (name, TAG_SELECTOR) == 0) /* ':' */
10044 obstack_1grow (&util_obstack, ':');
10049 else if (TREE_CODE (pointer_to) == INTEGER_TYPE
10050 && TYPE_MODE (pointer_to) == QImode)
10052 tree pname = TREE_CODE (OBJC_TYPE_NAME (pointer_to)) == IDENTIFIER_NODE
10053 ? OBJC_TYPE_NAME (pointer_to)
10054 : DECL_NAME (OBJC_TYPE_NAME (pointer_to));
10056 /* (BOOL *) are an exception and are encoded as ^c, while all
10057 other pointers to char are encoded as *. */
10058 if (strcmp (IDENTIFIER_POINTER (pname), "BOOL"))
10060 if (!flag_next_runtime)
10062 /* The NeXT runtime adds the 'r' before getting here. */
10064 /* It appears that "r*" means "const char *" rather than
10065 "char *const". "char *const" is encoded as "*",
10066 which is identical to "char *", so the "const" is
10067 unfortunately lost. */
10068 if (TYPE_READONLY (pointer_to))
10069 obstack_1grow (&util_obstack, 'r');
10072 obstack_1grow (&util_obstack, '*');
10077 /* We have a normal pointer type that does not get special treatment. */
10078 obstack_1grow (&util_obstack, '^');
10079 encode_type (pointer_to, curtype, format);
10083 encode_array (tree type, int curtype, int format)
10085 tree an_int_cst = TYPE_SIZE (type);
10086 tree array_of = TREE_TYPE (type);
10089 if (an_int_cst == NULL)
10091 /* We are trying to encode an incomplete array. An incomplete
10092 array is forbidden as part of an instance variable. */
10093 if (generating_instance_variables)
10095 /* TODO: Detect this error earlier. */
10096 error ("instance variable has unknown size");
10100 /* So the only case in which an incomplete array could occur is
10101 if we are encoding the arguments or return value of a method.
10102 In that case, an incomplete array argument or return value
10103 (eg, -(void)display: (char[])string) is treated like a
10104 pointer because that is how the compiler does the function
10105 call. A special, more complicated case, is when the
10106 incomplete array is the last member of a struct (eg, if we
10107 are encoding "struct { unsigned long int a;double b[];}"),
10108 which is again part of a method argument/return value. In
10109 that case, we really need to communicate to the runtime that
10110 there is an incomplete array (not a pointer!) there. So, we
10111 detect that special case and encode it as a zero-length
10114 Try to detect that we are part of a struct. We do this by
10115 searching for '=' in the type encoding for the current type.
10116 NB: This hack assumes that you can't use '=' as part of a C
10120 char *enc = obstack_base (&util_obstack) + curtype;
10121 if (memchr (enc, '=',
10122 obstack_object_size (&util_obstack) - curtype) == NULL)
10124 /* We are not inside a struct. Encode the array as a
10126 encode_pointer (type, curtype, format);
10131 /* Else, we are in a struct, and we encode it as a zero-length
10133 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT)0);
10135 else if (TREE_INT_CST_LOW (TYPE_SIZE (array_of)) == 0)
10136 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT)0);
10138 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC,
10139 TREE_INT_CST_LOW (an_int_cst)
10140 / TREE_INT_CST_LOW (TYPE_SIZE (array_of)));
10142 obstack_grow (&util_obstack, buffer, strlen (buffer));
10143 encode_type (array_of, curtype, format);
10144 obstack_1grow (&util_obstack, ']');
10148 /* Encode a vector. The vector type is a GCC extension to C. */
10150 encode_vector (tree type, int curtype, int format)
10152 tree vector_of = TREE_TYPE (type);
10155 /* Vectors are like simple fixed-size arrays. */
10157 /* Output ![xx,yy,<code>] where xx is the vector_size, yy is the
10158 alignment of the vector, and <code> is the base type. Eg, int
10159 __attribute__ ((vector_size (16))) gets encoded as ![16,32,i]
10160 assuming that the alignment is 32 bytes. We include size and
10161 alignment in bytes so that the runtime does not have to have any
10162 knowledge of the actual types.
10164 sprintf (buffer, "![" HOST_WIDE_INT_PRINT_DEC ",%d",
10165 /* We want to compute the equivalent of sizeof (<vector>).
10166 Code inspired by c_sizeof_or_alignof_type. */
10167 ((TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type))
10168 / (TYPE_PRECISION (char_type_node) / BITS_PER_UNIT))),
10169 /* We want to compute the equivalent of __alignof__
10170 (<vector>). Code inspired by
10171 c_sizeof_or_alignof_type. */
10172 TYPE_ALIGN_UNIT (type));
10173 obstack_grow (&util_obstack, buffer, strlen (buffer));
10174 encode_type (vector_of, curtype, format);
10175 obstack_1grow (&util_obstack, ']');
10180 encode_aggregate_fields (tree type, bool pointed_to, int curtype, int format)
10182 tree field = TYPE_FIELDS (type);
10184 for (; field; field = DECL_CHAIN (field))
10187 /* C++ static members, and things that are not field at all,
10188 should not appear in the encoding. */
10189 if (TREE_CODE (field) != FIELD_DECL || TREE_STATIC (field))
10193 /* Recursively encode fields of embedded base classes. */
10194 if (DECL_ARTIFICIAL (field) && !DECL_NAME (field)
10195 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
10197 encode_aggregate_fields (TREE_TYPE (field),
10198 pointed_to, curtype, format);
10202 if (generating_instance_variables && !pointed_to)
10204 tree fname = DECL_NAME (field);
10206 obstack_1grow (&util_obstack, '"');
10208 if (fname && TREE_CODE (fname) == IDENTIFIER_NODE)
10209 obstack_grow (&util_obstack,
10210 IDENTIFIER_POINTER (fname),
10211 strlen (IDENTIFIER_POINTER (fname)));
10213 obstack_1grow (&util_obstack, '"');
10216 encode_field_decl (field, curtype, format);
10221 encode_aggregate_within (tree type, int curtype, int format, int left,
10225 /* NB: aggregates that are pointed to have slightly different encoding
10226 rules in that you never encode the names of instance variables. */
10227 int ob_size = obstack_object_size (&util_obstack);
10228 bool inline_contents = false;
10229 bool pointed_to = false;
10231 if (flag_next_runtime)
10233 if (ob_size > 0 && *(obstack_next_free (&util_obstack) - 1) == '^')
10236 if ((format == OBJC_ENCODE_INLINE_DEFS || generating_instance_variables)
10237 && (!pointed_to || ob_size - curtype == 1
10238 || (ob_size - curtype == 2
10239 && *(obstack_next_free (&util_obstack) - 2) == 'r')))
10240 inline_contents = true;
10244 /* c0 and c1 are the last two characters in the encoding of the
10245 current type; if the last two characters were '^' or '^r',
10246 then we are encoding an aggregate that is "pointed to". The
10247 comment above applies: in that case we should avoid encoding
10248 the names of instance variables.
10250 char c1 = ob_size > 1 ? *(obstack_next_free (&util_obstack) - 2) : 0;
10251 char c0 = ob_size > 0 ? *(obstack_next_free (&util_obstack) - 1) : 0;
10253 if (c0 == '^' || (c1 == '^' && c0 == 'r'))
10256 if (format == OBJC_ENCODE_INLINE_DEFS || generating_instance_variables)
10259 inline_contents = true;
10262 /* Note that the check (ob_size - curtype < 2) prevents
10263 infinite recursion when encoding a structure which is
10264 a linked list (eg, struct node { struct node *next;
10265 }). Each time we follow a pointer, we add one
10266 character to ob_size, and curtype is fixed, so after
10267 at most two pointers we stop inlining contents and
10270 The other case where we don't inline is "^r", which
10271 is a pointer to a constant struct.
10273 if ((ob_size - curtype <= 2) && !(c0 == 'r'))
10274 inline_contents = true;
10279 /* Traverse struct aliases; it is important to get the
10280 original struct and its tag name (if any). */
10281 type = TYPE_MAIN_VARIANT (type);
10282 name = OBJC_TYPE_NAME (type);
10283 /* Open parenth/bracket. */
10284 obstack_1grow (&util_obstack, left);
10286 /* Encode the struct/union tag name, or '?' if a tag was
10287 not provided. Typedef aliases do not qualify. */
10289 /* For compatibility with the NeXT runtime, ObjC++ encodes template
10290 args as a composite struct tag name. */
10291 if (name && TREE_CODE (name) == IDENTIFIER_NODE
10292 /* Did this struct have a tag? */
10293 && !TYPE_WAS_ANONYMOUS (type))
10294 obstack_grow (&util_obstack,
10295 decl_as_string (type, TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME),
10296 strlen (decl_as_string (type, TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME)));
10298 if (name && TREE_CODE (name) == IDENTIFIER_NODE)
10299 obstack_grow (&util_obstack,
10300 IDENTIFIER_POINTER (name),
10301 strlen (IDENTIFIER_POINTER (name)));
10304 obstack_1grow (&util_obstack, '?');
10306 /* Encode the types (and possibly names) of the inner fields,
10308 if (inline_contents)
10310 obstack_1grow (&util_obstack, '=');
10311 encode_aggregate_fields (type, pointed_to, curtype, format);
10313 /* Close parenth/bracket. */
10314 obstack_1grow (&util_obstack, right);
10317 /* Encode a bitfield NeXT-style (i.e., without a bit offset or the underlying
10321 encode_next_bitfield (int width)
10324 sprintf (buffer, "b%d", width);
10325 obstack_grow (&util_obstack, buffer, strlen (buffer));
10329 /* Encodes 'type', ignoring type qualifiers (which you should encode
10330 beforehand if needed) with the exception of 'const', which is
10331 encoded by encode_type. See above for the explanation of
10332 'curtype'. 'format' can be OBJC_ENCODE_INLINE_DEFS or
10333 OBJC_ENCODE_DONT_INLINE_DEFS.
10336 encode_type (tree type, int curtype, int format)
10338 enum tree_code code = TREE_CODE (type);
10340 /* Ignore type qualifiers other than 'const' when encoding a
10343 if (type == error_mark_node)
10346 if (!flag_next_runtime)
10348 if (TYPE_READONLY (type))
10349 obstack_1grow (&util_obstack, 'r');
10354 case ENUMERAL_TYPE:
10355 if (flag_next_runtime)
10357 /* Kludge for backwards-compatibility with gcc-3.3: enums
10358 are always encoded as 'i' no matter what type they
10359 actually are (!). */
10360 obstack_1grow (&util_obstack, 'i');
10363 /* Else, they are encoded exactly like the integer type that is
10364 used by the compiler to store them. */
10368 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
10370 case 8: c = TYPE_UNSIGNED (type) ? 'C' : 'c'; break;
10371 case 16: c = TYPE_UNSIGNED (type) ? 'S' : 's'; break;
10374 tree int_type = type;
10375 if (flag_next_runtime)
10377 /* Another legacy kludge for compatiblity with
10378 gcc-3.3: 32-bit longs are encoded as 'l' or 'L',
10379 but not always. For typedefs, we need to use 'i'
10380 or 'I' instead if encoding a struct field, or a
10382 int_type = ((!generating_instance_variables
10383 && (obstack_object_size (&util_obstack)
10384 == (unsigned) curtype))
10385 ? TYPE_MAIN_VARIANT (type)
10388 if (int_type == long_unsigned_type_node
10389 || int_type == long_integer_type_node)
10390 c = TYPE_UNSIGNED (type) ? 'L' : 'l';
10392 c = TYPE_UNSIGNED (type) ? 'I' : 'i';
10395 case 64: c = TYPE_UNSIGNED (type) ? 'Q' : 'q'; break;
10396 case 128: c = TYPE_UNSIGNED (type) ? 'T' : 't'; break;
10397 default: gcc_unreachable ();
10399 obstack_1grow (&util_obstack, c);
10405 /* Floating point types. */
10406 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
10408 case 32: c = 'f'; break;
10409 case 64: c = 'd'; break;
10411 case 128: c = 'D'; break;
10412 default: gcc_unreachable ();
10414 obstack_1grow (&util_obstack, c);
10418 obstack_1grow (&util_obstack, 'v');
10422 obstack_1grow (&util_obstack, 'B');
10426 encode_array (type, curtype, format);
10431 case REFERENCE_TYPE:
10433 encode_pointer (type, curtype, format);
10437 encode_aggregate_within (type, curtype, format, '{', '}');
10441 encode_aggregate_within (type, curtype, format, '(', ')');
10444 case FUNCTION_TYPE: /* '?' means an unknown type. */
10445 obstack_1grow (&util_obstack, '?');
10449 /* A complex is encoded as 'j' followed by the inner type (eg,
10450 "_Complex int" is encoded as 'ji'). */
10451 obstack_1grow (&util_obstack, 'j');
10452 encode_type (TREE_TYPE (type), curtype, format);
10456 encode_vector (type, curtype, format);
10460 warning (0, "unknown type %s found during Objective-C encoding",
10461 gen_type_name (type));
10462 obstack_1grow (&util_obstack, '?');
10466 if (flag_next_runtime)
10468 /* Super-kludge. Some ObjC qualifier and type combinations need
10469 to be rearranged for compatibility with gcc-3.3. */
10470 if (code == POINTER_TYPE && obstack_object_size (&util_obstack) >= 3)
10472 char *enc = obstack_base (&util_obstack) + curtype;
10474 /* Rewrite "in const" from "nr" to "rn". */
10475 if (curtype >= 1 && !strncmp (enc - 1, "nr", 2))
10476 strncpy (enc - 1, "rn", 2);
10482 encode_gnu_bitfield (int position, tree type, int size)
10484 enum tree_code code = TREE_CODE (type);
10486 char charType = '?';
10488 /* This code is only executed for the GNU runtime, so we can ignore
10489 the NeXT runtime kludge of always encoding enums as 'i' no matter
10490 what integers they actually are. */
10491 if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
10493 if (integer_zerop (TYPE_MIN_VALUE (type)))
10494 /* Unsigned integer types. */
10496 switch (TYPE_MODE (type))
10499 charType = 'C'; break;
10501 charType = 'S'; break;
10504 if (type == long_unsigned_type_node)
10511 charType = 'Q'; break;
10513 gcc_unreachable ();
10517 /* Signed integer types. */
10519 switch (TYPE_MODE (type))
10522 charType = 'c'; break;
10524 charType = 's'; break;
10527 if (type == long_integer_type_node)
10534 charType = 'q'; break;
10536 gcc_unreachable ();
10542 /* Do not do any encoding, produce an error and keep going. */
10543 error ("trying to encode non-integer type as a bitfield");
10547 sprintf (buffer, "b%d%c%d", position, charType, size);
10548 obstack_grow (&util_obstack, buffer, strlen (buffer));
10552 encode_field_decl (tree field_decl, int curtype, int format)
10555 /* C++ static members, and things that are not fields at all,
10556 should not appear in the encoding. */
10557 if (TREE_CODE (field_decl) != FIELD_DECL || TREE_STATIC (field_decl))
10561 /* Generate the bitfield typing information, if needed. Note the difference
10562 between GNU and NeXT runtimes. */
10563 if (DECL_BIT_FIELD_TYPE (field_decl))
10565 int size = tree_low_cst (DECL_SIZE (field_decl), 1);
10567 if (flag_next_runtime)
10568 encode_next_bitfield (size);
10570 encode_gnu_bitfield (int_bit_position (field_decl),
10571 DECL_BIT_FIELD_TYPE (field_decl), size);
10574 encode_type (TREE_TYPE (field_decl), curtype, format);
10577 /* Decay array and function parameters into pointers. */
10580 objc_decay_parm_type (tree type)
10582 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE)
10583 type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE
10590 static GTY(()) tree objc_parmlist = NULL_TREE;
10592 /* Append PARM to a list of formal parameters of a method, making a necessary
10593 array-to-pointer adjustment along the way. */
10596 objc_push_parm (tree parm)
10600 if (TREE_TYPE (parm) == error_mark_node)
10602 objc_parmlist = chainon (objc_parmlist, parm);
10606 /* Decay arrays and functions into pointers. */
10607 type = objc_decay_parm_type (TREE_TYPE (parm));
10609 /* If the parameter type has been decayed, a new PARM_DECL needs to be
10611 if (type != TREE_TYPE (parm))
10612 parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type);
10614 DECL_ARG_TYPE (parm)
10615 = lang_hooks.types.type_promotes_to (TREE_TYPE (parm));
10617 /* Record constancy and volatility. */
10618 c_apply_type_quals_to_decl
10619 ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0)
10620 | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0)
10621 | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm);
10623 objc_parmlist = chainon (objc_parmlist, parm);
10626 /* Retrieve the formal parameter list constructed via preceding calls to
10627 objc_push_parm(). */
10631 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED)
10633 static struct c_arg_info *
10634 objc_get_parm_info (int have_ellipsis)
10638 tree parm_info = objc_parmlist;
10639 objc_parmlist = NULL_TREE;
10643 tree parm_info = objc_parmlist;
10644 struct c_arg_info *arg_info;
10645 /* The C front-end requires an elaborate song and dance at
10648 declare_parm_level ();
10651 tree next = DECL_CHAIN (parm_info);
10653 DECL_CHAIN (parm_info) = NULL_TREE;
10654 parm_info = pushdecl (parm_info);
10655 finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
10658 arg_info = get_parm_info (have_ellipsis);
10660 objc_parmlist = NULL_TREE;
10665 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
10666 method definitions. In the case of instance methods, we can be more
10667 specific as to the type of 'self'. */
10670 synth_self_and_ucmd_args (void)
10674 if (objc_method_context
10675 && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL)
10676 self_type = objc_instance_type;
10678 /* Really a `struct objc_class *'. However, we allow people to
10679 assign to self, which changes its type midstream. */
10680 self_type = objc_object_type;
10683 objc_push_parm (build_decl (input_location,
10684 PARM_DECL, self_id, self_type));
10687 objc_push_parm (build_decl (input_location,
10688 PARM_DECL, ucmd_id, objc_selector_type));
10691 /* Transform an Objective-C method definition into a static C function
10692 definition, synthesizing the first two arguments, "self" and "_cmd",
10696 start_method_def (tree method)
10702 struct c_arg_info *parm_info;
10704 int have_ellipsis = 0;
10706 /* If we are defining a "dealloc" method in a non-root class, we
10707 will need to check if a [super dealloc] is missing, and warn if
10709 if(CLASS_SUPER_NAME (objc_implementation_context)
10710 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method))))
10711 should_call_super_dealloc = 1;
10713 should_call_super_dealloc = 0;
10715 /* Required to implement _msgSuper. */
10716 objc_method_context = method;
10717 UOBJC_SUPER_decl = NULL_TREE;
10719 /* Generate prototype declarations for arguments..."new-style". */
10720 synth_self_and_ucmd_args ();
10722 /* Generate argument declarations if a keyword_decl. */
10723 parmlist = METHOD_SEL_ARGS (method);
10726 /* parmlist is a KEYWORD_DECL. */
10727 tree type = TREE_VALUE (TREE_TYPE (parmlist));
10730 parm = build_decl (input_location,
10731 PARM_DECL, KEYWORD_ARG_NAME (parmlist), type);
10732 decl_attributes (&parm, DECL_ATTRIBUTES (parmlist), 0);
10733 objc_push_parm (parm);
10734 parmlist = DECL_CHAIN (parmlist);
10737 if (METHOD_ADD_ARGS (method))
10741 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method));
10742 akey; akey = TREE_CHAIN (akey))
10744 objc_push_parm (TREE_VALUE (akey));
10747 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
10751 parm_info = objc_get_parm_info (have_ellipsis);
10753 really_start_method (objc_method_context, parm_info);
10756 /* Return 1 if TYPE1 is equivalent to TYPE2
10757 for purposes of method overloading. */
10760 objc_types_are_equivalent (tree type1, tree type2)
10762 if (type1 == type2)
10765 /* Strip away indirections. */
10766 while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE)
10767 && (TREE_CODE (type1) == TREE_CODE (type2)))
10768 type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2);
10769 if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
10772 type1 = (TYPE_HAS_OBJC_INFO (type1)
10773 ? TYPE_OBJC_PROTOCOL_LIST (type1)
10775 type2 = (TYPE_HAS_OBJC_INFO (type2)
10776 ? TYPE_OBJC_PROTOCOL_LIST (type2)
10779 if (list_length (type1) == list_length (type2))
10781 for (; type2; type2 = TREE_CHAIN (type2))
10782 if (!lookup_protocol_in_reflist (type1, TREE_VALUE (type2)))
10789 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
10792 objc_types_share_size_and_alignment (tree type1, tree type2)
10794 return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
10795 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2));
10798 /* Return 1 if PROTO1 is equivalent to PROTO2
10799 for purposes of method overloading. Ordinarily, the type signatures
10800 should match up exactly, unless STRICT is zero, in which case we
10801 shall allow differences in which the size and alignment of a type
10805 comp_proto_with_proto (tree proto1, tree proto2, int strict)
10807 /* The following test is needed in case there are hashing
10809 if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2))
10812 return match_proto_with_proto (proto1, proto2, strict);
10816 match_proto_with_proto (tree proto1, tree proto2, int strict)
10820 /* Compare return types. */
10821 type1 = TREE_VALUE (TREE_TYPE (proto1));
10822 type2 = TREE_VALUE (TREE_TYPE (proto2));
10824 if (!objc_types_are_equivalent (type1, type2)
10825 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
10828 /* Compare argument types. */
10829 for (type1 = get_arg_type_list (proto1, METHOD_REF, 0),
10830 type2 = get_arg_type_list (proto2, METHOD_REF, 0);
10832 type1 = TREE_CHAIN (type1), type2 = TREE_CHAIN (type2))
10834 if (!objc_types_are_equivalent (TREE_VALUE (type1), TREE_VALUE (type2))
10836 || !objc_types_share_size_and_alignment (TREE_VALUE (type1),
10837 TREE_VALUE (type2))))
10841 return (!type1 && !type2);
10844 /* Fold an OBJ_TYPE_REF expression for ObjC method dispatches, where
10845 this occurs. ObjC method dispatches are _not_ like C++ virtual
10846 member function dispatches, and we account for the difference here. */
10849 objc_fold_obj_type_ref (tree ref, tree known_type)
10851 objc_fold_obj_type_ref (tree ref ATTRIBUTE_UNUSED,
10852 tree known_type ATTRIBUTE_UNUSED)
10856 tree v = BINFO_VIRTUALS (TYPE_BINFO (known_type));
10858 /* If the receiver does not have virtual member functions, there
10859 is nothing we can (or need to) do here. */
10863 /* Let C++ handle C++ virtual functions. */
10864 return cp_fold_obj_type_ref (ref, known_type);
10866 /* For plain ObjC, we currently do not need to do anything. */
10872 objc_start_function (tree name, tree type, tree attrs,
10876 struct c_arg_info *params
10880 tree fndecl = build_decl (input_location,
10881 FUNCTION_DECL, name, type);
10884 DECL_ARGUMENTS (fndecl) = params;
10885 DECL_INITIAL (fndecl) = error_mark_node;
10886 DECL_EXTERNAL (fndecl) = 0;
10887 TREE_STATIC (fndecl) = 1;
10888 retrofit_lang_decl (fndecl);
10889 cplus_decl_attributes (&fndecl, attrs, 0);
10890 start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT);
10892 current_function_returns_value = 0; /* Assume, until we see it does. */
10893 current_function_returns_null = 0;
10894 decl_attributes (&fndecl, attrs, 0);
10895 announce_function (fndecl);
10896 DECL_INITIAL (fndecl) = error_mark_node;
10897 DECL_EXTERNAL (fndecl) = 0;
10898 TREE_STATIC (fndecl) = 1;
10899 current_function_decl = pushdecl (fndecl);
10901 declare_parm_level ();
10902 DECL_RESULT (current_function_decl)
10903 = build_decl (input_location,
10904 RESULT_DECL, NULL_TREE,
10905 TREE_TYPE (TREE_TYPE (current_function_decl)));
10906 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1;
10907 DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1;
10908 start_fname_decls ();
10909 store_parm_decls_from (params);
10912 TREE_USED (current_function_decl) = 1;
10915 /* - Generate an identifier for the function. the format is "_n_cls",
10916 where 1 <= n <= nMethods, and cls is the name the implementation we
10918 - Install the return type from the method declaration.
10919 - If we have a prototype, check for type consistency. */
10922 really_start_method (tree method,
10926 struct c_arg_info *parmlist
10930 tree ret_type, meth_type;
10932 const char *sel_name, *class_name, *cat_name;
10935 /* Synth the storage class & assemble the return type. */
10936 ret_type = TREE_VALUE (TREE_TYPE (method));
10938 sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
10939 class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
10940 cat_name = ((TREE_CODE (objc_implementation_context)
10941 == CLASS_IMPLEMENTATION_TYPE)
10943 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context)));
10946 /* Make sure this is big enough for any plausible method label. */
10947 buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
10948 + (cat_name ? strlen (cat_name) : 0));
10950 OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
10951 class_name, cat_name, sel_name, method_slot);
10953 method_id = get_identifier (buf);
10956 /* Objective-C methods cannot be overloaded, so we don't need
10957 the type encoding appended. It looks bad anyway... */
10958 push_lang_context (lang_name_c);
10962 = build_function_type (ret_type,
10963 get_arg_type_list (method, METHOD_DEF, 0));
10964 objc_start_function (method_id, meth_type, NULL_TREE, parmlist);
10966 /* Set self_decl from the first argument. */
10967 self_decl = DECL_ARGUMENTS (current_function_decl);
10969 /* Suppress unused warnings. */
10970 TREE_USED (self_decl) = 1;
10971 DECL_READ_P (self_decl) = 1;
10972 TREE_USED (DECL_CHAIN (self_decl)) = 1;
10973 DECL_READ_P (DECL_CHAIN (self_decl)) = 1;
10975 pop_lang_context ();
10978 METHOD_DEFINITION (method) = current_function_decl;
10980 /* Check consistency...start_function, pushdecl, duplicate_decls. */
10982 if (implementation_template != objc_implementation_context)
10985 = lookup_method_static (implementation_template,
10986 METHOD_SEL_NAME (method),
10987 ((TREE_CODE (method) == CLASS_METHOD_DECL)
10988 | OBJC_LOOKUP_NO_SUPER));
10992 if (!comp_proto_with_proto (method, proto, 1))
10994 bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL;
10996 warning_at (DECL_SOURCE_LOCATION (method), 0,
10997 "conflicting types for %<%c%s%>",
10998 (type ? '-' : '+'),
10999 identifier_to_locale (gen_method_decl (method)));
11000 inform (DECL_SOURCE_LOCATION (proto),
11001 "previous declaration of %<%c%s%>",
11002 (type ? '-' : '+'),
11003 identifier_to_locale (gen_method_decl (proto)));
11007 /* If the method in the @interface was deprecated, mark
11008 the implemented method as deprecated too. It should
11009 never be used for messaging (when the deprecation
11010 warnings are produced), but just in case. */
11011 if (TREE_DEPRECATED (proto))
11012 TREE_DEPRECATED (method) = 1;
11014 /* If the method in the @interface was marked as
11015 'noreturn', mark the function implementing the method
11016 as 'noreturn' too. */
11017 TREE_THIS_VOLATILE (current_function_decl) = TREE_THIS_VOLATILE (proto);
11022 /* We have a method @implementation even though we did not
11023 see a corresponding @interface declaration (which is allowed
11024 by Objective-C rules). Go ahead and place the method in
11025 the @interface anyway, so that message dispatch lookups
11027 tree interface = implementation_template;
11029 if (TREE_CODE (objc_implementation_context)
11030 == CATEGORY_IMPLEMENTATION_TYPE)
11031 interface = lookup_category
11033 CLASS_SUPER_NAME (objc_implementation_context));
11036 objc_add_method (interface, copy_node (method),
11037 TREE_CODE (method) == CLASS_METHOD_DECL,
11038 /* is_optional= */ false);
11043 static void *UOBJC_SUPER_scope = 0;
11045 /* _n_Method (id self, SEL sel, ...)
11047 struct objc_super _S;
11048 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
11052 get_super_receiver (void)
11054 if (objc_method_context)
11056 tree super_expr, super_expr_list;
11058 if (!UOBJC_SUPER_decl)
11060 UOBJC_SUPER_decl = build_decl (input_location,
11061 VAR_DECL, get_identifier (TAG_SUPER),
11062 objc_super_template);
11063 /* This prevents `unused variable' warnings when compiling with -Wall. */
11064 TREE_USED (UOBJC_SUPER_decl) = 1;
11065 DECL_READ_P (UOBJC_SUPER_decl) = 1;
11066 lang_hooks.decls.pushdecl (UOBJC_SUPER_decl);
11067 finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE,
11069 UOBJC_SUPER_scope = objc_get_current_scope ();
11072 /* Set receiver to self. */
11073 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id);
11074 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
11075 NOP_EXPR, input_location, self_decl,
11077 super_expr_list = super_expr;
11079 /* Set class to begin searching. */
11080 super_expr = objc_build_component_ref (UOBJC_SUPER_decl,
11081 get_identifier ("super_class"));
11083 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
11085 /* [_cls, __cls]Super are "pre-built" in
11086 synth_forward_declarations. */
11088 super_expr = build_modify_expr (input_location, super_expr,
11089 NULL_TREE, NOP_EXPR,
11091 ((TREE_CODE (objc_method_context)
11092 == INSTANCE_METHOD_DECL)
11094 : uucls_super_ref),
11099 /* We have a category. */
11101 tree super_name = CLASS_SUPER_NAME (implementation_template);
11104 /* Barf if super used in a category of Object. */
11107 error ("no super class declared in interface for %qE",
11108 CLASS_NAME (implementation_template));
11109 return error_mark_node;
11112 if (flag_next_runtime && !flag_zero_link)
11114 super_class = objc_get_class_reference (super_name);
11115 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
11116 /* If we are in a class method, we must retrieve the
11117 _metaclass_ for the current class, pointed at by
11118 the class's "isa" pointer. The following assumes that
11119 "isa" is the first ivar in a class (which it must be). */
11121 = build_indirect_ref
11123 build_c_cast (input_location,
11124 build_pointer_type (objc_class_type),
11125 super_class), RO_UNARY_STAR);
11129 add_class_reference (super_name);
11130 super_class = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
11131 ? objc_get_class_decl : objc_get_meta_class_decl);
11132 assemble_external (super_class);
11134 = build_function_call
11139 my_build_string_pointer
11140 (IDENTIFIER_LENGTH (super_name) + 1,
11141 IDENTIFIER_POINTER (super_name))));
11145 = build_modify_expr (input_location, super_expr, NULL_TREE,
11148 build_c_cast (input_location,
11149 TREE_TYPE (super_expr),
11154 super_expr_list = build_compound_expr (input_location,
11155 super_expr_list, super_expr);
11157 super_expr = build_unary_op (input_location,
11158 ADDR_EXPR, UOBJC_SUPER_decl, 0);
11159 super_expr_list = build_compound_expr (input_location,
11160 super_expr_list, super_expr);
11162 return super_expr_list;
11166 error ("[super ...] must appear in a method context");
11167 return error_mark_node;
11171 /* When exiting a scope, sever links to a 'super' declaration (if any)
11172 therein contained. */
11175 objc_clear_super_receiver (void)
11177 if (objc_method_context
11178 && UOBJC_SUPER_scope == objc_get_current_scope ()) {
11179 UOBJC_SUPER_decl = 0;
11180 UOBJC_SUPER_scope = 0;
11185 objc_finish_method_definition (tree fndecl)
11187 /* We cannot validly inline ObjC methods, at least not without a language
11188 extension to declare that a method need not be dynamically
11189 dispatched, so suppress all thoughts of doing so. */
11190 DECL_UNINLINABLE (fndecl) = 1;
11193 /* The C++ front-end will have called finish_function() for us. */
11194 finish_function ();
11197 METHOD_ENCODING (objc_method_context)
11198 = encode_method_prototype (objc_method_context);
11200 /* Required to implement _msgSuper. This must be done AFTER finish_function,
11201 since the optimizer may find "may be used before set" errors. */
11202 objc_method_context = NULL_TREE;
11204 if (should_call_super_dealloc)
11205 warning (0, "method possibly missing a [super dealloc] call");
11208 /* Given a tree DECL node, produce a printable description of it in the given
11209 buffer, overwriting the buffer. */
11212 gen_declaration (tree decl)
11218 gen_type_name_0 (TREE_TYPE (decl));
11220 if (DECL_NAME (decl))
11222 if (!POINTER_TYPE_P (TREE_TYPE (decl)))
11223 strcat (errbuf, " ");
11225 strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
11228 if (DECL_INITIAL (decl)
11229 && TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST)
11230 sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
11231 TREE_INT_CST_LOW (DECL_INITIAL (decl)));
11237 /* Given a tree TYPE node, produce a printable description of it in the given
11238 buffer, overwriting the buffer. */
11241 gen_type_name_0 (tree type)
11243 tree orig = type, proto;
11245 if (TYPE_P (type) && TYPE_NAME (type))
11246 type = TYPE_NAME (type);
11247 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
11249 tree inner = TREE_TYPE (type);
11251 while (TREE_CODE (inner) == ARRAY_TYPE)
11252 inner = TREE_TYPE (inner);
11254 gen_type_name_0 (inner);
11256 if (!POINTER_TYPE_P (inner))
11257 strcat (errbuf, " ");
11259 if (POINTER_TYPE_P (type))
11260 strcat (errbuf, "*");
11262 while (type != inner)
11264 strcat (errbuf, "[");
11266 if (TYPE_DOMAIN (type))
11270 sprintf (sz, HOST_WIDE_INT_PRINT_DEC,
11272 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1));
11273 strcat (errbuf, sz);
11276 strcat (errbuf, "]");
11277 type = TREE_TYPE (type);
11280 goto exit_function;
11283 if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type))
11284 type = DECL_NAME (type);
11286 strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE
11287 ? IDENTIFIER_POINTER (type)
11290 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
11291 if (objc_is_id (orig))
11292 orig = TREE_TYPE (orig);
11294 proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE;
11298 strcat (errbuf, " <");
11302 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto))));
11303 proto = TREE_CHAIN (proto);
11304 strcat (errbuf, proto ? ", " : ">");
11313 gen_type_name (tree type)
11317 return gen_type_name_0 (type);
11320 /* Given a method tree, put a printable description into the given
11321 buffer (overwriting) and return a pointer to the buffer. */
11324 gen_method_decl (tree method)
11328 strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */
11329 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method)));
11330 strcat (errbuf, ")");
11331 chain = METHOD_SEL_ARGS (method);
11335 /* We have a chain of keyword_decls. */
11338 if (KEYWORD_KEY_NAME (chain))
11339 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
11341 strcat (errbuf, ":(");
11342 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain)));
11343 strcat (errbuf, ")");
11345 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
11346 if ((chain = DECL_CHAIN (chain)))
11347 strcat (errbuf, " ");
11351 if (METHOD_ADD_ARGS (method))
11353 chain = TREE_CHAIN (METHOD_ADD_ARGS (method));
11355 /* Know we have a chain of parm_decls. */
11358 strcat (errbuf, ", ");
11359 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain)));
11360 chain = TREE_CHAIN (chain);
11363 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
11364 strcat (errbuf, ", ...");
11369 /* We have a unary selector. */
11370 strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
11378 /* Dump an @interface declaration of the supplied class CHAIN to the
11379 supplied file FP. Used to implement the -gen-decls option (which
11380 prints out an @interface declaration of all classes compiled in
11381 this run); potentially useful for debugging the compiler too. */
11383 dump_interface (FILE *fp, tree chain)
11385 /* FIXME: A heap overflow here whenever a method (or ivar)
11386 declaration is so long that it doesn't fit in the buffer. The
11387 code and all the related functions should be rewritten to avoid
11388 using fixed size buffers. */
11389 const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
11390 tree ivar_decls = CLASS_RAW_IVARS (chain);
11391 tree nst_methods = CLASS_NST_METHODS (chain);
11392 tree cls_methods = CLASS_CLS_METHODS (chain);
11394 fprintf (fp, "\n@interface %s", my_name);
11396 /* CLASS_SUPER_NAME is used to store the superclass name for
11397 classes, and the category name for categories. */
11398 if (CLASS_SUPER_NAME (chain))
11400 const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
11402 switch (TREE_CODE (chain))
11404 case CATEGORY_IMPLEMENTATION_TYPE:
11405 case CATEGORY_INTERFACE_TYPE:
11406 fprintf (fp, " (%s)\n", name);
11409 fprintf (fp, " : %s\n", name);
11414 fprintf (fp, "\n");
11416 /* FIXME - the following doesn't seem to work at the moment. */
11419 fprintf (fp, "{\n");
11422 fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls));
11423 ivar_decls = TREE_CHAIN (ivar_decls);
11425 while (ivar_decls);
11426 fprintf (fp, "}\n");
11429 while (nst_methods)
11431 fprintf (fp, "- %s;\n", gen_method_decl (nst_methods));
11432 nst_methods = TREE_CHAIN (nst_methods);
11435 while (cls_methods)
11437 fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods));
11438 cls_methods = TREE_CHAIN (cls_methods);
11441 fprintf (fp, "@end\n");
11445 /* Produce the pretty printing for an Objective-C method. This is
11446 currently unused, but could be handy while reorganizing the pretty
11447 printing to be more robust. */
11448 static const char *
11449 objc_pretty_print_method (bool is_class_method,
11450 const char *class_name,
11451 const char *category_name,
11452 const char *selector)
11456 char *result = XNEWVEC (char, strlen (class_name) + strlen (category_name)
11457 + strlen (selector) + 7);
11459 if (is_class_method)
11460 sprintf (result, "+[%s(%s) %s]", class_name, category_name, selector);
11462 sprintf (result, "-[%s(%s) %s]", class_name, category_name, selector);
11468 char *result = XNEWVEC (char, strlen (class_name)
11469 + strlen (selector) + 5);
11471 if (is_class_method)
11472 sprintf (result, "+[%s %s]", class_name, selector);
11474 sprintf (result, "-[%s %s]", class_name, selector);
11481 /* Demangle function for Objective-C. Attempt to demangle the
11482 function name associated with a method (eg, going from
11483 "_i_NSObject__class" to "-[NSObject class]"); usually for the
11484 purpose of pretty printing or error messages. Return the demangled
11485 name, or NULL if the string is not an Objective-C mangled method
11488 Because of how the mangling is done, any method that has a '_' in
11489 its original name is at risk of being demangled incorrectly. In
11490 some cases there are multiple valid ways to demangle a method name
11491 and there is no way we can decide.
11493 TODO: objc_demangle() can't always get it right; the right way to
11494 get this correct for all method names would be to store the
11495 Objective-C method name somewhere in the function decl. Then,
11496 there is no demangling to do; we'd just pull the method name out of
11497 the decl. As an additional bonus, when printing error messages we
11498 could check for such a method name, and if we find it, we know the
11499 function is actually an Objective-C method and we could print error
11500 messages saying "In method '+[NSObject class]" instead of "In
11501 function '+[NSObject class]" as we do now. */
11502 static const char *
11503 objc_demangle (const char *mangled)
11505 char *demangled, *cp;
11507 if (mangled[0] == '_' &&
11508 (mangled[1] == 'i' || mangled[1] == 'c') &&
11511 cp = demangled = XNEWVEC (char, strlen(mangled) + 2);
11512 if (mangled[1] == 'i')
11513 *cp++ = '-'; /* for instance method */
11515 *cp++ = '+'; /* for class method */
11516 *cp++ = '['; /* opening left brace */
11517 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
11518 while (*cp && *cp == '_')
11519 cp++; /* skip any initial underbars in class name */
11520 cp = strchr(cp, '_'); /* find first non-initial underbar */
11523 free(demangled); /* not mangled name */
11526 if (cp[1] == '_') /* easy case: no category name */
11528 *cp++ = ' '; /* replace two '_' with one ' ' */
11529 strcpy(cp, mangled + (cp - demangled) + 2);
11533 *cp++ = '('; /* less easy case: category name */
11534 cp = strchr(cp, '_');
11537 free(demangled); /* not mangled name */
11541 *cp++ = ' '; /* overwriting 1st char of method name... */
11542 strcpy(cp, mangled + (cp - demangled)); /* get it back */
11544 /* Now we have the method name. We need to generally replace
11545 '_' with ':' but trying to preserve '_' if it could only have
11546 been in the mangled string because it was already in the
11547 original name. In cases where it's ambiguous, we assume that
11548 any '_' originated from a ':'. */
11550 /* Initial '_'s in method name can't have been generating by
11551 converting ':'s. Skip them. */
11552 while (*cp && *cp == '_')
11555 /* If the method name does not end with '_', then it has no
11556 arguments and there was no replacement of ':'s with '_'s
11557 during mangling. Check for that case, and skip any
11558 replacement if so. This at least guarantees that methods
11559 with no arguments are always demangled correctly (unless the
11560 original name ends with '_'). */
11561 if (*(mangled + strlen (mangled) - 1) != '_')
11563 /* Skip to the end. */
11569 /* Replace remaining '_' with ':'. This may get it wrong if
11570 there were '_'s in the original name. In most cases it
11571 is impossible to disambiguate. */
11576 *cp++ = ']'; /* closing right brace */
11577 *cp++ = 0; /* string terminator */
11581 return NULL; /* not an objc mangled name */
11584 /* Try to pretty-print a decl. If the 'decl' is an Objective-C
11585 specific decl, return the printable name for it. If not, return
11588 objc_maybe_printable_name (tree decl, int v ATTRIBUTE_UNUSED)
11590 switch (TREE_CODE (decl))
11592 case FUNCTION_DECL:
11593 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)));
11596 /* The following happens when we are printing a deprecation
11597 warning for a method. The warn_deprecation() will end up
11598 trying to print the decl for INSTANCE_METHOD_DECL or
11599 CLASS_METHOD_DECL. It would be nice to be able to print
11600 "-[NSObject autorelease] is deprecated", but to do that, we'd
11601 need to store the class and method name in the method decl,
11602 which we currently don't do. For now, just return the name
11603 of the method. We don't return NULL, because that may
11604 trigger further attempts to pretty-print the decl in C/C++,
11605 but they wouldn't know how to pretty-print it. */
11606 case INSTANCE_METHOD_DECL:
11607 case CLASS_METHOD_DECL:
11608 return IDENTIFIER_POINTER (DECL_NAME (decl));
11610 /* This happens when printing a deprecation warning for a
11611 property. We may want to consider some sort of pretty
11612 printing (eg, include the class name where it was declared
11614 case PROPERTY_DECL:
11615 return IDENTIFIER_POINTER (PROPERTY_NAME (decl));
11623 /* Return a printable name for 'decl'. This first tries
11624 objc_maybe_printable_name(), and if that fails, it returns the name
11625 in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for
11626 Objective-C; in Objective-C++, setting the hook is not enough
11627 because lots of C++ Front-End code calls cxx_printable_name,
11628 dump_decl and other C++ functions directly. So instead we have
11629 modified dump_decl to call objc_maybe_printable_name directly. */
11631 objc_printable_name (tree decl, int v)
11633 const char *demangled_name = objc_maybe_printable_name (decl, v);
11635 if (demangled_name != NULL)
11636 return demangled_name;
11638 return IDENTIFIER_POINTER (DECL_NAME (decl));
11644 gcc_obstack_init (&util_obstack);
11645 util_firstobj = (char *) obstack_finish (&util_obstack);
11647 errbuf = XNEWVEC (char, 1024 * 10);
11649 synth_module_prologue ();
11655 struct imp_entry *impent;
11657 /* The internally generated initializers appear to have missing braces.
11658 Don't warn about this. */
11659 int save_warn_missing_braces = warn_missing_braces;
11660 warn_missing_braces = 0;
11662 /* A missing @end may not be detected by the parser. */
11663 if (objc_implementation_context)
11665 warning (0, "%<@end%> missing in implementation context");
11666 finish_class (objc_implementation_context);
11667 objc_ivar_chain = NULL_TREE;
11668 objc_implementation_context = NULL_TREE;
11671 /* Process the static instances here because initialization of objc_symtab
11672 depends on them. */
11673 if (objc_static_instances)
11674 generate_static_references ();
11676 /* forward declare categories */
11678 forward_declare_categories ();
11680 for (impent = imp_list; impent; impent = impent->next)
11682 objc_implementation_context = impent->imp_context;
11683 implementation_template = impent->imp_template;
11685 /* FIXME: This needs reworking to be more obvious. */
11687 UOBJC_CLASS_decl = impent->class_decl;
11688 UOBJC_METACLASS_decl = impent->meta_decl;
11690 /* Dump the @interface of each class as we compile it, if the
11691 -gen-decls option is in use. TODO: Dump the classes in the
11692 order they were found, rather than in reverse order as we
11694 if (flag_gen_declaration)
11696 dump_interface (gen_declaration_file, objc_implementation_context);
11699 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
11701 /* all of the following reference the string pool... */
11702 generate_ivar_lists ();
11703 generate_dispatch_tables ();
11704 generate_shared_structures (impent);
11708 generate_dispatch_tables ();
11709 generate_category (impent);
11712 impent->class_decl = UOBJC_CLASS_decl;
11713 impent->meta_decl = UOBJC_METACLASS_decl;
11716 /* If we are using an array of selectors, we must always
11717 finish up the array decl even if no selectors were used. */
11718 if (flag_next_runtime)
11719 build_next_selector_translation_table ();
11721 build_gnu_selector_translation_table ();
11723 if (protocol_chain)
11724 generate_protocols ();
11726 if (flag_next_runtime)
11727 generate_objc_image_info ();
11729 if (imp_list || class_names_chain
11730 || meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
11731 generate_objc_symtab_decl ();
11733 /* Arrange for ObjC data structures to be initialized at run time. */
11734 if (objc_implementation_context || class_names_chain || objc_static_instances
11735 || meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
11737 build_module_descriptor ();
11739 if (!flag_next_runtime)
11740 build_module_initializer_routine ();
11743 /* Dump the class references. This forces the appropriate classes
11744 to be linked into the executable image, preserving unix archive
11745 semantics. This can be removed when we move to a more dynamically
11746 linked environment. */
11748 for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
11750 handle_class_ref (chain);
11751 if (TREE_PURPOSE (chain))
11752 generate_classref_translation_entry (chain);
11755 for (impent = imp_list; impent; impent = impent->next)
11756 handle_impent (impent);
11763 /* Run through the selector hash tables and print a warning for any
11764 selector which has multiple methods. */
11766 for (slot = 0; slot < SIZEHASHTABLE; slot++)
11768 for (hsh = cls_method_hash_list[slot]; hsh; hsh = hsh->next)
11769 check_duplicates (hsh, 0, 1);
11770 for (hsh = nst_method_hash_list[slot]; hsh; hsh = hsh->next)
11771 check_duplicates (hsh, 0, 1);
11775 warn_missing_braces = save_warn_missing_braces;
11778 /* Subroutines of finish_objc. */
11781 generate_classref_translation_entry (tree chain)
11783 tree expr, decl, type;
11785 decl = TREE_PURPOSE (chain);
11786 type = TREE_TYPE (decl);
11788 expr = add_objc_string (TREE_VALUE (chain), class_names);
11789 expr = convert (type, expr); /* cast! */
11791 /* This is a class reference. It is re-written by the runtime,
11792 but will be optimized away unless we force it. */
11793 DECL_PRESERVE_P (decl) = 1;
11794 finish_var_decl (decl, expr);
11799 handle_class_ref (tree chain)
11801 const char *name = IDENTIFIER_POINTER (TREE_VALUE (chain));
11802 char *string = (char *) alloca (strlen (name) + 30);
11806 sprintf (string, "%sobjc_class_name_%s",
11807 (flag_next_runtime ? "." : "__"), name);
11809 #ifdef ASM_DECLARE_UNRESOLVED_REFERENCE
11810 if (flag_next_runtime)
11812 ASM_DECLARE_UNRESOLVED_REFERENCE (asm_out_file, string);
11817 /* Make a decl for this name, so we can use its address in a tree. */
11818 decl = build_decl (input_location,
11819 VAR_DECL, get_identifier (string), TREE_TYPE (integer_zero_node));
11820 DECL_EXTERNAL (decl) = 1;
11821 TREE_PUBLIC (decl) = 1;
11823 finish_var_decl (decl, 0);
11825 /* Make a decl for the address. */
11826 sprintf (string, "%sobjc_class_ref_%s",
11827 (flag_next_runtime ? "." : "__"), name);
11828 exp = build1 (ADDR_EXPR, string_type_node, decl);
11829 decl = build_decl (input_location,
11830 VAR_DECL, get_identifier (string), string_type_node);
11831 TREE_STATIC (decl) = 1;
11832 TREE_USED (decl) = 1;
11833 DECL_READ_P (decl) = 1;
11834 DECL_ARTIFICIAL (decl) = 1;
11835 DECL_INITIAL (decl) = error_mark_node;
11837 /* We must force the reference. */
11838 DECL_PRESERVE_P (decl) = 1;
11841 finish_var_decl (decl, exp);
11845 handle_impent (struct imp_entry *impent)
11849 objc_implementation_context = impent->imp_context;
11850 implementation_template = impent->imp_template;
11852 switch (TREE_CODE (impent->imp_context))
11854 case CLASS_IMPLEMENTATION_TYPE:
11856 const char *const class_name =
11857 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
11859 string = (char *) alloca (strlen (class_name) + 30);
11861 sprintf (string, "%sobjc_class_name_%s",
11862 (flag_next_runtime ? "." : "__"), class_name);
11865 case CATEGORY_IMPLEMENTATION_TYPE:
11867 const char *const class_name =
11868 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
11869 const char *const class_super_name =
11870 IDENTIFIER_POINTER (CLASS_SUPER_NAME (impent->imp_context));
11872 string = (char *) alloca (strlen (class_name)
11873 + strlen (class_super_name) + 30);
11875 /* Do the same for categories. Even though no references to
11876 these symbols are generated automatically by the compiler,
11877 it gives you a handle to pull them into an archive by
11879 sprintf (string, "*%sobjc_category_name_%s_%s",
11880 (flag_next_runtime ? "." : "__"), class_name, class_super_name);
11887 #ifdef ASM_DECLARE_CLASS_REFERENCE
11888 if (flag_next_runtime)
11890 ASM_DECLARE_CLASS_REFERENCE (asm_out_file, string);
11898 init = integer_zero_node;
11899 decl = build_decl (input_location,
11900 VAR_DECL, get_identifier (string), TREE_TYPE (init));
11901 TREE_PUBLIC (decl) = 1;
11902 TREE_READONLY (decl) = 1;
11903 TREE_USED (decl) = 1;
11904 TREE_CONSTANT (decl) = 1;
11905 DECL_CONTEXT (decl) = NULL_TREE;
11906 DECL_ARTIFICIAL (decl) = 1;
11907 TREE_STATIC (decl) = 1;
11908 DECL_INITIAL (decl) = error_mark_node; /* A real initializer is coming... */
11909 /* We must force the reference. */
11910 DECL_PRESERVE_P (decl) = 1;
11912 finish_var_decl(decl, init) ;
11916 /* The Fix-and-Continue functionality available in Mac OS X 10.3 and
11917 later requires that ObjC translation units participating in F&C be
11918 specially marked. The following routine accomplishes this. */
11920 /* static int _OBJC_IMAGE_INFO[2] = { 0, 1 }; */
11923 generate_objc_image_info (void)
11927 = ((flag_replace_objc_classes && imp_count ? 1 : 0)
11928 | (flag_objc_gc ? 2 : 0));
11929 VEC(constructor_elt,gc) *v = NULL;
11933 return; /* No need for an image_info entry. */
11935 array_type = build_sized_array_type (integer_type_node, 2);
11937 decl = start_var_decl (array_type, "_OBJC_IMAGE_INFO");
11939 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
11940 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (integer_type_node, flags));
11941 /* If we need this (determined above) it is because the runtime wants to
11942 refer to it in a manner hidden from the compiler. So we must force the
11944 DECL_PRESERVE_P (decl) = 1;
11945 finish_var_decl (decl, objc_build_constructor (TREE_TYPE (decl), v));
11948 /* Routine is called to issue diagnostic when reference to a private
11949 ivar is made and no other variable with same name is found in
11952 objc_diagnose_private_ivar (tree id)
11955 if (!objc_method_context)
11957 ivar = is_ivar (objc_ivar_chain, id);
11958 if (ivar && is_private (ivar))
11960 error ("instance variable %qs is declared private",
11961 IDENTIFIER_POINTER (id));
11967 /* Look up ID as an instance variable. OTHER contains the result of
11968 the C or C++ lookup, which we may want to use instead. */
11969 /* To use properties inside an instance method, use self.property. */
11971 objc_lookup_ivar (tree other, tree id)
11975 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
11976 if (!objc_method_context)
11979 if (!strcmp (IDENTIFIER_POINTER (id), "super"))
11980 /* We have a message to super. */
11981 return get_super_receiver ();
11983 /* In a class method, look up an instance variable only as a last
11985 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
11986 && other && other != error_mark_node)
11989 /* Look up the ivar, but do not use it if it is not accessible. */
11990 ivar = is_ivar (objc_ivar_chain, id);
11992 if (!ivar || is_private (ivar))
11995 /* In an instance method, a local variable (or parameter) may hide the
11996 instance variable. */
11997 if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
11998 && other && other != error_mark_node
12000 && CP_DECL_CONTEXT (other) != global_namespace)
12002 && !DECL_FILE_SCOPE_P (other))
12005 warning (0, "local declaration of %qE hides instance variable", id);
12010 /* At this point, we are either in an instance method with no obscuring
12011 local definitions, or in a class method with no alternate definitions
12013 return build_ivar_reference (id);
12016 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
12017 needs to be done if we are calling a function through a cast. */
12020 objc_rewrite_function_call (tree function, tree first_param)
12022 if (TREE_CODE (function) == NOP_EXPR
12023 && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR
12024 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0))
12027 function = build3 (OBJ_TYPE_REF, TREE_TYPE (function),
12028 TREE_OPERAND (function, 0),
12029 first_param, size_zero_node);
12035 /* This is called to "gimplify" a PROPERTY_REF node. It builds the
12036 corresponding 'getter' function call. Note that we assume the
12037 PROPERTY_REF to be valid since we generated it while parsing. */
12039 objc_gimplify_property_ref (tree *expr_p)
12041 tree object_expression = PROPERTY_REF_OBJECT (*expr_p);
12042 tree property_decl = PROPERTY_REF_PROPERTY_DECL (*expr_p);
12043 tree call_exp, getter;
12045 /* TODO: Implement super.property. */
12047 getter = objc_finish_message_expr (object_expression,
12048 PROPERTY_GETTER_NAME (property_decl),
12052 /* In C++, a getter which returns an aggregate value results in a
12053 target_expr which initializes a temporary to the call
12055 if (TREE_CODE (getter) == TARGET_EXPR)
12057 gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter)));
12058 gcc_assert (TREE_CODE (TREE_OPERAND (getter, 0)) == VAR_DECL);
12059 call_exp = TREE_OPERAND (getter, 1);
12062 gcc_assert (TREE_CODE (call_exp) == CALL_EXPR);
12064 *expr_p = call_exp;
12067 /* This is called when "gimplifying" the trees. We need to gimplify
12068 the Objective-C/Objective-C++ specific trees, then hand over the
12069 process to C/C++. */
12071 objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
12073 enum tree_code code = TREE_CODE (*expr_p);
12076 /* Look for the special case of OBJC_TYPE_REF with the address
12077 of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend
12078 or one of its cousins). */
12080 if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR
12081 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0))
12084 enum gimplify_status r0, r1;
12086 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
12087 value of the OBJ_TYPE_REF, so force them to be emitted
12088 during subexpression evaluation rather than after the
12089 OBJ_TYPE_REF. This permits objc_msgSend calls in
12090 Objective C to use direct rather than indirect calls when
12091 the object expression has a postincrement. */
12092 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL,
12093 is_gimple_val, fb_rvalue);
12094 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
12095 is_gimple_val, fb_rvalue);
12097 return MIN (r0, r1);
12101 objc_gimplify_property_ref (expr_p);
12102 /* Do not return yet; let C/C++ gimplify the resulting expression. */
12109 return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p);
12111 return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
12115 /* This routine returns true if TYPE is a valid objc object type,
12116 suitable for messaging; false otherwise. If 'accept_class' is
12117 'true', then a Class object is considered valid for messaging and
12118 'true' is returned if 'type' refers to a Class. If 'accept_class'
12119 is 'false', then a Class object is not considered valid for
12120 messaging and 'false' is returned in that case. */
12123 objc_type_valid_for_messaging (tree type, bool accept_classes)
12125 if (!POINTER_TYPE_P (type))
12128 /* Remove the pointer indirection; don't remove more than one
12129 otherwise we'd consider "NSObject **" a valid type for messaging,
12131 type = TREE_TYPE (type);
12133 if (TREE_CODE (type) != RECORD_TYPE)
12136 if (objc_is_object_id (type))
12139 if (accept_classes && objc_is_class_id (type))
12142 if (TYPE_HAS_OBJC_INFO (type))
12148 /* Begin code generation for fast enumeration (foreach) ... */
12152 struct __objcFastEnumerationState
12154 unsigned long state;
12156 unsigned long *mutationsPtr;
12157 unsigned long extra[5];
12160 Confusingly enough, NSFastEnumeration is then defined by libraries
12161 to be the same structure.
12165 build_fast_enumeration_state_template (void)
12167 tree decls, *chain = NULL;
12170 objc_fast_enumeration_state_template = objc_start_struct (get_identifier
12171 (TAG_FAST_ENUMERATION_STATE));
12173 /* unsigned long state; */
12174 decls = add_field_decl (long_unsigned_type_node, "state", &chain);
12176 /* id *itemsPtr; */
12177 add_field_decl (build_pointer_type (objc_object_type),
12178 "itemsPtr", &chain);
12180 /* unsigned long *mutationsPtr; */
12181 add_field_decl (build_pointer_type (long_unsigned_type_node),
12182 "mutationsPtr", &chain);
12184 /* unsigned long extra[5]; */
12185 add_field_decl (build_sized_array_type (long_unsigned_type_node, 5),
12189 objc_finish_struct (objc_fast_enumeration_state_template, decls);
12193 'objc_finish_foreach_loop()' generates the code for an Objective-C
12194 foreach loop. The 'location' argument is the location of the 'for'
12195 that starts the loop. The 'object_expression' is the expression of
12196 the 'object' that iterates; the 'collection_expression' is the
12197 expression of the collection that we iterate over (we need to make
12198 sure we evaluate this only once); the 'for_body' is the set of
12199 statements to be executed in each iteration; 'break_label' and
12200 'continue_label' are the break and continue labels which we need to
12201 emit since the <statements> may be jumping to 'break_label' (if they
12202 contain 'break') or to 'continue_label' (if they contain
12207 for (<object expression> in <collection expression>)
12210 which is compiled into the following blurb:
12213 id __objc_foreach_collection;
12214 __objc_fast_enumeration_state __objc_foreach_enum_state;
12215 unsigned long __objc_foreach_batchsize;
12216 id __objc_foreach_items[16];
12217 __objc_foreach_collection = <collection expression>;
12218 __objc_foreach_enum_state = { 0 };
12219 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
12221 if (__objc_foreach_batchsize == 0)
12222 <object expression> = nil;
12225 unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr;
12228 unsigned long __objc_foreach_index;
12229 __objc_foreach_index = 0;
12232 if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>);
12233 <object expression> = enumState.itemsPtr[__objc_foreach_index];
12234 <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label]
12237 __objc_foreach_index++;
12238 if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object;
12239 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
12241 if (__objc_foreach_batchsize != 0) goto next_batch;
12242 <object expression> = nil;
12247 'statements' may contain a 'continue' or 'break' instruction, which
12248 the user expects to 'continue' or 'break' the entire foreach loop.
12249 We are provided the labels that 'break' and 'continue' jump to, so
12250 we place them where we want them to jump to when they pick them.
12252 Optimization TODO: we could cache the IMP of
12253 countByEnumeratingWithState:objects:count:.
12256 /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */
12257 /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */
12259 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
12260 #include "tree-pretty-print.h"
12264 objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body,
12265 tree break_label, tree continue_label)
12267 /* A tree representing the __objcFastEnumerationState struct type,
12268 or NSFastEnumerationState struct, whatever we are using. */
12269 tree objc_fast_enumeration_state_type;
12271 /* The trees representing the declarations of each of the local variables. */
12272 tree objc_foreach_collection_decl;
12273 tree objc_foreach_enum_state_decl;
12274 tree objc_foreach_items_decl;
12275 tree objc_foreach_batchsize_decl;
12276 tree objc_foreach_mutations_pointer_decl;
12277 tree objc_foreach_index_decl;
12279 /* A tree representing the selector countByEnumeratingWithState:objects:count:. */
12280 tree selector_name;
12282 /* A tree representing the local bind. */
12285 /* A tree representing the external 'if (__objc_foreach_batchsize)' */
12288 /* A tree representing the 'else' part of 'first_if' */
12291 /* A tree representing the 'next_batch' label. */
12292 tree next_batch_label_decl;
12294 /* A tree representing the binding after the 'next_batch' label. */
12295 tree next_batch_bind;
12297 /* A tree representing the 'next_object' label. */
12298 tree next_object_label_decl;
12300 /* Temporary variables. */
12304 if (flag_objc1_only)
12305 error_at (location, "fast enumeration is not available in Objective-C 1.0");
12307 if (object_expression == error_mark_node)
12310 if (collection_expression == error_mark_node)
12313 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression), true))
12315 error ("iterating variable in fast enumeration is not an object");
12319 if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression), true))
12321 error ("collection in fast enumeration is not an object");
12325 /* TODO: Check that object_expression is either a variable
12326 declaration, or an lvalue. */
12328 /* This kludge is an idea from apple. We use the
12329 __objcFastEnumerationState struct implicitly defined by the
12330 compiler, unless a NSFastEnumerationState struct has been defined
12331 (by a Foundation library such as GNUstep Base) in which case, we
12334 objc_fast_enumeration_state_type = objc_fast_enumeration_state_template;
12336 tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState"));
12338 if (objc_NSFastEnumeration_type)
12340 /* TODO: We really need to check that
12341 objc_NSFastEnumeration_type is the same as ours! */
12342 if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL)
12344 /* If it's a typedef, use the original type. */
12345 if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type))
12346 objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type);
12348 objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type);
12354 /* Done by c-parser.c. */
12357 /* Done by c-parser.c. */
12359 /* id __objc_foreach_collection */
12360 objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection");
12362 /* __objcFastEnumerationState __objc_foreach_enum_state; */
12363 objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state");
12364 TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl;
12366 /* id __objc_foreach_items[16]; */
12367 objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items");
12368 TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl;
12370 /* unsigned long __objc_foreach_batchsize; */
12371 objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize");
12372 TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl;
12374 /* Generate the local variable binding. */
12375 bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL);
12376 SET_EXPR_LOCATION (bind, location);
12377 TREE_SIDE_EFFECTS (bind) = 1;
12379 /* __objc_foreach_collection = <collection expression>; */
12380 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression);
12381 SET_EXPR_LOCATION (t, location);
12382 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
12384 /* __objc_foreach_enum_state.state = 0; */
12385 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
12386 get_identifier ("state")),
12387 build_int_cst (long_unsigned_type_node, 0));
12388 SET_EXPR_LOCATION (t, location);
12389 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
12391 /* __objc_foreach_enum_state.itemsPtr = NULL; */
12392 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
12393 get_identifier ("itemsPtr")),
12394 null_pointer_node);
12395 SET_EXPR_LOCATION (t, location);
12396 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
12398 /* __objc_foreach_enum_state.mutationsPtr = NULL; */
12399 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
12400 get_identifier ("mutationsPtr")),
12401 null_pointer_node);
12402 SET_EXPR_LOCATION (t, location);
12403 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
12405 /* __objc_foreach_enum_state.extra[0] = 0; */
12406 /* __objc_foreach_enum_state.extra[1] = 0; */
12407 /* __objc_foreach_enum_state.extra[2] = 0; */
12408 /* __objc_foreach_enum_state.extra[3] = 0; */
12409 /* __objc_foreach_enum_state.extra[4] = 0; */
12410 for (i = 0; i < 5 ; i++)
12412 t = build2 (MODIFY_EXPR, void_type_node,
12413 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
12414 get_identifier ("extra")),
12415 build_int_cst (NULL_TREE, i)),
12416 build_int_cst (long_unsigned_type_node, 0));
12417 SET_EXPR_LOCATION (t, location);
12418 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
12421 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
12422 selector_name = get_identifier ("countByEnumeratingWithState:objects:count:");
12424 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
12426 tree_cons /* &__objc_foreach_enum_state */
12427 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
12428 tree_cons /* __objc_foreach_items */
12429 (NULL_TREE, objc_foreach_items_decl,
12431 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
12433 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
12435 struct c_expr array;
12436 array.value = objc_foreach_items_decl;
12437 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
12439 tree_cons /* &__objc_foreach_enum_state */
12440 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
12441 tree_cons /* __objc_foreach_items */
12442 (NULL_TREE, default_function_array_conversion (location, array).value,
12444 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
12447 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
12448 convert (long_unsigned_type_node, t));
12449 SET_EXPR_LOCATION (t, location);
12450 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
12452 /* if (__objc_foreach_batchsize == 0) */
12453 first_if = build3 (COND_EXPR, void_type_node,
12456 (c_common_truthvalue_conversion
12458 build_binary_op (location,
12460 objc_foreach_batchsize_decl,
12461 build_int_cst (long_unsigned_type_node, 0), 1)),
12463 /* Then block (we fill it in later). */
12465 /* Else block (we fill it in later). */
12467 SET_EXPR_LOCATION (first_if, location);
12468 append_to_statement_list (first_if, &BIND_EXPR_BODY (bind));
12470 /* then <object expression> = nil; */
12471 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
12472 SET_EXPR_LOCATION (t, location);
12473 COND_EXPR_THEN (first_if) = t;
12475 /* Now we build the 'else' part of the if; once we finish building
12476 it, we attach it to first_if as the 'else' part. */
12481 /* unsigned long __objc_foreach_mutations_pointer; */
12482 objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer");
12484 /* Generate the local variable binding. */
12485 first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL);
12486 SET_EXPR_LOCATION (first_else, location);
12487 TREE_SIDE_EFFECTS (first_else) = 1;
12489 /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */
12490 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl,
12491 build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
12492 get_identifier ("mutationsPtr")),
12494 SET_EXPR_LOCATION (t, location);
12495 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
12498 next_batch_label_decl = create_artificial_label (location);
12499 t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl);
12500 SET_EXPR_LOCATION (t, location);
12501 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
12505 /* unsigned long __objc_foreach_index; */
12506 objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index");
12508 /* Generate the local variable binding. */
12509 next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL);
12510 SET_EXPR_LOCATION (next_batch_bind, location);
12511 TREE_SIDE_EFFECTS (next_batch_bind) = 1;
12512 append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else));
12514 /* __objc_foreach_index = 0; */
12515 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
12516 build_int_cst (long_unsigned_type_node, 0));
12517 SET_EXPR_LOCATION (t, location);
12518 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12521 next_object_label_decl = create_artificial_label (location);
12522 t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl);
12523 SET_EXPR_LOCATION (t, location);
12524 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12526 /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */
12527 t = build3 (COND_EXPR, void_type_node,
12530 (c_common_truthvalue_conversion
12535 objc_foreach_mutations_pointer_decl,
12536 build_indirect_ref (location,
12537 objc_build_component_ref (objc_foreach_enum_state_decl,
12538 get_identifier ("mutationsPtr")),
12539 RO_UNARY_STAR), 1)),
12542 build_function_call (input_location,
12543 objc_enumeration_mutation_decl,
12544 tree_cons (NULL, collection_expression, NULL)),
12547 SET_EXPR_LOCATION (t, location);
12548 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12550 /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */
12551 t = build2 (MODIFY_EXPR, void_type_node, object_expression,
12552 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
12553 get_identifier ("itemsPtr")),
12554 objc_foreach_index_decl));
12555 SET_EXPR_LOCATION (t, location);
12556 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12558 /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */
12559 append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind));
12561 /* continue_label: */
12562 if (continue_label)
12564 t = build1 (LABEL_EXPR, void_type_node, continue_label);
12565 SET_EXPR_LOCATION (t, location);
12566 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12569 /* __objc_foreach_index++; */
12570 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
12571 build_binary_op (location,
12573 objc_foreach_index_decl,
12574 build_int_cst (long_unsigned_type_node, 1), 1));
12575 SET_EXPR_LOCATION (t, location);
12576 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12578 /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */
12579 t = build3 (COND_EXPR, void_type_node,
12582 (c_common_truthvalue_conversion
12584 build_binary_op (location,
12586 objc_foreach_index_decl,
12587 objc_foreach_batchsize_decl, 1)),
12590 build1 (GOTO_EXPR, void_type_node, next_object_label_decl),
12593 SET_EXPR_LOCATION (t, location);
12594 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12596 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
12598 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
12600 tree_cons /* &__objc_foreach_enum_state */
12601 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
12602 tree_cons /* __objc_foreach_items */
12603 (NULL_TREE, objc_foreach_items_decl,
12605 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
12607 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
12609 struct c_expr array;
12610 array.value = objc_foreach_items_decl;
12611 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
12613 tree_cons /* &__objc_foreach_enum_state */
12614 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
12615 tree_cons /* __objc_foreach_items */
12616 (NULL_TREE, default_function_array_conversion (location, array).value,
12618 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
12621 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
12622 convert (long_unsigned_type_node, t));
12623 SET_EXPR_LOCATION (t, location);
12624 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
12628 /* if (__objc_foreach_batchsize != 0) goto next_batch; */
12629 t = build3 (COND_EXPR, void_type_node,
12632 (c_common_truthvalue_conversion
12634 build_binary_op (location,
12636 objc_foreach_batchsize_decl,
12637 build_int_cst (long_unsigned_type_node, 0), 1)),
12640 build1 (GOTO_EXPR, void_type_node, next_batch_label_decl),
12643 SET_EXPR_LOCATION (t, location);
12644 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
12646 /* <object expression> = nil; */
12647 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
12648 SET_EXPR_LOCATION (t, location);
12649 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
12654 t = build1 (LABEL_EXPR, void_type_node, break_label);
12655 SET_EXPR_LOCATION (t, location);
12656 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
12660 COND_EXPR_ELSE (first_if) = first_else;
12662 /* Do the whole thing. */
12665 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
12666 /* This will print to stderr the whole blurb generated by the
12667 compiler while compiling (assuming the compiler doesn't crash
12668 before getting here).
12670 debug_generic_stmt (bind);
12674 /* Done by c-parser.c */
12677 /* Return true if we have an NxString object pointer. */
12680 objc_string_ref_type_p (tree strp)
12683 if (!strp || TREE_CODE (strp) != POINTER_TYPE)
12686 tmv = TYPE_MAIN_VARIANT (TREE_TYPE (strp));
12687 tmv = OBJC_TYPE_NAME (tmv);
12689 && TREE_CODE (tmv) == IDENTIFIER_NODE
12690 && IDENTIFIER_POINTER (tmv)
12691 && !strncmp (IDENTIFIER_POINTER (tmv), "NSString", 8));
12694 /* At present the behavior of this is undefined and it does nothing. */
12696 objc_check_format_arg (tree ARG_UNUSED (format_arg),
12697 tree ARG_UNUSED (args_list))
12701 #include "gt-objc-objc-act.h"