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 are not linking against the C front-end
67 and so need to replicate the C tree-construction functions in some way. */
69 #define OBJCP_REMAP_FUNCTIONS
70 #include "objcp-decl.h"
73 /* This is the default way of generating a method name. */
74 /* This has the problem that "test_method:argument:" and
75 "test:method_argument:" will generate the same name
76 ("_i_Test__test_method_argument_" for an instance method of the
77 class "Test"), so you can't have them both in the same class!
78 Moreover, the demangling (going from
79 "_i_Test__test_method_argument" back to the original name) is
80 undefined because there are two correct ways of demangling the
82 #ifndef OBJC_GEN_METHOD_LABEL
83 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
86 sprintf ((BUF), "_%s_%s_%s_%s", \
87 ((IS_INST) ? "i" : "c"), \
89 ((CAT_NAME)? (CAT_NAME) : ""), \
91 for (temp = (BUF); *temp; temp++) \
92 if (*temp == ':') *temp = '_'; \
96 /* These need specifying. */
97 #ifndef OBJC_FORWARDING_STACK_OFFSET
98 #define OBJC_FORWARDING_STACK_OFFSET 0
101 #ifndef OBJC_FORWARDING_MIN_OFFSET
102 #define OBJC_FORWARDING_MIN_OFFSET 0
105 /* Set up for use of obstacks. */
109 /* This obstack is used to accumulate the encoding of a data type. */
110 static struct obstack util_obstack;
112 /* This points to the beginning of obstack contents, so we can free
113 the whole contents. */
116 /* The version identifies which language generation and runtime
117 the module (file) was compiled for, and is recorded in the
118 module descriptor. */
120 #define OBJC_VERSION (flag_next_runtime ? 6 : 8)
121 #define PROTOCOL_VERSION 2
123 /* (Decide if these can ever be validly changed.) */
124 #define OBJC_ENCODE_INLINE_DEFS 0
125 #define OBJC_ENCODE_DONT_INLINE_DEFS 1
127 /*** Private Interface (procedures) ***/
129 /* Used by compile_file. */
131 static void init_objc (void);
132 static void finish_objc (void);
134 /* Code generation. */
136 static tree objc_build_constructor (tree, VEC(constructor_elt,gc) *);
137 static tree build_objc_method_call (location_t, int, tree, tree, tree, tree);
138 static tree get_proto_encoding (tree);
139 static tree lookup_interface (tree);
140 static tree objc_add_static_instance (tree, tree);
142 static tree start_class (enum tree_code, tree, tree, tree, tree);
143 static tree continue_class (tree);
144 static void finish_class (tree);
145 static void start_method_def (tree);
147 static void objc_start_function (tree, tree, tree, tree);
149 static void objc_start_function (tree, tree, tree, struct c_arg_info *);
151 static tree start_protocol (enum tree_code, tree, tree, tree);
152 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
153 static tree objc_add_method (tree, tree, int, bool);
154 static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree);
155 static tree build_ivar_reference (tree);
156 static tree is_ivar (tree, tree);
158 static void build_objc_exception_stuff (void);
159 static void build_next_objc_exception_stuff (void);
161 /* We only need the following for ObjC; ObjC++ will use C++'s definition
162 of DERIVED_FROM_P. */
164 static bool objc_derived_from_p (tree, tree);
165 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
169 static void objc_gen_property_data (tree, tree);
170 static void objc_synthesize_getter (tree, tree, tree);
171 static void objc_synthesize_setter (tree, tree, tree);
172 static char *objc_build_property_setter_name (tree);
173 static int match_proto_with_proto (tree, tree, int);
174 static tree lookup_property (tree, tree);
175 static tree lookup_property_in_list (tree, tree);
176 static tree lookup_property_in_protocol_list (tree, tree);
177 static void build_objc_property_accessor_helpers (void);
179 static void objc_xref_basetypes (tree, tree);
181 static void build_class_template (void);
182 static void build_selector_template (void);
183 static void build_category_template (void);
184 static void build_super_template (void);
185 static tree build_protocol_initializer (tree, tree, tree, tree, tree);
186 static tree get_class_ivars (tree, bool);
187 static tree generate_protocol_list (tree);
188 static void build_protocol_reference (tree);
190 static void build_fast_enumeration_state_template (void);
193 static void objc_generate_cxx_cdtors (void);
197 static void objc_decl_method_attributes (tree*, tree, int);
198 static tree build_keyword_selector (tree);
199 static const char *synth_id_with_class_suffix (const char *, tree);
201 /* Hash tables to manage the global pool of method prototypes. */
203 hash *nst_method_hash_list = 0;
204 hash *cls_method_hash_list = 0;
206 /* Hash tables to manage the global pool of class names. */
208 hash *cls_name_hash_list = 0;
209 hash *als_name_hash_list = 0;
211 static void hash_class_name_enter (hash *, tree, tree);
212 static hash hash_class_name_lookup (hash *, tree);
214 static hash hash_lookup (hash *, tree);
215 static tree lookup_method (tree, tree);
216 static tree lookup_method_static (tree, tree, int);
218 static tree add_class (tree, tree);
219 static void add_category (tree, tree);
220 static inline tree lookup_category (tree, tree);
224 class_names, /* class, category, protocol, module names */
225 meth_var_names, /* method and variable names */
226 meth_var_types /* method and variable type descriptors */
229 static tree add_objc_string (tree, enum string_section);
230 static void build_selector_table_decl (void);
234 static tree lookup_protocol (tree, bool);
235 static tree lookup_and_install_protocols (tree);
239 static void encode_type_qualifiers (tree);
240 static void encode_type (tree, int, int);
241 static void encode_field_decl (tree, int, int);
244 static void really_start_method (tree, tree);
246 static void really_start_method (tree, struct c_arg_info *);
248 static int comp_proto_with_proto (tree, tree, int);
249 static tree get_arg_type_list (tree, int, int);
250 static tree objc_decay_parm_type (tree);
251 static void objc_push_parm (tree);
253 static tree objc_get_parm_info (int);
255 static struct c_arg_info *objc_get_parm_info (int);
258 /* Utilities for debugging and error diagnostics. */
260 static char *gen_type_name (tree);
261 static char *gen_type_name_0 (tree);
262 static char *gen_method_decl (tree);
263 static char *gen_declaration (tree);
265 /* Everything else. */
267 static tree create_field_decl (tree, const char *);
268 static void add_class_reference (tree);
269 static void build_protocol_template (void);
270 static tree encode_method_prototype (tree);
271 static void generate_classref_translation_entry (tree);
272 static void handle_class_ref (tree);
273 static void generate_struct_by_value_array (void)
275 static void mark_referenced_methods (void);
276 static void generate_objc_image_info (void);
277 static bool objc_type_valid_for_messaging (tree type, bool allow_classes);
279 /*** Private Interface (data) ***/
281 /* Reserved tag definitions. */
283 #define OBJECT_TYPEDEF_NAME "id"
284 #define CLASS_TYPEDEF_NAME "Class"
286 #define TAG_OBJECT "objc_object"
287 #define TAG_CLASS "objc_class"
288 #define TAG_SUPER "objc_super"
289 #define TAG_SELECTOR "objc_selector"
291 #define UTAG_CLASS "_objc_class"
292 #define UTAG_IVAR "_objc_ivar"
293 #define UTAG_IVAR_LIST "_objc_ivar_list"
294 #define UTAG_METHOD "_objc_method"
295 #define UTAG_METHOD_LIST "_objc_method_list"
296 #define UTAG_CATEGORY "_objc_category"
297 #define UTAG_MODULE "_objc_module"
298 #define UTAG_SYMTAB "_objc_symtab"
299 #define UTAG_SUPER "_objc_super"
300 #define UTAG_SELECTOR "_objc_selector"
302 #define UTAG_PROTOCOL "_objc_protocol"
303 #define UTAG_METHOD_PROTOTYPE "_objc_method_prototype"
304 #define UTAG_METHOD_PROTOTYPE_LIST "_objc__method_prototype_list"
306 /* Note that the string object global name is only needed for the
308 #define STRING_OBJECT_GLOBAL_FORMAT "_%sClassReference"
310 #define PROTOCOL_OBJECT_CLASS_NAME "Protocol"
312 #define TAG_ENUMERATION_MUTATION "objc_enumerationMutation"
313 #define TAG_FAST_ENUMERATION_STATE "__objcFastEnumerationState"
315 static const char *TAG_GETCLASS;
316 static const char *TAG_GETMETACLASS;
317 static const char *TAG_MSGSEND;
318 static const char *TAG_MSGSENDSUPER;
319 /* The NeXT Objective-C messenger may have two extra entry points, for use
320 when returning a structure. */
321 static const char *TAG_MSGSEND_STRET;
322 static const char *TAG_MSGSENDSUPER_STRET;
323 static const char *default_constant_string_class_name;
325 /* Runtime metadata flags. */
326 #define CLS_FACTORY 0x0001L
327 #define CLS_META 0x0002L
328 #define CLS_HAS_CXX_STRUCTORS 0x2000L
330 #define OBJC_MODIFIER_STATIC 0x00000001
331 #define OBJC_MODIFIER_FINAL 0x00000002
332 #define OBJC_MODIFIER_PUBLIC 0x00000004
333 #define OBJC_MODIFIER_PRIVATE 0x00000008
334 #define OBJC_MODIFIER_PROTECTED 0x00000010
335 #define OBJC_MODIFIER_NATIVE 0x00000020
336 #define OBJC_MODIFIER_SYNCHRONIZED 0x00000040
337 #define OBJC_MODIFIER_ABSTRACT 0x00000080
338 #define OBJC_MODIFIER_VOLATILE 0x00000100
339 #define OBJC_MODIFIER_TRANSIENT 0x00000200
340 #define OBJC_MODIFIER_NONE_SPECIFIED 0x80000000
342 /* NeXT-specific tags. */
344 #define TAG_MSGSEND_NONNIL "objc_msgSendNonNil"
345 #define TAG_MSGSEND_NONNIL_STRET "objc_msgSendNonNil_stret"
346 #define TAG_EXCEPTIONEXTRACT "objc_exception_extract"
347 #define TAG_EXCEPTIONTRYENTER "objc_exception_try_enter"
348 #define TAG_EXCEPTIONTRYEXIT "objc_exception_try_exit"
349 #define TAG_EXCEPTIONMATCH "objc_exception_match"
350 #define TAG_EXCEPTIONTHROW "objc_exception_throw"
351 #define TAG_SYNCENTER "objc_sync_enter"
352 #define TAG_SYNCEXIT "objc_sync_exit"
353 #define TAG_SETJMP "_setjmp"
354 #define UTAG_EXCDATA "_objc_exception_data"
356 #define TAG_ASSIGNIVAR "objc_assign_ivar"
357 #define TAG_ASSIGNGLOBAL "objc_assign_global"
358 #define TAG_ASSIGNSTRONGCAST "objc_assign_strongCast"
360 /* Branch entry points. All that matters here are the addresses;
361 functions with these names do not really exist in libobjc. */
363 #define TAG_MSGSEND_FAST "objc_msgSend_Fast"
364 #define TAG_ASSIGNIVAR_FAST "objc_assign_ivar_Fast"
366 #define TAG_CXX_CONSTRUCT ".cxx_construct"
367 #define TAG_CXX_DESTRUCT ".cxx_destruct"
369 /* GNU-specific tags. */
371 #define TAG_EXECCLASS "__objc_exec_class"
372 #define TAG_GNUINIT "__objc_gnu_init"
374 /* Flags for lookup_method_static(). */
376 /* Look for class methods. */
377 #define OBJC_LOOKUP_CLASS 1
378 /* Do not examine superclasses. */
379 #define OBJC_LOOKUP_NO_SUPER 2
380 /* Disable returning an instance method of a root class when a class
381 method can't be found. */
382 #define OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS 4
384 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
385 tree objc_global_trees[OCTI_MAX];
387 static void handle_impent (struct imp_entry *);
389 struct imp_entry *imp_list = 0;
390 int imp_count = 0; /* `@implementation' */
391 int cat_count = 0; /* `@category' */
393 objc_ivar_visibility_kind objc_ivar_visibility;
395 /* Use to generate method labels. */
396 static int method_slot = 0;
398 /* Flag to say whether methods in a protocol are optional or
400 static bool objc_method_optional_flag = false;
402 static int objc_collecting_ivars = 0;
406 static char *errbuf; /* Buffer for error diagnostics */
408 /* Data imported from tree.c. */
410 extern enum debug_info_type write_symbols;
413 static int flag_typed_selectors;
415 /* Store all constructed constant strings in a hash table so that
416 they get uniqued properly. */
418 struct GTY(()) string_descriptor {
419 /* The literal argument . */
422 /* The resulting constant string. */
426 static GTY((param_is (struct string_descriptor))) htab_t string_htab;
428 FILE *gen_declaration_file;
430 /* Tells "encode_pointer/encode_aggregate" whether we are generating
431 type descriptors for instance variables (as opposed to methods).
432 Type descriptors for instance variables contain more information
433 than methods (for static typing and embedded structures). */
435 static int generating_instance_variables = 0;
437 /* For building an objc struct. These may not be used when this file
438 is compiled as part of obj-c++. */
440 static bool objc_building_struct;
441 static struct c_struct_parse_info *objc_struct_info ATTRIBUTE_UNUSED;
443 /* Start building a struct for objc. */
446 objc_start_struct (tree name)
448 gcc_assert (!objc_building_struct);
449 objc_building_struct = true;
450 return start_struct (input_location, RECORD_TYPE, name, &objc_struct_info);
453 /* Finish building a struct for objc. */
456 objc_finish_struct (tree type, tree fieldlist)
458 gcc_assert (objc_building_struct);
459 objc_building_struct = false;
460 return finish_struct (input_location, type, fieldlist, NULL_TREE,
465 build_sized_array_type (tree base_type, int size)
467 tree index_type = build_index_type (build_int_cst (NULL_TREE, size - 1));
468 return build_array_type (base_type, index_type);
472 add_field_decl (tree type, const char *name, tree **chain)
474 tree field = create_field_decl (type, name);
478 *chain = &DECL_CHAIN (field);
483 /* Create a temporary variable of type 'type'. If 'name' is set, uses
484 the specified name, else use no name. Returns the declaration of
485 the type. The 'name' is mostly useful for debugging.
488 objc_create_temporary_var (tree type, const char *name)
494 decl = build_decl (input_location,
495 VAR_DECL, get_identifier (name), type);
499 decl = build_decl (input_location,
500 VAR_DECL, NULL_TREE, type);
502 TREE_USED (decl) = 1;
503 DECL_ARTIFICIAL (decl) = 1;
504 DECL_IGNORED_P (decl) = 1;
505 DECL_CONTEXT (decl) = current_function_decl;
510 /* Some platforms pass small structures through registers versus
511 through an invisible pointer. Determine at what size structure is
512 the transition point between the two possibilities. */
515 generate_struct_by_value_array (void)
520 int aggregate_in_mem[32];
523 /* Presumably no platform passes 32 byte structures in a register. */
524 for (i = 1; i < 32; i++)
529 /* Create an unnamed struct that has `i' character components */
530 type = objc_start_struct (NULL_TREE);
532 strcpy (buffer, "c1");
533 decls = add_field_decl (char_type_node, buffer, &chain);
535 for (j = 1; j < i; j++)
537 sprintf (buffer, "c%d", j + 1);
538 add_field_decl (char_type_node, buffer, &chain);
540 objc_finish_struct (type, decls);
542 aggregate_in_mem[i] = aggregate_value_p (type, 0);
543 if (!aggregate_in_mem[i])
547 /* We found some structures that are returned in registers instead of memory
548 so output the necessary data. */
551 for (i = 31; i >= 0; i--)
552 if (!aggregate_in_mem[i])
554 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n\n", i);
556 /* The first member of the structure is always 0 because we don't handle
557 structures with 0 members */
558 printf ("static int struct_forward_array[] = {\n 0");
560 for (j = 1; j <= i; j++)
561 printf (", %d", aggregate_in_mem[j]);
572 if (cxx_init () == false)
574 if (c_objc_common_init () == false)
578 /* If gen_declaration desired, open the output file. */
579 if (flag_gen_declaration)
581 register char * const dumpname = concat (dump_base_name, ".decl", NULL);
582 gen_declaration_file = fopen (dumpname, "w");
583 if (gen_declaration_file == 0)
584 fatal_error ("can%'t open %s: %m", dumpname);
588 if (flag_next_runtime)
590 TAG_GETCLASS = "objc_getClass";
591 TAG_GETMETACLASS = "objc_getMetaClass";
592 TAG_MSGSEND = "objc_msgSend";
593 TAG_MSGSENDSUPER = "objc_msgSendSuper";
594 TAG_MSGSEND_STRET = "objc_msgSend_stret";
595 TAG_MSGSENDSUPER_STRET = "objc_msgSendSuper_stret";
596 default_constant_string_class_name = "NSConstantString";
600 TAG_GETCLASS = "objc_get_class";
601 TAG_GETMETACLASS = "objc_get_meta_class";
602 TAG_MSGSEND = "objc_msg_lookup";
603 TAG_MSGSENDSUPER = "objc_msg_lookup_super";
604 /* GNU runtime does not provide special functions to support
605 structure-returning methods. */
606 default_constant_string_class_name = "NXConstantString";
607 flag_typed_selectors = 1;
608 /* GNU runtime does not need the compiler to change code
609 in order to do GC. */
612 warning_at (0, 0, "%<-fobjc-gc%> is ignored for %<-fgnu-runtime%>");
619 if (print_struct_values && !flag_compare_debug)
620 generate_struct_by_value_array ();
625 /* This is called automatically (at the very end of compilation) by
626 c_write_global_declarations and cp_write_global_declarations. */
628 objc_write_global_declarations (void)
630 mark_referenced_methods ();
632 /* Finalize Objective-C runtime data. */
635 if (gen_declaration_file)
636 fclose (gen_declaration_file);
639 /* Return the first occurrence of a method declaration corresponding
640 to sel_name in rproto_list. Search rproto_list recursively.
641 If is_class is 0, search for instance methods, otherwise for class
644 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
649 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
651 p = TREE_VALUE (rproto);
654 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
656 /* First, search the @required protocol methods. */
658 m = lookup_method (PROTOCOL_CLS_METHODS (p), sel_name);
660 m = lookup_method (PROTOCOL_NST_METHODS (p), sel_name);
665 /* If still not found, search the @optional protocol methods. */
667 m = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (p), sel_name);
669 m = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (p), sel_name);
674 /* If still not found, search the attached protocols. */
675 if (PROTOCOL_LIST (p))
676 m = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
683 ; /* An identifier...if we could not find a protocol. */
691 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
695 /* Make sure the protocol is supported by the object on the rhs. */
696 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
699 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
701 p = TREE_VALUE (rproto);
703 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
708 else if (PROTOCOL_LIST (p))
709 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
718 ; /* An identifier...if we could not find a protocol. */
725 objc_start_class_interface (tree klass, tree super_class,
726 tree protos, tree attributes)
728 if (flag_objc1_only && attributes)
729 error_at (input_location, "class attributes are not available in Objective-C 1.0");
731 objc_interface_context
733 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos, attributes);
734 objc_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
738 objc_start_category_interface (tree klass, tree categ,
739 tree protos, tree attributes)
744 error_at (input_location, "category attributes are not available in Objective-C 1.0");
746 warning_at (input_location, OPT_Wattributes,
747 "category attributes are not available in this version"
748 " of the compiler, (ignored)");
750 objc_interface_context
751 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos, NULL_TREE);
753 = continue_class (objc_interface_context);
757 objc_start_protocol (tree name, tree protos, tree attributes)
759 if (flag_objc1_only && attributes)
760 error_at (input_location, "protocol attributes are not available in Objective-C 1.0");
762 objc_interface_context
763 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos, attributes);
764 objc_method_optional_flag = false;
768 objc_continue_interface (void)
771 = continue_class (objc_interface_context);
775 objc_finish_interface (void)
777 finish_class (objc_interface_context);
778 objc_interface_context = NULL_TREE;
779 objc_method_optional_flag = false;
783 objc_start_class_implementation (tree klass, tree super_class)
785 objc_implementation_context
787 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE,
789 objc_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
793 objc_start_category_implementation (tree klass, tree categ)
795 objc_implementation_context
796 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE,
799 = continue_class (objc_implementation_context);
803 objc_continue_implementation (void)
806 = continue_class (objc_implementation_context);
810 objc_finish_implementation (void)
813 if (flag_objc_call_cxx_cdtors)
814 objc_generate_cxx_cdtors ();
817 if (objc_implementation_context)
819 finish_class (objc_implementation_context);
820 objc_ivar_chain = NULL_TREE;
821 objc_implementation_context = NULL_TREE;
824 warning (0, "%<@end%> must appear in an @implementation context");
828 objc_set_visibility (objc_ivar_visibility_kind visibility)
830 if (visibility == OBJC_IVAR_VIS_PACKAGE)
833 error ("%<@package%> is not available in Objective-C 1.0");
835 warning (0, "%<@package%> presently has the same effect as %<@public%>");
837 objc_ivar_visibility = visibility;
841 objc_set_method_opt (bool optional)
844 error_at (input_location, "@optional/@required are not available in Objective-C 1.0");
846 objc_method_optional_flag = optional;
847 if (!objc_interface_context
848 || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
850 error ("@optional/@required is allowed in @protocol context only");
851 objc_method_optional_flag = false;
855 /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
858 lookup_property_in_list (tree chain, tree property)
861 for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x))
862 if (PROPERTY_NAME (x) == property)
867 /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
868 static tree lookup_property_in_protocol_list (tree rproto_list, tree property)
871 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
873 tree p = TREE_VALUE (rproto);
874 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
876 if ((x = lookup_property_in_list (p, property)))
878 if (PROTOCOL_LIST (p))
879 return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property);
883 ; /* An identifier...if we could not find a protocol. */
889 /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
890 chain of interface hierarchy. */
892 lookup_property (tree interface_type, tree property)
894 tree inter = interface_type;
898 if ((x = lookup_property_in_list (inter, property)))
900 /* Failing that, look for the property in each category of the class. */
902 while ((category = CLASS_CATEGORY_LIST (category)))
904 if ((x = lookup_property_in_list (category, property)))
907 /* When checking a category, also check the protocols
908 attached with the category itself. */
909 if (CLASS_PROTOCOL_LIST (category)
910 && (x = lookup_property_in_protocol_list
911 (CLASS_PROTOCOL_LIST (category), property)))
915 /* Failing to find in categories, look for property in protocol list. */
916 if (CLASS_PROTOCOL_LIST (inter)
917 && (x = lookup_property_in_protocol_list
918 (CLASS_PROTOCOL_LIST (inter), property)))
921 /* Failing that, climb up the inheritance hierarchy. */
922 inter = lookup_interface (CLASS_SUPER_NAME (inter));
927 /* This routine is called by the parser when a
928 @property... declaration is found. 'decl' is the declaration of
929 the property (type/identifier), and the other arguments represent
930 property attributes that may have been specified in the Objective-C
931 declaration. 'parsed_property_readonly' is 'true' if the attribute
932 'readonly' was specified, and 'false' if not; similarly for the
933 other bool parameters. 'parsed_property_getter_ident' is NULL_TREE
934 if the attribute 'getter' was not specified, and is the identifier
935 corresponding to the specified getter if it was; similarly for
936 'parsed_property_setter_ident'. */
938 objc_add_property_declaration (location_t location, tree decl,
939 bool parsed_property_readonly, bool parsed_property_readwrite,
940 bool parsed_property_assign, bool parsed_property_retain,
941 bool parsed_property_copy, bool parsed_property_nonatomic,
942 tree parsed_property_getter_ident, tree parsed_property_setter_ident)
946 /* 'property_readonly' and 'property_assign_semantics' are the final
947 attributes of the property after all parsed attributes have been
948 considered (eg, if we parsed no 'readonly' and no 'readwrite', ie
949 parsed_property_readonly = false and parsed_property_readwrite =
950 false, then property_readonly will be false because the default
952 bool property_readonly = false;
953 objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN;
956 error_at (input_location, "%<@property%> is not available in Objective-C 1.0");
958 if (parsed_property_readonly && parsed_property_readwrite)
960 error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute");
961 /* In case of conflicting attributes (here and below), after
962 producing an error, we pick one of the attributes and keep
964 property_readonly = false;
968 if (parsed_property_readonly)
969 property_readonly = true;
971 if (parsed_property_readwrite)
972 property_readonly = false;
975 if (parsed_property_readonly && parsed_property_setter_ident)
977 error_at (location, "%<readonly%> attribute conflicts with %<setter%> attribute");
978 property_readonly = false;
981 if (parsed_property_assign && parsed_property_retain)
983 error_at (location, "%<assign%> attribute conflicts with %<retain%> attribute");
984 property_assign_semantics = OBJC_PROPERTY_RETAIN;
986 else if (parsed_property_assign && parsed_property_copy)
988 error_at (location, "%<assign%> attribute conflicts with %<copy%> attribute");
989 property_assign_semantics = OBJC_PROPERTY_COPY;
991 else if (parsed_property_retain && parsed_property_copy)
993 error_at (location, "%<retain%> attribute conflicts with %<copy%> attribute");
994 property_assign_semantics = OBJC_PROPERTY_COPY;
998 if (parsed_property_assign)
999 property_assign_semantics = OBJC_PROPERTY_ASSIGN;
1001 if (parsed_property_retain)
1002 property_assign_semantics = OBJC_PROPERTY_RETAIN;
1004 if (parsed_property_copy)
1005 property_assign_semantics = OBJC_PROPERTY_COPY;
1008 if (!objc_interface_context)
1010 error_at (location, "property declaration not in @interface or @protocol context");
1014 /* At this point we know that we are either in an interface, a
1015 category, or a protocol. */
1017 /* We expect a FIELD_DECL from the parser. Make sure we didn't get
1018 something else, as that would confuse the checks below. */
1019 if (TREE_CODE (decl) != FIELD_DECL)
1021 error_at (location, "invalid property declaration");
1025 /* Do some spot-checks for the most obvious invalid types. */
1027 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1029 error_at (location, "property can not be an array");
1033 /* The C++/ObjC++ parser seems to reject the ':' for a bitfield when
1034 parsing, while the C/ObjC parser accepts it and gives us a
1035 FIELD_DECL with a DECL_INITIAL set. So we use the DECL_INITIAL
1036 to check for a bitfield when doing ObjC. */
1038 if (DECL_INITIAL (decl))
1040 /* A @property is not an actual variable, but it is a way to
1041 describe a pair of accessor methods, so its type (which is
1042 the type of the return value of the getter and the first
1043 argument of the setter) can't be a bitfield (as return values
1044 and arguments of functions can not be bitfields). The
1045 underlying instance variable could be a bitfield, but that is
1046 a different matter. */
1047 error_at (location, "property can not be a bit-field");
1052 /* TODO: Check that the property type is an Objective-C object or a
1055 /* Implement -Wproperty-assign-default (which is enabled by default). */
1056 if (warn_property_assign_default
1057 /* If garbage collection is not being used, then 'assign' is
1058 valid for objects (and typically used for delegates) but it
1059 is wrong in most cases (since most objects need to be
1060 retained or copied in setters). Warn users when 'assign' is
1062 && property_assign_semantics == OBJC_PROPERTY_ASSIGN
1063 /* Read-only properties are never assigned, so the assignment
1064 semantics do not matter in that case. */
1065 && !property_readonly
1068 /* Please note that it would make sense to default to 'assign'
1069 for non-{Objective-C objects}, and to 'retain' for
1070 Objective-C objects. But that would break compatibility with
1072 if (!parsed_property_assign && !parsed_property_retain && !parsed_property_copy)
1074 /* Use 'false' so we do not warn for Class objects. */
1075 if (objc_type_valid_for_messaging (TREE_TYPE (decl), false))
1077 warning_at (location,
1079 "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>",
1082 "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it");
1087 if (property_assign_semantics == OBJC_PROPERTY_RETAIN
1088 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
1089 error_at (location, "%<retain%> attribute is only valid for Objective-C objects");
1091 if (property_assign_semantics == OBJC_PROPERTY_COPY
1092 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
1093 error_at (location, "%<copy%> attribute is only valid for Objective-C objects");
1095 /* Now determine the final property getter and setter names. They
1096 will be stored in the PROPERTY_DECL, from which they'll always be
1097 extracted and used. */
1099 /* Adjust, or fill in, setter and getter names. We overwrite the
1100 parsed_property_setter_ident and parsed_property_getter_ident
1101 with the final setter and getter identifiers that will be
1103 if (parsed_property_setter_ident)
1105 /* The setter should be terminated by ':', but the parser only
1106 gives us an identifier without ':'. So, we need to add ':'
1108 const char *parsed_setter = IDENTIFIER_POINTER (parsed_property_setter_ident);
1109 size_t length = strlen (parsed_setter);
1110 char *final_setter = (char *)alloca (length + 2);
1112 sprintf (final_setter, "%s:", parsed_setter);
1113 parsed_property_setter_ident = get_identifier (final_setter);
1117 if (!property_readonly)
1118 parsed_property_setter_ident = get_identifier (objc_build_property_setter_name
1119 (DECL_NAME (decl)));
1122 if (!parsed_property_getter_ident)
1123 parsed_property_getter_ident = DECL_NAME (decl);
1125 /* Check for duplicate property declarations. We first check the
1126 immediate context for a property with the same name. Any such
1127 declarations are an error. */
1128 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
1130 if (PROPERTY_NAME (x) == DECL_NAME (decl))
1132 location_t original_location = DECL_SOURCE_LOCATION (x);
1134 error_at (location, "redeclaration of property %qD", decl);
1136 if (original_location != UNKNOWN_LOCATION)
1137 inform (original_location, "originally specified here");
1142 /* We now need to check for existing property declarations (in the
1143 superclass, other categories or protocols) and check that the new
1144 declaration is not in conflict with existing ones. */
1146 /* Search for a previous, existing declaration of a property with
1147 the same name in superclasses, protocols etc. If one is found,
1148 it will be in the 'x' variable. */
1151 /* Note that, for simplicity, the following may search again the
1152 local context. That's Ok as nothing will be found (else we'd
1153 have thrown an error above); it's only a little inefficient, but
1154 the code is simpler. */
1155 switch (TREE_CODE (objc_interface_context))
1157 case CLASS_INTERFACE_TYPE:
1158 /* Look up the property in the current @interface (which will
1159 find nothing), then its protocols and categories and
1161 x = lookup_property (objc_interface_context, DECL_NAME (decl));
1163 case CATEGORY_INTERFACE_TYPE:
1164 /* Look up the property in the main @interface, then protocols
1165 and categories (one of them is ours, and will find nothing)
1166 and superclasses. */
1167 x = lookup_property (lookup_interface (CLASS_NAME (objc_interface_context)),
1170 case PROTOCOL_INTERFACE_TYPE:
1171 /* Looks up the property in any protocols attached to the
1172 current protocol. */
1173 if (PROTOCOL_LIST (objc_interface_context))
1175 x = lookup_property_in_protocol_list (PROTOCOL_LIST (objc_interface_context),
1185 /* An existing property was found; check that it has the same
1186 types, or it is compatible. */
1187 location_t original_location = DECL_SOURCE_LOCATION (x);
1189 if (PROPERTY_NONATOMIC (x) != parsed_property_nonatomic)
1191 warning_at (location, 0,
1192 "'nonatomic' attribute of property %qD conflicts with previous declaration", decl);
1194 if (original_location != UNKNOWN_LOCATION)
1195 inform (original_location, "originally specified here");
1199 if (PROPERTY_GETTER_NAME (x) != parsed_property_getter_ident)
1201 warning_at (location, 0,
1202 "'getter' attribute of property %qD conflicts with previous declaration", decl);
1204 if (original_location != UNKNOWN_LOCATION)
1205 inform (original_location, "originally specified here");
1209 /* We can only compare the setter names if both the old and new property have a setter. */
1210 if (!property_readonly && !PROPERTY_READONLY(x))
1212 if (PROPERTY_SETTER_NAME (x) != parsed_property_setter_ident)
1214 warning_at (location, 0,
1215 "'setter' attribute of property %qD conflicts with previous declaration", decl);
1217 if (original_location != UNKNOWN_LOCATION)
1218 inform (original_location, "originally specified here");
1223 if (PROPERTY_ASSIGN_SEMANTICS (x) != property_assign_semantics)
1225 warning_at (location, 0,
1226 "assign semantics attributes of property %qD conflict with previous declaration", decl);
1228 if (original_location != UNKNOWN_LOCATION)
1229 inform (original_location, "originally specified here");
1233 /* It's ok to have a readonly property that becomes a readwrite, but not vice versa. */
1234 if (PROPERTY_READONLY (x) == 0 && property_readonly == 1)
1236 warning_at (location, 0,
1237 "'readonly' attribute of property %qD conflicts with previous declaration", decl);
1239 if (original_location != UNKNOWN_LOCATION)
1240 inform (original_location, "originally specified here");
1244 /* We now check that the new and old property declarations have
1245 the same types (or compatible one). In the Objective-C
1246 tradition of loose type checking, we do type-checking but
1247 only generate warnings (not errors) if they do not match.
1248 For non-readonly properties, the types must match exactly;
1249 for readonly properties, it is allowed to use a "more
1250 specialized" type in the new property declaration. Eg, the
1251 superclass has a getter returning (NSArray *) and the
1252 subclass a getter returning (NSMutableArray *). The object's
1253 getter returns an (NSMutableArray *); but if you cast the
1254 object to the superclass, which is allowed, you'd still
1255 expect the getter to return an (NSArray *), which works since
1256 an (NSMutableArray *) is an (NSArray *) too. So, the set of
1257 objects belonging to the type of the new @property should be
1258 a subset of the set of objects belonging to the type of the
1259 old @property. This is what "specialization" means. And the
1260 reason it only applies to readonly properties is that for a
1261 readwrite property the setter would have the opposite
1262 requirement - ie that the superclass type is more specialized
1263 then the subclass one; hence the only way to satisfy both
1264 constraints is that the types match. */
1266 /* If the types are not the same in the C sense, we warn ... */
1267 if (!comptypes (TREE_TYPE (x), TREE_TYPE (decl))
1268 /* ... unless the property is readonly, in which case we
1269 allow a new, more specialized, declaration. */
1270 && (!property_readonly
1271 || !objc_compare_types (TREE_TYPE (x),
1272 TREE_TYPE (decl), -5, NULL_TREE)))
1274 warning_at (location, 0,
1275 "type of property %qD conflicts with previous declaration", decl);
1276 if (original_location != UNKNOWN_LOCATION)
1277 inform (original_location, "originally specified here");
1282 /* Create a PROPERTY_DECL node. */
1283 property_decl = make_node (PROPERTY_DECL);
1285 /* Copy the basic information from the original decl. */
1286 TREE_TYPE (property_decl) = TREE_TYPE (decl);
1287 DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl);
1288 TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl);
1290 /* Add property-specific information. */
1291 PROPERTY_NAME (property_decl) = DECL_NAME (decl);
1292 PROPERTY_GETTER_NAME (property_decl) = parsed_property_getter_ident;
1293 PROPERTY_SETTER_NAME (property_decl) = parsed_property_setter_ident;
1294 PROPERTY_READONLY (property_decl) = property_readonly;
1295 PROPERTY_NONATOMIC (property_decl) = parsed_property_nonatomic;
1296 PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics;
1297 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1298 PROPERTY_DYNAMIC (property_decl) = 0;
1300 /* Note that PROPERTY_GETTER_NAME is always set for all
1301 PROPERTY_DECLs, and PROPERTY_SETTER_NAME is always set for all
1302 PROPERTY_DECLs where PROPERTY_READONLY == 0. Any time we deal
1303 with a getter or setter, we should get the PROPERTY_DECL and use
1304 PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know the correct
1307 /* Add the PROPERTY_DECL to the list of properties for the class. */
1308 TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context);
1309 CLASS_PROPERTY_DECL (objc_interface_context) = property_decl;
1312 /* This is a subroutine of objc_maybe_build_component_ref. Search the
1313 list of methods in the interface (and, failing that, the local list
1314 in the implementation, and failing that, the protocol list)
1315 provided for a 'setter' or 'getter' for 'component' with default
1316 names (ie, if 'component' is "name", then search for "name" and
1317 "setName:"). If any is found, then create an artificial property
1318 that uses them. Return NULL_TREE if 'getter' or 'setter' could not
1321 maybe_make_artificial_property_decl (tree interface, tree implementation,
1322 tree protocol_list, tree component, bool is_class)
1324 tree getter_name = component;
1325 tree setter_name = get_identifier (objc_build_property_setter_name (component));
1326 tree getter = NULL_TREE;
1327 tree setter = NULL_TREE;
1329 /* First, check the @interface and all superclasses. */
1334 /* Using instance methods of the root class as accessors is most
1335 likely unwanted and can be extremely confusing (and, most
1336 importantly, other Objective-C 2.0 compilers do not do it).
1339 flags = OBJC_LOOKUP_CLASS | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS;
1341 getter = lookup_method_static (interface, getter_name, flags);
1342 setter = lookup_method_static (interface, setter_name, flags);
1345 /* Second, check the local @implementation context. */
1346 if (!getter && !setter)
1352 getter = lookup_method (CLASS_CLS_METHODS (implementation), getter_name);
1353 setter = lookup_method (CLASS_CLS_METHODS (implementation), setter_name);
1357 getter = lookup_method (CLASS_NST_METHODS (implementation), getter_name);
1358 setter = lookup_method (CLASS_NST_METHODS (implementation), setter_name);
1363 /* Try the protocol_list if we didn't find anything in the
1364 @interface and in the @implementation. */
1365 if (!getter && !setter)
1367 getter = lookup_method_in_protocol_list (protocol_list, getter_name, is_class);
1368 setter = lookup_method_in_protocol_list (protocol_list, setter_name, is_class);
1371 /* There needs to be at least a getter or setter for this to be a
1372 valid 'object.component' syntax. */
1373 if (getter || setter)
1375 /* Yes ... determine the type of the expression. */
1380 type = TREE_VALUE (TREE_TYPE (getter));
1382 type = TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter)));
1384 /* Create an artificial property declaration with the
1385 information we collected on the type and getter/setter
1387 property_decl = make_node (PROPERTY_DECL);
1389 TREE_TYPE (property_decl) = type;
1390 DECL_SOURCE_LOCATION (property_decl) = input_location;
1391 TREE_DEPRECATED (property_decl) = 0;
1392 DECL_ARTIFICIAL (property_decl) = 1;
1394 /* Add property-specific information. Note that one of
1395 PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a
1396 non-existing method; this will generate an error when the
1397 expression is later compiled. At this stage we don't know if
1398 the getter or setter will be used, so we can't generate an
1400 PROPERTY_NAME (property_decl) = component;
1401 PROPERTY_GETTER_NAME (property_decl) = getter_name;
1402 PROPERTY_SETTER_NAME (property_decl) = setter_name;
1403 PROPERTY_READONLY (property_decl) = 0;
1404 PROPERTY_NONATOMIC (property_decl) = 0;
1405 PROPERTY_ASSIGN_SEMANTICS (property_decl) = 0;
1406 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1407 PROPERTY_DYNAMIC (property_decl) = 0;
1410 PROPERTY_HAS_NO_GETTER (property_decl) = 1;
1412 /* The following is currently unused, but it's nice to have
1413 there. We may use it if we need in the future. */
1415 PROPERTY_HAS_NO_SETTER (property_decl) = 1;
1417 return property_decl;
1423 /* This hook routine is invoked by the parser when an expression such
1424 as 'xxx.yyy' is parsed. We get a chance to process these
1425 expressions in a way that is specified to Objective-C (to implement
1426 the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars).
1427 If the expression is not an Objective-C specified expression, we
1428 should return NULL_TREE; else we return the expression.
1430 At the moment this only implements dot-syntax and properties (not
1431 non-fragile ivars yet), ie 'object.property' or 'object.component'
1432 where 'component' is not a declared property, but a valid getter or
1433 setter for it could be found. */
1435 objc_maybe_build_component_ref (tree object, tree property_ident)
1440 /* If we are in Objective-C 1.0 mode, dot-syntax and properties are
1442 if (flag_objc1_only)
1445 /* Try to determine if 'object' is an Objective-C object or not. If
1447 if (object == NULL_TREE || object == error_mark_node
1448 || (rtype = TREE_TYPE (object)) == NULL_TREE)
1451 if (property_ident == NULL_TREE || property_ident == error_mark_node
1452 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1455 /* The following analysis of 'object' is similar to the one used for
1456 the 'receiver' of a method invocation. We need to determine what
1457 'object' is and find the appropriate property (either declared,
1458 or artificial) for it (in the same way as we need to find the
1459 appropriate method prototype for a method invocation). There are
1460 some simplifications here though: "object.property" is invalid if
1461 "object" has a type of "id" or "Class"; it must at least have a
1462 protocol attached to it, and "object" is never a class name as
1463 that is done by objc_build_class_component_ref. Finally, we
1464 don't know if this really is a dot-syntax expression, so we want
1465 to make a quick exit if it is not; for this reason, we try to
1466 postpone checks after determining that 'object' looks like an
1467 Objective-C object. */
1469 if (objc_is_id (rtype))
1471 /* This is the case that the 'object' is of type 'id' or
1474 /* Check if at least it is of type 'id <Protocol>' or 'Class
1475 <Protocol>'; if so, look the property up in the
1477 if (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype)))
1479 tree rprotos = TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype));
1483 /* No point looking up declared @properties if we are
1484 dealing with a class. Classes have no declared
1486 if (!IS_CLASS (rtype))
1487 x = lookup_property_in_protocol_list (rprotos, property_ident);
1491 /* Ok, no property. Maybe it was an
1492 object.component dot-syntax without a declared
1493 property (this is valid for classes too). Look
1494 for getter/setter methods and internally declare
1495 an artifical property based on them if found. */
1496 x = maybe_make_artificial_property_decl (NULL_TREE,
1504 else if (objc_method_context)
1506 /* Else, if we are inside a method it could be the case of
1507 'super' or 'self'. */
1508 tree interface_type = NULL_TREE;
1510 while (TREE_CODE (t) == COMPOUND_EXPR
1511 || TREE_CODE (t) == MODIFY_EXPR
1512 || CONVERT_EXPR_P (t)
1513 || TREE_CODE (t) == COMPONENT_REF)
1514 t = TREE_OPERAND (t, 0);
1516 if (t == UOBJC_SUPER_decl)
1518 /* TODO: Check if this is correct also for 'super' in categories. */
1519 interface_type = lookup_interface (CLASS_SUPER_NAME (implementation_template));
1521 else if (t == self_decl)
1522 interface_type = lookup_interface (CLASS_NAME (implementation_template));
1526 if (TREE_CODE (objc_method_context) != CLASS_METHOD_DECL)
1527 x = lookup_property (interface_type, property_ident);
1531 /* Try the dot-syntax without a declared property.
1532 If this is an access to 'self', it is possible
1533 that they may refer to a setter/getter that is
1534 not declared in the interface, but exists locally
1535 in the implementation. In that case, get the
1536 implementation context and use it. */
1537 tree implementation = NULL_TREE;
1540 implementation = objc_implementation_context;
1542 x = maybe_make_artificial_property_decl
1543 (interface_type, implementation, NULL_TREE,
1545 (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL));
1552 /* This is the case where we have more information on 'rtype'. */
1553 tree basetype = TYPE_MAIN_VARIANT (rtype);
1555 /* Skip the pointer - if none, it's not an Objective-C object or
1557 if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE)
1558 basetype = TREE_TYPE (basetype);
1562 /* Traverse typedefs. */
1563 while (basetype != NULL_TREE
1564 && TREE_CODE (basetype) == RECORD_TYPE
1565 && OBJC_TYPE_NAME (basetype)
1566 && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL
1567 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)))
1568 basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype));
1570 if (basetype != NULL_TREE && TYPED_OBJECT (basetype))
1572 tree interface_type = TYPE_OBJC_INTERFACE (basetype);
1573 tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype);
1576 && (TREE_CODE (interface_type) == CLASS_INTERFACE_TYPE
1577 || TREE_CODE (interface_type) == CATEGORY_INTERFACE_TYPE
1578 || TREE_CODE (interface_type) == PROTOCOL_INTERFACE_TYPE))
1580 /* Not sure 'rtype' could ever be a class here! Just
1581 for safety we keep the checks. */
1582 if (!IS_CLASS (rtype))
1584 x = lookup_property (interface_type, property_ident);
1587 x = lookup_property_in_protocol_list (protocol_list,
1593 /* Try the dot-syntax without a declared property.
1594 If we are inside a method implementation, it is
1595 possible that they may refer to a setter/getter
1596 that is not declared in the interface, but exists
1597 locally in the implementation. In that case, get
1598 the implementation context and use it. */
1599 tree implementation = NULL_TREE;
1601 if (objc_implementation_context
1602 && CLASS_NAME (objc_implementation_context)
1603 == OBJC_TYPE_NAME (interface_type))
1604 implementation = objc_implementation_context;
1606 x = maybe_make_artificial_property_decl (interface_type,
1621 /* We have an additional nasty problem here; if this
1622 PROPERTY_REF needs to become a 'getter', then the conversion
1623 from PROPERTY_REF into a getter call happens in gimplify,
1624 after the selector table has already been generated and when
1625 it is too late to add another selector to it. To work around
1626 the problem, we always create the getter call at this stage,
1627 which puts the selector in the table. Note that if the
1628 PROPERTY_REF becomes a 'setter' instead of a 'getter', then
1629 we have added a selector too many to the selector table.
1630 This is a little inefficient.
1632 Also note that method calls to 'self' and 'super' require the
1633 context (self_decl, UOBJS_SUPER_decl,
1634 objc_implementation_context etc) to be built correctly; this
1635 is yet another reason why building the call at the gimplify
1636 stage (when this context has been lost) is not very
1637 practical. If we build it at this stage, we know it will
1638 always be built correctly.
1640 If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial
1641 property decl created to deal with a dotsyntax not really
1642 referring to an existing property) then do not try to build a
1643 call to the getter as there is no getter. */
1644 if (PROPERTY_HAS_NO_GETTER (x))
1645 getter_call = NULL_TREE;
1647 getter_call = objc_finish_message_expr (object,
1648 PROPERTY_GETTER_NAME (x),
1651 if (TREE_DEPRECATED (x))
1652 warn_deprecated_use (x, NULL_TREE);
1654 expression = build3 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call);
1655 SET_EXPR_LOCATION (expression, input_location);
1656 TREE_SIDE_EFFECTS (expression) = 1;
1664 /* This hook routine is invoked by the parser when an expression such
1665 as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the
1666 Objective-C 2.0 dot-syntax applied to classes, so we need to
1667 convert it into a setter/getter call on the class. */
1669 objc_build_class_component_ref (tree class_name, tree property_ident)
1674 if (flag_objc1_only)
1675 error_at (input_location, "the dot syntax is not available in Objective-C 1.0");
1677 if (class_name == NULL_TREE || class_name == error_mark_node
1678 || TREE_CODE (class_name) != IDENTIFIER_NODE)
1679 return error_mark_node;
1681 if (property_ident == NULL_TREE || property_ident == error_mark_node
1682 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1685 object = objc_get_class_reference (class_name);
1688 /* We know that 'class_name' is an Objective-C class name as the
1689 parser won't call this function if it is not. This is only a
1690 double-check for safety. */
1691 error_at (input_location, "could not find class %qE", class_name);
1692 return error_mark_node;
1695 rtype = lookup_interface (class_name);
1698 /* Again, this should never happen, but we do check. */
1699 error_at (input_location, "could not find interface for class %qE", class_name);
1700 return error_mark_node;
1704 if (TREE_DEPRECATED (rtype))
1705 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", class_name);
1708 x = maybe_make_artificial_property_decl (rtype, NULL_TREE, NULL_TREE,
1717 if (PROPERTY_HAS_NO_GETTER (x))
1718 getter_call = NULL_TREE;
1720 getter_call = objc_finish_message_expr (object,
1721 PROPERTY_GETTER_NAME (x),
1723 if (TREE_DEPRECATED (x))
1724 warn_deprecated_use (x, NULL_TREE);
1726 expression = build3 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call);
1727 SET_EXPR_LOCATION (expression, input_location);
1728 TREE_SIDE_EFFECTS (expression) = 1;
1734 error_at (input_location, "could not find setter/getter for %qE in class %qE",
1735 property_ident, class_name);
1736 return error_mark_node;
1744 /* This is used because we don't want to expose PROPERTY_REF to the
1745 C/C++ frontends. Maybe we should! */
1747 objc_is_property_ref (tree node)
1749 if (node && TREE_CODE (node) == PROPERTY_REF)
1755 /* This function builds a setter call for a PROPERTY_REF (real, for a
1756 declared property, or artificial, for a dot-syntax accessor which
1757 is not corresponding to a property). 'lhs' must be a PROPERTY_REF
1758 (the caller must check this beforehand). 'rhs' is the value to
1759 assign to the property. A plain setter call is returned, or
1760 error_mark_node if the property is readonly. */
1763 objc_build_setter_call (tree lhs, tree rhs)
1765 tree object_expr = PROPERTY_REF_OBJECT (lhs);
1766 tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs);
1768 if (PROPERTY_READONLY (property_decl))
1770 error ("readonly property can not be set");
1771 return error_mark_node;
1775 tree setter_argument = build_tree_list (NULL_TREE, rhs);
1778 /* TODO: Check that the setter return type is 'void'. */
1780 /* TODO: Decay arguments in C. */
1781 setter = objc_finish_message_expr (object_expr,
1782 PROPERTY_SETTER_NAME (property_decl),
1787 /* Unreachable, but the compiler may not realize. */
1788 return error_mark_node;
1791 /* This hook routine is called when a MODIFY_EXPR is being built. We
1792 check what is being modified; if it is a PROPERTY_REF, we need to
1793 generate a 'setter' function call for the property. If this is not
1794 a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go
1795 on creating their MODIFY_EXPR.
1797 This is used for example if you write
1801 where 'count' is a property. The left-hand side creates a
1802 PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR
1803 to assign something to it. We intercept that here, and generate a
1804 call to the 'setter' method instead. */
1806 objc_maybe_build_modify_expr (tree lhs, tree rhs)
1808 if (lhs && TREE_CODE (lhs) == PROPERTY_REF)
1810 /* Building a simple call to the setter method would work for cases such as
1814 but wouldn't work for cases such as
1816 count = object2.count = 1;
1818 to get these to work with very little effort, we build a
1819 compound statement which does the setter call (to set the
1820 property to 'rhs'), but which can also be evaluated returning
1821 the 'rhs'. So, we want to create the following:
1823 (temp = rhs; [object setProperty: temp]; temp)
1825 tree temp_variable_decl, bind;
1826 /* s1, s2 and s3 are the tree statements that we need in the
1827 compound expression. */
1828 tree s1, s2, s3, compound_expr;
1830 /* TODO: If 'rhs' is a constant, we could maybe do without the
1831 'temp' variable ? */
1833 /* Declare __objc_property_temp in a local bind. */
1834 temp_variable_decl = objc_create_temporary_var (TREE_TYPE (rhs), "__objc_property_temp");
1835 DECL_SOURCE_LOCATION (temp_variable_decl) = input_location;
1836 bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL);
1837 SET_EXPR_LOCATION (bind, input_location);
1838 TREE_SIDE_EFFECTS (bind) = 1;
1841 /* Now build the compound statement. */
1843 /* s1: __objc_property_temp = rhs */
1844 s1 = build_modify_expr (input_location, temp_variable_decl, NULL_TREE,
1846 input_location, rhs, NULL_TREE);
1847 SET_EXPR_LOCATION (s1, input_location);
1849 /* s2: [object setProperty: __objc_property_temp] */
1850 s2 = objc_build_setter_call (lhs, temp_variable_decl);
1852 /* This happens if building the setter failed because the property
1854 if (s2 == error_mark_node)
1855 return error_mark_node;
1857 SET_EXPR_LOCATION (s2, input_location);
1859 /* s3: __objc_property_temp */
1860 s3 = convert (TREE_TYPE (lhs), temp_variable_decl);
1862 /* Now build the compound statement (s1, s2, s3) */
1863 compound_expr = build_compound_expr (input_location, build_compound_expr (input_location, s1, s2), s3);
1865 /* Without this, with -Wall you get a 'valued computed is not
1866 used' every time there is a "object.property = x" where the
1867 value of the resulting MODIFY_EXPR is not used. That is
1868 correct (maybe a more sophisticated implementation could
1869 avoid generating the compound expression if not needed), but
1870 we need to turn it off. */
1871 TREE_NO_WARNING (compound_expr) = 1;
1872 return compound_expr;
1878 /* This hook is called by the frontend when one of the four unary
1879 expressions PREINCREMENT_EXPR, POSTINCREMENT_EXPR,
1880 PREDECREMENT_EXPR and POSTDECREMENT_EXPR is being built with an
1881 argument which is a PROPERTY_REF. For example, this happens if you have
1885 where 'count' is a property. We need to use the 'getter' and
1886 'setter' for the property in an appropriate way to build the
1887 appropriate expression. 'code' is the code for the expression (one
1888 of the four mentioned above); 'argument' is the PROPERTY_REF, and
1889 'increment' is how much we need to add or subtract. */
1891 objc_build_incr_expr_for_property_ref (location_t location,
1892 enum tree_code code,
1893 tree argument, tree increment)
1895 /* Here are the expressions that we want to build:
1897 For PREINCREMENT_EXPR / PREDECREMENT_EXPR:
1898 (temp = [object property] +/- increment, [object setProperty: temp], temp)
1900 For POSTINCREMENT_EXPR / POSTECREMENT_EXPR:
1901 (temp = [object property], [object setProperty: temp +/- increment], temp) */
1903 tree temp_variable_decl, bind;
1904 /* s1, s2 and s3 are the tree statements that we need in the
1905 compound expression. */
1906 tree s1, s2, s3, compound_expr;
1909 if (!argument || TREE_CODE (argument) != PROPERTY_REF)
1910 return error_mark_node;
1912 /* Declare __objc_property_temp in a local bind. */
1913 temp_variable_decl = objc_create_temporary_var (TREE_TYPE (argument), "__objc_property_temp");
1914 DECL_SOURCE_LOCATION (temp_variable_decl) = location;
1915 bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL);
1916 SET_EXPR_LOCATION (bind, location);
1917 TREE_SIDE_EFFECTS (bind) = 1;
1920 /* Now build the compound statement. */
1922 /* Note that the 'getter' is generated at gimplify time; at this
1923 time, we can simply put the property_ref (ie, argument) wherever
1924 we want the getter ultimately to be. */
1926 /* s1: __objc_property_temp = [object property] <+/- increment> */
1929 case PREINCREMENT_EXPR:
1930 /* __objc_property_temp = [object property] + increment */
1931 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1933 location, build2 (PLUS_EXPR, TREE_TYPE (argument),
1934 argument, increment), NULL_TREE);
1936 case PREDECREMENT_EXPR:
1937 /* __objc_property_temp = [object property] - increment */
1938 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1940 location, build2 (MINUS_EXPR, TREE_TYPE (argument),
1941 argument, increment), NULL_TREE);
1943 case POSTINCREMENT_EXPR:
1944 case POSTDECREMENT_EXPR:
1945 /* __objc_property_temp = [object property] */
1946 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1948 location, argument, NULL_TREE);
1954 /* s2: [object setProperty: __objc_property_temp <+/- increment>] */
1957 case PREINCREMENT_EXPR:
1958 case PREDECREMENT_EXPR:
1959 /* [object setProperty: __objc_property_temp] */
1960 s2 = objc_build_setter_call (argument, temp_variable_decl);
1962 case POSTINCREMENT_EXPR:
1963 /* [object setProperty: __objc_property_temp + increment] */
1964 s2 = objc_build_setter_call (argument,
1965 build2 (PLUS_EXPR, TREE_TYPE (argument),
1966 temp_variable_decl, increment));
1968 case POSTDECREMENT_EXPR:
1969 /* [object setProperty: __objc_property_temp - increment] */
1970 s2 = objc_build_setter_call (argument,
1971 build2 (MINUS_EXPR, TREE_TYPE (argument),
1972 temp_variable_decl, increment));
1978 /* This happens if building the setter failed because the property
1980 if (s2 == error_mark_node)
1981 return error_mark_node;
1983 SET_EXPR_LOCATION (s2, location);
1985 /* s3: __objc_property_temp */
1986 s3 = convert (TREE_TYPE (argument), temp_variable_decl);
1988 /* Now build the compound statement (s1, s2, s3) */
1989 compound_expr = build_compound_expr (location, build_compound_expr (location, s1, s2), s3);
1991 /* Prevent C++ from warning with -Wall that "right operand of comma
1992 operator has no effect". */
1993 TREE_NO_WARNING (compound_expr) = 1;
1994 return compound_expr;
1998 objc_build_method_signature (bool is_class_method, tree rettype, tree selector,
1999 tree optparms, bool ellipsis)
2001 if (is_class_method)
2002 return build_method_decl (CLASS_METHOD_DECL, rettype, selector,
2003 optparms, ellipsis);
2005 return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector,
2006 optparms, ellipsis);
2010 objc_add_method_declaration (bool is_class_method, tree decl, tree attributes)
2012 if (!objc_interface_context)
2014 /* PS: At the moment, due to how the parser works, it should be
2015 impossible to get here. But it's good to have the check in
2016 case the parser changes.
2018 fatal_error ("method declaration not in @interface context");
2021 if (flag_objc1_only && attributes)
2022 error_at (input_location, "method attributes are not available in Objective-C 1.0");
2024 objc_decl_method_attributes (&decl, attributes, 0);
2025 objc_add_method (objc_interface_context,
2028 objc_method_optional_flag);
2031 /* Return 'true' if the method definition could be started, and
2032 'false' if not (because we are outside an @implementation context).
2035 objc_start_method_definition (bool is_class_method, tree decl, tree attributes)
2037 if (!objc_implementation_context)
2039 error ("method definition not in @implementation context");
2043 if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node)
2047 /* Indicate no valid break/continue context by setting these variables
2048 to some non-null, non-label value. We'll notice and emit the proper
2049 error message in c_finish_bc_stmt. */
2050 c_break_label = c_cont_label = size_zero_node;
2054 warning_at (input_location, 0, "method attributes can not be specified in @implementation context");
2056 objc_decl_method_attributes (&decl, attributes, 0);
2058 objc_add_method (objc_implementation_context,
2061 /* is optional */ false);
2062 start_method_def (decl);
2067 objc_add_instance_variable (tree decl)
2069 (void) add_instance_variable (objc_ivar_context,
2070 objc_ivar_visibility,
2074 /* Return true if TYPE is 'id'. */
2077 objc_is_object_id (tree type)
2079 return OBJC_TYPE_NAME (type) == objc_object_id;
2083 objc_is_class_id (tree type)
2085 return OBJC_TYPE_NAME (type) == objc_class_id;
2088 /* Construct a C struct with same name as KLASS, a base struct with tag
2089 SUPER_NAME (if any), and FIELDS indicated. */
2092 objc_build_struct (tree klass, tree fields, tree super_name)
2094 tree name = CLASS_NAME (klass);
2095 tree s = objc_start_struct (name);
2096 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
2098 VEC(tree,heap) *objc_info = NULL;
2103 /* Prepend a packed variant of the base class into the layout. This
2104 is necessary to preserve ObjC ABI compatibility. */
2105 tree base = build_decl (input_location,
2106 FIELD_DECL, NULL_TREE, super);
2107 tree field = TYPE_FIELDS (super);
2109 while (field && DECL_CHAIN (field)
2110 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
2111 field = DECL_CHAIN (field);
2113 /* For ObjC ABI purposes, the "packed" size of a base class is
2114 the sum of the offset and the size (in bits) of the last field
2117 = (field && TREE_CODE (field) == FIELD_DECL
2118 ? size_binop (PLUS_EXPR,
2119 size_binop (PLUS_EXPR,
2122 convert (bitsizetype,
2123 DECL_FIELD_OFFSET (field)),
2124 bitsize_int (BITS_PER_UNIT)),
2125 DECL_FIELD_BIT_OFFSET (field)),
2127 : bitsize_zero_node);
2128 DECL_SIZE_UNIT (base)
2129 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
2130 size_int (BITS_PER_UNIT));
2131 DECL_ARTIFICIAL (base) = 1;
2132 DECL_ALIGN (base) = 1;
2133 DECL_FIELD_CONTEXT (base) = s;
2135 DECL_FIELD_IS_BASE (base) = 1;
2138 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
2139 #endif /* are following the ObjC ABI here. */
2140 DECL_CHAIN (base) = fields;
2144 /* NB: Calling finish_struct() may cause type TYPE_LANG_SPECIFIC fields
2145 in all variants of this RECORD_TYPE to be clobbered, but it is therein
2146 that we store protocol conformance info (e.g., 'NSObject <MyProtocol>').
2147 Hence, we must squirrel away the ObjC-specific information before calling
2148 finish_struct(), and then reinstate it afterwards. */
2150 for (t = TYPE_NEXT_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
2152 if (!TYPE_HAS_OBJC_INFO (t))
2154 INIT_TYPE_OBJC_INFO (t);
2155 TYPE_OBJC_INTERFACE (t) = klass;
2157 VEC_safe_push (tree, heap, objc_info, TYPE_OBJC_INFO (t));
2160 /* Point the struct at its related Objective-C class. */
2161 INIT_TYPE_OBJC_INFO (s);
2162 TYPE_OBJC_INTERFACE (s) = klass;
2164 s = objc_finish_struct (s, fields);
2166 for (i = 0, t = TYPE_NEXT_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
2168 TYPE_OBJC_INFO (t) = VEC_index (tree, objc_info, i);
2169 /* Replace the IDENTIFIER_NODE with an actual @interface. */
2170 TYPE_OBJC_INTERFACE (t) = klass;
2172 VEC_free (tree, heap, objc_info);
2174 /* Use TYPE_BINFO structures to point at the super class, if any. */
2175 objc_xref_basetypes (s, super);
2177 /* Mark this struct as a class template. */
2178 CLASS_STATIC_TEMPLATE (klass) = s;
2183 /* Build a type differing from TYPE only in that TYPE_VOLATILE is set.
2184 Unlike tree.c:build_qualified_type(), preserve TYPE_LANG_SPECIFIC in the
2187 objc_build_volatilized_type (tree type)
2191 /* Check if we have not constructed the desired variant already. */
2192 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2194 /* The type qualifiers must (obviously) match up. */
2195 if (!TYPE_VOLATILE (t)
2196 || (TYPE_READONLY (t) != TYPE_READONLY (type))
2197 || (TYPE_RESTRICT (t) != TYPE_RESTRICT (type)))
2200 /* For pointer types, the pointees (and hence their TYPE_LANG_SPECIFIC
2201 info, if any) must match up. */
2202 if (POINTER_TYPE_P (t)
2203 && (TREE_TYPE (t) != TREE_TYPE (type)))
2206 /* Only match up the types which were previously volatilized in similar fashion and not
2207 because they were declared as such. */
2208 if (!lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (t)))
2211 /* Everything matches up! */
2215 /* Ok, we could not re-use any of the pre-existing variants. Create
2217 t = build_variant_type_copy (type);
2218 TYPE_VOLATILE (t) = 1;
2220 TYPE_ATTRIBUTES (t) = merge_attributes (TYPE_ATTRIBUTES (type),
2221 tree_cons (get_identifier ("objc_volatilized"),
2224 if (TREE_CODE (t) == ARRAY_TYPE)
2225 TREE_TYPE (t) = objc_build_volatilized_type (TREE_TYPE (t));
2227 /* Set up the canonical type information. */
2228 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2229 SET_TYPE_STRUCTURAL_EQUALITY (t);
2230 else if (TYPE_CANONICAL (type) != type)
2231 TYPE_CANONICAL (t) = objc_build_volatilized_type (TYPE_CANONICAL (type));
2233 TYPE_CANONICAL (t) = t;
2238 /* Mark DECL as being 'volatile' for purposes of Darwin
2239 _setjmp()/_longjmp() exception handling. Called from
2240 objc_mark_locals_volatile(). */
2242 objc_volatilize_decl (tree decl)
2244 /* Do not mess with variables that are 'static' or (already)
2246 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
2247 && (TREE_CODE (decl) == VAR_DECL
2248 || TREE_CODE (decl) == PARM_DECL))
2250 tree t = TREE_TYPE (decl);
2252 t = objc_build_volatilized_type (t);
2254 TREE_TYPE (decl) = t;
2255 TREE_THIS_VOLATILE (decl) = 1;
2256 TREE_SIDE_EFFECTS (decl) = 1;
2257 DECL_REGISTER (decl) = 0;
2259 C_DECL_REGISTER (decl) = 0;
2264 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
2265 (including its categories and superclasses) or by object type TYP.
2266 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
2269 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
2271 bool class_type = (cls != NULL_TREE);
2277 /* Check protocols adopted by the class and its categories. */
2278 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
2280 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
2284 /* Repeat for superclasses. */
2285 cls = lookup_interface (CLASS_SUPER_NAME (cls));
2288 /* Check for any protocols attached directly to the object type. */
2289 if (TYPE_HAS_OBJC_INFO (typ))
2291 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
2298 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
2299 /* NB: Types 'id' and 'Class' cannot reasonably be described as
2300 "implementing" a given protocol, since they do not have an
2303 warning (0, "class %qs does not implement the %qE protocol",
2304 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2306 warning (0, "type %qs does not conform to the %qE protocol",
2307 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2313 /* Check if class RCLS and instance struct type RTYP conform to at least the
2314 same protocols that LCLS and LTYP conform to. */
2317 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
2320 bool have_lproto = false;
2324 /* NB: We do _not_ look at categories defined for LCLS; these may or
2325 may not get loaded in, and therefore it is unreasonable to require
2326 that RCLS/RTYP must implement any of their protocols. */
2327 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
2331 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2335 /* Repeat for superclasses. */
2336 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
2339 /* Check for any protocols attached directly to the object type. */
2340 if (TYPE_HAS_OBJC_INFO (ltyp))
2342 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
2346 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2351 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
2352 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
2353 away with simply checking for 'id' or 'Class' (!RCLS), since this
2354 routine will not get called in other cases. */
2355 return have_lproto || (rcls != NULL_TREE);
2358 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
2359 Both TYPE1 and TYPE2 must be pointers, and already determined to be
2360 compatible by objc_compare_types() below. */
2363 objc_common_type (tree type1, tree type2)
2365 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
2367 while (POINTER_TYPE_P (inner1))
2369 inner1 = TREE_TYPE (inner1);
2370 inner2 = TREE_TYPE (inner2);
2373 /* If one type is derived from another, return the base type. */
2374 if (DERIVED_FROM_P (inner1, inner2))
2376 else if (DERIVED_FROM_P (inner2, inner1))
2379 /* If both types are 'Class', return 'Class'. */
2380 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
2381 return objc_class_type;
2383 /* Otherwise, return 'id'. */
2384 return objc_object_type;
2387 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
2388 an instance of RTYP to an instance of LTYP or to compare the two
2389 (if ARGNO is equal to -3), per ObjC type system rules. Before
2390 returning 'true', this routine may issue warnings related to, e.g.,
2391 protocol conformance. When returning 'false', the routine must
2392 produce absolutely no warnings; the C or C++ front-end will do so
2393 instead, if needed. If either LTYP or RTYP is not an Objective-C
2394 type, the routine must return 'false'.
2396 The ARGNO parameter is encoded as follows:
2397 >= 1 Parameter number (CALLEE contains function being called);
2401 -3 Comparison (LTYP and RTYP may match in either direction);
2402 -4 Silent comparison (for C++ overload resolution);
2403 -5 Silent "specialization" comparison for RTYP to be a "specialization"
2404 of LTYP (a specialization means that RTYP is LTYP plus some constraints,
2405 so that each object of type RTYP is also of type LTYP). This is used
2406 when comparing property types. */
2409 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
2411 tree lcls, rcls, lproto, rproto;
2412 bool pointers_compatible;
2414 /* We must be dealing with pointer types */
2415 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
2420 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2421 rtyp = TREE_TYPE (rtyp);
2423 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2425 /* We must also handle function pointers, since ObjC is a bit more
2426 lenient than C or C++ on this. */
2427 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
2429 /* Return types must be covariant. */
2430 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
2431 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
2435 /* Argument types must be contravariant. */
2436 for (ltyp = TYPE_ARG_TYPES (ltyp), rtyp = TYPE_ARG_TYPES (rtyp);
2437 ltyp && rtyp; ltyp = TREE_CHAIN (ltyp), rtyp = TREE_CHAIN (rtyp))
2439 if (!comptypes (TREE_VALUE (rtyp), TREE_VALUE (ltyp))
2440 && !objc_compare_types (TREE_VALUE (rtyp), TREE_VALUE (ltyp),
2445 return (ltyp == rtyp);
2448 /* Past this point, we are only interested in ObjC class instances,
2449 or 'id' or 'Class'. */
2450 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
2453 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
2454 && !TYPE_HAS_OBJC_INFO (ltyp))
2457 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
2458 && !TYPE_HAS_OBJC_INFO (rtyp))
2461 /* Past this point, we are committed to returning 'true' to the caller
2462 (unless performing a silent comparison; see below). However, we can
2463 still warn about type and/or protocol mismatches. */
2465 if (TYPE_HAS_OBJC_INFO (ltyp))
2467 lcls = TYPE_OBJC_INTERFACE (ltyp);
2468 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
2471 lcls = lproto = NULL_TREE;
2473 if (TYPE_HAS_OBJC_INFO (rtyp))
2475 rcls = TYPE_OBJC_INTERFACE (rtyp);
2476 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
2479 rcls = rproto = NULL_TREE;
2481 /* If we could not find an @interface declaration, we must have
2482 only seen a @class declaration; for purposes of type comparison,
2483 treat it as a stand-alone (root) class. */
2485 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
2488 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
2491 /* If either type is an unqualified 'id', we're done. This is because
2492 an 'id' can be assigned to or from any type with no warnings. */
2495 if ((!lproto && objc_is_object_id (ltyp))
2496 || (!rproto && objc_is_object_id (rtyp)))
2501 /* For property checks, though, an 'id' is considered the most
2502 general type of object, hence if you try to specialize an
2503 'NSArray *' (ltyp) property with an 'id' (rtyp) one, we need
2505 if (!lproto && objc_is_object_id (ltyp))
2509 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
2511 /* If the underlying types are the same, and at most one of them has
2512 a protocol list, we do not need to issue any diagnostics. */
2513 if (pointers_compatible && (!lproto || !rproto))
2516 /* If exactly one of the types is 'Class', issue a diagnostic; any
2517 exceptions of this rule have already been handled. */
2518 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
2519 pointers_compatible = false;
2520 /* Otherwise, check for inheritance relations. */
2523 if (!pointers_compatible)
2525 /* Again, if any of the two is an 'id', we're satisfied,
2526 unless we're comparing properties, in which case only an
2527 'id' on the left-hand side (old property) is good
2531 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
2533 pointers_compatible = objc_is_object_id (ltyp);
2536 if (!pointers_compatible)
2537 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
2539 if (!pointers_compatible && (argno == -3 || argno == -4))
2540 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
2543 /* If the pointers match modulo protocols, check for protocol conformance
2545 if (pointers_compatible)
2547 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
2550 if (!pointers_compatible && argno == -3)
2551 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
2555 if (!pointers_compatible)
2557 /* The two pointers are not exactly compatible. Issue a warning, unless
2558 we are performing a silent comparison, in which case return 'false'
2560 /* NB: For the time being, we shall make our warnings look like their
2561 C counterparts. In the future, we may wish to make them more
2570 warning (0, "comparison of distinct Objective-C types lacks a cast");
2574 warning (0, "initialization from distinct Objective-C type");
2578 warning (0, "assignment from distinct Objective-C type");
2582 warning (0, "distinct Objective-C type in return");
2586 warning (0, "passing argument %d of %qE from distinct "
2587 "Objective-C type", argno, callee);
2595 /* This routine is similar to objc_compare_types except that function-pointers are
2596 excluded. This is because, caller assumes that common types are of (id, Object*)
2597 variety and calls objc_common_type to obtain a common type. There is no commonolty
2598 between two function-pointers in this regard. */
2601 objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
2603 if (objc_compare_types (ltyp, rtyp, argno, callee))
2605 /* exclude function-pointer types. */
2608 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2609 rtyp = TREE_TYPE (rtyp);
2611 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2612 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
2617 /* Check if LTYP and RTYP have the same type qualifiers. If either type
2618 lives in the volatilized hash table, ignore the 'volatile' bit when
2619 making the comparison. */
2622 objc_type_quals_match (tree ltyp, tree rtyp)
2624 int lquals = TYPE_QUALS (ltyp), rquals = TYPE_QUALS (rtyp);
2626 if (lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (ltyp)))
2627 lquals &= ~TYPE_QUAL_VOLATILE;
2629 if (lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (rtyp)))
2630 rquals &= ~TYPE_QUAL_VOLATILE;
2632 return (lquals == rquals);
2636 /* Determine if CHILD is derived from PARENT. The routine assumes that
2637 both parameters are RECORD_TYPEs, and is non-reflexive. */
2640 objc_derived_from_p (tree parent, tree child)
2642 parent = TYPE_MAIN_VARIANT (parent);
2644 for (child = TYPE_MAIN_VARIANT (child);
2645 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
2647 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
2648 (TYPE_BINFO (child),
2651 if (child == parent)
2660 objc_build_component_ref (tree datum, tree component)
2662 /* If COMPONENT is NULL, the caller is referring to the anonymous
2663 base class field. */
2666 tree base = TYPE_FIELDS (TREE_TYPE (datum));
2668 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
2671 /* The 'build_component_ref' routine has been removed from the C++
2672 front-end, but 'finish_class_member_access_expr' seems to be
2673 a worthy substitute. */
2675 return finish_class_member_access_expr (datum, component, false,
2676 tf_warning_or_error);
2678 return build_component_ref (input_location, datum, component);
2682 /* Recursively copy inheritance information rooted at BINFO. To do this,
2683 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
2686 objc_copy_binfo (tree binfo)
2688 tree btype = BINFO_TYPE (binfo);
2689 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
2693 BINFO_TYPE (binfo2) = btype;
2694 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
2695 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
2697 /* Recursively copy base binfos of BINFO. */
2698 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2700 tree base_binfo2 = objc_copy_binfo (base_binfo);
2702 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
2703 BINFO_BASE_APPEND (binfo2, base_binfo2);
2709 /* Record superclass information provided in BASETYPE for ObjC class REF.
2710 This is loosely based on cp/decl.c:xref_basetypes(). */
2713 objc_xref_basetypes (tree ref, tree basetype)
2715 tree binfo = make_tree_binfo (basetype ? 1 : 0);
2717 TYPE_BINFO (ref) = binfo;
2718 BINFO_OFFSET (binfo) = size_zero_node;
2719 BINFO_TYPE (binfo) = ref;
2723 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
2725 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
2726 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, 1);
2727 BINFO_BASE_APPEND (binfo, base_binfo);
2728 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
2732 /* Called from finish_decl. */
2735 objc_check_decl (tree decl)
2737 tree type = TREE_TYPE (decl);
2739 if (TREE_CODE (type) != RECORD_TYPE)
2741 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
2742 error ("statically allocated instance of Objective-C class %qE",
2747 objc_check_global_decl (tree decl)
2749 tree id = DECL_NAME (decl);
2750 if (objc_is_class_name (id) && global_bindings_p())
2751 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id));
2754 /* Return a non-volatalized version of TYPE. */
2757 objc_non_volatilized_type (tree type)
2759 if (lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (type)))
2760 type = build_qualified_type (type, (TYPE_QUALS (type) & ~TYPE_QUAL_VOLATILE));
2764 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where INTERFACE may
2765 either name an Objective-C class, or refer to the special 'id' or 'Class'
2766 types. If INTERFACE is not a valid ObjC type, just return it unchanged. */
2769 objc_get_protocol_qualified_type (tree interface, tree protocols)
2771 /* If INTERFACE is not provided, default to 'id'. */
2772 tree type = (interface ? objc_is_id (interface) : objc_object_type);
2773 bool is_ptr = (type != NULL_TREE);
2777 type = objc_is_class_name (interface);
2781 /* If looking at a typedef, retrieve the precise type it
2783 if (TREE_CODE (interface) == IDENTIFIER_NODE)
2784 interface = identifier_global_value (interface);
2786 type = ((interface && TREE_CODE (interface) == TYPE_DECL
2787 && DECL_ORIGINAL_TYPE (interface))
2788 ? DECL_ORIGINAL_TYPE (interface)
2789 : xref_tag (RECORD_TYPE, type));
2793 /* This case happens when we are given an 'interface' which
2794 is not a valid class name. For example if a typedef was
2795 used, and 'interface' really is the identifier of the
2796 typedef, but when you resolve it you don't get an
2797 Objective-C class, but something else, such as 'int'.
2798 This is an error; protocols make no sense unless you use
2799 them with Objective-C objects. */
2800 error_at (input_location, "only Objective-C object types can be qualified with a protocol");
2802 /* Try to recover. Ignore the invalid class name, and treat
2803 the object as an 'id' to silence further warnings about
2805 type = objc_object_type;
2812 type = build_variant_type_copy (type);
2814 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
2818 tree orig_pointee_type = TREE_TYPE (type);
2819 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
2821 /* Set up the canonical type information. */
2822 TYPE_CANONICAL (type)
2823 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
2825 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
2826 type = TREE_TYPE (type);
2829 /* Look up protocols and install in lang specific list. */
2830 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
2831 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols (protocols);
2833 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
2834 return the pointer to the new pointee variant. */
2836 type = TYPE_POINTER_TO (type);
2838 TYPE_OBJC_INTERFACE (type)
2839 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
2845 /* Check for circular dependencies in protocols. The arguments are
2846 PROTO, the protocol to check, and LIST, a list of protocol it
2850 check_protocol_recursively (tree proto, tree list)
2854 for (p = list; p; p = TREE_CHAIN (p))
2856 tree pp = TREE_VALUE (p);
2858 if (TREE_CODE (pp) == IDENTIFIER_NODE)
2859 pp = lookup_protocol (pp, /* warn if deprecated */ false);
2862 fatal_error ("protocol %qE has circular dependency",
2863 PROTOCOL_NAME (pp));
2865 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
2869 /* Look up PROTOCOLS, and return a list of those that are found. If
2870 none are found, return NULL. Note that this function will emit a
2871 warning if a protocol is found and is deprecated. */
2874 lookup_and_install_protocols (tree protocols)
2877 tree return_value = NULL_TREE;
2879 if (protocols == error_mark_node)
2882 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
2884 tree ident = TREE_VALUE (proto);
2885 tree p = lookup_protocol (ident, /* warn_if_deprecated */ true);
2888 return_value = chainon (return_value,
2889 build_tree_list (NULL_TREE, p));
2890 else if (ident != error_mark_node)
2891 error ("cannot find protocol declaration for %qE",
2895 return return_value;
2898 /* Create a declaration for field NAME of a given TYPE. */
2901 create_field_decl (tree type, const char *name)
2903 return build_decl (input_location,
2904 FIELD_DECL, get_identifier (name), type);
2907 /* Create a global, static declaration for variable NAME of a given TYPE. The
2908 finish_var_decl() routine will need to be called on it afterwards. */
2911 start_var_decl (tree type, const char *name)
2913 tree var = build_decl (input_location,
2914 VAR_DECL, get_identifier (name), type);
2916 TREE_STATIC (var) = 1;
2917 DECL_INITIAL (var) = error_mark_node; /* A real initializer is coming... */
2918 DECL_IGNORED_P (var) = 1;
2919 DECL_ARTIFICIAL (var) = 1;
2920 DECL_CONTEXT (var) = NULL_TREE;
2922 DECL_THIS_STATIC (var) = 1; /* squash redeclaration errors */
2928 /* Finish off the variable declaration created by start_var_decl(). */
2931 finish_var_decl (tree var, tree initializer)
2933 finish_decl (var, input_location, initializer, NULL_TREE, NULL_TREE);
2936 /* Find the decl for the constant string class reference. This is only
2937 used for the NeXT runtime. */
2940 setup_string_decl (void)
2945 /* %s in format will provide room for terminating null */
2946 length = strlen (STRING_OBJECT_GLOBAL_FORMAT)
2947 + strlen (constant_string_class_name);
2948 name = XNEWVEC (char, length);
2949 sprintf (name, STRING_OBJECT_GLOBAL_FORMAT,
2950 constant_string_class_name);
2951 constant_string_global_id = get_identifier (name);
2952 string_class_decl = lookup_name (constant_string_global_id);
2954 return string_class_decl;
2957 /* Purpose: "play" parser, creating/installing representations
2958 of the declarations that are required by Objective-C.
2962 type_spec--------->sc_spec
2963 (tree_list) (tree_list)
2966 identifier_node identifier_node */
2969 synth_module_prologue (void)
2972 enum debug_info_type save_write_symbols = write_symbols;
2973 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
2975 /* Suppress outputting debug symbols, because
2976 dbxout_init hasn't been called yet. */
2977 write_symbols = NO_DEBUG;
2978 debug_hooks = &do_nothing_debug_hooks;
2981 push_lang_context (lang_name_c); /* extern "C" */
2984 /* The following are also defined in <objc/objc.h> and friends. */
2986 objc_object_id = get_identifier (TAG_OBJECT);
2987 objc_class_id = get_identifier (TAG_CLASS);
2989 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
2990 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
2992 objc_object_type = build_pointer_type (objc_object_reference);
2993 objc_class_type = build_pointer_type (objc_class_reference);
2995 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
2996 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
2998 /* Declare the 'id' and 'Class' typedefs. */
3000 type = lang_hooks.decls.pushdecl (build_decl (input_location,
3004 TREE_NO_WARNING (type) = 1;
3005 type = lang_hooks.decls.pushdecl (build_decl (input_location,
3009 TREE_NO_WARNING (type) = 1;
3011 /* Forward-declare '@interface Protocol'. */
3013 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
3014 objc_declare_class (tree_cons (NULL_TREE, type, NULL_TREE));
3015 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE,
3018 /* Declare type of selector-objects that represent an operation name. */
3020 if (flag_next_runtime)
3021 /* `struct objc_selector *' */
3023 = build_pointer_type (xref_tag (RECORD_TYPE,
3024 get_identifier (TAG_SELECTOR)));
3026 /* `const struct objc_selector *' */
3028 = build_pointer_type
3029 (build_qualified_type (xref_tag (RECORD_TYPE,
3030 get_identifier (TAG_SELECTOR)),
3033 /* Declare receiver type used for dispatching messages to 'super'. */
3035 /* `struct objc_super *' */
3036 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
3037 get_identifier (TAG_SUPER)));
3039 /* Declare pointers to method and ivar lists. */
3040 objc_method_list_ptr = build_pointer_type
3041 (xref_tag (RECORD_TYPE,
3042 get_identifier (UTAG_METHOD_LIST)));
3043 objc_method_proto_list_ptr
3044 = build_pointer_type (xref_tag (RECORD_TYPE,
3045 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
3046 objc_ivar_list_ptr = build_pointer_type
3047 (xref_tag (RECORD_TYPE,
3048 get_identifier (UTAG_IVAR_LIST)));
3050 /* TREE_NOTHROW is cleared for the message-sending functions,
3051 because the function that gets called can throw in Obj-C++, or
3052 could itself call something that can throw even in Obj-C. */
3054 if (flag_next_runtime)
3056 /* NB: In order to call one of the ..._stret (struct-returning)
3057 functions, the function *MUST* first be cast to a signature that
3058 corresponds to the actual ObjC method being invoked. This is
3059 what is done by the build_objc_method_call() routine below. */
3061 /* id objc_msgSend (id, SEL, ...); */
3062 /* id objc_msgSendNonNil (id, SEL, ...); */
3063 /* id objc_msgSend_stret (id, SEL, ...); */
3064 /* id objc_msgSendNonNil_stret (id, SEL, ...); */
3066 = build_varargs_function_type_list (objc_object_type,
3070 umsg_decl = add_builtin_function (TAG_MSGSEND,
3071 type, 0, NOT_BUILT_IN,
3073 umsg_nonnil_decl = add_builtin_function (TAG_MSGSEND_NONNIL,
3074 type, 0, NOT_BUILT_IN,
3076 umsg_stret_decl = add_builtin_function (TAG_MSGSEND_STRET,
3077 type, 0, NOT_BUILT_IN,
3079 umsg_nonnil_stret_decl = add_builtin_function (TAG_MSGSEND_NONNIL_STRET,
3080 type, 0, NOT_BUILT_IN,
3083 /* These can throw, because the function that gets called can throw
3084 in Obj-C++, or could itself call something that can throw even
3086 TREE_NOTHROW (umsg_decl) = 0;
3087 TREE_NOTHROW (umsg_nonnil_decl) = 0;
3088 TREE_NOTHROW (umsg_stret_decl) = 0;
3089 TREE_NOTHROW (umsg_nonnil_stret_decl) = 0;
3091 /* id objc_msgSend_Fast (id, SEL, ...)
3092 __attribute__ ((hard_coded_address (OFFS_MSGSEND_FAST))); */
3093 #ifdef OFFS_MSGSEND_FAST
3094 umsg_fast_decl = add_builtin_function (TAG_MSGSEND_FAST,
3095 type, 0, NOT_BUILT_IN,
3097 TREE_NOTHROW (umsg_fast_decl) = 0;
3098 DECL_ATTRIBUTES (umsg_fast_decl)
3099 = tree_cons (get_identifier ("hard_coded_address"),
3100 build_int_cst (NULL_TREE, OFFS_MSGSEND_FAST),
3103 /* No direct dispatch available. */
3104 umsg_fast_decl = umsg_decl;
3107 /* id objc_msgSendSuper (struct objc_super *, SEL, ...); */
3108 /* id objc_msgSendSuper_stret (struct objc_super *, SEL, ...); */
3110 = build_varargs_function_type_list (objc_object_type,
3114 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
3115 type, 0, NOT_BUILT_IN,
3117 umsg_super_stret_decl = add_builtin_function (TAG_MSGSENDSUPER_STRET,
3118 type, 0, NOT_BUILT_IN, 0,
3120 TREE_NOTHROW (umsg_super_decl) = 0;
3121 TREE_NOTHROW (umsg_super_stret_decl) = 0;
3125 /* GNU runtime messenger entry points. */
3127 /* typedef id (*IMP)(id, SEL, ...); */
3129 build_varargs_function_type_list (objc_object_type,
3133 tree IMP_type = build_pointer_type (ftype);
3135 /* IMP objc_msg_lookup (id, SEL); */
3136 type = build_function_type_list (IMP_type,
3140 umsg_decl = add_builtin_function (TAG_MSGSEND,
3141 type, 0, NOT_BUILT_IN,
3143 TREE_NOTHROW (umsg_decl) = 0;
3145 /* IMP objc_msg_lookup_super (struct objc_super *, SEL); */
3147 = build_function_type_list (IMP_type,
3151 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
3152 type, 0, NOT_BUILT_IN,
3154 TREE_NOTHROW (umsg_super_decl) = 0;
3156 /* The following GNU runtime entry point is called to initialize
3159 __objc_exec_class (void *); */
3161 = build_function_type_list (void_type_node,
3164 execclass_decl = add_builtin_function (TAG_EXECCLASS,
3165 type, 0, NOT_BUILT_IN,
3169 /* id objc_getClass (const char *); */
3171 type = build_function_type_list (objc_object_type,
3172 const_string_type_node,
3176 = add_builtin_function (TAG_GETCLASS, type, 0, NOT_BUILT_IN,
3179 /* id objc_getMetaClass (const char *); */
3181 objc_get_meta_class_decl
3182 = add_builtin_function (TAG_GETMETACLASS, type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
3184 build_class_template ();
3185 build_super_template ();
3186 build_protocol_template ();
3187 build_category_template ();
3188 build_objc_exception_stuff ();
3190 /* Declare objc_getProperty, object_setProperty and other property
3191 accessor helpers. */
3192 build_objc_property_accessor_helpers ();
3194 if (flag_next_runtime)
3195 build_next_objc_exception_stuff ();
3197 /* static SEL _OBJC_SELECTOR_TABLE[]; */
3199 if (! flag_next_runtime)
3200 build_selector_table_decl ();
3202 /* Forward declare constant_string_id and constant_string_type. */
3203 if (!constant_string_class_name)
3204 constant_string_class_name = default_constant_string_class_name;
3206 constant_string_id = get_identifier (constant_string_class_name);
3207 objc_declare_class (tree_cons (NULL_TREE, constant_string_id, NULL_TREE));
3209 /* Pre-build the following entities - for speed/convenience. */
3210 self_id = get_identifier ("self");
3211 ucmd_id = get_identifier ("_cmd");
3213 /* Declare struct _objc_fast_enumeration_state { ... }; */
3214 build_fast_enumeration_state_template ();
3216 /* void objc_enumeration_mutation (id) */
3217 type = build_function_type (void_type_node,
3218 tree_cons (NULL_TREE, objc_object_type, NULL_TREE));
3219 objc_enumeration_mutation_decl
3220 = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN,
3222 TREE_NOTHROW (objc_enumeration_mutation_decl) = 0;
3225 pop_lang_context ();
3228 write_symbols = save_write_symbols;
3229 debug_hooks = save_hooks;
3232 /* Ensure that the ivar list for NSConstantString/NXConstantString
3233 (or whatever was specified via `-fconstant-string-class')
3234 contains fields at least as large as the following three, so that
3235 the runtime can stomp on them with confidence:
3237 struct STRING_OBJECT_CLASS_NAME
3241 unsigned int length;
3245 check_string_class_template (void)
3247 tree field_decl = objc_get_class_ivars (constant_string_id);
3249 #define AT_LEAST_AS_LARGE_AS(F, T) \
3250 (F && TREE_CODE (F) == FIELD_DECL \
3251 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
3252 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
3254 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3257 field_decl = DECL_CHAIN (field_decl);
3258 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3261 field_decl = DECL_CHAIN (field_decl);
3262 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
3264 #undef AT_LEAST_AS_LARGE_AS
3267 /* Avoid calling `check_string_class_template ()' more than once. */
3268 static GTY(()) int string_layout_checked;
3270 /* Construct an internal string layout to be used as a template for
3271 creating NSConstantString/NXConstantString instances. */
3274 objc_build_internal_const_str_type (void)
3276 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
3277 tree fields = build_decl (input_location,
3278 FIELD_DECL, NULL_TREE, ptr_type_node);
3279 tree field = build_decl (input_location,
3280 FIELD_DECL, NULL_TREE, ptr_type_node);
3282 DECL_CHAIN (field) = fields; fields = field;
3283 field = build_decl (input_location,
3284 FIELD_DECL, NULL_TREE, unsigned_type_node);
3285 DECL_CHAIN (field) = fields; fields = field;
3286 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
3288 finish_builtin_struct (type, "__builtin_ObjCString",
3294 /* Custom build_string which sets TREE_TYPE! */
3297 my_build_string (int len, const char *str)
3299 return fix_string_type (build_string (len, str));
3302 /* Build a string with contents STR and length LEN and convert it to a
3306 my_build_string_pointer (int len, const char *str)
3308 tree string = my_build_string (len, str);
3309 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
3310 return build1 (ADDR_EXPR, ptrtype, string);
3314 string_hash (const void *ptr)
3316 const_tree const str = ((const struct string_descriptor *)ptr)->literal;
3317 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3318 int i, len = TREE_STRING_LENGTH (str);
3321 for (i = 0; i < len; i++)
3322 h = ((h * 613) + p[i]);
3328 string_eq (const void *ptr1, const void *ptr2)
3330 const_tree const str1 = ((const struct string_descriptor *)ptr1)->literal;
3331 const_tree const str2 = ((const struct string_descriptor *)ptr2)->literal;
3332 int len1 = TREE_STRING_LENGTH (str1);
3334 return (len1 == TREE_STRING_LENGTH (str2)
3335 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3339 /* Given a chain of STRING_CST's, build a static instance of
3340 NXConstantString which points at the concatenation of those
3341 strings. We place the string object in the __string_objects
3342 section of the __OBJC segment. The Objective-C runtime will
3343 initialize the isa pointers of the string objects to point at the
3344 NXConstantString class object. */
3347 objc_build_string_object (tree string)
3349 tree constant_string_class;
3352 struct string_descriptor *desc, key;
3355 /* Prep the string argument. */
3356 string = fix_string_type (string);
3357 TREE_SET_CODE (string, STRING_CST);
3358 length = TREE_STRING_LENGTH (string) - 1;
3360 /* The target may have different ideas on how to construct an ObjC string
3361 literal. On Darwin (Mac OS X), for example, we may wish to obtain a
3362 constant CFString reference instead.
3363 At present, this is only supported for the NeXT runtime. */
3364 if (flag_next_runtime && targetcm.objc_construct_string_object)
3366 tree constructor = (*targetcm.objc_construct_string_object) (string);
3368 return build1 (NOP_EXPR, objc_object_type, constructor);
3371 /* Check whether the string class being used actually exists and has the
3372 correct ivar layout. */
3373 if (!string_layout_checked)
3375 string_layout_checked = -1;
3376 constant_string_class = lookup_interface (constant_string_id);
3377 internal_const_str_type = objc_build_internal_const_str_type ();
3379 if (!constant_string_class
3380 || !(constant_string_type
3381 = CLASS_STATIC_TEMPLATE (constant_string_class)))
3382 error ("cannot find interface declaration for %qE",
3383 constant_string_id);
3384 /* The NSConstantString/NXConstantString ivar layout is now known. */
3385 else if (!check_string_class_template ())
3386 error ("interface %qE does not have valid constant string layout",
3387 constant_string_id);
3388 /* For the NeXT runtime, we can generate a literal reference
3389 to the string class, don't need to run a constructor. */
3390 else if (flag_next_runtime && !setup_string_decl ())
3391 error ("cannot find reference tag for class %qE",
3392 constant_string_id);
3395 string_layout_checked = 1; /* Success! */
3396 add_class_reference (constant_string_id);
3400 if (string_layout_checked == -1)
3401 return error_mark_node;
3403 /* Perhaps we already constructed a constant string just like this one? */
3404 key.literal = string;
3405 loc = htab_find_slot (string_htab, &key, INSERT);
3406 desc = (struct string_descriptor *) *loc;
3410 tree var, constructor;
3411 VEC(constructor_elt,gc) *v = NULL;
3412 *loc = desc = ggc_alloc_string_descriptor ();
3413 desc->literal = string;
3415 /* GNU: (NXConstantString *) & ((__builtin_ObjCString) { NULL, string, length }) */
3416 /* NeXT: (NSConstantString *) & ((__builtin_ObjCString) { isa, string, length }) */
3417 fields = TYPE_FIELDS (internal_const_str_type);
3418 CONSTRUCTOR_APPEND_ELT (v, fields,
3420 ? build_unary_op (input_location,
3421 ADDR_EXPR, string_class_decl, 0)
3422 : build_int_cst (NULL_TREE, 0));
3423 fields = DECL_CHAIN (fields);
3424 CONSTRUCTOR_APPEND_ELT (v, fields,
3425 build_unary_op (input_location,
3426 ADDR_EXPR, string, 1));
3427 fields = DECL_CHAIN (fields);
3428 CONSTRUCTOR_APPEND_ELT (v, fields, build_int_cst (NULL_TREE, length));
3429 constructor = objc_build_constructor (internal_const_str_type, v);
3431 if (!flag_next_runtime)
3433 = objc_add_static_instance (constructor, constant_string_type);
3436 var = build_decl (input_location,
3437 CONST_DECL, NULL, TREE_TYPE (constructor));
3438 DECL_INITIAL (var) = constructor;
3439 TREE_STATIC (var) = 1;
3440 pushdecl_top_level (var);
3443 desc->constructor = constructor;
3446 addr = convert (build_pointer_type (constant_string_type),
3447 build_unary_op (input_location,
3448 ADDR_EXPR, desc->constructor, 1));
3453 /* Declare a static instance of CLASS_DECL initialized by CONSTRUCTOR. */
3455 static GTY(()) int num_static_inst;
3458 objc_add_static_instance (tree constructor, tree class_decl)
3463 /* Find the list of static instances for the CLASS_DECL. Create one if
3465 for (chain = &objc_static_instances;
3466 *chain && TREE_VALUE (*chain) != class_decl;
3467 chain = &TREE_CHAIN (*chain));
3470 *chain = tree_cons (NULL_TREE, class_decl, NULL_TREE);
3471 add_objc_string (OBJC_TYPE_NAME (class_decl), class_names);
3474 sprintf (buf, "_OBJC_INSTANCE_%d", num_static_inst++);
3475 decl = build_decl (input_location,
3476 VAR_DECL, get_identifier (buf), class_decl);
3477 TREE_STATIC (decl) = 1;
3478 DECL_ARTIFICIAL (decl) = 1;
3479 TREE_USED (decl) = 1;
3480 DECL_INITIAL (decl) = constructor;
3482 /* We may be writing something else just now.
3483 Postpone till end of input. */
3484 DECL_DEFER_OUTPUT (decl) = 1;
3485 pushdecl_top_level (decl);
3486 rest_of_decl_compilation (decl, 1, 0);
3488 /* Add the DECL to the head of this CLASS' list. */
3489 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE, decl, TREE_PURPOSE (*chain));
3494 /* Build a static constant CONSTRUCTOR
3495 with type TYPE and elements ELTS. */
3498 objc_build_constructor (tree type, VEC(constructor_elt,gc) *elts)
3500 tree constructor = build_constructor (type, elts);
3502 TREE_CONSTANT (constructor) = 1;
3503 TREE_STATIC (constructor) = 1;
3504 TREE_READONLY (constructor) = 1;
3507 /* Adjust for impedance mismatch. We should figure out how to build
3508 CONSTRUCTORs that consistently please both the C and C++ gods. */
3509 if (!VEC_index (constructor_elt, elts, 0)->index)
3510 TREE_TYPE (constructor) = init_list_type_node;
3516 /* Take care of defining and initializing _OBJC_SYMBOLS. */
3518 /* Predefine the following data type:
3526 void *defs[cls_def_cnt + cat_def_cnt];
3530 build_objc_symtab_template (void)
3532 tree fields, *chain = NULL;
3534 objc_symtab_template = objc_start_struct (get_identifier (UTAG_SYMTAB));
3536 /* long sel_ref_cnt; */
3537 fields = add_field_decl (long_integer_type_node, "sel_ref_cnt", &chain);
3540 add_field_decl (build_pointer_type (objc_selector_type), "refs", &chain);
3542 /* short cls_def_cnt; */
3543 add_field_decl (short_integer_type_node, "cls_def_cnt", &chain);
3545 /* short cat_def_cnt; */
3546 add_field_decl (short_integer_type_node, "cat_def_cnt", &chain);
3548 if (imp_count || cat_count || !flag_next_runtime)
3550 /* void *defs[imp_count + cat_count (+ 1)]; */
3551 /* NB: The index is one less than the size of the array. */
3552 int index = imp_count + cat_count + (flag_next_runtime ? -1: 0);
3553 tree array_type = build_sized_array_type (ptr_type_node, index + 1);
3554 add_field_decl (array_type, "defs", &chain);
3557 objc_finish_struct (objc_symtab_template, fields);
3560 /* Create the initial value for the `defs' field of _objc_symtab.
3561 This is a CONSTRUCTOR. */
3564 init_def_list (tree type)
3567 struct imp_entry *impent;
3568 VEC(constructor_elt,gc) *v = NULL;
3571 for (impent = imp_list; impent; impent = impent->next)
3573 if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
3575 expr = build_unary_op (input_location,
3576 ADDR_EXPR, impent->class_decl, 0);
3577 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3582 for (impent = imp_list; impent; impent = impent->next)
3584 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
3586 expr = build_unary_op (input_location,
3587 ADDR_EXPR, impent->class_decl, 0);
3588 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3592 if (!flag_next_runtime)
3594 /* statics = { ..., _OBJC_STATIC_INSTANCES, ... } */
3595 if (static_instances_decl)
3596 expr = build_unary_op (input_location,
3597 ADDR_EXPR, static_instances_decl, 0);
3599 expr = integer_zero_node;
3601 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3604 return objc_build_constructor (type, v);
3607 /* Construct the initial value for all of _objc_symtab. */
3610 init_objc_symtab (tree type)
3612 VEC(constructor_elt,gc) *v = NULL;
3614 /* sel_ref_cnt = { ..., 5, ... } */
3616 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3617 build_int_cst (long_integer_type_node, 0));
3619 /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
3621 if (flag_next_runtime || ! sel_ref_chain)
3622 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, convert (
3623 build_pointer_type (objc_selector_type),
3624 integer_zero_node));
3627 tree expr = build_unary_op (input_location, ADDR_EXPR,
3628 UOBJC_SELECTOR_TABLE_decl, 1);
3630 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3631 convert (build_pointer_type (objc_selector_type),
3635 /* cls_def_cnt = { ..., 5, ... } */
3637 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3638 build_int_cst (short_integer_type_node, imp_count));
3640 /* cat_def_cnt = { ..., 5, ... } */
3642 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3643 build_int_cst (short_integer_type_node, cat_count));
3645 /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
3647 if (imp_count || cat_count || !flag_next_runtime)
3650 tree field = TYPE_FIELDS (type);
3651 field = DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (field))));
3653 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init_def_list (TREE_TYPE (field)));
3656 return objc_build_constructor (type, v);
3659 /* Generate forward declarations for metadata such as
3660 'OBJC_CLASS_...'. */
3663 build_metadata_decl (const char *name, tree type)
3667 /* struct TYPE NAME_<name>; */
3668 decl = start_var_decl (type, synth_id_with_class_suffix
3670 objc_implementation_context));
3675 /* Push forward-declarations of all the categories so that
3676 init_def_list can use them in a CONSTRUCTOR. */
3679 forward_declare_categories (void)
3681 struct imp_entry *impent;
3682 tree sav = objc_implementation_context;
3684 for (impent = imp_list; impent; impent = impent->next)
3686 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
3688 /* Set an invisible arg to synth_id_with_class_suffix. */
3689 objc_implementation_context = impent->imp_context;
3690 /* extern struct objc_category _OBJC_CATEGORY_<name>; */
3691 impent->class_decl = build_metadata_decl ("_OBJC_CATEGORY",
3692 objc_category_template);
3695 objc_implementation_context = sav;
3698 /* Create the declaration of _OBJC_SYMBOLS, with type `struct _objc_symtab'
3699 and initialized appropriately. */
3702 generate_objc_symtab_decl (void)
3705 build_objc_symtab_template ();
3706 UOBJC_SYMBOLS_decl = start_var_decl (objc_symtab_template, "_OBJC_SYMBOLS");
3707 finish_var_decl (UOBJC_SYMBOLS_decl,
3708 init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl)));
3712 init_module_descriptor (tree type)
3715 VEC(constructor_elt,gc) *v = NULL;
3717 /* version = { 1, ... } */
3719 expr = build_int_cst (long_integer_type_node, OBJC_VERSION);
3720 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3722 /* size = { ..., sizeof (struct _objc_module), ... } */
3724 expr = convert (long_integer_type_node,
3725 size_in_bytes (objc_module_template));
3726 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3728 /* Don't provide any file name for security reasons. */
3729 /* name = { ..., "", ... } */
3731 expr = add_objc_string (get_identifier (""), class_names);
3732 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3734 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
3736 if (UOBJC_SYMBOLS_decl)
3737 expr = build_unary_op (input_location,
3738 ADDR_EXPR, UOBJC_SYMBOLS_decl, 0);
3740 expr = null_pointer_node;
3741 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3743 return objc_build_constructor (type, v);
3746 /* Write out the data structures to describe Objective C classes defined.
3748 struct _objc_module { ... } _OBJC_MODULE = { ... }; */
3751 build_module_descriptor (void)
3753 tree decls, *chain = NULL;
3756 push_lang_context (lang_name_c); /* extern "C" */
3759 objc_module_template = objc_start_struct (get_identifier (UTAG_MODULE));
3762 decls = add_field_decl (long_integer_type_node, "version", &chain);
3765 add_field_decl (long_integer_type_node, "size", &chain);
3768 add_field_decl (string_type_node, "name", &chain);
3770 /* struct _objc_symtab *symtab; */
3771 add_field_decl (build_pointer_type (xref_tag (RECORD_TYPE,
3772 get_identifier (UTAG_SYMTAB))),
3775 objc_finish_struct (objc_module_template, decls);
3777 /* Create an instance of "_objc_module". */
3778 UOBJC_MODULES_decl = start_var_decl (objc_module_template, "_OBJC_MODULES");
3779 /* This is the root of the metadata for defined classes and categories, it
3780 is referenced by the runtime and, therefore, needed. */
3781 DECL_PRESERVE_P (UOBJC_MODULES_decl) = 1;
3782 finish_var_decl (UOBJC_MODULES_decl,
3783 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl)));
3786 pop_lang_context ();
3790 /* The GNU runtime requires us to provide a static initializer function
3793 static void __objc_gnu_init (void) {
3794 __objc_exec_class (&L_OBJC_MODULES);
3798 build_module_initializer_routine (void)
3803 push_lang_context (lang_name_c); /* extern "C" */
3806 objc_push_parm (build_decl (input_location,
3807 PARM_DECL, NULL_TREE, void_type_node));
3809 objc_start_function (get_identifier (TAG_GNUINIT),
3810 build_function_type_list (void_type_node, NULL_TREE),
3811 NULL_TREE, NULL_TREE);
3813 objc_start_function (get_identifier (TAG_GNUINIT),
3814 build_function_type_list (void_type_node, NULL_TREE),
3815 NULL_TREE, objc_get_parm_info (0));
3817 body = c_begin_compound_stmt (true);
3818 add_stmt (build_function_call
3823 build_unary_op (input_location, ADDR_EXPR,
3824 UOBJC_MODULES_decl, 0))));
3825 add_stmt (c_end_compound_stmt (input_location, body, true));
3827 TREE_PUBLIC (current_function_decl) = 0;
3830 /* For Objective-C++, we will need to call __objc_gnu_init
3831 from objc_generate_static_init_call() below. */
3832 DECL_STATIC_CONSTRUCTOR (current_function_decl) = 1;
3835 GNU_INIT_decl = current_function_decl;
3839 pop_lang_context ();
3844 /* Return 1 if the __objc_gnu_init function has been synthesized and needs
3845 to be called by the module initializer routine. */
3848 objc_static_init_needed_p (void)
3850 return (GNU_INIT_decl != NULL_TREE);
3853 /* Generate a call to the __objc_gnu_init initializer function. */
3856 objc_generate_static_init_call (tree ctors ATTRIBUTE_UNUSED)
3858 add_stmt (build_stmt (input_location, EXPR_STMT,
3859 build_function_call (input_location,
3860 GNU_INIT_decl, NULL_TREE)));
3864 #endif /* OBJCPLUS */
3866 /* Return the DECL of the string IDENT in the SECTION. */
3869 get_objc_string_decl (tree ident, enum string_section section)
3876 chain = class_names_chain;
3878 case meth_var_names:
3879 chain = meth_var_names_chain;
3881 case meth_var_types:
3882 chain = meth_var_types_chain;
3888 for (; chain != 0; chain = TREE_CHAIN (chain))
3889 if (TREE_VALUE (chain) == ident)
3890 return (TREE_PURPOSE (chain));
3896 /* Output references to all statically allocated objects. Return the DECL
3897 for the array built. */
3900 generate_static_references (void)
3902 tree expr = NULL_TREE;
3903 tree class_name, klass, decl;
3904 tree cl_chain, in_chain, type
3905 = build_array_type (build_pointer_type (void_type_node), NULL_TREE);
3906 int num_inst, num_class;
3908 VEC(constructor_elt,gc) *decls = NULL;
3910 if (flag_next_runtime)
3913 for (cl_chain = objc_static_instances, num_class = 0;
3914 cl_chain; cl_chain = TREE_CHAIN (cl_chain), num_class++)
3916 VEC(constructor_elt,gc) *v = NULL;
3918 for (num_inst = 0, in_chain = TREE_PURPOSE (cl_chain);
3919 in_chain; num_inst++, in_chain = TREE_CHAIN (in_chain));
3921 sprintf (buf, "_OBJC_STATIC_INSTANCES_%d", num_class);
3922 decl = start_var_decl (type, buf);
3924 /* Output {class_name, ...}. */
3925 klass = TREE_VALUE (cl_chain);
3926 class_name = get_objc_string_decl (OBJC_TYPE_NAME (klass), class_names);
3927 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3928 build_unary_op (input_location,
3929 ADDR_EXPR, class_name, 1));
3931 /* Output {..., instance, ...}. */
3932 for (in_chain = TREE_PURPOSE (cl_chain);
3933 in_chain; in_chain = TREE_CHAIN (in_chain))
3935 expr = build_unary_op (input_location,
3936 ADDR_EXPR, TREE_VALUE (in_chain), 1);
3937 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3940 /* Output {..., NULL}. */
3941 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
3943 expr = objc_build_constructor (TREE_TYPE (decl), v);
3944 finish_var_decl (decl, expr);
3945 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE,
3946 build_unary_op (input_location,
3947 ADDR_EXPR, decl, 1));
3950 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE, build_int_cst (NULL_TREE, 0));
3951 expr = objc_build_constructor (type, decls);
3952 static_instances_decl = start_var_decl (type, "_OBJC_STATIC_INSTANCES");
3953 finish_var_decl (static_instances_decl, expr);
3956 static GTY(()) int selector_reference_idx;
3959 build_selector_reference_decl (void)
3964 sprintf (buf, "_OBJC_SELECTOR_REFERENCES_%d", selector_reference_idx++);
3965 decl = start_var_decl (objc_selector_type, buf);
3971 build_selector_table_decl (void)
3975 if (flag_typed_selectors)
3977 build_selector_template ();
3978 temp = build_array_type (objc_selector_template, NULL_TREE);
3981 temp = build_array_type (objc_selector_type, NULL_TREE);
3983 UOBJC_SELECTOR_TABLE_decl = start_var_decl (temp, "_OBJC_SELECTOR_TABLE");
3986 /* Just a handy wrapper for add_objc_string. */
3989 build_selector (tree ident)
3991 return convert (objc_selector_type,
3992 add_objc_string (ident, meth_var_names));
3995 /* Used only by build_*_selector_translation_table (). */
3997 diagnose_missing_method (tree meth, location_t here)
4001 for (method_chain = meth_var_names_chain;
4003 method_chain = TREE_CHAIN (method_chain))
4005 if (TREE_VALUE (method_chain) == meth)
4013 warning_at (here, 0, "creating selector for nonexistent method %qE",
4018 build_next_selector_translation_table (void)
4021 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
4024 tree decl = TREE_PURPOSE (chain);
4025 if (warn_selector && objc_implementation_context)
4029 loc = DECL_SOURCE_LOCATION (decl);
4031 loc = input_location;
4032 diagnose_missing_method (TREE_VALUE (chain), loc);
4035 expr = build_selector (TREE_VALUE (chain));
4039 /* Entries of this form are used for references to methods.
4040 The runtime re-writes these on start-up, but the compiler can't see
4041 that and optimizes it away unless we force it. */
4042 DECL_PRESERVE_P (decl) = 1;
4043 finish_var_decl (decl, expr);
4049 build_gnu_selector_translation_table (void)
4053 tree decl = NULL_TREE;*/
4054 VEC(constructor_elt,gc) *inits = NULL;
4056 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
4060 if (warn_selector && objc_implementation_context)
4061 diagnose_missing_method (TREE_VALUE (chain), input_location);
4063 expr = build_selector (TREE_VALUE (chain));
4064 /* add one for the '\0' character
4065 offset += IDENTIFIER_LENGTH (TREE_VALUE (chain)) + 1;*/
4068 if (flag_typed_selectors)
4070 VEC(constructor_elt,gc) *v = NULL;
4071 tree encoding = get_proto_encoding (TREE_PURPOSE (chain));
4072 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
4073 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, encoding);
4074 expr = objc_build_constructor (objc_selector_template, v);
4077 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
4079 } /* each element in the chain */
4082 /* Cause the selector table (previously forward-declared)
4083 to be actually output. */
4086 if (flag_typed_selectors)
4088 VEC(constructor_elt,gc) *v = NULL;
4089 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
4090 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
4091 expr = objc_build_constructor (objc_selector_template, v);
4094 expr = integer_zero_node;
4096 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
4097 expr = objc_build_constructor (TREE_TYPE (UOBJC_SELECTOR_TABLE_decl),
4099 finish_var_decl (UOBJC_SELECTOR_TABLE_decl, expr);
4104 get_proto_encoding (tree proto)
4109 if (! METHOD_ENCODING (proto))
4111 encoding = encode_method_prototype (proto);
4112 METHOD_ENCODING (proto) = encoding;
4115 encoding = METHOD_ENCODING (proto);
4117 return add_objc_string (encoding, meth_var_types);
4120 return build_int_cst (NULL_TREE, 0);
4123 /* sel_ref_chain is a list whose "value" fields will be instances of
4124 identifier_node that represent the selector. LOC is the location of
4128 build_typed_selector_reference (location_t loc, tree ident, tree prototype)
4130 tree *chain = &sel_ref_chain;
4136 if (TREE_PURPOSE (*chain) == prototype && TREE_VALUE (*chain) == ident)
4137 goto return_at_index;
4140 chain = &TREE_CHAIN (*chain);
4143 *chain = tree_cons (prototype, ident, NULL_TREE);
4146 expr = build_unary_op (loc, ADDR_EXPR,
4147 build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
4148 build_int_cst (NULL_TREE, index)),
4150 return convert (objc_selector_type, expr);
4154 build_selector_reference (location_t loc, tree ident)
4156 tree *chain = &sel_ref_chain;
4162 if (TREE_VALUE (*chain) == ident)
4163 return (flag_next_runtime
4164 ? TREE_PURPOSE (*chain)
4165 : build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
4166 build_int_cst (NULL_TREE, index)));
4169 chain = &TREE_CHAIN (*chain);
4172 expr = (flag_next_runtime ? build_selector_reference_decl (): NULL_TREE);
4174 *chain = tree_cons (expr, ident, NULL_TREE);
4176 return (flag_next_runtime
4178 : build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
4179 build_int_cst (NULL_TREE, index)));
4182 static GTY(()) int class_reference_idx;
4185 build_class_reference_decl (void)
4190 sprintf (buf, "_OBJC_CLASS_REFERENCES_%d", class_reference_idx++);
4191 decl = start_var_decl (objc_class_type, buf);
4196 /* Create a class reference, but don't create a variable to reference
4200 add_class_reference (tree ident)
4204 if ((chain = cls_ref_chain))
4209 if (ident == TREE_VALUE (chain))
4213 chain = TREE_CHAIN (chain);
4217 /* Append to the end of the list */
4218 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
4221 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
4224 /* Get a class reference, creating it if necessary. Also create the
4225 reference variable. */
4227 objc_get_class_reference (tree ident)
4229 tree orig_ident = (DECL_P (ident)
4232 ? OBJC_TYPE_NAME (ident)
4234 bool local_scope = false;
4237 if (processing_template_decl)
4238 /* Must wait until template instantiation time. */
4239 return build_min_nt (CLASS_REFERENCE_EXPR, ident);
4242 if (TREE_CODE (ident) == TYPE_DECL)
4243 ident = (DECL_ORIGINAL_TYPE (ident)
4244 ? DECL_ORIGINAL_TYPE (ident)
4245 : TREE_TYPE (ident));
4249 && CP_TYPE_CONTEXT (ident) != global_namespace)
4253 if (local_scope || !(ident = objc_is_class_name (ident)))
4255 error ("%qE is not an Objective-C class name or alias",
4257 return error_mark_node;
4260 if (flag_next_runtime && !flag_zero_link)
4265 for (chain = &cls_ref_chain; *chain; chain = &TREE_CHAIN (*chain))
4266 if (TREE_VALUE (*chain) == ident)
4268 if (! TREE_PURPOSE (*chain))
4269 TREE_PURPOSE (*chain) = build_class_reference_decl ();
4271 return TREE_PURPOSE (*chain);
4274 decl = build_class_reference_decl ();
4275 *chain = tree_cons (decl, ident, NULL_TREE);
4282 add_class_reference (ident);
4284 params = build_tree_list (NULL_TREE,
4285 my_build_string_pointer
4286 (IDENTIFIER_LENGTH (ident) + 1,
4287 IDENTIFIER_POINTER (ident)));
4289 assemble_external (objc_get_class_decl);
4290 return build_function_call (input_location, objc_get_class_decl, params);
4294 /* For each string section we have a chain which maps identifier nodes
4295 to decls for the strings. */
4297 static GTY(()) int class_names_idx;
4298 static GTY(()) int meth_var_names_idx;
4299 static GTY(()) int meth_var_types_idx;
4302 add_objc_string (tree ident, enum string_section section)
4304 tree *chain, decl, type, string_expr;
4311 chain = &class_names_chain;
4312 sprintf (buf, "_OBJC_CLASS_NAME_%d", class_names_idx++);
4314 case meth_var_names:
4315 chain = &meth_var_names_chain;
4316 sprintf (buf, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx++);
4318 case meth_var_types:
4319 chain = &meth_var_types_chain;
4320 sprintf (buf, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx++);
4328 if (TREE_VALUE (*chain) == ident)
4329 return convert (string_type_node,
4330 build_unary_op (input_location,
4331 ADDR_EXPR, TREE_PURPOSE (*chain), 1));
4333 chain = &TREE_CHAIN (*chain);
4336 type = build_sized_array_type (char_type_node, IDENTIFIER_LENGTH (ident) + 1);
4337 decl = start_var_decl (type, buf);
4338 string_expr = my_build_string (IDENTIFIER_LENGTH (ident) + 1,
4339 IDENTIFIER_POINTER (ident));
4340 TREE_CONSTANT (decl) = 1;
4341 finish_var_decl (decl, string_expr);
4343 *chain = tree_cons (decl, ident, NULL_TREE);
4345 return convert (string_type_node, build_unary_op (input_location,
4346 ADDR_EXPR, decl, 1));
4350 objc_declare_alias (tree alias_ident, tree class_ident)
4352 tree underlying_class;
4355 if (current_namespace != global_namespace) {
4356 error ("Objective-C declarations may only appear in global scope");
4358 #endif /* OBJCPLUS */
4360 if (!(underlying_class = objc_is_class_name (class_ident)))
4361 warning (0, "cannot find class %qE", class_ident);
4362 else if (objc_is_class_name (alias_ident))
4363 warning (0, "class %qE already exists", alias_ident);
4366 /* Implement @compatibility_alias as a typedef. */
4368 push_lang_context (lang_name_c); /* extern "C" */
4370 lang_hooks.decls.pushdecl (build_decl
4374 xref_tag (RECORD_TYPE, underlying_class)));
4376 pop_lang_context ();
4378 hash_class_name_enter (als_name_hash_list, alias_ident,
4384 objc_declare_class (tree ident_list)
4388 if (current_namespace != global_namespace) {
4389 error ("Objective-C declarations may only appear in global scope");
4391 #endif /* OBJCPLUS */
4393 for (list = ident_list; list; list = TREE_CHAIN (list))
4395 tree ident = TREE_VALUE (list);
4397 if (! objc_is_class_name (ident))
4399 tree record = lookup_name (ident), type = record;
4403 if (TREE_CODE (record) == TYPE_DECL)
4404 type = DECL_ORIGINAL_TYPE (record) ?
4405 DECL_ORIGINAL_TYPE (record) :
4408 if (!TYPE_HAS_OBJC_INFO (type)
4409 || !TYPE_OBJC_INTERFACE (type))
4411 error ("%qE redeclared as different kind of symbol",
4413 error ("previous declaration of %q+D",
4418 record = xref_tag (RECORD_TYPE, ident);
4419 INIT_TYPE_OBJC_INFO (record);
4420 TYPE_OBJC_INTERFACE (record) = ident;
4421 hash_class_name_enter (cls_name_hash_list, ident, NULL_TREE);
4427 objc_is_class_name (tree ident)
4431 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE
4432 && identifier_global_value (ident))
4433 ident = identifier_global_value (ident);
4434 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
4435 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
4437 if (ident && TREE_CODE (ident) == RECORD_TYPE)
4438 ident = OBJC_TYPE_NAME (ident);
4440 if (ident && TREE_CODE (ident) == TYPE_DECL)
4442 tree type = TREE_TYPE (ident);
4443 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
4445 ident = DECL_NAME (ident);
4448 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
4451 if (lookup_interface (ident))
4454 target = hash_class_name_lookup (cls_name_hash_list, ident);
4458 target = hash_class_name_lookup (als_name_hash_list, ident);
4461 gcc_assert (target->list && target->list->value);
4462 return target->list->value;
4468 /* Check whether TYPE is either 'id' or 'Class'. */
4471 objc_is_id (tree type)
4473 if (type && TREE_CODE (type) == IDENTIFIER_NODE
4474 && identifier_global_value (type))
4475 type = identifier_global_value (type);
4477 if (type && TREE_CODE (type) == TYPE_DECL)
4478 type = TREE_TYPE (type);
4480 /* NB: This function may be called before the ObjC front-end has
4481 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
4482 return (objc_object_type && type
4483 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
4488 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
4489 class instance. This is needed by other parts of the compiler to
4490 handle ObjC types gracefully. */
4493 objc_is_object_ptr (tree type)
4497 type = TYPE_MAIN_VARIANT (type);
4498 if (!POINTER_TYPE_P (type))
4501 ret = objc_is_id (type);
4503 ret = objc_is_class_name (TREE_TYPE (type));
4509 objc_is_gcable_type (tree type, int or_strong_p)
4515 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
4517 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
4519 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
4521 type = TREE_TYPE (type);
4522 if (TREE_CODE (type) != RECORD_TYPE)
4524 name = TYPE_NAME (type);
4525 return (objc_is_class_name (name) != NULL_TREE);
4529 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
4531 if (expr == oldexpr)
4534 switch (TREE_CODE (expr))
4537 return objc_build_component_ref
4538 (objc_substitute_decl (TREE_OPERAND (expr, 0),
4541 DECL_NAME (TREE_OPERAND (expr, 1)));
4543 return build_array_ref (input_location,
4544 objc_substitute_decl (TREE_OPERAND (expr, 0),
4547 TREE_OPERAND (expr, 1));
4549 return build_indirect_ref (input_location,
4550 objc_substitute_decl (TREE_OPERAND (expr, 0),
4552 newexpr), RO_ARROW);
4559 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
4562 /* The LHS parameter contains the expression 'outervar->memberspec';
4563 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
4564 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
4567 = objc_substitute_decl
4568 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
4570 = (flag_objc_direct_dispatch
4571 ? objc_assign_ivar_fast_decl
4572 : objc_assign_ivar_decl);
4574 offs = convert (integer_type_node, build_unary_op (input_location,
4575 ADDR_EXPR, offs, 0));
4577 func_params = tree_cons (NULL_TREE,
4578 convert (objc_object_type, rhs),
4579 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
4580 tree_cons (NULL_TREE, offs,
4583 assemble_external (func);
4584 return build_function_call (input_location, func, func_params);
4588 objc_build_global_assignment (tree lhs, tree rhs)
4590 tree func_params = tree_cons (NULL_TREE,
4591 convert (objc_object_type, rhs),
4592 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
4593 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
4596 assemble_external (objc_assign_global_decl);
4597 return build_function_call (input_location,
4598 objc_assign_global_decl, func_params);
4602 objc_build_strong_cast_assignment (tree lhs, tree rhs)
4604 tree func_params = tree_cons (NULL_TREE,
4605 convert (objc_object_type, rhs),
4606 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
4607 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
4610 assemble_external (objc_assign_strong_cast_decl);
4611 return build_function_call (input_location,
4612 objc_assign_strong_cast_decl, func_params);
4616 objc_is_gcable_p (tree expr)
4618 return (TREE_CODE (expr) == COMPONENT_REF
4619 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
4620 : TREE_CODE (expr) == ARRAY_REF
4621 ? (objc_is_gcable_p (TREE_TYPE (expr))
4622 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
4623 : TREE_CODE (expr) == ARRAY_TYPE
4624 ? objc_is_gcable_p (TREE_TYPE (expr))
4626 ? objc_is_gcable_type (expr, 1)
4627 : (objc_is_gcable_p (TREE_TYPE (expr))
4629 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
4633 objc_is_ivar_reference_p (tree expr)
4635 return (TREE_CODE (expr) == ARRAY_REF
4636 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
4637 : TREE_CODE (expr) == COMPONENT_REF
4638 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
4643 objc_is_global_reference_p (tree expr)
4645 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
4646 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
4648 ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr))
4653 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
4655 tree result = NULL_TREE, outer;
4656 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
4658 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
4659 will have been transformed to the form '*(type *)&expr'. */
4660 if (TREE_CODE (lhs) == INDIRECT_REF)
4662 outer = TREE_OPERAND (lhs, 0);
4664 while (!strong_cast_p
4665 && (CONVERT_EXPR_P (outer)
4666 || TREE_CODE (outer) == NON_LVALUE_EXPR))
4668 tree lhstype = TREE_TYPE (outer);
4670 /* Descend down the cast chain, and record the first objc_gc
4672 if (POINTER_TYPE_P (lhstype))
4675 = lookup_attribute ("objc_gc",
4676 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
4682 outer = TREE_OPERAND (outer, 0);
4686 /* If we have a __strong cast, it trumps all else. */
4689 if (modifycode != NOP_EXPR)
4690 goto invalid_pointer_arithmetic;
4692 if (warn_assign_intercept)
4693 warning (0, "strong-cast assignment has been intercepted");
4695 result = objc_build_strong_cast_assignment (lhs, rhs);
4700 /* the lhs must be of a suitable type, regardless of its underlying
4702 if (!objc_is_gcable_p (lhs))
4708 && (TREE_CODE (outer) == COMPONENT_REF
4709 || TREE_CODE (outer) == ARRAY_REF))
4710 outer = TREE_OPERAND (outer, 0);
4712 if (TREE_CODE (outer) == INDIRECT_REF)
4714 outer = TREE_OPERAND (outer, 0);
4718 outer_gc_p = objc_is_gcable_p (outer);
4720 /* Handle ivar assignments. */
4721 if (objc_is_ivar_reference_p (lhs))
4723 /* if the struct to the left of the ivar is not an Objective-C object (__strong
4724 doesn't cut it here), the best we can do here is suggest a cast. */
4725 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
4727 /* We may still be able to use the global write barrier... */
4728 if (!indirect_p && objc_is_global_reference_p (outer))
4729 goto global_reference;
4732 if (modifycode == NOP_EXPR)
4734 if (warn_assign_intercept)
4735 warning (0, "strong-cast may possibly be needed");
4741 if (modifycode != NOP_EXPR)
4742 goto invalid_pointer_arithmetic;
4744 if (warn_assign_intercept)
4745 warning (0, "instance variable assignment has been intercepted");
4747 result = objc_build_ivar_assignment (outer, lhs, rhs);
4752 /* Likewise, intercept assignment to global/static variables if their type is
4754 if (objc_is_global_reference_p (outer))
4760 if (modifycode != NOP_EXPR)
4762 invalid_pointer_arithmetic:
4764 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
4769 if (warn_assign_intercept)
4770 warning (0, "global/static variable assignment has been intercepted");
4772 result = objc_build_global_assignment (lhs, rhs);
4775 /* In all other cases, fall back to the normal mechanism. */
4780 struct GTY(()) interface_tuple {
4785 static GTY ((param_is (struct interface_tuple))) htab_t interface_htab;
4788 hash_interface (const void *p)
4790 const struct interface_tuple *d = (const struct interface_tuple *) p;
4791 return IDENTIFIER_HASH_VALUE (d->id);
4795 eq_interface (const void *p1, const void *p2)
4797 const struct interface_tuple *d = (const struct interface_tuple *) p1;
4802 lookup_interface (tree ident)
4805 if (ident && TREE_CODE (ident) == TYPE_DECL)
4806 ident = DECL_NAME (ident);
4809 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
4813 struct interface_tuple **slot;
4818 slot = (struct interface_tuple **)
4819 htab_find_slot_with_hash (interface_htab, ident,
4820 IDENTIFIER_HASH_VALUE (ident),
4823 i = (*slot)->class_name;
4829 /* Implement @defs (<classname>) within struct bodies. */
4832 objc_get_class_ivars (tree class_name)
4834 tree interface = lookup_interface (class_name);
4837 return get_class_ivars (interface, true);
4839 error ("cannot find interface declaration for %qE",
4842 return error_mark_node;
4845 /* Called when checking the variables in a struct. If we are not
4846 doing the ivars list inside an @interface context, then returns
4847 fieldlist unchanged. Else, returns the list of class ivars.
4850 objc_get_interface_ivars (tree fieldlist)
4852 if (!objc_collecting_ivars || !objc_interface_context
4853 || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE
4854 || CLASS_SUPER_NAME (objc_interface_context) == NULL_TREE)
4857 return get_class_ivars (objc_interface_context, true);
4860 /* Used by: build_private_template, continue_class,
4861 and for @defs constructs. */
4864 get_class_ivars (tree interface, bool inherited)
4866 tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface));
4868 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
4869 by the current class (i.e., they do not include super-class ivars).
4870 However, the CLASS_IVARS list will be side-effected by a call to
4871 finish_struct(), which will fill in field offsets. */
4872 if (!CLASS_IVARS (interface))
4873 CLASS_IVARS (interface) = ivar_chain;
4878 while (CLASS_SUPER_NAME (interface))
4880 /* Prepend super-class ivars. */
4881 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4882 ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)),
4890 /* Exception handling constructs. We begin by having the parser do most
4891 of the work and passing us blocks. What we do next depends on whether
4892 we're doing "native" exception handling or legacy Darwin setjmp exceptions.
4893 We abstract all of this in a handful of appropriately named routines. */
4895 /* Stack of open try blocks. */
4897 struct objc_try_context
4899 struct objc_try_context *outer;
4901 /* Statements (or statement lists) as processed by the parser. */
4905 /* Some file position locations. */
4906 location_t try_locus;
4907 location_t end_try_locus;
4908 location_t end_catch_locus;
4909 location_t finally_locus;
4910 location_t end_finally_locus;
4912 /* A STATEMENT_LIST of CATCH_EXPRs, appropriate for sticking into op1
4913 of a TRY_CATCH_EXPR. Even when doing Darwin setjmp. */
4916 /* The CATCH_EXPR of an open @catch clause. */
4919 /* The VAR_DECL holding the Darwin equivalent of __builtin_eh_pointer. */
4925 static struct objc_try_context *cur_try_context;
4927 static GTY(()) tree objc_eh_personality_decl;
4929 /* This hook, called via lang_eh_runtime_type, generates a runtime object
4930 that represents TYPE. For Objective-C, this is just the class name. */
4931 /* ??? Isn't there a class object or some such? Is it easy to get? */
4935 objc_eh_runtime_type (tree type)
4937 return add_objc_string (OBJC_TYPE_NAME (TREE_TYPE (type)), class_names);
4941 objc_eh_personality (void)
4943 if (!flag_objc_sjlj_exceptions && !objc_eh_personality_decl)
4944 objc_eh_personality_decl = build_personality_function ("gnu_objc");
4945 return objc_eh_personality_decl;
4949 /* Build __builtin_eh_pointer, or the moral equivalent. In the case
4950 of Darwin, we'll arrange for it to be initialized (and associated
4951 with a binding) later. */
4954 objc_build_exc_ptr (void)
4956 if (flag_objc_sjlj_exceptions)
4958 tree var = cur_try_context->caught_decl;
4961 var = objc_create_temporary_var (objc_object_type, NULL);
4962 cur_try_context->caught_decl = var;
4969 t = built_in_decls[BUILT_IN_EH_POINTER];
4970 t = build_call_expr (t, 1, integer_zero_node);
4971 return fold_convert (objc_object_type, t);
4975 /* Build "objc_exception_try_exit(&_stack)". */
4978 next_sjlj_build_try_exit (void)
4981 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
4982 t = tree_cons (NULL, t, NULL);
4983 t = build_function_call (input_location,
4984 objc_exception_try_exit_decl, t);
4989 objc_exception_try_enter (&_stack);
4990 if (_setjmp(&_stack.buf))
4994 Return the COND_EXPR. Note that the THEN and ELSE fields are left
4995 empty, ready for the caller to fill them in. */
4998 next_sjlj_build_enter_and_setjmp (void)
5000 tree t, enter, sj, cond;
5002 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
5003 t = tree_cons (NULL, t, NULL);
5004 enter = build_function_call (input_location,
5005 objc_exception_try_enter_decl, t);
5007 t = objc_build_component_ref (cur_try_context->stack_decl,
5008 get_identifier ("buf"));
5009 t = build_fold_addr_expr_loc (input_location, t);
5011 /* Convert _setjmp argument to type that is expected. */
5012 if (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl)))
5013 t = convert (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl))), t);
5015 t = convert (ptr_type_node, t);
5017 t = convert (ptr_type_node, t);
5019 t = tree_cons (NULL, t, NULL);
5020 sj = build_function_call (input_location,
5021 objc_setjmp_decl, t);
5023 cond = build2 (COMPOUND_EXPR, TREE_TYPE (sj), enter, sj);
5024 cond = c_common_truthvalue_conversion (input_location, cond);
5026 return build3 (COND_EXPR, void_type_node, cond, NULL, NULL);
5031 DECL = objc_exception_extract(&_stack); */
5034 next_sjlj_build_exc_extract (tree decl)
5038 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
5039 t = tree_cons (NULL, t, NULL);
5040 t = build_function_call (input_location,
5041 objc_exception_extract_decl, t);
5042 t = convert (TREE_TYPE (decl), t);
5043 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
5049 if (objc_exception_match(obj_get_class(TYPE), _caught)
5056 objc_exception_try_exit(&_stack);
5058 from the sequence of CATCH_EXPRs in the current try context. */
5061 next_sjlj_build_catch_list (void)
5063 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
5065 tree *last = &catch_seq;
5066 bool saw_id = false;
5068 for (; !tsi_end_p (i); tsi_next (&i))
5070 tree stmt = tsi_stmt (i);
5071 tree type = CATCH_TYPES (stmt);
5072 tree body = CATCH_BODY (stmt);
5084 if (type == error_mark_node)
5085 cond = error_mark_node;
5088 args = tree_cons (NULL, cur_try_context->caught_decl, NULL);
5089 t = objc_get_class_reference (OBJC_TYPE_NAME (TREE_TYPE (type)));
5090 args = tree_cons (NULL, t, args);
5091 t = build_function_call (input_location,
5092 objc_exception_match_decl, args);
5093 cond = c_common_truthvalue_conversion (input_location, t);
5095 t = build3 (COND_EXPR, void_type_node, cond, body, NULL);
5096 SET_EXPR_LOCATION (t, EXPR_LOCATION (stmt));
5099 last = &COND_EXPR_ELSE (t);
5105 t = build2 (MODIFY_EXPR, void_type_node, cur_try_context->rethrow_decl,
5106 cur_try_context->caught_decl);
5107 SET_EXPR_LOCATION (t, cur_try_context->end_catch_locus);
5108 append_to_statement_list (t, last);
5110 t = next_sjlj_build_try_exit ();
5111 SET_EXPR_LOCATION (t, cur_try_context->end_catch_locus);
5112 append_to_statement_list (t, last);
5118 /* Build a complete @try-@catch-@finally block for legacy Darwin setjmp
5119 exception handling. We aim to build:
5122 struct _objc_exception_data _stack;
5126 objc_exception_try_enter (&_stack);
5127 if (_setjmp(&_stack.buf))
5129 id _caught = objc_exception_extract(&_stack);
5130 objc_exception_try_enter (&_stack);
5131 if (_setjmp(&_stack.buf))
5132 _rethrow = objc_exception_extract(&_stack);
5142 objc_exception_try_exit(&_stack);
5145 objc_exception_throw(_rethrow);
5149 If CATCH-LIST is empty, we can omit all of the block containing
5150 "_caught" except for the setting of _rethrow. Note the use of
5151 a real TRY_FINALLY_EXPR here, which is not involved in EH per-se,
5152 but handles goto and other exits from the block. */
5155 next_sjlj_build_try_catch_finally (void)
5157 tree rethrow_decl, stack_decl, t;
5158 tree catch_seq, try_fin, bind;
5160 /* Create the declarations involved. */
5161 t = xref_tag (RECORD_TYPE, get_identifier (UTAG_EXCDATA));
5162 stack_decl = objc_create_temporary_var (t, NULL);
5163 cur_try_context->stack_decl = stack_decl;
5165 rethrow_decl = objc_create_temporary_var (objc_object_type, NULL);
5166 cur_try_context->rethrow_decl = rethrow_decl;
5167 TREE_CHAIN (rethrow_decl) = stack_decl;
5169 /* Build the outermost variable binding level. */
5170 bind = build3 (BIND_EXPR, void_type_node, rethrow_decl, NULL, NULL);
5171 SET_EXPR_LOCATION (bind, cur_try_context->try_locus);
5172 TREE_SIDE_EFFECTS (bind) = 1;
5174 /* Initialize rethrow_decl. */
5175 t = build2 (MODIFY_EXPR, void_type_node, rethrow_decl,
5176 convert (objc_object_type, null_pointer_node));
5177 SET_EXPR_LOCATION (t, cur_try_context->try_locus);
5178 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
5180 /* Build the outermost TRY_FINALLY_EXPR. */
5181 try_fin = build2 (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
5182 SET_EXPR_LOCATION (try_fin, cur_try_context->try_locus);
5183 TREE_SIDE_EFFECTS (try_fin) = 1;
5184 append_to_statement_list (try_fin, &BIND_EXPR_BODY (bind));
5186 /* Create the complete catch sequence. */
5187 if (cur_try_context->catch_list)
5189 tree caught_decl = objc_build_exc_ptr ();
5190 catch_seq = build_stmt (input_location, BIND_EXPR, caught_decl, NULL, NULL);
5191 TREE_SIDE_EFFECTS (catch_seq) = 1;
5193 t = next_sjlj_build_exc_extract (caught_decl);
5194 append_to_statement_list (t, &BIND_EXPR_BODY (catch_seq));
5196 t = next_sjlj_build_enter_and_setjmp ();
5197 COND_EXPR_THEN (t) = next_sjlj_build_exc_extract (rethrow_decl);
5198 COND_EXPR_ELSE (t) = next_sjlj_build_catch_list ();
5199 append_to_statement_list (t, &BIND_EXPR_BODY (catch_seq));
5202 catch_seq = next_sjlj_build_exc_extract (rethrow_decl);
5203 SET_EXPR_LOCATION (catch_seq, cur_try_context->end_try_locus);
5205 /* Build the main register-and-try if statement. */
5206 t = next_sjlj_build_enter_and_setjmp ();
5207 SET_EXPR_LOCATION (t, cur_try_context->try_locus);
5208 COND_EXPR_THEN (t) = catch_seq;
5209 COND_EXPR_ELSE (t) = cur_try_context->try_body;
5210 TREE_OPERAND (try_fin, 0) = t;
5212 /* Build the complete FINALLY statement list. */
5213 t = next_sjlj_build_try_exit ();
5214 t = build_stmt (input_location, COND_EXPR,
5215 c_common_truthvalue_conversion
5216 (input_location, rethrow_decl),
5218 SET_EXPR_LOCATION (t, cur_try_context->finally_locus);
5219 append_to_statement_list (t, &TREE_OPERAND (try_fin, 1));
5221 append_to_statement_list (cur_try_context->finally_body,
5222 &TREE_OPERAND (try_fin, 1));
5224 t = tree_cons (NULL, rethrow_decl, NULL);
5225 t = build_function_call (input_location,
5226 objc_exception_throw_decl, t);
5227 t = build_stmt (input_location, COND_EXPR,
5228 c_common_truthvalue_conversion (input_location,
5231 SET_EXPR_LOCATION (t, cur_try_context->end_finally_locus);
5232 append_to_statement_list (t, &TREE_OPERAND (try_fin, 1));
5237 /* Called just after parsing the @try and its associated BODY. We now
5238 must prepare for the tricky bits -- handling the catches and finally. */
5241 objc_begin_try_stmt (location_t try_locus, tree body)
5243 struct objc_try_context *c = XCNEW (struct objc_try_context);
5244 c->outer = cur_try_context;
5246 c->try_locus = try_locus;
5247 c->end_try_locus = input_location;
5248 cur_try_context = c;
5250 /* -fobjc-exceptions is required to enable Objective-C exceptions.
5251 For example, on Darwin, ObjC exceptions require a sufficiently
5252 recent version of the runtime, so the user must ask for them
5253 explicitly. On other platforms, at the moment -fobjc-exceptions
5254 triggers -fexceptions which again is required for exceptions to
5257 if (!flag_objc_exceptions)
5259 error_at (try_locus, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
5262 if (flag_objc_sjlj_exceptions)
5263 objc_mark_locals_volatile (NULL);
5266 /* Called just after parsing "@catch (parm)". Open a binding level,
5267 enter DECL into the binding level, and initialize it. Leave the
5268 binding level open while the body of the compound statement is parsed. */
5271 objc_begin_catch_clause (tree decl)
5273 tree compound, type, t;
5275 /* Begin a new scope that the entire catch clause will live in. */
5276 compound = c_begin_compound_stmt (true);
5278 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
5279 decl = build_decl (input_location,
5280 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
5281 lang_hooks.decls.pushdecl (decl);
5283 /* Since a decl is required here by syntax, don't warn if its unused. */
5284 /* ??? As opposed to __attribute__((unused))? Anyway, this appears to
5285 be what the previous objc implementation did. */
5286 TREE_USED (decl) = 1;
5287 DECL_READ_P (decl) = 1;
5289 /* Verify that the type of the catch is valid. It must be a pointer
5290 to an Objective-C class, or "id" (which is catch-all). */
5291 type = TREE_TYPE (decl);
5293 if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type)))
5295 else if (!POINTER_TYPE_P (type) || !TYPED_OBJECT (TREE_TYPE (type)))
5297 error ("@catch parameter is not a known Objective-C class type");
5298 type = error_mark_node;
5300 else if (cur_try_context->catch_list)
5302 /* Examine previous @catch clauses and see if we've already
5303 caught the type in question. */
5304 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
5305 for (; !tsi_end_p (i); tsi_next (&i))
5307 tree stmt = tsi_stmt (i);
5308 t = CATCH_TYPES (stmt);
5309 if (t == error_mark_node)
5311 if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type)))
5313 warning (0, "exception of type %<%T%> will be caught",
5315 warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>",
5316 TREE_TYPE (t ? t : objc_object_type));
5322 /* Record the data for the catch in the try context so that we can
5323 finalize it later. */
5324 t = build_stmt (input_location, CATCH_EXPR, type, compound);
5325 cur_try_context->current_catch = t;
5327 /* Initialize the decl from the EXC_PTR_EXPR we get from the runtime. */
5328 t = objc_build_exc_ptr ();
5329 t = convert (TREE_TYPE (decl), t);
5330 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
5334 /* Called just after parsing the closing brace of a @catch clause. Close
5335 the open binding level, and record a CATCH_EXPR for it. */
5338 objc_finish_catch_clause (void)
5340 tree c = cur_try_context->current_catch;
5341 cur_try_context->current_catch = NULL;
5342 cur_try_context->end_catch_locus = input_location;
5344 CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1);
5345 append_to_statement_list (c, &cur_try_context->catch_list);
5348 /* Called after parsing a @finally clause and its associated BODY.
5349 Record the body for later placement. */
5352 objc_build_finally_clause (location_t finally_locus, tree body)
5354 cur_try_context->finally_body = body;
5355 cur_try_context->finally_locus = finally_locus;
5356 cur_try_context->end_finally_locus = input_location;
5359 /* Called to finalize a @try construct. */
5362 objc_finish_try_stmt (void)
5364 struct objc_try_context *c = cur_try_context;
5367 if (c->catch_list == NULL && c->finally_body == NULL)
5368 error ("%<@try%> without %<@catch%> or %<@finally%>");
5370 /* If we're doing Darwin setjmp exceptions, build the big nasty. */
5371 if (flag_objc_sjlj_exceptions)
5373 bool save = in_late_binary_op;
5374 in_late_binary_op = true;
5375 if (!cur_try_context->finally_body)
5377 cur_try_context->finally_locus = input_location;
5378 cur_try_context->end_finally_locus = input_location;
5380 stmt = next_sjlj_build_try_catch_finally ();
5381 in_late_binary_op = save;
5385 /* Otherwise, nest the CATCH inside a FINALLY. */
5389 stmt = build_stmt (input_location, TRY_CATCH_EXPR, stmt, c->catch_list);
5390 SET_EXPR_LOCATION (stmt, cur_try_context->try_locus);
5392 if (c->finally_body)
5394 stmt = build_stmt (input_location, TRY_FINALLY_EXPR, stmt, c->finally_body);
5395 SET_EXPR_LOCATION (stmt, cur_try_context->try_locus);
5400 cur_try_context = c->outer;
5406 objc_build_throw_stmt (location_t loc, tree throw_expr)
5410 if (!flag_objc_exceptions)
5412 error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
5415 if (throw_expr == NULL)
5417 /* If we're not inside a @catch block, there is no "current
5418 exception" to be rethrown. */
5419 if (cur_try_context == NULL
5420 || cur_try_context->current_catch == NULL)
5422 error_at (loc, "%<@throw%> (rethrow) used outside of a @catch block");
5426 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
5427 value that we get from the runtime. */
5428 throw_expr = objc_build_exc_ptr ();
5431 /* A throw is just a call to the runtime throw function with the
5432 object as a parameter. */
5433 args = tree_cons (NULL, throw_expr, NULL);
5434 return add_stmt (build_function_call (loc,
5435 objc_exception_throw_decl, args));
5439 objc_build_synchronized (location_t start_locus, tree mutex, tree body)
5443 /* First lock the mutex. */
5444 mutex = save_expr (mutex);
5445 args = tree_cons (NULL, mutex, NULL);
5446 call = build_function_call (input_location,
5447 objc_sync_enter_decl, args);
5448 SET_EXPR_LOCATION (call, start_locus);
5451 /* Build the mutex unlock. */
5452 args = tree_cons (NULL, mutex, NULL);
5453 call = build_function_call (input_location,
5454 objc_sync_exit_decl, args);
5455 SET_EXPR_LOCATION (call, input_location);
5457 /* Put the that and the body in a TRY_FINALLY. */
5458 objc_begin_try_stmt (start_locus, body);
5459 objc_build_finally_clause (input_location, call);
5460 return objc_finish_try_stmt ();
5464 /* Predefine the following data type:
5466 struct _objc_exception_data
5468 int buf[OBJC_JBLEN];
5472 /* The following yuckiness should prevent users from having to #include
5473 <setjmp.h> in their code... */
5475 /* Define to a harmless positive value so the below code doesn't die. */
5477 #define OBJC_JBLEN 18
5481 build_next_objc_exception_stuff (void)
5483 tree decls, temp_type, *chain = NULL;
5485 objc_exception_data_template
5486 = objc_start_struct (get_identifier (UTAG_EXCDATA));
5488 /* int buf[OBJC_JBLEN]; */
5490 temp_type = build_sized_array_type (integer_type_node, OBJC_JBLEN);
5491 decls = add_field_decl (temp_type, "buf", &chain);
5493 /* void *pointers[4]; */
5495 temp_type = build_sized_array_type (ptr_type_node, 4);
5496 add_field_decl (temp_type, "pointers", &chain);
5498 objc_finish_struct (objc_exception_data_template, decls);
5500 /* int _setjmp(...); */
5501 /* If the user includes <setjmp.h>, this shall be superseded by
5502 'int _setjmp(jmp_buf);' */
5503 temp_type = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5505 = add_builtin_function (TAG_SETJMP, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
5507 /* id objc_exception_extract(struct _objc_exception_data *); */
5509 = build_function_type_list (objc_object_type,
5510 build_pointer_type (objc_exception_data_template),
5512 objc_exception_extract_decl
5513 = add_builtin_function (TAG_EXCEPTIONEXTRACT, temp_type, 0, NOT_BUILT_IN, NULL,
5515 /* void objc_exception_try_enter(struct _objc_exception_data *); */
5516 /* void objc_exception_try_exit(struct _objc_exception_data *); */
5518 = build_function_type_list (void_type_node,
5519 build_pointer_type (objc_exception_data_template),
5521 objc_exception_try_enter_decl
5522 = add_builtin_function (TAG_EXCEPTIONTRYENTER, temp_type, 0, NOT_BUILT_IN, NULL,
5524 objc_exception_try_exit_decl
5525 = add_builtin_function (TAG_EXCEPTIONTRYEXIT, temp_type, 0, NOT_BUILT_IN, NULL,
5528 /* int objc_exception_match(id, id); */
5530 = build_function_type_list (integer_type_node,
5531 objc_object_type, objc_object_type, NULL_TREE);
5532 objc_exception_match_decl
5533 = add_builtin_function (TAG_EXCEPTIONMATCH, temp_type, 0, NOT_BUILT_IN, NULL,
5536 /* id objc_assign_ivar (id, id, unsigned int); */
5537 /* id objc_assign_ivar_Fast (id, id, unsigned int)
5538 __attribute__ ((hard_coded_address (OFFS_ASSIGNIVAR_FAST))); */
5540 = build_function_type_list (objc_object_type,
5545 objc_assign_ivar_decl
5546 = add_builtin_function (TAG_ASSIGNIVAR, temp_type, 0, NOT_BUILT_IN,
5548 #ifdef OFFS_ASSIGNIVAR_FAST
5549 objc_assign_ivar_fast_decl
5550 = add_builtin_function (TAG_ASSIGNIVAR_FAST, temp_type, 0,
5551 NOT_BUILT_IN, NULL, NULL_TREE);
5552 DECL_ATTRIBUTES (objc_assign_ivar_fast_decl)
5553 = tree_cons (get_identifier ("hard_coded_address"),
5554 build_int_cst (NULL_TREE, OFFS_ASSIGNIVAR_FAST),
5557 /* Default to slower ivar method. */
5558 objc_assign_ivar_fast_decl = objc_assign_ivar_decl;
5561 /* id objc_assign_global (id, id *); */
5562 /* id objc_assign_strongCast (id, id *); */
5563 temp_type = build_function_type_list (objc_object_type,
5565 build_pointer_type (objc_object_type),
5567 objc_assign_global_decl
5568 = add_builtin_function (TAG_ASSIGNGLOBAL, temp_type, 0, NOT_BUILT_IN, NULL,
5570 objc_assign_strong_cast_decl
5571 = add_builtin_function (TAG_ASSIGNSTRONGCAST, temp_type, 0, NOT_BUILT_IN, NULL,
5576 build_objc_exception_stuff (void)
5578 tree noreturn_list, nothrow_list, temp_type;
5580 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
5581 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
5583 /* void objc_exception_throw(id) __attribute__((noreturn)); */
5584 /* void objc_sync_enter(id); */
5585 /* void objc_sync_exit(id); */
5586 temp_type = build_function_type_list (void_type_node,
5589 objc_exception_throw_decl
5590 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
5592 objc_sync_enter_decl
5593 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
5594 NULL, nothrow_list);
5596 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
5597 NULL, nothrow_list);
5600 /* Construct a C struct corresponding to ObjC class CLASS, with the same
5603 struct <classname> {
5604 struct _objc_class *isa;
5609 build_private_template (tree klass)
5611 if (!CLASS_STATIC_TEMPLATE (klass))
5613 tree record = objc_build_struct (klass,
5614 get_class_ivars (klass, false),
5615 CLASS_SUPER_NAME (klass));
5617 /* Set the TREE_USED bit for this struct, so that stab generator
5618 can emit stabs for this struct type. */
5619 if (flag_debug_only_used_symbols && TYPE_STUB_DECL (record))
5620 TREE_USED (TYPE_STUB_DECL (record)) = 1;
5622 /* Copy the attributes from the class to the type. */
5623 if (TREE_DEPRECATED (klass))
5624 TREE_DEPRECATED (record) = 1;
5628 /* Begin code generation for protocols... */
5630 /* struct _objc_protocol {
5631 struct _objc_class *isa;
5632 char *protocol_name;
5633 struct _objc_protocol **protocol_list;
5634 struct _objc__method_prototype_list *instance_methods;
5635 struct _objc__method_prototype_list *class_methods;
5639 build_protocol_template (void)
5641 tree ptype, decls, *chain = NULL;
5643 objc_protocol_template = objc_start_struct (get_identifier (UTAG_PROTOCOL));
5645 /* struct _objc_class *isa; */
5646 ptype = build_pointer_type (xref_tag (RECORD_TYPE,
5647 get_identifier (UTAG_CLASS)));
5648 decls = add_field_decl (ptype, "isa", &chain);
5650 /* char *protocol_name; */
5651 add_field_decl (string_type_node, "protocol_name", &chain);
5653 /* struct _objc_protocol **protocol_list; */
5654 ptype = build_pointer_type (build_pointer_type (objc_protocol_template));
5655 add_field_decl (ptype, "protocol_list", &chain);
5657 /* struct _objc__method_prototype_list *instance_methods; */
5658 add_field_decl (objc_method_proto_list_ptr, "instance_methods", &chain);
5660 /* struct _objc__method_prototype_list *class_methods; */
5661 add_field_decl (objc_method_proto_list_ptr, "class_methods", &chain);
5663 objc_finish_struct (objc_protocol_template, decls);
5667 build_descriptor_table_initializer (tree type, tree entries)
5669 VEC(constructor_elt,gc) *inits = NULL;
5673 VEC(constructor_elt,gc) *elts = NULL;
5675 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
5676 build_selector (METHOD_SEL_NAME (entries)));
5677 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
5678 add_objc_string (METHOD_ENCODING (entries),
5681 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
5682 objc_build_constructor (type, elts));
5684 entries = DECL_CHAIN (entries);
5688 return objc_build_constructor (build_array_type (type, 0), inits);
5691 /* struct objc_method_prototype_list {
5693 struct objc_method_prototype {
5700 build_method_prototype_list_template (tree list_type, int size)
5702 tree objc_ivar_list_record;
5703 tree array_type, decls, *chain = NULL;
5705 /* Generate an unnamed struct definition. */
5707 objc_ivar_list_record = objc_start_struct (NULL_TREE);
5709 /* int method_count; */
5710 decls = add_field_decl (integer_type_node, "method_count", &chain);
5712 /* struct objc_method method_list[]; */
5713 array_type = build_sized_array_type (list_type, size);
5714 add_field_decl (array_type, "method_list", &chain);
5716 objc_finish_struct (objc_ivar_list_record, decls);
5718 return objc_ivar_list_record;
5722 build_method_prototype_template (void)
5725 tree decls, *chain = NULL;
5727 proto_record = objc_start_struct (get_identifier (UTAG_METHOD_PROTOTYPE));
5730 decls = add_field_decl (objc_selector_type, "_cmd", &chain);
5732 /* char *method_types; */
5733 add_field_decl (string_type_node, "method_types", &chain);
5735 objc_finish_struct (proto_record, decls);
5737 return proto_record;
5741 objc_method_parm_type (tree type)
5743 type = TREE_VALUE (TREE_TYPE (type));
5744 if (TREE_CODE (type) == TYPE_DECL)
5745 type = TREE_TYPE (type);
5750 objc_encoded_type_size (tree type)
5752 int sz = int_size_in_bytes (type);
5754 /* Make all integer and enum types at least as large
5756 if (sz > 0 && INTEGRAL_TYPE_P (type))
5757 sz = MAX (sz, int_size_in_bytes (integer_type_node));
5758 /* Treat arrays as pointers, since that's how they're
5760 else if (TREE_CODE (type) == ARRAY_TYPE)
5761 sz = int_size_in_bytes (ptr_type_node);
5765 /* Encode a method prototype.
5767 The format is described in gcc/doc/objc.texi, section 'Method
5771 encode_method_prototype (tree method_decl)
5778 /* ONEWAY and BYCOPY, for remote object are the only method qualifiers. */
5779 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (method_decl)));
5781 /* Encode return type. */
5782 encode_type (objc_method_parm_type (method_decl),
5783 obstack_object_size (&util_obstack),
5784 OBJC_ENCODE_INLINE_DEFS);
5787 /* The first two arguments (self and _cmd) are pointers; account for
5789 i = int_size_in_bytes (ptr_type_node);
5790 parm_offset = 2 * i;
5791 for (parms = METHOD_SEL_ARGS (method_decl); parms;
5792 parms = DECL_CHAIN (parms))
5794 tree type = objc_method_parm_type (parms);
5795 int sz = objc_encoded_type_size (type);
5797 /* If a type size is not known, bail out. */
5800 error ("type %q+D does not have a known size",
5802 /* Pretend that the encoding succeeded; the compilation will
5803 fail nevertheless. */
5804 goto finish_encoding;
5809 sprintf (buf, "%d@0:%d", parm_offset, i);
5810 obstack_grow (&util_obstack, buf, strlen (buf));
5812 /* Argument types. */
5813 parm_offset = 2 * i;
5814 for (parms = METHOD_SEL_ARGS (method_decl); parms;
5815 parms = DECL_CHAIN (parms))
5817 tree type = objc_method_parm_type (parms);
5819 /* Process argument qualifiers for user supplied arguments. */
5820 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (parms)));
5823 encode_type (type, obstack_object_size (&util_obstack),
5824 OBJC_ENCODE_INLINE_DEFS);
5826 /* Compute offset. */
5827 sprintf (buf, "%d", parm_offset);
5828 parm_offset += objc_encoded_type_size (type);
5830 obstack_grow (&util_obstack, buf, strlen (buf));
5834 obstack_1grow (&util_obstack, '\0');
5835 result = get_identifier (XOBFINISH (&util_obstack, char *));
5836 obstack_free (&util_obstack, util_firstobj);
5841 generate_descriptor_table (tree type, const char *name, int size, tree list,
5845 VEC(constructor_elt,gc) *v = NULL;
5847 decl = start_var_decl (type, synth_id_with_class_suffix (name, proto));
5849 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, size));
5850 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, list);
5852 finish_var_decl (decl, objc_build_constructor (type, v));
5858 generate_method_descriptors (tree protocol)
5860 tree initlist, chain, method_list_template;
5863 if (!objc_method_prototype_template)
5864 objc_method_prototype_template = build_method_prototype_template ();
5866 chain = PROTOCOL_CLS_METHODS (protocol);
5869 size = list_length (chain);
5871 method_list_template
5872 = build_method_prototype_list_template (objc_method_prototype_template,
5876 = build_descriptor_table_initializer (objc_method_prototype_template,
5879 UOBJC_CLASS_METHODS_decl
5880 = generate_descriptor_table (method_list_template,
5881 "_OBJC_PROTOCOL_CLASS_METHODS",
5882 size, initlist, protocol);
5885 UOBJC_CLASS_METHODS_decl = 0;
5887 chain = PROTOCOL_NST_METHODS (protocol);
5890 size = list_length (chain);
5892 method_list_template
5893 = build_method_prototype_list_template (objc_method_prototype_template,
5896 = build_descriptor_table_initializer (objc_method_prototype_template,
5899 UOBJC_INSTANCE_METHODS_decl
5900 = generate_descriptor_table (method_list_template,
5901 "_OBJC_PROTOCOL_INSTANCE_METHODS",
5902 size, initlist, protocol);
5905 UOBJC_INSTANCE_METHODS_decl = 0;
5909 generate_protocol_references (tree plist)
5913 /* Forward declare protocols referenced. */
5914 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
5916 tree proto = TREE_VALUE (lproto);
5918 if (TREE_CODE (proto) == PROTOCOL_INTERFACE_TYPE
5919 && PROTOCOL_NAME (proto))
5921 if (! PROTOCOL_FORWARD_DECL (proto))
5922 build_protocol_reference (proto);
5924 if (PROTOCOL_LIST (proto))
5925 generate_protocol_references (PROTOCOL_LIST (proto));
5930 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
5934 objc_generate_cxx_ctor_or_dtor (bool dtor)
5936 tree fn, body, compound_stmt, ivar;
5938 /* - (id) .cxx_construct { ... return self; } */
5939 /* - (void) .cxx_construct { ... } */
5941 objc_start_method_definition
5942 (false /* is_class_method */,
5943 objc_build_method_signature (false /* is_class_method */,
5944 build_tree_list (NULL_TREE,
5947 : objc_object_type),
5948 get_identifier (dtor
5950 : TAG_CXX_CONSTRUCT),
5951 make_node (TREE_LIST),
5953 body = begin_function_body ();
5954 compound_stmt = begin_compound_stmt (0);
5956 ivar = CLASS_IVARS (implementation_template);
5957 /* Destroy ivars in reverse order. */
5959 ivar = nreverse (copy_list (ivar));
5961 for (; ivar; ivar = TREE_CHAIN (ivar))
5963 if (TREE_CODE (ivar) == FIELD_DECL)
5965 tree type = TREE_TYPE (ivar);
5967 /* Call the ivar's default constructor or destructor. Do not
5968 call the destructor unless a corresponding constructor call
5969 has also been made (or is not needed). */
5970 if (MAYBE_CLASS_TYPE_P (type)
5972 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
5973 && (!TYPE_NEEDS_CONSTRUCTING (type)
5974 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5975 : (TYPE_NEEDS_CONSTRUCTING (type)
5976 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))))
5978 (build_special_member_call
5979 (build_ivar_reference (DECL_NAME (ivar)),
5980 dtor ? complete_dtor_identifier : complete_ctor_identifier,
5981 NULL, type, LOOKUP_NORMAL, tf_warning_or_error));
5985 /* The constructor returns 'self'. */
5987 finish_return_stmt (self_decl);
5989 finish_compound_stmt (compound_stmt);
5990 finish_function_body (body);
5991 fn = current_function_decl;
5993 objc_finish_method_definition (fn);
5996 /* The following routine will examine the current @interface for any
5997 non-POD C++ ivars requiring non-trivial construction and/or
5998 destruction, and then synthesize special '- .cxx_construct' and/or
5999 '- .cxx_destruct' methods which will run the appropriate
6000 construction or destruction code. Note that ivars inherited from
6001 super-classes are _not_ considered. */
6003 objc_generate_cxx_cdtors (void)
6005 bool need_ctor = false, need_dtor = false;
6008 /* Error case, due to possibly an extra @end. */
6009 if (!objc_implementation_context)
6012 /* We do not want to do this for categories, since they do not have
6015 if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE)
6018 /* First, determine if we even need a constructor and/or destructor. */
6020 for (ivar = CLASS_IVARS (implementation_template); ivar;
6021 ivar = TREE_CHAIN (ivar))
6023 if (TREE_CODE (ivar) == FIELD_DECL)
6025 tree type = TREE_TYPE (ivar);
6027 if (MAYBE_CLASS_TYPE_P (type))
6029 if (TYPE_NEEDS_CONSTRUCTING (type)
6030 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
6031 /* NB: If a default constructor is not available, we will not
6032 be able to initialize this ivar; the add_instance_variable()
6033 routine will already have warned about this. */
6036 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
6037 && (!TYPE_NEEDS_CONSTRUCTING (type)
6038 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
6039 /* NB: If a default constructor is not available, we will not
6040 call the destructor either, for symmetry. */
6046 /* Generate '- .cxx_construct' if needed. */
6049 objc_generate_cxx_ctor_or_dtor (false);
6051 /* Generate '- .cxx_destruct' if needed. */
6054 objc_generate_cxx_ctor_or_dtor (true);
6056 /* The 'imp_list' variable points at an imp_entry record for the current
6057 @implementation. Record the existence of '- .cxx_construct' and/or
6058 '- .cxx_destruct' methods therein; it will be included in the
6059 metadata for the class. */
6060 if (flag_next_runtime)
6061 imp_list->has_cxx_cdtors = (need_ctor || need_dtor);
6065 /* For each protocol which was referenced either from a @protocol()
6066 expression, or because a class/category implements it (then a
6067 pointer to the protocol is stored in the struct describing the
6068 class/category), we create a statically allocated instance of the
6069 Protocol class. The code is written in such a way as to generate
6070 as few Protocol objects as possible; we generate a unique Protocol
6071 instance for each protocol, and we don't generate a Protocol
6072 instance if the protocol is never referenced (either from a
6073 @protocol() or from a class/category implementation). These
6074 statically allocated objects can be referred to via the static
6075 (that is, private to this module) symbols _OBJC_PROTOCOL_n.
6077 The statically allocated Protocol objects that we generate here
6078 need to be fixed up at runtime in order to be used: the 'isa'
6079 pointer of the objects need to be set up to point to the 'Protocol'
6080 class, as known at runtime.
6082 The NeXT runtime fixes up all protocols at program startup time,
6083 before main() is entered. It uses a low-level trick to look up all
6084 those symbols, then loops on them and fixes them up.
6086 The GNU runtime as well fixes up all protocols before user code
6087 from the module is executed; it requires pointers to those symbols
6088 to be put in the objc_symtab (which is then passed as argument to
6089 the function __objc_exec_class() which the compiler sets up to be
6090 executed automatically when the module is loaded); setup of those
6091 Protocol objects happen in two ways in the GNU runtime: all
6092 Protocol objects referred to by a class or category implementation
6093 are fixed up when the class/category is loaded; all Protocol
6094 objects referred to by a @protocol() expression are added by the
6095 compiler to the list of statically allocated instances to fixup
6096 (the same list holding the statically allocated constant string
6097 objects). Because, as explained above, the compiler generates as
6098 few Protocol objects as possible, some Protocol object might end up
6099 being referenced multiple times when compiled with the GNU runtime,
6100 and end up being fixed up multiple times at runtime initialization.
6101 But that doesn't hurt, it's just a little inefficient. */
6104 generate_protocols (void)
6108 tree initlist, protocol_name_expr, refs_decl, refs_expr;
6110 /* If a protocol was directly referenced, pull in indirect references. */
6111 for (p = protocol_chain; p; p = TREE_CHAIN (p))
6112 if (PROTOCOL_FORWARD_DECL (p) && PROTOCOL_LIST (p))
6113 generate_protocol_references (PROTOCOL_LIST (p));
6115 for (p = protocol_chain; p; p = TREE_CHAIN (p))
6117 tree nst_methods = PROTOCOL_NST_METHODS (p);
6118 tree cls_methods = PROTOCOL_CLS_METHODS (p);
6120 /* If protocol wasn't referenced, don't generate any code. */
6121 decl = PROTOCOL_FORWARD_DECL (p);
6126 /* Make sure we link in the Protocol class. */
6127 add_class_reference (get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
6131 if (! METHOD_ENCODING (nst_methods))
6133 encoding = encode_method_prototype (nst_methods);
6134 METHOD_ENCODING (nst_methods) = encoding;
6136 nst_methods = DECL_CHAIN (nst_methods);
6141 if (! METHOD_ENCODING (cls_methods))
6143 encoding = encode_method_prototype (cls_methods);
6144 METHOD_ENCODING (cls_methods) = encoding;
6147 cls_methods = DECL_CHAIN (cls_methods);
6149 generate_method_descriptors (p);
6151 if (PROTOCOL_LIST (p))
6152 refs_decl = generate_protocol_list (p);
6156 /* static struct objc_protocol _OBJC_PROTOCOL_<mumble>; */
6157 protocol_name_expr = add_objc_string (PROTOCOL_NAME (p), class_names);
6160 refs_expr = convert (build_pointer_type (build_pointer_type
6161 (objc_protocol_template)),
6162 build_unary_op (input_location,
6163 ADDR_EXPR, refs_decl, 0));
6165 refs_expr = build_int_cst (NULL_TREE, 0);
6167 /* UOBJC_INSTANCE_METHODS_decl/UOBJC_CLASS_METHODS_decl are set
6168 by generate_method_descriptors, which is called above. */
6169 initlist = build_protocol_initializer (TREE_TYPE (decl),
6170 protocol_name_expr, refs_expr,
6171 UOBJC_INSTANCE_METHODS_decl,
6172 UOBJC_CLASS_METHODS_decl);
6173 finish_var_decl (decl, initlist);
6178 build_protocol_initializer (tree type, tree protocol_name,
6179 tree protocol_list, tree instance_methods,
6183 tree cast_type = build_pointer_type
6184 (xref_tag (RECORD_TYPE,
6185 get_identifier (UTAG_CLASS)));
6186 VEC(constructor_elt,gc) *inits = NULL;
6188 /* Filling the "isa" in with one allows the runtime system to
6189 detect that the version change...should remove before final release. */
6191 expr = build_int_cst (cast_type, PROTOCOL_VERSION);
6192 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
6193 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, protocol_name);
6194 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, protocol_list);
6196 if (!instance_methods)
6197 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, 0));
6200 expr = convert (objc_method_proto_list_ptr,
6201 build_unary_op (input_location,
6202 ADDR_EXPR, instance_methods, 0));
6203 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
6207 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, 0));
6210 expr = convert (objc_method_proto_list_ptr,
6211 build_unary_op (input_location,
6212 ADDR_EXPR, class_methods, 0));
6213 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
6216 return objc_build_constructor (type, inits);
6219 /* struct _objc_category {
6220 char *category_name;
6222 struct _objc_method_list *instance_methods;
6223 struct _objc_method_list *class_methods;
6224 struct _objc_protocol_list *protocols;
6228 build_category_template (void)
6230 tree ptype, decls, *chain = NULL;
6232 objc_category_template = objc_start_struct (get_identifier (UTAG_CATEGORY));
6234 /* char *category_name; */
6235 decls = add_field_decl (string_type_node, "category_name", &chain);
6237 /* char *class_name; */
6238 add_field_decl (string_type_node, "class_name", &chain);
6240 /* struct _objc_method_list *instance_methods; */
6241 add_field_decl (objc_method_list_ptr, "instance_methods", &chain);
6243 /* struct _objc_method_list *class_methods; */
6244 add_field_decl (objc_method_list_ptr, "class_methods", &chain);
6246 /* struct _objc_protocol **protocol_list; */
6247 ptype = build_pointer_type (build_pointer_type (objc_protocol_template));
6248 add_field_decl (ptype, "protocol_list", &chain);
6250 objc_finish_struct (objc_category_template, decls);
6253 /* struct _objc_selector {
6259 build_selector_template (void)
6261 tree decls, *chain = NULL;
6263 objc_selector_template = objc_start_struct (get_identifier (UTAG_SELECTOR));
6266 decls = add_field_decl (objc_selector_type, "sel_id", &chain);
6268 /* char *sel_type; */
6269 add_field_decl (string_type_node, "sel_type", &chain);
6271 objc_finish_struct (objc_selector_template, decls);
6274 /* struct _objc_class {
6275 struct _objc_class *isa;
6276 struct _objc_class *super_class;
6281 struct _objc_ivar_list *ivars;
6282 struct _objc_method_list *methods;
6283 #ifdef __NEXT_RUNTIME__
6284 struct objc_cache *cache;
6286 struct sarray *dtable;
6287 struct _objc_class *subclass_list;
6288 struct _objc_class *sibling_class;
6290 struct _objc_protocol_list *protocols;
6291 #ifdef __NEXT_RUNTIME__
6294 void *gc_object_type;
6297 /* NB: The 'sel_id' and 'gc_object_type' fields are not being used by
6298 the NeXT/Apple runtime; still, the compiler must generate them to
6299 maintain backward binary compatibility (and to allow for future
6303 build_class_template (void)
6305 tree ptype, decls, *chain = NULL;
6307 objc_class_template = objc_start_struct (get_identifier (UTAG_CLASS));
6309 /* struct _objc_class *isa; */
6310 decls = add_field_decl (build_pointer_type (objc_class_template),
6313 /* struct _objc_class *super_class; */
6314 add_field_decl (build_pointer_type (objc_class_template),
6315 "super_class", &chain);
6318 add_field_decl (string_type_node, "name", &chain);
6321 add_field_decl (long_integer_type_node, "version", &chain);
6324 add_field_decl (long_integer_type_node, "info", &chain);
6326 /* long instance_size; */
6327 add_field_decl (long_integer_type_node, "instance_size", &chain);
6329 /* struct _objc_ivar_list *ivars; */
6330 add_field_decl (objc_ivar_list_ptr,"ivars", &chain);
6332 /* struct _objc_method_list *methods; */
6333 add_field_decl (objc_method_list_ptr, "methods", &chain);
6335 if (flag_next_runtime)
6337 /* struct objc_cache *cache; */
6338 ptype = build_pointer_type (xref_tag (RECORD_TYPE,
6339 get_identifier ("objc_cache")));
6340 add_field_decl (ptype, "cache", &chain);
6344 /* struct sarray *dtable; */
6345 ptype = build_pointer_type(xref_tag (RECORD_TYPE,
6346 get_identifier ("sarray")));
6347 add_field_decl (ptype, "dtable", &chain);
6349 /* struct objc_class *subclass_list; */
6350 ptype = build_pointer_type (objc_class_template);
6351 add_field_decl (ptype, "subclass_list", &chain);
6353 /* struct objc_class *sibling_class; */
6354 ptype = build_pointer_type (objc_class_template);
6355 add_field_decl (ptype, "sibling_class", &chain);
6358 /* struct _objc_protocol **protocol_list; */
6359 ptype = build_pointer_type (build_pointer_type
6360 (xref_tag (RECORD_TYPE,
6361 get_identifier (UTAG_PROTOCOL))));
6362 add_field_decl (ptype, "protocol_list", &chain);
6364 if (flag_next_runtime)
6367 add_field_decl (build_pointer_type (void_type_node), "sel_id", &chain);
6370 /* void *gc_object_type; */
6371 add_field_decl (build_pointer_type (void_type_node),
6372 "gc_object_type", &chain);
6374 objc_finish_struct (objc_class_template, decls);
6377 /* Generate appropriate forward declarations for an implementation. */
6380 synth_forward_declarations (void)
6384 /* static struct objc_class _OBJC_CLASS_<my_name>; */
6385 UOBJC_CLASS_decl = build_metadata_decl ("_OBJC_CLASS",
6386 objc_class_template);
6388 /* static struct objc_class _OBJC_METACLASS_<my_name>; */
6389 UOBJC_METACLASS_decl = build_metadata_decl ("_OBJC_METACLASS",
6390 objc_class_template);
6392 /* Pre-build the following entities - for speed/convenience. */
6394 an_id = get_identifier ("super_class");
6395 ucls_super_ref = objc_build_component_ref (UOBJC_CLASS_decl, an_id);
6396 uucls_super_ref = objc_build_component_ref (UOBJC_METACLASS_decl, an_id);
6400 error_with_ivar (const char *message, tree decl)
6402 error_at (DECL_SOURCE_LOCATION (decl), "%s %qs",
6403 message, identifier_to_locale (gen_declaration (decl)));
6408 check_ivars (tree inter, tree imp)
6410 tree intdecls = CLASS_RAW_IVARS (inter);
6411 tree impdecls = CLASS_RAW_IVARS (imp);
6418 if (intdecls && TREE_CODE (intdecls) == TYPE_DECL)
6419 intdecls = TREE_CHAIN (intdecls);
6421 if (intdecls == 0 && impdecls == 0)
6423 if (intdecls == 0 || impdecls == 0)
6425 error ("inconsistent instance variable specification");
6429 t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
6431 if (!comptypes (t1, t2)
6432 || !tree_int_cst_equal (DECL_INITIAL (intdecls),
6433 DECL_INITIAL (impdecls)))
6435 if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
6437 error_with_ivar ("conflicting instance variable type",
6439 error_with_ivar ("previous declaration of",
6442 else /* both the type and the name don't match */
6444 error ("inconsistent instance variable specification");
6449 else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
6451 error_with_ivar ("conflicting instance variable name",
6453 error_with_ivar ("previous declaration of",
6457 intdecls = DECL_CHAIN (intdecls);
6458 impdecls = DECL_CHAIN (impdecls);
6462 /* Set 'objc_super_template' to the data type node for 'struct _objc_super'.
6463 This needs to be done just once per compilation. */
6465 /* struct _objc_super {
6466 struct _objc_object *self;
6467 struct _objc_class *super_class;
6471 build_super_template (void)
6473 tree decls, *chain = NULL;
6475 objc_super_template = objc_start_struct (get_identifier (UTAG_SUPER));
6477 /* struct _objc_object *self; */
6478 decls = add_field_decl (objc_object_type, "self", &chain);
6480 /* struct _objc_class *super_class; */
6481 add_field_decl (build_pointer_type (objc_class_template),
6482 "super_class", &chain);
6484 objc_finish_struct (objc_super_template, decls);
6487 /* struct _objc_ivar {
6494 build_ivar_template (void)
6496 tree objc_ivar_id, objc_ivar_record;
6497 tree decls, *chain = NULL;
6499 objc_ivar_id = get_identifier (UTAG_IVAR);
6500 objc_ivar_record = objc_start_struct (objc_ivar_id);
6502 /* char *ivar_name; */
6503 decls = add_field_decl (string_type_node, "ivar_name", &chain);
6505 /* char *ivar_type; */
6506 add_field_decl (string_type_node, "ivar_type", &chain);
6508 /* int ivar_offset; */
6509 add_field_decl (integer_type_node, "ivar_offset", &chain);
6511 objc_finish_struct (objc_ivar_record, decls);
6513 return objc_ivar_record;
6518 struct objc_ivar ivar_list[ivar_count];
6522 build_ivar_list_template (tree list_type, int size)
6524 tree objc_ivar_list_record;
6525 tree array_type, decls, *chain = NULL;
6527 objc_ivar_list_record = objc_start_struct (NULL_TREE);
6529 /* int ivar_count; */
6530 decls = add_field_decl (integer_type_node, "ivar_count", &chain);
6532 /* struct objc_ivar ivar_list[]; */
6533 array_type = build_sized_array_type (list_type, size);
6534 add_field_decl (array_type, "ivar_list", &chain);
6536 objc_finish_struct (objc_ivar_list_record, decls);
6538 return objc_ivar_list_record;
6542 struct _objc__method_prototype_list *method_next;
6544 struct objc_method method_list[method_count];
6548 build_method_list_template (tree list_type, int size)
6550 tree objc_ivar_list_record;
6551 tree array_type, decls, *chain = NULL;
6553 objc_ivar_list_record = objc_start_struct (NULL_TREE);
6555 /* struct _objc__method_prototype_list *method_next; */
6556 decls = add_field_decl (objc_method_proto_list_ptr, "method_next", &chain);
6558 /* int method_count; */
6559 add_field_decl (integer_type_node, "method_count", &chain);
6561 /* struct objc_method method_list[]; */
6562 array_type = build_sized_array_type (list_type, size);
6563 add_field_decl (array_type, "method_list", &chain);
6565 objc_finish_struct (objc_ivar_list_record, decls);
6567 return objc_ivar_list_record;
6571 build_ivar_list_initializer (tree type, tree field_decl)
6573 VEC(constructor_elt,gc) *inits = NULL;
6577 VEC(constructor_elt,gc) *ivar = NULL;
6581 if (DECL_NAME (field_decl))
6582 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE,
6583 add_objc_string (DECL_NAME (field_decl),
6586 /* Unnamed bit-field ivar (yuck). */
6587 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, build_int_cst (NULL_TREE, 0));
6590 encode_field_decl (field_decl,
6591 obstack_object_size (&util_obstack),
6592 OBJC_ENCODE_DONT_INLINE_DEFS);
6594 /* Null terminate string. */
6595 obstack_1grow (&util_obstack, 0);
6596 id = add_objc_string (get_identifier (XOBFINISH (&util_obstack, char *)),
6598 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, id);
6599 obstack_free (&util_obstack, util_firstobj);
6602 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, byte_position (field_decl));
6603 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
6604 objc_build_constructor (type, ivar));
6606 field_decl = DECL_CHAIN (field_decl);
6607 while (field_decl && TREE_CODE (field_decl) != FIELD_DECL);
6611 return objc_build_constructor (build_array_type (type, 0), inits);
6615 generate_ivars_list (tree type, const char *name, int size, tree list)
6618 VEC(constructor_elt,gc) *inits = NULL;
6620 decl = start_var_decl (type, synth_id_with_class_suffix
6621 (name, objc_implementation_context));
6623 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, size));
6624 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, list);
6626 finish_var_decl (decl,
6627 objc_build_constructor (TREE_TYPE (decl), inits));
6632 /* Count only the fields occurring in T. */
6635 ivar_list_length (tree t)
6639 for (; t; t = DECL_CHAIN (t))
6640 if (TREE_CODE (t) == FIELD_DECL)
6647 generate_ivar_lists (void)
6649 tree initlist, ivar_list_template, chain;
6652 generating_instance_variables = 1;
6654 if (!objc_ivar_template)
6655 objc_ivar_template = build_ivar_template ();
6657 /* Only generate class variables for the root of the inheritance
6658 hierarchy since these will be the same for every class. */
6660 if (CLASS_SUPER_NAME (implementation_template) == NULL_TREE
6661 && (chain = TYPE_FIELDS (objc_class_template)))
6663 size = ivar_list_length (chain);
6665 ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
6666 initlist = build_ivar_list_initializer (objc_ivar_template, chain);
6668 UOBJC_CLASS_VARIABLES_decl
6669 = generate_ivars_list (ivar_list_template, "_OBJC_CLASS_VARIABLES",
6673 UOBJC_CLASS_VARIABLES_decl = 0;
6675 chain = CLASS_IVARS (implementation_template);
6678 size = ivar_list_length (chain);
6679 ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
6680 initlist = build_ivar_list_initializer (objc_ivar_template, chain);
6682 UOBJC_INSTANCE_VARIABLES_decl
6683 = generate_ivars_list (ivar_list_template, "_OBJC_INSTANCE_VARIABLES",
6687 UOBJC_INSTANCE_VARIABLES_decl = 0;
6689 generating_instance_variables = 0;
6693 build_dispatch_table_initializer (tree type, tree entries)
6695 VEC(constructor_elt,gc) *inits = NULL;
6699 VEC(constructor_elt,gc) *elems = NULL;
6702 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE,
6703 build_selector (METHOD_SEL_NAME (entries)));
6705 /* Generate the method encoding if we don't have one already. */
6706 if (! METHOD_ENCODING (entries))
6707 METHOD_ENCODING (entries) =
6708 encode_method_prototype (entries);
6710 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE,
6711 add_objc_string (METHOD_ENCODING (entries),
6714 expr = convert (ptr_type_node,
6715 build_unary_op (input_location, ADDR_EXPR,
6716 METHOD_DEFINITION (entries), 1));
6717 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE, expr);
6719 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
6720 objc_build_constructor (type, elems));
6722 entries = DECL_CHAIN (entries);
6726 return objc_build_constructor (build_array_type (type, 0), inits);
6729 /* To accomplish method prototyping without generating all kinds of
6730 inane warnings, the definition of the dispatch table entries were
6733 struct objc_method { SEL _cmd; ...; id (*_imp)(); };
6735 struct objc_method { SEL _cmd; ...; void *_imp; }; */
6738 build_method_template (void)
6741 tree decls, *chain = NULL;
6743 _SLT_record = objc_start_struct (get_identifier (UTAG_METHOD));
6746 decls = add_field_decl (objc_selector_type, "_cmd", &chain);
6748 /* char *method_types; */
6749 add_field_decl (string_type_node, "method_types", &chain);
6752 add_field_decl (build_pointer_type (void_type_node), "_imp", &chain);
6754 objc_finish_struct (_SLT_record, decls);
6761 generate_dispatch_table (tree type, const char *name, int size, tree list)
6764 VEC(constructor_elt,gc) *v = NULL;
6766 decl = start_var_decl (type, synth_id_with_class_suffix
6767 (name, objc_implementation_context));
6769 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
6770 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (integer_type_node, size));
6771 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, list);
6773 finish_var_decl (decl,
6774 objc_build_constructor (TREE_TYPE (decl), v));
6780 mark_referenced_methods (void)
6782 struct imp_entry *impent;
6785 for (impent = imp_list; impent; impent = impent->next)
6787 chain = CLASS_CLS_METHODS (impent->imp_context);
6790 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain)));
6791 chain = DECL_CHAIN (chain);
6794 chain = CLASS_NST_METHODS (impent->imp_context);
6797 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain)));
6798 chain = DECL_CHAIN (chain);
6804 generate_dispatch_tables (void)
6806 tree initlist, chain, method_list_template;
6809 if (!objc_method_template)
6810 objc_method_template = build_method_template ();
6812 chain = CLASS_CLS_METHODS (objc_implementation_context);
6815 size = list_length (chain);
6817 method_list_template
6818 = build_method_list_template (objc_method_template, size);
6820 = build_dispatch_table_initializer (objc_method_template, chain);
6822 UOBJC_CLASS_METHODS_decl
6823 = generate_dispatch_table (method_list_template,
6824 ((TREE_CODE (objc_implementation_context)
6825 == CLASS_IMPLEMENTATION_TYPE)
6826 ? "_OBJC_CLASS_METHODS"
6827 : "_OBJC_CATEGORY_CLASS_METHODS"),
6831 UOBJC_CLASS_METHODS_decl = 0;
6833 chain = CLASS_NST_METHODS (objc_implementation_context);
6836 size = list_length (chain);
6838 method_list_template
6839 = build_method_list_template (objc_method_template, size);
6841 = build_dispatch_table_initializer (objc_method_template, chain);
6843 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
6844 UOBJC_INSTANCE_METHODS_decl
6845 = generate_dispatch_table (method_list_template,
6846 "_OBJC_INSTANCE_METHODS",
6849 /* We have a category. */
6850 UOBJC_INSTANCE_METHODS_decl
6851 = generate_dispatch_table (method_list_template,
6852 "_OBJC_CATEGORY_INSTANCE_METHODS",
6856 UOBJC_INSTANCE_METHODS_decl = 0;
6860 generate_protocol_list (tree i_or_p)
6862 tree array_type, ptype, refs_decl, lproto, e, plist;
6864 const char *ref_name;
6865 VEC(constructor_elt,gc) *v = NULL;
6867 switch (TREE_CODE (i_or_p))
6869 case CLASS_INTERFACE_TYPE:
6870 case CATEGORY_INTERFACE_TYPE:
6871 plist = CLASS_PROTOCOL_LIST (i_or_p);
6873 case PROTOCOL_INTERFACE_TYPE:
6874 plist = PROTOCOL_LIST (i_or_p);
6881 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
6882 if (TREE_CODE (TREE_VALUE (lproto)) == PROTOCOL_INTERFACE_TYPE
6883 && PROTOCOL_FORWARD_DECL (TREE_VALUE (lproto)))
6886 /* Build initializer. */
6887 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6888 e = build_int_cst (build_pointer_type (objc_protocol_template), size);
6889 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, e);
6891 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
6893 tree pval = TREE_VALUE (lproto);
6895 if (TREE_CODE (pval) == PROTOCOL_INTERFACE_TYPE
6896 && PROTOCOL_FORWARD_DECL (pval))
6898 e = build_unary_op (input_location, ADDR_EXPR,
6899 PROTOCOL_FORWARD_DECL (pval), 0);
6900 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, e);
6904 /* static struct objc_protocol *refs[n]; */
6906 switch (TREE_CODE (i_or_p))
6908 case PROTOCOL_INTERFACE_TYPE:
6909 ref_name = synth_id_with_class_suffix ("_OBJC_PROTOCOL_REFS", i_or_p);
6911 case CLASS_INTERFACE_TYPE:
6912 ref_name = synth_id_with_class_suffix ("_OBJC_CLASS_PROTOCOLS", i_or_p);
6914 case CATEGORY_INTERFACE_TYPE:
6915 ref_name = synth_id_with_class_suffix ("_OBJC_CATEGORY_PROTOCOLS", i_or_p);
6921 ptype = build_pointer_type (objc_protocol_template);
6922 array_type = build_sized_array_type (ptype, size + 3);
6923 refs_decl = start_var_decl (array_type, ref_name);
6925 finish_var_decl (refs_decl,
6926 objc_build_constructor (TREE_TYPE (refs_decl), v));
6932 build_category_initializer (tree type, tree cat_name, tree class_name,
6933 tree instance_methods, tree class_methods,
6937 VEC(constructor_elt,gc) *v = NULL;
6939 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, cat_name);
6940 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, class_name);
6942 if (!instance_methods)
6943 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6946 expr = convert (objc_method_list_ptr,
6947 build_unary_op (input_location, ADDR_EXPR,
6948 instance_methods, 0));
6949 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6952 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6955 expr = convert (objc_method_list_ptr,
6956 build_unary_op (input_location, ADDR_EXPR,
6958 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6961 /* protocol_list = */
6963 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6966 expr = convert (build_pointer_type
6968 (objc_protocol_template)),
6969 build_unary_op (input_location, ADDR_EXPR,
6971 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6974 return objc_build_constructor (type, v);
6977 /* struct _objc_class {
6978 struct objc_class *isa;
6979 struct objc_class *super_class;
6984 struct objc_ivar_list *ivars;
6985 struct objc_method_list *methods;
6986 if (flag_next_runtime)
6987 struct objc_cache *cache;
6989 struct sarray *dtable;
6990 struct objc_class *subclass_list;
6991 struct objc_class *sibling_class;
6993 struct objc_protocol_list *protocols;
6994 if (flag_next_runtime)
6996 void *gc_object_type;
7000 build_shared_structure_initializer (tree type, tree isa, tree super,
7001 tree name, tree size, int status,
7002 tree dispatch_table, tree ivar_list,
7006 VEC(constructor_elt,gc) *v = NULL;
7009 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, isa);
7012 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, super);
7015 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, default_conversion (name));
7018 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
7019 build_int_cst (long_integer_type_node, 0));
7022 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
7023 build_int_cst (long_integer_type_node, status));
7025 /* instance_size = */
7026 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
7027 convert (long_integer_type_node, size));
7029 /* objc_ivar_list = */
7031 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7034 expr = convert (objc_ivar_list_ptr,
7035 build_unary_op (input_location, ADDR_EXPR,
7037 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
7040 /* objc_method_list = */
7041 if (!dispatch_table)
7042 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7045 expr = convert (objc_method_list_ptr,
7046 build_unary_op (input_location, ADDR_EXPR,
7047 dispatch_table, 0));
7048 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
7051 if (flag_next_runtime)
7052 /* method_cache = */
7053 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7057 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7059 /* subclass_list = */
7060 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7062 /* sibling_class = */
7063 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7066 /* protocol_list = */
7067 if (! protocol_list)
7068 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7071 expr = convert (build_pointer_type
7073 (objc_protocol_template)),
7074 build_unary_op (input_location, ADDR_EXPR,
7076 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
7079 if (flag_next_runtime)
7081 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7083 /* gc_object_type = NULL */
7084 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7086 return objc_build_constructor (type, v);
7089 /* Retrieve category interface CAT_NAME (if any) associated with CLASS. */
7092 lookup_category (tree klass, tree cat_name)
7094 tree category = CLASS_CATEGORY_LIST (klass);
7096 while (category && CLASS_SUPER_NAME (category) != cat_name)
7097 category = CLASS_CATEGORY_LIST (category);
7101 /* static struct objc_category _OBJC_CATEGORY_<name> = { ... }; */
7104 generate_category (struct imp_entry *impent)
7106 tree initlist, cat_name_expr, class_name_expr;
7107 tree protocol_decl, category;
7108 tree cat = impent->imp_context;
7110 implementation_template = impent->imp_template;
7111 UOBJC_CLASS_decl = impent->class_decl;
7112 UOBJC_METACLASS_decl = impent->meta_decl;
7114 add_class_reference (CLASS_NAME (cat));
7115 cat_name_expr = add_objc_string (CLASS_SUPER_NAME (cat), class_names);
7117 class_name_expr = add_objc_string (CLASS_NAME (cat), class_names);
7119 category = lookup_category (implementation_template,
7120 CLASS_SUPER_NAME (cat));
7122 if (category && CLASS_PROTOCOL_LIST (category))
7124 generate_protocol_references (CLASS_PROTOCOL_LIST (category));
7125 protocol_decl = generate_protocol_list (category);
7130 initlist = build_category_initializer (TREE_TYPE (UOBJC_CLASS_decl),
7131 cat_name_expr, class_name_expr,
7132 UOBJC_INSTANCE_METHODS_decl,
7133 UOBJC_CLASS_METHODS_decl,
7135 /* Finish and initialize the forward decl. */
7136 finish_var_decl (UOBJC_CLASS_decl, initlist);
7139 /* static struct objc_class _OBJC_METACLASS_Foo={ ... };
7140 static struct objc_class _OBJC_CLASS_Foo={ ... }; */
7143 generate_shared_structures (struct imp_entry *impent)
7145 tree name_expr, super_expr, root_expr;
7146 tree my_root_id, my_super_id;
7147 tree cast_type, initlist, protocol_decl;
7150 objc_implementation_context = impent->imp_context;
7151 implementation_template = impent->imp_template;
7152 UOBJC_CLASS_decl = impent->class_decl;
7153 UOBJC_METACLASS_decl = impent->meta_decl;
7154 cls_flags = impent->has_cxx_cdtors ? CLS_HAS_CXX_STRUCTORS : 0 ;
7156 my_super_id = CLASS_SUPER_NAME (implementation_template);
7159 add_class_reference (my_super_id);
7161 /* Compute "my_root_id" - this is required for code generation.
7162 the "isa" for all meta class structures points to the root of
7163 the inheritance hierarchy (e.g. "__Object")... */
7164 my_root_id = my_super_id;
7167 tree my_root_int = lookup_interface (my_root_id);
7169 if (my_root_int && CLASS_SUPER_NAME (my_root_int))
7170 my_root_id = CLASS_SUPER_NAME (my_root_int);
7177 /* No super class. */
7178 my_root_id = CLASS_NAME (implementation_template);
7180 cast_type = build_pointer_type (objc_class_template);
7181 name_expr = add_objc_string (CLASS_NAME (implementation_template),
7184 /* Install class `isa' and `super' pointers at runtime. */
7186 super_expr = add_objc_string (my_super_id, class_names);
7188 super_expr = integer_zero_node;
7190 super_expr = build_c_cast (input_location,
7191 cast_type, super_expr); /* cast! */
7193 root_expr = add_objc_string (my_root_id, class_names);
7194 root_expr = build_c_cast (input_location, cast_type, root_expr); /* cast! */
7196 if (CLASS_PROTOCOL_LIST (implementation_template))
7198 generate_protocol_references
7199 (CLASS_PROTOCOL_LIST (implementation_template));
7200 protocol_decl = generate_protocol_list (implementation_template);
7205 /* static struct objc_class _OBJC_METACLASS_Foo = { ... }; */
7208 = build_shared_structure_initializer
7209 (TREE_TYPE (UOBJC_METACLASS_decl),
7210 root_expr, super_expr, name_expr,
7211 convert (integer_type_node, TYPE_SIZE_UNIT (objc_class_template)),
7213 UOBJC_CLASS_METHODS_decl,
7214 UOBJC_CLASS_VARIABLES_decl,
7217 finish_var_decl (UOBJC_METACLASS_decl, initlist);
7219 /* static struct objc_class _OBJC_CLASS_Foo={ ... }; */
7222 = build_shared_structure_initializer
7223 (TREE_TYPE (UOBJC_CLASS_decl),
7224 build_unary_op (input_location, ADDR_EXPR, UOBJC_METACLASS_decl, 0),
7225 super_expr, name_expr,
7226 convert (integer_type_node,
7227 TYPE_SIZE_UNIT (CLASS_STATIC_TEMPLATE
7228 (implementation_template))),
7229 1 /*CLS_FACTORY*/ | cls_flags,
7230 UOBJC_INSTANCE_METHODS_decl,
7231 UOBJC_INSTANCE_VARIABLES_decl,
7234 finish_var_decl (UOBJC_CLASS_decl, initlist);
7239 synth_id_with_class_suffix (const char *preamble, tree ctxt)
7241 static char string[BUFSIZE];
7243 switch (TREE_CODE (ctxt))
7245 case CLASS_IMPLEMENTATION_TYPE:
7246 case CLASS_INTERFACE_TYPE:
7247 sprintf (string, "%s_%s", preamble,
7248 IDENTIFIER_POINTER (CLASS_NAME (ctxt)));
7250 case CATEGORY_IMPLEMENTATION_TYPE:
7251 case CATEGORY_INTERFACE_TYPE:
7253 /* We have a category. */
7254 const char *const class_name
7255 = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
7256 const char *const class_super_name
7257 = IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context));
7258 sprintf (string, "%s_%s_%s", preamble, class_name, class_super_name);
7261 case PROTOCOL_INTERFACE_TYPE:
7263 const char *protocol_name = IDENTIFIER_POINTER (PROTOCOL_NAME (ctxt));
7264 sprintf (string, "%s_%s", preamble, protocol_name);
7274 /* If type is empty or only type qualifiers are present, add default
7275 type of id (otherwise grokdeclarator will default to int). */
7277 adjust_type_for_id_default (tree type)
7280 type = make_node (TREE_LIST);
7282 if (!TREE_VALUE (type))
7283 TREE_VALUE (type) = objc_object_type;
7284 else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE
7285 && TYPED_OBJECT (TREE_VALUE (type)))
7286 error ("can not use an object as parameter to a method");
7291 /* Return a KEYWORD_DECL built using the specified key_name, arg_type,
7292 arg_name and attributes. (TODO: Rename KEYWORD_DECL to
7293 OBJC_METHOD_PARM_DECL ?)
7295 A KEYWORD_DECL is a tree representing the declaration of a
7296 parameter of an Objective-C method. It is produced when parsing a
7297 fragment of Objective-C method declaration of the form
7300 selector ':' '(' typename ')' identifier
7302 For example, take the Objective-C method
7304 -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type;
7306 the two fragments "pathForResource:(NSString *)resource" and
7307 "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The
7308 KEYWORD_DECL stores the 'key_name' (eg, identifier for
7309 "pathForResource"), the 'arg_type' (eg, tree representing a
7310 NSString *), the 'arg_name' (eg identifier for "resource") and
7311 potentially some attributes (for example, a tree representing
7312 __attribute__ ((unused)) if such an attribute was attached to a
7313 certain parameter). You can access this information using the
7314 TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name),
7315 KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes).
7317 'key_name' is an identifier node (and is optional as you can omit
7318 it in Objective-C methods).
7319 'arg_type' is a tree list (and is optional too if no parameter type
7321 'arg_name' is an identifier node and is required.
7322 'attributes' is an optional tree containing parameter attributes. */
7324 objc_build_keyword_decl (tree key_name, tree arg_type,
7325 tree arg_name, tree attributes)
7329 if (flag_objc1_only && attributes)
7330 error_at (input_location, "method argument attributes are not available in Objective-C 1.0");
7332 /* If no type is specified, default to "id". */
7333 arg_type = adjust_type_for_id_default (arg_type);
7335 keyword_decl = make_node (KEYWORD_DECL);
7337 TREE_TYPE (keyword_decl) = arg_type;
7338 KEYWORD_ARG_NAME (keyword_decl) = arg_name;
7339 KEYWORD_KEY_NAME (keyword_decl) = key_name;
7340 DECL_ATTRIBUTES (keyword_decl) = attributes;
7342 return keyword_decl;
7345 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
7347 build_keyword_selector (tree selector)
7350 tree key_chain, key_name;
7353 /* Scan the selector to see how much space we'll need. */
7354 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
7356 switch (TREE_CODE (selector))
7359 key_name = KEYWORD_KEY_NAME (key_chain);
7362 key_name = TREE_PURPOSE (key_chain);
7369 len += IDENTIFIER_LENGTH (key_name) + 1;
7371 /* Just a ':' arg. */
7375 buf = (char *) alloca (len + 1);
7376 /* Start the buffer out as an empty string. */
7379 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
7381 switch (TREE_CODE (selector))
7384 key_name = KEYWORD_KEY_NAME (key_chain);
7387 key_name = TREE_PURPOSE (key_chain);
7388 /* The keyword decl chain will later be used as a function
7389 argument chain. Unhook the selector itself so as to not
7390 confuse other parts of the compiler. */
7391 TREE_PURPOSE (key_chain) = NULL_TREE;
7398 strcat (buf, IDENTIFIER_POINTER (key_name));
7402 return get_identifier (buf);
7405 /* Used for declarations and definitions. */
7408 build_method_decl (enum tree_code code, tree ret_type, tree selector,
7409 tree add_args, bool ellipsis)
7413 /* If no type is specified, default to "id". */
7414 ret_type = adjust_type_for_id_default (ret_type);
7416 /* Note how a method_decl has a TREE_TYPE which is not the function
7417 type of the function implementing the method, but only the return
7418 type of the method. We may want to change this, and store the
7419 entire function type in there (eg, it may be used to simplify
7420 dealing with attributes below). */
7421 method_decl = make_node (code);
7422 TREE_TYPE (method_decl) = ret_type;
7424 /* If we have a keyword selector, create an identifier_node that
7425 represents the full selector name (`:' included)... */
7426 if (TREE_CODE (selector) == KEYWORD_DECL)
7428 METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
7429 METHOD_SEL_ARGS (method_decl) = selector;
7430 METHOD_ADD_ARGS (method_decl) = add_args;
7431 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis;
7435 METHOD_SEL_NAME (method_decl) = selector;
7436 METHOD_SEL_ARGS (method_decl) = NULL_TREE;
7437 METHOD_ADD_ARGS (method_decl) = NULL_TREE;
7443 #define METHOD_DEF 0
7444 #define METHOD_REF 1
7446 /* This routine processes objective-c method attributes. */
7449 objc_decl_method_attributes (tree *node, tree attributes, int flags)
7451 /* TODO: Replace the hackery below. An idea would be to store the
7452 full function type in the method declaration (for example in
7453 TREE_TYPE) and then expose ObjC method declarations to c-family
7454 and they could deal with them by simply treating them as
7457 /* Because of the dangers in the hackery below, we filter out any
7458 attribute that we do not know about. For the ones we know about,
7459 we know that they work with the hackery. For the other ones,
7460 there is no guarantee, so we have to filter them out. */
7461 tree filtered_attributes = NULL_TREE;
7466 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
7468 tree name = TREE_PURPOSE (attribute);
7470 if (is_attribute_p ("deprecated", name)
7471 || is_attribute_p ("sentinel", name)
7472 || is_attribute_p ("noreturn", name))
7474 /* An attribute that we support; add it to the filtered
7476 filtered_attributes = chainon (filtered_attributes,
7477 copy_node (attribute));
7479 else if (is_attribute_p ("format", name))
7481 /* "format" is special because before adding it to the
7482 filtered attributes we need to adjust the specified
7483 format by adding the hidden function parameters for
7484 an Objective-C method (self, _cmd). */
7485 tree new_attribute = copy_node (attribute);
7487 /* Check the arguments specified with the attribute, and
7488 modify them adding 2 for the two hidden arguments.
7489 Note how this differs from C++; according to the
7490 specs, C++ does not do it so you have to add the +1
7491 yourself. For Objective-C, instead, the compiler
7492 adds the +2 for you. */
7494 /* The attribute arguments have not been checked yet, so
7495 we need to be careful as they could be missing or
7496 invalid. If anything looks wrong, we skip the
7497 process and the compiler will complain about it later
7498 when it validates the attribute. */
7499 /* Check that we have at least three arguments. */
7500 if (TREE_VALUE (new_attribute)
7501 && TREE_CHAIN (TREE_VALUE (new_attribute))
7502 && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (new_attribute))))
7504 tree second_argument = TREE_CHAIN (TREE_VALUE (new_attribute));
7505 tree third_argument = TREE_CHAIN (second_argument);
7508 /* This is the second argument, the "string-index",
7509 which specifies the index of the format string
7511 number = TREE_VALUE (second_argument);
7513 && TREE_CODE (number) == INTEGER_CST
7514 && TREE_INT_CST_HIGH (number) == 0)
7516 TREE_VALUE (second_argument)
7517 = build_int_cst (integer_type_node,
7518 TREE_INT_CST_LOW (number) + 2);
7521 /* This is the third argument, the "first-to-check",
7522 which specifies the index of the first argument to
7523 check. This could be 0, meaning it is not available,
7524 in which case we don't need to add 2. Add 2 if not
7526 number = TREE_VALUE (third_argument);
7528 && TREE_CODE (number) == INTEGER_CST
7529 && TREE_INT_CST_HIGH (number) == 0
7530 && TREE_INT_CST_LOW (number) != 0)
7532 TREE_VALUE (third_argument)
7533 = build_int_cst (integer_type_node,
7534 TREE_INT_CST_LOW (number) + 2);
7537 filtered_attributes = chainon (filtered_attributes,
7541 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
7545 if (filtered_attributes)
7547 /* This hackery changes the TREE_TYPE of the ObjC method
7548 declaration to be a function type, so that decl_attributes
7549 will treat the ObjC method as if it was a function. Some
7550 attributes (sentinel, format) will be applied to the function
7551 type, changing it in place; so after calling decl_attributes,
7552 we extract the function type attributes and store them in
7553 METHOD_TYPE_ATTRIBUTES. Some other attributes (noreturn,
7554 deprecated) are applied directly to the method declaration
7555 (by setting TREE_DEPRECATED and TREE_THIS_VOLATILE) so there
7556 is nothing to do. */
7557 tree saved_type = TREE_TYPE (*node);
7558 TREE_TYPE (*node) = build_function_type
7559 (TREE_VALUE (saved_type), get_arg_type_list (*node, METHOD_REF, 0));
7560 decl_attributes (node, filtered_attributes, flags);
7561 METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node));
7562 TREE_TYPE (*node) = saved_type;
7567 objc_method_decl (enum tree_code opcode)
7569 return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL;
7572 /* Used by `build_objc_method_call' and `comp_proto_with_proto'. Return
7573 an argument list for method METH. CONTEXT is either METHOD_DEF or
7574 METHOD_REF, saying whether we are trying to define a method or call
7575 one. SUPERFLAG says this is for a send to super; this makes a
7576 difference for the NeXT calling sequence in which the lookup and
7577 the method call are done together. If METH is null, user-defined
7578 arguments (i.e., beyond self and _cmd) shall be represented by `...'. */
7581 get_arg_type_list (tree meth, int context, int superflag)
7585 /* Receiver type. */
7586 if (flag_next_runtime && superflag)
7587 arglist = build_tree_list (NULL_TREE, objc_super_type);
7588 else if (context == METHOD_DEF && TREE_CODE (meth) == INSTANCE_METHOD_DECL)
7589 arglist = build_tree_list (NULL_TREE, objc_instance_type);
7591 arglist = build_tree_list (NULL_TREE, objc_object_type);
7593 /* Selector type - will eventually change to `int'. */
7594 chainon (arglist, build_tree_list (NULL_TREE, objc_selector_type));
7596 /* No actual method prototype given -- assume that remaining arguments
7601 /* Build a list of argument types. */
7602 for (akey = METHOD_SEL_ARGS (meth); akey; akey = DECL_CHAIN (akey))
7604 tree arg_type = TREE_VALUE (TREE_TYPE (akey));
7606 /* Decay argument types for the underlying C function as appropriate. */
7607 arg_type = objc_decay_parm_type (arg_type);
7609 chainon (arglist, build_tree_list (NULL_TREE, arg_type));
7612 if (METHOD_ADD_ARGS (meth))
7614 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (meth));
7615 akey; akey = TREE_CHAIN (akey))
7617 tree arg_type = TREE_TYPE (TREE_VALUE (akey));
7619 arg_type = objc_decay_parm_type (arg_type);
7621 chainon (arglist, build_tree_list (NULL_TREE, arg_type));
7624 if (!METHOD_ADD_ARGS_ELLIPSIS_P (meth))
7625 goto lack_of_ellipsis;
7630 chainon (arglist, OBJC_VOID_AT_END);
7637 check_duplicates (hash hsh, int methods, int is_class)
7639 tree meth = NULL_TREE;
7647 /* We have two or more methods with the same name but
7651 /* But just how different are those types? If
7652 -Wno-strict-selector-match is specified, we shall not
7653 complain if the differences are solely among types with
7654 identical size and alignment. */
7655 if (!warn_strict_selector_match)
7657 for (loop = hsh->list; loop; loop = loop->next)
7658 if (!comp_proto_with_proto (meth, loop->value, 0))
7667 bool type = TREE_CODE (meth) == INSTANCE_METHOD_DECL;
7669 warning_at (input_location, 0,
7670 "multiple methods named %<%c%E%> found",
7671 (is_class ? '+' : '-'),
7672 METHOD_SEL_NAME (meth));
7673 inform (DECL_SOURCE_LOCATION (meth), "using %<%c%s%>",
7675 identifier_to_locale (gen_method_decl (meth)));
7679 bool type = TREE_CODE (meth) == INSTANCE_METHOD_DECL;
7681 warning_at (input_location, 0,
7682 "multiple selectors named %<%c%E%> found",
7683 (is_class ? '+' : '-'),
7684 METHOD_SEL_NAME (meth));
7685 inform (DECL_SOURCE_LOCATION (meth), "found %<%c%s%>",
7687 identifier_to_locale (gen_method_decl (meth)));
7690 for (loop = hsh->list; loop; loop = loop->next)
7692 bool type = TREE_CODE (loop->value) == INSTANCE_METHOD_DECL;
7694 inform (DECL_SOURCE_LOCATION (loop->value), "also found %<%c%s%>",
7696 identifier_to_locale (gen_method_decl (loop->value)));
7703 /* If RECEIVER is a class reference, return the identifier node for
7704 the referenced class. RECEIVER is created by objc_get_class_reference,
7705 so we check the exact form created depending on which runtimes are
7709 receiver_is_class_object (tree receiver, int self, int super)
7711 tree chain, exp, arg;
7713 /* The receiver is 'self' or 'super' in the context of a class method. */
7714 if (objc_method_context
7715 && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
7718 ? CLASS_SUPER_NAME (implementation_template)
7719 : CLASS_NAME (implementation_template));
7721 if (flag_next_runtime)
7723 /* The receiver is a variable created by
7724 build_class_reference_decl. */
7725 if (TREE_CODE (receiver) == VAR_DECL && IS_CLASS (TREE_TYPE (receiver)))
7726 /* Look up the identifier. */
7727 for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
7728 if (TREE_PURPOSE (chain) == receiver)
7729 return TREE_VALUE (chain);
7732 /* The receiver is a function call that returns an id. Check if
7733 it is a call to objc_getClass, if so, pick up the class name. */
7734 if (TREE_CODE (receiver) == CALL_EXPR
7735 && (exp = CALL_EXPR_FN (receiver))
7736 && TREE_CODE (exp) == ADDR_EXPR
7737 && (exp = TREE_OPERAND (exp, 0))
7738 && TREE_CODE (exp) == FUNCTION_DECL
7739 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
7740 prototypes for objc_get_class(). Thankfully, they seem to share the
7741 same function type. */
7742 && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl)
7743 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), TAG_GETCLASS)
7744 /* We have a call to objc_get_class/objc_getClass! */
7745 && (arg = CALL_EXPR_ARG (receiver, 0)))
7748 if (TREE_CODE (arg) == ADDR_EXPR
7749 && (arg = TREE_OPERAND (arg, 0))
7750 && TREE_CODE (arg) == STRING_CST)
7751 /* Finally, we have the class name. */
7752 return get_identifier (TREE_STRING_POINTER (arg));
7757 /* If we are currently building a message expr, this holds
7758 the identifier of the selector of the message. This is
7759 used when printing warnings about argument mismatches. */
7761 static tree current_objc_message_selector = 0;
7764 objc_message_selector (void)
7766 return current_objc_message_selector;
7769 /* Construct an expression for sending a message.
7770 MESS has the object to send to in TREE_PURPOSE
7771 and the argument list (including selector) in TREE_VALUE.
7773 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
7774 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
7777 objc_build_message_expr (tree mess)
7779 tree receiver = TREE_PURPOSE (mess);
7782 tree args = TREE_PURPOSE (TREE_VALUE (mess));
7784 tree args = TREE_VALUE (mess);
7786 tree method_params = NULL_TREE;
7788 if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK)
7789 return error_mark_node;
7791 /* Obtain the full selector name. */
7792 switch (TREE_CODE (args))
7794 case IDENTIFIER_NODE:
7795 /* A unary selector. */
7799 sel_name = build_keyword_selector (args);
7805 /* Build the parameter list to give to the method. */
7806 if (TREE_CODE (args) == TREE_LIST)
7808 method_params = chainon (args, TREE_VALUE (TREE_VALUE (mess)));
7811 tree chain = args, prev = NULL_TREE;
7813 /* We have a keyword selector--check for comma expressions. */
7816 tree element = TREE_VALUE (chain);
7818 /* We have a comma expression, must collapse... */
7819 if (TREE_CODE (element) == TREE_LIST)
7822 TREE_CHAIN (prev) = element;
7827 chain = TREE_CHAIN (chain);
7829 method_params = args;
7834 if (processing_template_decl)
7835 /* Must wait until template instantiation time. */
7836 return build_min_nt (MESSAGE_SEND_EXPR, receiver, sel_name,
7840 return objc_finish_message_expr (receiver, sel_name, method_params);
7843 /* Look up method SEL_NAME that would be suitable for receiver
7844 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
7845 nonzero), and report on any duplicates. */
7848 lookup_method_in_hash_lists (tree sel_name, int is_class)
7850 hash method_prototype = NULL;
7853 method_prototype = hash_lookup (nst_method_hash_list,
7856 if (!method_prototype)
7858 method_prototype = hash_lookup (cls_method_hash_list,
7863 return check_duplicates (method_prototype, 1, is_class);
7866 /* The 'objc_finish_message_expr' routine is called from within
7867 'objc_build_message_expr' for non-template functions. In the case of
7868 C++ template functions, it is called from 'build_expr_from_tree'
7869 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded. */
7872 objc_finish_message_expr (tree receiver, tree sel_name, tree method_params)
7874 tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype;
7875 tree selector, retval, class_tree;
7876 int self, super, have_cast;
7878 /* We have used the receiver, so mark it as read. */
7879 mark_exp_read (receiver);
7881 /* Extract the receiver of the message, as well as its type
7882 (where the latter may take the form of a cast or be inferred
7883 from the implementation context). */
7885 while (TREE_CODE (rtype) == COMPOUND_EXPR
7886 || TREE_CODE (rtype) == MODIFY_EXPR
7887 || CONVERT_EXPR_P (rtype)
7888 || TREE_CODE (rtype) == COMPONENT_REF)
7889 rtype = TREE_OPERAND (rtype, 0);
7891 self = (rtype == self_decl);
7892 super = (rtype == UOBJC_SUPER_decl);
7893 rtype = TREE_TYPE (receiver);
7895 have_cast = (TREE_CODE (receiver) == NOP_EXPR
7896 || (TREE_CODE (receiver) == COMPOUND_EXPR
7897 && !IS_SUPER (rtype)));
7899 /* If we are calling [super dealloc], reset our warning flag. */
7900 if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name)))
7901 should_call_super_dealloc = 0;
7903 /* If the receiver is a class object, retrieve the corresponding
7904 @interface, if one exists. */
7905 class_tree = receiver_is_class_object (receiver, self, super);
7907 /* Now determine the receiver type (if an explicit cast has not been
7912 rtype = lookup_interface (class_tree);
7913 /* Handle `self' and `super'. */
7916 if (!CLASS_SUPER_NAME (implementation_template))
7918 error ("no super class declared in @interface for %qE",
7919 CLASS_NAME (implementation_template));
7920 return error_mark_node;
7922 rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template));
7925 rtype = lookup_interface (CLASS_NAME (implementation_template));
7928 /* If receiver is of type `id' or `Class' (or if the @interface for a
7929 class is not visible), we shall be satisfied with the existence of
7930 any instance or class method. */
7931 if (objc_is_id (rtype))
7933 class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE);
7934 rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
7935 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
7941 /* If messaging 'id <Protos>' or 'Class <Proto>', first search
7942 in protocols themselves for the method prototype. */
7944 = lookup_method_in_protocol_list (rprotos, sel_name,
7945 class_tree != NULL_TREE);
7947 /* If messaging 'Class <Proto>' but did not find a class method
7948 prototype, search for an instance method instead, and warn
7949 about having done so. */
7950 if (!method_prototype && !rtype && class_tree != NULL_TREE)
7953 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
7955 if (method_prototype)
7956 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
7957 sel_name, sel_name);
7963 tree orig_rtype = rtype;
7965 if (TREE_CODE (rtype) == POINTER_TYPE)
7966 rtype = TREE_TYPE (rtype);
7967 /* Traverse typedef aliases */
7968 while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype)
7969 && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL
7970 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)))
7971 rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype));
7972 if (TYPED_OBJECT (rtype))
7974 rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype);
7975 rtype = TYPE_OBJC_INTERFACE (rtype);
7977 /* If we could not find an @interface declaration, we must have
7978 only seen a @class declaration; so, we cannot say anything
7979 more intelligent about which methods the receiver will
7981 if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE)
7984 /* We could not find an @interface declaration, yet Message maybe in a
7985 @class's protocol. */
7986 if (!method_prototype && rprotos)
7988 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
7990 else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE
7991 || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE)
7993 /* We have a valid ObjC class name. Look up the method name
7994 in the published @interface for the class (and its
7997 = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE);
7999 /* If the method was not found in the @interface, it may still
8000 exist locally as part of the @implementation. */
8001 if (!method_prototype && objc_implementation_context
8002 && CLASS_NAME (objc_implementation_context)
8003 == OBJC_TYPE_NAME (rtype))
8007 ? CLASS_CLS_METHODS (objc_implementation_context)
8008 : CLASS_NST_METHODS (objc_implementation_context)),
8011 /* If we haven't found a candidate method by now, try looking for
8012 it in the protocol list. */
8013 if (!method_prototype && rprotos)
8015 = lookup_method_in_protocol_list (rprotos, sel_name,
8016 class_tree != NULL_TREE);
8020 warning (0, "invalid receiver type %qs",
8021 identifier_to_locale (gen_type_name (orig_rtype)));
8022 /* After issuing the "invalid receiver" warning, perform method
8023 lookup as if we were messaging 'id'. */
8024 rtype = rprotos = NULL_TREE;
8029 /* For 'id' or 'Class' receivers, search in the global hash table
8030 as a last resort. For all receivers, warn if protocol searches
8032 if (!method_prototype)
8035 warning (0, "%<%c%E%> not found in protocol(s)",
8036 (class_tree ? '+' : '-'),
8041 = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE);
8044 if (!method_prototype)
8046 static bool warn_missing_methods = false;
8049 warning (0, "%qE may not respond to %<%c%E%>",
8050 OBJC_TYPE_NAME (rtype),
8051 (class_tree ? '+' : '-'),
8053 /* If we are messaging an 'id' or 'Class' object and made it here,
8054 then we have failed to find _any_ instance or class method,
8057 warning (0, "no %<%c%E%> method found",
8058 (class_tree ? '+' : '-'),
8061 if (!warn_missing_methods)
8063 warning_at (input_location,
8064 0, "(Messages without a matching method signature");
8065 warning_at (input_location,
8066 0, "will be assumed to return %<id%> and accept");
8067 warning_at (input_location,
8068 0, "%<...%> as arguments.)");
8069 warn_missing_methods = true;
8074 /* Warn if the method is deprecated, but not if the receiver is
8075 a generic 'id'. 'id' is used to cast an object to a generic
8076 object of an unspecified class; in that case, we'll use
8077 whatever method prototype we can find to get the method
8078 argument and return types, but it is not appropriate to
8079 produce deprecation warnings since we don't know the class
8080 that the object will be of at runtime. The @interface(s) for
8081 that class may not even be available to the compiler right
8082 now, and it is perfectly possible that the method is marked
8083 as non-deprecated in such @interface(s).
8085 In practice this makes sense since casting an object to 'id'
8086 is often used precisely to turn off warnings associated with
8087 the object being of a particular class. */
8088 if (TREE_DEPRECATED (method_prototype) && rtype != NULL_TREE)
8089 warn_deprecated_use (method_prototype, NULL_TREE);
8093 /* Save the selector name for printing error messages. */
8094 current_objc_message_selector = sel_name;
8096 /* Build the parameters list for looking up the method.
8097 These are the object itself and the selector. */
8099 if (flag_typed_selectors)
8100 selector = build_typed_selector_reference (input_location,
8101 sel_name, method_prototype);
8103 selector = build_selector_reference (input_location, sel_name);
8105 retval = build_objc_method_call (input_location, super, method_prototype,
8107 selector, method_params);
8109 current_objc_message_selector = 0;
8114 /* Build a tree expression to send OBJECT the operation SELECTOR,
8115 looking up the method on object LOOKUP_OBJECT (often same as OBJECT),
8116 assuming the method has prototype METHOD_PROTOTYPE.
8117 (That is an INSTANCE_METHOD_DECL or CLASS_METHOD_DECL.)
8118 LOC is the location of the expression to build.
8119 Use METHOD_PARAMS as list of args to pass to the method.
8120 If SUPER_FLAG is nonzero, we look up the superclass's method. */
8123 build_objc_method_call (location_t loc, int super_flag, tree method_prototype,
8124 tree lookup_object, tree selector,
8127 tree sender = (super_flag ? umsg_super_decl :
8128 (!flag_next_runtime || flag_nil_receivers
8129 ? (flag_objc_direct_dispatch
8132 : umsg_nonnil_decl));
8133 tree rcv_p = (super_flag ? objc_super_type : objc_object_type);
8134 VEC(tree, gc) *parms = NULL;
8135 unsigned nparm = (method_params ? list_length (method_params) : 0);
8137 /* If a prototype for the method to be called exists, then cast
8138 the sender's return type and arguments to match that of the method.
8139 Otherwise, leave sender as is. */
8142 ? TREE_VALUE (TREE_TYPE (method_prototype))
8143 : objc_object_type);
8145 tree method_param_types =
8146 get_arg_type_list (method_prototype, METHOD_REF, super_flag);
8147 tree ftype = build_function_type (ret_type, method_param_types);
8151 if (method_prototype && METHOD_TYPE_ATTRIBUTES (method_prototype))
8152 ftype = build_type_attribute_variant (ftype,
8153 METHOD_TYPE_ATTRIBUTES
8154 (method_prototype));
8156 sender_cast = build_pointer_type (ftype);
8158 lookup_object = build_c_cast (loc, rcv_p, lookup_object);
8160 /* Use SAVE_EXPR to avoid evaluating the receiver twice. */
8161 lookup_object = save_expr (lookup_object);
8163 /* Param list + 2 slots for object and selector. */
8164 parms = VEC_alloc (tree, gc, nparm + 2);
8166 if (flag_next_runtime)
8168 /* If we are returning a struct in memory, and the address
8169 of that memory location is passed as a hidden first
8170 argument, then change which messenger entry point this
8171 expr will call. NB: Note that sender_cast remains
8172 unchanged (it already has a struct return type). */
8173 if (!targetm.calls.struct_value_rtx (0, 0)
8174 && (TREE_CODE (ret_type) == RECORD_TYPE
8175 || TREE_CODE (ret_type) == UNION_TYPE)
8176 && targetm.calls.return_in_memory (ret_type, 0))
8177 sender = (super_flag ? umsg_super_stret_decl :
8178 flag_nil_receivers ? umsg_stret_decl : umsg_nonnil_stret_decl);
8180 method = build_fold_addr_expr_loc (input_location, sender);
8181 /* Pass the object to the method. */
8182 VEC_quick_push (tree, parms, lookup_object);
8186 /* This is the portable (GNU) way. */
8187 /* First, call the lookup function to get a pointer to the method,
8188 then cast the pointer, then call it with the method arguments. */
8189 VEC(tree, gc) *tv = VEC_alloc (tree, gc, 2);
8190 VEC_quick_push (tree, tv, lookup_object);
8191 VEC_quick_push (tree, tv, selector);
8192 method = build_function_call_vec (loc, sender, tv, NULL);
8193 VEC_free (tree, gc, tv);
8195 /* Pass the appropriate object to the method. */
8196 VEC_quick_push (tree, parms, (super_flag ? self_decl : lookup_object));
8199 /* Pass the selector to the method. */
8200 VEC_quick_push (tree, parms, selector);
8201 /* Now append the remainder of the parms. */
8203 for (; method_params; method_params = TREE_CHAIN (method_params))
8204 VEC_quick_push (tree, parms, TREE_VALUE (method_params));
8206 /* Build an obj_type_ref, with the correct cast for the method call. */
8207 t = build3 (OBJ_TYPE_REF, sender_cast, method,
8208 lookup_object, size_zero_node);
8209 t = build_function_call_vec (loc, t, parms, NULL);\
8210 VEC_free (tree, gc, parms);
8215 build_protocol_reference (tree p)
8218 const char *proto_name;
8220 /* static struct _objc_protocol _OBJC_PROTOCOL_<mumble>; */
8222 proto_name = synth_id_with_class_suffix ("_OBJC_PROTOCOL", p);
8223 decl = start_var_decl (objc_protocol_template, proto_name);
8225 PROTOCOL_FORWARD_DECL (p) = decl;
8228 /* This function is called by the parser when (and only when) a
8229 @protocol() expression is found, in order to compile it. */
8231 objc_build_protocol_expr (tree protoname)
8234 tree p = lookup_protocol (protoname, /* warn if deprecated */ true);
8238 error ("cannot find protocol declaration for %qE",
8240 return error_mark_node;
8243 if (!PROTOCOL_FORWARD_DECL (p))
8244 build_protocol_reference (p);
8246 expr = build_unary_op (input_location,
8247 ADDR_EXPR, PROTOCOL_FORWARD_DECL (p), 0);
8249 /* ??? Ideally we'd build the reference with objc_protocol_type directly,
8250 if we have it, rather than converting it here. */
8251 expr = convert (objc_protocol_type, expr);
8253 /* The @protocol() expression is being compiled into a pointer to a
8254 statically allocated instance of the Protocol class. To become
8255 usable at runtime, the 'isa' pointer of the instance need to be
8256 fixed up at runtime by the runtime library, to point to the
8257 actual 'Protocol' class. */
8259 /* For the GNU runtime, put the static Protocol instance in the list
8260 of statically allocated instances, so that we make sure that its
8261 'isa' pointer is fixed up at runtime by the GNU runtime library
8262 to point to the Protocol class (at runtime, when loading the
8263 module, the GNU runtime library loops on the statically allocated
8264 instances (as found in the defs field in objc_symtab) and fixups
8265 all the 'isa' pointers of those objects). */
8266 if (! flag_next_runtime)
8268 /* This type is a struct containing the fields of a Protocol
8269 object. (Cfr. objc_protocol_type instead is the type of a pointer
8270 to such a struct). */
8271 tree protocol_struct_type = xref_tag
8272 (RECORD_TYPE, get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
8275 /* Look for the list of Protocol statically allocated instances
8276 to fixup at runtime. Create a new list to hold Protocol
8277 statically allocated instances, if the list is not found. At
8278 present there is only another list, holding NSConstantString
8279 static instances to be fixed up at runtime. */
8280 for (chain = &objc_static_instances;
8281 *chain && TREE_VALUE (*chain) != protocol_struct_type;
8282 chain = &TREE_CHAIN (*chain));
8285 *chain = tree_cons (NULL_TREE, protocol_struct_type, NULL_TREE);
8286 add_objc_string (OBJC_TYPE_NAME (protocol_struct_type),
8290 /* Add this statically allocated instance to the Protocol list. */
8291 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE,
8292 PROTOCOL_FORWARD_DECL (p),
8293 TREE_PURPOSE (*chain));
8300 /* This function is called by the parser when a @selector() expression
8301 is found, in order to compile it. It is only called by the parser
8302 and only to compile a @selector(). LOC is the location of the
8305 objc_build_selector_expr (location_t loc, tree selnamelist)
8309 /* Obtain the full selector name. */
8310 switch (TREE_CODE (selnamelist))
8312 case IDENTIFIER_NODE:
8313 /* A unary selector. */
8314 selname = selnamelist;
8317 selname = build_keyword_selector (selnamelist);
8323 /* If we are required to check @selector() expressions as they
8324 are found, check that the selector has been declared. */
8325 if (warn_undeclared_selector)
8327 /* Look the selector up in the list of all known class and
8328 instance methods (up to this line) to check that the selector
8332 /* First try with instance methods. */
8333 hsh = hash_lookup (nst_method_hash_list, selname);
8335 /* If not found, try with class methods. */
8338 hsh = hash_lookup (cls_method_hash_list, selname);
8341 /* If still not found, print out a warning. */
8344 warning (0, "undeclared selector %qE", selname);
8349 if (flag_typed_selectors)
8350 return build_typed_selector_reference (loc, selname, 0);
8352 return build_selector_reference (loc, selname);
8355 /* This is used to implement @encode(). See gcc/doc/objc.texi,
8356 section '@encode'. */
8358 objc_build_encode_expr (tree type)
8363 encode_type (type, obstack_object_size (&util_obstack),
8364 OBJC_ENCODE_INLINE_DEFS);
8365 obstack_1grow (&util_obstack, 0); /* null terminate string */
8366 string = XOBFINISH (&util_obstack, const char *);
8368 /* Synthesize a string that represents the encoded struct/union. */
8369 result = my_build_string (strlen (string) + 1, string);
8370 obstack_free (&util_obstack, util_firstobj);
8375 build_ivar_reference (tree id)
8377 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
8379 /* Historically, a class method that produced objects (factory
8380 method) would assign `self' to the instance that it
8381 allocated. This would effectively turn the class method into
8382 an instance method. Following this assignment, the instance
8383 variables could be accessed. That practice, while safe,
8384 violates the simple rule that a class method should not refer
8385 to an instance variable. It's better to catch the cases
8386 where this is done unknowingly than to support the above
8388 warning (0, "instance variable %qE accessed in class method",
8390 self_decl = convert (objc_instance_type, self_decl); /* cast */
8393 return objc_build_component_ref (build_indirect_ref (input_location,
8394 self_decl, RO_ARROW),
8398 /* Compute a hash value for a given method SEL_NAME. */
8401 hash_func (tree sel_name)
8403 const unsigned char *s
8404 = (const unsigned char *)IDENTIFIER_POINTER (sel_name);
8408 h = h * 67 + *s++ - 113;
8415 nst_method_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
8416 cls_method_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
8418 cls_name_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
8419 als_name_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
8421 /* Initialize the hash table used to hold the constant string objects. */
8422 string_htab = htab_create_ggc (31, string_hash,
8426 /* This routine adds sel_name to the hash list. sel_name is a class or alias
8427 name for the class. If alias name, then value is its underlying class.
8428 If class, the value is NULL_TREE. */
8431 hash_class_name_enter (hash *hashlist, tree sel_name, tree value)
8434 int slot = hash_func (sel_name) % SIZEHASHTABLE;
8436 obj = ggc_alloc_hashed_entry ();
8437 if (value != NULL_TREE)
8439 /* Save the underlying class for the 'alias' in the hash table */
8440 attr obj_attr = ggc_alloc_hashed_attribute ();
8441 obj_attr->value = value;
8442 obj->list = obj_attr;
8446 obj->next = hashlist[slot];
8447 obj->key = sel_name;
8449 hashlist[slot] = obj; /* append to front */
8454 Searches in the hash table looking for a match for class or alias name.
8458 hash_class_name_lookup (hash *hashlist, tree sel_name)
8462 target = hashlist[hash_func (sel_name) % SIZEHASHTABLE];
8466 if (sel_name == target->key)
8469 target = target->next;
8474 /* WARNING!!!! hash_enter is called with a method, and will peek
8475 inside to find its selector! But hash_lookup is given a selector
8476 directly, and looks for the selector that's inside the found
8477 entry's key (method) for comparison. */
8480 hash_enter (hash *hashlist, tree method)
8483 int slot = hash_func (METHOD_SEL_NAME (method)) % SIZEHASHTABLE;
8485 obj = ggc_alloc_hashed_entry ();
8487 obj->next = hashlist[slot];
8490 hashlist[slot] = obj; /* append to front */
8494 hash_lookup (hash *hashlist, tree sel_name)
8498 target = hashlist[hash_func (sel_name) % SIZEHASHTABLE];
8502 if (sel_name == METHOD_SEL_NAME (target->key))
8505 target = target->next;
8511 hash_add_attr (hash entry, tree value)
8515 obj = ggc_alloc_hashed_attribute ();
8516 obj->next = entry->list;
8519 entry->list = obj; /* append to front */
8523 lookup_method (tree mchain, tree method)
8527 if (TREE_CODE (method) == IDENTIFIER_NODE)
8530 key = METHOD_SEL_NAME (method);
8534 if (METHOD_SEL_NAME (mchain) == key)
8537 mchain = DECL_CHAIN (mchain);
8542 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance
8543 method in INTERFACE, along with any categories and protocols
8544 attached thereto. If method is not found, and the
8545 OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS, recursively examine the
8546 INTERFACE's superclass. If OBJC_LOOKUP_CLASS is set,
8547 OBJC_LOOKUP_NO_SUPER is clear, and no suitable class method could
8548 be found in INTERFACE or any of its superclasses, look for an
8549 _instance_ method of the same name in the root class as a last
8550 resort. This behaviour can be turned off by using
8551 OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS.
8553 If a suitable method cannot be found, return NULL_TREE. */
8556 lookup_method_static (tree interface, tree ident, int flags)
8558 tree meth = NULL_TREE, root_inter = NULL_TREE;
8559 tree inter = interface;
8560 int is_class = (flags & OBJC_LOOKUP_CLASS);
8561 int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER);
8562 int no_instance_methods_of_root_class = (flags & OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS);
8566 tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter);
8567 tree category = inter;
8569 /* First, look up the method in the class itself. */
8570 if ((meth = lookup_method (chain, ident)))
8573 /* Failing that, look for the method in each category of the class. */
8574 while ((category = CLASS_CATEGORY_LIST (category)))
8576 chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category);
8578 /* Check directly in each category. */
8579 if ((meth = lookup_method (chain, ident)))
8582 /* Failing that, check in each category's protocols. */
8583 if (CLASS_PROTOCOL_LIST (category))
8585 if ((meth = (lookup_method_in_protocol_list
8586 (CLASS_PROTOCOL_LIST (category), ident, is_class))))
8591 /* If not found in categories, check in protocols of the main class. */
8592 if (CLASS_PROTOCOL_LIST (inter))
8594 if ((meth = (lookup_method_in_protocol_list
8595 (CLASS_PROTOCOL_LIST (inter), ident, is_class))))
8599 /* If we were instructed not to look in superclasses, don't. */
8600 if (no_superclasses)
8603 /* Failing that, climb up the inheritance hierarchy. */
8605 inter = lookup_interface (CLASS_SUPER_NAME (inter));
8609 if (is_class && !no_instance_methods_of_root_class)
8611 /* If no class (factory) method was found, check if an _instance_
8612 method of the same name exists in the root class. This is what
8613 the Objective-C runtime will do. */
8614 return lookup_method_static (root_inter, ident, 0);
8618 /* If an instance method was not found, return 0. */
8623 /* Add the method to the hash list if it doesn't contain an identical
8627 add_method_to_hash_list (hash *hash_list, tree method)
8631 if (!(hsh = hash_lookup (hash_list, METHOD_SEL_NAME (method))))
8633 /* Install on a global chain. */
8634 hash_enter (hash_list, method);
8638 /* Check types against those; if different, add to a list. */
8640 int already_there = comp_proto_with_proto (method, hsh->key, 1);
8641 for (loop = hsh->list; !already_there && loop; loop = loop->next)
8642 already_there |= comp_proto_with_proto (method, loop->value, 1);
8644 hash_add_attr (hsh, method);
8649 objc_add_method (tree klass, tree method, int is_class, bool is_optional)
8653 /* @optional methods are added to protocol's OPTIONAL list. Note
8654 that this disables checking that the methods are implemented by
8655 classes implementing the protocol, since these checks only use
8656 the CLASS_CLS_METHODS and CLASS_NST_METHODS. */
8659 gcc_assert (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE);
8660 if (!(mth = lookup_method (is_class
8661 ? PROTOCOL_OPTIONAL_CLS_METHODS (klass)
8662 : PROTOCOL_OPTIONAL_NST_METHODS (klass),
8667 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass);
8668 PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method;
8672 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass);
8673 PROTOCOL_OPTIONAL_NST_METHODS (klass) = method;
8677 else if (!(mth = lookup_method (is_class
8678 ? CLASS_CLS_METHODS (klass)
8679 : CLASS_NST_METHODS (klass), method)))
8681 /* put method on list in reverse order */
8684 DECL_CHAIN (method) = CLASS_CLS_METHODS (klass);
8685 CLASS_CLS_METHODS (klass) = method;
8689 DECL_CHAIN (method) = CLASS_NST_METHODS (klass);
8690 CLASS_NST_METHODS (klass) = method;
8695 /* When processing an @interface for a class or category, give hard
8696 errors on methods with identical selectors but differing argument
8697 and/or return types. We do not do this for @implementations, because
8698 C/C++ will do it for us (i.e., there will be duplicate function
8699 definition errors). */
8700 if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE
8701 || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE)
8702 && !comp_proto_with_proto (method, mth, 1))
8703 error ("duplicate declaration of method %<%c%E%>",
8704 is_class ? '+' : '-',
8705 METHOD_SEL_NAME (mth));
8709 add_method_to_hash_list (cls_method_hash_list, method);
8712 add_method_to_hash_list (nst_method_hash_list, method);
8714 /* Instance methods in root classes (and categories thereof)
8715 may act as class methods as a last resort. We also add
8716 instance methods listed in @protocol declarations to
8717 the class hash table, on the assumption that @protocols
8718 may be adopted by root classes or categories. */
8719 if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
8720 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
8721 klass = lookup_interface (CLASS_NAME (klass));
8723 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE
8724 || !CLASS_SUPER_NAME (klass))
8725 add_method_to_hash_list (cls_method_hash_list, method);
8732 add_class (tree class_name, tree name)
8734 struct interface_tuple **slot;
8736 /* Put interfaces on list in reverse order. */
8737 TREE_CHAIN (class_name) = interface_chain;
8738 interface_chain = class_name;
8740 if (interface_htab == NULL)
8741 interface_htab = htab_create_ggc (31, hash_interface, eq_interface, NULL);
8742 slot = (struct interface_tuple **)
8743 htab_find_slot_with_hash (interface_htab, name,
8744 IDENTIFIER_HASH_VALUE (name),
8748 *slot = ggc_alloc_cleared_interface_tuple ();
8751 (*slot)->class_name = class_name;
8753 return interface_chain;
8757 add_category (tree klass, tree category)
8759 /* Put categories on list in reverse order. */
8760 tree cat = lookup_category (klass, CLASS_SUPER_NAME (category));
8764 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
8766 CLASS_SUPER_NAME (category));
8770 CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass);
8771 CLASS_CATEGORY_LIST (klass) = category;
8775 /* Called after parsing each instance variable declaration. Necessary to
8776 preserve typedefs and implement public/private...
8778 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
8781 add_instance_variable (tree klass, objc_ivar_visibility_kind visibility,
8784 tree field_type = TREE_TYPE (field_decl);
8785 const char *ivar_name = DECL_NAME (field_decl)
8786 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl)))
8790 if (TREE_CODE (field_type) == REFERENCE_TYPE)
8792 error ("illegal reference type specified for instance variable %qs",
8794 /* Return class as is without adding this ivar. */
8799 if (field_type == error_mark_node || !TYPE_SIZE (field_type)
8800 || TYPE_SIZE (field_type) == error_mark_node)
8801 /* 'type[0]' is allowed, but 'type[]' is not! */
8803 error ("instance variable %qs has unknown size", ivar_name);
8804 /* Return class as is without adding this ivar. */
8809 /* Check if the ivar being added has a non-POD C++ type. If so, we will
8810 need to either (1) warn the user about it or (2) generate suitable
8811 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
8812 methods (if '-fobjc-call-cxx-cdtors' was specified). */
8813 if (MAYBE_CLASS_TYPE_P (field_type)
8814 && (TYPE_NEEDS_CONSTRUCTING (field_type)
8815 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)
8816 || TYPE_POLYMORPHIC_P (field_type)))
8818 tree type_name = OBJC_TYPE_NAME (field_type);
8820 if (flag_objc_call_cxx_cdtors)
8822 /* Since the ObjC runtime will be calling the constructors and
8823 destructors for us, the only thing we can't handle is the lack
8824 of a default constructor. */
8825 if (TYPE_NEEDS_CONSTRUCTING (field_type)
8826 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type))
8828 warning (0, "type %qE has no default constructor to call",
8831 /* If we cannot call a constructor, we should also avoid
8832 calling the destructor, for symmetry. */
8833 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
8834 warning (0, "destructor for %qE shall not be run either",
8840 static bool warn_cxx_ivars = false;
8842 if (TYPE_POLYMORPHIC_P (field_type))
8844 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
8846 error ("type %qE has virtual member functions", type_name);
8847 error ("illegal aggregate type %qE specified "
8848 "for instance variable %qs",
8849 type_name, ivar_name);
8850 /* Return class as is without adding this ivar. */
8854 /* User-defined constructors and destructors are not known to Obj-C
8855 and hence will not be called. This may or may not be a problem. */
8856 if (TYPE_NEEDS_CONSTRUCTING (field_type))
8857 warning (0, "type %qE has a user-defined constructor", type_name);
8858 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
8859 warning (0, "type %qE has a user-defined destructor", type_name);
8861 if (!warn_cxx_ivars)
8863 warning (0, "C++ constructors and destructors will not "
8864 "be invoked for Objective-C fields");
8865 warn_cxx_ivars = true;
8871 /* Overload the public attribute, it is not used for FIELD_DECLs. */
8874 case OBJC_IVAR_VIS_PROTECTED:
8875 TREE_PUBLIC (field_decl) = 0;
8876 TREE_PRIVATE (field_decl) = 0;
8877 TREE_PROTECTED (field_decl) = 1;
8880 case OBJC_IVAR_VIS_PACKAGE:
8881 /* TODO: Implement the package variant. */
8882 case OBJC_IVAR_VIS_PUBLIC:
8883 TREE_PUBLIC (field_decl) = 1;
8884 TREE_PRIVATE (field_decl) = 0;
8885 TREE_PROTECTED (field_decl) = 0;
8888 case OBJC_IVAR_VIS_PRIVATE:
8889 TREE_PUBLIC (field_decl) = 0;
8890 TREE_PRIVATE (field_decl) = 1;
8891 TREE_PROTECTED (field_decl) = 0;
8896 CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl);
8903 is_ivar (tree decl_chain, tree ident)
8905 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
8906 if (DECL_NAME (decl_chain) == ident)
8911 /* True if the ivar is private and we are not in its implementation. */
8914 is_private (tree decl)
8916 return (TREE_PRIVATE (decl)
8917 && ! is_ivar (CLASS_IVARS (implementation_template),
8921 /* We have an instance variable reference;, check to see if it is public. */
8924 objc_is_public (tree expr, tree identifier)
8926 tree basetype, decl;
8929 if (processing_template_decl)
8933 if (TREE_TYPE (expr) == error_mark_node)
8936 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8938 if (basetype && TREE_CODE (basetype) == RECORD_TYPE)
8940 if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype))
8942 tree klass = lookup_interface (OBJC_TYPE_NAME (basetype));
8946 error ("cannot find interface declaration for %qE",
8947 OBJC_TYPE_NAME (basetype));
8951 if ((decl = is_ivar (get_class_ivars (klass, true), identifier)))
8953 if (TREE_PUBLIC (decl))
8956 /* Important difference between the Stepstone translator:
8957 all instance variables should be public within the context
8958 of the implementation. */
8959 if (objc_implementation_context
8960 && ((TREE_CODE (objc_implementation_context)
8961 == CLASS_IMPLEMENTATION_TYPE)
8962 || (TREE_CODE (objc_implementation_context)
8963 == CATEGORY_IMPLEMENTATION_TYPE)))
8965 tree curtype = TYPE_MAIN_VARIANT
8966 (CLASS_STATIC_TEMPLATE
8967 (implementation_template));
8969 if (basetype == curtype
8970 || DERIVED_FROM_P (basetype, curtype))
8972 int priv = is_private (decl);
8975 error ("instance variable %qE is declared private",
8982 /* The 2.95.2 compiler sometimes allowed C functions to access
8983 non-@public ivars. We will let this slide for now... */
8984 if (!objc_method_context)
8986 warning (0, "instance variable %qE is %s; "
8987 "this will be a hard error in the future",
8989 TREE_PRIVATE (decl) ? "@private" : "@protected");
8993 error ("instance variable %qE is declared %s",
8995 TREE_PRIVATE (decl) ? "private" : "protected");
9004 /* Make sure all methods in CHAIN (a list of method declarations from
9005 an @interface or a @protocol) are in IMPLEMENTATION (the
9006 implementation context). This is used to check for example that
9007 all methods declared in an @interface were implemented in an
9010 Some special methods (property setters/getters) are special and if
9011 they are not found in IMPLEMENTATION, we look them up in its
9015 check_methods (tree chain, tree implementation, int mtype)
9020 if (mtype == (int)'+')
9021 list = CLASS_CLS_METHODS (implementation);
9023 list = CLASS_NST_METHODS (implementation);
9027 /* If the method is associated with a dynamic property, then it
9028 is Ok not to have the method implementation, as it will be
9029 generated dynamically at runtime. To decide if the method is
9030 associated with a @dynamic property, we search the list of
9031 @synthesize and @dynamic for this implementation, and look
9032 for any @dynamic property with the same setter or getter name
9035 for (x = IMPL_PROPERTY_DECL (implementation); x; x = TREE_CHAIN (x))
9036 if (PROPERTY_DYNAMIC (x)
9037 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
9038 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
9043 chain = TREE_CHAIN (chain); /* next method... */
9047 if (!lookup_method (list, chain))
9049 /* If the method is a property setter/getter, we'll still
9050 allow it to be missing if it is implemented by
9051 'interface' or any of its superclasses. */
9052 tree property = METHOD_PROPERTY_CONTEXT (chain);
9055 /* Note that since this is a property getter/setter, it
9056 is obviously an instance method. */
9057 tree interface = NULL_TREE;
9059 /* For a category, first check the main class
9061 if (TREE_CODE (implementation) == CATEGORY_IMPLEMENTATION_TYPE)
9063 interface = lookup_interface (CLASS_NAME (implementation));
9065 /* If the method is found in the main class, it's Ok. */
9066 if (lookup_method (CLASS_NST_METHODS (interface), chain))
9068 chain = DECL_CHAIN (chain);
9072 /* Else, get the superclass. */
9073 if (CLASS_SUPER_NAME (interface))
9074 interface = lookup_interface (CLASS_SUPER_NAME (interface));
9076 interface = NULL_TREE;
9079 /* Get the superclass for classes. */
9080 if (TREE_CODE (implementation) == CLASS_IMPLEMENTATION_TYPE)
9082 if (CLASS_SUPER_NAME (implementation))
9083 interface = lookup_interface (CLASS_SUPER_NAME (implementation));
9085 interface = NULL_TREE;
9088 /* Now, interface is the superclass, if any; go check it. */
9091 if (lookup_method_static (interface, chain, 0))
9093 chain = DECL_CHAIN (chain);
9097 /* Else, fall through - warn. */
9101 switch (TREE_CODE (implementation))
9103 case CLASS_IMPLEMENTATION_TYPE:
9104 warning (0, "incomplete implementation of class %qE",
9105 CLASS_NAME (implementation));
9107 case CATEGORY_IMPLEMENTATION_TYPE:
9108 warning (0, "incomplete implementation of category %qE",
9109 CLASS_SUPER_NAME (implementation));
9117 warning (0, "method definition for %<%c%E%> not found",
9118 mtype, METHOD_SEL_NAME (chain));
9121 chain = DECL_CHAIN (chain);
9127 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
9130 conforms_to_protocol (tree klass, tree protocol)
9132 if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE)
9134 tree p = CLASS_PROTOCOL_LIST (klass);
9135 while (p && TREE_VALUE (p) != protocol)
9140 tree super = (CLASS_SUPER_NAME (klass)
9141 ? lookup_interface (CLASS_SUPER_NAME (klass))
9143 int tmp = super ? conforms_to_protocol (super, protocol) : 0;
9152 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
9153 CONTEXT. This is one of two mechanisms to check protocol integrity. */
9156 check_methods_accessible (tree chain, tree context, int mtype)
9160 tree base_context = context;
9164 /* If the method is associated with a dynamic property, then it
9165 is Ok not to have the method implementation, as it will be
9166 generated dynamically at runtime. Search for any @dynamic
9167 property with the same setter or getter name as this
9168 method. TODO: Use a hashtable lookup. */
9170 for (x = IMPL_PROPERTY_DECL (base_context); x; x = TREE_CHAIN (x))
9171 if (PROPERTY_DYNAMIC (x)
9172 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
9173 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
9178 chain = TREE_CHAIN (chain); /* next method... */
9182 context = base_context;
9186 list = CLASS_CLS_METHODS (context);
9188 list = CLASS_NST_METHODS (context);
9190 if (lookup_method (list, chain))
9193 switch (TREE_CODE (context))
9195 case CLASS_IMPLEMENTATION_TYPE:
9196 case CLASS_INTERFACE_TYPE:
9197 context = (CLASS_SUPER_NAME (context)
9198 ? lookup_interface (CLASS_SUPER_NAME (context))
9201 case CATEGORY_IMPLEMENTATION_TYPE:
9202 case CATEGORY_INTERFACE_TYPE:
9203 context = (CLASS_NAME (context)
9204 ? lookup_interface (CLASS_NAME (context))
9212 if (context == NULL_TREE)
9216 switch (TREE_CODE (objc_implementation_context))
9218 case CLASS_IMPLEMENTATION_TYPE:
9219 warning (0, "incomplete implementation of class %qE",
9220 CLASS_NAME (objc_implementation_context));
9222 case CATEGORY_IMPLEMENTATION_TYPE:
9223 warning (0, "incomplete implementation of category %qE",
9224 CLASS_SUPER_NAME (objc_implementation_context));
9231 warning (0, "method definition for %<%c%E%> not found",
9232 mtype, METHOD_SEL_NAME (chain));
9235 chain = TREE_CHAIN (chain); /* next method... */
9240 /* Check whether the current interface (accessible via
9241 'objc_implementation_context') actually implements protocol P, along
9242 with any protocols that P inherits. */
9245 check_protocol (tree p, const char *type, tree name)
9247 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
9251 /* Ensure that all protocols have bodies! */
9254 f1 = check_methods (PROTOCOL_CLS_METHODS (p),
9255 objc_implementation_context,
9257 f2 = check_methods (PROTOCOL_NST_METHODS (p),
9258 objc_implementation_context,
9263 f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
9264 objc_implementation_context,
9266 f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
9267 objc_implementation_context,
9272 warning (0, "%s %qE does not fully implement the %qE protocol",
9273 type, name, PROTOCOL_NAME (p));
9276 /* Check protocols recursively. */
9277 if (PROTOCOL_LIST (p))
9279 tree subs = PROTOCOL_LIST (p);
9281 lookup_interface (CLASS_SUPER_NAME (implementation_template));
9285 tree sub = TREE_VALUE (subs);
9287 /* If the superclass does not conform to the protocols
9288 inherited by P, then we must! */
9289 if (!super_class || !conforms_to_protocol (super_class, sub))
9290 check_protocol (sub, type, name);
9291 subs = TREE_CHAIN (subs);
9296 /* Check whether the current interface (accessible via
9297 'objc_implementation_context') actually implements the protocols listed
9301 check_protocols (tree proto_list, const char *type, tree name)
9303 for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
9305 tree p = TREE_VALUE (proto_list);
9307 check_protocol (p, type, name);
9311 /* Make sure that the class CLASS_NAME is defined
9312 CODE says which kind of thing CLASS_NAME ought to be.
9313 It can be CLASS_INTERFACE_TYPE, CLASS_IMPLEMENTATION_TYPE,
9314 CATEGORY_INTERFACE_TYPE, or CATEGORY_IMPLEMENTATION_TYPE. */
9317 start_class (enum tree_code code, tree class_name, tree super_name,
9318 tree protocol_list, tree attributes)
9323 if (current_namespace != global_namespace) {
9324 error ("Objective-C declarations may only appear in global scope");
9326 #endif /* OBJCPLUS */
9328 if (objc_implementation_context)
9330 warning (0, "%<@end%> missing in implementation context");
9331 finish_class (objc_implementation_context);
9332 objc_ivar_chain = NULL_TREE;
9333 objc_implementation_context = NULL_TREE;
9336 klass = make_node (code);
9337 TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS);
9339 /* Check for existence of the super class, if one was specified. Note
9340 that we must have seen an @interface, not just a @class. If we
9341 are looking at a @compatibility_alias, traverse it first. */
9342 if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE)
9345 tree super = objc_is_class_name (super_name);
9346 tree super_interface = NULL_TREE;
9349 super_interface = lookup_interface (super);
9351 if (!super_interface)
9353 error ("cannot find interface declaration for %qE, superclass of %qE",
9354 super ? super : super_name,
9356 super_name = NULL_TREE;
9360 if (TREE_DEPRECATED (super_interface))
9361 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
9367 CLASS_NAME (klass) = class_name;
9368 CLASS_SUPER_NAME (klass) = super_name;
9369 CLASS_CLS_METHODS (klass) = NULL_TREE;
9371 if (! objc_is_class_name (class_name)
9372 && (decl = lookup_name (class_name)))
9374 error ("%qE redeclared as different kind of symbol",
9376 error ("previous declaration of %q+D",
9382 case CLASS_IMPLEMENTATION_TYPE:
9386 for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
9387 if (TREE_VALUE (chain) == class_name)
9389 error ("reimplementation of class %qE",
9391 return error_mark_node;
9393 implemented_classes = tree_cons (NULL_TREE, class_name,
9394 implemented_classes);
9397 /* Reset for multiple classes per file. */
9400 objc_implementation_context = klass;
9402 /* Lookup the interface for this implementation. */
9404 if (!(implementation_template = lookup_interface (class_name)))
9406 warning (0, "cannot find interface declaration for %qE",
9408 add_class (implementation_template = objc_implementation_context,
9412 /* If a super class has been specified in the implementation,
9413 insure it conforms to the one specified in the interface. */
9416 && (super_name != CLASS_SUPER_NAME (implementation_template)))
9418 tree previous_name = CLASS_SUPER_NAME (implementation_template);
9419 error ("conflicting super class name %qE",
9422 error ("previous declaration of %qE", previous_name);
9424 error ("previous declaration");
9427 else if (! super_name)
9429 CLASS_SUPER_NAME (objc_implementation_context)
9430 = CLASS_SUPER_NAME (implementation_template);
9434 case CLASS_INTERFACE_TYPE:
9435 if (lookup_interface (class_name))
9437 error ("duplicate interface declaration for class %qE", class_name);
9439 warning (0, "duplicate interface declaration for class %qE", class_name);
9442 add_class (klass, class_name);
9445 CLASS_PROTOCOL_LIST (klass)
9446 = lookup_and_install_protocols (protocol_list);
9448 /* Determine if 'deprecated', the only attribute we recognize
9449 for classes, was used. Ignore all other attributes for now,
9450 but store them in the klass. */
9454 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
9456 tree name = TREE_PURPOSE (attribute);
9458 if (is_attribute_p ("deprecated", name))
9459 TREE_DEPRECATED (klass) = 1;
9461 TYPE_ATTRIBUTES (klass) = attributes;
9465 case CATEGORY_INTERFACE_TYPE:
9467 tree class_category_is_assoc_with;
9469 /* For a category, class_name is really the name of the class that
9470 the following set of methods will be associated with. We must
9471 find the interface so that can derive the objects template. */
9472 if (!(class_category_is_assoc_with = lookup_interface (class_name)))
9474 error ("cannot find interface declaration for %qE",
9476 exit (FATAL_EXIT_CODE);
9480 if (TREE_DEPRECATED (class_category_is_assoc_with))
9481 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
9483 add_category (class_category_is_assoc_with, klass);
9487 CLASS_PROTOCOL_LIST (klass)
9488 = lookup_and_install_protocols (protocol_list);
9492 case CATEGORY_IMPLEMENTATION_TYPE:
9493 /* Reset for multiple classes per file. */
9496 objc_implementation_context = klass;
9498 /* For a category, class_name is really the name of the class that
9499 the following set of methods will be associated with. We must
9500 find the interface so that can derive the objects template. */
9502 if (!(implementation_template = lookup_interface (class_name)))
9504 error ("cannot find interface declaration for %qE",
9506 exit (FATAL_EXIT_CODE);
9516 continue_class (tree klass)
9518 switch (TREE_CODE (klass))
9520 case CLASS_IMPLEMENTATION_TYPE:
9521 case CATEGORY_IMPLEMENTATION_TYPE:
9523 struct imp_entry *imp_entry;
9525 /* Check consistency of the instance variables. */
9527 if (CLASS_RAW_IVARS (klass))
9528 check_ivars (implementation_template, klass);
9530 /* code generation */
9532 push_lang_context (lang_name_c);
9534 build_private_template (implementation_template);
9535 uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template);
9536 objc_instance_type = build_pointer_type (uprivate_record);
9538 imp_entry = ggc_alloc_imp_entry ();
9540 imp_entry->next = imp_list;
9541 imp_entry->imp_context = klass;
9542 imp_entry->imp_template = implementation_template;
9544 synth_forward_declarations ();
9545 imp_entry->class_decl = UOBJC_CLASS_decl;
9546 imp_entry->meta_decl = UOBJC_METACLASS_decl;
9547 imp_entry->has_cxx_cdtors = 0;
9549 /* Append to front and increment count. */
9550 imp_list = imp_entry;
9551 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
9556 pop_lang_context ();
9557 #endif /* OBJCPLUS */
9559 return get_class_ivars (implementation_template, true);
9562 case CLASS_INTERFACE_TYPE:
9565 push_lang_context (lang_name_c);
9566 #endif /* OBJCPLUS */
9567 objc_collecting_ivars = 1;
9568 build_private_template (klass);
9569 objc_collecting_ivars = 0;
9571 pop_lang_context ();
9572 #endif /* OBJCPLUS */
9577 return error_mark_node;
9581 /* This routine builds name of the setter synthesized function. */
9583 objc_build_property_setter_name (tree ident)
9585 /* TODO: Use alloca to allocate buffer of appropriate size. */
9586 static char string[BUFSIZE];
9587 sprintf (string, "set%s:", IDENTIFIER_POINTER (ident));
9588 string[3] = TOUPPER (string[3]);
9592 /* This routine prepares the declarations of the property accessor
9593 helper functions (objc_getProperty(), etc) that are used when
9594 @synthesize is used. */
9596 build_objc_property_accessor_helpers (void)
9600 /* Declare the following function:
9602 objc_getProperty (id self, SEL _cmd,
9603 ptrdiff_t offset, BOOL is_atomic); */
9604 type = build_function_type_list (objc_object_type,
9610 objc_getProperty_decl = add_builtin_function ("objc_getProperty",
9611 type, 0, NOT_BUILT_IN,
9613 TREE_NOTHROW (objc_getProperty_decl) = 0;
9615 /* Declare the following function:
9617 objc_setProperty (id self, SEL _cmd,
9618 ptrdiff_t offset, id new_value,
9619 BOOL is_atomic, BOOL should_copy); */
9620 type = build_function_type_list (void_type_node,
9628 objc_setProperty_decl = add_builtin_function ("objc_setProperty",
9629 type, 0, NOT_BUILT_IN,
9631 TREE_NOTHROW (objc_setProperty_decl) = 0;
9633 /* This is the type of all of the following functions
9634 (objc_copyStruct(), objc_getPropertyStruct() and
9635 objc_setPropertyStruct()). */
9636 type = build_function_type_list (void_type_node,
9638 const_ptr_type_node,
9644 if (flag_next_runtime)
9646 /* Declare the following function:
9648 objc_copyStruct (void *destination, const void *source,
9649 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
9650 objc_copyStruct_decl = add_builtin_function ("objc_copyStruct",
9651 type, 0, NOT_BUILT_IN,
9653 TREE_NOTHROW (objc_copyStruct_decl) = 0;
9654 objc_getPropertyStruct_decl = NULL_TREE;
9655 objc_setPropertyStruct_decl = NULL_TREE;
9659 objc_copyStruct_decl = NULL_TREE;
9661 /* Declare the following function:
9663 objc_getPropertyStruct (void *destination, const void *source,
9664 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
9665 objc_getPropertyStruct_decl = add_builtin_function ("objc_getPropertyStruct",
9666 type, 0, NOT_BUILT_IN,
9668 TREE_NOTHROW (objc_getPropertyStruct_decl) = 0;
9669 /* Declare the following function:
9671 objc_setPropertyStruct (void *destination, const void *source,
9672 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
9673 objc_setPropertyStruct_decl = add_builtin_function ("objc_setPropertyStruct",
9674 type, 0, NOT_BUILT_IN,
9676 TREE_NOTHROW (objc_setPropertyStruct_decl) = 0;
9680 /* This looks up an ivar in a class (including superclasses). */
9682 lookup_ivar (tree interface, tree instance_variable_name)
9688 for (decl_chain = CLASS_IVARS (interface); decl_chain; decl_chain = DECL_CHAIN (decl_chain))
9689 if (DECL_NAME (decl_chain) == instance_variable_name)
9692 /* Not found. Search superclass if any. */
9693 if (CLASS_SUPER_NAME (interface))
9694 interface = lookup_interface (CLASS_SUPER_NAME (interface));
9700 /* This routine synthesizes a 'getter' method. This is only called
9701 for @synthesize properties. */
9703 objc_synthesize_getter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
9705 location_t location = DECL_SOURCE_LOCATION (property);
9710 /* If user has implemented a getter with same name then do nothing. */
9711 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
9712 PROPERTY_GETTER_NAME (property)))
9715 /* Find declaration of the property getter in the interface (or
9716 superclass, or protocol). There must be one. */
9717 decl = lookup_method_static (klass, PROPERTY_GETTER_NAME (property), 0);
9719 /* If one not declared in the interface, this condition has already
9720 been reported as user error (because property was not declared in
9725 /* Adapt the 'decl'. Use the source location of the @synthesize
9726 statement for error messages. */
9727 decl = copy_node (decl);
9728 DECL_SOURCE_LOCATION (decl) = location;
9730 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE);
9731 body = c_begin_compound_stmt (true);
9733 /* Now we need to decide how we build the getter. There are three
9736 for 'copy' or 'retain' properties we need to use the
9737 objc_getProperty() accessor helper which knows about retain and
9738 copy. It supports both 'nonatomic' and 'atomic' access.
9740 for 'nonatomic, assign' properties we can access the instance
9741 variable directly. 'nonatomic' means we don't have to use locks,
9742 and 'assign' means we don't have to worry about retain or copy.
9743 If you combine the two, it means we can just access the instance
9746 for 'atomic, assign' properties we use objc_copyStruct() (for the
9747 next runtime) or objc_getPropertyStruct() (for the GNU runtime). */
9748 switch (PROPERTY_ASSIGN_SEMANTICS (property))
9750 case OBJC_PROPERTY_RETAIN:
9751 case OBJC_PROPERTY_COPY:
9753 /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */
9754 tree cmd, ivar, offset, is_atomic;
9755 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
9757 /* Find the ivar to compute the offset. */
9758 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
9759 if (!ivar || is_private (ivar))
9761 /* This should never happen. */
9763 "can not find instance variable associated with property");
9764 ret_val = error_mark_node;
9767 offset = byte_position (ivar);
9769 if (PROPERTY_NONATOMIC (property))
9770 is_atomic = boolean_false_node;
9772 is_atomic = boolean_true_node;
9774 ret_val = build_function_call
9776 /* Function prototype. */
9777 objc_getProperty_decl,
9779 tree_cons /* self */
9780 (NULL_TREE, self_decl,
9781 tree_cons /* _cmd */
9783 tree_cons /* offset */
9785 tree_cons /* is_atomic */
9786 (NULL_TREE, is_atomic, NULL_TREE)))));
9789 case OBJC_PROPERTY_ASSIGN:
9790 if (PROPERTY_NONATOMIC (property))
9792 /* We build "return self->PROPERTY_IVAR_NAME;" */
9793 ret_val = objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property));
9799 <property type> __objc_property_temp;
9800 objc_getPropertyStruct (&__objc_property_temp,
9801 &(self->PROPERTY_IVAR_NAME),
9802 sizeof (type of self->PROPERTY_IVAR_NAME),
9805 return __objc_property_temp;
9807 For the NeXT runtime, we need to use objc_copyStruct
9808 instead of objc_getPropertyStruct. */
9809 tree objc_property_temp_decl, function_decl, function_call;
9810 tree size_of, is_atomic;
9812 objc_property_temp_decl = objc_create_temporary_var (TREE_TYPE (property), "__objc_property_temp");
9813 DECL_SOURCE_LOCATION (objc_property_temp_decl) = location;
9814 objc_property_temp_decl = lang_hooks.decls.pushdecl (objc_property_temp_decl);
9816 /* sizeof (ivar type). Since the ivar and the property have
9817 the same type, there is no need to lookup the ivar. */
9818 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
9819 true /* is_sizeof */,
9820 false /* complain */);
9822 if (PROPERTY_NONATOMIC (property))
9823 is_atomic = boolean_false_node;
9825 is_atomic = boolean_true_node;
9827 if (flag_next_runtime)
9828 function_decl = objc_copyStruct_decl;
9830 function_decl = objc_getPropertyStruct_decl;
9832 function_call = build_function_call
9834 /* Function prototype. */
9837 tree_cons /* &__objc_property_temp_decl */
9838 /* Warning: note that using build_fold_addr_expr_loc()
9839 here causes invalid code to be generated. */
9840 (NULL_TREE, build_unary_op (location, ADDR_EXPR, objc_property_temp_decl, 0),
9841 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
9842 (NULL_TREE, build_fold_addr_expr_loc (location,
9844 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
9845 tree_cons /* sizeof (PROPERTY_IVAR) */
9846 (NULL_TREE, size_of,
9847 tree_cons /* is_atomic */
9848 (NULL_TREE, is_atomic,
9849 /* TODO: This is currently ignored by the GNU
9850 runtime, but what about the next one ? */
9851 tree_cons /* has_strong */
9852 (NULL_TREE, boolean_true_node, NULL_TREE))))));
9854 add_stmt (function_call);
9856 ret_val = objc_property_temp_decl;
9863 gcc_assert (ret_val);
9866 finish_return_stmt (ret_val);
9868 c_finish_return (location, ret_val, NULL_TREE);
9871 add_stmt (c_end_compound_stmt (location, body, true));
9872 fn = current_function_decl;
9876 objc_finish_method_definition (fn);
9879 /* This routine synthesizes a 'setter' method. */
9882 objc_synthesize_setter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
9884 location_t location = DECL_SOURCE_LOCATION (property);
9887 tree new_value, statement;
9889 /* If user has implemented a setter with same name then do nothing. */
9890 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
9891 PROPERTY_SETTER_NAME (property)))
9894 /* Find declaration of the property setter in the interface (or
9895 superclass, or protocol). There must be one. */
9896 decl = lookup_method_static (klass, PROPERTY_SETTER_NAME (property), 0);
9898 /* If one not declared in the interface, this condition has already
9899 been reported as user error (because property was not declared in
9904 /* Adapt the 'decl'. Use the source location of the @synthesize
9905 statement for error messages. */
9906 decl = copy_node (decl);
9907 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (property);
9909 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE);
9911 body = c_begin_compound_stmt (true);
9913 /* The 'new_value' is the only argument to the method, which is the
9914 3rd argument of the function, after self and _cmd. We use twice
9915 TREE_CHAIN to move forward two arguments. */
9916 new_value = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)));
9918 /* This would presumably happen if the user has specified a
9919 prototype for the setter that does not have an argument! */
9920 if (new_value == NULL_TREE)
9922 /* TODO: This should be caught much earlier than this. */
9923 error_at (DECL_SOURCE_LOCATION (decl), "invalid setter, it must have one argument");
9924 /* Try to recover somehow. */
9925 new_value = error_mark_node;
9928 /* Now we need to decide how we build the setter. There are three
9931 for 'copy' or 'retain' properties we need to use the
9932 objc_setProperty() accessor helper which knows about retain and
9933 copy. It supports both 'nonatomic' and 'atomic' access.
9935 for 'nonatomic, assign' properties we can access the instance
9936 variable directly. 'nonatomic' means we don't have to use locks,
9937 and 'assign' means we don't have to worry about retain or copy.
9938 If you combine the two, it means we can just access the instance
9941 for 'atomic, assign' properties we use objc_copyStruct() (for the
9942 next runtime) or objc_setPropertyStruct() (for the GNU runtime). */
9943 switch (PROPERTY_ASSIGN_SEMANTICS (property))
9945 case OBJC_PROPERTY_RETAIN:
9946 case OBJC_PROPERTY_COPY:
9948 /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */
9949 tree cmd, ivar, offset, is_atomic, should_copy;
9950 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
9952 /* Find the ivar to compute the offset. */
9953 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
9954 if (!ivar || is_private (ivar))
9957 "can not find instance variable associated with property");
9958 statement = error_mark_node;
9961 offset = byte_position (ivar);
9963 if (PROPERTY_NONATOMIC (property))
9964 is_atomic = boolean_false_node;
9966 is_atomic = boolean_true_node;
9968 if (PROPERTY_ASSIGN_SEMANTICS (property) == OBJC_PROPERTY_COPY)
9969 should_copy = boolean_true_node;
9971 should_copy = boolean_false_node;
9973 statement = build_function_call
9975 /* Function prototype. */
9976 objc_setProperty_decl,
9978 tree_cons /* self */
9979 (NULL_TREE, self_decl,
9980 tree_cons /* _cmd */
9982 tree_cons /* offset */
9984 tree_cons /* new_value */
9985 (NULL_TREE, new_value,
9986 tree_cons /* is_atomic */
9987 (NULL_TREE, is_atomic,
9988 tree_cons /* should_copy */
9989 (NULL_TREE, should_copy, NULL_TREE)))))));
9992 case OBJC_PROPERTY_ASSIGN:
9993 if (PROPERTY_NONATOMIC (property))
9995 /* We build "self->PROPERTY_IVAR_NAME = new_value;" */
9996 statement = build_modify_expr
9998 objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)),
9999 NULL_TREE, NOP_EXPR,
10000 location, new_value, NULL_TREE);
10006 objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME),
10008 sizeof (type of self->PROPERTY_IVAR_NAME),
10012 For the NeXT runtime, we need to use objc_copyStruct
10013 instead of objc_getPropertyStruct. */
10014 tree function_decl, size_of, is_atomic;
10016 /* sizeof (ivar type). Since the ivar and the property have
10017 the same type, there is no need to lookup the ivar. */
10018 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
10019 true /* is_sizeof */,
10020 false /* complain */);
10022 if (PROPERTY_NONATOMIC (property))
10023 is_atomic = boolean_false_node;
10025 is_atomic = boolean_true_node;
10027 if (flag_next_runtime)
10028 function_decl = objc_copyStruct_decl;
10030 function_decl = objc_setPropertyStruct_decl;
10032 statement = build_function_call
10034 /* Function prototype. */
10037 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
10038 (NULL_TREE, build_fold_addr_expr_loc (location,
10040 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
10041 tree_cons /* &new_value */
10042 (NULL_TREE, build_fold_addr_expr_loc (location, new_value),
10043 tree_cons /* sizeof (PROPERTY_IVAR) */
10044 (NULL_TREE, size_of,
10045 tree_cons /* is_atomic */
10046 (NULL_TREE, is_atomic,
10047 /* TODO: This is currently ignored by the GNU
10048 runtime, but what about the next one ? */
10049 tree_cons /* has_strong */
10050 (NULL_TREE, boolean_true_node, NULL_TREE))))));
10054 gcc_unreachable ();
10056 gcc_assert (statement);
10058 add_stmt (statement);
10059 add_stmt (c_end_compound_stmt (location, body, true));
10060 fn = current_function_decl;
10062 finish_function ();
10064 objc_finish_method_definition (fn);
10067 /* This function is a sub-routine of objc_add_synthesize_declaration.
10068 It is called for each property to synthesize once we have
10069 determined that the context is Ok. */
10071 objc_add_synthesize_declaration_for_property (location_t location, tree interface,
10072 tree property_name, tree ivar_name)
10074 /* Find the @property declaration. */
10078 /* Check that synthesize or dynamic has not already been used for
10079 the same property. */
10080 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
10081 if (PROPERTY_NAME (property) == property_name)
10083 location_t original_location = DECL_SOURCE_LOCATION (property);
10085 if (PROPERTY_DYNAMIC (property))
10086 error_at (location, "property %qs already specified in %<@dynamic%>",
10087 IDENTIFIER_POINTER (property_name));
10089 error_at (location, "property %qs already specified in %<@synthesize%>",
10090 IDENTIFIER_POINTER (property_name));
10092 if (original_location != UNKNOWN_LOCATION)
10093 inform (original_location, "originally specified here");
10097 /* Check that the property is declared in the interface. It could
10098 also be declared in a superclass or protocol. */
10099 property = lookup_property (interface, property_name);
10103 error_at (location, "no declaration of property %qs found in the interface",
10104 IDENTIFIER_POINTER (property_name));
10109 /* We have to copy the property, because we want to chain it to
10110 the implementation context, and we want to store the source
10111 location of the @synthesize, not of the original
10113 property = copy_node (property);
10114 DECL_SOURCE_LOCATION (property) = location;
10117 /* Determine PROPERTY_IVAR_NAME. */
10118 if (ivar_name == NULL_TREE)
10119 ivar_name = property_name;
10121 /* Check that the instance variable exists. You can only use an
10122 instance variable from the same class, not one from the
10123 superclass (this makes sense as it allows us to check that an
10124 instance variable is only used in one synthesized property). */
10126 tree ivar = is_ivar (CLASS_IVARS (interface), ivar_name);
10130 error_at (location, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar",
10131 IDENTIFIER_POINTER (property_name));
10135 if (DECL_BIT_FIELD_TYPE (ivar))
10136 type_of_ivar = DECL_BIT_FIELD_TYPE (ivar);
10138 type_of_ivar = TREE_TYPE (ivar);
10140 /* If the instance variable has a different C type, we throw an error ... */
10141 if (!comptypes (TREE_TYPE (property), type_of_ivar)
10142 /* ... unless the property is readonly, in which case we allow
10143 the instance variable to be more specialized (this means we
10144 can generate the getter all right and it works). */
10145 && (!PROPERTY_READONLY (property)
10146 || !objc_compare_types (TREE_TYPE (property),
10147 type_of_ivar, -5, NULL_TREE)))
10149 location_t original_location = DECL_SOURCE_LOCATION (ivar);
10151 error_at (location, "property %qs is using instance variable %qs of incompatible type",
10152 IDENTIFIER_POINTER (property_name),
10153 IDENTIFIER_POINTER (ivar_name));
10155 if (original_location != UNKNOWN_LOCATION)
10156 inform (original_location, "originally specified here");
10159 /* If the instance variable is a bitfield, the property must be
10160 'assign', 'nonatomic' because the runtime getter/setter helper
10161 do not work with bitfield instance variables. */
10162 if (DECL_BIT_FIELD_TYPE (ivar))
10164 /* If there is an error, we return and not generate any
10165 getter/setter because trying to set up the runtime
10166 getter/setter helper calls with bitfields is at high risk
10169 if (PROPERTY_ASSIGN_SEMANTICS (property) != OBJC_PROPERTY_ASSIGN)
10171 location_t original_location = DECL_SOURCE_LOCATION (ivar);
10173 error_at (location, "'assign' property %qs is using bit-field instance variable %qs",
10174 IDENTIFIER_POINTER (property_name),
10175 IDENTIFIER_POINTER (ivar_name));
10177 if (original_location != UNKNOWN_LOCATION)
10178 inform (original_location, "originally specified here");
10182 if (!PROPERTY_NONATOMIC (property))
10184 location_t original_location = DECL_SOURCE_LOCATION (ivar);
10186 error_at (location, "'atomic' property %qs is using bit-field instance variable %qs",
10187 IDENTIFIER_POINTER (property_name),
10188 IDENTIFIER_POINTER (ivar_name));
10190 if (original_location != UNKNOWN_LOCATION)
10191 inform (original_location, "originally specified here");
10197 /* Check that no other property is using the same instance
10199 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
10200 if (PROPERTY_IVAR_NAME (x) == ivar_name)
10202 location_t original_location = DECL_SOURCE_LOCATION (x);
10204 error_at (location, "property %qs is using the same instance variable as property %qs",
10205 IDENTIFIER_POINTER (property_name),
10206 IDENTIFIER_POINTER (PROPERTY_NAME (x)));
10208 if (original_location != UNKNOWN_LOCATION)
10209 inform (original_location, "originally specified here");
10211 /* We keep going on. This won't cause the compiler to fail;
10212 the failure would most likely be at runtime. */
10215 /* Note that a @synthesize (and only a @synthesize) always sets
10216 PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a
10217 @synthesize by that. */
10218 PROPERTY_IVAR_NAME (property) = ivar_name;
10220 /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the
10221 original declaration; they are always set (with the exception of
10222 PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */
10224 /* Add the property to the list of properties for current implementation. */
10225 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
10226 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
10228 /* Note how we don't actually synthesize the getter/setter here; it
10229 would be very natural, but we may miss the fact that the user has
10230 implemented his own getter/setter later on in the @implementation
10231 (in which case we shouldn't generate getter/setter). We wait
10232 until we have parsed it all before generating the code. */
10235 /* This function is called by the parser after a @synthesize
10236 expression is parsed. 'location' is the location of the
10237 @synthesize expression, and 'property_and_ivar_list' is a chained
10238 list of the property and ivar names. */
10240 objc_add_synthesize_declaration (location_t location, tree property_and_ivar_list)
10242 tree interface, chain;
10244 if (flag_objc1_only)
10245 error_at (input_location, "%<@synthesize%> is not available in Objective-C 1.0");
10247 if (property_and_ivar_list == error_mark_node)
10250 if (!objc_implementation_context)
10252 /* We can get here only in Objective-C; the Objective-C++ parser
10253 detects the problem while parsing, outputs the error
10254 "misplaced '@synthesize' Objective-C++ construct" and skips
10255 the declaration. */
10256 error_at (location, "%<@synthesize%> not in @implementation context");
10260 if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE)
10262 error_at (location, "%<@synthesize%> can not be used in categories");
10266 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
10269 /* I can't see how this could happen, but it is good as a safety check. */
10270 error_at (location,
10271 "%<@synthesize%> requires the @interface of the class to be available");
10275 /* Now, iterate over the properties and do each of them. */
10276 for (chain = property_and_ivar_list; chain; chain = TREE_CHAIN (chain))
10278 objc_add_synthesize_declaration_for_property (location, interface, TREE_VALUE (chain),
10279 TREE_PURPOSE (chain));
10283 /* This function is a sub-routine of objc_add_dynamic_declaration. It
10284 is called for each property to mark as dynamic once we have
10285 determined that the context is Ok. */
10287 objc_add_dynamic_declaration_for_property (location_t location, tree interface,
10288 tree property_name)
10290 /* Find the @property declaration. */
10293 /* Check that synthesize or dynamic has not already been used for
10294 the same property. */
10295 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
10296 if (PROPERTY_NAME (property) == property_name)
10298 location_t original_location = DECL_SOURCE_LOCATION (property);
10300 if (PROPERTY_DYNAMIC (property))
10301 error_at (location, "property %qs already specified in %<@dynamic%>",
10302 IDENTIFIER_POINTER (property_name));
10304 error_at (location, "property %qs already specified in %<@synthesize%>",
10305 IDENTIFIER_POINTER (property_name));
10307 if (original_location != UNKNOWN_LOCATION)
10308 inform (original_location, "originally specified here");
10312 /* Check that the property is declared in the interface. It could
10313 also be declared in a superclass or protocol. */
10314 property = lookup_property (interface, property_name);
10318 error_at (location, "no declaration of property %qs found in the interface",
10319 IDENTIFIER_POINTER (property_name));
10324 /* We have to copy the property, because we want to chain it to
10325 the implementation context, and we want to store the source
10326 location of the @synthesize, not of the original
10328 property = copy_node (property);
10329 DECL_SOURCE_LOCATION (property) = location;
10332 /* Note that a @dynamic (and only a @dynamic) always sets
10333 PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that.
10334 (actually, as explained above, PROPERTY_DECL generated by
10335 @property and associated with a @dynamic property are also marked
10336 as PROPERTY_DYNAMIC). */
10337 PROPERTY_DYNAMIC (property) = 1;
10339 /* Add the property to the list of properties for current implementation. */
10340 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
10341 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
10344 /* This function is called by the parser after a @dynamic expression
10345 is parsed. 'location' is the location of the @dynamic expression,
10346 and 'property_list' is a chained list of all the property
10349 objc_add_dynamic_declaration (location_t location, tree property_list)
10351 tree interface, chain;
10353 if (flag_objc1_only)
10354 error_at (input_location, "%<@dynamic%> is not available in Objective-C 1.0");
10356 if (property_list == error_mark_node)
10359 if (!objc_implementation_context)
10361 /* We can get here only in Objective-C; the Objective-C++ parser
10362 detects the problem while parsing, outputs the error
10363 "misplaced '@dynamic' Objective-C++ construct" and skips the
10365 error_at (location, "%<@dynamic%> not in @implementation context");
10369 /* @dynamic is allowed in categories. */
10370 switch (TREE_CODE (objc_implementation_context))
10372 case CLASS_IMPLEMENTATION_TYPE:
10373 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
10375 case CATEGORY_IMPLEMENTATION_TYPE:
10376 interface = lookup_category (implementation_template,
10377 CLASS_SUPER_NAME (objc_implementation_context));
10380 gcc_unreachable ();
10385 /* I can't see how this could happen, but it is good as a safety check. */
10386 error_at (location,
10387 "%<@dynamic%> requires the @interface of the class to be available");
10391 /* Now, iterate over the properties and do each of them. */
10392 for (chain = property_list; chain; chain = TREE_CHAIN (chain))
10394 objc_add_dynamic_declaration_for_property (location, interface, TREE_VALUE (chain));
10398 /* Main routine to generate code/data for all the property information for
10399 current implementation (class or category). CLASS is the interface where
10400 ivars are declared. CLASS_METHODS is where methods are found which
10401 could be a class or a category depending on whether we are implementing
10402 property of a class or a category. */
10405 objc_gen_property_data (tree klass, tree class_methods)
10409 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
10411 /* @dynamic property - nothing to check or synthesize. */
10412 if (PROPERTY_DYNAMIC (x))
10415 /* @synthesize property - need to synthesize the accessors. */
10416 if (PROPERTY_IVAR_NAME (x))
10418 objc_synthesize_getter (klass, class_methods, x);
10420 if (PROPERTY_READONLY (x) == 0)
10421 objc_synthesize_setter (klass, class_methods, x);
10426 gcc_unreachable ();
10430 /* This is called once we see the "@end" in an interface/implementation. */
10433 finish_class (tree klass)
10435 switch (TREE_CODE (klass))
10437 case CLASS_IMPLEMENTATION_TYPE:
10439 /* All code generation is done in finish_objc. */
10441 /* Generate what needed for property; setters, getters, etc. */
10442 objc_gen_property_data (implementation_template, implementation_template);
10444 if (implementation_template != objc_implementation_context)
10446 /* Ensure that all method listed in the interface contain bodies. */
10447 check_methods (CLASS_CLS_METHODS (implementation_template),
10448 objc_implementation_context, '+');
10449 check_methods (CLASS_NST_METHODS (implementation_template),
10450 objc_implementation_context, '-');
10452 if (CLASS_PROTOCOL_LIST (implementation_template))
10453 check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
10455 CLASS_NAME (objc_implementation_context));
10459 case CATEGORY_IMPLEMENTATION_TYPE:
10461 tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass));
10465 /* Generate what needed for property; setters, getters, etc. */
10466 objc_gen_property_data (implementation_template, category);
10468 /* Ensure all method listed in the interface contain bodies. */
10469 check_methods (CLASS_CLS_METHODS (category),
10470 objc_implementation_context, '+');
10471 check_methods (CLASS_NST_METHODS (category),
10472 objc_implementation_context, '-');
10474 if (CLASS_PROTOCOL_LIST (category))
10475 check_protocols (CLASS_PROTOCOL_LIST (category),
10477 CLASS_SUPER_NAME (objc_implementation_context));
10481 case CLASS_INTERFACE_TYPE:
10482 case CATEGORY_INTERFACE_TYPE:
10483 case PROTOCOL_INTERFACE_TYPE:
10485 /* Process properties of the class. */
10487 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
10489 /* Now we check that the appropriate getter is declared,
10490 and if not, we declare one ourselves. */
10491 tree getter_decl = lookup_method (CLASS_NST_METHODS (klass),
10492 PROPERTY_GETTER_NAME (x));
10496 /* TODO: Check that the declaration is consistent with the property. */
10501 /* Generate an instance method declaration for the
10502 getter; for example "- (id) name;". In general it
10503 will be of the form
10504 -(type)property_getter_name; */
10505 tree rettype = build_tree_list (NULL_TREE, TREE_TYPE (x));
10506 getter_decl = build_method_decl (INSTANCE_METHOD_DECL,
10507 rettype, PROPERTY_GETTER_NAME (x),
10509 objc_add_method (objc_interface_context, getter_decl, false, false);
10510 METHOD_PROPERTY_CONTEXT (getter_decl) = x;
10513 if (PROPERTY_READONLY (x) == 0)
10515 /* Now we check that the appropriate setter is declared,
10516 and if not, we declare on ourselves. */
10517 tree setter_decl = lookup_method (CLASS_NST_METHODS (klass),
10518 PROPERTY_SETTER_NAME (x));
10522 /* TODO: Check that the declaration is consistent with the property. */
10527 /* The setter name is something like 'setName:'.
10528 We need the substring 'setName' to build the
10529 method declaration due to how the declaration
10530 works. TODO: build_method_decl() will then
10531 generate back 'setName:' from 'setName'; it
10532 would be more efficient to hook into there. */
10533 const char *full_setter_name = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x));
10534 size_t length = strlen (full_setter_name);
10535 char *setter_name = (char *) alloca (length);
10536 tree ret_type, selector, arg_type, arg_name;
10538 strcpy (setter_name, full_setter_name);
10539 setter_name[length - 1] = '\0';
10540 ret_type = build_tree_list (NULL_TREE, void_type_node);
10541 arg_type = build_tree_list (NULL_TREE, TREE_TYPE (x));
10542 arg_name = get_identifier ("_value");
10543 selector = objc_build_keyword_decl (get_identifier (setter_name),
10544 arg_type, arg_name, NULL);
10545 setter_decl = build_method_decl (INSTANCE_METHOD_DECL,
10546 ret_type, selector,
10547 build_tree_list (NULL_TREE, NULL_TREE),
10549 objc_add_method (objc_interface_context, setter_decl, false, false);
10550 METHOD_PROPERTY_CONTEXT (setter_decl) = x;
10557 gcc_unreachable ();
10563 add_protocol (tree protocol)
10565 /* Put protocol on list in reverse order. */
10566 TREE_CHAIN (protocol) = protocol_chain;
10567 protocol_chain = protocol;
10568 return protocol_chain;
10571 /* Looks up a protocol. If 'warn_if_deprecated' is true, a warning is
10572 emitted if the protocol is deprecated. */
10575 lookup_protocol (tree ident, bool warn_if_deprecated)
10579 for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
10580 if (ident == PROTOCOL_NAME (chain))
10582 if (warn_if_deprecated && TREE_DEPRECATED (chain))
10584 /* It would be nice to use warn_deprecated_use() here, but
10585 we are using TREE_CHAIN (which is supposed to be the
10586 TYPE_STUB_DECL for a TYPE) for something different. */
10587 warning (OPT_Wdeprecated_declarations, "protocol %qE is deprecated",
10588 PROTOCOL_NAME (chain));
10597 /* This function forward declares the protocols named by NAMES. If
10598 they are already declared or defined, the function has no effect. */
10601 objc_declare_protocols (tree names, tree attributes)
10604 bool deprecated = false;
10607 if (current_namespace != global_namespace) {
10608 error ("Objective-C declarations may only appear in global scope");
10610 #endif /* OBJCPLUS */
10612 /* Determine if 'deprecated', the only attribute we recognize for
10613 protocols, was used. Ignore all other attributes. */
10617 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
10619 tree name = TREE_PURPOSE (attribute);
10621 if (is_attribute_p ("deprecated", name))
10626 for (list = names; list; list = TREE_CHAIN (list))
10628 tree name = TREE_VALUE (list);
10630 if (lookup_protocol (name, /* warn if deprecated */ false) == NULL_TREE)
10632 tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
10634 TYPE_LANG_SLOT_1 (protocol)
10635 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
10636 PROTOCOL_NAME (protocol) = name;
10637 PROTOCOL_LIST (protocol) = NULL_TREE;
10638 add_protocol (protocol);
10639 PROTOCOL_DEFINED (protocol) = 0;
10640 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
10644 TYPE_ATTRIBUTES (protocol) = attributes;
10646 TREE_DEPRECATED (protocol) = 1;
10653 start_protocol (enum tree_code code, tree name, tree list, tree attributes)
10656 bool deprecated = false;
10659 if (current_namespace != global_namespace) {
10660 error ("Objective-C declarations may only appear in global scope");
10662 #endif /* OBJCPLUS */
10664 /* Determine if 'deprecated', the only attribute we recognize for
10665 protocols, was used. Ignore all other attributes. */
10669 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
10671 tree name = TREE_PURPOSE (attribute);
10673 if (is_attribute_p ("deprecated", name))
10678 protocol = lookup_protocol (name, /* warn_if_deprecated */ false);
10682 protocol = make_node (code);
10683 TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
10685 PROTOCOL_NAME (protocol) = name;
10686 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list);
10687 add_protocol (protocol);
10688 PROTOCOL_DEFINED (protocol) = 1;
10689 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
10691 check_protocol_recursively (protocol, list);
10693 else if (! PROTOCOL_DEFINED (protocol))
10695 PROTOCOL_DEFINED (protocol) = 1;
10696 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list);
10698 check_protocol_recursively (protocol, list);
10702 warning (0, "duplicate declaration for protocol %qE",
10708 TYPE_ATTRIBUTES (protocol) = attributes;
10710 TREE_DEPRECATED (protocol) = 1;
10717 /* "Encode" a data type into a string, which grows in util_obstack.
10719 The format is described in gcc/doc/objc.texi, section 'Type
10722 Most of the encode_xxx functions have a 'type' argument, which is
10723 the type to encode, and an integer 'curtype' argument, which is the
10724 index in the encoding string of the beginning of the encoding of
10725 the current type, and allows you to find what characters have
10726 already been written for the current type (they are the ones in the
10727 current encoding string starting from 'curtype').
10729 For example, if we are encoding a method which returns 'int' and
10730 takes a 'char **' argument, then when we get to the point of
10731 encoding the 'char **' argument, the encoded string already
10732 contains 'i12@0:4' (assuming a pointer size of 4 bytes). So,
10733 'curtype' will be set to 7 when starting to encode 'char **'.
10734 During the whole of the encoding of 'char **', 'curtype' will be
10735 fixed at 7, so the routine encoding the second pointer can find out
10736 that it's actually encoding a pointer to a pointer by looking
10737 backwards at what has already been encoded for the current type,
10738 and seeing there is a "^" (meaning a pointer) in there.
10742 /* Encode type qualifiers encodes one of the "PQ" Objective-C
10743 keywords, ie 'in', 'out', 'inout', 'bycopy', 'byref', 'oneway'.
10744 'const', instead, is encoded directly as part of the type.
10748 encode_type_qualifiers (tree declspecs)
10752 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10754 /* FIXME: Shouldn't we use token->keyword here ? */
10755 if (ridpointers[(int) RID_IN] == TREE_VALUE (spec))
10756 obstack_1grow (&util_obstack, 'n');
10757 else if (ridpointers[(int) RID_INOUT] == TREE_VALUE (spec))
10758 obstack_1grow (&util_obstack, 'N');
10759 else if (ridpointers[(int) RID_OUT] == TREE_VALUE (spec))
10760 obstack_1grow (&util_obstack, 'o');
10761 else if (ridpointers[(int) RID_BYCOPY] == TREE_VALUE (spec))
10762 obstack_1grow (&util_obstack, 'O');
10763 else if (ridpointers[(int) RID_BYREF] == TREE_VALUE (spec))
10764 obstack_1grow (&util_obstack, 'R');
10765 else if (ridpointers[(int) RID_ONEWAY] == TREE_VALUE (spec))
10766 obstack_1grow (&util_obstack, 'V');
10768 gcc_unreachable ();
10772 /* Determine if a pointee is marked read-only. Only used by the NeXT
10773 runtime to be compatible with gcc-3.3. */
10776 pointee_is_readonly (tree pointee)
10778 while (POINTER_TYPE_P (pointee))
10779 pointee = TREE_TYPE (pointee);
10781 return TYPE_READONLY (pointee);
10784 /* Encode a pointer type. */
10787 encode_pointer (tree type, int curtype, int format)
10789 tree pointer_to = TREE_TYPE (type);
10791 if (flag_next_runtime)
10793 /* This code is used to be compatible with gcc-3.3. */
10794 /* For historical/compatibility reasons, the read-only qualifier
10795 of the pointee gets emitted _before_ the '^'. The read-only
10796 qualifier of the pointer itself gets ignored, _unless_ we are
10797 looking at a typedef! Also, do not emit the 'r' for anything
10798 but the outermost type! */
10799 if (!generating_instance_variables
10800 && (obstack_object_size (&util_obstack) - curtype <= 1)
10801 && (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10802 ? TYPE_READONLY (type)
10803 : pointee_is_readonly (pointer_to)))
10804 obstack_1grow (&util_obstack, 'r');
10807 if (TREE_CODE (pointer_to) == RECORD_TYPE)
10809 if (OBJC_TYPE_NAME (pointer_to)
10810 && TREE_CODE (OBJC_TYPE_NAME (pointer_to)) == IDENTIFIER_NODE)
10812 const char *name = IDENTIFIER_POINTER (OBJC_TYPE_NAME (pointer_to));
10814 if (strcmp (name, TAG_OBJECT) == 0) /* '@' */
10816 obstack_1grow (&util_obstack, '@');
10819 else if (TYPE_HAS_OBJC_INFO (pointer_to)
10820 && TYPE_OBJC_INTERFACE (pointer_to))
10822 if (generating_instance_variables)
10824 obstack_1grow (&util_obstack, '@');
10825 obstack_1grow (&util_obstack, '"');
10826 obstack_grow (&util_obstack, name, strlen (name));
10827 obstack_1grow (&util_obstack, '"');
10832 obstack_1grow (&util_obstack, '@');
10836 else if (strcmp (name, TAG_CLASS) == 0) /* '#' */
10838 obstack_1grow (&util_obstack, '#');
10841 else if (strcmp (name, TAG_SELECTOR) == 0) /* ':' */
10843 obstack_1grow (&util_obstack, ':');
10848 else if (TREE_CODE (pointer_to) == INTEGER_TYPE
10849 && TYPE_MODE (pointer_to) == QImode)
10851 tree pname = TREE_CODE (OBJC_TYPE_NAME (pointer_to)) == IDENTIFIER_NODE
10852 ? OBJC_TYPE_NAME (pointer_to)
10853 : DECL_NAME (OBJC_TYPE_NAME (pointer_to));
10855 /* (BOOL *) are an exception and are encoded as ^c, while all
10856 other pointers to char are encoded as *. */
10857 if (strcmp (IDENTIFIER_POINTER (pname), "BOOL"))
10859 if (!flag_next_runtime)
10861 /* The NeXT runtime adds the 'r' before getting here. */
10863 /* It appears that "r*" means "const char *" rather than
10864 "char *const". "char *const" is encoded as "*",
10865 which is identical to "char *", so the "const" is
10866 unfortunately lost. */
10867 if (TYPE_READONLY (pointer_to))
10868 obstack_1grow (&util_obstack, 'r');
10871 obstack_1grow (&util_obstack, '*');
10876 /* We have a normal pointer type that does not get special treatment. */
10877 obstack_1grow (&util_obstack, '^');
10878 encode_type (pointer_to, curtype, format);
10882 encode_array (tree type, int curtype, int format)
10884 tree an_int_cst = TYPE_SIZE (type);
10885 tree array_of = TREE_TYPE (type);
10888 if (an_int_cst == NULL)
10890 /* We are trying to encode an incomplete array. An incomplete
10891 array is forbidden as part of an instance variable. */
10892 if (generating_instance_variables)
10894 /* TODO: Detect this error earlier. */
10895 error ("instance variable has unknown size");
10899 /* So the only case in which an incomplete array could occur is
10900 if we are encoding the arguments or return value of a method.
10901 In that case, an incomplete array argument or return value
10902 (eg, -(void)display: (char[])string) is treated like a
10903 pointer because that is how the compiler does the function
10904 call. A special, more complicated case, is when the
10905 incomplete array is the last member of a struct (eg, if we
10906 are encoding "struct { unsigned long int a;double b[];}"),
10907 which is again part of a method argument/return value. In
10908 that case, we really need to communicate to the runtime that
10909 there is an incomplete array (not a pointer!) there. So, we
10910 detect that special case and encode it as a zero-length
10913 Try to detect that we are part of a struct. We do this by
10914 searching for '=' in the type encoding for the current type.
10915 NB: This hack assumes that you can't use '=' as part of a C
10919 char *enc = obstack_base (&util_obstack) + curtype;
10920 if (memchr (enc, '=',
10921 obstack_object_size (&util_obstack) - curtype) == NULL)
10923 /* We are not inside a struct. Encode the array as a
10925 encode_pointer (type, curtype, format);
10930 /* Else, we are in a struct, and we encode it as a zero-length
10932 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT)0);
10934 else if (TREE_INT_CST_LOW (TYPE_SIZE (array_of)) == 0)
10935 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT)0);
10937 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC,
10938 TREE_INT_CST_LOW (an_int_cst)
10939 / TREE_INT_CST_LOW (TYPE_SIZE (array_of)));
10941 obstack_grow (&util_obstack, buffer, strlen (buffer));
10942 encode_type (array_of, curtype, format);
10943 obstack_1grow (&util_obstack, ']');
10947 /* Encode a vector. The vector type is a GCC extension to C. */
10949 encode_vector (tree type, int curtype, int format)
10951 tree vector_of = TREE_TYPE (type);
10954 /* Vectors are like simple fixed-size arrays. */
10956 /* Output ![xx,yy,<code>] where xx is the vector_size, yy is the
10957 alignment of the vector, and <code> is the base type. Eg, int
10958 __attribute__ ((vector_size (16))) gets encoded as ![16,32,i]
10959 assuming that the alignment is 32 bytes. We include size and
10960 alignment in bytes so that the runtime does not have to have any
10961 knowledge of the actual types.
10963 sprintf (buffer, "![" HOST_WIDE_INT_PRINT_DEC ",%d",
10964 /* We want to compute the equivalent of sizeof (<vector>).
10965 Code inspired by c_sizeof_or_alignof_type. */
10966 ((TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type))
10967 / (TYPE_PRECISION (char_type_node) / BITS_PER_UNIT))),
10968 /* We want to compute the equivalent of __alignof__
10969 (<vector>). Code inspired by
10970 c_sizeof_or_alignof_type. */
10971 TYPE_ALIGN_UNIT (type));
10972 obstack_grow (&util_obstack, buffer, strlen (buffer));
10973 encode_type (vector_of, curtype, format);
10974 obstack_1grow (&util_obstack, ']');
10979 encode_aggregate_fields (tree type, bool pointed_to, int curtype, int format)
10981 tree field = TYPE_FIELDS (type);
10983 for (; field; field = DECL_CHAIN (field))
10986 /* C++ static members, and things that are not field at all,
10987 should not appear in the encoding. */
10988 if (TREE_CODE (field) != FIELD_DECL || TREE_STATIC (field))
10992 /* Recursively encode fields of embedded base classes. */
10993 if (DECL_ARTIFICIAL (field) && !DECL_NAME (field)
10994 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
10996 encode_aggregate_fields (TREE_TYPE (field),
10997 pointed_to, curtype, format);
11001 if (generating_instance_variables && !pointed_to)
11003 tree fname = DECL_NAME (field);
11005 obstack_1grow (&util_obstack, '"');
11007 if (fname && TREE_CODE (fname) == IDENTIFIER_NODE)
11008 obstack_grow (&util_obstack,
11009 IDENTIFIER_POINTER (fname),
11010 strlen (IDENTIFIER_POINTER (fname)));
11012 obstack_1grow (&util_obstack, '"');
11015 encode_field_decl (field, curtype, format);
11020 encode_aggregate_within (tree type, int curtype, int format, int left,
11024 /* NB: aggregates that are pointed to have slightly different encoding
11025 rules in that you never encode the names of instance variables. */
11026 int ob_size = obstack_object_size (&util_obstack);
11027 bool inline_contents = false;
11028 bool pointed_to = false;
11030 if (flag_next_runtime)
11032 if (ob_size > 0 && *(obstack_next_free (&util_obstack) - 1) == '^')
11035 if ((format == OBJC_ENCODE_INLINE_DEFS || generating_instance_variables)
11036 && (!pointed_to || ob_size - curtype == 1
11037 || (ob_size - curtype == 2
11038 && *(obstack_next_free (&util_obstack) - 2) == 'r')))
11039 inline_contents = true;
11043 /* c0 and c1 are the last two characters in the encoding of the
11044 current type; if the last two characters were '^' or '^r',
11045 then we are encoding an aggregate that is "pointed to". The
11046 comment above applies: in that case we should avoid encoding
11047 the names of instance variables.
11049 char c1 = ob_size > 1 ? *(obstack_next_free (&util_obstack) - 2) : 0;
11050 char c0 = ob_size > 0 ? *(obstack_next_free (&util_obstack) - 1) : 0;
11052 if (c0 == '^' || (c1 == '^' && c0 == 'r'))
11055 if (format == OBJC_ENCODE_INLINE_DEFS || generating_instance_variables)
11058 inline_contents = true;
11061 /* Note that the check (ob_size - curtype < 2) prevents
11062 infinite recursion when encoding a structure which is
11063 a linked list (eg, struct node { struct node *next;
11064 }). Each time we follow a pointer, we add one
11065 character to ob_size, and curtype is fixed, so after
11066 at most two pointers we stop inlining contents and
11069 The other case where we don't inline is "^r", which
11070 is a pointer to a constant struct.
11072 if ((ob_size - curtype <= 2) && !(c0 == 'r'))
11073 inline_contents = true;
11078 /* Traverse struct aliases; it is important to get the
11079 original struct and its tag name (if any). */
11080 type = TYPE_MAIN_VARIANT (type);
11081 name = OBJC_TYPE_NAME (type);
11082 /* Open parenth/bracket. */
11083 obstack_1grow (&util_obstack, left);
11085 /* Encode the struct/union tag name, or '?' if a tag was
11086 not provided. Typedef aliases do not qualify. */
11088 /* For compatibility with the NeXT runtime, ObjC++ encodes template
11089 args as a composite struct tag name. */
11090 if (name && TREE_CODE (name) == IDENTIFIER_NODE
11091 /* Did this struct have a tag? */
11092 && !TYPE_WAS_ANONYMOUS (type))
11093 obstack_grow (&util_obstack,
11094 decl_as_string (type, TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME),
11095 strlen (decl_as_string (type, TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME)));
11097 if (name && TREE_CODE (name) == IDENTIFIER_NODE)
11098 obstack_grow (&util_obstack,
11099 IDENTIFIER_POINTER (name),
11100 strlen (IDENTIFIER_POINTER (name)));
11103 obstack_1grow (&util_obstack, '?');
11105 /* Encode the types (and possibly names) of the inner fields,
11107 if (inline_contents)
11109 obstack_1grow (&util_obstack, '=');
11110 encode_aggregate_fields (type, pointed_to, curtype, format);
11112 /* Close parenth/bracket. */
11113 obstack_1grow (&util_obstack, right);
11116 /* Encode a bitfield NeXT-style (i.e., without a bit offset or the underlying
11120 encode_next_bitfield (int width)
11123 sprintf (buffer, "b%d", width);
11124 obstack_grow (&util_obstack, buffer, strlen (buffer));
11128 /* Encodes 'type', ignoring type qualifiers (which you should encode
11129 beforehand if needed) with the exception of 'const', which is
11130 encoded by encode_type. See above for the explanation of
11131 'curtype'. 'format' can be OBJC_ENCODE_INLINE_DEFS or
11132 OBJC_ENCODE_DONT_INLINE_DEFS.
11135 encode_type (tree type, int curtype, int format)
11137 enum tree_code code = TREE_CODE (type);
11139 /* Ignore type qualifiers other than 'const' when encoding a
11142 if (type == error_mark_node)
11145 if (!flag_next_runtime)
11147 if (TYPE_READONLY (type))
11148 obstack_1grow (&util_obstack, 'r');
11153 case ENUMERAL_TYPE:
11154 if (flag_next_runtime)
11156 /* Kludge for backwards-compatibility with gcc-3.3: enums
11157 are always encoded as 'i' no matter what type they
11158 actually are (!). */
11159 obstack_1grow (&util_obstack, 'i');
11162 /* Else, they are encoded exactly like the integer type that is
11163 used by the compiler to store them. */
11167 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
11169 case 8: c = TYPE_UNSIGNED (type) ? 'C' : 'c'; break;
11170 case 16: c = TYPE_UNSIGNED (type) ? 'S' : 's'; break;
11173 tree int_type = type;
11174 if (flag_next_runtime)
11176 /* Another legacy kludge for compatiblity with
11177 gcc-3.3: 32-bit longs are encoded as 'l' or 'L',
11178 but not always. For typedefs, we need to use 'i'
11179 or 'I' instead if encoding a struct field, or a
11181 int_type = ((!generating_instance_variables
11182 && (obstack_object_size (&util_obstack)
11183 == (unsigned) curtype))
11184 ? TYPE_MAIN_VARIANT (type)
11187 if (int_type == long_unsigned_type_node
11188 || int_type == long_integer_type_node)
11189 c = TYPE_UNSIGNED (type) ? 'L' : 'l';
11191 c = TYPE_UNSIGNED (type) ? 'I' : 'i';
11194 case 64: c = TYPE_UNSIGNED (type) ? 'Q' : 'q'; break;
11195 case 128: c = TYPE_UNSIGNED (type) ? 'T' : 't'; break;
11196 default: gcc_unreachable ();
11198 obstack_1grow (&util_obstack, c);
11204 /* Floating point types. */
11205 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
11207 case 32: c = 'f'; break;
11208 case 64: c = 'd'; break;
11210 case 128: c = 'D'; break;
11211 default: gcc_unreachable ();
11213 obstack_1grow (&util_obstack, c);
11217 obstack_1grow (&util_obstack, 'v');
11221 obstack_1grow (&util_obstack, 'B');
11225 encode_array (type, curtype, format);
11230 case REFERENCE_TYPE:
11232 encode_pointer (type, curtype, format);
11236 encode_aggregate_within (type, curtype, format, '{', '}');
11240 encode_aggregate_within (type, curtype, format, '(', ')');
11243 case FUNCTION_TYPE: /* '?' means an unknown type. */
11244 obstack_1grow (&util_obstack, '?');
11248 /* A complex is encoded as 'j' followed by the inner type (eg,
11249 "_Complex int" is encoded as 'ji'). */
11250 obstack_1grow (&util_obstack, 'j');
11251 encode_type (TREE_TYPE (type), curtype, format);
11255 encode_vector (type, curtype, format);
11259 warning (0, "unknown type %s found during Objective-C encoding",
11260 gen_type_name (type));
11261 obstack_1grow (&util_obstack, '?');
11265 if (flag_next_runtime)
11267 /* Super-kludge. Some ObjC qualifier and type combinations need
11268 to be rearranged for compatibility with gcc-3.3. */
11269 if (code == POINTER_TYPE && obstack_object_size (&util_obstack) >= 3)
11271 char *enc = obstack_base (&util_obstack) + curtype;
11273 /* Rewrite "in const" from "nr" to "rn". */
11274 if (curtype >= 1 && !strncmp (enc - 1, "nr", 2))
11275 strncpy (enc - 1, "rn", 2);
11281 encode_gnu_bitfield (int position, tree type, int size)
11283 enum tree_code code = TREE_CODE (type);
11285 char charType = '?';
11287 /* This code is only executed for the GNU runtime, so we can ignore
11288 the NeXT runtime kludge of always encoding enums as 'i' no matter
11289 what integers they actually are. */
11290 if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
11292 if (integer_zerop (TYPE_MIN_VALUE (type)))
11293 /* Unsigned integer types. */
11295 switch (TYPE_MODE (type))
11298 charType = 'C'; break;
11300 charType = 'S'; break;
11303 if (type == long_unsigned_type_node)
11310 charType = 'Q'; break;
11312 gcc_unreachable ();
11316 /* Signed integer types. */
11318 switch (TYPE_MODE (type))
11321 charType = 'c'; break;
11323 charType = 's'; break;
11326 if (type == long_integer_type_node)
11333 charType = 'q'; break;
11335 gcc_unreachable ();
11341 /* Do not do any encoding, produce an error and keep going. */
11342 error ("trying to encode non-integer type as a bitfield");
11346 sprintf (buffer, "b%d%c%d", position, charType, size);
11347 obstack_grow (&util_obstack, buffer, strlen (buffer));
11351 encode_field_decl (tree field_decl, int curtype, int format)
11354 /* C++ static members, and things that are not fields at all,
11355 should not appear in the encoding. */
11356 if (TREE_CODE (field_decl) != FIELD_DECL || TREE_STATIC (field_decl))
11360 /* Generate the bitfield typing information, if needed. Note the difference
11361 between GNU and NeXT runtimes. */
11362 if (DECL_BIT_FIELD_TYPE (field_decl))
11364 int size = tree_low_cst (DECL_SIZE (field_decl), 1);
11366 if (flag_next_runtime)
11367 encode_next_bitfield (size);
11369 encode_gnu_bitfield (int_bit_position (field_decl),
11370 DECL_BIT_FIELD_TYPE (field_decl), size);
11373 encode_type (TREE_TYPE (field_decl), curtype, format);
11376 /* Decay array and function parameters into pointers. */
11379 objc_decay_parm_type (tree type)
11381 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE)
11382 type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE
11389 static GTY(()) tree objc_parmlist = NULL_TREE;
11391 /* Append PARM to a list of formal parameters of a method, making a necessary
11392 array-to-pointer adjustment along the way. */
11395 objc_push_parm (tree parm)
11399 if (TREE_TYPE (parm) == error_mark_node)
11401 objc_parmlist = chainon (objc_parmlist, parm);
11405 /* Decay arrays and functions into pointers. */
11406 type = objc_decay_parm_type (TREE_TYPE (parm));
11408 /* If the parameter type has been decayed, a new PARM_DECL needs to be
11410 if (type != TREE_TYPE (parm))
11411 parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type);
11413 DECL_ARG_TYPE (parm)
11414 = lang_hooks.types.type_promotes_to (TREE_TYPE (parm));
11416 /* Record constancy and volatility. */
11417 c_apply_type_quals_to_decl
11418 ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0)
11419 | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0)
11420 | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm);
11422 objc_parmlist = chainon (objc_parmlist, parm);
11425 /* Retrieve the formal parameter list constructed via preceding calls to
11426 objc_push_parm(). */
11430 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED)
11432 static struct c_arg_info *
11433 objc_get_parm_info (int have_ellipsis)
11437 tree parm_info = objc_parmlist;
11438 objc_parmlist = NULL_TREE;
11442 tree parm_info = objc_parmlist;
11443 struct c_arg_info *arg_info;
11444 /* The C front-end requires an elaborate song and dance at
11447 declare_parm_level ();
11450 tree next = DECL_CHAIN (parm_info);
11452 DECL_CHAIN (parm_info) = NULL_TREE;
11453 parm_info = pushdecl (parm_info);
11454 finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
11457 arg_info = get_parm_info (have_ellipsis);
11459 objc_parmlist = NULL_TREE;
11464 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
11465 method definitions. In the case of instance methods, we can be more
11466 specific as to the type of 'self'. */
11469 synth_self_and_ucmd_args (void)
11473 if (objc_method_context
11474 && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL)
11475 self_type = objc_instance_type;
11477 /* Really a `struct objc_class *'. However, we allow people to
11478 assign to self, which changes its type midstream. */
11479 self_type = objc_object_type;
11482 objc_push_parm (build_decl (input_location,
11483 PARM_DECL, self_id, self_type));
11486 objc_push_parm (build_decl (input_location,
11487 PARM_DECL, ucmd_id, objc_selector_type));
11490 /* Transform an Objective-C method definition into a static C function
11491 definition, synthesizing the first two arguments, "self" and "_cmd",
11495 start_method_def (tree method)
11501 struct c_arg_info *parm_info;
11503 int have_ellipsis = 0;
11505 /* If we are defining a "dealloc" method in a non-root class, we
11506 will need to check if a [super dealloc] is missing, and warn if
11508 if(CLASS_SUPER_NAME (objc_implementation_context)
11509 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method))))
11510 should_call_super_dealloc = 1;
11512 should_call_super_dealloc = 0;
11514 /* Required to implement _msgSuper. */
11515 objc_method_context = method;
11516 UOBJC_SUPER_decl = NULL_TREE;
11518 /* Generate prototype declarations for arguments..."new-style". */
11519 synth_self_and_ucmd_args ();
11521 /* Generate argument declarations if a keyword_decl. */
11522 parmlist = METHOD_SEL_ARGS (method);
11525 /* parmlist is a KEYWORD_DECL. */
11526 tree type = TREE_VALUE (TREE_TYPE (parmlist));
11529 parm = build_decl (input_location,
11530 PARM_DECL, KEYWORD_ARG_NAME (parmlist), type);
11531 decl_attributes (&parm, DECL_ATTRIBUTES (parmlist), 0);
11532 objc_push_parm (parm);
11533 parmlist = DECL_CHAIN (parmlist);
11536 if (METHOD_ADD_ARGS (method))
11540 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method));
11541 akey; akey = TREE_CHAIN (akey))
11543 objc_push_parm (TREE_VALUE (akey));
11546 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
11550 parm_info = objc_get_parm_info (have_ellipsis);
11552 really_start_method (objc_method_context, parm_info);
11555 /* Return 1 if TYPE1 is equivalent to TYPE2
11556 for purposes of method overloading. */
11559 objc_types_are_equivalent (tree type1, tree type2)
11561 if (type1 == type2)
11564 /* Strip away indirections. */
11565 while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE)
11566 && (TREE_CODE (type1) == TREE_CODE (type2)))
11567 type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2);
11568 if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
11571 type1 = (TYPE_HAS_OBJC_INFO (type1)
11572 ? TYPE_OBJC_PROTOCOL_LIST (type1)
11574 type2 = (TYPE_HAS_OBJC_INFO (type2)
11575 ? TYPE_OBJC_PROTOCOL_LIST (type2)
11578 if (list_length (type1) == list_length (type2))
11580 for (; type2; type2 = TREE_CHAIN (type2))
11581 if (!lookup_protocol_in_reflist (type1, TREE_VALUE (type2)))
11588 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
11591 objc_types_share_size_and_alignment (tree type1, tree type2)
11593 return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
11594 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2));
11597 /* Return 1 if PROTO1 is equivalent to PROTO2
11598 for purposes of method overloading. Ordinarily, the type signatures
11599 should match up exactly, unless STRICT is zero, in which case we
11600 shall allow differences in which the size and alignment of a type
11604 comp_proto_with_proto (tree proto1, tree proto2, int strict)
11606 /* The following test is needed in case there are hashing
11608 if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2))
11611 return match_proto_with_proto (proto1, proto2, strict);
11615 match_proto_with_proto (tree proto1, tree proto2, int strict)
11619 /* Compare return types. */
11620 type1 = TREE_VALUE (TREE_TYPE (proto1));
11621 type2 = TREE_VALUE (TREE_TYPE (proto2));
11623 if (!objc_types_are_equivalent (type1, type2)
11624 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
11627 /* Compare argument types. */
11628 for (type1 = get_arg_type_list (proto1, METHOD_REF, 0),
11629 type2 = get_arg_type_list (proto2, METHOD_REF, 0);
11631 type1 = TREE_CHAIN (type1), type2 = TREE_CHAIN (type2))
11633 if (!objc_types_are_equivalent (TREE_VALUE (type1), TREE_VALUE (type2))
11635 || !objc_types_share_size_and_alignment (TREE_VALUE (type1),
11636 TREE_VALUE (type2))))
11640 return (!type1 && !type2);
11643 /* Fold an OBJ_TYPE_REF expression for ObjC method dispatches, where
11644 this occurs. ObjC method dispatches are _not_ like C++ virtual
11645 member function dispatches, and we account for the difference here. */
11648 objc_fold_obj_type_ref (tree ref, tree known_type)
11650 objc_fold_obj_type_ref (tree ref ATTRIBUTE_UNUSED,
11651 tree known_type ATTRIBUTE_UNUSED)
11655 tree v = BINFO_VIRTUALS (TYPE_BINFO (known_type));
11657 /* If the receiver does not have virtual member functions, there
11658 is nothing we can (or need to) do here. */
11662 /* Let C++ handle C++ virtual functions. */
11663 return cp_fold_obj_type_ref (ref, known_type);
11665 /* For plain ObjC, we currently do not need to do anything. */
11671 objc_start_function (tree name, tree type, tree attrs,
11675 struct c_arg_info *params
11679 tree fndecl = build_decl (input_location,
11680 FUNCTION_DECL, name, type);
11683 DECL_ARGUMENTS (fndecl) = params;
11684 DECL_INITIAL (fndecl) = error_mark_node;
11685 DECL_EXTERNAL (fndecl) = 0;
11686 TREE_STATIC (fndecl) = 1;
11687 retrofit_lang_decl (fndecl);
11688 cplus_decl_attributes (&fndecl, attrs, 0);
11689 start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT);
11691 current_function_returns_value = 0; /* Assume, until we see it does. */
11692 current_function_returns_null = 0;
11693 decl_attributes (&fndecl, attrs, 0);
11694 announce_function (fndecl);
11695 DECL_INITIAL (fndecl) = error_mark_node;
11696 DECL_EXTERNAL (fndecl) = 0;
11697 TREE_STATIC (fndecl) = 1;
11698 current_function_decl = pushdecl (fndecl);
11700 declare_parm_level ();
11701 DECL_RESULT (current_function_decl)
11702 = build_decl (input_location,
11703 RESULT_DECL, NULL_TREE,
11704 TREE_TYPE (TREE_TYPE (current_function_decl)));
11705 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1;
11706 DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1;
11707 start_fname_decls ();
11708 store_parm_decls_from (params);
11711 TREE_USED (current_function_decl) = 1;
11714 /* - Generate an identifier for the function. the format is "_n_cls",
11715 where 1 <= n <= nMethods, and cls is the name the implementation we
11717 - Install the return type from the method declaration.
11718 - If we have a prototype, check for type consistency. */
11721 really_start_method (tree method,
11725 struct c_arg_info *parmlist
11729 tree ret_type, meth_type;
11731 const char *sel_name, *class_name, *cat_name;
11734 /* Synth the storage class & assemble the return type. */
11735 ret_type = TREE_VALUE (TREE_TYPE (method));
11737 sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
11738 class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
11739 cat_name = ((TREE_CODE (objc_implementation_context)
11740 == CLASS_IMPLEMENTATION_TYPE)
11742 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context)));
11745 /* Make sure this is big enough for any plausible method label. */
11746 buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
11747 + (cat_name ? strlen (cat_name) : 0));
11749 OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
11750 class_name, cat_name, sel_name, method_slot);
11752 method_id = get_identifier (buf);
11755 /* Objective-C methods cannot be overloaded, so we don't need
11756 the type encoding appended. It looks bad anyway... */
11757 push_lang_context (lang_name_c);
11761 = build_function_type (ret_type,
11762 get_arg_type_list (method, METHOD_DEF, 0));
11763 objc_start_function (method_id, meth_type, NULL_TREE, parmlist);
11765 /* Set self_decl from the first argument. */
11766 self_decl = DECL_ARGUMENTS (current_function_decl);
11768 /* Suppress unused warnings. */
11769 TREE_USED (self_decl) = 1;
11770 DECL_READ_P (self_decl) = 1;
11771 TREE_USED (DECL_CHAIN (self_decl)) = 1;
11772 DECL_READ_P (DECL_CHAIN (self_decl)) = 1;
11774 pop_lang_context ();
11777 METHOD_DEFINITION (method) = current_function_decl;
11779 /* Check consistency...start_function, pushdecl, duplicate_decls. */
11781 if (implementation_template != objc_implementation_context)
11784 = lookup_method_static (implementation_template,
11785 METHOD_SEL_NAME (method),
11786 ((TREE_CODE (method) == CLASS_METHOD_DECL)
11787 | OBJC_LOOKUP_NO_SUPER));
11791 if (!comp_proto_with_proto (method, proto, 1))
11793 bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL;
11795 warning_at (DECL_SOURCE_LOCATION (method), 0,
11796 "conflicting types for %<%c%s%>",
11797 (type ? '-' : '+'),
11798 identifier_to_locale (gen_method_decl (method)));
11799 inform (DECL_SOURCE_LOCATION (proto),
11800 "previous declaration of %<%c%s%>",
11801 (type ? '-' : '+'),
11802 identifier_to_locale (gen_method_decl (proto)));
11806 /* If the method in the @interface was deprecated, mark
11807 the implemented method as deprecated too. It should
11808 never be used for messaging (when the deprecation
11809 warnings are produced), but just in case. */
11810 if (TREE_DEPRECATED (proto))
11811 TREE_DEPRECATED (method) = 1;
11813 /* If the method in the @interface was marked as
11814 'noreturn', mark the function implementing the method
11815 as 'noreturn' too. */
11816 TREE_THIS_VOLATILE (current_function_decl) = TREE_THIS_VOLATILE (proto);
11821 /* We have a method @implementation even though we did not
11822 see a corresponding @interface declaration (which is allowed
11823 by Objective-C rules). Go ahead and place the method in
11824 the @interface anyway, so that message dispatch lookups
11826 tree interface = implementation_template;
11828 if (TREE_CODE (objc_implementation_context)
11829 == CATEGORY_IMPLEMENTATION_TYPE)
11830 interface = lookup_category
11832 CLASS_SUPER_NAME (objc_implementation_context));
11835 objc_add_method (interface, copy_node (method),
11836 TREE_CODE (method) == CLASS_METHOD_DECL,
11837 /* is_optional= */ false);
11842 static void *UOBJC_SUPER_scope = 0;
11844 /* _n_Method (id self, SEL sel, ...)
11846 struct objc_super _S;
11847 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
11851 get_super_receiver (void)
11853 if (objc_method_context)
11855 tree super_expr, super_expr_list;
11857 if (!UOBJC_SUPER_decl)
11859 UOBJC_SUPER_decl = build_decl (input_location,
11860 VAR_DECL, get_identifier (TAG_SUPER),
11861 objc_super_template);
11862 /* This prevents `unused variable' warnings when compiling with -Wall. */
11863 TREE_USED (UOBJC_SUPER_decl) = 1;
11864 DECL_READ_P (UOBJC_SUPER_decl) = 1;
11865 lang_hooks.decls.pushdecl (UOBJC_SUPER_decl);
11866 finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE,
11868 UOBJC_SUPER_scope = objc_get_current_scope ();
11871 /* Set receiver to self. */
11872 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id);
11873 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
11874 NOP_EXPR, input_location, self_decl,
11876 super_expr_list = super_expr;
11878 /* Set class to begin searching. */
11879 super_expr = objc_build_component_ref (UOBJC_SUPER_decl,
11880 get_identifier ("super_class"));
11882 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
11884 /* [_cls, __cls]Super are "pre-built" in
11885 synth_forward_declarations. */
11887 super_expr = build_modify_expr (input_location, super_expr,
11888 NULL_TREE, NOP_EXPR,
11890 ((TREE_CODE (objc_method_context)
11891 == INSTANCE_METHOD_DECL)
11893 : uucls_super_ref),
11898 /* We have a category. */
11900 tree super_name = CLASS_SUPER_NAME (implementation_template);
11903 /* Barf if super used in a category of Object. */
11906 error ("no super class declared in interface for %qE",
11907 CLASS_NAME (implementation_template));
11908 return error_mark_node;
11911 if (flag_next_runtime && !flag_zero_link)
11913 super_class = objc_get_class_reference (super_name);
11914 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
11915 /* If we are in a class method, we must retrieve the
11916 _metaclass_ for the current class, pointed at by
11917 the class's "isa" pointer. The following assumes that
11918 "isa" is the first ivar in a class (which it must be). */
11920 = build_indirect_ref
11922 build_c_cast (input_location,
11923 build_pointer_type (objc_class_type),
11924 super_class), RO_UNARY_STAR);
11928 add_class_reference (super_name);
11929 super_class = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
11930 ? objc_get_class_decl : objc_get_meta_class_decl);
11931 assemble_external (super_class);
11933 = build_function_call
11938 my_build_string_pointer
11939 (IDENTIFIER_LENGTH (super_name) + 1,
11940 IDENTIFIER_POINTER (super_name))));
11944 = build_modify_expr (input_location, super_expr, NULL_TREE,
11947 build_c_cast (input_location,
11948 TREE_TYPE (super_expr),
11953 super_expr_list = build_compound_expr (input_location,
11954 super_expr_list, super_expr);
11956 super_expr = build_unary_op (input_location,
11957 ADDR_EXPR, UOBJC_SUPER_decl, 0);
11958 super_expr_list = build_compound_expr (input_location,
11959 super_expr_list, super_expr);
11961 return super_expr_list;
11965 error ("[super ...] must appear in a method context");
11966 return error_mark_node;
11970 /* When exiting a scope, sever links to a 'super' declaration (if any)
11971 therein contained. */
11974 objc_clear_super_receiver (void)
11976 if (objc_method_context
11977 && UOBJC_SUPER_scope == objc_get_current_scope ()) {
11978 UOBJC_SUPER_decl = 0;
11979 UOBJC_SUPER_scope = 0;
11984 objc_finish_method_definition (tree fndecl)
11986 /* We cannot validly inline ObjC methods, at least not without a language
11987 extension to declare that a method need not be dynamically
11988 dispatched, so suppress all thoughts of doing so. */
11989 DECL_UNINLINABLE (fndecl) = 1;
11992 /* The C++ front-end will have called finish_function() for us. */
11993 finish_function ();
11996 METHOD_ENCODING (objc_method_context)
11997 = encode_method_prototype (objc_method_context);
11999 /* Required to implement _msgSuper. This must be done AFTER finish_function,
12000 since the optimizer may find "may be used before set" errors. */
12001 objc_method_context = NULL_TREE;
12003 if (should_call_super_dealloc)
12004 warning (0, "method possibly missing a [super dealloc] call");
12007 /* Given a tree DECL node, produce a printable description of it in the given
12008 buffer, overwriting the buffer. */
12011 gen_declaration (tree decl)
12017 gen_type_name_0 (TREE_TYPE (decl));
12019 if (DECL_NAME (decl))
12021 if (!POINTER_TYPE_P (TREE_TYPE (decl)))
12022 strcat (errbuf, " ");
12024 strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
12027 if (DECL_INITIAL (decl)
12028 && TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST)
12029 sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
12030 TREE_INT_CST_LOW (DECL_INITIAL (decl)));
12036 /* Given a tree TYPE node, produce a printable description of it in the given
12037 buffer, overwriting the buffer. */
12040 gen_type_name_0 (tree type)
12042 tree orig = type, proto;
12044 if (TYPE_P (type) && TYPE_NAME (type))
12045 type = TYPE_NAME (type);
12046 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
12048 tree inner = TREE_TYPE (type);
12050 while (TREE_CODE (inner) == ARRAY_TYPE)
12051 inner = TREE_TYPE (inner);
12053 gen_type_name_0 (inner);
12055 if (!POINTER_TYPE_P (inner))
12056 strcat (errbuf, " ");
12058 if (POINTER_TYPE_P (type))
12059 strcat (errbuf, "*");
12061 while (type != inner)
12063 strcat (errbuf, "[");
12065 if (TYPE_DOMAIN (type))
12069 sprintf (sz, HOST_WIDE_INT_PRINT_DEC,
12071 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1));
12072 strcat (errbuf, sz);
12075 strcat (errbuf, "]");
12076 type = TREE_TYPE (type);
12079 goto exit_function;
12082 if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type))
12083 type = DECL_NAME (type);
12085 strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE
12086 ? IDENTIFIER_POINTER (type)
12089 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
12090 if (objc_is_id (orig))
12091 orig = TREE_TYPE (orig);
12093 proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE;
12097 strcat (errbuf, " <");
12101 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto))));
12102 proto = TREE_CHAIN (proto);
12103 strcat (errbuf, proto ? ", " : ">");
12112 gen_type_name (tree type)
12116 return gen_type_name_0 (type);
12119 /* Given a method tree, put a printable description into the given
12120 buffer (overwriting) and return a pointer to the buffer. */
12123 gen_method_decl (tree method)
12127 strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */
12128 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method)));
12129 strcat (errbuf, ")");
12130 chain = METHOD_SEL_ARGS (method);
12134 /* We have a chain of keyword_decls. */
12137 if (KEYWORD_KEY_NAME (chain))
12138 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
12140 strcat (errbuf, ":(");
12141 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain)));
12142 strcat (errbuf, ")");
12144 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
12145 if ((chain = DECL_CHAIN (chain)))
12146 strcat (errbuf, " ");
12150 if (METHOD_ADD_ARGS (method))
12152 chain = TREE_CHAIN (METHOD_ADD_ARGS (method));
12154 /* Know we have a chain of parm_decls. */
12157 strcat (errbuf, ", ");
12158 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain)));
12159 chain = TREE_CHAIN (chain);
12162 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
12163 strcat (errbuf, ", ...");
12168 /* We have a unary selector. */
12169 strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
12177 /* Dump an @interface declaration of the supplied class CHAIN to the
12178 supplied file FP. Used to implement the -gen-decls option (which
12179 prints out an @interface declaration of all classes compiled in
12180 this run); potentially useful for debugging the compiler too. */
12182 dump_interface (FILE *fp, tree chain)
12184 /* FIXME: A heap overflow here whenever a method (or ivar)
12185 declaration is so long that it doesn't fit in the buffer. The
12186 code and all the related functions should be rewritten to avoid
12187 using fixed size buffers. */
12188 const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
12189 tree ivar_decls = CLASS_RAW_IVARS (chain);
12190 tree nst_methods = CLASS_NST_METHODS (chain);
12191 tree cls_methods = CLASS_CLS_METHODS (chain);
12193 fprintf (fp, "\n@interface %s", my_name);
12195 /* CLASS_SUPER_NAME is used to store the superclass name for
12196 classes, and the category name for categories. */
12197 if (CLASS_SUPER_NAME (chain))
12199 const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
12201 switch (TREE_CODE (chain))
12203 case CATEGORY_IMPLEMENTATION_TYPE:
12204 case CATEGORY_INTERFACE_TYPE:
12205 fprintf (fp, " (%s)\n", name);
12208 fprintf (fp, " : %s\n", name);
12213 fprintf (fp, "\n");
12215 /* FIXME - the following doesn't seem to work at the moment. */
12218 fprintf (fp, "{\n");
12221 fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls));
12222 ivar_decls = TREE_CHAIN (ivar_decls);
12224 while (ivar_decls);
12225 fprintf (fp, "}\n");
12228 while (nst_methods)
12230 fprintf (fp, "- %s;\n", gen_method_decl (nst_methods));
12231 nst_methods = TREE_CHAIN (nst_methods);
12234 while (cls_methods)
12236 fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods));
12237 cls_methods = TREE_CHAIN (cls_methods);
12240 fprintf (fp, "@end\n");
12244 /* Produce the pretty printing for an Objective-C method. This is
12245 currently unused, but could be handy while reorganizing the pretty
12246 printing to be more robust. */
12247 static const char *
12248 objc_pretty_print_method (bool is_class_method,
12249 const char *class_name,
12250 const char *category_name,
12251 const char *selector)
12255 char *result = XNEWVEC (char, strlen (class_name) + strlen (category_name)
12256 + strlen (selector) + 7);
12258 if (is_class_method)
12259 sprintf (result, "+[%s(%s) %s]", class_name, category_name, selector);
12261 sprintf (result, "-[%s(%s) %s]", class_name, category_name, selector);
12267 char *result = XNEWVEC (char, strlen (class_name)
12268 + strlen (selector) + 5);
12270 if (is_class_method)
12271 sprintf (result, "+[%s %s]", class_name, selector);
12273 sprintf (result, "-[%s %s]", class_name, selector);
12280 /* Demangle function for Objective-C. Attempt to demangle the
12281 function name associated with a method (eg, going from
12282 "_i_NSObject__class" to "-[NSObject class]"); usually for the
12283 purpose of pretty printing or error messages. Return the demangled
12284 name, or NULL if the string is not an Objective-C mangled method
12287 Because of how the mangling is done, any method that has a '_' in
12288 its original name is at risk of being demangled incorrectly. In
12289 some cases there are multiple valid ways to demangle a method name
12290 and there is no way we can decide.
12292 TODO: objc_demangle() can't always get it right; the right way to
12293 get this correct for all method names would be to store the
12294 Objective-C method name somewhere in the function decl. Then,
12295 there is no demangling to do; we'd just pull the method name out of
12296 the decl. As an additional bonus, when printing error messages we
12297 could check for such a method name, and if we find it, we know the
12298 function is actually an Objective-C method and we could print error
12299 messages saying "In method '+[NSObject class]" instead of "In
12300 function '+[NSObject class]" as we do now. */
12301 static const char *
12302 objc_demangle (const char *mangled)
12304 char *demangled, *cp;
12306 if (mangled[0] == '_' &&
12307 (mangled[1] == 'i' || mangled[1] == 'c') &&
12310 cp = demangled = XNEWVEC (char, strlen(mangled) + 2);
12311 if (mangled[1] == 'i')
12312 *cp++ = '-'; /* for instance method */
12314 *cp++ = '+'; /* for class method */
12315 *cp++ = '['; /* opening left brace */
12316 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
12317 while (*cp && *cp == '_')
12318 cp++; /* skip any initial underbars in class name */
12319 cp = strchr(cp, '_'); /* find first non-initial underbar */
12322 free(demangled); /* not mangled name */
12325 if (cp[1] == '_') /* easy case: no category name */
12327 *cp++ = ' '; /* replace two '_' with one ' ' */
12328 strcpy(cp, mangled + (cp - demangled) + 2);
12332 *cp++ = '('; /* less easy case: category name */
12333 cp = strchr(cp, '_');
12336 free(demangled); /* not mangled name */
12340 *cp++ = ' '; /* overwriting 1st char of method name... */
12341 strcpy(cp, mangled + (cp - demangled)); /* get it back */
12343 /* Now we have the method name. We need to generally replace
12344 '_' with ':' but trying to preserve '_' if it could only have
12345 been in the mangled string because it was already in the
12346 original name. In cases where it's ambiguous, we assume that
12347 any '_' originated from a ':'. */
12349 /* Initial '_'s in method name can't have been generating by
12350 converting ':'s. Skip them. */
12351 while (*cp && *cp == '_')
12354 /* If the method name does not end with '_', then it has no
12355 arguments and there was no replacement of ':'s with '_'s
12356 during mangling. Check for that case, and skip any
12357 replacement if so. This at least guarantees that methods
12358 with no arguments are always demangled correctly (unless the
12359 original name ends with '_'). */
12360 if (*(mangled + strlen (mangled) - 1) != '_')
12362 /* Skip to the end. */
12368 /* Replace remaining '_' with ':'. This may get it wrong if
12369 there were '_'s in the original name. In most cases it
12370 is impossible to disambiguate. */
12375 *cp++ = ']'; /* closing right brace */
12376 *cp++ = 0; /* string terminator */
12380 return NULL; /* not an objc mangled name */
12383 /* Try to pretty-print a decl. If the 'decl' is an Objective-C
12384 specific decl, return the printable name for it. If not, return
12387 objc_maybe_printable_name (tree decl, int v ATTRIBUTE_UNUSED)
12389 switch (TREE_CODE (decl))
12391 case FUNCTION_DECL:
12392 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)));
12395 /* The following happens when we are printing a deprecation
12396 warning for a method. The warn_deprecation() will end up
12397 trying to print the decl for INSTANCE_METHOD_DECL or
12398 CLASS_METHOD_DECL. It would be nice to be able to print
12399 "-[NSObject autorelease] is deprecated", but to do that, we'd
12400 need to store the class and method name in the method decl,
12401 which we currently don't do. For now, just return the name
12402 of the method. We don't return NULL, because that may
12403 trigger further attempts to pretty-print the decl in C/C++,
12404 but they wouldn't know how to pretty-print it. */
12405 case INSTANCE_METHOD_DECL:
12406 case CLASS_METHOD_DECL:
12407 return IDENTIFIER_POINTER (DECL_NAME (decl));
12409 /* This happens when printing a deprecation warning for a
12410 property. We may want to consider some sort of pretty
12411 printing (eg, include the class name where it was declared
12413 case PROPERTY_DECL:
12414 return IDENTIFIER_POINTER (PROPERTY_NAME (decl));
12422 /* Return a printable name for 'decl'. This first tries
12423 objc_maybe_printable_name(), and if that fails, it returns the name
12424 in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for
12425 Objective-C; in Objective-C++, setting the hook is not enough
12426 because lots of C++ Front-End code calls cxx_printable_name,
12427 dump_decl and other C++ functions directly. So instead we have
12428 modified dump_decl to call objc_maybe_printable_name directly. */
12430 objc_printable_name (tree decl, int v)
12432 const char *demangled_name = objc_maybe_printable_name (decl, v);
12434 if (demangled_name != NULL)
12435 return demangled_name;
12437 return IDENTIFIER_POINTER (DECL_NAME (decl));
12443 gcc_obstack_init (&util_obstack);
12444 util_firstobj = (char *) obstack_finish (&util_obstack);
12446 errbuf = XNEWVEC (char, 1024 * 10);
12448 synth_module_prologue ();
12454 struct imp_entry *impent;
12456 /* The internally generated initializers appear to have missing braces.
12457 Don't warn about this. */
12458 int save_warn_missing_braces = warn_missing_braces;
12459 warn_missing_braces = 0;
12461 /* A missing @end may not be detected by the parser. */
12462 if (objc_implementation_context)
12464 warning (0, "%<@end%> missing in implementation context");
12465 finish_class (objc_implementation_context);
12466 objc_ivar_chain = NULL_TREE;
12467 objc_implementation_context = NULL_TREE;
12470 /* Process the static instances here because initialization of objc_symtab
12471 depends on them. */
12472 if (objc_static_instances)
12473 generate_static_references ();
12475 /* forward declare categories */
12477 forward_declare_categories ();
12479 for (impent = imp_list; impent; impent = impent->next)
12481 objc_implementation_context = impent->imp_context;
12482 implementation_template = impent->imp_template;
12484 /* FIXME: This needs reworking to be more obvious. */
12486 UOBJC_CLASS_decl = impent->class_decl;
12487 UOBJC_METACLASS_decl = impent->meta_decl;
12489 /* Dump the @interface of each class as we compile it, if the
12490 -gen-decls option is in use. TODO: Dump the classes in the
12491 order they were found, rather than in reverse order as we
12493 if (flag_gen_declaration)
12495 dump_interface (gen_declaration_file, objc_implementation_context);
12498 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
12500 /* all of the following reference the string pool... */
12501 generate_ivar_lists ();
12502 generate_dispatch_tables ();
12503 generate_shared_structures (impent);
12507 generate_dispatch_tables ();
12508 generate_category (impent);
12511 impent->class_decl = UOBJC_CLASS_decl;
12512 impent->meta_decl = UOBJC_METACLASS_decl;
12515 /* If we are using an array of selectors, we must always
12516 finish up the array decl even if no selectors were used. */
12517 if (flag_next_runtime)
12518 build_next_selector_translation_table ();
12520 build_gnu_selector_translation_table ();
12522 if (protocol_chain)
12523 generate_protocols ();
12525 if (flag_next_runtime)
12526 generate_objc_image_info ();
12528 if (imp_list || class_names_chain
12529 || meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
12530 generate_objc_symtab_decl ();
12532 /* Arrange for ObjC data structures to be initialized at run time. */
12533 if (objc_implementation_context || class_names_chain || objc_static_instances
12534 || meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
12536 build_module_descriptor ();
12538 if (!flag_next_runtime)
12539 build_module_initializer_routine ();
12542 /* Dump the class references. This forces the appropriate classes
12543 to be linked into the executable image, preserving unix archive
12544 semantics. This can be removed when we move to a more dynamically
12545 linked environment. */
12547 for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
12549 handle_class_ref (chain);
12550 if (TREE_PURPOSE (chain))
12551 generate_classref_translation_entry (chain);
12554 for (impent = imp_list; impent; impent = impent->next)
12555 handle_impent (impent);
12562 /* Run through the selector hash tables and print a warning for any
12563 selector which has multiple methods. */
12565 for (slot = 0; slot < SIZEHASHTABLE; slot++)
12567 for (hsh = cls_method_hash_list[slot]; hsh; hsh = hsh->next)
12568 check_duplicates (hsh, 0, 1);
12569 for (hsh = nst_method_hash_list[slot]; hsh; hsh = hsh->next)
12570 check_duplicates (hsh, 0, 1);
12574 warn_missing_braces = save_warn_missing_braces;
12577 /* Subroutines of finish_objc. */
12580 generate_classref_translation_entry (tree chain)
12582 tree expr, decl, type;
12584 decl = TREE_PURPOSE (chain);
12585 type = TREE_TYPE (decl);
12587 expr = add_objc_string (TREE_VALUE (chain), class_names);
12588 expr = convert (type, expr); /* cast! */
12590 /* This is a class reference. It is re-written by the runtime,
12591 but will be optimized away unless we force it. */
12592 DECL_PRESERVE_P (decl) = 1;
12593 finish_var_decl (decl, expr);
12598 handle_class_ref (tree chain)
12600 const char *name = IDENTIFIER_POINTER (TREE_VALUE (chain));
12601 char *string = (char *) alloca (strlen (name) + 30);
12605 sprintf (string, "%sobjc_class_name_%s",
12606 (flag_next_runtime ? "." : "__"), name);
12608 #ifdef ASM_DECLARE_UNRESOLVED_REFERENCE
12609 if (flag_next_runtime)
12611 ASM_DECLARE_UNRESOLVED_REFERENCE (asm_out_file, string);
12616 /* Make a decl for this name, so we can use its address in a tree. */
12617 decl = build_decl (input_location,
12618 VAR_DECL, get_identifier (string), TREE_TYPE (integer_zero_node));
12619 DECL_EXTERNAL (decl) = 1;
12620 TREE_PUBLIC (decl) = 1;
12622 finish_var_decl (decl, 0);
12624 /* Make a decl for the address. */
12625 sprintf (string, "%sobjc_class_ref_%s",
12626 (flag_next_runtime ? "." : "__"), name);
12627 exp = build1 (ADDR_EXPR, string_type_node, decl);
12628 decl = build_decl (input_location,
12629 VAR_DECL, get_identifier (string), string_type_node);
12630 TREE_STATIC (decl) = 1;
12631 TREE_USED (decl) = 1;
12632 DECL_READ_P (decl) = 1;
12633 DECL_ARTIFICIAL (decl) = 1;
12634 DECL_INITIAL (decl) = error_mark_node;
12636 /* We must force the reference. */
12637 DECL_PRESERVE_P (decl) = 1;
12640 finish_var_decl (decl, exp);
12644 handle_impent (struct imp_entry *impent)
12648 objc_implementation_context = impent->imp_context;
12649 implementation_template = impent->imp_template;
12651 switch (TREE_CODE (impent->imp_context))
12653 case CLASS_IMPLEMENTATION_TYPE:
12655 const char *const class_name =
12656 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
12658 string = (char *) alloca (strlen (class_name) + 30);
12660 sprintf (string, "%sobjc_class_name_%s",
12661 (flag_next_runtime ? "." : "__"), class_name);
12664 case CATEGORY_IMPLEMENTATION_TYPE:
12666 const char *const class_name =
12667 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
12668 const char *const class_super_name =
12669 IDENTIFIER_POINTER (CLASS_SUPER_NAME (impent->imp_context));
12671 string = (char *) alloca (strlen (class_name)
12672 + strlen (class_super_name) + 30);
12674 /* Do the same for categories. Even though no references to
12675 these symbols are generated automatically by the compiler,
12676 it gives you a handle to pull them into an archive by
12678 sprintf (string, "*%sobjc_category_name_%s_%s",
12679 (flag_next_runtime ? "." : "__"), class_name, class_super_name);
12686 #ifdef ASM_DECLARE_CLASS_REFERENCE
12687 if (flag_next_runtime)
12689 ASM_DECLARE_CLASS_REFERENCE (asm_out_file, string);
12697 init = integer_zero_node;
12698 decl = build_decl (input_location,
12699 VAR_DECL, get_identifier (string), TREE_TYPE (init));
12700 TREE_PUBLIC (decl) = 1;
12701 TREE_READONLY (decl) = 1;
12702 TREE_USED (decl) = 1;
12703 TREE_CONSTANT (decl) = 1;
12704 DECL_CONTEXT (decl) = NULL_TREE;
12705 DECL_ARTIFICIAL (decl) = 1;
12706 TREE_STATIC (decl) = 1;
12707 DECL_INITIAL (decl) = error_mark_node; /* A real initializer is coming... */
12708 /* We must force the reference. */
12709 DECL_PRESERVE_P (decl) = 1;
12711 finish_var_decl(decl, init) ;
12715 /* The Fix-and-Continue functionality available in Mac OS X 10.3 and
12716 later requires that ObjC translation units participating in F&C be
12717 specially marked. The following routine accomplishes this. */
12719 /* static int _OBJC_IMAGE_INFO[2] = { 0, 1 }; */
12722 generate_objc_image_info (void)
12726 = ((flag_replace_objc_classes && imp_count ? 1 : 0)
12727 | (flag_objc_gc ? 2 : 0));
12728 VEC(constructor_elt,gc) *v = NULL;
12732 return; /* No need for an image_info entry. */
12734 array_type = build_sized_array_type (integer_type_node, 2);
12736 decl = start_var_decl (array_type, "_OBJC_IMAGE_INFO");
12738 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
12739 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (integer_type_node, flags));
12740 /* If we need this (determined above) it is because the runtime wants to
12741 refer to it in a manner hidden from the compiler. So we must force the
12743 DECL_PRESERVE_P (decl) = 1;
12744 finish_var_decl (decl, objc_build_constructor (TREE_TYPE (decl), v));
12747 /* Routine is called to issue diagnostic when reference to a private
12748 ivar is made and no other variable with same name is found in
12751 objc_diagnose_private_ivar (tree id)
12754 if (!objc_method_context)
12756 ivar = is_ivar (objc_ivar_chain, id);
12757 if (ivar && is_private (ivar))
12759 error ("instance variable %qs is declared private",
12760 IDENTIFIER_POINTER (id));
12766 /* Look up ID as an instance variable. OTHER contains the result of
12767 the C or C++ lookup, which we may want to use instead. */
12768 /* To use properties inside an instance method, use self.property. */
12770 objc_lookup_ivar (tree other, tree id)
12774 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
12775 if (!objc_method_context)
12778 if (!strcmp (IDENTIFIER_POINTER (id), "super"))
12779 /* We have a message to super. */
12780 return get_super_receiver ();
12782 /* In a class method, look up an instance variable only as a last
12784 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
12785 && other && other != error_mark_node)
12788 /* Look up the ivar, but do not use it if it is not accessible. */
12789 ivar = is_ivar (objc_ivar_chain, id);
12791 if (!ivar || is_private (ivar))
12794 /* In an instance method, a local variable (or parameter) may hide the
12795 instance variable. */
12796 if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
12797 && other && other != error_mark_node
12799 && CP_DECL_CONTEXT (other) != global_namespace)
12801 && !DECL_FILE_SCOPE_P (other))
12804 warning (0, "local declaration of %qE hides instance variable", id);
12809 /* At this point, we are either in an instance method with no obscuring
12810 local definitions, or in a class method with no alternate definitions
12812 return build_ivar_reference (id);
12815 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
12816 needs to be done if we are calling a function through a cast. */
12819 objc_rewrite_function_call (tree function, tree first_param)
12821 if (TREE_CODE (function) == NOP_EXPR
12822 && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR
12823 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0))
12826 function = build3 (OBJ_TYPE_REF, TREE_TYPE (function),
12827 TREE_OPERAND (function, 0),
12828 first_param, size_zero_node);
12834 /* This is called to "gimplify" a PROPERTY_REF node. It builds the
12835 corresponding 'getter' function call. Note that we assume the
12836 PROPERTY_REF to be valid since we generated it while parsing. */
12838 objc_gimplify_property_ref (tree *expr_p)
12840 tree getter = PROPERTY_REF_GETTER_CALL (*expr_p);
12843 if (getter == NULL_TREE)
12845 tree property_decl = PROPERTY_REF_PROPERTY_DECL (*expr_p);
12846 /* This can happen if DECL_ARTIFICIAL (*expr_p), but
12847 should be impossible for real properties, which always
12849 error_at (EXPR_LOCATION (*expr_p), "no %qs getter found",
12850 IDENTIFIER_POINTER (PROPERTY_NAME (property_decl)));
12851 /* Try to recover from the error to prevent an ICE. We take
12852 zero and cast it to the type of the property. */
12853 *expr_p = convert (TREE_TYPE (property_decl),
12854 integer_zero_node);
12860 /* In C++, a getter which returns an aggregate value results in a
12861 target_expr which initializes a temporary to the call
12863 if (TREE_CODE (getter) == TARGET_EXPR)
12865 gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter)));
12866 gcc_assert (TREE_CODE (TREE_OPERAND (getter, 0)) == VAR_DECL);
12867 call_exp = TREE_OPERAND (getter, 1);
12870 gcc_assert (TREE_CODE (call_exp) == CALL_EXPR);
12872 *expr_p = call_exp;
12875 /* This is called when "gimplifying" the trees. We need to gimplify
12876 the Objective-C/Objective-C++ specific trees, then hand over the
12877 process to C/C++. */
12879 objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
12881 enum tree_code code = TREE_CODE (*expr_p);
12884 /* Look for the special case of OBJC_TYPE_REF with the address
12885 of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend
12886 or one of its cousins). */
12888 if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR
12889 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0))
12892 enum gimplify_status r0, r1;
12894 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
12895 value of the OBJ_TYPE_REF, so force them to be emitted
12896 during subexpression evaluation rather than after the
12897 OBJ_TYPE_REF. This permits objc_msgSend calls in
12898 Objective C to use direct rather than indirect calls when
12899 the object expression has a postincrement. */
12900 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL,
12901 is_gimple_val, fb_rvalue);
12902 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
12903 is_gimple_val, fb_rvalue);
12905 return MIN (r0, r1);
12909 objc_gimplify_property_ref (expr_p);
12910 /* Do not return yet; let C/C++ gimplify the resulting expression. */
12917 return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p);
12919 return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
12923 /* This routine returns true if TYPE is a valid objc object type,
12924 suitable for messaging; false otherwise. If 'accept_class' is
12925 'true', then a Class object is considered valid for messaging and
12926 'true' is returned if 'type' refers to a Class. If 'accept_class'
12927 is 'false', then a Class object is not considered valid for
12928 messaging and 'false' is returned in that case. */
12931 objc_type_valid_for_messaging (tree type, bool accept_classes)
12933 if (!POINTER_TYPE_P (type))
12936 /* Remove the pointer indirection; don't remove more than one
12937 otherwise we'd consider "NSObject **" a valid type for messaging,
12939 type = TREE_TYPE (type);
12941 if (TREE_CODE (type) != RECORD_TYPE)
12944 if (objc_is_object_id (type))
12947 if (objc_is_class_id (type))
12948 return accept_classes;
12950 if (TYPE_HAS_OBJC_INFO (type))
12956 /* Begin code generation for fast enumeration (foreach) ... */
12960 struct __objcFastEnumerationState
12962 unsigned long state;
12964 unsigned long *mutationsPtr;
12965 unsigned long extra[5];
12968 Confusingly enough, NSFastEnumeration is then defined by libraries
12969 to be the same structure.
12973 build_fast_enumeration_state_template (void)
12975 tree decls, *chain = NULL;
12978 objc_fast_enumeration_state_template = objc_start_struct (get_identifier
12979 (TAG_FAST_ENUMERATION_STATE));
12981 /* unsigned long state; */
12982 decls = add_field_decl (long_unsigned_type_node, "state", &chain);
12984 /* id *itemsPtr; */
12985 add_field_decl (build_pointer_type (objc_object_type),
12986 "itemsPtr", &chain);
12988 /* unsigned long *mutationsPtr; */
12989 add_field_decl (build_pointer_type (long_unsigned_type_node),
12990 "mutationsPtr", &chain);
12992 /* unsigned long extra[5]; */
12993 add_field_decl (build_sized_array_type (long_unsigned_type_node, 5),
12997 objc_finish_struct (objc_fast_enumeration_state_template, decls);
13001 'objc_finish_foreach_loop()' generates the code for an Objective-C
13002 foreach loop. The 'location' argument is the location of the 'for'
13003 that starts the loop. The 'object_expression' is the expression of
13004 the 'object' that iterates; the 'collection_expression' is the
13005 expression of the collection that we iterate over (we need to make
13006 sure we evaluate this only once); the 'for_body' is the set of
13007 statements to be executed in each iteration; 'break_label' and
13008 'continue_label' are the break and continue labels which we need to
13009 emit since the <statements> may be jumping to 'break_label' (if they
13010 contain 'break') or to 'continue_label' (if they contain
13015 for (<object expression> in <collection expression>)
13018 which is compiled into the following blurb:
13021 id __objc_foreach_collection;
13022 __objc_fast_enumeration_state __objc_foreach_enum_state;
13023 unsigned long __objc_foreach_batchsize;
13024 id __objc_foreach_items[16];
13025 __objc_foreach_collection = <collection expression>;
13026 __objc_foreach_enum_state = { 0 };
13027 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
13029 if (__objc_foreach_batchsize == 0)
13030 <object expression> = nil;
13033 unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr;
13036 unsigned long __objc_foreach_index;
13037 __objc_foreach_index = 0;
13040 if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>);
13041 <object expression> = enumState.itemsPtr[__objc_foreach_index];
13042 <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label]
13045 __objc_foreach_index++;
13046 if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object;
13047 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
13049 if (__objc_foreach_batchsize != 0) goto next_batch;
13050 <object expression> = nil;
13055 'statements' may contain a 'continue' or 'break' instruction, which
13056 the user expects to 'continue' or 'break' the entire foreach loop.
13057 We are provided the labels that 'break' and 'continue' jump to, so
13058 we place them where we want them to jump to when they pick them.
13060 Optimization TODO: we could cache the IMP of
13061 countByEnumeratingWithState:objects:count:.
13064 /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */
13065 /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */
13067 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
13068 #include "tree-pretty-print.h"
13072 objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body,
13073 tree break_label, tree continue_label)
13075 /* A tree representing the __objcFastEnumerationState struct type,
13076 or NSFastEnumerationState struct, whatever we are using. */
13077 tree objc_fast_enumeration_state_type;
13079 /* The trees representing the declarations of each of the local variables. */
13080 tree objc_foreach_collection_decl;
13081 tree objc_foreach_enum_state_decl;
13082 tree objc_foreach_items_decl;
13083 tree objc_foreach_batchsize_decl;
13084 tree objc_foreach_mutations_pointer_decl;
13085 tree objc_foreach_index_decl;
13087 /* A tree representing the selector countByEnumeratingWithState:objects:count:. */
13088 tree selector_name;
13090 /* A tree representing the local bind. */
13093 /* A tree representing the external 'if (__objc_foreach_batchsize)' */
13096 /* A tree representing the 'else' part of 'first_if' */
13099 /* A tree representing the 'next_batch' label. */
13100 tree next_batch_label_decl;
13102 /* A tree representing the binding after the 'next_batch' label. */
13103 tree next_batch_bind;
13105 /* A tree representing the 'next_object' label. */
13106 tree next_object_label_decl;
13108 /* Temporary variables. */
13112 if (flag_objc1_only)
13113 error_at (location, "fast enumeration is not available in Objective-C 1.0");
13115 if (object_expression == error_mark_node)
13118 if (collection_expression == error_mark_node)
13121 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression), true))
13123 error ("iterating variable in fast enumeration is not an object");
13127 if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression), true))
13129 error ("collection in fast enumeration is not an object");
13133 /* TODO: Check that object_expression is either a variable
13134 declaration, or an lvalue. */
13136 /* This kludge is an idea from apple. We use the
13137 __objcFastEnumerationState struct implicitly defined by the
13138 compiler, unless a NSFastEnumerationState struct has been defined
13139 (by a Foundation library such as GNUstep Base) in which case, we
13142 objc_fast_enumeration_state_type = objc_fast_enumeration_state_template;
13144 tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState"));
13146 if (objc_NSFastEnumeration_type)
13148 /* TODO: We really need to check that
13149 objc_NSFastEnumeration_type is the same as ours! */
13150 if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL)
13152 /* If it's a typedef, use the original type. */
13153 if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type))
13154 objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type);
13156 objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type);
13162 /* Done by c-parser.c. */
13165 /* Done by c-parser.c. */
13167 /* id __objc_foreach_collection */
13168 objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection");
13170 /* __objcFastEnumerationState __objc_foreach_enum_state; */
13171 objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state");
13172 TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl;
13174 /* id __objc_foreach_items[16]; */
13175 objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items");
13176 TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl;
13178 /* unsigned long __objc_foreach_batchsize; */
13179 objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize");
13180 TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl;
13182 /* Generate the local variable binding. */
13183 bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL);
13184 SET_EXPR_LOCATION (bind, location);
13185 TREE_SIDE_EFFECTS (bind) = 1;
13187 /* __objc_foreach_collection = <collection expression>; */
13188 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression);
13189 SET_EXPR_LOCATION (t, location);
13190 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
13192 /* __objc_foreach_enum_state.state = 0; */
13193 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
13194 get_identifier ("state")),
13195 build_int_cst (long_unsigned_type_node, 0));
13196 SET_EXPR_LOCATION (t, location);
13197 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
13199 /* __objc_foreach_enum_state.itemsPtr = NULL; */
13200 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
13201 get_identifier ("itemsPtr")),
13202 null_pointer_node);
13203 SET_EXPR_LOCATION (t, location);
13204 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
13206 /* __objc_foreach_enum_state.mutationsPtr = NULL; */
13207 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
13208 get_identifier ("mutationsPtr")),
13209 null_pointer_node);
13210 SET_EXPR_LOCATION (t, location);
13211 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
13213 /* __objc_foreach_enum_state.extra[0] = 0; */
13214 /* __objc_foreach_enum_state.extra[1] = 0; */
13215 /* __objc_foreach_enum_state.extra[2] = 0; */
13216 /* __objc_foreach_enum_state.extra[3] = 0; */
13217 /* __objc_foreach_enum_state.extra[4] = 0; */
13218 for (i = 0; i < 5 ; i++)
13220 t = build2 (MODIFY_EXPR, void_type_node,
13221 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
13222 get_identifier ("extra")),
13223 build_int_cst (NULL_TREE, i)),
13224 build_int_cst (long_unsigned_type_node, 0));
13225 SET_EXPR_LOCATION (t, location);
13226 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
13229 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
13230 selector_name = get_identifier ("countByEnumeratingWithState:objects:count:");
13232 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
13234 tree_cons /* &__objc_foreach_enum_state */
13235 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
13236 tree_cons /* __objc_foreach_items */
13237 (NULL_TREE, objc_foreach_items_decl,
13239 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
13241 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
13243 struct c_expr array;
13244 array.value = objc_foreach_items_decl;
13245 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
13247 tree_cons /* &__objc_foreach_enum_state */
13248 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
13249 tree_cons /* __objc_foreach_items */
13250 (NULL_TREE, default_function_array_conversion (location, array).value,
13252 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
13255 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
13256 convert (long_unsigned_type_node, t));
13257 SET_EXPR_LOCATION (t, location);
13258 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
13260 /* if (__objc_foreach_batchsize == 0) */
13261 first_if = build3 (COND_EXPR, void_type_node,
13264 (c_common_truthvalue_conversion
13266 build_binary_op (location,
13268 objc_foreach_batchsize_decl,
13269 build_int_cst (long_unsigned_type_node, 0), 1)),
13271 /* Then block (we fill it in later). */
13273 /* Else block (we fill it in later). */
13275 SET_EXPR_LOCATION (first_if, location);
13276 append_to_statement_list (first_if, &BIND_EXPR_BODY (bind));
13278 /* then <object expression> = nil; */
13279 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
13280 SET_EXPR_LOCATION (t, location);
13281 COND_EXPR_THEN (first_if) = t;
13283 /* Now we build the 'else' part of the if; once we finish building
13284 it, we attach it to first_if as the 'else' part. */
13289 /* unsigned long __objc_foreach_mutations_pointer; */
13290 objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer");
13292 /* Generate the local variable binding. */
13293 first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL);
13294 SET_EXPR_LOCATION (first_else, location);
13295 TREE_SIDE_EFFECTS (first_else) = 1;
13297 /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */
13298 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl,
13299 build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
13300 get_identifier ("mutationsPtr")),
13302 SET_EXPR_LOCATION (t, location);
13303 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
13306 next_batch_label_decl = create_artificial_label (location);
13307 t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl);
13308 SET_EXPR_LOCATION (t, location);
13309 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
13313 /* unsigned long __objc_foreach_index; */
13314 objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index");
13316 /* Generate the local variable binding. */
13317 next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL);
13318 SET_EXPR_LOCATION (next_batch_bind, location);
13319 TREE_SIDE_EFFECTS (next_batch_bind) = 1;
13320 append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else));
13322 /* __objc_foreach_index = 0; */
13323 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
13324 build_int_cst (long_unsigned_type_node, 0));
13325 SET_EXPR_LOCATION (t, location);
13326 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13329 next_object_label_decl = create_artificial_label (location);
13330 t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl);
13331 SET_EXPR_LOCATION (t, location);
13332 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13334 /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */
13335 t = build3 (COND_EXPR, void_type_node,
13338 (c_common_truthvalue_conversion
13343 objc_foreach_mutations_pointer_decl,
13344 build_indirect_ref (location,
13345 objc_build_component_ref (objc_foreach_enum_state_decl,
13346 get_identifier ("mutationsPtr")),
13347 RO_UNARY_STAR), 1)),
13350 build_function_call (input_location,
13351 objc_enumeration_mutation_decl,
13352 tree_cons (NULL, collection_expression, NULL)),
13355 SET_EXPR_LOCATION (t, location);
13356 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13358 /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */
13359 t = build2 (MODIFY_EXPR, void_type_node, object_expression,
13360 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
13361 get_identifier ("itemsPtr")),
13362 objc_foreach_index_decl));
13363 SET_EXPR_LOCATION (t, location);
13364 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13366 /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */
13367 append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind));
13369 /* continue_label: */
13370 if (continue_label)
13372 t = build1 (LABEL_EXPR, void_type_node, continue_label);
13373 SET_EXPR_LOCATION (t, location);
13374 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13377 /* __objc_foreach_index++; */
13378 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
13379 build_binary_op (location,
13381 objc_foreach_index_decl,
13382 build_int_cst (long_unsigned_type_node, 1), 1));
13383 SET_EXPR_LOCATION (t, location);
13384 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13386 /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */
13387 t = build3 (COND_EXPR, void_type_node,
13390 (c_common_truthvalue_conversion
13392 build_binary_op (location,
13394 objc_foreach_index_decl,
13395 objc_foreach_batchsize_decl, 1)),
13398 build1 (GOTO_EXPR, void_type_node, next_object_label_decl),
13401 SET_EXPR_LOCATION (t, location);
13402 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13404 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
13406 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
13408 tree_cons /* &__objc_foreach_enum_state */
13409 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
13410 tree_cons /* __objc_foreach_items */
13411 (NULL_TREE, objc_foreach_items_decl,
13413 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
13415 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
13417 struct c_expr array;
13418 array.value = objc_foreach_items_decl;
13419 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
13421 tree_cons /* &__objc_foreach_enum_state */
13422 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
13423 tree_cons /* __objc_foreach_items */
13424 (NULL_TREE, default_function_array_conversion (location, array).value,
13426 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
13429 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
13430 convert (long_unsigned_type_node, t));
13431 SET_EXPR_LOCATION (t, location);
13432 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13436 /* if (__objc_foreach_batchsize != 0) goto next_batch; */
13437 t = build3 (COND_EXPR, void_type_node,
13440 (c_common_truthvalue_conversion
13442 build_binary_op (location,
13444 objc_foreach_batchsize_decl,
13445 build_int_cst (long_unsigned_type_node, 0), 1)),
13448 build1 (GOTO_EXPR, void_type_node, next_batch_label_decl),
13451 SET_EXPR_LOCATION (t, location);
13452 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
13454 /* <object expression> = nil; */
13455 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
13456 SET_EXPR_LOCATION (t, location);
13457 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
13462 t = build1 (LABEL_EXPR, void_type_node, break_label);
13463 SET_EXPR_LOCATION (t, location);
13464 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
13468 COND_EXPR_ELSE (first_if) = first_else;
13470 /* Do the whole thing. */
13473 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
13474 /* This will print to stderr the whole blurb generated by the
13475 compiler while compiling (assuming the compiler doesn't crash
13476 before getting here).
13478 debug_generic_stmt (bind);
13482 /* Done by c-parser.c */
13485 /* Return true if we have an NxString object pointer. */
13488 objc_string_ref_type_p (tree strp)
13491 if (!strp || TREE_CODE (strp) != POINTER_TYPE)
13494 tmv = TYPE_MAIN_VARIANT (TREE_TYPE (strp));
13495 tmv = OBJC_TYPE_NAME (tmv);
13497 && TREE_CODE (tmv) == IDENTIFIER_NODE
13498 && IDENTIFIER_POINTER (tmv)
13499 && !strncmp (IDENTIFIER_POINTER (tmv), "NSString", 8));
13502 /* At present the behavior of this is undefined and it does nothing. */
13504 objc_check_format_arg (tree ARG_UNUSED (format_arg),
13505 tree ARG_UNUSED (args_list))
13509 #include "gt-objc-objc-act.h"